org.el 823 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680216812168221683216842168521686216872168821689216902169121692216932169421695216962169721698216992170021701217022170321704217052170621707217082170921710217112171221713217142171521716217172171821719217202172121722217232172421725217262172721728217292173021731217322173321734217352173621737217382173921740217412174221743217442174521746217472174821749217502175121752217532175421755217562175721758217592176021761217622176321764217652176621767217682176921770217712177221773217742177521776217772177821779217802178121782217832178421785217862178721788217892179021791217922179321794217952179621797217982179921800218012180221803218042180521806218072180821809218102181121812218132181421815218162181721818218192182021821218222182321824218252182621827218282182921830218312183221833218342183521836218372183821839218402184121842218432184421845218462184721848218492185021851218522185321854218552185621857218582185921860218612186221863218642186521866218672186821869218702187121872
  1. ;;; org.el --- Outline-based notes management and organizer
  2. ;; Carstens outline-mode for keeping track of everything.
  3. ;; Copyright (C) 2004-2012 Free Software Foundation, Inc.
  4. ;;
  5. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  6. ;; Maintainer: Bastien Guerry <bzg at gnu dot org>
  7. ;; Keywords: outlines, hypermedia, calendar, wp
  8. ;; Homepage: http://orgmode.org
  9. ;;
  10. ;; This file is part of GNU Emacs.
  11. ;;
  12. ;; GNU Emacs is free software: you can redistribute it and/or modify
  13. ;; it under the terms of the GNU General Public License as published by
  14. ;; the Free Software Foundation, either version 3 of the License, or
  15. ;; (at your option) any later version.
  16. ;; GNU Emacs is distributed in the hope that it will be useful,
  17. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. ;; GNU General Public License for more details.
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  22. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  23. ;;
  24. ;;; Commentary:
  25. ;;
  26. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  27. ;; project planning with a fast and effective plain-text system.
  28. ;;
  29. ;; Org-mode develops organizational tasks around NOTES files that contain
  30. ;; information about projects as plain text. Org-mode is implemented on
  31. ;; top of outline-mode, which makes it possible to keep the content of
  32. ;; large files well structured. Visibility cycling and structure editing
  33. ;; help to work with the tree. Tables are easily created with a built-in
  34. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  35. ;; and scheduling. It dynamically compiles entries into an agenda that
  36. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  37. ;; Plain text URL-like links connect to websites, emails, Usenet
  38. ;; messages, BBDB entries, and any files related to the projects. For
  39. ;; printing and sharing of notes, an Org-mode file can be exported as a
  40. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  41. ;; iCalendar file. It can also serve as a publishing tool for a set of
  42. ;; linked webpages.
  43. ;;
  44. ;; Installation and Activation
  45. ;; ---------------------------
  46. ;; See the corresponding sections in the manual at
  47. ;;
  48. ;; http://orgmode.org/org.html#Installation
  49. ;;
  50. ;; Documentation
  51. ;; -------------
  52. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  53. ;; distribution also contains a PDF version of it. At the homepage of
  54. ;; Org-mode, you can read the same text online as HTML. There is also an
  55. ;; excellent reference card made by Philip Rooke. This card can be found
  56. ;; in the etc/ directory of Emacs 22.
  57. ;;
  58. ;; A list of recent changes can be found at
  59. ;; http://orgmode.org/Changes.html
  60. ;;
  61. ;;; Code:
  62. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  63. (defvar org-table-formula-constants-local nil
  64. "Local version of `org-table-formula-constants'.")
  65. (make-variable-buffer-local 'org-table-formula-constants-local)
  66. ;;;; Require other packages
  67. (eval-when-compile
  68. (require 'cl)
  69. (require 'gnus-sum))
  70. (require 'calendar)
  71. (require 'find-func)
  72. (require 'format-spec)
  73. ;; `org-outline-regexp' ought to be a defconst but is let-binding in
  74. ;; some places -- e.g. see the macro org-with-limited-levels.
  75. ;;
  76. ;; In Org buffers, the value of `outline-regexp' is that of
  77. ;; `org-outline-regexp'. The only function still directly relying on
  78. ;; `outline-regexp' is `org-overview' so that `org-cycle' can do its
  79. ;; job when `orgstruct-mode' is active.
  80. (defvar org-outline-regexp "\\*+ "
  81. "Regexp to match Org headlines.")
  82. (defconst org-outline-regexp-bol "^\\*+ "
  83. "Regexp to match Org headlines.
  84. This is similar to `org-outline-regexp' but additionally makes
  85. sure that we are at the beginning of the line.")
  86. (defconst org-heading-regexp "^\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  87. "Matches an headline, putting stars and text into groups.
  88. Stars are put in group 1 and the trimmed body in group 2.")
  89. ;; Emacs 22 calendar compatibility: Make sure the new variables are available
  90. (when (fboundp 'defvaralias)
  91. (unless (boundp 'calendar-view-holidays-initially-flag)
  92. (defvaralias 'calendar-view-holidays-initially-flag
  93. 'view-calendar-holidays-initially))
  94. (unless (boundp 'calendar-view-diary-initially-flag)
  95. (defvaralias 'calendar-view-diary-initially-flag
  96. 'view-diary-entries-initially))
  97. (unless (boundp 'diary-fancy-buffer)
  98. (defvaralias 'diary-fancy-buffer 'fancy-diary-buffer)))
  99. (declare-function org-inlinetask-at-task-p "org-inlinetask" ())
  100. (declare-function org-inlinetask-outline-regexp "org-inlinetask" ())
  101. (declare-function org-inlinetask-toggle-visibility "org-inlinetask" ())
  102. (declare-function org-pop-to-buffer-same-window "org-compat" (&optional buffer-or-name norecord label))
  103. (declare-function org-at-clock-log-p "org-clock" ())
  104. (declare-function org-clock-timestamps-up "org-clock" ())
  105. (declare-function org-clock-timestamps-down "org-clock" ())
  106. ;; load languages based on value of `org-babel-load-languages'
  107. (defvar org-babel-load-languages)
  108. ;;;###autoload
  109. (defun org-babel-do-load-languages (sym value)
  110. "Load the languages defined in `org-babel-load-languages'."
  111. (set-default sym value)
  112. (mapc (lambda (pair)
  113. (let ((active (cdr pair)) (lang (symbol-name (car pair))))
  114. (if active
  115. (progn
  116. (require (intern (concat "ob-" lang))))
  117. (progn
  118. (funcall 'fmakunbound
  119. (intern (concat "org-babel-execute:" lang)))
  120. (funcall 'fmakunbound
  121. (intern (concat "org-babel-expand-body:" lang)))))))
  122. org-babel-load-languages))
  123. (defcustom org-babel-load-languages '((emacs-lisp . t))
  124. "Languages which can be evaluated in Org-mode buffers.
  125. This list can be used to load support for any of the languages
  126. below, note that each language will depend on a different set of
  127. system executables and/or Emacs modes. When a language is
  128. \"loaded\", then code blocks in that language can be evaluated
  129. with `org-babel-execute-src-block' bound by default to C-c
  130. C-c (note the `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can
  131. be set to remove code block evaluation from the C-c C-c
  132. keybinding. By default only Emacs Lisp (which has no
  133. requirements) is loaded."
  134. :group 'org-babel
  135. :set 'org-babel-do-load-languages
  136. :version "24.1"
  137. :type '(alist :tag "Babel Languages"
  138. :key-type
  139. (choice
  140. (const :tag "Awk" awk)
  141. (const :tag "C" C)
  142. (const :tag "R" R)
  143. (const :tag "Asymptote" asymptote)
  144. (const :tag "Calc" calc)
  145. (const :tag "Clojure" clojure)
  146. (const :tag "CSS" css)
  147. (const :tag "Ditaa" ditaa)
  148. (const :tag "Dot" dot)
  149. (const :tag "Emacs Lisp" emacs-lisp)
  150. (const :tag "Fortran" fortran)
  151. (const :tag "Gnuplot" gnuplot)
  152. (const :tag "Haskell" haskell)
  153. (const :tag "IO" io)
  154. (const :tag "Java" java)
  155. (const :tag "Javascript" js)
  156. (const :tag "LaTeX" latex)
  157. (const :tag "Ledger" ledger)
  158. (const :tag "Lilypond" lilypond)
  159. (const :tag "Maxima" maxima)
  160. (const :tag "Matlab" matlab)
  161. (const :tag "Mscgen" mscgen)
  162. (const :tag "Ocaml" ocaml)
  163. (const :tag "Octave" octave)
  164. (const :tag "Org" org)
  165. (const :tag "Perl" perl)
  166. (const :tag "Pico Lisp" picolisp)
  167. (const :tag "PlantUML" plantuml)
  168. (const :tag "Python" python)
  169. (const :tag "Ruby" ruby)
  170. (const :tag "Sass" sass)
  171. (const :tag "Scala" scala)
  172. (const :tag "Scheme" scheme)
  173. (const :tag "Screen" screen)
  174. (const :tag "Shell Script" sh)
  175. (const :tag "Shen" shen)
  176. (const :tag "Sql" sql)
  177. (const :tag "Sqlite" sqlite))
  178. :value-type (boolean :tag "Activate" :value t)))
  179. ;;;; Customization variables
  180. (defcustom org-clone-delete-id nil
  181. "Remove ID property of clones of a subtree.
  182. When non-nil, clones of a subtree don't inherit the ID property.
  183. Otherwise they inherit the ID property with a new unique
  184. identifier."
  185. :type 'boolean
  186. :version "24.1"
  187. :group 'org-id)
  188. ;;; Version
  189. (org-check-version)
  190. ;;;###autoload
  191. (defun org-version (&optional here full message)
  192. "Show the org-mode version in the echo area.
  193. With prefix arg HERE, insert it at point."
  194. (interactive "P")
  195. (let* ((org-dir (ignore-errors (org-find-library-dir "org")))
  196. (org-install-dir (ignore-errors (org-find-library-dir "org-install.el")))
  197. (org-trash (or
  198. (and (fboundp 'org-release) (fboundp 'org-git-version))
  199. (load (concat org-dir "org-version.el")
  200. 'noerror 'nomessage 'nosuffix)))
  201. (org-version (org-release))
  202. (git-version (org-git-version))
  203. (version (format "Org-mode version %s (%s @ %s)"
  204. org-version
  205. git-version
  206. (if org-install-dir
  207. (if (string= org-dir org-install-dir)
  208. org-install-dir
  209. (concat "mixed installation! " org-install-dir " and " org-dir))
  210. "org-install.el can not be found!")))
  211. (_version (if full version org-version)))
  212. (if (org-called-interactively-p 'interactive)
  213. (if here
  214. (insert version)
  215. (message version))
  216. (if message (message _version))
  217. _version)))
  218. ;;; Compatibility constants
  219. ;;; The custom variables
  220. (defgroup org nil
  221. "Outline-based notes management and organizer."
  222. :tag "Org"
  223. :group 'outlines
  224. :group 'calendar)
  225. (defcustom org-mode-hook nil
  226. "Mode hook for Org-mode, run after the mode was turned on."
  227. :group 'org
  228. :type 'hook)
  229. (defcustom org-load-hook nil
  230. "Hook that is run after org.el has been loaded."
  231. :group 'org
  232. :type 'hook)
  233. (defcustom org-log-buffer-setup-hook nil
  234. "Hook that is run after an Org log buffer is created."
  235. :group 'org
  236. :version "24.1"
  237. :type 'hook)
  238. (defvar org-modules) ; defined below
  239. (defvar org-modules-loaded nil
  240. "Have the modules been loaded already?")
  241. (defun org-load-modules-maybe (&optional force)
  242. "Load all extensions listed in `org-modules'."
  243. (when (or force (not org-modules-loaded))
  244. (mapc (lambda (ext)
  245. (condition-case nil (require ext)
  246. (error (message "Problems while trying to load feature `%s'" ext))))
  247. org-modules)
  248. (setq org-modules-loaded t)))
  249. (defun org-set-modules (var value)
  250. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  251. (set var value)
  252. (when (featurep 'org)
  253. (org-load-modules-maybe 'force)))
  254. (when (org-bound-and-true-p org-modules)
  255. (let ((a (member 'org-infojs org-modules)))
  256. (and a (setcar a 'org-jsinfo))))
  257. (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)
  258. "Modules that should always be loaded together with org.el.
  259. If a description starts with <C>, the file is not part of Emacs
  260. and loading it will require that you have downloaded and properly installed
  261. the org-mode distribution.
  262. You can also use this system to load external packages (i.e. neither Org
  263. core modules, nor modules from the CONTRIB directory). Just add symbols
  264. to the end of the list. If the package is called org-xyz.el, then you need
  265. to add the symbol `xyz', and the package must have a call to
  266. (provide 'org-xyz)"
  267. :group 'org
  268. :set 'org-set-modules
  269. :type
  270. '(set :greedy t
  271. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  272. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  273. (const :tag " crypt: Encryption of subtrees" org-crypt)
  274. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  275. (const :tag " docview: Links to doc-view buffers" org-docview)
  276. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  277. (const :tag " id: Global IDs for identifying entries" org-id)
  278. (const :tag " info: Links to Info nodes" org-info)
  279. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  280. (const :tag " habit: Track your consistency with habits" org-habit)
  281. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  282. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  283. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  284. (const :tag " mew Links to Mew folders/messages" org-mew)
  285. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  286. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  287. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  288. (const :tag " special-blocks: Turn blocks into LaTeX envs and HTML divs" org-special-blocks)
  289. (const :tag " vm: Links to VM folders/messages" org-vm)
  290. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  291. (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  292. (const :tag " mouse: Additional mouse support" org-mouse)
  293. (const :tag " TaskJuggler: Export tasks to a TaskJuggler project" org-taskjuggler)
  294. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  295. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  296. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  297. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  298. (const :tag "C collector: Collect properties into tables" org-collector)
  299. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  300. (const :tag "C drill: Flashcards and spaced repetition for Org-mode" org-drill)
  301. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  302. (const :tag "C eshell Support for links to working directories in eshell" org-eshell)
  303. (const :tag "C eval: Include command output as text" org-eval)
  304. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  305. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  306. (const :tag "C exp-bibtex: Export citations using BibTeX" org-exp-bibtex)
  307. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  308. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  309. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  310. (const :tag "C jira: Add a jira:ticket protocol to Org-mode" org-jira)
  311. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  312. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  313. (const :tag "C notmuch: Provide org links to notmuch searches or messages" org-notmuch)
  314. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  315. (const :tag "C mac-link-grabber Grab links and URLs from various Mac applications" org-mac-link-grabber)
  316. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  317. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  318. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  319. (const :tag "C registry: A registry for Org-mode links" org-registry)
  320. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  321. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  322. (const :tag "C secretary: Team management with org-mode" org-secretary)
  323. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  324. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  325. (const :tag "C track: Keep up with Org-mode development" org-track)
  326. (const :tag "C velocity Something like Notational Velocity for Org" org-velocity)
  327. (const :tag "C wikinodes: CamelCase wiki-like links" org-wikinodes)
  328. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  329. (defcustom org-support-shift-select nil
  330. "Non-nil means make shift-cursor commands select text when possible.
  331. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
  332. start selecting a region, or enlarge regions started in this way.
  333. In Org-mode, in special contexts, these same keys are used for
  334. other purposes, important enough to compete with shift selection.
  335. Org tries to balance these needs by supporting `shift-select-mode'
  336. outside these special contexts, under control of this variable.
  337. The default of this variable is nil, to avoid confusing behavior. Shifted
  338. cursor keys will then execute Org commands in the following contexts:
  339. - on a headline, changing TODO state (left/right) and priority (up/down)
  340. - on a time stamp, changing the time
  341. - in a plain list item, changing the bullet type
  342. - in a property definition line, switching between allowed values
  343. - in the BEGIN line of a clock table (changing the time block).
  344. Outside these contexts, the commands will throw an error.
  345. When this variable is t and the cursor is not in a special
  346. context, Org-mode will support shift-selection for making and
  347. enlarging regions. To make this more effective, the bullet
  348. cycling will no longer happen anywhere in an item line, but only
  349. if the cursor is exactly on the bullet.
  350. If you set this variable to the symbol `always', then the keys
  351. will not be special in headlines, property lines, and item lines,
  352. to make shift selection work there as well. If this is what you
  353. want, you can use the following alternative commands: `C-c C-t'
  354. and `C-c ,' to change TODO state and priority, `C-u C-u C-c C-t'
  355. can be used to switch TODO sets, `C-c -' to cycle item bullet
  356. types, and properties can be edited by hand or in column view.
  357. However, when the cursor is on a timestamp, shift-cursor commands
  358. will still edit the time stamp - this is just too good to give up.
  359. XEmacs user should have this variable set to nil, because
  360. `shift-select-mode' is in Emacs 23 or later only."
  361. :group 'org
  362. :type '(choice
  363. (const :tag "Never" nil)
  364. (const :tag "When outside special context" t)
  365. (const :tag "Everywhere except timestamps" always)))
  366. (defcustom org-loop-over-headlines-in-active-region nil
  367. "Shall some commands act upon headlines in the active region?
  368. When set to `t', some commands will be performed in all headlines
  369. within the active region.
  370. When set to `start-level', some commands will be performed in all
  371. headlines within the active region, provided that these headlines
  372. are of the same level than the first one.
  373. When set to a string, those commands will be performed on the
  374. matching headlines within the active region. Such string must be
  375. a tags/property/todo match as it is used in the agenda tags view.
  376. The list of commands is: `org-schedule', `org-deadline',
  377. `org-todo', `org-archive-subtree', `org-archive-set-tag' and
  378. `org-archive-to-archive-sibling'. The archiving commands skip
  379. already archived entries."
  380. :type '(choice (const :tag "Don't loop" nil)
  381. (const :tag "All headlines in active region" t)
  382. (const :tag "In active region, headlines at the same level than the first one" 'start-level)
  383. (string :tag "Tags/Property/Todo matcher"))
  384. :version "24.1"
  385. :group 'org-todo
  386. :group 'org-archive)
  387. (defgroup org-startup nil
  388. "Options concerning startup of Org-mode."
  389. :tag "Org Startup"
  390. :group 'org)
  391. (defcustom org-startup-folded t
  392. "Non-nil means entering Org-mode will switch to OVERVIEW.
  393. This can also be configured on a per-file basis by adding one of
  394. the following lines anywhere in the buffer:
  395. #+STARTUP: fold (or `overview', this is equivalent)
  396. #+STARTUP: nofold (or `showall', this is equivalent)
  397. #+STARTUP: content
  398. #+STARTUP: showeverything"
  399. :group 'org-startup
  400. :type '(choice
  401. (const :tag "nofold: show all" nil)
  402. (const :tag "fold: overview" t)
  403. (const :tag "content: all headlines" content)
  404. (const :tag "show everything, even drawers" showeverything)))
  405. (defcustom org-startup-truncated t
  406. "Non-nil means entering Org-mode will set `truncate-lines'.
  407. This is useful since some lines containing links can be very long and
  408. uninteresting. Also tables look terrible when wrapped."
  409. :group 'org-startup
  410. :type 'boolean)
  411. (defcustom org-startup-indented nil
  412. "Non-nil means turn on `org-indent-mode' on startup.
  413. This can also be configured on a per-file basis by adding one of
  414. the following lines anywhere in the buffer:
  415. #+STARTUP: indent
  416. #+STARTUP: noindent"
  417. :group 'org-structure
  418. :type '(choice
  419. (const :tag "Not" nil)
  420. (const :tag "Globally (slow on startup in large files)" t)))
  421. (defcustom org-use-sub-superscripts t
  422. "Non-nil means interpret \"_\" and \"^\" for export.
  423. When this option is turned on, you can use TeX-like syntax for sub- and
  424. superscripts. Several characters after \"_\" or \"^\" will be
  425. considered as a single item - so grouping with {} is normally not
  426. needed. For example, the following things will be parsed as single
  427. sub- or superscripts.
  428. 10^24 or 10^tau several digits will be considered 1 item.
  429. 10^-12 or 10^-tau a leading sign with digits or a word
  430. x^2-y^3 will be read as x^2 - y^3, because items are
  431. terminated by almost any nonword/nondigit char.
  432. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  433. Still, ambiguity is possible - so when in doubt use {} to enclose the
  434. sub/superscript. If you set this variable to the symbol `{}',
  435. the braces are *required* in order to trigger interpretations as
  436. sub/superscript. This can be helpful in documents that need \"_\"
  437. frequently in plain text.
  438. Not all export backends support this, but HTML does.
  439. This option can also be set with the #+OPTIONS line, e.g. \"^:nil\"."
  440. :group 'org-startup
  441. :group 'org-export-translation
  442. :version "24.1"
  443. :type '(choice
  444. (const :tag "Always interpret" t)
  445. (const :tag "Only with braces" {})
  446. (const :tag "Never interpret" nil)))
  447. (if (fboundp 'defvaralias)
  448. (defvaralias 'org-export-with-sub-superscripts 'org-use-sub-superscripts))
  449. (defcustom org-startup-with-beamer-mode nil
  450. "Non-nil means turn on `org-beamer-mode' on startup.
  451. This can also be configured on a per-file basis by adding one of
  452. the following lines anywhere in the buffer:
  453. #+STARTUP: beamer"
  454. :group 'org-startup
  455. :version "24.1"
  456. :type 'boolean)
  457. (defcustom org-startup-align-all-tables nil
  458. "Non-nil means align all tables when visiting a file.
  459. This is useful when the column width in tables is forced with <N> cookies
  460. in table fields. Such tables will look correct only after the first re-align.
  461. This can also be configured on a per-file basis by adding one of
  462. the following lines anywhere in the buffer:
  463. #+STARTUP: align
  464. #+STARTUP: noalign"
  465. :group 'org-startup
  466. :type 'boolean)
  467. (defcustom org-startup-with-inline-images nil
  468. "Non-nil means show inline images when loading a new Org file.
  469. This can also be configured on a per-file basis by adding one of
  470. the following lines anywhere in the buffer:
  471. #+STARTUP: inlineimages
  472. #+STARTUP: noinlineimages"
  473. :group 'org-startup
  474. :version "24.1"
  475. :type 'boolean)
  476. (defcustom org-insert-mode-line-in-empty-file nil
  477. "Non-nil means insert the first line setting Org-mode in empty files.
  478. When the function `org-mode' is called interactively in an empty file, this
  479. normally means that the file name does not automatically trigger Org-mode.
  480. To ensure that the file will always be in Org-mode in the future, a
  481. line enforcing Org-mode will be inserted into the buffer, if this option
  482. has been set."
  483. :group 'org-startup
  484. :type 'boolean)
  485. (defcustom org-replace-disputed-keys nil
  486. "Non-nil means use alternative key bindings for some keys.
  487. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  488. These keys are also used by other packages like shift-selection-mode'
  489. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  490. If you want to use Org-mode together with one of these other modes,
  491. or more generally if you would like to move some Org-mode commands to
  492. other keys, set this variable and configure the keys with the variable
  493. `org-disputed-keys'.
  494. This option is only relevant at load-time of Org-mode, and must be set
  495. *before* org.el is loaded. Changing it requires a restart of Emacs to
  496. become effective."
  497. :group 'org-startup
  498. :type 'boolean)
  499. (defcustom org-use-extra-keys nil
  500. "Non-nil means use extra key sequence definitions for certain commands.
  501. This happens automatically if you run XEmacs or if `window-system'
  502. is nil. This variable lets you do the same manually. You must
  503. set it before loading org.
  504. Example: on Carbon Emacs 22 running graphically, with an external
  505. keyboard on a Powerbook, the default way of setting M-left might
  506. not work for either Alt or ESC. Setting this variable will make
  507. it work for ESC."
  508. :group 'org-startup
  509. :type 'boolean)
  510. (if (fboundp 'defvaralias)
  511. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  512. (defcustom org-disputed-keys
  513. '(([(shift up)] . [(meta p)])
  514. ([(shift down)] . [(meta n)])
  515. ([(shift left)] . [(meta -)])
  516. ([(shift right)] . [(meta +)])
  517. ([(control shift right)] . [(meta shift +)])
  518. ([(control shift left)] . [(meta shift -)]))
  519. "Keys for which Org-mode and other modes compete.
  520. This is an alist, cars are the default keys, second element specifies
  521. the alternative to use when `org-replace-disputed-keys' is t.
  522. Keys can be specified in any syntax supported by `define-key'.
  523. The value of this option takes effect only at Org-mode's startup,
  524. therefore you'll have to restart Emacs to apply it after changing."
  525. :group 'org-startup
  526. :type 'alist)
  527. (defun org-key (key)
  528. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  529. Or return the original if not disputed.
  530. Also apply the translations defined in `org-xemacs-key-equivalents'."
  531. (when org-replace-disputed-keys
  532. (let* ((nkey (key-description key))
  533. (x (org-find-if (lambda (x)
  534. (equal (key-description (car x)) nkey))
  535. org-disputed-keys)))
  536. (setq key (if x (cdr x) key))))
  537. (when (featurep 'xemacs)
  538. (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
  539. key)
  540. (defun org-find-if (predicate seq)
  541. (catch 'exit
  542. (while seq
  543. (if (funcall predicate (car seq))
  544. (throw 'exit (car seq))
  545. (pop seq)))))
  546. (defun org-defkey (keymap key def)
  547. "Define a key, possibly translated, as returned by `org-key'."
  548. (define-key keymap (org-key key) def))
  549. (defcustom org-ellipsis nil
  550. "The ellipsis to use in the Org-mode outline.
  551. When nil, just use the standard three dots. When a string, use that instead,
  552. When a face, use the standard 3 dots, but with the specified face.
  553. The change affects only Org-mode (which will then use its own display table).
  554. Changing this requires executing `M-x org-mode' in a buffer to become
  555. effective."
  556. :group 'org-startup
  557. :type '(choice (const :tag "Default" nil)
  558. (face :tag "Face" :value org-warning)
  559. (string :tag "String" :value "...#")))
  560. (defvar org-display-table nil
  561. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  562. (defgroup org-keywords nil
  563. "Keywords in Org-mode."
  564. :tag "Org Keywords"
  565. :group 'org)
  566. (defcustom org-deadline-string "DEADLINE:"
  567. "String to mark deadline entries.
  568. A deadline is this string, followed by a time stamp. Should be a word,
  569. terminated by a colon. You can insert a schedule keyword and
  570. a timestamp with \\[org-deadline].
  571. Changes become only effective after restarting Emacs."
  572. :group 'org-keywords
  573. :type 'string)
  574. (defcustom org-scheduled-string "SCHEDULED:"
  575. "String to mark scheduled TODO entries.
  576. A schedule is this string, followed by a time stamp. Should be a word,
  577. terminated by a colon. You can insert a schedule keyword and
  578. a timestamp with \\[org-schedule].
  579. Changes become only effective after restarting Emacs."
  580. :group 'org-keywords
  581. :type 'string)
  582. (defcustom org-closed-string "CLOSED:"
  583. "String used as the prefix for timestamps logging closing a TODO entry."
  584. :group 'org-keywords
  585. :type 'string)
  586. (defcustom org-clock-string "CLOCK:"
  587. "String used as prefix for timestamps clocking work hours on an item."
  588. :group 'org-keywords
  589. :type 'string)
  590. (defconst org-planning-or-clock-line-re (concat "^[ \t]*\\("
  591. org-scheduled-string "\\|"
  592. org-deadline-string "\\|"
  593. org-closed-string "\\|"
  594. org-clock-string "\\)")
  595. "Matches a line with planning or clock info.")
  596. (defcustom org-comment-string "COMMENT"
  597. "Entries starting with this keyword will never be exported.
  598. An entry can be toggled between COMMENT and normal with
  599. \\[org-toggle-comment].
  600. Changes become only effective after restarting Emacs."
  601. :group 'org-keywords
  602. :type 'string)
  603. (defcustom org-quote-string "QUOTE"
  604. "Entries starting with this keyword will be exported in fixed-width font.
  605. Quoting applies only to the text in the entry following the headline, and does
  606. not extend beyond the next headline, even if that is lower level.
  607. An entry can be toggled between QUOTE and normal with
  608. \\[org-toggle-fixed-width-section]."
  609. :group 'org-keywords
  610. :type 'string)
  611. (defconst org-repeat-re
  612. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)"
  613. "Regular expression for specifying repeated events.
  614. After a match, group 1 contains the repeat expression.")
  615. (defgroup org-structure nil
  616. "Options concerning the general structure of Org-mode files."
  617. :tag "Org Structure"
  618. :group 'org)
  619. (defgroup org-reveal-location nil
  620. "Options about how to make context of a location visible."
  621. :tag "Org Reveal Location"
  622. :group 'org-structure)
  623. (defconst org-context-choice
  624. '(choice
  625. (const :tag "Always" t)
  626. (const :tag "Never" nil)
  627. (repeat :greedy t :tag "Individual contexts"
  628. (cons
  629. (choice :tag "Context"
  630. (const agenda)
  631. (const org-goto)
  632. (const occur-tree)
  633. (const tags-tree)
  634. (const link-search)
  635. (const mark-goto)
  636. (const bookmark-jump)
  637. (const isearch)
  638. (const default))
  639. (boolean))))
  640. "Contexts for the reveal options.")
  641. (defcustom org-show-hierarchy-above '((default . t))
  642. "Non-nil means show full hierarchy when revealing a location.
  643. Org-mode often shows locations in an org-mode file which might have
  644. been invisible before. When this is set, the hierarchy of headings
  645. above the exposed location is shown.
  646. Turning this off for example for sparse trees makes them very compact.
  647. Instead of t, this can also be an alist specifying this option for different
  648. contexts. Valid contexts are
  649. agenda when exposing an entry from the agenda
  650. org-goto when using the command `org-goto' on key C-c C-j
  651. occur-tree when using the command `org-occur' on key C-c /
  652. tags-tree when constructing a sparse tree based on tags matches
  653. link-search when exposing search matches associated with a link
  654. mark-goto when exposing the jump goal of a mark
  655. bookmark-jump when exposing a bookmark location
  656. isearch when exiting from an incremental search
  657. default default for all contexts not set explicitly"
  658. :group 'org-reveal-location
  659. :type org-context-choice)
  660. (defcustom org-show-following-heading '((default . nil))
  661. "Non-nil means show following heading when revealing a location.
  662. Org-mode often shows locations in an org-mode file which might have
  663. been invisible before. When this is set, the heading following the
  664. match is shown.
  665. Turning this off for example for sparse trees makes them very compact,
  666. but makes it harder to edit the location of the match. In such a case,
  667. use the command \\[org-reveal] to show more context.
  668. Instead of t, this can also be an alist specifying this option for different
  669. contexts. See `org-show-hierarchy-above' for valid contexts."
  670. :group 'org-reveal-location
  671. :type org-context-choice)
  672. (defcustom org-show-siblings '((default . nil) (isearch t))
  673. "Non-nil means show all sibling heading when revealing a location.
  674. Org-mode often shows locations in an org-mode file which might have
  675. been invisible before. When this is set, the sibling of the current entry
  676. heading are all made visible. If `org-show-hierarchy-above' is t,
  677. the same happens on each level of the hierarchy above the current entry.
  678. By default this is on for the isearch context, off for all other contexts.
  679. Turning this off for example for sparse trees makes them very compact,
  680. but makes it harder to edit the location of the match. In such a case,
  681. use the command \\[org-reveal] to show more context.
  682. Instead of t, this can also be an alist specifying this option for different
  683. contexts. See `org-show-hierarchy-above' for valid contexts."
  684. :group 'org-reveal-location
  685. :type org-context-choice)
  686. (defcustom org-show-entry-below '((default . nil))
  687. "Non-nil means show the entry below a headline when revealing a location.
  688. Org-mode often shows locations in an org-mode file which might have
  689. been invisible before. When this is set, the text below the headline that is
  690. exposed is also shown.
  691. By default this is off for all contexts.
  692. Instead of t, this can also be an alist specifying this option for different
  693. contexts. See `org-show-hierarchy-above' for valid contexts."
  694. :group 'org-reveal-location
  695. :type org-context-choice)
  696. (defcustom org-indirect-buffer-display 'other-window
  697. "How should indirect tree buffers be displayed?
  698. This applies to indirect buffers created with the commands
  699. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  700. Valid values are:
  701. current-window Display in the current window
  702. other-window Just display in another window.
  703. dedicated-frame Create one new frame, and re-use it each time.
  704. new-frame Make a new frame each time. Note that in this case
  705. previously-made indirect buffers are kept, and you need to
  706. kill these buffers yourself."
  707. :group 'org-structure
  708. :group 'org-agenda-windows
  709. :type '(choice
  710. (const :tag "In current window" current-window)
  711. (const :tag "In current frame, other window" other-window)
  712. (const :tag "Each time a new frame" new-frame)
  713. (const :tag "One dedicated frame" dedicated-frame)))
  714. (defcustom org-use-speed-commands nil
  715. "Non-nil means activate single letter commands at beginning of a headline.
  716. This may also be a function to test for appropriate locations where speed
  717. commands should be active."
  718. :group 'org-structure
  719. :type '(choice
  720. (const :tag "Never" nil)
  721. (const :tag "At beginning of headline stars" t)
  722. (function)))
  723. (defcustom org-speed-commands-user nil
  724. "Alist of additional speed commands.
  725. This list will be checked before `org-speed-commands-default'
  726. when the variable `org-use-speed-commands' is non-nil
  727. and when the cursor is at the beginning of a headline.
  728. The car if each entry is a string with a single letter, which must
  729. be assigned to `self-insert-command' in the global map.
  730. The cdr is either a command to be called interactively, a function
  731. to be called, or a form to be evaluated.
  732. An entry that is just a list with a single string will be interpreted
  733. as a descriptive headline that will be added when listing the speed
  734. commands in the Help buffer using the `?' speed command."
  735. :group 'org-structure
  736. :type '(repeat :value ("k" . ignore)
  737. (choice :value ("k" . ignore)
  738. (list :tag "Descriptive Headline" (string :tag "Headline"))
  739. (cons :tag "Letter and Command"
  740. (string :tag "Command letter")
  741. (choice
  742. (function)
  743. (sexp))))))
  744. (defgroup org-cycle nil
  745. "Options concerning visibility cycling in Org-mode."
  746. :tag "Org Cycle"
  747. :group 'org-structure)
  748. (defcustom org-cycle-skip-children-state-if-no-children t
  749. "Non-nil means skip CHILDREN state in entries that don't have any."
  750. :group 'org-cycle
  751. :type 'boolean)
  752. (defcustom org-cycle-max-level nil
  753. "Maximum level which should still be subject to visibility cycling.
  754. Levels higher than this will, for cycling, be treated as text, not a headline.
  755. When `org-odd-levels-only' is set, a value of N in this variable actually
  756. means 2N-1 stars as the limiting headline.
  757. When nil, cycle all levels.
  758. Note that the limiting level of cycling is also influenced by
  759. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  760. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  761. than its value."
  762. :group 'org-cycle
  763. :type '(choice
  764. (const :tag "No limit" nil)
  765. (integer :tag "Maximum level")))
  766. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK" "RESULTS")
  767. "Names of drawers. Drawers are not opened by cycling on the headline above.
  768. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  769. this:
  770. :DRAWERNAME:
  771. .....
  772. :END:
  773. The drawer \"PROPERTIES\" is special for capturing properties through
  774. the property API.
  775. Drawers can be defined on the per-file basis with a line like:
  776. #+DRAWERS: HIDDEN STATE PROPERTIES"
  777. :group 'org-structure
  778. :group 'org-cycle
  779. :type '(repeat (string :tag "Drawer Name")))
  780. (defcustom org-hide-block-startup nil
  781. "Non-nil means entering Org-mode will fold all blocks.
  782. This can also be set in on a per-file basis with
  783. #+STARTUP: hideblocks
  784. #+STARTUP: showblocks"
  785. :group 'org-startup
  786. :group 'org-cycle
  787. :type 'boolean)
  788. (defcustom org-cycle-global-at-bob nil
  789. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  790. This makes it possible to do global cycling without having to use S-TAB or
  791. \\[universal-argument] TAB. For this special case to work, the first line \
  792. of the buffer
  793. must not be a headline - it may be empty or some other text. When used in
  794. this way, `org-cycle-hook' is disables temporarily, to make sure the
  795. cursor stays at the beginning of the buffer.
  796. When this option is nil, don't do anything special at the beginning
  797. of the buffer."
  798. :group 'org-cycle
  799. :type 'boolean)
  800. (defcustom org-cycle-level-after-item/entry-creation t
  801. "Non-nil means cycle entry level or item indentation in new empty entries.
  802. When the cursor is at the end of an empty headline, i.e with only stars
  803. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  804. and then all possible ancestor states, before returning to the original state.
  805. This makes data entry extremely fast: M-RET to create a new headline,
  806. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  807. When the cursor is at the end of an empty plain list item, one TAB will
  808. make it a subitem, two or more tabs will back up to make this an item
  809. higher up in the item hierarchy."
  810. :group 'org-cycle
  811. :type 'boolean)
  812. (defcustom org-cycle-emulate-tab t
  813. "Where should `org-cycle' emulate TAB.
  814. nil Never
  815. white Only in completely white lines
  816. whitestart Only at the beginning of lines, before the first non-white char
  817. t Everywhere except in headlines
  818. exc-hl-bol Everywhere except at the start of a headline
  819. If TAB is used in a place where it does not emulate TAB, the current subtree
  820. visibility is cycled."
  821. :group 'org-cycle
  822. :type '(choice (const :tag "Never" nil)
  823. (const :tag "Only in completely white lines" white)
  824. (const :tag "Before first char in a line" whitestart)
  825. (const :tag "Everywhere except in headlines" t)
  826. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  827. ))
  828. (defcustom org-cycle-separator-lines 2
  829. "Number of empty lines needed to keep an empty line between collapsed trees.
  830. If you leave an empty line between the end of a subtree and the following
  831. headline, this empty line is hidden when the subtree is folded.
  832. Org-mode will leave (exactly) one empty line visible if the number of
  833. empty lines is equal or larger to the number given in this variable.
  834. So the default 2 means at least 2 empty lines after the end of a subtree
  835. are needed to produce free space between a collapsed subtree and the
  836. following headline.
  837. If the number is negative, and the number of empty lines is at least -N,
  838. all empty lines are shown.
  839. Special case: when 0, never leave empty lines in collapsed view."
  840. :group 'org-cycle
  841. :type 'integer)
  842. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  843. (defcustom org-pre-cycle-hook nil
  844. "Hook that is run before visibility cycling is happening.
  845. The function(s) in this hook must accept a single argument which indicates
  846. the new state that will be set right after running this hook. The
  847. argument is a symbol. Before a global state change, it can have the values
  848. `overview', `content', or `all'. Before a local state change, it can have
  849. the values `folded', `children', or `subtree'."
  850. :group 'org-cycle
  851. :type 'hook)
  852. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  853. org-cycle-hide-drawers
  854. org-cycle-show-empty-lines
  855. org-optimize-window-after-visibility-change)
  856. "Hook that is run after `org-cycle' has changed the buffer visibility.
  857. The function(s) in this hook must accept a single argument which indicates
  858. the new state that was set by the most recent `org-cycle' command. The
  859. argument is a symbol. After a global state change, it can have the values
  860. `overview', `content', or `all'. After a local state change, it can have
  861. the values `folded', `children', or `subtree'."
  862. :group 'org-cycle
  863. :type 'hook)
  864. (defgroup org-edit-structure nil
  865. "Options concerning structure editing in Org-mode."
  866. :tag "Org Edit Structure"
  867. :group 'org-structure)
  868. (defcustom org-odd-levels-only nil
  869. "Non-nil means skip even levels and only use odd levels for the outline.
  870. This has the effect that two stars are being added/taken away in
  871. promotion/demotion commands. It also influences how levels are
  872. handled by the exporters.
  873. Changing it requires restart of `font-lock-mode' to become effective
  874. for fontification also in regions already fontified.
  875. You may also set this on a per-file basis by adding one of the following
  876. lines to the buffer:
  877. #+STARTUP: odd
  878. #+STARTUP: oddeven"
  879. :group 'org-edit-structure
  880. :group 'org-appearance
  881. :type 'boolean)
  882. (defcustom org-adapt-indentation t
  883. "Non-nil means adapt indentation to outline node level.
  884. When this variable is set, Org assumes that you write outlines by
  885. indenting text in each node to align with the headline (after the stars).
  886. The following issues are influenced by this variable:
  887. - When this is set and the *entire* text in an entry is indented, the
  888. indentation is increased by one space in a demotion command, and
  889. decreased by one in a promotion command. If any line in the entry
  890. body starts with text at column 0, indentation is not changed at all.
  891. - Property drawers and planning information is inserted indented when
  892. this variable s set. When nil, they will not be indented.
  893. - TAB indents a line relative to context. The lines below a headline
  894. will be indented when this variable is set.
  895. Note that this is all about true indentation, by adding and removing
  896. space characters. See also `org-indent.el' which does level-dependent
  897. indentation in a virtual way, i.e. at display time in Emacs."
  898. :group 'org-edit-structure
  899. :type 'boolean)
  900. (defcustom org-special-ctrl-a/e nil
  901. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  902. When t, `C-a' will bring back the cursor to the beginning of the
  903. headline text, i.e. after the stars and after a possible TODO
  904. keyword. In an item, this will be the position after bullet and
  905. check-box, if any. When the cursor is already at that position,
  906. another `C-a' will bring it to the beginning of the line.
  907. `C-e' will jump to the end of the headline, ignoring the presence
  908. of tags in the headline. A second `C-e' will then jump to the
  909. true end of the line, after any tags. This also means that, when
  910. this variable is non-nil, `C-e' also will never jump beyond the
  911. end of the heading of a folded section, i.e. not after the
  912. ellipses.
  913. When set to the symbol `reversed', the first `C-a' or `C-e' works
  914. normally, going to the true line boundary first. Only a directly
  915. following, identical keypress will bring the cursor to the
  916. special positions.
  917. This may also be a cons cell where the behavior for `C-a' and
  918. `C-e' is set separately."
  919. :group 'org-edit-structure
  920. :type '(choice
  921. (const :tag "off" nil)
  922. (const :tag "on: after stars/bullet and before tags first" t)
  923. (const :tag "reversed: true line boundary first" reversed)
  924. (cons :tag "Set C-a and C-e separately"
  925. (choice :tag "Special C-a"
  926. (const :tag "off" nil)
  927. (const :tag "on: after stars/bullet first" t)
  928. (const :tag "reversed: before stars/bullet first" reversed))
  929. (choice :tag "Special C-e"
  930. (const :tag "off" nil)
  931. (const :tag "on: before tags first" t)
  932. (const :tag "reversed: after tags first" reversed)))))
  933. (if (fboundp 'defvaralias)
  934. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  935. (defcustom org-special-ctrl-k nil
  936. "Non-nil means `C-k' will behave specially in headlines.
  937. When nil, `C-k' will call the default `kill-line' command.
  938. When t, the following will happen while the cursor is in the headline:
  939. - When the cursor is at the beginning of a headline, kill the entire
  940. line and possible the folded subtree below the line.
  941. - When in the middle of the headline text, kill the headline up to the tags.
  942. - When after the headline text, kill the tags."
  943. :group 'org-edit-structure
  944. :type 'boolean)
  945. (defcustom org-ctrl-k-protect-subtree nil
  946. "Non-nil means, do not delete a hidden subtree with C-k.
  947. When set to the symbol `error', simply throw an error when C-k is
  948. used to kill (part-of) a headline that has hidden text behind it.
  949. Any other non-nil value will result in a query to the user, if it is
  950. OK to kill that hidden subtree. When nil, kill without remorse."
  951. :group 'org-edit-structure
  952. :version "24.1"
  953. :type '(choice
  954. (const :tag "Do not protect hidden subtrees" nil)
  955. (const :tag "Protect hidden subtrees with a security query" t)
  956. (const :tag "Never kill a hidden subtree with C-k" error)))
  957. (defcustom org-catch-invisible-edits nil
  958. "Check if in invisible region before inserting or deleting a character.
  959. Valid values are:
  960. nil Do not check, so just do invisible edits.
  961. error Throw an error and do nothing.
  962. show Make point visible, and do the requested edit.
  963. show-and-error Make point visible, then throw an error and abort the edit.
  964. smart Make point visible, and do insertion/deletion if it is
  965. adjacent to visible text and the change feels predictable.
  966. Never delete a previously invisible character or add in the
  967. middle or right after an invisible region. Basically, this
  968. allows insertion and backward-delete right before ellipses.
  969. FIXME: maybe in this case we should not even show?"
  970. :group 'org-edit-structure
  971. :version "24.1"
  972. :type '(choice
  973. (const :tag "Do not check" nil)
  974. (const :tag "Throw error when trying to edit" error)
  975. (const :tag "Unhide, but do not do the edit" show-and-error)
  976. (const :tag "Show invisible part and do the edit" show)
  977. (const :tag "Be smart and do the right thing" smart)))
  978. (defcustom org-yank-folded-subtrees t
  979. "Non-nil means when yanking subtrees, fold them.
  980. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  981. it starts with a heading and all other headings in it are either children
  982. or siblings, then fold all the subtrees. However, do this only if no
  983. text after the yank would be swallowed into a folded tree by this action."
  984. :group 'org-edit-structure
  985. :type 'boolean)
  986. (defcustom org-yank-adjusted-subtrees nil
  987. "Non-nil means when yanking subtrees, adjust the level.
  988. With this setting, `org-paste-subtree' is used to insert the subtree, see
  989. this function for details."
  990. :group 'org-edit-structure
  991. :type 'boolean)
  992. (defcustom org-M-RET-may-split-line '((default . t))
  993. "Non-nil means M-RET will split the line at the cursor position.
  994. When nil, it will go to the end of the line before making a
  995. new line.
  996. You may also set this option in a different way for different
  997. contexts. Valid contexts are:
  998. headline when creating a new headline
  999. item when creating a new item
  1000. table in a table field
  1001. default the value to be used for all contexts not explicitly
  1002. customized"
  1003. :group 'org-structure
  1004. :group 'org-table
  1005. :type '(choice
  1006. (const :tag "Always" t)
  1007. (const :tag "Never" nil)
  1008. (repeat :greedy t :tag "Individual contexts"
  1009. (cons
  1010. (choice :tag "Context"
  1011. (const headline)
  1012. (const item)
  1013. (const table)
  1014. (const default))
  1015. (boolean)))))
  1016. (defcustom org-insert-heading-respect-content nil
  1017. "Non-nil means insert new headings after the current subtree.
  1018. When nil, the new heading is created directly after the current line.
  1019. The commands \\[org-insert-heading-respect-content] and
  1020. \\[org-insert-todo-heading-respect-content] turn this variable on
  1021. for the duration of the command."
  1022. :group 'org-structure
  1023. :type 'boolean)
  1024. (defcustom org-blank-before-new-entry '((heading . auto)
  1025. (plain-list-item . auto))
  1026. "Should `org-insert-heading' leave a blank line before new heading/item?
  1027. The value is an alist, with `heading' and `plain-list-item' as CAR,
  1028. and a boolean flag as CDR. The cdr may also be the symbol `auto', in
  1029. which case Org will look at the surrounding headings/items and try to
  1030. make an intelligent decision whether to insert a blank line or not.
  1031. For plain lists, if the variable `org-empty-line-terminates-plain-lists' is
  1032. set, the setting here is ignored and no empty line is inserted, to avoid
  1033. breaking the list structure."
  1034. :group 'org-edit-structure
  1035. :type '(list
  1036. (cons (const heading)
  1037. (choice (const :tag "Never" nil)
  1038. (const :tag "Always" t)
  1039. (const :tag "Auto" auto)))
  1040. (cons (const plain-list-item)
  1041. (choice (const :tag "Never" nil)
  1042. (const :tag "Always" t)
  1043. (const :tag "Auto" auto)))))
  1044. (defcustom org-insert-heading-hook nil
  1045. "Hook being run after inserting a new heading."
  1046. :group 'org-edit-structure
  1047. :type 'hook)
  1048. (defcustom org-enable-fixed-width-editor t
  1049. "Non-nil means lines starting with \":\" are treated as fixed-width.
  1050. This currently only means they are never auto-wrapped.
  1051. When nil, such lines will be treated like ordinary lines.
  1052. See also the QUOTE keyword."
  1053. :group 'org-edit-structure
  1054. :type 'boolean)
  1055. (defcustom org-goto-auto-isearch t
  1056. "Non-nil means typing characters in `org-goto' starts incremental search."
  1057. :group 'org-edit-structure
  1058. :type 'boolean)
  1059. (defgroup org-sparse-trees nil
  1060. "Options concerning sparse trees in Org-mode."
  1061. :tag "Org Sparse Trees"
  1062. :group 'org-structure)
  1063. (defcustom org-highlight-sparse-tree-matches t
  1064. "Non-nil means highlight all matches that define a sparse tree.
  1065. The highlights will automatically disappear the next time the buffer is
  1066. changed by an edit command."
  1067. :group 'org-sparse-trees
  1068. :type 'boolean)
  1069. (defcustom org-remove-highlights-with-change t
  1070. "Non-nil means any change to the buffer will remove temporary highlights.
  1071. Such highlights are created by `org-occur' and `org-clock-display'.
  1072. When nil, `C-c C-c needs to be used to get rid of the highlights.
  1073. The highlights created by `org-preview-latex-fragment' always need
  1074. `C-c C-c' to be removed."
  1075. :group 'org-sparse-trees
  1076. :group 'org-time
  1077. :type 'boolean)
  1078. (defcustom org-occur-hook '(org-first-headline-recenter)
  1079. "Hook that is run after `org-occur' has constructed a sparse tree.
  1080. This can be used to recenter the window to show as much of the structure
  1081. as possible."
  1082. :group 'org-sparse-trees
  1083. :type 'hook)
  1084. (defgroup org-imenu-and-speedbar nil
  1085. "Options concerning imenu and speedbar in Org-mode."
  1086. :tag "Org Imenu and Speedbar"
  1087. :group 'org-structure)
  1088. (defcustom org-imenu-depth 2
  1089. "The maximum level for Imenu access to Org-mode headlines.
  1090. This also applied for speedbar access."
  1091. :group 'org-imenu-and-speedbar
  1092. :type 'integer)
  1093. (defgroup org-table nil
  1094. "Options concerning tables in Org-mode."
  1095. :tag "Org Table"
  1096. :group 'org)
  1097. (defcustom org-enable-table-editor 'optimized
  1098. "Non-nil means lines starting with \"|\" are handled by the table editor.
  1099. When nil, such lines will be treated like ordinary lines.
  1100. When equal to the symbol `optimized', the table editor will be optimized to
  1101. do the following:
  1102. - Automatic overwrite mode in front of whitespace in table fields.
  1103. This makes the structure of the table stay in tact as long as the edited
  1104. field does not exceed the column width.
  1105. - Minimize the number of realigns. Normally, the table is aligned each time
  1106. TAB or RET are pressed to move to another field. With optimization this
  1107. happens only if changes to a field might have changed the column width.
  1108. Optimization requires replacing the functions `self-insert-command',
  1109. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  1110. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  1111. very good at guessing when a re-align will be necessary, but you can always
  1112. force one with \\[org-ctrl-c-ctrl-c].
  1113. If you would like to use the optimized version in Org-mode, but the
  1114. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  1115. This variable can be used to turn on and off the table editor during a session,
  1116. but in order to toggle optimization, a restart is required.
  1117. See also the variable `org-table-auto-blank-field'."
  1118. :group 'org-table
  1119. :type '(choice
  1120. (const :tag "off" nil)
  1121. (const :tag "on" t)
  1122. (const :tag "on, optimized" optimized)))
  1123. (defcustom org-self-insert-cluster-for-undo t
  1124. "Non-nil means cluster self-insert commands for undo when possible.
  1125. If this is set, then, like in the Emacs command loop, 20 consecutive
  1126. characters will be undone together.
  1127. This is configurable, because there is some impact on typing performance."
  1128. :group 'org-table
  1129. :type 'boolean)
  1130. (defcustom org-table-tab-recognizes-table.el t
  1131. "Non-nil means TAB will automatically notice a table.el table.
  1132. When it sees such a table, it moves point into it and - if necessary -
  1133. calls `table-recognize-table'."
  1134. :group 'org-table-editing
  1135. :type 'boolean)
  1136. (defgroup org-link nil
  1137. "Options concerning links in Org-mode."
  1138. :tag "Org Link"
  1139. :group 'org)
  1140. (defvar org-link-abbrev-alist-local nil
  1141. "Buffer-local version of `org-link-abbrev-alist', which see.
  1142. The value of this is taken from the #+LINK lines.")
  1143. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  1144. (defcustom org-link-abbrev-alist nil
  1145. "Alist of link abbreviations.
  1146. The car of each element is a string, to be replaced at the start of a link.
  1147. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1148. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  1149. [[linkkey:tag][description]]
  1150. The 'linkkey' must be a word word, starting with a letter, followed
  1151. by letters, numbers, '-' or '_'.
  1152. If REPLACE is a string, the tag will simply be appended to create the link.
  1153. If the string contains \"%s\", the tag will be inserted there. If the string
  1154. contains \"%h\", it will cause a url-encoded version of the tag to be inserted
  1155. at that point (see the function `url-hexify-string'). If the string contains
  1156. the specifier \"%(my-function)\", then the custom function `my-function' will
  1157. be invoked: this function takes the tag as its only argument and must return
  1158. a string.
  1159. REPLACE may also be a function that will be called with the tag as the
  1160. only argument to create the link, which should be returned as a string.
  1161. See the manual for examples."
  1162. :group 'org-link
  1163. :type '(repeat
  1164. (cons
  1165. (string :tag "Protocol")
  1166. (choice
  1167. (string :tag "Format")
  1168. (function)))))
  1169. (defcustom org-descriptive-links t
  1170. "Non-nil means Org will display descriptive links.
  1171. E.g. [[http://orgmode.org][Org website]] will be displayed as
  1172. \"Org Website\", hiding the link itself and just displaying its
  1173. description. When set to `nil', Org will display the full links
  1174. literally.
  1175. You can interactively set the value of this variable by calling
  1176. `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
  1177. :group 'org-link
  1178. :type 'boolean)
  1179. (defcustom org-link-file-path-type 'adaptive
  1180. "How the path name in file links should be stored.
  1181. Valid values are:
  1182. relative Relative to the current directory, i.e. the directory of the file
  1183. into which the link is being inserted.
  1184. absolute Absolute path, if possible with ~ for home directory.
  1185. noabbrev Absolute path, no abbreviation of home directory.
  1186. adaptive Use relative path for files in the current directory and sub-
  1187. directories of it. For other files, use an absolute path."
  1188. :group 'org-link
  1189. :type '(choice
  1190. (const relative)
  1191. (const absolute)
  1192. (const noabbrev)
  1193. (const adaptive)))
  1194. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  1195. "Types of links that should be activated in Org-mode files.
  1196. This is a list of symbols, each leading to the activation of a certain link
  1197. type. In principle, it does not hurt to turn on most link types - there may
  1198. be a small gain when turning off unused link types. The types are:
  1199. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1200. angle Links in angular brackets that may contain whitespace like
  1201. <bbdb:Carsten Dominik>.
  1202. plain Plain links in normal text, no whitespace, like http://google.com.
  1203. radio Text that is matched by a radio target, see manual for details.
  1204. tag Tag settings in a headline (link to tag search).
  1205. date Time stamps (link to calendar).
  1206. footnote Footnote labels.
  1207. Changing this variable requires a restart of Emacs to become effective."
  1208. :group 'org-link
  1209. :type '(set :greedy t
  1210. (const :tag "Double bracket links" bracket)
  1211. (const :tag "Angular bracket links" angle)
  1212. (const :tag "Plain text links" plain)
  1213. (const :tag "Radio target matches" radio)
  1214. (const :tag "Tags" tag)
  1215. (const :tag "Timestamps" date)
  1216. (const :tag "Footnotes" footnote)))
  1217. (defcustom org-make-link-description-function nil
  1218. "Function to use to generate link descriptions from links.
  1219. If nil the link location will be used. This function must take
  1220. two parameters; the first is the link and the second the
  1221. description `org-insert-link' has generated, and should return the
  1222. description to use."
  1223. :group 'org-link
  1224. :type 'function)
  1225. (defgroup org-link-store nil
  1226. "Options concerning storing links in Org-mode."
  1227. :tag "Org Store Link"
  1228. :group 'org-link)
  1229. (defcustom org-email-link-description-format "Email %c: %.30s"
  1230. "Format of the description part of a link to an email or usenet message.
  1231. The following %-escapes will be replaced by corresponding information:
  1232. %F full \"From\" field
  1233. %f name, taken from \"From\" field, address if no name
  1234. %T full \"To\" field
  1235. %t first name in \"To\" field, address if no name
  1236. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1237. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1238. %s subject
  1239. %d date
  1240. %m message-id.
  1241. You may use normal field width specification between the % and the letter.
  1242. This is for example useful to limit the length of the subject.
  1243. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1244. :group 'org-link-store
  1245. :type 'string)
  1246. (defcustom org-from-is-user-regexp
  1247. (let (r1 r2)
  1248. (when (and user-mail-address (not (string= user-mail-address "")))
  1249. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1250. (when (and user-full-name (not (string= user-full-name "")))
  1251. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1252. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1253. "Regexp matched against the \"From:\" header of an email or usenet message.
  1254. It should match if the message is from the user him/herself."
  1255. :group 'org-link-store
  1256. :type 'regexp)
  1257. (defcustom org-link-to-org-use-id 'create-if-interactive-and-no-custom-id
  1258. "Non-nil means storing a link to an Org file will use entry IDs.
  1259. Note that before this variable is even considered, org-id must be loaded,
  1260. so please customize `org-modules' and turn it on.
  1261. The variable can have the following values:
  1262. t Create an ID if needed to make a link to the current entry.
  1263. create-if-interactive
  1264. If `org-store-link' is called directly (interactively, as a user
  1265. command), do create an ID to support the link. But when doing the
  1266. job for remember, only use the ID if it already exists. The
  1267. purpose of this setting is to avoid proliferation of unwanted
  1268. IDs, just because you happen to be in an Org file when you
  1269. call `org-remember' that automatically and preemptively
  1270. creates a link. If you do want to get an ID link in a remember
  1271. template to an entry not having an ID, create it first by
  1272. explicitly creating a link to it, using `C-c C-l' first.
  1273. create-if-interactive-and-no-custom-id
  1274. Like create-if-interactive, but do not create an ID if there is
  1275. a CUSTOM_ID property defined in the entry. This is the default.
  1276. use-existing
  1277. Use existing ID, do not create one.
  1278. nil Never use an ID to make a link, instead link using a text search for
  1279. the headline text."
  1280. :group 'org-link-store
  1281. :type '(choice
  1282. (const :tag "Create ID to make link" t)
  1283. (const :tag "Create if storing link interactively"
  1284. create-if-interactive)
  1285. (const :tag "Create if storing link interactively and no CUSTOM_ID is present"
  1286. create-if-interactive-and-no-custom-id)
  1287. (const :tag "Only use existing" use-existing)
  1288. (const :tag "Do not use ID to create link" nil)))
  1289. (defcustom org-context-in-file-links t
  1290. "Non-nil means file links from `org-store-link' contain context.
  1291. A search string will be added to the file name with :: as separator and
  1292. used to find the context when the link is activated by the command
  1293. `org-open-at-point'. When this option is t, the entire active region
  1294. will be placed in the search string of the file link. If set to a
  1295. positive integer, only the first n lines of context will be stored.
  1296. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1297. negates this setting for the duration of the command."
  1298. :group 'org-link-store
  1299. :type '(choice boolean integer))
  1300. (defcustom org-keep-stored-link-after-insertion nil
  1301. "Non-nil means keep link in list for entire session.
  1302. The command `org-store-link' adds a link pointing to the current
  1303. location to an internal list. These links accumulate during a session.
  1304. The command `org-insert-link' can be used to insert links into any
  1305. Org-mode file (offering completion for all stored links). When this
  1306. option is nil, every link which has been inserted once using \\[org-insert-link]
  1307. will be removed from the list, to make completing the unused links
  1308. more efficient."
  1309. :group 'org-link-store
  1310. :type 'boolean)
  1311. (defgroup org-link-follow nil
  1312. "Options concerning following links in Org-mode."
  1313. :tag "Org Follow Link"
  1314. :group 'org-link)
  1315. (defcustom org-link-translation-function nil
  1316. "Function to translate links with different syntax to Org syntax.
  1317. This can be used to translate links created for example by the Planner
  1318. or emacs-wiki packages to Org syntax.
  1319. The function must accept two parameters, a TYPE containing the link
  1320. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1321. which is everything after the link protocol. It should return a cons
  1322. with possibly modified values of type and path.
  1323. Org contains a function for this, so if you set this variable to
  1324. `org-translate-link-from-planner', you should be able follow many
  1325. links created by planner."
  1326. :group 'org-link-follow
  1327. :type 'function)
  1328. (defcustom org-follow-link-hook nil
  1329. "Hook that is run after a link has been followed."
  1330. :group 'org-link-follow
  1331. :type 'hook)
  1332. (defcustom org-tab-follows-link nil
  1333. "Non-nil means on links TAB will follow the link.
  1334. Needs to be set before org.el is loaded.
  1335. This really should not be used, it does not make sense, and the
  1336. implementation is bad."
  1337. :group 'org-link-follow
  1338. :type 'boolean)
  1339. (defcustom org-return-follows-link nil
  1340. "Non-nil means on links RET will follow the link."
  1341. :group 'org-link-follow
  1342. :type 'boolean)
  1343. (defcustom org-mouse-1-follows-link
  1344. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1345. "Non-nil means mouse-1 on a link will follow the link.
  1346. A longer mouse click will still set point. Does not work on XEmacs.
  1347. Needs to be set before org.el is loaded."
  1348. :group 'org-link-follow
  1349. :type 'boolean)
  1350. (defcustom org-mark-ring-length 4
  1351. "Number of different positions to be recorded in the ring.
  1352. Changing this requires a restart of Emacs to work correctly."
  1353. :group 'org-link-follow
  1354. :type 'integer)
  1355. (defcustom org-link-search-must-match-exact-headline 'query-to-create
  1356. "Non-nil means internal links in Org files must exactly match a headline.
  1357. When nil, the link search tries to match a phrase with all words
  1358. in the search text."
  1359. :group 'org-link-follow
  1360. :version "24.1"
  1361. :type '(choice
  1362. (const :tag "Use fuzzy text search" nil)
  1363. (const :tag "Match only exact headline" t)
  1364. (const :tag "Match exact headline or query to create it"
  1365. query-to-create)))
  1366. (defcustom org-link-frame-setup
  1367. '((vm . vm-visit-folder-other-frame)
  1368. (vm-imap . vm-visit-imap-folder-other-frame)
  1369. (gnus . org-gnus-no-new-news)
  1370. (file . find-file-other-window)
  1371. (wl . wl-other-frame))
  1372. "Setup the frame configuration for following links.
  1373. When following a link with Emacs, it may often be useful to display
  1374. this link in another window or frame. This variable can be used to
  1375. set this up for the different types of links.
  1376. For VM, use any of
  1377. `vm-visit-folder'
  1378. `vm-visit-folder-other-window'
  1379. `vm-visit-folder-other-frame'
  1380. For Gnus, use any of
  1381. `gnus'
  1382. `gnus-other-frame'
  1383. `org-gnus-no-new-news'
  1384. For FILE, use any of
  1385. `find-file'
  1386. `find-file-other-window'
  1387. `find-file-other-frame'
  1388. For Wanderlust use any of
  1389. `wl'
  1390. `wl-other-frame'
  1391. For the calendar, use the variable `calendar-setup'.
  1392. For BBDB, it is currently only possible to display the matches in
  1393. another window."
  1394. :group 'org-link-follow
  1395. :type '(list
  1396. (cons (const vm)
  1397. (choice
  1398. (const vm-visit-folder)
  1399. (const vm-visit-folder-other-window)
  1400. (const vm-visit-folder-other-frame)))
  1401. (cons (const gnus)
  1402. (choice
  1403. (const gnus)
  1404. (const gnus-other-frame)
  1405. (const org-gnus-no-new-news)))
  1406. (cons (const file)
  1407. (choice
  1408. (const find-file)
  1409. (const find-file-other-window)
  1410. (const find-file-other-frame)))
  1411. (cons (const wl)
  1412. (choice
  1413. (const wl)
  1414. (const wl-other-frame)))))
  1415. (defcustom org-display-internal-link-with-indirect-buffer nil
  1416. "Non-nil means use indirect buffer to display infile links.
  1417. Activating internal links (from one location in a file to another location
  1418. in the same file) normally just jumps to the location. When the link is
  1419. activated with a \\[universal-argument] prefix (or with mouse-3), the link \
  1420. is displayed in
  1421. another window. When this option is set, the other window actually displays
  1422. an indirect buffer clone of the current buffer, to avoid any visibility
  1423. changes to the current buffer."
  1424. :group 'org-link-follow
  1425. :type 'boolean)
  1426. (defcustom org-open-non-existing-files nil
  1427. "Non-nil means `org-open-file' will open non-existing files.
  1428. When nil, an error will be generated.
  1429. This variable applies only to external applications because they
  1430. might choke on non-existing files. If the link is to a file that
  1431. will be opened in Emacs, the variable is ignored."
  1432. :group 'org-link-follow
  1433. :type 'boolean)
  1434. (defcustom org-open-directory-means-index-dot-org nil
  1435. "Non-nil means a link to a directory really means to index.org.
  1436. When nil, following a directory link will run dired or open a finder/explorer
  1437. window on that directory."
  1438. :group 'org-link-follow
  1439. :type 'boolean)
  1440. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1441. "Function and arguments to call for following mailto links.
  1442. This is a list with the first element being a Lisp function, and the
  1443. remaining elements being arguments to the function. In string arguments,
  1444. %a will be replaced by the address, and %s will be replaced by the subject
  1445. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1446. :group 'org-link-follow
  1447. :type '(choice
  1448. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1449. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1450. (const :tag "message-mail" (message-mail "%a" "%s"))
  1451. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1452. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1453. "Non-nil means ask for confirmation before executing shell links.
  1454. Shell links can be dangerous: just think about a link
  1455. [[shell:rm -rf ~/*][Google Search]]
  1456. This link would show up in your Org-mode document as \"Google Search\",
  1457. but really it would remove your entire home directory.
  1458. Therefore we advise against setting this variable to nil.
  1459. Just change it to `y-or-n-p' if you want to confirm with a
  1460. single keystroke rather than having to type \"yes\"."
  1461. :group 'org-link-follow
  1462. :type '(choice
  1463. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1464. (const :tag "with y-or-n (faster)" y-or-n-p)
  1465. (const :tag "no confirmation (dangerous)" nil)))
  1466. (put 'org-confirm-shell-link-function
  1467. 'safe-local-variable
  1468. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1469. (defcustom org-confirm-shell-link-not-regexp ""
  1470. "A regexp to skip confirmation for shell links."
  1471. :group 'org-link-follow
  1472. :version "24.1"
  1473. :type 'regexp)
  1474. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1475. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1476. Elisp links can be dangerous: just think about a link
  1477. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1478. This link would show up in your Org-mode document as \"Google Search\",
  1479. but really it would remove your entire home directory.
  1480. Therefore we advise against setting this variable to nil.
  1481. Just change it to `y-or-n-p' if you want to confirm with a
  1482. single keystroke rather than having to type \"yes\"."
  1483. :group 'org-link-follow
  1484. :type '(choice
  1485. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1486. (const :tag "with y-or-n (faster)" y-or-n-p)
  1487. (const :tag "no confirmation (dangerous)" nil)))
  1488. (put 'org-confirm-shell-link-function
  1489. 'safe-local-variable
  1490. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1491. (defcustom org-confirm-elisp-link-not-regexp ""
  1492. "A regexp to skip confirmation for Elisp links."
  1493. :group 'org-link-follow
  1494. :version "24.1"
  1495. :type 'regexp)
  1496. (defconst org-file-apps-defaults-gnu
  1497. '((remote . emacs)
  1498. (system . mailcap)
  1499. (t . mailcap))
  1500. "Default file applications on a UNIX or GNU/Linux system.
  1501. See `org-file-apps'.")
  1502. (defconst org-file-apps-defaults-macosx
  1503. '((remote . emacs)
  1504. (t . "open %s")
  1505. (system . "open %s")
  1506. ("ps.gz" . "gv %s")
  1507. ("eps.gz" . "gv %s")
  1508. ("dvi" . "xdvi %s")
  1509. ("fig" . "xfig %s"))
  1510. "Default file applications on a MacOS X system.
  1511. The system \"open\" is known as a default, but we use X11 applications
  1512. for some files for which the OS does not have a good default.
  1513. See `org-file-apps'.")
  1514. (defconst org-file-apps-defaults-windowsnt
  1515. (list
  1516. '(remote . emacs)
  1517. (cons t
  1518. (list (if (featurep 'xemacs)
  1519. 'mswindows-shell-execute
  1520. 'w32-shell-execute)
  1521. "open" 'file))
  1522. (cons 'system
  1523. (list (if (featurep 'xemacs)
  1524. 'mswindows-shell-execute
  1525. 'w32-shell-execute)
  1526. "open" 'file)))
  1527. "Default file applications on a Windows NT system.
  1528. The system \"open\" is used for most files.
  1529. See `org-file-apps'.")
  1530. (defcustom org-file-apps
  1531. '(
  1532. (auto-mode . emacs)
  1533. ("\\.mm\\'" . default)
  1534. ("\\.x?html?\\'" . default)
  1535. ("\\.pdf\\'" . default)
  1536. )
  1537. "External applications for opening `file:path' items in a document.
  1538. Org-mode uses system defaults for different file types, but
  1539. you can use this variable to set the application for a given file
  1540. extension. The entries in this list are cons cells where the car identifies
  1541. files and the cdr the corresponding command. Possible values for the
  1542. file identifier are
  1543. \"string\" A string as a file identifier can be interpreted in different
  1544. ways, depending on its contents:
  1545. - Alphanumeric characters only:
  1546. Match links with this file extension.
  1547. Example: (\"pdf\" . \"evince %s\")
  1548. to open PDFs with evince.
  1549. - Regular expression: Match links where the
  1550. filename matches the regexp. If you want to
  1551. use groups here, use shy groups.
  1552. Example: (\"\\.x?html\\'\" . \"firefox %s\")
  1553. (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1554. to open *.html and *.xhtml with firefox.
  1555. - Regular expression which contains (non-shy) groups:
  1556. Match links where the whole link, including \"::\", and
  1557. anything after that, matches the regexp.
  1558. In a custom command string, %1, %2, etc. are replaced with
  1559. the parts of the link that were matched by the groups.
  1560. For backwards compatibility, if a command string is given
  1561. that does not use any of the group matches, this case is
  1562. handled identically to the second one (i.e. match against
  1563. file name only).
  1564. In a custom lisp form, you can access the group matches with
  1565. (match-string n link).
  1566. Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
  1567. to open [[file:document.pdf::5]] with evince at page 5.
  1568. `directory' Matches a directory
  1569. `remote' Matches a remote file, accessible through tramp or efs.
  1570. Remote files most likely should be visited through Emacs
  1571. because external applications cannot handle such paths.
  1572. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1573. so all files Emacs knows how to handle. Using this with
  1574. command `emacs' will open most files in Emacs. Beware that this
  1575. will also open html files inside Emacs, unless you add
  1576. (\"html\" . default) to the list as well.
  1577. t Default for files not matched by any of the other options.
  1578. `system' The system command to open files, like `open' on Windows
  1579. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1580. that will be selected if you call `C-c C-o' with a double
  1581. \\[universal-argument] \\[universal-argument] prefix.
  1582. Possible values for the command are:
  1583. `emacs' The file will be visited by the current Emacs process.
  1584. `default' Use the default application for this file type, which is the
  1585. association for t in the list, most likely in the system-specific
  1586. part.
  1587. This can be used to overrule an unwanted setting in the
  1588. system-specific variable.
  1589. `system' Use the system command for opening files, like \"open\".
  1590. This command is specified by the entry whose car is `system'.
  1591. Most likely, the system-specific version of this variable
  1592. does define this command, but you can overrule/replace it
  1593. here.
  1594. string A command to be executed by a shell; %s will be replaced
  1595. by the path to the file.
  1596. sexp A Lisp form which will be evaluated. The file path will
  1597. be available in the Lisp variable `file'.
  1598. For more examples, see the system specific constants
  1599. `org-file-apps-defaults-macosx'
  1600. `org-file-apps-defaults-windowsnt'
  1601. `org-file-apps-defaults-gnu'."
  1602. :group 'org-link-follow
  1603. :type '(repeat
  1604. (cons (choice :value ""
  1605. (string :tag "Extension")
  1606. (const :tag "System command to open files" system)
  1607. (const :tag "Default for unrecognized files" t)
  1608. (const :tag "Remote file" remote)
  1609. (const :tag "Links to a directory" directory)
  1610. (const :tag "Any files that have Emacs modes"
  1611. auto-mode))
  1612. (choice :value ""
  1613. (const :tag "Visit with Emacs" emacs)
  1614. (const :tag "Use default" default)
  1615. (const :tag "Use the system command" system)
  1616. (string :tag "Command")
  1617. (sexp :tag "Lisp form")))))
  1618. (defcustom org-doi-server-url "http://dx.doi.org/"
  1619. "The URL of the DOI server."
  1620. :type 'string
  1621. :version "24.2"
  1622. :group 'org-link-follow)
  1623. (defgroup org-refile nil
  1624. "Options concerning refiling entries in Org-mode."
  1625. :tag "Org Refile"
  1626. :group 'org)
  1627. (defcustom org-directory "~/org"
  1628. "Directory with org files.
  1629. This is just a default location to look for Org files. There is no need
  1630. at all to put your files into this directory. It is only used in the
  1631. following situations:
  1632. 1. When a remember template specifies a target file that is not an
  1633. absolute path. The path will then be interpreted relative to
  1634. `org-directory'
  1635. 2. When a remember note is filed away in an interactive way (when exiting the
  1636. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1637. with `org-directory' as the default path."
  1638. :group 'org-refile
  1639. :group 'org-remember
  1640. :type 'directory)
  1641. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1642. "Default target for storing notes.
  1643. Used as a fall back file for org-remember.el and org-capture.el, for
  1644. templates that do not specify a target file."
  1645. :group 'org-refile
  1646. :group 'org-remember
  1647. :type '(choice
  1648. (const :tag "Default from remember-data-file" nil)
  1649. file))
  1650. (defcustom org-goto-interface 'outline
  1651. "The default interface to be used for `org-goto'.
  1652. Allowed values are:
  1653. outline The interface shows an outline of the relevant file
  1654. and the correct heading is found by moving through
  1655. the outline or by searching with incremental search.
  1656. outline-path-completion Headlines in the current buffer are offered via
  1657. completion. This is the interface also used by
  1658. the refile command."
  1659. :group 'org-refile
  1660. :type '(choice
  1661. (const :tag "Outline" outline)
  1662. (const :tag "Outline-path-completion" outline-path-completion)))
  1663. (defcustom org-goto-max-level 5
  1664. "Maximum target level when running `org-goto' with refile interface."
  1665. :group 'org-refile
  1666. :type 'integer)
  1667. (defcustom org-reverse-note-order nil
  1668. "Non-nil means store new notes at the beginning of a file or entry.
  1669. When nil, new notes will be filed to the end of a file or entry.
  1670. This can also be a list with cons cells of regular expressions that
  1671. are matched against file names, and values."
  1672. :group 'org-remember
  1673. :group 'org-refile
  1674. :type '(choice
  1675. (const :tag "Reverse always" t)
  1676. (const :tag "Reverse never" nil)
  1677. (repeat :tag "By file name regexp"
  1678. (cons regexp boolean))))
  1679. (defcustom org-log-refile nil
  1680. "Information to record when a task is refiled.
  1681. Possible values are:
  1682. nil Don't add anything
  1683. time Add a time stamp to the task
  1684. note Prompt for a note and add it with template `org-log-note-headings'
  1685. This option can also be set with on a per-file-basis with
  1686. #+STARTUP: nologrefile
  1687. #+STARTUP: logrefile
  1688. #+STARTUP: lognoterefile
  1689. You can have local logging settings for a subtree by setting the LOGGING
  1690. property to one or more of these keywords.
  1691. When bulk-refiling from the agenda, the value `note' is forbidden and
  1692. will temporarily be changed to `time'."
  1693. :group 'org-refile
  1694. :group 'org-progress
  1695. :version "24.1"
  1696. :type '(choice
  1697. (const :tag "No logging" nil)
  1698. (const :tag "Record timestamp" time)
  1699. (const :tag "Record timestamp with note." note)))
  1700. (defcustom org-refile-targets nil
  1701. "Targets for refiling entries with \\[org-refile].
  1702. This is a list of cons cells. Each cell contains:
  1703. - a specification of the files to be considered, either a list of files,
  1704. or a symbol whose function or variable value will be used to retrieve
  1705. a file name or a list of file names. If you use `org-agenda-files' for
  1706. that, all agenda files will be scanned for targets. Nil means consider
  1707. headings in the current buffer.
  1708. - A specification of how to find candidate refile targets. This may be
  1709. any of:
  1710. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1711. This tag has to be present in all target headlines, inheritance will
  1712. not be considered.
  1713. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1714. todo keyword.
  1715. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1716. headlines that are refiling targets.
  1717. - a cons cell (:level . N). Any headline of level N is considered a target.
  1718. Note that, when `org-odd-levels-only' is set, level corresponds to
  1719. order in hierarchy, not to the number of stars.
  1720. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1721. Note that, when `org-odd-levels-only' is set, level corresponds to
  1722. order in hierarchy, not to the number of stars.
  1723. Each element of this list generates a set of possible targets.
  1724. The union of these sets is presented (with completion) to
  1725. the user by `org-refile'.
  1726. You can set the variable `org-refile-target-verify-function' to a function
  1727. to verify each headline found by the simple criteria above.
  1728. When this variable is nil, all top-level headlines in the current buffer
  1729. are used, equivalent to the value `((nil . (:level . 1))'."
  1730. :group 'org-refile
  1731. :type '(repeat
  1732. (cons
  1733. (choice :value org-agenda-files
  1734. (const :tag "All agenda files" org-agenda-files)
  1735. (const :tag "Current buffer" nil)
  1736. (function) (variable) (file))
  1737. (choice :tag "Identify target headline by"
  1738. (cons :tag "Specific tag" (const :value :tag) (string))
  1739. (cons :tag "TODO keyword" (const :value :todo) (string))
  1740. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1741. (cons :tag "Level number" (const :value :level) (integer))
  1742. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1743. (defcustom org-refile-target-verify-function nil
  1744. "Function to verify if the headline at point should be a refile target.
  1745. The function will be called without arguments, with point at the
  1746. beginning of the headline. It should return t and leave point
  1747. where it is if the headline is a valid target for refiling.
  1748. If the target should not be selected, the function must return nil.
  1749. In addition to this, it may move point to a place from where the search
  1750. should be continued. For example, the function may decide that the entire
  1751. subtree of the current entry should be excluded and move point to the end
  1752. of the subtree."
  1753. :group 'org-refile
  1754. :type 'function)
  1755. (defcustom org-refile-use-cache nil
  1756. "Non-nil means cache refile targets to speed up the process.
  1757. The cache for a particular file will be updated automatically when
  1758. the buffer has been killed, or when any of the marker used for flagging
  1759. refile targets no longer points at a live buffer.
  1760. If you have added new entries to a buffer that might themselves be targets,
  1761. you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
  1762. find that easier, `C-u C-u C-u C-c C-w'."
  1763. :group 'org-refile
  1764. :version "24.1"
  1765. :type 'boolean)
  1766. (defcustom org-refile-use-outline-path nil
  1767. "Non-nil means provide refile targets as paths.
  1768. So a level 3 headline will be available as level1/level2/level3.
  1769. When the value is `file', also include the file name (without directory)
  1770. into the path. In this case, you can also stop the completion after
  1771. the file name, to get entries inserted as top level in the file.
  1772. When `full-file-path', include the full file path."
  1773. :group 'org-refile
  1774. :type '(choice
  1775. (const :tag "Not" nil)
  1776. (const :tag "Yes" t)
  1777. (const :tag "Start with file name" file)
  1778. (const :tag "Start with full file path" full-file-path)))
  1779. (defcustom org-outline-path-complete-in-steps t
  1780. "Non-nil means complete the outline path in hierarchical steps.
  1781. When Org-mode uses the refile interface to select an outline path
  1782. \(see variable `org-refile-use-outline-path'), the completion of
  1783. the path can be done is a single go, or if can be done in steps down
  1784. the headline hierarchy. Going in steps is probably the best if you
  1785. do not use a special completion package like `ido' or `icicles'.
  1786. However, when using these packages, going in one step can be very
  1787. fast, while still showing the whole path to the entry."
  1788. :group 'org-refile
  1789. :type 'boolean)
  1790. (defcustom org-refile-allow-creating-parent-nodes nil
  1791. "Non-nil means allow to create new nodes as refile targets.
  1792. New nodes are then created by adding \"/new node name\" to the completion
  1793. of an existing node. When the value of this variable is `confirm',
  1794. new node creation must be confirmed by the user (recommended)
  1795. When nil, the completion must match an existing entry.
  1796. Note that, if the new heading is not seen by the criteria
  1797. listed in `org-refile-targets', multiple instances of the same
  1798. heading would be created by trying again to file under the new
  1799. heading."
  1800. :group 'org-refile
  1801. :type '(choice
  1802. (const :tag "Never" nil)
  1803. (const :tag "Always" t)
  1804. (const :tag "Prompt for confirmation" confirm)))
  1805. (defcustom org-refile-active-region-within-subtree nil
  1806. "Non-nil means also refile active region within a subtree.
  1807. By default `org-refile' doesn't allow refiling regions if they
  1808. don't contain a set of subtrees, but it might be convenient to
  1809. do so sometimes: in that case, the first line of the region is
  1810. converted to a headline before refiling."
  1811. :group 'org-refile
  1812. :version "24.1"
  1813. :type 'boolean)
  1814. (defgroup org-todo nil
  1815. "Options concerning TODO items in Org-mode."
  1816. :tag "Org TODO"
  1817. :group 'org)
  1818. (defgroup org-progress nil
  1819. "Options concerning Progress logging in Org-mode."
  1820. :tag "Org Progress"
  1821. :group 'org-time)
  1822. (defvar org-todo-interpretation-widgets
  1823. '((:tag "Sequence (cycling hits every state)" sequence)
  1824. (:tag "Type (cycling directly to DONE)" type))
  1825. "The available interpretation symbols for customizing `org-todo-keywords'.
  1826. Interested libraries should add to this list.")
  1827. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1828. "List of TODO entry keyword sequences and their interpretation.
  1829. \\<org-mode-map>This is a list of sequences.
  1830. Each sequence starts with a symbol, either `sequence' or `type',
  1831. indicating if the keywords should be interpreted as a sequence of
  1832. action steps, or as different types of TODO items. The first
  1833. keywords are states requiring action - these states will select a headline
  1834. for inclusion into the global TODO list Org-mode produces. If one of
  1835. the \"keywords\" is the vertical bar, \"|\", the remaining keywords
  1836. signify that no further action is necessary. If \"|\" is not found,
  1837. the last keyword is treated as the only DONE state of the sequence.
  1838. The command \\[org-todo] cycles an entry through these states, and one
  1839. additional state where no keyword is present. For details about this
  1840. cycling, see the manual.
  1841. TODO keywords and interpretation can also be set on a per-file basis with
  1842. the special #+SEQ_TODO and #+TYP_TODO lines.
  1843. Each keyword can optionally specify a letter for fast state selection
  1844. \(in combination with the variable `org-use-fast-todo-selection')
  1845. and specifiers for state change logging, using the same syntax that
  1846. is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says that
  1847. the WAIT state can be selected with the \"w\" letter. \"WAIT(w!)\"
  1848. indicates to record a time stamp each time this state is selected.
  1849. Each keyword may also specify if a timestamp or a note should be
  1850. recorded when entering or leaving the state, by adding additional
  1851. characters in the parenthesis after the keyword. This looks like this:
  1852. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1853. record only the time of the state change. With X and Y being either
  1854. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1855. Y when leaving the state if and only if the *target* state does not
  1856. define X. You may omit any of the fast-selection key or X or /Y,
  1857. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1858. For backward compatibility, this variable may also be just a list
  1859. of keywords. In this case the interpretation (sequence or type) will be
  1860. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1861. :group 'org-todo
  1862. :group 'org-keywords
  1863. :type '(choice
  1864. (repeat :tag "Old syntax, just keywords"
  1865. (string :tag "Keyword"))
  1866. (repeat :tag "New syntax"
  1867. (cons
  1868. (choice
  1869. :tag "Interpretation"
  1870. ;;Quick and dirty way to see
  1871. ;;`org-todo-interpretations'. This takes the
  1872. ;;place of item arguments
  1873. :convert-widget
  1874. (lambda (widget)
  1875. (widget-put widget
  1876. :args (mapcar
  1877. #'(lambda (x)
  1878. (widget-convert
  1879. (cons 'const x)))
  1880. org-todo-interpretation-widgets))
  1881. widget))
  1882. (repeat
  1883. (string :tag "Keyword"))))))
  1884. (defvar org-todo-keywords-1 nil
  1885. "All TODO and DONE keywords active in a buffer.")
  1886. (make-variable-buffer-local 'org-todo-keywords-1)
  1887. (defvar org-todo-keywords-for-agenda nil)
  1888. (defvar org-done-keywords-for-agenda nil)
  1889. (defvar org-drawers-for-agenda nil)
  1890. (defvar org-todo-keyword-alist-for-agenda nil)
  1891. (defvar org-tag-alist-for-agenda nil)
  1892. (defvar org-agenda-contributing-files nil)
  1893. (defvar org-not-done-keywords nil)
  1894. (make-variable-buffer-local 'org-not-done-keywords)
  1895. (defvar org-done-keywords nil)
  1896. (make-variable-buffer-local 'org-done-keywords)
  1897. (defvar org-todo-heads nil)
  1898. (make-variable-buffer-local 'org-todo-heads)
  1899. (defvar org-todo-sets nil)
  1900. (make-variable-buffer-local 'org-todo-sets)
  1901. (defvar org-todo-log-states nil)
  1902. (make-variable-buffer-local 'org-todo-log-states)
  1903. (defvar org-todo-kwd-alist nil)
  1904. (make-variable-buffer-local 'org-todo-kwd-alist)
  1905. (defvar org-todo-key-alist nil)
  1906. (make-variable-buffer-local 'org-todo-key-alist)
  1907. (defvar org-todo-key-trigger nil)
  1908. (make-variable-buffer-local 'org-todo-key-trigger)
  1909. (defcustom org-todo-interpretation 'sequence
  1910. "Controls how TODO keywords are interpreted.
  1911. This variable is in principle obsolete and is only used for
  1912. backward compatibility, if the interpretation of todo keywords is
  1913. not given already in `org-todo-keywords'. See that variable for
  1914. more information."
  1915. :group 'org-todo
  1916. :group 'org-keywords
  1917. :type '(choice (const sequence)
  1918. (const type)))
  1919. (defcustom org-use-fast-todo-selection t
  1920. "Non-nil means use the fast todo selection scheme with C-c C-t.
  1921. This variable describes if and under what circumstances the cycling
  1922. mechanism for TODO keywords will be replaced by a single-key, direct
  1923. selection scheme.
  1924. When nil, fast selection is never used.
  1925. When the symbol `prefix', it will be used when `org-todo' is called
  1926. with a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and
  1927. `C-u t' in an agenda buffer.
  1928. When t, fast selection is used by default. In this case, the prefix
  1929. argument forces cycling instead.
  1930. In all cases, the special interface is only used if access keys have
  1931. actually been assigned by the user, i.e. if keywords in the configuration
  1932. are followed by a letter in parenthesis, like TODO(t)."
  1933. :group 'org-todo
  1934. :type '(choice
  1935. (const :tag "Never" nil)
  1936. (const :tag "By default" t)
  1937. (const :tag "Only with C-u C-c C-t" prefix)))
  1938. (defcustom org-provide-todo-statistics t
  1939. "Non-nil means update todo statistics after insert and toggle.
  1940. ALL-HEADLINES means update todo statistics by including headlines
  1941. with no TODO keyword as well, counting them as not done.
  1942. A list of TODO keywords means the same, but skip keywords that are
  1943. not in this list.
  1944. When this is set, todo statistics is updated in the parent of the
  1945. current entry each time a todo state is changed."
  1946. :group 'org-todo
  1947. :type '(choice
  1948. (const :tag "Yes, only for TODO entries" t)
  1949. (const :tag "Yes, including all entries" 'all-headlines)
  1950. (repeat :tag "Yes, for TODOs in this list"
  1951. (string :tag "TODO keyword"))
  1952. (other :tag "No TODO statistics" nil)))
  1953. (defcustom org-hierarchical-todo-statistics t
  1954. "Non-nil means TODO statistics covers just direct children.
  1955. When nil, all entries in the subtree are considered.
  1956. This has only an effect if `org-provide-todo-statistics' is set.
  1957. To set this to nil for only a single subtree, use a COOKIE_DATA
  1958. property and include the word \"recursive\" into the value."
  1959. :group 'org-todo
  1960. :type 'boolean)
  1961. (defcustom org-after-todo-state-change-hook nil
  1962. "Hook which is run after the state of a TODO item was changed.
  1963. The new state (a string with a TODO keyword, or nil) is available in the
  1964. Lisp variable `org-state'."
  1965. :group 'org-todo
  1966. :type 'hook)
  1967. (defvar org-blocker-hook nil
  1968. "Hook for functions that are allowed to block a state change.
  1969. Each function gets as its single argument a property list, see
  1970. `org-trigger-hook' for more information about this list.
  1971. If any of the functions in this hook returns nil, the state change
  1972. is blocked.")
  1973. (defvar org-trigger-hook nil
  1974. "Hook for functions that are triggered by a state change.
  1975. Each function gets as its single argument a property list with at least
  1976. the following elements:
  1977. (:type type-of-change :position pos-at-entry-start
  1978. :from old-state :to new-state)
  1979. Depending on the type, more properties may be present.
  1980. This mechanism is currently implemented for:
  1981. TODO state changes
  1982. ------------------
  1983. :type todo-state-change
  1984. :from previous state (keyword as a string), or nil, or a symbol
  1985. 'todo' or 'done', to indicate the general type of state.
  1986. :to new state, like in :from")
  1987. (defcustom org-enforce-todo-dependencies nil
  1988. "Non-nil means undone TODO entries will block switching the parent to DONE.
  1989. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  1990. be blocked if any prior sibling is not yet done.
  1991. Finally, if the parent is blocked because of ordered siblings of its own,
  1992. the child will also be blocked."
  1993. :set (lambda (var val)
  1994. (set var val)
  1995. (if val
  1996. (add-hook 'org-blocker-hook
  1997. 'org-block-todo-from-children-or-siblings-or-parent)
  1998. (remove-hook 'org-blocker-hook
  1999. 'org-block-todo-from-children-or-siblings-or-parent)))
  2000. :group 'org-todo
  2001. :type 'boolean)
  2002. (defcustom org-enforce-todo-checkbox-dependencies nil
  2003. "Non-nil means unchecked boxes will block switching the parent to DONE.
  2004. When this is nil, checkboxes have no influence on switching TODO states.
  2005. When non-nil, you first need to check off all check boxes before the TODO
  2006. entry can be switched to DONE.
  2007. This variable needs to be set before org.el is loaded, and you need to
  2008. restart Emacs after a change to make the change effective. The only way
  2009. to change is while Emacs is running is through the customize interface."
  2010. :set (lambda (var val)
  2011. (set var val)
  2012. (if val
  2013. (add-hook 'org-blocker-hook
  2014. 'org-block-todo-from-checkboxes)
  2015. (remove-hook 'org-blocker-hook
  2016. 'org-block-todo-from-checkboxes)))
  2017. :group 'org-todo
  2018. :type 'boolean)
  2019. (defcustom org-treat-insert-todo-heading-as-state-change nil
  2020. "Non-nil means inserting a TODO heading is treated as state change.
  2021. So when the command \\[org-insert-todo-heading] is used, state change
  2022. logging will apply if appropriate. When nil, the new TODO item will
  2023. be inserted directly, and no logging will take place."
  2024. :group 'org-todo
  2025. :type 'boolean)
  2026. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  2027. "Non-nil means switching TODO states with S-cursor counts as state change.
  2028. This is the default behavior. However, setting this to nil allows a
  2029. convenient way to select a TODO state and bypass any logging associated
  2030. with that."
  2031. :group 'org-todo
  2032. :type 'boolean)
  2033. (defcustom org-todo-state-tags-triggers nil
  2034. "Tag changes that should be triggered by TODO state changes.
  2035. This is a list. Each entry is
  2036. (state-change (tag . flag) .......)
  2037. State-change can be a string with a state, and empty string to indicate the
  2038. state that has no TODO keyword, or it can be one of the symbols `todo'
  2039. or `done', meaning any not-done or done state, respectively."
  2040. :group 'org-todo
  2041. :group 'org-tags
  2042. :type '(repeat
  2043. (cons (choice :tag "When changing to"
  2044. (const :tag "Not-done state" todo)
  2045. (const :tag "Done state" done)
  2046. (string :tag "State"))
  2047. (repeat
  2048. (cons :tag "Tag action"
  2049. (string :tag "Tag")
  2050. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  2051. (defcustom org-log-done nil
  2052. "Information to record when a task moves to the DONE state.
  2053. Possible values are:
  2054. nil Don't add anything, just change the keyword
  2055. time Add a time stamp to the task
  2056. note Prompt for a note and add it with template `org-log-note-headings'
  2057. This option can also be set with on a per-file-basis with
  2058. #+STARTUP: nologdone
  2059. #+STARTUP: logdone
  2060. #+STARTUP: lognotedone
  2061. You can have local logging settings for a subtree by setting the LOGGING
  2062. property to one or more of these keywords."
  2063. :group 'org-todo
  2064. :group 'org-progress
  2065. :type '(choice
  2066. (const :tag "No logging" nil)
  2067. (const :tag "Record CLOSED timestamp" time)
  2068. (const :tag "Record CLOSED timestamp with note." note)))
  2069. ;; Normalize old uses of org-log-done.
  2070. (cond
  2071. ((eq org-log-done t) (setq org-log-done 'time))
  2072. ((and (listp org-log-done) (memq 'done org-log-done))
  2073. (setq org-log-done 'note)))
  2074. (defcustom org-log-reschedule nil
  2075. "Information to record when the scheduling date of a tasks is modified.
  2076. Possible values are:
  2077. nil Don't add anything, just change the date
  2078. time Add a time stamp to the task
  2079. note Prompt for a note and add it with template `org-log-note-headings'
  2080. This option can also be set with on a per-file-basis with
  2081. #+STARTUP: nologreschedule
  2082. #+STARTUP: logreschedule
  2083. #+STARTUP: lognotereschedule"
  2084. :group 'org-todo
  2085. :group 'org-progress
  2086. :type '(choice
  2087. (const :tag "No logging" nil)
  2088. (const :tag "Record timestamp" time)
  2089. (const :tag "Record timestamp with note." note)))
  2090. (defcustom org-log-redeadline nil
  2091. "Information to record when the deadline date of a tasks is modified.
  2092. Possible values are:
  2093. nil Don't add anything, just change the date
  2094. time Add a time stamp to the task
  2095. note Prompt for a note and add it with template `org-log-note-headings'
  2096. This option can also be set with on a per-file-basis with
  2097. #+STARTUP: nologredeadline
  2098. #+STARTUP: logredeadline
  2099. #+STARTUP: lognoteredeadline
  2100. You can have local logging settings for a subtree by setting the LOGGING
  2101. property to one or more of these keywords."
  2102. :group 'org-todo
  2103. :group 'org-progress
  2104. :type '(choice
  2105. (const :tag "No logging" nil)
  2106. (const :tag "Record timestamp" time)
  2107. (const :tag "Record timestamp with note." note)))
  2108. (defcustom org-log-note-clock-out nil
  2109. "Non-nil means record a note when clocking out of an item.
  2110. This can also be configured on a per-file basis by adding one of
  2111. the following lines anywhere in the buffer:
  2112. #+STARTUP: lognoteclock-out
  2113. #+STARTUP: nolognoteclock-out"
  2114. :group 'org-todo
  2115. :group 'org-progress
  2116. :type 'boolean)
  2117. (defcustom org-log-done-with-time t
  2118. "Non-nil means the CLOSED time stamp will contain date and time.
  2119. When nil, only the date will be recorded."
  2120. :group 'org-progress
  2121. :type 'boolean)
  2122. (defcustom org-log-note-headings
  2123. '((done . "CLOSING NOTE %t")
  2124. (state . "State %-12s from %-12S %t")
  2125. (note . "Note taken on %t")
  2126. (reschedule . "Rescheduled from %S on %t")
  2127. (delschedule . "Not scheduled, was %S on %t")
  2128. (redeadline . "New deadline from %S on %t")
  2129. (deldeadline . "Removed deadline, was %S on %t")
  2130. (refile . "Refiled on %t")
  2131. (clock-out . ""))
  2132. "Headings for notes added to entries.
  2133. The value is an alist, with the car being a symbol indicating the note
  2134. context, and the cdr is the heading to be used. The heading may also be the
  2135. empty string.
  2136. %t in the heading will be replaced by a time stamp.
  2137. %T will be an active time stamp instead the default inactive one
  2138. %d will be replaced by a short-format time stamp.
  2139. %D will be replaced by an active short-format time stamp.
  2140. %s will be replaced by the new TODO state, in double quotes.
  2141. %S will be replaced by the old TODO state, in double quotes.
  2142. %u will be replaced by the user name.
  2143. %U will be replaced by the full user name.
  2144. In fact, it is not a good idea to change the `state' entry, because
  2145. agenda log mode depends on the format of these entries."
  2146. :group 'org-todo
  2147. :group 'org-progress
  2148. :type '(list :greedy t
  2149. (cons (const :tag "Heading when closing an item" done) string)
  2150. (cons (const :tag
  2151. "Heading when changing todo state (todo sequence only)"
  2152. state) string)
  2153. (cons (const :tag "Heading when just taking a note" note) string)
  2154. (cons (const :tag "Heading when clocking out" clock-out) string)
  2155. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2156. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2157. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2158. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2159. (cons (const :tag "Heading when refiling" refile) string)))
  2160. (unless (assq 'note org-log-note-headings)
  2161. (push '(note . "%t") org-log-note-headings))
  2162. (defcustom org-log-into-drawer nil
  2163. "Non-nil means insert state change notes and time stamps into a drawer.
  2164. When nil, state changes notes will be inserted after the headline and
  2165. any scheduling and clock lines, but not inside a drawer.
  2166. The value of this variable should be the name of the drawer to use.
  2167. LOGBOOK is proposed as the default drawer for this purpose, you can
  2168. also set this to a string to define the drawer of your choice.
  2169. A value of t is also allowed, representing \"LOGBOOK\".
  2170. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2171. will be ignored.
  2172. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2173. a subtree."
  2174. :group 'org-todo
  2175. :group 'org-progress
  2176. :type '(choice
  2177. (const :tag "Not into a drawer" nil)
  2178. (const :tag "LOGBOOK" t)
  2179. (string :tag "Other")))
  2180. (if (fboundp 'defvaralias)
  2181. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  2182. (defun org-log-into-drawer ()
  2183. "Return the value of `org-log-into-drawer', but let properties overrule.
  2184. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  2185. used instead of the default value."
  2186. (let ((p (org-entry-get nil "LOG_INTO_DRAWER" 'inherit)))
  2187. (cond
  2188. ((or (not p) (equal p "nil")) org-log-into-drawer)
  2189. ((equal p "t") "LOGBOOK")
  2190. (t p))))
  2191. (defcustom org-log-state-notes-insert-after-drawers nil
  2192. "Non-nil means insert state change notes after any drawers in entry.
  2193. Only the drawers that *immediately* follow the headline and the
  2194. deadline/scheduled line are skipped.
  2195. When nil, insert notes right after the heading and perhaps the line
  2196. with deadline/scheduling if present.
  2197. This variable will have no effect if `org-log-into-drawer' is
  2198. set."
  2199. :group 'org-todo
  2200. :group 'org-progress
  2201. :type 'boolean)
  2202. (defcustom org-log-states-order-reversed t
  2203. "Non-nil means the latest state note will be directly after heading.
  2204. When nil, the state change notes will be ordered according to time."
  2205. :group 'org-todo
  2206. :group 'org-progress
  2207. :type 'boolean)
  2208. (defcustom org-todo-repeat-to-state nil
  2209. "The TODO state to which a repeater should return the repeating task.
  2210. By default this is the first task in a TODO sequence, or the previous state
  2211. in a TODO_TYP set. But you can specify another task here.
  2212. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2213. :group 'org-todo
  2214. :version "24.1"
  2215. :type '(choice (const :tag "Head of sequence" nil)
  2216. (string :tag "Specific state")))
  2217. (defcustom org-log-repeat 'time
  2218. "Non-nil means record moving through the DONE state when triggering repeat.
  2219. An auto-repeating task is immediately switched back to TODO when
  2220. marked DONE. If you are not logging state changes (by adding \"@\"
  2221. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2222. record a closing note, there will be no record of the task moving
  2223. through DONE. This variable forces taking a note anyway.
  2224. nil Don't force a record
  2225. time Record a time stamp
  2226. note Record a note
  2227. This option can also be set with on a per-file-basis with
  2228. #+STARTUP: logrepeat
  2229. #+STARTUP: lognoterepeat
  2230. #+STARTUP: nologrepeat
  2231. You can have local logging settings for a subtree by setting the LOGGING
  2232. property to one or more of these keywords."
  2233. :group 'org-todo
  2234. :group 'org-progress
  2235. :type '(choice
  2236. (const :tag "Don't force a record" nil)
  2237. (const :tag "Force recording the DONE state" time)
  2238. (const :tag "Force recording a note with the DONE state" note)))
  2239. (defgroup org-priorities nil
  2240. "Priorities in Org-mode."
  2241. :tag "Org Priorities"
  2242. :group 'org-todo)
  2243. (defcustom org-enable-priority-commands t
  2244. "Non-nil means priority commands are active.
  2245. When nil, these commands will be disabled, so that you never accidentally
  2246. set a priority."
  2247. :group 'org-priorities
  2248. :type 'boolean)
  2249. (defcustom org-highest-priority ?A
  2250. "The highest priority of TODO items. A character like ?A, ?B etc.
  2251. Must have a smaller ASCII number than `org-lowest-priority'."
  2252. :group 'org-priorities
  2253. :type 'character)
  2254. (defcustom org-lowest-priority ?C
  2255. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2256. Must have a larger ASCII number than `org-highest-priority'."
  2257. :group 'org-priorities
  2258. :type 'character)
  2259. (defcustom org-default-priority ?B
  2260. "The default priority of TODO items.
  2261. This is the priority an item gets if no explicit priority is given.
  2262. When starting to cycle on an empty priority the first step in the cycle
  2263. depends on `org-priority-start-cycle-with-default'. The resulting first
  2264. step priority must not exceed the range from `org-highest-priority' to
  2265. `org-lowest-priority' which means that `org-default-priority' has to be
  2266. in this range exclusive or inclusive the range boundaries. Else the
  2267. first step refuses to set the default and the second will fall back
  2268. to (depending on the command used) the highest or lowest priority."
  2269. :group 'org-priorities
  2270. :type 'character)
  2271. (defcustom org-priority-start-cycle-with-default t
  2272. "Non-nil means start with default priority when starting to cycle.
  2273. When this is nil, the first step in the cycle will be (depending on the
  2274. command used) one higher or lower than the default priority.
  2275. See also `org-default-priority'."
  2276. :group 'org-priorities
  2277. :type 'boolean)
  2278. (defcustom org-get-priority-function nil
  2279. "Function to extract the priority from a string.
  2280. The string is normally the headline. If this is nil Org computes the
  2281. priority from the priority cookie like [#A] in the headline. It returns
  2282. an integer, increasing by 1000 for each priority level.
  2283. The user can set a different function here, which should take a string
  2284. as an argument and return the numeric priority."
  2285. :group 'org-priorities
  2286. :version "24.1"
  2287. :type 'function)
  2288. (defgroup org-time nil
  2289. "Options concerning time stamps and deadlines in Org-mode."
  2290. :tag "Org Time"
  2291. :group 'org)
  2292. (defcustom org-insert-labeled-timestamps-at-point nil
  2293. "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
  2294. When nil, these labeled time stamps are forces into the second line of an
  2295. entry, just after the headline. When scheduling from the global TODO list,
  2296. the time stamp will always be forced into the second line."
  2297. :group 'org-time
  2298. :type 'boolean)
  2299. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  2300. "Formats for `format-time-string' which are used for time stamps.
  2301. It is not recommended to change this constant.")
  2302. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2303. "Number of minutes to round time stamps to.
  2304. These are two values, the first applies when first creating a time stamp.
  2305. The second applies when changing it with the commands `S-up' and `S-down'.
  2306. When changing the time stamp, this means that it will change in steps
  2307. of N minutes, as given by the second value.
  2308. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2309. numbers should be factors of 60, so for example 5, 10, 15.
  2310. When this is larger than 1, you can still force an exact time stamp by using
  2311. a double prefix argument to a time stamp command like `C-c .' or `C-c !',
  2312. and by using a prefix arg to `S-up/down' to specify the exact number
  2313. of minutes to shift."
  2314. :group 'org-time
  2315. :get #'(lambda (var) ; Make sure both elements are there
  2316. (if (integerp (default-value var))
  2317. (list (default-value var) 5)
  2318. (default-value var)))
  2319. :type '(list
  2320. (integer :tag "when inserting times")
  2321. (integer :tag "when modifying times")))
  2322. ;; Normalize old customizations of this variable.
  2323. (when (integerp org-time-stamp-rounding-minutes)
  2324. (setq org-time-stamp-rounding-minutes
  2325. (list org-time-stamp-rounding-minutes
  2326. org-time-stamp-rounding-minutes)))
  2327. (defcustom org-display-custom-times nil
  2328. "Non-nil means overlay custom formats over all time stamps.
  2329. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2330. To turn this on on a per-file basis, insert anywhere in the file:
  2331. #+STARTUP: customtime"
  2332. :group 'org-time
  2333. :set 'set-default
  2334. :type 'sexp)
  2335. (make-variable-buffer-local 'org-display-custom-times)
  2336. (defcustom org-time-stamp-custom-formats
  2337. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2338. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2339. These are overlaid over the default ISO format if the variable
  2340. `org-display-custom-times' is set. Time like %H:%M should be at the
  2341. end of the second format. The custom formats are also honored by export
  2342. commands, if custom time display is turned on at the time of export."
  2343. :group 'org-time
  2344. :type 'sexp)
  2345. (defun org-time-stamp-format (&optional long inactive)
  2346. "Get the right format for a time string."
  2347. (let ((f (if long (cdr org-time-stamp-formats)
  2348. (car org-time-stamp-formats))))
  2349. (if inactive
  2350. (concat "[" (substring f 1 -1) "]")
  2351. f)))
  2352. (defcustom org-time-clocksum-format "%d:%02d"
  2353. "The format string used when creating CLOCKSUM lines.
  2354. This is also used when org-mode generates a time duration."
  2355. :group 'org-time
  2356. :type 'string)
  2357. (defcustom org-time-clocksum-use-fractional nil
  2358. "If non-nil, \\[org-clock-display] uses fractional times.
  2359. org-mode generates a time duration."
  2360. :group 'org-time
  2361. :type 'boolean)
  2362. (defcustom org-time-clocksum-fractional-format "%.2f"
  2363. "The format string used when creating CLOCKSUM lines, or when
  2364. org-mode generates a time duration."
  2365. :group 'org-time
  2366. :type 'string)
  2367. (defcustom org-deadline-warning-days 14
  2368. "No. of days before expiration during which a deadline becomes active.
  2369. This variable governs the display in sparse trees and in the agenda.
  2370. When 0 or negative, it means use this number (the absolute value of it)
  2371. even if a deadline has a different individual lead time specified.
  2372. Custom commands can set this variable in the options section."
  2373. :group 'org-time
  2374. :group 'org-agenda-daily/weekly
  2375. :type 'integer)
  2376. (defcustom org-read-date-prefer-future t
  2377. "Non-nil means assume future for incomplete date input from user.
  2378. This affects the following situations:
  2379. 1. The user gives a month but not a year.
  2380. For example, if it is April and you enter \"feb 2\", this will be read
  2381. as Feb 2, *next* year. \"May 5\", however, will be this year.
  2382. 2. The user gives a day, but no month.
  2383. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2384. read this as the third of *next* month. However, if you enter \"17\",
  2385. it will be considered as *this* month.
  2386. If you set this variable to the symbol `time', then also the following
  2387. will work:
  2388. 3. If the user gives a time, but no day. If the time is before now,
  2389. to will be interpreted as tomorrow.
  2390. Currently none of this works for ISO week specifications.
  2391. When this option is nil, the current day, month and year will always be
  2392. used as defaults.
  2393. See also `org-agenda-jump-prefer-future'."
  2394. :group 'org-time
  2395. :type '(choice
  2396. (const :tag "Never" nil)
  2397. (const :tag "Check month and day" t)
  2398. (const :tag "Check month, day, and time" time)))
  2399. (defcustom org-agenda-jump-prefer-future 'org-read-date-prefer-future
  2400. "Should the agenda jump command prefer the future for incomplete dates?
  2401. The default is to do the same as configured in `org-read-date-prefer-future'.
  2402. But you can also set a deviating value here.
  2403. This may t or nil, or the symbol `org-read-date-prefer-future'."
  2404. :group 'org-agenda
  2405. :group 'org-time
  2406. :version "24.1"
  2407. :type '(choice
  2408. (const :tag "Use org-read-date-prefer-future"
  2409. org-read-date-prefer-future)
  2410. (const :tag "Never" nil)
  2411. (const :tag "Always" t)))
  2412. (defcustom org-read-date-force-compatible-dates t
  2413. "Should date/time prompt force dates that are guaranteed to work in Emacs?
  2414. Depending on the system Emacs is running on, certain dates cannot
  2415. be represented with the type used internally to represent time.
  2416. Dates between 1970-1-1 and 2038-1-1 can always be represented
  2417. correctly. Some systems allow for earlier dates, some for later,
  2418. some for both. One way to find out it to insert any date into an
  2419. Org buffer, putting the cursor on the year and hitting S-up and
  2420. S-down to test the range.
  2421. When this variable is set to t, the date/time prompt will not let
  2422. you specify dates outside the 1970-2037 range, so it is certain that
  2423. these dates will work in whatever version of Emacs you are
  2424. running, and also that you can move a file from one Emacs implementation
  2425. to another. WHenever Org is forcing the year for you, it will display
  2426. a message and beep.
  2427. When this variable is nil, Org will check if the date is
  2428. representable in the specific Emacs implementation you are using.
  2429. If not, it will force a year, usually the current year, and beep
  2430. to remind you. Currently this setting is not recommended because
  2431. the likelihood that you will open your Org files in an Emacs that
  2432. has limited date range is not negligible.
  2433. A workaround for this problem is to use diary sexp dates for time
  2434. stamps outside of this range."
  2435. :group 'org-time
  2436. :version "24.1"
  2437. :type 'boolean)
  2438. (defcustom org-read-date-display-live t
  2439. "Non-nil means display current interpretation of date prompt live.
  2440. This display will be in an overlay, in the minibuffer."
  2441. :group 'org-time
  2442. :type 'boolean)
  2443. (defcustom org-read-date-popup-calendar t
  2444. "Non-nil means pop up a calendar when prompting for a date.
  2445. In the calendar, the date can be selected with mouse-1. However, the
  2446. minibuffer will also be active, and you can simply enter the date as well.
  2447. When nil, only the minibuffer will be available."
  2448. :group 'org-time
  2449. :type 'boolean)
  2450. (if (fboundp 'defvaralias)
  2451. (defvaralias 'org-popup-calendar-for-date-prompt
  2452. 'org-read-date-popup-calendar))
  2453. (defcustom org-read-date-minibuffer-setup-hook nil
  2454. "Hook to be used to set up keys for the date/time interface.
  2455. Add key definitions to `minibuffer-local-map', which will be a temporary
  2456. copy."
  2457. :group 'org-time
  2458. :type 'hook)
  2459. (defcustom org-extend-today-until 0
  2460. "The hour when your day really ends. Must be an integer.
  2461. This has influence for the following applications:
  2462. - When switching the agenda to \"today\". It it is still earlier than
  2463. the time given here, the day recognized as TODAY is actually yesterday.
  2464. - When a date is read from the user and it is still before the time given
  2465. here, the current date and time will be assumed to be yesterday, 23:59.
  2466. Also, timestamps inserted in remember templates follow this rule.
  2467. IMPORTANT: This is a feature whose implementation is and likely will
  2468. remain incomplete. Really, it is only here because past midnight seems to
  2469. be the favorite working time of John Wiegley :-)"
  2470. :group 'org-time
  2471. :type 'integer)
  2472. (defcustom org-use-effective-time nil
  2473. "If non-nil, consider `org-extend-today-until' when creating timestamps.
  2474. For example, if `org-extend-today-until' is 8, and it's 4am, then the
  2475. \"effective time\" of any timestamps between midnight and 8am will be
  2476. 23:59 of the previous day."
  2477. :group 'org-time
  2478. :version "24.1"
  2479. :type 'boolean)
  2480. (defcustom org-edit-timestamp-down-means-later nil
  2481. "Non-nil means S-down will increase the time in a time stamp.
  2482. When nil, S-up will increase."
  2483. :group 'org-time
  2484. :type 'boolean)
  2485. (defcustom org-calendar-follow-timestamp-change t
  2486. "Non-nil means make the calendar window follow timestamp changes.
  2487. When a timestamp is modified and the calendar window is visible, it will be
  2488. moved to the new date."
  2489. :group 'org-time
  2490. :type 'boolean)
  2491. (defgroup org-tags nil
  2492. "Options concerning tags in Org-mode."
  2493. :tag "Org Tags"
  2494. :group 'org)
  2495. (defcustom org-tag-alist nil
  2496. "List of tags allowed in Org-mode files.
  2497. When this list is nil, Org-mode will base TAG input on what is already in the
  2498. buffer.
  2499. The value of this variable is an alist, the car of each entry must be a
  2500. keyword as a string, the cdr may be a character that is used to select
  2501. that tag through the fast-tag-selection interface.
  2502. See the manual for details."
  2503. :group 'org-tags
  2504. :type '(repeat
  2505. (choice
  2506. (cons (string :tag "Tag name")
  2507. (character :tag "Access char"))
  2508. (list :tag "Start radio group"
  2509. (const :startgroup)
  2510. (option (string :tag "Group description")))
  2511. (list :tag "End radio group"
  2512. (const :endgroup)
  2513. (option (string :tag "Group description")))
  2514. (const :tag "New line" (:newline)))))
  2515. (defcustom org-tag-persistent-alist nil
  2516. "List of tags that will always appear in all Org-mode files.
  2517. This is in addition to any in buffer settings or customizations
  2518. of `org-tag-alist'.
  2519. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  2520. The value of this variable is an alist, the car of each entry must be a
  2521. keyword as a string, the cdr may be a character that is used to select
  2522. that tag through the fast-tag-selection interface.
  2523. See the manual for details.
  2524. To disable these tags on a per-file basis, insert anywhere in the file:
  2525. #+STARTUP: noptag"
  2526. :group 'org-tags
  2527. :type '(repeat
  2528. (choice
  2529. (cons (string :tag "Tag name")
  2530. (character :tag "Access char"))
  2531. (const :tag "Start radio group" (:startgroup))
  2532. (const :tag "End radio group" (:endgroup))
  2533. (const :tag "New line" (:newline)))))
  2534. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  2535. "If non-nil, always offer completion for all tags of all agenda files.
  2536. Instead of customizing this variable directly, you might want to
  2537. set it locally for capture buffers, because there no list of
  2538. tags in that file can be created dynamically (there are none).
  2539. (add-hook 'org-capture-mode-hook
  2540. (lambda ()
  2541. (set (make-local-variable
  2542. 'org-complete-tags-always-offer-all-agenda-tags)
  2543. t)))"
  2544. :group 'org-tags
  2545. :version "24.1"
  2546. :type 'boolean)
  2547. (defvar org-file-tags nil
  2548. "List of tags that can be inherited by all entries in the file.
  2549. The tags will be inherited if the variable `org-use-tag-inheritance'
  2550. says they should be.
  2551. This variable is populated from #+FILETAGS lines.")
  2552. (defcustom org-use-fast-tag-selection 'auto
  2553. "Non-nil means use fast tag selection scheme.
  2554. This is a special interface to select and deselect tags with single keys.
  2555. When nil, fast selection is never used.
  2556. When the symbol `auto', fast selection is used if and only if selection
  2557. characters for tags have been configured, either through the variable
  2558. `org-tag-alist' or through a #+TAGS line in the buffer.
  2559. When t, fast selection is always used and selection keys are assigned
  2560. automatically if necessary."
  2561. :group 'org-tags
  2562. :type '(choice
  2563. (const :tag "Always" t)
  2564. (const :tag "Never" nil)
  2565. (const :tag "When selection characters are configured" 'auto)))
  2566. (defcustom org-fast-tag-selection-single-key nil
  2567. "Non-nil means fast tag selection exits after first change.
  2568. When nil, you have to press RET to exit it.
  2569. During fast tag selection, you can toggle this flag with `C-c'.
  2570. This variable can also have the value `expert'. In this case, the window
  2571. displaying the tags menu is not even shown, until you press C-c again."
  2572. :group 'org-tags
  2573. :type '(choice
  2574. (const :tag "No" nil)
  2575. (const :tag "Yes" t)
  2576. (const :tag "Expert" expert)))
  2577. (defvar org-fast-tag-selection-include-todo nil
  2578. "Non-nil means fast tags selection interface will also offer TODO states.
  2579. This is an undocumented feature, you should not rely on it.")
  2580. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2581. "The column to which tags should be indented in a headline.
  2582. If this number is positive, it specifies the column. If it is negative,
  2583. it means that the tags should be flushright to that column. For example,
  2584. -80 works well for a normal 80 character screen.
  2585. When 0, place tags directly after headline text, with only one space in
  2586. between."
  2587. :group 'org-tags
  2588. :type 'integer)
  2589. (defcustom org-auto-align-tags t
  2590. "Non-nil keeps tags aligned when modifying headlines.
  2591. Some operations (i.e. demoting) change the length of a headline and
  2592. therefore shift the tags around. With this option turned on, after
  2593. each such operation the tags are again aligned to `org-tags-column'."
  2594. :group 'org-tags
  2595. :type 'boolean)
  2596. (defcustom org-use-tag-inheritance t
  2597. "Non-nil means tags in levels apply also for sublevels.
  2598. When nil, only the tags directly given in a specific line apply there.
  2599. This may also be a list of tags that should be inherited, or a regexp that
  2600. matches tags that should be inherited. Additional control is possible
  2601. with the variable `org-tags-exclude-from-inheritance' which gives an
  2602. explicit list of tags to be excluded from inheritance., even if the value of
  2603. `org-use-tag-inheritance' would select it for inheritance.
  2604. If this option is t, a match early-on in a tree can lead to a large
  2605. number of matches in the subtree when constructing the agenda or creating
  2606. a sparse tree. If you only want to see the first match in a tree during
  2607. a search, check out the variable `org-tags-match-list-sublevels'."
  2608. :group 'org-tags
  2609. :type '(choice
  2610. (const :tag "Not" nil)
  2611. (const :tag "Always" t)
  2612. (repeat :tag "Specific tags" (string :tag "Tag"))
  2613. (regexp :tag "Tags matched by regexp")))
  2614. (defcustom org-tags-exclude-from-inheritance nil
  2615. "List of tags that should never be inherited.
  2616. This is a way to exclude a few tags from inheritance. For way to do
  2617. the opposite, to actively allow inheritance for selected tags,
  2618. see the variable `org-use-tag-inheritance'."
  2619. :group 'org-tags
  2620. :type '(repeat (string :tag "Tag")))
  2621. (defun org-tag-inherit-p (tag)
  2622. "Check if TAG is one that should be inherited."
  2623. (cond
  2624. ((member tag org-tags-exclude-from-inheritance) nil)
  2625. ((eq org-use-tag-inheritance t) t)
  2626. ((not org-use-tag-inheritance) nil)
  2627. ((stringp org-use-tag-inheritance)
  2628. (string-match org-use-tag-inheritance tag))
  2629. ((listp org-use-tag-inheritance)
  2630. (member tag org-use-tag-inheritance))
  2631. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2632. (defcustom org-tags-match-list-sublevels t
  2633. "Non-nil means list also sublevels of headlines matching a search.
  2634. This variable applies to tags/property searches, and also to stuck
  2635. projects because this search is based on a tags match as well.
  2636. When set to the symbol `indented', sublevels are indented with
  2637. leading dots.
  2638. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2639. the sublevels of a headline matching a tag search often also match
  2640. the same search. Listing all of them can create very long lists.
  2641. Setting this variable to nil causes subtrees of a match to be skipped.
  2642. This variable is semi-obsolete and probably should always be true. It
  2643. is better to limit inheritance to certain tags using the variables
  2644. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2645. :group 'org-tags
  2646. :type '(choice
  2647. (const :tag "No, don't list them" nil)
  2648. (const :tag "Yes, do list them" t)
  2649. (const :tag "List them, indented with leading dots" indented)))
  2650. (defcustom org-tags-sort-function nil
  2651. "When set, tags are sorted using this function as a comparator."
  2652. :group 'org-tags
  2653. :type '(choice
  2654. (const :tag "No sorting" nil)
  2655. (const :tag "Alphabetical" string<)
  2656. (const :tag "Reverse alphabetical" string>)
  2657. (function :tag "Custom function" nil)))
  2658. (defvar org-tags-history nil
  2659. "History of minibuffer reads for tags.")
  2660. (defvar org-last-tags-completion-table nil
  2661. "The last used completion table for tags.")
  2662. (defvar org-after-tags-change-hook nil
  2663. "Hook that is run after the tags in a line have changed.")
  2664. (defgroup org-properties nil
  2665. "Options concerning properties in Org-mode."
  2666. :tag "Org Properties"
  2667. :group 'org)
  2668. (defcustom org-property-format "%-10s %s"
  2669. "How property key/value pairs should be formatted by `indent-line'.
  2670. When `indent-line' hits a property definition, it will format the line
  2671. according to this format, mainly to make sure that the values are
  2672. lined-up with respect to each other."
  2673. :group 'org-properties
  2674. :type 'string)
  2675. (defcustom org-properties-postprocess-alist nil
  2676. "Alist of properties and functions to adjust inserted values.
  2677. Elements of this alist must be of the form
  2678. ([string] [function])
  2679. where [string] must be a property name and [function] must be a
  2680. lambda expression: this lambda expression must take one argument,
  2681. the value to adjust, and return the new value as a string.
  2682. For example, this element will allow the property \"Remaining\"
  2683. to be updated wrt the relation between the \"Effort\" property
  2684. and the clock summary:
  2685. ((\"Remaining\" (lambda(value)
  2686. (let ((clocksum (org-clock-sum-current-item))
  2687. (effort (org-duration-string-to-minutes
  2688. (org-entry-get (point) \"Effort\"))))
  2689. (org-minutes-to-hh:mm-string (- effort clocksum))))))"
  2690. :group 'org-properties
  2691. :version "24.1"
  2692. :type '(alist :key-type (string :tag "Property")
  2693. :value-type (function :tag "Function")))
  2694. (defcustom org-use-property-inheritance nil
  2695. "Non-nil means properties apply also for sublevels.
  2696. This setting is chiefly used during property searches. Turning it on can
  2697. cause significant overhead when doing a search, which is why it is not
  2698. on by default.
  2699. When nil, only the properties directly given in the current entry count.
  2700. When t, every property is inherited. The value may also be a list of
  2701. properties that should have inheritance, or a regular expression matching
  2702. properties that should be inherited.
  2703. However, note that some special properties use inheritance under special
  2704. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2705. and the properties ending in \"_ALL\" when they are used as descriptor
  2706. for valid values of a property.
  2707. Note for programmers:
  2708. When querying an entry with `org-entry-get', you can control if inheritance
  2709. should be used. By default, `org-entry-get' looks only at the local
  2710. properties. You can request inheritance by setting the inherit argument
  2711. to t (to force inheritance) or to `selective' (to respect the setting
  2712. in this variable)."
  2713. :group 'org-properties
  2714. :type '(choice
  2715. (const :tag "Not" nil)
  2716. (const :tag "Always" t)
  2717. (repeat :tag "Specific properties" (string :tag "Property"))
  2718. (regexp :tag "Properties matched by regexp")))
  2719. (defun org-property-inherit-p (property)
  2720. "Check if PROPERTY is one that should be inherited."
  2721. (cond
  2722. ((eq org-use-property-inheritance t) t)
  2723. ((not org-use-property-inheritance) nil)
  2724. ((stringp org-use-property-inheritance)
  2725. (string-match org-use-property-inheritance property))
  2726. ((listp org-use-property-inheritance)
  2727. (member property org-use-property-inheritance))
  2728. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2729. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2730. "The default column format, if no other format has been defined.
  2731. This variable can be set on the per-file basis by inserting a line
  2732. #+COLUMNS: %25ITEM ....."
  2733. :group 'org-properties
  2734. :type 'string)
  2735. (defcustom org-columns-ellipses ".."
  2736. "The ellipses to be used when a field in column view is truncated.
  2737. When this is the empty string, as many characters as possible are shown,
  2738. but then there will be no visual indication that the field has been truncated.
  2739. When this is a string of length N, the last N characters of a truncated
  2740. field are replaced by this string. If the column is narrower than the
  2741. ellipses string, only part of the ellipses string will be shown."
  2742. :group 'org-properties
  2743. :type 'string)
  2744. (defcustom org-columns-modify-value-for-display-function nil
  2745. "Function that modifies values for display in column view.
  2746. For example, it can be used to cut out a certain part from a time stamp.
  2747. The function must take 2 arguments:
  2748. column-title The title of the column (*not* the property name)
  2749. value The value that should be modified.
  2750. The function should return the value that should be displayed,
  2751. or nil if the normal value should be used."
  2752. :group 'org-properties
  2753. :type 'function)
  2754. (defcustom org-effort-property "Effort"
  2755. "The property that is being used to keep track of effort estimates.
  2756. Effort estimates given in this property need to have the format H:MM."
  2757. :group 'org-properties
  2758. :group 'org-progress
  2759. :type '(string :tag "Property"))
  2760. (defconst org-global-properties-fixed
  2761. '(("VISIBILITY_ALL" . "folded children content all")
  2762. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  2763. "List of property/value pairs that can be inherited by any entry.
  2764. These are fixed values, for the preset properties. The user variable
  2765. that can be used to add to this list is `org-global-properties'.
  2766. The entries in this list are cons cells where the car is a property
  2767. name and cdr is a string with the value. If the value represents
  2768. multiple items like an \"_ALL\" property, separate the items by
  2769. spaces.")
  2770. (defcustom org-global-properties nil
  2771. "List of property/value pairs that can be inherited by any entry.
  2772. This list will be combined with the constant `org-global-properties-fixed'.
  2773. The entries in this list are cons cells where the car is a property
  2774. name and cdr is a string with the value.
  2775. You can set buffer-local values for the same purpose in the variable
  2776. `org-file-properties' this by adding lines like
  2777. #+PROPERTY: NAME VALUE"
  2778. :group 'org-properties
  2779. :type '(repeat
  2780. (cons (string :tag "Property")
  2781. (string :tag "Value"))))
  2782. (defvar org-file-properties nil
  2783. "List of property/value pairs that can be inherited by any entry.
  2784. Valid for the current buffer.
  2785. This variable is populated from #+PROPERTY lines.")
  2786. (make-variable-buffer-local 'org-file-properties)
  2787. (defgroup org-agenda nil
  2788. "Options concerning agenda views in Org-mode."
  2789. :tag "Org Agenda"
  2790. :group 'org)
  2791. (defvar org-category nil
  2792. "Variable used by org files to set a category for agenda display.
  2793. Such files should use a file variable to set it, for example
  2794. # -*- mode: org; org-category: \"ELisp\"
  2795. or contain a special line
  2796. #+CATEGORY: ELisp
  2797. If the file does not specify a category, then file's base name
  2798. is used instead.")
  2799. (make-variable-buffer-local 'org-category)
  2800. (put 'org-category 'safe-local-variable #'(lambda (x) (or (symbolp x) (stringp x))))
  2801. (defcustom org-agenda-files nil
  2802. "The files to be used for agenda display.
  2803. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2804. \\[org-remove-file]. You can also use customize to edit the list.
  2805. If an entry is a directory, all files in that directory that are matched by
  2806. `org-agenda-file-regexp' will be part of the file list.
  2807. If the value of the variable is not a list but a single file name, then
  2808. the list of agenda files is actually stored and maintained in that file, one
  2809. agenda file per line. In this file paths can be given relative to
  2810. `org-directory'. Tilde expansion and environment variable substitution
  2811. are also made."
  2812. :group 'org-agenda
  2813. :type '(choice
  2814. (repeat :tag "List of files and directories" file)
  2815. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2816. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2817. "Regular expression to match files for `org-agenda-files'.
  2818. If any element in the list in that variable contains a directory instead
  2819. of a normal file, all files in that directory that are matched by this
  2820. regular expression will be included."
  2821. :group 'org-agenda
  2822. :type 'regexp)
  2823. (defcustom org-agenda-text-search-extra-files nil
  2824. "List of extra files to be searched by text search commands.
  2825. These files will be search in addition to the agenda files by the
  2826. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2827. Note that these files will only be searched for text search commands,
  2828. not for the other agenda views like todo lists, tag searches or the weekly
  2829. agenda. This variable is intended to list notes and possibly archive files
  2830. that should also be searched by these two commands.
  2831. In fact, if the first element in the list is the symbol `agenda-archives',
  2832. than all archive files of all agenda files will be added to the search
  2833. scope."
  2834. :group 'org-agenda
  2835. :type '(set :greedy t
  2836. (const :tag "Agenda Archives" agenda-archives)
  2837. (repeat :inline t (file))))
  2838. (if (fboundp 'defvaralias)
  2839. (defvaralias 'org-agenda-multi-occur-extra-files
  2840. 'org-agenda-text-search-extra-files))
  2841. (defcustom org-agenda-skip-unavailable-files nil
  2842. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2843. A nil value means to remove them, after a query, from the list."
  2844. :group 'org-agenda
  2845. :type 'boolean)
  2846. (defcustom org-calendar-to-agenda-key [?c]
  2847. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2848. The command `org-calendar-goto-agenda' will be bound to this key. The
  2849. default is the character `c' because then `c' can be used to switch back and
  2850. forth between agenda and calendar."
  2851. :group 'org-agenda
  2852. :type 'sexp)
  2853. (defcustom org-calendar-agenda-action-key [?k]
  2854. "The key to be installed in `calendar-mode-map' for agenda-action.
  2855. The command `org-agenda-action' will be bound to this key. The
  2856. default is the character `k' because we use the same key in the agenda."
  2857. :group 'org-agenda
  2858. :type 'sexp)
  2859. (defcustom org-calendar-insert-diary-entry-key [?i]
  2860. "The key to be installed in `calendar-mode-map' for adding diary entries.
  2861. This option is irrelevant until `org-agenda-diary-file' has been configured
  2862. to point to an Org-mode file. When that is the case, the command
  2863. `org-agenda-diary-entry' will be bound to the key given here, by default
  2864. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  2865. if you want to continue doing this, you need to change this to a different
  2866. key."
  2867. :group 'org-agenda
  2868. :type 'sexp)
  2869. (defcustom org-agenda-diary-file 'diary-file
  2870. "File to which to add new entries with the `i' key in agenda and calendar.
  2871. When this is the symbol `diary-file', the functionality in the Emacs
  2872. calendar will be used to add entries to the `diary-file'. But when this
  2873. points to a file, `org-agenda-diary-entry' will be used instead."
  2874. :group 'org-agenda
  2875. :type '(choice
  2876. (const :tag "The standard Emacs diary file" diary-file)
  2877. (file :tag "Special Org file diary entries")))
  2878. (eval-after-load "calendar"
  2879. '(progn
  2880. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  2881. 'org-calendar-goto-agenda)
  2882. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  2883. 'org-agenda-action)
  2884. (add-hook 'calendar-mode-hook
  2885. (lambda ()
  2886. (unless (eq org-agenda-diary-file 'diary-file)
  2887. (define-key calendar-mode-map
  2888. org-calendar-insert-diary-entry-key
  2889. 'org-agenda-diary-entry))))))
  2890. (defgroup org-latex nil
  2891. "Options for embedding LaTeX code into Org-mode."
  2892. :tag "Org LaTeX"
  2893. :group 'org)
  2894. (defcustom org-format-latex-options
  2895. '(:foreground default :background default :scale 1.0
  2896. :html-foreground "Black" :html-background "Transparent"
  2897. :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  2898. "Options for creating images from LaTeX fragments.
  2899. This is a property list with the following properties:
  2900. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  2901. `default' means use the foreground of the default face.
  2902. :background the background color, or \"Transparent\".
  2903. `default' means use the background of the default face.
  2904. :scale a scaling factor for the size of the images, to get more pixels
  2905. :html-foreground, :html-background, :html-scale
  2906. the same numbers for HTML export.
  2907. :matchers a list indicating which matchers should be used to
  2908. find LaTeX fragments. Valid members of this list are:
  2909. \"begin\" find environments
  2910. \"$1\" find single characters surrounded by $.$
  2911. \"$\" find math expressions surrounded by $...$
  2912. \"$$\" find math expressions surrounded by $$....$$
  2913. \"\\(\" find math expressions surrounded by \\(...\\)
  2914. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2915. :group 'org-latex
  2916. :type 'plist)
  2917. (defcustom org-format-latex-signal-error t
  2918. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  2919. When nil, just push out a message."
  2920. :group 'org-latex
  2921. :version "24.1"
  2922. :type 'boolean)
  2923. (defcustom org-latex-to-mathml-jar-file nil
  2924. "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
  2925. Use this to specify additional executable file say a jar file.
  2926. When using MathToWeb as the converter, specify the full-path to
  2927. your mathtoweb.jar file."
  2928. :group 'org-latex
  2929. :version "24.1"
  2930. :type '(choice
  2931. (const :tag "None" nil)
  2932. (file :tag "JAR file" :must-match t)))
  2933. (defcustom org-latex-to-mathml-convert-command nil
  2934. "Command to convert LaTeX fragments to MathML.
  2935. Replace format-specifiers in the command as noted below and use
  2936. `shell-command' to convert LaTeX to MathML.
  2937. %j: Executable file in fully expanded form as specified by
  2938. `org-latex-to-mathml-jar-file'.
  2939. %I: Input LaTeX file in fully expanded form
  2940. %o: Output MathML file
  2941. This command is used by `org-create-math-formula'.
  2942. When using MathToWeb as the converter, set this to
  2943. \"java -jar %j -unicode -force -df %o %I\"."
  2944. :group 'org-latex
  2945. :version "24.1"
  2946. :type '(choice
  2947. (const :tag "None" nil)
  2948. (string :tag "\nShell command")))
  2949. (defcustom org-latex-create-formula-image-program 'dvipng
  2950. "Program to convert LaTeX fragments with.
  2951. dvipng Process the LaTeX fragments to dvi file, then convert
  2952. dvi files to png files using dvipng.
  2953. This will also include processing of non-math environments.
  2954. imagemagick Convert the LaTeX fragments to pdf files and use imagemagick
  2955. to convert pdf files to png files"
  2956. :group 'org-latex
  2957. :version "24.1"
  2958. :type '(choice
  2959. (const :tag "dvipng" dvipng)
  2960. (const :tag "imagemagick" imagemagick)))
  2961. (defun org-format-latex-mathml-available-p ()
  2962. "Return t if `org-latex-to-mathml-convert-command' is usable."
  2963. (save-match-data
  2964. (when (and (boundp 'org-latex-to-mathml-convert-command)
  2965. org-latex-to-mathml-convert-command)
  2966. (let ((executable (car (split-string
  2967. org-latex-to-mathml-convert-command))))
  2968. (when (executable-find executable)
  2969. (if (string-match
  2970. "%j" org-latex-to-mathml-convert-command)
  2971. (file-readable-p org-latex-to-mathml-jar-file)
  2972. t))))))
  2973. (defcustom org-format-latex-header "\\documentclass{article}
  2974. \\usepackage[usenames]{color}
  2975. \\usepackage{amsmath}
  2976. \\usepackage[mathscr]{eucal}
  2977. \\pagestyle{empty} % do not remove
  2978. \[PACKAGES]
  2979. \[DEFAULT-PACKAGES]
  2980. % The settings below are copied from fullpage.sty
  2981. \\setlength{\\textwidth}{\\paperwidth}
  2982. \\addtolength{\\textwidth}{-3cm}
  2983. \\setlength{\\oddsidemargin}{1.5cm}
  2984. \\addtolength{\\oddsidemargin}{-2.54cm}
  2985. \\setlength{\\evensidemargin}{\\oddsidemargin}
  2986. \\setlength{\\textheight}{\\paperheight}
  2987. \\addtolength{\\textheight}{-\\headheight}
  2988. \\addtolength{\\textheight}{-\\headsep}
  2989. \\addtolength{\\textheight}{-\\footskip}
  2990. \\addtolength{\\textheight}{-3cm}
  2991. \\setlength{\\topmargin}{1.5cm}
  2992. \\addtolength{\\topmargin}{-2.54cm}"
  2993. "The document header used for processing LaTeX fragments.
  2994. It is imperative that this header make sure that no page number
  2995. appears on the page. The package defined in the variables
  2996. `org-export-latex-default-packages-alist' and `org-export-latex-packages-alist'
  2997. will either replace the placeholder \"[PACKAGES]\" in this header, or they
  2998. will be appended."
  2999. :group 'org-latex
  3000. :type 'string)
  3001. (defvar org-format-latex-header-extra nil)
  3002. (defun org-set-packages-alist (var val)
  3003. "Set the packages alist and make sure it has 3 elements per entry."
  3004. (set var (mapcar (lambda (x)
  3005. (if (and (consp x) (= (length x) 2))
  3006. (list (car x) (nth 1 x) t)
  3007. x))
  3008. val)))
  3009. (defun org-get-packages-alist (var)
  3010. "Get the packages alist and make sure it has 3 elements per entry."
  3011. (mapcar (lambda (x)
  3012. (if (and (consp x) (= (length x) 2))
  3013. (list (car x) (nth 1 x) t)
  3014. x))
  3015. (default-value var)))
  3016. ;; The following variables are defined here because is it also used
  3017. ;; when formatting latex fragments. Originally it was part of the
  3018. ;; LaTeX exporter, which is why the name includes "export".
  3019. (defcustom org-export-latex-default-packages-alist
  3020. '(("AUTO" "inputenc" t)
  3021. ("T1" "fontenc" t)
  3022. ("" "fixltx2e" nil)
  3023. ("" "graphicx" t)
  3024. ("" "longtable" nil)
  3025. ("" "float" nil)
  3026. ("" "wrapfig" nil)
  3027. ("" "soul" t)
  3028. ("" "textcomp" t)
  3029. ("" "marvosym" t)
  3030. ("" "wasysym" t)
  3031. ("" "latexsym" t)
  3032. ("" "amssymb" t)
  3033. ("" "hyperref" nil)
  3034. "\\tolerance=1000"
  3035. )
  3036. "Alist of default packages to be inserted in the header.
  3037. Change this only if one of the packages here causes an incompatibility
  3038. with another package you are using.
  3039. The packages in this list are needed by one part or another of Org-mode
  3040. to function properly.
  3041. - inputenc, fontenc: for basic font and character selection
  3042. - textcomp, marvosymb, wasysym, latexsym, amssym: for various symbols used
  3043. for interpreting the entities in `org-entities'. You can skip some of these
  3044. packages if you don't use any of the symbols in it.
  3045. - graphicx: for including images
  3046. - float, wrapfig: for figure placement
  3047. - longtable: for long tables
  3048. - hyperref: for cross references
  3049. Therefore you should not modify this variable unless you know what you
  3050. are doing. The one reason to change it anyway is that you might be loading
  3051. some other package that conflicts with one of the default packages.
  3052. Each cell is of the format \( \"options\" \"package\" snippet-flag\).
  3053. If SNIPPET-FLAG is t, the package also needs to be included when
  3054. compiling LaTeX snippets into images for inclusion into HTML."
  3055. :group 'org-export-latex
  3056. :set 'org-set-packages-alist
  3057. :get 'org-get-packages-alist
  3058. :version "24.1"
  3059. :type '(repeat
  3060. (choice
  3061. (list :tag "options/package pair"
  3062. (string :tag "options")
  3063. (string :tag "package")
  3064. (boolean :tag "Snippet"))
  3065. (string :tag "A line of LaTeX"))))
  3066. (defcustom org-export-latex-packages-alist nil
  3067. "Alist of packages to be inserted in every LaTeX header.
  3068. These will be inserted after `org-export-latex-default-packages-alist'.
  3069. Each cell is of the format \( \"options\" \"package\" snippet-flag \).
  3070. SNIPPET-FLAG, when t, indicates that this package is also needed when
  3071. turning LaTeX snippets into images for inclusion into HTML.
  3072. Make sure that you only list packages here which:
  3073. - you want in every file
  3074. - do not conflict with the default packages in
  3075. `org-export-latex-default-packages-alist'
  3076. - do not conflict with the setup in `org-format-latex-header'."
  3077. :group 'org-export-latex
  3078. :set 'org-set-packages-alist
  3079. :get 'org-get-packages-alist
  3080. :type '(repeat
  3081. (choice
  3082. (list :tag "options/package pair"
  3083. (string :tag "options")
  3084. (string :tag "package")
  3085. (boolean :tag "Snippet"))
  3086. (string :tag "A line of LaTeX"))))
  3087. (defgroup org-appearance nil
  3088. "Settings for Org-mode appearance."
  3089. :tag "Org Appearance"
  3090. :group 'org)
  3091. (defcustom org-level-color-stars-only nil
  3092. "Non-nil means fontify only the stars in each headline.
  3093. When nil, the entire headline is fontified.
  3094. Changing it requires restart of `font-lock-mode' to become effective
  3095. also in regions already fontified."
  3096. :group 'org-appearance
  3097. :type 'boolean)
  3098. (defcustom org-hide-leading-stars nil
  3099. "Non-nil means hide the first N-1 stars in a headline.
  3100. This works by using the face `org-hide' for these stars. This
  3101. face is white for a light background, and black for a dark
  3102. background. You may have to customize the face `org-hide' to
  3103. make this work.
  3104. Changing it requires restart of `font-lock-mode' to become effective
  3105. also in regions already fontified.
  3106. You may also set this on a per-file basis by adding one of the following
  3107. lines to the buffer:
  3108. #+STARTUP: hidestars
  3109. #+STARTUP: showstars"
  3110. :group 'org-appearance
  3111. :type 'boolean)
  3112. (defcustom org-hidden-keywords nil
  3113. "List of symbols corresponding to keywords to be hidden the org buffer.
  3114. For example, a value '(title) for this list will make the document's title
  3115. appear in the buffer without the initial #+TITLE: keyword."
  3116. :group 'org-appearance
  3117. :version "24.1"
  3118. :type '(set (const :tag "#+AUTHOR" author)
  3119. (const :tag "#+DATE" date)
  3120. (const :tag "#+EMAIL" email)
  3121. (const :tag "#+TITLE" title)))
  3122. (defcustom org-fontify-done-headline nil
  3123. "Non-nil means change the face of a headline if it is marked DONE.
  3124. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3125. When this is non-nil, the headline after the keyword is set to the
  3126. `org-headline-done' as an additional indication."
  3127. :group 'org-appearance
  3128. :type 'boolean)
  3129. (defcustom org-fontify-emphasized-text t
  3130. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3131. Changing this variable requires a restart of Emacs to take effect."
  3132. :group 'org-appearance
  3133. :type 'boolean)
  3134. (defcustom org-fontify-whole-heading-line nil
  3135. "Non-nil means fontify the whole line for headings.
  3136. This is useful when setting a background color for the
  3137. org-level-* faces."
  3138. :group 'org-appearance
  3139. :type 'boolean)
  3140. (defcustom org-highlight-latex-fragments-and-specials nil
  3141. "Non-nil means fontify what is treated specially by the exporters."
  3142. :group 'org-appearance
  3143. :type 'boolean)
  3144. (defcustom org-hide-emphasis-markers nil
  3145. "Non-nil mean font-lock should hide the emphasis marker characters."
  3146. :group 'org-appearance
  3147. :type 'boolean)
  3148. (defcustom org-pretty-entities nil
  3149. "Non-nil means show entities as UTF8 characters.
  3150. When nil, the \\name form remains in the buffer."
  3151. :group 'org-appearance
  3152. :version "24.1"
  3153. :type 'boolean)
  3154. (defcustom org-pretty-entities-include-sub-superscripts t
  3155. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  3156. :group 'org-appearance
  3157. :version "24.1"
  3158. :type 'boolean)
  3159. (defvar org-emph-re nil
  3160. "Regular expression for matching emphasis.
  3161. After a match, the match groups contain these elements:
  3162. 0 The match of the full regular expression, including the characters
  3163. before and after the proper match
  3164. 1 The character before the proper match, or empty at beginning of line
  3165. 2 The proper match, including the leading and trailing markers
  3166. 3 The leading marker like * or /, indicating the type of highlighting
  3167. 4 The text between the emphasis markers, not including the markers
  3168. 5 The character after the match, empty at the end of a line")
  3169. (defvar org-verbatim-re nil
  3170. "Regular expression for matching verbatim text.")
  3171. (defvar org-emphasis-regexp-components) ; defined just below
  3172. (defvar org-emphasis-alist) ; defined just below
  3173. (defun org-set-emph-re (var val)
  3174. "Set variable and compute the emphasis regular expression."
  3175. (set var val)
  3176. (when (and (boundp 'org-emphasis-alist)
  3177. (boundp 'org-emphasis-regexp-components)
  3178. org-emphasis-alist org-emphasis-regexp-components)
  3179. (let* ((e org-emphasis-regexp-components)
  3180. (pre (car e))
  3181. (post (nth 1 e))
  3182. (border (nth 2 e))
  3183. (body (nth 3 e))
  3184. (nl (nth 4 e))
  3185. (body1 (concat body "*?"))
  3186. (markers (mapconcat 'car org-emphasis-alist ""))
  3187. (vmarkers (mapconcat
  3188. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  3189. org-emphasis-alist "")))
  3190. ;; make sure special characters appear at the right position in the class
  3191. (if (string-match "\\^" markers)
  3192. (setq markers (concat (replace-match "" t t markers) "^")))
  3193. (if (string-match "-" markers)
  3194. (setq markers (concat (replace-match "" t t markers) "-")))
  3195. (if (string-match "\\^" vmarkers)
  3196. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3197. (if (string-match "-" vmarkers)
  3198. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3199. (if (> nl 0)
  3200. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3201. (int-to-string nl) "\\}")))
  3202. ;; Make the regexp
  3203. (setq org-emph-re
  3204. (concat "\\([" pre "]\\|^\\)"
  3205. "\\("
  3206. "\\([" markers "]\\)"
  3207. "\\("
  3208. "[^" border "]\\|"
  3209. "[^" border "]"
  3210. body1
  3211. "[^" border "]"
  3212. "\\)"
  3213. "\\3\\)"
  3214. "\\([" post "]\\|$\\)"))
  3215. (setq org-verbatim-re
  3216. (concat "\\([" pre "]\\|^\\)"
  3217. "\\("
  3218. "\\([" vmarkers "]\\)"
  3219. "\\("
  3220. "[^" border "]\\|"
  3221. "[^" border "]"
  3222. body1
  3223. "[^" border "]"
  3224. "\\)"
  3225. "\\3\\)"
  3226. "\\([" post "]\\|$\\)")))))
  3227. (defcustom org-emphasis-regexp-components
  3228. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  3229. "Components used to build the regular expression for emphasis.
  3230. This is a list with five entries. Terminology: In an emphasis string
  3231. like \" *strong word* \", we call the initial space PREMATCH, the final
  3232. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3233. and \"trong wor\" is the body. The different components in this variable
  3234. specify what is allowed/forbidden in each part:
  3235. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3236. post Chars allowed as postmatch. End of line will be allowed too.
  3237. border The chars *forbidden* as border characters.
  3238. body-regexp A regexp like \".\" to match a body character. Don't use
  3239. non-shy groups here, and don't allow newline here.
  3240. newline The maximum number of newlines allowed in an emphasis exp.
  3241. Use customize to modify this, or restart Emacs after changing it."
  3242. :group 'org-appearance
  3243. :set 'org-set-emph-re
  3244. :type '(list
  3245. (sexp :tag "Allowed chars in pre ")
  3246. (sexp :tag "Allowed chars in post ")
  3247. (sexp :tag "Forbidden chars in border ")
  3248. (sexp :tag "Regexp for body ")
  3249. (integer :tag "number of newlines allowed")
  3250. (option (boolean :tag "Please ignore this button"))))
  3251. (defcustom org-emphasis-alist
  3252. `(("*" bold "<b>" "</b>")
  3253. ("/" italic "<i>" "</i>")
  3254. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  3255. ("=" org-code "<code>" "</code>" verbatim)
  3256. ("~" org-verbatim "<code>" "</code>" verbatim)
  3257. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  3258. "<del>" "</del>")
  3259. )
  3260. "Special syntax for emphasized text.
  3261. Text starting and ending with a special character will be emphasized, for
  3262. example *bold*, _underlined_ and /italic/. This variable sets the marker
  3263. characters, the face to be used by font-lock for highlighting in Org-mode
  3264. Emacs buffers, and the HTML tags to be used for this.
  3265. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  3266. For DocBook export, see the variable `org-export-docbook-emphasis-alist'.
  3267. Use customize to modify this, or restart Emacs after changing it."
  3268. :group 'org-appearance
  3269. :set 'org-set-emph-re
  3270. :type '(repeat
  3271. (list
  3272. (string :tag "Marker character")
  3273. (choice
  3274. (face :tag "Font-lock-face")
  3275. (plist :tag "Face property list"))
  3276. (string :tag "HTML start tag")
  3277. (string :tag "HTML end tag")
  3278. (option (const verbatim)))))
  3279. (defvar org-protecting-blocks
  3280. '("src" "example" "latex" "ascii" "html" "docbook" "ditaa" "dot" "r" "R")
  3281. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3282. This is needed for font-lock setup.")
  3283. ;;; Miscellaneous options
  3284. (defgroup org-completion nil
  3285. "Completion in Org-mode."
  3286. :tag "Org Completion"
  3287. :group 'org)
  3288. (defcustom org-completion-use-ido nil
  3289. "Non-nil means use ido completion wherever possible.
  3290. Note that `ido-mode' must be active for this variable to be relevant.
  3291. If you decide to turn this variable on, you might well want to turn off
  3292. `org-outline-path-complete-in-steps'.
  3293. See also `org-completion-use-iswitchb'."
  3294. :group 'org-completion
  3295. :type 'boolean)
  3296. (defcustom org-completion-use-iswitchb nil
  3297. "Non-nil means use iswitchb completion wherever possible.
  3298. Note that `iswitchb-mode' must be active for this variable to be relevant.
  3299. If you decide to turn this variable on, you might well want to turn off
  3300. `org-outline-path-complete-in-steps'.
  3301. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  3302. :group 'org-completion
  3303. :type 'boolean)
  3304. (defcustom org-completion-fallback-command 'hippie-expand
  3305. "The expansion command called by \\[pcomplete] in normal context.
  3306. Normal means, no org-mode-specific context."
  3307. :group 'org-completion
  3308. :type 'function)
  3309. ;;; Functions and variables from their packages
  3310. ;; Declared here to avoid compiler warnings
  3311. ;; XEmacs only
  3312. (defvar outline-mode-menu-heading)
  3313. (defvar outline-mode-menu-show)
  3314. (defvar outline-mode-menu-hide)
  3315. (defvar zmacs-regions) ; XEmacs regions
  3316. ;; Emacs only
  3317. (defvar mark-active)
  3318. ;; Various packages
  3319. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  3320. (declare-function calendar-forward-day "cal-move" (arg))
  3321. (declare-function calendar-goto-date "cal-move" (date))
  3322. (declare-function calendar-goto-today "cal-move" ())
  3323. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3324. (defvar calc-embedded-close-formula)
  3325. (defvar calc-embedded-open-formula)
  3326. (declare-function cdlatex-tab "ext:cdlatex" ())
  3327. (declare-function cdlatex-compute-tables "ext:cdlatex" ())
  3328. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3329. (defvar font-lock-unfontify-region-function)
  3330. (declare-function iswitchb-read-buffer "iswitchb"
  3331. (prompt &optional default require-match start matches-set))
  3332. (defvar iswitchb-temp-buflist)
  3333. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3334. (defvar org-agenda-tags-todo-honor-ignore-options)
  3335. (declare-function org-agenda-skip "org-agenda" ())
  3336. (declare-function
  3337. org-agenda-format-item "org-agenda"
  3338. (extra txt &optional category tags dotime noprefix remove-re habitp))
  3339. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3340. (declare-function org-agenda-change-all-lines "org-agenda"
  3341. (newhead hdmarker &optional fixface just-this))
  3342. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3343. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3344. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  3345. (beg end))
  3346. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3347. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3348. "org-agenda" (&optional end))
  3349. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3350. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  3351. (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
  3352. (declare-function org-inlinetask-goto-end "org-inlinetask" ())
  3353. (declare-function org-indent-mode "org-indent" (&optional arg))
  3354. (declare-function parse-time-string "parse-time" (string))
  3355. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3356. (declare-function org-export-latex-fix-inputenc "org-latex" ())
  3357. (declare-function orgtbl-send-table "org-table" (&optional maybe))
  3358. (defvar remember-data-file)
  3359. (defvar texmathp-why)
  3360. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3361. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3362. (defvar w3m-current-url)
  3363. (defvar w3m-current-title)
  3364. (defvar org-latex-regexps)
  3365. ;;; Autoload and prepare some org modules
  3366. ;; Some table stuff that needs to be defined here, because it is used
  3367. ;; by the functions setting up org-mode or checking for table context.
  3368. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  3369. "Detect an org-type or table-type table.")
  3370. (defconst org-table-line-regexp "^[ \t]*|"
  3371. "Detect an org-type table line.")
  3372. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  3373. "Detect an org-type table line.")
  3374. (defconst org-table-hline-regexp "^[ \t]*|-"
  3375. "Detect an org-type table hline.")
  3376. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  3377. "Detect a table-type table hline.")
  3378. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  3379. "Detect the first line outside a table when searching from within it.
  3380. This works for both table types.")
  3381. ;; Autoload the functions in org-table.el that are needed by functions here.
  3382. (eval-and-compile
  3383. (org-autoload "org-table"
  3384. '(org-table-align org-table-begin org-table-blank-field
  3385. org-table-convert org-table-convert-region org-table-copy-down
  3386. org-table-copy-region org-table-create
  3387. org-table-create-or-convert-from-region
  3388. org-table-create-with-table.el org-table-current-dline
  3389. org-table-cut-region org-table-delete-column org-table-edit-field
  3390. org-table-edit-formulas org-table-end org-table-eval-formula
  3391. org-table-export org-table-field-info
  3392. org-table-get-stored-formulas org-table-goto-column
  3393. org-table-hline-and-move org-table-import org-table-insert-column
  3394. org-table-insert-hline org-table-insert-row org-table-iterate
  3395. org-table-justify-field-maybe org-table-kill-row
  3396. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  3397. org-table-move-column org-table-move-column-left
  3398. org-table-move-column-right org-table-move-row
  3399. org-table-move-row-down org-table-move-row-up
  3400. org-table-next-field org-table-next-row org-table-paste-rectangle
  3401. org-table-previous-field org-table-recalculate
  3402. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  3403. org-table-toggle-coordinate-overlays
  3404. org-table-toggle-formula-debugger org-table-wrap-region
  3405. orgtbl-mode turn-on-orgtbl org-table-to-lisp
  3406. orgtbl-to-generic orgtbl-to-tsv orgtbl-to-csv orgtbl-to-latex
  3407. orgtbl-to-orgtbl orgtbl-to-html orgtbl-to-texinfo)))
  3408. (defun org-at-table-p (&optional table-type)
  3409. "Return t if the cursor is inside an org-type table.
  3410. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  3411. (if org-enable-table-editor
  3412. (save-excursion
  3413. (beginning-of-line 1)
  3414. (looking-at (if table-type org-table-any-line-regexp
  3415. org-table-line-regexp)))
  3416. nil))
  3417. (defsubst org-table-p () (org-at-table-p))
  3418. (defun org-at-table.el-p ()
  3419. "Return t if and only if we are at a table.el table."
  3420. (and (org-at-table-p 'any)
  3421. (save-excursion
  3422. (goto-char (org-table-begin 'any))
  3423. (looking-at org-table1-hline-regexp))))
  3424. (defun org-table-recognize-table.el ()
  3425. "If there is a table.el table nearby, recognize it and move into it."
  3426. (if org-table-tab-recognizes-table.el
  3427. (if (org-at-table.el-p)
  3428. (progn
  3429. (beginning-of-line 1)
  3430. (if (looking-at org-table-dataline-regexp)
  3431. nil
  3432. (if (looking-at org-table1-hline-regexp)
  3433. (progn
  3434. (beginning-of-line 2)
  3435. (if (looking-at org-table-any-border-regexp)
  3436. (beginning-of-line -1)))))
  3437. (if (re-search-forward "|" (org-table-end t) t)
  3438. (progn
  3439. (require 'table)
  3440. (if (table--at-cell-p (point))
  3441. t
  3442. (message "recognizing table.el table...")
  3443. (table-recognize-table)
  3444. (message "recognizing table.el table...done")))
  3445. (error "This should not happen"))
  3446. t)
  3447. nil)
  3448. nil))
  3449. (defun org-at-table-hline-p ()
  3450. "Return t if the cursor is inside a hline in a table."
  3451. (if org-enable-table-editor
  3452. (save-excursion
  3453. (beginning-of-line 1)
  3454. (looking-at org-table-hline-regexp))
  3455. nil))
  3456. (defvar org-table-clean-did-remove-column nil)
  3457. (defun org-table-map-tables (function &optional quietly)
  3458. "Apply FUNCTION to the start of all tables in the buffer."
  3459. (save-excursion
  3460. (save-restriction
  3461. (widen)
  3462. (goto-char (point-min))
  3463. (while (re-search-forward org-table-any-line-regexp nil t)
  3464. (unless quietly
  3465. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
  3466. (beginning-of-line 1)
  3467. (when (and (looking-at org-table-line-regexp)
  3468. ;; Exclude tables in src/example/verbatim/clocktable blocks
  3469. (not (org-in-block-p '("src" "example"))))
  3470. (save-excursion (funcall function))
  3471. (or (looking-at org-table-line-regexp)
  3472. (forward-char 1)))
  3473. (re-search-forward org-table-any-border-regexp nil 1))))
  3474. (unless quietly (message "Mapping tables: done")))
  3475. ;; Declare and autoload functions from org-exp.el & Co
  3476. (declare-function org-default-export-plist "org-exp")
  3477. (declare-function org-infile-export-plist "org-exp")
  3478. (declare-function org-get-current-options "org-exp")
  3479. (eval-and-compile
  3480. (org-autoload "org-exp"
  3481. '(org-export org-export-visible
  3482. org-insert-export-options-template
  3483. org-table-clean-before-export))
  3484. (org-autoload "org-ascii"
  3485. '(org-export-as-ascii org-export-ascii-preprocess
  3486. org-export-as-ascii-to-buffer org-replace-region-by-ascii
  3487. org-export-region-as-ascii))
  3488. (org-autoload "org-latex"
  3489. '(org-export-as-latex-batch org-export-as-latex-to-buffer
  3490. org-replace-region-by-latex org-export-region-as-latex
  3491. org-export-as-latex org-export-as-pdf
  3492. org-export-as-pdf-and-open))
  3493. (org-autoload "org-html"
  3494. '(org-export-as-html-and-open
  3495. org-export-as-html-batch org-export-as-html-to-buffer
  3496. org-replace-region-by-html org-export-region-as-html
  3497. org-export-as-html))
  3498. (org-autoload "org-docbook"
  3499. '(org-export-as-docbook-batch org-export-as-docbook-to-buffer
  3500. org-replace-region-by-docbook org-export-region-as-docbook
  3501. org-export-as-docbook-pdf org-export-as-docbook-pdf-and-open
  3502. org-export-as-docbook))
  3503. (org-autoload "org-icalendar"
  3504. '(org-export-icalendar-this-file
  3505. org-export-icalendar-all-agenda-files
  3506. org-export-icalendar-combine-agenda-files))
  3507. (org-autoload "org-xoxo" '(org-export-as-xoxo))
  3508. (org-autoload "org-beamer" '(org-beamer-mode org-beamer-sectioning)))
  3509. ;; Declare and autoload functions from org-agenda.el
  3510. (eval-and-compile
  3511. (org-autoload "org-agenda"
  3512. '(org-agenda org-agenda-list org-search-view
  3513. org-todo-list org-tags-view org-agenda-list-stuck-projects
  3514. org-diary org-agenda-to-appt
  3515. org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  3516. ;; Autoload org-remember
  3517. (eval-and-compile
  3518. (org-autoload "org-remember"
  3519. '(org-remember-insinuate org-remember-annotation
  3520. org-remember-apply-template org-remember org-remember-handler)))
  3521. (eval-and-compile
  3522. (org-autoload "org-capture"
  3523. '(org-capture org-capture-insert-template-here
  3524. org-capture-import-remember-templates)))
  3525. ;; Autoload org-clock.el
  3526. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  3527. (beg end))
  3528. (declare-function org-clock-update-mode-line "org-clock" ())
  3529. (declare-function org-resolve-clocks "org-clock"
  3530. (&optional also-non-dangling-p prompt last-valid))
  3531. (defvar org-clock-start-time)
  3532. (defvar org-clock-marker (make-marker)
  3533. "Marker recording the last clock-in.")
  3534. (defvar org-clock-hd-marker (make-marker)
  3535. "Marker recording the last clock-in, but the headline position.")
  3536. (defvar org-clock-heading ""
  3537. "The heading of the current clock entry.")
  3538. (defun org-clock-is-active ()
  3539. "Return non-nil if clock is currently running.
  3540. The return value is actually the clock marker."
  3541. (marker-buffer org-clock-marker))
  3542. (eval-and-compile
  3543. (org-autoload
  3544. "org-clock"
  3545. '(org-clock-in org-clock-out org-clock-cancel
  3546. org-clock-goto org-clock-sum org-clock-display
  3547. org-clock-remove-overlays org-clock-report
  3548. org-clocktable-shift org-dblock-write:clocktable
  3549. org-get-clocktable org-resolve-clocks)))
  3550. (defun org-clock-update-time-maybe ()
  3551. "If this is a CLOCK line, update it and return t.
  3552. Otherwise, return nil."
  3553. (interactive)
  3554. (save-excursion
  3555. (beginning-of-line 1)
  3556. (skip-chars-forward " \t")
  3557. (when (looking-at org-clock-string)
  3558. (let ((re (concat "[ \t]*" org-clock-string
  3559. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  3560. "\\([ \t]*=>.*\\)?\\)?"))
  3561. ts te h m s neg)
  3562. (cond
  3563. ((not (looking-at re))
  3564. nil)
  3565. ((not (match-end 2))
  3566. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3567. (> org-clock-marker (point))
  3568. (<= org-clock-marker (point-at-eol)))
  3569. ;; The clock is running here
  3570. (setq org-clock-start-time
  3571. (apply 'encode-time
  3572. (org-parse-time-string (match-string 1))))
  3573. (org-clock-update-mode-line)))
  3574. (t
  3575. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  3576. (end-of-line 1)
  3577. (setq ts (match-string 1)
  3578. te (match-string 3))
  3579. (setq s (- (org-float-time
  3580. (apply 'encode-time (org-parse-time-string te)))
  3581. (org-float-time
  3582. (apply 'encode-time (org-parse-time-string ts))))
  3583. neg (< s 0)
  3584. s (abs s)
  3585. h (floor (/ s 3600))
  3586. s (- s (* 3600 h))
  3587. m (floor (/ s 60))
  3588. s (- s (* 60 s)))
  3589. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  3590. t))))))
  3591. (defun org-check-running-clock ()
  3592. "Check if the current buffer contains the running clock.
  3593. If yes, offer to stop it and to save the buffer with the changes."
  3594. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3595. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3596. (buffer-name))))
  3597. (org-clock-out)
  3598. (when (y-or-n-p "Save changed buffer?")
  3599. (save-buffer))))
  3600. (defun org-clocktable-try-shift (dir n)
  3601. "Check if this line starts a clock table, if yes, shift the time block."
  3602. (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
  3603. (org-clocktable-shift dir n)))
  3604. ;; Autoload org-timer.el
  3605. (eval-and-compile
  3606. (org-autoload
  3607. "org-timer"
  3608. '(org-timer-start org-timer org-timer-item
  3609. org-timer-change-times-in-region
  3610. org-timer-set-timer
  3611. org-timer-reset-timers
  3612. org-timer-show-remaining-time)))
  3613. ;; Autoload org-feed.el
  3614. (eval-and-compile
  3615. (org-autoload
  3616. "org-feed"
  3617. '(org-feed-update org-feed-update-all org-feed-goto-inbox)))
  3618. ;; Autoload org-indent.el
  3619. ;; Define the variable already here, to make sure we have it.
  3620. (defvar org-indent-mode nil
  3621. "Non-nil if Org-Indent mode is enabled.
  3622. Use the command `org-indent-mode' to change this variable.")
  3623. (eval-and-compile
  3624. (org-autoload
  3625. "org-indent"
  3626. '(org-indent-mode)))
  3627. ;; Autoload org-mobile.el
  3628. (eval-and-compile
  3629. (org-autoload
  3630. "org-mobile"
  3631. '(org-mobile-push org-mobile-pull org-mobile-create-sumo-agenda)))
  3632. ;; Autoload archiving code
  3633. ;; The stuff that is needed for cycling and tags has to be defined here.
  3634. (defgroup org-archive nil
  3635. "Options concerning archiving in Org-mode."
  3636. :tag "Org Archive"
  3637. :group 'org-structure)
  3638. (defcustom org-archive-location "%s_archive::"
  3639. "The location where subtrees should be archived.
  3640. The value of this variable is a string, consisting of two parts,
  3641. separated by a double-colon. The first part is a filename and
  3642. the second part is a headline.
  3643. When the filename is omitted, archiving happens in the same file.
  3644. %s in the filename will be replaced by the current file
  3645. name (without the directory part). Archiving to a different file
  3646. is useful to keep archived entries from contributing to the
  3647. Org-mode Agenda.
  3648. The archived entries will be filed as subtrees of the specified
  3649. headline. When the headline is omitted, the subtrees are simply
  3650. filed away at the end of the file, as top-level entries. Also in
  3651. the heading you can use %s to represent the file name, this can be
  3652. useful when using the same archive for a number of different files.
  3653. Here are a few examples:
  3654. \"%s_archive::\"
  3655. If the current file is Projects.org, archive in file
  3656. Projects.org_archive, as top-level trees. This is the default.
  3657. \"::* Archived Tasks\"
  3658. Archive in the current file, under the top-level headline
  3659. \"* Archived Tasks\".
  3660. \"~/org/archive.org::\"
  3661. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  3662. \"~/org/archive.org::* From %s\"
  3663. Archive in file ~/org/archive.org (absolute path), under headlines
  3664. \"From FILENAME\" where file name is the current file name.
  3665. \"~/org/datetree.org::datetree/* Finished Tasks\"
  3666. The \"datetree/\" string is special, signifying to archive
  3667. items to the datetree. Items are placed in either the CLOSED
  3668. date of the item, or the current date if there is no CLOSED date.
  3669. The heading will be a subentry to the current date. There doesn't
  3670. need to be a heading, but there always needs to be a slash after
  3671. datetree. For example, to store archived items directly in the
  3672. datetree, use \"~/org/datetree.org::datetree/\".
  3673. \"basement::** Finished Tasks\"
  3674. Archive in file ./basement (relative path), as level 3 trees
  3675. below the level 2 heading \"** Finished Tasks\".
  3676. You may set this option on a per-file basis by adding to the buffer a
  3677. line like
  3678. #+ARCHIVE: basement::** Finished Tasks
  3679. You may also define it locally for a subtree by setting an ARCHIVE property
  3680. in the entry. If such a property is found in an entry, or anywhere up
  3681. the hierarchy, it will be used."
  3682. :group 'org-archive
  3683. :type 'string)
  3684. (defcustom org-archive-tag "ARCHIVE"
  3685. "The tag that marks a subtree as archived.
  3686. An archived subtree does not open during visibility cycling, and does
  3687. not contribute to the agenda listings.
  3688. After changing this, font-lock must be restarted in the relevant buffers to
  3689. get the proper fontification."
  3690. :group 'org-archive
  3691. :group 'org-keywords
  3692. :type 'string)
  3693. (defcustom org-agenda-skip-archived-trees t
  3694. "Non-nil means the agenda will skip any items located in archived trees.
  3695. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  3696. variable is no longer recommended, you should leave it at the value t.
  3697. Instead, use the key `v' to cycle the archives-mode in the agenda."
  3698. :group 'org-archive
  3699. :group 'org-agenda-skip
  3700. :type 'boolean)
  3701. (defcustom org-columns-skip-archived-trees t
  3702. "Non-nil means ignore archived trees when creating column view."
  3703. :group 'org-archive
  3704. :group 'org-properties
  3705. :type 'boolean)
  3706. (defcustom org-cycle-open-archived-trees nil
  3707. "Non-nil means `org-cycle' will open archived trees.
  3708. An archived tree is a tree marked with the tag ARCHIVE.
  3709. When nil, archived trees will stay folded. You can still open them with
  3710. normal outline commands like `show-all', but not with the cycling commands."
  3711. :group 'org-archive
  3712. :group 'org-cycle
  3713. :type 'boolean)
  3714. (defcustom org-sparse-tree-open-archived-trees nil
  3715. "Non-nil means sparse tree construction shows matches in archived trees.
  3716. When nil, matches in these trees are highlighted, but the trees are kept in
  3717. collapsed state."
  3718. :group 'org-archive
  3719. :group 'org-sparse-trees
  3720. :type 'boolean)
  3721. (defcustom org-sparse-tree-default-date-type 'scheduled-or-deadline
  3722. "The default date type when building a sparse tree.
  3723. When this is nil, a date is a scheduled or a deadline timestamp.
  3724. Otherwise, these types are allowed:
  3725. all: all timestamps
  3726. active: only active timestamps (<...>)
  3727. inactive: only inactive timestamps (<...)
  3728. scheduled: only scheduled timestamps
  3729. deadline: only deadline timestamps"
  3730. :type '(choice (const :tag "Scheduled or deadline" 'scheduled-or-deadline)
  3731. (const :tag "All timestamps" all)
  3732. (const :tag "Only active timestamps" active)
  3733. (const :tag "Only inactive timestamps" inactive)
  3734. (const :tag "Only scheduled timestamps" scheduled)
  3735. (const :tag "Only deadline timestamps" deadline))
  3736. :group 'org-sparse-trees
  3737. :version "24.2")
  3738. (defun org-cycle-hide-archived-subtrees (state)
  3739. "Re-hide all archived subtrees after a visibility state change."
  3740. (when (and (not org-cycle-open-archived-trees)
  3741. (not (memq state '(overview folded))))
  3742. (save-excursion
  3743. (let* ((globalp (memq state '(contents all)))
  3744. (beg (if globalp (point-min) (point)))
  3745. (end (if globalp (point-max) (org-end-of-subtree t))))
  3746. (org-hide-archived-subtrees beg end)
  3747. (goto-char beg)
  3748. (if (looking-at (concat ".*:" org-archive-tag ":"))
  3749. (message "%s" (substitute-command-keys
  3750. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  3751. (defun org-force-cycle-archived ()
  3752. "Cycle subtree even if it is archived."
  3753. (interactive)
  3754. (setq this-command 'org-cycle)
  3755. (let ((org-cycle-open-archived-trees t))
  3756. (call-interactively 'org-cycle)))
  3757. (defun org-hide-archived-subtrees (beg end)
  3758. "Re-hide all archived subtrees after a visibility state change."
  3759. (save-excursion
  3760. (let* ((re (concat ":" org-archive-tag ":")))
  3761. (goto-char beg)
  3762. (while (re-search-forward re end t)
  3763. (when (org-at-heading-p)
  3764. (org-flag-subtree t)
  3765. (org-end-of-subtree t))))))
  3766. (defun org-flag-subtree (flag)
  3767. (save-excursion
  3768. (org-back-to-heading t)
  3769. (outline-end-of-heading)
  3770. (outline-flag-region (point)
  3771. (progn (org-end-of-subtree t) (point))
  3772. flag)))
  3773. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  3774. (eval-and-compile
  3775. (org-autoload "org-archive"
  3776. '(org-add-archive-files org-archive-subtree
  3777. org-archive-to-archive-sibling org-toggle-archive-tag
  3778. org-archive-subtree-default
  3779. org-archive-subtree-default-with-confirmation)))
  3780. ;; Autoload Column View Code
  3781. (declare-function org-columns-number-to-string "org-colview")
  3782. (declare-function org-columns-get-format-and-top-level "org-colview")
  3783. (declare-function org-columns-compute "org-colview")
  3784. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  3785. '(org-columns-number-to-string org-columns-get-format-and-top-level
  3786. org-columns-compute org-agenda-columns org-columns-remove-overlays
  3787. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  3788. ;; Autoload ID code
  3789. (declare-function org-id-store-link "org-id")
  3790. (declare-function org-id-locations-load "org-id")
  3791. (declare-function org-id-locations-save "org-id")
  3792. (defvar org-id-track-globally)
  3793. (org-autoload "org-id"
  3794. '(org-id-get-create org-id-new org-id-copy org-id-get
  3795. org-id-get-with-outline-path-completion
  3796. org-id-get-with-outline-drilling org-id-store-link
  3797. org-id-goto org-id-find org-id-store-link))
  3798. ;; Autoload Plotting Code
  3799. (org-autoload "org-plot"
  3800. '(org-plot/gnuplot))
  3801. ;;; Variables for pre-computed regular expressions, all buffer local
  3802. (defvar org-drawer-regexp "^[ \t]*:PROPERTIES:[ \t]*$"
  3803. "Matches first line of a hidden block.")
  3804. (make-variable-buffer-local 'org-drawer-regexp)
  3805. (defvar org-todo-regexp nil
  3806. "Matches any of the TODO state keywords.")
  3807. (make-variable-buffer-local 'org-todo-regexp)
  3808. (defvar org-not-done-regexp nil
  3809. "Matches any of the TODO state keywords except the last one.")
  3810. (make-variable-buffer-local 'org-not-done-regexp)
  3811. (defvar org-not-done-heading-regexp nil
  3812. "Matches a TODO headline that is not done.")
  3813. (make-variable-buffer-local 'org-not-done-regexp)
  3814. (defvar org-todo-line-regexp nil
  3815. "Matches a headline and puts TODO state into group 2 if present.")
  3816. (make-variable-buffer-local 'org-todo-line-regexp)
  3817. (defvar org-complex-heading-regexp nil
  3818. "Matches a headline and puts everything into groups:
  3819. group 1: the stars
  3820. group 2: The todo keyword, maybe
  3821. group 3: Priority cookie
  3822. group 4: True headline
  3823. group 5: Tags")
  3824. (make-variable-buffer-local 'org-complex-heading-regexp)
  3825. (defvar org-complex-heading-regexp-format nil
  3826. "Printf format to make regexp to match an exact headline.
  3827. This regexp will match the headline of any node which has the
  3828. exact headline text that is put into the format, but may have any
  3829. TODO state, priority and tags.")
  3830. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  3831. (defvar org-todo-line-tags-regexp nil
  3832. "Matches a headline and puts TODO state into group 2 if present.
  3833. Also put tags into group 4 if tags are present.")
  3834. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3835. (defvar org-ds-keyword-length 12
  3836. "Maximum length of the Deadline and SCHEDULED keywords.")
  3837. (make-variable-buffer-local 'org-ds-keyword-length)
  3838. (defvar org-deadline-regexp nil
  3839. "Matches the DEADLINE keyword.")
  3840. (make-variable-buffer-local 'org-deadline-regexp)
  3841. (defvar org-deadline-time-regexp nil
  3842. "Matches the DEADLINE keyword together with a time stamp.")
  3843. (make-variable-buffer-local 'org-deadline-time-regexp)
  3844. (defvar org-deadline-line-regexp nil
  3845. "Matches the DEADLINE keyword and the rest of the line.")
  3846. (make-variable-buffer-local 'org-deadline-line-regexp)
  3847. (defvar org-scheduled-regexp nil
  3848. "Matches the SCHEDULED keyword.")
  3849. (make-variable-buffer-local 'org-scheduled-regexp)
  3850. (defvar org-scheduled-time-regexp nil
  3851. "Matches the SCHEDULED keyword together with a time stamp.")
  3852. (make-variable-buffer-local 'org-scheduled-time-regexp)
  3853. (defvar org-closed-time-regexp nil
  3854. "Matches the CLOSED keyword together with a time stamp.")
  3855. (make-variable-buffer-local 'org-closed-time-regexp)
  3856. (defvar org-keyword-time-regexp nil
  3857. "Matches any of the 4 keywords, together with the time stamp.")
  3858. (make-variable-buffer-local 'org-keyword-time-regexp)
  3859. (defvar org-keyword-time-not-clock-regexp nil
  3860. "Matches any of the 3 keywords, together with the time stamp.")
  3861. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  3862. (defvar org-maybe-keyword-time-regexp nil
  3863. "Matches a timestamp, possibly preceded by a keyword.")
  3864. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  3865. (defvar org-all-time-keywords nil
  3866. "List of time keywords.")
  3867. (make-variable-buffer-local 'org-all-time-keywords)
  3868. (defconst org-plain-time-of-day-regexp
  3869. (concat
  3870. "\\(\\<[012]?[0-9]"
  3871. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3872. "\\(--?"
  3873. "\\(\\<[012]?[0-9]"
  3874. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3875. "\\)?")
  3876. "Regular expression to match a plain time or time range.
  3877. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3878. groups carry important information:
  3879. 0 the full match
  3880. 1 the first time, range or not
  3881. 8 the second time, if it is a range.")
  3882. (defconst org-plain-time-extension-regexp
  3883. (concat
  3884. "\\(\\<[012]?[0-9]"
  3885. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3886. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  3887. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  3888. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3889. groups carry important information:
  3890. 0 the full match
  3891. 7 hours of duration
  3892. 9 minutes of duration")
  3893. (defconst org-stamp-time-of-day-regexp
  3894. (concat
  3895. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  3896. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  3897. "\\(--?"
  3898. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  3899. "Regular expression to match a timestamp time or time range.
  3900. After a match, the following groups carry important information:
  3901. 0 the full match
  3902. 1 date plus weekday, for back referencing to make sure both times are on the same day
  3903. 2 the first time, range or not
  3904. 4 the second time, if it is a range.")
  3905. (defconst org-startup-options
  3906. '(("fold" org-startup-folded t)
  3907. ("overview" org-startup-folded t)
  3908. ("nofold" org-startup-folded nil)
  3909. ("showall" org-startup-folded nil)
  3910. ("showeverything" org-startup-folded showeverything)
  3911. ("content" org-startup-folded content)
  3912. ("indent" org-startup-indented t)
  3913. ("noindent" org-startup-indented nil)
  3914. ("hidestars" org-hide-leading-stars t)
  3915. ("showstars" org-hide-leading-stars nil)
  3916. ("odd" org-odd-levels-only t)
  3917. ("oddeven" org-odd-levels-only nil)
  3918. ("align" org-startup-align-all-tables t)
  3919. ("noalign" org-startup-align-all-tables nil)
  3920. ("inlineimages" org-startup-with-inline-images t)
  3921. ("noinlineimages" org-startup-with-inline-images nil)
  3922. ("customtime" org-display-custom-times t)
  3923. ("logdone" org-log-done time)
  3924. ("lognotedone" org-log-done note)
  3925. ("nologdone" org-log-done nil)
  3926. ("lognoteclock-out" org-log-note-clock-out t)
  3927. ("nolognoteclock-out" org-log-note-clock-out nil)
  3928. ("logrepeat" org-log-repeat state)
  3929. ("lognoterepeat" org-log-repeat note)
  3930. ("nologrepeat" org-log-repeat nil)
  3931. ("logreschedule" org-log-reschedule time)
  3932. ("lognotereschedule" org-log-reschedule note)
  3933. ("nologreschedule" org-log-reschedule nil)
  3934. ("logredeadline" org-log-redeadline time)
  3935. ("lognoteredeadline" org-log-redeadline note)
  3936. ("nologredeadline" org-log-redeadline nil)
  3937. ("logrefile" org-log-refile time)
  3938. ("lognoterefile" org-log-refile note)
  3939. ("nologrefile" org-log-refile nil)
  3940. ("fninline" org-footnote-define-inline t)
  3941. ("nofninline" org-footnote-define-inline nil)
  3942. ("fnlocal" org-footnote-section nil)
  3943. ("fnauto" org-footnote-auto-label t)
  3944. ("fnprompt" org-footnote-auto-label nil)
  3945. ("fnconfirm" org-footnote-auto-label confirm)
  3946. ("fnplain" org-footnote-auto-label plain)
  3947. ("fnadjust" org-footnote-auto-adjust t)
  3948. ("nofnadjust" org-footnote-auto-adjust nil)
  3949. ("constcgs" constants-unit-system cgs)
  3950. ("constSI" constants-unit-system SI)
  3951. ("noptag" org-tag-persistent-alist nil)
  3952. ("hideblocks" org-hide-block-startup t)
  3953. ("nohideblocks" org-hide-block-startup nil)
  3954. ("beamer" org-startup-with-beamer-mode t)
  3955. ("entitiespretty" org-pretty-entities t)
  3956. ("entitiesplain" org-pretty-entities nil))
  3957. "Variable associated with STARTUP options for org-mode.
  3958. Each element is a list of three items: the startup options (as written
  3959. in the #+STARTUP line), the corresponding variable, and the value to set
  3960. this variable to if the option is found. An optional forth element PUSH
  3961. means to push this value onto the list in the variable.")
  3962. (defun org-update-property-plist (key val props)
  3963. "Update PROPS with KEY and VAL."
  3964. (let* ((appending (string= "+" (substring key (- (length key) 1))))
  3965. (key (if appending (substring key 0 (- (length key) 1)) key))
  3966. (remainder (org-remove-if (lambda (p) (string= (car p) key)) props))
  3967. (previous (cdr (assoc key props))))
  3968. (if appending
  3969. (cons (cons key (if previous (concat previous " " val) val)) remainder)
  3970. (cons (cons key val) remainder))))
  3971. (defconst org-block-regexp
  3972. "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
  3973. "Regular expression for hiding blocks.")
  3974. (defconst org-heading-keyword-regexp-format
  3975. "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  3976. "Printf format for a regexp matching an headline with some keyword.
  3977. This regexp will match the headline of any node which has the
  3978. exact keyword that is put into the format. The keyword isn't in
  3979. any group by default, but the stars and the body are.")
  3980. (defconst org-heading-keyword-maybe-regexp-format
  3981. "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
  3982. "Printf format for a regexp matching an headline, possibly with some keyword.
  3983. This regexp can match any headline with the specified keyword, or
  3984. without a keyword. The keyword isn't in any group by default,
  3985. but the stars and the body are.")
  3986. (defun org-set-regexps-and-options ()
  3987. "Precompute regular expressions for current buffer."
  3988. (when (derived-mode-p 'org-mode)
  3989. (org-set-local 'org-todo-kwd-alist nil)
  3990. (org-set-local 'org-todo-key-alist nil)
  3991. (org-set-local 'org-todo-key-trigger nil)
  3992. (org-set-local 'org-todo-keywords-1 nil)
  3993. (org-set-local 'org-done-keywords nil)
  3994. (org-set-local 'org-todo-heads nil)
  3995. (org-set-local 'org-todo-sets nil)
  3996. (org-set-local 'org-todo-log-states nil)
  3997. (org-set-local 'org-file-properties nil)
  3998. (org-set-local 'org-file-tags nil)
  3999. (let ((re (org-make-options-regexp
  4000. '("CATEGORY" "TODO" "COLUMNS"
  4001. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  4002. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE" "LATEX_CLASS"
  4003. "OPTIONS")
  4004. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  4005. (splitre "[ \t]+")
  4006. (scripts org-use-sub-superscripts)
  4007. kwds kws0 kwsa key log value cat arch tags const links hw dws
  4008. tail sep kws1 prio props ftags drawers beamer-p
  4009. ext-setup-or-nil setup-contents (start 0))
  4010. (save-excursion
  4011. (save-restriction
  4012. (widen)
  4013. (goto-char (point-min))
  4014. (while (or (and ext-setup-or-nil
  4015. (string-match re ext-setup-or-nil start)
  4016. (setq start (match-end 0)))
  4017. (and (setq ext-setup-or-nil nil start 0)
  4018. (re-search-forward re nil t)))
  4019. (setq key (upcase (match-string 1 ext-setup-or-nil))
  4020. value (org-match-string-no-properties 2 ext-setup-or-nil))
  4021. (if (stringp value) (setq value (org-trim value)))
  4022. (cond
  4023. ((equal key "CATEGORY")
  4024. (setq cat value))
  4025. ((member key '("SEQ_TODO" "TODO"))
  4026. (push (cons 'sequence (org-split-string value splitre)) kwds))
  4027. ((equal key "TYP_TODO")
  4028. (push (cons 'type (org-split-string value splitre)) kwds))
  4029. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  4030. ;; general TODO-like setup
  4031. (push (cons (intern (downcase (match-string 1 key)))
  4032. (org-split-string value splitre)) kwds))
  4033. ((equal key "TAGS")
  4034. (setq tags (append tags (if tags '("\\n") nil)
  4035. (org-split-string value splitre))))
  4036. ((equal key "COLUMNS")
  4037. (org-set-local 'org-columns-default-format value))
  4038. ((equal key "LINK")
  4039. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  4040. (push (cons (match-string 1 value)
  4041. (org-trim (match-string 2 value)))
  4042. links)))
  4043. ((equal key "PRIORITIES")
  4044. (setq prio (org-split-string value " +")))
  4045. ((equal key "PROPERTY")
  4046. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4047. (setq props (org-update-property-plist (match-string 1 value)
  4048. (match-string 2 value)
  4049. props))))
  4050. ((equal key "FILETAGS")
  4051. (when (string-match "\\S-" value)
  4052. (setq ftags
  4053. (append
  4054. ftags
  4055. (apply 'append
  4056. (mapcar (lambda (x) (org-split-string x ":"))
  4057. (org-split-string value)))))))
  4058. ((equal key "DRAWERS")
  4059. (setq drawers (delete-dups (append org-drawers (org-split-string value splitre)))))
  4060. ((equal key "CONSTANTS")
  4061. (setq const (append const (org-split-string value splitre))))
  4062. ((equal key "STARTUP")
  4063. (let ((opts (org-split-string value splitre))
  4064. l var val)
  4065. (while (setq l (pop opts))
  4066. (when (setq l (assoc l org-startup-options))
  4067. (setq var (nth 1 l) val (nth 2 l))
  4068. (if (not (nth 3 l))
  4069. (set (make-local-variable var) val)
  4070. (if (not (listp (symbol-value var)))
  4071. (set (make-local-variable var) nil))
  4072. (set (make-local-variable var) (symbol-value var))
  4073. (add-to-list var val))))))
  4074. ((equal key "ARCHIVE")
  4075. (setq arch value)
  4076. (remove-text-properties 0 (length arch)
  4077. '(face t fontified t) arch))
  4078. ((equal key "LATEX_CLASS")
  4079. (setq beamer-p (equal value "beamer")))
  4080. ((equal key "OPTIONS")
  4081. (if (string-match "\\([ \t]\\|\\`\\)\\^:\\(t\\|nil\\|{}\\)" value)
  4082. (setq scripts (read (match-string 2 value)))))
  4083. ((equal key "SETUPFILE")
  4084. (setq setup-contents (org-file-contents
  4085. (expand-file-name
  4086. (org-remove-double-quotes value))
  4087. 'noerror))
  4088. (if (not ext-setup-or-nil)
  4089. (setq ext-setup-or-nil setup-contents start 0)
  4090. (setq ext-setup-or-nil
  4091. (concat (substring ext-setup-or-nil 0 start)
  4092. "\n" setup-contents "\n"
  4093. (substring ext-setup-or-nil start)))))))
  4094. ;; search for property blocks
  4095. (goto-char (point-min))
  4096. (while (re-search-forward org-block-regexp nil t)
  4097. (when (equal "PROPERTY" (upcase (match-string 1)))
  4098. (setq value (replace-regexp-in-string
  4099. "[\n\r]" " " (match-string 4)))
  4100. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4101. (setq props (org-update-property-plist (match-string 1 value)
  4102. (match-string 2 value)
  4103. props)))))))
  4104. (org-set-local 'org-use-sub-superscripts scripts)
  4105. (when cat
  4106. (org-set-local 'org-category (intern cat))
  4107. (push (cons "CATEGORY" cat) props))
  4108. (when prio
  4109. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  4110. (setq prio (mapcar 'string-to-char prio))
  4111. (org-set-local 'org-highest-priority (nth 0 prio))
  4112. (org-set-local 'org-lowest-priority (nth 1 prio))
  4113. (org-set-local 'org-default-priority (nth 2 prio)))
  4114. (and props (org-set-local 'org-file-properties (nreverse props)))
  4115. (and ftags (org-set-local 'org-file-tags
  4116. (mapcar 'org-add-prop-inherited ftags)))
  4117. (and drawers (org-set-local 'org-drawers drawers))
  4118. (and arch (org-set-local 'org-archive-location arch))
  4119. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  4120. ;; Process the TODO keywords
  4121. (unless kwds
  4122. ;; Use the global values as if they had been given locally.
  4123. (setq kwds (default-value 'org-todo-keywords))
  4124. (if (stringp (car kwds))
  4125. (setq kwds (list (cons org-todo-interpretation
  4126. (default-value 'org-todo-keywords)))))
  4127. (setq kwds (reverse kwds)))
  4128. (setq kwds (nreverse kwds))
  4129. (let (inter kws kw)
  4130. (while (setq kws (pop kwds))
  4131. (let ((kws (or
  4132. (run-hook-with-args-until-success
  4133. 'org-todo-setup-filter-hook kws)
  4134. kws)))
  4135. (setq inter (pop kws) sep (member "|" kws)
  4136. kws0 (delete "|" (copy-sequence kws))
  4137. kwsa nil
  4138. kws1 (mapcar
  4139. (lambda (x)
  4140. ;; 1 2
  4141. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  4142. (progn
  4143. (setq kw (match-string 1 x)
  4144. key (and (match-end 2) (match-string 2 x))
  4145. log (org-extract-log-state-settings x))
  4146. (push (cons kw (and key (string-to-char key))) kwsa)
  4147. (and log (push log org-todo-log-states))
  4148. kw)
  4149. (error "Invalid TODO keyword %s" x)))
  4150. kws0)
  4151. kwsa (if kwsa (append '((:startgroup))
  4152. (nreverse kwsa)
  4153. '((:endgroup))))
  4154. hw (car kws1)
  4155. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  4156. tail (list inter hw (car dws) (org-last dws))))
  4157. (add-to-list 'org-todo-heads hw 'append)
  4158. (push kws1 org-todo-sets)
  4159. (setq org-done-keywords (append org-done-keywords dws nil))
  4160. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  4161. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  4162. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  4163. (setq org-todo-sets (nreverse org-todo-sets)
  4164. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  4165. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  4166. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  4167. ;; Process the constants
  4168. (when const
  4169. (let (e cst)
  4170. (while (setq e (pop const))
  4171. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  4172. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  4173. (setq org-table-formula-constants-local cst)))
  4174. ;; Process the tags.
  4175. (when tags
  4176. (let (e tgs)
  4177. (while (setq e (pop tags))
  4178. (cond
  4179. ((equal e "{") (push '(:startgroup) tgs))
  4180. ((equal e "}") (push '(:endgroup) tgs))
  4181. ((equal e "\\n") (push '(:newline) tgs))
  4182. ((string-match (org-re "^\\([[:alnum:]_@#%]+\\)(\\(.\\))$") e)
  4183. (push (cons (match-string 1 e)
  4184. (string-to-char (match-string 2 e)))
  4185. tgs))
  4186. (t (push (list e) tgs))))
  4187. (org-set-local 'org-tag-alist nil)
  4188. (while (setq e (pop tgs))
  4189. (or (and (stringp (car e))
  4190. (assoc (car e) org-tag-alist))
  4191. (push e org-tag-alist)))))
  4192. ;; Compute the regular expressions and other local variables.
  4193. ;; Using `org-outline-regexp-bol' would complicate them much,
  4194. ;; because of the fixed white space at the end of that string.
  4195. (if (not org-done-keywords)
  4196. (setq org-done-keywords (and org-todo-keywords-1
  4197. (list (org-last org-todo-keywords-1)))))
  4198. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  4199. (length org-scheduled-string)
  4200. (length org-clock-string)
  4201. (length org-closed-string)))
  4202. org-drawer-regexp
  4203. (concat "^[ \t]*:\\("
  4204. (mapconcat 'regexp-quote org-drawers "\\|")
  4205. "\\):[ \t]*$")
  4206. org-not-done-keywords
  4207. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  4208. org-todo-regexp
  4209. (concat "\\("
  4210. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4211. "\\)")
  4212. org-not-done-regexp
  4213. (concat "\\("
  4214. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  4215. "\\)")
  4216. org-not-done-heading-regexp
  4217. (format org-heading-keyword-regexp-format org-not-done-regexp)
  4218. org-todo-line-regexp
  4219. (format org-heading-keyword-maybe-regexp-format org-todo-regexp)
  4220. org-complex-heading-regexp
  4221. (concat "^\\(\\*+\\)"
  4222. "\\(?: +" org-todo-regexp "\\)?"
  4223. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4224. "\\(?: +\\(.*?\\)\\)?"
  4225. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?")
  4226. "[ \t]*$")
  4227. org-complex-heading-regexp-format
  4228. (concat "^\\(\\*+\\)"
  4229. "\\(?: +" org-todo-regexp "\\)?"
  4230. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4231. "\\(?: +"
  4232. ;; Stats cookies can be stuck to body.
  4233. "\\(?:\\[[0-9%%/]+\\] *\\)?"
  4234. "\\(%s\\)"
  4235. "\\(?: *\\[[0-9%%/]+\\]\\)?"
  4236. "\\)"
  4237. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?")
  4238. "[ \t]*$")
  4239. org-todo-line-tags-regexp
  4240. (concat "^\\(\\*+\\)"
  4241. "\\(?: +" org-todo-regexp "\\)?"
  4242. "\\(?: +\\(.*?\\)\\)?"
  4243. (org-re "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?")
  4244. "[ \t]*$")
  4245. org-deadline-regexp (concat "\\<" org-deadline-string)
  4246. org-deadline-time-regexp
  4247. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  4248. org-deadline-line-regexp
  4249. (concat "\\<\\(" org-deadline-string "\\).*")
  4250. org-scheduled-regexp
  4251. (concat "\\<" org-scheduled-string)
  4252. org-scheduled-time-regexp
  4253. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  4254. org-closed-time-regexp
  4255. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  4256. org-keyword-time-regexp
  4257. (concat "\\<\\(" org-scheduled-string
  4258. "\\|" org-deadline-string
  4259. "\\|" org-closed-string
  4260. "\\|" org-clock-string "\\)"
  4261. " *[[<]\\([^]>]+\\)[]>]")
  4262. org-keyword-time-not-clock-regexp
  4263. (concat "\\<\\(" org-scheduled-string
  4264. "\\|" org-deadline-string
  4265. "\\|" org-closed-string
  4266. "\\)"
  4267. " *[[<]\\([^]>]+\\)[]>]")
  4268. org-maybe-keyword-time-regexp
  4269. (concat "\\(\\<\\(" org-scheduled-string
  4270. "\\|" org-deadline-string
  4271. "\\|" org-closed-string
  4272. "\\|" org-clock-string "\\)\\)?"
  4273. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  4274. org-all-time-keywords
  4275. (mapcar (lambda (w) (substring w 0 -1))
  4276. (list org-scheduled-string org-deadline-string
  4277. org-clock-string org-closed-string))
  4278. )
  4279. (org-compute-latex-and-specials-regexp)
  4280. (org-set-font-lock-defaults))))
  4281. (defun org-file-contents (file &optional noerror)
  4282. "Return the contents of FILE, as a string."
  4283. (if (or (not file)
  4284. (not (file-readable-p file)))
  4285. (if noerror
  4286. (progn
  4287. (message "Cannot read file \"%s\"" file)
  4288. (ding) (sit-for 2)
  4289. "")
  4290. (error "Cannot read file \"%s\"" file))
  4291. (with-temp-buffer
  4292. (insert-file-contents file)
  4293. (buffer-string))))
  4294. (defun org-extract-log-state-settings (x)
  4295. "Extract the log state setting from a TODO keyword string.
  4296. This will extract info from a string like \"WAIT(w@/!)\"."
  4297. (let (kw key log1 log2)
  4298. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4299. (setq kw (match-string 1 x)
  4300. key (and (match-end 2) (match-string 2 x))
  4301. log1 (and (match-end 3) (match-string 3 x))
  4302. log2 (and (match-end 4) (match-string 4 x)))
  4303. (and (or log1 log2)
  4304. (list kw
  4305. (and log1 (if (equal log1 "!") 'time 'note))
  4306. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4307. (defun org-remove-keyword-keys (list)
  4308. "Remove a pair of parenthesis at the end of each string in LIST."
  4309. (mapcar (lambda (x)
  4310. (if (string-match "(.*)$" x)
  4311. (substring x 0 (match-beginning 0))
  4312. x))
  4313. list))
  4314. (defun org-assign-fast-keys (alist)
  4315. "Assign fast keys to a keyword-key alist.
  4316. Respect keys that are already there."
  4317. (let (new e (alt ?0))
  4318. (while (setq e (pop alist))
  4319. (if (or (memq (car e) '(:newline :endgroup :startgroup))
  4320. (cdr e)) ;; Key already assigned.
  4321. (push e new)
  4322. (let ((clist (string-to-list (downcase (car e))))
  4323. (used (append new alist)))
  4324. (when (= (car clist) ?@)
  4325. (pop clist))
  4326. (while (and clist (rassoc (car clist) used))
  4327. (pop clist))
  4328. (unless clist
  4329. (while (rassoc alt used)
  4330. (incf alt)))
  4331. (push (cons (car e) (or (car clist) alt)) new))))
  4332. (nreverse new)))
  4333. ;;; Some variables used in various places
  4334. (defvar org-window-configuration nil
  4335. "Used in various places to store a window configuration.")
  4336. (defvar org-selected-window nil
  4337. "Used in various places to store a window configuration.")
  4338. (defvar org-finish-function nil
  4339. "Function to be called when `C-c C-c' is used.
  4340. This is for getting out of special buffers like remember.")
  4341. ;; FIXME: Occasionally check by commenting these, to make sure
  4342. ;; no other functions uses these, forgetting to let-bind them.
  4343. (org-no-warnings (defvar entry)) ;; unprefixed, from calendar.el
  4344. (defvar org-last-state)
  4345. (org-no-warnings (defvar date)) ;; unprefixed, from calendar.el
  4346. ;; Defined somewhere in this file, but used before definition.
  4347. (defvar org-entities) ;; defined in org-entities.el
  4348. (defvar org-struct-menu)
  4349. (defvar org-org-menu)
  4350. (defvar org-tbl-menu)
  4351. ;;;; Define the Org-mode
  4352. ;; We use a before-change function to check if a table might need
  4353. ;; an update.
  4354. (defvar org-table-may-need-update t
  4355. "Indicates that a table might need an update.
  4356. This variable is set by `org-before-change-function'.
  4357. `org-table-align' sets it back to nil.")
  4358. (defun org-before-change-function (beg end)
  4359. "Every change indicates that a table might need an update."
  4360. (setq org-table-may-need-update t))
  4361. (defvar org-mode-map)
  4362. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4363. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4364. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4365. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4366. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4367. (defvar org-table-buffer-is-an nil)
  4368. (defvar bidi-paragraph-direction)
  4369. (defvar buffer-face-mode-face)
  4370. (require 'outline)
  4371. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4372. (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"))
  4373. (require 'noutline "noutline" 'noerror) ;; stock XEmacs does not have it
  4374. ;; Other stuff we need.
  4375. (require 'time-date)
  4376. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  4377. (require 'easymenu)
  4378. (require 'overlay)
  4379. (require 'org-macs)
  4380. (require 'org-entities)
  4381. (require 'org-compat)
  4382. (require 'org-faces)
  4383. (require 'org-list)
  4384. (require 'org-pcomplete)
  4385. (require 'org-src)
  4386. (require 'org-footnote)
  4387. (require 'org-element)
  4388. ;; babel
  4389. (require 'ob)
  4390. (require 'ob-table)
  4391. (require 'ob-lob)
  4392. (require 'ob-ref)
  4393. (require 'ob-tangle)
  4394. (require 'ob-comint)
  4395. (require 'ob-keys)
  4396. ;;;###autoload
  4397. (define-derived-mode org-mode outline-mode "Org"
  4398. "Outline-based notes management and organizer, alias
  4399. \"Carsten's outline-mode for keeping track of everything.\"
  4400. Org-mode develops organizational tasks around a NOTES file which
  4401. contains information about projects as plain text. Org-mode is
  4402. implemented on top of outline-mode, which is ideal to keep the content
  4403. of large files well structured. It supports ToDo items, deadlines and
  4404. time stamps, which magically appear in the diary listing of the Emacs
  4405. calendar. Tables are easily created with a built-in table editor.
  4406. Plain text URL-like links connect to websites, emails (VM), Usenet
  4407. messages (Gnus), BBDB entries, and any files related to the project.
  4408. For printing and sharing of notes, an Org-mode file (or a part of it)
  4409. can be exported as a structured ASCII or HTML file.
  4410. The following commands are available:
  4411. \\{org-mode-map}"
  4412. ;; Get rid of Outline menus, they are not needed
  4413. ;; Need to do this here because define-derived-mode sets up
  4414. ;; the keymap so late. Still, it is a waste to call this each time
  4415. ;; we switch another buffer into org-mode.
  4416. (if (featurep 'xemacs)
  4417. (when (boundp 'outline-mode-menu-heading)
  4418. ;; Assume this is Greg's port, it uses easymenu
  4419. (easy-menu-remove outline-mode-menu-heading)
  4420. (easy-menu-remove outline-mode-menu-show)
  4421. (easy-menu-remove outline-mode-menu-hide))
  4422. (define-key org-mode-map [menu-bar headings] 'undefined)
  4423. (define-key org-mode-map [menu-bar hide] 'undefined)
  4424. (define-key org-mode-map [menu-bar show] 'undefined))
  4425. (org-load-modules-maybe)
  4426. (easy-menu-add org-org-menu)
  4427. (easy-menu-add org-tbl-menu)
  4428. (org-install-agenda-files-menu)
  4429. (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4430. (add-to-invisibility-spec '(org-cwidth))
  4431. (add-to-invisibility-spec '(org-hide-block . t))
  4432. (when (featurep 'xemacs)
  4433. (org-set-local 'line-move-ignore-invisible t))
  4434. (org-set-local 'outline-regexp org-outline-regexp)
  4435. (org-set-local 'outline-level 'org-outline-level)
  4436. (setq bidi-paragraph-direction 'left-to-right)
  4437. (when (and org-ellipsis
  4438. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4439. (fboundp 'make-glyph-code))
  4440. (unless org-display-table
  4441. (setq org-display-table (make-display-table)))
  4442. (set-display-table-slot
  4443. org-display-table 4
  4444. (vconcat (mapcar
  4445. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4446. org-ellipsis)))
  4447. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4448. (setq buffer-display-table org-display-table))
  4449. (org-set-regexps-and-options)
  4450. (when (and org-tag-faces (not org-tags-special-faces-re))
  4451. ;; tag faces set outside customize.... force initialization.
  4452. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4453. ;; Calc embedded
  4454. (org-set-local 'calc-embedded-open-mode "# ")
  4455. (modify-syntax-entry ?@ "w")
  4456. (if org-startup-truncated (setq truncate-lines t))
  4457. (org-set-local 'font-lock-unfontify-region-function
  4458. 'org-unfontify-region)
  4459. ;; Activate before-change-function
  4460. (org-set-local 'org-table-may-need-update t)
  4461. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4462. 'local)
  4463. ;; Check for running clock before killing a buffer
  4464. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4465. ;; Paragraphs and auto-filling
  4466. (org-set-autofill-regexps)
  4467. (org-set-local 'indent-line-function 'org-indent-line)
  4468. (org-set-local 'indent-region-function 'org-indent-region)
  4469. (org-update-radio-target-regexp)
  4470. ;; Beginning/end of defun
  4471. (org-set-local 'beginning-of-defun-function 'org-beginning-of-defun)
  4472. (org-set-local 'end-of-defun-function 'org-end-of-defun)
  4473. ;; Next error for sparse trees
  4474. (org-set-local 'next-error-function 'org-occur-next-match)
  4475. ;; Make sure dependence stuff works reliably, even for users who set it
  4476. ;; too late :-(
  4477. (if org-enforce-todo-dependencies
  4478. (add-hook 'org-blocker-hook
  4479. 'org-block-todo-from-children-or-siblings-or-parent)
  4480. (remove-hook 'org-blocker-hook
  4481. 'org-block-todo-from-children-or-siblings-or-parent))
  4482. (if org-enforce-todo-checkbox-dependencies
  4483. (add-hook 'org-blocker-hook
  4484. 'org-block-todo-from-checkboxes)
  4485. (remove-hook 'org-blocker-hook
  4486. 'org-block-todo-from-checkboxes))
  4487. ;; Align options lines
  4488. (org-set-local
  4489. 'align-mode-rules-list
  4490. '((org-in-buffer-settings
  4491. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4492. (modes . '(org-mode)))))
  4493. ;; Imenu
  4494. (org-set-local 'imenu-create-index-function
  4495. 'org-imenu-get-tree)
  4496. ;; Make isearch reveal context
  4497. (if (or (featurep 'xemacs)
  4498. (not (boundp 'outline-isearch-open-invisible-function)))
  4499. ;; Emacs 21 and XEmacs make use of the hook
  4500. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4501. ;; Emacs 22 deals with this through a special variable
  4502. (org-set-local 'outline-isearch-open-invisible-function
  4503. (lambda (&rest ignore) (org-show-context 'isearch))))
  4504. ;; Turn on org-beamer-mode?
  4505. (and org-startup-with-beamer-mode (org-beamer-mode 1))
  4506. ;; Setup the pcomplete hooks
  4507. (set (make-local-variable 'pcomplete-command-completion-function)
  4508. 'org-pcomplete-initial)
  4509. (set (make-local-variable 'pcomplete-command-name-function)
  4510. 'org-command-at-point)
  4511. (set (make-local-variable 'pcomplete-default-completion-function)
  4512. 'ignore)
  4513. (set (make-local-variable 'pcomplete-parse-arguments-function)
  4514. 'org-parse-arguments)
  4515. (set (make-local-variable 'pcomplete-termination-string) "")
  4516. (when (>= emacs-major-version 23)
  4517. (set (make-local-variable 'buffer-face-mode-face) 'org-default))
  4518. ;; If empty file that did not turn on org-mode automatically, make it to.
  4519. (if (and org-insert-mode-line-in-empty-file
  4520. (org-called-interactively-p 'any)
  4521. (= (point-min) (point-max)))
  4522. (insert "# -*- mode: org -*-\n\n"))
  4523. (unless org-inhibit-startup
  4524. (when org-startup-align-all-tables
  4525. (let ((bmp (buffer-modified-p)))
  4526. (org-table-map-tables 'org-table-align 'quietly)
  4527. (set-buffer-modified-p bmp)))
  4528. (when org-startup-with-inline-images
  4529. (org-display-inline-images))
  4530. (when org-startup-indented
  4531. (require 'org-indent)
  4532. (org-indent-mode 1))
  4533. (unless org-inhibit-startup-visibility-stuff
  4534. (org-set-startup-visibility))))
  4535. (when (fboundp 'abbrev-table-put)
  4536. (abbrev-table-put org-mode-abbrev-table
  4537. :parents (list text-mode-abbrev-table)))
  4538. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4539. (defun org-current-time ()
  4540. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  4541. (if (> (car org-time-stamp-rounding-minutes) 1)
  4542. (let ((r (car org-time-stamp-rounding-minutes))
  4543. (time (decode-time)))
  4544. (apply 'encode-time
  4545. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4546. (nthcdr 2 time))))
  4547. (current-time)))
  4548. (defun org-today ()
  4549. "Return today date, considering `org-extend-today-until'."
  4550. (time-to-days
  4551. (time-subtract (current-time)
  4552. (list 0 (* 3600 org-extend-today-until) 0))))
  4553. ;;;; Font-Lock stuff, including the activators
  4554. (defvar org-mouse-map (make-sparse-keymap))
  4555. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4556. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4557. (when org-mouse-1-follows-link
  4558. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4559. (when org-tab-follows-link
  4560. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4561. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4562. (require 'font-lock)
  4563. (defconst org-non-link-chars "]\t\n\r<>")
  4564. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  4565. "shell" "elisp" "doi" "message"))
  4566. (defvar org-link-types-re nil
  4567. "Matches a link that has a url-like prefix like \"http:\"")
  4568. (defvar org-link-re-with-space nil
  4569. "Matches a link with spaces, optional angular brackets around it.")
  4570. (defvar org-link-re-with-space2 nil
  4571. "Matches a link with spaces, optional angular brackets around it.")
  4572. (defvar org-link-re-with-space3 nil
  4573. "Matches a link with spaces, only for internal part in bracket links.")
  4574. (defvar org-angle-link-re nil
  4575. "Matches link with angular brackets, spaces are allowed.")
  4576. (defvar org-plain-link-re nil
  4577. "Matches plain link, without spaces.")
  4578. (defvar org-bracket-link-regexp nil
  4579. "Matches a link in double brackets.")
  4580. (defvar org-bracket-link-analytic-regexp nil
  4581. "Regular expression used to analyze links.
  4582. Here is what the match groups contain after a match:
  4583. 1: http:
  4584. 2: http
  4585. 3: path
  4586. 4: [desc]
  4587. 5: desc")
  4588. (defvar org-bracket-link-analytic-regexp++ nil
  4589. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4590. (defvar org-any-link-re nil
  4591. "Regular expression matching any link.")
  4592. (defcustom org-match-sexp-depth 3
  4593. "Number of stacked braces for sub/superscript matching.
  4594. This has to be set before loading org.el to be effective."
  4595. :group 'org-export-translation ; ??????????????????????????/
  4596. :type 'integer)
  4597. (defun org-create-multibrace-regexp (left right n)
  4598. "Create a regular expression which will match a balanced sexp.
  4599. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4600. as single character strings.
  4601. The regexp returned will match the entire expression including the
  4602. delimiters. It will also define a single group which contains the
  4603. match except for the outermost delimiters. The maximum depth of
  4604. stacked delimiters is N. Escaping delimiters is not possible."
  4605. (let* ((nothing (concat "[^" left right "]*?"))
  4606. (or "\\|")
  4607. (re nothing)
  4608. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4609. (while (> n 1)
  4610. (setq n (1- n)
  4611. re (concat re or next)
  4612. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4613. (concat left "\\(" re "\\)" right)))
  4614. (defvar org-match-substring-regexp
  4615. (concat
  4616. "\\([^\\]\\|^\\)\\([_^]\\)\\("
  4617. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4618. "\\|"
  4619. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  4620. "\\|"
  4621. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  4622. "The regular expression matching a sub- or superscript.")
  4623. (defvar org-match-substring-with-braces-regexp
  4624. (concat
  4625. "\\([^\\]\\|^\\)\\([_^]\\)\\("
  4626. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4627. "\\)")
  4628. "The regular expression matching a sub- or superscript, forcing braces.")
  4629. (defun org-make-link-regexps ()
  4630. "Update the link regular expressions.
  4631. This should be called after the variable `org-link-types' has changed."
  4632. (setq org-link-types-re
  4633. (concat
  4634. "\\`\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):")
  4635. org-link-re-with-space
  4636. (concat
  4637. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4638. "\\([^" org-non-link-chars " ]"
  4639. "[^" org-non-link-chars "]*"
  4640. "[^" org-non-link-chars " ]\\)>?")
  4641. org-link-re-with-space2
  4642. (concat
  4643. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4644. "\\([^" org-non-link-chars " ]"
  4645. "[^\t\n\r]*"
  4646. "[^" org-non-link-chars " ]\\)>?")
  4647. org-link-re-with-space3
  4648. (concat
  4649. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4650. "\\([^" org-non-link-chars " ]"
  4651. "[^\t\n\r]*\\)")
  4652. org-angle-link-re
  4653. (concat
  4654. "<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4655. "\\([^" org-non-link-chars " ]"
  4656. "[^" org-non-link-chars "]*"
  4657. "\\)>")
  4658. org-plain-link-re
  4659. (concat
  4660. "\\<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4661. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  4662. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4663. org-bracket-link-regexp
  4664. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4665. org-bracket-link-analytic-regexp
  4666. (concat
  4667. "\\[\\["
  4668. "\\(\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):\\)?"
  4669. "\\([^]]+\\)"
  4670. "\\]"
  4671. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4672. "\\]")
  4673. org-bracket-link-analytic-regexp++
  4674. (concat
  4675. "\\[\\["
  4676. "\\(\\(" (mapconcat 'regexp-quote (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  4677. "\\([^]]+\\)"
  4678. "\\]"
  4679. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4680. "\\]")
  4681. org-any-link-re
  4682. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4683. org-angle-link-re "\\)\\|\\("
  4684. org-plain-link-re "\\)")))
  4685. (org-make-link-regexps)
  4686. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
  4687. "Regular expression for fast time stamp matching.")
  4688. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
  4689. "Regular expression for fast time stamp matching.")
  4690. (defconst org-ts-regexp0
  4691. "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\( +[^]+0-9>\r\n -]+\\)?\\( +\\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4692. "Regular expression matching time strings for analysis.
  4693. This one does not require the space after the date, so it can be used
  4694. on a string that terminates immediately after the date.")
  4695. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4696. "Regular expression matching time strings for analysis.")
  4697. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  4698. "Regular expression matching time stamps, with groups.")
  4699. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  4700. "Regular expression matching time stamps (also [..]), with groups.")
  4701. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  4702. "Regular expression matching a time stamp range.")
  4703. (defconst org-tr-regexp-both
  4704. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  4705. "Regular expression matching a time stamp range.")
  4706. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  4707. org-ts-regexp "\\)?")
  4708. "Regular expression matching a time stamp or time stamp range.")
  4709. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  4710. org-ts-regexp-both "\\)?")
  4711. "Regular expression matching a time stamp or time stamp range.
  4712. The time stamps may be either active or inactive.")
  4713. (defvar org-emph-face nil)
  4714. (defun org-do-emphasis-faces (limit)
  4715. "Run through the buffer and add overlays to emphasized strings."
  4716. (let (rtn a)
  4717. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  4718. (if (not (= (char-after (match-beginning 3))
  4719. (char-after (match-beginning 4))))
  4720. (progn
  4721. (setq rtn t)
  4722. (setq a (assoc (match-string 3) org-emphasis-alist))
  4723. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  4724. 'face
  4725. (nth 1 a))
  4726. (and (nth 4 a)
  4727. (org-remove-flyspell-overlays-in
  4728. (match-beginning 0) (match-end 0)))
  4729. (add-text-properties (match-beginning 2) (match-end 2)
  4730. '(font-lock-multiline t org-emphasis t))
  4731. (when org-hide-emphasis-markers
  4732. (add-text-properties (match-end 4) (match-beginning 5)
  4733. '(invisible org-link))
  4734. (add-text-properties (match-beginning 3) (match-end 3)
  4735. '(invisible org-link)))))
  4736. (backward-char 1))
  4737. rtn))
  4738. (defun org-emphasize (&optional char)
  4739. "Insert or change an emphasis, i.e. a font like bold or italic.
  4740. If there is an active region, change that region to a new emphasis.
  4741. If there is no region, just insert the marker characters and position
  4742. the cursor between them.
  4743. CHAR should be either the marker character, or the first character of the
  4744. HTML tag associated with that emphasis. If CHAR is a space, the means
  4745. to remove the emphasis of the selected region.
  4746. If char is not given (for example in an interactive call) it
  4747. will be prompted for."
  4748. (interactive)
  4749. (let ((eal org-emphasis-alist) e det
  4750. (erc org-emphasis-regexp-components)
  4751. (prompt "")
  4752. (string "") beg end move tag c s)
  4753. (if (org-region-active-p)
  4754. (setq beg (region-beginning) end (region-end)
  4755. string (buffer-substring beg end))
  4756. (setq move t))
  4757. (while (setq e (pop eal))
  4758. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  4759. c (aref tag 0))
  4760. (push (cons c (string-to-char (car e))) det)
  4761. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  4762. (substring tag 1)))))
  4763. (setq det (nreverse det))
  4764. (unless char
  4765. (message "%s" (concat "Emphasis marker or tag:" prompt))
  4766. (setq char (read-char-exclusive)))
  4767. (setq char (or (cdr (assoc char det)) char))
  4768. (if (equal char ?\ )
  4769. (setq s "" move nil)
  4770. (unless (assoc (char-to-string char) org-emphasis-alist)
  4771. (error "No such emphasis marker: \"%c\"" char))
  4772. (setq s (char-to-string char)))
  4773. (while (and (> (length string) 1)
  4774. (equal (substring string 0 1) (substring string -1))
  4775. (assoc (substring string 0 1) org-emphasis-alist))
  4776. (setq string (substring string 1 -1)))
  4777. (setq string (concat s string s))
  4778. (if beg (delete-region beg end))
  4779. (unless (or (bolp)
  4780. (string-match (concat "[" (nth 0 erc) "\n]")
  4781. (char-to-string (char-before (point)))))
  4782. (insert " "))
  4783. (unless (or (eobp)
  4784. (string-match (concat "[" (nth 1 erc) "\n]")
  4785. (char-to-string (char-after (point)))))
  4786. (insert " ") (backward-char 1))
  4787. (insert string)
  4788. (and move (backward-char 1))))
  4789. (defconst org-nonsticky-props
  4790. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  4791. (defsubst org-rear-nonsticky-at (pos)
  4792. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  4793. (defun org-activate-plain-links (limit)
  4794. "Run through the buffer and add overlays to links."
  4795. (catch 'exit
  4796. (let (f)
  4797. (if (re-search-forward org-plain-link-re limit t)
  4798. (progn
  4799. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4800. (setq f (get-text-property (match-beginning 0) 'face))
  4801. (if (or (eq f 'org-tag)
  4802. (and (listp f) (memq 'org-tag f)))
  4803. nil
  4804. (add-text-properties (match-beginning 0) (match-end 0)
  4805. (list 'mouse-face 'highlight
  4806. 'face 'org-link
  4807. 'keymap org-mouse-map))
  4808. (org-rear-nonsticky-at (match-end 0)))
  4809. t)))))
  4810. (defun org-activate-code (limit)
  4811. (if (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t)
  4812. (progn
  4813. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4814. (remove-text-properties (match-beginning 0) (match-end 0)
  4815. '(display t invisible t intangible t))
  4816. t)))
  4817. (defcustom org-src-fontify-natively nil
  4818. "When non-nil, fontify code in code blocks."
  4819. :type 'boolean
  4820. :version "24.1"
  4821. :group 'org-appearance
  4822. :group 'org-babel)
  4823. (defcustom org-src-prevent-auto-filling nil
  4824. "When non-nil, prevent auto-filling in src blocks."
  4825. :type 'boolean
  4826. :version "24.1"
  4827. :group 'org-appearance
  4828. :group 'org-babel)
  4829. (defcustom org-allow-promoting-top-level-subtree nil
  4830. "When non-nil, allow promoting a top level subtree.
  4831. The leading star of the top level headline will be replaced
  4832. by a #."
  4833. :type 'boolean
  4834. :version "24.1"
  4835. :group 'org-appearance)
  4836. (defun org-fontify-meta-lines-and-blocks (limit)
  4837. (condition-case nil
  4838. (org-fontify-meta-lines-and-blocks-1 limit)
  4839. (error (message "org-mode fontification error"))))
  4840. (defun org-fontify-meta-lines-and-blocks-1 (limit)
  4841. "Fontify #+ lines and blocks, in the correct ways."
  4842. (let ((case-fold-search t))
  4843. (if (re-search-forward
  4844. "^\\([ \t]*#\\+\\(\\([a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  4845. limit t)
  4846. (let ((beg (match-beginning 0))
  4847. (block-start (match-end 0))
  4848. (block-end nil)
  4849. (lang (match-string 7))
  4850. (beg1 (line-beginning-position 2))
  4851. (dc1 (downcase (match-string 2)))
  4852. (dc3 (downcase (match-string 3)))
  4853. end end1 quoting block-type ovl)
  4854. (cond
  4855. ((member dc1 '("html:" "ascii:" "latex:" "docbook:"))
  4856. ;; a single line of backend-specific content
  4857. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4858. (remove-text-properties (match-beginning 0) (match-end 0)
  4859. '(display t invisible t intangible t))
  4860. (add-text-properties (match-beginning 1) (match-end 3)
  4861. '(font-lock-fontified t face org-meta-line))
  4862. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  4863. '(font-lock-fontified t face org-block))
  4864. ; for backend-specific code
  4865. t)
  4866. ((and (match-end 4) (equal dc3 "begin"))
  4867. ;; Truly a block
  4868. (setq block-type (downcase (match-string 5))
  4869. quoting (member block-type org-protecting-blocks))
  4870. (when (re-search-forward
  4871. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  4872. nil t) ;; on purpose, we look further than LIMIT
  4873. (setq end (min (point-max) (match-end 0))
  4874. end1 (min (point-max) (1- (match-beginning 0))))
  4875. (setq block-end (match-beginning 0))
  4876. (when quoting
  4877. (remove-text-properties beg end
  4878. '(display t invisible t intangible t)))
  4879. (add-text-properties
  4880. beg end
  4881. '(font-lock-fontified t font-lock-multiline t))
  4882. (add-text-properties beg beg1 '(face org-meta-line))
  4883. (add-text-properties end1 (min (point-max) (1+ end))
  4884. '(face org-meta-line)) ; for end_src
  4885. (cond
  4886. ((and lang (not (string= lang "")) org-src-fontify-natively)
  4887. (org-src-font-lock-fontify-block lang block-start block-end)
  4888. ;; remove old background overlays
  4889. (mapc (lambda (ov)
  4890. (if (eq (overlay-get ov 'face) 'org-block-background)
  4891. (delete-overlay ov)))
  4892. (overlays-at (/ (+ beg1 block-end) 2)))
  4893. ;; add a background overlay
  4894. (setq ovl (make-overlay beg1 block-end))
  4895. (overlay-put ovl 'face 'org-block-background)
  4896. (overlay-put ovl 'evaporate t)) ;; make it go away when empty
  4897. (quoting
  4898. (add-text-properties beg1 (min (point-max) (1+ end1))
  4899. '(face org-block))) ; end of source block
  4900. ((not org-fontify-quote-and-verse-blocks))
  4901. ((string= block-type "quote")
  4902. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-quote)))
  4903. ((string= block-type "verse")
  4904. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-verse))))
  4905. (add-text-properties beg beg1 '(face org-block-begin-line))
  4906. (add-text-properties (min (point-max) (1+ end)) (min (point-max) (1+ end1))
  4907. '(face org-block-end-line))
  4908. t))
  4909. ((member dc1 '("title:" "author:" "email:" "date:"))
  4910. (add-text-properties
  4911. beg (match-end 3)
  4912. (if (member (intern (substring dc1 0 -1)) org-hidden-keywords)
  4913. '(font-lock-fontified t invisible t)
  4914. '(font-lock-fontified t face org-document-info-keyword)))
  4915. (add-text-properties
  4916. (match-beginning 6) (match-end 6)
  4917. (if (string-equal dc1 "title:")
  4918. '(font-lock-fontified t face org-document-title)
  4919. '(font-lock-fontified t face org-document-info))))
  4920. ((not (member (char-after beg) '(?\ ?\t)))
  4921. ;; just any other in-buffer setting, but not indented
  4922. (add-text-properties
  4923. beg (match-end 0)
  4924. '(font-lock-fontified t face org-meta-line))
  4925. t)
  4926. ((or (member dc1 '("begin:" "end:" "caption:" "label:"
  4927. "orgtbl:" "tblfm:" "tblname:" "results:"
  4928. "call:" "header:" "headers:" "name:"))
  4929. (and (match-end 4) (equal dc3 "attr")))
  4930. (add-text-properties
  4931. beg (match-end 0)
  4932. '(font-lock-fontified t face org-meta-line))
  4933. t)
  4934. ((member dc3 '(" " ""))
  4935. (add-text-properties
  4936. beg (match-end 0)
  4937. '(font-lock-fontified t face font-lock-comment-face)))
  4938. (t nil))))))
  4939. (defun org-strip-protective-commas (beg end)
  4940. "Strip protective commas between BEG and END in the current buffer."
  4941. (interactive "r")
  4942. (save-excursion
  4943. (save-match-data
  4944. (goto-char beg)
  4945. (let ((front-line (save-excursion
  4946. (re-search-forward
  4947. "[^[:space:]]" end t)
  4948. (goto-char (match-beginning 0))
  4949. (current-column))))
  4950. (while (re-search-forward "^[ \t]*\\(,\\)\\([*]\\|#\\+\\)" end t)
  4951. (goto-char (match-beginning 1))
  4952. (when (= (current-column) front-line)
  4953. (replace-match "" nil nil nil 1)))))))
  4954. (defun org-activate-angle-links (limit)
  4955. "Run through the buffer and add overlays to links."
  4956. (if (re-search-forward org-angle-link-re limit t)
  4957. (progn
  4958. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4959. (add-text-properties (match-beginning 0) (match-end 0)
  4960. (list 'mouse-face 'highlight
  4961. 'keymap org-mouse-map))
  4962. (org-rear-nonsticky-at (match-end 0))
  4963. t)))
  4964. (defun org-activate-footnote-links (limit)
  4965. "Run through the buffer and add overlays to footnotes."
  4966. (let ((fn (org-footnote-next-reference-or-definition limit)))
  4967. (when fn
  4968. (let ((beg (nth 1 fn)) (end (nth 2 fn)))
  4969. (org-remove-flyspell-overlays-in beg end)
  4970. (add-text-properties beg end
  4971. (list 'mouse-face 'highlight
  4972. 'keymap org-mouse-map
  4973. 'help-echo
  4974. (if (= (point-at-bol) beg)
  4975. "Footnote definition"
  4976. "Footnote reference")
  4977. 'font-lock-fontified t
  4978. 'font-lock-multiline t
  4979. 'face 'org-footnote))))))
  4980. (defun org-activate-bracket-links (limit)
  4981. "Run through the buffer and add overlays to bracketed links."
  4982. (if (re-search-forward org-bracket-link-regexp limit t)
  4983. (let* ((help (concat "LINK: "
  4984. (org-match-string-no-properties 1)))
  4985. ;; FIXME: above we should remove the escapes.
  4986. ;; but that requires another match, protecting match data,
  4987. ;; a lot of overhead for font-lock.
  4988. (ip (org-maybe-intangible
  4989. (list 'invisible 'org-link
  4990. 'keymap org-mouse-map 'mouse-face 'highlight
  4991. 'font-lock-multiline t 'help-echo help)))
  4992. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  4993. 'font-lock-multiline t 'help-echo help)))
  4994. ;; We need to remove the invisible property here. Table narrowing
  4995. ;; may have made some of this invisible.
  4996. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4997. (remove-text-properties (match-beginning 0) (match-end 0)
  4998. '(invisible nil))
  4999. (if (match-end 3)
  5000. (progn
  5001. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  5002. (org-rear-nonsticky-at (match-beginning 3))
  5003. (add-text-properties (match-beginning 3) (match-end 3) vp)
  5004. (org-rear-nonsticky-at (match-end 3))
  5005. (add-text-properties (match-end 3) (match-end 0) ip)
  5006. (org-rear-nonsticky-at (match-end 0)))
  5007. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  5008. (org-rear-nonsticky-at (match-beginning 1))
  5009. (add-text-properties (match-beginning 1) (match-end 1) vp)
  5010. (org-rear-nonsticky-at (match-end 1))
  5011. (add-text-properties (match-end 1) (match-end 0) ip)
  5012. (org-rear-nonsticky-at (match-end 0)))
  5013. t)))
  5014. (defun org-activate-dates (limit)
  5015. "Run through the buffer and add overlays to dates."
  5016. (if (re-search-forward org-tsr-regexp-both limit t)
  5017. (progn
  5018. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5019. (add-text-properties (match-beginning 0) (match-end 0)
  5020. (list 'mouse-face 'highlight
  5021. 'keymap org-mouse-map))
  5022. (org-rear-nonsticky-at (match-end 0))
  5023. (when org-display-custom-times
  5024. (if (match-end 3)
  5025. (org-display-custom-time (match-beginning 3) (match-end 3)))
  5026. (org-display-custom-time (match-beginning 1) (match-end 1)))
  5027. t)))
  5028. (defvar org-target-link-regexp nil
  5029. "Regular expression matching radio targets in plain text.")
  5030. (make-variable-buffer-local 'org-target-link-regexp)
  5031. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  5032. "Regular expression matching a link target.")
  5033. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  5034. "Regular expression matching a radio target.")
  5035. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  5036. "Regular expression matching any target.")
  5037. (defun org-activate-target-links (limit)
  5038. "Run through the buffer and add overlays to target matches."
  5039. (when org-target-link-regexp
  5040. (let ((case-fold-search t))
  5041. (if (re-search-forward org-target-link-regexp limit t)
  5042. (progn
  5043. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5044. (add-text-properties (match-beginning 0) (match-end 0)
  5045. (list 'mouse-face 'highlight
  5046. 'keymap org-mouse-map
  5047. 'help-echo "Radio target link"
  5048. 'org-linked-text t))
  5049. (org-rear-nonsticky-at (match-end 0))
  5050. t)))))
  5051. (defun org-update-radio-target-regexp ()
  5052. "Find all radio targets in this file and update the regular expression."
  5053. (interactive)
  5054. (when (memq 'radio org-activate-links)
  5055. (setq org-target-link-regexp
  5056. (org-make-target-link-regexp (org-all-targets 'radio)))
  5057. (org-restart-font-lock)))
  5058. (defun org-hide-wide-columns (limit)
  5059. (let (s e)
  5060. (setq s (text-property-any (point) (or limit (point-max))
  5061. 'org-cwidth t))
  5062. (when s
  5063. (setq e (next-single-property-change s 'org-cwidth))
  5064. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  5065. (goto-char e)
  5066. t)))
  5067. (defvar org-latex-and-specials-regexp nil
  5068. "Regular expression for highlighting export special stuff.")
  5069. (defvar org-match-substring-regexp)
  5070. (defvar org-match-substring-with-braces-regexp)
  5071. ;; This should be with the exporter code, but we also use if for font-locking
  5072. (defconst org-export-html-special-string-regexps
  5073. '(("\\\\-" . "&shy;")
  5074. ("---\\([^-]\\)" . "&mdash;\\1")
  5075. ("--\\([^-]\\)" . "&ndash;\\1")
  5076. ("\\.\\.\\." . "&hellip;"))
  5077. "Regular expressions for special string conversion.")
  5078. (defun org-compute-latex-and-specials-regexp ()
  5079. "Compute regular expression for stuff treated specially by exporters."
  5080. (if (not org-highlight-latex-fragments-and-specials)
  5081. (org-set-local 'org-latex-and-specials-regexp nil)
  5082. (require 'org-exp)
  5083. (let*
  5084. ((matchers (plist-get org-format-latex-options :matchers))
  5085. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  5086. org-latex-regexps)))
  5087. (org-export-allow-BIND nil)
  5088. (options (org-combine-plists (org-default-export-plist)
  5089. (org-infile-export-plist)))
  5090. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  5091. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  5092. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  5093. (org-export-html-expand (plist-get options :expand-quoted-html))
  5094. (org-export-with-special-strings (plist-get options :special-strings))
  5095. (re-sub
  5096. (cond
  5097. ((equal org-export-with-sub-superscripts '{})
  5098. (list org-match-substring-with-braces-regexp))
  5099. (org-export-with-sub-superscripts
  5100. (list org-match-substring-regexp))
  5101. (t nil)))
  5102. (re-latex
  5103. (if org-export-with-LaTeX-fragments
  5104. (mapcar (lambda (x) (nth 1 x)) latexs)))
  5105. (re-macros
  5106. (if org-export-with-TeX-macros
  5107. (list (concat "\\\\"
  5108. (regexp-opt
  5109. (append
  5110. (delq nil
  5111. (mapcar 'car-safe
  5112. (append org-entities-user
  5113. org-entities)))
  5114. (if (boundp 'org-latex-entities)
  5115. (mapcar (lambda (x)
  5116. (or (car-safe x) x))
  5117. org-latex-entities)
  5118. nil))
  5119. 'words))) ; FIXME
  5120. ))
  5121. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  5122. (re-special (if org-export-with-special-strings
  5123. (mapcar (lambda (x) (car x))
  5124. org-export-html-special-string-regexps)))
  5125. (re-rest
  5126. (delq nil
  5127. (list
  5128. (if org-export-html-expand "@<[^>\n]+>")
  5129. ))))
  5130. (org-set-local
  5131. 'org-latex-and-specials-regexp
  5132. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  5133. re-rest) "\\|")))))
  5134. (defun org-do-latex-and-special-faces (limit)
  5135. "Run through the buffer and add overlays to links."
  5136. (when org-latex-and-specials-regexp
  5137. (let (rtn d)
  5138. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  5139. limit t))
  5140. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  5141. 'face))
  5142. '(org-code org-verbatim underline)))
  5143. (progn
  5144. (setq rtn t
  5145. d (cond ((member (char-after (1+ (match-beginning 0)))
  5146. '(?_ ?^)) 1)
  5147. (t 0)))
  5148. (font-lock-prepend-text-property
  5149. (+ d (match-beginning 0)) (match-end 0)
  5150. 'face 'org-latex-and-export-specials)
  5151. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  5152. '(font-lock-multiline t)))))
  5153. rtn)))
  5154. (defun org-restart-font-lock ()
  5155. "Restart `font-lock-mode', to force refontification."
  5156. (when (and (boundp 'font-lock-mode) font-lock-mode)
  5157. (font-lock-mode -1)
  5158. (font-lock-mode 1)))
  5159. (defun org-all-targets (&optional radio)
  5160. "Return a list of all targets in this file.
  5161. With optional argument RADIO, only find radio targets."
  5162. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  5163. rtn)
  5164. (save-excursion
  5165. (goto-char (point-min))
  5166. (while (re-search-forward re nil t)
  5167. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  5168. rtn)))
  5169. (defun org-make-target-link-regexp (targets)
  5170. "Make regular expression matching all strings in TARGETS.
  5171. The regular expression finds the targets also if there is a line break
  5172. between words."
  5173. (and targets
  5174. (concat
  5175. "\\<\\("
  5176. (mapconcat
  5177. (lambda (x)
  5178. (setq x (regexp-quote x))
  5179. (while (string-match " +" x)
  5180. (setq x (replace-match "\\s-+" t t x)))
  5181. x)
  5182. targets
  5183. "\\|")
  5184. "\\)\\>")))
  5185. (defun org-activate-tags (limit)
  5186. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t)
  5187. (progn
  5188. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5189. (add-text-properties (match-beginning 1) (match-end 1)
  5190. (list 'mouse-face 'highlight
  5191. 'keymap org-mouse-map))
  5192. (org-rear-nonsticky-at (match-end 1))
  5193. t)))
  5194. (defun org-outline-level ()
  5195. "Compute the outline level of the heading at point.
  5196. This function assumes that the cursor is at the beginning of a line matched
  5197. by `outline-regexp'. Otherwise it returns garbage.
  5198. If this is called at a normal headline, the level is the number of stars.
  5199. Use `org-reduced-level' to remove the effect of `org-odd-levels'."
  5200. (save-excursion
  5201. (looking-at org-outline-regexp)
  5202. (1- (- (match-end 0) (match-beginning 0)))))
  5203. (defvar org-font-lock-keywords nil)
  5204. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\+?\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  5205. "Regular expression matching a property line.")
  5206. (defvar org-font-lock-hook nil
  5207. "Functions to be called for special font lock stuff.")
  5208. (defvar org-font-lock-set-keywords-hook nil
  5209. "Functions that can manipulate `org-font-lock-extra-keywords'.
  5210. This is called after `org-font-lock-extra-keywords' is defined, but before
  5211. it is installed to be used by font lock. This can be useful if something
  5212. needs to be inserted at a specific position in the font-lock sequence.")
  5213. (defun org-font-lock-hook (limit)
  5214. (run-hook-with-args 'org-font-lock-hook limit))
  5215. (defun org-set-font-lock-defaults ()
  5216. (let* ((em org-fontify-emphasized-text)
  5217. (lk org-activate-links)
  5218. (org-font-lock-extra-keywords
  5219. (list
  5220. ;; Call the hook
  5221. '(org-font-lock-hook)
  5222. ;; Headlines
  5223. `(,(if org-fontify-whole-heading-line
  5224. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  5225. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  5226. (1 (org-get-level-face 1))
  5227. (2 (org-get-level-face 2))
  5228. (3 (org-get-level-face 3)))
  5229. ;; Table lines
  5230. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  5231. (1 'org-table t))
  5232. ;; Table internals
  5233. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  5234. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  5235. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  5236. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  5237. ;; Drawers
  5238. (list org-drawer-regexp '(0 'org-special-keyword t))
  5239. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  5240. ;; Properties
  5241. (list org-property-re
  5242. '(1 'org-special-keyword t)
  5243. '(3 'org-property-value t))
  5244. ;; Links
  5245. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  5246. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  5247. (if (memq 'plain lk) '(org-activate-plain-links))
  5248. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  5249. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  5250. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  5251. (if (memq 'footnote lk) '(org-activate-footnote-links))
  5252. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  5253. '(org-hide-wide-columns (0 nil append))
  5254. ;; TODO keyword
  5255. (list (format org-heading-keyword-regexp-format
  5256. org-todo-regexp)
  5257. '(2 (org-get-todo-face 2) t))
  5258. ;; DONE
  5259. (if org-fontify-done-headline
  5260. (list (format org-heading-keyword-regexp-format
  5261. (concat
  5262. "\\(?:"
  5263. (mapconcat 'regexp-quote org-done-keywords "\\|")
  5264. "\\)"))
  5265. '(2 'org-headline-done t))
  5266. nil)
  5267. ;; Priorities
  5268. '(org-font-lock-add-priority-faces)
  5269. ;; Tags
  5270. '(org-font-lock-add-tag-faces)
  5271. ;; Special keywords
  5272. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  5273. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  5274. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  5275. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  5276. ;; Emphasis
  5277. (if em
  5278. (if (featurep 'xemacs)
  5279. '(org-do-emphasis-faces (0 nil append))
  5280. '(org-do-emphasis-faces)))
  5281. ;; Checkboxes
  5282. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  5283. 1 'org-checkbox prepend)
  5284. (if (cdr (assq 'checkbox org-list-automatic-rules))
  5285. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5286. (0 (org-get-checkbox-statistics-face) t)))
  5287. ;; Description list items
  5288. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  5289. 1 'org-list-dt prepend)
  5290. ;; ARCHIVEd headings
  5291. (list (concat
  5292. org-outline-regexp-bol
  5293. "\\(.*:" org-archive-tag ":.*\\)")
  5294. '(1 'org-archived prepend))
  5295. ;; Specials
  5296. '(org-do-latex-and-special-faces)
  5297. '(org-fontify-entities)
  5298. '(org-raise-scripts)
  5299. ;; Code
  5300. '(org-activate-code (1 'org-code t))
  5301. ;; COMMENT
  5302. (list (format org-heading-keyword-regexp-format
  5303. (concat "\\("
  5304. org-comment-string "\\|" org-quote-string
  5305. "\\)"))
  5306. '(2 'org-special-keyword t))
  5307. '("^#.*" (0 'font-lock-comment-face t))
  5308. ;; Blocks and meta lines
  5309. '(org-fontify-meta-lines-and-blocks)
  5310. )))
  5311. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5312. (run-hooks 'org-font-lock-set-keywords-hook)
  5313. ;; Now set the full font-lock-keywords
  5314. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  5315. (org-set-local 'font-lock-defaults
  5316. '(org-font-lock-keywords t nil nil backward-paragraph))
  5317. (kill-local-variable 'font-lock-keywords) nil))
  5318. (defun org-toggle-pretty-entities ()
  5319. "Toggle the composition display of entities as UTF8 characters."
  5320. (interactive)
  5321. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  5322. (org-restart-font-lock)
  5323. (if org-pretty-entities
  5324. (message "Entities are displayed as UTF8 characters")
  5325. (save-restriction
  5326. (widen)
  5327. (org-decompose-region (point-min) (point-max))
  5328. (message "Entities are displayed plain"))))
  5329. (defun org-fontify-entities (limit)
  5330. "Find an entity to fontify."
  5331. (let (ee)
  5332. (when org-pretty-entities
  5333. (catch 'match
  5334. (while (re-search-forward
  5335. "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]\n]\\)"
  5336. limit t)
  5337. (if (and (not (org-in-indented-comment-line))
  5338. (setq ee (org-entity-get (match-string 1)))
  5339. (= (length (nth 6 ee)) 1))
  5340. (let*
  5341. ((end (if (equal (match-string 2) "{}")
  5342. (match-end 2)
  5343. (match-end 1))))
  5344. (add-text-properties
  5345. (match-beginning 0) end
  5346. (list 'font-lock-fontified t))
  5347. (compose-region (match-beginning 0) end
  5348. (nth 6 ee) nil)
  5349. (backward-char 1)
  5350. (throw 'match t))))
  5351. nil))))
  5352. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5353. "Fontify string S like in Org-mode."
  5354. (with-temp-buffer
  5355. (insert s)
  5356. (let ((org-odd-levels-only odd-levels))
  5357. (org-mode)
  5358. (font-lock-fontify-buffer)
  5359. (buffer-string))))
  5360. (defvar org-m nil)
  5361. (defvar org-l nil)
  5362. (defvar org-f nil)
  5363. (defun org-get-level-face (n)
  5364. "Get the right face for match N in font-lock matching of headlines."
  5365. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5366. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5367. (if org-cycle-level-faces
  5368. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5369. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5370. (cond
  5371. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5372. ((eq n 2) org-f)
  5373. (t (if org-level-color-stars-only nil org-f))))
  5374. (defun org-get-todo-face (kwd)
  5375. "Get the right face for a TODO keyword KWD.
  5376. If KWD is a number, get the corresponding match group."
  5377. (if (numberp kwd) (setq kwd (match-string kwd)))
  5378. (or (org-face-from-face-or-color
  5379. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5380. (and (member kwd org-done-keywords) 'org-done)
  5381. 'org-todo))
  5382. (defun org-face-from-face-or-color (context inherit face-or-color)
  5383. "Create a face list that inherits INHERIT, but sets the foreground color.
  5384. When FACE-OR-COLOR is not a string, just return it."
  5385. (if (stringp face-or-color)
  5386. (list :inherit inherit
  5387. (cdr (assoc context org-faces-easy-properties))
  5388. face-or-color)
  5389. face-or-color))
  5390. (defun org-font-lock-add-tag-faces (limit)
  5391. "Add the special tag faces."
  5392. (when (and org-tag-faces org-tags-special-faces-re)
  5393. (while (re-search-forward org-tags-special-faces-re limit t)
  5394. (add-text-properties (match-beginning 1) (match-end 1)
  5395. (list 'face (org-get-tag-face 1)
  5396. 'font-lock-fontified t))
  5397. (backward-char 1))))
  5398. (defun org-font-lock-add-priority-faces (limit)
  5399. "Add the special priority faces."
  5400. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  5401. (when (save-match-data (org-at-heading-p))
  5402. (add-text-properties
  5403. (match-beginning 0) (match-end 0)
  5404. (list 'face (or (org-face-from-face-or-color
  5405. 'priority 'org-special-keyword
  5406. (cdr (assoc (char-after (match-beginning 1))
  5407. org-priority-faces)))
  5408. 'org-special-keyword)
  5409. 'font-lock-fontified t)))))
  5410. (defun org-get-tag-face (kwd)
  5411. "Get the right face for a TODO keyword KWD.
  5412. If KWD is a number, get the corresponding match group."
  5413. (if (numberp kwd) (setq kwd (match-string kwd)))
  5414. (or (org-face-from-face-or-color
  5415. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  5416. 'org-tag))
  5417. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5418. "Remove fontification and activation overlays from links."
  5419. (font-lock-default-unfontify-region beg end)
  5420. (let* ((buffer-undo-list t)
  5421. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5422. (inhibit-modification-hooks t)
  5423. deactivate-mark buffer-file-name buffer-file-truename)
  5424. (org-decompose-region beg end)
  5425. (remove-text-properties beg end
  5426. '(mouse-face t keymap t org-linked-text t
  5427. invisible t intangible t
  5428. org-no-flyspell t org-emphasis t))
  5429. (org-remove-font-lock-display-properties beg end)))
  5430. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5431. ((raise 0.3) (height 0.7))
  5432. ((raise -0.5))
  5433. ((raise 0.5)))
  5434. "Display properties for showing superscripts and subscripts.")
  5435. (defun org-remove-font-lock-display-properties (beg end)
  5436. "Remove specific display properties that have been added by font lock.
  5437. The will remove the raise properties that are used to show superscripts
  5438. and subscripts."
  5439. (let (next prop)
  5440. (while (< beg end)
  5441. (setq next (next-single-property-change beg 'display nil end)
  5442. prop (get-text-property beg 'display))
  5443. (if (member prop org-script-display)
  5444. (put-text-property beg next 'display nil))
  5445. (setq beg next))))
  5446. (defun org-raise-scripts (limit)
  5447. "Add raise properties to sub/superscripts."
  5448. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts)
  5449. (if (re-search-forward
  5450. (if (eq org-use-sub-superscripts t)
  5451. org-match-substring-regexp
  5452. org-match-substring-with-braces-regexp)
  5453. limit t)
  5454. (let* ((pos (point)) table-p comment-p
  5455. (mpos (match-beginning 3))
  5456. (emph-p (get-text-property mpos 'org-emphasis))
  5457. (link-p (get-text-property mpos 'mouse-face))
  5458. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5459. (goto-char (point-at-bol))
  5460. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5461. comment-p (org-looking-at-p "[ \t]*#"))
  5462. (goto-char pos)
  5463. ;; FIXME: Should we go back one character here, for a_b^c
  5464. ;; (goto-char (1- pos)) ;????????????????????
  5465. (if (or comment-p emph-p link-p keyw-p)
  5466. t
  5467. (put-text-property (match-beginning 3) (match-end 0)
  5468. 'display
  5469. (if (equal (char-after (match-beginning 2)) ?^)
  5470. (nth (if table-p 3 1) org-script-display)
  5471. (nth (if table-p 2 0) org-script-display)))
  5472. (add-text-properties (match-beginning 2) (match-end 2)
  5473. (list 'invisible t
  5474. 'org-dwidth t 'org-dwidth-n 1))
  5475. (if (and (eq (char-after (match-beginning 3)) ?{)
  5476. (eq (char-before (match-end 3)) ?}))
  5477. (progn
  5478. (add-text-properties
  5479. (match-beginning 3) (1+ (match-beginning 3))
  5480. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5481. (add-text-properties
  5482. (1- (match-end 3)) (match-end 3)
  5483. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))))
  5484. t)))))
  5485. ;;;; Visibility cycling, including org-goto and indirect buffer
  5486. ;;; Cycling
  5487. (defvar org-cycle-global-status nil)
  5488. (make-variable-buffer-local 'org-cycle-global-status)
  5489. (defvar org-cycle-subtree-status nil)
  5490. (make-variable-buffer-local 'org-cycle-subtree-status)
  5491. ;;;###autoload
  5492. (defvar org-inlinetask-min-level)
  5493. (defun org-cycle (&optional arg)
  5494. "TAB-action and visibility cycling for Org-mode.
  5495. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5496. is outline visibility cycling, but it also invokes other actions
  5497. in special contexts.
  5498. - When this function is called with a prefix argument, rotate the entire
  5499. buffer through 3 states (global cycling)
  5500. 1. OVERVIEW: Show only top-level headlines.
  5501. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5502. 3. SHOW ALL: Show everything.
  5503. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  5504. determined by the variable `org-startup-folded', and by any VISIBILITY
  5505. properties in the buffer.
  5506. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  5507. including any drawers.
  5508. - When inside a table, re-align the table and move to the next field.
  5509. - When point is at the beginning of a headline, rotate the subtree started
  5510. by this line through 3 different states (local cycling)
  5511. 1. FOLDED: Only the main headline is shown.
  5512. 2. CHILDREN: The main headline and the direct children are shown.
  5513. From this state, you can move to one of the children
  5514. and zoom in further.
  5515. 3. SUBTREE: Show the entire subtree, including body text.
  5516. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5517. - When point is at the beginning of an empty headline and the variable
  5518. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5519. of the headline by demoting and promoting it to likely levels. This
  5520. speeds up creation document structure by pressing TAB once or several
  5521. times right after creating a new headline.
  5522. - When there is a numeric prefix, go up to a heading with level ARG, do
  5523. a `show-subtree' and return to the previous cursor position. If ARG
  5524. is negative, go up that many levels.
  5525. - When point is not at the beginning of a headline, execute the global
  5526. binding for TAB, which is re-indenting the line. See the option
  5527. `org-cycle-emulate-tab' for details.
  5528. - Special case: if point is at the beginning of the buffer and there is
  5529. no headline in line 1, this function will act as if called with prefix arg
  5530. (C-u TAB, same as S-TAB) also when called without prefix arg.
  5531. But only if also the variable `org-cycle-global-at-bob' is t."
  5532. (interactive "P")
  5533. (org-load-modules-maybe)
  5534. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5535. (and org-cycle-level-after-item/entry-creation
  5536. (or (org-cycle-level)
  5537. (org-cycle-item-indentation))))
  5538. (let* ((limit-level
  5539. (or org-cycle-max-level
  5540. (and (boundp 'org-inlinetask-min-level)
  5541. org-inlinetask-min-level
  5542. (1- org-inlinetask-min-level))))
  5543. (nstars (and limit-level
  5544. (if org-odd-levels-only
  5545. (and limit-level (1- (* limit-level 2)))
  5546. limit-level)))
  5547. (org-outline-regexp
  5548. (if (not (derived-mode-p 'org-mode))
  5549. outline-regexp
  5550. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
  5551. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5552. (not (looking-at org-outline-regexp))))
  5553. (org-cycle-hook
  5554. (if bob-special
  5555. (delq 'org-optimize-window-after-visibility-change
  5556. (copy-sequence org-cycle-hook))
  5557. org-cycle-hook))
  5558. (pos (point)))
  5559. (if (or bob-special (equal arg '(4)))
  5560. ;; special case: use global cycling
  5561. (setq arg t))
  5562. (cond
  5563. ((equal arg '(16))
  5564. (setq last-command 'dummy)
  5565. (org-set-startup-visibility)
  5566. (message "Startup visibility, plus VISIBILITY properties"))
  5567. ((equal arg '(64))
  5568. (show-all)
  5569. (message "Entire buffer visible, including drawers"))
  5570. ;; Table: enter it or move to the next field.
  5571. ((org-at-table-p 'any)
  5572. (if (org-at-table.el-p)
  5573. (message "Use C-c ' to edit table.el tables")
  5574. (if arg (org-table-edit-field t)
  5575. (org-table-justify-field-maybe)
  5576. (call-interactively 'org-table-next-field))))
  5577. ((run-hook-with-args-until-success
  5578. 'org-tab-after-check-for-table-hook))
  5579. ;; Global cycling: delegate to `org-cycle-internal-global'.
  5580. ((eq arg t) (org-cycle-internal-global))
  5581. ;; Drawers: delegate to `org-flag-drawer'.
  5582. ((and org-drawers org-drawer-regexp
  5583. (save-excursion
  5584. (beginning-of-line 1)
  5585. (looking-at org-drawer-regexp)))
  5586. (org-flag-drawer ; toggle block visibility
  5587. (not (get-char-property (match-end 0) 'invisible))))
  5588. ;; Show-subtree, ARG levels up from here.
  5589. ((integerp arg)
  5590. (save-excursion
  5591. (org-back-to-heading)
  5592. (outline-up-heading (if (< arg 0) (- arg)
  5593. (- (funcall outline-level) arg)))
  5594. (org-show-subtree)))
  5595. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  5596. ((and (featurep 'org-inlinetask)
  5597. (org-inlinetask-at-task-p)
  5598. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5599. (org-inlinetask-toggle-visibility))
  5600. ((org-try-cdlatex-tab))
  5601. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  5602. ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
  5603. (save-excursion (beginning-of-line 1)
  5604. (looking-at org-outline-regexp)))
  5605. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5606. (org-cycle-internal-local))
  5607. ;; From there: TAB emulation and template completion.
  5608. (buffer-read-only (org-back-to-heading))
  5609. ((run-hook-with-args-until-success
  5610. 'org-tab-after-check-for-cycling-hook))
  5611. ((org-try-structure-completion))
  5612. ((run-hook-with-args-until-success
  5613. 'org-tab-before-tab-emulation-hook))
  5614. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5615. (or (not (bolp))
  5616. (not (looking-at org-outline-regexp))))
  5617. (call-interactively (global-key-binding "\t")))
  5618. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5619. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5620. (or (and (eq org-cycle-emulate-tab 'white)
  5621. (= (match-end 0) (point-at-eol)))
  5622. (and (eq org-cycle-emulate-tab 'whitestart)
  5623. (>= (match-end 0) pos))))
  5624. t
  5625. (eq org-cycle-emulate-tab t))
  5626. (call-interactively (global-key-binding "\t")))
  5627. (t (save-excursion
  5628. (org-back-to-heading)
  5629. (org-cycle)))))))
  5630. (defun org-cycle-internal-global ()
  5631. "Do the global cycling action."
  5632. ;; Hack to avoid display of messages for .org attachments in Gnus
  5633. (let ((ga (string-match "\\*fontification" (buffer-name))))
  5634. (cond
  5635. ((and (eq last-command this-command)
  5636. (eq org-cycle-global-status 'overview))
  5637. ;; We just created the overview - now do table of contents
  5638. ;; This can be slow in very large buffers, so indicate action
  5639. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  5640. (unless ga (message "CONTENTS..."))
  5641. (org-content)
  5642. (unless ga (message "CONTENTS...done"))
  5643. (setq org-cycle-global-status 'contents)
  5644. (run-hook-with-args 'org-cycle-hook 'contents))
  5645. ((and (eq last-command this-command)
  5646. (eq org-cycle-global-status 'contents))
  5647. ;; We just showed the table of contents - now show everything
  5648. (run-hook-with-args 'org-pre-cycle-hook 'all)
  5649. (show-all)
  5650. (unless ga (message "SHOW ALL"))
  5651. (setq org-cycle-global-status 'all)
  5652. (run-hook-with-args 'org-cycle-hook 'all))
  5653. (t
  5654. ;; Default action: go to overview
  5655. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  5656. (org-overview)
  5657. (unless ga (message "OVERVIEW"))
  5658. (setq org-cycle-global-status 'overview)
  5659. (run-hook-with-args 'org-cycle-hook 'overview)))))
  5660. (defun org-cycle-internal-local ()
  5661. "Do the local cycling action."
  5662. (let ((goal-column 0) eoh eol eos has-children children-skipped struct)
  5663. ;; First, determine end of headline (EOH), end of subtree or item
  5664. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  5665. (save-excursion
  5666. (if (org-at-item-p)
  5667. (progn
  5668. (beginning-of-line)
  5669. (setq struct (org-list-struct))
  5670. (setq eoh (point-at-eol))
  5671. (setq eos (org-list-get-item-end-before-blank (point) struct))
  5672. (setq has-children (org-list-has-child-p (point) struct)))
  5673. (org-back-to-heading)
  5674. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  5675. (setq eos (save-excursion
  5676. (org-end-of-subtree t)
  5677. (unless (eobp)
  5678. (skip-chars-forward " \t\n"))
  5679. (if (eobp) (point) (1- (point)))))
  5680. (setq has-children
  5681. (or (save-excursion
  5682. (let ((level (funcall outline-level)))
  5683. (outline-next-heading)
  5684. (and (org-at-heading-p t)
  5685. (> (funcall outline-level) level))))
  5686. (save-excursion
  5687. (org-list-search-forward (org-item-beginning-re) eos t)))))
  5688. ;; Determine end invisible part of buffer (EOL)
  5689. (beginning-of-line 2)
  5690. ;; XEmacs doesn't have `next-single-char-property-change'
  5691. (if (featurep 'xemacs)
  5692. (while (and (not (eobp)) ;; this is like `next-line'
  5693. (get-char-property (1- (point)) 'invisible))
  5694. (beginning-of-line 2))
  5695. (while (and (not (eobp)) ;; this is like `next-line'
  5696. (get-char-property (1- (point)) 'invisible))
  5697. (goto-char (next-single-char-property-change (point) 'invisible))
  5698. (and (eolp) (beginning-of-line 2))))
  5699. (setq eol (point)))
  5700. ;; Find out what to do next and set `this-command'
  5701. (cond
  5702. ((= eos eoh)
  5703. ;; Nothing is hidden behind this heading
  5704. (run-hook-with-args 'org-pre-cycle-hook 'empty)
  5705. (message "EMPTY ENTRY")
  5706. (setq org-cycle-subtree-status nil)
  5707. (save-excursion
  5708. (goto-char eos)
  5709. (outline-next-heading)
  5710. (if (outline-invisible-p) (org-flag-heading nil))))
  5711. ((and (or (>= eol eos)
  5712. (not (string-match "\\S-" (buffer-substring eol eos))))
  5713. (or has-children
  5714. (not (setq children-skipped
  5715. org-cycle-skip-children-state-if-no-children))))
  5716. ;; Entire subtree is hidden in one line: children view
  5717. (run-hook-with-args 'org-pre-cycle-hook 'children)
  5718. (if (org-at-item-p)
  5719. (org-list-set-item-visibility (point-at-bol) struct 'children)
  5720. (org-show-entry)
  5721. (org-with-limited-levels (show-children))
  5722. ;; FIXME: This slows down the func way too much.
  5723. ;; How keep drawers hidden in subtree anyway?
  5724. ;; (when (memq 'org-cycle-hide-drawers org-cycle-hook)
  5725. ;; (org-cycle-hide-drawers 'subtree))
  5726. ;; Fold every list in subtree to top-level items.
  5727. (when (eq org-cycle-include-plain-lists 'integrate)
  5728. (save-excursion
  5729. (org-back-to-heading)
  5730. (while (org-list-search-forward (org-item-beginning-re) eos t)
  5731. (beginning-of-line 1)
  5732. (let* ((struct (org-list-struct))
  5733. (prevs (org-list-prevs-alist struct))
  5734. (end (org-list-get-bottom-point struct)))
  5735. (mapc (lambda (e) (org-list-set-item-visibility e struct 'folded))
  5736. (org-list-get-all-items (point) struct prevs))
  5737. (goto-char end))))))
  5738. (message "CHILDREN")
  5739. (save-excursion
  5740. (goto-char eos)
  5741. (outline-next-heading)
  5742. (if (outline-invisible-p) (org-flag-heading nil)))
  5743. (setq org-cycle-subtree-status 'children)
  5744. (run-hook-with-args 'org-cycle-hook 'children))
  5745. ((or children-skipped
  5746. (and (eq last-command this-command)
  5747. (eq org-cycle-subtree-status 'children)))
  5748. ;; We just showed the children, or no children are there,
  5749. ;; now show everything.
  5750. (run-hook-with-args 'org-pre-cycle-hook 'subtree)
  5751. (outline-flag-region eoh eos nil)
  5752. (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  5753. (setq org-cycle-subtree-status 'subtree)
  5754. (run-hook-with-args 'org-cycle-hook 'subtree))
  5755. (t
  5756. ;; Default action: hide the subtree.
  5757. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  5758. (outline-flag-region eoh eos t)
  5759. (message "FOLDED")
  5760. (setq org-cycle-subtree-status 'folded)
  5761. (run-hook-with-args 'org-cycle-hook 'folded)))))
  5762. ;;;###autoload
  5763. (defun org-global-cycle (&optional arg)
  5764. "Cycle the global visibility. For details see `org-cycle'.
  5765. With \\[universal-argument] prefix arg, switch to startup visibility.
  5766. With a numeric prefix, show all headlines up to that level."
  5767. (interactive "P")
  5768. (let ((org-cycle-include-plain-lists
  5769. (if (derived-mode-p 'org-mode) org-cycle-include-plain-lists nil)))
  5770. (cond
  5771. ((integerp arg)
  5772. (show-all)
  5773. (hide-sublevels arg)
  5774. (setq org-cycle-global-status 'contents))
  5775. ((equal arg '(4))
  5776. (org-set-startup-visibility)
  5777. (message "Startup visibility, plus VISIBILITY properties."))
  5778. (t
  5779. (org-cycle '(4))))))
  5780. (defun org-set-startup-visibility ()
  5781. "Set the visibility required by startup options and properties."
  5782. (cond
  5783. ((eq org-startup-folded t)
  5784. (org-cycle '(4)))
  5785. ((eq org-startup-folded 'content)
  5786. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  5787. (org-cycle '(4)) (org-cycle '(4)))))
  5788. (unless (eq org-startup-folded 'showeverything)
  5789. (if org-hide-block-startup (org-hide-block-all))
  5790. (org-set-visibility-according-to-property 'no-cleanup)
  5791. (org-cycle-hide-archived-subtrees 'all)
  5792. (org-cycle-hide-drawers 'all)
  5793. (org-cycle-show-empty-lines t)))
  5794. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  5795. "Switch subtree visibilities according to :VISIBILITY: property."
  5796. (interactive)
  5797. (let (org-show-entry-below state)
  5798. (save-excursion
  5799. (goto-char (point-min))
  5800. (while (re-search-forward
  5801. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  5802. nil t)
  5803. (setq state (match-string 1))
  5804. (save-excursion
  5805. (org-back-to-heading t)
  5806. (hide-subtree)
  5807. (org-reveal)
  5808. (cond
  5809. ((equal state '("fold" "folded"))
  5810. (hide-subtree))
  5811. ((equal state "children")
  5812. (org-show-hidden-entry)
  5813. (show-children))
  5814. ((equal state "content")
  5815. (save-excursion
  5816. (save-restriction
  5817. (org-narrow-to-subtree)
  5818. (org-content))))
  5819. ((member state '("all" "showall"))
  5820. (show-subtree)))))
  5821. (unless no-cleanup
  5822. (org-cycle-hide-archived-subtrees 'all)
  5823. (org-cycle-hide-drawers 'all)
  5824. (org-cycle-show-empty-lines 'all)))))
  5825. ;; This function uses outline-regexp instead of the more fundamental
  5826. ;; org-outline-regexp so that org-cycle-global works outside of Org
  5827. ;; buffers, where outline-regexp is needed.
  5828. (defun org-overview ()
  5829. "Switch to overview mode, showing only top-level headlines.
  5830. Really, this shows all headlines with level equal or greater than the level
  5831. of the first headline in the buffer. This is important, because if the
  5832. first headline is not level one, then (hide-sublevels 1) gives confusing
  5833. results."
  5834. (interactive)
  5835. (let ((level (save-excursion
  5836. (goto-char (point-min))
  5837. (if (re-search-forward (concat "^" outline-regexp) nil t)
  5838. (progn
  5839. (goto-char (match-beginning 0))
  5840. (funcall outline-level))))))
  5841. (and level (hide-sublevels level))))
  5842. (defun org-content (&optional arg)
  5843. "Show all headlines in the buffer, like a table of contents.
  5844. With numerical argument N, show content up to level N."
  5845. (interactive "P")
  5846. (save-excursion
  5847. ;; Visit all headings and show their offspring
  5848. (and (integerp arg) (org-overview))
  5849. (goto-char (point-max))
  5850. (catch 'exit
  5851. (while (and (progn (condition-case nil
  5852. (outline-previous-visible-heading 1)
  5853. (error (goto-char (point-min))))
  5854. t)
  5855. (looking-at org-outline-regexp))
  5856. (if (integerp arg)
  5857. (show-children (1- arg))
  5858. (show-branches))
  5859. (if (bobp) (throw 'exit nil))))))
  5860. (defun org-optimize-window-after-visibility-change (state)
  5861. "Adjust the window after a change in outline visibility.
  5862. This function is the default value of the hook `org-cycle-hook'."
  5863. (when (get-buffer-window (current-buffer))
  5864. (cond
  5865. ((eq state 'content) nil)
  5866. ((eq state 'all) nil)
  5867. ((eq state 'folded) nil)
  5868. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  5869. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  5870. (defun org-remove-empty-overlays-at (pos)
  5871. "Remove outline overlays that do not contain non-white stuff."
  5872. (mapc
  5873. (lambda (o)
  5874. (and (eq 'outline (overlay-get o 'invisible))
  5875. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  5876. (overlay-end o))))
  5877. (delete-overlay o)))
  5878. (overlays-at pos)))
  5879. (defun org-clean-visibility-after-subtree-move ()
  5880. "Fix visibility issues after moving a subtree."
  5881. ;; First, find a reasonable region to look at:
  5882. ;; Start two siblings above, end three below
  5883. (let* ((beg (save-excursion
  5884. (and (org-get-last-sibling)
  5885. (org-get-last-sibling))
  5886. (point)))
  5887. (end (save-excursion
  5888. (and (org-get-next-sibling)
  5889. (org-get-next-sibling)
  5890. (org-get-next-sibling))
  5891. (if (org-at-heading-p)
  5892. (point-at-eol)
  5893. (point))))
  5894. (level (looking-at "\\*+"))
  5895. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  5896. (save-excursion
  5897. (save-restriction
  5898. (narrow-to-region beg end)
  5899. (when re
  5900. ;; Properly fold already folded siblings
  5901. (goto-char (point-min))
  5902. (while (re-search-forward re nil t)
  5903. (if (and (not (outline-invisible-p))
  5904. (save-excursion
  5905. (goto-char (point-at-eol)) (outline-invisible-p)))
  5906. (hide-entry))))
  5907. (org-cycle-show-empty-lines 'overview)
  5908. (org-cycle-hide-drawers 'overview)))))
  5909. (defun org-cycle-show-empty-lines (state)
  5910. "Show empty lines above all visible headlines.
  5911. The region to be covered depends on STATE when called through
  5912. `org-cycle-hook'. Lisp program can use t for STATE to get the
  5913. entire buffer covered. Note that an empty line is only shown if there
  5914. are at least `org-cycle-separator-lines' empty lines before the headline."
  5915. (when (not (= org-cycle-separator-lines 0))
  5916. (save-excursion
  5917. (let* ((n (abs org-cycle-separator-lines))
  5918. (re (cond
  5919. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  5920. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  5921. (t (let ((ns (number-to-string (- n 2))))
  5922. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  5923. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  5924. beg end b e)
  5925. (cond
  5926. ((memq state '(overview contents t))
  5927. (setq beg (point-min) end (point-max)))
  5928. ((memq state '(children folded))
  5929. (setq beg (point) end (progn (org-end-of-subtree t t)
  5930. (beginning-of-line 2)
  5931. (point)))))
  5932. (when beg
  5933. (goto-char beg)
  5934. (while (re-search-forward re end t)
  5935. (unless (get-char-property (match-end 1) 'invisible)
  5936. (setq e (match-end 1))
  5937. (if (< org-cycle-separator-lines 0)
  5938. (setq b (save-excursion
  5939. (goto-char (match-beginning 0))
  5940. (org-back-over-empty-lines)
  5941. (if (save-excursion
  5942. (goto-char (max (point-min) (1- (point))))
  5943. (org-at-heading-p))
  5944. (1- (point))
  5945. (point))))
  5946. (setq b (match-beginning 1)))
  5947. (outline-flag-region b e nil)))))))
  5948. ;; Never hide empty lines at the end of the file.
  5949. (save-excursion
  5950. (goto-char (point-max))
  5951. (outline-previous-heading)
  5952. (outline-end-of-heading)
  5953. (if (and (looking-at "[ \t\n]+")
  5954. (= (match-end 0) (point-max)))
  5955. (outline-flag-region (point) (match-end 0) nil))))
  5956. (defun org-show-empty-lines-in-parent ()
  5957. "Move to the parent and re-show empty lines before visible headlines."
  5958. (save-excursion
  5959. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  5960. (org-cycle-show-empty-lines context))))
  5961. (defun org-files-list ()
  5962. "Return `org-agenda-files' list, plus all open org-mode files.
  5963. This is useful for operations that need to scan all of a user's
  5964. open and agenda-wise Org files."
  5965. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  5966. (dolist (buf (buffer-list))
  5967. (with-current-buffer buf
  5968. (if (and (derived-mode-p 'org-mode) (buffer-file-name))
  5969. (let ((file (expand-file-name (buffer-file-name))))
  5970. (unless (member file files)
  5971. (push file files))))))
  5972. files))
  5973. (defsubst org-entry-beginning-position ()
  5974. "Return the beginning position of the current entry."
  5975. (save-excursion (outline-back-to-heading t) (point)))
  5976. (defsubst org-entry-end-position ()
  5977. "Return the end position of the current entry."
  5978. (save-excursion (outline-next-heading) (point)))
  5979. (defun org-cycle-hide-drawers (state)
  5980. "Re-hide all drawers after a visibility state change."
  5981. (when (and (derived-mode-p 'org-mode)
  5982. (not (memq state '(overview folded contents))))
  5983. (save-excursion
  5984. (let* ((globalp (memq state '(contents all)))
  5985. (beg (if globalp (point-min) (point)))
  5986. (end (if globalp (point-max)
  5987. (if (eq state 'children)
  5988. (save-excursion (outline-next-heading) (point))
  5989. (org-end-of-subtree t)))))
  5990. (goto-char beg)
  5991. (while (re-search-forward org-drawer-regexp end t)
  5992. (org-flag-drawer t))))))
  5993. (defun org-flag-drawer (flag)
  5994. "When FLAG is non-nil, hide the drawer we are within.
  5995. Otherwise make it visible."
  5996. (save-excursion
  5997. (beginning-of-line 1)
  5998. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  5999. (let ((b (match-end 0)))
  6000. (if (re-search-forward
  6001. "^[ \t]*:END:"
  6002. (save-excursion (outline-next-heading) (point)) t)
  6003. (outline-flag-region b (point-at-eol) flag)
  6004. (error ":END: line missing at position %s" b))))))
  6005. (defun org-subtree-end-visible-p ()
  6006. "Is the end of the current subtree visible?"
  6007. (pos-visible-in-window-p
  6008. (save-excursion (org-end-of-subtree t) (point))))
  6009. (defun org-first-headline-recenter (&optional N)
  6010. "Move cursor to the first headline and recenter the headline.
  6011. Optional argument N means put the headline into the Nth line of the window."
  6012. (goto-char (point-min))
  6013. (when (re-search-forward (concat "^\\(" org-outline-regexp "\\)") nil t)
  6014. (beginning-of-line)
  6015. (recenter (prefix-numeric-value N))))
  6016. ;;; Saving and restoring visibility
  6017. (defun org-outline-overlay-data (&optional use-markers)
  6018. "Return a list of the locations of all outline overlays.
  6019. These are overlays with the `invisible' property value `outline'.
  6020. The return value is a list of cons cells, with start and stop
  6021. positions for each overlay.
  6022. If USE-MARKERS is set, return the positions as markers."
  6023. (let (beg end)
  6024. (save-excursion
  6025. (save-restriction
  6026. (widen)
  6027. (delq nil
  6028. (mapcar (lambda (o)
  6029. (when (eq (overlay-get o 'invisible) 'outline)
  6030. (setq beg (overlay-start o)
  6031. end (overlay-end o))
  6032. (and beg end (> end beg)
  6033. (if use-markers
  6034. (cons (move-marker (make-marker) beg)
  6035. (move-marker (make-marker) end))
  6036. (cons beg end)))))
  6037. (overlays-in (point-min) (point-max))))))))
  6038. (defun org-set-outline-overlay-data (data)
  6039. "Create visibility overlays for all positions in DATA.
  6040. DATA should have been made by `org-outline-overlay-data'."
  6041. (let (o)
  6042. (save-excursion
  6043. (save-restriction
  6044. (widen)
  6045. (show-all)
  6046. (mapc (lambda (c)
  6047. (outline-flag-region (car c) (cdr c) t))
  6048. data)))))
  6049. ;;; Folding of blocks
  6050. (defvar org-hide-block-overlays nil
  6051. "Overlays hiding blocks.")
  6052. (make-variable-buffer-local 'org-hide-block-overlays)
  6053. (defun org-block-map (function &optional start end)
  6054. "Call FUNCTION at the head of all source blocks in the current buffer.
  6055. Optional arguments START and END can be used to limit the range."
  6056. (let ((start (or start (point-min)))
  6057. (end (or end (point-max))))
  6058. (save-excursion
  6059. (goto-char start)
  6060. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  6061. (save-excursion
  6062. (save-match-data
  6063. (goto-char (match-beginning 0))
  6064. (funcall function)))))))
  6065. (defun org-hide-block-toggle-all ()
  6066. "Toggle the visibility of all blocks in the current buffer."
  6067. (org-block-map #'org-hide-block-toggle))
  6068. (defun org-hide-block-all ()
  6069. "Fold all blocks in the current buffer."
  6070. (interactive)
  6071. (org-show-block-all)
  6072. (org-block-map #'org-hide-block-toggle-maybe))
  6073. (defun org-show-block-all ()
  6074. "Unfold all blocks in the current buffer."
  6075. (interactive)
  6076. (mapc 'delete-overlay org-hide-block-overlays)
  6077. (setq org-hide-block-overlays nil))
  6078. (defun org-hide-block-toggle-maybe ()
  6079. "Toggle visibility of block at point."
  6080. (interactive)
  6081. (let ((case-fold-search t))
  6082. (if (save-excursion
  6083. (beginning-of-line 1)
  6084. (looking-at org-block-regexp))
  6085. (progn (org-hide-block-toggle)
  6086. t) ;; to signal that we took action
  6087. nil))) ;; to signal that we did not
  6088. (defun org-hide-block-toggle (&optional force)
  6089. "Toggle the visibility of the current block."
  6090. (interactive)
  6091. (save-excursion
  6092. (beginning-of-line)
  6093. (if (re-search-forward org-block-regexp nil t)
  6094. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  6095. (end (match-end 0)) ;; end of entire body
  6096. ov)
  6097. (if (memq t (mapcar (lambda (overlay)
  6098. (eq (overlay-get overlay 'invisible)
  6099. 'org-hide-block))
  6100. (overlays-at start)))
  6101. (if (or (not force) (eq force 'off))
  6102. (mapc (lambda (ov)
  6103. (when (member ov org-hide-block-overlays)
  6104. (setq org-hide-block-overlays
  6105. (delq ov org-hide-block-overlays)))
  6106. (when (eq (overlay-get ov 'invisible)
  6107. 'org-hide-block)
  6108. (delete-overlay ov)))
  6109. (overlays-at start)))
  6110. (setq ov (make-overlay start end))
  6111. (overlay-put ov 'invisible 'org-hide-block)
  6112. ;; make the block accessible to isearch
  6113. (overlay-put
  6114. ov 'isearch-open-invisible
  6115. (lambda (ov)
  6116. (when (member ov org-hide-block-overlays)
  6117. (setq org-hide-block-overlays
  6118. (delq ov org-hide-block-overlays)))
  6119. (when (eq (overlay-get ov 'invisible)
  6120. 'org-hide-block)
  6121. (delete-overlay ov))))
  6122. (push ov org-hide-block-overlays)))
  6123. (error "Not looking at a source block"))))
  6124. ;; org-tab-after-check-for-cycling-hook
  6125. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  6126. ;; Remove overlays when changing major mode
  6127. (add-hook 'org-mode-hook
  6128. (lambda () (org-add-hook 'change-major-mode-hook
  6129. 'org-show-block-all 'append 'local)))
  6130. ;;; Org-goto
  6131. (defvar org-goto-window-configuration nil)
  6132. (defvar org-goto-marker nil)
  6133. (defvar org-goto-map
  6134. (let ((map (make-sparse-keymap)))
  6135. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  6136. (while (setq cmd (pop cmds))
  6137. (substitute-key-definition cmd cmd map global-map)))
  6138. (suppress-keymap map)
  6139. (org-defkey map "\C-m" 'org-goto-ret)
  6140. (org-defkey map [(return)] 'org-goto-ret)
  6141. (org-defkey map [(left)] 'org-goto-left)
  6142. (org-defkey map [(right)] 'org-goto-right)
  6143. (org-defkey map [(control ?g)] 'org-goto-quit)
  6144. (org-defkey map "\C-i" 'org-cycle)
  6145. (org-defkey map [(tab)] 'org-cycle)
  6146. (org-defkey map [(down)] 'outline-next-visible-heading)
  6147. (org-defkey map [(up)] 'outline-previous-visible-heading)
  6148. (if org-goto-auto-isearch
  6149. (if (fboundp 'define-key-after)
  6150. (define-key-after map [t] 'org-goto-local-auto-isearch)
  6151. nil)
  6152. (org-defkey map "q" 'org-goto-quit)
  6153. (org-defkey map "n" 'outline-next-visible-heading)
  6154. (org-defkey map "p" 'outline-previous-visible-heading)
  6155. (org-defkey map "f" 'outline-forward-same-level)
  6156. (org-defkey map "b" 'outline-backward-same-level)
  6157. (org-defkey map "u" 'outline-up-heading))
  6158. (org-defkey map "/" 'org-occur)
  6159. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  6160. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  6161. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  6162. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  6163. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  6164. map))
  6165. (defconst org-goto-help
  6166. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  6167. RET=jump to location [Q]uit and return to previous location
  6168. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  6169. (defvar org-goto-start-pos) ; dynamically scoped parameter
  6170. ;; FIXME: Docstring does not mention both interfaces
  6171. (defun org-goto (&optional alternative-interface)
  6172. "Look up a different location in the current file, keeping current visibility.
  6173. When you want look-up or go to a different location in a
  6174. document, the fastest way is often to fold the entire buffer and
  6175. then dive into the tree. This method has the disadvantage, that
  6176. the previous location will be folded, which may not be what you
  6177. want.
  6178. This command works around this by showing a copy of the current
  6179. buffer in an indirect buffer, in overview mode. You can dive
  6180. into the tree in that copy, use org-occur and incremental search
  6181. to find a location. When pressing RET or `Q', the command
  6182. returns to the original buffer in which the visibility is still
  6183. unchanged. After RET it will also jump to the location selected
  6184. in the indirect buffer and expose the headline hierarchy above.
  6185. With a prefix argument, use the alternative interface: e.g. if
  6186. `org-goto-interface' is 'outline use 'outline-path-completion."
  6187. (interactive "P")
  6188. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  6189. (org-refile-use-outline-path t)
  6190. (org-refile-target-verify-function nil)
  6191. (interface
  6192. (if (not alternative-interface)
  6193. org-goto-interface
  6194. (if (eq org-goto-interface 'outline)
  6195. 'outline-path-completion
  6196. 'outline)))
  6197. (org-goto-start-pos (point))
  6198. (selected-point
  6199. (if (eq interface 'outline)
  6200. (car (org-get-location (current-buffer) org-goto-help))
  6201. (let ((pa (org-refile-get-location "Goto" nil nil t)))
  6202. (org-refile-check-position pa)
  6203. (nth 3 pa)))))
  6204. (if selected-point
  6205. (progn
  6206. (org-mark-ring-push org-goto-start-pos)
  6207. (goto-char selected-point)
  6208. (if (or (outline-invisible-p) (org-invisible-p2))
  6209. (org-show-context 'org-goto)))
  6210. (message "Quit"))))
  6211. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  6212. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  6213. (defvar org-goto-local-auto-isearch-map) ; defined below
  6214. (defun org-get-location (buf help)
  6215. "Let the user select a location in the Org-mode buffer BUF.
  6216. This function uses a recursive edit. It returns the selected position
  6217. or nil."
  6218. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  6219. (isearch-hide-immediately nil)
  6220. (isearch-search-fun-function
  6221. (lambda () 'org-goto-local-search-headings))
  6222. (org-goto-selected-point org-goto-exit-command)
  6223. (pop-up-frames nil)
  6224. (special-display-buffer-names nil)
  6225. (special-display-regexps nil)
  6226. (special-display-function nil))
  6227. (save-excursion
  6228. (save-window-excursion
  6229. (delete-other-windows)
  6230. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  6231. (org-pop-to-buffer-same-window
  6232. (condition-case nil
  6233. (make-indirect-buffer (current-buffer) "*org-goto*")
  6234. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  6235. (with-output-to-temp-buffer "*Help*"
  6236. (princ help))
  6237. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  6238. (setq buffer-read-only nil)
  6239. (let ((org-startup-truncated t)
  6240. (org-startup-folded nil)
  6241. (org-startup-align-all-tables nil))
  6242. (org-mode)
  6243. (org-overview))
  6244. (setq buffer-read-only t)
  6245. (if (and (boundp 'org-goto-start-pos)
  6246. (integer-or-marker-p org-goto-start-pos))
  6247. (let ((org-show-hierarchy-above t)
  6248. (org-show-siblings t)
  6249. (org-show-following-heading t))
  6250. (goto-char org-goto-start-pos)
  6251. (and (outline-invisible-p) (org-show-context)))
  6252. (goto-char (point-min)))
  6253. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  6254. (message "Select location and press RET")
  6255. (use-local-map org-goto-map)
  6256. (recursive-edit)
  6257. ))
  6258. (kill-buffer "*org-goto*")
  6259. (cons org-goto-selected-point org-goto-exit-command)))
  6260. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  6261. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  6262. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  6263. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  6264. (defun org-goto-local-search-headings (string bound noerror)
  6265. "Search and make sure that any matches are in headlines."
  6266. (catch 'return
  6267. (while (if isearch-forward
  6268. (search-forward string bound noerror)
  6269. (search-backward string bound noerror))
  6270. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  6271. (and (member :headline context)
  6272. (not (member :tags context))))
  6273. (throw 'return (point))))))
  6274. (defun org-goto-local-auto-isearch ()
  6275. "Start isearch."
  6276. (interactive)
  6277. (goto-char (point-min))
  6278. (let ((keys (this-command-keys)))
  6279. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  6280. (isearch-mode t)
  6281. (isearch-process-search-char (string-to-char keys)))))
  6282. (defun org-goto-ret (&optional arg)
  6283. "Finish `org-goto' by going to the new location."
  6284. (interactive "P")
  6285. (setq org-goto-selected-point (point)
  6286. org-goto-exit-command 'return)
  6287. (throw 'exit nil))
  6288. (defun org-goto-left ()
  6289. "Finish `org-goto' by going to the new location."
  6290. (interactive)
  6291. (if (org-at-heading-p)
  6292. (progn
  6293. (beginning-of-line 1)
  6294. (setq org-goto-selected-point (point)
  6295. org-goto-exit-command 'left)
  6296. (throw 'exit nil))
  6297. (error "Not on a heading")))
  6298. (defun org-goto-right ()
  6299. "Finish `org-goto' by going to the new location."
  6300. (interactive)
  6301. (if (org-at-heading-p)
  6302. (progn
  6303. (setq org-goto-selected-point (point)
  6304. org-goto-exit-command 'right)
  6305. (throw 'exit nil))
  6306. (error "Not on a heading")))
  6307. (defun org-goto-quit ()
  6308. "Finish `org-goto' without cursor motion."
  6309. (interactive)
  6310. (setq org-goto-selected-point nil)
  6311. (setq org-goto-exit-command 'quit)
  6312. (throw 'exit nil))
  6313. ;;; Indirect buffer display of subtrees
  6314. (defvar org-indirect-dedicated-frame nil
  6315. "This is the frame being used for indirect tree display.")
  6316. (defvar org-last-indirect-buffer nil)
  6317. (defun org-tree-to-indirect-buffer (&optional arg)
  6318. "Create indirect buffer and narrow it to current subtree.
  6319. With numerical prefix ARG, go up to this level and then take that tree.
  6320. If ARG is negative, go up that many levels.
  6321. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  6322. indirect buffer previously made with this command, to avoid proliferation of
  6323. indirect buffers. However, when you call the command with a \
  6324. \\[universal-argument] prefix, or
  6325. when `org-indirect-buffer-display' is `new-frame', the last buffer
  6326. is kept so that you can work with several indirect buffers at the same time.
  6327. If `org-indirect-buffer-display' is `dedicated-frame', the \
  6328. \\[universal-argument] prefix also
  6329. requests that a new frame be made for the new buffer, so that the dedicated
  6330. frame is not changed."
  6331. (interactive "P")
  6332. (let ((cbuf (current-buffer))
  6333. (cwin (selected-window))
  6334. (pos (point))
  6335. beg end level heading ibuf)
  6336. (save-excursion
  6337. (org-back-to-heading t)
  6338. (when (numberp arg)
  6339. (setq level (org-outline-level))
  6340. (if (< arg 0) (setq arg (+ level arg)))
  6341. (while (> (setq level (org-outline-level)) arg)
  6342. (outline-up-heading 1 t)))
  6343. (setq beg (point)
  6344. heading (org-get-heading))
  6345. (org-end-of-subtree t t)
  6346. (if (org-at-heading-p) (backward-char 1))
  6347. (setq end (point)))
  6348. (if (and (buffer-live-p org-last-indirect-buffer)
  6349. (not (eq org-indirect-buffer-display 'new-frame))
  6350. (not arg))
  6351. (kill-buffer org-last-indirect-buffer))
  6352. (setq ibuf (org-get-indirect-buffer cbuf)
  6353. org-last-indirect-buffer ibuf)
  6354. (cond
  6355. ((or (eq org-indirect-buffer-display 'new-frame)
  6356. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  6357. (select-frame (make-frame))
  6358. (delete-other-windows)
  6359. (org-pop-to-buffer-same-window ibuf)
  6360. (org-set-frame-title heading))
  6361. ((eq org-indirect-buffer-display 'dedicated-frame)
  6362. (raise-frame
  6363. (select-frame (or (and org-indirect-dedicated-frame
  6364. (frame-live-p org-indirect-dedicated-frame)
  6365. org-indirect-dedicated-frame)
  6366. (setq org-indirect-dedicated-frame (make-frame)))))
  6367. (delete-other-windows)
  6368. (org-pop-to-buffer-same-window ibuf)
  6369. (org-set-frame-title (concat "Indirect: " heading)))
  6370. ((eq org-indirect-buffer-display 'current-window)
  6371. (org-pop-to-buffer-same-window ibuf))
  6372. ((eq org-indirect-buffer-display 'other-window)
  6373. (pop-to-buffer ibuf))
  6374. (t (error "Invalid value")))
  6375. (if (featurep 'xemacs)
  6376. (save-excursion (org-mode) (turn-on-font-lock)))
  6377. (narrow-to-region beg end)
  6378. (show-all)
  6379. (goto-char pos)
  6380. (run-hook-with-args 'org-cycle-hook 'all)
  6381. (and (window-live-p cwin) (select-window cwin))))
  6382. (defun org-get-indirect-buffer (&optional buffer)
  6383. (setq buffer (or buffer (current-buffer)))
  6384. (let ((n 1) (base (buffer-name buffer)) bname)
  6385. (while (buffer-live-p
  6386. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  6387. (setq n (1+ n)))
  6388. (condition-case nil
  6389. (make-indirect-buffer buffer bname 'clone)
  6390. (error (make-indirect-buffer buffer bname)))))
  6391. (defun org-set-frame-title (title)
  6392. "Set the title of the current frame to the string TITLE."
  6393. ;; FIXME: how to name a single frame in XEmacs???
  6394. (unless (featurep 'xemacs)
  6395. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  6396. ;;;; Structure editing
  6397. ;;; Inserting headlines
  6398. (defun org-previous-line-empty-p ()
  6399. (save-excursion
  6400. (and (not (bobp))
  6401. (or (beginning-of-line 0) t)
  6402. (save-match-data
  6403. (looking-at "[ \t]*$")))))
  6404. (defun org-insert-heading (&optional force-heading invisible-ok)
  6405. "Insert a new heading or item with same depth at point.
  6406. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  6407. If point is at the beginning of a headline, insert a sibling before the
  6408. current headline. If point is not at the beginning, split the line,
  6409. create the new headline with the text in the current line after point
  6410. \(but see also the variable `org-M-RET-may-split-line').
  6411. When INVISIBLE-OK is set, stop at invisible headlines when going back.
  6412. This is important for non-interactive uses of the command."
  6413. (interactive "P")
  6414. (if (or (= (buffer-size) 0)
  6415. (and (not (save-excursion
  6416. (and (ignore-errors (org-back-to-heading invisible-ok))
  6417. (org-at-heading-p))))
  6418. (or force-heading (not (org-in-item-p)))))
  6419. (progn
  6420. (insert "\n* ")
  6421. (run-hooks 'org-insert-heading-hook))
  6422. (when (or force-heading (not (org-insert-item)))
  6423. (let* ((empty-line-p nil)
  6424. (level nil)
  6425. (on-heading (org-at-heading-p))
  6426. (head (save-excursion
  6427. (condition-case nil
  6428. (progn
  6429. (org-back-to-heading invisible-ok)
  6430. (when (and (not on-heading)
  6431. (featurep 'org-inlinetask)
  6432. (integerp org-inlinetask-min-level)
  6433. (>= (length (match-string 0))
  6434. org-inlinetask-min-level))
  6435. ;; Find a heading level before the inline task
  6436. (while (and (setq level (org-up-heading-safe))
  6437. (>= level org-inlinetask-min-level)))
  6438. (if (org-at-heading-p)
  6439. (org-back-to-heading invisible-ok)
  6440. (error "This should not happen")))
  6441. (setq empty-line-p (org-previous-line-empty-p))
  6442. (match-string 0))
  6443. (error "*"))))
  6444. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6445. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  6446. pos hide-previous previous-pos)
  6447. (cond
  6448. ((and (org-at-heading-p) (bolp)
  6449. (or (bobp)
  6450. (save-excursion (backward-char 1) (not (outline-invisible-p)))))
  6451. ;; insert before the current line
  6452. (open-line (if blank 2 1)))
  6453. ((and (bolp)
  6454. (not org-insert-heading-respect-content)
  6455. (or (bobp)
  6456. (save-excursion
  6457. (backward-char 1) (not (outline-invisible-p)))))
  6458. ;; insert right here
  6459. nil)
  6460. (t
  6461. ;; somewhere in the line
  6462. (save-excursion
  6463. (setq previous-pos (point-at-bol))
  6464. (end-of-line)
  6465. (setq hide-previous (outline-invisible-p)))
  6466. (and org-insert-heading-respect-content (org-show-subtree))
  6467. (let ((split
  6468. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  6469. (save-excursion
  6470. (let ((p (point)))
  6471. (goto-char (point-at-bol))
  6472. (and (looking-at org-complex-heading-regexp)
  6473. (match-beginning 4)
  6474. (> p (match-beginning 4)))))))
  6475. tags pos)
  6476. (cond
  6477. (org-insert-heading-respect-content
  6478. (org-end-of-subtree nil t)
  6479. (when (featurep 'org-inlinetask)
  6480. (while (and (not (eobp))
  6481. (looking-at "\\(\\*+\\)[ \t]+")
  6482. (>= (length (match-string 1))
  6483. org-inlinetask-min-level))
  6484. (org-end-of-subtree nil t)))
  6485. (or (bolp) (newline))
  6486. (or (org-previous-line-empty-p)
  6487. (and blank (newline)))
  6488. (open-line 1))
  6489. ((org-at-heading-p)
  6490. (when hide-previous
  6491. (show-children)
  6492. (org-show-entry))
  6493. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?[ \t]*$")
  6494. (setq tags (and (match-end 2) (match-string 2)))
  6495. (and (match-end 1)
  6496. (delete-region (match-beginning 1) (match-end 1)))
  6497. (setq pos (point-at-bol))
  6498. (or split (end-of-line 1))
  6499. (delete-horizontal-space)
  6500. (if (string-match "\\`\\*+\\'"
  6501. (buffer-substring (point-at-bol) (point)))
  6502. (insert " "))
  6503. (newline (if blank 2 1))
  6504. (when tags
  6505. (save-excursion
  6506. (goto-char pos)
  6507. (end-of-line 1)
  6508. (insert " " tags)
  6509. (org-set-tags nil 'align))))
  6510. (t
  6511. (or split (end-of-line 1))
  6512. (newline (if blank 2 1)))))))
  6513. (insert head) (just-one-space)
  6514. (setq pos (point))
  6515. (end-of-line 1)
  6516. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  6517. (when (and org-insert-heading-respect-content hide-previous)
  6518. (save-excursion
  6519. (goto-char previous-pos)
  6520. (hide-subtree)))
  6521. (run-hooks 'org-insert-heading-hook)))))
  6522. (defun org-get-heading (&optional no-tags no-todo)
  6523. "Return the heading of the current entry, without the stars.
  6524. When NO-TAGS is non-nil, don't include tags.
  6525. When NO-TODO is non-nil, don't include TODO keywords."
  6526. (save-excursion
  6527. (org-back-to-heading t)
  6528. (cond
  6529. ((and no-tags no-todo)
  6530. (looking-at org-complex-heading-regexp)
  6531. (match-string 4))
  6532. (no-tags
  6533. (looking-at (concat org-outline-regexp
  6534. "\\(.*?\\)"
  6535. "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
  6536. (match-string 1))
  6537. (no-todo
  6538. (looking-at org-todo-line-regexp)
  6539. (match-string 3))
  6540. (t (looking-at org-heading-regexp)
  6541. (match-string 2)))))
  6542. (defun org-heading-components ()
  6543. "Return the components of the current heading.
  6544. This is a list with the following elements:
  6545. - the level as an integer
  6546. - the reduced level, different if `org-odd-levels-only' is set.
  6547. - the TODO keyword, or nil
  6548. - the priority character, like ?A, or nil if no priority is given
  6549. - the headline text itself, or the tags string if no headline text
  6550. - the tags string, or nil."
  6551. (save-excursion
  6552. (org-back-to-heading t)
  6553. (if (let (case-fold-search) (looking-at org-complex-heading-regexp))
  6554. (list (length (match-string 1))
  6555. (org-reduced-level (length (match-string 1)))
  6556. (org-match-string-no-properties 2)
  6557. (and (match-end 3) (aref (match-string 3) 2))
  6558. (org-match-string-no-properties 4)
  6559. (org-match-string-no-properties 5)))))
  6560. (defun org-get-entry ()
  6561. "Get the entry text, after heading, entire subtree."
  6562. (save-excursion
  6563. (org-back-to-heading t)
  6564. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  6565. (defun org-insert-heading-after-current ()
  6566. "Insert a new heading with same level as current, after current subtree."
  6567. (interactive)
  6568. (org-back-to-heading)
  6569. (org-insert-heading)
  6570. (org-move-subtree-down)
  6571. (end-of-line 1))
  6572. (defun org-insert-heading-respect-content ()
  6573. (interactive)
  6574. (let ((org-insert-heading-respect-content t))
  6575. (org-insert-heading t)))
  6576. (defun org-insert-todo-heading-respect-content (&optional force-state)
  6577. (interactive "P")
  6578. (let ((org-insert-heading-respect-content t))
  6579. (org-insert-todo-heading force-state t)))
  6580. (defun org-insert-todo-heading (arg &optional force-heading)
  6581. "Insert a new heading with the same level and TODO state as current heading.
  6582. If the heading has no TODO state, or if the state is DONE, use the first
  6583. state (TODO by default). Also with prefix arg, force first state."
  6584. (interactive "P")
  6585. (when (or force-heading (not (org-insert-item 'checkbox)))
  6586. (org-insert-heading force-heading)
  6587. (save-excursion
  6588. (org-back-to-heading)
  6589. (outline-previous-heading)
  6590. (looking-at org-todo-line-regexp))
  6591. (let*
  6592. ((new-mark-x
  6593. (if (or arg
  6594. (not (match-beginning 2))
  6595. (member (match-string 2) org-done-keywords))
  6596. (car org-todo-keywords-1)
  6597. (match-string 2)))
  6598. (new-mark
  6599. (or
  6600. (run-hook-with-args-until-success
  6601. 'org-todo-get-default-hook new-mark-x nil)
  6602. new-mark-x)))
  6603. (beginning-of-line 1)
  6604. (and (looking-at org-outline-regexp) (goto-char (match-end 0))
  6605. (if org-treat-insert-todo-heading-as-state-change
  6606. (org-todo new-mark)
  6607. (insert new-mark " "))))
  6608. (when org-provide-todo-statistics
  6609. (org-update-parent-todo-statistics))))
  6610. (defun org-insert-subheading (arg)
  6611. "Insert a new subheading and demote it.
  6612. Works for outline headings and for plain lists alike."
  6613. (interactive "P")
  6614. (org-insert-heading arg)
  6615. (cond
  6616. ((org-at-heading-p) (org-do-demote))
  6617. ((org-at-item-p) (org-indent-item))))
  6618. (defun org-insert-todo-subheading (arg)
  6619. "Insert a new subheading with TODO keyword or checkbox and demote it.
  6620. Works for outline headings and for plain lists alike."
  6621. (interactive "P")
  6622. (org-insert-todo-heading arg)
  6623. (cond
  6624. ((org-at-heading-p) (org-do-demote))
  6625. ((org-at-item-p) (org-indent-item))))
  6626. ;;; Promotion and Demotion
  6627. (defvar org-after-demote-entry-hook nil
  6628. "Hook run after an entry has been demoted.
  6629. The cursor will be at the beginning of the entry.
  6630. When a subtree is being demoted, the hook will be called for each node.")
  6631. (defvar org-after-promote-entry-hook nil
  6632. "Hook run after an entry has been promoted.
  6633. The cursor will be at the beginning of the entry.
  6634. When a subtree is being promoted, the hook will be called for each node.")
  6635. (defun org-promote-subtree ()
  6636. "Promote the entire subtree.
  6637. See also `org-promote'."
  6638. (interactive)
  6639. (save-excursion
  6640. (org-with-limited-levels (org-map-tree 'org-promote)))
  6641. (org-fix-position-after-promote))
  6642. (defun org-demote-subtree ()
  6643. "Demote the entire subtree. See `org-demote'.
  6644. See also `org-promote'."
  6645. (interactive)
  6646. (save-excursion
  6647. (org-with-limited-levels (org-map-tree 'org-demote)))
  6648. (org-fix-position-after-promote))
  6649. (defun org-do-promote ()
  6650. "Promote the current heading higher up the tree.
  6651. If the region is active in `transient-mark-mode', promote all headings
  6652. in the region."
  6653. (interactive)
  6654. (save-excursion
  6655. (if (org-region-active-p)
  6656. (org-map-region 'org-promote (region-beginning) (region-end))
  6657. (org-promote)))
  6658. (org-fix-position-after-promote))
  6659. (defun org-do-demote ()
  6660. "Demote the current heading lower down the tree.
  6661. If the region is active in `transient-mark-mode', demote all headings
  6662. in the region."
  6663. (interactive)
  6664. (save-excursion
  6665. (if (org-region-active-p)
  6666. (org-map-region 'org-demote (region-beginning) (region-end))
  6667. (org-demote)))
  6668. (org-fix-position-after-promote))
  6669. (defun org-fix-position-after-promote ()
  6670. "Make sure that after pro/demotion cursor position is right."
  6671. (let ((pos (point)))
  6672. (when (save-excursion
  6673. (beginning-of-line 1)
  6674. (looking-at org-todo-line-regexp)
  6675. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  6676. (cond ((eobp) (insert " "))
  6677. ((eolp) (insert " "))
  6678. ((equal (char-after) ?\ ) (forward-char 1))))))
  6679. (defun org-current-level ()
  6680. "Return the level of the current entry, or nil if before the first headline.
  6681. The level is the number of stars at the beginning of the headline."
  6682. (save-excursion
  6683. (org-with-limited-levels
  6684. (if (ignore-errors (org-back-to-heading t))
  6685. (funcall outline-level)))))
  6686. (defun org-get-previous-line-level ()
  6687. "Return the outline depth of the last headline before the current line.
  6688. Returns 0 for the first headline in the buffer, and nil if before the
  6689. first headline."
  6690. (let ((current-level (org-current-level))
  6691. (prev-level (when (> (line-number-at-pos) 1)
  6692. (save-excursion
  6693. (beginning-of-line 0)
  6694. (org-current-level)))))
  6695. (cond ((null current-level) nil) ; Before first headline
  6696. ((null prev-level) 0) ; At first headline
  6697. (prev-level))))
  6698. (defun org-reduced-level (l)
  6699. "Compute the effective level of a heading.
  6700. This takes into account the setting of `org-odd-levels-only'."
  6701. (cond
  6702. ((zerop l) 0)
  6703. (org-odd-levels-only (1+ (floor (/ l 2))))
  6704. (t l)))
  6705. (defun org-level-increment ()
  6706. "Return the number of stars that will be added or removed at a
  6707. time to headlines when structure editing, based on the value of
  6708. `org-odd-levels-only'."
  6709. (if org-odd-levels-only 2 1))
  6710. (defun org-get-valid-level (level &optional change)
  6711. "Rectify a level change under the influence of `org-odd-levels-only'
  6712. LEVEL is a current level, CHANGE is by how much the level should be
  6713. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  6714. even level numbers will become the next higher odd number."
  6715. (if org-odd-levels-only
  6716. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  6717. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  6718. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  6719. (max 1 (+ level (or change 0)))))
  6720. (if (boundp 'define-obsolete-function-alias)
  6721. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  6722. (define-obsolete-function-alias 'org-get-legal-level
  6723. 'org-get-valid-level)
  6724. (define-obsolete-function-alias 'org-get-legal-level
  6725. 'org-get-valid-level "23.1")))
  6726. (defvar org-called-with-limited-levels nil) ;; Dynamically bound in
  6727. ;; ̀org-with-limited-levels'
  6728. (defun org-promote ()
  6729. "Promote the current heading higher up the tree.
  6730. If the region is active in `transient-mark-mode', promote all headings
  6731. in the region."
  6732. (org-back-to-heading t)
  6733. (let* ((level (save-match-data (funcall outline-level)))
  6734. (after-change-functions (remove 'flyspell-after-change-function
  6735. after-change-functions))
  6736. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  6737. (diff (abs (- level (length up-head) -1))))
  6738. (cond ((and (= level 1) org-called-with-limited-levels
  6739. org-allow-promoting-top-level-subtree)
  6740. (replace-match "# " nil t))
  6741. ((= level 1)
  6742. (error "Cannot promote to level 0. UNDO to recover if necessary"))
  6743. (t (replace-match up-head nil t)))
  6744. ;; Fixup tag positioning
  6745. (unless (= level 1)
  6746. (and org-auto-align-tags (org-set-tags nil t))
  6747. (if org-adapt-indentation (org-fixup-indentation (- diff))))
  6748. (run-hooks 'org-after-promote-entry-hook)))
  6749. (defun org-demote ()
  6750. "Demote the current heading lower down the tree.
  6751. If the region is active in `transient-mark-mode', demote all headings
  6752. in the region."
  6753. (org-back-to-heading t)
  6754. (let* ((level (save-match-data (funcall outline-level)))
  6755. (after-change-functions (remove 'flyspell-after-change-function
  6756. after-change-functions))
  6757. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  6758. (diff (abs (- level (length down-head) -1))))
  6759. (replace-match down-head nil t)
  6760. ;; Fixup tag positioning
  6761. (and org-auto-align-tags (org-set-tags nil t))
  6762. (if org-adapt-indentation (org-fixup-indentation diff))
  6763. (run-hooks 'org-after-demote-entry-hook)))
  6764. (defun org-cycle-level ()
  6765. "Cycle the level of an empty headline through possible states.
  6766. This goes first to child, then to parent, level, then up the hierarchy.
  6767. After top level, it switches back to sibling level."
  6768. (interactive)
  6769. (let ((org-adapt-indentation nil))
  6770. (when (org-point-at-end-of-empty-headline)
  6771. (setq this-command 'org-cycle-level) ; Only needed for caching
  6772. (let ((cur-level (org-current-level))
  6773. (prev-level (org-get-previous-line-level)))
  6774. (cond
  6775. ;; If first headline in file, promote to top-level.
  6776. ((= prev-level 0)
  6777. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6778. do (org-do-promote)))
  6779. ;; If same level as prev, demote one.
  6780. ((= prev-level cur-level)
  6781. (org-do-demote))
  6782. ;; If parent is top-level, promote to top level if not already.
  6783. ((= prev-level 1)
  6784. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6785. do (org-do-promote)))
  6786. ;; If top-level, return to prev-level.
  6787. ((= cur-level 1)
  6788. (loop repeat (/ (- prev-level 1) (org-level-increment))
  6789. do (org-do-demote)))
  6790. ;; If less than prev-level, promote one.
  6791. ((< cur-level prev-level)
  6792. (org-do-promote))
  6793. ;; If deeper than prev-level, promote until higher than
  6794. ;; prev-level.
  6795. ((> cur-level prev-level)
  6796. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  6797. do (org-do-promote))))
  6798. t))))
  6799. (defun org-map-tree (fun)
  6800. "Call FUN for every heading underneath the current one."
  6801. (org-back-to-heading)
  6802. (let ((level (funcall outline-level)))
  6803. (save-excursion
  6804. (funcall fun)
  6805. (while (and (progn
  6806. (outline-next-heading)
  6807. (> (funcall outline-level) level))
  6808. (not (eobp)))
  6809. (funcall fun)))))
  6810. (defun org-map-region (fun beg end)
  6811. "Call FUN for every heading between BEG and END."
  6812. (let ((org-ignore-region t))
  6813. (save-excursion
  6814. (setq end (copy-marker end))
  6815. (goto-char beg)
  6816. (if (and (re-search-forward org-outline-regexp-bol nil t)
  6817. (< (point) end))
  6818. (funcall fun))
  6819. (while (and (progn
  6820. (outline-next-heading)
  6821. (< (point) end))
  6822. (not (eobp)))
  6823. (funcall fun)))))
  6824. (defvar org-property-end-re) ; silence byte-compiler
  6825. (defun org-fixup-indentation (diff)
  6826. "Change the indentation in the current entry by DIFF.
  6827. However, if any line in the current entry has no indentation, or if it
  6828. would end up with no indentation after the change, nothing at all is done."
  6829. (save-excursion
  6830. (let ((end (save-excursion (outline-next-heading)
  6831. (point-marker)))
  6832. (prohibit (if (> diff 0)
  6833. "^\\S-"
  6834. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  6835. col)
  6836. (unless (save-excursion (end-of-line 1)
  6837. (re-search-forward prohibit end t))
  6838. (while (and (< (point) end)
  6839. (re-search-forward "^[ \t]+" end t))
  6840. (goto-char (match-end 0))
  6841. (setq col (current-column))
  6842. (if (< diff 0) (replace-match ""))
  6843. (org-indent-to-column (+ diff col))))
  6844. (move-marker end nil))))
  6845. (defun org-convert-to-odd-levels ()
  6846. "Convert an org-mode file with all levels allowed to one with odd levels.
  6847. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  6848. level 5 etc."
  6849. (interactive)
  6850. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  6851. (let ((outline-level 'org-outline-level)
  6852. (org-odd-levels-only nil) n)
  6853. (save-excursion
  6854. (goto-char (point-min))
  6855. (while (re-search-forward "^\\*\\*+ " nil t)
  6856. (setq n (- (length (match-string 0)) 2))
  6857. (while (>= (setq n (1- n)) 0)
  6858. (org-demote))
  6859. (end-of-line 1))))))
  6860. (defun org-convert-to-oddeven-levels ()
  6861. "Convert an org-mode file with only odd levels to one with odd/even levels.
  6862. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  6863. file contains a section with an even level, conversion would
  6864. destroy the structure of the file. An error is signaled in this
  6865. case."
  6866. (interactive)
  6867. (goto-char (point-min))
  6868. ;; First check if there are no even levels
  6869. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  6870. (org-show-context t)
  6871. (error "Not all levels are odd in this file. Conversion not possible"))
  6872. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  6873. (let ((outline-regexp org-outline-regexp)
  6874. (outline-level 'org-outline-level)
  6875. (org-odd-levels-only nil) n)
  6876. (save-excursion
  6877. (goto-char (point-min))
  6878. (while (re-search-forward "^\\*\\*+ " nil t)
  6879. (setq n (/ (1- (length (match-string 0))) 2))
  6880. (while (>= (setq n (1- n)) 0)
  6881. (org-promote))
  6882. (end-of-line 1))))))
  6883. (defun org-tr-level (n)
  6884. "Make N odd if required."
  6885. (if org-odd-levels-only (1+ (/ n 2)) n))
  6886. ;;; Vertical tree motion, cutting and pasting of subtrees
  6887. (defun org-move-subtree-up (&optional arg)
  6888. "Move the current subtree up past ARG headlines of the same level."
  6889. (interactive "p")
  6890. (org-move-subtree-down (- (prefix-numeric-value arg))))
  6891. (defun org-move-subtree-down (&optional arg)
  6892. "Move the current subtree down past ARG headlines of the same level."
  6893. (interactive "p")
  6894. (setq arg (prefix-numeric-value arg))
  6895. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  6896. 'org-get-last-sibling))
  6897. (ins-point (make-marker))
  6898. (cnt (abs arg))
  6899. (col (current-column))
  6900. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  6901. ;; Select the tree
  6902. (org-back-to-heading)
  6903. (setq beg0 (point))
  6904. (save-excursion
  6905. (setq ne-beg (org-back-over-empty-lines))
  6906. (setq beg (point)))
  6907. (save-match-data
  6908. (save-excursion (outline-end-of-heading)
  6909. (setq folded (outline-invisible-p)))
  6910. (outline-end-of-subtree))
  6911. (outline-next-heading)
  6912. (setq ne-end (org-back-over-empty-lines))
  6913. (setq end (point))
  6914. (goto-char beg0)
  6915. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  6916. ;; include less whitespace
  6917. (save-excursion
  6918. (goto-char beg)
  6919. (forward-line (- ne-beg ne-end))
  6920. (setq beg (point))))
  6921. ;; Find insertion point, with error handling
  6922. (while (> cnt 0)
  6923. (or (and (funcall movfunc) (looking-at org-outline-regexp))
  6924. (progn (goto-char beg0)
  6925. (error "Cannot move past superior level or buffer limit")))
  6926. (setq cnt (1- cnt)))
  6927. (if (> arg 0)
  6928. ;; Moving forward - still need to move over subtree
  6929. (progn (org-end-of-subtree t t)
  6930. (save-excursion
  6931. (org-back-over-empty-lines)
  6932. (or (bolp) (newline)))))
  6933. (setq ne-ins (org-back-over-empty-lines))
  6934. (move-marker ins-point (point))
  6935. (setq txt (buffer-substring beg end))
  6936. (org-save-markers-in-region beg end)
  6937. (delete-region beg end)
  6938. (org-remove-empty-overlays-at beg)
  6939. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  6940. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  6941. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  6942. (let ((bbb (point)))
  6943. (insert-before-markers txt)
  6944. (org-reinstall-markers-in-region bbb)
  6945. (move-marker ins-point bbb))
  6946. (or (bolp) (insert "\n"))
  6947. (setq ins-end (point))
  6948. (goto-char ins-point)
  6949. (org-skip-whitespace)
  6950. (when (and (< arg 0)
  6951. (org-first-sibling-p)
  6952. (> ne-ins ne-beg))
  6953. ;; Move whitespace back to beginning
  6954. (save-excursion
  6955. (goto-char ins-end)
  6956. (let ((kill-whole-line t))
  6957. (kill-line (- ne-ins ne-beg)) (point)))
  6958. (insert (make-string (- ne-ins ne-beg) ?\n)))
  6959. (move-marker ins-point nil)
  6960. (if folded
  6961. (hide-subtree)
  6962. (org-show-entry)
  6963. (show-children)
  6964. (org-cycle-hide-drawers 'children))
  6965. (org-clean-visibility-after-subtree-move)
  6966. ;; move back to the initial column we were at
  6967. (move-to-column col)))
  6968. (defvar org-subtree-clip ""
  6969. "Clipboard for cut and paste of subtrees.
  6970. This is actually only a copy of the kill, because we use the normal kill
  6971. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  6972. (defvar org-subtree-clip-folded nil
  6973. "Was the last copied subtree folded?
  6974. This is used to fold the tree back after pasting.")
  6975. (defun org-cut-subtree (&optional n)
  6976. "Cut the current subtree into the clipboard.
  6977. With prefix arg N, cut this many sequential subtrees.
  6978. This is a short-hand for marking the subtree and then cutting it."
  6979. (interactive "p")
  6980. (org-copy-subtree n 'cut))
  6981. (defun org-copy-subtree (&optional n cut force-store-markers)
  6982. "Cut the current subtree into the clipboard.
  6983. With prefix arg N, cut this many sequential subtrees.
  6984. This is a short-hand for marking the subtree and then copying it.
  6985. If CUT is non-nil, actually cut the subtree.
  6986. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  6987. of some markers in the region, even if CUT is non-nil. This is
  6988. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  6989. (interactive "p")
  6990. (let (beg end folded (beg0 (point)))
  6991. (if (org-called-interactively-p 'any)
  6992. (org-back-to-heading nil) ; take what looks like a subtree
  6993. (org-back-to-heading t)) ; take what is really there
  6994. (org-back-over-empty-lines)
  6995. (setq beg (point))
  6996. (skip-chars-forward " \t\r\n")
  6997. (save-match-data
  6998. (save-excursion (outline-end-of-heading)
  6999. (setq folded (outline-invisible-p)))
  7000. (condition-case nil
  7001. (org-forward-same-level (1- n) t)
  7002. (error nil))
  7003. (org-end-of-subtree t t))
  7004. (org-back-over-empty-lines)
  7005. (setq end (point))
  7006. (goto-char beg0)
  7007. (when (> end beg)
  7008. (setq org-subtree-clip-folded folded)
  7009. (when (or cut force-store-markers)
  7010. (org-save-markers-in-region beg end))
  7011. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  7012. (setq org-subtree-clip (current-kill 0))
  7013. (message "%s: Subtree(s) with %d characters"
  7014. (if cut "Cut" "Copied")
  7015. (length org-subtree-clip)))))
  7016. (defun org-paste-subtree (&optional level tree for-yank)
  7017. "Paste the clipboard as a subtree, with modification of headline level.
  7018. The entire subtree is promoted or demoted in order to match a new headline
  7019. level.
  7020. If the cursor is at the beginning of a headline, the same level as
  7021. that headline is used to paste the tree
  7022. If not, the new level is derived from the *visible* headings
  7023. before and after the insertion point, and taken to be the inferior headline
  7024. level of the two. So if the previous visible heading is level 3 and the
  7025. next is level 4 (or vice versa), level 4 will be used for insertion.
  7026. This makes sure that the subtree remains an independent subtree and does
  7027. not swallow low level entries.
  7028. You can also force a different level, either by using a numeric prefix
  7029. argument, or by inserting the heading marker by hand. For example, if the
  7030. cursor is after \"*****\", then the tree will be shifted to level 5.
  7031. If optional TREE is given, use this text instead of the kill ring.
  7032. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  7033. move back over whitespace before inserting, and move point to the end of
  7034. the inserted text when done."
  7035. (interactive "P")
  7036. (setq tree (or tree (and kill-ring (current-kill 0))))
  7037. (unless (org-kill-is-subtree-p tree)
  7038. (error "%s"
  7039. (substitute-command-keys
  7040. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  7041. (org-with-limited-levels
  7042. (let* ((visp (not (outline-invisible-p)))
  7043. (txt tree)
  7044. (^re_ "\\(\\*+\\)[ \t]*")
  7045. (old-level (if (string-match org-outline-regexp-bol txt)
  7046. (- (match-end 0) (match-beginning 0) 1)
  7047. -1))
  7048. (force-level (cond (level (prefix-numeric-value level))
  7049. ((and (looking-at "[ \t]*$")
  7050. (string-match
  7051. "^\\*+$" (buffer-substring
  7052. (point-at-bol) (point))))
  7053. (- (match-end 1) (match-beginning 1)))
  7054. ((and (bolp)
  7055. (looking-at org-outline-regexp))
  7056. (- (match-end 0) (point) 1))
  7057. (t nil)))
  7058. (previous-level (save-excursion
  7059. (condition-case nil
  7060. (progn
  7061. (outline-previous-visible-heading 1)
  7062. (if (looking-at ^re_)
  7063. (- (match-end 0) (match-beginning 0) 1)
  7064. 1))
  7065. (error 1))))
  7066. (next-level (save-excursion
  7067. (condition-case nil
  7068. (progn
  7069. (or (looking-at org-outline-regexp)
  7070. (outline-next-visible-heading 1))
  7071. (if (looking-at ^re_)
  7072. (- (match-end 0) (match-beginning 0) 1)
  7073. 1))
  7074. (error 1))))
  7075. (new-level (or force-level (max previous-level next-level)))
  7076. (shift (if (or (= old-level -1)
  7077. (= new-level -1)
  7078. (= old-level new-level))
  7079. 0
  7080. (- new-level old-level)))
  7081. (delta (if (> shift 0) -1 1))
  7082. (func (if (> shift 0) 'org-demote 'org-promote))
  7083. (org-odd-levels-only nil)
  7084. beg end newend)
  7085. ;; Remove the forced level indicator
  7086. (if force-level
  7087. (delete-region (point-at-bol) (point)))
  7088. ;; Paste
  7089. (beginning-of-line (if (bolp) 1 2))
  7090. (unless for-yank (org-back-over-empty-lines))
  7091. (setq beg (point))
  7092. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  7093. (insert-before-markers txt)
  7094. (unless (string-match "\n\\'" txt) (insert "\n"))
  7095. (setq newend (point))
  7096. (org-reinstall-markers-in-region beg)
  7097. (setq end (point))
  7098. (goto-char beg)
  7099. (skip-chars-forward " \t\n\r")
  7100. (setq beg (point))
  7101. (if (and (outline-invisible-p) visp)
  7102. (save-excursion (outline-show-heading)))
  7103. ;; Shift if necessary
  7104. (unless (= shift 0)
  7105. (save-restriction
  7106. (narrow-to-region beg end)
  7107. (while (not (= shift 0))
  7108. (org-map-region func (point-min) (point-max))
  7109. (setq shift (+ delta shift)))
  7110. (goto-char (point-min))
  7111. (setq newend (point-max))))
  7112. (when (or (org-called-interactively-p 'interactive) for-yank)
  7113. (message "Clipboard pasted as level %d subtree" new-level))
  7114. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  7115. kill-ring
  7116. (eq org-subtree-clip (current-kill 0))
  7117. org-subtree-clip-folded)
  7118. ;; The tree was folded before it was killed/copied
  7119. (hide-subtree))
  7120. (and for-yank (goto-char newend)))))
  7121. (defun org-kill-is-subtree-p (&optional txt)
  7122. "Check if the current kill is an outline subtree, or a set of trees.
  7123. Returns nil if kill does not start with a headline, or if the first
  7124. headline level is not the largest headline level in the tree.
  7125. So this will actually accept several entries of equal levels as well,
  7126. which is OK for `org-paste-subtree'.
  7127. If optional TXT is given, check this string instead of the current kill."
  7128. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  7129. (re (org-get-limited-outline-regexp))
  7130. (^re (concat "^" re))
  7131. (start-level (and kill
  7132. (string-match
  7133. (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
  7134. kill)
  7135. (- (match-end 2) (match-beginning 2) 1)))
  7136. (start (1+ (or (match-beginning 2) -1))))
  7137. (if (not start-level)
  7138. (progn
  7139. nil) ;; does not even start with a heading
  7140. (catch 'exit
  7141. (while (setq start (string-match ^re kill (1+ start)))
  7142. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  7143. (throw 'exit nil)))
  7144. t))))
  7145. (defvar org-markers-to-move nil
  7146. "Markers that should be moved with a cut-and-paste operation.
  7147. Those markers are stored together with their positions relative to
  7148. the start of the region.")
  7149. (defun org-save-markers-in-region (beg end)
  7150. "Check markers in region.
  7151. If these markers are between BEG and END, record their position relative
  7152. to BEG, so that after moving the block of text, we can put the markers back
  7153. into place.
  7154. This function gets called just before an entry or tree gets cut from the
  7155. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  7156. called immediately, to move the markers with the entries."
  7157. (setq org-markers-to-move nil)
  7158. (when (featurep 'org-clock)
  7159. (org-clock-save-markers-for-cut-and-paste beg end))
  7160. (when (featurep 'org-agenda)
  7161. (org-agenda-save-markers-for-cut-and-paste beg end)))
  7162. (defun org-check-and-save-marker (marker beg end)
  7163. "Check if MARKER is between BEG and END.
  7164. If yes, remember the marker and the distance to BEG."
  7165. (when (and (marker-buffer marker)
  7166. (equal (marker-buffer marker) (current-buffer)))
  7167. (if (and (>= marker beg) (< marker end))
  7168. (push (cons marker (- marker beg)) org-markers-to-move))))
  7169. (defun org-reinstall-markers-in-region (beg)
  7170. "Move all remembered markers to their position relative to BEG."
  7171. (mapc (lambda (x)
  7172. (move-marker (car x) (+ beg (cdr x))))
  7173. org-markers-to-move)
  7174. (setq org-markers-to-move nil))
  7175. (defun org-narrow-to-subtree ()
  7176. "Narrow buffer to the current subtree."
  7177. (interactive)
  7178. (save-excursion
  7179. (save-match-data
  7180. (org-with-limited-levels
  7181. (narrow-to-region
  7182. (progn (org-back-to-heading t) (point))
  7183. (progn (org-end-of-subtree t t)
  7184. (if (and (org-at-heading-p) (not (eobp))) (backward-char 1))
  7185. (point)))))))
  7186. (defun org-narrow-to-block ()
  7187. "Narrow buffer to the current block."
  7188. (interactive)
  7189. (let* ((case-fold-search t)
  7190. (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
  7191. "^[ \t]*#\\+end_.*")))
  7192. (if blockp
  7193. (narrow-to-region (car blockp) (cdr blockp))
  7194. (error "Not in a block"))))
  7195. (eval-when-compile
  7196. (defvar org-property-drawer-re))
  7197. (defvar org-property-start-re) ;; defined below
  7198. (defun org-clone-subtree-with-time-shift (n &optional shift)
  7199. "Clone the task (subtree) at point N times.
  7200. The clones will be inserted as siblings.
  7201. In interactive use, the user will be prompted for the number of
  7202. clones to be produced, and for a time SHIFT, which may be a
  7203. repeater as used in time stamps, for example `+3d'.
  7204. When a valid repeater is given and the entry contains any time
  7205. stamps, the clones will become a sequence in time, with time
  7206. stamps in the subtree shifted for each clone produced. If SHIFT
  7207. is nil or the empty string, time stamps will be left alone. The
  7208. ID property of the original subtree is removed.
  7209. If the original subtree did contain time stamps with a repeater,
  7210. the following will happen:
  7211. - the repeater will be removed in each clone
  7212. - an additional clone will be produced, with the current, unshifted
  7213. date(s) in the entry.
  7214. - the original entry will be placed *after* all the clones, with
  7215. repeater intact.
  7216. - the start days in the repeater in the original entry will be shifted
  7217. to past the last clone.
  7218. In this way you can spell out a number of instances of a repeating task,
  7219. and still retain the repeater to cover future instances of the task."
  7220. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  7221. (let (beg end template task idprop
  7222. shift-n shift-what doshift nmin nmax (n-no-remove -1)
  7223. (drawer-re org-drawer-regexp))
  7224. (if (not (and (integerp n) (> n 0)))
  7225. (error "Invalid number of replications %s" n))
  7226. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  7227. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([hdwmy]\\)[ \t]*\\'"
  7228. shift)))
  7229. (error "Invalid shift specification %s" shift))
  7230. (when doshift
  7231. (setq shift-n (string-to-number (match-string 1 shift))
  7232. shift-what (cdr (assoc (match-string 2 shift)
  7233. '(("d" . day) ("w" . week)
  7234. ("m" . month) ("y" . year))))))
  7235. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  7236. (setq nmin 1 nmax n)
  7237. (org-back-to-heading t)
  7238. (setq beg (point))
  7239. (setq idprop (org-entry-get nil "ID"))
  7240. (org-end-of-subtree t t)
  7241. (or (bolp) (insert "\n"))
  7242. (setq end (point))
  7243. (setq template (buffer-substring beg end))
  7244. (when (and doshift
  7245. (string-match "<[^<>\n]+ [.+]?\\+[0-9]+[hdwmy][^<>\n]*>" template))
  7246. (delete-region beg end)
  7247. (setq end beg)
  7248. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  7249. (goto-char end)
  7250. (loop for n from nmin to nmax do
  7251. ;; prepare clone
  7252. (with-temp-buffer
  7253. (insert template)
  7254. (org-mode)
  7255. (goto-char (point-min))
  7256. (org-show-subtree)
  7257. (and idprop (if org-clone-delete-id
  7258. (org-entry-delete nil "ID")
  7259. (org-id-get-create t)))
  7260. (unless (= n 0)
  7261. (while (re-search-forward "^[ \t]*CLOCK:.*$" nil t)
  7262. (kill-whole-line))
  7263. (goto-char (point-min))
  7264. (while (re-search-forward drawer-re nil t)
  7265. (mapc (lambda (d)
  7266. (org-remove-empty-drawer-at d (point))) org-drawers)))
  7267. (goto-char (point-min))
  7268. (when doshift
  7269. (while (re-search-forward org-ts-regexp-both nil t)
  7270. (org-timestamp-change (* n shift-n) shift-what))
  7271. (unless (= n n-no-remove)
  7272. (goto-char (point-min))
  7273. (while (re-search-forward org-ts-regexp nil t)
  7274. (save-excursion
  7275. (goto-char (match-beginning 0))
  7276. (if (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[hdwmy]\\)")
  7277. (delete-region (match-beginning 1) (match-end 1)))))))
  7278. (setq task (buffer-string)))
  7279. (insert task))
  7280. (goto-char beg)))
  7281. ;;; Outline Sorting
  7282. (defun org-sort (with-case)
  7283. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  7284. Optional argument WITH-CASE means sort case-sensitively."
  7285. (interactive "P")
  7286. (cond
  7287. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  7288. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  7289. (t
  7290. (org-call-with-arg 'org-sort-entries with-case))))
  7291. (defun org-sort-remove-invisible (s)
  7292. (remove-text-properties 0 (length s) org-rm-props s)
  7293. (while (string-match org-bracket-link-regexp s)
  7294. (setq s (replace-match (if (match-end 2)
  7295. (match-string 3 s)
  7296. (match-string 1 s)) t t s)))
  7297. s)
  7298. (defvar org-priority-regexp) ; defined later in the file
  7299. (defvar org-after-sorting-entries-or-items-hook nil
  7300. "Hook that is run after a bunch of entries or items have been sorted.
  7301. When children are sorted, the cursor is in the parent line when this
  7302. hook gets called. When a region or a plain list is sorted, the cursor
  7303. will be in the first entry of the sorted region/list.")
  7304. (defun org-sort-entries
  7305. (&optional with-case sorting-type getkey-func compare-func property)
  7306. "Sort entries on a certain level of an outline tree.
  7307. If there is an active region, the entries in the region are sorted.
  7308. Else, if the cursor is before the first entry, sort the top-level items.
  7309. Else, the children of the entry at point are sorted.
  7310. Sorting can be alphabetically, numerically, by date/time as given by
  7311. a time stamp, by a property or by priority.
  7312. The command prompts for the sorting type unless it has been given to the
  7313. function through the SORTING-TYPE argument, which needs to be a character,
  7314. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?r ?R ?f ?F). Here is the
  7315. precise meaning of each character:
  7316. n Numerically, by converting the beginning of the entry/item to a number.
  7317. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  7318. t By date/time, either the first active time stamp in the entry, or, if
  7319. none exist, by the first inactive one.
  7320. s By the scheduled date/time.
  7321. d By deadline date/time.
  7322. c By creation time, which is assumed to be the first inactive time stamp
  7323. at the beginning of a line.
  7324. p By priority according to the cookie.
  7325. r By the value of a property.
  7326. Capital letters will reverse the sort order.
  7327. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  7328. called with point at the beginning of the record. It must return either
  7329. a string or a number that should serve as the sorting key for that record.
  7330. Comparing entries ignores case by default. However, with an optional argument
  7331. WITH-CASE, the sorting considers case as well."
  7332. (interactive "P")
  7333. (let ((case-func (if with-case 'identity 'downcase))
  7334. start beg end stars re re2
  7335. txt what tmp)
  7336. ;; Find beginning and end of region to sort
  7337. (cond
  7338. ((org-region-active-p)
  7339. ;; we will sort the region
  7340. (setq end (region-end)
  7341. what "region")
  7342. (goto-char (region-beginning))
  7343. (if (not (org-at-heading-p)) (outline-next-heading))
  7344. (setq start (point)))
  7345. ((or (org-at-heading-p)
  7346. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  7347. ;; we will sort the children of the current headline
  7348. (org-back-to-heading)
  7349. (setq start (point)
  7350. end (progn (org-end-of-subtree t t)
  7351. (or (bolp) (insert "\n"))
  7352. (org-back-over-empty-lines)
  7353. (point))
  7354. what "children")
  7355. (goto-char start)
  7356. (show-subtree)
  7357. (outline-next-heading))
  7358. (t
  7359. ;; we will sort the top-level entries in this file
  7360. (goto-char (point-min))
  7361. (or (org-at-heading-p) (outline-next-heading))
  7362. (setq start (point))
  7363. (goto-char (point-max))
  7364. (beginning-of-line 1)
  7365. (when (looking-at ".*?\\S-")
  7366. ;; File ends in a non-white line
  7367. (end-of-line 1)
  7368. (insert "\n"))
  7369. (setq end (point-max))
  7370. (setq what "top-level")
  7371. (goto-char start)
  7372. (show-all)))
  7373. (setq beg (point))
  7374. (if (>= beg end) (error "Nothing to sort"))
  7375. (looking-at "\\(\\*+\\)")
  7376. (setq stars (match-string 1)
  7377. re (concat "^" (regexp-quote stars) " +")
  7378. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  7379. txt (buffer-substring beg end))
  7380. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  7381. (if (and (not (equal stars "*")) (string-match re2 txt))
  7382. (error "Region to sort contains a level above the first entry"))
  7383. (unless sorting-type
  7384. (message
  7385. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  7386. [t]ime [s]cheduled [d]eadline [c]reated
  7387. A/N/T/S/D/C/P/O/F means reversed:"
  7388. what)
  7389. (setq sorting-type (read-char-exclusive))
  7390. (and (= (downcase sorting-type) ?f)
  7391. (setq getkey-func
  7392. (org-icompleting-read "Sort using function: "
  7393. obarray 'fboundp t nil nil))
  7394. (setq getkey-func (intern getkey-func)))
  7395. (and (= (downcase sorting-type) ?r)
  7396. (setq property
  7397. (org-icompleting-read "Property: "
  7398. (mapcar 'list (org-buffer-property-keys t))
  7399. nil t))))
  7400. (message "Sorting entries...")
  7401. (save-restriction
  7402. (narrow-to-region start end)
  7403. (let ((dcst (downcase sorting-type))
  7404. (case-fold-search nil)
  7405. (now (current-time)))
  7406. (sort-subr
  7407. (/= dcst sorting-type)
  7408. ;; This function moves to the beginning character of the "record" to
  7409. ;; be sorted.
  7410. (lambda nil
  7411. (if (re-search-forward re nil t)
  7412. (goto-char (match-beginning 0))
  7413. (goto-char (point-max))))
  7414. ;; This function moves to the last character of the "record" being
  7415. ;; sorted.
  7416. (lambda nil
  7417. (save-match-data
  7418. (condition-case nil
  7419. (outline-forward-same-level 1)
  7420. (error
  7421. (goto-char (point-max))))))
  7422. ;; This function returns the value that gets sorted against.
  7423. (lambda nil
  7424. (cond
  7425. ((= dcst ?n)
  7426. (if (looking-at org-complex-heading-regexp)
  7427. (string-to-number (match-string 4))
  7428. nil))
  7429. ((= dcst ?a)
  7430. (if (looking-at org-complex-heading-regexp)
  7431. (funcall case-func (match-string 4))
  7432. nil))
  7433. ((= dcst ?t)
  7434. (let ((end (save-excursion (outline-next-heading) (point))))
  7435. (if (or (re-search-forward org-ts-regexp end t)
  7436. (re-search-forward org-ts-regexp-both end t))
  7437. (org-time-string-to-seconds (match-string 0))
  7438. (org-float-time now))))
  7439. ((= dcst ?c)
  7440. (let ((end (save-excursion (outline-next-heading) (point))))
  7441. (if (re-search-forward
  7442. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  7443. end t)
  7444. (org-time-string-to-seconds (match-string 0))
  7445. (org-float-time now))))
  7446. ((= dcst ?s)
  7447. (let ((end (save-excursion (outline-next-heading) (point))))
  7448. (if (re-search-forward org-scheduled-time-regexp end t)
  7449. (org-time-string-to-seconds (match-string 1))
  7450. (org-float-time now))))
  7451. ((= dcst ?d)
  7452. (let ((end (save-excursion (outline-next-heading) (point))))
  7453. (if (re-search-forward org-deadline-time-regexp end t)
  7454. (org-time-string-to-seconds (match-string 1))
  7455. (org-float-time now))))
  7456. ((= dcst ?p)
  7457. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  7458. (string-to-char (match-string 2))
  7459. org-default-priority))
  7460. ((= dcst ?r)
  7461. (or (org-entry-get nil property) ""))
  7462. ((= dcst ?o)
  7463. (if (looking-at org-complex-heading-regexp)
  7464. (- 9999 (length (member (match-string 2)
  7465. org-todo-keywords-1)))))
  7466. ((= dcst ?f)
  7467. (if getkey-func
  7468. (progn
  7469. (setq tmp (funcall getkey-func))
  7470. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  7471. tmp)
  7472. (error "Invalid key function `%s'" getkey-func)))
  7473. (t (error "Invalid sorting type `%c'" sorting-type))))
  7474. nil
  7475. (cond
  7476. ((= dcst ?a) 'string<)
  7477. ((= dcst ?f) compare-func)
  7478. ((member dcst '(?p ?t ?s ?d ?c)) '<)
  7479. (t nil)))))
  7480. (run-hooks 'org-after-sorting-entries-or-items-hook)
  7481. (message "Sorting entries...done")))
  7482. (defun org-do-sort (table what &optional with-case sorting-type)
  7483. "Sort TABLE of WHAT according to SORTING-TYPE.
  7484. The user will be prompted for the SORTING-TYPE if the call to this
  7485. function does not specify it. WHAT is only for the prompt, to indicate
  7486. what is being sorted. The sorting key will be extracted from
  7487. the car of the elements of the table.
  7488. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  7489. (unless sorting-type
  7490. (message
  7491. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  7492. what)
  7493. (setq sorting-type (read-char-exclusive)))
  7494. (let ((dcst (downcase sorting-type))
  7495. extractfun comparefun)
  7496. ;; Define the appropriate functions
  7497. (cond
  7498. ((= dcst ?n)
  7499. (setq extractfun 'string-to-number
  7500. comparefun (if (= dcst sorting-type) '< '>)))
  7501. ((= dcst ?a)
  7502. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  7503. (lambda(x) (downcase (org-sort-remove-invisible x))))
  7504. comparefun (if (= dcst sorting-type)
  7505. 'string<
  7506. (lambda (a b) (and (not (string< a b))
  7507. (not (string= a b)))))))
  7508. ((= dcst ?t)
  7509. (setq extractfun
  7510. (lambda (x)
  7511. (if (or (string-match org-ts-regexp x)
  7512. (string-match org-ts-regexp-both x))
  7513. (org-float-time
  7514. (org-time-string-to-time (match-string 0 x)))
  7515. 0))
  7516. comparefun (if (= dcst sorting-type) '< '>)))
  7517. (t (error "Invalid sorting type `%c'" sorting-type)))
  7518. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  7519. table)
  7520. (lambda (a b) (funcall comparefun (car a) (car b))))))
  7521. ;;; The orgstruct minor mode
  7522. ;; Define a minor mode which can be used in other modes in order to
  7523. ;; integrate the org-mode structure editing commands.
  7524. ;; This is really a hack, because the org-mode structure commands use
  7525. ;; keys which normally belong to the major mode. Here is how it
  7526. ;; works: The minor mode defines all the keys necessary to operate the
  7527. ;; structure commands, but wraps the commands into a function which
  7528. ;; tests if the cursor is currently at a headline or a plain list
  7529. ;; item. If that is the case, the structure command is used,
  7530. ;; temporarily setting many Org-mode variables like regular
  7531. ;; expressions for filling etc. However, when any of those keys is
  7532. ;; used at a different location, function uses `key-binding' to look
  7533. ;; up if the key has an associated command in another currently active
  7534. ;; keymap (minor modes, major mode, global), and executes that
  7535. ;; command. There might be problems if any of the keys is otherwise
  7536. ;; used as a prefix key.
  7537. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  7538. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  7539. ;; addresses this by checking explicitly for both bindings.
  7540. (defvar orgstruct-mode-map (make-sparse-keymap)
  7541. "Keymap for the minor `orgstruct-mode'.")
  7542. (defvar org-local-vars nil
  7543. "List of local variables, for use by `orgstruct-mode'.")
  7544. ;;;###autoload
  7545. (define-minor-mode orgstruct-mode
  7546. "Toggle the minor mode `orgstruct-mode'.
  7547. This mode is for using Org-mode structure commands in other
  7548. modes. The following keys behave as if Org-mode were active, if
  7549. the cursor is on a headline, or on a plain list item (both as
  7550. defined by Org-mode).
  7551. M-up Move entry/item up
  7552. M-down Move entry/item down
  7553. M-left Promote
  7554. M-right Demote
  7555. M-S-up Move entry/item up
  7556. M-S-down Move entry/item down
  7557. M-S-left Promote subtree
  7558. M-S-right Demote subtree
  7559. M-q Fill paragraph and items like in Org-mode
  7560. C-c ^ Sort entries
  7561. C-c - Cycle list bullet
  7562. TAB Cycle item visibility
  7563. M-RET Insert new heading/item
  7564. S-M-RET Insert new TODO heading / Checkbox item
  7565. C-c C-c Set tags / toggle checkbox"
  7566. nil " OrgStruct" nil
  7567. (org-load-modules-maybe)
  7568. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  7569. ;;;###autoload
  7570. (defun turn-on-orgstruct ()
  7571. "Unconditionally turn on `orgstruct-mode'."
  7572. (orgstruct-mode 1))
  7573. (defvar org-fb-vars nil)
  7574. (make-variable-buffer-local 'org-fb-vars)
  7575. (defun orgstruct++-mode (&optional arg)
  7576. "Toggle `orgstruct-mode', the enhanced version of it.
  7577. In addition to setting orgstruct-mode, this also exports all
  7578. indentation and autofilling variables from org-mode into the
  7579. buffer. It will also recognize item context in multiline items."
  7580. (interactive "P")
  7581. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  7582. (if (< arg 1)
  7583. (progn (orgstruct-mode -1)
  7584. (mapc (lambda(v)
  7585. (org-set-local (car v)
  7586. (if (eq (car-safe (cadr v)) 'quote) (cadadr v) (cadr v))))
  7587. org-fb-vars))
  7588. (orgstruct-mode 1)
  7589. (setq org-fb-vars nil)
  7590. (let (var val)
  7591. (mapc
  7592. (lambda (x)
  7593. (when (string-match
  7594. "^\\(paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|indent-\\)"
  7595. (symbol-name (car x)))
  7596. (setq var (car x) val (nth 1 x))
  7597. (push (list var `(quote ,(eval var))) org-fb-vars)
  7598. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  7599. org-local-vars)
  7600. (org-set-local 'orgstruct-is-++ t))))
  7601. (defvar orgstruct-is-++ nil
  7602. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  7603. (make-variable-buffer-local 'orgstruct-is-++)
  7604. ;;;###autoload
  7605. (defun turn-on-orgstruct++ ()
  7606. "Unconditionally turn on `orgstruct++-mode'."
  7607. (orgstruct++-mode 1))
  7608. (defun orgstruct-error ()
  7609. "Error when there is no default binding for a structure key."
  7610. (interactive)
  7611. (error "This key has no function outside structure elements"))
  7612. (defun orgstruct-setup ()
  7613. "Setup orgstruct keymaps."
  7614. (let ((nfunc 0)
  7615. (bindings
  7616. (list
  7617. '([(meta up)] org-metaup)
  7618. '([(meta down)] org-metadown)
  7619. '([(meta left)] org-metaleft)
  7620. '([(meta right)] org-metaright)
  7621. '([(meta shift up)] org-shiftmetaup)
  7622. '([(meta shift down)] org-shiftmetadown)
  7623. '([(meta shift left)] org-shiftmetaleft)
  7624. '([(meta shift right)] org-shiftmetaright)
  7625. '([?\e (up)] org-metaup)
  7626. '([?\e (down)] org-metadown)
  7627. '([?\e (left)] org-metaleft)
  7628. '([?\e (right)] org-metaright)
  7629. '([?\e (shift up)] org-shiftmetaup)
  7630. '([?\e (shift down)] org-shiftmetadown)
  7631. '([?\e (shift left)] org-shiftmetaleft)
  7632. '([?\e (shift right)] org-shiftmetaright)
  7633. '([(shift up)] org-shiftup)
  7634. '([(shift down)] org-shiftdown)
  7635. '([(shift left)] org-shiftleft)
  7636. '([(shift right)] org-shiftright)
  7637. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  7638. '("\M-q" fill-paragraph)
  7639. '("\C-c^" org-sort)
  7640. '("\C-c-" org-cycle-list-bullet)))
  7641. elt key fun cmd)
  7642. (while (setq elt (pop bindings))
  7643. (setq nfunc (1+ nfunc))
  7644. (setq key (org-key (car elt))
  7645. fun (nth 1 elt)
  7646. cmd (orgstruct-make-binding fun nfunc key))
  7647. (org-defkey orgstruct-mode-map key cmd))
  7648. ;; Special treatment needed for TAB and RET
  7649. (org-defkey orgstruct-mode-map [(tab)]
  7650. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  7651. (org-defkey orgstruct-mode-map "\C-i"
  7652. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  7653. (org-defkey orgstruct-mode-map "\M-\C-m"
  7654. (orgstruct-make-binding 'org-insert-heading 105
  7655. "\M-\C-m" [(meta return)]))
  7656. (org-defkey orgstruct-mode-map [(meta return)]
  7657. (orgstruct-make-binding 'org-insert-heading 106
  7658. [(meta return)] "\M-\C-m"))
  7659. (org-defkey orgstruct-mode-map [(shift meta return)]
  7660. (orgstruct-make-binding 'org-insert-todo-heading 107
  7661. [(meta return)] "\M-\C-m"))
  7662. (org-defkey orgstruct-mode-map "\e\C-m"
  7663. (orgstruct-make-binding 'org-insert-heading 108
  7664. "\e\C-m" [?\e (return)]))
  7665. (org-defkey orgstruct-mode-map [?\e (return)]
  7666. (orgstruct-make-binding 'org-insert-heading 109
  7667. [?\e (return)] "\e\C-m"))
  7668. (org-defkey orgstruct-mode-map [?\e (shift return)]
  7669. (orgstruct-make-binding 'org-insert-todo-heading 110
  7670. [?\e (return)] "\e\C-m"))
  7671. (unless org-local-vars
  7672. (setq org-local-vars (org-get-local-variables)))
  7673. t))
  7674. (defun orgstruct-make-binding (fun n &rest keys)
  7675. "Create a function for binding in the structure minor mode.
  7676. FUN is the command to call inside a table. N is used to create a unique
  7677. command name. KEYS are keys that should be checked in for a command
  7678. to execute outside of tables."
  7679. (eval
  7680. (list 'defun
  7681. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  7682. '(arg)
  7683. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  7684. "Outside of structure, run the binding of `"
  7685. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  7686. "'.")
  7687. '(interactive "p")
  7688. (list 'if
  7689. `(org-context-p 'headline 'item
  7690. (and orgstruct-is-++
  7691. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  7692. 'item-body))
  7693. (list 'org-run-like-in-org-mode (list 'quote fun))
  7694. (list 'let '(orgstruct-mode)
  7695. (list 'call-interactively
  7696. (append '(or)
  7697. (mapcar (lambda (k)
  7698. (list 'key-binding k))
  7699. keys)
  7700. '('orgstruct-error))))))))
  7701. (defun org-context-p (&rest contexts)
  7702. "Check if local context is any of CONTEXTS.
  7703. Possible values in the list of contexts are `table', `headline', and `item'."
  7704. (let ((pos (point)))
  7705. (goto-char (point-at-bol))
  7706. (prog1 (or (and (memq 'table contexts)
  7707. (looking-at "[ \t]*|"))
  7708. (and (memq 'headline contexts)
  7709. (looking-at org-outline-regexp))
  7710. (and (memq 'item contexts)
  7711. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  7712. (and (memq 'item-body contexts)
  7713. (org-in-item-p)))
  7714. (goto-char pos))))
  7715. (defun org-get-local-variables ()
  7716. "Return a list of all local variables in an Org mode buffer."
  7717. (let (varlist)
  7718. (with-current-buffer (get-buffer-create "*Org tmp*")
  7719. (erase-buffer)
  7720. (org-mode)
  7721. (setq varlist (buffer-local-variables)))
  7722. (kill-buffer "*Org tmp*")
  7723. (delq nil
  7724. (mapcar
  7725. (lambda (x)
  7726. (setq x
  7727. (if (symbolp x)
  7728. (list x)
  7729. (list (car x) (list 'quote (cdr x)))))
  7730. (if (string-match
  7731. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|indent-\\)"
  7732. (symbol-name (car x)))
  7733. x nil))
  7734. varlist))))
  7735. (defun org-clone-local-variables (from-buffer &optional regexp)
  7736. "Clone local variables from FROM-BUFFER.
  7737. Optional argument REGEXP selects variables to clone."
  7738. (mapc
  7739. (lambda (pair)
  7740. (and (symbolp (car pair))
  7741. (or (null regexp)
  7742. (string-match regexp (symbol-name (car pair))))
  7743. (set (make-local-variable (car pair))
  7744. (cdr pair))))
  7745. (buffer-local-variables from-buffer)))
  7746. ;;;###autoload
  7747. (defun org-run-like-in-org-mode (cmd)
  7748. "Run a command, pretending that the current buffer is in Org-mode.
  7749. This will temporarily bind local variables that are typically bound in
  7750. Org-mode to the values they have in Org-mode, and then interactively
  7751. call CMD."
  7752. (org-load-modules-maybe)
  7753. (unless org-local-vars
  7754. (setq org-local-vars (org-get-local-variables)))
  7755. (eval (list 'let org-local-vars
  7756. (list 'call-interactively (list 'quote cmd)))))
  7757. ;;;; Archiving
  7758. (defun org-get-category (&optional pos force-refresh)
  7759. "Get the category applying to position POS."
  7760. (save-match-data
  7761. (if force-refresh (org-refresh-category-properties))
  7762. (let ((pos (or pos (point))))
  7763. (or (get-text-property pos 'org-category)
  7764. (progn (org-refresh-category-properties)
  7765. (get-text-property pos 'org-category))))))
  7766. (defun org-refresh-category-properties ()
  7767. "Refresh category text properties in the buffer."
  7768. (let ((inhibit-read-only t)
  7769. (def-cat (cond
  7770. ((null org-category)
  7771. (if buffer-file-name
  7772. (file-name-sans-extension
  7773. (file-name-nondirectory buffer-file-name))
  7774. "???"))
  7775. ((symbolp org-category) (symbol-name org-category))
  7776. (t org-category)))
  7777. beg end cat pos optionp)
  7778. (org-unmodified
  7779. (save-excursion
  7780. (save-restriction
  7781. (widen)
  7782. (goto-char (point-min))
  7783. (put-text-property (point) (point-max) 'org-category def-cat)
  7784. (while (re-search-forward
  7785. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  7786. (setq pos (match-end 0)
  7787. optionp (equal (char-after (match-beginning 0)) ?#)
  7788. cat (org-trim (match-string 2)))
  7789. (if optionp
  7790. (setq beg (point-at-bol) end (point-max))
  7791. (org-back-to-heading t)
  7792. (setq beg (point) end (org-end-of-subtree t t)))
  7793. (put-text-property beg end 'org-category cat)
  7794. (put-text-property beg end 'org-category-position beg)
  7795. (goto-char pos)))))))
  7796. ;;;; Link Stuff
  7797. ;;; Link abbreviations
  7798. (defun org-link-expand-abbrev (link)
  7799. "Apply replacements as defined in `org-link-abbrev-alist'."
  7800. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  7801. (let* ((key (match-string 1 link))
  7802. (as (or (assoc key org-link-abbrev-alist-local)
  7803. (assoc key org-link-abbrev-alist)))
  7804. (tag (and (match-end 2) (match-string 3 link)))
  7805. rpl)
  7806. (if (not as)
  7807. link
  7808. (setq rpl (cdr as))
  7809. (cond
  7810. ((symbolp rpl) (funcall rpl tag))
  7811. ((string-match "%(\\([^)]+\\))" rpl)
  7812. (replace-match (funcall (intern-soft (match-string 1 rpl)) tag) t t rpl))
  7813. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  7814. ((string-match "%h" rpl)
  7815. (replace-match (url-hexify-string (or tag "")) t t rpl))
  7816. (t (concat rpl tag)))))
  7817. link))
  7818. ;;; Storing and inserting links
  7819. (defvar org-insert-link-history nil
  7820. "Minibuffer history for links inserted with `org-insert-link'.")
  7821. (defvar org-stored-links nil
  7822. "Contains the links stored with `org-store-link'.")
  7823. (defvar org-store-link-plist nil
  7824. "Plist with info about the most recently link created with `org-store-link'.")
  7825. (defvar org-link-protocols nil
  7826. "Link protocols added to Org-mode using `org-add-link-type'.")
  7827. (defvar org-store-link-functions nil
  7828. "List of functions that are called to create and store a link.
  7829. Each function will be called in turn until one returns a non-nil
  7830. value. Each function should check if it is responsible for creating
  7831. this link (for example by looking at the major mode).
  7832. If not, it must exit and return nil.
  7833. If yes, it should return a non-nil value after a calling
  7834. `org-store-link-props' with a list of properties and values.
  7835. Special properties are:
  7836. :type The link prefix, like \"http\". This must be given.
  7837. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  7838. This is obligatory as well.
  7839. :description Optional default description for the second pair
  7840. of brackets in an Org-mode link. The user can still change
  7841. this when inserting this link into an Org-mode buffer.
  7842. In addition to these, any additional properties can be specified
  7843. and then used in remember templates.")
  7844. (defun org-add-link-type (type &optional follow export)
  7845. "Add TYPE to the list of `org-link-types'.
  7846. Re-compute all regular expressions depending on `org-link-types'
  7847. FOLLOW and EXPORT are two functions.
  7848. FOLLOW should take the link path as the single argument and do whatever
  7849. is necessary to follow the link, for example find a file or display
  7850. a mail message.
  7851. EXPORT should format the link path for export to one of the export formats.
  7852. It should be a function accepting three arguments:
  7853. path the path of the link, the text after the prefix (like \"http:\")
  7854. desc the description of the link, if any, or a description added by
  7855. org-export-normalize-links if there is none
  7856. format the export format, a symbol like `html' or `latex' or `ascii'..
  7857. The function may use the FORMAT information to return different values
  7858. depending on the format. The return value will be put literally into
  7859. the exported file. If the return value is nil, this means Org should
  7860. do what it normally does with links which do not have EXPORT defined.
  7861. Org-mode has a built-in default for exporting links. If you are happy with
  7862. this default, there is no need to define an export function for the link
  7863. type. For a simple example of an export function, see `org-bbdb.el'."
  7864. (add-to-list 'org-link-types type t)
  7865. (org-make-link-regexps)
  7866. (if (assoc type org-link-protocols)
  7867. (setcdr (assoc type org-link-protocols) (list follow export))
  7868. (push (list type follow export) org-link-protocols)))
  7869. (defvar org-agenda-buffer-name)
  7870. ;;;###autoload
  7871. (defun org-store-link (arg)
  7872. "\\<org-mode-map>Store an org-link to the current location.
  7873. This link is added to `org-stored-links' and can later be inserted
  7874. into an org-buffer with \\[org-insert-link].
  7875. For some link types, a prefix arg is interpreted:
  7876. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  7877. For file links, arg negates `org-context-in-file-links'."
  7878. (interactive "P")
  7879. (org-load-modules-maybe)
  7880. (setq org-store-link-plist nil) ; reset
  7881. (org-with-limited-levels
  7882. (let (link cpltxt desc description search txt custom-id agenda-link)
  7883. (cond
  7884. ((run-hook-with-args-until-success 'org-store-link-functions)
  7885. (setq link (plist-get org-store-link-plist :link)
  7886. desc (or (plist-get org-store-link-plist :description) link)))
  7887. ((org-src-edit-buffer-p)
  7888. (let (label gc)
  7889. (while (or (not label)
  7890. (save-excursion
  7891. (save-restriction
  7892. (widen)
  7893. (goto-char (point-min))
  7894. (re-search-forward
  7895. (regexp-quote (format org-coderef-label-format label))
  7896. nil t))))
  7897. (when label (message "Label exists already") (sit-for 2))
  7898. (setq label (read-string "Code line label: " label)))
  7899. (end-of-line 1)
  7900. (setq link (format org-coderef-label-format label))
  7901. (setq gc (- 79 (length link)))
  7902. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  7903. (insert link)
  7904. (setq link (concat "(" label ")") desc nil)))
  7905. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  7906. ;; We are in the agenda, link to referenced location
  7907. (let ((m (or (get-text-property (point) 'org-hd-marker)
  7908. (get-text-property (point) 'org-marker))))
  7909. (when m
  7910. (org-with-point-at m
  7911. (setq agenda-link
  7912. (if (org-called-interactively-p 'any)
  7913. (call-interactively 'org-store-link)
  7914. (org-store-link nil)))))))
  7915. ((eq major-mode 'calendar-mode)
  7916. (let ((cd (calendar-cursor-to-date)))
  7917. (setq link
  7918. (format-time-string
  7919. (car org-time-stamp-formats)
  7920. (apply 'encode-time
  7921. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  7922. nil nil nil))))
  7923. (org-store-link-props :type "calendar" :date cd)))
  7924. ((eq major-mode 'w3-mode)
  7925. (setq cpltxt (if (and (buffer-name)
  7926. (not (string-match "Untitled" (buffer-name))))
  7927. (buffer-name)
  7928. (url-view-url t))
  7929. link (org-make-link (url-view-url t)))
  7930. (org-store-link-props :type "w3" :url (url-view-url t)))
  7931. ((eq major-mode 'w3m-mode)
  7932. (setq cpltxt (or w3m-current-title w3m-current-url)
  7933. link (org-make-link w3m-current-url))
  7934. (org-store-link-props :type "w3m" :url (url-view-url t)))
  7935. ((setq search (run-hook-with-args-until-success
  7936. 'org-create-file-search-functions))
  7937. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  7938. "::" search))
  7939. (setq cpltxt (or description link)))
  7940. ((eq major-mode 'image-mode)
  7941. (setq cpltxt (concat "file:"
  7942. (abbreviate-file-name buffer-file-name))
  7943. link (org-make-link cpltxt))
  7944. (org-store-link-props :type "image" :file buffer-file-name))
  7945. ((eq major-mode 'dired-mode)
  7946. ;; link to the file in the current line
  7947. (let ((file (dired-get-filename nil t)))
  7948. (setq file (if file
  7949. (abbreviate-file-name
  7950. (expand-file-name (dired-get-filename nil t)))
  7951. ;; otherwise, no file so use current directory.
  7952. default-directory))
  7953. (setq cpltxt (concat "file:" file)
  7954. link (org-make-link cpltxt))))
  7955. ((and (buffer-file-name (buffer-base-buffer)) (derived-mode-p 'org-mode))
  7956. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  7957. (cond
  7958. ((org-in-regexp "<<\\(.*?\\)>>")
  7959. (setq cpltxt
  7960. (concat "file:"
  7961. (abbreviate-file-name
  7962. (buffer-file-name (buffer-base-buffer)))
  7963. "::" (match-string 1))
  7964. link (org-make-link cpltxt)))
  7965. ((and (featurep 'org-id)
  7966. (or (eq org-link-to-org-use-id t)
  7967. (and (eq org-link-to-org-use-id 'create-if-interactive)
  7968. (org-called-interactively-p 'any))
  7969. (and (eq org-link-to-org-use-id
  7970. 'create-if-interactive-and-no-custom-id)
  7971. (org-called-interactively-p 'any)
  7972. (not custom-id))
  7973. (and org-link-to-org-use-id
  7974. (org-entry-get nil "ID"))))
  7975. ;; We can make a link using the ID.
  7976. (setq link (condition-case nil
  7977. (prog1 (org-id-store-link)
  7978. (setq desc (plist-get org-store-link-plist
  7979. :description)))
  7980. (error
  7981. ;; probably before first headline, link to file only
  7982. (concat "file:"
  7983. (abbreviate-file-name
  7984. (buffer-file-name (buffer-base-buffer))))))))
  7985. (t
  7986. ;; Just link to current headline
  7987. (setq cpltxt (concat "file:"
  7988. (abbreviate-file-name
  7989. (buffer-file-name (buffer-base-buffer)))))
  7990. ;; Add a context search string
  7991. (when (org-xor org-context-in-file-links arg)
  7992. (setq txt (cond
  7993. ((org-at-heading-p) nil)
  7994. ((org-region-active-p)
  7995. (buffer-substring (region-beginning) (region-end)))
  7996. (t nil)))
  7997. (when (or (null txt) (string-match "\\S-" txt))
  7998. (setq cpltxt
  7999. (concat cpltxt "::"
  8000. (condition-case nil
  8001. (org-make-org-heading-search-string txt)
  8002. (error "")))
  8003. desc (or (nth 4 (ignore-errors
  8004. (org-heading-components))) "NONE"))))
  8005. (if (string-match "::\\'" cpltxt)
  8006. (setq cpltxt (substring cpltxt 0 -2)))
  8007. (setq link (org-make-link cpltxt)))))
  8008. ((buffer-file-name (buffer-base-buffer))
  8009. ;; Just link to this file here.
  8010. (setq cpltxt (concat "file:"
  8011. (abbreviate-file-name
  8012. (buffer-file-name (buffer-base-buffer)))))
  8013. ;; Add a context string
  8014. (when (org-xor org-context-in-file-links arg)
  8015. (setq txt (if (org-region-active-p)
  8016. (buffer-substring (region-beginning) (region-end))
  8017. (buffer-substring (point-at-bol) (point-at-eol))))
  8018. ;; Only use search option if there is some text.
  8019. (when (string-match "\\S-" txt)
  8020. (setq cpltxt
  8021. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  8022. desc "NONE")))
  8023. (setq link (org-make-link cpltxt)))
  8024. ((org-called-interactively-p 'interactive)
  8025. (error "Cannot link to a buffer which is not visiting a file"))
  8026. (t (setq link nil)))
  8027. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  8028. (setq link (or link cpltxt)
  8029. desc (or desc cpltxt))
  8030. (if (equal desc "NONE") (setq desc nil))
  8031. (if (and (or (org-called-interactively-p 'any) executing-kbd-macro) link)
  8032. (progn
  8033. (setq org-stored-links
  8034. (cons (list link desc) org-stored-links))
  8035. (message "Stored: %s" (or desc link))
  8036. (when custom-id
  8037. (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
  8038. "::#" custom-id))
  8039. (setq org-stored-links
  8040. (cons (list link desc) org-stored-links))))
  8041. (or agenda-link (and link (org-make-link-string link desc)))))))
  8042. (defun org-store-link-props (&rest plist)
  8043. "Store link properties, extract names and addresses."
  8044. (let (x adr)
  8045. (when (setq x (plist-get plist :from))
  8046. (setq adr (mail-extract-address-components x))
  8047. (setq plist (plist-put plist :fromname (car adr)))
  8048. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  8049. (when (setq x (plist-get plist :to))
  8050. (setq adr (mail-extract-address-components x))
  8051. (setq plist (plist-put plist :toname (car adr)))
  8052. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  8053. (let ((from (plist-get plist :from))
  8054. (to (plist-get plist :to)))
  8055. (when (and from to org-from-is-user-regexp)
  8056. (setq plist
  8057. (plist-put plist :fromto
  8058. (if (string-match org-from-is-user-regexp from)
  8059. (concat "to %t")
  8060. (concat "from %f"))))))
  8061. (setq org-store-link-plist plist))
  8062. (defun org-add-link-props (&rest plist)
  8063. "Add these properties to the link property list."
  8064. (let (key value)
  8065. (while plist
  8066. (setq key (pop plist) value (pop plist))
  8067. (setq org-store-link-plist
  8068. (plist-put org-store-link-plist key value)))))
  8069. (defun org-email-link-description (&optional fmt)
  8070. "Return the description part of an email link.
  8071. This takes information from `org-store-link-plist' and formats it
  8072. according to FMT (default from `org-email-link-description-format')."
  8073. (setq fmt (or fmt org-email-link-description-format))
  8074. (let* ((p org-store-link-plist)
  8075. (to (plist-get p :toaddress))
  8076. (from (plist-get p :fromaddress))
  8077. (table
  8078. (list
  8079. (cons "%c" (plist-get p :fromto))
  8080. (cons "%F" (plist-get p :from))
  8081. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  8082. (cons "%T" (plist-get p :to))
  8083. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  8084. (cons "%s" (plist-get p :subject))
  8085. (cons "%d" (plist-get p :date))
  8086. (cons "%m" (plist-get p :message-id)))))
  8087. (when (string-match "%c" fmt)
  8088. ;; Check if the user wrote this message
  8089. (if (and org-from-is-user-regexp from to
  8090. (save-match-data (string-match org-from-is-user-regexp from)))
  8091. (setq fmt (replace-match "to %t" t t fmt))
  8092. (setq fmt (replace-match "from %f" t t fmt))))
  8093. (org-replace-escapes fmt table)))
  8094. (defun org-make-org-heading-search-string (&optional string heading)
  8095. "Make search string for STRING or current headline."
  8096. (interactive)
  8097. (let ((s (or string (org-get-heading)))
  8098. (lines org-context-in-file-links))
  8099. (unless (and string (not heading))
  8100. ;; We are using a headline, clean up garbage in there.
  8101. (if (string-match org-todo-regexp s)
  8102. (setq s (replace-match "" t t s)))
  8103. (if (string-match (org-re ":[[:alnum:]_@#%:]+:[ \t]*$") s)
  8104. (setq s (replace-match "" t t s)))
  8105. (setq s (org-trim s))
  8106. (if (string-match (concat "^\\(" org-quote-string "\\|"
  8107. org-comment-string "\\)") s)
  8108. (setq s (replace-match "" t t s)))
  8109. (while (string-match org-ts-regexp s)
  8110. (setq s (replace-match "" t t s))))
  8111. (or string (setq s (concat "*" s))) ; Add * for headlines
  8112. (when (and string (integerp lines) (> lines 0))
  8113. (let ((slines (org-split-string s "\n")))
  8114. (when (< lines (length slines))
  8115. (setq s (mapconcat
  8116. 'identity
  8117. (reverse (nthcdr (- (length slines) lines)
  8118. (reverse slines))) "\n")))))
  8119. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  8120. (defun org-make-link (&rest strings)
  8121. "Concatenate STRINGS."
  8122. (apply 'concat strings))
  8123. (defun org-make-link-string (link &optional description)
  8124. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  8125. (unless (string-match "\\S-" link)
  8126. (error "Empty link"))
  8127. (when (and description
  8128. (stringp description)
  8129. (not (string-match "\\S-" description)))
  8130. (setq description nil))
  8131. (when (stringp description)
  8132. ;; Remove brackets from the description, they are fatal.
  8133. (while (string-match "\\[" description)
  8134. (setq description (replace-match "{" t t description)))
  8135. (while (string-match "\\]" description)
  8136. (setq description (replace-match "}" t t description))))
  8137. (when (equal link description)
  8138. ;; No description needed, it is identical
  8139. (setq description nil))
  8140. (when (and (not description)
  8141. (not (string-match (org-image-file-name-regexp) link))
  8142. (not (equal link (org-link-escape link))))
  8143. (setq description (org-extract-attributes link)))
  8144. (setq link
  8145. (cond ((string-match (org-image-file-name-regexp) link) link)
  8146. ((string-match org-link-types-re link)
  8147. (concat (match-string 1 link)
  8148. (org-link-escape (substring link (match-end 1)))))
  8149. (t (org-link-escape link))))
  8150. (concat "[[" link "]"
  8151. (if description (concat "[" description "]") "")
  8152. "]"))
  8153. (defconst org-link-escape-chars
  8154. '(?\ ?\[ ?\] ?\; ?\= ?\+)
  8155. "List of characters that should be escaped in link.
  8156. This is the list that is used for internal purposes.")
  8157. (defvar org-url-encoding-use-url-hexify nil)
  8158. (defconst org-link-escape-chars-browser
  8159. '(?\ )
  8160. "List of escapes for characters that are problematic in links.
  8161. This is the list that is used before handing over to the browser.")
  8162. (defun org-link-escape (text &optional table merge)
  8163. "Return percent escaped representation of TEXT.
  8164. TEXT is a string with the text to escape.
  8165. Optional argument TABLE is a list with characters that should be
  8166. escaped. When nil, `org-link-escape-chars' is used.
  8167. If optional argument MERGE is set, merge TABLE into
  8168. `org-link-escape-chars'."
  8169. (if (and org-url-encoding-use-url-hexify (not table))
  8170. (url-hexify-string text)
  8171. (cond
  8172. ((and table merge)
  8173. (mapc (lambda (defchr)
  8174. (unless (member defchr table)
  8175. (setq table (cons defchr table)))) org-link-escape-chars))
  8176. ((null table)
  8177. (setq table org-link-escape-chars)))
  8178. (mapconcat
  8179. (lambda (char)
  8180. (if (or (member char table)
  8181. (< char 32) (= char 37) (> char 126))
  8182. (mapconcat (lambda (sequence-element)
  8183. (format "%%%.2X" sequence-element))
  8184. (or (encode-coding-char char 'utf-8)
  8185. (error "Unable to percent escape character: %s"
  8186. (char-to-string char))) "")
  8187. (char-to-string char))) text "")))
  8188. (defun org-link-unescape (str)
  8189. "Unhex hexified Unicode strings as returned from the JavaScript function
  8190. encodeURIComponent. E.g. `%C3%B6' is the german Umlaut `ö'."
  8191. (unless (and (null str) (string= "" str))
  8192. (let ((pos 0) (case-fold-search t) unhexed)
  8193. (while (setq pos (string-match "\\(%[0-9a-f][0-9a-f]\\)+" str pos))
  8194. (setq unhexed (org-link-unescape-compound (match-string 0 str)))
  8195. (setq str (replace-match unhexed t t str))
  8196. (setq pos (+ pos (length unhexed))))))
  8197. str)
  8198. (defun org-link-unescape-compound (hex)
  8199. "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German Umlaut `ö'.
  8200. Note: this function also decodes single byte encodings like
  8201. `%E1' (\"á\") if not followed by another `%[A-F0-9]{2}' group."
  8202. (save-match-data
  8203. (let* ((bytes (cdr (split-string hex "%")))
  8204. (ret "")
  8205. (eat 0)
  8206. (sum 0))
  8207. (while bytes
  8208. (let* ((val (string-to-number (pop bytes) 16))
  8209. (shift-xor
  8210. (if (= 0 eat)
  8211. (cond
  8212. ((>= val 252) (cons 6 252))
  8213. ((>= val 248) (cons 5 248))
  8214. ((>= val 240) (cons 4 240))
  8215. ((>= val 224) (cons 3 224))
  8216. ((>= val 192) (cons 2 192))
  8217. (t (cons 0 0)))
  8218. (cons 6 128))))
  8219. (if (>= val 192) (setq eat (car shift-xor)))
  8220. (setq val (logxor val (cdr shift-xor)))
  8221. (setq sum (+ (lsh sum (car shift-xor)) val))
  8222. (if (> eat 0) (setq eat (- eat 1)))
  8223. (cond
  8224. ((= 0 eat) ;multi byte
  8225. (setq ret (concat ret (org-char-to-string sum)))
  8226. (setq sum 0))
  8227. ((not bytes) ; single byte(s)
  8228. (setq ret (org-link-unescape-single-byte-sequence hex))))
  8229. )) ;; end (while bytes
  8230. ret )))
  8231. (defun org-link-unescape-single-byte-sequence (hex)
  8232. "Unhexify hex-encoded single byte character sequences."
  8233. (mapconcat (lambda (byte)
  8234. (char-to-string (string-to-number byte 16)))
  8235. (cdr (split-string hex "%")) ""))
  8236. (defun org-xor (a b)
  8237. "Exclusive or."
  8238. (if a (not b) b))
  8239. (defun org-fixup-message-id-for-http (s)
  8240. "Replace special characters in a message id, so it can be used in an http query."
  8241. (when (string-match "%" s)
  8242. (setq s (mapconcat (lambda (c)
  8243. (if (eq c ?%)
  8244. "%25"
  8245. (char-to-string c)))
  8246. s "")))
  8247. (while (string-match "<" s)
  8248. (setq s (replace-match "%3C" t t s)))
  8249. (while (string-match ">" s)
  8250. (setq s (replace-match "%3E" t t s)))
  8251. (while (string-match "@" s)
  8252. (setq s (replace-match "%40" t t s)))
  8253. s)
  8254. (defun org-link-prettify (link)
  8255. "Return a human-readable representation of LINK.
  8256. The car of LINK must be a raw link the cdr of LINK must be either
  8257. a link description or nil."
  8258. (let ((desc (or (cadr link) "<no description>")))
  8259. (concat (format "%-45s" (substring desc 0 (min (length desc) 40)))
  8260. "[[" (car link) "]]")))
  8261. ;;;###autoload
  8262. (defun org-insert-link-global ()
  8263. "Insert a link like Org-mode does.
  8264. This command can be called in any mode to insert a link in Org-mode syntax."
  8265. (interactive)
  8266. (org-load-modules-maybe)
  8267. (org-run-like-in-org-mode 'org-insert-link))
  8268. (defun org-insert-all-links (&optional keep)
  8269. "Insert all links in `org-stored-links'."
  8270. (interactive "P")
  8271. (let ((links (copy-sequence org-stored-links)) l)
  8272. (while (setq l (if keep (pop links) (pop org-stored-links)))
  8273. (insert "- ")
  8274. (org-insert-link nil (car l) (cadr l))
  8275. (insert "\n"))))
  8276. (defun org-insert-link (&optional complete-file link-location default-description)
  8277. "Insert a link. At the prompt, enter the link.
  8278. Completion can be used to insert any of the link protocol prefixes like
  8279. http or ftp in use.
  8280. The history can be used to select a link previously stored with
  8281. `org-store-link'. When the empty string is entered (i.e. if you just
  8282. press RET at the prompt), the link defaults to the most recently
  8283. stored link. As SPC triggers completion in the minibuffer, you need to
  8284. use M-SPC or C-q SPC to force the insertion of a space character.
  8285. You will also be prompted for a description, and if one is given, it will
  8286. be displayed in the buffer instead of the link.
  8287. If there is already a link at point, this command will allow you to edit link
  8288. and description parts.
  8289. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  8290. be selected using completion. The path to the file will be relative to the
  8291. current directory if the file is in the current directory or a subdirectory.
  8292. Otherwise, the link will be the absolute path as completed in the minibuffer
  8293. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  8294. option `org-link-file-path-type'.
  8295. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  8296. the current directory or below.
  8297. With three \\[universal-argument] prefixes, negate the meaning of
  8298. `org-keep-stored-link-after-insertion'.
  8299. If `org-make-link-description-function' is non-nil, this function will be
  8300. called with the link target, and the result will be the default
  8301. link description.
  8302. If the LINK-LOCATION parameter is non-nil, this value will be
  8303. used as the link location instead of reading one interactively.
  8304. If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
  8305. be used as the default description."
  8306. (interactive "P")
  8307. (let* ((wcf (current-window-configuration))
  8308. (region (if (org-region-active-p)
  8309. (buffer-substring (region-beginning) (region-end))))
  8310. (remove (and region (list (region-beginning) (region-end))))
  8311. (desc region)
  8312. tmphist ; byte-compile incorrectly complains about this
  8313. (link link-location)
  8314. entry file all-prefixes)
  8315. (cond
  8316. (link-location) ; specified by arg, just use it.
  8317. ((org-in-regexp org-bracket-link-regexp 1)
  8318. ;; We do have a link at point, and we are going to edit it.
  8319. (setq remove (list (match-beginning 0) (match-end 0)))
  8320. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  8321. (setq link (read-string "Link: "
  8322. (org-link-unescape
  8323. (org-match-string-no-properties 1)))))
  8324. ((or (org-in-regexp org-angle-link-re)
  8325. (org-in-regexp org-plain-link-re))
  8326. ;; Convert to bracket link
  8327. (setq remove (list (match-beginning 0) (match-end 0))
  8328. link (read-string "Link: "
  8329. (org-remove-angle-brackets (match-string 0)))))
  8330. ((member complete-file '((4) (16)))
  8331. ;; Completing read for file names.
  8332. (setq link (org-file-complete-link complete-file)))
  8333. (t
  8334. ;; Read link, with completion for stored links.
  8335. (with-output-to-temp-buffer "*Org Links*"
  8336. (princ "Insert a link.
  8337. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  8338. (when org-stored-links
  8339. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  8340. (princ (mapconcat 'org-link-prettify
  8341. (reverse org-stored-links) "\n"))))
  8342. (let ((cw (selected-window)))
  8343. (select-window (get-buffer-window "*Org Links*" 'visible))
  8344. (with-current-buffer "*Org Links*" (setq truncate-lines t))
  8345. (unless (pos-visible-in-window-p (point-max))
  8346. (org-fit-window-to-buffer))
  8347. (and (window-live-p cw) (select-window cw)))
  8348. ;; Fake a link history, containing the stored links.
  8349. (setq tmphist (append (mapcar 'car org-stored-links)
  8350. org-insert-link-history))
  8351. (setq all-prefixes (append (mapcar 'car org-link-abbrev-alist-local)
  8352. (mapcar 'car org-link-abbrev-alist)
  8353. org-link-types))
  8354. (unwind-protect
  8355. (progn
  8356. (setq link
  8357. (let ((org-completion-use-ido nil)
  8358. (org-completion-use-iswitchb nil))
  8359. (org-completing-read
  8360. "Link: "
  8361. (append
  8362. (mapcar (lambda (x) (list (concat x ":")))
  8363. all-prefixes)
  8364. (mapcar 'car org-stored-links))
  8365. nil nil nil
  8366. 'tmphist
  8367. (car (car org-stored-links)))))
  8368. (if (not (string-match "\\S-" link))
  8369. (error "No link selected"))
  8370. (if (or (member link all-prefixes)
  8371. (and (equal ":" (substring link -1))
  8372. (member (substring link 0 -1) all-prefixes)
  8373. (setq link (substring link 0 -1))))
  8374. (setq link (org-link-try-special-completion link))))
  8375. (set-window-configuration wcf)
  8376. (kill-buffer "*Org Links*"))
  8377. (setq entry (assoc link org-stored-links))
  8378. (or entry (push link org-insert-link-history))
  8379. (setq desc (or desc (nth 1 entry)))))
  8380. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  8381. (not org-keep-stored-link-after-insertion))
  8382. (setq org-stored-links (delq (assoc link org-stored-links)
  8383. org-stored-links)))
  8384. (if (string-match org-plain-link-re link)
  8385. ;; URL-like link, normalize the use of angular brackets.
  8386. (setq link (org-make-link (org-remove-angle-brackets link))))
  8387. ;; Check if we are linking to the current file with a search option
  8388. ;; If yes, simplify the link by using only the search option.
  8389. (when (and buffer-file-name
  8390. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  8391. (let* ((path (match-string 1 link))
  8392. (case-fold-search nil)
  8393. (search (match-string 2 link)))
  8394. (save-match-data
  8395. (if (equal (file-truename buffer-file-name) (file-truename path))
  8396. ;; We are linking to this same file, with a search option
  8397. (setq link search)))))
  8398. ;; Check if we can/should use a relative path. If yes, simplify the link
  8399. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  8400. (let* ((type (match-string 1 link))
  8401. (path (match-string 2 link))
  8402. (origpath path)
  8403. (case-fold-search nil))
  8404. (cond
  8405. ((or (eq org-link-file-path-type 'absolute)
  8406. (equal complete-file '(16)))
  8407. (setq path (abbreviate-file-name (expand-file-name path))))
  8408. ((eq org-link-file-path-type 'noabbrev)
  8409. (setq path (expand-file-name path)))
  8410. ((eq org-link-file-path-type 'relative)
  8411. (setq path (file-relative-name path)))
  8412. (t
  8413. (save-match-data
  8414. (if (string-match (concat "^" (regexp-quote
  8415. (expand-file-name
  8416. (file-name-as-directory
  8417. default-directory))))
  8418. (expand-file-name path))
  8419. ;; We are linking a file with relative path name.
  8420. (setq path (substring (expand-file-name path)
  8421. (match-end 0)))
  8422. (setq path (abbreviate-file-name (expand-file-name path)))))))
  8423. (setq link (concat type path))
  8424. (if (equal desc origpath)
  8425. (setq desc path))))
  8426. (if org-make-link-description-function
  8427. (setq desc (funcall org-make-link-description-function link desc))
  8428. (if default-description (setq desc default-description)
  8429. (setq desc (read-string "Description: " desc))))
  8430. (unless (string-match "\\S-" desc) (setq desc nil))
  8431. (if remove (apply 'delete-region remove))
  8432. (insert (org-make-link-string link desc))))
  8433. (defun org-link-try-special-completion (type)
  8434. "If there is completion support for link type TYPE, offer it."
  8435. (let ((fun (intern (concat "org-" type "-complete-link"))))
  8436. (if (functionp fun)
  8437. (funcall fun)
  8438. (read-string "Link (no completion support): " (concat type ":")))))
  8439. (defun org-file-complete-link (&optional arg)
  8440. "Create a file link using completion."
  8441. (let (file link)
  8442. (setq file (read-file-name "File: "))
  8443. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  8444. (pwd1 (file-name-as-directory (abbreviate-file-name
  8445. (expand-file-name ".")))))
  8446. (cond
  8447. ((equal arg '(16))
  8448. (setq link (org-make-link
  8449. "file:"
  8450. (abbreviate-file-name (expand-file-name file)))))
  8451. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  8452. (setq link (org-make-link "file:" (match-string 1 file))))
  8453. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  8454. (expand-file-name file))
  8455. (setq link (org-make-link
  8456. "file:" (match-string 1 (expand-file-name file)))))
  8457. (t (setq link (org-make-link "file:" file)))))
  8458. link))
  8459. (defun org-completing-read (&rest args)
  8460. "Completing-read with SPACE being a normal character."
  8461. (let ((enable-recursive-minibuffers t)
  8462. (minibuffer-local-completion-map
  8463. (copy-keymap minibuffer-local-completion-map)))
  8464. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  8465. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  8466. (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
  8467. (apply 'org-icompleting-read args)))
  8468. (defun org-completing-read-no-i (&rest args)
  8469. (let (org-completion-use-ido org-completion-use-iswitchb)
  8470. (apply 'org-completing-read args)))
  8471. (defun org-iswitchb-completing-read (prompt choices &rest args)
  8472. "Use iswitch as a completing-read replacement to choose from choices.
  8473. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  8474. from."
  8475. (let* ((iswitchb-use-virtual-buffers nil)
  8476. (iswitchb-make-buflist-hook
  8477. (lambda ()
  8478. (setq iswitchb-temp-buflist choices))))
  8479. (iswitchb-read-buffer prompt)))
  8480. (defun org-icompleting-read (&rest args)
  8481. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  8482. (org-without-partial-completion
  8483. (if (and org-completion-use-ido
  8484. (fboundp 'ido-completing-read)
  8485. (boundp 'ido-mode) ido-mode
  8486. (listp (second args)))
  8487. (let ((ido-enter-matching-directory nil))
  8488. (apply 'ido-completing-read (concat (car args))
  8489. (if (consp (car (nth 1 args)))
  8490. (mapcar 'car (nth 1 args))
  8491. (nth 1 args))
  8492. (cddr args)))
  8493. (if (and org-completion-use-iswitchb
  8494. (boundp 'iswitchb-mode) iswitchb-mode
  8495. (listp (second args)))
  8496. (apply 'org-iswitchb-completing-read (concat (car args))
  8497. (if (consp (car (nth 1 args)))
  8498. (mapcar 'car (nth 1 args))
  8499. (nth 1 args))
  8500. (cddr args))
  8501. (apply 'completing-read args)))))
  8502. (defun org-extract-attributes (s)
  8503. "Extract the attributes cookie from a string and set as text property."
  8504. (let (a attr (start 0) key value)
  8505. (save-match-data
  8506. (when (string-match "{{\\([^}]+\\)}}$" s)
  8507. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  8508. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  8509. (setq key (match-string 1 a) value (match-string 2 a)
  8510. start (match-end 0)
  8511. attr (plist-put attr (intern key) value))))
  8512. (org-add-props s nil 'org-attr attr))
  8513. s))
  8514. (defun org-extract-attributes-from-string (tag)
  8515. (let (key value attr)
  8516. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  8517. (setq key (match-string 1 tag) value (match-string 2 tag)
  8518. tag (replace-match "" t t tag)
  8519. attr (plist-put attr (intern key) value)))
  8520. (cons tag attr)))
  8521. (defun org-attributes-to-string (plist)
  8522. "Format a property list into an HTML attribute list."
  8523. (let ((s "") key value)
  8524. (while plist
  8525. (setq key (pop plist) value (pop plist))
  8526. (and value
  8527. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  8528. s))
  8529. ;;; Opening/following a link
  8530. (defvar org-link-search-failed nil)
  8531. (defvar org-open-link-functions nil
  8532. "Hook for functions finding a plain text link.
  8533. These functions must take a single argument, the link content.
  8534. They will be called for links that look like [[link text][description]]
  8535. when LINK TEXT does not have a protocol like \"http:\" and does not look
  8536. like a filename (e.g. \"./blue.png\").
  8537. These functions will be called *before* Org attempts to resolve the
  8538. link by doing text searches in the current buffer - so if you want a
  8539. link \"[[target]]\" to still find \"<<target>>\", your function should
  8540. handle this as a special case.
  8541. When the function does handle the link, it must return a non-nil value.
  8542. If it decides that it is not responsible for this link, it must return
  8543. nil to indicate that that Org-mode can continue with other options
  8544. like exact and fuzzy text search.")
  8545. (defun org-next-link ()
  8546. "Move forward to the next link.
  8547. If the link is in hidden text, expose it."
  8548. (interactive)
  8549. (when (and org-link-search-failed (eq this-command last-command))
  8550. (goto-char (point-min))
  8551. (message "Link search wrapped back to beginning of buffer"))
  8552. (setq org-link-search-failed nil)
  8553. (let* ((pos (point))
  8554. (ct (org-context))
  8555. (a (assoc :link ct)))
  8556. (if a (goto-char (nth 2 a)))
  8557. (if (re-search-forward org-any-link-re nil t)
  8558. (progn
  8559. (goto-char (match-beginning 0))
  8560. (if (outline-invisible-p) (org-show-context)))
  8561. (goto-char pos)
  8562. (setq org-link-search-failed t)
  8563. (error "No further link found"))))
  8564. (defun org-previous-link ()
  8565. "Move backward to the previous link.
  8566. If the link is in hidden text, expose it."
  8567. (interactive)
  8568. (when (and org-link-search-failed (eq this-command last-command))
  8569. (goto-char (point-max))
  8570. (message "Link search wrapped back to end of buffer"))
  8571. (setq org-link-search-failed nil)
  8572. (let* ((pos (point))
  8573. (ct (org-context))
  8574. (a (assoc :link ct)))
  8575. (if a (goto-char (nth 1 a)))
  8576. (if (re-search-backward org-any-link-re nil t)
  8577. (progn
  8578. (goto-char (match-beginning 0))
  8579. (if (outline-invisible-p) (org-show-context)))
  8580. (goto-char pos)
  8581. (setq org-link-search-failed t)
  8582. (error "No further link found"))))
  8583. (defun org-translate-link (s)
  8584. "Translate a link string if a translation function has been defined."
  8585. (if (and org-link-translation-function
  8586. (fboundp org-link-translation-function)
  8587. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  8588. (progn
  8589. (setq s (funcall org-link-translation-function
  8590. (match-string 1 s) (match-string 2 s)))
  8591. (concat (car s) ":" (cdr s)))
  8592. s))
  8593. (defun org-translate-link-from-planner (type path)
  8594. "Translate a link from Emacs Planner syntax so that Org can follow it.
  8595. This is still an experimental function, your mileage may vary."
  8596. (cond
  8597. ((member type '("http" "https" "news" "ftp"))
  8598. ;; standard Internet links are the same.
  8599. nil)
  8600. ((and (equal type "irc") (string-match "^//" path))
  8601. ;; Planner has two / at the beginning of an irc link, we have 1.
  8602. ;; We should have zero, actually....
  8603. (setq path (substring path 1)))
  8604. ((and (equal type "lisp") (string-match "^/" path))
  8605. ;; Planner has a slash, we do not.
  8606. (setq type "elisp" path (substring path 1)))
  8607. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  8608. ;; A typical message link. Planner has the id after the final slash,
  8609. ;; we separate it with a hash mark
  8610. (setq path (concat (match-string 1 path) "#"
  8611. (org-remove-angle-brackets (match-string 2 path)))))
  8612. )
  8613. (cons type path))
  8614. (defun org-find-file-at-mouse (ev)
  8615. "Open file link or URL at mouse."
  8616. (interactive "e")
  8617. (mouse-set-point ev)
  8618. (org-open-at-point 'in-emacs))
  8619. (defun org-open-at-mouse (ev)
  8620. "Open file link or URL at mouse.
  8621. See the docstring of `org-open-file' for details."
  8622. (interactive "e")
  8623. (mouse-set-point ev)
  8624. (if (eq major-mode 'org-agenda-mode)
  8625. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  8626. (org-open-at-point))
  8627. (defvar org-window-config-before-follow-link nil
  8628. "The window configuration before following a link.
  8629. This is saved in case the need arises to restore it.")
  8630. (defvar org-open-link-marker (make-marker)
  8631. "Marker pointing to the location where `org-open-at-point; was called.")
  8632. ;;;###autoload
  8633. (defun org-open-at-point-global ()
  8634. "Follow a link like Org-mode does.
  8635. This command can be called in any mode to follow a link that has
  8636. Org-mode syntax."
  8637. (interactive)
  8638. (org-run-like-in-org-mode 'org-open-at-point))
  8639. ;;;###autoload
  8640. (defun org-open-link-from-string (s &optional arg reference-buffer)
  8641. "Open a link in the string S, as if it was in Org-mode."
  8642. (interactive "sLink: \nP")
  8643. (let ((reference-buffer (or reference-buffer (current-buffer))))
  8644. (with-temp-buffer
  8645. (let ((org-inhibit-startup (not reference-buffer)))
  8646. (org-mode)
  8647. (insert s)
  8648. (goto-char (point-min))
  8649. (when reference-buffer
  8650. (setq org-link-abbrev-alist-local
  8651. (with-current-buffer reference-buffer
  8652. org-link-abbrev-alist-local)))
  8653. (org-open-at-point arg reference-buffer)))))
  8654. (defvar org-open-at-point-functions nil
  8655. "Hook that is run when following a link at point.
  8656. Functions in this hook must return t if they identify and follow
  8657. a link at point. If they don't find anything interesting at point,
  8658. they must return nil.")
  8659. (defun org-open-at-point (&optional arg reference-buffer)
  8660. "Open link at or after point.
  8661. If there is no link at point, this function will search forward up to
  8662. the end of the current line.
  8663. Normally, files will be opened by an appropriate application. If the
  8664. optional prefix argument ARG is non-nil, Emacs will visit the file.
  8665. With a double prefix argument, try to open outside of Emacs, in the
  8666. application the system uses for this file type."
  8667. (interactive "P")
  8668. ;; if in a code block, then open the block's results
  8669. (unless (call-interactively #'org-babel-open-src-block-result)
  8670. (org-load-modules-maybe)
  8671. (move-marker org-open-link-marker (point))
  8672. (setq org-window-config-before-follow-link (current-window-configuration))
  8673. (org-remove-occur-highlights nil nil t)
  8674. (cond
  8675. ((and (org-at-heading-p)
  8676. (not (org-at-timestamp-p t))
  8677. (not (org-in-regexp
  8678. (concat org-plain-link-re "\\|"
  8679. org-bracket-link-regexp "\\|"
  8680. org-angle-link-re "\\|"
  8681. "[ \t]:[^ \t\n]+:[ \t]*$")))
  8682. (not (get-text-property (point) 'org-linked-text)))
  8683. (or (org-offer-links-in-entry arg)
  8684. (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
  8685. ((run-hook-with-args-until-success 'org-open-at-point-functions))
  8686. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  8687. ((and (or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  8688. (not (org-in-regexp org-bracket-link-regexp)))
  8689. (org-footnote-action))
  8690. (t
  8691. (let (type path link line search (pos (point)))
  8692. (catch 'match
  8693. (save-excursion
  8694. (skip-chars-forward "^]\n\r")
  8695. (when (org-in-regexp org-bracket-link-regexp 1)
  8696. (setq link (org-extract-attributes
  8697. (org-link-unescape (org-match-string-no-properties 1))))
  8698. (while (string-match " *\n *" link)
  8699. (setq link (replace-match " " t t link)))
  8700. (setq link (org-link-expand-abbrev link))
  8701. (cond
  8702. ((or (file-name-absolute-p link)
  8703. (string-match "^\\.\\.?/" link))
  8704. (setq type "file" path link))
  8705. ((string-match org-link-re-with-space3 link)
  8706. (setq type (match-string 1 link) path (match-string 2 link)))
  8707. (t (setq type "thisfile" path link)))
  8708. (throw 'match t)))
  8709. (when (get-text-property (point) 'org-linked-text)
  8710. (setq type "thisfile"
  8711. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  8712. (1+ (point)) (point))
  8713. path (buffer-substring
  8714. (or (previous-single-property-change pos 'org-linked-text)
  8715. (point-min))
  8716. (or (next-single-property-change pos 'org-linked-text)
  8717. (point-max))))
  8718. (throw 'match t))
  8719. (save-excursion
  8720. (when (or (org-in-regexp org-angle-link-re)
  8721. (org-in-regexp org-plain-link-re))
  8722. (setq type (match-string 1)
  8723. path (org-link-unescape (match-string 2)))
  8724. (throw 'match t)))
  8725. (save-excursion
  8726. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@#%:]+\\):[ \t]*$"))
  8727. (setq type "tags"
  8728. path (match-string 1))
  8729. (while (string-match ":" path)
  8730. (setq path (replace-match "+" t t path)))
  8731. (throw 'match t)))
  8732. (when (org-in-regexp "<\\([^><\n]+\\)>")
  8733. (setq type "tree-match"
  8734. path (match-string 1))
  8735. (throw 'match t)))
  8736. (unless path
  8737. (error "No link found"))
  8738. ;; switch back to reference buffer
  8739. ;; needed when if called in a temporary buffer through
  8740. ;; org-open-link-from-string
  8741. (with-current-buffer (or reference-buffer (current-buffer))
  8742. ;; Remove any trailing spaces in path
  8743. (if (string-match " +\\'" path)
  8744. (setq path (replace-match "" t t path)))
  8745. (if (and org-link-translation-function
  8746. (fboundp org-link-translation-function))
  8747. ;; Check if we need to translate the link
  8748. (let ((tmp (funcall org-link-translation-function type path)))
  8749. (setq type (car tmp) path (cdr tmp))))
  8750. (cond
  8751. ((assoc type org-link-protocols)
  8752. (funcall (nth 1 (assoc type org-link-protocols)) path))
  8753. ((equal type "mailto")
  8754. (let ((cmd (car org-link-mailto-program))
  8755. (args (cdr org-link-mailto-program)) args1
  8756. (address path) (subject "") a)
  8757. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  8758. (setq address (match-string 1 path)
  8759. subject (org-link-escape (match-string 2 path))))
  8760. (while args
  8761. (cond
  8762. ((not (stringp (car args))) (push (pop args) args1))
  8763. (t (setq a (pop args))
  8764. (if (string-match "%a" a)
  8765. (setq a (replace-match address t t a)))
  8766. (if (string-match "%s" a)
  8767. (setq a (replace-match subject t t a)))
  8768. (push a args1))))
  8769. (apply cmd (nreverse args1))))
  8770. ((member type '("http" "https" "ftp" "news"))
  8771. (browse-url (concat type ":" (if (org-string-match-p "[[:nonascii:] ]" path)
  8772. (org-link-escape
  8773. path org-link-escape-chars-browser)
  8774. path))))
  8775. ((string= type "doi")
  8776. (browse-url (concat org-doi-server-url (if (org-string-match-p "[[:nonascii:] ]" path)
  8777. (org-link-escape
  8778. path org-link-escape-chars-browser)
  8779. path))))
  8780. ((member type '("message"))
  8781. (browse-url (concat type ":" path)))
  8782. ((string= type "tags")
  8783. (org-tags-view arg path))
  8784. ((string= type "tree-match")
  8785. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  8786. ((string= type "file")
  8787. (if (string-match "::\\([0-9]+\\)\\'" path)
  8788. (setq line (string-to-number (match-string 1 path))
  8789. path (substring path 0 (match-beginning 0)))
  8790. (if (string-match "::\\(.+\\)\\'" path)
  8791. (setq search (match-string 1 path)
  8792. path (substring path 0 (match-beginning 0)))))
  8793. (if (string-match "[*?{]" (file-name-nondirectory path))
  8794. (dired path)
  8795. (org-open-file path arg line search)))
  8796. ((string= type "shell")
  8797. (let ((cmd path))
  8798. (if (or (and (not (string= org-confirm-shell-link-not-regexp ""))
  8799. (string-match org-confirm-shell-link-not-regexp cmd))
  8800. (not org-confirm-shell-link-function)
  8801. (funcall org-confirm-shell-link-function
  8802. (format "Execute \"%s\" in shell? "
  8803. (org-add-props cmd nil
  8804. 'face 'org-warning))))
  8805. (progn
  8806. (message "Executing %s" cmd)
  8807. (shell-command cmd))
  8808. (error "Abort"))))
  8809. ((string= type "elisp")
  8810. (let ((cmd path))
  8811. (if (or (and (not (string= org-confirm-elisp-link-not-regexp ""))
  8812. (string-match org-confirm-elisp-link-not-regexp cmd))
  8813. (not org-confirm-elisp-link-function)
  8814. (funcall org-confirm-elisp-link-function
  8815. (format "Execute \"%s\" as elisp? "
  8816. (org-add-props cmd nil
  8817. 'face 'org-warning))))
  8818. (message "%s => %s" cmd
  8819. (if (equal (string-to-char cmd) ?\()
  8820. (eval (read cmd))
  8821. (call-interactively (read cmd))))
  8822. (error "Abort"))))
  8823. ((and (string= type "thisfile")
  8824. (run-hook-with-args-until-success
  8825. 'org-open-link-functions path)))
  8826. ((string= type "thisfile")
  8827. (if arg
  8828. (switch-to-buffer-other-window
  8829. (org-get-buffer-for-internal-link (current-buffer)))
  8830. (org-mark-ring-push))
  8831. (let ((cmd `(org-link-search
  8832. ,path
  8833. ,(cond ((equal arg '(4)) ''occur)
  8834. ((equal arg '(16)) ''org-occur)
  8835. (t nil))
  8836. ,pos)))
  8837. (condition-case nil (let ((org-link-search-inhibit-query t))
  8838. (eval cmd))
  8839. (error (progn (widen) (eval cmd))))))
  8840. (t
  8841. (browse-url-at-point)))))))
  8842. (move-marker org-open-link-marker nil)
  8843. (run-hook-with-args 'org-follow-link-hook)))
  8844. (defun org-offer-links-in-entry (&optional nth zero)
  8845. "Offer links in the current entry and follow the selected link.
  8846. If there is only one link, follow it immediately as well.
  8847. If NTH is an integer, immediately pick the NTH link found.
  8848. If ZERO is a string, check also this string for a link, and if
  8849. there is one, offer it as link number zero."
  8850. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  8851. "\\(" org-angle-link-re "\\)\\|"
  8852. "\\(" org-plain-link-re "\\)"))
  8853. (cnt ?0)
  8854. (in-emacs (if (integerp nth) nil nth))
  8855. have-zero end links link c)
  8856. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  8857. (push (match-string 0 zero) links)
  8858. (setq cnt (1- cnt) have-zero t))
  8859. (save-excursion
  8860. (org-back-to-heading t)
  8861. (setq end (save-excursion (outline-next-heading) (point)))
  8862. (while (re-search-forward re end t)
  8863. (push (match-string 0) links))
  8864. (setq links (org-uniquify (reverse links))))
  8865. (cond
  8866. ((null links)
  8867. (message "No links"))
  8868. ((equal (length links) 1)
  8869. (setq link (list (car links))))
  8870. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  8871. (setq link (list (nth (if have-zero nth (1- nth)) links))))
  8872. (t ; we have to select a link
  8873. (save-excursion
  8874. (save-window-excursion
  8875. (delete-other-windows)
  8876. (with-output-to-temp-buffer "*Select Link*"
  8877. (mapc (lambda (l)
  8878. (if (not (string-match org-bracket-link-regexp l))
  8879. (princ (format "[%c] %s\n" (incf cnt)
  8880. (org-remove-angle-brackets l)))
  8881. (if (match-end 3)
  8882. (princ (format "[%c] %s (%s)\n" (incf cnt)
  8883. (match-string 3 l) (match-string 1 l)))
  8884. (princ (format "[%c] %s\n" (incf cnt)
  8885. (match-string 1 l))))))
  8886. links))
  8887. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  8888. (message "Select link to open, RET to open all:")
  8889. (setq c (read-char-exclusive))
  8890. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  8891. (when (equal c ?q) (error "Abort"))
  8892. (if (equal c ?\C-m)
  8893. (setq link links)
  8894. (setq nth (- c ?0))
  8895. (if have-zero (setq nth (1+ nth)))
  8896. (unless (and (integerp nth) (>= (length links) nth))
  8897. (error "Invalid link selection"))
  8898. (setq link (list (nth (1- nth) links))))))
  8899. (if link
  8900. (let ((buf (current-buffer)))
  8901. (dolist (l link)
  8902. (org-open-link-from-string l in-emacs buf))
  8903. t)
  8904. nil)))
  8905. ;; Add special file links that specify the way of opening
  8906. (org-add-link-type "file+sys" 'org-open-file-with-system)
  8907. (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
  8908. (defun org-open-file-with-system (path)
  8909. "Open file at PATH using the system way of opening it."
  8910. (org-open-file path 'system))
  8911. (defun org-open-file-with-emacs (path)
  8912. "Open file at PATH in Emacs."
  8913. (org-open-file path 'emacs))
  8914. (defun org-remove-file-link-modifiers ()
  8915. "Remove the file link modifiers in `file+sys:' and `file+emacs:' links."
  8916. (goto-char (point-min))
  8917. (while (re-search-forward "\\<file\\+\\(sys\\|emacs\\):" nil t)
  8918. (org-if-unprotected
  8919. (replace-match "file:" t t))))
  8920. (eval-after-load "org-exp"
  8921. '(add-hook 'org-export-preprocess-before-normalizing-links-hook
  8922. 'org-remove-file-link-modifiers))
  8923. ;;;; Time estimates
  8924. (defun org-get-effort (&optional pom)
  8925. "Get the effort estimate for the current entry."
  8926. (org-entry-get pom org-effort-property))
  8927. ;;; File search
  8928. (defvar org-create-file-search-functions nil
  8929. "List of functions to construct the right search string for a file link.
  8930. These functions are called in turn with point at the location to
  8931. which the link should point.
  8932. A function in the hook should first test if it would like to
  8933. handle this file type, for example by checking the `major-mode'
  8934. or the file extension. If it decides not to handle this file, it
  8935. should just return nil to give other functions a chance. If it
  8936. does handle the file, it must return the search string to be used
  8937. when following the link. The search string will be part of the
  8938. file link, given after a double colon, and `org-open-at-point'
  8939. will automatically search for it. If special measures must be
  8940. taken to make the search successful, another function should be
  8941. added to the companion hook `org-execute-file-search-functions',
  8942. which see.
  8943. A function in this hook may also use `setq' to set the variable
  8944. `description' to provide a suggestion for the descriptive text to
  8945. be used for this link when it gets inserted into an Org-mode
  8946. buffer with \\[org-insert-link].")
  8947. (defvar org-execute-file-search-functions nil
  8948. "List of functions to execute a file search triggered by a link.
  8949. Functions added to this hook must accept a single argument, the
  8950. search string that was part of the file link, the part after the
  8951. double colon. The function must first check if it would like to
  8952. handle this search, for example by checking the `major-mode' or
  8953. the file extension. If it decides not to handle this search, it
  8954. should just return nil to give other functions a chance. If it
  8955. does handle the search, it must return a non-nil value to keep
  8956. other functions from trying.
  8957. Each function can access the current prefix argument through the
  8958. variable `current-prefix-argument'. Note that a single prefix is
  8959. used to force opening a link in Emacs, so it may be good to only
  8960. use a numeric or double prefix to guide the search function.
  8961. In case this is needed, a function in this hook can also restore
  8962. the window configuration before `org-open-at-point' was called using:
  8963. (set-window-configuration org-window-config-before-follow-link)")
  8964. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  8965. (defun org-link-search (s &optional type avoid-pos stealth)
  8966. "Search for a link search option.
  8967. If S is surrounded by forward slashes, it is interpreted as a
  8968. regular expression. In org-mode files, this will create an `org-occur'
  8969. sparse tree. In ordinary files, `occur' will be used to list matches.
  8970. If the current buffer is in `dired-mode', grep will be used to search
  8971. in all files. If AVOID-POS is given, ignore matches near that position.
  8972. When optional argument STEALTH is non-nil, do not modify
  8973. visibility around point, thus ignoring
  8974. `org-show-hierarchy-above', `org-show-following-heading' and
  8975. `org-show-siblings' variables."
  8976. (let ((case-fold-search t)
  8977. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  8978. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  8979. (append '(("") (" ") ("\t") ("\n"))
  8980. org-emphasis-alist)
  8981. "\\|") "\\)"))
  8982. (pos (point))
  8983. (pre nil) (post nil)
  8984. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  8985. (cond
  8986. ;; First check if there are any special search functions
  8987. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  8988. ;; Now try the builtin stuff
  8989. ((and (equal (string-to-char s0) ?#)
  8990. (> (length s0) 1)
  8991. (save-excursion
  8992. (goto-char (point-min))
  8993. (and
  8994. (re-search-forward
  8995. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  8996. (setq type 'dedicated
  8997. pos (match-beginning 0))))
  8998. ;; There is an exact target for this
  8999. (goto-char pos)
  9000. (org-back-to-heading t)))
  9001. ((save-excursion
  9002. (goto-char (point-min))
  9003. (and
  9004. (re-search-forward
  9005. (concat "<<" (regexp-quote s0) ">>") nil t)
  9006. (setq type 'dedicated
  9007. pos (match-beginning 0))))
  9008. ;; There is an exact target for this
  9009. (goto-char pos))
  9010. ((save-excursion
  9011. (goto-char (point-min))
  9012. (and
  9013. (re-search-forward
  9014. (format "^[ \t]*#\\+TARGET: %s" (regexp-quote s0)) nil t)
  9015. (setq type 'dedicated pos (match-beginning 0))))
  9016. ;; Found an invisible target.
  9017. (goto-char pos))
  9018. ((save-excursion
  9019. (goto-char (point-min))
  9020. (and
  9021. (re-search-forward
  9022. (format "^[ \t]*#\\+NAME: %s" (regexp-quote s0)) nil t)
  9023. (setq type 'dedicated pos (match-beginning 0))))
  9024. ;; Found an element with a matching #+name affiliated keyword.
  9025. (goto-char pos))
  9026. ((and (string-match "^(\\(.*\\))$" s0)
  9027. (save-excursion
  9028. (goto-char (point-min))
  9029. (and
  9030. (re-search-forward
  9031. (concat "[^[]" (regexp-quote
  9032. (format org-coderef-label-format
  9033. (match-string 1 s0))))
  9034. nil t)
  9035. (setq type 'dedicated
  9036. pos (1+ (match-beginning 0))))))
  9037. ;; There is a coderef target for this
  9038. (goto-char pos))
  9039. ((string-match "^/\\(.*\\)/$" s)
  9040. ;; A regular expression
  9041. (cond
  9042. ((derived-mode-p 'org-mode)
  9043. (org-occur (match-string 1 s)))
  9044. ;;((eq major-mode 'dired-mode)
  9045. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  9046. (t (org-do-occur (match-string 1 s)))))
  9047. ((and (derived-mode-p 'org-mode) org-link-search-must-match-exact-headline)
  9048. (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
  9049. (goto-char (point-min))
  9050. (cond
  9051. ((let (case-fold-search)
  9052. (re-search-forward (format org-complex-heading-regexp-format
  9053. (regexp-quote s))
  9054. nil t))
  9055. ;; OK, found a match
  9056. (setq type 'dedicated)
  9057. (goto-char (match-beginning 0)))
  9058. ((and (not org-link-search-inhibit-query)
  9059. (eq org-link-search-must-match-exact-headline 'query-to-create)
  9060. (y-or-n-p "No match - create this as a new heading? "))
  9061. (goto-char (point-max))
  9062. (or (bolp) (newline))
  9063. (insert "* " s "\n")
  9064. (beginning-of-line 0))
  9065. (t
  9066. (goto-char pos)
  9067. (error "No match"))))
  9068. (t
  9069. ;; A normal search string
  9070. (when (equal (string-to-char s) ?*)
  9071. ;; Anchor on headlines, post may include tags.
  9072. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  9073. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
  9074. s (substring s 1)))
  9075. (remove-text-properties
  9076. 0 (length s)
  9077. '(face nil mouse-face nil keymap nil fontified nil) s)
  9078. ;; Make a series of regular expressions to find a match
  9079. (setq words (org-split-string s "[ \n\r\t]+")
  9080. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  9081. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  9082. "\\)" markers)
  9083. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  9084. re2a (concat "[ \t\r\n]" re2a_)
  9085. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  9086. re4 (concat "[^a-zA-Z_]" re4_)
  9087. re1 (concat pre re2 post)
  9088. re3 (concat pre (if pre re4_ re4) post)
  9089. re5 (concat pre ".*" re4)
  9090. re2 (concat pre re2)
  9091. re2a (concat pre (if pre re2a_ re2a))
  9092. re4 (concat pre (if pre re4_ re4))
  9093. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  9094. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  9095. re5 "\\)"
  9096. ))
  9097. (cond
  9098. ((eq type 'org-occur) (org-occur reall))
  9099. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  9100. (t (goto-char (point-min))
  9101. (setq type 'fuzzy)
  9102. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  9103. (org-search-not-self 1 re1 nil t)
  9104. (org-search-not-self 1 re2 nil t)
  9105. (org-search-not-self 1 re2a nil t)
  9106. (org-search-not-self 1 re3 nil t)
  9107. (org-search-not-self 1 re4 nil t)
  9108. (org-search-not-self 1 re5 nil t)
  9109. )
  9110. (goto-char (match-beginning 1))
  9111. (goto-char pos)
  9112. (error "No match"))))))
  9113. (and (derived-mode-p 'org-mode)
  9114. (not stealth)
  9115. (org-show-context 'link-search))
  9116. type))
  9117. (defun org-search-not-self (group &rest args)
  9118. "Execute `re-search-forward', but only accept matches that do not
  9119. enclose the position of `org-open-link-marker'."
  9120. (let ((m org-open-link-marker))
  9121. (catch 'exit
  9122. (while (apply 're-search-forward args)
  9123. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  9124. (goto-char (match-end group))
  9125. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  9126. (> (match-beginning 0) (marker-position m))
  9127. (< (match-end 0) (marker-position m)))
  9128. (save-match-data
  9129. (or (not (org-in-regexp
  9130. org-bracket-link-analytic-regexp 1))
  9131. (not (match-end 4)) ; no description
  9132. (and (<= (match-beginning 4) (point))
  9133. (>= (match-end 4) (point))))))
  9134. (throw 'exit (point))))))))
  9135. (defun org-get-buffer-for-internal-link (buffer)
  9136. "Return a buffer to be used for displaying the link target of internal links."
  9137. (cond
  9138. ((not org-display-internal-link-with-indirect-buffer)
  9139. buffer)
  9140. ((string-match "(Clone)$" (buffer-name buffer))
  9141. (message "Buffer is already a clone, not making another one")
  9142. ;; we also do not modify visibility in this case
  9143. buffer)
  9144. (t ; make a new indirect buffer for displaying the link
  9145. (let* ((bn (buffer-name buffer))
  9146. (ibn (concat bn "(Clone)"))
  9147. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  9148. (with-current-buffer ib (org-overview))
  9149. ib))))
  9150. (defun org-do-occur (regexp &optional cleanup)
  9151. "Call the Emacs command `occur'.
  9152. If CLEANUP is non-nil, remove the printout of the regular expression
  9153. in the *Occur* buffer. This is useful if the regex is long and not useful
  9154. to read."
  9155. (occur regexp)
  9156. (when cleanup
  9157. (let ((cwin (selected-window)) win beg end)
  9158. (when (setq win (get-buffer-window "*Occur*"))
  9159. (select-window win))
  9160. (goto-char (point-min))
  9161. (when (re-search-forward "match[a-z]+" nil t)
  9162. (setq beg (match-end 0))
  9163. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  9164. (setq end (1- (match-beginning 0)))))
  9165. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  9166. (goto-char (point-min))
  9167. (select-window cwin))))
  9168. ;;; The mark ring for links jumps
  9169. (defvar org-mark-ring nil
  9170. "Mark ring for positions before jumps in Org-mode.")
  9171. (defvar org-mark-ring-last-goto nil
  9172. "Last position in the mark ring used to go back.")
  9173. ;; Fill and close the ring
  9174. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  9175. (loop for i from 1 to org-mark-ring-length do
  9176. (push (make-marker) org-mark-ring))
  9177. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  9178. org-mark-ring)
  9179. (defun org-mark-ring-push (&optional pos buffer)
  9180. "Put the current position or POS into the mark ring and rotate it."
  9181. (interactive)
  9182. (setq pos (or pos (point)))
  9183. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  9184. (move-marker (car org-mark-ring)
  9185. (or pos (point))
  9186. (or buffer (current-buffer)))
  9187. (message "%s"
  9188. (substitute-command-keys
  9189. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  9190. (defun org-mark-ring-goto (&optional n)
  9191. "Jump to the previous position in the mark ring.
  9192. With prefix arg N, jump back that many stored positions. When
  9193. called several times in succession, walk through the entire ring.
  9194. Org-mode commands jumping to a different position in the current file,
  9195. or to another Org-mode file, automatically push the old position
  9196. onto the ring."
  9197. (interactive "p")
  9198. (let (p m)
  9199. (if (eq last-command this-command)
  9200. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  9201. (setq p org-mark-ring))
  9202. (setq org-mark-ring-last-goto p)
  9203. (setq m (car p))
  9204. (org-pop-to-buffer-same-window (marker-buffer m))
  9205. (goto-char m)
  9206. (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  9207. (defun org-remove-angle-brackets (s)
  9208. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  9209. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  9210. s)
  9211. (defun org-add-angle-brackets (s)
  9212. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  9213. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  9214. s)
  9215. (defun org-remove-double-quotes (s)
  9216. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  9217. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  9218. s)
  9219. ;;; Following specific links
  9220. (defun org-follow-timestamp-link ()
  9221. (cond
  9222. ((org-at-date-range-p t)
  9223. (let ((org-agenda-start-on-weekday)
  9224. (t1 (match-string 1))
  9225. (t2 (match-string 2)))
  9226. (setq t1 (time-to-days (org-time-string-to-time t1))
  9227. t2 (time-to-days (org-time-string-to-time t2)))
  9228. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  9229. ((org-at-timestamp-p t)
  9230. (org-agenda-list nil (time-to-days (org-time-string-to-time
  9231. (substring (match-string 1) 0 10)))
  9232. 1))
  9233. (t (error "This should not happen"))))
  9234. ;;; Following file links
  9235. (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
  9236. (declare-function mailcap-extension-to-mime "mailcap" (extn))
  9237. (declare-function mailcap-mime-info
  9238. "mailcap" (string &optional request no-decode))
  9239. (defvar org-wait nil)
  9240. (defun org-open-file (path &optional in-emacs line search)
  9241. "Open the file at PATH.
  9242. First, this expands any special file name abbreviations. Then the
  9243. configuration variable `org-file-apps' is checked if it contains an
  9244. entry for this file type, and if yes, the corresponding command is launched.
  9245. If no application is found, Emacs simply visits the file.
  9246. With optional prefix argument IN-EMACS, Emacs will visit the file.
  9247. With a double \\[universal-argument] \\[universal-argument] \
  9248. prefix arg, Org tries to avoid opening in Emacs
  9249. and to use an external application to visit the file.
  9250. Optional LINE specifies a line to go to, optional SEARCH a string
  9251. to search for. If LINE or SEARCH is given, the file will be
  9252. opened in Emacs, unless an entry from org-file-apps that makes
  9253. use of groups in a regexp matches.
  9254. If you want to change the way frames are used when following a
  9255. link, please customize `org-link-frame-setup'.
  9256. If the file does not exist, an error is thrown."
  9257. (let* ((file (if (equal path "")
  9258. buffer-file-name
  9259. (substitute-in-file-name (expand-file-name path))))
  9260. (file-apps (append org-file-apps (org-default-apps)))
  9261. (apps (org-remove-if
  9262. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9263. (apps-dlink (org-remove-if-not
  9264. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9265. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  9266. (dirp (if remp nil (file-directory-p file)))
  9267. (file (if (and dirp org-open-directory-means-index-dot-org)
  9268. (concat (file-name-as-directory file) "index.org")
  9269. file))
  9270. (a-m-a-p (assq 'auto-mode apps))
  9271. (dfile (downcase file))
  9272. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  9273. (link (cond ((and (eq line nil)
  9274. (eq search nil))
  9275. file)
  9276. (line
  9277. (concat file "::" (number-to-string line)))
  9278. (search
  9279. (concat file "::" search))))
  9280. (dlink (downcase link))
  9281. (old-buffer (current-buffer))
  9282. (old-pos (point))
  9283. (old-mode major-mode)
  9284. ext cmd link-match-data)
  9285. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  9286. (setq ext (match-string 1 dfile))
  9287. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  9288. (setq ext (match-string 1 dfile))))
  9289. (cond
  9290. ((member in-emacs '((16) system))
  9291. (setq cmd (cdr (assoc 'system apps))))
  9292. (in-emacs (setq cmd 'emacs))
  9293. (t
  9294. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  9295. (and dirp (cdr (assoc 'directory apps)))
  9296. ; first, try matching against apps-dlink
  9297. ; if we get a match here, store the match data for later
  9298. (let ((match (assoc-default dlink apps-dlink
  9299. 'string-match)))
  9300. (if match
  9301. (progn (setq link-match-data (match-data))
  9302. match)
  9303. (progn (setq in-emacs (or in-emacs line search))
  9304. nil))) ; if we have no match in apps-dlink,
  9305. ; always open the file in emacs if line or search
  9306. ; is given (for backwards compatibility)
  9307. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  9308. 'string-match)
  9309. (cdr (assoc ext apps))
  9310. (cdr (assoc t apps))))))
  9311. (when (eq cmd 'system)
  9312. (setq cmd (cdr (assoc 'system apps))))
  9313. (when (eq cmd 'default)
  9314. (setq cmd (cdr (assoc t apps))))
  9315. (when (eq cmd 'mailcap)
  9316. (require 'mailcap)
  9317. (mailcap-parse-mailcaps)
  9318. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  9319. (command (mailcap-mime-info mime-type)))
  9320. (if (stringp command)
  9321. (setq cmd command)
  9322. (setq cmd 'emacs))))
  9323. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  9324. (not (file-exists-p file))
  9325. (not org-open-non-existing-files))
  9326. (error "No such file: %s" file))
  9327. (cond
  9328. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  9329. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  9330. (while (string-match "['\"]%s['\"]" cmd)
  9331. (setq cmd (replace-match "%s" t t cmd)))
  9332. (while (string-match "%s" cmd)
  9333. (setq cmd (replace-match
  9334. (save-match-data
  9335. (shell-quote-argument
  9336. (convert-standard-filename file)))
  9337. t t cmd)))
  9338. ;; Replace "%1", "%2" etc. in command with group matches from regex
  9339. (save-match-data
  9340. (let ((match-index 1)
  9341. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  9342. (set-match-data link-match-data)
  9343. (while (<= match-index number-of-groups)
  9344. (let ((regex (concat "%" (number-to-string match-index)))
  9345. (replace-with (match-string match-index dlink)))
  9346. (while (string-match regex cmd)
  9347. (setq cmd (replace-match replace-with t t cmd))))
  9348. (setq match-index (+ match-index 1)))))
  9349. (save-window-excursion
  9350. (start-process-shell-command cmd nil cmd)
  9351. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  9352. ))
  9353. ((or (stringp cmd)
  9354. (eq cmd 'emacs))
  9355. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  9356. (widen)
  9357. (if line (org-goto-line line)
  9358. (if search (org-link-search search))))
  9359. ((consp cmd)
  9360. (let ((file (convert-standard-filename file)))
  9361. (save-match-data
  9362. (set-match-data link-match-data)
  9363. (eval cmd))))
  9364. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  9365. (and (derived-mode-p 'org-mode) (eq old-mode 'org-mode)
  9366. (or (not (equal old-buffer (current-buffer)))
  9367. (not (equal old-pos (point))))
  9368. (org-mark-ring-push old-pos old-buffer))))
  9369. (defun org-file-apps-entry-match-against-dlink-p (entry)
  9370. "This function returns non-nil if `entry' uses a regular
  9371. expression which should be matched against the whole link by
  9372. org-open-file.
  9373. It assumes that is the case when the entry uses a regular
  9374. expression which has at least one grouping construct and the
  9375. action is either a lisp form or a command string containing
  9376. '%1', i.e. using at least one subexpression match as a
  9377. parameter."
  9378. (let ((selector (car entry))
  9379. (action (cdr entry)))
  9380. (if (stringp selector)
  9381. (and (> (regexp-opt-depth selector) 0)
  9382. (or (and (stringp action)
  9383. (string-match "%[0-9]" action))
  9384. (consp action)))
  9385. nil)))
  9386. (defun org-default-apps ()
  9387. "Return the default applications for this operating system."
  9388. (cond
  9389. ((eq system-type 'darwin)
  9390. org-file-apps-defaults-macosx)
  9391. ((eq system-type 'windows-nt)
  9392. org-file-apps-defaults-windowsnt)
  9393. (t org-file-apps-defaults-gnu)))
  9394. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  9395. "Convert extensions to regular expressions in the cars of LIST.
  9396. Also, weed out any non-string entries, because the return value is used
  9397. only for regexp matching.
  9398. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  9399. point to the symbol `emacs', indicating that the file should
  9400. be opened in Emacs."
  9401. (append
  9402. (delq nil
  9403. (mapcar (lambda (x)
  9404. (if (not (stringp (car x)))
  9405. nil
  9406. (if (string-match "\\W" (car x))
  9407. x
  9408. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  9409. list))
  9410. (if add-auto-mode
  9411. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  9412. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  9413. (defun org-file-remote-p (file)
  9414. "Test whether FILE specifies a location on a remote system.
  9415. Return non-nil if the location is indeed remote.
  9416. For example, the filename \"/user@host:/foo\" specifies a location
  9417. on the system \"/user@host:\"."
  9418. (cond ((fboundp 'file-remote-p)
  9419. (file-remote-p file))
  9420. ((fboundp 'tramp-handle-file-remote-p)
  9421. (tramp-handle-file-remote-p file))
  9422. ((and (boundp 'ange-ftp-name-format)
  9423. (string-match (car ange-ftp-name-format) file))
  9424. t)
  9425. (t nil)))
  9426. ;;;; Refiling
  9427. (defun org-get-org-file ()
  9428. "Read a filename, with default directory `org-directory'."
  9429. (let ((default (or org-default-notes-file remember-data-file)))
  9430. (read-file-name (format "File name [%s]: " default)
  9431. (file-name-as-directory org-directory)
  9432. default)))
  9433. (defun org-notes-order-reversed-p ()
  9434. "Check if the current file should receive notes in reversed order."
  9435. (cond
  9436. ((not org-reverse-note-order) nil)
  9437. ((eq t org-reverse-note-order) t)
  9438. ((not (listp org-reverse-note-order)) nil)
  9439. (t (catch 'exit
  9440. (let ((all org-reverse-note-order)
  9441. entry)
  9442. (while (setq entry (pop all))
  9443. (if (string-match (car entry) buffer-file-name)
  9444. (throw 'exit (cdr entry))))
  9445. nil)))))
  9446. (defvar org-refile-target-table nil
  9447. "The list of refile targets, created by `org-refile'.")
  9448. (defvar org-agenda-new-buffers nil
  9449. "Buffers created to visit agenda files.")
  9450. (defvar org-refile-cache nil
  9451. "Cache for refile targets.")
  9452. (defvar org-refile-markers nil
  9453. "All the markers used for caching refile locations.")
  9454. (defun org-refile-marker (pos)
  9455. "Get a new refile marker, but only if caching is in use."
  9456. (if (not org-refile-use-cache)
  9457. pos
  9458. (let ((m (make-marker)))
  9459. (move-marker m pos)
  9460. (push m org-refile-markers)
  9461. m)))
  9462. (defun org-refile-cache-clear ()
  9463. "Clear the refile cache and disable all the markers."
  9464. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  9465. (setq org-refile-markers nil)
  9466. (setq org-refile-cache nil)
  9467. (message "Refile cache has been cleared"))
  9468. (defun org-refile-cache-check-set (set)
  9469. "Check if all the markers in the cache still have live buffers."
  9470. (let (marker)
  9471. (catch 'exit
  9472. (while (and set (setq marker (nth 3 (pop set))))
  9473. ;; if org-refile-use-outline-path is 'file, marker may be nil
  9474. (when (and marker (null (marker-buffer marker)))
  9475. (message "not found") (sit-for 3)
  9476. (throw 'exit nil)))
  9477. t)))
  9478. (defun org-refile-cache-put (set &rest identifiers)
  9479. "Push the refile targets SET into the cache, under IDENTIFIERS."
  9480. (let* ((key (sha1 (prin1-to-string identifiers)))
  9481. (entry (assoc key org-refile-cache)))
  9482. (if entry
  9483. (setcdr entry set)
  9484. (push (cons key set) org-refile-cache))))
  9485. (defun org-refile-cache-get (&rest identifiers)
  9486. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  9487. (cond
  9488. ((not org-refile-cache) nil)
  9489. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  9490. (t
  9491. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  9492. org-refile-cache))))
  9493. (and set (org-refile-cache-check-set set) set)))))
  9494. (defun org-refile-get-targets (&optional default-buffer excluded-entries)
  9495. "Produce a table with refile targets."
  9496. (let ((case-fold-search nil)
  9497. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  9498. (entries (or org-refile-targets '((nil . (:level . 1)))))
  9499. targets tgs txt re files f desc descre fast-path-p level pos0)
  9500. (message "Getting targets...")
  9501. (with-current-buffer (or default-buffer (current-buffer))
  9502. (while (setq entry (pop entries))
  9503. (setq files (car entry) desc (cdr entry))
  9504. (setq fast-path-p nil)
  9505. (cond
  9506. ((null files) (setq files (list (current-buffer))))
  9507. ((eq files 'org-agenda-files)
  9508. (setq files (org-agenda-files 'unrestricted)))
  9509. ((and (symbolp files) (fboundp files))
  9510. (setq files (funcall files)))
  9511. ((and (symbolp files) (boundp files))
  9512. (setq files (symbol-value files))))
  9513. (if (stringp files) (setq files (list files)))
  9514. (cond
  9515. ((eq (car desc) :tag)
  9516. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  9517. ((eq (car desc) :todo)
  9518. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  9519. ((eq (car desc) :regexp)
  9520. (setq descre (cdr desc)))
  9521. ((eq (car desc) :level)
  9522. (setq descre (concat "^\\*\\{" (number-to-string
  9523. (if org-odd-levels-only
  9524. (1- (* 2 (cdr desc)))
  9525. (cdr desc)))
  9526. "\\}[ \t]")))
  9527. ((eq (car desc) :maxlevel)
  9528. (setq fast-path-p t)
  9529. (setq descre (concat "^\\*\\{1," (number-to-string
  9530. (if org-odd-levels-only
  9531. (1- (* 2 (cdr desc)))
  9532. (cdr desc)))
  9533. "\\}[ \t]")))
  9534. (t (error "Bad refiling target description %s" desc)))
  9535. (while (setq f (pop files))
  9536. (with-current-buffer
  9537. (if (bufferp f) f (org-get-agenda-file-buffer f))
  9538. (or
  9539. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  9540. (progn
  9541. (if (bufferp f) (setq f (buffer-file-name
  9542. (buffer-base-buffer f))))
  9543. (setq f (and f (expand-file-name f)))
  9544. (if (eq org-refile-use-outline-path 'file)
  9545. (push (list (file-name-nondirectory f) f nil nil) tgs))
  9546. (save-excursion
  9547. (save-restriction
  9548. (widen)
  9549. (goto-char (point-min))
  9550. (while (re-search-forward descre nil t)
  9551. (goto-char (setq pos0 (point-at-bol)))
  9552. (catch 'next
  9553. (when org-refile-target-verify-function
  9554. (save-match-data
  9555. (or (funcall org-refile-target-verify-function)
  9556. (throw 'next t))))
  9557. (when (and (looking-at org-complex-heading-regexp)
  9558. (not (member (match-string 4) excluded-entries))
  9559. (match-string 4))
  9560. (setq level (org-reduced-level
  9561. (- (match-end 1) (match-beginning 1)))
  9562. txt (org-link-display-format (match-string 4))
  9563. txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
  9564. re (format org-complex-heading-regexp-format
  9565. (regexp-quote (match-string 4))))
  9566. (when org-refile-use-outline-path
  9567. (setq txt (mapconcat
  9568. 'org-protect-slash
  9569. (append
  9570. (if (eq org-refile-use-outline-path
  9571. 'file)
  9572. (list (file-name-nondirectory
  9573. (buffer-file-name
  9574. (buffer-base-buffer))))
  9575. (if (eq org-refile-use-outline-path
  9576. 'full-file-path)
  9577. (list (buffer-file-name
  9578. (buffer-base-buffer)))))
  9579. (org-get-outline-path fast-path-p
  9580. level txt)
  9581. (list txt))
  9582. "/")))
  9583. (push (list txt f re (org-refile-marker (point)))
  9584. tgs)))
  9585. (when (= (point) pos0)
  9586. ;; verification function has not moved point
  9587. (goto-char (point-at-eol))))))))
  9588. (when org-refile-use-cache
  9589. (org-refile-cache-put tgs (buffer-file-name) descre))
  9590. (setq targets (append tgs targets))
  9591. ))))
  9592. (message "Getting targets...done")
  9593. (nreverse targets)))
  9594. (defun org-protect-slash (s)
  9595. (while (string-match "/" s)
  9596. (setq s (replace-match "\\" t t s)))
  9597. s)
  9598. (defvar org-olpa (make-vector 20 nil))
  9599. (defun org-get-outline-path (&optional fastp level heading)
  9600. "Return the outline path to the current entry, as a list.
  9601. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  9602. routine which makes outline path derivations for an entire file,
  9603. avoiding backtracing. Refile target collection makes use of that."
  9604. (if fastp
  9605. (progn
  9606. (if (> level 19)
  9607. (error "Outline path failure, more than 19 levels"))
  9608. (loop for i from level upto 19 do
  9609. (aset org-olpa i nil))
  9610. (prog1
  9611. (delq nil (append org-olpa nil))
  9612. (aset org-olpa level heading)))
  9613. (let (rtn case-fold-search)
  9614. (save-excursion
  9615. (save-restriction
  9616. (widen)
  9617. (while (org-up-heading-safe)
  9618. (when (looking-at org-complex-heading-regexp)
  9619. (push (org-match-string-no-properties 4) rtn)))
  9620. rtn)))))
  9621. (defun org-format-outline-path (path &optional width prefix)
  9622. "Format the outline path PATH for display.
  9623. Width is the maximum number of characters that is available.
  9624. Prefix is a prefix to be included in the returned string,
  9625. such as the file name."
  9626. (setq width (or width 79))
  9627. (if prefix (setq width (- width (length prefix))))
  9628. (if (not path)
  9629. (or prefix "")
  9630. (let* ((nsteps (length path))
  9631. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  9632. (maxwidth (if (<= total-width width)
  9633. 10000 ;; everything fits
  9634. ;; we need to shorten the level headings
  9635. (/ (- width nsteps) nsteps)))
  9636. (org-odd-levels-only nil)
  9637. (n 0)
  9638. (total (1+ (length prefix))))
  9639. (setq maxwidth (max maxwidth 10))
  9640. (concat prefix
  9641. (mapconcat
  9642. (lambda (h)
  9643. (setq n (1+ n))
  9644. (if (and (= n nsteps) (< maxwidth 10000))
  9645. (setq maxwidth (- total-width total)))
  9646. (if (< (length h) maxwidth)
  9647. (progn (setq total (+ total (length h) 1)) h)
  9648. (setq h (substring h 0 (- maxwidth 2))
  9649. total (+ total maxwidth 1))
  9650. (if (string-match "[ \t]+\\'" h)
  9651. (setq h (substring h 0 (match-beginning 0))))
  9652. (setq h (concat h "..")))
  9653. (org-add-props h nil 'face
  9654. (nth (% (1- n) org-n-level-faces)
  9655. org-level-faces))
  9656. h)
  9657. path "/")))))
  9658. (defun org-display-outline-path (&optional file current)
  9659. "Display the current outline path in the echo area."
  9660. (interactive "P")
  9661. (let* ((bfn (buffer-file-name (buffer-base-buffer)))
  9662. (case-fold-search nil)
  9663. (path (and (derived-mode-p 'org-mode) (org-get-outline-path))))
  9664. (if current (setq path (append path
  9665. (save-excursion
  9666. (org-back-to-heading t)
  9667. (if (looking-at org-complex-heading-regexp)
  9668. (list (match-string 4)))))))
  9669. (message "%s"
  9670. (org-format-outline-path
  9671. path
  9672. (1- (frame-width))
  9673. (and file bfn (concat (file-name-nondirectory bfn) "/"))))))
  9674. (defvar org-refile-history nil
  9675. "History for refiling operations.")
  9676. (defvar org-after-refile-insert-hook nil
  9677. "Hook run after `org-refile' has inserted its stuff at the new location.
  9678. Note that this is still *before* the stuff will be removed from
  9679. the *old* location.")
  9680. (defvar org-capture-last-stored-marker)
  9681. (defun org-refile (&optional goto default-buffer rfloc)
  9682. "Move the entry or entries at point to another heading.
  9683. The list of target headings is compiled using the information in
  9684. `org-refile-targets', which see.
  9685. At the target location, the entry is filed as a subitem of the target
  9686. heading. Depending on `org-reverse-note-order', the new subitem will
  9687. either be the first or the last subitem.
  9688. If there is an active region, all entries in that region will be moved.
  9689. However, the region must fulfill the requirement that the first heading
  9690. is the first one sets the top-level of the moved text - at most siblings
  9691. below it are allowed.
  9692. With prefix arg GOTO, the command will only visit the target location
  9693. and not actually move anything.
  9694. With a double prefix arg \\[universal-argument] \\[universal-argument], \
  9695. go to the location where the last refiling operation has put the subtree.
  9696. With a prefix argument of `2', refile to the running clock.
  9697. RFLOC can be a refile location obtained in a different way.
  9698. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  9699. If you are using target caching (see `org-refile-use-cache'),
  9700. you have to clear the target cache in order to find new targets.
  9701. This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
  9702. prefix argument (`C-u C-u C-u C-c C-w')."
  9703. (interactive "P")
  9704. (if (member goto '(0 (64)))
  9705. (org-refile-cache-clear)
  9706. (let* ((cbuf (current-buffer))
  9707. (regionp (org-region-active-p))
  9708. (region-start (and regionp (region-beginning)))
  9709. (region-end (and regionp (region-end)))
  9710. (region-length (and regionp (- region-end region-start)))
  9711. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  9712. pos it nbuf file re level reversed)
  9713. (setq last-command nil)
  9714. (when regionp
  9715. (goto-char region-start)
  9716. (or (bolp) (goto-char (point-at-bol)))
  9717. (setq region-start (point))
  9718. (unless (or (org-kill-is-subtree-p
  9719. (buffer-substring region-start region-end))
  9720. (prog1 org-refile-active-region-within-subtree
  9721. (org-toggle-heading)))
  9722. (error "The region is not a (sequence of) subtree(s)")))
  9723. (if (equal goto '(16))
  9724. (org-refile-goto-last-stored)
  9725. (when (or
  9726. (and (equal goto 2)
  9727. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  9728. (prog1
  9729. (setq it (list (or org-clock-heading "running clock")
  9730. (buffer-file-name
  9731. (marker-buffer org-clock-hd-marker))
  9732. ""
  9733. (marker-position org-clock-hd-marker)))
  9734. (setq goto nil)))
  9735. (setq it (or rfloc
  9736. (let (heading-text)
  9737. (save-excursion
  9738. (unless goto
  9739. (org-back-to-heading t)
  9740. (setq heading-text
  9741. (nth 4 (org-heading-components))))
  9742. (org-refile-get-location
  9743. (cond (goto "Goto")
  9744. (regionp "Refile region to")
  9745. (t (concat "Refile subtree \""
  9746. heading-text "\" to")))
  9747. default-buffer
  9748. (and (not (equal '(4) goto))
  9749. org-refile-allow-creating-parent-nodes)
  9750. goto))))))
  9751. (setq file (nth 1 it)
  9752. re (nth 2 it)
  9753. pos (nth 3 it))
  9754. (if (and (not goto)
  9755. pos
  9756. (equal (buffer-file-name) file)
  9757. (if regionp
  9758. (and (>= pos region-start)
  9759. (<= pos region-end))
  9760. (and (>= pos (point))
  9761. (< pos (save-excursion
  9762. (org-end-of-subtree t t))))))
  9763. (error "Cannot refile to position inside the tree or region"))
  9764. (setq nbuf (or (find-buffer-visiting file)
  9765. (find-file-noselect file)))
  9766. (if goto
  9767. (progn
  9768. (org-pop-to-buffer-same-window nbuf)
  9769. (goto-char pos)
  9770. (org-show-context 'org-goto))
  9771. (if regionp
  9772. (progn
  9773. (org-kill-new (buffer-substring region-start region-end))
  9774. (org-save-markers-in-region region-start region-end))
  9775. (org-copy-subtree 1 nil t))
  9776. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  9777. (find-file-noselect file)))
  9778. (setq reversed (org-notes-order-reversed-p))
  9779. (save-excursion
  9780. (save-restriction
  9781. (widen)
  9782. (if pos
  9783. (progn
  9784. (goto-char pos)
  9785. (looking-at org-outline-regexp)
  9786. (setq level (org-get-valid-level (funcall outline-level) 1))
  9787. (goto-char
  9788. (if reversed
  9789. (or (outline-next-heading) (point-max))
  9790. (or (save-excursion (org-get-next-sibling))
  9791. (org-end-of-subtree t t)
  9792. (point-max)))))
  9793. (setq level 1)
  9794. (if (not reversed)
  9795. (goto-char (point-max))
  9796. (goto-char (point-min))
  9797. (or (outline-next-heading) (goto-char (point-max)))))
  9798. (if (not (bolp)) (newline))
  9799. (org-paste-subtree level)
  9800. (when org-log-refile
  9801. (org-add-log-setup 'refile nil nil 'findpos
  9802. org-log-refile)
  9803. (unless (eq org-log-refile 'note)
  9804. (save-excursion (org-add-log-note))))
  9805. (and org-auto-align-tags
  9806. (let ((org-loop-over-headlines-in-active-region nil))
  9807. (org-set-tags nil t)))
  9808. (bookmark-set "org-refile-last-stored")
  9809. ;; If we are refiling for capture, make sure that the
  9810. ;; last-capture pointers point here
  9811. (when (org-bound-and-true-p org-refile-for-capture)
  9812. (bookmark-set "org-capture-last-stored-marker")
  9813. (move-marker org-capture-last-stored-marker (point)))
  9814. (if (fboundp 'deactivate-mark) (deactivate-mark))
  9815. (run-hooks 'org-after-refile-insert-hook))))
  9816. (if regionp
  9817. (delete-region (point) (+ (point) region-length))
  9818. (org-cut-subtree))
  9819. (when (featurep 'org-inlinetask)
  9820. (org-inlinetask-remove-END-maybe))
  9821. (setq org-markers-to-move nil)
  9822. (message "Refiled to \"%s\" in file %s" (car it) file)))))))
  9823. (defun org-refile-goto-last-stored ()
  9824. "Go to the location where the last refile was stored."
  9825. (interactive)
  9826. (bookmark-jump "org-refile-last-stored")
  9827. (message "This is the location of the last refile"))
  9828. (defun org-refile-get-location (&optional prompt default-buffer new-nodes
  9829. no-exclude)
  9830. "Prompt the user for a refile location, using PROMPT.
  9831. PROMPT should not be suffixed with a colon and a space, because
  9832. this function appends the default value from
  9833. `org-refile-history' automatically, if that is not empty.
  9834. When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
  9835. this is used for the GOTO interface."
  9836. (let ((org-refile-targets org-refile-targets)
  9837. (org-refile-use-outline-path org-refile-use-outline-path)
  9838. excluded-entries)
  9839. (when (and (derived-mode-p 'org-mode)
  9840. (not org-refile-use-cache)
  9841. (not no-exclude))
  9842. (org-map-tree
  9843. (lambda()
  9844. (setq excluded-entries
  9845. (append excluded-entries (list (org-get-heading t t)))))))
  9846. (setq org-refile-target-table
  9847. (org-refile-get-targets default-buffer excluded-entries)))
  9848. (unless org-refile-target-table
  9849. (error "No refile targets"))
  9850. (let* ((prompt (concat prompt
  9851. (and (car org-refile-history)
  9852. (concat " (default " (car org-refile-history) ")"))
  9853. ": "))
  9854. (cbuf (current-buffer))
  9855. (partial-completion-mode nil)
  9856. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  9857. (cfunc (if (and org-refile-use-outline-path
  9858. org-outline-path-complete-in-steps)
  9859. 'org-olpath-completing-read
  9860. 'org-icompleting-read))
  9861. (extra (if org-refile-use-outline-path "/" ""))
  9862. (filename (and cfn (expand-file-name cfn)))
  9863. (tbl (mapcar
  9864. (lambda (x)
  9865. (if (and (not (member org-refile-use-outline-path
  9866. '(file full-file-path)))
  9867. (not (equal filename (nth 1 x))))
  9868. (cons (concat (car x) extra " ("
  9869. (file-name-nondirectory (nth 1 x)) ")")
  9870. (cdr x))
  9871. (cons (concat (car x) extra) (cdr x))))
  9872. org-refile-target-table))
  9873. (completion-ignore-case t)
  9874. pa answ parent-target child parent old-hist)
  9875. (setq old-hist org-refile-history)
  9876. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  9877. nil 'org-refile-history (car org-refile-history)))
  9878. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  9879. (org-refile-check-position pa)
  9880. (if pa
  9881. (progn
  9882. (when (or (not org-refile-history)
  9883. (not (eq old-hist org-refile-history))
  9884. (not (equal (car pa) (car org-refile-history))))
  9885. (setq org-refile-history
  9886. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  9887. org-refile-history
  9888. (cdr org-refile-history))))
  9889. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  9890. (pop org-refile-history)))
  9891. pa)
  9892. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  9893. (progn
  9894. (setq parent (match-string 1 answ)
  9895. child (match-string 2 answ))
  9896. (setq parent-target (or (assoc parent tbl)
  9897. (assoc (concat parent "/") tbl)))
  9898. (when (and parent-target
  9899. (or (eq new-nodes t)
  9900. (and (eq new-nodes 'confirm)
  9901. (y-or-n-p (format "Create new node \"%s\"? "
  9902. child)))))
  9903. (org-refile-new-child parent-target child)))
  9904. (error "Invalid target location")))))
  9905. (declare-function org-string-nw-p "org-macs.el" (s))
  9906. (defun org-refile-check-position (refile-pointer)
  9907. "Check if the refile pointer matches the readline to which it points."
  9908. (let* ((file (nth 1 refile-pointer))
  9909. (re (nth 2 refile-pointer))
  9910. (pos (nth 3 refile-pointer))
  9911. buffer)
  9912. (when (org-string-nw-p re)
  9913. (setq buffer (if (markerp pos)
  9914. (marker-buffer pos)
  9915. (or (find-buffer-visiting file)
  9916. (find-file-noselect file))))
  9917. (with-current-buffer buffer
  9918. (save-excursion
  9919. (save-restriction
  9920. (widen)
  9921. (goto-char pos)
  9922. (beginning-of-line 1)
  9923. (unless (org-looking-at-p re)
  9924. (error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling"))))))))
  9925. (defun org-refile-new-child (parent-target child)
  9926. "Use refile target PARENT-TARGET to add new CHILD below it."
  9927. (unless parent-target
  9928. (error "Cannot find parent for new node"))
  9929. (let ((file (nth 1 parent-target))
  9930. (pos (nth 3 parent-target))
  9931. level)
  9932. (with-current-buffer (or (find-buffer-visiting file)
  9933. (find-file-noselect file))
  9934. (save-excursion
  9935. (save-restriction
  9936. (widen)
  9937. (if pos
  9938. (goto-char pos)
  9939. (goto-char (point-max))
  9940. (if (not (bolp)) (newline)))
  9941. (when (looking-at org-outline-regexp)
  9942. (setq level (funcall outline-level))
  9943. (org-end-of-subtree t t))
  9944. (org-back-over-empty-lines)
  9945. (insert "\n" (make-string
  9946. (if pos (org-get-valid-level level 1) 1) ?*)
  9947. " " child "\n")
  9948. (beginning-of-line 0)
  9949. (list (concat (car parent-target) "/" child) file "" (point)))))))
  9950. (defun org-olpath-completing-read (prompt collection &rest args)
  9951. "Read an outline path like a file name."
  9952. (let ((thetable collection)
  9953. (org-completion-use-ido nil) ; does not work with ido.
  9954. (org-completion-use-iswitchb nil)) ; or iswitchb
  9955. (apply
  9956. 'org-icompleting-read prompt
  9957. (lambda (string predicate &optional flag)
  9958. (let (rtn r f (l (length string)))
  9959. (cond
  9960. ((eq flag nil)
  9961. ;; try completion
  9962. (try-completion string thetable))
  9963. ((eq flag t)
  9964. ;; all-completions
  9965. (setq rtn (all-completions string thetable predicate))
  9966. (mapcar
  9967. (lambda (x)
  9968. (setq r (substring x l))
  9969. (if (string-match " ([^)]*)$" x)
  9970. (setq f (match-string 0 x))
  9971. (setq f ""))
  9972. (if (string-match "/" r)
  9973. (concat string (substring r 0 (match-end 0)) f)
  9974. x))
  9975. rtn))
  9976. ((eq flag 'lambda)
  9977. ;; exact match?
  9978. (assoc string thetable)))))
  9979. args)))
  9980. ;;;; Dynamic blocks
  9981. (defun org-find-dblock (name)
  9982. "Find the first dynamic block with name NAME in the buffer.
  9983. If not found, stay at current position and return nil."
  9984. (let (pos)
  9985. (save-excursion
  9986. (goto-char (point-min))
  9987. (setq pos (and (re-search-forward (concat "^[ \t]*#\\+BEGIN:[ \t]+" name "\\>")
  9988. nil t)
  9989. (match-beginning 0))))
  9990. (if pos (goto-char pos))
  9991. pos))
  9992. (defconst org-dblock-start-re
  9993. "^[ \t]*#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  9994. "Matches the start line of a dynamic block, with parameters.")
  9995. (defconst org-dblock-end-re "^[ \t]*#\\+END\\([: \t\r\n]\\|$\\)"
  9996. "Matches the end of a dynamic block.")
  9997. (defun org-create-dblock (plist)
  9998. "Create a dynamic block section, with parameters taken from PLIST.
  9999. PLIST must contain a :name entry which is used as name of the block."
  10000. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  10001. (end-of-line 1)
  10002. (newline))
  10003. (let ((col (current-column))
  10004. (name (plist-get plist :name)))
  10005. (insert "#+BEGIN: " name)
  10006. (while plist
  10007. (if (eq (car plist) :name)
  10008. (setq plist (cddr plist))
  10009. (insert " " (prin1-to-string (pop plist)))))
  10010. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  10011. (beginning-of-line -2)))
  10012. (defun org-prepare-dblock ()
  10013. "Prepare dynamic block for refresh.
  10014. This empties the block, puts the cursor at the insert position and returns
  10015. the property list including an extra property :name with the block name."
  10016. (unless (looking-at org-dblock-start-re)
  10017. (error "Not at a dynamic block"))
  10018. (let* ((begdel (1+ (match-end 0)))
  10019. (name (org-no-properties (match-string 1)))
  10020. (params (append (list :name name)
  10021. (read (concat "(" (match-string 3) ")")))))
  10022. (save-excursion
  10023. (beginning-of-line 1)
  10024. (skip-chars-forward " \t")
  10025. (setq params (plist-put params :indentation-column (current-column))))
  10026. (unless (re-search-forward org-dblock-end-re nil t)
  10027. (error "Dynamic block not terminated"))
  10028. (setq params
  10029. (append params
  10030. (list :content (buffer-substring
  10031. begdel (match-beginning 0)))))
  10032. (delete-region begdel (match-beginning 0))
  10033. (goto-char begdel)
  10034. (open-line 1)
  10035. params))
  10036. (defun org-map-dblocks (&optional command)
  10037. "Apply COMMAND to all dynamic blocks in the current buffer.
  10038. If COMMAND is not given, use `org-update-dblock'."
  10039. (let ((cmd (or command 'org-update-dblock)))
  10040. (save-excursion
  10041. (goto-char (point-min))
  10042. (while (re-search-forward org-dblock-start-re nil t)
  10043. (goto-char (match-beginning 0))
  10044. (save-excursion
  10045. (condition-case nil
  10046. (funcall cmd)
  10047. (error (message "Error during update of dynamic block"))))
  10048. (unless (re-search-forward org-dblock-end-re nil t)
  10049. (error "Dynamic block not terminated"))))))
  10050. (defun org-dblock-update (&optional arg)
  10051. "User command for updating dynamic blocks.
  10052. Update the dynamic block at point. With prefix ARG, update all dynamic
  10053. blocks in the buffer."
  10054. (interactive "P")
  10055. (if arg
  10056. (org-update-all-dblocks)
  10057. (or (looking-at org-dblock-start-re)
  10058. (org-beginning-of-dblock))
  10059. (org-update-dblock)))
  10060. (defun org-update-dblock ()
  10061. "Update the dynamic block at point.
  10062. This means to empty the block, parse for parameters and then call
  10063. the correct writing function."
  10064. (interactive)
  10065. (save-window-excursion
  10066. (let* ((pos (point))
  10067. (line (org-current-line))
  10068. (params (org-prepare-dblock))
  10069. (name (plist-get params :name))
  10070. (indent (plist-get params :indentation-column))
  10071. (cmd (intern (concat "org-dblock-write:" name))))
  10072. (message "Updating dynamic block `%s' at line %d..." name line)
  10073. (funcall cmd params)
  10074. (message "Updating dynamic block `%s' at line %d...done" name line)
  10075. (goto-char pos)
  10076. (when (and indent (> indent 0))
  10077. (setq indent (make-string indent ?\ ))
  10078. (save-excursion
  10079. (org-beginning-of-dblock)
  10080. (forward-line 1)
  10081. (while (not (looking-at org-dblock-end-re))
  10082. (insert indent)
  10083. (beginning-of-line 2))
  10084. (when (looking-at org-dblock-end-re)
  10085. (and (looking-at "[ \t]+")
  10086. (replace-match ""))
  10087. (insert indent)))))))
  10088. (defun org-beginning-of-dblock ()
  10089. "Find the beginning of the dynamic block at point.
  10090. Error if there is no such block at point."
  10091. (let ((pos (point))
  10092. beg)
  10093. (end-of-line 1)
  10094. (if (and (re-search-backward org-dblock-start-re nil t)
  10095. (setq beg (match-beginning 0))
  10096. (re-search-forward org-dblock-end-re nil t)
  10097. (> (match-end 0) pos))
  10098. (goto-char beg)
  10099. (goto-char pos)
  10100. (error "Not in a dynamic block"))))
  10101. ;;;###autoload
  10102. (defun org-update-all-dblocks ()
  10103. "Update all dynamic blocks in the buffer.
  10104. This function can be used in a hook."
  10105. (interactive)
  10106. (when (derived-mode-p 'org-mode)
  10107. (org-map-dblocks 'org-update-dblock)))
  10108. ;;;; Completion
  10109. (defconst org-additional-option-like-keywords
  10110. '("BEGIN_HTML" "END_HTML" "HTML:" "ATTR_HTML:"
  10111. "BEGIN_DocBook" "END_DocBook" "DocBook:" "ATTR_DocBook:"
  10112. "BEGIN_LaTeX" "END_LaTeX" "LaTeX:" "LATEX_HEADER:"
  10113. "LATEX_CLASS:" "LATEX_CLASS_OPTIONS:" "ATTR_LaTeX:"
  10114. "BEGIN:" "END:"
  10115. "ORGTBL" "TBLFM:" "TBLNAME:"
  10116. "BEGIN_EXAMPLE" "END_EXAMPLE"
  10117. "BEGIN_QUOTE" "END_QUOTE"
  10118. "BEGIN_VERSE" "END_VERSE"
  10119. "BEGIN_CENTER" "END_CENTER"
  10120. "BEGIN_SRC" "END_SRC"
  10121. "BEGIN_RESULT" "END_RESULT"
  10122. "NAME:" "RESULTS:"
  10123. "HEADER:" "HEADERS:"
  10124. "CATEGORY:" "COLUMNS:" "PROPERTY:"
  10125. "CAPTION:" "LABEL:"
  10126. "SETUPFILE:"
  10127. "INCLUDE:"
  10128. "BIND:"
  10129. "MACRO:"))
  10130. (defcustom org-structure-template-alist
  10131. '(
  10132. ("s" "#+BEGIN_SRC ?\n\n#+END_SRC"
  10133. "<src lang=\"?\">\n\n</src>")
  10134. ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE"
  10135. "<example>\n?\n</example>")
  10136. ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE"
  10137. "<quote>\n?\n</quote>")
  10138. ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE"
  10139. "<verse>\n?\n</verse>")
  10140. ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER"
  10141. "<center>\n?\n</center>")
  10142. ("l" "#+BEGIN_LaTeX\n?\n#+END_LaTeX"
  10143. "<literal style=\"latex\">\n?\n</literal>")
  10144. ("L" "#+LaTeX: "
  10145. "<literal style=\"latex\">?</literal>")
  10146. ("h" "#+BEGIN_HTML\n?\n#+END_HTML"
  10147. "<literal style=\"html\">\n?\n</literal>")
  10148. ("H" "#+HTML: "
  10149. "<literal style=\"html\">?</literal>")
  10150. ("a" "#+BEGIN_ASCII\n?\n#+END_ASCII")
  10151. ("A" "#+ASCII: ")
  10152. ("i" "#+INDEX: ?"
  10153. "#+INDEX: ?")
  10154. ("I" "#+INCLUDE %file ?"
  10155. "<include file=%file markup=\"?\">")
  10156. )
  10157. "Structure completion elements.
  10158. This is a list of abbreviation keys and values. The value gets inserted
  10159. if you type `<' followed by the key and then press the completion key,
  10160. usually `M-TAB'. %file will be replaced by a file name after prompting
  10161. for the file using completion. The cursor will be placed at the position
  10162. of the `?` in the template.
  10163. There are two templates for each key, the first uses the original Org syntax,
  10164. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  10165. the default when the /org-mtags.el/ module has been loaded. See also the
  10166. variable `org-mtags-prefer-muse-templates'."
  10167. :group 'org-completion
  10168. :type '(repeat
  10169. (string :tag "Key")
  10170. (string :tag "Template")
  10171. (string :tag "Muse Template")))
  10172. (defun org-try-structure-completion ()
  10173. "Try to complete a structure template before point.
  10174. This looks for strings like \"<e\" on an otherwise empty line and
  10175. expands them."
  10176. (let ((l (buffer-substring (point-at-bol) (point)))
  10177. a)
  10178. (when (and (looking-at "[ \t]*$")
  10179. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  10180. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  10181. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  10182. (match-beginning 1)) a)
  10183. t)))
  10184. (defun org-complete-expand-structure-template (start cell)
  10185. "Expand a structure template."
  10186. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  10187. (rpl (nth (if musep 2 1) cell))
  10188. (ind ""))
  10189. (delete-region start (point))
  10190. (when (string-match "\\`#\\+" rpl)
  10191. (cond
  10192. ((bolp))
  10193. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  10194. (setq ind (buffer-substring (point-at-bol) (point))))
  10195. (t (newline))))
  10196. (setq start (point))
  10197. (if (string-match "%file" rpl)
  10198. (setq rpl (replace-match
  10199. (concat
  10200. "\""
  10201. (save-match-data
  10202. (abbreviate-file-name (read-file-name "Include file: ")))
  10203. "\"")
  10204. t t rpl)))
  10205. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  10206. (concat "\n" ind)))
  10207. (insert rpl)
  10208. (if (re-search-backward "\\?" start t) (delete-char 1))))
  10209. ;;;; TODO, DEADLINE, Comments
  10210. (defun org-toggle-comment ()
  10211. "Change the COMMENT state of an entry."
  10212. (interactive)
  10213. (save-excursion
  10214. (org-back-to-heading)
  10215. (let (case-fold-search)
  10216. (cond
  10217. ((looking-at (format org-heading-keyword-regexp-format
  10218. org-comment-string))
  10219. (goto-char (match-end 1))
  10220. (looking-at (concat " +" org-comment-string))
  10221. (replace-match "" t t)
  10222. (when (eolp) (insert " ")))
  10223. ((looking-at org-outline-regexp)
  10224. (goto-char (match-end 0))
  10225. (insert org-comment-string " "))))))
  10226. (defvar org-last-todo-state-is-todo nil
  10227. "This is non-nil when the last TODO state change led to a TODO state.
  10228. If the last change removed the TODO tag or switched to DONE, then
  10229. this is nil.")
  10230. (defvar org-setting-tags nil) ; dynamically skipped
  10231. (defvar org-todo-setup-filter-hook nil
  10232. "Hook for functions that pre-filter todo specs.
  10233. Each function takes a todo spec and returns either nil or the spec
  10234. transformed into canonical form." )
  10235. (defvar org-todo-get-default-hook nil
  10236. "Hook for functions that get a default item for todo.
  10237. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  10238. nil or a string to be used for the todo mark." )
  10239. (defvar org-agenda-headline-snapshot-before-repeat)
  10240. (defun org-current-effective-time ()
  10241. "Return current time adjusted for `org-extend-today-until' variable"
  10242. (let* ((ct (org-current-time))
  10243. (dct (decode-time ct))
  10244. (ct1
  10245. (if (and org-use-effective-time
  10246. (< (nth 2 dct) org-extend-today-until))
  10247. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct))
  10248. ct)))
  10249. ct1))
  10250. (defun org-todo-yesterday (&optional arg)
  10251. "Like `org-todo' but the time of change will be 23:59 of yesterday."
  10252. (interactive "P")
  10253. (if (eq major-mode 'org-agenda-mode)
  10254. (apply 'org-agenda-todo-yesterday arg)
  10255. (let* ((hour (third (decode-time
  10256. (org-current-time))))
  10257. (org-extend-today-until (1+ hour)))
  10258. (org-todo arg))))
  10259. (defun org-todo (&optional arg)
  10260. "Change the TODO state of an item.
  10261. The state of an item is given by a keyword at the start of the heading,
  10262. like
  10263. *** TODO Write paper
  10264. *** DONE Call mom
  10265. The different keywords are specified in the variable `org-todo-keywords'.
  10266. By default the available states are \"TODO\" and \"DONE\".
  10267. So for this example: when the item starts with TODO, it is changed to DONE.
  10268. When it starts with DONE, the DONE is removed. And when neither TODO nor
  10269. DONE are present, add TODO at the beginning of the heading.
  10270. With \\[universal-argument] prefix arg, use completion to determine the new \
  10271. state.
  10272. With numeric prefix arg, switch to that state.
  10273. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  10274. keywords (nextset).
  10275. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  10276. With a numeric prefix arg of 0, inhibit note taking for the change.
  10277. For calling through lisp, arg is also interpreted in the following way:
  10278. 'none -> empty state
  10279. \"\"(empty string) -> switch to empty state
  10280. 'done -> switch to DONE
  10281. 'nextset -> switch to the next set of keywords
  10282. 'previousset -> switch to the previous set of keywords
  10283. \"WAITING\" -> switch to the specified keyword, but only if it
  10284. really is a member of `org-todo-keywords'."
  10285. (interactive "P")
  10286. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  10287. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  10288. 'region-start-level 'region))
  10289. org-loop-over-headlines-in-active-region)
  10290. (org-map-entries
  10291. `(org-todo ,arg)
  10292. org-loop-over-headlines-in-active-region
  10293. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  10294. (if (equal arg '(16)) (setq arg 'nextset))
  10295. (let ((org-blocker-hook org-blocker-hook)
  10296. (case-fold-search nil))
  10297. (when (equal arg '(64))
  10298. (setq arg nil org-blocker-hook nil))
  10299. (when (and org-blocker-hook
  10300. (or org-inhibit-blocking
  10301. (org-entry-get nil "NOBLOCKING")))
  10302. (setq org-blocker-hook nil))
  10303. (save-excursion
  10304. (catch 'exit
  10305. (org-back-to-heading t)
  10306. (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
  10307. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
  10308. (looking-at "\\(?: *\\|[ \t]*$\\)"))
  10309. (let* ((match-data (match-data))
  10310. (startpos (point-at-bol))
  10311. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  10312. (org-log-done org-log-done)
  10313. (org-log-repeat org-log-repeat)
  10314. (org-todo-log-states org-todo-log-states)
  10315. (org-inhibit-logging
  10316. (if (equal arg 0)
  10317. (progn (setq arg nil) 'note) org-inhibit-logging))
  10318. (this (match-string 1))
  10319. (hl-pos (match-beginning 0))
  10320. (head (org-get-todo-sequence-head this))
  10321. (ass (assoc head org-todo-kwd-alist))
  10322. (interpret (nth 1 ass))
  10323. (done-word (nth 3 ass))
  10324. (final-done-word (nth 4 ass))
  10325. (org-last-state (or this ""))
  10326. (completion-ignore-case t)
  10327. (member (member this org-todo-keywords-1))
  10328. (tail (cdr member))
  10329. (org-state (cond
  10330. ((and org-todo-key-trigger
  10331. (or (and (equal arg '(4))
  10332. (eq org-use-fast-todo-selection 'prefix))
  10333. (and (not arg) org-use-fast-todo-selection
  10334. (not (eq org-use-fast-todo-selection
  10335. 'prefix)))))
  10336. ;; Use fast selection
  10337. (org-fast-todo-selection))
  10338. ((and (equal arg '(4))
  10339. (or (not org-use-fast-todo-selection)
  10340. (not org-todo-key-trigger)))
  10341. ;; Read a state with completion
  10342. (org-icompleting-read
  10343. "State: " (mapcar (lambda(x) (list x))
  10344. org-todo-keywords-1)
  10345. nil t))
  10346. ((eq arg 'right)
  10347. (if this
  10348. (if tail (car tail) nil)
  10349. (car org-todo-keywords-1)))
  10350. ((eq arg 'left)
  10351. (if (equal member org-todo-keywords-1)
  10352. nil
  10353. (if this
  10354. (nth (- (length org-todo-keywords-1)
  10355. (length tail) 2)
  10356. org-todo-keywords-1)
  10357. (org-last org-todo-keywords-1))))
  10358. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  10359. (setq arg nil))) ; hack to fall back to cycling
  10360. (arg
  10361. ;; user or caller requests a specific state
  10362. (cond
  10363. ((equal arg "") nil)
  10364. ((eq arg 'none) nil)
  10365. ((eq arg 'done) (or done-word (car org-done-keywords)))
  10366. ((eq arg 'nextset)
  10367. (or (car (cdr (member head org-todo-heads)))
  10368. (car org-todo-heads)))
  10369. ((eq arg 'previousset)
  10370. (let ((org-todo-heads (reverse org-todo-heads)))
  10371. (or (car (cdr (member head org-todo-heads)))
  10372. (car org-todo-heads))))
  10373. ((car (member arg org-todo-keywords-1)))
  10374. ((stringp arg)
  10375. (error "State `%s' not valid in this file" arg))
  10376. ((nth (1- (prefix-numeric-value arg))
  10377. org-todo-keywords-1))))
  10378. ((null member) (or head (car org-todo-keywords-1)))
  10379. ((equal this final-done-word) nil) ;; -> make empty
  10380. ((null tail) nil) ;; -> first entry
  10381. ((memq interpret '(type priority))
  10382. (if (eq this-command last-command)
  10383. (car tail)
  10384. (if (> (length tail) 0)
  10385. (or done-word (car org-done-keywords))
  10386. nil)))
  10387. (t
  10388. (car tail))))
  10389. (org-state (or
  10390. (run-hook-with-args-until-success
  10391. 'org-todo-get-default-hook org-state org-last-state)
  10392. org-state))
  10393. (next (if org-state (concat " " org-state " ") " "))
  10394. (change-plist (list :type 'todo-state-change :from this :to org-state
  10395. :position startpos))
  10396. dolog now-done-p)
  10397. (when org-blocker-hook
  10398. (setq org-last-todo-state-is-todo
  10399. (not (member this org-done-keywords)))
  10400. (unless (save-excursion
  10401. (save-match-data
  10402. (org-with-wide-buffer
  10403. (run-hook-with-args-until-failure
  10404. 'org-blocker-hook change-plist))))
  10405. (if (org-called-interactively-p 'interactive)
  10406. (error "TODO state change from %s to %s blocked" this org-state)
  10407. ;; fail silently
  10408. (message "TODO state change from %s to %s blocked" this org-state)
  10409. (throw 'exit nil))))
  10410. (store-match-data match-data)
  10411. (replace-match next t t)
  10412. (unless (pos-visible-in-window-p hl-pos)
  10413. (message "TODO state changed to %s" (org-trim next)))
  10414. (unless head
  10415. (setq head (org-get-todo-sequence-head org-state)
  10416. ass (assoc head org-todo-kwd-alist)
  10417. interpret (nth 1 ass)
  10418. done-word (nth 3 ass)
  10419. final-done-word (nth 4 ass)))
  10420. (when (memq arg '(nextset previousset))
  10421. (message "Keyword-Set %d/%d: %s"
  10422. (- (length org-todo-sets) -1
  10423. (length (memq (assoc org-state org-todo-sets) org-todo-sets)))
  10424. (length org-todo-sets)
  10425. (mapconcat 'identity (assoc org-state org-todo-sets) " ")))
  10426. (setq org-last-todo-state-is-todo
  10427. (not (member org-state org-done-keywords)))
  10428. (setq now-done-p (and (member org-state org-done-keywords)
  10429. (not (member this org-done-keywords))))
  10430. (and logging (org-local-logging logging))
  10431. (when (and (or org-todo-log-states org-log-done)
  10432. (not (eq org-inhibit-logging t))
  10433. (not (memq arg '(nextset previousset))))
  10434. ;; we need to look at recording a time and note
  10435. (setq dolog (or (nth 1 (assoc org-state org-todo-log-states))
  10436. (nth 2 (assoc this org-todo-log-states))))
  10437. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  10438. (setq dolog 'time))
  10439. (when (and org-state
  10440. (member org-state org-not-done-keywords)
  10441. (not (member this org-not-done-keywords)))
  10442. ;; This is now a todo state and was not one before
  10443. ;; If there was a CLOSED time stamp, get rid of it.
  10444. (org-add-planning-info nil nil 'closed))
  10445. (when (and now-done-p org-log-done)
  10446. ;; It is now done, and it was not done before
  10447. (org-add-planning-info 'closed (org-current-effective-time))
  10448. (if (and (not dolog) (eq 'note org-log-done))
  10449. (org-add-log-setup 'done org-state this 'findpos 'note)))
  10450. (when (and org-state dolog)
  10451. ;; This is a non-nil state, and we need to log it
  10452. (org-add-log-setup 'state org-state this 'findpos dolog)))
  10453. ;; Fixup tag positioning
  10454. (org-todo-trigger-tag-changes org-state)
  10455. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  10456. (when org-provide-todo-statistics
  10457. (org-update-parent-todo-statistics))
  10458. (run-hooks 'org-after-todo-state-change-hook)
  10459. (if (and arg (not (member org-state org-done-keywords)))
  10460. (setq head (org-get-todo-sequence-head org-state)))
  10461. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  10462. ;; Do we need to trigger a repeat?
  10463. (when now-done-p
  10464. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  10465. ;; This is for the agenda, take a snapshot of the headline.
  10466. (save-match-data
  10467. (setq org-agenda-headline-snapshot-before-repeat
  10468. (org-get-heading))))
  10469. (org-auto-repeat-maybe org-state))
  10470. ;; Fixup cursor location if close to the keyword
  10471. (if (and (outline-on-heading-p)
  10472. (not (bolp))
  10473. (save-excursion (beginning-of-line 1)
  10474. (looking-at org-todo-line-regexp))
  10475. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  10476. (progn
  10477. (goto-char (or (match-end 2) (match-end 1)))
  10478. (and (looking-at " ") (just-one-space))))
  10479. (when org-trigger-hook
  10480. (save-excursion
  10481. (run-hook-with-args 'org-trigger-hook change-plist)))))))))
  10482. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  10483. "Block turning an entry into a TODO, using the hierarchy.
  10484. This checks whether the current task should be blocked from state
  10485. changes. Such blocking occurs when:
  10486. 1. The task has children which are not all in a completed state.
  10487. 2. A task has a parent with the property :ORDERED:, and there
  10488. are siblings prior to the current task with incomplete
  10489. status.
  10490. 3. The parent of the task is blocked because it has siblings that should
  10491. be done first, or is child of a block grandparent TODO entry."
  10492. (if (not org-enforce-todo-dependencies)
  10493. t ; if locally turned off don't block
  10494. (catch 'dont-block
  10495. ;; If this is not a todo state change, or if this entry is already DONE,
  10496. ;; do not block
  10497. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  10498. (member (plist-get change-plist :from)
  10499. (cons 'done org-done-keywords))
  10500. (member (plist-get change-plist :to)
  10501. (cons 'todo org-not-done-keywords))
  10502. (not (plist-get change-plist :to)))
  10503. (throw 'dont-block t))
  10504. ;; If this task has children, and any are undone, it's blocked
  10505. (save-excursion
  10506. (org-back-to-heading t)
  10507. (let ((this-level (funcall outline-level)))
  10508. (outline-next-heading)
  10509. (let ((child-level (funcall outline-level)))
  10510. (while (and (not (eobp))
  10511. (> child-level this-level))
  10512. ;; this todo has children, check whether they are all
  10513. ;; completed
  10514. (if (and (not (org-entry-is-done-p))
  10515. (org-entry-is-todo-p))
  10516. (throw 'dont-block nil))
  10517. (outline-next-heading)
  10518. (setq child-level (funcall outline-level))))))
  10519. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  10520. ;; any previous siblings are undone, it's blocked
  10521. (save-excursion
  10522. (org-back-to-heading t)
  10523. (let* ((pos (point))
  10524. (parent-pos (and (org-up-heading-safe) (point))))
  10525. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  10526. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  10527. (forward-line 1)
  10528. (re-search-forward org-not-done-heading-regexp pos t))
  10529. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  10530. ;; Search further up the hierarchy, to see if an ancestor is blocked
  10531. (while t
  10532. (goto-char parent-pos)
  10533. (if (not (looking-at org-not-done-heading-regexp))
  10534. (throw 'dont-block t)) ; do not block, parent is not a TODO
  10535. (setq pos (point))
  10536. (setq parent-pos (and (org-up-heading-safe) (point)))
  10537. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  10538. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  10539. (forward-line 1)
  10540. (re-search-forward org-not-done-heading-regexp pos t))
  10541. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  10542. (defcustom org-track-ordered-property-with-tag nil
  10543. "Should the ORDERED property also be shown as a tag?
  10544. The ORDERED property decides if an entry should require subtasks to be
  10545. completed in sequence. Since a property is not very visible, setting
  10546. this option means that toggling the ORDERED property with the command
  10547. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  10548. not relevant for the behavior, but it makes things more visible.
  10549. Note that toggling the tag with tags commands will not change the property
  10550. and therefore not influence behavior!
  10551. This can be t, meaning the tag ORDERED should be used, It can also be a
  10552. string to select a different tag for this task."
  10553. :group 'org-todo
  10554. :type '(choice
  10555. (const :tag "No tracking" nil)
  10556. (const :tag "Track with ORDERED tag" t)
  10557. (string :tag "Use other tag")))
  10558. (defun org-toggle-ordered-property ()
  10559. "Toggle the ORDERED property of the current entry.
  10560. For better visibility, you can track the value of this property with a tag.
  10561. See variable `org-track-ordered-property-with-tag'."
  10562. (interactive)
  10563. (let* ((t1 org-track-ordered-property-with-tag)
  10564. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  10565. (save-excursion
  10566. (org-back-to-heading)
  10567. (if (org-entry-get nil "ORDERED")
  10568. (progn
  10569. (org-delete-property "ORDERED")
  10570. (and tag (org-toggle-tag tag 'off))
  10571. (message "Subtasks can be completed in arbitrary order"))
  10572. (org-entry-put nil "ORDERED" "t")
  10573. (and tag (org-toggle-tag tag 'on))
  10574. (message "Subtasks must be completed in sequence")))))
  10575. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  10576. (defun org-block-todo-from-checkboxes (change-plist)
  10577. "Block turning an entry into a TODO, using checkboxes.
  10578. This checks whether the current task should be blocked from state
  10579. changes because there are unchecked boxes in this entry."
  10580. (if (not org-enforce-todo-checkbox-dependencies)
  10581. t ; if locally turned off don't block
  10582. (catch 'dont-block
  10583. ;; If this is not a todo state change, or if this entry is already DONE,
  10584. ;; do not block
  10585. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  10586. (member (plist-get change-plist :from)
  10587. (cons 'done org-done-keywords))
  10588. (member (plist-get change-plist :to)
  10589. (cons 'todo org-not-done-keywords))
  10590. (not (plist-get change-plist :to)))
  10591. (throw 'dont-block t))
  10592. ;; If this task has checkboxes that are not checked, it's blocked
  10593. (save-excursion
  10594. (org-back-to-heading t)
  10595. (let ((beg (point)) end)
  10596. (outline-next-heading)
  10597. (setq end (point))
  10598. (goto-char beg)
  10599. (if (org-list-search-forward
  10600. (concat (org-item-beginning-re)
  10601. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
  10602. "\\[[- ]\\]")
  10603. end t)
  10604. (progn
  10605. (if (boundp 'org-blocked-by-checkboxes)
  10606. (setq org-blocked-by-checkboxes t))
  10607. (throw 'dont-block nil)))))
  10608. t))) ; do not block
  10609. (defun org-entry-blocked-p ()
  10610. "Is the current entry blocked?"
  10611. (if (org-entry-get nil "NOBLOCKING")
  10612. nil ;; Never block this entry
  10613. (not
  10614. (run-hook-with-args-until-failure
  10615. 'org-blocker-hook
  10616. (list :type 'todo-state-change
  10617. :position (point)
  10618. :from 'todo
  10619. :to 'done)))))
  10620. (defun org-update-statistics-cookies (all)
  10621. "Update the statistics cookie, either from TODO or from checkboxes.
  10622. This should be called with the cursor in a line with a statistics cookie."
  10623. (interactive "P")
  10624. (if all
  10625. (progn
  10626. (org-update-checkbox-count 'all)
  10627. (org-map-entries 'org-update-parent-todo-statistics))
  10628. (if (not (org-at-heading-p))
  10629. (org-update-checkbox-count)
  10630. (let ((pos (move-marker (make-marker) (point)))
  10631. end l1 l2)
  10632. (ignore-errors (org-back-to-heading t))
  10633. (if (not (org-at-heading-p))
  10634. (org-update-checkbox-count)
  10635. (setq l1 (org-outline-level))
  10636. (setq end (save-excursion
  10637. (outline-next-heading)
  10638. (if (org-at-heading-p) (setq l2 (org-outline-level)))
  10639. (point)))
  10640. (if (and (save-excursion
  10641. (re-search-forward
  10642. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  10643. (not (save-excursion (re-search-forward
  10644. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  10645. (org-update-checkbox-count)
  10646. (if (and l2 (> l2 l1))
  10647. (progn
  10648. (goto-char end)
  10649. (org-update-parent-todo-statistics))
  10650. (goto-char pos)
  10651. (beginning-of-line 1)
  10652. (while (re-search-forward
  10653. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  10654. (point-at-eol) t)
  10655. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  10656. (goto-char pos)
  10657. (move-marker pos nil)))))
  10658. (defvar org-entry-property-inherited-from) ;; defined below
  10659. (defun org-update-parent-todo-statistics ()
  10660. "Update any statistics cookie in the parent of the current headline.
  10661. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  10662. the entire subtree and this will travel up the hierarchy and update
  10663. statistics everywhere."
  10664. (let* ((prop (save-excursion (org-up-heading-safe)
  10665. (org-entry-get nil "COOKIE_DATA" 'inherit)))
  10666. (recursive (or (not org-hierarchical-todo-statistics)
  10667. (and prop (string-match "\\<recursive\\>" prop))))
  10668. (lim (or (and prop (marker-position org-entry-property-inherited-from))
  10669. 0))
  10670. (first t)
  10671. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  10672. level ltoggle l1 new ndel
  10673. (cnt-all 0) (cnt-done 0) is-percent kwd
  10674. checkbox-beg ov ovs ove cookie-present)
  10675. (catch 'exit
  10676. (save-excursion
  10677. (beginning-of-line 1)
  10678. (setq ltoggle (funcall outline-level))
  10679. ;; Three situations are to consider:
  10680. ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
  10681. ;; to the top-level ancestor on the headline;
  10682. ;; 2. If parent has "recursive" property, repeat up to the
  10683. ;; headline setting that property, taking inheritance into
  10684. ;; account;
  10685. ;; 3. Else, move up to direct parent and proceed only once.
  10686. (while (and (setq level (org-up-heading-safe))
  10687. (or recursive first)
  10688. (>= (point) lim))
  10689. (setq first nil cookie-present nil)
  10690. (unless (and level
  10691. (not (string-match
  10692. "\\<checkbox\\>"
  10693. (downcase (or (org-entry-get nil "COOKIE_DATA")
  10694. "")))))
  10695. (throw 'exit nil))
  10696. (while (re-search-forward box-re (point-at-eol) t)
  10697. (setq cnt-all 0 cnt-done 0 cookie-present t)
  10698. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  10699. (save-match-data
  10700. (unless (outline-next-heading) (throw 'exit nil))
  10701. (while (and (looking-at org-complex-heading-regexp)
  10702. (> (setq l1 (length (match-string 1))) level))
  10703. (setq kwd (and (or recursive (= l1 ltoggle))
  10704. (match-string 2)))
  10705. (if (or (eq org-provide-todo-statistics 'all-headlines)
  10706. (and (listp org-provide-todo-statistics)
  10707. (or (member kwd org-provide-todo-statistics)
  10708. (member kwd org-done-keywords))))
  10709. (setq cnt-all (1+ cnt-all))
  10710. (if (eq org-provide-todo-statistics t)
  10711. (and kwd (setq cnt-all (1+ cnt-all)))))
  10712. (and (member kwd org-done-keywords)
  10713. (setq cnt-done (1+ cnt-done)))
  10714. (outline-next-heading)))
  10715. (setq new
  10716. (if is-percent
  10717. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  10718. (format "[%d/%d]" cnt-done cnt-all))
  10719. ndel (- (match-end 0) checkbox-beg))
  10720. ;; handle overlays when updating cookie from column view
  10721. (when (setq ov (car (overlays-at checkbox-beg)))
  10722. (setq ovs (overlay-start ov) ove (overlay-end ov))
  10723. (delete-overlay ov))
  10724. (goto-char checkbox-beg)
  10725. (insert new)
  10726. (delete-region (point) (+ (point) ndel))
  10727. (when org-auto-align-tags (org-fix-tags-on-the-fly))
  10728. (when ov (move-overlay ov ovs ove)))
  10729. (when cookie-present
  10730. (run-hook-with-args 'org-after-todo-statistics-hook
  10731. cnt-done (- cnt-all cnt-done))))))
  10732. (run-hooks 'org-todo-statistics-hook)))
  10733. (defvar org-after-todo-statistics-hook nil
  10734. "Hook that is called after a TODO statistics cookie has been updated.
  10735. Each function is called with two arguments: the number of not-done entries
  10736. and the number of done entries.
  10737. For example, the following function, when added to this hook, will switch
  10738. an entry to DONE when all children are done, and back to TODO when new
  10739. entries are set to a TODO status. Note that this hook is only called
  10740. when there is a statistics cookie in the headline!
  10741. (defun org-summary-todo (n-done n-not-done)
  10742. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  10743. (let (org-log-done org-log-states) ; turn off logging
  10744. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  10745. ")
  10746. (defvar org-todo-statistics-hook nil
  10747. "Hook that is run whenever Org thinks TODO statistics should be updated.
  10748. This hook runs even if there is no statistics cookie present, in which case
  10749. `org-after-todo-statistics-hook' would not run.")
  10750. (defun org-todo-trigger-tag-changes (state)
  10751. "Apply the changes defined in `org-todo-state-tags-triggers'."
  10752. (let ((l org-todo-state-tags-triggers)
  10753. changes)
  10754. (when (or (not state) (equal state ""))
  10755. (setq changes (append changes (cdr (assoc "" l)))))
  10756. (when (and (stringp state) (> (length state) 0))
  10757. (setq changes (append changes (cdr (assoc state l)))))
  10758. (when (member state org-not-done-keywords)
  10759. (setq changes (append changes (cdr (assoc 'todo l)))))
  10760. (when (member state org-done-keywords)
  10761. (setq changes (append changes (cdr (assoc 'done l)))))
  10762. (dolist (c changes)
  10763. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  10764. (defun org-local-logging (value)
  10765. "Get logging settings from a property VALUE."
  10766. (let* (words w a)
  10767. ;; directly set the variables, they are already local.
  10768. (setq org-log-done nil
  10769. org-log-repeat nil
  10770. org-todo-log-states nil)
  10771. (setq words (org-split-string value))
  10772. (while (setq w (pop words))
  10773. (cond
  10774. ((setq a (assoc w org-startup-options))
  10775. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  10776. (set (nth 1 a) (nth 2 a))))
  10777. ((setq a (org-extract-log-state-settings w))
  10778. (and (member (car a) org-todo-keywords-1)
  10779. (push a org-todo-log-states)))))))
  10780. (defun org-get-todo-sequence-head (kwd)
  10781. "Return the head of the TODO sequence to which KWD belongs.
  10782. If KWD is not set, check if there is a text property remembering the
  10783. right sequence."
  10784. (let (p)
  10785. (cond
  10786. ((not kwd)
  10787. (or (get-text-property (point-at-bol) 'org-todo-head)
  10788. (progn
  10789. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  10790. nil (point-at-eol)))
  10791. (get-text-property p 'org-todo-head))))
  10792. ((not (member kwd org-todo-keywords-1))
  10793. (car org-todo-keywords-1))
  10794. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  10795. (defun org-fast-todo-selection ()
  10796. "Fast TODO keyword selection with single keys.
  10797. Returns the new TODO keyword, or nil if no state change should occur."
  10798. (let* ((fulltable org-todo-key-alist)
  10799. (done-keywords org-done-keywords) ;; needed for the faces.
  10800. (maxlen (apply 'max (mapcar
  10801. (lambda (x)
  10802. (if (stringp (car x)) (string-width (car x)) 0))
  10803. fulltable)))
  10804. (expert nil)
  10805. (fwidth (+ maxlen 3 1 3))
  10806. (ncol (/ (- (window-width) 4) fwidth))
  10807. tg cnt e c tbl
  10808. groups ingroup)
  10809. (save-excursion
  10810. (save-window-excursion
  10811. (if expert
  10812. (set-buffer (get-buffer-create " *Org todo*"))
  10813. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  10814. (erase-buffer)
  10815. (org-set-local 'org-done-keywords done-keywords)
  10816. (setq tbl fulltable cnt 0)
  10817. (while (setq e (pop tbl))
  10818. (cond
  10819. ((equal e '(:startgroup))
  10820. (push '() groups) (setq ingroup t)
  10821. (when (not (= cnt 0))
  10822. (setq cnt 0)
  10823. (insert "\n"))
  10824. (insert "{ "))
  10825. ((equal e '(:endgroup))
  10826. (setq ingroup nil cnt 0)
  10827. (insert "}\n"))
  10828. ((equal e '(:newline))
  10829. (when (not (= cnt 0))
  10830. (setq cnt 0)
  10831. (insert "\n")
  10832. (setq e (car tbl))
  10833. (while (equal (car tbl) '(:newline))
  10834. (insert "\n")
  10835. (setq tbl (cdr tbl)))))
  10836. (t
  10837. (setq tg (car e) c (cdr e))
  10838. (if ingroup (push tg (car groups)))
  10839. (setq tg (org-add-props tg nil 'face
  10840. (org-get-todo-face tg)))
  10841. (if (and (= cnt 0) (not ingroup)) (insert " "))
  10842. (insert "[" c "] " tg (make-string
  10843. (- fwidth 4 (length tg)) ?\ ))
  10844. (when (= (setq cnt (1+ cnt)) ncol)
  10845. (insert "\n")
  10846. (if ingroup (insert " "))
  10847. (setq cnt 0)))))
  10848. (insert "\n")
  10849. (goto-char (point-min))
  10850. (if (not expert) (org-fit-window-to-buffer))
  10851. (message "[a-z..]:Set [SPC]:clear")
  10852. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  10853. (cond
  10854. ((or (= c ?\C-g)
  10855. (and (= c ?q) (not (rassoc c fulltable))))
  10856. (setq quit-flag t))
  10857. ((= c ?\ ) nil)
  10858. ((setq e (rassoc c fulltable) tg (car e))
  10859. tg)
  10860. (t (setq quit-flag t)))))))
  10861. (defun org-entry-is-todo-p ()
  10862. (member (org-get-todo-state) org-not-done-keywords))
  10863. (defun org-entry-is-done-p ()
  10864. (member (org-get-todo-state) org-done-keywords))
  10865. (defun org-get-todo-state ()
  10866. (save-excursion
  10867. (org-back-to-heading t)
  10868. (and (looking-at org-todo-line-regexp)
  10869. (match-end 2)
  10870. (match-string 2))))
  10871. (defun org-at-date-range-p (&optional inactive-ok)
  10872. "Is the cursor inside a date range?"
  10873. (interactive)
  10874. (save-excursion
  10875. (catch 'exit
  10876. (let ((pos (point)))
  10877. (skip-chars-backward "^[<\r\n")
  10878. (skip-chars-backward "<[")
  10879. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  10880. (>= (match-end 0) pos)
  10881. (throw 'exit t))
  10882. (skip-chars-backward "^<[\r\n")
  10883. (skip-chars-backward "<[")
  10884. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  10885. (>= (match-end 0) pos)
  10886. (throw 'exit t)))
  10887. nil)))
  10888. (defun org-get-repeat (&optional tagline)
  10889. "Check if there is a deadline/schedule with repeater in this entry."
  10890. (save-match-data
  10891. (save-excursion
  10892. (org-back-to-heading t)
  10893. (and (re-search-forward (if tagline
  10894. (concat tagline "\\s-*" org-repeat-re)
  10895. org-repeat-re)
  10896. (org-entry-end-position) t)
  10897. (match-string-no-properties 1)))))
  10898. (defvar org-last-changed-timestamp)
  10899. (defvar org-last-inserted-timestamp)
  10900. (defvar org-log-post-message)
  10901. (defvar org-log-note-purpose)
  10902. (defvar org-log-note-how)
  10903. (defvar org-log-note-extra)
  10904. (defun org-auto-repeat-maybe (done-word)
  10905. "Check if the current headline contains a repeated deadline/schedule.
  10906. If yes, set TODO state back to what it was and change the base date
  10907. of repeating deadline/scheduled time stamps to new date.
  10908. This function is run automatically after each state change to a DONE state."
  10909. ;; last-state is dynamically scoped into this function
  10910. (let* ((repeat (org-get-repeat))
  10911. (aa (assoc org-last-state org-todo-kwd-alist))
  10912. (interpret (nth 1 aa))
  10913. (head (nth 2 aa))
  10914. (whata '(("h" . hour) ("d" . day) ("m" . month) ("y" . year)))
  10915. (msg "Entry repeats: ")
  10916. (org-log-done nil)
  10917. (org-todo-log-states nil)
  10918. re type n what ts time to-state)
  10919. (when repeat
  10920. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  10921. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  10922. org-todo-repeat-to-state))
  10923. (unless (and to-state (member to-state org-todo-keywords-1))
  10924. (setq to-state (if (eq interpret 'type) org-last-state head)))
  10925. (org-todo to-state)
  10926. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  10927. (org-entry-put nil "LAST_REPEAT" (format-time-string
  10928. (org-time-stamp-format t t))))
  10929. (when org-log-repeat
  10930. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  10931. (memq 'org-add-log-note post-command-hook))
  10932. ;; OK, we are already setup for some record
  10933. (if (eq org-log-repeat 'note)
  10934. ;; make sure we take a note, not only a time stamp
  10935. (setq org-log-note-how 'note))
  10936. ;; Set up for taking a record
  10937. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  10938. org-last-state
  10939. 'findpos org-log-repeat)))
  10940. (org-back-to-heading t)
  10941. (org-add-planning-info nil nil 'closed)
  10942. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  10943. org-deadline-time-regexp "\\)\\|\\("
  10944. org-ts-regexp "\\)"))
  10945. (while (re-search-forward
  10946. re (save-excursion (outline-next-heading) (point)) t)
  10947. (setq type (if (match-end 1) org-scheduled-string
  10948. (if (match-end 3) org-deadline-string "Plain:"))
  10949. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  10950. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts)
  10951. (setq n (string-to-number (match-string 2 ts))
  10952. what (match-string 3 ts))
  10953. (if (equal what "w") (setq n (* n 7) what "d"))
  10954. (if (and (equal what "h") (not (string-match "[0-9]\\{1,2\\}:[0-9]\\{2\\}" ts)))
  10955. (error "Cannot repeat in Repeat in %d hour(s) because no hour has been set" n))
  10956. ;; Preparation, see if we need to modify the start date for the change
  10957. (when (match-end 1)
  10958. (setq time (save-match-data (org-time-string-to-time ts)))
  10959. (cond
  10960. ((equal (match-string 1 ts) ".")
  10961. ;; Shift starting date to today
  10962. (org-timestamp-change
  10963. (- (org-today) (time-to-days time))
  10964. 'day))
  10965. ((equal (match-string 1 ts) "+")
  10966. (let ((nshiftmax 10) (nshift 0))
  10967. (while (or (= nshift 0)
  10968. (<= (time-to-days time)
  10969. (time-to-days (current-time))))
  10970. (when (= (incf nshift) nshiftmax)
  10971. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  10972. (error "Abort")))
  10973. (org-timestamp-change n (cdr (assoc what whata)))
  10974. (org-at-timestamp-p t)
  10975. (setq ts (match-string 1))
  10976. (setq time (save-match-data (org-time-string-to-time ts)))))
  10977. (org-timestamp-change (- n) (cdr (assoc what whata)))
  10978. ;; rematch, so that we have everything in place for the real shift
  10979. (org-at-timestamp-p t)
  10980. (setq ts (match-string 1))
  10981. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts))))
  10982. (org-timestamp-change n (cdr (assoc what whata)))
  10983. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  10984. (setq org-log-post-message msg)
  10985. (message "%s" msg))))
  10986. (defun org-show-todo-tree (arg)
  10987. "Make a compact tree which shows all headlines marked with TODO.
  10988. The tree will show the lines where the regexp matches, and all higher
  10989. headlines above the match.
  10990. With a \\[universal-argument] prefix, prompt for a regexp to match.
  10991. With a numeric prefix N, construct a sparse tree for the Nth element
  10992. of `org-todo-keywords-1'."
  10993. (interactive "P")
  10994. (let ((case-fold-search nil)
  10995. (kwd-re
  10996. (cond ((null arg) org-not-done-regexp)
  10997. ((equal arg '(4))
  10998. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  10999. (mapcar 'list org-todo-keywords-1))))
  11000. (concat "\\("
  11001. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  11002. "\\)\\>")))
  11003. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  11004. (regexp-quote (nth (1- (prefix-numeric-value arg))
  11005. org-todo-keywords-1)))
  11006. (t (error "Invalid prefix argument: %s" arg)))))
  11007. (message "%d TODO entries found"
  11008. (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
  11009. (defun org-deadline (&optional remove time)
  11010. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  11011. With argument REMOVE, remove any deadline from the item.
  11012. With argument TIME, set the deadline at the corresponding date. TIME
  11013. can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11014. (interactive "P")
  11015. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11016. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11017. 'region-start-level 'region))
  11018. org-loop-over-headlines-in-active-region)
  11019. (org-map-entries
  11020. `(org-deadline ',remove ,time)
  11021. org-loop-over-headlines-in-active-region
  11022. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11023. (let* ((old-date (org-entry-get nil "DEADLINE"))
  11024. (repeater (and old-date
  11025. (string-match
  11026. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11027. old-date)
  11028. (match-string 1 old-date))))
  11029. (if remove
  11030. (progn
  11031. (when (and old-date org-log-redeadline)
  11032. (org-add-log-setup 'deldeadline nil old-date 'findpos
  11033. org-log-redeadline))
  11034. (org-remove-timestamp-with-keyword org-deadline-string)
  11035. (message "Item no longer has a deadline."))
  11036. (org-add-planning-info 'deadline time 'closed)
  11037. (when (and old-date org-log-redeadline
  11038. (not (equal old-date
  11039. (substring org-last-inserted-timestamp 1 -1))))
  11040. (org-add-log-setup 'redeadline nil old-date 'findpos
  11041. org-log-redeadline))
  11042. (when repeater
  11043. (save-excursion
  11044. (org-back-to-heading t)
  11045. (when (re-search-forward (concat org-deadline-string " "
  11046. org-last-inserted-timestamp)
  11047. (save-excursion
  11048. (outline-next-heading) (point)) t)
  11049. (goto-char (1- (match-end 0)))
  11050. (insert " " repeater)
  11051. (setq org-last-inserted-timestamp
  11052. (concat (substring org-last-inserted-timestamp 0 -1)
  11053. " " repeater
  11054. (substring org-last-inserted-timestamp -1))))))
  11055. (message "Deadline on %s" org-last-inserted-timestamp)))))
  11056. (defun org-schedule (&optional remove time)
  11057. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  11058. With argument REMOVE, remove any scheduling date from the item.
  11059. With argument TIME, scheduled at the corresponding date. TIME can
  11060. either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11061. (interactive "P")
  11062. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11063. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11064. 'region-start-level 'region))
  11065. org-loop-over-headlines-in-active-region)
  11066. (org-map-entries
  11067. `(org-schedule ',remove ,time)
  11068. org-loop-over-headlines-in-active-region
  11069. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11070. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  11071. (repeater (and old-date
  11072. (string-match
  11073. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11074. old-date)
  11075. (match-string 1 old-date))))
  11076. (if remove
  11077. (progn
  11078. (when (and old-date org-log-reschedule)
  11079. (org-add-log-setup 'delschedule nil old-date 'findpos
  11080. org-log-reschedule))
  11081. (org-remove-timestamp-with-keyword org-scheduled-string)
  11082. (message "Item is no longer scheduled."))
  11083. (org-add-planning-info 'scheduled time 'closed)
  11084. (when (and old-date org-log-reschedule
  11085. (not (equal old-date
  11086. (substring org-last-inserted-timestamp 1 -1))))
  11087. (org-add-log-setup 'reschedule nil old-date 'findpos
  11088. org-log-reschedule))
  11089. (when repeater
  11090. (save-excursion
  11091. (org-back-to-heading t)
  11092. (when (re-search-forward (concat org-scheduled-string " "
  11093. org-last-inserted-timestamp)
  11094. (save-excursion
  11095. (outline-next-heading) (point)) t)
  11096. (goto-char (1- (match-end 0)))
  11097. (insert " " repeater)
  11098. (setq org-last-inserted-timestamp
  11099. (concat (substring org-last-inserted-timestamp 0 -1)
  11100. " " repeater
  11101. (substring org-last-inserted-timestamp -1))))))
  11102. (message "Scheduled to %s" org-last-inserted-timestamp)))))
  11103. (defun org-get-scheduled-time (pom &optional inherit)
  11104. "Get the scheduled time as a time tuple, of a format suitable
  11105. for calling org-schedule with, or if there is no scheduling,
  11106. returns nil."
  11107. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  11108. (when time
  11109. (apply 'encode-time (org-parse-time-string time)))))
  11110. (defun org-get-deadline-time (pom &optional inherit)
  11111. "Get the deadline as a time tuple, of a format suitable for
  11112. calling org-deadline with, or if there is no scheduling, returns
  11113. nil."
  11114. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  11115. (when time
  11116. (apply 'encode-time (org-parse-time-string time)))))
  11117. (defun org-remove-timestamp-with-keyword (keyword)
  11118. "Remove all time stamps with KEYWORD in the current entry."
  11119. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  11120. beg)
  11121. (save-excursion
  11122. (org-back-to-heading t)
  11123. (setq beg (point))
  11124. (outline-next-heading)
  11125. (while (re-search-backward re beg t)
  11126. (replace-match "")
  11127. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  11128. (equal (char-before) ?\ ))
  11129. (backward-delete-char 1)
  11130. (if (string-match "^[ \t]*$" (buffer-substring
  11131. (point-at-bol) (point-at-eol)))
  11132. (delete-region (point-at-bol)
  11133. (min (point-max) (1+ (point-at-eol))))))))))
  11134. (defun org-add-planning-info (what &optional time &rest remove)
  11135. "Insert new timestamp with keyword in the line directly after the headline.
  11136. WHAT indicates what kind of time stamp to add. TIME indicates the time to use.
  11137. If non is given, the user is prompted for a date.
  11138. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  11139. be removed."
  11140. (interactive)
  11141. (let (org-time-was-given org-end-time-was-given ts
  11142. end default-time default-input)
  11143. (catch 'exit
  11144. (when (and (memq what '(scheduled deadline))
  11145. (or (not time)
  11146. (and (stringp time)
  11147. (string-match "^[-+]+[0-9]" time))))
  11148. ;; Try to get a default date/time from existing timestamp
  11149. (save-excursion
  11150. (org-back-to-heading t)
  11151. (setq end (save-excursion (outline-next-heading) (point)))
  11152. (when (re-search-forward (if (eq what 'scheduled)
  11153. org-scheduled-time-regexp
  11154. org-deadline-time-regexp)
  11155. end t)
  11156. (setq ts (match-string 1)
  11157. default-time
  11158. (apply 'encode-time (org-parse-time-string ts))
  11159. default-input (and ts (org-get-compact-tod ts))))))
  11160. (when what
  11161. (setq time
  11162. (if (stringp time)
  11163. ;; This is a string (relative or absolute), set proper date
  11164. (apply 'encode-time
  11165. (org-read-date-analyze
  11166. time default-time (decode-time default-time)))
  11167. ;; If necessary, get the time from the user
  11168. (or time (org-read-date nil 'to-time nil nil
  11169. default-time default-input)))))
  11170. (when (and org-insert-labeled-timestamps-at-point
  11171. (member what '(scheduled deadline)))
  11172. (insert
  11173. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  11174. (org-insert-time-stamp time org-time-was-given
  11175. nil nil nil (list org-end-time-was-given))
  11176. (setq what nil))
  11177. (save-excursion
  11178. (save-restriction
  11179. (let (col list elt ts buffer-invisibility-spec)
  11180. (org-back-to-heading t)
  11181. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"))
  11182. (goto-char (match-end 1))
  11183. (setq col (current-column))
  11184. (goto-char (match-end 0))
  11185. (if (eobp) (insert "\n") (forward-char 1))
  11186. (when (and (not what)
  11187. (not (looking-at
  11188. (concat "[ \t]*"
  11189. org-keyword-time-not-clock-regexp))))
  11190. ;; Nothing to add, nothing to remove...... :-)
  11191. (throw 'exit nil))
  11192. (if (and (not (looking-at org-outline-regexp))
  11193. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  11194. "[^\r\n]*"))
  11195. (not (equal (match-string 1) org-clock-string)))
  11196. (narrow-to-region (match-beginning 0) (match-end 0))
  11197. (insert-before-markers "\n")
  11198. (backward-char 1)
  11199. (narrow-to-region (point) (point))
  11200. (and org-adapt-indentation (org-indent-to-column col)))
  11201. ;; Check if we have to remove something.
  11202. (setq list (cons what remove))
  11203. (while list
  11204. (setq elt (pop list))
  11205. (when (or (and (eq elt 'scheduled)
  11206. (re-search-forward org-scheduled-time-regexp nil t))
  11207. (and (eq elt 'deadline)
  11208. (re-search-forward org-deadline-time-regexp nil t))
  11209. (and (eq elt 'closed)
  11210. (re-search-forward org-closed-time-regexp nil t)))
  11211. (replace-match "")
  11212. (if (looking-at "--+<[^>]+>") (replace-match ""))))
  11213. (and (looking-at "[ \t]+") (replace-match ""))
  11214. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  11215. (when what
  11216. (insert
  11217. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  11218. (cond ((eq what 'scheduled) org-scheduled-string)
  11219. ((eq what 'deadline) org-deadline-string)
  11220. ((eq what 'closed) org-closed-string))
  11221. " ")
  11222. (setq ts (org-insert-time-stamp
  11223. time
  11224. (or org-time-was-given
  11225. (and (eq what 'closed) org-log-done-with-time))
  11226. (eq what 'closed)
  11227. nil nil (list org-end-time-was-given)))
  11228. (insert
  11229. (if (not (or (bolp) (eq (char-before) ?\ )
  11230. (memq (char-after) '(32 10))
  11231. (eobp))) " " ""))
  11232. (end-of-line 1))
  11233. (goto-char (point-min))
  11234. (widen)
  11235. (if (and (looking-at "[ \t]*\n")
  11236. (equal (char-before) ?\n))
  11237. (delete-region (1- (point)) (point-at-eol)))
  11238. ts))))))
  11239. (defvar org-log-note-marker (make-marker))
  11240. (defvar org-log-note-purpose nil)
  11241. (defvar org-log-note-state nil)
  11242. (defvar org-log-note-previous-state nil)
  11243. (defvar org-log-note-how nil)
  11244. (defvar org-log-note-extra nil)
  11245. (defvar org-log-note-window-configuration nil)
  11246. (defvar org-log-note-return-to (make-marker))
  11247. (defvar org-log-note-effective-time nil
  11248. "Remembered current time so that dynamically scoped
  11249. `org-extend-today-until' affects tha timestamps in state change
  11250. log")
  11251. (defvar org-log-post-message nil
  11252. "Message to be displayed after a log note has been stored.
  11253. The auto-repeater uses this.")
  11254. (defun org-add-note ()
  11255. "Add a note to the current entry.
  11256. This is done in the same way as adding a state change note."
  11257. (interactive)
  11258. (org-add-log-setup 'note nil nil 'findpos nil))
  11259. (defvar org-property-end-re)
  11260. (defun org-add-log-setup (&optional purpose state prev-state
  11261. findpos how extra)
  11262. "Set up the post command hook to take a note.
  11263. If this is about to TODO state change, the new state is expected in STATE.
  11264. When FINDPOS is non-nil, find the correct position for the note in
  11265. the current entry. If not, assume that it can be inserted at point.
  11266. HOW is an indicator what kind of note should be created.
  11267. EXTRA is additional text that will be inserted into the notes buffer."
  11268. (let* ((org-log-into-drawer (org-log-into-drawer))
  11269. (drawer (cond ((stringp org-log-into-drawer)
  11270. org-log-into-drawer)
  11271. (org-log-into-drawer "LOGBOOK")
  11272. (t nil))))
  11273. (save-restriction
  11274. (save-excursion
  11275. (when findpos
  11276. (org-back-to-heading t)
  11277. (narrow-to-region (point) (save-excursion
  11278. (outline-next-heading) (point)))
  11279. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"
  11280. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  11281. "[^\r\n]*\\)?"))
  11282. (goto-char (match-end 0))
  11283. (cond
  11284. (drawer
  11285. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  11286. nil t)
  11287. (progn
  11288. (goto-char (match-end 0))
  11289. (or org-log-states-order-reversed
  11290. (and (re-search-forward org-property-end-re nil t)
  11291. (goto-char (1- (match-beginning 0))))))
  11292. (insert "\n:" drawer ":\n:END:")
  11293. (beginning-of-line 0)
  11294. (org-indent-line)
  11295. (beginning-of-line 2)
  11296. (org-indent-line)
  11297. (end-of-line 0)))
  11298. ((and org-log-state-notes-insert-after-drawers
  11299. (save-excursion
  11300. (forward-line) (looking-at org-drawer-regexp)))
  11301. (forward-line)
  11302. (while (looking-at org-drawer-regexp)
  11303. (goto-char (match-end 0))
  11304. (re-search-forward org-property-end-re (point-max) t)
  11305. (forward-line))
  11306. (forward-line -1)))
  11307. (unless org-log-states-order-reversed
  11308. (and (= (char-after) ?\n) (forward-char 1))
  11309. (org-skip-over-state-notes)
  11310. (skip-chars-backward " \t\n\r")))
  11311. (move-marker org-log-note-marker (point))
  11312. (setq org-log-note-purpose purpose
  11313. org-log-note-state state
  11314. org-log-note-previous-state prev-state
  11315. org-log-note-how how
  11316. org-log-note-extra extra
  11317. org-log-note-effective-time (org-current-effective-time))
  11318. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  11319. (defun org-skip-over-state-notes ()
  11320. "Skip past the list of State notes in an entry."
  11321. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  11322. (when (ignore-errors (goto-char (org-in-item-p)))
  11323. (let* ((struct (org-list-struct))
  11324. (prevs (org-list-prevs-alist struct)))
  11325. (while (looking-at "[ \t]*- State")
  11326. (goto-char (or (org-list-get-next-item (point) struct prevs)
  11327. (org-list-get-item-end (point) struct)))))))
  11328. (defun org-add-log-note (&optional purpose)
  11329. "Pop up a window for taking a note, and add this note later at point."
  11330. (remove-hook 'post-command-hook 'org-add-log-note)
  11331. (setq org-log-note-window-configuration (current-window-configuration))
  11332. (delete-other-windows)
  11333. (move-marker org-log-note-return-to (point))
  11334. (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
  11335. (goto-char org-log-note-marker)
  11336. (org-switch-to-buffer-other-window "*Org Note*")
  11337. (erase-buffer)
  11338. (if (memq org-log-note-how '(time state))
  11339. (let (current-prefix-arg) (org-store-log-note))
  11340. (let ((org-inhibit-startup t)) (org-mode))
  11341. (insert (format "# Insert note for %s.
  11342. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  11343. (cond
  11344. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  11345. ((eq org-log-note-purpose 'done) "closed todo item")
  11346. ((eq org-log-note-purpose 'state)
  11347. (format "state change from \"%s\" to \"%s\""
  11348. (or org-log-note-previous-state "")
  11349. (or org-log-note-state "")))
  11350. ((eq org-log-note-purpose 'reschedule)
  11351. "rescheduling")
  11352. ((eq org-log-note-purpose 'delschedule)
  11353. "no longer scheduled")
  11354. ((eq org-log-note-purpose 'redeadline)
  11355. "changing deadline")
  11356. ((eq org-log-note-purpose 'deldeadline)
  11357. "removing deadline")
  11358. ((eq org-log-note-purpose 'refile)
  11359. "refiling")
  11360. ((eq org-log-note-purpose 'note)
  11361. "this entry")
  11362. (t (error "This should not happen")))))
  11363. (if org-log-note-extra (insert org-log-note-extra))
  11364. (org-set-local 'org-finish-function 'org-store-log-note)
  11365. (run-hooks 'org-log-buffer-setup-hook)))
  11366. (defvar org-note-abort nil) ; dynamically scoped
  11367. (defun org-store-log-note ()
  11368. "Finish taking a log note, and insert it to where it belongs."
  11369. (let ((txt (buffer-string))
  11370. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  11371. lines ind bul)
  11372. (kill-buffer (current-buffer))
  11373. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  11374. (setq txt (replace-match "" t t txt)))
  11375. (if (string-match "\\s-+\\'" txt)
  11376. (setq txt (replace-match "" t t txt)))
  11377. (setq lines (org-split-string txt "\n"))
  11378. (when (and note (string-match "\\S-" note))
  11379. (setq note
  11380. (org-replace-escapes
  11381. note
  11382. (list (cons "%u" (user-login-name))
  11383. (cons "%U" user-full-name)
  11384. (cons "%t" (format-time-string
  11385. (org-time-stamp-format 'long 'inactive)
  11386. org-log-note-effective-time))
  11387. (cons "%T" (format-time-string
  11388. (org-time-stamp-format 'long nil)
  11389. org-log-note-effective-time))
  11390. (cons "%d" (format-time-string
  11391. (org-time-stamp-format nil 'inactive)
  11392. org-log-note-effective-time))
  11393. (cons "%D" (format-time-string
  11394. (org-time-stamp-format nil nil)
  11395. org-log-note-effective-time))
  11396. (cons "%s" (if org-log-note-state
  11397. (concat "\"" org-log-note-state "\"")
  11398. ""))
  11399. (cons "%S" (if org-log-note-previous-state
  11400. (concat "\"" org-log-note-previous-state "\"")
  11401. "\"\"")))))
  11402. (if lines (setq note (concat note " \\\\")))
  11403. (push note lines))
  11404. (when (or current-prefix-arg org-note-abort)
  11405. (when org-log-into-drawer
  11406. (org-remove-empty-drawer-at
  11407. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  11408. org-log-note-marker))
  11409. (setq lines nil))
  11410. (when lines
  11411. (with-current-buffer (marker-buffer org-log-note-marker)
  11412. (save-excursion
  11413. (goto-char org-log-note-marker)
  11414. (move-marker org-log-note-marker nil)
  11415. (end-of-line 1)
  11416. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  11417. (setq ind (save-excursion
  11418. (if (ignore-errors (goto-char (org-in-item-p)))
  11419. (let ((struct (org-list-struct)))
  11420. (org-list-get-ind
  11421. (org-list-get-top-point struct) struct))
  11422. (skip-chars-backward " \r\t\n")
  11423. (cond
  11424. ((and (org-at-heading-p)
  11425. org-adapt-indentation)
  11426. (1+ (org-current-level)))
  11427. ((org-at-heading-p) 0)
  11428. (t (org-get-indentation))))))
  11429. (setq bul (org-list-bullet-string "-"))
  11430. (org-indent-line-to ind)
  11431. (insert bul (pop lines))
  11432. (let ((ind-body (+ (length bul) ind)))
  11433. (while lines
  11434. (insert "\n")
  11435. (org-indent-line-to ind-body)
  11436. (insert (pop lines))))
  11437. (message "Note stored")
  11438. (org-back-to-heading t)
  11439. (org-cycle-hide-drawers 'children)))))
  11440. (set-window-configuration org-log-note-window-configuration)
  11441. (with-current-buffer (marker-buffer org-log-note-return-to)
  11442. (goto-char org-log-note-return-to))
  11443. (move-marker org-log-note-return-to nil)
  11444. (and org-log-post-message (message "%s" org-log-post-message)))
  11445. (defun org-remove-empty-drawer-at (drawer pos)
  11446. "Remove an empty drawer DRAWER at position POS.
  11447. POS may also be a marker."
  11448. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  11449. (save-excursion
  11450. (save-restriction
  11451. (widen)
  11452. (goto-char pos)
  11453. (if (org-in-regexp
  11454. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  11455. (replace-match ""))))))
  11456. (defvar org-ts-type nil)
  11457. (defun org-sparse-tree (&optional arg type)
  11458. "Create a sparse tree, prompt for the details.
  11459. This command can create sparse trees. You first need to select the type
  11460. of match used to create the tree:
  11461. t Show all TODO entries.
  11462. T Show entries with a specific TODO keyword.
  11463. m Show entries selected by a tags/property match.
  11464. p Enter a property name and its value (both with completion on existing
  11465. names/values) and show entries with that property.
  11466. r Show entries matching a regular expression (`/' can be used as well).
  11467. b Show deadlines and scheduled items before a date.
  11468. a Show deadlines and scheduled items after a date.
  11469. d Show deadlines due within `org-deadline-warning-days'.
  11470. D Show deadlines and scheduled items between a date range."
  11471. (interactive "P")
  11472. (let (ans kwd value ts-type)
  11473. (setq type (or type org-sparse-tree-default-date-type))
  11474. (setq org-ts-type type)
  11475. (message "Sparse tree: [r]egexp [/]regexp [t]odo [T]odo-kwd [m]atch [p]roperty\n [d]eadlines [b]efore-date [a]fter-date [D]ates range\n [c]ycle through date types: %s"
  11476. (cond ((eq type 'all) "all timestamps")
  11477. ((eq type 'scheduled) "only scheduled")
  11478. ((eq type 'deadline) "only deadline")
  11479. ((eq type 'active) "only active timestamps")
  11480. ((eq type 'inactive) "only inactive timestamps")
  11481. ((eq type 'scheduled-or-deadline) "scheduled/deadline")
  11482. (t "scheduled/deadline")))
  11483. (setq ans (read-char-exclusive))
  11484. (cond
  11485. ((equal ans ?c)
  11486. (org-sparse-tree arg (cadr (member type '(scheduled-or-deadline all scheduled deadline active inactive)))))
  11487. ((equal ans ?d)
  11488. (call-interactively 'org-check-deadlines))
  11489. ((equal ans ?b)
  11490. (call-interactively 'org-check-before-date))
  11491. ((equal ans ?a)
  11492. (call-interactively 'org-check-after-date))
  11493. ((equal ans ?D)
  11494. (call-interactively 'org-check-dates-range))
  11495. ((equal ans ?t)
  11496. (org-show-todo-tree nil))
  11497. ((equal ans ?T)
  11498. (org-show-todo-tree '(4)))
  11499. ((member ans '(?T ?m))
  11500. (call-interactively 'org-match-sparse-tree))
  11501. ((member ans '(?p ?P))
  11502. (setq kwd (org-icompleting-read "Property: "
  11503. (mapcar 'list (org-buffer-property-keys))))
  11504. (setq value (org-icompleting-read "Value: "
  11505. (mapcar 'list (org-property-values kwd))))
  11506. (unless (string-match "\\`{.*}\\'" value)
  11507. (setq value (concat "\"" value "\"")))
  11508. (org-match-sparse-tree arg (concat kwd "=" value)))
  11509. ((member ans '(?r ?R ?/))
  11510. (call-interactively 'org-occur))
  11511. (t (error "No such sparse tree command \"%c\"" ans)))))
  11512. (defvar org-occur-highlights nil
  11513. "List of overlays used for occur matches.")
  11514. (make-variable-buffer-local 'org-occur-highlights)
  11515. (defvar org-occur-parameters nil
  11516. "Parameters of the active org-occur calls.
  11517. This is a list, each call to org-occur pushes as cons cell,
  11518. containing the regular expression and the callback, onto the list.
  11519. The list can contain several entries if `org-occur' has been called
  11520. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  11521. will only contain one set of parameters. When the highlights are
  11522. removed (for example with `C-c C-c', or with the next edit (depending
  11523. on `org-remove-highlights-with-change'), this variable is emptied
  11524. as well.")
  11525. (make-variable-buffer-local 'org-occur-parameters)
  11526. (defun org-occur (regexp &optional keep-previous callback)
  11527. "Make a compact tree which shows all matches of REGEXP.
  11528. The tree will show the lines where the regexp matches, and all higher
  11529. headlines above the match. It will also show the heading after the match,
  11530. to make sure editing the matching entry is easy.
  11531. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  11532. call to `org-occur' will be kept, to allow stacking of calls to this
  11533. command.
  11534. If CALLBACK is non-nil, it is a function which is called to confirm
  11535. that the match should indeed be shown."
  11536. (interactive "sRegexp: \nP")
  11537. (when (equal regexp "")
  11538. (error "Regexp cannot be empty"))
  11539. (unless keep-previous
  11540. (org-remove-occur-highlights nil nil t))
  11541. (push (cons regexp callback) org-occur-parameters)
  11542. (let ((cnt 0))
  11543. (save-excursion
  11544. (goto-char (point-min))
  11545. (if (or (not keep-previous) ; do not want to keep
  11546. (not org-occur-highlights)) ; no previous matches
  11547. ;; hide everything
  11548. (org-overview))
  11549. (while (re-search-forward regexp nil t)
  11550. (when (or (not callback)
  11551. (save-match-data (funcall callback)))
  11552. (setq cnt (1+ cnt))
  11553. (when org-highlight-sparse-tree-matches
  11554. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  11555. (org-show-context 'occur-tree))))
  11556. (when org-remove-highlights-with-change
  11557. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  11558. nil 'local))
  11559. (unless org-sparse-tree-open-archived-trees
  11560. (org-hide-archived-subtrees (point-min) (point-max)))
  11561. (run-hooks 'org-occur-hook)
  11562. (if (org-called-interactively-p 'interactive)
  11563. (message "%d match(es) for regexp %s" cnt regexp))
  11564. cnt))
  11565. (defun org-occur-next-match (&optional n reset)
  11566. "Function for `next-error-function' to find sparse tree matches.
  11567. N is the number of matches to move, when negative move backwards.
  11568. RESET is entirely ignored - this function always goes back to the
  11569. starting point when no match is found."
  11570. (let* ((limit (if (< n 0) (point-min) (point-max)))
  11571. (search-func (if (< n 0)
  11572. 'previous-single-char-property-change
  11573. 'next-single-char-property-change))
  11574. (n (abs n))
  11575. (pos (point))
  11576. p1)
  11577. (catch 'exit
  11578. (while (setq p1 (funcall search-func (point) 'org-type))
  11579. (when (equal p1 limit)
  11580. (goto-char pos)
  11581. (error "No more matches"))
  11582. (when (equal (get-char-property p1 'org-type) 'org-occur)
  11583. (setq n (1- n))
  11584. (when (= n 0)
  11585. (goto-char p1)
  11586. (throw 'exit (point))))
  11587. (goto-char p1))
  11588. (goto-char p1)
  11589. (error "No more matches"))))
  11590. (defun org-show-context (&optional key)
  11591. "Make sure point and context are visible.
  11592. How much context is shown depends upon the variables
  11593. `org-show-hierarchy-above', `org-show-following-heading',
  11594. `org-show-entry-below' and `org-show-siblings'."
  11595. (let ((heading-p (org-at-heading-p t))
  11596. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  11597. (following-p (org-get-alist-option org-show-following-heading key))
  11598. (entry-p (org-get-alist-option org-show-entry-below key))
  11599. (siblings-p (org-get-alist-option org-show-siblings key)))
  11600. (catch 'exit
  11601. ;; Show heading or entry text
  11602. (if (and heading-p (not entry-p))
  11603. (org-flag-heading nil) ; only show the heading
  11604. (and (or entry-p (outline-invisible-p) (org-invisible-p2))
  11605. (org-show-hidden-entry))) ; show entire entry
  11606. (when following-p
  11607. ;; Show next sibling, or heading below text
  11608. (save-excursion
  11609. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  11610. (org-flag-heading nil))))
  11611. (when siblings-p (org-show-siblings))
  11612. (when hierarchy-p
  11613. ;; show all higher headings, possibly with siblings
  11614. (save-excursion
  11615. (while (and (condition-case nil
  11616. (progn (org-up-heading-all 1) t)
  11617. (error nil))
  11618. (not (bobp)))
  11619. (org-flag-heading nil)
  11620. (when siblings-p (org-show-siblings))))))))
  11621. (defvar org-reveal-start-hook nil
  11622. "Hook run before revealing a location.")
  11623. (defun org-reveal (&optional siblings)
  11624. "Show current entry, hierarchy above it, and the following headline.
  11625. This can be used to show a consistent set of context around locations
  11626. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  11627. not t for the search context.
  11628. With optional argument SIBLINGS, on each level of the hierarchy all
  11629. siblings are shown. This repairs the tree structure to what it would
  11630. look like when opened with hierarchical calls to `org-cycle'.
  11631. With double optional argument \\[universal-argument] \\[universal-argument], \
  11632. go to the parent and show the
  11633. entire tree."
  11634. (interactive "P")
  11635. (run-hooks 'org-reveal-start-hook)
  11636. (let ((org-show-hierarchy-above t)
  11637. (org-show-following-heading t)
  11638. (org-show-siblings (if siblings t org-show-siblings)))
  11639. (org-show-context nil))
  11640. (when (equal siblings '(16))
  11641. (save-excursion
  11642. (when (org-up-heading-safe)
  11643. (org-show-subtree)
  11644. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  11645. (defun org-highlight-new-match (beg end)
  11646. "Highlight from BEG to END and mark the highlight is an occur headline."
  11647. (let ((ov (make-overlay beg end)))
  11648. (overlay-put ov 'face 'secondary-selection)
  11649. (overlay-put ov 'org-type 'org-occur)
  11650. (push ov org-occur-highlights)))
  11651. (defun org-remove-occur-highlights (&optional beg end noremove)
  11652. "Remove the occur highlights from the buffer.
  11653. BEG and END are ignored. If NOREMOVE is nil, remove this function
  11654. from the `before-change-functions' in the current buffer."
  11655. (interactive)
  11656. (unless org-inhibit-highlight-removal
  11657. (mapc 'delete-overlay org-occur-highlights)
  11658. (setq org-occur-highlights nil)
  11659. (setq org-occur-parameters nil)
  11660. (unless noremove
  11661. (remove-hook 'before-change-functions
  11662. 'org-remove-occur-highlights 'local))))
  11663. ;;;; Priorities
  11664. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  11665. "Regular expression matching the priority indicator.")
  11666. (defvar org-remove-priority-next-time nil)
  11667. (defun org-priority-up ()
  11668. "Increase the priority of the current item."
  11669. (interactive)
  11670. (org-priority 'up))
  11671. (defun org-priority-down ()
  11672. "Decrease the priority of the current item."
  11673. (interactive)
  11674. (org-priority 'down))
  11675. (defun org-priority (&optional action)
  11676. "Change the priority of an item by ARG.
  11677. ACTION can be `set', `up', `down', or a character."
  11678. (interactive)
  11679. (unless org-enable-priority-commands
  11680. (error "Priority commands are disabled"))
  11681. (setq action (or action 'set))
  11682. (let (current new news have remove)
  11683. (save-excursion
  11684. (org-back-to-heading t)
  11685. (if (looking-at org-priority-regexp)
  11686. (setq current (string-to-char (match-string 2))
  11687. have t))
  11688. (cond
  11689. ((eq action 'remove)
  11690. (setq remove t new ?\ ))
  11691. ((or (eq action 'set)
  11692. (if (featurep 'xemacs) (characterp action) (integerp action)))
  11693. (if (not (eq action 'set))
  11694. (setq new action)
  11695. (message "Priority %c-%c, SPC to remove: "
  11696. org-highest-priority org-lowest-priority)
  11697. (save-match-data
  11698. (setq new (read-char-exclusive))))
  11699. (if (and (= (upcase org-highest-priority) org-highest-priority)
  11700. (= (upcase org-lowest-priority) org-lowest-priority))
  11701. (setq new (upcase new)))
  11702. (cond ((equal new ?\ ) (setq remove t))
  11703. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  11704. (error "Priority must be between `%c' and `%c'"
  11705. org-highest-priority org-lowest-priority))))
  11706. ((eq action 'up)
  11707. (setq new (if have
  11708. (1- current) ; normal cycling
  11709. ;; last priority was empty
  11710. (if (eq last-command this-command)
  11711. org-lowest-priority ; wrap around empty to lowest
  11712. ;; default
  11713. (if org-priority-start-cycle-with-default
  11714. org-default-priority
  11715. (1- org-default-priority))))))
  11716. ((eq action 'down)
  11717. (setq new (if have
  11718. (1+ current) ; normal cycling
  11719. ;; last priority was empty
  11720. (if (eq last-command this-command)
  11721. org-highest-priority ; wrap around empty to highest
  11722. ;; default
  11723. (if org-priority-start-cycle-with-default
  11724. org-default-priority
  11725. (1+ org-default-priority))))))
  11726. (t (error "Invalid action")))
  11727. (if (or (< (upcase new) org-highest-priority)
  11728. (> (upcase new) org-lowest-priority))
  11729. (if (and (memq action '(up down))
  11730. (not have) (not (eq last-command this-command)))
  11731. ;; `new' is from default priority
  11732. (error
  11733. "The default can not be set, see `org-default-priority' why")
  11734. ;; normal cycling: `new' is beyond highest/lowest priority
  11735. ;; and is wrapped around to the empty priority
  11736. (setq remove t)))
  11737. (setq news (format "%c" new))
  11738. (if have
  11739. (if remove
  11740. (replace-match "" t t nil 1)
  11741. (replace-match news t t nil 2))
  11742. (if remove
  11743. (error "No priority cookie found in line")
  11744. (let ((case-fold-search nil))
  11745. (looking-at org-todo-line-regexp))
  11746. (if (match-end 2)
  11747. (progn
  11748. (goto-char (match-end 2))
  11749. (insert " [#" news "]"))
  11750. (goto-char (match-beginning 3))
  11751. (insert "[#" news "] "))))
  11752. (org-preserve-lc (org-set-tags nil 'align)))
  11753. (if remove
  11754. (message "Priority removed")
  11755. (message "Priority of current item set to %s" news))))
  11756. (defun org-get-priority (s)
  11757. "Find priority cookie and return priority."
  11758. (if (functionp org-get-priority-function)
  11759. (funcall org-get-priority-function)
  11760. (save-match-data
  11761. (if (not (string-match org-priority-regexp s))
  11762. (* 1000 (- org-lowest-priority org-default-priority))
  11763. (* 1000 (- org-lowest-priority
  11764. (string-to-char (match-string 2 s))))))))
  11765. ;;;; Tags
  11766. (defvar org-agenda-archives-mode)
  11767. (defvar org-map-continue-from nil
  11768. "Position from where mapping should continue.
  11769. Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
  11770. (defvar org-scanner-tags nil
  11771. "The current tag list while the tags scanner is running.")
  11772. (defvar org-trust-scanner-tags nil
  11773. "Should `org-get-tags-at' use the tags for the scanner.
  11774. This is for internal dynamical scoping only.
  11775. When this is non-nil, the function `org-get-tags-at' will return the value
  11776. of `org-scanner-tags' instead of building the list by itself. This
  11777. can lead to large speed-ups when the tags scanner is used in a file with
  11778. many entries, and when the list of tags is retrieved, for example to
  11779. obtain a list of properties. Building the tags list for each entry in such
  11780. a file becomes an N^2 operation - but with this variable set, it scales
  11781. as N.")
  11782. (defun org-scan-tags (action matcher todo-only &optional start-level)
  11783. "Scan headline tags with inheritance and produce output ACTION.
  11784. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  11785. or `agenda' to produce an entry list for an agenda view. It can also be
  11786. a Lisp form or a function that should be called at each matched headline, in
  11787. this case the return value is a list of all return values from these calls.
  11788. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  11789. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  11790. only lines with a not-done TODO keyword are included in the output.
  11791. This should be the same variable that was scoped into
  11792. and set by `org-make-tags-matcher' when it constructed MATCHER.
  11793. START-LEVEL can be a string with asterisks, reducing the scope to
  11794. headlines matching this string."
  11795. (require 'org-agenda)
  11796. (let* ((re (concat "^"
  11797. (if start-level
  11798. ;; Get the correct level to match
  11799. (concat "\\*\\{" (number-to-string start-level) "\\} ")
  11800. org-outline-regexp)
  11801. " *\\(\\<\\("
  11802. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  11803. (org-re "\\)\\>\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  11804. (props (list 'face 'default
  11805. 'done-face 'org-agenda-done
  11806. 'undone-face 'default
  11807. 'mouse-face 'highlight
  11808. 'org-not-done-regexp org-not-done-regexp
  11809. 'org-todo-regexp org-todo-regexp
  11810. 'org-complex-heading-regexp org-complex-heading-regexp
  11811. 'help-echo
  11812. (format "mouse-2 or RET jump to org file %s"
  11813. (abbreviate-file-name
  11814. (or (buffer-file-name (buffer-base-buffer))
  11815. (buffer-name (buffer-base-buffer)))))))
  11816. (case-fold-search nil)
  11817. (org-map-continue-from nil)
  11818. lspos tags tags-list
  11819. (tags-alist (list (cons 0 org-file-tags)))
  11820. (llast 0) rtn rtn1 level category i txt
  11821. todo marker entry priority)
  11822. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  11823. (setq action (list 'lambda nil action)))
  11824. (save-excursion
  11825. (goto-char (point-min))
  11826. (when (eq action 'sparse-tree)
  11827. (org-overview)
  11828. (org-remove-occur-highlights))
  11829. (while (re-search-forward re nil t)
  11830. (setq org-map-continue-from nil)
  11831. (catch :skip
  11832. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  11833. tags (if (match-end 4) (org-match-string-no-properties 4)))
  11834. (goto-char (setq lspos (match-beginning 0)))
  11835. (setq level (org-reduced-level (funcall outline-level))
  11836. category (org-get-category))
  11837. (setq i llast llast level)
  11838. ;; remove tag lists from same and sublevels
  11839. (while (>= i level)
  11840. (when (setq entry (assoc i tags-alist))
  11841. (setq tags-alist (delete entry tags-alist)))
  11842. (setq i (1- i)))
  11843. ;; add the next tags
  11844. (when tags
  11845. (setq tags (org-split-string tags ":")
  11846. tags-alist
  11847. (cons (cons level tags) tags-alist)))
  11848. ;; compile tags for current headline
  11849. (setq tags-list
  11850. (if org-use-tag-inheritance
  11851. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  11852. tags)
  11853. org-scanner-tags tags-list)
  11854. (when org-use-tag-inheritance
  11855. (setcdr (car tags-alist)
  11856. (mapcar (lambda (x)
  11857. (setq x (copy-sequence x))
  11858. (org-add-prop-inherited x))
  11859. (cdar tags-alist))))
  11860. (when (and tags org-use-tag-inheritance
  11861. (or (not (eq t org-use-tag-inheritance))
  11862. org-tags-exclude-from-inheritance))
  11863. ;; selective inheritance, remove uninherited ones
  11864. (setcdr (car tags-alist)
  11865. (org-remove-uninherited-tags (cdar tags-alist))))
  11866. (when (and
  11867. ;; eval matcher only when the todo condition is OK
  11868. (and (or (not todo-only) (member todo org-not-done-keywords))
  11869. (let ((case-fold-search t) (org-trust-scanner-tags t))
  11870. (eval matcher)))
  11871. ;; Call the skipper, but return t if it does not skip,
  11872. ;; so that the `and' form continues evaluating
  11873. (progn
  11874. (unless (eq action 'sparse-tree) (org-agenda-skip))
  11875. t)
  11876. ;; Check if timestamps are deselecting this entry
  11877. (or (not todo-only)
  11878. (and (member todo org-not-done-keywords)
  11879. (or (not org-agenda-tags-todo-honor-ignore-options)
  11880. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  11881. ;; Extra check for the archive tag
  11882. ;; FIXME: Does the skipper already do this????
  11883. (or
  11884. (not (member org-archive-tag tags-list))
  11885. ;; we have an archive tag, should we use this anyway?
  11886. (or (not org-agenda-skip-archived-trees)
  11887. (and (eq action 'agenda) org-agenda-archives-mode))))
  11888. ;; select this headline
  11889. (cond
  11890. ((eq action 'sparse-tree)
  11891. (and org-highlight-sparse-tree-matches
  11892. (org-get-heading) (match-end 0)
  11893. (org-highlight-new-match
  11894. (match-beginning 1) (match-end 1)))
  11895. (org-show-context 'tags-tree))
  11896. ((eq action 'agenda)
  11897. (setq txt (org-agenda-format-item
  11898. ""
  11899. (concat
  11900. (if (eq org-tags-match-list-sublevels 'indented)
  11901. (make-string (1- level) ?.) "")
  11902. (org-get-heading))
  11903. category
  11904. tags-list)
  11905. priority (org-get-priority txt))
  11906. (goto-char lspos)
  11907. (setq marker (org-agenda-new-marker))
  11908. (org-add-props txt props
  11909. 'org-marker marker 'org-hd-marker marker 'org-category category
  11910. 'todo-state todo
  11911. 'priority priority 'type "tagsmatch")
  11912. (push txt rtn))
  11913. ((functionp action)
  11914. (setq org-map-continue-from nil)
  11915. (save-excursion
  11916. (setq rtn1 (funcall action))
  11917. (push rtn1 rtn)))
  11918. (t (error "Invalid action")))
  11919. ;; if we are to skip sublevels, jump to end of subtree
  11920. (unless org-tags-match-list-sublevels
  11921. (org-end-of-subtree t)
  11922. (backward-char 1))))
  11923. ;; Get the correct position from where to continue
  11924. (if org-map-continue-from
  11925. (goto-char org-map-continue-from)
  11926. (and (= (point) lspos) (end-of-line 1)))))
  11927. (when (and (eq action 'sparse-tree)
  11928. (not org-sparse-tree-open-archived-trees))
  11929. (org-hide-archived-subtrees (point-min) (point-max)))
  11930. (nreverse rtn)))
  11931. (defun org-remove-uninherited-tags (tags)
  11932. "Remove all tags that are not inherited from the list TAGS."
  11933. (cond
  11934. ((eq org-use-tag-inheritance t)
  11935. (if org-tags-exclude-from-inheritance
  11936. (org-delete-all org-tags-exclude-from-inheritance tags)
  11937. tags))
  11938. ((not org-use-tag-inheritance) nil)
  11939. ((stringp org-use-tag-inheritance)
  11940. (delq nil (mapcar
  11941. (lambda (x)
  11942. (if (and (string-match org-use-tag-inheritance x)
  11943. (not (member x org-tags-exclude-from-inheritance)))
  11944. x nil))
  11945. tags)))
  11946. ((listp org-use-tag-inheritance)
  11947. (delq nil (mapcar
  11948. (lambda (x)
  11949. (if (member x org-use-tag-inheritance) x nil))
  11950. tags)))))
  11951. (defun org-match-sparse-tree (&optional todo-only match)
  11952. "Create a sparse tree according to tags string MATCH.
  11953. MATCH can contain positive and negative selection of tags, like
  11954. \"+WORK+URGENT-WITHBOSS\".
  11955. If optional argument TODO-ONLY is non-nil, only select lines that are
  11956. also TODO lines."
  11957. (interactive "P")
  11958. (org-prepare-agenda-buffers (list (current-buffer)))
  11959. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  11960. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  11961. (defvar org-cached-props nil)
  11962. (defun org-cached-entry-get (pom property)
  11963. (if (or (eq t org-use-property-inheritance)
  11964. (and (stringp org-use-property-inheritance)
  11965. (string-match org-use-property-inheritance property))
  11966. (and (listp org-use-property-inheritance)
  11967. (member property org-use-property-inheritance)))
  11968. ;; Caching is not possible, check it directly
  11969. (org-entry-get pom property 'inherit)
  11970. ;; Get all properties, so that we can do complicated checks easily
  11971. (cdr (assoc property (or org-cached-props
  11972. (setq org-cached-props
  11973. (org-entry-properties pom)))))))
  11974. (defun org-global-tags-completion-table (&optional files)
  11975. "Return the list of all tags in all agenda buffer/files.
  11976. Optional FILES argument is a list of files to which can be used
  11977. instead of the agenda files."
  11978. (save-excursion
  11979. (org-uniquify
  11980. (delq nil
  11981. (apply 'append
  11982. (mapcar
  11983. (lambda (file)
  11984. (set-buffer (find-file-noselect file))
  11985. (append (org-get-buffer-tags)
  11986. (mapcar (lambda (x) (if (stringp (car-safe x))
  11987. (list (car-safe x)) nil))
  11988. org-tag-alist)))
  11989. (if (and files (car files))
  11990. files
  11991. (org-agenda-files))))))))
  11992. (defun org-make-tags-matcher (match)
  11993. "Create the TAGS/TODO matcher form for the selection string MATCH.
  11994. The variable `todo-only' is scoped dynamically into this function; it will be
  11995. set to t if the matcher restricts matching to TODO entries,
  11996. otherwise will not be touched.
  11997. Returns a cons of the selection string MATCH and the constructed
  11998. lisp form implementing the matcher. The matcher is to be
  11999. evaluated at an Org entry, with point on the headline,
  12000. and returns t if the entry matches the
  12001. selection string MATCH. The returned lisp form references
  12002. two variables with information about the entry, which must be
  12003. bound around the form's evaluation: todo, the TODO keyword at the
  12004. entry (or nil of none); and tags-list, the list of all tags at the
  12005. entry including inherited ones. Additionally, the category
  12006. of the entry (if any) must be specified as the text property
  12007. 'org-category on the headline.
  12008. See also `org-scan-tags'.
  12009. "
  12010. (declare (special todo-only))
  12011. (unless (boundp 'todo-only)
  12012. (error "org-make-tags-matcher expects todo-only to be scoped in"))
  12013. (unless match
  12014. ;; Get a new match request, with completion
  12015. (let ((org-last-tags-completion-table
  12016. (org-global-tags-completion-table)))
  12017. (setq match (org-completing-read-no-i
  12018. "Match: " 'org-tags-completion-function nil nil nil
  12019. 'org-tags-history))))
  12020. ;; Parse the string and create a lisp form
  12021. (let ((match0 match)
  12022. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  12023. minus tag mm
  12024. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  12025. orterms term orlist re-p str-p level-p level-op time-p
  12026. prop-p pn pv po gv rest)
  12027. (if (string-match "/+" match)
  12028. ;; match contains also a todo-matching request
  12029. (progn
  12030. (setq tagsmatch (substring match 0 (match-beginning 0))
  12031. todomatch (substring match (match-end 0)))
  12032. (if (string-match "^!" todomatch)
  12033. (setq todo-only t todomatch (substring todomatch 1)))
  12034. (if (string-match "^\\s-*$" todomatch)
  12035. (setq todomatch nil)))
  12036. ;; only matching tags
  12037. (setq tagsmatch match todomatch nil))
  12038. ;; Make the tags matcher
  12039. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  12040. (setq tagsmatcher t)
  12041. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  12042. (while (setq term (pop orterms))
  12043. (while (and (equal (substring term -1) "\\") orterms)
  12044. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  12045. (while (string-match re term)
  12046. (setq rest (substring term (match-end 0))
  12047. minus (and (match-end 1)
  12048. (equal (match-string 1 term) "-"))
  12049. tag (save-match-data (replace-regexp-in-string
  12050. "\\\\-" "-"
  12051. (match-string 2 term)))
  12052. re-p (equal (string-to-char tag) ?{)
  12053. level-p (match-end 4)
  12054. prop-p (match-end 5)
  12055. mm (cond
  12056. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  12057. (level-p
  12058. (setq level-op (org-op-to-function (match-string 3 term)))
  12059. `(,level-op level ,(string-to-number
  12060. (match-string 4 term))))
  12061. (prop-p
  12062. (setq pn (match-string 5 term)
  12063. po (match-string 6 term)
  12064. pv (match-string 7 term)
  12065. re-p (equal (string-to-char pv) ?{)
  12066. str-p (equal (string-to-char pv) ?\")
  12067. time-p (save-match-data
  12068. (string-match "^\"[[<].*[]>]\"$" pv))
  12069. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  12070. (if time-p (setq pv (org-matcher-time pv)))
  12071. (setq po (org-op-to-function po (if time-p 'time str-p)))
  12072. (cond
  12073. ((equal pn "CATEGORY")
  12074. (setq gv '(get-text-property (point) 'org-category)))
  12075. ((equal pn "TODO")
  12076. (setq gv 'todo))
  12077. (t
  12078. (setq gv `(org-cached-entry-get nil ,pn))))
  12079. (if re-p
  12080. (if (eq po 'org<>)
  12081. `(not (string-match ,pv (or ,gv "")))
  12082. `(string-match ,pv (or ,gv "")))
  12083. (if str-p
  12084. `(,po (or ,gv "") ,pv)
  12085. `(,po (string-to-number (or ,gv ""))
  12086. ,(string-to-number pv) ))))
  12087. (t `(member ,tag tags-list)))
  12088. mm (if minus (list 'not mm) mm)
  12089. term rest)
  12090. (push mm tagsmatcher))
  12091. (push (if (> (length tagsmatcher) 1)
  12092. (cons 'and tagsmatcher)
  12093. (car tagsmatcher))
  12094. orlist)
  12095. (setq tagsmatcher nil))
  12096. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  12097. (setq tagsmatcher
  12098. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  12099. ;; Make the todo matcher
  12100. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  12101. (setq todomatcher t)
  12102. (setq orterms (org-split-string todomatch "|") orlist nil)
  12103. (while (setq term (pop orterms))
  12104. (while (string-match re term)
  12105. (setq minus (and (match-end 1)
  12106. (equal (match-string 1 term) "-"))
  12107. kwd (match-string 2 term)
  12108. re-p (equal (string-to-char kwd) ?{)
  12109. term (substring term (match-end 0))
  12110. mm (if re-p
  12111. `(string-match ,(substring kwd 1 -1) todo)
  12112. (list 'equal 'todo kwd))
  12113. mm (if minus (list 'not mm) mm))
  12114. (push mm todomatcher))
  12115. (push (if (> (length todomatcher) 1)
  12116. (cons 'and todomatcher)
  12117. (car todomatcher))
  12118. orlist)
  12119. (setq todomatcher nil))
  12120. (setq todomatcher (if (> (length orlist) 1)
  12121. (cons 'or orlist) (car orlist))))
  12122. ;; Return the string and lisp forms of the matcher
  12123. (setq matcher (if todomatcher
  12124. (list 'and tagsmatcher todomatcher)
  12125. tagsmatcher))
  12126. (when todo-only
  12127. (setq matcher (list 'and '(member todo org-not-done-keywords)
  12128. matcher)))
  12129. (cons match0 matcher)))
  12130. (defun org-op-to-function (op &optional stringp)
  12131. "Turn an operator into the appropriate function."
  12132. (setq op
  12133. (cond
  12134. ((equal op "<" ) '(< string< org-time<))
  12135. ((equal op ">" ) '(> org-string> org-time>))
  12136. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  12137. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  12138. ((member op '("=" "==")) '(= string= org-time=))
  12139. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  12140. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  12141. (defun org<> (a b) (not (= a b)))
  12142. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  12143. (defun org-string>= (a b) (not (string< a b)))
  12144. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  12145. (defun org-string<> (a b) (not (string= a b)))
  12146. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  12147. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  12148. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  12149. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  12150. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  12151. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  12152. (defun org-2ft (s)
  12153. "Convert S to a floating point time.
  12154. If S is already a number, just return it. If it is a string, parse
  12155. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  12156. (cond
  12157. ((numberp s) s)
  12158. ((stringp s)
  12159. (condition-case nil
  12160. (float-time (apply 'encode-time (org-parse-time-string s)))
  12161. (error 0.)))
  12162. (t 0.)))
  12163. (defun org-time-today ()
  12164. "Time in seconds today at 0:00.
  12165. Returns the float number of seconds since the beginning of the
  12166. epoch to the beginning of today (00:00)."
  12167. (float-time (apply 'encode-time
  12168. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  12169. (defun org-matcher-time (s)
  12170. "Interpret a time comparison value."
  12171. (save-match-data
  12172. (cond
  12173. ((string= s "<now>") (float-time))
  12174. ((string= s "<today>") (org-time-today))
  12175. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  12176. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  12177. ((string-match "^<\\([-+][0-9]+\\)\\([hdwmy]\\)>$" s)
  12178. (+ (org-time-today)
  12179. (* (string-to-number (match-string 1 s))
  12180. (cdr (assoc (match-string 2 s)
  12181. '(("d" . 86400.0) ("w" . 604800.0)
  12182. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  12183. (t (org-2ft s)))))
  12184. (defun org-match-any-p (re list)
  12185. "Does re match any element of list?"
  12186. (setq list (mapcar (lambda (x) (string-match re x)) list))
  12187. (delq nil list))
  12188. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  12189. (defvar org-tags-overlay (make-overlay 1 1))
  12190. (org-detach-overlay org-tags-overlay)
  12191. (defun org-get-local-tags-at (&optional pos)
  12192. "Get a list of tags defined in the current headline."
  12193. (org-get-tags-at pos 'local))
  12194. (defun org-get-local-tags ()
  12195. "Get a list of tags defined in the current headline."
  12196. (org-get-tags-at nil 'local))
  12197. (defun org-get-tags-at (&optional pos local)
  12198. "Get a list of all headline tags applicable at POS.
  12199. POS defaults to point. If tags are inherited, the list contains
  12200. the targets in the same sequence as the headlines appear, i.e.
  12201. the tags of the current headline come last.
  12202. When LOCAL is non-nil, only return tags from the current headline,
  12203. ignore inherited ones."
  12204. (interactive)
  12205. (if (and org-trust-scanner-tags
  12206. (or (not pos) (equal pos (point)))
  12207. (not local))
  12208. org-scanner-tags
  12209. (let (tags ltags lastpos parent)
  12210. (save-excursion
  12211. (save-restriction
  12212. (widen)
  12213. (goto-char (or pos (point)))
  12214. (save-match-data
  12215. (catch 'done
  12216. (condition-case nil
  12217. (progn
  12218. (org-back-to-heading t)
  12219. (while (not (equal lastpos (point)))
  12220. (setq lastpos (point))
  12221. (when (looking-at
  12222. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  12223. (setq ltags (org-split-string
  12224. (org-match-string-no-properties 1) ":"))
  12225. (when parent
  12226. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  12227. (setq tags (append
  12228. (if parent
  12229. (org-remove-uninherited-tags ltags)
  12230. ltags)
  12231. tags)))
  12232. (or org-use-tag-inheritance (throw 'done t))
  12233. (if local (throw 'done t))
  12234. (or (org-up-heading-safe) (error nil))
  12235. (setq parent t)))
  12236. (error nil)))))
  12237. (if local
  12238. tags
  12239. (append (org-remove-uninherited-tags org-file-tags) tags))))))
  12240. (defun org-add-prop-inherited (s)
  12241. (add-text-properties 0 (length s) '(inherited t) s)
  12242. s)
  12243. (defun org-toggle-tag (tag &optional onoff)
  12244. "Toggle the tag TAG for the current line.
  12245. If ONOFF is `on' or `off', don't toggle but set to this state."
  12246. (let (res current)
  12247. (save-excursion
  12248. (org-back-to-heading t)
  12249. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  12250. (point-at-eol) t)
  12251. (progn
  12252. (setq current (match-string 1))
  12253. (replace-match ""))
  12254. (setq current ""))
  12255. (setq current (nreverse (org-split-string current ":")))
  12256. (cond
  12257. ((eq onoff 'on)
  12258. (setq res t)
  12259. (or (member tag current) (push tag current)))
  12260. ((eq onoff 'off)
  12261. (or (not (member tag current)) (setq current (delete tag current))))
  12262. (t (if (member tag current)
  12263. (setq current (delete tag current))
  12264. (setq res t)
  12265. (push tag current))))
  12266. (end-of-line 1)
  12267. (if current
  12268. (progn
  12269. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  12270. (org-set-tags nil t))
  12271. (delete-horizontal-space))
  12272. (run-hooks 'org-after-tags-change-hook))
  12273. res))
  12274. (defun org-align-tags-here (to-col)
  12275. ;; Assumes that this is a headline
  12276. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  12277. (beginning-of-line 1)
  12278. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12279. (< pos (match-beginning 2)))
  12280. (progn
  12281. (setq tags-l (- (match-end 2) (match-beginning 2)))
  12282. (goto-char (match-beginning 1))
  12283. (insert " ")
  12284. (delete-region (point) (1+ (match-beginning 2)))
  12285. (setq ncol (max (current-column)
  12286. (1+ col)
  12287. (if (> to-col 0)
  12288. to-col
  12289. (- (abs to-col) tags-l))))
  12290. (setq p (point))
  12291. (insert (make-string (- ncol (current-column)) ?\ ))
  12292. (setq ncol (current-column))
  12293. (when indent-tabs-mode (tabify p (point-at-eol)))
  12294. (org-move-to-column (min ncol col) t))
  12295. (goto-char pos))))
  12296. (defun org-set-tags-command (&optional arg just-align)
  12297. "Call the set-tags command for the current entry."
  12298. (interactive "P")
  12299. (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
  12300. (org-set-tags arg just-align)
  12301. (save-excursion
  12302. (org-back-to-heading t)
  12303. (org-set-tags arg just-align))))
  12304. (defun org-set-tags-to (data)
  12305. "Set the tags of the current entry to DATA, replacing the current tags.
  12306. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  12307. If DATA is nil or the empty string, any tags will be removed."
  12308. (interactive "sTags: ")
  12309. (setq data
  12310. (cond
  12311. ((eq data nil) "")
  12312. ((equal data "") "")
  12313. ((stringp data)
  12314. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  12315. ":"))
  12316. ((listp data)
  12317. (concat ":" (mapconcat 'identity data ":") ":"))
  12318. (t nil)))
  12319. (when data
  12320. (save-excursion
  12321. (org-back-to-heading t)
  12322. (when (looking-at org-complex-heading-regexp)
  12323. (if (match-end 5)
  12324. (progn
  12325. (goto-char (match-beginning 5))
  12326. (insert data)
  12327. (delete-region (point) (point-at-eol))
  12328. (org-set-tags nil 'align))
  12329. (goto-char (point-at-eol))
  12330. (insert " " data)
  12331. (org-set-tags nil 'align)))
  12332. (beginning-of-line 1)
  12333. (if (looking-at ".*?\\([ \t]+\\)$")
  12334. (delete-region (match-beginning 1) (match-end 1))))))
  12335. (defun org-align-all-tags ()
  12336. "Align the tags i all headings."
  12337. (interactive)
  12338. (save-excursion
  12339. (or (ignore-errors (org-back-to-heading t))
  12340. (outline-next-heading))
  12341. (if (org-at-heading-p)
  12342. (org-set-tags t)
  12343. (message "No headings"))))
  12344. (defvar org-indent-indentation-per-level)
  12345. (defun org-set-tags (&optional arg just-align)
  12346. "Set the tags for the current headline.
  12347. With prefix ARG, realign all tags in headings in the current buffer."
  12348. (interactive "P")
  12349. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  12350. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  12351. 'region-start-level 'region))
  12352. org-loop-over-headlines-in-active-region)
  12353. (org-map-entries
  12354. ;; We don't use ARG and JUST-ALIGN here these args are not
  12355. ;; useful when looping over headlines
  12356. `(org-set-tags)
  12357. org-loop-over-headlines-in-active-region
  12358. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  12359. (let* ((re org-outline-regexp-bol)
  12360. (current (unless arg (org-get-tags-string)))
  12361. (col (current-column))
  12362. (org-setting-tags t)
  12363. table current-tags inherited-tags ; computed below when needed
  12364. tags p0 c0 c1 rpl di tc level)
  12365. (if arg
  12366. (save-excursion
  12367. (goto-char (point-min))
  12368. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  12369. (while (re-search-forward re nil t)
  12370. (org-set-tags nil t)
  12371. (end-of-line 1)))
  12372. (message "All tags realigned to column %d" org-tags-column))
  12373. (if just-align
  12374. (setq tags current)
  12375. ;; Get a new set of tags from the user
  12376. (save-excursion
  12377. (setq table (append org-tag-persistent-alist
  12378. (or org-tag-alist (org-get-buffer-tags))
  12379. (and
  12380. org-complete-tags-always-offer-all-agenda-tags
  12381. (org-global-tags-completion-table
  12382. (org-agenda-files))))
  12383. org-last-tags-completion-table table
  12384. current-tags (org-split-string current ":")
  12385. inherited-tags (nreverse
  12386. (nthcdr (length current-tags)
  12387. (nreverse (org-get-tags-at))))
  12388. tags
  12389. (if (or (eq t org-use-fast-tag-selection)
  12390. (and org-use-fast-tag-selection
  12391. (delq nil (mapcar 'cdr table))))
  12392. (org-fast-tag-selection
  12393. current-tags inherited-tags table
  12394. (if org-fast-tag-selection-include-todo
  12395. org-todo-key-alist))
  12396. (let ((org-add-colon-after-tag-completion (< 1 (length table))))
  12397. (org-trim
  12398. (org-icompleting-read "Tags: "
  12399. 'org-tags-completion-function
  12400. nil nil current 'org-tags-history))))))
  12401. (while (string-match "[-+&]+" tags)
  12402. ;; No boolean logic, just a list
  12403. (setq tags (replace-match ":" t t tags))))
  12404. (setq tags (replace-regexp-in-string "[,]" ":" tags))
  12405. (if org-tags-sort-function
  12406. (setq tags (mapconcat 'identity
  12407. (sort (org-split-string
  12408. tags (org-re "[^[:alnum:]_@#%]+"))
  12409. org-tags-sort-function) ":")))
  12410. (if (string-match "\\`[\t ]*\\'" tags)
  12411. (setq tags "")
  12412. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  12413. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  12414. ;; Insert new tags at the correct column
  12415. (beginning-of-line 1)
  12416. (setq level (or (and (looking-at org-outline-regexp)
  12417. (- (match-end 0) (point) 1))
  12418. 1))
  12419. (cond
  12420. ((and (equal current "") (equal tags "")))
  12421. ((re-search-forward
  12422. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  12423. (point-at-eol) t)
  12424. (if (equal tags "")
  12425. (setq rpl "")
  12426. (goto-char (match-beginning 0))
  12427. (setq c0 (current-column)
  12428. ;; compute offset for the case of org-indent-mode active
  12429. di (if org-indent-mode
  12430. (* (1- org-indent-indentation-per-level) (1- level))
  12431. 0)
  12432. p0 (if (equal (char-before) ?*) (1+ (point)) (point))
  12433. tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
  12434. c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (length tags))))
  12435. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  12436. (replace-match rpl t t)
  12437. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  12438. tags)
  12439. (t (error "Tags alignment failed")))
  12440. (org-move-to-column col)
  12441. (unless just-align
  12442. (run-hooks 'org-after-tags-change-hook))))))
  12443. (defun org-change-tag-in-region (beg end tag off)
  12444. "Add or remove TAG for each entry in the region.
  12445. This works in the agenda, and also in an org-mode buffer."
  12446. (interactive
  12447. (list (region-beginning) (region-end)
  12448. (let ((org-last-tags-completion-table
  12449. (if (derived-mode-p 'org-mode)
  12450. (org-get-buffer-tags)
  12451. (org-global-tags-completion-table))))
  12452. (org-icompleting-read
  12453. "Tag: " 'org-tags-completion-function nil nil nil
  12454. 'org-tags-history))
  12455. (progn
  12456. (message "[s]et or [r]emove? ")
  12457. (equal (read-char-exclusive) ?r))))
  12458. (if (fboundp 'deactivate-mark) (deactivate-mark))
  12459. (let ((agendap (equal major-mode 'org-agenda-mode))
  12460. l1 l2 m buf pos newhead (cnt 0))
  12461. (goto-char end)
  12462. (setq l2 (1- (org-current-line)))
  12463. (goto-char beg)
  12464. (setq l1 (org-current-line))
  12465. (loop for l from l1 to l2 do
  12466. (org-goto-line l)
  12467. (setq m (get-text-property (point) 'org-hd-marker))
  12468. (when (or (and (derived-mode-p 'org-mode) (org-at-heading-p))
  12469. (and agendap m))
  12470. (setq buf (if agendap (marker-buffer m) (current-buffer))
  12471. pos (if agendap m (point)))
  12472. (with-current-buffer buf
  12473. (save-excursion
  12474. (save-restriction
  12475. (goto-char pos)
  12476. (setq cnt (1+ cnt))
  12477. (org-toggle-tag tag (if off 'off 'on))
  12478. (setq newhead (org-get-heading)))))
  12479. (and agendap (org-agenda-change-all-lines newhead m))))
  12480. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  12481. (defun org-tags-completion-function (string predicate &optional flag)
  12482. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  12483. (confirm (lambda (x) (stringp (car x)))))
  12484. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  12485. (setq s1 (match-string 1 string)
  12486. s2 (match-string 2 string))
  12487. (setq s1 "" s2 string))
  12488. (cond
  12489. ((eq flag nil)
  12490. ;; try completion
  12491. (setq rtn (try-completion s2 ctable confirm))
  12492. (if (stringp rtn)
  12493. (setq rtn
  12494. (concat s1 s2 (substring rtn (length s2))
  12495. (if (and org-add-colon-after-tag-completion
  12496. (assoc rtn ctable))
  12497. ":" ""))))
  12498. rtn)
  12499. ((eq flag t)
  12500. ;; all-completions
  12501. (all-completions s2 ctable confirm)
  12502. )
  12503. ((eq flag 'lambda)
  12504. ;; exact match?
  12505. (assoc s2 ctable)))
  12506. ))
  12507. (defun org-fast-tag-insert (kwd tags face &optional end)
  12508. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  12509. (insert (format "%-12s" (concat kwd ":"))
  12510. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  12511. (or end "")))
  12512. (defun org-fast-tag-show-exit (flag)
  12513. (save-excursion
  12514. (org-goto-line 3)
  12515. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  12516. (replace-match ""))
  12517. (when flag
  12518. (end-of-line 1)
  12519. (org-move-to-column (- (window-width) 19) t)
  12520. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  12521. (defun org-set-current-tags-overlay (current prefix)
  12522. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  12523. (if (featurep 'xemacs)
  12524. (org-overlay-display org-tags-overlay (concat prefix s)
  12525. 'secondary-selection)
  12526. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  12527. (org-overlay-display org-tags-overlay (concat prefix s)))))
  12528. (defvar org-last-tag-selection-key nil)
  12529. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  12530. "Fast tag selection with single keys.
  12531. CURRENT is the current list of tags in the headline, INHERITED is the
  12532. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  12533. possibly with grouping information. TODO-TABLE is a similar table with
  12534. TODO keywords, should these have keys assigned to them.
  12535. If the keys are nil, a-z are automatically assigned.
  12536. Returns the new tags string, or nil to not change the current settings."
  12537. (let* ((fulltable (append table todo-table))
  12538. (maxlen (apply 'max (mapcar
  12539. (lambda (x)
  12540. (if (stringp (car x)) (string-width (car x)) 0))
  12541. fulltable)))
  12542. (buf (current-buffer))
  12543. (expert (eq org-fast-tag-selection-single-key 'expert))
  12544. (buffer-tags nil)
  12545. (fwidth (+ maxlen 3 1 3))
  12546. (ncol (/ (- (window-width) 4) fwidth))
  12547. (i-face 'org-done)
  12548. (c-face 'org-todo)
  12549. tg cnt e c char c1 c2 ntable tbl rtn
  12550. ov-start ov-end ov-prefix
  12551. (exit-after-next org-fast-tag-selection-single-key)
  12552. (done-keywords org-done-keywords)
  12553. groups ingroup)
  12554. (save-excursion
  12555. (beginning-of-line 1)
  12556. (if (looking-at
  12557. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12558. (setq ov-start (match-beginning 1)
  12559. ov-end (match-end 1)
  12560. ov-prefix "")
  12561. (setq ov-start (1- (point-at-eol))
  12562. ov-end (1+ ov-start))
  12563. (skip-chars-forward "^\n\r")
  12564. (setq ov-prefix
  12565. (concat
  12566. (buffer-substring (1- (point)) (point))
  12567. (if (> (current-column) org-tags-column)
  12568. " "
  12569. (make-string (- org-tags-column (current-column)) ?\ ))))))
  12570. (move-overlay org-tags-overlay ov-start ov-end)
  12571. (save-window-excursion
  12572. (if expert
  12573. (set-buffer (get-buffer-create " *Org tags*"))
  12574. (delete-other-windows)
  12575. (split-window-vertically)
  12576. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  12577. (erase-buffer)
  12578. (org-set-local 'org-done-keywords done-keywords)
  12579. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  12580. (org-fast-tag-insert "Current" current c-face "\n\n")
  12581. (org-fast-tag-show-exit exit-after-next)
  12582. (org-set-current-tags-overlay current ov-prefix)
  12583. (setq tbl fulltable char ?a cnt 0)
  12584. (while (setq e (pop tbl))
  12585. (cond
  12586. ((equal (car e) :startgroup)
  12587. (push '() groups) (setq ingroup t)
  12588. (when (not (= cnt 0))
  12589. (setq cnt 0)
  12590. (insert "\n"))
  12591. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  12592. ((equal (car e) :endgroup)
  12593. (setq ingroup nil cnt 0)
  12594. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  12595. ((equal e '(:newline))
  12596. (when (not (= cnt 0))
  12597. (setq cnt 0)
  12598. (insert "\n")
  12599. (setq e (car tbl))
  12600. (while (equal (car tbl) '(:newline))
  12601. (insert "\n")
  12602. (setq tbl (cdr tbl)))))
  12603. (t
  12604. (setq tg (copy-sequence (car e)) c2 nil)
  12605. (if (cdr e)
  12606. (setq c (cdr e))
  12607. ;; automatically assign a character.
  12608. (setq c1 (string-to-char
  12609. (downcase (substring
  12610. tg (if (= (string-to-char tg) ?@) 1 0)))))
  12611. (if (or (rassoc c1 ntable) (rassoc c1 table))
  12612. (while (or (rassoc char ntable) (rassoc char table))
  12613. (setq char (1+ char)))
  12614. (setq c2 c1))
  12615. (setq c (or c2 char)))
  12616. (if ingroup (push tg (car groups)))
  12617. (setq tg (org-add-props tg nil 'face
  12618. (cond
  12619. ((not (assoc tg table))
  12620. (org-get-todo-face tg))
  12621. ((member tg current) c-face)
  12622. ((member tg inherited) i-face)
  12623. (t nil))))
  12624. (if (and (= cnt 0) (not ingroup)) (insert " "))
  12625. (insert "[" c "] " tg (make-string
  12626. (- fwidth 4 (length tg)) ?\ ))
  12627. (push (cons tg c) ntable)
  12628. (when (= (setq cnt (1+ cnt)) ncol)
  12629. (insert "\n")
  12630. (if ingroup (insert " "))
  12631. (setq cnt 0)))))
  12632. (setq ntable (nreverse ntable))
  12633. (insert "\n")
  12634. (goto-char (point-min))
  12635. (if (not expert) (org-fit-window-to-buffer))
  12636. (setq rtn
  12637. (catch 'exit
  12638. (while t
  12639. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  12640. (if (not groups) "no " "")
  12641. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  12642. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  12643. (setq org-last-tag-selection-key c)
  12644. (cond
  12645. ((= c ?\r) (throw 'exit t))
  12646. ((= c ?!)
  12647. (setq groups (not groups))
  12648. (goto-char (point-min))
  12649. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  12650. ((= c ?\C-c)
  12651. (if (not expert)
  12652. (org-fast-tag-show-exit
  12653. (setq exit-after-next (not exit-after-next)))
  12654. (setq expert nil)
  12655. (delete-other-windows)
  12656. (set-window-buffer (split-window-vertically) " *Org tags*")
  12657. (org-switch-to-buffer-other-window " *Org tags*")
  12658. (org-fit-window-to-buffer)))
  12659. ((or (= c ?\C-g)
  12660. (and (= c ?q) (not (rassoc c ntable))))
  12661. (org-detach-overlay org-tags-overlay)
  12662. (setq quit-flag t))
  12663. ((= c ?\ )
  12664. (setq current nil)
  12665. (if exit-after-next (setq exit-after-next 'now)))
  12666. ((= c ?\t)
  12667. (condition-case nil
  12668. (setq tg (org-icompleting-read
  12669. "Tag: "
  12670. (or buffer-tags
  12671. (with-current-buffer buf
  12672. (org-get-buffer-tags)))))
  12673. (quit (setq tg "")))
  12674. (when (string-match "\\S-" tg)
  12675. (add-to-list 'buffer-tags (list tg))
  12676. (if (member tg current)
  12677. (setq current (delete tg current))
  12678. (push tg current)))
  12679. (if exit-after-next (setq exit-after-next 'now)))
  12680. ((setq e (rassoc c todo-table) tg (car e))
  12681. (with-current-buffer buf
  12682. (save-excursion (org-todo tg)))
  12683. (if exit-after-next (setq exit-after-next 'now)))
  12684. ((setq e (rassoc c ntable) tg (car e))
  12685. (if (member tg current)
  12686. (setq current (delete tg current))
  12687. (loop for g in groups do
  12688. (if (member tg g)
  12689. (mapc (lambda (x)
  12690. (setq current (delete x current)))
  12691. g)))
  12692. (push tg current))
  12693. (if exit-after-next (setq exit-after-next 'now))))
  12694. ;; Create a sorted list
  12695. (setq current
  12696. (sort current
  12697. (lambda (a b)
  12698. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  12699. (if (eq exit-after-next 'now) (throw 'exit t))
  12700. (goto-char (point-min))
  12701. (beginning-of-line 2)
  12702. (delete-region (point) (point-at-eol))
  12703. (org-fast-tag-insert "Current" current c-face)
  12704. (org-set-current-tags-overlay current ov-prefix)
  12705. (while (re-search-forward
  12706. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  12707. (setq tg (match-string 1))
  12708. (add-text-properties
  12709. (match-beginning 1) (match-end 1)
  12710. (list 'face
  12711. (cond
  12712. ((member tg current) c-face)
  12713. ((member tg inherited) i-face)
  12714. (t (get-text-property (match-beginning 1) 'face))))))
  12715. (goto-char (point-min)))))
  12716. (org-detach-overlay org-tags-overlay)
  12717. (if rtn
  12718. (mapconcat 'identity current ":")
  12719. nil))))
  12720. (defun org-get-tags-string ()
  12721. "Get the TAGS string in the current headline."
  12722. (unless (org-at-heading-p t)
  12723. (error "Not on a heading"))
  12724. (save-excursion
  12725. (beginning-of-line 1)
  12726. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12727. (org-match-string-no-properties 1)
  12728. "")))
  12729. (defun org-get-tags ()
  12730. "Get the list of tags specified in the current headline."
  12731. (org-split-string (org-get-tags-string) ":"))
  12732. (defun org-get-buffer-tags ()
  12733. "Get a table of all tags used in the buffer, for completion."
  12734. (let (tags)
  12735. (save-excursion
  12736. (goto-char (point-min))
  12737. (while (re-search-forward
  12738. (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
  12739. (when (equal (char-after (point-at-bol 0)) ?*)
  12740. (mapc (lambda (x) (add-to-list 'tags x))
  12741. (org-split-string (org-match-string-no-properties 1) ":")))))
  12742. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  12743. (mapcar 'list tags)))
  12744. ;;;; The mapping API
  12745. ;;;###autoload
  12746. (defun org-map-entries (func &optional match scope &rest skip)
  12747. "Call FUNC at each headline selected by MATCH in SCOPE.
  12748. FUNC is a function or a lisp form. The function will be called without
  12749. arguments, with the cursor positioned at the beginning of the headline.
  12750. The return values of all calls to the function will be collected and
  12751. returned as a list.
  12752. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  12753. does not need to preserve point. After evaluation, the cursor will be
  12754. moved to the end of the line (presumably of the headline of the
  12755. processed entry) and search continues from there. Under some
  12756. circumstances, this may not produce the wanted results. For example,
  12757. if you have removed (e.g. archived) the current (sub)tree it could
  12758. mean that the next entry will be skipped entirely. In such cases, you
  12759. can specify the position from where search should continue by making
  12760. FUNC set the variable `org-map-continue-from' to the desired buffer
  12761. position.
  12762. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  12763. Only headlines that are matched by this query will be considered during
  12764. the iteration. When MATCH is nil or t, all headlines will be
  12765. visited by the iteration.
  12766. SCOPE determines the scope of this command. It can be any of:
  12767. nil The current buffer, respecting the restriction if any
  12768. tree The subtree started with the entry at point
  12769. region The entries within the active region, if any
  12770. region-start-level
  12771. The entries within the active region, but only those at
  12772. the same level than the first one.
  12773. file The current buffer, without restriction
  12774. file-with-archives
  12775. The current buffer, and any archives associated with it
  12776. agenda All agenda files
  12777. agenda-with-archives
  12778. All agenda files with any archive files associated with them
  12779. \(file1 file2 ...)
  12780. If this is a list, all files in the list will be scanned
  12781. The remaining args are treated as settings for the skipping facilities of
  12782. the scanner. The following items can be given here:
  12783. archive skip trees with the archive tag.
  12784. comment skip trees with the COMMENT keyword
  12785. function or Emacs Lisp form:
  12786. will be used as value for `org-agenda-skip-function', so whenever
  12787. the function returns t, FUNC will not be called for that
  12788. entry and search will continue from the point where the
  12789. function leaves it.
  12790. If your function needs to retrieve the tags including inherited tags
  12791. at the *current* entry, you can use the value of the variable
  12792. `org-scanner-tags' which will be much faster than getting the value
  12793. with `org-get-tags-at'. If your function gets properties with
  12794. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  12795. to t around the call to `org-entry-properties' to get the same speedup.
  12796. Note that if your function moves around to retrieve tags and properties at
  12797. a *different* entry, you cannot use these techniques."
  12798. (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
  12799. (not (org-region-active-p)))
  12800. (let* ((org-agenda-archives-mode nil) ; just to make sure
  12801. (org-agenda-skip-archived-trees (memq 'archive skip))
  12802. (org-agenda-skip-comment-trees (memq 'comment skip))
  12803. (org-agenda-skip-function
  12804. (car (org-delete-all '(comment archive) skip)))
  12805. (org-tags-match-list-sublevels t)
  12806. (start-level (eq scope 'region-start-level))
  12807. matcher file res
  12808. org-todo-keywords-for-agenda
  12809. org-done-keywords-for-agenda
  12810. org-todo-keyword-alist-for-agenda
  12811. org-drawers-for-agenda
  12812. org-tag-alist-for-agenda
  12813. todo-only)
  12814. (cond
  12815. ((eq match t) (setq matcher t))
  12816. ((eq match nil) (setq matcher t))
  12817. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  12818. (save-excursion
  12819. (save-restriction
  12820. (cond ((eq scope 'tree)
  12821. (org-back-to-heading t)
  12822. (org-narrow-to-subtree)
  12823. (setq scope nil))
  12824. ((and (or (eq scope 'region) (eq scope 'region-start-level))
  12825. (org-region-active-p))
  12826. ;; If needed, set start-level to a string like "2"
  12827. (when start-level
  12828. (save-excursion
  12829. (goto-char (region-beginning))
  12830. (unless (org-at-heading-p) (outline-next-heading))
  12831. (setq start-level (org-current-level))))
  12832. (narrow-to-region (region-beginning)
  12833. (save-excursion
  12834. (goto-char (region-end))
  12835. (unless (and (bolp) (org-at-heading-p))
  12836. (outline-next-heading))
  12837. (point)))
  12838. (setq scope nil)))
  12839. (if (not scope)
  12840. (progn
  12841. (org-prepare-agenda-buffers
  12842. (list (buffer-file-name (current-buffer))))
  12843. (setq res (org-scan-tags func matcher todo-only start-level)))
  12844. ;; Get the right scope
  12845. (cond
  12846. ((and scope (listp scope) (symbolp (car scope)))
  12847. (setq scope (eval scope)))
  12848. ((eq scope 'agenda)
  12849. (setq scope (org-agenda-files t)))
  12850. ((eq scope 'agenda-with-archives)
  12851. (setq scope (org-agenda-files t))
  12852. (setq scope (org-add-archive-files scope)))
  12853. ((eq scope 'file)
  12854. (setq scope (list (buffer-file-name))))
  12855. ((eq scope 'file-with-archives)
  12856. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  12857. (org-prepare-agenda-buffers scope)
  12858. (while (setq file (pop scope))
  12859. (with-current-buffer (org-find-base-buffer-visiting file)
  12860. (save-excursion
  12861. (save-restriction
  12862. (widen)
  12863. (goto-char (point-min))
  12864. (setq res (append res (org-scan-tags func matcher todo-only))))))))))
  12865. res)))
  12866. ;;;; Properties
  12867. ;;; Setting and retrieving properties
  12868. (defconst org-special-properties
  12869. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  12870. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED" "FILE" "CLOCKSUM")
  12871. "The special properties valid in Org-mode.
  12872. These are properties that are not defined in the property drawer,
  12873. but in some other way.")
  12874. (defconst org-default-properties
  12875. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  12876. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  12877. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  12878. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  12879. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  12880. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  12881. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  12882. "Some properties that are used by Org-mode for various purposes.
  12883. Being in this list makes sure that they are offered for completion.")
  12884. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  12885. "Regular expression matching the first line of a property drawer.")
  12886. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  12887. "Regular expression matching the last line of a property drawer.")
  12888. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  12889. "Regular expression matching the first line of a property drawer.")
  12890. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  12891. "Regular expression matching the first line of a property drawer.")
  12892. (defconst org-property-drawer-re
  12893. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  12894. org-property-end-re "\\)\n?")
  12895. "Matches an entire property drawer.")
  12896. (defconst org-clock-drawer-re
  12897. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  12898. org-property-end-re "\\)\n?")
  12899. "Matches an entire clock drawer.")
  12900. (defsubst org-re-property (property)
  12901. "Return a regexp matching a PROPERTY line.
  12902. Match group 1 will be set to the value."
  12903. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)"))
  12904. (defsubst org-re-property-keyword (property)
  12905. "Return a regexp matching a PROPERTY line, possibly with no
  12906. value for the property."
  12907. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)?"))
  12908. (defun org-property-action ()
  12909. "Do an action on properties."
  12910. (interactive)
  12911. (let (c)
  12912. (org-at-property-p)
  12913. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  12914. (setq c (read-char-exclusive))
  12915. (cond
  12916. ((equal c ?s)
  12917. (call-interactively 'org-set-property))
  12918. ((equal c ?d)
  12919. (call-interactively 'org-delete-property))
  12920. ((equal c ?D)
  12921. (call-interactively 'org-delete-property-globally))
  12922. ((equal c ?c)
  12923. (call-interactively 'org-compute-property-at-point))
  12924. (t (error "No such property action %c" c)))))
  12925. (defun org-inc-effort ()
  12926. "Increment the value of the effort property in the current entry."
  12927. (interactive)
  12928. (org-set-effort nil t))
  12929. (defun org-set-effort (&optional value increment)
  12930. "Set the effort property of the current entry.
  12931. With numerical prefix arg, use the nth allowed value, 0 stands for the
  12932. 10th allowed value.
  12933. When INCREMENT is non-nil, set the property to the next allowed value."
  12934. (interactive "P")
  12935. (if (equal value 0) (setq value 10))
  12936. (let* ((completion-ignore-case t)
  12937. (prop org-effort-property)
  12938. (cur (org-entry-get nil prop))
  12939. (allowed (org-property-get-allowed-values nil prop 'table))
  12940. (existing (mapcar 'list (org-property-values prop)))
  12941. rpl
  12942. (val (cond
  12943. ((stringp value) value)
  12944. ((and allowed (integerp value))
  12945. (or (car (nth (1- value) allowed))
  12946. (car (org-last allowed))))
  12947. ((and allowed increment)
  12948. (or (caadr (member (list cur) allowed))
  12949. (error "Allowed effort values are not set")))
  12950. (allowed
  12951. (message "Select 1-9,0, [RET%s]: %s"
  12952. (if cur (concat "=" cur) "")
  12953. (mapconcat 'car allowed " "))
  12954. (setq rpl (read-char-exclusive))
  12955. (if (equal rpl ?\r)
  12956. cur
  12957. (setq rpl (- rpl ?0))
  12958. (if (equal rpl 0) (setq rpl 10))
  12959. (if (and (> rpl 0) (<= rpl (length allowed)))
  12960. (car (nth (1- rpl) allowed))
  12961. (org-completing-read "Effort: " allowed nil))))
  12962. (t
  12963. (let (org-completion-use-ido org-completion-use-iswitchb)
  12964. (org-completing-read
  12965. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  12966. (concat "[" cur "]") "")
  12967. ": ")
  12968. existing nil nil "" nil cur))))))
  12969. (unless (equal (org-entry-get nil prop) val)
  12970. (org-entry-put nil prop val))
  12971. (message "%s is now %s" prop val)))
  12972. (defun org-at-property-p ()
  12973. "Is cursor inside a property drawer?"
  12974. (save-excursion
  12975. (beginning-of-line 1)
  12976. (when (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))
  12977. (save-match-data ;; Used by calling procedures
  12978. (let ((p (point))
  12979. (range (unless (org-before-first-heading-p)
  12980. (org-get-property-block))))
  12981. (and range (<= (car range) p) (< p (cdr range))))))))
  12982. (defun org-get-property-block (&optional beg end force)
  12983. "Return the (beg . end) range of the body of the property drawer.
  12984. BEG and END can be beginning and end of subtree, if not given
  12985. they will be found.
  12986. If the drawer does not exist and FORCE is non-nil, create the drawer."
  12987. (catch 'exit
  12988. (save-excursion
  12989. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  12990. (end (or end (progn (outline-next-heading) (point)))))
  12991. (goto-char beg)
  12992. (if (re-search-forward org-property-start-re end t)
  12993. (setq beg (1+ (match-end 0)))
  12994. (if force
  12995. (save-excursion
  12996. (org-insert-property-drawer)
  12997. (setq end (progn (outline-next-heading) (point))))
  12998. (throw 'exit nil))
  12999. (goto-char beg)
  13000. (if (re-search-forward org-property-start-re end t)
  13001. (setq beg (1+ (match-end 0)))))
  13002. (if (re-search-forward org-property-end-re end t)
  13003. (setq end (match-beginning 0))
  13004. (or force (throw 'exit nil))
  13005. (goto-char beg)
  13006. (setq end beg)
  13007. (org-indent-line)
  13008. (insert ":END:\n"))
  13009. (cons beg end)))))
  13010. (defun org-entry-properties (&optional pom which specific)
  13011. "Get all properties of the entry at point-or-marker POM.
  13012. This includes the TODO keyword, the tags, time strings for deadline,
  13013. scheduled, and clocking, and any additional properties defined in the
  13014. entry. The return value is an alist, keys may occur multiple times
  13015. if the property key was used several times.
  13016. POM may also be nil, in which case the current entry is used.
  13017. If WHICH is nil or `all', get all properties. If WHICH is
  13018. `special' or `standard', only get that subclass. If WHICH
  13019. is a string only get exactly this property. SPECIFIC can be a string, the
  13020. specific property we are interested in. Specifying it can speed
  13021. things up because then unnecessary parsing is avoided."
  13022. (setq which (or which 'all))
  13023. (org-with-point-at pom
  13024. (let ((clockstr (substring org-clock-string 0 -1))
  13025. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
  13026. (case-fold-search nil)
  13027. beg end range props sum-props key key1 value string clocksum)
  13028. (save-excursion
  13029. (when (condition-case nil
  13030. (and (derived-mode-p 'org-mode) (org-back-to-heading t))
  13031. (error nil))
  13032. (setq beg (point))
  13033. (setq sum-props (get-text-property (point) 'org-summaries))
  13034. (setq clocksum (get-text-property (point) :org-clock-minutes))
  13035. (outline-next-heading)
  13036. (setq end (point))
  13037. (when (memq which '(all special))
  13038. ;; Get the special properties, like TODO and tags
  13039. (goto-char beg)
  13040. (when (and (or (not specific) (string= specific "TODO"))
  13041. (looking-at org-todo-line-regexp) (match-end 2))
  13042. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  13043. (when (and (or (not specific) (string= specific "PRIORITY"))
  13044. (looking-at org-priority-regexp))
  13045. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  13046. (when (or (not specific) (string= specific "FILE"))
  13047. (push (cons "FILE" buffer-file-name) props))
  13048. (when (and (or (not specific) (string= specific "TAGS"))
  13049. (setq value (org-get-tags-string))
  13050. (string-match "\\S-" value))
  13051. (push (cons "TAGS" value) props))
  13052. (when (and (or (not specific) (string= specific "ALLTAGS"))
  13053. (setq value (org-get-tags-at)))
  13054. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
  13055. ":"))
  13056. props))
  13057. (when (or (not specific) (string= specific "BLOCKED"))
  13058. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
  13059. (when (or (not specific)
  13060. (member specific
  13061. '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
  13062. "TIMESTAMP" "TIMESTAMP_IA")))
  13063. (catch 'match
  13064. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  13065. (setq key (if (match-end 1)
  13066. (substring (org-match-string-no-properties 1)
  13067. 0 -1))
  13068. string (if (equal key clockstr)
  13069. (org-no-properties
  13070. (org-trim
  13071. (buffer-substring
  13072. (match-beginning 3) (goto-char
  13073. (point-at-eol)))))
  13074. (substring (org-match-string-no-properties 3)
  13075. 1 -1)))
  13076. ;; Get the correct property name from the key. This is
  13077. ;; necessary if the user has configured time keywords.
  13078. (setq key1 (concat key ":"))
  13079. (cond
  13080. ((not key)
  13081. (setq key
  13082. (if (= (char-after (match-beginning 3)) ?\[)
  13083. "TIMESTAMP_IA" "TIMESTAMP")))
  13084. ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
  13085. ((equal key1 org-deadline-string) (setq key "DEADLINE"))
  13086. ((equal key1 org-closed-string) (setq key "CLOSED"))
  13087. ((equal key1 org-clock-string) (setq key "CLOCK")))
  13088. (if (and specific (equal key specific) (not (equal key "CLOCK")))
  13089. (progn
  13090. (push (cons key string) props)
  13091. ;; no need to search further if match is found
  13092. (throw 'match t))
  13093. (when (or (equal key "CLOCK") (not (assoc key props)))
  13094. (push (cons key string) props))))))
  13095. )
  13096. (when (memq which '(all standard))
  13097. ;; Get the standard properties, like :PROP: ...
  13098. (setq range (org-get-property-block beg end))
  13099. (when range
  13100. (goto-char (car range))
  13101. (while (re-search-forward
  13102. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  13103. (cdr range) t)
  13104. (setq key (org-match-string-no-properties 1)
  13105. value (org-trim (or (org-match-string-no-properties 2) "")))
  13106. (unless (member key excluded)
  13107. (push (cons key (or value "")) props)))))
  13108. (if clocksum
  13109. (push (cons "CLOCKSUM"
  13110. (org-columns-number-to-string (/ (float clocksum) 60.)
  13111. 'add_times))
  13112. props))
  13113. (unless (assoc "CATEGORY" props)
  13114. (push (cons "CATEGORY" (org-get-category)) props))
  13115. (append sum-props (nreverse props)))))))
  13116. (defun org-entry-get (pom property &optional inherit literal-nil)
  13117. "Get value of PROPERTY for entry at point-or-marker POM.
  13118. If INHERIT is non-nil and the entry does not have the property,
  13119. then also check higher levels of the hierarchy.
  13120. If INHERIT is the symbol `selective', use inheritance only if the setting
  13121. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  13122. If the property is present but empty, the return value is the empty string.
  13123. If the property is not present at all, nil is returned.
  13124. If LITERAL-NIL is set, return the string value \"nil\" as a string,
  13125. do not interpret it as the list atom nil. This is used for inheritance
  13126. when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
  13127. (org-with-point-at pom
  13128. (if (and inherit (if (eq inherit 'selective)
  13129. (org-property-inherit-p property)
  13130. t))
  13131. (org-entry-get-with-inheritance property literal-nil)
  13132. (if (member property org-special-properties)
  13133. ;; We need a special property. Use `org-entry-properties' to
  13134. ;; retrieve it, but specify the wanted property
  13135. (cdr (assoc property (org-entry-properties nil 'special property)))
  13136. (let ((range (unless (org-before-first-heading-p)
  13137. (org-get-property-block)))
  13138. (props (list (or (assoc property org-file-properties)
  13139. (assoc property org-global-properties)
  13140. (assoc property org-global-properties-fixed))))
  13141. val)
  13142. (org-flet ((ap (key)
  13143. (when (re-search-forward
  13144. (org-re-property key) (cdr range) t)
  13145. (setq props
  13146. (org-update-property-plist
  13147. key
  13148. (if (match-end 1)
  13149. (org-match-string-no-properties 1) "")
  13150. props)))))
  13151. (when (and range (goto-char (car range)))
  13152. (ap property)
  13153. (goto-char (car range))
  13154. (while (ap (concat property "+")))
  13155. (setq val (cdr (assoc property props)))
  13156. (when val (if literal-nil val (org-not-nil val))))))))))
  13157. (defun org-property-or-variable-value (var &optional inherit)
  13158. "Check if there is a property fixing the value of VAR.
  13159. If yes, return this value. If not, return the current value of the variable."
  13160. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  13161. (if (and prop (stringp prop) (string-match "\\S-" prop))
  13162. (read prop)
  13163. (symbol-value var))))
  13164. (defun org-entry-delete (pom property)
  13165. "Delete the property PROPERTY from entry at point-or-marker POM."
  13166. (org-with-point-at pom
  13167. (if (member property org-special-properties)
  13168. nil ; cannot delete these properties.
  13169. (let ((range (org-get-property-block)))
  13170. (if (and range
  13171. (goto-char (car range))
  13172. (re-search-forward
  13173. (org-re-property property)
  13174. (cdr range) t))
  13175. (progn
  13176. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  13177. t)
  13178. nil)))))
  13179. ;; Multi-values properties are properties that contain multiple values
  13180. ;; These values are assumed to be single words, separated by whitespace.
  13181. (defun org-entry-add-to-multivalued-property (pom property value)
  13182. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  13183. (let* ((old (org-entry-get pom property))
  13184. (values (and old (org-split-string old "[ \t]"))))
  13185. (setq value (org-entry-protect-space value))
  13186. (unless (member value values)
  13187. (setq values (cons value values))
  13188. (org-entry-put pom property
  13189. (mapconcat 'identity values " ")))))
  13190. (defun org-entry-remove-from-multivalued-property (pom property value)
  13191. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  13192. (let* ((old (org-entry-get pom property))
  13193. (values (and old (org-split-string old "[ \t]"))))
  13194. (setq value (org-entry-protect-space value))
  13195. (when (member value values)
  13196. (setq values (delete value values))
  13197. (org-entry-put pom property
  13198. (mapconcat 'identity values " ")))))
  13199. (defun org-entry-member-in-multivalued-property (pom property value)
  13200. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  13201. (let* ((old (org-entry-get pom property))
  13202. (values (and old (org-split-string old "[ \t]"))))
  13203. (setq value (org-entry-protect-space value))
  13204. (member value values)))
  13205. (defun org-entry-get-multivalued-property (pom property)
  13206. "Return a list of values in a multivalued property."
  13207. (let* ((value (org-entry-get pom property))
  13208. (values (and value (org-split-string value "[ \t]"))))
  13209. (mapcar 'org-entry-restore-space values)))
  13210. (defun org-entry-put-multivalued-property (pom property &rest values)
  13211. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  13212. VALUES should be a list of strings. Spaces will be protected."
  13213. (org-entry-put pom property
  13214. (mapconcat 'org-entry-protect-space values " "))
  13215. (let* ((value (org-entry-get pom property))
  13216. (values (and value (org-split-string value "[ \t]"))))
  13217. (mapcar 'org-entry-restore-space values)))
  13218. (defun org-entry-protect-space (s)
  13219. "Protect spaces and newline in string S."
  13220. (while (string-match " " s)
  13221. (setq s (replace-match "%20" t t s)))
  13222. (while (string-match "\n" s)
  13223. (setq s (replace-match "%0A" t t s)))
  13224. s)
  13225. (defun org-entry-restore-space (s)
  13226. "Restore spaces and newline in string S."
  13227. (while (string-match "%20" s)
  13228. (setq s (replace-match " " t t s)))
  13229. (while (string-match "%0A" s)
  13230. (setq s (replace-match "\n" t t s)))
  13231. s)
  13232. (defvar org-entry-property-inherited-from (make-marker)
  13233. "Marker pointing to the entry from where a property was inherited.
  13234. Each call to `org-entry-get-with-inheritance' will set this marker to the
  13235. location of the entry where the inheritance search matched. If there was
  13236. no match, the marker will point nowhere.
  13237. Note that also `org-entry-get' calls this function, if the INHERIT flag
  13238. is set.")
  13239. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  13240. "Get entry property, and search higher levels if not present.
  13241. The search will stop at the first ancestor which has the property defined.
  13242. If the value found is \"nil\", return nil to show that the property
  13243. should be considered as undefined (this is the meaning of nil here).
  13244. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  13245. (move-marker org-entry-property-inherited-from nil)
  13246. (let (tmp)
  13247. (unless (org-before-first-heading-p)
  13248. (save-excursion
  13249. (save-restriction
  13250. (widen)
  13251. (catch 'ex
  13252. (while t
  13253. (when (setq tmp (org-entry-get nil property nil 'literal-nil))
  13254. (org-back-to-heading t)
  13255. (move-marker org-entry-property-inherited-from (point))
  13256. (throw 'ex tmp))
  13257. (or (org-up-heading-safe) (throw 'ex nil)))))))
  13258. (setq tmp (or tmp
  13259. (cdr (assoc property org-file-properties))
  13260. (cdr (assoc property org-global-properties))
  13261. (cdr (assoc property org-global-properties-fixed))))
  13262. (if literal-nil tmp (org-not-nil tmp))))
  13263. (defvar org-property-changed-functions nil
  13264. "Hook called when the value of a property has changed.
  13265. Each hook function should accept two arguments, the name of the property
  13266. and the new value.")
  13267. (defun org-entry-put (pom property value)
  13268. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  13269. (org-with-point-at pom
  13270. (org-back-to-heading t)
  13271. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  13272. range)
  13273. (cond
  13274. ((equal property "TODO")
  13275. (when (and (stringp value) (string-match "\\S-" value)
  13276. (not (member value org-todo-keywords-1)))
  13277. (error "\"%s\" is not a valid TODO state" value))
  13278. (if (or (not value)
  13279. (not (string-match "\\S-" value)))
  13280. (setq value 'none))
  13281. (org-todo value)
  13282. (org-set-tags nil 'align))
  13283. ((equal property "PRIORITY")
  13284. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  13285. (string-to-char value) ?\ ))
  13286. (org-set-tags nil 'align))
  13287. ((equal property "SCHEDULED")
  13288. (if (re-search-forward org-scheduled-time-regexp end t)
  13289. (cond
  13290. ((eq value 'earlier) (org-timestamp-change -1 'day))
  13291. ((eq value 'later) (org-timestamp-change 1 'day))
  13292. (t (call-interactively 'org-schedule)))
  13293. (call-interactively 'org-schedule)))
  13294. ((equal property "DEADLINE")
  13295. (if (re-search-forward org-deadline-time-regexp end t)
  13296. (cond
  13297. ((eq value 'earlier) (org-timestamp-change -1 'day))
  13298. ((eq value 'later) (org-timestamp-change 1 'day))
  13299. (t (call-interactively 'org-deadline)))
  13300. (call-interactively 'org-deadline)))
  13301. ((member property org-special-properties)
  13302. (error "The %s property can not yet be set with `org-entry-put'"
  13303. property))
  13304. (t ; a non-special property
  13305. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  13306. (setq range (org-get-property-block beg end 'force))
  13307. (goto-char (car range))
  13308. (if (re-search-forward
  13309. (org-re-property-keyword property) (cdr range) t)
  13310. (progn
  13311. (delete-region (match-beginning 0) (match-end 0))
  13312. (goto-char (match-beginning 0)))
  13313. (goto-char (cdr range))
  13314. (insert "\n")
  13315. (backward-char 1)
  13316. (org-indent-line))
  13317. (insert ":" property ":")
  13318. (and value (insert " " value))
  13319. (org-indent-line)))))
  13320. (run-hook-with-args 'org-property-changed-functions property value)))
  13321. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  13322. "Get all property keys in the current buffer.
  13323. With INCLUDE-SPECIALS, also list the special properties that reflect things
  13324. like tags and TODO state.
  13325. With INCLUDE-DEFAULTS, also include properties that has special meaning
  13326. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING
  13327. and others.
  13328. With INCLUDE-COLUMNS, also include property names given in COLUMN
  13329. formats in the current buffer."
  13330. (let (rtn range cfmt s p)
  13331. (save-excursion
  13332. (save-restriction
  13333. (widen)
  13334. (goto-char (point-min))
  13335. (while (re-search-forward org-property-start-re nil t)
  13336. (setq range (org-get-property-block))
  13337. (goto-char (car range))
  13338. (while (re-search-forward
  13339. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  13340. (cdr range) t)
  13341. (add-to-list 'rtn (org-match-string-no-properties 1)))
  13342. (outline-next-heading))))
  13343. (when include-specials
  13344. (setq rtn (append org-special-properties rtn)))
  13345. (when include-defaults
  13346. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  13347. (add-to-list 'rtn org-effort-property))
  13348. (when include-columns
  13349. (save-excursion
  13350. (save-restriction
  13351. (widen)
  13352. (goto-char (point-min))
  13353. (while (re-search-forward
  13354. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  13355. nil t)
  13356. (setq cfmt (match-string 2) s 0)
  13357. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  13358. cfmt s)
  13359. (setq s (match-end 0)
  13360. p (match-string 1 cfmt))
  13361. (unless (or (equal p "ITEM")
  13362. (member p org-special-properties))
  13363. (add-to-list 'rtn (match-string 1 cfmt))))))))
  13364. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  13365. (defun org-property-values (key)
  13366. "Return a list of all values of property KEY in the current buffer."
  13367. (save-excursion
  13368. (save-restriction
  13369. (widen)
  13370. (goto-char (point-min))
  13371. (let ((re (org-re-property key))
  13372. values)
  13373. (while (re-search-forward re nil t)
  13374. (add-to-list 'values (org-trim (match-string 1))))
  13375. (delete "" values)))))
  13376. (defun org-insert-property-drawer ()
  13377. "Insert a property drawer into the current entry."
  13378. (org-back-to-heading t)
  13379. (looking-at org-outline-regexp)
  13380. (let ((indent (if org-adapt-indentation
  13381. (- (match-end 0) (match-beginning 0))
  13382. 0))
  13383. (beg (point))
  13384. (re (concat "^[ \t]*" org-keyword-time-regexp))
  13385. end hiddenp)
  13386. (outline-next-heading)
  13387. (setq end (point))
  13388. (goto-char beg)
  13389. (while (re-search-forward re end t))
  13390. (setq hiddenp (outline-invisible-p))
  13391. (end-of-line 1)
  13392. (and (equal (char-after) ?\n) (forward-char 1))
  13393. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  13394. (if (member (match-string 1) '("CLOCK:" ":END:"))
  13395. ;; just skip this line
  13396. (beginning-of-line 2)
  13397. ;; Drawer start, find the end
  13398. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  13399. (beginning-of-line 1)))
  13400. (org-skip-over-state-notes)
  13401. (skip-chars-backward " \t\n\r")
  13402. (if (eq (char-before) ?*) (forward-char 1))
  13403. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  13404. (beginning-of-line 0)
  13405. (org-indent-to-column indent)
  13406. (beginning-of-line 2)
  13407. (org-indent-to-column indent)
  13408. (beginning-of-line 0)
  13409. (if hiddenp
  13410. (save-excursion
  13411. (org-back-to-heading t)
  13412. (hide-entry))
  13413. (org-flag-drawer t))))
  13414. (defun org-insert-drawer (&optional arg drawer)
  13415. "Insert a drawer at point.
  13416. Optional argument DRAWER, when non-nil, is a string representing
  13417. drawer's name. Otherwise, the user is prompted for a name.
  13418. If a region is active, insert the drawer around that region
  13419. instead.
  13420. Point is left between drawer's boundaries."
  13421. (interactive "P")
  13422. (let* ((logbook (if (stringp org-log-into-drawer) org-log-into-drawer
  13423. "LOGBOOK"))
  13424. ;; SYSTEM-DRAWERS is a list of drawer names that are used
  13425. ;; internally by Org. They are meant to be inserted
  13426. ;; automatically.
  13427. (system-drawers `("CLOCK" ,logbook "PROPERTIES"))
  13428. ;; Remove system drawers from list. Note: For some reason,
  13429. ;; `org-completing-read' ignores the predicate while
  13430. ;; `completing-read' handles it fine.
  13431. (drawer (if arg "PROPERTIES"
  13432. (or drawer
  13433. (completing-read
  13434. "Drawer: " org-drawers
  13435. (lambda (d) (not (member d system-drawers))))))))
  13436. (cond
  13437. ;; With C-u, fall back on `org-insert-property-drawer'
  13438. (arg (org-insert-property-drawer))
  13439. ;; With an active region, insert a drawer at point.
  13440. ((not (org-region-active-p))
  13441. (progn
  13442. (unless (bolp) (insert "\n"))
  13443. (insert (format ":%s:\n\n:END:\n" drawer))
  13444. (forward-line -2)))
  13445. ;; Otherwise, insert the drawer at point
  13446. (t
  13447. (let ((rbeg (region-beginning))
  13448. (rend (copy-marker (region-end))))
  13449. (unwind-protect
  13450. (progn
  13451. (goto-char rbeg)
  13452. (beginning-of-line)
  13453. (when (save-excursion
  13454. (re-search-forward org-outline-regexp-bol rend t))
  13455. (error "Drawers cannot contain headlines"))
  13456. ;; Position point at the beginning of the first
  13457. ;; non-blank line in region. Insert drawer's opening
  13458. ;; there, then indent it.
  13459. (org-skip-whitespace)
  13460. (beginning-of-line)
  13461. (insert ":" drawer ":\n")
  13462. (forward-line -1)
  13463. (indent-for-tab-command)
  13464. ;; Move point to the beginning of the first blank line
  13465. ;; after the last non-blank line in region. Insert
  13466. ;; drawer's closing, then indent it.
  13467. (goto-char rend)
  13468. (skip-chars-backward " \r\t\n")
  13469. (insert "\n:END:")
  13470. (indent-for-tab-command)
  13471. (unless (eolp) (insert "\n")))
  13472. ;; Clear marker, whatever the outcome of insertion is.
  13473. (set-marker rend nil)))))))
  13474. (defvar org-property-set-functions-alist nil
  13475. "Property set function alist.
  13476. Each entry should have the following format:
  13477. (PROPERTY . READ-FUNCTION)
  13478. The read function will be called with the same argument as
  13479. `org-completing-read'.")
  13480. (defun org-set-property-function (property)
  13481. "Get the function that should be used to set PROPERTY.
  13482. This is computed according to `org-property-set-functions-alist'."
  13483. (or (cdr (assoc property org-property-set-functions-alist))
  13484. 'org-completing-read))
  13485. (defun org-read-property-value (property)
  13486. "Read PROPERTY value from user."
  13487. (let* ((completion-ignore-case t)
  13488. (allowed (org-property-get-allowed-values nil property 'table))
  13489. (cur (org-entry-get nil property))
  13490. (prompt (concat property " value"
  13491. (if (and cur (string-match "\\S-" cur))
  13492. (concat " [" cur "]") "") ": "))
  13493. (set-function (org-set-property-function property))
  13494. (val (if allowed
  13495. (funcall set-function prompt allowed nil
  13496. (not (get-text-property 0 'org-unrestricted
  13497. (caar allowed))))
  13498. (let (org-completion-use-ido org-completion-use-iswitchb)
  13499. (funcall set-function prompt
  13500. (mapcar 'list (org-property-values property))
  13501. nil nil "" nil cur)))))
  13502. (if (equal val "")
  13503. cur
  13504. val)))
  13505. (defvar org-last-set-property nil)
  13506. (defun org-read-property-name ()
  13507. "Read a property name."
  13508. (let* ((completion-ignore-case t)
  13509. (keys (org-buffer-property-keys nil t t))
  13510. (default-prop (or (save-excursion
  13511. (save-match-data
  13512. (beginning-of-line)
  13513. (and (looking-at "^\\s-*:\\([^:\n]+\\):")
  13514. (null (string= (match-string 1) "END"))
  13515. (match-string 1))))
  13516. org-last-set-property))
  13517. (property (org-icompleting-read
  13518. (concat "Property"
  13519. (if default-prop (concat " [" default-prop "]") "")
  13520. ": ")
  13521. (mapcar 'list keys)
  13522. nil nil nil nil
  13523. default-prop
  13524. )))
  13525. (if (member property keys)
  13526. property
  13527. (or (cdr (assoc (downcase property)
  13528. (mapcar (lambda (x) (cons (downcase x) x))
  13529. keys)))
  13530. property))))
  13531. (defun org-set-property (property value)
  13532. "In the current entry, set PROPERTY to VALUE.
  13533. When called interactively, this will prompt for a property name, offering
  13534. completion on existing and default properties. And then it will prompt
  13535. for a value, offering completion either on allowed values (via an inherited
  13536. xxx_ALL property) or on existing values in other instances of this property
  13537. in the current file."
  13538. (interactive (list nil nil))
  13539. (let* ((property (or property (org-read-property-name)))
  13540. (value (or value (org-read-property-value property)))
  13541. (fn (cdr (assoc property org-properties-postprocess-alist))))
  13542. (setq org-last-set-property property)
  13543. ;; Possibly postprocess the inserted value:
  13544. (when fn (setq value (funcall fn value)))
  13545. (unless (equal (org-entry-get nil property) value)
  13546. (org-entry-put nil property value))))
  13547. (defun org-delete-property (property)
  13548. "In the current entry, delete PROPERTY."
  13549. (interactive
  13550. (let* ((completion-ignore-case t)
  13551. (prop (org-icompleting-read "Property: "
  13552. (org-entry-properties nil 'standard))))
  13553. (list prop)))
  13554. (message "Property %s %s" property
  13555. (if (org-entry-delete nil property)
  13556. "deleted"
  13557. "was not present in the entry")))
  13558. (defun org-delete-property-globally (property)
  13559. "Remove PROPERTY globally, from all entries."
  13560. (interactive
  13561. (let* ((completion-ignore-case t)
  13562. (prop (org-icompleting-read
  13563. "Globally remove property: "
  13564. (mapcar 'list (org-buffer-property-keys)))))
  13565. (list prop)))
  13566. (save-excursion
  13567. (save-restriction
  13568. (widen)
  13569. (goto-char (point-min))
  13570. (let ((cnt 0))
  13571. (while (re-search-forward
  13572. (org-re-property property)
  13573. nil t)
  13574. (setq cnt (1+ cnt))
  13575. (delete-region (match-beginning 0) (1+ (point-at-eol))))
  13576. (message "Property \"%s\" removed from %d entries" property cnt)))))
  13577. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  13578. (defun org-compute-property-at-point ()
  13579. "Compute the property at point.
  13580. This looks for an enclosing column format, extracts the operator and
  13581. then applies it to the property in the column format's scope."
  13582. (interactive)
  13583. (unless (org-at-property-p)
  13584. (error "Not at a property"))
  13585. (let ((prop (org-match-string-no-properties 2)))
  13586. (org-columns-get-format-and-top-level)
  13587. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  13588. (error "No operator defined for property %s" prop))
  13589. (org-columns-compute prop)))
  13590. (defvar org-property-allowed-value-functions nil
  13591. "Hook for functions supplying allowed values for a specific property.
  13592. The functions must take a single argument, the name of the property, and
  13593. return a flat list of allowed values. If \":ETC\" is one of
  13594. the values, this means that these values are intended as defaults for
  13595. completion, but that other values should be allowed too.
  13596. The functions must return nil if they are not responsible for this
  13597. property.")
  13598. (defun org-property-get-allowed-values (pom property &optional table)
  13599. "Get allowed values for the property PROPERTY.
  13600. When TABLE is non-nil, return an alist that can directly be used for
  13601. completion."
  13602. (let (vals)
  13603. (cond
  13604. ((equal property "TODO")
  13605. (setq vals (org-with-point-at pom
  13606. (append org-todo-keywords-1 '("")))))
  13607. ((equal property "PRIORITY")
  13608. (let ((n org-lowest-priority))
  13609. (while (>= n org-highest-priority)
  13610. (push (char-to-string n) vals)
  13611. (setq n (1- n)))))
  13612. ((member property org-special-properties))
  13613. ((setq vals (run-hook-with-args-until-success
  13614. 'org-property-allowed-value-functions property)))
  13615. (t
  13616. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  13617. (when (and vals (string-match "\\S-" vals))
  13618. (setq vals (car (read-from-string (concat "(" vals ")"))))
  13619. (setq vals (mapcar (lambda (x)
  13620. (cond ((stringp x) x)
  13621. ((numberp x) (number-to-string x))
  13622. ((symbolp x) (symbol-name x))
  13623. (t "???")))
  13624. vals)))))
  13625. (when (member ":ETC" vals)
  13626. (setq vals (remove ":ETC" vals))
  13627. (org-add-props (car vals) '(org-unrestricted t)))
  13628. (if table (mapcar 'list vals) vals)))
  13629. (defun org-property-previous-allowed-value (&optional previous)
  13630. "Switch to the next allowed value for this property."
  13631. (interactive)
  13632. (org-property-next-allowed-value t))
  13633. (defun org-property-next-allowed-value (&optional previous)
  13634. "Switch to the next allowed value for this property."
  13635. (interactive)
  13636. (unless (org-at-property-p)
  13637. (error "Not at a property"))
  13638. (let* ((key (match-string 2))
  13639. (value (match-string 3))
  13640. (allowed (or (org-property-get-allowed-values (point) key)
  13641. (and (member value '("[ ]" "[-]" "[X]"))
  13642. '("[ ]" "[X]"))))
  13643. nval)
  13644. (unless allowed
  13645. (error "Allowed values for this property have not been defined"))
  13646. (if previous (setq allowed (reverse allowed)))
  13647. (if (member value allowed)
  13648. (setq nval (car (cdr (member value allowed)))))
  13649. (setq nval (or nval (car allowed)))
  13650. (if (equal nval value)
  13651. (error "Only one allowed value for this property"))
  13652. (org-at-property-p)
  13653. (replace-match (concat " :" key ": " nval) t t)
  13654. (org-indent-line)
  13655. (beginning-of-line 1)
  13656. (skip-chars-forward " \t")
  13657. (run-hook-with-args 'org-property-changed-functions key nval)))
  13658. (defun org-find-olp (path &optional this-buffer)
  13659. "Return a marker pointing to the entry at outline path OLP.
  13660. If anything goes wrong, throw an error.
  13661. You can wrap this call to catch the error like this:
  13662. (condition-case msg
  13663. (org-mobile-locate-entry (match-string 4))
  13664. (error (nth 1 msg)))
  13665. The return value will then be either a string with the error message,
  13666. or a marker if everything is OK.
  13667. If THIS-BUFFER is set, the outline path does not contain a file,
  13668. only headings."
  13669. (let* ((file (if this-buffer buffer-file-name (pop path)))
  13670. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  13671. (level 1)
  13672. (lmin 1)
  13673. (lmax 1)
  13674. limit re end found pos heading cnt flevel)
  13675. (unless buffer (error "File not found :%s" file))
  13676. (with-current-buffer buffer
  13677. (save-excursion
  13678. (save-restriction
  13679. (widen)
  13680. (setq limit (point-max))
  13681. (goto-char (point-min))
  13682. (while (setq heading (pop path))
  13683. (setq re (format org-complex-heading-regexp-format
  13684. (regexp-quote heading)))
  13685. (setq cnt 0 pos (point))
  13686. (while (re-search-forward re end t)
  13687. (setq level (- (match-end 1) (match-beginning 1)))
  13688. (if (and (>= level lmin) (<= level lmax))
  13689. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  13690. (when (= cnt 0) (error "Heading not found on level %d: %s"
  13691. lmax heading))
  13692. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  13693. lmax heading))
  13694. (goto-char found)
  13695. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  13696. (setq end (save-excursion (org-end-of-subtree t t))))
  13697. (when (org-at-heading-p)
  13698. (move-marker (make-marker) (point))))))))
  13699. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  13700. "Find node HEADING in BUFFER.
  13701. Return a marker to the heading if it was found, or nil if not.
  13702. If POS-ONLY is set, return just the position instead of a marker.
  13703. The heading text must match exact, but it may have a TODO keyword,
  13704. a priority cookie and tags in the standard locations."
  13705. (with-current-buffer (or buffer (current-buffer))
  13706. (save-excursion
  13707. (save-restriction
  13708. (widen)
  13709. (goto-char (point-min))
  13710. (let (case-fold-search)
  13711. (if (re-search-forward
  13712. (format org-complex-heading-regexp-format
  13713. (regexp-quote heading)) nil t)
  13714. (if pos-only
  13715. (match-beginning 0)
  13716. (move-marker (make-marker) (match-beginning 0)))))))))
  13717. (defun org-find-exact-heading-in-directory (heading &optional dir)
  13718. "Find Org node headline HEADING in all .org files in directory DIR.
  13719. When the target headline is found, return a marker to this location."
  13720. (let ((files (directory-files (or dir default-directory)
  13721. nil "\\`[^.#].*\\.org\\'"))
  13722. file visiting m buffer)
  13723. (catch 'found
  13724. (while (setq file (pop files))
  13725. (message "trying %s" file)
  13726. (setq visiting (org-find-base-buffer-visiting file))
  13727. (setq buffer (or visiting (find-file-noselect file)))
  13728. (setq m (org-find-exact-headline-in-buffer
  13729. heading buffer))
  13730. (when (and (not m) (not visiting)) (kill-buffer buffer))
  13731. (and m (throw 'found m))))))
  13732. (defun org-find-entry-with-id (ident)
  13733. "Locate the entry that contains the ID property with exact value IDENT.
  13734. IDENT can be a string, a symbol or a number, this function will search for
  13735. the string representation of it.
  13736. Return the position where this entry starts, or nil if there is no such entry."
  13737. (interactive "sID: ")
  13738. (let ((id (cond
  13739. ((stringp ident) ident)
  13740. ((symbol-name ident) (symbol-name ident))
  13741. ((numberp ident) (number-to-string ident))
  13742. (t (error "IDENT %s must be a string, symbol or number" ident))))
  13743. (case-fold-search nil))
  13744. (save-excursion
  13745. (save-restriction
  13746. (widen)
  13747. (goto-char (point-min))
  13748. (when (re-search-forward
  13749. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  13750. nil t)
  13751. (org-back-to-heading t)
  13752. (point))))))
  13753. ;;;; Timestamps
  13754. (defvar org-last-changed-timestamp nil)
  13755. (defvar org-last-inserted-timestamp nil
  13756. "The last time stamp inserted with `org-insert-time-stamp'.")
  13757. (defvar org-time-was-given) ; dynamically scoped parameter
  13758. (defvar org-end-time-was-given) ; dynamically scoped parameter
  13759. (defvar org-ts-what) ; dynamically scoped parameter
  13760. (defun org-time-stamp (arg &optional inactive)
  13761. "Prompt for a date/time and insert a time stamp.
  13762. If the user specifies a time like HH:MM or if this command is
  13763. called with at least one prefix argument, the time stamp contains
  13764. the date and the time. Otherwise, only the date is be included.
  13765. All parts of a date not specified by the user is filled in from
  13766. the current date/time. So if you just press return without
  13767. typing anything, the time stamp will represent the current
  13768. date/time.
  13769. If there is already a timestamp at the cursor, it will be
  13770. modified.
  13771. With two universal prefix arguments, insert an active timestamp
  13772. with the current time without prompting the user."
  13773. (interactive "P")
  13774. (let* ((ts nil)
  13775. (default-time
  13776. ;; Default time is either today, or, when entering a range,
  13777. ;; the range start.
  13778. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  13779. (save-excursion
  13780. (re-search-backward
  13781. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  13782. (- (point) 20) t)))
  13783. (apply 'encode-time (org-parse-time-string (match-string 1)))
  13784. (current-time)))
  13785. (default-input (and ts (org-get-compact-tod ts)))
  13786. (repeater (save-excursion
  13787. (save-match-data
  13788. (beginning-of-line)
  13789. (when (re-search-forward
  13790. "\\([.+-]+[0-9]+[hdwmy] ?\\)+" ;;\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  13791. (save-excursion (progn (end-of-line) (point))) t)
  13792. (match-string 0)))))
  13793. org-time-was-given org-end-time-was-given time)
  13794. (cond
  13795. ((and (org-at-timestamp-p t)
  13796. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  13797. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  13798. (insert "--")
  13799. (setq time (let ((this-command this-command))
  13800. (org-read-date arg 'totime nil nil
  13801. default-time default-input inactive)))
  13802. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  13803. ((org-at-timestamp-p t)
  13804. (setq time (let ((this-command this-command))
  13805. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  13806. (when (org-at-timestamp-p t) ; just to get the match data
  13807. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  13808. (replace-match "")
  13809. (setq org-last-changed-timestamp
  13810. (org-insert-time-stamp
  13811. time (or org-time-was-given arg)
  13812. inactive nil nil (list org-end-time-was-given)))
  13813. (when repeater (goto-char (1- (point))) (insert " " repeater)
  13814. (setq org-last-changed-timestamp
  13815. (concat (substring org-last-inserted-timestamp 0 -1)
  13816. " " repeater ">"))))
  13817. (message "Timestamp updated"))
  13818. ((equal arg '(16))
  13819. (org-insert-time-stamp (current-time) t))
  13820. (t
  13821. (setq time (let ((this-command this-command))
  13822. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  13823. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  13824. nil nil (list org-end-time-was-given))))))
  13825. ;; FIXME: can we use this for something else, like computing time differences?
  13826. (defun org-get-compact-tod (s)
  13827. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  13828. (let* ((t1 (match-string 1 s))
  13829. (h1 (string-to-number (match-string 2 s)))
  13830. (m1 (string-to-number (match-string 3 s)))
  13831. (t2 (and (match-end 4) (match-string 5 s)))
  13832. (h2 (and t2 (string-to-number (match-string 6 s))))
  13833. (m2 (and t2 (string-to-number (match-string 7 s))))
  13834. dh dm)
  13835. (if (not t2)
  13836. t1
  13837. (setq dh (- h2 h1) dm (- m2 m1))
  13838. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  13839. (concat t1 "+" (number-to-string dh)
  13840. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  13841. (defun org-time-stamp-inactive (&optional arg)
  13842. "Insert an inactive time stamp.
  13843. An inactive time stamp is enclosed in square brackets instead of angle
  13844. brackets. It is inactive in the sense that it does not trigger agenda entries,
  13845. does not link to the calendar and cannot be changed with the S-cursor keys.
  13846. So these are more for recording a certain time/date."
  13847. (interactive "P")
  13848. (org-time-stamp arg 'inactive))
  13849. (defvar org-date-ovl (make-overlay 1 1))
  13850. (overlay-put org-date-ovl 'face 'org-date-selected)
  13851. (org-detach-overlay org-date-ovl)
  13852. (defvar org-ans1) ; dynamically scoped parameter
  13853. (defvar org-ans2) ; dynamically scoped parameter
  13854. (defvar org-plain-time-of-day-regexp) ; defined below
  13855. (defvar org-overriding-default-time nil) ; dynamically scoped
  13856. (defvar org-read-date-overlay nil)
  13857. (defvar org-dcst nil) ; dynamically scoped
  13858. (defvar org-read-date-history nil)
  13859. (defvar org-read-date-final-answer nil)
  13860. (defvar org-read-date-analyze-futurep nil)
  13861. (defvar org-read-date-analyze-forced-year nil)
  13862. (defvar org-read-date-inactive)
  13863. (defun org-read-date (&optional org-with-time to-time from-string prompt
  13864. default-time default-input inactive)
  13865. "Read a date, possibly a time, and make things smooth for the user.
  13866. The prompt will suggest to enter an ISO date, but you can also enter anything
  13867. which will at least partially be understood by `parse-time-string'.
  13868. Unrecognized parts of the date will default to the current day, month, year,
  13869. hour and minute. If this command is called to replace a timestamp at point,
  13870. or to enter the second timestamp of a range, the default time is taken
  13871. from the existing stamp. Furthermore, the command prefers the future,
  13872. so if you are giving a date where the year is not given, and the day-month
  13873. combination is already past in the current year, it will assume you
  13874. mean next year. For details, see the manual. A few examples:
  13875. 3-2-5 --> 2003-02-05
  13876. feb 15 --> currentyear-02-15
  13877. 2/15 --> currentyear-02-15
  13878. sep 12 9 --> 2009-09-12
  13879. 12:45 --> today 12:45
  13880. 22 sept 0:34 --> currentyear-09-22 0:34
  13881. 12 --> currentyear-currentmonth-12
  13882. Fri --> nearest Friday (today or later)
  13883. etc.
  13884. Furthermore you can specify a relative date by giving, as the *first* thing
  13885. in the input: a plus/minus sign, a number and a letter [hdwmy] to indicate
  13886. change in days weeks, months, years.
  13887. With a single plus or minus, the date is relative to today. With a double
  13888. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  13889. +4d --> four days from today
  13890. +4 --> same as above
  13891. +2w --> two weeks from today
  13892. ++5 --> five days from default date
  13893. The function understands only English month and weekday abbreviations.
  13894. While prompting, a calendar is popped up - you can also select the
  13895. date with the mouse (button 1). The calendar shows a period of three
  13896. months. To scroll it to other months, use the keys `>' and `<'.
  13897. If you don't like the calendar, turn it off with
  13898. \(setq org-read-date-popup-calendar nil)
  13899. With optional argument TO-TIME, the date will immediately be converted
  13900. to an internal time.
  13901. With an optional argument WITH-TIME, the prompt will suggest to also
  13902. insert a time. Note that when WITH-TIME is not set, you can still
  13903. enter a time, and this function will inform the calling routine about
  13904. this change. The calling routine may then choose to change the format
  13905. used to insert the time stamp into the buffer to include the time.
  13906. With optional argument FROM-STRING, read from this string instead from
  13907. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  13908. the time/date that is used for everything that is not specified by the
  13909. user."
  13910. (require 'parse-time)
  13911. (let* ((org-time-stamp-rounding-minutes
  13912. (if (equal org-with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  13913. (org-dcst org-display-custom-times)
  13914. (ct (org-current-time))
  13915. (org-def (or org-overriding-default-time default-time ct))
  13916. (org-defdecode (decode-time org-def))
  13917. (dummy (progn
  13918. (when (< (nth 2 org-defdecode) org-extend-today-until)
  13919. (setcar (nthcdr 2 org-defdecode) -1)
  13920. (setcar (nthcdr 1 org-defdecode) 59)
  13921. (setq org-def (apply 'encode-time org-defdecode)
  13922. org-defdecode (decode-time org-def)))))
  13923. (calendar-frame-setup nil)
  13924. (calendar-setup nil)
  13925. (calendar-move-hook nil)
  13926. (calendar-view-diary-initially-flag nil)
  13927. (calendar-view-holidays-initially-flag nil)
  13928. (timestr (format-time-string
  13929. (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") org-def))
  13930. (prompt (concat (if prompt (concat prompt " ") "")
  13931. (format "Date+time [%s]: " timestr)))
  13932. ans (org-ans0 "") org-ans1 org-ans2 final)
  13933. (cond
  13934. (from-string (setq ans from-string))
  13935. (org-read-date-popup-calendar
  13936. (save-excursion
  13937. (save-window-excursion
  13938. (calendar)
  13939. (org-eval-in-calendar '(setq cursor-type nil) t)
  13940. (unwind-protect
  13941. (progn
  13942. (calendar-forward-day (- (time-to-days org-def)
  13943. (calendar-absolute-from-gregorian
  13944. (calendar-current-date))))
  13945. (org-eval-in-calendar nil t)
  13946. (let* ((old-map (current-local-map))
  13947. (map (copy-keymap calendar-mode-map))
  13948. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  13949. (org-defkey map (kbd "RET") 'org-calendar-select)
  13950. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  13951. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  13952. (org-defkey minibuffer-local-map [(meta shift left)]
  13953. (lambda () (interactive)
  13954. (org-eval-in-calendar '(calendar-backward-month 1))))
  13955. (org-defkey minibuffer-local-map [(meta shift right)]
  13956. (lambda () (interactive)
  13957. (org-eval-in-calendar '(calendar-forward-month 1))))
  13958. (org-defkey minibuffer-local-map [(meta shift up)]
  13959. (lambda () (interactive)
  13960. (org-eval-in-calendar '(calendar-backward-year 1))))
  13961. (org-defkey minibuffer-local-map [(meta shift down)]
  13962. (lambda () (interactive)
  13963. (org-eval-in-calendar '(calendar-forward-year 1))))
  13964. (org-defkey minibuffer-local-map [?\e (shift left)]
  13965. (lambda () (interactive)
  13966. (org-eval-in-calendar '(calendar-backward-month 1))))
  13967. (org-defkey minibuffer-local-map [?\e (shift right)]
  13968. (lambda () (interactive)
  13969. (org-eval-in-calendar '(calendar-forward-month 1))))
  13970. (org-defkey minibuffer-local-map [?\e (shift up)]
  13971. (lambda () (interactive)
  13972. (org-eval-in-calendar '(calendar-backward-year 1))))
  13973. (org-defkey minibuffer-local-map [?\e (shift down)]
  13974. (lambda () (interactive)
  13975. (org-eval-in-calendar '(calendar-forward-year 1))))
  13976. (org-defkey minibuffer-local-map [(shift up)]
  13977. (lambda () (interactive)
  13978. (org-eval-in-calendar '(calendar-backward-week 1))))
  13979. (org-defkey minibuffer-local-map [(shift down)]
  13980. (lambda () (interactive)
  13981. (org-eval-in-calendar '(calendar-forward-week 1))))
  13982. (org-defkey minibuffer-local-map [(shift left)]
  13983. (lambda () (interactive)
  13984. (org-eval-in-calendar '(calendar-backward-day 1))))
  13985. (org-defkey minibuffer-local-map [(shift right)]
  13986. (lambda () (interactive)
  13987. (org-eval-in-calendar '(calendar-forward-day 1))))
  13988. (org-defkey minibuffer-local-map ">"
  13989. (lambda () (interactive)
  13990. (org-eval-in-calendar '(scroll-calendar-left 1))))
  13991. (org-defkey minibuffer-local-map "<"
  13992. (lambda () (interactive)
  13993. (org-eval-in-calendar '(scroll-calendar-right 1))))
  13994. (org-defkey minibuffer-local-map "\C-v"
  13995. (lambda () (interactive)
  13996. (org-eval-in-calendar
  13997. '(calendar-scroll-left-three-months 1))))
  13998. (org-defkey minibuffer-local-map "\M-v"
  13999. (lambda () (interactive)
  14000. (org-eval-in-calendar
  14001. '(calendar-scroll-right-three-months 1))))
  14002. (run-hooks 'org-read-date-minibuffer-setup-hook)
  14003. (unwind-protect
  14004. (progn
  14005. (use-local-map map)
  14006. (setq org-read-date-inactive inactive)
  14007. (add-hook 'post-command-hook 'org-read-date-display)
  14008. (setq org-ans0 (read-string prompt default-input
  14009. 'org-read-date-history nil))
  14010. ;; org-ans0: from prompt
  14011. ;; org-ans1: from mouse click
  14012. ;; org-ans2: from calendar motion
  14013. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  14014. (remove-hook 'post-command-hook 'org-read-date-display)
  14015. (use-local-map old-map)
  14016. (when org-read-date-overlay
  14017. (delete-overlay org-read-date-overlay)
  14018. (setq org-read-date-overlay nil)))))
  14019. (bury-buffer "*Calendar*")))))
  14020. (t ; Naked prompt only
  14021. (unwind-protect
  14022. (setq ans (read-string prompt default-input
  14023. 'org-read-date-history timestr))
  14024. (when org-read-date-overlay
  14025. (delete-overlay org-read-date-overlay)
  14026. (setq org-read-date-overlay nil)))))
  14027. (setq final (org-read-date-analyze ans org-def org-defdecode))
  14028. (when org-read-date-analyze-forced-year
  14029. (message "Year was forced into %s"
  14030. (if org-read-date-force-compatible-dates
  14031. "compatible range (1970-2037)"
  14032. "range representable on this machine"))
  14033. (ding))
  14034. ;; One round trip to get rid of 34th of August and stuff like that....
  14035. (setq final (decode-time (apply 'encode-time final)))
  14036. (setq org-read-date-final-answer ans)
  14037. (if to-time
  14038. (apply 'encode-time final)
  14039. (if (and (boundp 'org-time-was-given) org-time-was-given)
  14040. (format "%04d-%02d-%02d %02d:%02d"
  14041. (nth 5 final) (nth 4 final) (nth 3 final)
  14042. (nth 2 final) (nth 1 final))
  14043. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  14044. (defvar org-def)
  14045. (defvar org-defdecode)
  14046. (defvar org-with-time)
  14047. (defun org-read-date-display ()
  14048. "Display the current date prompt interpretation in the minibuffer."
  14049. (when org-read-date-display-live
  14050. (when org-read-date-overlay
  14051. (delete-overlay org-read-date-overlay))
  14052. (when (minibufferp (current-buffer))
  14053. (save-excursion
  14054. (end-of-line 1)
  14055. (while (not (equal (buffer-substring
  14056. (max (point-min) (- (point) 4)) (point))
  14057. " "))
  14058. (insert " ")))
  14059. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  14060. " " (or org-ans1 org-ans2)))
  14061. (org-end-time-was-given nil)
  14062. (f (org-read-date-analyze ans org-def org-defdecode))
  14063. (fmts (if org-dcst
  14064. org-time-stamp-custom-formats
  14065. org-time-stamp-formats))
  14066. (fmt (if (or org-with-time
  14067. (and (boundp 'org-time-was-given) org-time-was-given))
  14068. (cdr fmts)
  14069. (car fmts)))
  14070. (txt (format-time-string fmt (apply 'encode-time f)))
  14071. (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
  14072. (txt (concat "=> " txt)))
  14073. (when (and org-end-time-was-given
  14074. (string-match org-plain-time-of-day-regexp txt))
  14075. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  14076. org-end-time-was-given
  14077. (substring txt (match-end 0)))))
  14078. (when org-read-date-analyze-futurep
  14079. (setq txt (concat txt " (=>F)")))
  14080. (setq org-read-date-overlay
  14081. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  14082. (org-overlay-display org-read-date-overlay txt 'secondary-selection)))))
  14083. (defun org-read-date-analyze (ans org-def org-defdecode)
  14084. "Analyze the combined answer of the date prompt."
  14085. ;; FIXME: cleanup and comment
  14086. (let ((nowdecode (decode-time (current-time)))
  14087. delta deltan deltaw deltadef year month day
  14088. hour minute second wday pm h2 m2 tl wday1
  14089. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  14090. (setq org-read-date-analyze-futurep nil
  14091. org-read-date-analyze-forced-year nil)
  14092. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  14093. (setq ans "+0"))
  14094. (when (setq delta (org-read-date-get-relative ans (current-time) org-def))
  14095. (setq ans (replace-match "" t t ans)
  14096. deltan (car delta)
  14097. deltaw (nth 1 delta)
  14098. deltadef (nth 2 delta)))
  14099. ;; Check if there is an iso week date in there
  14100. ;; If yes, store the info and postpone interpreting it until the rest
  14101. ;; of the parsing is done
  14102. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  14103. (setq iso-year (if (match-end 1)
  14104. (org-small-year-to-year
  14105. (string-to-number (match-string 1 ans))))
  14106. iso-weekday (if (match-end 3)
  14107. (string-to-number (match-string 3 ans)))
  14108. iso-week (string-to-number (match-string 2 ans)))
  14109. (setq ans (replace-match "" t t ans)))
  14110. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  14111. (when (string-match
  14112. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  14113. (setq year (if (match-end 2)
  14114. (string-to-number (match-string 2 ans))
  14115. (progn (setq kill-year t)
  14116. (string-to-number (format-time-string "%Y"))))
  14117. month (string-to-number (match-string 3 ans))
  14118. day (string-to-number (match-string 4 ans)))
  14119. (if (< year 100) (setq year (+ 2000 year)))
  14120. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  14121. t nil ans)))
  14122. ;; Help matching dotted european dates
  14123. (when (string-match
  14124. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\. ?\\([1-9][0-9][0-9][0-9]\\)?" ans)
  14125. (setq year (if (match-end 3)
  14126. (string-to-number (match-string 3 ans))
  14127. (progn (setq kill-year t)
  14128. (string-to-number (format-time-string "%Y"))))
  14129. day (string-to-number (match-string 1 ans))
  14130. month (string-to-number (match-string 2 ans))
  14131. ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  14132. t nil ans)))
  14133. ;; Help matching american dates, like 5/30 or 5/30/7
  14134. (when (string-match
  14135. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  14136. (setq year (if (match-end 4)
  14137. (string-to-number (match-string 4 ans))
  14138. (progn (setq kill-year t)
  14139. (string-to-number (format-time-string "%Y"))))
  14140. month (string-to-number (match-string 1 ans))
  14141. day (string-to-number (match-string 2 ans)))
  14142. (if (< year 100) (setq year (+ 2000 year)))
  14143. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  14144. t nil ans)))
  14145. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  14146. ;; If there is a time with am/pm, and *no* time without it, we convert
  14147. ;; so that matching will be successful.
  14148. (loop for i from 1 to 2 do ; twice, for end time as well
  14149. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  14150. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  14151. (setq hour (string-to-number (match-string 1 ans))
  14152. minute (if (match-end 3)
  14153. (string-to-number (match-string 3 ans))
  14154. 0)
  14155. pm (equal ?p
  14156. (string-to-char (downcase (match-string 4 ans)))))
  14157. (if (and (= hour 12) (not pm))
  14158. (setq hour 0)
  14159. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  14160. (setq ans (replace-match (format "%02d:%02d" hour minute)
  14161. t t ans))))
  14162. ;; Check if a time range is given as a duration
  14163. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  14164. (setq hour (string-to-number (match-string 1 ans))
  14165. h2 (+ hour (string-to-number (match-string 3 ans)))
  14166. minute (string-to-number (match-string 2 ans))
  14167. m2 (+ minute (if (match-end 5) (string-to-number
  14168. (match-string 5 ans))0)))
  14169. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  14170. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  14171. t t ans)))
  14172. ;; Check if there is a time range
  14173. (when (boundp 'org-end-time-was-given)
  14174. (setq org-time-was-given nil)
  14175. (when (and (string-match org-plain-time-of-day-regexp ans)
  14176. (match-end 8))
  14177. (setq org-end-time-was-given (match-string 8 ans))
  14178. (setq ans (concat (substring ans 0 (match-beginning 7))
  14179. (substring ans (match-end 7))))))
  14180. (setq tl (parse-time-string ans)
  14181. day (or (nth 3 tl) (nth 3 org-defdecode))
  14182. month (or (nth 4 tl)
  14183. (if (and org-read-date-prefer-future
  14184. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  14185. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  14186. (nth 4 org-defdecode)))
  14187. year (or (and (not kill-year) (nth 5 tl))
  14188. (if (and org-read-date-prefer-future
  14189. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  14190. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  14191. (nth 5 org-defdecode)))
  14192. hour (or (nth 2 tl) (nth 2 org-defdecode))
  14193. minute (or (nth 1 tl) (nth 1 org-defdecode))
  14194. second (or (nth 0 tl) 0)
  14195. wday (nth 6 tl))
  14196. (when (and (eq org-read-date-prefer-future 'time)
  14197. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  14198. (equal day (nth 3 nowdecode))
  14199. (equal month (nth 4 nowdecode))
  14200. (equal year (nth 5 nowdecode))
  14201. (nth 2 tl)
  14202. (or (< (nth 2 tl) (nth 2 nowdecode))
  14203. (and (= (nth 2 tl) (nth 2 nowdecode))
  14204. (nth 1 tl)
  14205. (< (nth 1 tl) (nth 1 nowdecode)))))
  14206. (setq day (1+ day)
  14207. futurep t))
  14208. ;; Special date definitions below
  14209. (cond
  14210. (iso-week
  14211. ;; There was an iso week
  14212. (require 'cal-iso)
  14213. (setq futurep nil)
  14214. (setq year (or iso-year year)
  14215. day (or iso-weekday wday 1)
  14216. wday nil ; to make sure that the trigger below does not match
  14217. iso-date (calendar-gregorian-from-absolute
  14218. (calendar-absolute-from-iso
  14219. (list iso-week day year))))
  14220. ; FIXME: Should we also push ISO weeks into the future?
  14221. ; (when (and org-read-date-prefer-future
  14222. ; (not iso-year)
  14223. ; (< (calendar-absolute-from-gregorian iso-date)
  14224. ; (time-to-days (current-time))))
  14225. ; (setq year (1+ year)
  14226. ; iso-date (calendar-gregorian-from-absolute
  14227. ; (calendar-absolute-from-iso
  14228. ; (list iso-week day year)))))
  14229. (setq month (car iso-date)
  14230. year (nth 2 iso-date)
  14231. day (nth 1 iso-date)))
  14232. (deltan
  14233. (setq futurep nil)
  14234. (unless deltadef
  14235. (let ((now (decode-time (current-time))))
  14236. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  14237. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  14238. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  14239. ((equal deltaw "m") (setq month (+ month deltan)))
  14240. ((equal deltaw "y") (setq year (+ year deltan)))))
  14241. ((and wday (not (nth 3 tl)))
  14242. (setq futurep nil)
  14243. ;; Weekday was given, but no day, so pick that day in the week
  14244. ;; on or after the derived date.
  14245. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  14246. (unless (equal wday wday1)
  14247. (setq day (+ day (% (- wday wday1 -7) 7))))))
  14248. (if (and (boundp 'org-time-was-given)
  14249. (nth 2 tl))
  14250. (setq org-time-was-given t))
  14251. (if (< year 100) (setq year (+ 2000 year)))
  14252. ;; Check of the date is representable
  14253. (if org-read-date-force-compatible-dates
  14254. (progn
  14255. (if (< year 1970)
  14256. (setq year 1970 org-read-date-analyze-forced-year t))
  14257. (if (> year 2037)
  14258. (setq year 2037 org-read-date-analyze-forced-year t)))
  14259. (condition-case nil
  14260. (ignore (encode-time second minute hour day month year))
  14261. (error
  14262. (setq year (nth 5 org-defdecode))
  14263. (setq org-read-date-analyze-forced-year t))))
  14264. (setq org-read-date-analyze-futurep futurep)
  14265. (list second minute hour day month year)))
  14266. (defvar parse-time-weekdays)
  14267. (defun org-read-date-get-relative (s today default)
  14268. "Check string S for special relative date string.
  14269. TODAY and DEFAULT are internal times, for today and for a default.
  14270. Return shift list (N what def-flag)
  14271. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  14272. N is the number of WHATs to shift.
  14273. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  14274. the DEFAULT date rather than TODAY."
  14275. (require 'parse-time)
  14276. (when (and
  14277. (string-match
  14278. (concat
  14279. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  14280. "\\([0-9]+\\)?"
  14281. "\\([hdwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  14282. "\\([ \t]\\|$\\)") s)
  14283. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  14284. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  14285. (string-to-char (substring (match-string 1 s) -1))
  14286. ?+))
  14287. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  14288. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  14289. (what (if (match-end 3) (match-string 3 s) "d"))
  14290. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  14291. (date (if rel default today))
  14292. (wday (nth 6 (decode-time date)))
  14293. delta)
  14294. (if wday1
  14295. (progn
  14296. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  14297. (if (= dir ?-) (setq delta (- delta 7)))
  14298. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  14299. (list delta "d" rel))
  14300. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  14301. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  14302. "Turn a user-specified date into the internal representation.
  14303. The internal representation needed by the calendar is (month day year).
  14304. This is a wrapper to handle the brain-dead convention in calendar that
  14305. user function argument order change dependent on argument order."
  14306. (if (boundp 'calendar-date-style)
  14307. (cond
  14308. ((eq calendar-date-style 'american)
  14309. (list arg1 arg2 arg3))
  14310. ((eq calendar-date-style 'european)
  14311. (list arg2 arg1 arg3))
  14312. ((eq calendar-date-style 'iso)
  14313. (list arg2 arg3 arg1)))
  14314. (org-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  14315. (if (org-bound-and-true-p european-calendar-style)
  14316. (list arg2 arg1 arg3)
  14317. (list arg1 arg2 arg3)))))
  14318. (defun org-eval-in-calendar (form &optional keepdate)
  14319. "Eval FORM in the calendar window and return to current window.
  14320. When KEEPDATE is non-nil, update `org-ans2' from the cursor date,
  14321. otherwise stick to the current value of `org-ans2'."
  14322. (let ((sf (selected-frame))
  14323. (sw (selected-window)))
  14324. (select-window (get-buffer-window "*Calendar*" t))
  14325. (eval form)
  14326. (when (and (not keepdate) (calendar-cursor-to-date))
  14327. (let* ((date (calendar-cursor-to-date))
  14328. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14329. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  14330. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  14331. (select-window sw)
  14332. (org-select-frame-set-input-focus sf)))
  14333. (defun org-calendar-select ()
  14334. "Return to `org-read-date' with the date currently selected.
  14335. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  14336. (interactive)
  14337. (when (calendar-cursor-to-date)
  14338. (let* ((date (calendar-cursor-to-date))
  14339. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14340. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  14341. (if (active-minibuffer-window) (exit-minibuffer))))
  14342. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  14343. "Insert a date stamp for the date given by the internal TIME.
  14344. WITH-HM means use the stamp format that includes the time of the day.
  14345. INACTIVE means use square brackets instead of angular ones, so that the
  14346. stamp will not contribute to the agenda.
  14347. PRE and POST are optional strings to be inserted before and after the
  14348. stamp.
  14349. The command returns the inserted time stamp."
  14350. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  14351. stamp)
  14352. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  14353. (insert-before-markers (or pre ""))
  14354. (when (listp extra)
  14355. (setq extra (car extra))
  14356. (if (and (stringp extra)
  14357. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  14358. (setq extra (format "-%02d:%02d"
  14359. (string-to-number (match-string 1 extra))
  14360. (string-to-number (match-string 2 extra))))
  14361. (setq extra nil)))
  14362. (when extra
  14363. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  14364. (insert-before-markers (setq stamp (format-time-string fmt time)))
  14365. (insert-before-markers (or post ""))
  14366. (setq org-last-inserted-timestamp stamp)))
  14367. (defun org-toggle-time-stamp-overlays ()
  14368. "Toggle the use of custom time stamp formats."
  14369. (interactive)
  14370. (setq org-display-custom-times (not org-display-custom-times))
  14371. (unless org-display-custom-times
  14372. (let ((p (point-min)) (bmp (buffer-modified-p)))
  14373. (while (setq p (next-single-property-change p 'display))
  14374. (if (and (get-text-property p 'display)
  14375. (eq (get-text-property p 'face) 'org-date))
  14376. (remove-text-properties
  14377. p (setq p (next-single-property-change p 'display))
  14378. '(display t))))
  14379. (set-buffer-modified-p bmp)))
  14380. (if (featurep 'xemacs)
  14381. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  14382. (org-restart-font-lock)
  14383. (setq org-table-may-need-update t)
  14384. (if org-display-custom-times
  14385. (message "Time stamps are overlaid with custom format")
  14386. (message "Time stamp overlays removed")))
  14387. (defun org-display-custom-time (beg end)
  14388. "Overlay modified time stamp format over timestamp between BEG and END."
  14389. (let* ((ts (buffer-substring beg end))
  14390. t1 w1 with-hm tf time str w2 (off 0))
  14391. (save-match-data
  14392. (setq t1 (org-parse-time-string ts t))
  14393. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)?\\'" ts)
  14394. (setq off (- (match-end 0) (match-beginning 0)))))
  14395. (setq end (- end off))
  14396. (setq w1 (- end beg)
  14397. with-hm (and (nth 1 t1) (nth 2 t1))
  14398. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  14399. time (org-fix-decoded-time t1)
  14400. str (org-add-props
  14401. (format-time-string
  14402. (substring tf 1 -1) (apply 'encode-time time))
  14403. nil 'mouse-face 'highlight)
  14404. w2 (length str))
  14405. (if (not (= w2 w1))
  14406. (add-text-properties (1+ beg) (+ 2 beg)
  14407. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  14408. (if (featurep 'xemacs)
  14409. (progn
  14410. (put-text-property beg end 'invisible t)
  14411. (put-text-property beg end 'end-glyph (make-glyph str)))
  14412. (put-text-property beg end 'display str))))
  14413. (defun org-translate-time (string)
  14414. "Translate all timestamps in STRING to custom format.
  14415. But do this only if the variable `org-display-custom-times' is set."
  14416. (when org-display-custom-times
  14417. (save-match-data
  14418. (let* ((start 0)
  14419. (re org-ts-regexp-both)
  14420. t1 with-hm inactive tf time str beg end)
  14421. (while (setq start (string-match re string start))
  14422. (setq beg (match-beginning 0)
  14423. end (match-end 0)
  14424. t1 (save-match-data
  14425. (org-parse-time-string (substring string beg end) t))
  14426. with-hm (and (nth 1 t1) (nth 2 t1))
  14427. inactive (equal (substring string beg (1+ beg)) "[")
  14428. tf (funcall (if with-hm 'cdr 'car)
  14429. org-time-stamp-custom-formats)
  14430. time (org-fix-decoded-time t1)
  14431. str (format-time-string
  14432. (concat
  14433. (if inactive "[" "<") (substring tf 1 -1)
  14434. (if inactive "]" ">"))
  14435. (apply 'encode-time time))
  14436. string (replace-match str t t string)
  14437. start (+ start (length str)))))))
  14438. string)
  14439. (defun org-fix-decoded-time (time)
  14440. "Set 0 instead of nil for the first 6 elements of time.
  14441. Don't touch the rest."
  14442. (let ((n 0))
  14443. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  14444. (defun org-days-to-time (timestamp-string)
  14445. "Difference between TIMESTAMP-STRING and now in days."
  14446. (- (time-to-days (org-time-string-to-time timestamp-string))
  14447. (time-to-days (current-time))))
  14448. (defun org-deadline-close (timestamp-string &optional ndays)
  14449. "Is the time in TIMESTAMP-STRING close to the current date?"
  14450. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  14451. (and (< (org-days-to-time timestamp-string) ndays)
  14452. (not (org-entry-is-done-p))))
  14453. (defun org-get-wdays (ts)
  14454. "Get the deadline lead time appropriate for timestring TS."
  14455. (cond
  14456. ((<= org-deadline-warning-days 0)
  14457. ;; 0 or negative, enforce this value no matter what
  14458. (- org-deadline-warning-days))
  14459. ((string-match "-\\([0-9]+\\)\\([hdwmy]\\)\\(\\'\\|>\\| \\)" ts)
  14460. ;; lead time is specified.
  14461. (floor (* (string-to-number (match-string 1 ts))
  14462. (cdr (assoc (match-string 2 ts)
  14463. '(("d" . 1) ("w" . 7)
  14464. ("m" . 30.4) ("y" . 365.25)))))))
  14465. ;; go for the default.
  14466. (t org-deadline-warning-days)))
  14467. (defun org-calendar-select-mouse (ev)
  14468. "Return to `org-read-date' with the date currently selected.
  14469. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  14470. (interactive "e")
  14471. (mouse-set-point ev)
  14472. (when (calendar-cursor-to-date)
  14473. (let* ((date (calendar-cursor-to-date))
  14474. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14475. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  14476. (if (active-minibuffer-window) (exit-minibuffer))))
  14477. (defun org-check-deadlines (ndays)
  14478. "Check if there are any deadlines due or past due.
  14479. A deadline is considered due if it happens within `org-deadline-warning-days'
  14480. days from today's date. If the deadline appears in an entry marked DONE,
  14481. it is not shown. The prefix arg NDAYS can be used to test that many
  14482. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  14483. (interactive "P")
  14484. (let* ((org-warn-days
  14485. (cond
  14486. ((equal ndays '(4)) 100000)
  14487. (ndays (prefix-numeric-value ndays))
  14488. (t (abs org-deadline-warning-days))))
  14489. (case-fold-search nil)
  14490. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  14491. (callback
  14492. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  14493. (message "%d deadlines past-due or due within %d days"
  14494. (org-occur regexp nil callback)
  14495. org-warn-days)))
  14496. (defsubst org-re-timestamp (type)
  14497. "Return a regexp for timestamp TYPE.
  14498. Allowed values for TYPE are:
  14499. all: all timestamps
  14500. active: only active timestamps (<...>)
  14501. inactive: only inactive timestamps ([...])
  14502. scheduled: only scheduled timestamps
  14503. deadline: only deadline timestamps
  14504. When TYPE is nil, fall back on returning a regexp that matches
  14505. both scheduled and deadline timestamps."
  14506. (cond ((eq type 'all) "\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}\\(?: +[^]+0-9> \n -]+\\)?\\(?: +[0-9]\\{1,2\\}:[0-9]\\{2\\}\\)?\\)")
  14507. ((eq type 'active) org-ts-regexp)
  14508. ((eq type 'inactive) "\\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^ \n>]*?\\)\\]")
  14509. ((eq type 'scheduled) (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>"))
  14510. ((eq type 'deadline) (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  14511. ((eq type 'scheduled-or-deadline)
  14512. (concat "\\<\\(?:" org-deadline-string "\\|" org-scheduled-string "\\) *<\\([^>]+\\)>"))))
  14513. (defun org-check-before-date (date)
  14514. "Check if there are deadlines or scheduled entries before DATE."
  14515. (interactive (list (org-read-date)))
  14516. (let ((case-fold-search nil)
  14517. (regexp (org-re-timestamp org-ts-type))
  14518. (callback
  14519. (lambda () (time-less-p
  14520. (org-time-string-to-time (match-string 1))
  14521. (org-time-string-to-time date)))))
  14522. (message "%d entries before %s"
  14523. (org-occur regexp nil callback) date)))
  14524. (defun org-check-after-date (date)
  14525. "Check if there are deadlines or scheduled entries after DATE."
  14526. (interactive (list (org-read-date)))
  14527. (let ((case-fold-search nil)
  14528. (regexp (org-re-timestamp org-ts-type))
  14529. (callback
  14530. (lambda () (not
  14531. (time-less-p
  14532. (org-time-string-to-time (match-string 1))
  14533. (org-time-string-to-time date))))))
  14534. (message "%d entries after %s"
  14535. (org-occur regexp nil callback) date)))
  14536. (defun org-check-dates-range (start-date end-date)
  14537. "Check for deadlines/scheduled entries between START-DATE and END-DATE."
  14538. (interactive (list (org-read-date nil nil nil "Range starts")
  14539. (org-read-date nil nil nil "Range end")))
  14540. (let ((case-fold-search nil)
  14541. (regexp (org-re-timestamp org-ts-type))
  14542. (callback
  14543. (lambda ()
  14544. (let ((match (match-string 1)))
  14545. (and
  14546. (not (time-less-p
  14547. (org-time-string-to-time match)
  14548. (org-time-string-to-time start-date)))
  14549. (time-less-p
  14550. (org-time-string-to-time match)
  14551. (org-time-string-to-time end-date)))))))
  14552. (message "%d entries between %s and %s"
  14553. (org-occur regexp nil callback) start-date end-date)))
  14554. (defun org-evaluate-time-range (&optional to-buffer)
  14555. "Evaluate a time range by computing the difference between start and end.
  14556. Normally the result is just printed in the echo area, but with prefix arg
  14557. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  14558. If the time range is actually in a table, the result is inserted into the
  14559. next column.
  14560. For time difference computation, a year is assumed to be exactly 365
  14561. days in order to avoid rounding problems."
  14562. (interactive "P")
  14563. (or
  14564. (org-clock-update-time-maybe)
  14565. (save-excursion
  14566. (unless (org-at-date-range-p t)
  14567. (goto-char (point-at-bol))
  14568. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  14569. (if (not (org-at-date-range-p t))
  14570. (error "Not at a time-stamp range, and none found in current line")))
  14571. (let* ((ts1 (match-string 1))
  14572. (ts2 (match-string 2))
  14573. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  14574. (match-end (match-end 0))
  14575. (time1 (org-time-string-to-time ts1))
  14576. (time2 (org-time-string-to-time ts2))
  14577. (t1 (org-float-time time1))
  14578. (t2 (org-float-time time2))
  14579. (diff (abs (- t2 t1)))
  14580. (negative (< (- t2 t1) 0))
  14581. ;; (ys (floor (* 365 24 60 60)))
  14582. (ds (* 24 60 60))
  14583. (hs (* 60 60))
  14584. (fy "%dy %dd %02d:%02d")
  14585. (fy1 "%dy %dd")
  14586. (fd "%dd %02d:%02d")
  14587. (fd1 "%dd")
  14588. (fh "%02d:%02d")
  14589. y d h m align)
  14590. (if havetime
  14591. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  14592. y 0
  14593. d (floor (/ diff ds)) diff (mod diff ds)
  14594. h (floor (/ diff hs)) diff (mod diff hs)
  14595. m (floor (/ diff 60)))
  14596. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  14597. y 0
  14598. d (floor (+ (/ diff ds) 0.5))
  14599. h 0 m 0))
  14600. (if (not to-buffer)
  14601. (message "%s" (org-make-tdiff-string y d h m))
  14602. (if (org-at-table-p)
  14603. (progn
  14604. (goto-char match-end)
  14605. (setq align t)
  14606. (and (looking-at " *|") (goto-char (match-end 0))))
  14607. (goto-char match-end))
  14608. (if (looking-at
  14609. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  14610. (replace-match ""))
  14611. (if negative (insert " -"))
  14612. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  14613. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  14614. (insert " " (format fh h m))))
  14615. (if align (org-table-align))
  14616. (message "Time difference inserted")))))
  14617. (defun org-make-tdiff-string (y d h m)
  14618. (let ((fmt "")
  14619. (l nil))
  14620. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  14621. l (push y l)))
  14622. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  14623. l (push d l)))
  14624. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  14625. l (push h l)))
  14626. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  14627. l (push m l)))
  14628. (apply 'format fmt (nreverse l))))
  14629. (defun org-time-string-to-time (s &optional buffer pos)
  14630. (condition-case errdata
  14631. (apply 'encode-time (org-parse-time-string s))
  14632. (error (error "Bad timestamp `%s'%s\nError was: %s"
  14633. s (if (not (and buffer pos))
  14634. ""
  14635. (format " at %d in buffer `%s'" pos buffer))
  14636. (cdr errdata)))))
  14637. (defun org-time-string-to-seconds (s)
  14638. (org-float-time (org-time-string-to-time s)))
  14639. (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
  14640. "Convert a time stamp to an absolute day number.
  14641. If there is a specifier for a cyclic time stamp, get the closest date to
  14642. DAYNR.
  14643. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  14644. The variable date is bound by the calendar when this is called."
  14645. (cond
  14646. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  14647. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  14648. daynr
  14649. (+ daynr 1000)))
  14650. ((and daynr (string-match "\\+[0-9]+[hdwmy]" s))
  14651. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  14652. (time-to-days (current-time))) (match-string 0 s)
  14653. prefer show-all))
  14654. (t (time-to-days
  14655. (condition-case errdata
  14656. (apply 'encode-time (org-parse-time-string s))
  14657. (error (error "Bad timestamp `%s'%s\nError was: %s"
  14658. s (if (not (and buffer pos))
  14659. ""
  14660. (format " at %d in buffer `%s'" pos buffer))
  14661. (cdr errdata))))))))
  14662. (defun org-days-to-iso-week (days)
  14663. "Return the iso week number."
  14664. (require 'cal-iso)
  14665. (car (calendar-iso-from-absolute days)))
  14666. (defun org-small-year-to-year (year)
  14667. "Convert 2-digit years into 4-digit years.
  14668. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  14669. The year 2000 cannot be abbreviated. Any year larger than 99
  14670. is returned unchanged."
  14671. (if (< year 38)
  14672. (setq year (+ 2000 year))
  14673. (if (< year 100)
  14674. (setq year (+ 1900 year))))
  14675. year)
  14676. (defun org-time-from-absolute (d)
  14677. "Return the time corresponding to date D.
  14678. D may be an absolute day number, or a calendar-type list (month day year)."
  14679. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  14680. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  14681. (defun org-calendar-holiday ()
  14682. "List of holidays, for Diary display in Org-mode."
  14683. (require 'holidays)
  14684. (let ((hl (funcall
  14685. (if (fboundp 'calendar-check-holidays)
  14686. 'calendar-check-holidays 'check-calendar-holidays) date)))
  14687. (if hl (mapconcat 'identity hl "; "))))
  14688. (defun org-diary-sexp-entry (sexp entry date)
  14689. "Process a SEXP diary ENTRY for DATE."
  14690. (require 'diary-lib)
  14691. (let ((result (if calendar-debug-sexp
  14692. (let ((stack-trace-on-error t))
  14693. (eval (car (read-from-string sexp))))
  14694. (condition-case nil
  14695. (eval (car (read-from-string sexp)))
  14696. (error
  14697. (beep)
  14698. (message "Bad sexp at line %d in %s: %s"
  14699. (org-current-line)
  14700. (buffer-file-name) sexp)
  14701. (sleep-for 2))))))
  14702. (cond ((stringp result) (split-string result "; "))
  14703. ((and (consp result)
  14704. (not (consp (cdr result)))
  14705. (stringp (cdr result))) (cdr result))
  14706. ((and (consp result)
  14707. (stringp (car result))) result)
  14708. (result entry)
  14709. (t nil))))
  14710. (defun org-diary-to-ical-string (frombuf)
  14711. "Get iCalendar entries from diary entries in buffer FROMBUF.
  14712. This uses the icalendar.el library."
  14713. (let* ((tmpdir (if (featurep 'xemacs)
  14714. (temp-directory)
  14715. temporary-file-directory))
  14716. (tmpfile (make-temp-name
  14717. (expand-file-name "orgics" tmpdir)))
  14718. buf rtn b e)
  14719. (with-current-buffer frombuf
  14720. (icalendar-export-region (point-min) (point-max) tmpfile)
  14721. (setq buf (find-buffer-visiting tmpfile))
  14722. (set-buffer buf)
  14723. (goto-char (point-min))
  14724. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  14725. (setq b (match-beginning 0)))
  14726. (goto-char (point-max))
  14727. (if (re-search-backward "^END:VEVENT" nil t)
  14728. (setq e (match-end 0)))
  14729. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  14730. (kill-buffer buf)
  14731. (delete-file tmpfile)
  14732. rtn))
  14733. (defun org-closest-date (start current change prefer show-all)
  14734. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  14735. When PREFER is `past', return a date that is either CURRENT or past.
  14736. When PREFER is `future', return a date that is either CURRENT or future.
  14737. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  14738. ;; Make the proper lists from the dates
  14739. (catch 'exit
  14740. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  14741. dn dw sday cday n1 n2 n0
  14742. d m y y1 y2 date1 date2 nmonths nm ny m2)
  14743. (setq start (org-date-to-gregorian start)
  14744. current (org-date-to-gregorian
  14745. (if show-all
  14746. current
  14747. (time-to-days (current-time))))
  14748. sday (calendar-absolute-from-gregorian start)
  14749. cday (calendar-absolute-from-gregorian current))
  14750. (if (<= cday sday) (throw 'exit sday))
  14751. (if (string-match "\\(\\+[0-9]+\\)\\([hdwmy]\\)" change)
  14752. (setq dn (string-to-number (match-string 1 change))
  14753. dw (cdr (assoc (match-string 2 change) a1)))
  14754. (error "Invalid change specifier: %s" change))
  14755. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  14756. (cond
  14757. ((eq dw 'day)
  14758. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  14759. n2 (+ n1 dn)))
  14760. ((eq dw 'year)
  14761. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  14762. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  14763. (setq date1 (list m d y1)
  14764. n1 (calendar-absolute-from-gregorian date1)
  14765. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  14766. n2 (calendar-absolute-from-gregorian date2)))
  14767. ((eq dw 'month)
  14768. ;; approx number of month between the two dates
  14769. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  14770. ;; How often does dn fit in there?
  14771. (setq d (nth 1 start) m (car start) y (nth 2 start)
  14772. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  14773. m (+ m nm)
  14774. ny (floor (/ m 12))
  14775. y (+ y ny)
  14776. m (- m (* ny 12)))
  14777. (while (> m 12) (setq m (- m 12) y (1+ y)))
  14778. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  14779. (setq m2 (+ m dn) y2 y)
  14780. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  14781. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  14782. (while (<= n2 cday)
  14783. (setq n1 n2 m m2 y y2)
  14784. (setq m2 (+ m dn) y2 y)
  14785. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  14786. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  14787. ;; Make sure n1 is the earlier date
  14788. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  14789. (if show-all
  14790. (cond
  14791. ((eq prefer 'past) (if (= cday n2) n2 n1))
  14792. ((eq prefer 'future) (if (= cday n1) n1 n2))
  14793. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  14794. (cond
  14795. ((eq prefer 'past) (if (= cday n2) n2 n1))
  14796. ((eq prefer 'future) (if (= cday n1) n1 n2))
  14797. (t (if (= cday n1) n1 n2)))))))
  14798. (defun org-date-to-gregorian (date)
  14799. "Turn any specification of DATE into a Gregorian date for the calendar."
  14800. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  14801. ((and (listp date) (= (length date) 3)) date)
  14802. ((stringp date)
  14803. (setq date (org-parse-time-string date))
  14804. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  14805. ((listp date)
  14806. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  14807. (defun org-parse-time-string (s &optional nodefault)
  14808. "Parse the standard Org-mode time string.
  14809. This should be a lot faster than the normal `parse-time-string'.
  14810. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  14811. hour and minute fields will be nil if not given."
  14812. (if (string-match org-ts-regexp0 s)
  14813. (list 0
  14814. (if (or (match-beginning 8) (not nodefault))
  14815. (string-to-number (or (match-string 8 s) "0")))
  14816. (if (or (match-beginning 7) (not nodefault))
  14817. (string-to-number (or (match-string 7 s) "0")))
  14818. (string-to-number (match-string 4 s))
  14819. (string-to-number (match-string 3 s))
  14820. (string-to-number (match-string 2 s))
  14821. nil nil nil)
  14822. (error "Not a standard Org-mode time string: %s" s)))
  14823. (defun org-timestamp-up (&optional arg)
  14824. "Increase the date item at the cursor by one.
  14825. If the cursor is on the year, change the year. If it is on the month,
  14826. the day or the time, change that.
  14827. With prefix ARG, change by that many units."
  14828. (interactive "p")
  14829. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  14830. (defun org-timestamp-down (&optional arg)
  14831. "Decrease the date item at the cursor by one.
  14832. If the cursor is on the year, change the year. If it is on the month,
  14833. the day or the time, change that.
  14834. With prefix ARG, change by that many units."
  14835. (interactive "p")
  14836. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  14837. (defun org-timestamp-up-day (&optional arg)
  14838. "Increase the date in the time stamp by one day.
  14839. With prefix ARG, change that many days."
  14840. (interactive "p")
  14841. (if (and (not (org-at-timestamp-p t))
  14842. (org-at-heading-p))
  14843. (org-todo 'up)
  14844. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  14845. (defun org-timestamp-down-day (&optional arg)
  14846. "Decrease the date in the time stamp by one day.
  14847. With prefix ARG, change that many days."
  14848. (interactive "p")
  14849. (if (and (not (org-at-timestamp-p t))
  14850. (org-at-heading-p))
  14851. (org-todo 'down)
  14852. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  14853. (defun org-at-timestamp-p (&optional inactive-ok)
  14854. "Determine if the cursor is in or at a timestamp."
  14855. (interactive)
  14856. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  14857. (pos (point))
  14858. (ans (or (looking-at tsr)
  14859. (save-excursion
  14860. (skip-chars-backward "^[<\n\r\t")
  14861. (if (> (point) (point-min)) (backward-char 1))
  14862. (and (looking-at tsr)
  14863. (> (- (match-end 0) pos) -1))))))
  14864. (and ans
  14865. (boundp 'org-ts-what)
  14866. (setq org-ts-what
  14867. (cond
  14868. ((= pos (match-beginning 0)) 'bracket)
  14869. ;; Point is considered to be "on the bracket" whether
  14870. ;; it's really on it or right after it.
  14871. ((= pos (1- (match-end 0))) 'bracket)
  14872. ((= pos (match-end 0)) 'after)
  14873. ((org-pos-in-match-range pos 2) 'year)
  14874. ((org-pos-in-match-range pos 3) 'month)
  14875. ((org-pos-in-match-range pos 7) 'hour)
  14876. ((org-pos-in-match-range pos 8) 'minute)
  14877. ((or (org-pos-in-match-range pos 4)
  14878. (org-pos-in-match-range pos 5)) 'day)
  14879. ((and (> pos (or (match-end 8) (match-end 5)))
  14880. (< pos (match-end 0)))
  14881. (- pos (or (match-end 8) (match-end 5))))
  14882. (t 'day))))
  14883. ans))
  14884. (defun org-toggle-timestamp-type ()
  14885. "Toggle the type (<active> or [inactive]) of a time stamp."
  14886. (interactive)
  14887. (when (org-at-timestamp-p t)
  14888. (let ((beg (match-beginning 0)) (end (match-end 0))
  14889. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  14890. (save-excursion
  14891. (goto-char beg)
  14892. (while (re-search-forward "[][<>]" end t)
  14893. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  14894. t t)))
  14895. (message "Timestamp is now %sactive"
  14896. (if (equal (char-after beg) ?<) "" "in")))))
  14897. (defvar org-clock-history) ; defined in org-clock.el
  14898. (defvar org-clock-adjust-closest nil) ; defined in org-clock.el
  14899. (defun org-timestamp-change (n &optional what updown)
  14900. "Change the date in the time stamp at point.
  14901. The date will be changed by N times WHAT. WHAT can be `day', `month',
  14902. `year', `minute', `second'. If WHAT is not given, the cursor position
  14903. in the timestamp determines what will be changed."
  14904. (let ((origin (point)) origin-cat
  14905. with-hm inactive
  14906. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  14907. org-ts-what
  14908. extra rem
  14909. ts time time0 fixnext clrgx)
  14910. (if (not (org-at-timestamp-p t))
  14911. (error "Not at a timestamp"))
  14912. (if (and (not what) (eq org-ts-what 'bracket))
  14913. (org-toggle-timestamp-type)
  14914. ;; Point isn't on brackets. Remember the part of the time-stamp
  14915. ;; the point was in. Indeed, size of time-stamps may change,
  14916. ;; but point must be kept in the same category nonetheless.
  14917. (setq origin-cat org-ts-what)
  14918. (if (and (not what) (not (eq org-ts-what 'day))
  14919. org-display-custom-times
  14920. (get-text-property (point) 'display)
  14921. (not (get-text-property (1- (point)) 'display)))
  14922. (setq org-ts-what 'day))
  14923. (setq org-ts-what (or what org-ts-what)
  14924. inactive (= (char-after (match-beginning 0)) ?\[)
  14925. ts (match-string 0))
  14926. (replace-match "")
  14927. (if (string-match
  14928. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)*\\)[]>]"
  14929. ts)
  14930. (setq extra (match-string 1 ts)))
  14931. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  14932. (setq with-hm t))
  14933. (setq time0 (org-parse-time-string ts))
  14934. (when (and updown
  14935. (eq org-ts-what 'minute)
  14936. (not current-prefix-arg))
  14937. ;; This looks like s-up and s-down. Change by one rounding step.
  14938. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  14939. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  14940. (setcar (cdr time0) (+ (nth 1 time0)
  14941. (if (> n 0) (- rem) (- dm rem))))))
  14942. (setq time
  14943. (encode-time (or (car time0) 0)
  14944. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  14945. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  14946. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  14947. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  14948. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  14949. (nthcdr 6 time0)))
  14950. (when (and (member org-ts-what '(hour minute))
  14951. extra
  14952. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  14953. (setq extra (org-modify-ts-extra
  14954. extra
  14955. (if (eq org-ts-what 'hour) 2 5)
  14956. n dm)))
  14957. (when (integerp org-ts-what)
  14958. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  14959. (if (eq what 'calendar)
  14960. (let ((cal-date (org-get-date-from-calendar)))
  14961. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  14962. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  14963. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  14964. (setcar time0 (or (car time0) 0))
  14965. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  14966. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  14967. (setq time (apply 'encode-time time0))))
  14968. ;; Insert the new time-stamp, and ensure point stays in the same
  14969. ;; category as before (i.e. not after the last position in that
  14970. ;; category).
  14971. (let ((pos (point)))
  14972. ;; Stay before inserted string. `save-excursion' is of no use.
  14973. (setq org-last-changed-timestamp
  14974. (org-insert-time-stamp time with-hm inactive nil nil extra))
  14975. (goto-char pos))
  14976. (save-match-data
  14977. (looking-at org-ts-regexp3)
  14978. (goto-char (cond
  14979. ;; `day' category ends before `hour' if any, or at
  14980. ;; the end of the day name.
  14981. ((eq origin-cat 'day)
  14982. (min (or (match-beginning 7) (1- (match-end 5))) origin))
  14983. ((eq origin-cat 'hour) (min (match-end 7) origin))
  14984. ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
  14985. ((integerp origin-cat) (min (1- (match-end 0)) origin))
  14986. ;; `year' and `month' have both fixed size: point
  14987. ;; couldn't have moved into another part.
  14988. (t origin))))
  14989. ;; Update clock if on a CLOCK line.
  14990. (org-clock-update-time-maybe)
  14991. ;; Maybe adjust the closest clock in `org-clock-history'
  14992. (when org-clock-adjust-closest
  14993. (if (not (and (org-at-clock-log-p)
  14994. (< 1 (length (delq nil (mapcar (lambda(m) (marker-position m))
  14995. org-clock-history))))))
  14996. (message "No clock to adjust")
  14997. (cond ((save-excursion ; fix previous clock?
  14998. (re-search-backward org-ts-regexp0 nil t)
  14999. (looking-back (concat org-clock-string " \\[")))
  15000. (setq fixnext 1 clrgx (concat org-ts-regexp0 "\\] =>.*$")))
  15001. ((save-excursion ; fix next clock?
  15002. (re-search-backward org-ts-regexp0 nil t)
  15003. (looking-at (concat org-ts-regexp0 "\\] =>")))
  15004. (setq fixnext -1 clrgx (concat org-clock-string " \\[" org-ts-regexp0))))
  15005. (save-window-excursion
  15006. ;; Find closest clock to point, adjust the previous/next one in history
  15007. (let* ((p (save-excursion (org-back-to-heading t)))
  15008. (cl (mapcar (lambda(c) (abs (- (marker-position c) p))) org-clock-history))
  15009. (clfixnth
  15010. (+ fixnext (- (length cl) (or (length (member (apply #'min cl) cl)) 100))))
  15011. (clfixpos (if (> 0 clfixnth) nil (nth clfixnth org-clock-history))))
  15012. (if (not clfixpos)
  15013. (message "No clock to adjust")
  15014. (org-goto-marker-or-bmk clfixpos)
  15015. (org-show-subtree)
  15016. (when (re-search-forward clrgx nil t)
  15017. (goto-char (match-beginning 1))
  15018. (let (org-clock-adjust-closest)
  15019. (org-timestamp-change n org-ts-what updown))
  15020. (message "Clock adjusted in %s for heading: %s"
  15021. (file-name-nondirectory (buffer-file-name))
  15022. (org-get-heading t t))))))))
  15023. ;; Try to recenter the calendar window, if any.
  15024. (if (and org-calendar-follow-timestamp-change
  15025. (get-buffer-window "*Calendar*" t)
  15026. (memq org-ts-what '(day month year)))
  15027. (org-recenter-calendar (time-to-days time))))))
  15028. (defun org-modify-ts-extra (s pos n dm)
  15029. "Change the different parts of the lead-time and repeat fields in timestamp."
  15030. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  15031. ng h m new rem)
  15032. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  15033. (cond
  15034. ((or (org-pos-in-match-range pos 2)
  15035. (org-pos-in-match-range pos 3))
  15036. (setq m (string-to-number (match-string 3 s))
  15037. h (string-to-number (match-string 2 s)))
  15038. (if (org-pos-in-match-range pos 2)
  15039. (setq h (+ h n))
  15040. (setq n (* dm (org-no-warnings (signum n))))
  15041. (when (not (= 0 (setq rem (% m dm))))
  15042. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  15043. (setq m (+ m n)))
  15044. (if (< m 0) (setq m (+ m 60) h (1- h)))
  15045. (if (> m 59) (setq m (- m 60) h (1+ h)))
  15046. (setq h (min 24 (max 0 h)))
  15047. (setq ng 1 new (format "-%02d:%02d" h m)))
  15048. ((org-pos-in-match-range pos 6)
  15049. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  15050. ((org-pos-in-match-range pos 5)
  15051. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  15052. ((org-pos-in-match-range pos 9)
  15053. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  15054. ((org-pos-in-match-range pos 8)
  15055. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  15056. (when ng
  15057. (setq s (concat
  15058. (substring s 0 (match-beginning ng))
  15059. new
  15060. (substring s (match-end ng))))))
  15061. s))
  15062. (defun org-recenter-calendar (date)
  15063. "If the calendar is visible, recenter it to DATE."
  15064. (let ((cwin (get-buffer-window "*Calendar*" t)))
  15065. (when cwin
  15066. (let ((calendar-move-hook nil))
  15067. (with-selected-window cwin
  15068. (calendar-goto-date (if (listp date) date
  15069. (calendar-gregorian-from-absolute date))))))))
  15070. (defun org-goto-calendar (&optional arg)
  15071. "Go to the Emacs calendar at the current date.
  15072. If there is a time stamp in the current line, go to that date.
  15073. A prefix ARG can be used to force the current date."
  15074. (interactive "P")
  15075. (let ((tsr org-ts-regexp) diff
  15076. (calendar-move-hook nil)
  15077. (calendar-view-holidays-initially-flag nil)
  15078. (calendar-view-diary-initially-flag nil))
  15079. (if (or (org-at-timestamp-p)
  15080. (save-excursion
  15081. (beginning-of-line 1)
  15082. (looking-at (concat ".*" tsr))))
  15083. (let ((d1 (time-to-days (current-time)))
  15084. (d2 (time-to-days
  15085. (org-time-string-to-time (match-string 1)))))
  15086. (setq diff (- d2 d1))))
  15087. (calendar)
  15088. (calendar-goto-today)
  15089. (if (and diff (not arg)) (calendar-forward-day diff))))
  15090. (defun org-get-date-from-calendar ()
  15091. "Return a list (month day year) of date at point in calendar."
  15092. (with-current-buffer "*Calendar*"
  15093. (save-match-data
  15094. (calendar-cursor-to-date))))
  15095. (defun org-date-from-calendar ()
  15096. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  15097. If there is already a time stamp at the cursor position, update it."
  15098. (interactive)
  15099. (if (org-at-timestamp-p t)
  15100. (org-timestamp-change 0 'calendar)
  15101. (let ((cal-date (org-get-date-from-calendar)))
  15102. (org-insert-time-stamp
  15103. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  15104. (defun org-minutes-to-hh:mm-string (m)
  15105. "Compute H:MM from a number of minutes."
  15106. (let ((h (/ m 60)))
  15107. (setq m (- m (* 60 h)))
  15108. (format org-time-clocksum-format h m)))
  15109. (defun org-hh:mm-string-to-minutes (s)
  15110. "Convert a string H:MM to a number of minutes.
  15111. If the string is just a number, interpret it as minutes.
  15112. In fact, the first hh:mm or number in the string will be taken,
  15113. there can be extra stuff in the string.
  15114. If no number is found, the return value is 0."
  15115. (cond
  15116. ((integerp s) s)
  15117. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  15118. (+ (* (string-to-number (match-string 1 s)) 60)
  15119. (string-to-number (match-string 2 s))))
  15120. ((string-match "\\([0-9]+\\)" s)
  15121. (string-to-number (match-string 1 s)))
  15122. (t 0)))
  15123. (defcustom org-effort-durations
  15124. `(("h" . 60)
  15125. ("d" . ,(* 60 8))
  15126. ("w" . ,(* 60 8 5))
  15127. ("m" . ,(* 60 8 5 4))
  15128. ("y" . ,(* 60 8 5 40)))
  15129. "Conversion factor to minutes for an effort modifier.
  15130. Each entry has the form (MODIFIER . MINUTES).
  15131. In an effort string, a number followed by MODIFIER is multiplied
  15132. by the specified number of MINUTES to obtain an effort in
  15133. minutes.
  15134. For example, if the value of this variable is ((\"hours\" . 60)), then an
  15135. effort string \"2hours\" is equivalent to 120 minutes."
  15136. :group 'org-agenda
  15137. :version "24.1"
  15138. :type '(alist :key-type (string :tag "Modifier")
  15139. :value-type (number :tag "Minutes")))
  15140. (defun org-duration-string-to-minutes (s &optional output-to-string)
  15141. "Convert a duration string S to minutes.
  15142. A bare number is interpreted as minutes, modifiers can be set by
  15143. customizing `org-effort-durations' (which see).
  15144. Entries containing a colon are interpreted as H:MM by
  15145. `org-hh:mm-string-to-minutes'."
  15146. (let ((result 0)
  15147. (re (concat "\\([0-9.]+\\) *\\("
  15148. (regexp-opt (mapcar 'car org-effort-durations))
  15149. "\\)")))
  15150. (while (string-match re s)
  15151. (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  15152. (string-to-number (match-string 1 s))))
  15153. (setq s (replace-match "" nil t s)))
  15154. (setq result (floor result))
  15155. (incf result (org-hh:mm-string-to-minutes s))
  15156. (if output-to-string (number-to-string result) result)))
  15157. ;;;; Files
  15158. (defun org-save-all-org-buffers ()
  15159. "Save all Org-mode buffers without user confirmation."
  15160. (interactive)
  15161. (message "Saving all Org-mode buffers...")
  15162. (save-some-buffers t (lambda () (derived-mode-p 'org-mode)))
  15163. (when (featurep 'org-id) (org-id-locations-save))
  15164. (message "Saving all Org-mode buffers... done"))
  15165. (defun org-revert-all-org-buffers ()
  15166. "Revert all Org-mode buffers.
  15167. Prompt for confirmation when there are unsaved changes.
  15168. Be sure you know what you are doing before letting this function
  15169. overwrite your changes.
  15170. This function is useful in a setup where one tracks org files
  15171. with a version control system, to revert on one machine after pulling
  15172. changes from another. I believe the procedure must be like this:
  15173. 1. M-x org-save-all-org-buffers
  15174. 2. Pull changes from the other machine, resolve conflicts
  15175. 3. M-x org-revert-all-org-buffers"
  15176. (interactive)
  15177. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  15178. (error "Abort"))
  15179. (save-excursion
  15180. (save-window-excursion
  15181. (mapc
  15182. (lambda (b)
  15183. (when (and (with-current-buffer b (derived-mode-p 'org-mode))
  15184. (with-current-buffer b buffer-file-name))
  15185. (org-pop-to-buffer-same-window b)
  15186. (revert-buffer t 'no-confirm)))
  15187. (buffer-list))
  15188. (when (and (featurep 'org-id) org-id-track-globally)
  15189. (org-id-locations-load)))))
  15190. ;;;; Agenda files
  15191. ;;;###autoload
  15192. (defun org-switchb (&optional arg)
  15193. "Switch between Org buffers.
  15194. With one prefix argument, restrict available buffers to files.
  15195. With two prefix arguments, restrict available buffers to agenda files.
  15196. Defaults to `iswitchb' for buffer name completion.
  15197. Set `org-completion-use-ido' to make it use ido instead."
  15198. (interactive "P")
  15199. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  15200. ((equal arg '(16)) (org-buffer-list 'agenda))
  15201. (t (org-buffer-list))))
  15202. (org-completion-use-iswitchb org-completion-use-iswitchb)
  15203. (org-completion-use-ido org-completion-use-ido))
  15204. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  15205. (setq org-completion-use-iswitchb t))
  15206. (org-pop-to-buffer-same-window
  15207. (org-icompleting-read "Org buffer: "
  15208. (mapcar 'list (mapcar 'buffer-name blist))
  15209. nil t))))
  15210. ;;; Define some older names previously used for this functionality
  15211. ;;;###autoload
  15212. (defalias 'org-ido-switchb 'org-switchb)
  15213. ;;;###autoload
  15214. (defalias 'org-iswitchb 'org-switchb)
  15215. (defun org-buffer-list (&optional predicate exclude-tmp)
  15216. "Return a list of Org buffers.
  15217. PREDICATE can be `export', `files' or `agenda'.
  15218. export restrict the list to Export buffers.
  15219. files restrict the list to buffers visiting Org files.
  15220. agenda restrict the list to buffers visiting agenda files.
  15221. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  15222. (let* ((bfn nil)
  15223. (agenda-files (and (eq predicate 'agenda)
  15224. (mapcar 'file-truename (org-agenda-files t))))
  15225. (filter
  15226. (cond
  15227. ((eq predicate 'files)
  15228. (lambda (b) (with-current-buffer b (derived-mode-p 'org-mode))))
  15229. ((eq predicate 'export)
  15230. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  15231. ((eq predicate 'agenda)
  15232. (lambda (b)
  15233. (with-current-buffer b
  15234. (and (derived-mode-p 'org-mode)
  15235. (setq bfn (buffer-file-name b))
  15236. (member (file-truename bfn) agenda-files)))))
  15237. (t (lambda (b) (with-current-buffer b
  15238. (or (derived-mode-p 'org-mode)
  15239. (string-match "\*Org .*Export"
  15240. (buffer-name b)))))))))
  15241. (delq nil
  15242. (mapcar
  15243. (lambda(b)
  15244. (if (and (funcall filter b)
  15245. (or (not exclude-tmp)
  15246. (not (string-match "tmp" (buffer-name b)))))
  15247. b
  15248. nil))
  15249. (buffer-list)))))
  15250. (defun org-agenda-files (&optional unrestricted archives)
  15251. "Get the list of agenda files.
  15252. Optional UNRESTRICTED means return the full list even if a restriction
  15253. is currently in place.
  15254. When ARCHIVES is t, include all archive files that are really being
  15255. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  15256. `org-agenda-archives-mode' is t."
  15257. (let ((files
  15258. (cond
  15259. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  15260. ((stringp org-agenda-files) (org-read-agenda-file-list))
  15261. ((listp org-agenda-files) org-agenda-files)
  15262. (t (error "Invalid value of `org-agenda-files'")))))
  15263. (setq files (apply 'append
  15264. (mapcar (lambda (f)
  15265. (if (file-directory-p f)
  15266. (directory-files
  15267. f t org-agenda-file-regexp)
  15268. (list f)))
  15269. files)))
  15270. (when org-agenda-skip-unavailable-files
  15271. (setq files (delq nil
  15272. (mapcar (function
  15273. (lambda (file)
  15274. (and (file-readable-p file) file)))
  15275. files))))
  15276. (when (or (eq archives t)
  15277. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  15278. (setq files (org-add-archive-files files)))
  15279. files))
  15280. (defun org-agenda-file-p (&optional file)
  15281. "Return non-nil, if FILE is an agenda file.
  15282. If FILE is omitted, use the file associated with the current
  15283. buffer."
  15284. (member (or file (buffer-file-name))
  15285. (org-agenda-files t)))
  15286. (defun org-edit-agenda-file-list ()
  15287. "Edit the list of agenda files.
  15288. Depending on setup, this either uses customize to edit the variable
  15289. `org-agenda-files', or it visits the file that is holding the list. In the
  15290. latter case, the buffer is set up in a way that saving it automatically kills
  15291. the buffer and restores the previous window configuration."
  15292. (interactive)
  15293. (if (stringp org-agenda-files)
  15294. (let ((cw (current-window-configuration)))
  15295. (find-file org-agenda-files)
  15296. (org-set-local 'org-window-configuration cw)
  15297. (org-add-hook 'after-save-hook
  15298. (lambda ()
  15299. (set-window-configuration
  15300. (prog1 org-window-configuration
  15301. (kill-buffer (current-buffer))))
  15302. (org-install-agenda-files-menu)
  15303. (message "New agenda file list installed"))
  15304. nil 'local)
  15305. (message "%s" (substitute-command-keys
  15306. "Edit list and finish with \\[save-buffer]")))
  15307. (customize-variable 'org-agenda-files)))
  15308. (defun org-store-new-agenda-file-list (list)
  15309. "Set new value for the agenda file list and save it correctly."
  15310. (if (stringp org-agenda-files)
  15311. (let ((fe (org-read-agenda-file-list t)) b u)
  15312. (while (setq b (find-buffer-visiting org-agenda-files))
  15313. (kill-buffer b))
  15314. (with-temp-file org-agenda-files
  15315. (insert
  15316. (mapconcat
  15317. (lambda (f) ;; Keep un-expanded entries.
  15318. (if (setq u (assoc f fe))
  15319. (cdr u)
  15320. f))
  15321. list "\n")
  15322. "\n")))
  15323. (let ((org-mode-hook nil) (org-inhibit-startup t)
  15324. (org-insert-mode-line-in-empty-file nil))
  15325. (setq org-agenda-files list)
  15326. (customize-save-variable 'org-agenda-files org-agenda-files))))
  15327. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  15328. "Read the list of agenda files from a file.
  15329. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  15330. filenames, used by `org-store-new-agenda-file-list' to write back
  15331. un-expanded file names."
  15332. (when (file-directory-p org-agenda-files)
  15333. (error "`org-agenda-files' cannot be a single directory"))
  15334. (when (stringp org-agenda-files)
  15335. (with-temp-buffer
  15336. (insert-file-contents org-agenda-files)
  15337. (mapcar
  15338. (lambda (f)
  15339. (let ((e (expand-file-name (substitute-in-file-name f)
  15340. org-directory)))
  15341. (if pair-with-expansion
  15342. (cons e f)
  15343. e)))
  15344. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  15345. ;;;###autoload
  15346. (defun org-cycle-agenda-files ()
  15347. "Cycle through the files in `org-agenda-files'.
  15348. If the current buffer visits an agenda file, find the next one in the list.
  15349. If the current buffer does not, find the first agenda file."
  15350. (interactive)
  15351. (let* ((fs (org-agenda-files t))
  15352. (files (append fs (list (car fs))))
  15353. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  15354. file)
  15355. (unless files (error "No agenda files"))
  15356. (catch 'exit
  15357. (while (setq file (pop files))
  15358. (if (equal (file-truename file) tcf)
  15359. (when (car files)
  15360. (find-file (car files))
  15361. (throw 'exit t))))
  15362. (find-file (car fs)))
  15363. (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
  15364. (defun org-agenda-file-to-front (&optional to-end)
  15365. "Move/add the current file to the top of the agenda file list.
  15366. If the file is not present in the list, it is added to the front. If it is
  15367. present, it is moved there. With optional argument TO-END, add/move to the
  15368. end of the list."
  15369. (interactive "P")
  15370. (let ((org-agenda-skip-unavailable-files nil)
  15371. (file-alist (mapcar (lambda (x)
  15372. (cons (file-truename x) x))
  15373. (org-agenda-files t)))
  15374. (ctf (file-truename buffer-file-name))
  15375. x had)
  15376. (setq x (assoc ctf file-alist) had x)
  15377. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  15378. (if to-end
  15379. (setq file-alist (append (delq x file-alist) (list x)))
  15380. (setq file-alist (cons x (delq x file-alist))))
  15381. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  15382. (org-install-agenda-files-menu)
  15383. (message "File %s to %s of agenda file list"
  15384. (if had "moved" "added") (if to-end "end" "front"))))
  15385. (defun org-remove-file (&optional file)
  15386. "Remove current file from the list of files in variable `org-agenda-files'.
  15387. These are the files which are being checked for agenda entries.
  15388. Optional argument FILE means use this file instead of the current."
  15389. (interactive)
  15390. (let* ((org-agenda-skip-unavailable-files nil)
  15391. (file (or file buffer-file-name))
  15392. (true-file (file-truename file))
  15393. (afile (abbreviate-file-name file))
  15394. (files (delq nil (mapcar
  15395. (lambda (x)
  15396. (if (equal true-file
  15397. (file-truename x))
  15398. nil x))
  15399. (org-agenda-files t)))))
  15400. (if (not (= (length files) (length (org-agenda-files t))))
  15401. (progn
  15402. (org-store-new-agenda-file-list files)
  15403. (org-install-agenda-files-menu)
  15404. (message "Removed file: %s" afile))
  15405. (message "File was not in list: %s (not removed)" afile))))
  15406. (defun org-file-menu-entry (file)
  15407. (vector file (list 'find-file file) t))
  15408. (defun org-check-agenda-file (file)
  15409. "Make sure FILE exists. If not, ask user what to do."
  15410. (when (not (file-exists-p file))
  15411. (message "non-existent agenda file %s. [R]emove from list or [A]bort?"
  15412. (abbreviate-file-name file))
  15413. (let ((r (downcase (read-char-exclusive))))
  15414. (cond
  15415. ((equal r ?r)
  15416. (org-remove-file file)
  15417. (throw 'nextfile t))
  15418. (t (error "Abort"))))))
  15419. (defun org-get-agenda-file-buffer (file)
  15420. "Get a buffer visiting FILE. If the buffer needs to be created, add
  15421. it to the list of buffers which might be released later."
  15422. (let ((buf (org-find-base-buffer-visiting file)))
  15423. (if buf
  15424. buf ; just return it
  15425. ;; Make a new buffer and remember it
  15426. (setq buf (find-file-noselect file))
  15427. (if buf (push buf org-agenda-new-buffers))
  15428. buf)))
  15429. (defun org-release-buffers (blist)
  15430. "Release all buffers in list, asking the user for confirmation when needed.
  15431. When a buffer is unmodified, it is just killed. When modified, it is saved
  15432. \(if the user agrees) and then killed."
  15433. (let (buf file)
  15434. (while (setq buf (pop blist))
  15435. (setq file (buffer-file-name buf))
  15436. (when (and (buffer-modified-p buf)
  15437. file
  15438. (y-or-n-p (format "Save file %s? " file)))
  15439. (with-current-buffer buf (save-buffer)))
  15440. (kill-buffer buf))))
  15441. (defun org-prepare-agenda-buffers (files)
  15442. "Create buffers for all agenda files, protect archived trees and comments."
  15443. (interactive)
  15444. (let ((pa '(:org-archived t))
  15445. (pc '(:org-comment t))
  15446. (pall '(:org-archived t :org-comment t))
  15447. (inhibit-read-only t)
  15448. (rea (concat ":" org-archive-tag ":"))
  15449. bmp file re)
  15450. (save-excursion
  15451. (save-restriction
  15452. (while (setq file (pop files))
  15453. (catch 'nextfile
  15454. (if (bufferp file)
  15455. (set-buffer file)
  15456. (org-check-agenda-file file)
  15457. (set-buffer (org-get-agenda-file-buffer file)))
  15458. (widen)
  15459. (setq bmp (buffer-modified-p))
  15460. (org-refresh-category-properties)
  15461. (setq org-todo-keywords-for-agenda
  15462. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  15463. (setq org-done-keywords-for-agenda
  15464. (append org-done-keywords-for-agenda org-done-keywords))
  15465. (setq org-todo-keyword-alist-for-agenda
  15466. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  15467. (setq org-drawers-for-agenda
  15468. (append org-drawers-for-agenda org-drawers))
  15469. (setq org-tag-alist-for-agenda
  15470. (append org-tag-alist-for-agenda org-tag-alist))
  15471. (save-excursion
  15472. (remove-text-properties (point-min) (point-max) pall)
  15473. (when org-agenda-skip-archived-trees
  15474. (goto-char (point-min))
  15475. (while (re-search-forward rea nil t)
  15476. (if (org-at-heading-p t)
  15477. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  15478. (goto-char (point-min))
  15479. (setq re (format org-heading-keyword-regexp-format
  15480. org-comment-string))
  15481. (while (re-search-forward re nil t)
  15482. (add-text-properties
  15483. (match-beginning 0) (org-end-of-subtree t) pc)))
  15484. (set-buffer-modified-p bmp)))))
  15485. (setq org-todo-keywords-for-agenda
  15486. (org-uniquify org-todo-keywords-for-agenda))
  15487. (setq org-todo-keyword-alist-for-agenda
  15488. (org-uniquify org-todo-keyword-alist-for-agenda)
  15489. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  15490. ;;;; Embedded LaTeX
  15491. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  15492. "Keymap for the minor `org-cdlatex-mode'.")
  15493. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  15494. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  15495. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  15496. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  15497. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  15498. (defvar org-cdlatex-texmathp-advice-is-done nil
  15499. "Flag remembering if we have applied the advice to texmathp already.")
  15500. (define-minor-mode org-cdlatex-mode
  15501. "Toggle the minor `org-cdlatex-mode'.
  15502. This mode supports entering LaTeX environment and math in LaTeX fragments
  15503. in Org-mode.
  15504. \\{org-cdlatex-mode-map}"
  15505. nil " OCDL" nil
  15506. (when org-cdlatex-mode
  15507. (require 'cdlatex)
  15508. (run-hooks 'cdlatex-mode-hook)
  15509. (cdlatex-compute-tables))
  15510. (unless org-cdlatex-texmathp-advice-is-done
  15511. (setq org-cdlatex-texmathp-advice-is-done t)
  15512. (defadvice texmathp (around org-math-always-on activate)
  15513. "Always return t in org-mode buffers.
  15514. This is because we want to insert math symbols without dollars even outside
  15515. the LaTeX math segments. If Orgmode thinks that point is actually inside
  15516. an embedded LaTeX fragment, let texmathp do its job.
  15517. \\[org-cdlatex-mode-map]"
  15518. (interactive)
  15519. (let (p)
  15520. (cond
  15521. ((not (derived-mode-p 'org-mode)) ad-do-it)
  15522. ((eq this-command 'cdlatex-math-symbol)
  15523. (setq ad-return-value t
  15524. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  15525. (t
  15526. (let ((p (org-inside-LaTeX-fragment-p)))
  15527. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  15528. (setq ad-return-value t
  15529. texmathp-why '("Org-mode embedded math" . 0))
  15530. (if p ad-do-it)))))))))
  15531. (defun turn-on-org-cdlatex ()
  15532. "Unconditionally turn on `org-cdlatex-mode'."
  15533. (org-cdlatex-mode 1))
  15534. (defun org-inside-LaTeX-fragment-p ()
  15535. "Test if point is inside a LaTeX fragment.
  15536. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  15537. sequence appearing also before point.
  15538. Even though the matchers for math are configurable, this function assumes
  15539. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  15540. delimiters are skipped when they have been removed by customization.
  15541. The return value is nil, or a cons cell with the delimiter and the
  15542. position of this delimiter.
  15543. This function does a reasonably good job, but can locally be fooled by
  15544. for example currency specifications. For example it will assume being in
  15545. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  15546. fragments that are properly closed, but during editing, we have to live
  15547. with the uncertainty caused by missing closing delimiters. This function
  15548. looks only before point, not after."
  15549. (catch 'exit
  15550. (let ((pos (point))
  15551. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  15552. (lim (progn
  15553. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  15554. (point)))
  15555. dd-on str (start 0) m re)
  15556. (goto-char pos)
  15557. (when dodollar
  15558. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  15559. re (nth 1 (assoc "$" org-latex-regexps)))
  15560. (while (string-match re str start)
  15561. (cond
  15562. ((= (match-end 0) (length str))
  15563. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  15564. ((= (match-end 0) (- (length str) 5))
  15565. (throw 'exit nil))
  15566. (t (setq start (match-end 0))))))
  15567. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  15568. (goto-char pos)
  15569. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  15570. (and (match-beginning 2) (throw 'exit nil))
  15571. ;; count $$
  15572. (while (re-search-backward "\\$\\$" lim t)
  15573. (setq dd-on (not dd-on)))
  15574. (goto-char pos)
  15575. (if dd-on (cons "$$" m))))))
  15576. (defun org-inside-latex-macro-p ()
  15577. "Is point inside a LaTeX macro or its arguments?"
  15578. (save-match-data
  15579. (org-in-regexp
  15580. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  15581. (defun org-try-cdlatex-tab ()
  15582. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  15583. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  15584. - inside a LaTeX fragment, or
  15585. - after the first word in a line, where an abbreviation expansion could
  15586. insert a LaTeX environment."
  15587. (when org-cdlatex-mode
  15588. (cond
  15589. ;; Before any word on the line: No expansion possible.
  15590. ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
  15591. ;; Just after first word on the line: Expand it. Make sure it
  15592. ;; cannot happen on headlines, though.
  15593. ((save-excursion
  15594. (skip-chars-backward "a-zA-Z0-9*")
  15595. (skip-chars-backward " \t")
  15596. (and (bolp) (not (org-at-heading-p))))
  15597. (cdlatex-tab) t)
  15598. ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
  15599. (defun org-cdlatex-underscore-caret (&optional arg)
  15600. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  15601. Revert to the normal definition outside of these fragments."
  15602. (interactive "P")
  15603. (if (org-inside-LaTeX-fragment-p)
  15604. (call-interactively 'cdlatex-sub-superscript)
  15605. (let (org-cdlatex-mode)
  15606. (call-interactively (key-binding (vector last-input-event))))))
  15607. (defun org-cdlatex-math-modify (&optional arg)
  15608. "Execute `cdlatex-math-modify' in LaTeX fragments.
  15609. Revert to the normal definition outside of these fragments."
  15610. (interactive "P")
  15611. (if (org-inside-LaTeX-fragment-p)
  15612. (call-interactively 'cdlatex-math-modify)
  15613. (let (org-cdlatex-mode)
  15614. (call-interactively (key-binding (vector last-input-event))))))
  15615. (defvar org-latex-fragment-image-overlays nil
  15616. "List of overlays carrying the images of latex fragments.")
  15617. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  15618. (defun org-remove-latex-fragment-image-overlays ()
  15619. "Remove all overlays with LaTeX fragment images in current buffer."
  15620. (mapc 'delete-overlay org-latex-fragment-image-overlays)
  15621. (setq org-latex-fragment-image-overlays nil))
  15622. (defun org-preview-latex-fragment (&optional subtree)
  15623. "Preview the LaTeX fragment at point, or all locally or globally.
  15624. If the cursor is in a LaTeX fragment, create the image and overlay
  15625. it over the source code. If there is no fragment at point, display
  15626. all fragments in the current text, from one headline to the next. With
  15627. prefix SUBTREE, display all fragments in the current subtree. With a
  15628. double prefix arg \\[universal-argument] \\[universal-argument], or when \
  15629. the cursor is before the first headline,
  15630. display all fragments in the buffer.
  15631. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  15632. (interactive "P")
  15633. (unless buffer-file-name
  15634. (error "Can't preview LaTeX fragment in a non-file buffer"))
  15635. (org-remove-latex-fragment-image-overlays)
  15636. (save-excursion
  15637. (save-restriction
  15638. (let (beg end at msg)
  15639. (cond
  15640. ((or (equal subtree '(16))
  15641. (not (save-excursion
  15642. (re-search-backward org-outline-regexp-bol nil t))))
  15643. (setq beg (point-min) end (point-max)
  15644. msg "Creating images for buffer...%s"))
  15645. ((equal subtree '(4))
  15646. (org-back-to-heading)
  15647. (setq beg (point) end (org-end-of-subtree t)
  15648. msg "Creating images for subtree...%s"))
  15649. (t
  15650. (if (setq at (org-inside-LaTeX-fragment-p))
  15651. (goto-char (max (point-min) (- (cdr at) 2)))
  15652. (org-back-to-heading))
  15653. (setq beg (point) end (progn (outline-next-heading) (point))
  15654. msg (if at "Creating image...%s"
  15655. "Creating images for entry...%s"))))
  15656. (message msg "")
  15657. (narrow-to-region beg end)
  15658. (goto-char beg)
  15659. (org-format-latex
  15660. (concat "ltxpng/" (file-name-sans-extension
  15661. (file-name-nondirectory
  15662. buffer-file-name)))
  15663. default-directory 'overlays msg at 'forbuffer
  15664. org-latex-create-formula-image-program)
  15665. (message msg "done. Use `C-c C-c' to remove images.")))))
  15666. (defvar org-latex-regexps
  15667. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  15668. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  15669. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  15670. ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  15671. ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  15672. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  15673. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  15674. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  15675. "Regular expressions for matching embedded LaTeX.")
  15676. (defvar org-export-have-math nil) ;; dynamic scoping
  15677. (defun org-format-latex (prefix &optional dir overlays msg at
  15678. forbuffer processing-type)
  15679. "Replace LaTeX fragments with links to an image, and produce images.
  15680. Some of the options can be changed using the variable
  15681. `org-format-latex-options'."
  15682. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  15683. (let* ((prefixnodir (file-name-nondirectory prefix))
  15684. (absprefix (expand-file-name prefix dir))
  15685. (todir (file-name-directory absprefix))
  15686. (opt org-format-latex-options)
  15687. (matchers (plist-get opt :matchers))
  15688. (re-list org-latex-regexps)
  15689. (org-format-latex-header-extra
  15690. (plist-get (org-infile-export-plist) :latex-header-extra))
  15691. (cnt 0) txt hash link beg end re e checkdir
  15692. executables-checked string
  15693. m n block-type block linkfile movefile ov)
  15694. ;; Check the different regular expressions
  15695. (while (setq e (pop re-list))
  15696. (setq m (car e) re (nth 1 e) n (nth 2 e) block-type (nth 3 e)
  15697. block (if block-type "\n\n" ""))
  15698. (when (member m matchers)
  15699. (goto-char (point-min))
  15700. (while (re-search-forward re nil t)
  15701. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  15702. (not (get-text-property (match-beginning n)
  15703. 'org-protected))
  15704. (or (not overlays)
  15705. (not (eq (get-char-property (match-beginning n)
  15706. 'org-overlay-type)
  15707. 'org-latex-overlay))))
  15708. (setq org-export-have-math t)
  15709. (cond
  15710. ((eq processing-type 'verbatim)
  15711. ;; Leave the text verbatim, just protect it
  15712. (add-text-properties (match-beginning n) (match-end n)
  15713. '(org-protected t)))
  15714. ((eq processing-type 'mathjax)
  15715. ;; Prepare for MathJax processing
  15716. (setq string (match-string n))
  15717. (if (member m '("$" "$1"))
  15718. (save-excursion
  15719. (delete-region (match-beginning n) (match-end n))
  15720. (goto-char (match-beginning n))
  15721. (insert (org-add-props (concat "\\(" (substring string 1 -1)
  15722. "\\)")
  15723. '(org-protected t))))
  15724. (add-text-properties (match-beginning n) (match-end n)
  15725. '(org-protected t))))
  15726. ((or (eq processing-type 'dvipng)
  15727. (eq processing-type 'imagemagick))
  15728. ;; Process to an image
  15729. (setq txt (match-string n)
  15730. beg (match-beginning n) end (match-end n)
  15731. cnt (1+ cnt))
  15732. (let (print-length print-level) ; make sure full list is printed
  15733. (setq hash (sha1 (prin1-to-string
  15734. (list org-format-latex-header
  15735. org-format-latex-header-extra
  15736. org-export-latex-default-packages-alist
  15737. org-export-latex-packages-alist
  15738. org-format-latex-options
  15739. forbuffer txt)))
  15740. linkfile (format "%s_%s.png" prefix hash)
  15741. movefile (format "%s_%s.png" absprefix hash)))
  15742. (setq link (concat block "[[file:" linkfile "]]" block))
  15743. (if msg (message msg cnt))
  15744. (goto-char beg)
  15745. (unless checkdir ; make sure the directory exists
  15746. (setq checkdir t)
  15747. (or (file-directory-p todir) (make-directory todir t)))
  15748. (cond
  15749. ((eq processing-type 'dvipng)
  15750. (unless executables-checked
  15751. (org-check-external-command
  15752. "latex" "needed to convert LaTeX fragments to images")
  15753. (org-check-external-command
  15754. "dvipng" "needed to convert LaTeX fragments to images")
  15755. (setq executables-checked t))
  15756. (unless (file-exists-p movefile)
  15757. (org-create-formula-image-with-dvipng
  15758. txt movefile opt forbuffer)))
  15759. ((eq processing-type 'imagemagick)
  15760. (unless executables-checked
  15761. (org-check-external-command
  15762. "convert" "you need to install imagemagick")
  15763. (setq executables-checked t))
  15764. (unless (file-exists-p movefile)
  15765. (org-create-formula-image-with-imagemagick
  15766. txt movefile opt forbuffer))))
  15767. (if overlays
  15768. (progn
  15769. (mapc (lambda (o)
  15770. (if (eq (overlay-get o 'org-overlay-type)
  15771. 'org-latex-overlay)
  15772. (delete-overlay o)))
  15773. (overlays-in beg end))
  15774. (setq ov (make-overlay beg end))
  15775. (overlay-put ov 'org-overlay-type 'org-latex-overlay)
  15776. (if (featurep 'xemacs)
  15777. (progn
  15778. (overlay-put ov 'invisible t)
  15779. (overlay-put
  15780. ov 'end-glyph
  15781. (make-glyph (vector 'png :file movefile))))
  15782. (overlay-put
  15783. ov 'display
  15784. (list 'image :type 'png :file movefile :ascent 'center)))
  15785. (push ov org-latex-fragment-image-overlays)
  15786. (goto-char end))
  15787. (delete-region beg end)
  15788. (insert (org-add-props link
  15789. (list 'org-latex-src
  15790. (replace-regexp-in-string
  15791. "\"" "" txt)
  15792. 'org-latex-src-embed-type
  15793. (if block-type 'paragraph 'character))))))
  15794. ((eq processing-type 'mathml)
  15795. ;; Process to MathML
  15796. (unless executables-checked
  15797. (unless (save-match-data (org-format-latex-mathml-available-p))
  15798. (error "LaTeX to MathML converter not configured"))
  15799. (setq executables-checked t))
  15800. (setq txt (match-string n)
  15801. beg (match-beginning n) end (match-end n)
  15802. cnt (1+ cnt))
  15803. (if msg (message msg cnt))
  15804. (goto-char beg)
  15805. (delete-region beg end)
  15806. (insert (org-format-latex-as-mathml
  15807. txt block-type prefix dir)))
  15808. (t
  15809. (error "Unknown conversion type %s for latex fragments"
  15810. processing-type)))))))))
  15811. (defun org-create-math-formula (latex-frag &optional mathml-file)
  15812. "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
  15813. Use `org-latex-to-mathml-convert-command'. If the conversion is
  15814. sucessful, return the portion between \"<math...> </math>\"
  15815. elements otherwise return nil. When MATHML-FILE is specified,
  15816. write the results in to that file. When invoked as an
  15817. interactive command, prompt for LATEX-FRAG, with initial value
  15818. set to the current active region and echo the results for user
  15819. inspection."
  15820. (interactive (list (let ((frag (when (region-active-p)
  15821. (buffer-substring-no-properties
  15822. (region-beginning) (region-end)))))
  15823. (read-string "LaTeX Fragment: " frag nil frag))))
  15824. (unless latex-frag (error "Invalid latex-frag"))
  15825. (let* ((tmp-in-file (file-relative-name
  15826. (make-temp-name (expand-file-name "ltxmathml-in"))))
  15827. (ignore (write-region latex-frag nil tmp-in-file))
  15828. (tmp-out-file (file-relative-name
  15829. (make-temp-name (expand-file-name "ltxmathml-out"))))
  15830. (cmd (format-spec
  15831. org-latex-to-mathml-convert-command
  15832. `((?j . ,(shell-quote-argument
  15833. (expand-file-name org-latex-to-mathml-jar-file)))
  15834. (?I . ,(shell-quote-argument tmp-in-file))
  15835. (?o . ,(shell-quote-argument tmp-out-file)))))
  15836. mathml shell-command-output)
  15837. (when (org-called-interactively-p 'any)
  15838. (unless (org-format-latex-mathml-available-p)
  15839. (error "LaTeX to MathML converter not configured")))
  15840. (message "Running %s" cmd)
  15841. (setq shell-command-output (shell-command-to-string cmd))
  15842. (setq mathml
  15843. (when (file-readable-p tmp-out-file)
  15844. (with-current-buffer (find-file-noselect tmp-out-file t)
  15845. (goto-char (point-min))
  15846. (when (re-search-forward
  15847. (concat
  15848. (regexp-quote
  15849. "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">")
  15850. "\\(.\\|\n\\)*"
  15851. (regexp-quote "</math>")) nil t)
  15852. (prog1 (match-string 0) (kill-buffer))))))
  15853. (cond
  15854. (mathml
  15855. (setq mathml
  15856. (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
  15857. (when mathml-file
  15858. (write-region mathml nil mathml-file))
  15859. (when (org-called-interactively-p 'any)
  15860. (message mathml)))
  15861. ((message "LaTeX to MathML conversion failed")
  15862. (message shell-command-output)))
  15863. (delete-file tmp-in-file)
  15864. (when (file-exists-p tmp-out-file)
  15865. (delete-file tmp-out-file))
  15866. mathml))
  15867. (defun org-format-latex-as-mathml (latex-frag latex-frag-type
  15868. prefix &optional dir)
  15869. "Use `org-create-math-formula' but check local cache first."
  15870. (let* ((absprefix (expand-file-name prefix dir))
  15871. (print-length nil) (print-level nil)
  15872. (formula-id (concat
  15873. "formula-"
  15874. (sha1
  15875. (prin1-to-string
  15876. (list latex-frag
  15877. org-latex-to-mathml-convert-command)))))
  15878. (formula-cache (format "%s-%s.mathml" absprefix formula-id))
  15879. (formula-cache-dir (file-name-directory formula-cache)))
  15880. (unless (file-directory-p formula-cache-dir)
  15881. (make-directory formula-cache-dir t))
  15882. (unless (file-exists-p formula-cache)
  15883. (org-create-math-formula latex-frag formula-cache))
  15884. (if (file-exists-p formula-cache)
  15885. ;; Successful conversion. Return the link to MathML file.
  15886. (org-add-props
  15887. (format "[[file:%s]]" (file-relative-name formula-cache dir))
  15888. (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
  15889. 'org-latex-src-embed-type (if latex-frag-type
  15890. 'paragraph 'character)))
  15891. ;; Failed conversion. Return the LaTeX fragment verbatim
  15892. (add-text-properties
  15893. 0 (1- (length latex-frag)) '(org-protected t) latex-frag)
  15894. latex-frag)))
  15895. ;; This function borrows from Ganesh Swami's latex2png.el
  15896. (defun org-create-formula-image-with-dvipng (string tofile options buffer)
  15897. "This calls dvipng."
  15898. (require 'org-latex)
  15899. (let* ((tmpdir (if (featurep 'xemacs)
  15900. (temp-directory)
  15901. temporary-file-directory))
  15902. (texfilebase (make-temp-name
  15903. (expand-file-name "orgtex" tmpdir)))
  15904. (texfile (concat texfilebase ".tex"))
  15905. (dvifile (concat texfilebase ".dvi"))
  15906. (pngfile (concat texfilebase ".png"))
  15907. (fnh (if (featurep 'xemacs)
  15908. (font-height (face-font 'default))
  15909. (face-attribute 'default :height nil)))
  15910. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  15911. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  15912. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  15913. "Black"))
  15914. (bg (or (plist-get options (if buffer :background :html-background))
  15915. "Transparent")))
  15916. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  15917. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  15918. (with-temp-file texfile
  15919. (insert (org-splice-latex-header
  15920. org-format-latex-header
  15921. org-export-latex-default-packages-alist
  15922. org-export-latex-packages-alist t
  15923. org-format-latex-header-extra))
  15924. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")
  15925. (require 'org-latex)
  15926. (org-export-latex-fix-inputenc))
  15927. (let ((dir default-directory))
  15928. (condition-case nil
  15929. (progn
  15930. (cd tmpdir)
  15931. (call-process "latex" nil nil nil texfile))
  15932. (error nil))
  15933. (cd dir))
  15934. (if (not (file-exists-p dvifile))
  15935. (progn (message "Failed to create dvi file from %s" texfile) nil)
  15936. (condition-case nil
  15937. (if (featurep 'xemacs)
  15938. (call-process "dvipng" nil nil nil
  15939. "-fg" fg "-bg" bg
  15940. "-T" "tight"
  15941. "-o" pngfile
  15942. dvifile)
  15943. (call-process "dvipng" nil nil nil
  15944. "-fg" fg "-bg" bg
  15945. "-D" dpi
  15946. ;;"-x" scale "-y" scale
  15947. "-T" "tight"
  15948. "-o" pngfile
  15949. dvifile))
  15950. (error nil))
  15951. (if (not (file-exists-p pngfile))
  15952. (if org-format-latex-signal-error
  15953. (error "Failed to create png file from %s" texfile)
  15954. (message "Failed to create png file from %s" texfile)
  15955. nil)
  15956. ;; Use the requested file name and clean up
  15957. (copy-file pngfile tofile 'replace)
  15958. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png" ".out") do
  15959. (if (file-exists-p (concat texfilebase e))
  15960. (delete-file (concat texfilebase e))))
  15961. pngfile))))
  15962. (defvar org-latex-to-pdf-process) ;; Defined in org-latex.el
  15963. (defun org-create-formula-image-with-imagemagick (string tofile options buffer)
  15964. "This calls convert, which is included into imagemagick."
  15965. (require 'org-latex)
  15966. (let* ((tmpdir (if (featurep 'xemacs)
  15967. (temp-directory)
  15968. temporary-file-directory))
  15969. (texfilebase (make-temp-name
  15970. (expand-file-name "orgtex" tmpdir)))
  15971. (texfile (concat texfilebase ".tex"))
  15972. (pdffile (concat texfilebase ".pdf"))
  15973. (pngfile (concat texfilebase ".png"))
  15974. (fnh (if (featurep 'xemacs)
  15975. (font-height (face-font 'default))
  15976. (face-attribute 'default :height nil)))
  15977. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  15978. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  15979. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  15980. "black"))
  15981. (bg (or (plist-get options (if buffer :background :html-background))
  15982. "white")))
  15983. (if (eq fg 'default) (setq fg (org-latex-color :foreground))
  15984. (setq fg (org-latex-color-format fg)))
  15985. (if (eq bg 'default) (setq bg (org-latex-color :background))
  15986. (setq bg (org-latex-color-format
  15987. (if (string= bg "Transparent")(setq bg "white")))))
  15988. (with-temp-file texfile
  15989. (insert (org-splice-latex-header
  15990. org-format-latex-header
  15991. org-export-latex-default-packages-alist
  15992. org-export-latex-packages-alist t
  15993. org-format-latex-header-extra))
  15994. (insert "\n\\begin{document}\n"
  15995. "\\definecolor{fg}{rgb}{" fg "}\n"
  15996. "\\definecolor{bg}{rgb}{" bg "}\n"
  15997. "\n\\pagecolor{bg}\n"
  15998. "\n{\\color{fg}\n"
  15999. string
  16000. "\n}\n"
  16001. "\n\\end{document}\n" )
  16002. (require 'org-latex)
  16003. (org-export-latex-fix-inputenc))
  16004. (let ((dir default-directory) cmd cmds latex-frags-cmds)
  16005. (condition-case nil
  16006. (progn
  16007. (cd tmpdir)
  16008. (setq cmds org-latex-to-pdf-process)
  16009. (while cmds
  16010. (setq latex-frags-cmds (pop cmds))
  16011. (if (listp latex-frags-cmds)
  16012. (setq cmds nil)
  16013. (setq latex-frags-cmds (list (car org-latex-to-pdf-process)))))
  16014. (while latex-frags-cmds
  16015. (setq cmd (pop latex-frags-cmds))
  16016. (while (string-match "%b" cmd)
  16017. (setq cmd (replace-match
  16018. (save-match-data
  16019. (shell-quote-argument texfile))
  16020. t t cmd)))
  16021. (while (string-match "%f" cmd)
  16022. (setq cmd (replace-match
  16023. (save-match-data
  16024. (shell-quote-argument (file-name-nondirectory texfile)))
  16025. t t cmd)))
  16026. (while (string-match "%o" cmd)
  16027. (setq cmd (replace-match
  16028. (save-match-data
  16029. (shell-quote-argument (file-name-directory texfile)))
  16030. t t cmd)))
  16031. (setq cmd (split-string cmd))
  16032. (eval (append (list 'call-process (pop cmd) nil nil nil) cmd))))
  16033. (error nil))
  16034. (cd dir))
  16035. (if (not (file-exists-p pdffile))
  16036. (progn (message "Failed to create pdf file from %s" texfile) nil)
  16037. (condition-case nil
  16038. (if (featurep 'xemacs)
  16039. (call-process "convert" nil nil nil
  16040. "-density" "96"
  16041. "-trim"
  16042. "-antialias"
  16043. pdffile
  16044. "-quality" "100"
  16045. ;; "-sharpen" "0x1.0"
  16046. pngfile)
  16047. (call-process "convert" nil nil nil
  16048. "-density" dpi
  16049. "-trim"
  16050. "-antialias"
  16051. pdffile
  16052. "-quality" "100"
  16053. ; "-sharpen" "0x1.0"
  16054. pngfile))
  16055. (error nil))
  16056. (if (not (file-exists-p pngfile))
  16057. (if org-format-latex-signal-error
  16058. (error "Failed to create png file from %s" texfile)
  16059. (message "Failed to create png file from %s" texfile)
  16060. nil)
  16061. ;; Use the requested file name and clean up
  16062. (copy-file pngfile tofile 'replace)
  16063. (loop for e in '(".pdf" ".tex" ".aux" ".log" ".png") do
  16064. (if (file-exists-p (concat texfilebase e))
  16065. (delete-file (concat texfilebase e))))
  16066. pngfile))))
  16067. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  16068. "Fill a LaTeX header template TPL.
  16069. In the template, the following place holders will be recognized:
  16070. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  16071. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  16072. [PACKAGES] \\usepackage statements for PKG
  16073. [NO-PACKAGES] do not include PKG
  16074. [EXTRA] the string EXTRA
  16075. [NO-EXTRA] do not include EXTRA
  16076. For backward compatibility, if both the positive and the negative place
  16077. holder is missing, the positive one (without the \"NO-\") will be
  16078. assumed to be present at the end of the template.
  16079. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  16080. EXTRA is a string.
  16081. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  16082. (let (rpl (end ""))
  16083. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  16084. (setq rpl (if (or (match-end 1) (not def-pkg))
  16085. "" (org-latex-packages-to-string def-pkg snippets-p t))
  16086. tpl (replace-match rpl t t tpl))
  16087. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  16088. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  16089. (setq rpl (if (or (match-end 1) (not pkg))
  16090. "" (org-latex-packages-to-string pkg snippets-p t))
  16091. tpl (replace-match rpl t t tpl))
  16092. (if pkg (setq end
  16093. (concat end "\n"
  16094. (org-latex-packages-to-string pkg snippets-p)))))
  16095. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  16096. (setq rpl (if (or (match-end 1) (not extra))
  16097. "" (concat extra "\n"))
  16098. tpl (replace-match rpl t t tpl))
  16099. (if (and extra (string-match "\\S-" extra))
  16100. (setq end (concat end "\n" extra))))
  16101. (if (string-match "\\S-" end)
  16102. (concat tpl "\n" end)
  16103. tpl)))
  16104. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  16105. "Turn an alist of packages into a string with the \\usepackage macros."
  16106. (setq pkg (mapconcat (lambda(p)
  16107. (cond
  16108. ((stringp p) p)
  16109. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  16110. (format "%% Package %s omitted" (cadr p)))
  16111. ((equal "" (car p))
  16112. (format "\\usepackage{%s}" (cadr p)))
  16113. (t
  16114. (format "\\usepackage[%s]{%s}"
  16115. (car p) (cadr p)))))
  16116. pkg
  16117. "\n"))
  16118. (if newline (concat pkg "\n") pkg))
  16119. (defun org-dvipng-color (attr)
  16120. "Return a RGB color specification for dvipng."
  16121. (apply 'format "rgb %s %s %s"
  16122. (mapcar 'org-normalize-color
  16123. (if (featurep 'xemacs)
  16124. (color-rgb-components
  16125. (face-property 'default
  16126. (cond ((eq attr :foreground) 'foreground)
  16127. ((eq attr :background) 'background))))
  16128. (color-values (face-attribute 'default attr nil))))))
  16129. (defun org-latex-color (attr)
  16130. "Return a RGB color for the LaTeX color package."
  16131. (apply 'format "%s,%s,%s"
  16132. (mapcar 'org-normalize-color
  16133. (if (featurep 'xemacs)
  16134. (color-rgb-components
  16135. (face-property 'default
  16136. (cond ((eq attr :foreground) 'foreground)
  16137. ((eq attr :background) 'background))))
  16138. (color-values (face-attribute 'default attr nil))))))
  16139. (defun org-latex-color-format (color-name)
  16140. "Convert COLOR-NAME to a RGB color value."
  16141. (apply 'format "%s,%s,%s"
  16142. (mapcar 'org-normalize-color
  16143. (color-values color-name))))
  16144. (defun org-normalize-color (value)
  16145. "Return string to be used as color value for an RGB component."
  16146. (format "%g" (/ value 65535.0)))
  16147. ;; Image display
  16148. (defvar org-inline-image-overlays nil)
  16149. (make-variable-buffer-local 'org-inline-image-overlays)
  16150. (defun org-toggle-inline-images (&optional include-linked)
  16151. "Toggle the display of inline images.
  16152. INCLUDE-LINKED is passed to `org-display-inline-images'."
  16153. (interactive "P")
  16154. (if org-inline-image-overlays
  16155. (progn
  16156. (org-remove-inline-images)
  16157. (message "Inline image display turned off"))
  16158. (org-display-inline-images include-linked)
  16159. (if org-inline-image-overlays
  16160. (message "%d images displayed inline"
  16161. (length org-inline-image-overlays))
  16162. (message "No images to display inline"))))
  16163. (defun org-display-inline-images (&optional include-linked refresh beg end)
  16164. "Display inline images.
  16165. Normally only links without a description part are inlined, because this
  16166. is how it will work for export. When INCLUDE-LINKED is set, also links
  16167. with a description part will be inlined. This can be nice for a quick
  16168. look at those images, but it does not reflect what exported files will look
  16169. like.
  16170. When REFRESH is set, refresh existing images between BEG and END.
  16171. This will create new image displays only if necessary.
  16172. BEG and END default to the buffer boundaries."
  16173. (interactive "P")
  16174. (unless refresh
  16175. (org-remove-inline-images)
  16176. (if (fboundp 'clear-image-cache) (clear-image-cache)))
  16177. (save-excursion
  16178. (save-restriction
  16179. (widen)
  16180. (setq beg (or beg (point-min)) end (or end (point-max)))
  16181. (goto-char beg)
  16182. (let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([^]\n]+?"
  16183. (substring (org-image-file-name-regexp) 0 -2)
  16184. "\\)\\]" (if include-linked "" "\\]")))
  16185. old file ov img)
  16186. (while (re-search-forward re end t)
  16187. (setq old (get-char-property-and-overlay (match-beginning 1)
  16188. 'org-image-overlay))
  16189. (setq file (expand-file-name
  16190. (concat (or (match-string 3) "") (match-string 4))))
  16191. (when (file-exists-p file)
  16192. (if (and (car-safe old) refresh)
  16193. (image-refresh (overlay-get (cdr old) 'display))
  16194. (setq img (save-match-data (create-image file)))
  16195. (when img
  16196. (setq ov (make-overlay (match-beginning 0) (match-end 0)))
  16197. (overlay-put ov 'display img)
  16198. (overlay-put ov 'face 'default)
  16199. (overlay-put ov 'org-image-overlay t)
  16200. (overlay-put ov 'modification-hooks
  16201. (list 'org-display-inline-modification-hook))
  16202. (push ov org-inline-image-overlays)))))))))
  16203. (defun org-display-inline-modification-hook (ov after beg end &optional len)
  16204. "Remove inline-display overlay if a corresponding region is modified."
  16205. (let ((inhibit-modification-hooks t))
  16206. (when (and ov after)
  16207. (delete ov org-inline-image-overlays)
  16208. (delete-overlay ov))))
  16209. (defun org-remove-inline-images ()
  16210. "Remove inline display of images."
  16211. (interactive)
  16212. (mapc 'delete-overlay org-inline-image-overlays)
  16213. (setq org-inline-image-overlays nil))
  16214. ;;;; Key bindings
  16215. ;; Outline functions from `outline-mode-prefix-map'
  16216. ;; that can be remapped in Org:
  16217. (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
  16218. (define-key org-mode-map [remap show-subtree] 'org-show-subtree)
  16219. (define-key org-mode-map [remap outline-forward-same-level]
  16220. 'org-forward-same-level)
  16221. (define-key org-mode-map [remap outline-backward-same-level]
  16222. 'org-backward-same-level)
  16223. (define-key org-mode-map [remap show-branches]
  16224. 'org-kill-note-or-show-branches)
  16225. (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
  16226. (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
  16227. (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
  16228. ;; Outline functions from `outline-mode-prefix-map' that can not
  16229. ;; be remapped in Org:
  16230. ;;
  16231. ;; - the column "key binding" shows whether the Outline function is still
  16232. ;; available in Org mode on the same key that it has been bound to in
  16233. ;; Outline mode:
  16234. ;; - "overridden": key used for a different functionality in Org mode
  16235. ;; - else: key still bound to the same Outline function in Org mode
  16236. ;;
  16237. ;; | Outline function | key binding | Org replacement |
  16238. ;; |------------------------------------+-------------+-----------------------|
  16239. ;; | `outline-next-visible-heading' | `C-c C-n' | still same function |
  16240. ;; | `outline-previous-visible-heading' | `C-c C-p' | still same function |
  16241. ;; | `outline-up-heading' | `C-c C-u' | still same function |
  16242. ;; | `outline-move-subtree-up' | `C-c C-^' | better: org-shiftup |
  16243. ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
  16244. ;; | `show-entry' | overridden | no replacement |
  16245. ;; | `show-children' | `C-c C-i' | visibility cycling |
  16246. ;; | `show-branches' | `C-c C-k' | still same function |
  16247. ;; | `show-subtree' | overridden | visibility cycling |
  16248. ;; | `show-all' | overridden | no replacement |
  16249. ;; | `hide-subtree' | overridden | visibility cycling |
  16250. ;; | `hide-body' | overridden | no replacement |
  16251. ;; | `hide-entry' | overridden | visibility cycling |
  16252. ;; | `hide-leaves' | overridden | no replacement |
  16253. ;; | `hide-sublevels' | overridden | no replacement |
  16254. ;; | `hide-other' | overridden | no replacement |
  16255. ;; Make `C-c C-x' a prefix key
  16256. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  16257. ;; TAB key with modifiers
  16258. (org-defkey org-mode-map "\C-i" 'org-cycle)
  16259. (org-defkey org-mode-map [(tab)] 'org-cycle)
  16260. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  16261. (org-defkey org-mode-map "\M-\t" 'pcomplete)
  16262. ;; The following line is necessary under Suse GNU/Linux
  16263. (unless (featurep 'xemacs)
  16264. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  16265. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  16266. (define-key org-mode-map [backtab] 'org-shifttab)
  16267. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  16268. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  16269. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  16270. ;; Cursor keys with modifiers
  16271. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  16272. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  16273. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  16274. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  16275. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  16276. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  16277. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  16278. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  16279. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  16280. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  16281. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  16282. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  16283. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  16284. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  16285. (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
  16286. (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
  16287. ;; Babel keys
  16288. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  16289. (mapc (lambda (pair)
  16290. (define-key org-babel-map (car pair) (cdr pair)))
  16291. org-babel-key-bindings)
  16292. ;;; Extra keys for tty access.
  16293. ;; We only set them when really needed because otherwise the
  16294. ;; menus don't show the simple keys
  16295. (when (or org-use-extra-keys
  16296. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  16297. (not window-system))
  16298. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  16299. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  16300. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  16301. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  16302. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  16303. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  16304. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  16305. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  16306. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  16307. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  16308. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  16309. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  16310. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  16311. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  16312. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  16313. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  16314. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  16315. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  16316. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  16317. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  16318. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  16319. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  16320. (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
  16321. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  16322. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  16323. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  16324. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  16325. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  16326. ;; All the other keys
  16327. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  16328. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  16329. (if (boundp 'narrow-map)
  16330. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  16331. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  16332. (if (boundp 'narrow-map)
  16333. (org-defkey narrow-map "b" 'org-narrow-to-block)
  16334. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  16335. (if (boundp 'narrow-map)
  16336. (org-defkey narrow-map "e" 'org-narrow-to-element)
  16337. (org-defkey org-mode-map "\C-xne" 'org-narrow-to-element))
  16338. (org-defkey org-mode-map "\C-\M-t" 'org-element-transpose)
  16339. (org-defkey org-mode-map "\C-\M-f" 'org-element-forward)
  16340. (org-defkey org-mode-map "\C-\M-b" 'org-element-backward)
  16341. (org-defkey org-mode-map "\C-\M-u" 'org-element-up)
  16342. (org-defkey org-mode-map "\C-\M-d" 'org-element-down)
  16343. (org-defkey org-mode-map "\C-\M-t" 'org-element-transpose)
  16344. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-same-level)
  16345. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-same-level)
  16346. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  16347. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  16348. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  16349. (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
  16350. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  16351. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  16352. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  16353. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  16354. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  16355. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  16356. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  16357. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  16358. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  16359. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  16360. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  16361. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  16362. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  16363. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  16364. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  16365. (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
  16366. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  16367. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  16368. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  16369. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  16370. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  16371. (org-defkey org-mode-map "\C-c\C-\M-l" 'org-insert-all-links)
  16372. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  16373. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  16374. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  16375. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  16376. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  16377. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  16378. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  16379. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  16380. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  16381. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  16382. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  16383. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  16384. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  16385. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  16386. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  16387. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  16388. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  16389. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  16390. (org-defkey org-mode-map "\C-c^" 'org-sort)
  16391. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  16392. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  16393. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  16394. (org-defkey org-mode-map "\C-m" 'org-return)
  16395. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  16396. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  16397. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  16398. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  16399. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  16400. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  16401. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  16402. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  16403. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  16404. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  16405. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  16406. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  16407. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  16408. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  16409. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  16410. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  16411. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  16412. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  16413. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  16414. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  16415. (org-defkey org-mode-map "\C-c\C-@" 'org-mark-list)
  16416. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  16417. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  16418. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  16419. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  16420. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  16421. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  16422. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  16423. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  16424. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-in-last)
  16425. (org-defkey org-mode-map "\C-c\C-x\C-z" 'org-resolve-clocks)
  16426. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  16427. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  16428. (org-defkey org-mode-map "\C-c\C-x\C-q" 'org-clock-cancel)
  16429. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  16430. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  16431. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  16432. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  16433. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  16434. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  16435. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  16436. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  16437. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  16438. (org-defkey org-mode-map "\C-c\C-xE" 'org-inc-effort)
  16439. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  16440. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  16441. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  16442. (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
  16443. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  16444. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  16445. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  16446. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  16447. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  16448. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  16449. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  16450. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  16451. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  16452. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  16453. (when (featurep 'xemacs)
  16454. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  16455. (defconst org-speed-commands-default
  16456. '(
  16457. ("Outline Navigation")
  16458. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  16459. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  16460. ("f" . (org-speed-move-safe 'org-forward-same-level))
  16461. ("b" . (org-speed-move-safe 'org-backward-same-level))
  16462. ("u" . (org-speed-move-safe 'outline-up-heading))
  16463. ("j" . org-goto)
  16464. ("g" . (org-refile t))
  16465. ("Outline Visibility")
  16466. ("c" . org-cycle)
  16467. ("C" . org-shifttab)
  16468. (" " . org-display-outline-path)
  16469. (":" . org-columns)
  16470. ("Outline Structure Editing")
  16471. ("U" . org-shiftmetaup)
  16472. ("D" . org-shiftmetadown)
  16473. ("r" . org-metaright)
  16474. ("l" . org-metaleft)
  16475. ("R" . org-shiftmetaright)
  16476. ("L" . org-shiftmetaleft)
  16477. ("i" . (progn (forward-char 1) (call-interactively
  16478. 'org-insert-heading-respect-content)))
  16479. ("^" . org-sort)
  16480. ("w" . org-refile)
  16481. ("a" . org-archive-subtree-default-with-confirmation)
  16482. ("." . org-mark-subtree)
  16483. ("#" . org-toggle-comment)
  16484. ("Clock Commands")
  16485. ("I" . org-clock-in)
  16486. ("O" . org-clock-out)
  16487. ("Meta Data Editing")
  16488. ("t" . org-todo)
  16489. ("0" . (org-priority ?\ ))
  16490. ("1" . (org-priority ?A))
  16491. ("2" . (org-priority ?B))
  16492. ("3" . (org-priority ?C))
  16493. (";" . org-set-tags-command)
  16494. ("e" . org-set-effort)
  16495. ("E" . org-inc-effort)
  16496. ("Agenda Views etc")
  16497. ("v" . org-agenda)
  16498. ("/" . org-sparse-tree)
  16499. ("Misc")
  16500. ("o" . org-open-at-point)
  16501. ("?" . org-speed-command-help)
  16502. ("<" . (org-agenda-set-restriction-lock 'subtree))
  16503. (">" . (org-agenda-remove-restriction-lock))
  16504. )
  16505. "The default speed commands.")
  16506. (defun org-print-speed-command (e)
  16507. (if (> (length (car e)) 1)
  16508. (progn
  16509. (princ "\n")
  16510. (princ (car e))
  16511. (princ "\n")
  16512. (princ (make-string (length (car e)) ?-))
  16513. (princ "\n"))
  16514. (princ (car e))
  16515. (princ " ")
  16516. (if (symbolp (cdr e))
  16517. (princ (symbol-name (cdr e)))
  16518. (prin1 (cdr e)))
  16519. (princ "\n")))
  16520. (defun org-speed-command-help ()
  16521. "Show the available speed commands."
  16522. (interactive)
  16523. (if (not org-use-speed-commands)
  16524. (error "Speed commands are not activated, customize `org-use-speed-commands'")
  16525. (with-output-to-temp-buffer "*Help*"
  16526. (princ "User-defined Speed commands\n===========================\n")
  16527. (mapc 'org-print-speed-command org-speed-commands-user)
  16528. (princ "\n")
  16529. (princ "Built-in Speed commands\n=======================\n")
  16530. (mapc 'org-print-speed-command org-speed-commands-default))
  16531. (with-current-buffer "*Help*"
  16532. (setq truncate-lines t))))
  16533. (defun org-speed-move-safe (cmd)
  16534. "Execute CMD, but make sure that the cursor always ends up in a headline.
  16535. If not, return to the original position and throw an error."
  16536. (interactive)
  16537. (let ((pos (point)))
  16538. (call-interactively cmd)
  16539. (unless (and (bolp) (org-at-heading-p))
  16540. (goto-char pos)
  16541. (error "Boundary reached while executing %s" cmd))))
  16542. (defvar org-self-insert-command-undo-counter 0)
  16543. (defvar org-table-auto-blank-field) ; defined in org-table.el
  16544. (defvar org-speed-command nil)
  16545. (defun org-speed-command-default-hook (keys)
  16546. "Hook for activating single-letter speed commands.
  16547. `org-speed-commands-default' specifies a minimal command set.
  16548. Use `org-speed-commands-user' for further customization."
  16549. (when (or (and (bolp) (looking-at org-outline-regexp))
  16550. (and (functionp org-use-speed-commands)
  16551. (funcall org-use-speed-commands)))
  16552. (cdr (assoc keys (append org-speed-commands-user
  16553. org-speed-commands-default)))))
  16554. (defun org-babel-speed-command-hook (keys)
  16555. "Hook for activating single-letter code block commands."
  16556. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  16557. (cdr (assoc keys org-babel-key-bindings))))
  16558. (defcustom org-speed-command-hook
  16559. '(org-speed-command-default-hook org-babel-speed-command-hook)
  16560. "Hook for activating speed commands at strategic locations.
  16561. Hook functions are called in sequence until a valid handler is
  16562. found.
  16563. Each hook takes a single argument, a user-pressed command key
  16564. which is also a `self-insert-command' from the global map.
  16565. Within the hook, examine the cursor position and the command key
  16566. and return nil or a valid handler as appropriate. Handler could
  16567. be one of an interactive command, a function, or a form.
  16568. Set `org-use-speed-commands' to non-nil value to enable this
  16569. hook. The default setting is `org-speed-command-default-hook'."
  16570. :group 'org-structure
  16571. :version "24.1"
  16572. :type 'hook)
  16573. (defun org-self-insert-command (N)
  16574. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  16575. If the cursor is in a table looking at whitespace, the whitespace is
  16576. overwritten, and the table is not marked as requiring realignment."
  16577. (interactive "p")
  16578. (org-check-before-invisible-edit 'insert)
  16579. (cond
  16580. ((and org-use-speed-commands
  16581. (setq org-speed-command
  16582. (run-hook-with-args-until-success
  16583. 'org-speed-command-hook (this-command-keys))))
  16584. (cond
  16585. ((commandp org-speed-command)
  16586. (setq this-command org-speed-command)
  16587. (call-interactively org-speed-command))
  16588. ((functionp org-speed-command)
  16589. (funcall org-speed-command))
  16590. ((and org-speed-command (listp org-speed-command))
  16591. (eval org-speed-command))
  16592. (t (let (org-use-speed-commands)
  16593. (call-interactively 'org-self-insert-command)))))
  16594. ((and
  16595. (org-table-p)
  16596. (progn
  16597. ;; check if we blank the field, and if that triggers align
  16598. (and (featurep 'org-table) org-table-auto-blank-field
  16599. (member last-command
  16600. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  16601. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  16602. ;; got extra space, this field does not determine column width
  16603. (let (org-table-may-need-update) (org-table-blank-field))
  16604. ;; no extra space, this field may determine column width
  16605. (org-table-blank-field)))
  16606. t)
  16607. (eq N 1)
  16608. (looking-at "[^|\n]* |"))
  16609. (let (org-table-may-need-update)
  16610. (goto-char (1- (match-end 0)))
  16611. (backward-delete-char 1)
  16612. (goto-char (match-beginning 0))
  16613. (self-insert-command N)))
  16614. (t
  16615. (setq org-table-may-need-update t)
  16616. (self-insert-command N)
  16617. (org-fix-tags-on-the-fly)
  16618. (if org-self-insert-cluster-for-undo
  16619. (if (not (eq last-command 'org-self-insert-command))
  16620. (setq org-self-insert-command-undo-counter 1)
  16621. (if (>= org-self-insert-command-undo-counter 20)
  16622. (setq org-self-insert-command-undo-counter 1)
  16623. (and (> org-self-insert-command-undo-counter 0)
  16624. buffer-undo-list (listp buffer-undo-list)
  16625. (not (cadr buffer-undo-list)) ; remove nil entry
  16626. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  16627. (setq org-self-insert-command-undo-counter
  16628. (1+ org-self-insert-command-undo-counter))))))))
  16629. (defun org-check-before-invisible-edit (kind)
  16630. "Check is editing if kind KIND would be dangerous with invisible text around.
  16631. The detailed reaction depends on the user option `org-catch-invisible-edits'."
  16632. ;; First, try to get out of here as quickly as possible, to reduce overhead
  16633. (if (and org-catch-invisible-edits
  16634. (or (not (boundp 'visible-mode)) (not visible-mode))
  16635. (or (get-char-property (point) 'invisible)
  16636. (get-char-property (max (point-min) (1- (point))) 'invisible)))
  16637. ;; OK, we need to take a closer look
  16638. (let* ((invisible-at-point (get-char-property (point) 'invisible))
  16639. (invisible-before-point (if (bobp) nil (get-char-property
  16640. (1- (point)) 'invisible)))
  16641. (border-and-ok-direction
  16642. (or
  16643. ;; Check if we are acting predictably before invisible text
  16644. (and invisible-at-point (not invisible-before-point)
  16645. (memq kind '(insert delete-backward)))
  16646. ;; Check if we are acting predictably after invisible text
  16647. ;; This works not well, and I have turned it off. It seems
  16648. ;; better to always show and stop after invisible text.
  16649. ;; (and (not invisible-at-point) invisible-before-point
  16650. ;; (memq kind '(insert delete)))
  16651. )))
  16652. (when (or (memq invisible-at-point '(outline org-hide-block))
  16653. (memq invisible-before-point '(outline org-hide-block)))
  16654. (if (eq org-catch-invisible-edits 'error)
  16655. (error "Editing in invisible areas is prohibited - make visible first"))
  16656. ;; Make the area visible
  16657. (save-excursion
  16658. (if invisible-before-point
  16659. (goto-char (previous-single-char-property-change
  16660. (point) 'invisible)))
  16661. (org-cycle))
  16662. (cond
  16663. ((eq org-catch-invisible-edits 'show)
  16664. ;; That's it, we do the edit after showing
  16665. (message
  16666. "Unfolding invisible region around point before editing")
  16667. (sit-for 1))
  16668. ((and (eq org-catch-invisible-edits 'smart)
  16669. border-and-ok-direction)
  16670. (message "Unfolding invisible region around point before editing"))
  16671. (t
  16672. ;; Don't do the edit, make the user repeat it in full visibility
  16673. (error "Edit in invisible region aborted, repeat to confirm with text visible")))))))
  16674. (defun org-fix-tags-on-the-fly ()
  16675. (when (and (equal (char-after (point-at-bol)) ?*)
  16676. (org-at-heading-p))
  16677. (org-align-tags-here org-tags-column)))
  16678. (defun org-delete-backward-char (N)
  16679. "Like `delete-backward-char', insert whitespace at field end in tables.
  16680. When deleting backwards, in tables this function will insert whitespace in
  16681. front of the next \"|\" separator, to keep the table aligned. The table will
  16682. still be marked for re-alignment if the field did fill the entire column,
  16683. because, in this case the deletion might narrow the column."
  16684. (interactive "p")
  16685. (org-check-before-invisible-edit 'delete-backward)
  16686. (if (and (org-table-p)
  16687. (eq N 1)
  16688. (string-match "|" (buffer-substring (point-at-bol) (point)))
  16689. (looking-at ".*?|"))
  16690. (let ((pos (point))
  16691. (noalign (looking-at "[^|\n\r]* |"))
  16692. (c org-table-may-need-update))
  16693. (backward-delete-char N)
  16694. (if (not overwrite-mode)
  16695. (progn
  16696. (skip-chars-forward "^|")
  16697. (insert " ")
  16698. (goto-char (1- pos))))
  16699. ;; noalign: if there were two spaces at the end, this field
  16700. ;; does not determine the width of the column.
  16701. (if noalign (setq org-table-may-need-update c)))
  16702. (backward-delete-char N)
  16703. (org-fix-tags-on-the-fly)))
  16704. (defun org-delete-char (N)
  16705. "Like `delete-char', but insert whitespace at field end in tables.
  16706. When deleting characters, in tables this function will insert whitespace in
  16707. front of the next \"|\" separator, to keep the table aligned. The table will
  16708. still be marked for re-alignment if the field did fill the entire column,
  16709. because, in this case the deletion might narrow the column."
  16710. (interactive "p")
  16711. (org-check-before-invisible-edit 'delete)
  16712. (if (and (org-table-p)
  16713. (not (bolp))
  16714. (not (= (char-after) ?|))
  16715. (eq N 1))
  16716. (if (looking-at ".*?|")
  16717. (let ((pos (point))
  16718. (noalign (looking-at "[^|\n\r]* |"))
  16719. (c org-table-may-need-update))
  16720. (replace-match (concat
  16721. (substring (match-string 0) 1 -1)
  16722. " |"))
  16723. (goto-char pos)
  16724. ;; noalign: if there were two spaces at the end, this field
  16725. ;; does not determine the width of the column.
  16726. (if noalign (setq org-table-may-need-update c)))
  16727. (delete-char N))
  16728. (delete-char N)
  16729. (org-fix-tags-on-the-fly)))
  16730. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  16731. (put 'org-self-insert-command 'delete-selection t)
  16732. (put 'orgtbl-self-insert-command 'delete-selection t)
  16733. (put 'org-delete-char 'delete-selection 'supersede)
  16734. (put 'org-delete-backward-char 'delete-selection 'supersede)
  16735. (put 'org-yank 'delete-selection 'yank)
  16736. ;; Make `flyspell-mode' delay after some commands
  16737. (put 'org-self-insert-command 'flyspell-delayed t)
  16738. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  16739. (put 'org-delete-char 'flyspell-delayed t)
  16740. (put 'org-delete-backward-char 'flyspell-delayed t)
  16741. ;; Make pabbrev-mode expand after org-mode commands
  16742. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  16743. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  16744. ;; How to do this: Measure non-white length of current string
  16745. ;; If equal to column width, we should realign.
  16746. (defun org-remap (map &rest commands)
  16747. "In MAP, remap the functions given in COMMANDS.
  16748. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  16749. (let (new old)
  16750. (while commands
  16751. (setq old (pop commands) new (pop commands))
  16752. (if (fboundp 'command-remapping)
  16753. (org-defkey map (vector 'remap old) new)
  16754. (substitute-key-definition old new map global-map)))))
  16755. (when (eq org-enable-table-editor 'optimized)
  16756. ;; If the user wants maximum table support, we need to hijack
  16757. ;; some standard editing functions
  16758. (org-remap org-mode-map
  16759. 'self-insert-command 'org-self-insert-command
  16760. 'delete-char 'org-delete-char
  16761. 'delete-backward-char 'org-delete-backward-char)
  16762. (org-defkey org-mode-map "|" 'org-force-self-insert))
  16763. (defvar org-ctrl-c-ctrl-c-hook nil
  16764. "Hook for functions attaching themselves to `C-c C-c'.
  16765. This can be used to add additional functionality to the C-c C-c
  16766. key which executes context-dependent commands. This hook is run
  16767. before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
  16768. run after the last test.
  16769. Each function will be called with no arguments. The function
  16770. must check if the context is appropriate for it to act. If yes,
  16771. it should do its thing and then return a non-nil value. If the
  16772. context is wrong, just do nothing and return nil.")
  16773. (defvar org-ctrl-c-ctrl-c-final-hook nil
  16774. "Hook for functions attaching themselves to `C-c C-c'.
  16775. This can be used to add additional functionality to the C-c C-c
  16776. key which executes context-dependent commands. This hook is run
  16777. after any other test, while `org-ctrl-c-ctrl-c-hook' is run
  16778. before the first test.
  16779. Each function will be called with no arguments. The function
  16780. must check if the context is appropriate for it to act. If yes,
  16781. it should do its thing and then return a non-nil value. If the
  16782. context is wrong, just do nothing and return nil.")
  16783. (defvar org-tab-first-hook nil
  16784. "Hook for functions to attach themselves to TAB.
  16785. See `org-ctrl-c-ctrl-c-hook' for more information.
  16786. This hook runs as the first action when TAB is pressed, even before
  16787. `org-cycle' messes around with the `outline-regexp' to cater for
  16788. inline tasks and plain list item folding.
  16789. If any function in this hook returns t, any other actions that
  16790. would have been caused by TAB (such as table field motion or visibility
  16791. cycling) will not occur.")
  16792. (defvar org-tab-after-check-for-table-hook nil
  16793. "Hook for functions to attach themselves to TAB.
  16794. See `org-ctrl-c-ctrl-c-hook' for more information.
  16795. This hook runs after it has been established that the cursor is not in a
  16796. table, but before checking if the cursor is in a headline or if global cycling
  16797. should be done.
  16798. If any function in this hook returns t, not other actions like visibility
  16799. cycling will be done.")
  16800. (defvar org-tab-after-check-for-cycling-hook nil
  16801. "Hook for functions to attach themselves to TAB.
  16802. See `org-ctrl-c-ctrl-c-hook' for more information.
  16803. This hook runs after it has been established that not table field motion and
  16804. not visibility should be done because of current context. This is probably
  16805. the place where a package like yasnippets can hook in.")
  16806. (defvar org-tab-before-tab-emulation-hook nil
  16807. "Hook for functions to attach themselves to TAB.
  16808. See `org-ctrl-c-ctrl-c-hook' for more information.
  16809. This hook runs after every other options for TAB have been exhausted, but
  16810. before indentation and \t insertion takes place.")
  16811. (defvar org-metaleft-hook nil
  16812. "Hook for functions attaching themselves to `M-left'.
  16813. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16814. (defvar org-metaright-hook nil
  16815. "Hook for functions attaching themselves to `M-right'.
  16816. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16817. (defvar org-metaup-hook nil
  16818. "Hook for functions attaching themselves to `M-up'.
  16819. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16820. (defvar org-metadown-hook nil
  16821. "Hook for functions attaching themselves to `M-down'.
  16822. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16823. (defvar org-shiftmetaleft-hook nil
  16824. "Hook for functions attaching themselves to `M-S-left'.
  16825. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16826. (defvar org-shiftmetaright-hook nil
  16827. "Hook for functions attaching themselves to `M-S-right'.
  16828. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16829. (defvar org-shiftmetaup-hook nil
  16830. "Hook for functions attaching themselves to `M-S-up'.
  16831. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16832. (defvar org-shiftmetadown-hook nil
  16833. "Hook for functions attaching themselves to `M-S-down'.
  16834. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16835. (defvar org-metareturn-hook nil
  16836. "Hook for functions attaching themselves to `M-RET'.
  16837. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16838. (defvar org-shiftup-hook nil
  16839. "Hook for functions attaching themselves to `S-up'.
  16840. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16841. (defvar org-shiftup-final-hook nil
  16842. "Hook for functions attaching themselves to `S-up'.
  16843. This one runs after all other options except shift-select have been excluded.
  16844. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16845. (defvar org-shiftdown-hook nil
  16846. "Hook for functions attaching themselves to `S-down'.
  16847. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16848. (defvar org-shiftdown-final-hook nil
  16849. "Hook for functions attaching themselves to `S-down'.
  16850. This one runs after all other options except shift-select have been excluded.
  16851. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16852. (defvar org-shiftleft-hook nil
  16853. "Hook for functions attaching themselves to `S-left'.
  16854. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16855. (defvar org-shiftleft-final-hook nil
  16856. "Hook for functions attaching themselves to `S-left'.
  16857. This one runs after all other options except shift-select have been excluded.
  16858. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16859. (defvar org-shiftright-hook nil
  16860. "Hook for functions attaching themselves to `S-right'.
  16861. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16862. (defvar org-shiftright-final-hook nil
  16863. "Hook for functions attaching themselves to `S-right'.
  16864. This one runs after all other options except shift-select have been excluded.
  16865. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16866. (defun org-modifier-cursor-error ()
  16867. "Throw an error, a modified cursor command was applied in wrong context."
  16868. (error "This command is active in special context like tables, headlines or items"))
  16869. (defun org-shiftselect-error ()
  16870. "Throw an error because Shift-Cursor command was applied in wrong context."
  16871. (if (and (boundp 'shift-select-mode) shift-select-mode)
  16872. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  16873. (error "This command works only in special context like headlines or timestamps")))
  16874. (defun org-call-for-shift-select (cmd)
  16875. (let ((this-command-keys-shift-translated t))
  16876. (call-interactively cmd)))
  16877. (defun org-shifttab (&optional arg)
  16878. "Global visibility cycling or move to previous table field.
  16879. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  16880. on context.
  16881. See the individual commands for more information."
  16882. (interactive "P")
  16883. (cond
  16884. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  16885. ((integerp arg)
  16886. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  16887. (message "Content view to level: %d" arg)
  16888. (org-content (prefix-numeric-value arg2))
  16889. (setq org-cycle-global-status 'overview)))
  16890. (t (call-interactively 'org-global-cycle))))
  16891. (defun org-shiftmetaleft ()
  16892. "Promote subtree or delete table column.
  16893. Calls `org-promote-subtree', `org-outdent-item-tree', or
  16894. `org-table-delete-column', depending on context. See the
  16895. individual commands for more information."
  16896. (interactive)
  16897. (cond
  16898. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  16899. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  16900. ((org-at-heading-p) (call-interactively 'org-promote-subtree))
  16901. ((if (not (org-region-active-p)) (org-at-item-p)
  16902. (save-excursion (goto-char (region-beginning))
  16903. (org-at-item-p)))
  16904. (call-interactively 'org-outdent-item-tree))
  16905. (t (org-modifier-cursor-error))))
  16906. (defun org-shiftmetaright ()
  16907. "Demote subtree or insert table column.
  16908. Calls `org-demote-subtree', `org-indent-item-tree', or
  16909. `org-table-insert-column', depending on context. See the
  16910. individual commands for more information."
  16911. (interactive)
  16912. (cond
  16913. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  16914. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  16915. ((org-at-heading-p) (call-interactively 'org-demote-subtree))
  16916. ((if (not (org-region-active-p)) (org-at-item-p)
  16917. (save-excursion (goto-char (region-beginning))
  16918. (org-at-item-p)))
  16919. (call-interactively 'org-indent-item-tree))
  16920. (t (org-modifier-cursor-error))))
  16921. (defun org-shiftmetaup (&optional arg)
  16922. "Move subtree up or kill table row.
  16923. Calls `org-move-subtree-up' or `org-table-kill-row' or
  16924. `org-move-item-up' or `org-timestamp-up', depending on context.
  16925. See the individual commands for more information."
  16926. (interactive "P")
  16927. (cond
  16928. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  16929. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  16930. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  16931. ((org-at-item-p) (call-interactively 'org-move-item-up))
  16932. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  16933. (call-interactively 'org-timestamp-up)))
  16934. (t (org-modifier-cursor-error))))
  16935. (defun org-shiftmetadown (&optional arg)
  16936. "Move subtree down or insert table row.
  16937. Calls `org-move-subtree-down' or `org-table-insert-row' or
  16938. `org-move-item-down' or `org-timestamp-up', depending on context.
  16939. See the individual commands for more information."
  16940. (interactive "P")
  16941. (cond
  16942. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  16943. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  16944. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  16945. ((org-at-item-p) (call-interactively 'org-move-item-down))
  16946. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  16947. (call-interactively 'org-timestamp-down)))
  16948. (t (org-modifier-cursor-error))))
  16949. (defsubst org-hidden-tree-error ()
  16950. (error
  16951. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  16952. (defun org-metaleft (&optional arg)
  16953. "Promote heading or move table column to left.
  16954. Calls `org-do-promote' or `org-table-move-column', depending on context.
  16955. With no specific context, calls the Emacs default `backward-word'.
  16956. See the individual commands for more information."
  16957. (interactive "P")
  16958. (cond
  16959. ((run-hook-with-args-until-success 'org-metaleft-hook))
  16960. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  16961. ((org-with-limited-levels
  16962. (or (org-at-heading-p)
  16963. (and (org-region-active-p)
  16964. (save-excursion
  16965. (goto-char (region-beginning))
  16966. (org-at-heading-p)))))
  16967. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  16968. (call-interactively 'org-do-promote))
  16969. ;; At an inline task.
  16970. ((org-at-heading-p)
  16971. (call-interactively 'org-inlinetask-promote))
  16972. ((or (org-at-item-p)
  16973. (and (org-region-active-p)
  16974. (save-excursion
  16975. (goto-char (region-beginning))
  16976. (org-at-item-p))))
  16977. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  16978. (call-interactively 'org-outdent-item))
  16979. (t (call-interactively 'backward-word))))
  16980. (defun org-metaright (&optional arg)
  16981. "Demote a subtree, a list item or move table column to right.
  16982. In front of a drawer or a block keyword, indent it correctly.
  16983. With no specific context, calls the Emacs default `forward-word'.
  16984. See the individual commands for more information."
  16985. (interactive "P")
  16986. (cond
  16987. ((run-hook-with-args-until-success 'org-metaright-hook))
  16988. ((org-at-table-p) (call-interactively 'org-table-move-column))
  16989. ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
  16990. ((org-at-block-p) (call-interactively 'org-indent-block))
  16991. ((org-with-limited-levels
  16992. (or (org-at-heading-p)
  16993. (and (org-region-active-p)
  16994. (save-excursion
  16995. (goto-char (region-beginning))
  16996. (org-at-heading-p)))))
  16997. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  16998. (call-interactively 'org-do-demote))
  16999. ;; At an inline task.
  17000. ((org-at-heading-p)
  17001. (call-interactively 'org-inlinetask-demote))
  17002. ((or (org-at-item-p)
  17003. (and (org-region-active-p)
  17004. (save-excursion
  17005. (goto-char (region-beginning))
  17006. (org-at-item-p))))
  17007. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  17008. (call-interactively 'org-indent-item))
  17009. (t (call-interactively 'forward-word))))
  17010. (defun org-check-for-hidden (what)
  17011. "Check if there are hidden headlines/items in the current visual line.
  17012. WHAT can be either `headlines' or `items'. If the current line is
  17013. an outline or item heading and it has a folded subtree below it,
  17014. this function returns t, nil otherwise."
  17015. (let ((re (cond
  17016. ((eq what 'headlines) org-outline-regexp-bol)
  17017. ((eq what 'items) (org-item-beginning-re))
  17018. (t (error "This should not happen"))))
  17019. beg end)
  17020. (save-excursion
  17021. (catch 'exit
  17022. (unless (org-region-active-p)
  17023. (setq beg (point-at-bol))
  17024. (beginning-of-line 2)
  17025. (while (and (not (eobp)) ;; this is like `next-line'
  17026. (get-char-property (1- (point)) 'invisible))
  17027. (beginning-of-line 2))
  17028. (setq end (point))
  17029. (goto-char beg)
  17030. (goto-char (point-at-eol))
  17031. (setq end (max end (point)))
  17032. (while (re-search-forward re end t)
  17033. (if (get-char-property (match-beginning 0) 'invisible)
  17034. (throw 'exit t))))
  17035. nil))))
  17036. (defun org-metaup (&optional arg)
  17037. "Move subtree up or move table row up.
  17038. Calls `org-move-subtree-up' or `org-table-move-row' or
  17039. `org-move-item-up', depending on context. See the individual commands
  17040. for more information."
  17041. (interactive "P")
  17042. (cond
  17043. ((run-hook-with-args-until-success 'org-metaup-hook))
  17044. ((org-region-active-p)
  17045. (let* ((a (min (region-beginning) (region-end)))
  17046. (b (1- (max (region-beginning) (region-end))))
  17047. (c (save-excursion (goto-char a)
  17048. (move-beginning-of-line 0)))
  17049. (d (save-excursion (goto-char a)
  17050. (move-end-of-line 0) (point))))
  17051. (transpose-regions a b c d)
  17052. (goto-char c)))
  17053. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  17054. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  17055. ((org-at-item-p) (call-interactively 'org-move-item-up))
  17056. (t (org-element-drag-backward))))
  17057. (defun org-metadown (&optional arg)
  17058. "Move subtree down or move table row down.
  17059. Calls `org-move-subtree-down' or `org-table-move-row' or
  17060. `org-move-item-down', depending on context. See the individual
  17061. commands for more information."
  17062. (interactive "P")
  17063. (cond
  17064. ((run-hook-with-args-until-success 'org-metadown-hook))
  17065. ((org-region-active-p)
  17066. (let* ((a (min (region-beginning) (region-end)))
  17067. (b (max (region-beginning) (region-end)))
  17068. (c (save-excursion (goto-char b)
  17069. (move-beginning-of-line 1)))
  17070. (d (save-excursion (goto-char b)
  17071. (move-end-of-line 1) (1+ (point)))))
  17072. (transpose-regions a b c d)
  17073. (goto-char d)))
  17074. ((org-at-table-p) (call-interactively 'org-table-move-row))
  17075. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  17076. ((org-at-item-p) (call-interactively 'org-move-item-down))
  17077. (t (org-element-drag-forward))))
  17078. (defun org-shiftup (&optional arg)
  17079. "Increase item in timestamp or increase priority of current headline.
  17080. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  17081. depending on context. See the individual commands for more information."
  17082. (interactive "P")
  17083. (cond
  17084. ((run-hook-with-args-until-success 'org-shiftup-hook))
  17085. ((and org-support-shift-select (org-region-active-p))
  17086. (org-call-for-shift-select 'previous-line))
  17087. ((org-at-timestamp-p t)
  17088. (call-interactively (if org-edit-timestamp-down-means-later
  17089. 'org-timestamp-down 'org-timestamp-up)))
  17090. ((and (not (eq org-support-shift-select 'always))
  17091. org-enable-priority-commands
  17092. (org-at-heading-p))
  17093. (call-interactively 'org-priority-up))
  17094. ((and (not org-support-shift-select) (org-at-item-p))
  17095. (call-interactively 'org-previous-item))
  17096. ((org-clocktable-try-shift 'up arg))
  17097. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  17098. (org-support-shift-select
  17099. (org-call-for-shift-select 'previous-line))
  17100. (t (org-shiftselect-error))))
  17101. (defun org-shiftdown (&optional arg)
  17102. "Decrease item in timestamp or decrease priority of current headline.
  17103. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  17104. depending on context. See the individual commands for more information."
  17105. (interactive "P")
  17106. (cond
  17107. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  17108. ((and org-support-shift-select (org-region-active-p))
  17109. (org-call-for-shift-select 'next-line))
  17110. ((org-at-timestamp-p t)
  17111. (call-interactively (if org-edit-timestamp-down-means-later
  17112. 'org-timestamp-up 'org-timestamp-down)))
  17113. ((and (not (eq org-support-shift-select 'always))
  17114. org-enable-priority-commands
  17115. (org-at-heading-p))
  17116. (call-interactively 'org-priority-down))
  17117. ((and (not org-support-shift-select) (org-at-item-p))
  17118. (call-interactively 'org-next-item))
  17119. ((org-clocktable-try-shift 'down arg))
  17120. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  17121. (org-support-shift-select
  17122. (org-call-for-shift-select 'next-line))
  17123. (t (org-shiftselect-error))))
  17124. (defun org-shiftright (&optional arg)
  17125. "Cycle the thing at point or in the current line, depending on context.
  17126. Depending on context, this does one of the following:
  17127. - switch a timestamp at point one day into the future
  17128. - on a headline, switch to the next TODO keyword.
  17129. - on an item, switch entire list to the next bullet type
  17130. - on a property line, switch to the next allowed value
  17131. - on a clocktable definition line, move time block into the future"
  17132. (interactive "P")
  17133. (cond
  17134. ((run-hook-with-args-until-success 'org-shiftright-hook))
  17135. ((and org-support-shift-select (org-region-active-p))
  17136. (org-call-for-shift-select 'forward-char))
  17137. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  17138. ((and (not (eq org-support-shift-select 'always))
  17139. (org-at-heading-p))
  17140. (let ((org-inhibit-logging
  17141. (not org-treat-S-cursor-todo-selection-as-state-change))
  17142. (org-inhibit-blocking
  17143. (not org-treat-S-cursor-todo-selection-as-state-change)))
  17144. (org-call-with-arg 'org-todo 'right)))
  17145. ((or (and org-support-shift-select
  17146. (not (eq org-support-shift-select 'always))
  17147. (org-at-item-bullet-p))
  17148. (and (not org-support-shift-select) (org-at-item-p)))
  17149. (org-call-with-arg 'org-cycle-list-bullet nil))
  17150. ((and (not (eq org-support-shift-select 'always))
  17151. (org-at-property-p))
  17152. (call-interactively 'org-property-next-allowed-value))
  17153. ((org-clocktable-try-shift 'right arg))
  17154. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  17155. (org-support-shift-select
  17156. (org-call-for-shift-select 'forward-char))
  17157. (t (org-shiftselect-error))))
  17158. (defun org-shiftleft (&optional arg)
  17159. "Cycle the thing at point or in the current line, depending on context.
  17160. Depending on context, this does one of the following:
  17161. - switch a timestamp at point one day into the past
  17162. - on a headline, switch to the previous TODO keyword.
  17163. - on an item, switch entire list to the previous bullet type
  17164. - on a property line, switch to the previous allowed value
  17165. - on a clocktable definition line, move time block into the past"
  17166. (interactive "P")
  17167. (cond
  17168. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  17169. ((and org-support-shift-select (org-region-active-p))
  17170. (org-call-for-shift-select 'backward-char))
  17171. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  17172. ((and (not (eq org-support-shift-select 'always))
  17173. (org-at-heading-p))
  17174. (let ((org-inhibit-logging
  17175. (not org-treat-S-cursor-todo-selection-as-state-change))
  17176. (org-inhibit-blocking
  17177. (not org-treat-S-cursor-todo-selection-as-state-change)))
  17178. (org-call-with-arg 'org-todo 'left)))
  17179. ((or (and org-support-shift-select
  17180. (not (eq org-support-shift-select 'always))
  17181. (org-at-item-bullet-p))
  17182. (and (not org-support-shift-select) (org-at-item-p)))
  17183. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  17184. ((and (not (eq org-support-shift-select 'always))
  17185. (org-at-property-p))
  17186. (call-interactively 'org-property-previous-allowed-value))
  17187. ((org-clocktable-try-shift 'left arg))
  17188. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  17189. (org-support-shift-select
  17190. (org-call-for-shift-select 'backward-char))
  17191. (t (org-shiftselect-error))))
  17192. (defun org-shiftcontrolright ()
  17193. "Switch to next TODO set."
  17194. (interactive)
  17195. (cond
  17196. ((and org-support-shift-select (org-region-active-p))
  17197. (org-call-for-shift-select 'forward-word))
  17198. ((and (not (eq org-support-shift-select 'always))
  17199. (org-at-heading-p))
  17200. (org-call-with-arg 'org-todo 'nextset))
  17201. (org-support-shift-select
  17202. (org-call-for-shift-select 'forward-word))
  17203. (t (org-shiftselect-error))))
  17204. (defun org-shiftcontrolleft ()
  17205. "Switch to previous TODO set."
  17206. (interactive)
  17207. (cond
  17208. ((and org-support-shift-select (org-region-active-p))
  17209. (org-call-for-shift-select 'backward-word))
  17210. ((and (not (eq org-support-shift-select 'always))
  17211. (org-at-heading-p))
  17212. (org-call-with-arg 'org-todo 'previousset))
  17213. (org-support-shift-select
  17214. (org-call-for-shift-select 'backward-word))
  17215. (t (org-shiftselect-error))))
  17216. (defun org-shiftcontrolup ()
  17217. "Change timestamps synchronously up in CLOCK log lines."
  17218. (interactive)
  17219. (cond ((and (not org-support-shift-select)
  17220. (org-at-clock-log-p)
  17221. (org-at-timestamp-p t))
  17222. (org-clock-timestamps-up))
  17223. (t (org-shiftselect-error))))
  17224. (defun org-shiftcontroldown ()
  17225. "Change timestamps synchronously down in CLOCK log lines."
  17226. (interactive)
  17227. (cond ((and (not org-support-shift-select)
  17228. (org-at-clock-log-p)
  17229. (org-at-timestamp-p t))
  17230. (org-clock-timestamps-down))
  17231. (t (org-shiftselect-error))))
  17232. (defun org-ctrl-c-ret ()
  17233. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  17234. (interactive)
  17235. (cond
  17236. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  17237. (t (call-interactively 'org-insert-heading))))
  17238. (defun org-find-visible ()
  17239. (let ((s (point)))
  17240. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  17241. (get-char-property s 'invisible)))
  17242. s))
  17243. (defun org-find-invisible ()
  17244. (let ((s (point)))
  17245. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  17246. (not (get-char-property s 'invisible))))
  17247. s))
  17248. (defun org-copy-visible (beg end)
  17249. "Copy the visible parts of the region."
  17250. (interactive "r")
  17251. (let (snippets s)
  17252. (save-excursion
  17253. (save-restriction
  17254. (narrow-to-region beg end)
  17255. (setq s (goto-char (point-min)))
  17256. (while (not (= (point) (point-max)))
  17257. (goto-char (org-find-invisible))
  17258. (push (buffer-substring s (point)) snippets)
  17259. (setq s (goto-char (org-find-visible))))))
  17260. (kill-new (apply 'concat (nreverse snippets)))))
  17261. (defun org-copy-special ()
  17262. "Copy region in table or copy current subtree.
  17263. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  17264. See the individual commands for more information."
  17265. (interactive)
  17266. (call-interactively
  17267. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  17268. (defun org-cut-special ()
  17269. "Cut region in table or cut current subtree.
  17270. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  17271. See the individual commands for more information."
  17272. (interactive)
  17273. (call-interactively
  17274. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  17275. (defun org-paste-special (arg)
  17276. "Paste rectangular region into table, or past subtree relative to level.
  17277. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  17278. See the individual commands for more information."
  17279. (interactive "P")
  17280. (if (org-at-table-p)
  17281. (org-table-paste-rectangle)
  17282. (org-paste-subtree arg)))
  17283. (defun org-edit-special (&optional arg)
  17284. "Call a special editor for the stuff at point.
  17285. When at a table, call the formula editor with `org-table-edit-formulas'.
  17286. When at the first line of an src example, call `org-edit-src-code'.
  17287. When in an #+include line, visit the include file. Otherwise call
  17288. `ffap' to visit the file at point."
  17289. (interactive)
  17290. ;; possibly prep session before editing source
  17291. (when arg
  17292. (let* ((info (org-babel-get-src-block-info))
  17293. (lang (nth 0 info))
  17294. (params (nth 2 info))
  17295. (session (cdr (assoc :session params))))
  17296. (when (and info session) ;; we are in a source-code block with a session
  17297. (funcall
  17298. (intern (concat "org-babel-prep-session:" lang)) session params))))
  17299. (cond ;; proceed with `org-edit-special'
  17300. ((save-excursion
  17301. (beginning-of-line 1)
  17302. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  17303. (find-file (org-trim (match-string 1))))
  17304. ((org-edit-src-code))
  17305. ((org-edit-fixed-width-region))
  17306. ((org-at-table.el-p)
  17307. (org-edit-src-code))
  17308. ((or (org-at-table-p)
  17309. (save-excursion
  17310. (beginning-of-line 1)
  17311. (let ((case-fold-search )) (looking-at "[ \t]*#\\+tblfm:"))))
  17312. (call-interactively 'org-table-edit-formulas))
  17313. (t (call-interactively 'ffap))))
  17314. (defvar org-table-coordinate-overlays) ; defined in org-table.el
  17315. (defun org-ctrl-c-ctrl-c (&optional arg)
  17316. "Set tags in headline, or update according to changed information at point.
  17317. This command does many different things, depending on context:
  17318. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  17319. this is what we do.
  17320. - If the cursor is on a statistics cookie, update it.
  17321. - If the cursor is in a headline, prompt for tags and insert them
  17322. into the current line, aligned to `org-tags-column'. When called
  17323. with prefix arg, realign all tags in the current buffer.
  17324. - If the cursor is in one of the special #+KEYWORD lines, this
  17325. triggers scanning the buffer for these lines and updating the
  17326. information.
  17327. - If the cursor is inside a table, realign the table. This command
  17328. works even if the automatic table editor has been turned off.
  17329. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  17330. the entire table.
  17331. - If the cursor is at a footnote reference or definition, jump to
  17332. the corresponding definition or references, respectively.
  17333. - If the cursor is a the beginning of a dynamic block, update it.
  17334. - If the current buffer is a capture buffer, close note and file it.
  17335. - If the cursor is on a <<<target>>>, update radio targets and
  17336. corresponding links in this buffer.
  17337. - If the cursor is on a numbered item in a plain list, renumber the
  17338. ordered list.
  17339. - If the cursor is on a checkbox, toggle it.
  17340. - If the cursor is on a code block, evaluate it. The variable
  17341. `org-confirm-babel-evaluate' can be used to control prompting
  17342. before code block evaluation, by default every code block
  17343. evaluation requires confirmation. Code block evaluation can be
  17344. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  17345. (interactive "P")
  17346. (let ((org-enable-table-editor t))
  17347. (cond
  17348. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  17349. org-occur-highlights
  17350. org-latex-fragment-image-overlays)
  17351. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  17352. (org-remove-occur-highlights)
  17353. (org-remove-latex-fragment-image-overlays)
  17354. (message "Temporary highlights/overlays removed from current buffer"))
  17355. ((and (local-variable-p 'org-finish-function (current-buffer))
  17356. (fboundp org-finish-function))
  17357. (funcall org-finish-function))
  17358. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  17359. ((org-in-regexp org-ts-regexp-both)
  17360. (org-timestamp-change 0 'day))
  17361. ((or (looking-at org-property-start-re)
  17362. (org-at-property-p))
  17363. (call-interactively 'org-property-action))
  17364. ((org-at-target-p) (call-interactively 'org-update-radio-target-regexp))
  17365. ((and (org-in-regexp "\\[\\([0-9]*%\\|[0-9]*/[0-9]*\\)\\]")
  17366. (or (org-at-heading-p) (org-at-item-p)))
  17367. (call-interactively 'org-update-statistics-cookies))
  17368. ((org-at-heading-p) (call-interactively 'org-set-tags))
  17369. ((org-at-table.el-p)
  17370. (message "Use C-c ' to edit table.el tables"))
  17371. ((org-at-table-p)
  17372. (org-table-maybe-eval-formula)
  17373. (if arg
  17374. (call-interactively 'org-table-recalculate)
  17375. (org-table-maybe-recalculate-line))
  17376. (call-interactively 'org-table-align)
  17377. (orgtbl-send-table 'maybe))
  17378. ((or (org-footnote-at-reference-p)
  17379. (org-footnote-at-definition-p))
  17380. (call-interactively 'org-footnote-action))
  17381. ((org-at-item-checkbox-p)
  17382. ;; Cursor at a checkbox: repair list and update checkboxes. Send
  17383. ;; list only if at top item.
  17384. (let* ((cbox (match-string 1))
  17385. (struct (org-list-struct))
  17386. (old-struct (copy-tree struct))
  17387. (parents (org-list-parents-alist struct))
  17388. (orderedp (org-entry-get nil "ORDERED"))
  17389. (firstp (= (org-list-get-top-point struct) (point-at-bol)))
  17390. block-item)
  17391. ;; Use a light version of `org-toggle-checkbox' to avoid
  17392. ;; computing list structure twice.
  17393. (let ((new-box (cond
  17394. ((equal arg '(16)) "[-]")
  17395. ((equal arg '(4)) nil)
  17396. ((equal "[X]" cbox) "[ ]")
  17397. (t "[X]"))))
  17398. (if (and firstp arg)
  17399. ;; If at first item of sub-list, remove check-box from
  17400. ;; every item at the same level.
  17401. (mapc
  17402. (lambda (pos) (org-list-set-checkbox pos struct new-box))
  17403. (org-list-get-all-items
  17404. (point-at-bol) struct (org-list-prevs-alist struct)))
  17405. (org-list-set-checkbox (point-at-bol) struct new-box)))
  17406. ;; Replicate `org-list-write-struct', while grabbing a return
  17407. ;; value from `org-list-struct-fix-box'.
  17408. (org-list-struct-fix-ind struct parents 2)
  17409. (org-list-struct-fix-item-end struct)
  17410. (let ((prevs (org-list-prevs-alist struct)))
  17411. (org-list-struct-fix-bul struct prevs)
  17412. (org-list-struct-fix-ind struct parents)
  17413. (setq block-item
  17414. (org-list-struct-fix-box struct parents prevs orderedp)))
  17415. (org-list-struct-apply-struct struct old-struct)
  17416. (org-update-checkbox-count-maybe)
  17417. (when block-item
  17418. (message
  17419. "Checkboxes were removed due to unchecked box at line %d"
  17420. (org-current-line block-item)))
  17421. (when firstp (org-list-send-list 'maybe))))
  17422. ((org-at-item-p)
  17423. ;; Cursor at an item: repair list. Do checkbox related actions
  17424. ;; only if function was called with an argument. Send list only
  17425. ;; if at top item.
  17426. (let* ((struct (org-list-struct))
  17427. (firstp (= (org-list-get-top-point struct) (point-at-bol)))
  17428. old-struct)
  17429. (when arg
  17430. (setq old-struct (copy-tree struct))
  17431. (if firstp
  17432. ;; If at first item of sub-list, add check-box to every
  17433. ;; item at the same level.
  17434. (mapc
  17435. (lambda (pos)
  17436. (unless (org-list-get-checkbox pos struct)
  17437. (org-list-set-checkbox pos struct "[ ]")))
  17438. (org-list-get-all-items
  17439. (point-at-bol) struct (org-list-prevs-alist struct)))
  17440. (org-list-set-checkbox (point-at-bol) struct "[ ]")))
  17441. (org-list-write-struct
  17442. struct (org-list-parents-alist struct) old-struct)
  17443. (when arg (org-update-checkbox-count-maybe))
  17444. (when firstp (org-list-send-list 'maybe))))
  17445. ((save-excursion (beginning-of-line 1) (looking-at org-dblock-start-re))
  17446. ;; Dynamic block
  17447. (beginning-of-line 1)
  17448. (save-excursion (org-update-dblock)))
  17449. ((save-excursion
  17450. (let ((case-fold-search t))
  17451. (beginning-of-line 1)
  17452. (looking-at "[ \t]*#\\+\\([a-z]+\\)")))
  17453. (cond
  17454. ((or (equal (match-string 1) "TBLFM")
  17455. (equal (match-string 1) "tblfm"))
  17456. ;; Recalculate the table before this line
  17457. (save-excursion
  17458. (beginning-of-line 1)
  17459. (skip-chars-backward " \r\n\t")
  17460. (if (org-at-table-p)
  17461. (org-call-with-arg 'org-table-recalculate (or arg t)))))
  17462. (t
  17463. (let ((org-inhibit-startup-visibility-stuff t)
  17464. (org-startup-align-all-tables nil))
  17465. (when (boundp 'org-table-coordinate-overlays)
  17466. (mapc 'delete-overlay org-table-coordinate-overlays)
  17467. (setq org-table-coordinate-overlays nil))
  17468. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  17469. (message "Local setup has been refreshed"))))
  17470. ((org-clock-update-time-maybe))
  17471. (t
  17472. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  17473. (error "C-c C-c can do nothing useful at this location"))))))
  17474. (defun org-mode-restart ()
  17475. "Restart Org-mode, to scan again for special lines.
  17476. Also updates the keyword regular expressions."
  17477. (interactive)
  17478. (org-mode)
  17479. (message "Org-mode restarted"))
  17480. (defun org-kill-note-or-show-branches ()
  17481. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  17482. (interactive)
  17483. (if (not org-finish-function)
  17484. (progn
  17485. (hide-subtree)
  17486. (call-interactively 'show-branches))
  17487. (let ((org-note-abort t))
  17488. (funcall org-finish-function))))
  17489. (defun org-return (&optional indent)
  17490. "Goto next table row or insert a newline.
  17491. Calls `org-table-next-row' or `newline', depending on context.
  17492. See the individual commands for more information."
  17493. (interactive)
  17494. (let (org-ts-what)
  17495. (cond
  17496. ((or (bobp) (org-in-src-block-p))
  17497. (if indent (newline-and-indent) (newline)))
  17498. ((org-at-table-p)
  17499. (org-table-justify-field-maybe)
  17500. (call-interactively 'org-table-next-row))
  17501. ;; when `newline-and-indent' is called within a list, make sure
  17502. ;; text moved stays inside the item.
  17503. ((and (org-in-item-p) indent)
  17504. (if (and (org-at-item-p) (>= (point) (match-end 0)))
  17505. (progn
  17506. (save-match-data (newline))
  17507. (org-indent-line-to (length (match-string 0))))
  17508. (let ((ind (org-get-indentation)))
  17509. (newline)
  17510. (if (org-looking-back org-list-end-re)
  17511. (org-indent-line)
  17512. (org-indent-line-to ind)))))
  17513. ((and org-return-follows-link
  17514. (org-at-timestamp-p t)
  17515. (not (eq org-ts-what 'after)))
  17516. (org-follow-timestamp-link))
  17517. ((and org-return-follows-link
  17518. (let ((tprop (get-text-property (point) 'face)))
  17519. (or (eq tprop 'org-link)
  17520. (and (listp tprop) (memq 'org-link tprop)))))
  17521. (call-interactively 'org-open-at-point))
  17522. ((and (org-at-heading-p)
  17523. (looking-at
  17524. (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
  17525. (org-show-entry)
  17526. (end-of-line 1)
  17527. (newline))
  17528. (t (if indent (newline-and-indent) (newline))))))
  17529. (defun org-return-indent ()
  17530. "Goto next table row or insert a newline and indent.
  17531. Calls `org-table-next-row' or `newline-and-indent', depending on
  17532. context. See the individual commands for more information."
  17533. (interactive)
  17534. (org-return t))
  17535. (defun org-ctrl-c-star ()
  17536. "Compute table, or change heading status of lines.
  17537. Calls `org-table-recalculate' or `org-toggle-heading',
  17538. depending on context."
  17539. (interactive)
  17540. (cond
  17541. ((org-at-table-p)
  17542. (call-interactively 'org-table-recalculate))
  17543. (t
  17544. ;; Convert all lines in region to list items
  17545. (call-interactively 'org-toggle-heading))))
  17546. (defun org-ctrl-c-minus ()
  17547. "Insert separator line in table or modify bullet status of line.
  17548. Also turns a plain line or a region of lines into list items.
  17549. Calls `org-table-insert-hline', `org-toggle-item', or
  17550. `org-cycle-list-bullet', depending on context."
  17551. (interactive)
  17552. (cond
  17553. ((org-at-table-p)
  17554. (call-interactively 'org-table-insert-hline))
  17555. ((org-region-active-p)
  17556. (call-interactively 'org-toggle-item))
  17557. ((org-in-item-p)
  17558. (call-interactively 'org-cycle-list-bullet))
  17559. (t
  17560. (call-interactively 'org-toggle-item))))
  17561. (defun org-toggle-item (arg)
  17562. "Convert headings or normal lines to items, items to normal lines.
  17563. If there is no active region, only the current line is considered.
  17564. If the first non blank line in the region is an headline, convert
  17565. all headlines to items, shifting text accordingly.
  17566. If it is an item, convert all items to normal lines.
  17567. If it is normal text, change region into an item. With a prefix
  17568. argument ARG, change each line in region into an item."
  17569. (interactive "P")
  17570. (let ((shift-text
  17571. (function
  17572. ;; Shift text in current section to IND, from point to END.
  17573. ;; The function leaves point to END line.
  17574. (lambda (ind end)
  17575. (let ((min-i 1000) (end (copy-marker end)))
  17576. ;; First determine the minimum indentation (MIN-I) of
  17577. ;; the text.
  17578. (save-excursion
  17579. (catch 'exit
  17580. (while (< (point) end)
  17581. (let ((i (org-get-indentation)))
  17582. (cond
  17583. ;; Skip blank lines and inline tasks.
  17584. ((looking-at "^[ \t]*$"))
  17585. ((looking-at org-outline-regexp-bol))
  17586. ;; We can't find less than 0 indentation.
  17587. ((zerop i) (throw 'exit (setq min-i 0)))
  17588. ((< i min-i) (setq min-i i))))
  17589. (forward-line))))
  17590. ;; Then indent each line so that a line indented to
  17591. ;; MIN-I becomes indented to IND. Ignore blank lines
  17592. ;; and inline tasks in the process.
  17593. (let ((delta (- ind min-i)))
  17594. (while (< (point) end)
  17595. (unless (or (looking-at "^[ \t]*$")
  17596. (looking-at org-outline-regexp-bol))
  17597. (org-indent-line-to (+ (org-get-indentation) delta)))
  17598. (forward-line)))))))
  17599. (skip-blanks
  17600. (function
  17601. ;; Return beginning of first non-blank line, starting from
  17602. ;; line at POS.
  17603. (lambda (pos)
  17604. (save-excursion
  17605. (goto-char pos)
  17606. (skip-chars-forward " \r\t\n")
  17607. (point-at-bol)))))
  17608. beg end)
  17609. ;; Determine boundaries of changes.
  17610. (if (org-region-active-p)
  17611. (setq beg (funcall skip-blanks (region-beginning))
  17612. end (copy-marker (region-end)))
  17613. (setq beg (funcall skip-blanks (point-at-bol))
  17614. end (copy-marker (point-at-eol))))
  17615. ;; Depending on the starting line, choose an action on the text
  17616. ;; between BEG and END.
  17617. (org-with-limited-levels
  17618. (save-excursion
  17619. (goto-char beg)
  17620. (cond
  17621. ;; Case 1. Start at an item: de-itemize. Note that it only
  17622. ;; happens when a region is active: `org-ctrl-c-minus'
  17623. ;; would call `org-cycle-list-bullet' otherwise.
  17624. ((org-at-item-p)
  17625. (while (< (point) end)
  17626. (when (org-at-item-p)
  17627. (skip-chars-forward " \t")
  17628. (delete-region (point) (match-end 0)))
  17629. (forward-line)))
  17630. ;; Case 2. Start at an heading: convert to items.
  17631. ((org-at-heading-p)
  17632. (let* ((bul (org-list-bullet-string "-"))
  17633. (bul-len (length bul))
  17634. ;; Indentation of the first heading. It should be
  17635. ;; relative to the indentation of its parent, if any.
  17636. (start-ind (save-excursion
  17637. (cond
  17638. ((not org-adapt-indentation) 0)
  17639. ((not (outline-previous-heading)) 0)
  17640. (t (length (match-string 0))))))
  17641. ;; Level of first heading. Further headings will be
  17642. ;; compared to it to determine hierarchy in the list.
  17643. (ref-level (org-reduced-level (org-outline-level))))
  17644. (while (< (point) end)
  17645. (let* ((level (org-reduced-level (org-outline-level)))
  17646. (delta (max 0 (- level ref-level))))
  17647. ;; If current headline is less indented than the first
  17648. ;; one, set it as reference, in order to preserve
  17649. ;; subtrees.
  17650. (when (< level ref-level) (setq ref-level level))
  17651. (replace-match bul t t)
  17652. (org-indent-line-to (+ start-ind (* delta bul-len)))
  17653. ;; Ensure all text down to END (or SECTION-END) belongs
  17654. ;; to the newly created item.
  17655. (let ((section-end (save-excursion
  17656. (or (outline-next-heading) (point)))))
  17657. (forward-line)
  17658. (funcall shift-text
  17659. (+ start-ind (* (1+ delta) bul-len))
  17660. (min end section-end)))))))
  17661. ;; Case 3. Normal line with ARG: turn each non-item line into
  17662. ;; an item.
  17663. (arg
  17664. (while (< (point) end)
  17665. (unless (or (org-at-heading-p) (org-at-item-p))
  17666. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  17667. (replace-match
  17668. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  17669. (forward-line)))
  17670. ;; Case 4. Normal line without ARG: make the first line of
  17671. ;; region an item, and shift indentation of others
  17672. ;; lines to set them as item's body.
  17673. (t (let* ((bul (org-list-bullet-string "-"))
  17674. (bul-len (length bul))
  17675. (ref-ind (org-get-indentation)))
  17676. (skip-chars-forward " \t")
  17677. (insert bul)
  17678. (forward-line)
  17679. (while (< (point) end)
  17680. ;; Ensure that lines less indented than first one
  17681. ;; still get included in item body.
  17682. (funcall shift-text
  17683. (+ ref-ind bul-len)
  17684. (min end (save-excursion (or (outline-next-heading)
  17685. (point)))))
  17686. (forward-line)))))))))
  17687. (defun org-toggle-heading (&optional nstars)
  17688. "Convert headings to normal text, or items or text to headings.
  17689. If there is no active region, only the current line is considered.
  17690. With a \\[universal-argument] prefix, convert the whole list at
  17691. point into heading.
  17692. In a region:
  17693. - If the first non blank line is an headline, remove the stars
  17694. from all headlines in the region.
  17695. - If it is a normal line turn each and every normal line (i.e. not an
  17696. heading or an item) in the region into a heading.
  17697. - If it is a plain list item, turn all plain list items into headings.
  17698. When converting a line into a heading, the number of stars is chosen
  17699. such that the lines become children of the current entry. However,
  17700. when a prefix argument is given, its value determines the number of
  17701. stars to add."
  17702. (interactive "P")
  17703. (let ((skip-blanks
  17704. (function
  17705. ;; Return beginning of first non-blank line, starting from
  17706. ;; line at POS.
  17707. (lambda (pos)
  17708. (save-excursion
  17709. (goto-char pos)
  17710. (while (org-at-comment-p) (forward-line))
  17711. (skip-chars-forward " \r\t\n")
  17712. (point-at-bol)))))
  17713. beg end toggled)
  17714. ;; Determine boundaries of changes. If a universal prefix has
  17715. ;; been given, put the list in a region. If region ends at a bol,
  17716. ;; do not consider the last line to be in the region.
  17717. (when (and current-prefix-arg (org-at-item-p))
  17718. (if (equal current-prefix-arg '(4)) (setq current-prefix-arg 1))
  17719. (org-mark-list))
  17720. (if (org-region-active-p)
  17721. (setq beg (funcall skip-blanks (region-beginning))
  17722. end (copy-marker (save-excursion
  17723. (goto-char (region-end))
  17724. (if (bolp) (point) (point-at-eol)))))
  17725. (setq beg (funcall skip-blanks (point-at-bol))
  17726. end (copy-marker (point-at-eol))))
  17727. ;; Ensure inline tasks don't count as headings.
  17728. (org-with-limited-levels
  17729. (save-excursion
  17730. (goto-char beg)
  17731. (cond
  17732. ;; Case 1. Started at an heading: de-star headings.
  17733. ((org-at-heading-p)
  17734. (while (< (point) end)
  17735. (when (org-at-heading-p t)
  17736. (looking-at org-outline-regexp) (replace-match "")
  17737. (setq toggled t))
  17738. (forward-line)))
  17739. ;; Case 2. Started at an item: change items into headlines.
  17740. ;; One star will be added by `org-list-to-subtree'.
  17741. ((org-at-item-p)
  17742. (let* ((stars (make-string
  17743. (if nstars
  17744. ;; subtract the star that will be added again by
  17745. ;; `org-list-to-subtree'
  17746. (1- (prefix-numeric-value current-prefix-arg))
  17747. (or (org-current-level) 0))
  17748. ?*))
  17749. (add-stars
  17750. (cond (nstars "") ; stars from prefix only
  17751. ((equal stars "") "") ; before first heading
  17752. (org-odd-levels-only "*") ; inside heading, odd
  17753. (t "")))) ; inside heading, oddeven
  17754. (while (< (point) end)
  17755. (when (org-at-item-p)
  17756. ;; Pay attention to cases when region ends before list.
  17757. (let* ((struct (org-list-struct))
  17758. (list-end (min (org-list-get-bottom-point struct) (1+ end))))
  17759. (save-restriction
  17760. (narrow-to-region (point) list-end)
  17761. (insert
  17762. (org-list-to-subtree
  17763. (org-list-parse-list t)
  17764. '(:istart (concat stars add-stars (funcall get-stars depth))
  17765. :icount (concat stars add-stars (funcall get-stars depth)))))))
  17766. (setq toggled t))
  17767. (forward-line))))
  17768. ;; Case 3. Started at normal text: make every line an heading,
  17769. ;; skipping headlines and items.
  17770. (t (let* ((stars (make-string
  17771. (if nstars
  17772. (prefix-numeric-value current-prefix-arg)
  17773. (or (org-current-level) 0))
  17774. ?*))
  17775. (add-stars
  17776. (cond (nstars "") ; stars from prefix only
  17777. ((equal stars "") "*") ; before first heading
  17778. (org-odd-levels-only "**") ; inside heading, odd
  17779. (t "*"))) ; inside heading, oddeven
  17780. (rpl (concat stars add-stars " ")))
  17781. (while (< (point) end)
  17782. (when (and (not (or (org-at-heading-p) (org-at-item-p) (org-at-comment-p)))
  17783. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  17784. (replace-match (concat rpl (match-string 2))) (setq toggled t))
  17785. (forward-line)))))))
  17786. (unless toggled (message "Cannot toggle heading from here"))))
  17787. (defun org-meta-return (&optional arg)
  17788. "Insert a new heading or wrap a region in a table.
  17789. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  17790. See the individual commands for more information."
  17791. (interactive "P")
  17792. (cond
  17793. ((run-hook-with-args-until-success 'org-metareturn-hook))
  17794. ((or (org-at-drawer-p) (org-at-property-p))
  17795. (newline-and-indent))
  17796. ((org-at-table-p)
  17797. (call-interactively 'org-table-wrap-region))
  17798. (t (call-interactively 'org-insert-heading))))
  17799. ;;; Menu entries
  17800. ;; Define the Org-mode menus
  17801. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  17802. '("Tbl"
  17803. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  17804. ["Next Field" org-cycle (org-at-table-p)]
  17805. ["Previous Field" org-shifttab (org-at-table-p)]
  17806. ["Next Row" org-return (org-at-table-p)]
  17807. "--"
  17808. ["Blank Field" org-table-blank-field (org-at-table-p)]
  17809. ["Edit Field" org-table-edit-field (org-at-table-p)]
  17810. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  17811. "--"
  17812. ("Column"
  17813. ["Move Column Left" org-metaleft (org-at-table-p)]
  17814. ["Move Column Right" org-metaright (org-at-table-p)]
  17815. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  17816. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  17817. ("Row"
  17818. ["Move Row Up" org-metaup (org-at-table-p)]
  17819. ["Move Row Down" org-metadown (org-at-table-p)]
  17820. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  17821. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  17822. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  17823. "--"
  17824. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  17825. ("Rectangle"
  17826. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  17827. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  17828. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  17829. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  17830. "--"
  17831. ("Calculate"
  17832. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  17833. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  17834. ["Edit Formulas" org-edit-special (org-at-table-p)]
  17835. "--"
  17836. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  17837. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  17838. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  17839. "--"
  17840. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  17841. "--"
  17842. ["Sum Column/Rectangle" org-table-sum
  17843. (or (org-at-table-p) (org-region-active-p))]
  17844. ["Which Column?" org-table-current-column (org-at-table-p)])
  17845. ["Debug Formulas"
  17846. org-table-toggle-formula-debugger
  17847. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  17848. ["Show Col/Row Numbers"
  17849. org-table-toggle-coordinate-overlays
  17850. :style toggle
  17851. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  17852. "--"
  17853. ["Create" org-table-create (and (not (org-at-table-p))
  17854. org-enable-table-editor)]
  17855. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  17856. ["Import from File" org-table-import (not (org-at-table-p))]
  17857. ["Export to File" org-table-export (org-at-table-p)]
  17858. "--"
  17859. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  17860. (easy-menu-define org-org-menu org-mode-map "Org menu"
  17861. '("Org"
  17862. ("Show/Hide"
  17863. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  17864. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  17865. ["Sparse Tree..." org-sparse-tree t]
  17866. ["Reveal Context" org-reveal t]
  17867. ["Show All" show-all t]
  17868. "--"
  17869. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  17870. "--"
  17871. ["New Heading" org-insert-heading t]
  17872. ("Navigate Headings"
  17873. ["Up" outline-up-heading t]
  17874. ["Next" outline-next-visible-heading t]
  17875. ["Previous" outline-previous-visible-heading t]
  17876. ["Next Same Level" outline-forward-same-level t]
  17877. ["Previous Same Level" outline-backward-same-level t]
  17878. "--"
  17879. ["Jump" org-goto t])
  17880. ("Edit Structure"
  17881. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  17882. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  17883. "--"
  17884. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  17885. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  17886. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  17887. "--"
  17888. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  17889. "--"
  17890. ["Copy visible text" org-copy-visible t]
  17891. "--"
  17892. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  17893. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  17894. ["Demote Heading" org-metaright (not (org-at-table-p))]
  17895. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  17896. "--"
  17897. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  17898. "--"
  17899. ["Convert to odd levels" org-convert-to-odd-levels t]
  17900. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  17901. ("Editing"
  17902. ["Emphasis..." org-emphasize t]
  17903. ["Edit Source Example" org-edit-special t]
  17904. "--"
  17905. ["Footnote new/jump" org-footnote-action t]
  17906. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  17907. ("Archive"
  17908. ["Archive (default method)" org-archive-subtree-default t]
  17909. "--"
  17910. ["Move Subtree to Archive file" org-advertized-archive-subtree t]
  17911. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  17912. ["Move subtree to Archive sibling" org-archive-to-archive-sibling t]
  17913. )
  17914. "--"
  17915. ("Hyperlinks"
  17916. ["Store Link (Global)" org-store-link t]
  17917. ["Find existing link to here" org-occur-link-in-agenda-files t]
  17918. ["Insert Link" org-insert-link t]
  17919. ["Follow Link" org-open-at-point t]
  17920. "--"
  17921. ["Next link" org-next-link t]
  17922. ["Previous link" org-previous-link t]
  17923. "--"
  17924. ["Descriptive Links"
  17925. org-toggle-link-display
  17926. :style radio
  17927. :selected org-descriptive-links
  17928. ]
  17929. ["Literal Links"
  17930. org-toggle-link-display
  17931. :style radio
  17932. :selected (not org-descriptive-links)])
  17933. "--"
  17934. ("TODO Lists"
  17935. ["TODO/DONE/-" org-todo t]
  17936. ("Select keyword"
  17937. ["Next keyword" org-shiftright (org-at-heading-p)]
  17938. ["Previous keyword" org-shiftleft (org-at-heading-p)]
  17939. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  17940. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
  17941. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
  17942. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  17943. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  17944. "--"
  17945. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  17946. :selected org-enforce-todo-dependencies :style toggle :active t]
  17947. "Settings for tree at point"
  17948. ["Do Children sequentially" org-toggle-ordered-property :style radio
  17949. :selected (org-entry-get nil "ORDERED")
  17950. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  17951. ["Do Children parallel" org-toggle-ordered-property :style radio
  17952. :selected (not (org-entry-get nil "ORDERED"))
  17953. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  17954. "--"
  17955. ["Set Priority" org-priority t]
  17956. ["Priority Up" org-shiftup t]
  17957. ["Priority Down" org-shiftdown t]
  17958. "--"
  17959. ["Get news from all feeds" org-feed-update-all t]
  17960. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  17961. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  17962. ("TAGS and Properties"
  17963. ["Set Tags" org-set-tags-command t]
  17964. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  17965. "--"
  17966. ["Set property" org-set-property t]
  17967. ["Column view of properties" org-columns t]
  17968. ["Insert Column View DBlock" org-insert-columns-dblock t])
  17969. ("Dates and Scheduling"
  17970. ["Timestamp" org-time-stamp t]
  17971. ["Timestamp (inactive)" org-time-stamp-inactive t]
  17972. ("Change Date"
  17973. ["1 Day Later" org-shiftright t]
  17974. ["1 Day Earlier" org-shiftleft t]
  17975. ["1 ... Later" org-shiftup t]
  17976. ["1 ... Earlier" org-shiftdown t])
  17977. ["Compute Time Range" org-evaluate-time-range t]
  17978. ["Schedule Item" org-schedule t]
  17979. ["Deadline" org-deadline t]
  17980. "--"
  17981. ["Custom time format" org-toggle-time-stamp-overlays
  17982. :style radio :selected org-display-custom-times]
  17983. "--"
  17984. ["Goto Calendar" org-goto-calendar t]
  17985. ["Date from Calendar" org-date-from-calendar t]
  17986. "--"
  17987. ["Start/Restart Timer" org-timer-start t]
  17988. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  17989. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  17990. ["Insert Timer String" org-timer t]
  17991. ["Insert Timer Item" org-timer-item t])
  17992. ("Logging work"
  17993. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  17994. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  17995. ["Clock out" org-clock-out t]
  17996. ["Clock cancel" org-clock-cancel t]
  17997. "--"
  17998. ["Mark as default task" org-clock-mark-default-task t]
  17999. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  18000. ["Goto running clock" org-clock-goto t]
  18001. "--"
  18002. ["Display times" org-clock-display t]
  18003. ["Create clock table" org-clock-report t]
  18004. "--"
  18005. ["Record DONE time"
  18006. (progn (setq org-log-done (not org-log-done))
  18007. (message "Switching to %s will %s record a timestamp"
  18008. (car org-done-keywords)
  18009. (if org-log-done "automatically" "not")))
  18010. :style toggle :selected org-log-done])
  18011. "--"
  18012. ["Agenda Command..." org-agenda t]
  18013. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  18014. ("File List for Agenda")
  18015. ("Special views current file"
  18016. ["TODO Tree" org-show-todo-tree t]
  18017. ["Check Deadlines" org-check-deadlines t]
  18018. ["Timeline" org-timeline t]
  18019. ["Tags/Property tree" org-match-sparse-tree t])
  18020. "--"
  18021. ["Export/Publish..." org-export t]
  18022. ("LaTeX"
  18023. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  18024. :selected org-cdlatex-mode]
  18025. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  18026. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  18027. ["Modify math symbol" org-cdlatex-math-modify
  18028. (org-inside-LaTeX-fragment-p)]
  18029. ["Insert citation" org-reftex-citation t]
  18030. "--"
  18031. ["Template for BEAMER" (progn (require 'org-beamer)
  18032. (org-insert-beamer-options-template)) t])
  18033. "--"
  18034. ("MobileOrg"
  18035. ["Push Files and Views" org-mobile-push t]
  18036. ["Get Captured and Flagged" org-mobile-pull t]
  18037. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  18038. "--"
  18039. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  18040. "--"
  18041. ("Documentation"
  18042. ["Show Version" org-version t]
  18043. ["Info Documentation" org-info t])
  18044. ("Customize"
  18045. ["Browse Org Group" org-customize t]
  18046. "--"
  18047. ["Expand This Menu" org-create-customize-menu
  18048. (fboundp 'customize-menu-create)])
  18049. ["Send bug report" org-submit-bug-report t]
  18050. "--"
  18051. ("Refresh/Reload"
  18052. ["Refresh setup current buffer" org-mode-restart t]
  18053. ["Reload Org (after update)" org-reload t]
  18054. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
  18055. ))
  18056. (defun org-info (&optional node)
  18057. "Read documentation for Org-mode in the info system.
  18058. With optional NODE, go directly to that node."
  18059. (interactive)
  18060. (info (format "(org)%s" (or node ""))))
  18061. ;;;###autoload
  18062. (defun org-submit-bug-report ()
  18063. "Submit a bug report on Org-mode via mail.
  18064. Don't hesitate to report any problems or inaccurate documentation.
  18065. If you don't have setup sending mail from (X)Emacs, please copy the
  18066. output buffer into your mail program, as it gives us important
  18067. information about your Org-mode version and configuration."
  18068. (interactive)
  18069. (require 'reporter)
  18070. (org-load-modules-maybe)
  18071. (org-require-autoloaded-modules)
  18072. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  18073. (reporter-submit-bug-report
  18074. "emacs-orgmode@gnu.org"
  18075. (org-version nil 'full)
  18076. (let (list)
  18077. (save-window-excursion
  18078. (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
  18079. (delete-other-windows)
  18080. (erase-buffer)
  18081. (insert "You are about to submit a bug report to the Org-mode mailing list.
  18082. We would like to add your full Org-mode and Outline configuration to the
  18083. bug report. This greatly simplifies the work of the maintainer and
  18084. other experts on the mailing list.
  18085. HOWEVER, some variables you have customized may contain private
  18086. information. The names of customers, colleagues, or friends, might
  18087. appear in the form of file names, tags, todo states, or search strings.
  18088. If you answer yes to the prompt, you might want to check and remove
  18089. such private information before sending the email.")
  18090. (add-text-properties (point-min) (point-max) '(face org-warning))
  18091. (when (yes-or-no-p "Include your Org-mode configuration ")
  18092. (mapatoms
  18093. (lambda (v)
  18094. (and (boundp v)
  18095. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  18096. (or (and (symbol-value v)
  18097. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  18098. (and
  18099. (get v 'custom-type) (get v 'standard-value)
  18100. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  18101. (push v list)))))
  18102. (kill-buffer (get-buffer "*Warn about privacy*"))
  18103. list))
  18104. nil nil
  18105. "Remember to cover the basics, that is, what you expected to happen and
  18106. what in fact did happen. You don't know how to make a good report? See
  18107. http://orgmode.org/manual/Feedback.html#Feedback
  18108. Your bug report will be posted to the Org-mode mailing list.
  18109. ------------------------------------------------------------------------")
  18110. (save-excursion
  18111. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  18112. (replace-match "\\1Bug: \\3 [\\2]")))))
  18113. (defun org-install-agenda-files-menu ()
  18114. (let ((bl (buffer-list)))
  18115. (save-excursion
  18116. (while bl
  18117. (set-buffer (pop bl))
  18118. (if (derived-mode-p 'org-mode) (setq bl nil)))
  18119. (when (derived-mode-p 'org-mode)
  18120. (easy-menu-change
  18121. '("Org") "File List for Agenda"
  18122. (append
  18123. (list
  18124. ["Edit File List" (org-edit-agenda-file-list) t]
  18125. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  18126. ["Remove Current File from List" org-remove-file t]
  18127. ["Cycle through agenda files" org-cycle-agenda-files t]
  18128. ["Occur in all agenda files" org-occur-in-agenda-files t]
  18129. "--")
  18130. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  18131. ;;;; Documentation
  18132. ;;;###autoload
  18133. (defun org-require-autoloaded-modules ()
  18134. (interactive)
  18135. (mapc 'require
  18136. '(org-agenda org-archive org-ascii org-attach org-clock org-colview
  18137. org-docbook org-exp org-html org-icalendar
  18138. org-id org-latex
  18139. org-publish org-remember org-table
  18140. org-timer org-xoxo)))
  18141. ;;;###autoload
  18142. (defun org-reload (&optional uncompiled)
  18143. "Reload all org lisp files.
  18144. With prefix arg UNCOMPILED, load the uncompiled versions."
  18145. (interactive "P")
  18146. (require 'find-func)
  18147. (let* ((file-re "^org\\(-.*\\)?\\.el")
  18148. (dir-org (file-name-directory (org-find-library-dir "org")))
  18149. (dir-org-contrib (ignore-errors
  18150. (file-name-directory
  18151. (org-find-library-dir "org-contribdir"))))
  18152. (babel-files
  18153. (mapcar (lambda (el) (concat "ob" (when el (format "-%s" el)) ".el"))
  18154. (append (list nil "comint" "eval" "exp" "keys"
  18155. "lob" "ref" "table" "tangle")
  18156. (delq nil
  18157. (mapcar
  18158. (lambda (lang)
  18159. (when (cdr lang) (symbol-name (car lang))))
  18160. org-babel-load-languages)))))
  18161. (files
  18162. (append babel-files
  18163. (and dir-org-contrib
  18164. (directory-files dir-org-contrib t file-re))
  18165. (directory-files dir-org t file-re)))
  18166. (remove-re (concat (if (featurep 'xemacs)
  18167. "org-colview" "org-colview-xemacs")
  18168. "\\'")))
  18169. (setq files (mapcar 'file-name-sans-extension files))
  18170. (setq files (mapcar
  18171. (lambda (x) (if (string-match remove-re x) nil x))
  18172. files))
  18173. (setq files (delq nil files))
  18174. (mapc
  18175. (lambda (f)
  18176. (when (featurep (intern (file-name-nondirectory f)))
  18177. (if (and (not uncompiled)
  18178. (file-exists-p (concat f ".elc")))
  18179. (load (concat f ".elc") nil nil 'nosuffix)
  18180. (load (concat f ".el") nil nil 'nosuffix))))
  18181. files)
  18182. (load (concat dir-org "org-version.el") 'noerror nil 'nosuffix))
  18183. (org-version nil 'full 'message))
  18184. ;;;###autoload
  18185. (defun org-customize ()
  18186. "Call the customize function with org as argument."
  18187. (interactive)
  18188. (org-load-modules-maybe)
  18189. (org-require-autoloaded-modules)
  18190. (customize-browse 'org))
  18191. (defun org-create-customize-menu ()
  18192. "Create a full customization menu for Org-mode, insert it into the menu."
  18193. (interactive)
  18194. (org-load-modules-maybe)
  18195. (org-require-autoloaded-modules)
  18196. (if (fboundp 'customize-menu-create)
  18197. (progn
  18198. (easy-menu-change
  18199. '("Org") "Customize"
  18200. `(["Browse Org group" org-customize t]
  18201. "--"
  18202. ,(customize-menu-create 'org)
  18203. ["Set" Custom-set t]
  18204. ["Save" Custom-save t]
  18205. ["Reset to Current" Custom-reset-current t]
  18206. ["Reset to Saved" Custom-reset-saved t]
  18207. ["Reset to Standard Settings" Custom-reset-standard t]))
  18208. (message "\"Org\"-menu now contains full customization menu"))
  18209. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  18210. ;;;; Miscellaneous stuff
  18211. ;;; Generally useful functions
  18212. (defun org-get-at-bol (property)
  18213. "Get text property PROPERTY at beginning of line."
  18214. (get-text-property (point-at-bol) property))
  18215. (defun org-find-text-property-in-string (prop s)
  18216. "Return the first non-nil value of property PROP in string S."
  18217. (or (get-text-property 0 prop s)
  18218. (get-text-property (or (next-single-property-change 0 prop s) 0)
  18219. prop s)))
  18220. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  18221. "Display the given MESSAGE as a warning."
  18222. (if (fboundp 'display-warning)
  18223. (display-warning 'org message
  18224. (if (featurep 'xemacs) 'warning :warning))
  18225. (let ((buf (get-buffer-create "*Org warnings*")))
  18226. (with-current-buffer buf
  18227. (goto-char (point-max))
  18228. (insert "Warning (Org): " message)
  18229. (unless (bolp)
  18230. (newline)))
  18231. (display-buffer buf)
  18232. (sit-for 0))))
  18233. (defun org-eval (form)
  18234. "Eval FORM and return result."
  18235. (condition-case error
  18236. (eval form)
  18237. (error (format "%%![Error: %s]" error))))
  18238. (defun org-in-clocktable-p ()
  18239. "Check if the cursor is in a clocktable."
  18240. (let ((pos (point)) start)
  18241. (save-excursion
  18242. (end-of-line 1)
  18243. (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
  18244. (setq start (match-beginning 0))
  18245. (re-search-forward "^[ \t]*#\\+END:.*" nil t)
  18246. (>= (match-end 0) pos)
  18247. start))))
  18248. (defun org-in-commented-line ()
  18249. "Is point in a line starting with `#'?"
  18250. (equal (char-after (point-at-bol)) ?#))
  18251. (defun org-in-indented-comment-line ()
  18252. "Is point in a line starting with `#' after some white space?"
  18253. (save-excursion
  18254. (save-match-data
  18255. (goto-char (point-at-bol))
  18256. (looking-at "[ \t]*#"))))
  18257. (defun org-in-verbatim-emphasis ()
  18258. (save-match-data
  18259. (and (org-in-regexp org-emph-re 2) (member (match-string 3) '("=" "~")))))
  18260. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  18261. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  18262. (if (and marker (marker-buffer marker)
  18263. (buffer-live-p (marker-buffer marker)))
  18264. (progn
  18265. (org-pop-to-buffer-same-window (marker-buffer marker))
  18266. (if (or (> marker (point-max)) (< marker (point-min)))
  18267. (widen))
  18268. (goto-char marker)
  18269. (org-show-context 'org-goto))
  18270. (if bookmark
  18271. (bookmark-jump bookmark)
  18272. (error "Cannot find location"))))
  18273. (defun org-quote-csv-field (s)
  18274. "Quote field for inclusion in CSV material."
  18275. (if (string-match "[\",]" s)
  18276. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  18277. s))
  18278. (defun org-force-self-insert (N)
  18279. "Needed to enforce self-insert under remapping."
  18280. (interactive "p")
  18281. (self-insert-command N))
  18282. (defun org-string-width (s)
  18283. "Compute width of string, ignoring invisible characters.
  18284. This ignores character with invisibility property `org-link', and also
  18285. characters with property `org-cwidth', because these will become invisible
  18286. upon the next fontification round."
  18287. (let (b l)
  18288. (when (or (eq t buffer-invisibility-spec)
  18289. (assq 'org-link buffer-invisibility-spec))
  18290. (while (setq b (text-property-any 0 (length s)
  18291. 'invisible 'org-link s))
  18292. (setq s (concat (substring s 0 b)
  18293. (substring s (or (next-single-property-change
  18294. b 'invisible s) (length s)))))))
  18295. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  18296. (setq s (concat (substring s 0 b)
  18297. (substring s (or (next-single-property-change
  18298. b 'org-cwidth s) (length s))))))
  18299. (setq l (string-width s) b -1)
  18300. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  18301. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  18302. l))
  18303. (defun org-shorten-string (s maxlength)
  18304. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  18305. If the string is shorter or has length MAXLENGTH, just return the
  18306. original string. If it is longer, the functions finds a space in the
  18307. string, breaks this string off at that locations and adds three dots
  18308. as ellipsis. Including the ellipsis, the string will not be longer
  18309. than MAXLENGTH. If finding a good breaking point in the string does
  18310. not work, the string is just chopped off in the middle of a word
  18311. if necessary."
  18312. (if (<= (length s) maxlength)
  18313. s
  18314. (let* ((n (max (- maxlength 4) 1))
  18315. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  18316. (if (string-match re s)
  18317. (concat (match-string 1 s) "...")
  18318. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  18319. (defun org-get-indentation (&optional line)
  18320. "Get the indentation of the current line, interpreting tabs.
  18321. When LINE is given, assume it represents a line and compute its indentation."
  18322. (if line
  18323. (if (string-match "^ *" (org-remove-tabs line))
  18324. (match-end 0))
  18325. (save-excursion
  18326. (beginning-of-line 1)
  18327. (skip-chars-forward " \t")
  18328. (current-column))))
  18329. (defun org-get-string-indentation (s)
  18330. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  18331. (let ((n -1) (i 0) (w tab-width) c)
  18332. (catch 'exit
  18333. (while (< (setq n (1+ n)) (length s))
  18334. (setq c (aref s n))
  18335. (cond ((= c ?\ ) (setq i (1+ i)))
  18336. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  18337. (t (throw 'exit t)))))
  18338. i))
  18339. (defun org-remove-tabs (s &optional width)
  18340. "Replace tabulators in S with spaces.
  18341. Assumes that s is a single line, starting in column 0."
  18342. (setq width (or width tab-width))
  18343. (while (string-match "\t" s)
  18344. (setq s (replace-match
  18345. (make-string
  18346. (- (* width (/ (+ (match-beginning 0) width) width))
  18347. (match-beginning 0)) ?\ )
  18348. t t s)))
  18349. s)
  18350. (defun org-fix-indentation (line ind)
  18351. "Fix indentation in LINE.
  18352. IND is a cons cell with target and minimum indentation.
  18353. If the current indentation in LINE is smaller than the minimum,
  18354. leave it alone. If it is larger than ind, set it to the target."
  18355. (let* ((l (org-remove-tabs line))
  18356. (i (org-get-indentation l))
  18357. (i1 (car ind)) (i2 (cdr ind)))
  18358. (if (>= i i2) (setq l (substring line i2)))
  18359. (if (> i1 0)
  18360. (concat (make-string i1 ?\ ) l)
  18361. l)))
  18362. (defun org-remove-indentation (code &optional n)
  18363. "Remove the maximum common indentation from the lines in CODE.
  18364. N may optionally be the number of spaces to remove."
  18365. (with-temp-buffer
  18366. (insert code)
  18367. (org-do-remove-indentation n)
  18368. (buffer-string)))
  18369. (defun org-do-remove-indentation (&optional n)
  18370. "Remove the maximum common indentation from the buffer."
  18371. (untabify (point-min) (point-max))
  18372. (let ((min 10000) re)
  18373. (if n
  18374. (setq min n)
  18375. (goto-char (point-min))
  18376. (while (re-search-forward "^ *[^ \n]" nil t)
  18377. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  18378. (unless (or (= min 0) (= min 10000))
  18379. (setq re (format "^ \\{%d\\}" min))
  18380. (goto-char (point-min))
  18381. (while (re-search-forward re nil t)
  18382. (replace-match "")
  18383. (end-of-line 1))
  18384. min)))
  18385. (defun org-fill-template (template alist)
  18386. "Find each %key of ALIST in TEMPLATE and replace it."
  18387. (let ((case-fold-search nil)
  18388. entry key value)
  18389. (setq alist (sort (copy-sequence alist)
  18390. (lambda (a b) (< (length (car a)) (length (car b))))))
  18391. (while (setq entry (pop alist))
  18392. (setq template
  18393. (replace-regexp-in-string
  18394. (concat "%" (regexp-quote (car entry)))
  18395. (cdr entry) template t t)))
  18396. template))
  18397. (defun org-base-buffer (buffer)
  18398. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  18399. (if (not buffer)
  18400. buffer
  18401. (or (buffer-base-buffer buffer)
  18402. buffer)))
  18403. (defun org-trim (s)
  18404. "Remove whitespace at beginning and end of string."
  18405. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  18406. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  18407. s)
  18408. (defun org-wrap (string &optional width lines)
  18409. "Wrap string to either a number of lines, or a width in characters.
  18410. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  18411. that costs. If there is a word longer than WIDTH, the text is actually
  18412. wrapped to the length of that word.
  18413. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  18414. many lines, whatever width that takes.
  18415. The return value is a list of lines, without newlines at the end."
  18416. (let* ((words (org-split-string string "[ \t\n]+"))
  18417. (maxword (apply 'max (mapcar 'org-string-width words)))
  18418. w ll)
  18419. (cond (width
  18420. (org-do-wrap words (max maxword width)))
  18421. (lines
  18422. (setq w maxword)
  18423. (setq ll (org-do-wrap words maxword))
  18424. (if (<= (length ll) lines)
  18425. ll
  18426. (setq ll words)
  18427. (while (> (length ll) lines)
  18428. (setq w (1+ w))
  18429. (setq ll (org-do-wrap words w)))
  18430. ll))
  18431. (t (error "Cannot wrap this")))))
  18432. (defun org-do-wrap (words width)
  18433. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  18434. (let (lines line)
  18435. (while words
  18436. (setq line (pop words))
  18437. (while (and words (< (+ (length line) (length (car words))) width))
  18438. (setq line (concat line " " (pop words))))
  18439. (setq lines (push line lines)))
  18440. (nreverse lines)))
  18441. (defun org-split-string (string &optional separators)
  18442. "Splits STRING into substrings at SEPARATORS.
  18443. No empty strings are returned if there are matches at the beginning
  18444. and end of string."
  18445. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  18446. (start 0)
  18447. notfirst
  18448. (list nil))
  18449. (while (and (string-match rexp string
  18450. (if (and notfirst
  18451. (= start (match-beginning 0))
  18452. (< start (length string)))
  18453. (1+ start) start))
  18454. (< (match-beginning 0) (length string)))
  18455. (setq notfirst t)
  18456. (or (eq (match-beginning 0) 0)
  18457. (and (eq (match-beginning 0) (match-end 0))
  18458. (eq (match-beginning 0) start))
  18459. (setq list
  18460. (cons (substring string start (match-beginning 0))
  18461. list)))
  18462. (setq start (match-end 0)))
  18463. (or (eq start (length string))
  18464. (setq list
  18465. (cons (substring string start)
  18466. list)))
  18467. (nreverse list)))
  18468. (defun org-quote-vert (s)
  18469. "Replace \"|\" with \"\\vert\"."
  18470. (while (string-match "|" s)
  18471. (setq s (replace-match "\\vert" t t s)))
  18472. s)
  18473. (defun org-uuidgen-p (s)
  18474. "Is S an ID created by UUIDGEN?"
  18475. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  18476. (defun org-in-src-block-p nil
  18477. "Whether point is in a code source block."
  18478. (let (ov)
  18479. (when (setq ov (overlays-at (point)))
  18480. (memq 'org-block-background
  18481. (overlay-properties
  18482. (car ov))))))
  18483. (defun org-context ()
  18484. "Return a list of contexts of the current cursor position.
  18485. If several contexts apply, all are returned.
  18486. Each context entry is a list with a symbol naming the context, and
  18487. two positions indicating start and end of the context. Possible
  18488. contexts are:
  18489. :headline anywhere in a headline
  18490. :headline-stars on the leading stars in a headline
  18491. :todo-keyword on a TODO keyword (including DONE) in a headline
  18492. :tags on the TAGS in a headline
  18493. :priority on the priority cookie in a headline
  18494. :item on the first line of a plain list item
  18495. :item-bullet on the bullet/number of a plain list item
  18496. :checkbox on the checkbox in a plain list item
  18497. :table in an org-mode table
  18498. :table-special on a special filed in a table
  18499. :table-table in a table.el table
  18500. :clocktable in a clocktable
  18501. :src-block in a source block
  18502. :link on a hyperlink
  18503. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT, QUOTE.
  18504. :target on a <<target>>
  18505. :radio-target on a <<<radio-target>>>
  18506. :latex-fragment on a LaTeX fragment
  18507. :latex-preview on a LaTeX fragment with overlaid preview image
  18508. This function expects the position to be visible because it uses font-lock
  18509. faces as a help to recognize the following contexts: :table-special, :link,
  18510. and :keyword."
  18511. (let* ((f (get-text-property (point) 'face))
  18512. (faces (if (listp f) f (list f)))
  18513. (case-fold-search t)
  18514. (p (point)) clist o)
  18515. ;; First the large context
  18516. (cond
  18517. ((org-at-heading-p t)
  18518. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  18519. (when (progn
  18520. (beginning-of-line 1)
  18521. (looking-at org-todo-line-tags-regexp))
  18522. (push (org-point-in-group p 1 :headline-stars) clist)
  18523. (push (org-point-in-group p 2 :todo-keyword) clist)
  18524. (push (org-point-in-group p 4 :tags) clist))
  18525. (goto-char p)
  18526. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  18527. (if (looking-at "\\[#[A-Z0-9]\\]")
  18528. (push (org-point-in-group p 0 :priority) clist)))
  18529. ((org-at-item-p)
  18530. (push (org-point-in-group p 2 :item-bullet) clist)
  18531. (push (list :item (point-at-bol)
  18532. (save-excursion (org-end-of-item) (point)))
  18533. clist)
  18534. (and (org-at-item-checkbox-p)
  18535. (push (org-point-in-group p 0 :checkbox) clist)))
  18536. ((org-at-table-p)
  18537. (push (list :table (org-table-begin) (org-table-end)) clist)
  18538. (if (memq 'org-formula faces)
  18539. (push (list :table-special
  18540. (previous-single-property-change p 'face)
  18541. (next-single-property-change p 'face)) clist)))
  18542. ((org-at-table-p 'any)
  18543. (push (list :table-table) clist)))
  18544. (goto-char p)
  18545. ;; New the "medium" contexts: clocktables, source blocks
  18546. (cond ((org-in-clocktable-p)
  18547. (push (list :clocktable
  18548. (and (or (looking-at "#\\+BEGIN: clocktable")
  18549. (search-backward "#+BEGIN: clocktable" nil t))
  18550. (match-beginning 0))
  18551. (and (re-search-forward "#\\+END:?" nil t)
  18552. (match-end 0))) clist))
  18553. ((org-in-src-block-p)
  18554. (push (list :src-block
  18555. (and (or (looking-at "#\\+BEGIN_SRC")
  18556. (search-backward "#+BEGIN_SRC" nil t))
  18557. (match-beginning 0))
  18558. (and (search-forward "#+END_SRC" nil t)
  18559. (match-beginning 0))) clist)))
  18560. (goto-char p)
  18561. ;; Now the small context
  18562. (cond
  18563. ((org-at-timestamp-p)
  18564. (push (org-point-in-group p 0 :timestamp) clist))
  18565. ((memq 'org-link faces)
  18566. (push (list :link
  18567. (previous-single-property-change p 'face)
  18568. (next-single-property-change p 'face)) clist))
  18569. ((memq 'org-special-keyword faces)
  18570. (push (list :keyword
  18571. (previous-single-property-change p 'face)
  18572. (next-single-property-change p 'face)) clist))
  18573. ((org-at-target-p)
  18574. (push (org-point-in-group p 0 :target) clist)
  18575. (goto-char (1- (match-beginning 0)))
  18576. (if (looking-at org-radio-target-regexp)
  18577. (push (org-point-in-group p 0 :radio-target) clist))
  18578. (goto-char p))
  18579. ((setq o (car (delq nil
  18580. (mapcar
  18581. (lambda (x)
  18582. (if (memq x org-latex-fragment-image-overlays) x))
  18583. (overlays-at (point))))))
  18584. (push (list :latex-fragment
  18585. (overlay-start o) (overlay-end o)) clist)
  18586. (push (list :latex-preview
  18587. (overlay-start o) (overlay-end o)) clist))
  18588. ((org-inside-LaTeX-fragment-p)
  18589. ;; FIXME: positions wrong.
  18590. (push (list :latex-fragment (point) (point)) clist)))
  18591. (setq clist (nreverse (delq nil clist)))
  18592. clist))
  18593. ;; FIXME: Compare with at-regexp-p Do we need both?
  18594. (defun org-in-regexp (re &optional nlines visually)
  18595. "Check if point is inside a match of regexp.
  18596. Normally only the current line is checked, but you can include NLINES extra
  18597. lines both before and after point into the search.
  18598. If VISUALLY is set, require that the cursor is not after the match but
  18599. really on, so that the block visually is on the match."
  18600. (catch 'exit
  18601. (let ((pos (point))
  18602. (eol (point-at-eol (+ 1 (or nlines 0))))
  18603. (inc (if visually 1 0)))
  18604. (save-excursion
  18605. (beginning-of-line (- 1 (or nlines 0)))
  18606. (while (re-search-forward re eol t)
  18607. (if (and (<= (match-beginning 0) pos)
  18608. (>= (+ inc (match-end 0)) pos))
  18609. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  18610. (defun org-at-regexp-p (regexp)
  18611. "Is point inside a match of REGEXP in the current line?"
  18612. (catch 'exit
  18613. (save-excursion
  18614. (let ((pos (point)) (end (point-at-eol)))
  18615. (beginning-of-line 1)
  18616. (while (re-search-forward regexp end t)
  18617. (if (and (<= (match-beginning 0) pos)
  18618. (>= (match-end 0) pos))
  18619. (throw 'exit t)))
  18620. nil))))
  18621. (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
  18622. "Non-nil when point is between matches of START-RE and END-RE.
  18623. Also return a non-nil value when point is on one of the matches.
  18624. Optional arguments LIM-UP and LIM-DOWN bound the search; they are
  18625. buffer positions. Default values are the positions of headlines
  18626. surrounding the point.
  18627. The functions returns a cons cell whose car (resp. cdr) is the
  18628. position before START-RE (resp. after END-RE)."
  18629. (save-match-data
  18630. (let ((pos (point))
  18631. (limit-up (or lim-up (save-excursion (outline-previous-heading))))
  18632. (limit-down (or lim-down (save-excursion (outline-next-heading))))
  18633. beg end)
  18634. (save-excursion
  18635. ;; Point is on a block when on START-RE or if START-RE can be
  18636. ;; found before it...
  18637. (and (or (org-at-regexp-p start-re)
  18638. (re-search-backward start-re limit-up t))
  18639. (setq beg (match-beginning 0))
  18640. ;; ... and END-RE after it...
  18641. (goto-char (match-end 0))
  18642. (re-search-forward end-re limit-down t)
  18643. (> (setq end (match-end 0)) pos)
  18644. ;; ... without another START-RE in-between.
  18645. (goto-char (match-beginning 0))
  18646. (not (re-search-backward start-re (1+ beg) t))
  18647. ;; Return value.
  18648. (cons beg end))))))
  18649. (defun org-in-block-p (names)
  18650. "Non-nil when point belongs to a block whose name belongs to NAMES.
  18651. NAMES is a list of strings containing names of blocks.
  18652. Return first block name matched, or nil. Beware that in case of
  18653. nested blocks, the returned name may not belong to the closest
  18654. block from point."
  18655. (save-match-data
  18656. (catch 'exit
  18657. (let ((case-fold-search t)
  18658. (lim-up (save-excursion (outline-previous-heading)))
  18659. (lim-down (save-excursion (outline-next-heading))))
  18660. (mapc (lambda (name)
  18661. (let ((n (regexp-quote name)))
  18662. (when (org-between-regexps-p
  18663. (concat "^[ \t]*#\\+begin_" n)
  18664. (concat "^[ \t]*#\\+end_" n)
  18665. lim-up lim-down)
  18666. (throw 'exit n))))
  18667. names))
  18668. nil)))
  18669. (defun org-occur-in-agenda-files (regexp &optional nlines)
  18670. "Call `multi-occur' with buffers for all agenda files."
  18671. (interactive "sOrg-files matching: \np")
  18672. (let* ((files (org-agenda-files))
  18673. (tnames (mapcar 'file-truename files))
  18674. (extra org-agenda-text-search-extra-files)
  18675. f)
  18676. (when (eq (car extra) 'agenda-archives)
  18677. (setq extra (cdr extra))
  18678. (setq files (org-add-archive-files files)))
  18679. (while (setq f (pop extra))
  18680. (unless (member (file-truename f) tnames)
  18681. (add-to-list 'files f 'append)
  18682. (add-to-list 'tnames (file-truename f) 'append)))
  18683. (multi-occur
  18684. (mapcar (lambda (x)
  18685. (with-current-buffer
  18686. (or (get-file-buffer x) (find-file-noselect x))
  18687. (widen)
  18688. (current-buffer)))
  18689. files)
  18690. regexp)))
  18691. (if (boundp 'occur-mode-find-occurrence-hook)
  18692. ;; Emacs 23
  18693. (add-hook 'occur-mode-find-occurrence-hook
  18694. (lambda ()
  18695. (when (derived-mode-p 'org-mode)
  18696. (org-reveal))))
  18697. ;; Emacs 22
  18698. (defadvice occur-mode-goto-occurrence
  18699. (after org-occur-reveal activate)
  18700. (and (derived-mode-p 'org-mode) (org-reveal)))
  18701. (defadvice occur-mode-goto-occurrence-other-window
  18702. (after org-occur-reveal activate)
  18703. (and (derived-mode-p 'org-mode) (org-reveal)))
  18704. (defadvice occur-mode-display-occurrence
  18705. (after org-occur-reveal activate)
  18706. (when (derived-mode-p 'org-mode)
  18707. (let ((pos (occur-mode-find-occurrence)))
  18708. (with-current-buffer (marker-buffer pos)
  18709. (save-excursion
  18710. (goto-char pos)
  18711. (org-reveal)))))))
  18712. (defun org-occur-link-in-agenda-files ()
  18713. "Create a link and search for it in the agendas.
  18714. The link is not stored in `org-stored-links', it is just created
  18715. for the search purpose."
  18716. (interactive)
  18717. (let ((link (condition-case nil
  18718. (org-store-link nil)
  18719. (error "Unable to create a link to here"))))
  18720. (org-occur-in-agenda-files (regexp-quote link))))
  18721. (defun org-uniquify (list)
  18722. "Remove duplicate elements from LIST."
  18723. (let (res)
  18724. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  18725. res))
  18726. (defun org-delete-all (elts list)
  18727. "Remove all elements in ELTS from LIST."
  18728. (while elts
  18729. (setq list (delete (pop elts) list)))
  18730. list)
  18731. (defun org-count (cl-item cl-seq)
  18732. "Count the number of occurrences of ITEM in SEQ.
  18733. Taken from `count' in cl-seq.el with all keyword arguments removed."
  18734. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  18735. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  18736. (while (< cl-start cl-end)
  18737. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  18738. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  18739. (setq cl-start (1+ cl-start)))
  18740. cl-count))
  18741. (defun org-remove-if (predicate seq)
  18742. "Remove everything from SEQ that fulfills PREDICATE."
  18743. (let (res e)
  18744. (while seq
  18745. (setq e (pop seq))
  18746. (if (not (funcall predicate e)) (push e res)))
  18747. (nreverse res)))
  18748. (defun org-remove-if-not (predicate seq)
  18749. "Remove everything from SEQ that does not fulfill PREDICATE."
  18750. (let (res e)
  18751. (while seq
  18752. (setq e (pop seq))
  18753. (if (funcall predicate e) (push e res)))
  18754. (nreverse res)))
  18755. (defun org-reduce (cl-func cl-seq &rest cl-keys)
  18756. "Reduce two-argument FUNCTION across SEQ.
  18757. Taken from `reduce' in cl-seq.el with all keyword arguments but
  18758. \":initial-value\" removed."
  18759. (let ((cl-accum (cond ((memq :initial-value cl-keys)
  18760. (cadr (memq :initial-value cl-keys)))
  18761. (cl-seq (pop cl-seq))
  18762. (t (funcall cl-func)))))
  18763. (while cl-seq
  18764. (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
  18765. cl-accum))
  18766. (defun org-back-over-empty-lines ()
  18767. "Move backwards over whitespace, to the beginning of the first empty line.
  18768. Returns the number of empty lines passed."
  18769. (let ((pos (point)))
  18770. (if (cdr (assoc 'heading org-blank-before-new-entry))
  18771. (skip-chars-backward " \t\n\r")
  18772. (unless (eobp)
  18773. (forward-line -1)))
  18774. (beginning-of-line 2)
  18775. (goto-char (min (point) pos))
  18776. (count-lines (point) pos)))
  18777. (defun org-skip-whitespace ()
  18778. (skip-chars-forward " \t\n\r"))
  18779. (defun org-point-in-group (point group &optional context)
  18780. "Check if POINT is in match-group GROUP.
  18781. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  18782. match. If the match group does not exist or point is not inside it,
  18783. return nil."
  18784. (and (match-beginning group)
  18785. (>= point (match-beginning group))
  18786. (<= point (match-end group))
  18787. (if context
  18788. (list context (match-beginning group) (match-end group))
  18789. t)))
  18790. (defun org-switch-to-buffer-other-window (&rest args)
  18791. "Switch to buffer in a second window on the current frame.
  18792. In particular, do not allow pop-up frames.
  18793. Returns the newly created buffer."
  18794. (let (pop-up-frames special-display-buffer-names special-display-regexps
  18795. special-display-function)
  18796. (apply 'switch-to-buffer-other-window args)))
  18797. (defun org-combine-plists (&rest plists)
  18798. "Create a single property list from all plists in PLISTS.
  18799. The process starts by copying the first list, and then setting properties
  18800. from the other lists. Settings in the last list are the most significant
  18801. ones and overrule settings in the other lists."
  18802. (let ((rtn (copy-sequence (pop plists)))
  18803. p v ls)
  18804. (while plists
  18805. (setq ls (pop plists))
  18806. (while ls
  18807. (setq p (pop ls) v (pop ls))
  18808. (setq rtn (plist-put rtn p v))))
  18809. rtn))
  18810. (defun org-replace-escapes (string table)
  18811. "Replace %-escapes in STRING with values in TABLE.
  18812. TABLE is an association list with keys like \"%a\" and string values.
  18813. The sequences in STRING may contain normal field width and padding information,
  18814. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  18815. so values can contain further %-escapes if they are define later in TABLE."
  18816. (let ((tbl (copy-alist table))
  18817. (case-fold-search nil)
  18818. (pchg 0)
  18819. e re rpl)
  18820. (while (setq e (pop tbl))
  18821. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  18822. (when (and (cdr e) (string-match re (cdr e)))
  18823. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  18824. (safe "SREF"))
  18825. (add-text-properties 0 3 (list 'sref sref) safe)
  18826. (setcdr e (replace-match safe t t (cdr e)))))
  18827. (while (string-match re string)
  18828. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  18829. (cdr e)))
  18830. (setq string (replace-match rpl t t string))))
  18831. (while (setq pchg (next-property-change pchg string))
  18832. (let ((sref (get-text-property pchg 'sref string)))
  18833. (when (and sref (string-match "SREF" string pchg))
  18834. (setq string (replace-match sref t t string)))))
  18835. string))
  18836. (defun org-sublist (list start end)
  18837. "Return a section of LIST, from START to END.
  18838. Counting starts at 1."
  18839. (let (rtn (c start))
  18840. (setq list (nthcdr (1- start) list))
  18841. (while (and list (<= c end))
  18842. (push (pop list) rtn)
  18843. (setq c (1+ c)))
  18844. (nreverse rtn)))
  18845. (defun org-find-base-buffer-visiting (file)
  18846. "Like `find-buffer-visiting' but always return the base buffer and
  18847. not an indirect buffer."
  18848. (let ((buf (or (get-file-buffer file)
  18849. (find-buffer-visiting file))))
  18850. (if buf
  18851. (or (buffer-base-buffer buf) buf)
  18852. nil)))
  18853. (defun org-image-file-name-regexp (&optional extensions)
  18854. "Return regexp matching the file names of images.
  18855. If EXTENSIONS is given, only match these."
  18856. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  18857. (image-file-name-regexp)
  18858. (let ((image-file-name-extensions
  18859. (or extensions
  18860. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  18861. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  18862. (concat "\\."
  18863. (regexp-opt (nconc (mapcar 'upcase
  18864. image-file-name-extensions)
  18865. image-file-name-extensions)
  18866. t)
  18867. "\\'"))))
  18868. (defun org-file-image-p (file &optional extensions)
  18869. "Return non-nil if FILE is an image."
  18870. (save-match-data
  18871. (string-match (org-image-file-name-regexp extensions) file)))
  18872. (defun org-get-cursor-date ()
  18873. "Return the date at cursor in as a time.
  18874. This works in the calendar and in the agenda, anywhere else it just
  18875. returns the current time."
  18876. (let (date day defd)
  18877. (cond
  18878. ((eq major-mode 'calendar-mode)
  18879. (setq date (calendar-cursor-to-date)
  18880. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  18881. ((eq major-mode 'org-agenda-mode)
  18882. (setq day (get-text-property (point) 'day))
  18883. (if day
  18884. (setq date (calendar-gregorian-from-absolute day)
  18885. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  18886. (nth 2 date))))))
  18887. (or defd (current-time))))
  18888. (defvar org-agenda-action-marker (make-marker)
  18889. "Marker pointing to the entry for the next agenda action.")
  18890. (defun org-mark-entry-for-agenda-action ()
  18891. "Mark the current entry as target of an agenda action.
  18892. Agenda actions are actions executed from the agenda with the key `k',
  18893. which make use of the date at the cursor."
  18894. (interactive)
  18895. (move-marker org-agenda-action-marker
  18896. (save-excursion (org-back-to-heading t) (point))
  18897. (current-buffer))
  18898. (message
  18899. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  18900. (defun org-mark-subtree ()
  18901. "Mark the current subtree.
  18902. This puts point at the start of the current subtree, and mark at the end.
  18903. If point is in an inline task, mark that task instead."
  18904. (interactive)
  18905. (let ((inline-task-p
  18906. (and (featurep 'org-inlinetask)
  18907. (org-inlinetask-in-task-p)))
  18908. (beg))
  18909. ;; Get beginning of subtree
  18910. (cond
  18911. (inline-task-p (org-inlinetask-goto-beginning))
  18912. ((org-at-heading-p) (beginning-of-line))
  18913. (t (org-with-limited-levels (outline-previous-visible-heading 1))))
  18914. (setq beg (point))
  18915. ;; Get end of it
  18916. (if inline-task-p
  18917. (org-inlinetask-goto-end)
  18918. (org-end-of-subtree))
  18919. ;; Mark zone
  18920. (push-mark (point) nil t)
  18921. (goto-char beg)))
  18922. ;;; Paragraph filling stuff.
  18923. ;; We want this to be just right, so use the full arsenal.
  18924. (defun org-indent-line ()
  18925. "Indent line depending on context."
  18926. (interactive)
  18927. (let* ((pos (point))
  18928. (itemp (org-at-item-p))
  18929. (case-fold-search t)
  18930. (org-drawer-regexp (or org-drawer-regexp "\000"))
  18931. (inline-task-p (and (featurep 'org-inlinetask)
  18932. (org-inlinetask-in-task-p)))
  18933. (inline-re (and inline-task-p
  18934. (org-inlinetask-outline-regexp)))
  18935. column)
  18936. (beginning-of-line 1)
  18937. (cond
  18938. ;; Comments
  18939. ((looking-at "# ") (setq column 0))
  18940. ;; Headings
  18941. ((looking-at org-outline-regexp) (setq column 0))
  18942. ;; Included files
  18943. ((looking-at "#\\+include:") (setq column 0))
  18944. ;; Footnote definition
  18945. ((looking-at org-footnote-definition-re) (setq column 0))
  18946. ;; Literal examples
  18947. ((looking-at "[ \t]*:\\( \\|$\\)")
  18948. (setq column (org-get-indentation))) ; do nothing
  18949. ;; Lists
  18950. ((ignore-errors (goto-char (org-in-item-p)))
  18951. (setq column (if itemp
  18952. (org-get-indentation)
  18953. (org-list-item-body-column (point))))
  18954. (goto-char pos))
  18955. ;; Drawers
  18956. ((and (looking-at "[ \t]*:END:")
  18957. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  18958. (save-excursion
  18959. (goto-char (1- (match-beginning 1)))
  18960. (setq column (current-column))))
  18961. ;; Special blocks
  18962. ((and (looking-at "[ \t]*#\\+end_\\([a-z]+\\)")
  18963. (save-excursion
  18964. (re-search-backward
  18965. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  18966. (setq column (org-get-indentation (match-string 0))))
  18967. ((and (not (looking-at "[ \t]*#\\+begin_"))
  18968. (org-between-regexps-p "^[ \t]*#\\+begin_" "[ \t]*#\\+end_"))
  18969. (save-excursion
  18970. (re-search-backward "^[ \t]*#\\+begin_\\([a-z]+\\)" nil t))
  18971. (setq column
  18972. (cond ((equal (downcase (match-string 1)) "src")
  18973. ;; src blocks: let `org-edit-src-exit' handle them
  18974. (org-get-indentation))
  18975. ((equal (downcase (match-string 1)) "example")
  18976. (max (org-get-indentation)
  18977. (org-get-indentation (match-string 0))))
  18978. (t
  18979. (org-get-indentation (match-string 0))))))
  18980. ;; This line has nothing special, look at the previous relevant
  18981. ;; line to compute indentation
  18982. (t
  18983. (beginning-of-line 0)
  18984. (while (and (not (bobp))
  18985. (not (looking-at org-drawer-regexp))
  18986. ;; When point started in an inline task, do not move
  18987. ;; above task starting line.
  18988. (not (and inline-task-p (looking-at inline-re)))
  18989. ;; Skip drawers, blocks, empty lines, verbatim,
  18990. ;; comments, tables, footnotes definitions, lists,
  18991. ;; inline tasks.
  18992. (or (and (looking-at "[ \t]*:END:")
  18993. (re-search-backward org-drawer-regexp nil t))
  18994. (and (looking-at "[ \t]*#\\+end_")
  18995. (re-search-backward "[ \t]*#\\+begin_"nil t))
  18996. (looking-at "[ \t]*[\n:#|]")
  18997. (looking-at org-footnote-definition-re)
  18998. (and (ignore-errors (goto-char (org-in-item-p)))
  18999. (goto-char
  19000. (org-list-get-top-point (org-list-struct))))
  19001. (and (not inline-task-p)
  19002. (featurep 'org-inlinetask)
  19003. (org-inlinetask-in-task-p)
  19004. (or (org-inlinetask-goto-beginning) t))))
  19005. (beginning-of-line 0))
  19006. (cond
  19007. ;; There was an heading above.
  19008. ((looking-at "\\*+[ \t]+")
  19009. (if (not org-adapt-indentation)
  19010. (setq column 0)
  19011. (goto-char (match-end 0))
  19012. (setq column (current-column))))
  19013. ;; A drawer had started and is unfinished
  19014. ((looking-at org-drawer-regexp)
  19015. (goto-char (1- (match-beginning 1)))
  19016. (setq column (current-column)))
  19017. ;; Else, nothing noticeable found: get indentation and go on.
  19018. (t (setq column (org-get-indentation))))))
  19019. ;; Now apply indentation and move cursor accordingly
  19020. (goto-char pos)
  19021. (if (<= (current-column) (current-indentation))
  19022. (org-indent-line-to column)
  19023. (save-excursion (org-indent-line-to column)))
  19024. ;; Special polishing for properties, see `org-property-format'
  19025. (setq column (current-column))
  19026. (beginning-of-line 1)
  19027. (if (looking-at
  19028. "\\([ \t]*\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  19029. (replace-match (concat (match-string 1)
  19030. (format org-property-format
  19031. (match-string 2) (match-string 3)))
  19032. t t))
  19033. (org-move-to-column column)
  19034. (when (and orgstruct-is-++ (eq pos (point)))
  19035. (org-let org-fb-vars
  19036. '(indent-according-to-mode)))))
  19037. (defun org-indent-drawer ()
  19038. "Indent the drawer at point."
  19039. (interactive)
  19040. (let ((p (point))
  19041. (e (and (save-excursion (re-search-forward ":END:" nil t))
  19042. (match-end 0)))
  19043. (folded
  19044. (save-excursion
  19045. (end-of-line)
  19046. (when (overlays-at (point))
  19047. (member 'invisible (overlay-properties
  19048. (car (overlays-at (point)))))))))
  19049. (when folded (org-cycle))
  19050. (indent-for-tab-command)
  19051. (while (and (move-beginning-of-line 2) (< (point) e))
  19052. (indent-for-tab-command))
  19053. (goto-char p)
  19054. (when folded (org-cycle)))
  19055. (message "Drawer at point indented"))
  19056. (defun org-indent-block ()
  19057. "Indent the block at point."
  19058. (interactive)
  19059. (let ((p (point))
  19060. (case-fold-search t)
  19061. (e (and (save-excursion (re-search-forward "#\\+end_?\\(?:[a-z]+\\)?" nil t))
  19062. (match-end 0)))
  19063. (folded
  19064. (save-excursion
  19065. (end-of-line)
  19066. (when (overlays-at (point))
  19067. (member 'invisible (overlay-properties
  19068. (car (overlays-at (point)))))))))
  19069. (when folded (org-cycle))
  19070. (indent-for-tab-command)
  19071. (while (and (move-beginning-of-line 2) (< (point) e))
  19072. (indent-for-tab-command))
  19073. (goto-char p)
  19074. (when folded (org-cycle)))
  19075. (message "Block at point indented"))
  19076. (defun org-indent-region (start end)
  19077. "Indent region."
  19078. (interactive "r")
  19079. (save-excursion
  19080. (let ((line-end (org-current-line end)))
  19081. (goto-char start)
  19082. (while (< (org-current-line) line-end)
  19083. (cond ((org-in-src-block-p) (org-src-native-tab-command-maybe))
  19084. (t (call-interactively 'org-indent-line)))
  19085. (move-beginning-of-line 2)))))
  19086. (defun org-set-autofill-regexps ()
  19087. (interactive)
  19088. ;; We use our own fill-paragraph and auto-fill functions. These
  19089. ;; functions will shadow `fill-prefix' (computed internally with
  19090. ;; `org-fill-context-prefix') and pass through to
  19091. ;; `fill-region-as-paragraph' and `do-auto-fill' as appropriate.
  19092. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  19093. ;; Prevent auto-fill from inserting unwanted new items.
  19094. (when (boundp 'fill-nobreak-predicate)
  19095. (org-set-local
  19096. 'fill-nobreak-predicate
  19097. (org-uniquify
  19098. (append fill-nobreak-predicate
  19099. '(org-fill-item-nobreak-p org-fill-line-break-nobreak-p)))))
  19100. (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
  19101. (org-set-local 'align-mode-rules-list
  19102. '((org-in-buffer-settings
  19103. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  19104. (modes . '(org-mode))))))
  19105. (defun org-fill-item-nobreak-p ()
  19106. "Non-nil when a line break at point would insert a new item."
  19107. (and (looking-at (org-item-re)) (org-list-in-valid-context-p)))
  19108. (defun org-fill-line-break-nobreak-p ()
  19109. "Non-nil when a line break at point would create an Org line break."
  19110. (save-excursion
  19111. (skip-chars-backward "[ \t]")
  19112. (skip-chars-backward "\\\\")
  19113. (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
  19114. (defun org-fill-context-prefix (p)
  19115. "Compute a fill prefix for the line at point P.
  19116. Return fill prefix, as a string, or nil if current line isn't
  19117. meant to be filled."
  19118. (save-excursion
  19119. (let* ((elements (org-element-at-point t))
  19120. (element (car elements))
  19121. (type (org-element-type element))
  19122. (post-affiliated
  19123. (progn
  19124. (goto-char (org-element-property :begin element))
  19125. (while (looking-at org-element--affiliated-re) (forward-line))
  19126. (point))))
  19127. (unless (< p post-affiliated)
  19128. (goto-char p)
  19129. (beginning-of-line)
  19130. (case type
  19131. (comment (looking-at "[ \t]*#\\+? ?") (match-string 0))
  19132. ((item plain-list)
  19133. (make-string (org-list-item-body-column
  19134. (org-element-property :begin element))
  19135. ? ))
  19136. (paragraph
  19137. ;; Fill prefix is usually the same as the current line,
  19138. ;; except if the paragraph is at the beginning of an item.
  19139. (let ((parent (cadr elements)))
  19140. (if (eq (org-element-type parent) 'item)
  19141. (make-string (org-list-item-body-column
  19142. (org-element-property :begin parent))
  19143. ? )
  19144. (if (looking-at "\\s-+") (match-string 0) ""))))
  19145. ((comment-block verse-block)
  19146. ;; Only fill contents if P is within block boundaries.
  19147. (let* ((cbeg (save-excursion (goto-char post-affiliated)
  19148. (forward-line)
  19149. (point)))
  19150. (cend (save-excursion
  19151. (goto-char (org-element-property :end element))
  19152. (skip-chars-backward " \r\t\n")
  19153. (line-beginning-position))))
  19154. (when (and (>= p cbeg) (< p cend))
  19155. (if (looking-at "\\s-+") (match-string 0) "")))))))))
  19156. (defun org-fill-paragraph (&optional justify)
  19157. "Fill element at point, when applicable.
  19158. This function only applies to comment blocks, comments, example
  19159. blocks, paragraphs and verse blocks. Also, as a special case,
  19160. re-align table when point is at one.
  19161. If JUSTIFY is non-nil (interactively, with prefix argument),
  19162. justify as well. If `sentence-end-double-space' is non-nil, then
  19163. period followed by one space does not end a sentence, so don't
  19164. break a line there. The variable `fill-column' controls the
  19165. width for filling."
  19166. (let ((element (org-element-at-point)))
  19167. ;; First check if point is in a blank line at the beginning of the
  19168. ;; buffer. In that case, ignore filling.
  19169. (if (< (point) (org-element-property :begin element)) t
  19170. (case (org-element-type element)
  19171. ;; Align Org tables, leave table.el tables as-is.
  19172. (table-row (org-table-align) t)
  19173. (table
  19174. (when (eq (org-element-property :type element) 'org) (org-table-align))
  19175. t)
  19176. ;; Elements that may contain `line-break' type objects.
  19177. ((paragraph verse-block)
  19178. (let ((beg (org-element-property :contents-begin element))
  19179. (end (org-element-property :contents-end element)))
  19180. ;; Do nothing if point is at an affiliated keyword or at
  19181. ;; verse block markers.
  19182. (if (or (< (point) beg) (>= (point) end)) t
  19183. ;; At a verse block, first narrow to current "paragraph"
  19184. ;; and set current element to that paragraph.
  19185. (save-restriction
  19186. (when (eq (org-element-type element) 'verse-block)
  19187. (narrow-to-region beg end)
  19188. (save-excursion
  19189. (end-of-line)
  19190. (let ((bol-pos (point-at-bol)))
  19191. (re-search-backward org-element-paragraph-separate nil 'm)
  19192. (unless (or (bobp) (= (point-at-bol) bol-pos))
  19193. (forward-line))
  19194. (setq element (org-element-paragraph-parser end)
  19195. beg (org-element-property :contents-begin element)
  19196. end (org-element-property :contents-end element)))))
  19197. ;; Fill paragraph, taking line breaks into consideration.
  19198. ;; For that, slice the paragraph using line breaks as
  19199. ;; separators, and fill the parts in reverse order to
  19200. ;; avoid messing with markers.
  19201. (save-excursion
  19202. (goto-char end)
  19203. (mapc
  19204. (lambda (pos)
  19205. (let ((fill-prefix (org-fill-context-prefix pos)))
  19206. (fill-region-as-paragraph pos (point) justify))
  19207. (goto-char pos))
  19208. ;; Find the list of ending positions for line breaks
  19209. ;; in the current paragraph. Add paragraph beginning
  19210. ;; to include first slice.
  19211. (nreverse
  19212. (cons beg
  19213. (org-element-map
  19214. (org-element--parse-objects
  19215. beg end nil org-element-all-objects)
  19216. 'line-break
  19217. (lambda (lb) (org-element-property :end lb))))))))
  19218. t)))
  19219. ;; Contents of `comment-block' type elements should be filled as
  19220. ;; plain text.
  19221. (comment-block
  19222. (let ((fill-prefix (org-fill-context-prefix (point))))
  19223. (save-excursion
  19224. (fill-region-as-paragraph
  19225. (progn
  19226. (goto-char (org-element-property :begin element))
  19227. (while (looking-at org-element--affiliated-re) (forward-line))
  19228. (forward-line)
  19229. (point))
  19230. (progn
  19231. (goto-char (org-element-property :end element))
  19232. (skip-chars-backward " \r\t\n")
  19233. (line-beginning-position))
  19234. justify))) t)
  19235. (comment
  19236. (let ((fill-prefix (org-fill-context-prefix (point))))
  19237. (save-excursion
  19238. (fill-region-as-paragraph
  19239. (progn
  19240. (goto-char (org-element-property :begin element))
  19241. (while (looking-at org-element--affiliated-re) (forward-line))
  19242. (point))
  19243. (progn
  19244. (goto-char (org-element-property :end element))
  19245. (skip-chars-backward " \r\t\n")
  19246. (line-end-position))))))
  19247. ;; Ignore every other element.
  19248. (otherwise t)))))
  19249. (defun org-auto-fill-function ()
  19250. "Auto-fill function."
  19251. ;; Check if auto-filling is meaningful before computing fill prefix.
  19252. (let ((fc (current-fill-column)))
  19253. (when (and fc (> (current-column) fc))
  19254. (let ((fill-prefix (org-fill-context-prefix (point))))
  19255. (when fill-prefix (do-auto-fill))))))
  19256. ;;; Other stuff.
  19257. (defun org-toggle-fixed-width-section (arg)
  19258. "Toggle the fixed-width export.
  19259. If there is no active region, the QUOTE keyword at the current headline is
  19260. inserted or removed. When present, it causes the text between this headline
  19261. and the next to be exported as fixed-width text, and unmodified.
  19262. If there is an active region, this command adds or removes a colon as the
  19263. first character of this line. If the first character of a line is a colon,
  19264. this line is also exported in fixed-width font."
  19265. (interactive "P")
  19266. (let* ((cc 0)
  19267. (regionp (org-region-active-p))
  19268. (beg (if regionp (region-beginning) (point)))
  19269. (end (if regionp (region-end)))
  19270. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  19271. (case-fold-search nil)
  19272. (re "[ \t]*\\(:\\(?: \\|$\\)\\)")
  19273. off)
  19274. (if regionp
  19275. (save-excursion
  19276. (goto-char beg)
  19277. (setq cc (current-column))
  19278. (beginning-of-line 1)
  19279. (setq off (looking-at re))
  19280. (while (> nlines 0)
  19281. (setq nlines (1- nlines))
  19282. (beginning-of-line 1)
  19283. (cond
  19284. (arg
  19285. (org-move-to-column cc t)
  19286. (insert ": \n")
  19287. (forward-line -1))
  19288. ((and off (looking-at re))
  19289. (replace-match "" t t nil 1))
  19290. ((not off) (org-move-to-column cc t) (insert ": ")))
  19291. (forward-line 1)))
  19292. (save-excursion
  19293. (org-back-to-heading)
  19294. (cond
  19295. ((looking-at (format org-heading-keyword-regexp-format
  19296. org-quote-string))
  19297. (goto-char (match-end 1))
  19298. (looking-at (concat " +" org-quote-string))
  19299. (replace-match "" t t)
  19300. (when (eolp) (insert " ")))
  19301. ((looking-at org-outline-regexp)
  19302. (goto-char (match-end 0))
  19303. (insert org-quote-string " ")))))))
  19304. (defun org-reftex-citation ()
  19305. "Use reftex-citation to insert a citation into the buffer.
  19306. This looks for a line like
  19307. #+BIBLIOGRAPHY: foo plain option:-d
  19308. and derives from it that foo.bib is the bibliography file relevant
  19309. for this document. It then installs the necessary environment for RefTeX
  19310. to work in this buffer and calls `reftex-citation' to insert a citation
  19311. into the buffer.
  19312. Export of such citations to both LaTeX and HTML is handled by the contributed
  19313. package org-exp-bibtex by Taru Karttunen."
  19314. (interactive)
  19315. (let ((reftex-docstruct-symbol 'rds)
  19316. (reftex-cite-format "\\cite{%l}")
  19317. rds bib)
  19318. (save-excursion
  19319. (save-restriction
  19320. (widen)
  19321. (let ((case-fold-search t)
  19322. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  19323. (if (not (save-excursion
  19324. (or (re-search-forward re nil t)
  19325. (re-search-backward re nil t))))
  19326. (error "No bibliography defined in file")
  19327. (setq bib (concat (match-string 1) ".bib")
  19328. rds (list (list 'bib bib)))))))
  19329. (call-interactively 'reftex-citation)))
  19330. ;;;; Functions extending outline functionality
  19331. (defun org-beginning-of-line (&optional arg)
  19332. "Go to the beginning of the current line. If that is invisible, continue
  19333. to a visible line beginning. This makes the function of C-a more intuitive.
  19334. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  19335. first attempt, and only move to after the tags when the cursor is already
  19336. beyond the end of the headline."
  19337. (interactive "P")
  19338. (let ((pos (point))
  19339. (special (if (consp org-special-ctrl-a/e)
  19340. (car org-special-ctrl-a/e)
  19341. org-special-ctrl-a/e))
  19342. refpos)
  19343. (if (org-bound-and-true-p line-move-visual)
  19344. (beginning-of-visual-line 1)
  19345. (beginning-of-line 1))
  19346. (if (and arg (fboundp 'move-beginning-of-line))
  19347. (call-interactively 'move-beginning-of-line)
  19348. (if (bobp)
  19349. nil
  19350. (backward-char 1)
  19351. (if (org-truely-invisible-p)
  19352. (while (and (not (bobp)) (org-truely-invisible-p))
  19353. (backward-char 1)
  19354. (beginning-of-line 1))
  19355. (forward-char 1))))
  19356. (when special
  19357. (cond
  19358. ((and (looking-at org-complex-heading-regexp)
  19359. (= (char-after (match-end 1)) ?\ ))
  19360. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  19361. (point-at-eol)))
  19362. (goto-char
  19363. (if (eq special t)
  19364. (cond ((> pos refpos) refpos)
  19365. ((= pos (point)) refpos)
  19366. (t (point)))
  19367. (cond ((> pos (point)) (point))
  19368. ((not (eq last-command this-command)) (point))
  19369. (t refpos)))))
  19370. ((org-at-item-p)
  19371. ;; Being at an item and not looking at an the item means point
  19372. ;; was previously moved to beginning of a visual line, which
  19373. ;; doesn't contain the item. Therefore, do nothing special,
  19374. ;; just stay here.
  19375. (when (looking-at org-list-full-item-re)
  19376. ;; Set special position at first white space character after
  19377. ;; bullet, and check-box, if any.
  19378. (let ((after-bullet
  19379. (let ((box (match-end 3)))
  19380. (if (not box) (match-end 1)
  19381. (let ((after (char-after box)))
  19382. (if (and after (= after ? )) (1+ box) box))))))
  19383. ;; Special case: Move point to special position when
  19384. ;; currently after it or at beginning of line.
  19385. (if (eq special t)
  19386. (when (or (> pos after-bullet) (= (point) pos))
  19387. (goto-char after-bullet))
  19388. ;; Reversed case: Move point to special position when
  19389. ;; point was already at beginning of line and command is
  19390. ;; repeated.
  19391. (when (and (= (point) pos) (eq last-command this-command))
  19392. (goto-char after-bullet))))))))
  19393. (org-no-warnings
  19394. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  19395. (defun org-end-of-line (&optional arg)
  19396. "Go to the end of the line.
  19397. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  19398. first attempt, and only move to after the tags when the cursor is already
  19399. beyond the end of the headline."
  19400. (interactive "P")
  19401. (let ((special (if (consp org-special-ctrl-a/e)
  19402. (cdr org-special-ctrl-a/e)
  19403. org-special-ctrl-a/e)))
  19404. (cond
  19405. ((or (not special) arg
  19406. (not (or (org-at-heading-p) (org-at-item-p) (org-at-drawer-p))))
  19407. (call-interactively
  19408. (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
  19409. ((fboundp 'move-end-of-line) 'move-end-of-line)
  19410. (t 'end-of-line))))
  19411. ((org-at-heading-p)
  19412. (let ((pos (point)))
  19413. (beginning-of-line 1)
  19414. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  19415. (if (eq special t)
  19416. (if (or (< pos (match-beginning 1))
  19417. (= pos (match-end 0)))
  19418. (goto-char (match-beginning 1))
  19419. (goto-char (match-end 0)))
  19420. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  19421. (goto-char (match-end 0))
  19422. (goto-char (match-beginning 1))))
  19423. (call-interactively (if (fboundp 'move-end-of-line)
  19424. 'move-end-of-line
  19425. 'end-of-line)))))
  19426. ((org-at-drawer-p)
  19427. (move-end-of-line 1)
  19428. (when (overlays-at (1- (point))) (backward-char 1)))
  19429. ;; At an item: Move before any hidden text.
  19430. (t (call-interactively
  19431. (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
  19432. ((fboundp 'move-end-of-line) 'move-end-of-line)
  19433. (t 'end-of-line)))))
  19434. (org-no-warnings
  19435. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  19436. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  19437. (define-key org-mode-map "\C-e" 'org-end-of-line)
  19438. (defun org-backward-sentence (&optional arg)
  19439. "Go to beginning of sentence, or beginning of table field.
  19440. This will call `backward-sentence' or `org-table-beginning-of-field',
  19441. depending on context."
  19442. (interactive "P")
  19443. (cond
  19444. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  19445. (t (call-interactively 'backward-sentence))))
  19446. (defun org-forward-sentence (&optional arg)
  19447. "Go to end of sentence, or end of table field.
  19448. This will call `forward-sentence' or `org-table-end-of-field',
  19449. depending on context."
  19450. (interactive "P")
  19451. (cond
  19452. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  19453. (t (call-interactively 'forward-sentence))))
  19454. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  19455. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  19456. (defun org-kill-line (&optional arg)
  19457. "Kill line, to tags or end of line."
  19458. (interactive "P")
  19459. (cond
  19460. ((or (not org-special-ctrl-k)
  19461. (bolp)
  19462. (not (org-at-heading-p)))
  19463. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  19464. org-ctrl-k-protect-subtree)
  19465. (if (or (eq org-ctrl-k-protect-subtree 'error)
  19466. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  19467. (error "C-k aborted - would kill hidden subtree")))
  19468. (call-interactively
  19469. (if (and (boundp 'visual-line-mode) visual-line-mode) 'kill-visual-line 'kill-line)))
  19470. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  19471. (kill-region (point) (match-beginning 1))
  19472. (org-set-tags nil t))
  19473. (t (kill-region (point) (point-at-eol)))))
  19474. (define-key org-mode-map "\C-k" 'org-kill-line)
  19475. (defun org-yank (&optional arg)
  19476. "Yank. If the kill is a subtree, treat it specially.
  19477. This command will look at the current kill and check if is a single
  19478. subtree, or a series of subtrees[1]. If it passes the test, and if the
  19479. cursor is at the beginning of a line or after the stars of a currently
  19480. empty headline, then the yank is handled specially. How exactly depends
  19481. on the value of the following variables, both set by default.
  19482. org-yank-folded-subtrees
  19483. When set, the subtree(s) will be folded after insertion, but only
  19484. if doing so would now swallow text after the yanked text.
  19485. org-yank-adjusted-subtrees
  19486. When set, the subtree will be promoted or demoted in order to
  19487. fit into the local outline tree structure, which means that the level
  19488. will be adjusted so that it becomes the smaller one of the two
  19489. *visible* surrounding headings.
  19490. Any prefix to this command will cause `yank' to be called directly with
  19491. no special treatment. In particular, a simple \\[universal-argument] prefix \
  19492. will just
  19493. plainly yank the text as it is.
  19494. \[1] The test checks if the first non-white line is a heading
  19495. and if there are no other headings with fewer stars."
  19496. (interactive "P")
  19497. (org-yank-generic 'yank arg))
  19498. (defun org-yank-generic (command arg)
  19499. "Perform some yank-like command.
  19500. This function implements the behavior described in the `org-yank'
  19501. documentation. However, it has been generalized to work for any
  19502. interactive command with similar behavior."
  19503. ;; pretend to be command COMMAND
  19504. (setq this-command command)
  19505. (if arg
  19506. (call-interactively command)
  19507. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  19508. (and (org-kill-is-subtree-p)
  19509. (or (bolp)
  19510. (and (looking-at "[ \t]*$")
  19511. (string-match
  19512. "\\`\\*+\\'"
  19513. (buffer-substring (point-at-bol) (point)))))))
  19514. swallowp)
  19515. (cond
  19516. ((and subtreep org-yank-folded-subtrees)
  19517. (let ((beg (point))
  19518. end)
  19519. (if (and subtreep org-yank-adjusted-subtrees)
  19520. (org-paste-subtree nil nil 'for-yank)
  19521. (call-interactively command))
  19522. (setq end (point))
  19523. (goto-char beg)
  19524. (when (and (bolp) subtreep
  19525. (not (setq swallowp
  19526. (org-yank-folding-would-swallow-text beg end))))
  19527. (org-with-limited-levels
  19528. (or (looking-at org-outline-regexp)
  19529. (re-search-forward org-outline-regexp-bol end t))
  19530. (while (and (< (point) end) (looking-at org-outline-regexp))
  19531. (hide-subtree)
  19532. (org-cycle-show-empty-lines 'folded)
  19533. (condition-case nil
  19534. (outline-forward-same-level 1)
  19535. (error (goto-char end))))))
  19536. (when swallowp
  19537. (message
  19538. "Inserted text not folded because that would swallow text"))
  19539. (goto-char end)
  19540. (skip-chars-forward " \t\n\r")
  19541. (beginning-of-line 1)
  19542. (push-mark beg 'nomsg)))
  19543. ((and subtreep org-yank-adjusted-subtrees)
  19544. (let ((beg (point-at-bol)))
  19545. (org-paste-subtree nil nil 'for-yank)
  19546. (push-mark beg 'nomsg)))
  19547. (t
  19548. (call-interactively command))))))
  19549. (defun org-yank-folding-would-swallow-text (beg end)
  19550. "Would hide-subtree at BEG swallow any text after END?"
  19551. (let (level)
  19552. (org-with-limited-levels
  19553. (save-excursion
  19554. (goto-char beg)
  19555. (when (or (looking-at org-outline-regexp)
  19556. (re-search-forward org-outline-regexp-bol end t))
  19557. (setq level (org-outline-level)))
  19558. (goto-char end)
  19559. (skip-chars-forward " \t\r\n\v\f")
  19560. (if (or (eobp)
  19561. (and (bolp) (looking-at org-outline-regexp)
  19562. (<= (org-outline-level) level)))
  19563. nil ; Nothing would be swallowed
  19564. t))))) ; something would swallow
  19565. (define-key org-mode-map "\C-y" 'org-yank)
  19566. (defun org-truely-invisible-p ()
  19567. "Check if point is at a character currently not visible.
  19568. This version does not only check the character property, but also
  19569. `visible-mode'."
  19570. ;; Early versions of noutline don't have `outline-invisible-p'.
  19571. (if (org-bound-and-true-p visible-mode)
  19572. nil
  19573. (outline-invisible-p)))
  19574. (defun org-invisible-p2 ()
  19575. "Check if point is at a character currently not visible."
  19576. (save-excursion
  19577. (if (and (eolp) (not (bobp))) (backward-char 1))
  19578. ;; Early versions of noutline don't have `outline-invisible-p'.
  19579. (outline-invisible-p)))
  19580. (defun org-back-to-heading (&optional invisible-ok)
  19581. "Call `outline-back-to-heading', but provide a better error message."
  19582. (condition-case nil
  19583. (outline-back-to-heading invisible-ok)
  19584. (error (error "Before first headline at position %d in buffer %s"
  19585. (point) (current-buffer)))))
  19586. (defun org-beginning-of-defun ()
  19587. "Go to the beginning of the subtree, i.e. back to the heading."
  19588. (org-back-to-heading))
  19589. (defun org-end-of-defun ()
  19590. "Go to the end of the subtree."
  19591. (org-end-of-subtree nil t))
  19592. (defun org-before-first-heading-p ()
  19593. "Before first heading?"
  19594. (save-excursion
  19595. (end-of-line)
  19596. (null (re-search-backward org-outline-regexp-bol nil t))))
  19597. (defun org-at-heading-p (&optional ignored)
  19598. (outline-on-heading-p t))
  19599. ;; Compatibility alias with Org versions < 7.8.03
  19600. (defalias 'org-on-heading-p 'org-at-heading-p)
  19601. (defun org-at-comment-p nil
  19602. "Is cursor in a line starting with a # character?"
  19603. (save-excursion
  19604. (beginning-of-line)
  19605. (looking-at "^#")))
  19606. (defun org-at-drawer-p nil
  19607. "Is cursor at a drawer keyword?"
  19608. (save-excursion
  19609. (move-beginning-of-line 1)
  19610. (looking-at org-drawer-regexp)))
  19611. (defun org-at-block-p nil
  19612. "Is cursor at a block keyword?"
  19613. (save-excursion
  19614. (move-beginning-of-line 1)
  19615. (looking-at org-block-regexp)))
  19616. (defun org-point-at-end-of-empty-headline ()
  19617. "If point is at the end of an empty headline, return t, else nil.
  19618. If the heading only contains a TODO keyword, it is still still considered
  19619. empty."
  19620. (and (looking-at "[ \t]*$")
  19621. (when org-todo-line-regexp
  19622. (save-excursion
  19623. (beginning-of-line 1)
  19624. (let ((case-fold-search nil))
  19625. (looking-at org-todo-line-regexp)
  19626. (string= (match-string 3) ""))))))
  19627. (defun org-at-heading-or-item-p ()
  19628. (or (org-at-heading-p) (org-at-item-p)))
  19629. (defun org-at-target-p ()
  19630. (or (org-in-regexp org-radio-target-regexp)
  19631. (org-in-regexp org-target-regexp)))
  19632. ;; Compatibility alias with Org versions < 7.8.03
  19633. (defalias 'org-on-target-p 'org-at-target-p)
  19634. (defun org-up-heading-all (arg)
  19635. "Move to the heading line of which the present line is a subheading.
  19636. This function considers both visible and invisible heading lines.
  19637. With argument, move up ARG levels."
  19638. (if (fboundp 'outline-up-heading-all)
  19639. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  19640. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  19641. (defun org-up-heading-safe ()
  19642. "Move to the heading line of which the present line is a subheading.
  19643. This version will not throw an error. It will return the level of the
  19644. headline found, or nil if no higher level is found.
  19645. Also, this function will be a lot faster than `outline-up-heading',
  19646. because it relies on stars being the outline starters. This can really
  19647. make a significant difference in outlines with very many siblings."
  19648. (let (start-level re)
  19649. (org-back-to-heading t)
  19650. (setq start-level (funcall outline-level))
  19651. (if (equal start-level 1)
  19652. nil
  19653. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  19654. (if (re-search-backward re nil t)
  19655. (funcall outline-level)))))
  19656. (defun org-first-sibling-p ()
  19657. "Is this heading the first child of its parents?"
  19658. (interactive)
  19659. (let ((re org-outline-regexp-bol)
  19660. level l)
  19661. (unless (org-at-heading-p t)
  19662. (error "Not at a heading"))
  19663. (setq level (funcall outline-level))
  19664. (save-excursion
  19665. (if (not (re-search-backward re nil t))
  19666. t
  19667. (setq l (funcall outline-level))
  19668. (< l level)))))
  19669. (defun org-goto-sibling (&optional previous)
  19670. "Goto the next sibling, even if it is invisible.
  19671. When PREVIOUS is set, go to the previous sibling instead. Returns t
  19672. when a sibling was found. When none is found, return nil and don't
  19673. move point."
  19674. (let ((fun (if previous 're-search-backward 're-search-forward))
  19675. (pos (point))
  19676. (re org-outline-regexp-bol)
  19677. level l)
  19678. (when (condition-case nil (org-back-to-heading t) (error nil))
  19679. (setq level (funcall outline-level))
  19680. (catch 'exit
  19681. (or previous (forward-char 1))
  19682. (while (funcall fun re nil t)
  19683. (setq l (funcall outline-level))
  19684. (when (< l level) (goto-char pos) (throw 'exit nil))
  19685. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  19686. (goto-char pos)
  19687. nil))))
  19688. (defun org-show-siblings ()
  19689. "Show all siblings of the current headline."
  19690. (save-excursion
  19691. (while (org-goto-sibling) (org-flag-heading nil)))
  19692. (save-excursion
  19693. (while (org-goto-sibling 'previous)
  19694. (org-flag-heading nil))))
  19695. (defun org-goto-first-child ()
  19696. "Goto the first child, even if it is invisible.
  19697. Return t when a child was found. Otherwise don't move point and
  19698. return nil."
  19699. (let (level (pos (point)) (re org-outline-regexp-bol))
  19700. (when (condition-case nil (org-back-to-heading t) (error nil))
  19701. (setq level (outline-level))
  19702. (forward-char 1)
  19703. (if (and (re-search-forward re nil t) (> (outline-level) level))
  19704. (progn (goto-char (match-beginning 0)) t)
  19705. (goto-char pos) nil))))
  19706. (defun org-show-hidden-entry ()
  19707. "Show an entry where even the heading is hidden."
  19708. (save-excursion
  19709. (org-show-entry)))
  19710. (defun org-flag-heading (flag &optional entry)
  19711. "Flag the current heading. FLAG non-nil means make invisible.
  19712. When ENTRY is non-nil, show the entire entry."
  19713. (save-excursion
  19714. (org-back-to-heading t)
  19715. ;; Check if we should show the entire entry
  19716. (if entry
  19717. (progn
  19718. (org-show-entry)
  19719. (save-excursion
  19720. (and (outline-next-heading)
  19721. (org-flag-heading nil))))
  19722. (outline-flag-region (max (point-min) (1- (point)))
  19723. (save-excursion (outline-end-of-heading) (point))
  19724. flag))))
  19725. (defun org-get-next-sibling ()
  19726. "Move to next heading of the same level, and return point.
  19727. If there is no such heading, return nil.
  19728. This is like outline-next-sibling, but invisible headings are ok."
  19729. (let ((level (funcall outline-level)))
  19730. (outline-next-heading)
  19731. (while (and (not (eobp)) (> (funcall outline-level) level))
  19732. (outline-next-heading))
  19733. (if (or (eobp) (< (funcall outline-level) level))
  19734. nil
  19735. (point))))
  19736. (defun org-get-last-sibling ()
  19737. "Move to previous heading of the same level, and return point.
  19738. If there is no such heading, return nil."
  19739. (let ((opoint (point))
  19740. (level (funcall outline-level)))
  19741. (outline-previous-heading)
  19742. (when (and (/= (point) opoint) (outline-on-heading-p t))
  19743. (while (and (> (funcall outline-level) level)
  19744. (not (bobp)))
  19745. (outline-previous-heading))
  19746. (if (< (funcall outline-level) level)
  19747. nil
  19748. (point)))))
  19749. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  19750. ;; This contains an exact copy of the original function, but it uses
  19751. ;; `org-back-to-heading', to make it work also in invisible
  19752. ;; trees. And is uses an invisible-OK argument.
  19753. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  19754. ;; Furthermore, when used inside Org, finding the end of a large subtree
  19755. ;; with many children and grandchildren etc, this can be much faster
  19756. ;; than the outline version.
  19757. (org-back-to-heading invisible-OK)
  19758. (let ((first t)
  19759. (level (funcall outline-level)))
  19760. (if (and (derived-mode-p 'org-mode) (< level 1000))
  19761. ;; A true heading (not a plain list item), in Org-mode
  19762. ;; This means we can easily find the end by looking
  19763. ;; only for the right number of stars. Using a regexp to do
  19764. ;; this is so much faster than using a Lisp loop.
  19765. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  19766. (forward-char 1)
  19767. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  19768. ;; something else, do it the slow way
  19769. (while (and (not (eobp))
  19770. (or first (> (funcall outline-level) level)))
  19771. (setq first nil)
  19772. (outline-next-heading)))
  19773. (unless to-heading
  19774. (if (memq (preceding-char) '(?\n ?\^M))
  19775. (progn
  19776. ;; Go to end of line before heading
  19777. (forward-char -1)
  19778. (if (memq (preceding-char) '(?\n ?\^M))
  19779. ;; leave blank line before heading
  19780. (forward-char -1))))))
  19781. (point))
  19782. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  19783. "Use Org version in org-mode, for dramatic speed-up."
  19784. (if (derived-mode-p 'org-mode)
  19785. (progn
  19786. (org-end-of-subtree nil t)
  19787. (unless (eobp) (backward-char 1)))
  19788. ad-do-it))
  19789. (defun org-end-of-meta-data-and-drawers ()
  19790. "Jump to the first text after meta data and drawers in the current entry.
  19791. This will move over empty lines, lines with planning time stamps,
  19792. clocking lines, and drawers."
  19793. (org-back-to-heading t)
  19794. (let ((end (save-excursion (outline-next-heading) (point)))
  19795. (re (concat "\\(" org-drawer-regexp "\\)"
  19796. "\\|" "[ \t]*" org-keyword-time-regexp)))
  19797. (forward-line 1)
  19798. (while (re-search-forward re end t)
  19799. (if (not (match-end 1))
  19800. ;; empty or planning line
  19801. (forward-line 1)
  19802. ;; a drawer, find the end
  19803. (re-search-forward "^[ \t]*:END:" end 'move)
  19804. (forward-line 1)))
  19805. (and (re-search-forward "[^\n]" nil t) (backward-char 1))
  19806. (point)))
  19807. (defun org-forward-same-level (arg &optional invisible-ok)
  19808. "Move forward to the arg'th subheading at same level as this one.
  19809. Stop at the first and last subheadings of a superior heading.
  19810. Normally this only looks at visible headings, but when INVISIBLE-OK is non-nil
  19811. it wil also look at invisible ones."
  19812. (interactive "p")
  19813. (org-back-to-heading invisible-ok)
  19814. (org-at-heading-p)
  19815. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  19816. (re (format "^\\*\\{1,%d\\} " level))
  19817. l)
  19818. (forward-char 1)
  19819. (while (> arg 0)
  19820. (while (and (re-search-forward re nil 'move)
  19821. (setq l (- (match-end 0) (match-beginning 0) 1))
  19822. (= l level)
  19823. (not invisible-ok)
  19824. (progn (backward-char 1) (outline-invisible-p)))
  19825. (if (< l level) (setq arg 1)))
  19826. (setq arg (1- arg)))
  19827. (beginning-of-line 1)))
  19828. (defun org-backward-same-level (arg &optional invisible-ok)
  19829. "Move backward to the arg'th subheading at same level as this one.
  19830. Stop at the first and last subheadings of a superior heading."
  19831. (interactive "p")
  19832. (org-back-to-heading)
  19833. (org-at-heading-p)
  19834. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  19835. (re (format "^\\*\\{1,%d\\} " level))
  19836. l)
  19837. (while (> arg 0)
  19838. (while (and (re-search-backward re nil 'move)
  19839. (setq l (- (match-end 0) (match-beginning 0) 1))
  19840. (= l level)
  19841. (not invisible-ok)
  19842. (outline-invisible-p))
  19843. (if (< l level) (setq arg 1)))
  19844. (setq arg (1- arg)))))
  19845. (defun org-show-subtree ()
  19846. "Show everything after this heading at deeper levels."
  19847. (interactive)
  19848. (outline-flag-region
  19849. (point)
  19850. (save-excursion
  19851. (org-end-of-subtree t t))
  19852. nil))
  19853. (defun org-show-entry ()
  19854. "Show the body directly following this heading.
  19855. Show the heading too, if it is currently invisible."
  19856. (interactive)
  19857. (save-excursion
  19858. (condition-case nil
  19859. (progn
  19860. (org-back-to-heading t)
  19861. (outline-flag-region
  19862. (max (point-min) (1- (point)))
  19863. (save-excursion
  19864. (if (re-search-forward
  19865. (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
  19866. (match-beginning 1)
  19867. (point-max)))
  19868. nil)
  19869. (org-cycle-hide-drawers 'children))
  19870. (error nil))))
  19871. (defun org-make-options-regexp (kwds &optional extra)
  19872. "Make a regular expression for keyword lines."
  19873. (concat
  19874. "^"
  19875. "#?[ \t]*\\+\\("
  19876. (mapconcat 'regexp-quote kwds "\\|")
  19877. (if extra (concat "\\|" extra))
  19878. "\\):[ \t]*"
  19879. "\\(.*\\)"))
  19880. ;; Make isearch reveal the necessary context
  19881. (defun org-isearch-end ()
  19882. "Reveal context after isearch exits."
  19883. (when isearch-success ; only if search was successful
  19884. (if (featurep 'xemacs)
  19885. ;; Under XEmacs, the hook is run in the correct place,
  19886. ;; we directly show the context.
  19887. (org-show-context 'isearch)
  19888. ;; In Emacs the hook runs *before* restoring the overlays.
  19889. ;; So we have to use a one-time post-command-hook to do this.
  19890. ;; (Emacs 22 has a special variable, see function `org-mode')
  19891. (unless (and (boundp 'isearch-mode-end-hook-quit)
  19892. isearch-mode-end-hook-quit)
  19893. ;; Only when the isearch was not quitted.
  19894. (org-add-hook 'post-command-hook 'org-isearch-post-command
  19895. 'append 'local)))))
  19896. (defun org-isearch-post-command ()
  19897. "Remove self from hook, and show context."
  19898. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  19899. (org-show-context 'isearch))
  19900. ;;;; Integration with and fixes for other packages
  19901. ;;; Imenu support
  19902. (defvar org-imenu-markers nil
  19903. "All markers currently used by Imenu.")
  19904. (make-variable-buffer-local 'org-imenu-markers)
  19905. (defun org-imenu-new-marker (&optional pos)
  19906. "Return a new marker for use by Imenu, and remember the marker."
  19907. (let ((m (make-marker)))
  19908. (move-marker m (or pos (point)))
  19909. (push m org-imenu-markers)
  19910. m))
  19911. (defun org-imenu-get-tree ()
  19912. "Produce the index for Imenu."
  19913. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  19914. (setq org-imenu-markers nil)
  19915. (let* ((n org-imenu-depth)
  19916. (re (concat "^" (org-get-limited-outline-regexp)))
  19917. (subs (make-vector (1+ n) nil))
  19918. (last-level 0)
  19919. m level head)
  19920. (save-excursion
  19921. (save-restriction
  19922. (widen)
  19923. (goto-char (point-max))
  19924. (while (re-search-backward re nil t)
  19925. (setq level (org-reduced-level (funcall outline-level)))
  19926. (when (and (<= level n)
  19927. (looking-at org-complex-heading-regexp))
  19928. (setq head (org-link-display-format
  19929. (org-match-string-no-properties 4))
  19930. m (org-imenu-new-marker))
  19931. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  19932. (if (>= level last-level)
  19933. (push (cons head m) (aref subs level))
  19934. (push (cons head (aref subs (1+ level))) (aref subs level))
  19935. (loop for i from (1+ level) to n do (aset subs i nil)))
  19936. (setq last-level level)))))
  19937. (aref subs 1)))
  19938. (eval-after-load "imenu"
  19939. '(progn
  19940. (add-hook 'imenu-after-jump-hook
  19941. (lambda ()
  19942. (if (derived-mode-p 'org-mode)
  19943. (org-show-context 'org-goto))))))
  19944. (defun org-link-display-format (link)
  19945. "Replace a link with either the description, or the link target
  19946. if no description is present"
  19947. (save-match-data
  19948. (if (string-match org-bracket-link-analytic-regexp link)
  19949. (replace-match (if (match-end 5)
  19950. (match-string 5 link)
  19951. (concat (match-string 1 link)
  19952. (match-string 3 link)))
  19953. nil t link)
  19954. link)))
  19955. (defun org-toggle-link-display ()
  19956. "Toggle the literal or descriptive display of links."
  19957. (interactive)
  19958. (if org-descriptive-links
  19959. (progn (org-remove-from-invisibility-spec '(org-link))
  19960. (org-restart-font-lock)
  19961. (setq org-descriptive-links nil))
  19962. (progn (add-to-invisibility-spec '(org-link))
  19963. (org-restart-font-lock)
  19964. (setq org-descriptive-links t))))
  19965. ;; Speedbar support
  19966. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  19967. "Overlay marking the agenda restriction line in speedbar.")
  19968. (overlay-put org-speedbar-restriction-lock-overlay
  19969. 'face 'org-agenda-restriction-lock)
  19970. (overlay-put org-speedbar-restriction-lock-overlay
  19971. 'help-echo "Agendas are currently limited to this item.")
  19972. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  19973. (defun org-speedbar-set-agenda-restriction ()
  19974. "Restrict future agenda commands to the location at point in speedbar.
  19975. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  19976. (interactive)
  19977. (require 'org-agenda)
  19978. (let (p m tp np dir txt)
  19979. (cond
  19980. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  19981. 'org-imenu t))
  19982. (setq m (get-text-property p 'org-imenu-marker))
  19983. (with-current-buffer (marker-buffer m)
  19984. (goto-char m)
  19985. (org-agenda-set-restriction-lock 'subtree)))
  19986. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  19987. 'speedbar-function 'speedbar-find-file))
  19988. (setq tp (previous-single-property-change
  19989. (1+ p) 'speedbar-function)
  19990. np (next-single-property-change
  19991. tp 'speedbar-function)
  19992. dir (speedbar-line-directory)
  19993. txt (buffer-substring-no-properties (or tp (point-min))
  19994. (or np (point-max))))
  19995. (with-current-buffer (find-file-noselect
  19996. (let ((default-directory dir))
  19997. (expand-file-name txt)))
  19998. (unless (derived-mode-p 'org-mode)
  19999. (error "Cannot restrict to non-Org-mode file"))
  20000. (org-agenda-set-restriction-lock 'file)))
  20001. (t (error "Don't know how to restrict Org-mode's agenda")))
  20002. (move-overlay org-speedbar-restriction-lock-overlay
  20003. (point-at-bol) (point-at-eol))
  20004. (setq current-prefix-arg nil)
  20005. (org-agenda-maybe-redo)))
  20006. (eval-after-load "speedbar"
  20007. '(progn
  20008. (speedbar-add-supported-extension ".org")
  20009. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  20010. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  20011. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  20012. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  20013. (add-hook 'speedbar-visiting-tag-hook
  20014. (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
  20015. ;;; Fixes and Hacks for problems with other packages
  20016. ;; Make flyspell not check words in links, to not mess up our keymap
  20017. (defun org-mode-flyspell-verify ()
  20018. "Don't let flyspell put overlays at active buttons, or on
  20019. {todo,all-time,additional-option-like}-keywords."
  20020. (let ((pos (max (1- (point)) (point-min)))
  20021. (word (thing-at-point 'word)))
  20022. (and (not (get-text-property pos 'keymap))
  20023. (not (get-text-property pos 'org-no-flyspell))
  20024. (not (member word org-todo-keywords-1))
  20025. (not (member word org-all-time-keywords))
  20026. (not (member word org-additional-option-like-keywords)))))
  20027. (defun org-remove-flyspell-overlays-in (beg end)
  20028. "Remove flyspell overlays in region."
  20029. (and (org-bound-and-true-p flyspell-mode)
  20030. (fboundp 'flyspell-delete-region-overlays)
  20031. (flyspell-delete-region-overlays beg end))
  20032. (add-text-properties beg end '(org-no-flyspell t)))
  20033. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  20034. (eval-after-load "bookmark"
  20035. '(if (boundp 'bookmark-after-jump-hook)
  20036. ;; We can use the hook
  20037. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  20038. ;; Hook not available, use advice
  20039. (defadvice bookmark-jump (after org-make-visible activate)
  20040. "Make the position visible."
  20041. (org-bookmark-jump-unhide))))
  20042. ;; Make sure saveplace shows the location if it was hidden
  20043. (eval-after-load "saveplace"
  20044. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  20045. "Make the position visible."
  20046. (org-bookmark-jump-unhide)))
  20047. ;; Make sure ecb shows the location if it was hidden
  20048. (eval-after-load "ecb"
  20049. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  20050. "Make hierarchy visible when jumping into location from ECB tree buffer."
  20051. (if (derived-mode-p 'org-mode)
  20052. (org-show-context))))
  20053. (defun org-bookmark-jump-unhide ()
  20054. "Unhide the current position, to show the bookmark location."
  20055. (and (derived-mode-p 'org-mode)
  20056. (or (outline-invisible-p)
  20057. (save-excursion (goto-char (max (point-min) (1- (point))))
  20058. (outline-invisible-p)))
  20059. (org-show-context 'bookmark-jump)))
  20060. ;; Make session.el ignore our circular variable
  20061. (eval-after-load "session"
  20062. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  20063. ;;;; Experimental code
  20064. (defun org-closed-in-range ()
  20065. "Sparse tree of items closed in a certain time range.
  20066. Still experimental, may disappear in the future."
  20067. (interactive)
  20068. ;; Get the time interval from the user.
  20069. (let* ((time1 (org-float-time
  20070. (org-read-date nil 'to-time nil "Starting date: ")))
  20071. (time2 (org-float-time
  20072. (org-read-date nil 'to-time nil "End date:")))
  20073. ;; callback function
  20074. (callback (lambda ()
  20075. (let ((time
  20076. (org-float-time
  20077. (apply 'encode-time
  20078. (org-parse-time-string
  20079. (match-string 1))))))
  20080. ;; check if time in interval
  20081. (and (>= time time1) (<= time time2))))))
  20082. ;; make tree, check each match with the callback
  20083. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  20084. ;;;; Finish up
  20085. (provide 'org)
  20086. (run-hooks 'org-load-hook)
  20087. ;;; org.el ends here