org.el 881 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287
  1. ;;; org.el --- Outline-based notes management and organizer
  2. ;; Carstens outline-mode for keeping track of everything.
  3. ;; Copyright (C) 2004-2013 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. (load "org-loaddefs.el" t t t)
  74. (require 'org-macs)
  75. (require 'org-compat)
  76. ;; `org-outline-regexp' ought to be a defconst but is let-binding in
  77. ;; some places -- e.g. see the macro `org-with-limited-levels'.
  78. ;;
  79. ;; In Org buffers, the value of `outline-regexp' is that of
  80. ;; `org-outline-regexp'. The only function still directly relying on
  81. ;; `outline-regexp' is `org-overview' so that `org-cycle' can do its
  82. ;; job when `orgstruct-mode' is active.
  83. (defvar org-outline-regexp "\\*+ "
  84. "Regexp to match Org headlines.")
  85. (defvar org-outline-regexp-bol "^\\*+ "
  86. "Regexp to match Org headlines.
  87. This is similar to `org-outline-regexp' but additionally makes
  88. sure that we are at the beginning of the line.")
  89. (defvar org-heading-regexp "^\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  90. "Matches a headline, putting stars and text into groups.
  91. Stars are put in group 1 and the trimmed body in group 2.")
  92. ;; Emacs 22 calendar compatibility: Make sure the new variables are available
  93. (when (fboundp 'defvaralias)
  94. (unless (boundp 'calendar-view-holidays-initially-flag)
  95. (defvaralias 'calendar-view-holidays-initially-flag
  96. 'view-calendar-holidays-initially))
  97. (unless (boundp 'calendar-view-diary-initially-flag)
  98. (defvaralias 'calendar-view-diary-initially-flag
  99. 'view-diary-entries-initially))
  100. (unless (boundp 'diary-fancy-buffer)
  101. (defvaralias 'diary-fancy-buffer 'fancy-diary-buffer)))
  102. (declare-function org-inlinetask-at-task-p "org-inlinetask" ())
  103. (declare-function org-inlinetask-outline-regexp "org-inlinetask" ())
  104. (declare-function org-inlinetask-toggle-visibility "org-inlinetask" ())
  105. (declare-function org-pop-to-buffer-same-window "org-compat" (&optional buffer-or-name norecord label))
  106. (declare-function org-clock-get-last-clock-out-time "org-clock" ())
  107. (declare-function org-clock-timestamps-up "org-clock" (&optional n))
  108. (declare-function org-clock-timestamps-down "org-clock" (&optional n))
  109. (declare-function org-clock-sum-current-item "org-clock" (&optional tstart))
  110. (declare-function orgtbl-mode "org-table" (&optional arg))
  111. (declare-function org-clock-out "org-clock" (&optional switch-to-state fail-quietly at-time))
  112. (declare-function org-beamer-mode "ox-beamer" ())
  113. (declare-function org-table-edit-field "org-table" (arg))
  114. (declare-function org-table-justify-field-maybe "org-table" (&optional new))
  115. (declare-function org-id-get-create "org-id" (&optional force))
  116. (declare-function org-id-find-id-file "org-id" (id))
  117. (declare-function org-tags-view "org-agenda" (&optional todo-only match))
  118. (declare-function org-agenda-list "org-agenda" (&optional arg start-day span))
  119. (declare-function org-table-align "org-table" ())
  120. (declare-function org-table-paste-rectangle "org-table" ())
  121. (declare-function org-table-maybe-eval-formula "org-table" ())
  122. (declare-function org-table-maybe-recalculate-line "org-table" ())
  123. (declare-function org-element--parse-objects "org-element"
  124. (beg end acc restriction))
  125. (declare-function org-element-at-point "org-element" (&optional keep-trail))
  126. (declare-function org-element-contents "org-element" (element))
  127. (declare-function org-element-context "org-element" (&optional element))
  128. (declare-function org-element-interpret-data "org-element"
  129. (data &optional parent))
  130. (declare-function org-element-map "org-element"
  131. (data types fun &optional info first-match no-recursion))
  132. (declare-function org-element-nested-p "org-element" (elem-a elem-b))
  133. (declare-function org-element-parse-buffer "org-element"
  134. (&optional granularity visible-only))
  135. (declare-function org-element-property "org-element" (property element))
  136. (declare-function org-element-put-property "org-element"
  137. (element property value))
  138. (declare-function org-element-swap-A-B "org-element" (elem-a elem-b))
  139. (declare-function org-element--parse-objects "org-element"
  140. (beg end acc restriction))
  141. (declare-function org-element-parse-buffer "org-element"
  142. (&optional granularity visible-only))
  143. (declare-function org-element-restriction "org-element" (element))
  144. (declare-function org-element-type "org-element" (element))
  145. ;; load languages based on value of `org-babel-load-languages'
  146. (defvar org-babel-load-languages)
  147. ;;;###autoload
  148. (defun org-babel-do-load-languages (sym value)
  149. "Load the languages defined in `org-babel-load-languages'."
  150. (set-default sym value)
  151. (mapc (lambda (pair)
  152. (let ((active (cdr pair)) (lang (symbol-name (car pair))))
  153. (if active
  154. (progn
  155. (require (intern (concat "ob-" lang))))
  156. (progn
  157. (funcall 'fmakunbound
  158. (intern (concat "org-babel-execute:" lang)))
  159. (funcall 'fmakunbound
  160. (intern (concat "org-babel-expand-body:" lang)))))))
  161. org-babel-load-languages))
  162. (defcustom org-babel-load-languages '((emacs-lisp . t))
  163. "Languages which can be evaluated in Org-mode buffers.
  164. This list can be used to load support for any of the languages
  165. below, note that each language will depend on a different set of
  166. system executables and/or Emacs modes. When a language is
  167. \"loaded\", then code blocks in that language can be evaluated
  168. with `org-babel-execute-src-block' bound by default to C-c
  169. C-c (note the `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can
  170. be set to remove code block evaluation from the C-c C-c
  171. keybinding. By default only Emacs Lisp (which has no
  172. requirements) is loaded."
  173. :group 'org-babel
  174. :set 'org-babel-do-load-languages
  175. :version "24.1"
  176. :type '(alist :tag "Babel Languages"
  177. :key-type
  178. (choice
  179. (const :tag "Awk" awk)
  180. (const :tag "C" C)
  181. (const :tag "R" R)
  182. (const :tag "Asymptote" asymptote)
  183. (const :tag "Calc" calc)
  184. (const :tag "Clojure" clojure)
  185. (const :tag "CSS" css)
  186. (const :tag "Ditaa" ditaa)
  187. (const :tag "Dot" dot)
  188. (const :tag "Emacs Lisp" emacs-lisp)
  189. (const :tag "Fortran" fortran)
  190. (const :tag "Gnuplot" gnuplot)
  191. (const :tag "Haskell" haskell)
  192. (const :tag "IO" io)
  193. (const :tag "Java" java)
  194. (const :tag "Javascript" js)
  195. (const :tag "LaTeX" latex)
  196. (const :tag "Ledger" ledger)
  197. (const :tag "Lilypond" lilypond)
  198. (const :tag "Lisp" lisp)
  199. (const :tag "Makefile" makefile)
  200. (const :tag "Maxima" maxima)
  201. (const :tag "Matlab" matlab)
  202. (const :tag "Mscgen" mscgen)
  203. (const :tag "Ocaml" ocaml)
  204. (const :tag "Octave" octave)
  205. (const :tag "Org" org)
  206. (const :tag "Perl" perl)
  207. (const :tag "Pico Lisp" picolisp)
  208. (const :tag "PlantUML" plantuml)
  209. (const :tag "Python" python)
  210. (const :tag "Ruby" ruby)
  211. (const :tag "Sass" sass)
  212. (const :tag "Scala" scala)
  213. (const :tag "Scheme" scheme)
  214. (const :tag "Screen" screen)
  215. (const :tag "Shell Script" sh)
  216. (const :tag "Shen" shen)
  217. (const :tag "Sql" sql)
  218. (const :tag "Sqlite" sqlite))
  219. :value-type (boolean :tag "Activate" :value t)))
  220. ;;;; Customization variables
  221. (defcustom org-clone-delete-id nil
  222. "Remove ID property of clones of a subtree.
  223. When non-nil, clones of a subtree don't inherit the ID property.
  224. Otherwise they inherit the ID property with a new unique
  225. identifier."
  226. :type 'boolean
  227. :version "24.1"
  228. :group 'org-id)
  229. ;;; Version
  230. (org-check-version)
  231. ;;;###autoload
  232. (defun org-version (&optional here full message)
  233. "Show the org-mode version in the echo area.
  234. With prefix argument HERE, insert it at point.
  235. When FULL is non-nil, use a verbose version string.
  236. When MESSAGE is non-nil, display a message with the version."
  237. (interactive "P")
  238. (let* ((org-dir (ignore-errors (org-find-library-dir "org")))
  239. (save-load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  240. (load-suffixes (list ".el"))
  241. (org-install-dir (ignore-errors (org-find-library-dir "org-loaddefs")))
  242. (org-trash (or
  243. (and (fboundp 'org-release) (fboundp 'org-git-version))
  244. (org-load-noerror-mustsuffix (concat org-dir "org-version"))))
  245. (load-suffixes save-load-suffixes)
  246. (org-version (org-release))
  247. (git-version (org-git-version))
  248. (version (format "Org-mode version %s (%s @ %s)"
  249. org-version
  250. git-version
  251. (if org-install-dir
  252. (if (string= org-dir org-install-dir)
  253. org-install-dir
  254. (concat "mixed installation! " org-install-dir " and " org-dir))
  255. "org-loaddefs.el can not be found!")))
  256. (_version (if full version org-version)))
  257. (if (org-called-interactively-p 'interactive)
  258. (if here
  259. (insert version)
  260. (message version))
  261. (if message (message _version))
  262. _version)))
  263. (defconst org-version (org-version))
  264. ;;; Compatibility constants
  265. ;;; The custom variables
  266. (defgroup org nil
  267. "Outline-based notes management and organizer."
  268. :tag "Org"
  269. :group 'outlines
  270. :group 'calendar)
  271. (defcustom org-mode-hook nil
  272. "Mode hook for Org-mode, run after the mode was turned on."
  273. :group 'org
  274. :type 'hook)
  275. (defcustom org-load-hook nil
  276. "Hook that is run after org.el has been loaded."
  277. :group 'org
  278. :type 'hook)
  279. (defcustom org-log-buffer-setup-hook nil
  280. "Hook that is run after an Org log buffer is created."
  281. :group 'org
  282. :version "24.1"
  283. :type 'hook)
  284. (defvar org-modules) ; defined below
  285. (defvar org-modules-loaded nil
  286. "Have the modules been loaded already?")
  287. (defun org-load-modules-maybe (&optional force)
  288. "Load all extensions listed in `org-modules'."
  289. (when (or force (not org-modules-loaded))
  290. (mapc (lambda (ext)
  291. (condition-case nil (require ext)
  292. (error (message "Problems while trying to load feature `%s'" ext))))
  293. org-modules)
  294. (setq org-modules-loaded t)))
  295. (defun org-set-modules (var value)
  296. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  297. (set var value)
  298. (when (featurep 'org)
  299. (org-load-modules-maybe 'force)))
  300. (defcustom org-modules '(org-bbdb org-bibtex org-docview org-gnus org-info org-irc org-mhe org-rmail)
  301. "Modules that should always be loaded together with org.el.
  302. If a description starts with <C>, the file is not part of Emacs
  303. and loading it will require that you have downloaded and properly
  304. installed the Org mode distribution.
  305. You can also use this system to load external packages (i.e. neither Org
  306. core modules, nor modules from the CONTRIB directory). Just add symbols
  307. to the end of the list. If the package is called org-xyz.el, then you need
  308. to add the symbol `xyz', and the package must have a call to:
  309. \(provide 'org-xyz)
  310. For export specific modules, see also `org-export-backends'."
  311. :group 'org
  312. :set 'org-set-modules
  313. :version "24.4"
  314. :package-version '(Org . "8.0")
  315. :type
  316. '(set :greedy t
  317. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  318. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  319. (const :tag " crypt: Encryption of subtrees" org-crypt)
  320. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  321. (const :tag " docview: Links to doc-view buffers" org-docview)
  322. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  323. (const :tag " id: Global IDs for identifying entries" org-id)
  324. (const :tag " info: Links to Info nodes" org-info)
  325. (const :tag " habit: Track your consistency with habits" org-habit)
  326. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  327. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  328. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  329. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  330. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  331. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  332. (const :tag " mouse: Additional mouse support" org-mouse)
  333. (const :tag "C vm: Links to VM folders/messages" org-vm)
  334. (const :tag "C wl: Links to Wanderlust folders/messages" org-wl)
  335. (const :tag "C w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  336. (const :tag "C mew: Links to Mew folders/messages" org-mew)
  337. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  338. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  339. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  340. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  341. (const :tag "C collector: Collect properties into tables" org-collector)
  342. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  343. (const :tag "C drill: Flashcards and spaced repetition for Org-mode" org-drill)
  344. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  345. (const :tag "C eshell Support for links to working directories in eshell" org-eshell)
  346. (const :tag "C eval: Include command output as text" org-eval)
  347. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  348. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  349. (const :tag "C exp-bibtex: Export citations using BibTeX" org-exp-bibtex)
  350. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  351. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  352. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  353. (const :tag "C jira: Add a jira:ticket protocol to Org-mode" org-jira)
  354. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  355. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  356. (const :tag "C notmuch: Provide org links to notmuch searches or messages" org-notmuch)
  357. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  358. (const :tag "C mac-link-grabber Grab links and URLs from various Mac applications" org-mac-link-grabber)
  359. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  360. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  361. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  362. (const :tag "C registry: A registry for Org-mode links" org-registry)
  363. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  364. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  365. (const :tag "C secretary: Team management with org-mode" org-secretary)
  366. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  367. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  368. (const :tag "C track: Keep up with Org-mode development" org-track)
  369. (const :tag "C velocity Something like Notational Velocity for Org" org-velocity)
  370. (const :tag "C wikinodes: CamelCase wiki-like links" org-wikinodes)
  371. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  372. (defvar org-export-registered-backends) ; From ox.el
  373. (declare-function org-export-derived-backend-p "ox" (backend &rest backends))
  374. (defcustom org-export-backends '(ascii html icalendar latex)
  375. "List of export back-ends that should be always available.
  376. If a description starts with <C>, the file is not part of Emacs
  377. and loading it will require that you have downloaded and properly
  378. installed the Org mode distribution.
  379. Unlike to `org-modules', libraries in this list will not be
  380. loaded along with Org, but only once the export framework is
  381. needed.
  382. This variable needs to be set before org.el is loaded. If you
  383. need to make a change while Emacs is running, use the customize
  384. interface or run the following code, where VALUE stands for the
  385. new value of the variable, after updating it:
  386. \(progn
  387. \(setq org-export-registered-backends
  388. \(org-remove-if-not
  389. \(lambda (backend)
  390. \(or (memq backend val)
  391. \(catch 'parentp
  392. \(mapc
  393. \(lambda (b)
  394. \(and (org-export-derived-backend-p b (car backend))
  395. \(throw 'parentp t)))
  396. val)
  397. nil)))
  398. org-export-registered-backends))
  399. \(let ((new-list (mapcar 'car org-export-registered-backends)))
  400. \(dolist (backend val)
  401. \(cond
  402. \((not (load (format \"ox-%s\" backend) t t))
  403. \(message \"Problems while trying to load export back-end `%s'\"
  404. backend))
  405. \((not (memq backend new-list)) (push backend new-list))))
  406. \(set-default var new-list)))
  407. Adding a back-end to this list will also pull the back-end it
  408. depends on, if any."
  409. :group 'org
  410. :group 'org-export
  411. :version "24.4"
  412. :package-version '(Org . "8.0")
  413. :initialize 'custom-initialize-set
  414. :set (lambda (var val)
  415. (if (not (featurep 'ox)) (set-default var val)
  416. ;; Any back-end not required anymore (not present in VAL and not
  417. ;; a parent of any back-end in the new value) is removed from the
  418. ;; list of registered back-ends.
  419. (setq org-export-registered-backends
  420. (org-remove-if-not
  421. (lambda (backend)
  422. (or (memq backend val)
  423. (catch 'parentp
  424. (mapc
  425. (lambda (b)
  426. (and (org-export-derived-backend-p b (car backend))
  427. (throw 'parentp t)))
  428. val)
  429. nil)))
  430. org-export-registered-backends))
  431. ;; Now build NEW-LIST of both new back-ends and required
  432. ;; parents.
  433. (let ((new-list (mapcar 'car org-export-registered-backends)))
  434. (dolist (backend val)
  435. (cond
  436. ((not (load (format "ox-%s" backend) t t))
  437. (message "Problems while trying to load export back-end `%s'"
  438. backend))
  439. ((not (memq backend new-list)) (push backend new-list))))
  440. ;; Set VAR to that list with fixed dependencies.
  441. (set-default var new-list))))
  442. :type '(set :greedy t
  443. (const :tag " ascii Export buffer to ASCII format" ascii)
  444. (const :tag " beamer Export buffer to Beamer presentation" beamer)
  445. (const :tag " html Export buffer to HTML format" html)
  446. (const :tag " icalendar Export buffer to iCalendar format" icalendar)
  447. (const :tag " latex Export buffer to LaTeX format" latex)
  448. (const :tag " man Export buffer to MAN format" man)
  449. (const :tag " md Export buffer to Markdown format" md)
  450. (const :tag " odt Export buffer to ODT format" odt)
  451. (const :tag " texinfo Export buffer to Texinfo format" texinfo)
  452. (const :tag "C confluence Export buffer to Confluence Wiki format" confluence)
  453. (const :tag "C freemind Export buffer to Freemind mindmap format" freemind)
  454. (const :tag "C groff Export buffer to Groff format" groff)
  455. (const :tag "C koma-letter Export buffer to KOMA Scrlttrl2 format" koma-letter)
  456. (const :tag "C taskjuggler Export buffer to TaskJuggler format" taskjuggler)))
  457. (eval-after-load 'ox
  458. '(mapc
  459. (lambda (backend)
  460. (condition-case nil (require (intern (format "ox-%s" backend)))
  461. (error (message "Problems while trying to load export back-end `%s'"
  462. backend))))
  463. org-export-backends))
  464. (defcustom org-support-shift-select nil
  465. "Non-nil means make shift-cursor commands select text when possible.
  466. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
  467. start selecting a region, or enlarge regions started in this way.
  468. In Org-mode, in special contexts, these same keys are used for
  469. other purposes, important enough to compete with shift selection.
  470. Org tries to balance these needs by supporting `shift-select-mode'
  471. outside these special contexts, under control of this variable.
  472. The default of this variable is nil, to avoid confusing behavior. Shifted
  473. cursor keys will then execute Org commands in the following contexts:
  474. - on a headline, changing TODO state (left/right) and priority (up/down)
  475. - on a time stamp, changing the time
  476. - in a plain list item, changing the bullet type
  477. - in a property definition line, switching between allowed values
  478. - in the BEGIN line of a clock table (changing the time block).
  479. Outside these contexts, the commands will throw an error.
  480. When this variable is t and the cursor is not in a special
  481. context, Org-mode will support shift-selection for making and
  482. enlarging regions. To make this more effective, the bullet
  483. cycling will no longer happen anywhere in an item line, but only
  484. if the cursor is exactly on the bullet.
  485. If you set this variable to the symbol `always', then the keys
  486. will not be special in headlines, property lines, and item lines,
  487. to make shift selection work there as well. If this is what you
  488. want, you can use the following alternative commands: `C-c C-t'
  489. and `C-c ,' to change TODO state and priority, `C-u C-u C-c C-t'
  490. can be used to switch TODO sets, `C-c -' to cycle item bullet
  491. types, and properties can be edited by hand or in column view.
  492. However, when the cursor is on a timestamp, shift-cursor commands
  493. will still edit the time stamp - this is just too good to give up.
  494. XEmacs user should have this variable set to nil, because
  495. `shift-select-mode' is in Emacs 23 or later only."
  496. :group 'org
  497. :type '(choice
  498. (const :tag "Never" nil)
  499. (const :tag "When outside special context" t)
  500. (const :tag "Everywhere except timestamps" always)))
  501. (defcustom org-loop-over-headlines-in-active-region nil
  502. "Shall some commands act upon headlines in the active region?
  503. When set to `t', some commands will be performed in all headlines
  504. within the active region.
  505. When set to `start-level', some commands will be performed in all
  506. headlines within the active region, provided that these headlines
  507. are of the same level than the first one.
  508. When set to a string, those commands will be performed on the
  509. matching headlines within the active region. Such string must be
  510. a tags/property/todo match as it is used in the agenda tags view.
  511. The list of commands is: `org-schedule', `org-deadline',
  512. `org-todo', `org-archive-subtree', `org-archive-set-tag' and
  513. `org-archive-to-archive-sibling'. The archiving commands skip
  514. already archived entries."
  515. :type '(choice (const :tag "Don't loop" nil)
  516. (const :tag "All headlines in active region" t)
  517. (const :tag "In active region, headlines at the same level than the first one" 'start-level)
  518. (string :tag "Tags/Property/Todo matcher"))
  519. :version "24.1"
  520. :group 'org-todo
  521. :group 'org-archive)
  522. (defgroup org-startup nil
  523. "Options concerning startup of Org-mode."
  524. :tag "Org Startup"
  525. :group 'org)
  526. (defcustom org-startup-folded t
  527. "Non-nil means entering Org-mode will switch to OVERVIEW.
  528. This can also be configured on a per-file basis by adding one of
  529. the following lines anywhere in the buffer:
  530. #+STARTUP: fold (or `overview', this is equivalent)
  531. #+STARTUP: nofold (or `showall', this is equivalent)
  532. #+STARTUP: content
  533. #+STARTUP: showeverything
  534. By default, this option is ignored when Org opens agenda files
  535. for the first time. If you want the agenda to honor the startup
  536. option, set `org-agenda-inhibit-startup' to nil."
  537. :group 'org-startup
  538. :type '(choice
  539. (const :tag "nofold: show all" nil)
  540. (const :tag "fold: overview" t)
  541. (const :tag "content: all headlines" content)
  542. (const :tag "show everything, even drawers" showeverything)))
  543. (defcustom org-startup-truncated t
  544. "Non-nil means entering Org-mode will set `truncate-lines'.
  545. This is useful since some lines containing links can be very long and
  546. uninteresting. Also tables look terrible when wrapped."
  547. :group 'org-startup
  548. :type 'boolean)
  549. (defcustom org-startup-indented nil
  550. "Non-nil means turn on `org-indent-mode' on startup.
  551. This can also be configured on a per-file basis by adding one of
  552. the following lines anywhere in the buffer:
  553. #+STARTUP: indent
  554. #+STARTUP: noindent"
  555. :group 'org-structure
  556. :type '(choice
  557. (const :tag "Not" nil)
  558. (const :tag "Globally (slow on startup in large files)" t)))
  559. (defcustom org-use-sub-superscripts t
  560. "Non-nil means interpret \"_\" and \"^\" for display.
  561. When this option is turned on, you can use TeX-like syntax for sub- and
  562. superscripts. Several characters after \"_\" or \"^\" will be
  563. considered as a single item - so grouping with {} is normally not
  564. needed. For example, the following things will be parsed as single
  565. sub- or superscripts.
  566. 10^24 or 10^tau several digits will be considered 1 item.
  567. 10^-12 or 10^-tau a leading sign with digits or a word
  568. x^2-y^3 will be read as x^2 - y^3, because items are
  569. terminated by almost any nonword/nondigit char.
  570. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  571. Still, ambiguity is possible - so when in doubt use {} to enclose
  572. the sub/superscript. If you set this variable to the symbol
  573. `{}', the braces are *required* in order to trigger
  574. interpretations as sub/superscript. This can be helpful in
  575. documents that need \"_\" frequently in plain text."
  576. :group 'org-startup
  577. :version "24.1"
  578. :type '(choice
  579. (const :tag "Always interpret" t)
  580. (const :tag "Only with braces" {})
  581. (const :tag "Never interpret" nil)))
  582. (defcustom org-startup-with-beamer-mode nil
  583. "Non-nil means turn on `org-beamer-mode' on startup.
  584. This can also be configured on a per-file basis by adding one of
  585. the following lines anywhere in the buffer:
  586. #+STARTUP: beamer"
  587. :group 'org-startup
  588. :version "24.1"
  589. :type 'boolean)
  590. (defcustom org-startup-align-all-tables nil
  591. "Non-nil means align all tables when visiting a file.
  592. This is useful when the column width in tables is forced with <N> cookies
  593. in table fields. Such tables will look correct only after the first re-align.
  594. This can also be configured on a per-file basis by adding one of
  595. the following lines anywhere in the buffer:
  596. #+STARTUP: align
  597. #+STARTUP: noalign"
  598. :group 'org-startup
  599. :type 'boolean)
  600. (defcustom org-startup-with-inline-images nil
  601. "Non-nil means show inline images when loading a new Org file.
  602. This can also be configured on a per-file basis by adding one of
  603. the following lines anywhere in the buffer:
  604. #+STARTUP: inlineimages
  605. #+STARTUP: noinlineimages"
  606. :group 'org-startup
  607. :version "24.1"
  608. :type 'boolean)
  609. (defcustom org-startup-with-latex-preview nil
  610. "Non-nil means preview LaTeX fragments when loading a new Org file.
  611. This can also be configured on a per-file basis by adding one of
  612. the followinglines anywhere in the buffer:
  613. #+STARTUP: latexpreview
  614. #+STARTUP: nolatexpreview"
  615. :group 'org-startup
  616. :version "24.4"
  617. :package-version '(Org . "8.0")
  618. :type 'boolean)
  619. (defcustom org-insert-mode-line-in-empty-file nil
  620. "Non-nil means insert the first line setting Org-mode in empty files.
  621. When the function `org-mode' is called interactively in an empty file, this
  622. normally means that the file name does not automatically trigger Org-mode.
  623. To ensure that the file will always be in Org-mode in the future, a
  624. line enforcing Org-mode will be inserted into the buffer, if this option
  625. has been set."
  626. :group 'org-startup
  627. :type 'boolean)
  628. (defcustom org-replace-disputed-keys nil
  629. "Non-nil means use alternative key bindings for some keys.
  630. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  631. These keys are also used by other packages like shift-selection-mode'
  632. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  633. If you want to use Org-mode together with one of these other modes,
  634. or more generally if you would like to move some Org-mode commands to
  635. other keys, set this variable and configure the keys with the variable
  636. `org-disputed-keys'.
  637. This option is only relevant at load-time of Org-mode, and must be set
  638. *before* org.el is loaded. Changing it requires a restart of Emacs to
  639. become effective."
  640. :group 'org-startup
  641. :type 'boolean)
  642. (defcustom org-use-extra-keys nil
  643. "Non-nil means use extra key sequence definitions for certain commands.
  644. This happens automatically if you run XEmacs or if `window-system'
  645. is nil. This variable lets you do the same manually. You must
  646. set it before loading org.
  647. Example: on Carbon Emacs 22 running graphically, with an external
  648. keyboard on a Powerbook, the default way of setting M-left might
  649. not work for either Alt or ESC. Setting this variable will make
  650. it work for ESC."
  651. :group 'org-startup
  652. :type 'boolean)
  653. (if (fboundp 'defvaralias)
  654. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  655. (defcustom org-disputed-keys
  656. '(([(shift up)] . [(meta p)])
  657. ([(shift down)] . [(meta n)])
  658. ([(shift left)] . [(meta -)])
  659. ([(shift right)] . [(meta +)])
  660. ([(control shift right)] . [(meta shift +)])
  661. ([(control shift left)] . [(meta shift -)]))
  662. "Keys for which Org-mode and other modes compete.
  663. This is an alist, cars are the default keys, second element specifies
  664. the alternative to use when `org-replace-disputed-keys' is t.
  665. Keys can be specified in any syntax supported by `define-key'.
  666. The value of this option takes effect only at Org-mode's startup,
  667. therefore you'll have to restart Emacs to apply it after changing."
  668. :group 'org-startup
  669. :type 'alist)
  670. (defun org-key (key)
  671. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  672. Or return the original if not disputed.
  673. Also apply the translations defined in `org-xemacs-key-equivalents'."
  674. (when org-replace-disputed-keys
  675. (let* ((nkey (key-description key))
  676. (x (org-find-if (lambda (x)
  677. (equal (key-description (car x)) nkey))
  678. org-disputed-keys)))
  679. (setq key (if x (cdr x) key))))
  680. (when (featurep 'xemacs)
  681. (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
  682. key)
  683. (defun org-find-if (predicate seq)
  684. (catch 'exit
  685. (while seq
  686. (if (funcall predicate (car seq))
  687. (throw 'exit (car seq))
  688. (pop seq)))))
  689. (defun org-defkey (keymap key def)
  690. "Define a key, possibly translated, as returned by `org-key'."
  691. (define-key keymap (org-key key) def))
  692. (defcustom org-ellipsis nil
  693. "The ellipsis to use in the Org-mode outline.
  694. When nil, just use the standard three dots. When a string, use that instead,
  695. When a face, use the standard 3 dots, but with the specified face.
  696. The change affects only Org-mode (which will then use its own display table).
  697. Changing this requires executing `M-x org-mode' in a buffer to become
  698. effective."
  699. :group 'org-startup
  700. :type '(choice (const :tag "Default" nil)
  701. (face :tag "Face" :value org-warning)
  702. (string :tag "String" :value "...#")))
  703. (defvar org-display-table nil
  704. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  705. (defgroup org-keywords nil
  706. "Keywords in Org-mode."
  707. :tag "Org Keywords"
  708. :group 'org)
  709. (defcustom org-deadline-string "DEADLINE:"
  710. "String to mark deadline entries.
  711. A deadline is this string, followed by a time stamp. Should be a word,
  712. terminated by a colon. You can insert a schedule keyword and
  713. a timestamp with \\[org-deadline].
  714. Changes become only effective after restarting Emacs."
  715. :group 'org-keywords
  716. :type 'string)
  717. (defcustom org-scheduled-string "SCHEDULED:"
  718. "String to mark scheduled TODO entries.
  719. A schedule is this string, followed by a time stamp. Should be a word,
  720. terminated by a colon. You can insert a schedule keyword and
  721. a timestamp with \\[org-schedule].
  722. Changes become only effective after restarting Emacs."
  723. :group 'org-keywords
  724. :type 'string)
  725. (defcustom org-closed-string "CLOSED:"
  726. "String used as the prefix for timestamps logging closing a TODO entry."
  727. :group 'org-keywords
  728. :type 'string)
  729. (defcustom org-clock-string "CLOCK:"
  730. "String used as prefix for timestamps clocking work hours on an item."
  731. :group 'org-keywords
  732. :type 'string)
  733. (defconst org-planning-or-clock-line-re (concat "^[ \t]*\\("
  734. org-scheduled-string "\\|"
  735. org-deadline-string "\\|"
  736. org-closed-string "\\|"
  737. org-clock-string "\\)")
  738. "Matches a line with planning or clock info.")
  739. (defcustom org-comment-string "COMMENT"
  740. "Entries starting with this keyword will never be exported.
  741. An entry can be toggled between COMMENT and normal with
  742. \\[org-toggle-comment].
  743. Changes become only effective after restarting Emacs."
  744. :group 'org-keywords
  745. :type 'string)
  746. (defcustom org-quote-string "QUOTE"
  747. "Entries starting with this keyword will be exported in fixed-width font.
  748. Quoting applies only to the text in the entry following the headline, and does
  749. not extend beyond the next headline, even if that is lower level.
  750. An entry can be toggled between QUOTE and normal with
  751. \\[org-toggle-fixed-width-section]."
  752. :group 'org-keywords
  753. :type 'string)
  754. (defconst org-repeat-re
  755. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)"
  756. "Regular expression for specifying repeated events.
  757. After a match, group 1 contains the repeat expression.")
  758. (defgroup org-structure nil
  759. "Options concerning the general structure of Org-mode files."
  760. :tag "Org Structure"
  761. :group 'org)
  762. (defgroup org-reveal-location nil
  763. "Options about how to make context of a location visible."
  764. :tag "Org Reveal Location"
  765. :group 'org-structure)
  766. (defconst org-context-choice
  767. '(choice
  768. (const :tag "Always" t)
  769. (const :tag "Never" nil)
  770. (repeat :greedy t :tag "Individual contexts"
  771. (cons
  772. (choice :tag "Context"
  773. (const agenda)
  774. (const org-goto)
  775. (const occur-tree)
  776. (const tags-tree)
  777. (const link-search)
  778. (const mark-goto)
  779. (const bookmark-jump)
  780. (const isearch)
  781. (const default))
  782. (boolean))))
  783. "Contexts for the reveal options.")
  784. (defcustom org-show-hierarchy-above '((default . t))
  785. "Non-nil means show full hierarchy when revealing a location.
  786. Org-mode often shows locations in an org-mode file which might have
  787. been invisible before. When this is set, the hierarchy of headings
  788. above the exposed location is shown.
  789. Turning this off for example for sparse trees makes them very compact.
  790. Instead of t, this can also be an alist specifying this option for different
  791. contexts. Valid contexts are
  792. agenda when exposing an entry from the agenda
  793. org-goto when using the command `org-goto' on key C-c C-j
  794. occur-tree when using the command `org-occur' on key C-c /
  795. tags-tree when constructing a sparse tree based on tags matches
  796. link-search when exposing search matches associated with a link
  797. mark-goto when exposing the jump goal of a mark
  798. bookmark-jump when exposing a bookmark location
  799. isearch when exiting from an incremental search
  800. default default for all contexts not set explicitly"
  801. :group 'org-reveal-location
  802. :type org-context-choice)
  803. (defcustom org-show-following-heading '((default . nil))
  804. "Non-nil means show following heading when revealing a location.
  805. Org-mode often shows locations in an org-mode file which might have
  806. been invisible before. When this is set, the heading following the
  807. match is shown.
  808. Turning this off for example for sparse trees makes them very compact,
  809. but makes it harder to edit the location of the match. In such a case,
  810. use the command \\[org-reveal] to show more context.
  811. Instead of t, this can also be an alist specifying this option for different
  812. contexts. See `org-show-hierarchy-above' for valid contexts."
  813. :group 'org-reveal-location
  814. :type org-context-choice)
  815. (defcustom org-show-siblings '((default . nil) (isearch t))
  816. "Non-nil means show all sibling heading when revealing a location.
  817. Org-mode often shows locations in an org-mode file which might have
  818. been invisible before. When this is set, the sibling of the current entry
  819. heading are all made visible. If `org-show-hierarchy-above' is t,
  820. the same happens on each level of the hierarchy above the current entry.
  821. By default this is on for the isearch context, off for all other contexts.
  822. Turning this off for example for sparse trees makes them very compact,
  823. but makes it harder to edit the location of the match. In such a case,
  824. use the command \\[org-reveal] to show more context.
  825. Instead of t, this can also be an alist specifying this option for different
  826. contexts. See `org-show-hierarchy-above' for valid contexts."
  827. :group 'org-reveal-location
  828. :type org-context-choice)
  829. (defcustom org-show-entry-below '((default . nil))
  830. "Non-nil means show the entry below a headline when revealing a location.
  831. Org-mode often shows locations in an org-mode file which might have
  832. been invisible before. When this is set, the text below the headline that is
  833. exposed is also shown.
  834. By default this is off for all contexts.
  835. Instead of t, this can also be an alist specifying this option for different
  836. contexts. See `org-show-hierarchy-above' for valid contexts."
  837. :group 'org-reveal-location
  838. :type org-context-choice)
  839. (defcustom org-indirect-buffer-display 'other-window
  840. "How should indirect tree buffers be displayed?
  841. This applies to indirect buffers created with the commands
  842. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  843. Valid values are:
  844. current-window Display in the current window
  845. other-window Just display in another window.
  846. dedicated-frame Create one new frame, and re-use it each time.
  847. new-frame Make a new frame each time. Note that in this case
  848. previously-made indirect buffers are kept, and you need to
  849. kill these buffers yourself."
  850. :group 'org-structure
  851. :group 'org-agenda-windows
  852. :type '(choice
  853. (const :tag "In current window" current-window)
  854. (const :tag "In current frame, other window" other-window)
  855. (const :tag "Each time a new frame" new-frame)
  856. (const :tag "One dedicated frame" dedicated-frame)))
  857. (defcustom org-use-speed-commands nil
  858. "Non-nil means activate single letter commands at beginning of a headline.
  859. This may also be a function to test for appropriate locations where speed
  860. commands should be active."
  861. :group 'org-structure
  862. :type '(choice
  863. (const :tag "Never" nil)
  864. (const :tag "At beginning of headline stars" t)
  865. (function)))
  866. (defcustom org-speed-commands-user nil
  867. "Alist of additional speed commands.
  868. This list will be checked before `org-speed-commands-default'
  869. when the variable `org-use-speed-commands' is non-nil
  870. and when the cursor is at the beginning of a headline.
  871. The car if each entry is a string with a single letter, which must
  872. be assigned to `self-insert-command' in the global map.
  873. The cdr is either a command to be called interactively, a function
  874. to be called, or a form to be evaluated.
  875. An entry that is just a list with a single string will be interpreted
  876. as a descriptive headline that will be added when listing the speed
  877. commands in the Help buffer using the `?' speed command."
  878. :group 'org-structure
  879. :type '(repeat :value ("k" . ignore)
  880. (choice :value ("k" . ignore)
  881. (list :tag "Descriptive Headline" (string :tag "Headline"))
  882. (cons :tag "Letter and Command"
  883. (string :tag "Command letter")
  884. (choice
  885. (function)
  886. (sexp))))))
  887. (defgroup org-cycle nil
  888. "Options concerning visibility cycling in Org-mode."
  889. :tag "Org Cycle"
  890. :group 'org-structure)
  891. (defcustom org-cycle-skip-children-state-if-no-children t
  892. "Non-nil means skip CHILDREN state in entries that don't have any."
  893. :group 'org-cycle
  894. :type 'boolean)
  895. (defcustom org-cycle-max-level nil
  896. "Maximum level which should still be subject to visibility cycling.
  897. Levels higher than this will, for cycling, be treated as text, not a headline.
  898. When `org-odd-levels-only' is set, a value of N in this variable actually
  899. means 2N-1 stars as the limiting headline.
  900. When nil, cycle all levels.
  901. Note that the limiting level of cycling is also influenced by
  902. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  903. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  904. than its value."
  905. :group 'org-cycle
  906. :type '(choice
  907. (const :tag "No limit" nil)
  908. (integer :tag "Maximum level")))
  909. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK" "RESULTS")
  910. "Names of drawers. Drawers are not opened by cycling on the headline above.
  911. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  912. this:
  913. :DRAWERNAME:
  914. .....
  915. :END:
  916. The drawer \"PROPERTIES\" is special for capturing properties through
  917. the property API.
  918. Drawers can be defined on the per-file basis with a line like:
  919. #+DRAWERS: HIDDEN STATE PROPERTIES"
  920. :group 'org-structure
  921. :group 'org-cycle
  922. :type '(repeat (string :tag "Drawer Name")))
  923. (defcustom org-hide-block-startup nil
  924. "Non-nil means entering Org-mode will fold all blocks.
  925. This can also be set in on a per-file basis with
  926. #+STARTUP: hideblocks
  927. #+STARTUP: showblocks"
  928. :group 'org-startup
  929. :group 'org-cycle
  930. :type 'boolean)
  931. (defcustom org-cycle-global-at-bob nil
  932. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  933. This makes it possible to do global cycling without having to use S-TAB or
  934. \\[universal-argument] TAB. For this special case to work, the first line
  935. of the buffer must not be a headline -- it may be empty or some other text.
  936. When used in this way, `org-cycle-hook' is disabled temporarily to make
  937. sure the cursor stays at the beginning of the buffer. When this option is
  938. nil, don't do anything special at the beginning of the buffer."
  939. :group 'org-cycle
  940. :type 'boolean)
  941. (defcustom org-cycle-level-after-item/entry-creation t
  942. "Non-nil means cycle entry level or item indentation in new empty entries.
  943. When the cursor is at the end of an empty headline, i.e., with only stars
  944. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  945. and then all possible ancestor states, before returning to the original state.
  946. This makes data entry extremely fast: M-RET to create a new headline,
  947. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  948. When the cursor is at the end of an empty plain list item, one TAB will
  949. make it a subitem, two or more tabs will back up to make this an item
  950. higher up in the item hierarchy."
  951. :group 'org-cycle
  952. :type 'boolean)
  953. (defcustom org-cycle-emulate-tab t
  954. "Where should `org-cycle' emulate TAB.
  955. nil Never
  956. white Only in completely white lines
  957. whitestart Only at the beginning of lines, before the first non-white char
  958. t Everywhere except in headlines
  959. exc-hl-bol Everywhere except at the start of a headline
  960. If TAB is used in a place where it does not emulate TAB, the current subtree
  961. visibility is cycled."
  962. :group 'org-cycle
  963. :type '(choice (const :tag "Never" nil)
  964. (const :tag "Only in completely white lines" white)
  965. (const :tag "Before first char in a line" whitestart)
  966. (const :tag "Everywhere except in headlines" t)
  967. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  968. ))
  969. (defcustom org-cycle-separator-lines 2
  970. "Number of empty lines needed to keep an empty line between collapsed trees.
  971. If you leave an empty line between the end of a subtree and the following
  972. headline, this empty line is hidden when the subtree is folded.
  973. Org-mode will leave (exactly) one empty line visible if the number of
  974. empty lines is equal or larger to the number given in this variable.
  975. So the default 2 means at least 2 empty lines after the end of a subtree
  976. are needed to produce free space between a collapsed subtree and the
  977. following headline.
  978. If the number is negative, and the number of empty lines is at least -N,
  979. all empty lines are shown.
  980. Special case: when 0, never leave empty lines in collapsed view."
  981. :group 'org-cycle
  982. :type 'integer)
  983. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  984. (defcustom org-pre-cycle-hook nil
  985. "Hook that is run before visibility cycling is happening.
  986. The function(s) in this hook must accept a single argument which indicates
  987. the new state that will be set right after running this hook. The
  988. argument is a symbol. Before a global state change, it can have the values
  989. `overview', `content', or `all'. Before a local state change, it can have
  990. the values `folded', `children', or `subtree'."
  991. :group 'org-cycle
  992. :type 'hook)
  993. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  994. org-cycle-hide-drawers
  995. org-cycle-hide-inline-tasks
  996. org-cycle-show-empty-lines
  997. org-optimize-window-after-visibility-change)
  998. "Hook that is run after `org-cycle' has changed the buffer visibility.
  999. The function(s) in this hook must accept a single argument which indicates
  1000. the new state that was set by the most recent `org-cycle' command. The
  1001. argument is a symbol. After a global state change, it can have the values
  1002. `overview', `contents', or `all'. After a local state change, it can have
  1003. the values `folded', `children', or `subtree'."
  1004. :group 'org-cycle
  1005. :type 'hook)
  1006. (defgroup org-edit-structure nil
  1007. "Options concerning structure editing in Org-mode."
  1008. :tag "Org Edit Structure"
  1009. :group 'org-structure)
  1010. (defcustom org-odd-levels-only nil
  1011. "Non-nil means skip even levels and only use odd levels for the outline.
  1012. This has the effect that two stars are being added/taken away in
  1013. promotion/demotion commands. It also influences how levels are
  1014. handled by the exporters.
  1015. Changing it requires restart of `font-lock-mode' to become effective
  1016. for fontification also in regions already fontified.
  1017. You may also set this on a per-file basis by adding one of the following
  1018. lines to the buffer:
  1019. #+STARTUP: odd
  1020. #+STARTUP: oddeven"
  1021. :group 'org-edit-structure
  1022. :group 'org-appearance
  1023. :type 'boolean)
  1024. (defcustom org-adapt-indentation t
  1025. "Non-nil means adapt indentation to outline node level.
  1026. When this variable is set, Org assumes that you write outlines by
  1027. indenting text in each node to align with the headline (after the stars).
  1028. The following issues are influenced by this variable:
  1029. - When this is set and the *entire* text in an entry is indented, the
  1030. indentation is increased by one space in a demotion command, and
  1031. decreased by one in a promotion command. If any line in the entry
  1032. body starts with text at column 0, indentation is not changed at all.
  1033. - Property drawers and planning information is inserted indented when
  1034. this variable s set. When nil, they will not be indented.
  1035. - TAB indents a line relative to context. The lines below a headline
  1036. will be indented when this variable is set.
  1037. Note that this is all about true indentation, by adding and removing
  1038. space characters. See also `org-indent.el' which does level-dependent
  1039. indentation in a virtual way, i.e. at display time in Emacs."
  1040. :group 'org-edit-structure
  1041. :type 'boolean)
  1042. (defcustom org-special-ctrl-a/e nil
  1043. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  1044. When t, `C-a' will bring back the cursor to the beginning of the
  1045. headline text, i.e. after the stars and after a possible TODO
  1046. keyword. In an item, this will be the position after bullet and
  1047. check-box, if any. When the cursor is already at that position,
  1048. another `C-a' will bring it to the beginning of the line.
  1049. `C-e' will jump to the end of the headline, ignoring the presence
  1050. of tags in the headline. A second `C-e' will then jump to the
  1051. true end of the line, after any tags. This also means that, when
  1052. this variable is non-nil, `C-e' also will never jump beyond the
  1053. end of the heading of a folded section, i.e. not after the
  1054. ellipses.
  1055. When set to the symbol `reversed', the first `C-a' or `C-e' works
  1056. normally, going to the true line boundary first. Only a directly
  1057. following, identical keypress will bring the cursor to the
  1058. special positions.
  1059. This may also be a cons cell where the behavior for `C-a' and
  1060. `C-e' is set separately."
  1061. :group 'org-edit-structure
  1062. :type '(choice
  1063. (const :tag "off" nil)
  1064. (const :tag "on: after stars/bullet and before tags first" t)
  1065. (const :tag "reversed: true line boundary first" reversed)
  1066. (cons :tag "Set C-a and C-e separately"
  1067. (choice :tag "Special C-a"
  1068. (const :tag "off" nil)
  1069. (const :tag "on: after stars/bullet first" t)
  1070. (const :tag "reversed: before stars/bullet first" reversed))
  1071. (choice :tag "Special C-e"
  1072. (const :tag "off" nil)
  1073. (const :tag "on: before tags first" t)
  1074. (const :tag "reversed: after tags first" reversed)))))
  1075. (if (fboundp 'defvaralias)
  1076. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  1077. (defcustom org-special-ctrl-k nil
  1078. "Non-nil means `C-k' will behave specially in headlines.
  1079. When nil, `C-k' will call the default `kill-line' command.
  1080. When t, the following will happen while the cursor is in the headline:
  1081. - When the cursor is at the beginning of a headline, kill the entire
  1082. line and possible the folded subtree below the line.
  1083. - When in the middle of the headline text, kill the headline up to the tags.
  1084. - When after the headline text, kill the tags."
  1085. :group 'org-edit-structure
  1086. :type 'boolean)
  1087. (defcustom org-ctrl-k-protect-subtree nil
  1088. "Non-nil means, do not delete a hidden subtree with C-k.
  1089. When set to the symbol `error', simply throw an error when C-k is
  1090. used to kill (part-of) a headline that has hidden text behind it.
  1091. Any other non-nil value will result in a query to the user, if it is
  1092. OK to kill that hidden subtree. When nil, kill without remorse."
  1093. :group 'org-edit-structure
  1094. :version "24.1"
  1095. :type '(choice
  1096. (const :tag "Do not protect hidden subtrees" nil)
  1097. (const :tag "Protect hidden subtrees with a security query" t)
  1098. (const :tag "Never kill a hidden subtree with C-k" error)))
  1099. (defcustom org-catch-invisible-edits nil
  1100. "Check if in invisible region before inserting or deleting a character.
  1101. Valid values are:
  1102. nil Do not check, so just do invisible edits.
  1103. error Throw an error and do nothing.
  1104. show Make point visible, and do the requested edit.
  1105. show-and-error Make point visible, then throw an error and abort the edit.
  1106. smart Make point visible, and do insertion/deletion if it is
  1107. adjacent to visible text and the change feels predictable.
  1108. Never delete a previously invisible character or add in the
  1109. middle or right after an invisible region. Basically, this
  1110. allows insertion and backward-delete right before ellipses.
  1111. FIXME: maybe in this case we should not even show?"
  1112. :group 'org-edit-structure
  1113. :version "24.1"
  1114. :type '(choice
  1115. (const :tag "Do not check" nil)
  1116. (const :tag "Throw error when trying to edit" error)
  1117. (const :tag "Unhide, but do not do the edit" show-and-error)
  1118. (const :tag "Show invisible part and do the edit" show)
  1119. (const :tag "Be smart and do the right thing" smart)))
  1120. (defcustom org-yank-folded-subtrees t
  1121. "Non-nil means when yanking subtrees, fold them.
  1122. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  1123. it starts with a heading and all other headings in it are either children
  1124. or siblings, then fold all the subtrees. However, do this only if no
  1125. text after the yank would be swallowed into a folded tree by this action."
  1126. :group 'org-edit-structure
  1127. :type 'boolean)
  1128. (defcustom org-yank-adjusted-subtrees nil
  1129. "Non-nil means when yanking subtrees, adjust the level.
  1130. With this setting, `org-paste-subtree' is used to insert the subtree, see
  1131. this function for details."
  1132. :group 'org-edit-structure
  1133. :type 'boolean)
  1134. (defcustom org-M-RET-may-split-line '((default . t))
  1135. "Non-nil means M-RET will split the line at the cursor position.
  1136. When nil, it will go to the end of the line before making a
  1137. new line.
  1138. You may also set this option in a different way for different
  1139. contexts. Valid contexts are:
  1140. headline when creating a new headline
  1141. item when creating a new item
  1142. table in a table field
  1143. default the value to be used for all contexts not explicitly
  1144. customized"
  1145. :group 'org-structure
  1146. :group 'org-table
  1147. :type '(choice
  1148. (const :tag "Always" t)
  1149. (const :tag "Never" nil)
  1150. (repeat :greedy t :tag "Individual contexts"
  1151. (cons
  1152. (choice :tag "Context"
  1153. (const headline)
  1154. (const item)
  1155. (const table)
  1156. (const default))
  1157. (boolean)))))
  1158. (defcustom org-insert-heading-respect-content nil
  1159. "Non-nil means insert new headings after the current subtree.
  1160. When nil, the new heading is created directly after the current line.
  1161. The commands \\[org-insert-heading-respect-content] and
  1162. \\[org-insert-todo-heading-respect-content] turn this variable on
  1163. for the duration of the command."
  1164. :group 'org-structure
  1165. :type 'boolean)
  1166. (defcustom org-blank-before-new-entry '((heading . auto)
  1167. (plain-list-item . auto))
  1168. "Should `org-insert-heading' leave a blank line before new heading/item?
  1169. The value is an alist, with `heading' and `plain-list-item' as CAR,
  1170. and a boolean flag as CDR. The cdr may also be the symbol `auto', in
  1171. which case Org will look at the surrounding headings/items and try to
  1172. make an intelligent decision whether to insert a blank line or not.
  1173. For plain lists, if the variable `org-empty-line-terminates-plain-lists' is
  1174. set, the setting here is ignored and no empty line is inserted, to avoid
  1175. breaking the list structure."
  1176. :group 'org-edit-structure
  1177. :type '(list
  1178. (cons (const heading)
  1179. (choice (const :tag "Never" nil)
  1180. (const :tag "Always" t)
  1181. (const :tag "Auto" auto)))
  1182. (cons (const plain-list-item)
  1183. (choice (const :tag "Never" nil)
  1184. (const :tag "Always" t)
  1185. (const :tag "Auto" auto)))))
  1186. (defcustom org-insert-heading-hook nil
  1187. "Hook being run after inserting a new heading."
  1188. :group 'org-edit-structure
  1189. :type 'hook)
  1190. (defcustom org-enable-fixed-width-editor t
  1191. "Non-nil means lines starting with \":\" are treated as fixed-width.
  1192. This currently only means they are never auto-wrapped.
  1193. When nil, such lines will be treated like ordinary lines.
  1194. See also the QUOTE keyword."
  1195. :group 'org-edit-structure
  1196. :type 'boolean)
  1197. (defcustom org-goto-auto-isearch t
  1198. "Non-nil means typing characters in `org-goto' starts incremental search.
  1199. When nil, you can use these keybindings to navigate the buffer:
  1200. q Quit the org-goto interface
  1201. n Go to the next visible heading
  1202. p Go to the previous visible heading
  1203. f Go one heading forward on same level
  1204. b Go one heading backward on same level
  1205. u Go one heading up"
  1206. :group 'org-edit-structure
  1207. :type 'boolean)
  1208. (defgroup org-sparse-trees nil
  1209. "Options concerning sparse trees in Org-mode."
  1210. :tag "Org Sparse Trees"
  1211. :group 'org-structure)
  1212. (defcustom org-highlight-sparse-tree-matches t
  1213. "Non-nil means highlight all matches that define a sparse tree.
  1214. The highlights will automatically disappear the next time the buffer is
  1215. changed by an edit command."
  1216. :group 'org-sparse-trees
  1217. :type 'boolean)
  1218. (defcustom org-remove-highlights-with-change t
  1219. "Non-nil means any change to the buffer will remove temporary highlights.
  1220. Such highlights are created by `org-occur' and `org-clock-display'.
  1221. When nil, `C-c C-c needs to be used to get rid of the highlights.
  1222. The highlights created by `org-preview-latex-fragment' always need
  1223. `C-c C-c' to be removed."
  1224. :group 'org-sparse-trees
  1225. :group 'org-time
  1226. :type 'boolean)
  1227. (defcustom org-occur-hook '(org-first-headline-recenter)
  1228. "Hook that is run after `org-occur' has constructed a sparse tree.
  1229. This can be used to recenter the window to show as much of the structure
  1230. as possible."
  1231. :group 'org-sparse-trees
  1232. :type 'hook)
  1233. (defgroup org-imenu-and-speedbar nil
  1234. "Options concerning imenu and speedbar in Org-mode."
  1235. :tag "Org Imenu and Speedbar"
  1236. :group 'org-structure)
  1237. (defcustom org-imenu-depth 2
  1238. "The maximum level for Imenu access to Org-mode headlines.
  1239. This also applied for speedbar access."
  1240. :group 'org-imenu-and-speedbar
  1241. :type 'integer)
  1242. (defgroup org-table nil
  1243. "Options concerning tables in Org-mode."
  1244. :tag "Org Table"
  1245. :group 'org)
  1246. (defcustom org-enable-table-editor 'optimized
  1247. "Non-nil means lines starting with \"|\" are handled by the table editor.
  1248. When nil, such lines will be treated like ordinary lines.
  1249. When equal to the symbol `optimized', the table editor will be optimized to
  1250. do the following:
  1251. - Automatic overwrite mode in front of whitespace in table fields.
  1252. This makes the structure of the table stay in tact as long as the edited
  1253. field does not exceed the column width.
  1254. - Minimize the number of realigns. Normally, the table is aligned each time
  1255. TAB or RET are pressed to move to another field. With optimization this
  1256. happens only if changes to a field might have changed the column width.
  1257. Optimization requires replacing the functions `self-insert-command',
  1258. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  1259. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  1260. very good at guessing when a re-align will be necessary, but you can always
  1261. force one with \\[org-ctrl-c-ctrl-c].
  1262. If you would like to use the optimized version in Org-mode, but the
  1263. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  1264. This variable can be used to turn on and off the table editor during a session,
  1265. but in order to toggle optimization, a restart is required.
  1266. See also the variable `org-table-auto-blank-field'."
  1267. :group 'org-table
  1268. :type '(choice
  1269. (const :tag "off" nil)
  1270. (const :tag "on" t)
  1271. (const :tag "on, optimized" optimized)))
  1272. (defcustom org-self-insert-cluster-for-undo (or (featurep 'xemacs)
  1273. (version<= emacs-version "24.1"))
  1274. "Non-nil means cluster self-insert commands for undo when possible.
  1275. If this is set, then, like in the Emacs command loop, 20 consecutive
  1276. characters will be undone together.
  1277. This is configurable, because there is some impact on typing performance."
  1278. :group 'org-table
  1279. :type 'boolean)
  1280. (defcustom org-table-tab-recognizes-table.el t
  1281. "Non-nil means TAB will automatically notice a table.el table.
  1282. When it sees such a table, it moves point into it and - if necessary -
  1283. calls `table-recognize-table'."
  1284. :group 'org-table-editing
  1285. :type 'boolean)
  1286. (defgroup org-link nil
  1287. "Options concerning links in Org-mode."
  1288. :tag "Org Link"
  1289. :group 'org)
  1290. (defvar org-link-abbrev-alist-local nil
  1291. "Buffer-local version of `org-link-abbrev-alist', which see.
  1292. The value of this is taken from the #+LINK lines.")
  1293. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  1294. (defcustom org-link-abbrev-alist nil
  1295. "Alist of link abbreviations.
  1296. The car of each element is a string, to be replaced at the start of a link.
  1297. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1298. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  1299. [[linkkey:tag][description]]
  1300. The 'linkkey' must be a word word, starting with a letter, followed
  1301. by letters, numbers, '-' or '_'.
  1302. If REPLACE is a string, the tag will simply be appended to create the link.
  1303. If the string contains \"%s\", the tag will be inserted there. If the string
  1304. contains \"%h\", it will cause a url-encoded version of the tag to be inserted
  1305. at that point (see the function `url-hexify-string'). If the string contains
  1306. the specifier \"%(my-function)\", then the custom function `my-function' will
  1307. be invoked: this function takes the tag as its only argument and must return
  1308. a string.
  1309. REPLACE may also be a function that will be called with the tag as the
  1310. only argument to create the link, which should be returned as a string.
  1311. See the manual for examples."
  1312. :group 'org-link
  1313. :type '(repeat
  1314. (cons
  1315. (string :tag "Protocol")
  1316. (choice
  1317. (string :tag "Format")
  1318. (function)))))
  1319. (defcustom org-descriptive-links t
  1320. "Non-nil means Org will display descriptive links.
  1321. E.g. [[http://orgmode.org][Org website]] will be displayed as
  1322. \"Org Website\", hiding the link itself and just displaying its
  1323. description. When set to `nil', Org will display the full links
  1324. literally.
  1325. You can interactively set the value of this variable by calling
  1326. `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
  1327. :group 'org-link
  1328. :type 'boolean)
  1329. (defcustom org-link-file-path-type 'adaptive
  1330. "How the path name in file links should be stored.
  1331. Valid values are:
  1332. relative Relative to the current directory, i.e. the directory of the file
  1333. into which the link is being inserted.
  1334. absolute Absolute path, if possible with ~ for home directory.
  1335. noabbrev Absolute path, no abbreviation of home directory.
  1336. adaptive Use relative path for files in the current directory and sub-
  1337. directories of it. For other files, use an absolute path."
  1338. :group 'org-link
  1339. :type '(choice
  1340. (const relative)
  1341. (const absolute)
  1342. (const noabbrev)
  1343. (const adaptive)))
  1344. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  1345. "Types of links that should be activated in Org-mode files.
  1346. This is a list of symbols, each leading to the activation of a certain link
  1347. type. In principle, it does not hurt to turn on most link types - there may
  1348. be a small gain when turning off unused link types. The types are:
  1349. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1350. angle Links in angular brackets that may contain whitespace like
  1351. <bbdb:Carsten Dominik>.
  1352. plain Plain links in normal text, no whitespace, like http://google.com.
  1353. radio Text that is matched by a radio target, see manual for details.
  1354. tag Tag settings in a headline (link to tag search).
  1355. date Time stamps (link to calendar).
  1356. footnote Footnote labels.
  1357. Changing this variable requires a restart of Emacs to become effective."
  1358. :group 'org-link
  1359. :type '(set :greedy t
  1360. (const :tag "Double bracket links" bracket)
  1361. (const :tag "Angular bracket links" angle)
  1362. (const :tag "Plain text links" plain)
  1363. (const :tag "Radio target matches" radio)
  1364. (const :tag "Tags" tag)
  1365. (const :tag "Timestamps" date)
  1366. (const :tag "Footnotes" footnote)))
  1367. (defcustom org-make-link-description-function nil
  1368. "Function to use for generating link descriptions from links.
  1369. When nil, the link location will be used. This function must take
  1370. two parameters: the first one is the link, the second one is the
  1371. description generated by `org-insert-link'. The function should
  1372. return the description to use."
  1373. :group 'org-link
  1374. :type 'function)
  1375. (defgroup org-link-store nil
  1376. "Options concerning storing links in Org-mode."
  1377. :tag "Org Store Link"
  1378. :group 'org-link)
  1379. (defcustom org-url-hexify-p t
  1380. "When non-nil, hexify URL when creating a link."
  1381. :type 'boolean
  1382. :version "24.3"
  1383. :group 'org-link-store)
  1384. (defcustom org-email-link-description-format "Email %c: %.30s"
  1385. "Format of the description part of a link to an email or usenet message.
  1386. The following %-escapes will be replaced by corresponding information:
  1387. %F full \"From\" field
  1388. %f name, taken from \"From\" field, address if no name
  1389. %T full \"To\" field
  1390. %t first name in \"To\" field, address if no name
  1391. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1392. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1393. %s subject
  1394. %d date
  1395. %m message-id.
  1396. You may use normal field width specification between the % and the letter.
  1397. This is for example useful to limit the length of the subject.
  1398. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1399. :group 'org-link-store
  1400. :type 'string)
  1401. (defcustom org-from-is-user-regexp
  1402. (let (r1 r2)
  1403. (when (and user-mail-address (not (string= user-mail-address "")))
  1404. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1405. (when (and user-full-name (not (string= user-full-name "")))
  1406. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1407. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1408. "Regexp matched against the \"From:\" header of an email or usenet message.
  1409. It should match if the message is from the user him/herself."
  1410. :group 'org-link-store
  1411. :type 'regexp)
  1412. (defcustom org-context-in-file-links t
  1413. "Non-nil means file links from `org-store-link' contain context.
  1414. A search string will be added to the file name with :: as separator and
  1415. used to find the context when the link is activated by the command
  1416. `org-open-at-point'. When this option is t, the entire active region
  1417. will be placed in the search string of the file link. If set to a
  1418. positive integer, only the first n lines of context will be stored.
  1419. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1420. negates this setting for the duration of the command."
  1421. :group 'org-link-store
  1422. :type '(choice boolean integer))
  1423. (defcustom org-keep-stored-link-after-insertion nil
  1424. "Non-nil means keep link in list for entire session.
  1425. The command `org-store-link' adds a link pointing to the current
  1426. location to an internal list. These links accumulate during a session.
  1427. The command `org-insert-link' can be used to insert links into any
  1428. Org-mode file (offering completion for all stored links). When this
  1429. option is nil, every link which has been inserted once using \\[org-insert-link]
  1430. will be removed from the list, to make completing the unused links
  1431. more efficient."
  1432. :group 'org-link-store
  1433. :type 'boolean)
  1434. (defgroup org-link-follow nil
  1435. "Options concerning following links in Org-mode."
  1436. :tag "Org Follow Link"
  1437. :group 'org-link)
  1438. (defcustom org-link-translation-function nil
  1439. "Function to translate links with different syntax to Org syntax.
  1440. This can be used to translate links created for example by the Planner
  1441. or emacs-wiki packages to Org syntax.
  1442. The function must accept two parameters, a TYPE containing the link
  1443. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1444. which is everything after the link protocol. It should return a cons
  1445. with possibly modified values of type and path.
  1446. Org contains a function for this, so if you set this variable to
  1447. `org-translate-link-from-planner', you should be able follow many
  1448. links created by planner."
  1449. :group 'org-link-follow
  1450. :type 'function)
  1451. (defcustom org-follow-link-hook nil
  1452. "Hook that is run after a link has been followed."
  1453. :group 'org-link-follow
  1454. :type 'hook)
  1455. (defcustom org-tab-follows-link nil
  1456. "Non-nil means on links TAB will follow the link.
  1457. Needs to be set before org.el is loaded.
  1458. This really should not be used, it does not make sense, and the
  1459. implementation is bad."
  1460. :group 'org-link-follow
  1461. :type 'boolean)
  1462. (defcustom org-return-follows-link nil
  1463. "Non-nil means on links RET will follow the link.
  1464. In tables, the special behavior of RET has precedence."
  1465. :group 'org-link-follow
  1466. :type 'boolean)
  1467. (defcustom org-mouse-1-follows-link
  1468. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1469. "Non-nil means mouse-1 on a link will follow the link.
  1470. A longer mouse click will still set point. Does not work on XEmacs.
  1471. Needs to be set before org.el is loaded."
  1472. :group 'org-link-follow
  1473. :type 'boolean)
  1474. (defcustom org-mark-ring-length 4
  1475. "Number of different positions to be recorded in the ring.
  1476. Changing this requires a restart of Emacs to work correctly."
  1477. :group 'org-link-follow
  1478. :type 'integer)
  1479. (defcustom org-link-search-must-match-exact-headline 'query-to-create
  1480. "Non-nil means internal links in Org files must exactly match a headline.
  1481. When nil, the link search tries to match a phrase with all words
  1482. in the search text."
  1483. :group 'org-link-follow
  1484. :version "24.1"
  1485. :type '(choice
  1486. (const :tag "Use fuzzy text search" nil)
  1487. (const :tag "Match only exact headline" t)
  1488. (const :tag "Match exact headline or query to create it"
  1489. query-to-create)))
  1490. (defcustom org-link-frame-setup
  1491. '((vm . vm-visit-folder-other-frame)
  1492. (vm-imap . vm-visit-imap-folder-other-frame)
  1493. (gnus . org-gnus-no-new-news)
  1494. (file . find-file-other-window)
  1495. (wl . wl-other-frame))
  1496. "Setup the frame configuration for following links.
  1497. When following a link with Emacs, it may often be useful to display
  1498. this link in another window or frame. This variable can be used to
  1499. set this up for the different types of links.
  1500. For VM, use any of
  1501. `vm-visit-folder'
  1502. `vm-visit-folder-other-window'
  1503. `vm-visit-folder-other-frame'
  1504. For Gnus, use any of
  1505. `gnus'
  1506. `gnus-other-frame'
  1507. `org-gnus-no-new-news'
  1508. For FILE, use any of
  1509. `find-file'
  1510. `find-file-other-window'
  1511. `find-file-other-frame'
  1512. For Wanderlust use any of
  1513. `wl'
  1514. `wl-other-frame'
  1515. For the calendar, use the variable `calendar-setup'.
  1516. For BBDB, it is currently only possible to display the matches in
  1517. another window."
  1518. :group 'org-link-follow
  1519. :type '(list
  1520. (cons (const vm)
  1521. (choice
  1522. (const vm-visit-folder)
  1523. (const vm-visit-folder-other-window)
  1524. (const vm-visit-folder-other-frame)))
  1525. (cons (const gnus)
  1526. (choice
  1527. (const gnus)
  1528. (const gnus-other-frame)
  1529. (const org-gnus-no-new-news)))
  1530. (cons (const file)
  1531. (choice
  1532. (const find-file)
  1533. (const find-file-other-window)
  1534. (const find-file-other-frame)))
  1535. (cons (const wl)
  1536. (choice
  1537. (const wl)
  1538. (const wl-other-frame)))))
  1539. (defcustom org-display-internal-link-with-indirect-buffer nil
  1540. "Non-nil means use indirect buffer to display infile links.
  1541. Activating internal links (from one location in a file to another location
  1542. in the same file) normally just jumps to the location. When the link is
  1543. activated with a \\[universal-argument] prefix (or with mouse-3), the link \
  1544. is displayed in
  1545. another window. When this option is set, the other window actually displays
  1546. an indirect buffer clone of the current buffer, to avoid any visibility
  1547. changes to the current buffer."
  1548. :group 'org-link-follow
  1549. :type 'boolean)
  1550. (defcustom org-open-non-existing-files nil
  1551. "Non-nil means `org-open-file' will open non-existing files.
  1552. When nil, an error will be generated.
  1553. This variable applies only to external applications because they
  1554. might choke on non-existing files. If the link is to a file that
  1555. will be opened in Emacs, the variable is ignored."
  1556. :group 'org-link-follow
  1557. :type 'boolean)
  1558. (defcustom org-open-directory-means-index-dot-org nil
  1559. "Non-nil means a link to a directory really means to index.org.
  1560. When nil, following a directory link will run dired or open a finder/explorer
  1561. window on that directory."
  1562. :group 'org-link-follow
  1563. :type 'boolean)
  1564. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1565. "Function and arguments to call for following mailto links.
  1566. This is a list with the first element being a Lisp function, and the
  1567. remaining elements being arguments to the function. In string arguments,
  1568. %a will be replaced by the address, and %s will be replaced by the subject
  1569. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1570. :group 'org-link-follow
  1571. :type '(choice
  1572. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1573. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1574. (const :tag "message-mail" (message-mail "%a" "%s"))
  1575. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1576. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1577. "Non-nil means ask for confirmation before executing shell links.
  1578. Shell links can be dangerous: just think about a link
  1579. [[shell:rm -rf ~/*][Google Search]]
  1580. This link would show up in your Org-mode document as \"Google Search\",
  1581. but really it would remove your entire home directory.
  1582. Therefore we advise against setting this variable to nil.
  1583. Just change it to `y-or-n-p' if you want to confirm with a
  1584. single keystroke rather than having to type \"yes\"."
  1585. :group 'org-link-follow
  1586. :type '(choice
  1587. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1588. (const :tag "with y-or-n (faster)" y-or-n-p)
  1589. (const :tag "no confirmation (dangerous)" nil)))
  1590. (put 'org-confirm-shell-link-function
  1591. 'safe-local-variable
  1592. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1593. (defcustom org-confirm-shell-link-not-regexp ""
  1594. "A regexp to skip confirmation for shell links."
  1595. :group 'org-link-follow
  1596. :version "24.1"
  1597. :type 'regexp)
  1598. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1599. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1600. Elisp links can be dangerous: just think about a link
  1601. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1602. This link would show up in your Org-mode document as \"Google Search\",
  1603. but really it would remove your entire home directory.
  1604. Therefore we advise against setting this variable to nil.
  1605. Just change it to `y-or-n-p' if you want to confirm with a
  1606. single keystroke rather than having to type \"yes\"."
  1607. :group 'org-link-follow
  1608. :type '(choice
  1609. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1610. (const :tag "with y-or-n (faster)" y-or-n-p)
  1611. (const :tag "no confirmation (dangerous)" nil)))
  1612. (put 'org-confirm-shell-link-function
  1613. 'safe-local-variable
  1614. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1615. (defcustom org-confirm-elisp-link-not-regexp ""
  1616. "A regexp to skip confirmation for Elisp links."
  1617. :group 'org-link-follow
  1618. :version "24.1"
  1619. :type 'regexp)
  1620. (defconst org-file-apps-defaults-gnu
  1621. '((remote . emacs)
  1622. (system . mailcap)
  1623. (t . mailcap))
  1624. "Default file applications on a UNIX or GNU/Linux system.
  1625. See `org-file-apps'.")
  1626. (defconst org-file-apps-defaults-macosx
  1627. '((remote . emacs)
  1628. (t . "open %s")
  1629. (system . "open %s")
  1630. ("ps.gz" . "gv %s")
  1631. ("eps.gz" . "gv %s")
  1632. ("dvi" . "xdvi %s")
  1633. ("fig" . "xfig %s"))
  1634. "Default file applications on a MacOS X system.
  1635. The system \"open\" is known as a default, but we use X11 applications
  1636. for some files for which the OS does not have a good default.
  1637. See `org-file-apps'.")
  1638. (defconst org-file-apps-defaults-windowsnt
  1639. (list
  1640. '(remote . emacs)
  1641. (cons t
  1642. (list (if (featurep 'xemacs)
  1643. 'mswindows-shell-execute
  1644. 'w32-shell-execute)
  1645. "open" 'file))
  1646. (cons 'system
  1647. (list (if (featurep 'xemacs)
  1648. 'mswindows-shell-execute
  1649. 'w32-shell-execute)
  1650. "open" 'file)))
  1651. "Default file applications on a Windows NT system.
  1652. The system \"open\" is used for most files.
  1653. See `org-file-apps'.")
  1654. (defcustom org-file-apps
  1655. '(
  1656. (auto-mode . emacs)
  1657. ("\\.mm\\'" . default)
  1658. ("\\.x?html?\\'" . default)
  1659. ("\\.pdf\\'" . default)
  1660. )
  1661. "External applications for opening `file:path' items in a document.
  1662. Org-mode uses system defaults for different file types, but
  1663. you can use this variable to set the application for a given file
  1664. extension. The entries in this list are cons cells where the car identifies
  1665. files and the cdr the corresponding command. Possible values for the
  1666. file identifier are
  1667. \"string\" A string as a file identifier can be interpreted in different
  1668. ways, depending on its contents:
  1669. - Alphanumeric characters only:
  1670. Match links with this file extension.
  1671. Example: (\"pdf\" . \"evince %s\")
  1672. to open PDFs with evince.
  1673. - Regular expression: Match links where the
  1674. filename matches the regexp. If you want to
  1675. use groups here, use shy groups.
  1676. Example: (\"\\.x?html\\'\" . \"firefox %s\")
  1677. (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1678. to open *.html and *.xhtml with firefox.
  1679. - Regular expression which contains (non-shy) groups:
  1680. Match links where the whole link, including \"::\", and
  1681. anything after that, matches the regexp.
  1682. In a custom command string, %1, %2, etc. are replaced with
  1683. the parts of the link that were matched by the groups.
  1684. For backwards compatibility, if a command string is given
  1685. that does not use any of the group matches, this case is
  1686. handled identically to the second one (i.e. match against
  1687. file name only).
  1688. In a custom lisp form, you can access the group matches with
  1689. (match-string n link).
  1690. Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
  1691. to open [[file:document.pdf::5]] with evince at page 5.
  1692. `directory' Matches a directory
  1693. `remote' Matches a remote file, accessible through tramp or efs.
  1694. Remote files most likely should be visited through Emacs
  1695. because external applications cannot handle such paths.
  1696. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1697. so all files Emacs knows how to handle. Using this with
  1698. command `emacs' will open most files in Emacs. Beware that this
  1699. will also open html files inside Emacs, unless you add
  1700. (\"html\" . default) to the list as well.
  1701. t Default for files not matched by any of the other options.
  1702. `system' The system command to open files, like `open' on Windows
  1703. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1704. that will be selected if you call `C-c C-o' with a double
  1705. \\[universal-argument] \\[universal-argument] prefix.
  1706. Possible values for the command are:
  1707. `emacs' The file will be visited by the current Emacs process.
  1708. `default' Use the default application for this file type, which is the
  1709. association for t in the list, most likely in the system-specific
  1710. part.
  1711. This can be used to overrule an unwanted setting in the
  1712. system-specific variable.
  1713. `system' Use the system command for opening files, like \"open\".
  1714. This command is specified by the entry whose car is `system'.
  1715. Most likely, the system-specific version of this variable
  1716. does define this command, but you can overrule/replace it
  1717. here.
  1718. string A command to be executed by a shell; %s will be replaced
  1719. by the path to the file.
  1720. sexp A Lisp form which will be evaluated. The file path will
  1721. be available in the Lisp variable `file'.
  1722. For more examples, see the system specific constants
  1723. `org-file-apps-defaults-macosx'
  1724. `org-file-apps-defaults-windowsnt'
  1725. `org-file-apps-defaults-gnu'."
  1726. :group 'org-link-follow
  1727. :type '(repeat
  1728. (cons (choice :value ""
  1729. (string :tag "Extension")
  1730. (const :tag "System command to open files" system)
  1731. (const :tag "Default for unrecognized files" t)
  1732. (const :tag "Remote file" remote)
  1733. (const :tag "Links to a directory" directory)
  1734. (const :tag "Any files that have Emacs modes"
  1735. auto-mode))
  1736. (choice :value ""
  1737. (const :tag "Visit with Emacs" emacs)
  1738. (const :tag "Use default" default)
  1739. (const :tag "Use the system command" system)
  1740. (string :tag "Command")
  1741. (sexp :tag "Lisp form")))))
  1742. (defcustom org-doi-server-url "http://dx.doi.org/"
  1743. "The URL of the DOI server."
  1744. :type 'string
  1745. :version "24.3"
  1746. :group 'org-link-follow)
  1747. (defgroup org-refile nil
  1748. "Options concerning refiling entries in Org-mode."
  1749. :tag "Org Refile"
  1750. :group 'org)
  1751. (defcustom org-directory "~/org"
  1752. "Directory with org files.
  1753. This is just a default location to look for Org files. There is no need
  1754. at all to put your files into this directory. It is only used in the
  1755. following situations:
  1756. 1. When a capture template specifies a target file that is not an
  1757. absolute path. The path will then be interpreted relative to
  1758. `org-directory'
  1759. 2. When a capture note is filed away in an interactive way (when exiting the
  1760. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1761. with `org-directory' as the default path."
  1762. :group 'org-refile
  1763. :group 'org-remember
  1764. :group 'org-capture
  1765. :type 'directory)
  1766. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1767. "Default target for storing notes.
  1768. Used as a fall back file for org-remember.el and org-capture.el, for
  1769. templates that do not specify a target file."
  1770. :group 'org-refile
  1771. :group 'org-remember
  1772. :group 'org-capture
  1773. :type '(choice
  1774. (const :tag "Default from remember-data-file" nil)
  1775. file))
  1776. (defcustom org-goto-interface 'outline
  1777. "The default interface to be used for `org-goto'.
  1778. Allowed values are:
  1779. outline The interface shows an outline of the relevant file
  1780. and the correct heading is found by moving through
  1781. the outline or by searching with incremental search.
  1782. outline-path-completion Headlines in the current buffer are offered via
  1783. completion. This is the interface also used by
  1784. the refile command."
  1785. :group 'org-refile
  1786. :type '(choice
  1787. (const :tag "Outline" outline)
  1788. (const :tag "Outline-path-completion" outline-path-completion)))
  1789. (defcustom org-goto-max-level 5
  1790. "Maximum target level when running `org-goto' with refile interface."
  1791. :group 'org-refile
  1792. :type 'integer)
  1793. (defcustom org-reverse-note-order nil
  1794. "Non-nil means store new notes at the beginning of a file or entry.
  1795. When nil, new notes will be filed to the end of a file or entry.
  1796. This can also be a list with cons cells of regular expressions that
  1797. are matched against file names, and values."
  1798. :group 'org-remember
  1799. :group 'org-capture
  1800. :group 'org-refile
  1801. :type '(choice
  1802. (const :tag "Reverse always" t)
  1803. (const :tag "Reverse never" nil)
  1804. (repeat :tag "By file name regexp"
  1805. (cons regexp boolean))))
  1806. (defcustom org-log-refile nil
  1807. "Information to record when a task is refiled.
  1808. Possible values are:
  1809. nil Don't add anything
  1810. time Add a time stamp to the task
  1811. note Prompt for a note and add it with template `org-log-note-headings'
  1812. This option can also be set with on a per-file-basis with
  1813. #+STARTUP: nologrefile
  1814. #+STARTUP: logrefile
  1815. #+STARTUP: lognoterefile
  1816. You can have local logging settings for a subtree by setting the LOGGING
  1817. property to one or more of these keywords.
  1818. When bulk-refiling from the agenda, the value `note' is forbidden and
  1819. will temporarily be changed to `time'."
  1820. :group 'org-refile
  1821. :group 'org-progress
  1822. :version "24.1"
  1823. :type '(choice
  1824. (const :tag "No logging" nil)
  1825. (const :tag "Record timestamp" time)
  1826. (const :tag "Record timestamp with note." note)))
  1827. (defcustom org-refile-targets nil
  1828. "Targets for refiling entries with \\[org-refile].
  1829. This is a list of cons cells. Each cell contains:
  1830. - a specification of the files to be considered, either a list of files,
  1831. or a symbol whose function or variable value will be used to retrieve
  1832. a file name or a list of file names. If you use `org-agenda-files' for
  1833. that, all agenda files will be scanned for targets. Nil means consider
  1834. headings in the current buffer.
  1835. - A specification of how to find candidate refile targets. This may be
  1836. any of:
  1837. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1838. This tag has to be present in all target headlines, inheritance will
  1839. not be considered.
  1840. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1841. todo keyword.
  1842. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1843. headlines that are refiling targets.
  1844. - a cons cell (:level . N). Any headline of level N is considered a target.
  1845. Note that, when `org-odd-levels-only' is set, level corresponds to
  1846. order in hierarchy, not to the number of stars.
  1847. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1848. Note that, when `org-odd-levels-only' is set, level corresponds to
  1849. order in hierarchy, not to the number of stars.
  1850. Each element of this list generates a set of possible targets.
  1851. The union of these sets is presented (with completion) to
  1852. the user by `org-refile'.
  1853. You can set the variable `org-refile-target-verify-function' to a function
  1854. to verify each headline found by the simple criteria above.
  1855. When this variable is nil, all top-level headlines in the current buffer
  1856. are used, equivalent to the value `((nil . (:level . 1))'."
  1857. :group 'org-refile
  1858. :type '(repeat
  1859. (cons
  1860. (choice :value org-agenda-files
  1861. (const :tag "All agenda files" org-agenda-files)
  1862. (const :tag "Current buffer" nil)
  1863. (function) (variable) (file))
  1864. (choice :tag "Identify target headline by"
  1865. (cons :tag "Specific tag" (const :value :tag) (string))
  1866. (cons :tag "TODO keyword" (const :value :todo) (string))
  1867. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1868. (cons :tag "Level number" (const :value :level) (integer))
  1869. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1870. (defcustom org-refile-target-verify-function nil
  1871. "Function to verify if the headline at point should be a refile target.
  1872. The function will be called without arguments, with point at the
  1873. beginning of the headline. It should return t and leave point
  1874. where it is if the headline is a valid target for refiling.
  1875. If the target should not be selected, the function must return nil.
  1876. In addition to this, it may move point to a place from where the search
  1877. should be continued. For example, the function may decide that the entire
  1878. subtree of the current entry should be excluded and move point to the end
  1879. of the subtree."
  1880. :group 'org-refile
  1881. :type 'function)
  1882. (defcustom org-refile-use-cache nil
  1883. "Non-nil means cache refile targets to speed up the process.
  1884. The cache for a particular file will be updated automatically when
  1885. the buffer has been killed, or when any of the marker used for flagging
  1886. refile targets no longer points at a live buffer.
  1887. If you have added new entries to a buffer that might themselves be targets,
  1888. you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
  1889. find that easier, `C-u C-u C-u C-c C-w'."
  1890. :group 'org-refile
  1891. :version "24.1"
  1892. :type 'boolean)
  1893. (defcustom org-refile-use-outline-path nil
  1894. "Non-nil means provide refile targets as paths.
  1895. So a level 3 headline will be available as level1/level2/level3.
  1896. When the value is `file', also include the file name (without directory)
  1897. into the path. In this case, you can also stop the completion after
  1898. the file name, to get entries inserted as top level in the file.
  1899. When `full-file-path', include the full file path."
  1900. :group 'org-refile
  1901. :type '(choice
  1902. (const :tag "Not" nil)
  1903. (const :tag "Yes" t)
  1904. (const :tag "Start with file name" file)
  1905. (const :tag "Start with full file path" full-file-path)))
  1906. (defcustom org-outline-path-complete-in-steps t
  1907. "Non-nil means complete the outline path in hierarchical steps.
  1908. When Org-mode uses the refile interface to select an outline path
  1909. \(see variable `org-refile-use-outline-path'), the completion of
  1910. the path can be done is a single go, or if can be done in steps down
  1911. the headline hierarchy. Going in steps is probably the best if you
  1912. do not use a special completion package like `ido' or `icicles'.
  1913. However, when using these packages, going in one step can be very
  1914. fast, while still showing the whole path to the entry."
  1915. :group 'org-refile
  1916. :type 'boolean)
  1917. (defcustom org-refile-allow-creating-parent-nodes nil
  1918. "Non-nil means allow to create new nodes as refile targets.
  1919. New nodes are then created by adding \"/new node name\" to the completion
  1920. of an existing node. When the value of this variable is `confirm',
  1921. new node creation must be confirmed by the user (recommended)
  1922. When nil, the completion must match an existing entry.
  1923. Note that, if the new heading is not seen by the criteria
  1924. listed in `org-refile-targets', multiple instances of the same
  1925. heading would be created by trying again to file under the new
  1926. heading."
  1927. :group 'org-refile
  1928. :type '(choice
  1929. (const :tag "Never" nil)
  1930. (const :tag "Always" t)
  1931. (const :tag "Prompt for confirmation" confirm)))
  1932. (defcustom org-refile-active-region-within-subtree nil
  1933. "Non-nil means also refile active region within a subtree.
  1934. By default `org-refile' doesn't allow refiling regions if they
  1935. don't contain a set of subtrees, but it might be convenient to
  1936. do so sometimes: in that case, the first line of the region is
  1937. converted to a headline before refiling."
  1938. :group 'org-refile
  1939. :version "24.1"
  1940. :type 'boolean)
  1941. (defgroup org-todo nil
  1942. "Options concerning TODO items in Org-mode."
  1943. :tag "Org TODO"
  1944. :group 'org)
  1945. (defgroup org-progress nil
  1946. "Options concerning Progress logging in Org-mode."
  1947. :tag "Org Progress"
  1948. :group 'org-time)
  1949. (defvar org-todo-interpretation-widgets
  1950. '((:tag "Sequence (cycling hits every state)" sequence)
  1951. (:tag "Type (cycling directly to DONE)" type))
  1952. "The available interpretation symbols for customizing `org-todo-keywords'.
  1953. Interested libraries should add to this list.")
  1954. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1955. "List of TODO entry keyword sequences and their interpretation.
  1956. \\<org-mode-map>This is a list of sequences.
  1957. Each sequence starts with a symbol, either `sequence' or `type',
  1958. indicating if the keywords should be interpreted as a sequence of
  1959. action steps, or as different types of TODO items. The first
  1960. keywords are states requiring action - these states will select a headline
  1961. for inclusion into the global TODO list Org-mode produces. If one of
  1962. the \"keywords\" is the vertical bar, \"|\", the remaining keywords
  1963. signify that no further action is necessary. If \"|\" is not found,
  1964. the last keyword is treated as the only DONE state of the sequence.
  1965. The command \\[org-todo] cycles an entry through these states, and one
  1966. additional state where no keyword is present. For details about this
  1967. cycling, see the manual.
  1968. TODO keywords and interpretation can also be set on a per-file basis with
  1969. the special #+SEQ_TODO and #+TYP_TODO lines.
  1970. Each keyword can optionally specify a character for fast state selection
  1971. \(in combination with the variable `org-use-fast-todo-selection')
  1972. and specifiers for state change logging, using the same syntax that
  1973. is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says that
  1974. the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1975. indicates to record a time stamp each time this state is selected.
  1976. Each keyword may also specify if a timestamp or a note should be
  1977. recorded when entering or leaving the state, by adding additional
  1978. characters in the parenthesis after the keyword. This looks like this:
  1979. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1980. record only the time of the state change. With X and Y being either
  1981. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1982. Y when leaving the state if and only if the *target* state does not
  1983. define X. You may omit any of the fast-selection key or X or /Y,
  1984. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1985. For backward compatibility, this variable may also be just a list
  1986. of keywords. In this case the interpretation (sequence or type) will be
  1987. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1988. :group 'org-todo
  1989. :group 'org-keywords
  1990. :type '(choice
  1991. (repeat :tag "Old syntax, just keywords"
  1992. (string :tag "Keyword"))
  1993. (repeat :tag "New syntax"
  1994. (cons
  1995. (choice
  1996. :tag "Interpretation"
  1997. ;;Quick and dirty way to see
  1998. ;;`org-todo-interpretations'. This takes the
  1999. ;;place of item arguments
  2000. :convert-widget
  2001. (lambda (widget)
  2002. (widget-put widget
  2003. :args (mapcar
  2004. #'(lambda (x)
  2005. (widget-convert
  2006. (cons 'const x)))
  2007. org-todo-interpretation-widgets))
  2008. widget))
  2009. (repeat
  2010. (string :tag "Keyword"))))))
  2011. (defvar org-todo-keywords-1 nil
  2012. "All TODO and DONE keywords active in a buffer.")
  2013. (make-variable-buffer-local 'org-todo-keywords-1)
  2014. (defvar org-todo-keywords-for-agenda nil)
  2015. (defvar org-done-keywords-for-agenda nil)
  2016. (defvar org-drawers-for-agenda nil)
  2017. (defvar org-todo-keyword-alist-for-agenda nil)
  2018. (defvar org-tag-alist-for-agenda nil)
  2019. (defvar org-agenda-contributing-files nil)
  2020. (defvar org-not-done-keywords nil)
  2021. (make-variable-buffer-local 'org-not-done-keywords)
  2022. (defvar org-done-keywords nil)
  2023. (make-variable-buffer-local 'org-done-keywords)
  2024. (defvar org-todo-heads nil)
  2025. (make-variable-buffer-local 'org-todo-heads)
  2026. (defvar org-todo-sets nil)
  2027. (make-variable-buffer-local 'org-todo-sets)
  2028. (defvar org-todo-log-states nil)
  2029. (make-variable-buffer-local 'org-todo-log-states)
  2030. (defvar org-todo-kwd-alist nil)
  2031. (make-variable-buffer-local 'org-todo-kwd-alist)
  2032. (defvar org-todo-key-alist nil)
  2033. (make-variable-buffer-local 'org-todo-key-alist)
  2034. (defvar org-todo-key-trigger nil)
  2035. (make-variable-buffer-local 'org-todo-key-trigger)
  2036. (defcustom org-todo-interpretation 'sequence
  2037. "Controls how TODO keywords are interpreted.
  2038. This variable is in principle obsolete and is only used for
  2039. backward compatibility, if the interpretation of todo keywords is
  2040. not given already in `org-todo-keywords'. See that variable for
  2041. more information."
  2042. :group 'org-todo
  2043. :group 'org-keywords
  2044. :type '(choice (const sequence)
  2045. (const type)))
  2046. (defcustom org-use-fast-todo-selection t
  2047. "Non-nil means use the fast todo selection scheme with C-c C-t.
  2048. This variable describes if and under what circumstances the cycling
  2049. mechanism for TODO keywords will be replaced by a single-key, direct
  2050. selection scheme.
  2051. When nil, fast selection is never used.
  2052. When the symbol `prefix', it will be used when `org-todo' is called
  2053. with a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and
  2054. `C-u t' in an agenda buffer.
  2055. When t, fast selection is used by default. In this case, the prefix
  2056. argument forces cycling instead.
  2057. In all cases, the special interface is only used if access keys have
  2058. actually been assigned by the user, i.e. if keywords in the configuration
  2059. are followed by a letter in parenthesis, like TODO(t)."
  2060. :group 'org-todo
  2061. :type '(choice
  2062. (const :tag "Never" nil)
  2063. (const :tag "By default" t)
  2064. (const :tag "Only with C-u C-c C-t" prefix)))
  2065. (defcustom org-provide-todo-statistics t
  2066. "Non-nil means update todo statistics after insert and toggle.
  2067. ALL-HEADLINES means update todo statistics by including headlines
  2068. with no TODO keyword as well, counting them as not done.
  2069. A list of TODO keywords means the same, but skip keywords that are
  2070. not in this list.
  2071. When this is set, todo statistics is updated in the parent of the
  2072. current entry each time a todo state is changed."
  2073. :group 'org-todo
  2074. :type '(choice
  2075. (const :tag "Yes, only for TODO entries" t)
  2076. (const :tag "Yes, including all entries" 'all-headlines)
  2077. (repeat :tag "Yes, for TODOs in this list"
  2078. (string :tag "TODO keyword"))
  2079. (other :tag "No TODO statistics" nil)))
  2080. (defcustom org-hierarchical-todo-statistics t
  2081. "Non-nil means TODO statistics covers just direct children.
  2082. When nil, all entries in the subtree are considered.
  2083. This has only an effect if `org-provide-todo-statistics' is set.
  2084. To set this to nil for only a single subtree, use a COOKIE_DATA
  2085. property and include the word \"recursive\" into the value."
  2086. :group 'org-todo
  2087. :type 'boolean)
  2088. (defcustom org-after-todo-state-change-hook nil
  2089. "Hook which is run after the state of a TODO item was changed.
  2090. The new state (a string with a TODO keyword, or nil) is available in the
  2091. Lisp variable `org-state'."
  2092. :group 'org-todo
  2093. :type 'hook)
  2094. (defvar org-blocker-hook nil
  2095. "Hook for functions that are allowed to block a state change.
  2096. Functions in this hook should not modify the buffer.
  2097. Each function gets as its single argument a property list,
  2098. see `org-trigger-hook' for more information about this list.
  2099. If any of the functions in this hook returns nil, the state change
  2100. is blocked.")
  2101. (defvar org-trigger-hook nil
  2102. "Hook for functions that are triggered by a state change.
  2103. Each function gets as its single argument a property list with at
  2104. least the following elements:
  2105. (:type type-of-change :position pos-at-entry-start
  2106. :from old-state :to new-state)
  2107. Depending on the type, more properties may be present.
  2108. This mechanism is currently implemented for:
  2109. TODO state changes
  2110. ------------------
  2111. :type todo-state-change
  2112. :from previous state (keyword as a string), or nil, or a symbol
  2113. 'todo' or 'done', to indicate the general type of state.
  2114. :to new state, like in :from")
  2115. (defcustom org-enforce-todo-dependencies nil
  2116. "Non-nil means undone TODO entries will block switching the parent to DONE.
  2117. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  2118. be blocked if any prior sibling is not yet done.
  2119. Finally, if the parent is blocked because of ordered siblings of its own,
  2120. the child will also be blocked."
  2121. :set (lambda (var val)
  2122. (set var val)
  2123. (if val
  2124. (add-hook 'org-blocker-hook
  2125. 'org-block-todo-from-children-or-siblings-or-parent)
  2126. (remove-hook 'org-blocker-hook
  2127. 'org-block-todo-from-children-or-siblings-or-parent)))
  2128. :group 'org-todo
  2129. :type 'boolean)
  2130. (defcustom org-enforce-todo-checkbox-dependencies nil
  2131. "Non-nil means unchecked boxes will block switching the parent to DONE.
  2132. When this is nil, checkboxes have no influence on switching TODO states.
  2133. When non-nil, you first need to check off all check boxes before the TODO
  2134. entry can be switched to DONE.
  2135. This variable needs to be set before org.el is loaded, and you need to
  2136. restart Emacs after a change to make the change effective. The only way
  2137. to change is while Emacs is running is through the customize interface."
  2138. :set (lambda (var val)
  2139. (set var val)
  2140. (if val
  2141. (add-hook 'org-blocker-hook
  2142. 'org-block-todo-from-checkboxes)
  2143. (remove-hook 'org-blocker-hook
  2144. 'org-block-todo-from-checkboxes)))
  2145. :group 'org-todo
  2146. :type 'boolean)
  2147. (defcustom org-treat-insert-todo-heading-as-state-change nil
  2148. "Non-nil means inserting a TODO heading is treated as state change.
  2149. So when the command \\[org-insert-todo-heading] is used, state change
  2150. logging will apply if appropriate. When nil, the new TODO item will
  2151. be inserted directly, and no logging will take place."
  2152. :group 'org-todo
  2153. :type 'boolean)
  2154. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  2155. "Non-nil means switching TODO states with S-cursor counts as state change.
  2156. This is the default behavior. However, setting this to nil allows a
  2157. convenient way to select a TODO state and bypass any logging associated
  2158. with that."
  2159. :group 'org-todo
  2160. :type 'boolean)
  2161. (defcustom org-todo-state-tags-triggers nil
  2162. "Tag changes that should be triggered by TODO state changes.
  2163. This is a list. Each entry is
  2164. (state-change (tag . flag) .......)
  2165. State-change can be a string with a state, and empty string to indicate the
  2166. state that has no TODO keyword, or it can be one of the symbols `todo'
  2167. or `done', meaning any not-done or done state, respectively."
  2168. :group 'org-todo
  2169. :group 'org-tags
  2170. :type '(repeat
  2171. (cons (choice :tag "When changing to"
  2172. (const :tag "Not-done state" todo)
  2173. (const :tag "Done state" done)
  2174. (string :tag "State"))
  2175. (repeat
  2176. (cons :tag "Tag action"
  2177. (string :tag "Tag")
  2178. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  2179. (defcustom org-log-done nil
  2180. "Information to record when a task moves to the DONE state.
  2181. Possible values are:
  2182. nil Don't add anything, just change the keyword
  2183. time Add a time stamp to the task
  2184. note Prompt for a note and add it with template `org-log-note-headings'
  2185. This option can also be set with on a per-file-basis with
  2186. #+STARTUP: nologdone
  2187. #+STARTUP: logdone
  2188. #+STARTUP: lognotedone
  2189. You can have local logging settings for a subtree by setting the LOGGING
  2190. property to one or more of these keywords."
  2191. :group 'org-todo
  2192. :group 'org-progress
  2193. :type '(choice
  2194. (const :tag "No logging" nil)
  2195. (const :tag "Record CLOSED timestamp" time)
  2196. (const :tag "Record CLOSED timestamp with note." note)))
  2197. ;; Normalize old uses of org-log-done.
  2198. (cond
  2199. ((eq org-log-done t) (setq org-log-done 'time))
  2200. ((and (listp org-log-done) (memq 'done org-log-done))
  2201. (setq org-log-done 'note)))
  2202. (defcustom org-log-reschedule nil
  2203. "Information to record when the scheduling date of a tasks is modified.
  2204. Possible values are:
  2205. nil Don't add anything, just change the date
  2206. time Add a time stamp to the task
  2207. note Prompt for a note and add it with template `org-log-note-headings'
  2208. This option can also be set with on a per-file-basis with
  2209. #+STARTUP: nologreschedule
  2210. #+STARTUP: logreschedule
  2211. #+STARTUP: lognotereschedule"
  2212. :group 'org-todo
  2213. :group 'org-progress
  2214. :type '(choice
  2215. (const :tag "No logging" nil)
  2216. (const :tag "Record timestamp" time)
  2217. (const :tag "Record timestamp with note." note)))
  2218. (defcustom org-log-redeadline nil
  2219. "Information to record when the deadline date of a tasks is modified.
  2220. Possible values are:
  2221. nil Don't add anything, just change the date
  2222. time Add a time stamp to the task
  2223. note Prompt for a note and add it with template `org-log-note-headings'
  2224. This option can also be set with on a per-file-basis with
  2225. #+STARTUP: nologredeadline
  2226. #+STARTUP: logredeadline
  2227. #+STARTUP: lognoteredeadline
  2228. You can have local logging settings for a subtree by setting the LOGGING
  2229. property to one or more of these keywords."
  2230. :group 'org-todo
  2231. :group 'org-progress
  2232. :type '(choice
  2233. (const :tag "No logging" nil)
  2234. (const :tag "Record timestamp" time)
  2235. (const :tag "Record timestamp with note." note)))
  2236. (defcustom org-log-note-clock-out nil
  2237. "Non-nil means record a note when clocking out of an item.
  2238. This can also be configured on a per-file basis by adding one of
  2239. the following lines anywhere in the buffer:
  2240. #+STARTUP: lognoteclock-out
  2241. #+STARTUP: nolognoteclock-out"
  2242. :group 'org-todo
  2243. :group 'org-progress
  2244. :type 'boolean)
  2245. (defcustom org-log-done-with-time t
  2246. "Non-nil means the CLOSED time stamp will contain date and time.
  2247. When nil, only the date will be recorded."
  2248. :group 'org-progress
  2249. :type 'boolean)
  2250. (defcustom org-log-note-headings
  2251. '((done . "CLOSING NOTE %t")
  2252. (state . "State %-12s from %-12S %t")
  2253. (note . "Note taken on %t")
  2254. (reschedule . "Rescheduled from %S on %t")
  2255. (delschedule . "Not scheduled, was %S on %t")
  2256. (redeadline . "New deadline from %S on %t")
  2257. (deldeadline . "Removed deadline, was %S on %t")
  2258. (refile . "Refiled on %t")
  2259. (clock-out . ""))
  2260. "Headings for notes added to entries.
  2261. The value is an alist, with the car being a symbol indicating the note
  2262. context, and the cdr is the heading to be used. The heading may also be the
  2263. empty string.
  2264. %t in the heading will be replaced by a time stamp.
  2265. %T will be an active time stamp instead the default inactive one
  2266. %d will be replaced by a short-format time stamp.
  2267. %D will be replaced by an active short-format time stamp.
  2268. %s will be replaced by the new TODO state, in double quotes.
  2269. %S will be replaced by the old TODO state, in double quotes.
  2270. %u will be replaced by the user name.
  2271. %U will be replaced by the full user name.
  2272. In fact, it is not a good idea to change the `state' entry, because
  2273. agenda log mode depends on the format of these entries."
  2274. :group 'org-todo
  2275. :group 'org-progress
  2276. :type '(list :greedy t
  2277. (cons (const :tag "Heading when closing an item" done) string)
  2278. (cons (const :tag
  2279. "Heading when changing todo state (todo sequence only)"
  2280. state) string)
  2281. (cons (const :tag "Heading when just taking a note" note) string)
  2282. (cons (const :tag "Heading when clocking out" clock-out) string)
  2283. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2284. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2285. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2286. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2287. (cons (const :tag "Heading when refiling" refile) string)))
  2288. (unless (assq 'note org-log-note-headings)
  2289. (push '(note . "%t") org-log-note-headings))
  2290. (defcustom org-log-into-drawer nil
  2291. "Non-nil means insert state change notes and time stamps into a drawer.
  2292. When nil, state changes notes will be inserted after the headline and
  2293. any scheduling and clock lines, but not inside a drawer.
  2294. The value of this variable should be the name of the drawer to use.
  2295. LOGBOOK is proposed as the default drawer for this purpose, you can
  2296. also set this to a string to define the drawer of your choice.
  2297. A value of t is also allowed, representing \"LOGBOOK\".
  2298. A value of t or nil can also be set with on a per-file-basis with
  2299. #+STARTUP: logdrawer
  2300. #+STARTUP: nologdrawer
  2301. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2302. will be ignored.
  2303. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2304. a subtree."
  2305. :group 'org-todo
  2306. :group 'org-progress
  2307. :type '(choice
  2308. (const :tag "Not into a drawer" nil)
  2309. (const :tag "LOGBOOK" t)
  2310. (string :tag "Other")))
  2311. (if (fboundp 'defvaralias)
  2312. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  2313. (defun org-log-into-drawer ()
  2314. "Return the value of `org-log-into-drawer', but let properties overrule.
  2315. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  2316. used instead of the default value."
  2317. (let ((p (org-entry-get nil "LOG_INTO_DRAWER" 'inherit t)))
  2318. (cond
  2319. ((not p) org-log-into-drawer)
  2320. ((equal p "nil") nil)
  2321. ((equal p "t") "LOGBOOK")
  2322. (t p))))
  2323. (defcustom org-log-state-notes-insert-after-drawers nil
  2324. "Non-nil means insert state change notes after any drawers in entry.
  2325. Only the drawers that *immediately* follow the headline and the
  2326. deadline/scheduled line are skipped.
  2327. When nil, insert notes right after the heading and perhaps the line
  2328. with deadline/scheduling if present.
  2329. This variable will have no effect if `org-log-into-drawer' is
  2330. set."
  2331. :group 'org-todo
  2332. :group 'org-progress
  2333. :type 'boolean)
  2334. (defcustom org-log-states-order-reversed t
  2335. "Non-nil means the latest state note will be directly after heading.
  2336. When nil, the state change notes will be ordered according to time.
  2337. This option can also be set with on a per-file-basis with
  2338. #+STARTUP: logstatesreversed
  2339. #+STARTUP: nologstatesreversed"
  2340. :group 'org-todo
  2341. :group 'org-progress
  2342. :type 'boolean)
  2343. (defcustom org-todo-repeat-to-state nil
  2344. "The TODO state to which a repeater should return the repeating task.
  2345. By default this is the first task in a TODO sequence, or the previous state
  2346. in a TODO_TYP set. But you can specify another task here.
  2347. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2348. :group 'org-todo
  2349. :version "24.1"
  2350. :type '(choice (const :tag "Head of sequence" nil)
  2351. (string :tag "Specific state")))
  2352. (defcustom org-log-repeat 'time
  2353. "Non-nil means record moving through the DONE state when triggering repeat.
  2354. An auto-repeating task is immediately switched back to TODO when
  2355. marked DONE. If you are not logging state changes (by adding \"@\"
  2356. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2357. record a closing note, there will be no record of the task moving
  2358. through DONE. This variable forces taking a note anyway.
  2359. nil Don't force a record
  2360. time Record a time stamp
  2361. note Prompt for a note and add it with template `org-log-note-headings'
  2362. This option can also be set with on a per-file-basis with
  2363. #+STARTUP: nologrepeat
  2364. #+STARTUP: logrepeat
  2365. #+STARTUP: lognoterepeat
  2366. You can have local logging settings for a subtree by setting the LOGGING
  2367. property to one or more of these keywords."
  2368. :group 'org-todo
  2369. :group 'org-progress
  2370. :type '(choice
  2371. (const :tag "Don't force a record" nil)
  2372. (const :tag "Force recording the DONE state" time)
  2373. (const :tag "Force recording a note with the DONE state" note)))
  2374. (defgroup org-priorities nil
  2375. "Priorities in Org-mode."
  2376. :tag "Org Priorities"
  2377. :group 'org-todo)
  2378. (defcustom org-enable-priority-commands t
  2379. "Non-nil means priority commands are active.
  2380. When nil, these commands will be disabled, so that you never accidentally
  2381. set a priority."
  2382. :group 'org-priorities
  2383. :type 'boolean)
  2384. (defcustom org-highest-priority ?A
  2385. "The highest priority of TODO items. A character like ?A, ?B etc.
  2386. Must have a smaller ASCII number than `org-lowest-priority'."
  2387. :group 'org-priorities
  2388. :type 'character)
  2389. (defcustom org-lowest-priority ?C
  2390. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2391. Must have a larger ASCII number than `org-highest-priority'."
  2392. :group 'org-priorities
  2393. :type 'character)
  2394. (defcustom org-default-priority ?B
  2395. "The default priority of TODO items.
  2396. This is the priority an item gets if no explicit priority is given.
  2397. When starting to cycle on an empty priority the first step in the cycle
  2398. depends on `org-priority-start-cycle-with-default'. The resulting first
  2399. step priority must not exceed the range from `org-highest-priority' to
  2400. `org-lowest-priority' which means that `org-default-priority' has to be
  2401. in this range exclusive or inclusive the range boundaries. Else the
  2402. first step refuses to set the default and the second will fall back
  2403. to (depending on the command used) the highest or lowest priority."
  2404. :group 'org-priorities
  2405. :type 'character)
  2406. (defcustom org-priority-start-cycle-with-default t
  2407. "Non-nil means start with default priority when starting to cycle.
  2408. When this is nil, the first step in the cycle will be (depending on the
  2409. command used) one higher or lower than the default priority.
  2410. See also `org-default-priority'."
  2411. :group 'org-priorities
  2412. :type 'boolean)
  2413. (defcustom org-get-priority-function nil
  2414. "Function to extract the priority from a string.
  2415. The string is normally the headline. If this is nil Org computes the
  2416. priority from the priority cookie like [#A] in the headline. It returns
  2417. an integer, increasing by 1000 for each priority level.
  2418. The user can set a different function here, which should take a string
  2419. as an argument and return the numeric priority."
  2420. :group 'org-priorities
  2421. :version "24.1"
  2422. :type 'function)
  2423. (defgroup org-time nil
  2424. "Options concerning time stamps and deadlines in Org-mode."
  2425. :tag "Org Time"
  2426. :group 'org)
  2427. (defcustom org-insert-labeled-timestamps-at-point nil
  2428. "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
  2429. When nil, these labeled time stamps are forces into the second line of an
  2430. entry, just after the headline. When scheduling from the global TODO list,
  2431. the time stamp will always be forced into the second line."
  2432. :group 'org-time
  2433. :type 'boolean)
  2434. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  2435. "Formats for `format-time-string' which are used for time stamps.
  2436. It is not recommended to change this constant.")
  2437. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2438. "Number of minutes to round time stamps to.
  2439. These are two values, the first applies when first creating a time stamp.
  2440. The second applies when changing it with the commands `S-up' and `S-down'.
  2441. When changing the time stamp, this means that it will change in steps
  2442. of N minutes, as given by the second value.
  2443. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2444. numbers should be factors of 60, so for example 5, 10, 15.
  2445. When this is larger than 1, you can still force an exact time stamp by using
  2446. a double prefix argument to a time stamp command like `C-c .' or `C-c !',
  2447. and by using a prefix arg to `S-up/down' to specify the exact number
  2448. of minutes to shift."
  2449. :group 'org-time
  2450. :get #'(lambda (var) ; Make sure both elements are there
  2451. (if (integerp (default-value var))
  2452. (list (default-value var) 5)
  2453. (default-value var)))
  2454. :type '(list
  2455. (integer :tag "when inserting times")
  2456. (integer :tag "when modifying times")))
  2457. ;; Normalize old customizations of this variable.
  2458. (when (integerp org-time-stamp-rounding-minutes)
  2459. (setq org-time-stamp-rounding-minutes
  2460. (list org-time-stamp-rounding-minutes
  2461. org-time-stamp-rounding-minutes)))
  2462. (defcustom org-display-custom-times nil
  2463. "Non-nil means overlay custom formats over all time stamps.
  2464. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2465. To turn this on on a per-file basis, insert anywhere in the file:
  2466. #+STARTUP: customtime"
  2467. :group 'org-time
  2468. :set 'set-default
  2469. :type 'sexp)
  2470. (make-variable-buffer-local 'org-display-custom-times)
  2471. (defcustom org-time-stamp-custom-formats
  2472. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2473. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2474. These are overlaid over the default ISO format if the variable
  2475. `org-display-custom-times' is set. Time like %H:%M should be at the
  2476. end of the second format. The custom formats are also honored by export
  2477. commands, if custom time display is turned on at the time of export."
  2478. :group 'org-time
  2479. :type 'sexp)
  2480. (defun org-time-stamp-format (&optional long inactive)
  2481. "Get the right format for a time string."
  2482. (let ((f (if long (cdr org-time-stamp-formats)
  2483. (car org-time-stamp-formats))))
  2484. (if inactive
  2485. (concat "[" (substring f 1 -1) "]")
  2486. f)))
  2487. (defcustom org-time-clocksum-format
  2488. '(:days "%dd " :hours "%d" :require-hours t :minutes ":%02d" :require-minutes t)
  2489. "The format string used when creating CLOCKSUM lines.
  2490. This is also used when Org mode generates a time duration.
  2491. The value can be a single format string containing two
  2492. %-sequences, which will be filled with the number of hours and
  2493. minutes in that order.
  2494. Alternatively, the value can be a plist associating any of the
  2495. keys :years, :months, :weeks, :days, :hours or :minutes with
  2496. format strings. The time duration is formatted using only the
  2497. time components that are needed and concatenating the results.
  2498. If a time unit in absent, it falls back to the next smallest
  2499. unit.
  2500. The keys :require-years, :require-months, :require-days,
  2501. :require-weeks, :require-hours, :require-minutes are also
  2502. meaningful. A non-nil value for these keys indicates that the
  2503. corresponding time component should always be included, even if
  2504. its value is 0.
  2505. For example,
  2506. \(:days \"%dd\" :hours \"%d\" :require-hours t :minutes \":%02d\"
  2507. :require-minutes t)
  2508. means durations longer than a day will be expressed in days,
  2509. hours and minutes, and durations less than a day will always be
  2510. expressed in hours and minutes (even for durations less than an
  2511. hour).
  2512. The value
  2513. \(:days \"%dd\" :minutes \"%dm\")
  2514. means durations longer than a day will be expressed in days and
  2515. minutes, and durations less than a day will be expressed entirely
  2516. in minutes (even for durations longer than an hour)."
  2517. :group 'org-time
  2518. :group 'org-clock
  2519. :version "24.4"
  2520. :package-version '(Org . "8.0")
  2521. :type '(choice (string :tag "Format string")
  2522. (set :tag "Plist"
  2523. (group :inline t (const :tag "Years" :years)
  2524. (string :tag "Format string"))
  2525. (group :inline t
  2526. (const :tag "Always show years" :require-years)
  2527. (const t))
  2528. (group :inline t (const :tag "Months" :months)
  2529. (string :tag "Format string"))
  2530. (group :inline t
  2531. (const :tag "Always show months" :require-months)
  2532. (const t))
  2533. (group :inline t (const :tag "Weeks" :weeks)
  2534. (string :tag "Format string"))
  2535. (group :inline t
  2536. (const :tag "Always show weeks" :require-weeks)
  2537. (const t))
  2538. (group :inline t (const :tag "Days" :days)
  2539. (string :tag "Format string"))
  2540. (group :inline t
  2541. (const :tag "Always show days" :require-days)
  2542. (const t))
  2543. (group :inline t (const :tag "Hours" :hours)
  2544. (string :tag "Format string"))
  2545. (group :inline t
  2546. (const :tag "Always show hours" :require-hours)
  2547. (const t))
  2548. (group :inline t (const :tag "Minutes" :minutes)
  2549. (string :tag "Format string"))
  2550. (group :inline t
  2551. (const :tag "Always show minutes" :require-minutes)
  2552. (const t)))))
  2553. (defcustom org-time-clocksum-use-fractional nil
  2554. "When non-nil, \\[org-clock-display] uses fractional times.
  2555. See `org-time-clocksum-format' for more on time clock formats."
  2556. :group 'org-time
  2557. :group 'org-clock
  2558. :version "24.3"
  2559. :type 'boolean)
  2560. (defcustom org-time-clocksum-use-effort-durations nil
  2561. "When non-nil, \\[org-clock-display] uses effort durations.
  2562. E.g. by default, one day is considered to be a 8 hours effort,
  2563. so a task that has been clocked for 16 hours will be displayed
  2564. as during 2 days in the clock display or in the clocktable.
  2565. See `org-effort-durations' on how to set effort durations
  2566. and `org-time-clocksum-format' for more on time clock formats."
  2567. :group 'org-time
  2568. :group 'org-clock
  2569. :version "24.4"
  2570. :package-version '(Org . "8.0")
  2571. :type 'boolean)
  2572. (defcustom org-time-clocksum-fractional-format "%.2f"
  2573. "The format string used when creating CLOCKSUM lines,
  2574. or when Org mode generates a time duration, if
  2575. `org-time-clocksum-use-fractional' is enabled.
  2576. The value can be a single format string containing one
  2577. %-sequence, which will be filled with the number of hours as
  2578. a float.
  2579. Alternatively, the value can be a plist associating any of the
  2580. keys :years, :months, :weeks, :days, :hours or :minutes with
  2581. a format string. The time duration is formatted using the
  2582. largest time unit which gives a non-zero integer part. If all
  2583. specified formats have zero integer part, the smallest time unit
  2584. is used."
  2585. :group 'org-time
  2586. :type '(choice (string :tag "Format string")
  2587. (set (group :inline t (const :tag "Years" :years)
  2588. (string :tag "Format string"))
  2589. (group :inline t (const :tag "Months" :months)
  2590. (string :tag "Format string"))
  2591. (group :inline t (const :tag "Weeks" :weeks)
  2592. (string :tag "Format string"))
  2593. (group :inline t (const :tag "Days" :days)
  2594. (string :tag "Format string"))
  2595. (group :inline t (const :tag "Hours" :hours)
  2596. (string :tag "Format string"))
  2597. (group :inline t (const :tag "Minutes" :minutes)
  2598. (string :tag "Format string")))))
  2599. (defcustom org-deadline-warning-days 14
  2600. "Number of days before expiration during which a deadline becomes active.
  2601. This variable governs the display in sparse trees and in the agenda.
  2602. When 0 or negative, it means use this number (the absolute value of it)
  2603. even if a deadline has a different individual lead time specified.
  2604. Custom commands can set this variable in the options section."
  2605. :group 'org-time
  2606. :group 'org-agenda-daily/weekly
  2607. :type 'integer)
  2608. (defcustom org-scheduled-delay-days 0
  2609. "Number of days before a scheduled item becomes active.
  2610. This variable governs the display in sparse trees and in the agenda.
  2611. The default value (i.e. 0) means: don't delay scheduled item.
  2612. When negative, it means use this number (the absolute value of it)
  2613. even if a scheduled item has a different individual delay time
  2614. specified.
  2615. Custom commands can set this variable in the options section."
  2616. :group 'org-time
  2617. :group 'org-agenda-daily/weekly
  2618. :version "24.4"
  2619. :package-version '(Org . "8.0")
  2620. :type 'integer)
  2621. (defcustom org-read-date-prefer-future t
  2622. "Non-nil means assume future for incomplete date input from user.
  2623. This affects the following situations:
  2624. 1. The user gives a month but not a year.
  2625. For example, if it is April and you enter \"feb 2\", this will be read
  2626. as Feb 2, *next* year. \"May 5\", however, will be this year.
  2627. 2. The user gives a day, but no month.
  2628. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2629. read this as the third of *next* month. However, if you enter \"17\",
  2630. it will be considered as *this* month.
  2631. If you set this variable to the symbol `time', then also the following
  2632. will work:
  2633. 3. If the user gives a time.
  2634. If the time is before now, it will be interpreted as tomorrow.
  2635. Currently none of this works for ISO week specifications.
  2636. When this option is nil, the current day, month and year will always be
  2637. used as defaults.
  2638. See also `org-agenda-jump-prefer-future'."
  2639. :group 'org-time
  2640. :type '(choice
  2641. (const :tag "Never" nil)
  2642. (const :tag "Check month and day" t)
  2643. (const :tag "Check month, day, and time" time)))
  2644. (defcustom org-agenda-jump-prefer-future 'org-read-date-prefer-future
  2645. "Should the agenda jump command prefer the future for incomplete dates?
  2646. The default is to do the same as configured in `org-read-date-prefer-future'.
  2647. But you can also set a deviating value here.
  2648. This may t or nil, or the symbol `org-read-date-prefer-future'."
  2649. :group 'org-agenda
  2650. :group 'org-time
  2651. :version "24.1"
  2652. :type '(choice
  2653. (const :tag "Use org-read-date-prefer-future"
  2654. org-read-date-prefer-future)
  2655. (const :tag "Never" nil)
  2656. (const :tag "Always" t)))
  2657. (defcustom org-read-date-force-compatible-dates t
  2658. "Should date/time prompt force dates that are guaranteed to work in Emacs?
  2659. Depending on the system Emacs is running on, certain dates cannot
  2660. be represented with the type used internally to represent time.
  2661. Dates between 1970-1-1 and 2038-1-1 can always be represented
  2662. correctly. Some systems allow for earlier dates, some for later,
  2663. some for both. One way to find out it to insert any date into an
  2664. Org buffer, putting the cursor on the year and hitting S-up and
  2665. S-down to test the range.
  2666. When this variable is set to t, the date/time prompt will not let
  2667. you specify dates outside the 1970-2037 range, so it is certain that
  2668. these dates will work in whatever version of Emacs you are
  2669. running, and also that you can move a file from one Emacs implementation
  2670. to another. WHenever Org is forcing the year for you, it will display
  2671. a message and beep.
  2672. When this variable is nil, Org will check if the date is
  2673. representable in the specific Emacs implementation you are using.
  2674. If not, it will force a year, usually the current year, and beep
  2675. to remind you. Currently this setting is not recommended because
  2676. the likelihood that you will open your Org files in an Emacs that
  2677. has limited date range is not negligible.
  2678. A workaround for this problem is to use diary sexp dates for time
  2679. stamps outside of this range."
  2680. :group 'org-time
  2681. :version "24.1"
  2682. :type 'boolean)
  2683. (defcustom org-read-date-display-live t
  2684. "Non-nil means display current interpretation of date prompt live.
  2685. This display will be in an overlay, in the minibuffer."
  2686. :group 'org-time
  2687. :type 'boolean)
  2688. (defcustom org-read-date-popup-calendar t
  2689. "Non-nil means pop up a calendar when prompting for a date.
  2690. In the calendar, the date can be selected with mouse-1. However, the
  2691. minibuffer will also be active, and you can simply enter the date as well.
  2692. When nil, only the minibuffer will be available."
  2693. :group 'org-time
  2694. :type 'boolean)
  2695. (if (fboundp 'defvaralias)
  2696. (defvaralias 'org-popup-calendar-for-date-prompt
  2697. 'org-read-date-popup-calendar))
  2698. (make-obsolete-variable
  2699. 'org-read-date-minibuffer-setup-hook
  2700. "Set `org-read-date-minibuffer-local-map' instead." "24.4")
  2701. (defcustom org-read-date-minibuffer-setup-hook nil
  2702. "Hook to be used to set up keys for the date/time interface.
  2703. Add key definitions to `minibuffer-local-map', which will be a
  2704. temporary copy.
  2705. WARNING: This option is obsolete, you should use
  2706. `org-read-date-minibuffer-local-map' to set up keys."
  2707. :group 'org-time
  2708. :type 'hook)
  2709. (defcustom org-extend-today-until 0
  2710. "The hour when your day really ends. Must be an integer.
  2711. This has influence for the following applications:
  2712. - When switching the agenda to \"today\". It it is still earlier than
  2713. the time given here, the day recognized as TODAY is actually yesterday.
  2714. - When a date is read from the user and it is still before the time given
  2715. here, the current date and time will be assumed to be yesterday, 23:59.
  2716. Also, timestamps inserted in capture templates follow this rule.
  2717. IMPORTANT: This is a feature whose implementation is and likely will
  2718. remain incomplete. Really, it is only here because past midnight seems to
  2719. be the favorite working time of John Wiegley :-)"
  2720. :group 'org-time
  2721. :type 'integer)
  2722. (defcustom org-use-effective-time nil
  2723. "If non-nil, consider `org-extend-today-until' when creating timestamps.
  2724. For example, if `org-extend-today-until' is 8, and it's 4am, then the
  2725. \"effective time\" of any timestamps between midnight and 8am will be
  2726. 23:59 of the previous day."
  2727. :group 'org-time
  2728. :version "24.1"
  2729. :type 'boolean)
  2730. (defcustom org-use-last-clock-out-time-as-effective-time nil
  2731. "When non-nil, use the last clock out time for `org-todo'.
  2732. Note that this option has precedence over the combined use of
  2733. `org-use-effective-time' and `org-extend-today-until'."
  2734. :group 'org-time
  2735. :version "24.4"
  2736. :package-version '(Org . "8.0")
  2737. :type 'boolean)
  2738. (defcustom org-edit-timestamp-down-means-later nil
  2739. "Non-nil means S-down will increase the time in a time stamp.
  2740. When nil, S-up will increase."
  2741. :group 'org-time
  2742. :type 'boolean)
  2743. (defcustom org-calendar-follow-timestamp-change t
  2744. "Non-nil means make the calendar window follow timestamp changes.
  2745. When a timestamp is modified and the calendar window is visible, it will be
  2746. moved to the new date."
  2747. :group 'org-time
  2748. :type 'boolean)
  2749. (defgroup org-tags nil
  2750. "Options concerning tags in Org-mode."
  2751. :tag "Org Tags"
  2752. :group 'org)
  2753. (defcustom org-tag-alist nil
  2754. "List of tags allowed in Org-mode files.
  2755. When this list is nil, Org-mode will base TAG input on what is already in the
  2756. buffer.
  2757. The value of this variable is an alist, the car of each entry must be a
  2758. keyword as a string, the cdr may be a character that is used to select
  2759. that tag through the fast-tag-selection interface.
  2760. See the manual for details."
  2761. :group 'org-tags
  2762. :type '(repeat
  2763. (choice
  2764. (cons (string :tag "Tag name")
  2765. (character :tag "Access char"))
  2766. (list :tag "Start radio group"
  2767. (const :startgroup)
  2768. (option (string :tag "Group description")))
  2769. (list :tag "End radio group"
  2770. (const :endgroup)
  2771. (option (string :tag "Group description")))
  2772. (const :tag "New line" (:newline)))))
  2773. (defcustom org-tag-persistent-alist nil
  2774. "List of tags that will always appear in all Org-mode files.
  2775. This is in addition to any in buffer settings or customizations
  2776. of `org-tag-alist'.
  2777. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  2778. The value of this variable is an alist, the car of each entry must be a
  2779. keyword as a string, the cdr may be a character that is used to select
  2780. that tag through the fast-tag-selection interface.
  2781. See the manual for details.
  2782. To disable these tags on a per-file basis, insert anywhere in the file:
  2783. #+STARTUP: noptag"
  2784. :group 'org-tags
  2785. :type '(repeat
  2786. (choice
  2787. (cons (string :tag "Tag name")
  2788. (character :tag "Access char"))
  2789. (const :tag "Start radio group" (:startgroup))
  2790. (const :tag "End radio group" (:endgroup))
  2791. (const :tag "New line" (:newline)))))
  2792. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  2793. "If non-nil, always offer completion for all tags of all agenda files.
  2794. Instead of customizing this variable directly, you might want to
  2795. set it locally for capture buffers, because there no list of
  2796. tags in that file can be created dynamically (there are none).
  2797. (add-hook 'org-capture-mode-hook
  2798. (lambda ()
  2799. (set (make-local-variable
  2800. 'org-complete-tags-always-offer-all-agenda-tags)
  2801. t)))"
  2802. :group 'org-tags
  2803. :version "24.1"
  2804. :type 'boolean)
  2805. (defvar org-file-tags nil
  2806. "List of tags that can be inherited by all entries in the file.
  2807. The tags will be inherited if the variable `org-use-tag-inheritance'
  2808. says they should be.
  2809. This variable is populated from #+FILETAGS lines.")
  2810. (defcustom org-use-fast-tag-selection 'auto
  2811. "Non-nil means use fast tag selection scheme.
  2812. This is a special interface to select and deselect tags with single keys.
  2813. When nil, fast selection is never used.
  2814. When the symbol `auto', fast selection is used if and only if selection
  2815. characters for tags have been configured, either through the variable
  2816. `org-tag-alist' or through a #+TAGS line in the buffer.
  2817. When t, fast selection is always used and selection keys are assigned
  2818. automatically if necessary."
  2819. :group 'org-tags
  2820. :type '(choice
  2821. (const :tag "Always" t)
  2822. (const :tag "Never" nil)
  2823. (const :tag "When selection characters are configured" 'auto)))
  2824. (defcustom org-fast-tag-selection-single-key nil
  2825. "Non-nil means fast tag selection exits after first change.
  2826. When nil, you have to press RET to exit it.
  2827. During fast tag selection, you can toggle this flag with `C-c'.
  2828. This variable can also have the value `expert'. In this case, the window
  2829. displaying the tags menu is not even shown, until you press C-c again."
  2830. :group 'org-tags
  2831. :type '(choice
  2832. (const :tag "No" nil)
  2833. (const :tag "Yes" t)
  2834. (const :tag "Expert" expert)))
  2835. (defvar org-fast-tag-selection-include-todo nil
  2836. "Non-nil means fast tags selection interface will also offer TODO states.
  2837. This is an undocumented feature, you should not rely on it.")
  2838. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2839. "The column to which tags should be indented in a headline.
  2840. If this number is positive, it specifies the column. If it is negative,
  2841. it means that the tags should be flushright to that column. For example,
  2842. -80 works well for a normal 80 character screen.
  2843. When 0, place tags directly after headline text, with only one space in
  2844. between."
  2845. :group 'org-tags
  2846. :type 'integer)
  2847. (defcustom org-auto-align-tags t
  2848. "Non-nil keeps tags aligned when modifying headlines.
  2849. Some operations (i.e. demoting) change the length of a headline and
  2850. therefore shift the tags around. With this option turned on, after
  2851. each such operation the tags are again aligned to `org-tags-column'."
  2852. :group 'org-tags
  2853. :type 'boolean)
  2854. (defcustom org-use-tag-inheritance t
  2855. "Non-nil means tags in levels apply also for sublevels.
  2856. When nil, only the tags directly given in a specific line apply there.
  2857. This may also be a list of tags that should be inherited, or a regexp that
  2858. matches tags that should be inherited. Additional control is possible
  2859. with the variable `org-tags-exclude-from-inheritance' which gives an
  2860. explicit list of tags to be excluded from inheritance, even if the value of
  2861. `org-use-tag-inheritance' would select it for inheritance.
  2862. If this option is t, a match early-on in a tree can lead to a large
  2863. number of matches in the subtree when constructing the agenda or creating
  2864. a sparse tree. If you only want to see the first match in a tree during
  2865. a search, check out the variable `org-tags-match-list-sublevels'."
  2866. :group 'org-tags
  2867. :type '(choice
  2868. (const :tag "Not" nil)
  2869. (const :tag "Always" t)
  2870. (repeat :tag "Specific tags" (string :tag "Tag"))
  2871. (regexp :tag "Tags matched by regexp")))
  2872. (defcustom org-tags-exclude-from-inheritance nil
  2873. "List of tags that should never be inherited.
  2874. This is a way to exclude a few tags from inheritance. For way to do
  2875. the opposite, to actively allow inheritance for selected tags,
  2876. see the variable `org-use-tag-inheritance'."
  2877. :group 'org-tags
  2878. :type '(repeat (string :tag "Tag")))
  2879. (defun org-tag-inherit-p (tag)
  2880. "Check if TAG is one that should be inherited."
  2881. (cond
  2882. ((member tag org-tags-exclude-from-inheritance) nil)
  2883. ((eq org-use-tag-inheritance t) t)
  2884. ((not org-use-tag-inheritance) nil)
  2885. ((stringp org-use-tag-inheritance)
  2886. (string-match org-use-tag-inheritance tag))
  2887. ((listp org-use-tag-inheritance)
  2888. (member tag org-use-tag-inheritance))
  2889. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2890. (defcustom org-tags-match-list-sublevels t
  2891. "Non-nil means list also sublevels of headlines matching a search.
  2892. This variable applies to tags/property searches, and also to stuck
  2893. projects because this search is based on a tags match as well.
  2894. When set to the symbol `indented', sublevels are indented with
  2895. leading dots.
  2896. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2897. the sublevels of a headline matching a tag search often also match
  2898. the same search. Listing all of them can create very long lists.
  2899. Setting this variable to nil causes subtrees of a match to be skipped.
  2900. This variable is semi-obsolete and probably should always be true. It
  2901. is better to limit inheritance to certain tags using the variables
  2902. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2903. :group 'org-tags
  2904. :type '(choice
  2905. (const :tag "No, don't list them" nil)
  2906. (const :tag "Yes, do list them" t)
  2907. (const :tag "List them, indented with leading dots" indented)))
  2908. (defcustom org-tags-sort-function nil
  2909. "When set, tags are sorted using this function as a comparator."
  2910. :group 'org-tags
  2911. :type '(choice
  2912. (const :tag "No sorting" nil)
  2913. (const :tag "Alphabetical" string<)
  2914. (const :tag "Reverse alphabetical" string>)
  2915. (function :tag "Custom function" nil)))
  2916. (defvar org-tags-history nil
  2917. "History of minibuffer reads for tags.")
  2918. (defvar org-last-tags-completion-table nil
  2919. "The last used completion table for tags.")
  2920. (defvar org-after-tags-change-hook nil
  2921. "Hook that is run after the tags in a line have changed.")
  2922. (defgroup org-properties nil
  2923. "Options concerning properties in Org-mode."
  2924. :tag "Org Properties"
  2925. :group 'org)
  2926. (defcustom org-property-format "%-10s %s"
  2927. "How property key/value pairs should be formatted by `indent-line'.
  2928. When `indent-line' hits a property definition, it will format the line
  2929. according to this format, mainly to make sure that the values are
  2930. lined-up with respect to each other."
  2931. :group 'org-properties
  2932. :type 'string)
  2933. (defcustom org-properties-postprocess-alist nil
  2934. "Alist of properties and functions to adjust inserted values.
  2935. Elements of this alist must be of the form
  2936. ([string] [function])
  2937. where [string] must be a property name and [function] must be a
  2938. lambda expression: this lambda expression must take one argument,
  2939. the value to adjust, and return the new value as a string.
  2940. For example, this element will allow the property \"Remaining\"
  2941. to be updated wrt the relation between the \"Effort\" property
  2942. and the clock summary:
  2943. ((\"Remaining\" (lambda(value)
  2944. (let ((clocksum (org-clock-sum-current-item))
  2945. (effort (org-duration-string-to-minutes
  2946. (org-entry-get (point) \"Effort\"))))
  2947. (org-minutes-to-clocksum-string (- effort clocksum))))))"
  2948. :group 'org-properties
  2949. :version "24.1"
  2950. :type '(alist :key-type (string :tag "Property")
  2951. :value-type (function :tag "Function")))
  2952. (defcustom org-use-property-inheritance nil
  2953. "Non-nil means properties apply also for sublevels.
  2954. This setting is chiefly used during property searches. Turning it on can
  2955. cause significant overhead when doing a search, which is why it is not
  2956. on by default.
  2957. When nil, only the properties directly given in the current entry count.
  2958. When t, every property is inherited. The value may also be a list of
  2959. properties that should have inheritance, or a regular expression matching
  2960. properties that should be inherited.
  2961. However, note that some special properties use inheritance under special
  2962. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2963. and the properties ending in \"_ALL\" when they are used as descriptor
  2964. for valid values of a property.
  2965. Note for programmers:
  2966. When querying an entry with `org-entry-get', you can control if inheritance
  2967. should be used. By default, `org-entry-get' looks only at the local
  2968. properties. You can request inheritance by setting the inherit argument
  2969. to t (to force inheritance) or to `selective' (to respect the setting
  2970. in this variable)."
  2971. :group 'org-properties
  2972. :type '(choice
  2973. (const :tag "Not" nil)
  2974. (const :tag "Always" t)
  2975. (repeat :tag "Specific properties" (string :tag "Property"))
  2976. (regexp :tag "Properties matched by regexp")))
  2977. (defun org-property-inherit-p (property)
  2978. "Check if PROPERTY is one that should be inherited."
  2979. (cond
  2980. ((eq org-use-property-inheritance t) t)
  2981. ((not org-use-property-inheritance) nil)
  2982. ((stringp org-use-property-inheritance)
  2983. (string-match org-use-property-inheritance property))
  2984. ((listp org-use-property-inheritance)
  2985. (member property org-use-property-inheritance))
  2986. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2987. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2988. "The default column format, if no other format has been defined.
  2989. This variable can be set on the per-file basis by inserting a line
  2990. #+COLUMNS: %25ITEM ....."
  2991. :group 'org-properties
  2992. :type 'string)
  2993. (defcustom org-columns-ellipses ".."
  2994. "The ellipses to be used when a field in column view is truncated.
  2995. When this is the empty string, as many characters as possible are shown,
  2996. but then there will be no visual indication that the field has been truncated.
  2997. When this is a string of length N, the last N characters of a truncated
  2998. field are replaced by this string. If the column is narrower than the
  2999. ellipses string, only part of the ellipses string will be shown."
  3000. :group 'org-properties
  3001. :type 'string)
  3002. (defcustom org-columns-modify-value-for-display-function nil
  3003. "Function that modifies values for display in column view.
  3004. For example, it can be used to cut out a certain part from a time stamp.
  3005. The function must take 2 arguments:
  3006. column-title The title of the column (*not* the property name)
  3007. value The value that should be modified.
  3008. The function should return the value that should be displayed,
  3009. or nil if the normal value should be used."
  3010. :group 'org-properties
  3011. :type 'function)
  3012. (defcustom org-effort-property "Effort"
  3013. "The property that is being used to keep track of effort estimates.
  3014. Effort estimates given in this property need to have the format H:MM."
  3015. :group 'org-properties
  3016. :group 'org-progress
  3017. :type '(string :tag "Property"))
  3018. (defconst org-global-properties-fixed
  3019. '(("VISIBILITY_ALL" . "folded children content all")
  3020. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  3021. "List of property/value pairs that can be inherited by any entry.
  3022. These are fixed values, for the preset properties. The user variable
  3023. that can be used to add to this list is `org-global-properties'.
  3024. The entries in this list are cons cells where the car is a property
  3025. name and cdr is a string with the value. If the value represents
  3026. multiple items like an \"_ALL\" property, separate the items by
  3027. spaces.")
  3028. (defcustom org-global-properties nil
  3029. "List of property/value pairs that can be inherited by any entry.
  3030. This list will be combined with the constant `org-global-properties-fixed'.
  3031. The entries in this list are cons cells where the car is a property
  3032. name and cdr is a string with the value.
  3033. You can set buffer-local values for the same purpose in the variable
  3034. `org-file-properties' this by adding lines like
  3035. #+PROPERTY: NAME VALUE"
  3036. :group 'org-properties
  3037. :type '(repeat
  3038. (cons (string :tag "Property")
  3039. (string :tag "Value"))))
  3040. (defvar org-file-properties nil
  3041. "List of property/value pairs that can be inherited by any entry.
  3042. Valid for the current buffer.
  3043. This variable is populated from #+PROPERTY lines.")
  3044. (make-variable-buffer-local 'org-file-properties)
  3045. (defgroup org-agenda nil
  3046. "Options concerning agenda views in Org-mode."
  3047. :tag "Org Agenda"
  3048. :group 'org)
  3049. (defvar org-category nil
  3050. "Variable used by org files to set a category for agenda display.
  3051. Such files should use a file variable to set it, for example
  3052. # -*- mode: org; org-category: \"ELisp\"
  3053. or contain a special line
  3054. #+CATEGORY: ELisp
  3055. If the file does not specify a category, then file's base name
  3056. is used instead.")
  3057. (make-variable-buffer-local 'org-category)
  3058. (put 'org-category 'safe-local-variable #'(lambda (x) (or (symbolp x) (stringp x))))
  3059. (defcustom org-agenda-files nil
  3060. "The files to be used for agenda display.
  3061. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  3062. \\[org-remove-file]. You can also use customize to edit the list.
  3063. If an entry is a directory, all files in that directory that are matched by
  3064. `org-agenda-file-regexp' will be part of the file list.
  3065. If the value of the variable is not a list but a single file name, then
  3066. the list of agenda files is actually stored and maintained in that file, one
  3067. agenda file per line. In this file paths can be given relative to
  3068. `org-directory'. Tilde expansion and environment variable substitution
  3069. are also made."
  3070. :group 'org-agenda
  3071. :type '(choice
  3072. (repeat :tag "List of files and directories" file)
  3073. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  3074. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  3075. "Regular expression to match files for `org-agenda-files'.
  3076. If any element in the list in that variable contains a directory instead
  3077. of a normal file, all files in that directory that are matched by this
  3078. regular expression will be included."
  3079. :group 'org-agenda
  3080. :type 'regexp)
  3081. (defcustom org-agenda-text-search-extra-files nil
  3082. "List of extra files to be searched by text search commands.
  3083. These files will be search in addition to the agenda files by the
  3084. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  3085. Note that these files will only be searched for text search commands,
  3086. not for the other agenda views like todo lists, tag searches or the weekly
  3087. agenda. This variable is intended to list notes and possibly archive files
  3088. that should also be searched by these two commands.
  3089. In fact, if the first element in the list is the symbol `agenda-archives',
  3090. than all archive files of all agenda files will be added to the search
  3091. scope."
  3092. :group 'org-agenda
  3093. :type '(set :greedy t
  3094. (const :tag "Agenda Archives" agenda-archives)
  3095. (repeat :inline t (file))))
  3096. (if (fboundp 'defvaralias)
  3097. (defvaralias 'org-agenda-multi-occur-extra-files
  3098. 'org-agenda-text-search-extra-files))
  3099. (defcustom org-agenda-skip-unavailable-files nil
  3100. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  3101. A nil value means to remove them, after a query, from the list."
  3102. :group 'org-agenda
  3103. :type 'boolean)
  3104. (defcustom org-calendar-to-agenda-key [?c]
  3105. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  3106. The command `org-calendar-goto-agenda' will be bound to this key. The
  3107. default is the character `c' because then `c' can be used to switch back and
  3108. forth between agenda and calendar."
  3109. :group 'org-agenda
  3110. :type 'sexp)
  3111. (defcustom org-calendar-insert-diary-entry-key [?i]
  3112. "The key to be installed in `calendar-mode-map' for adding diary entries.
  3113. This option is irrelevant until `org-agenda-diary-file' has been configured
  3114. to point to an Org-mode file. When that is the case, the command
  3115. `org-agenda-diary-entry' will be bound to the key given here, by default
  3116. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  3117. if you want to continue doing this, you need to change this to a different
  3118. key."
  3119. :group 'org-agenda
  3120. :type 'sexp)
  3121. (defcustom org-agenda-diary-file 'diary-file
  3122. "File to which to add new entries with the `i' key in agenda and calendar.
  3123. When this is the symbol `diary-file', the functionality in the Emacs
  3124. calendar will be used to add entries to the `diary-file'. But when this
  3125. points to a file, `org-agenda-diary-entry' will be used instead."
  3126. :group 'org-agenda
  3127. :type '(choice
  3128. (const :tag "The standard Emacs diary file" diary-file)
  3129. (file :tag "Special Org file diary entries")))
  3130. (eval-after-load "calendar"
  3131. '(progn
  3132. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  3133. 'org-calendar-goto-agenda)
  3134. (add-hook 'calendar-mode-hook
  3135. (lambda ()
  3136. (unless (eq org-agenda-diary-file 'diary-file)
  3137. (define-key calendar-mode-map
  3138. org-calendar-insert-diary-entry-key
  3139. 'org-agenda-diary-entry))))))
  3140. (defgroup org-latex nil
  3141. "Options for embedding LaTeX code into Org-mode."
  3142. :tag "Org LaTeX"
  3143. :group 'org)
  3144. (defcustom org-format-latex-options
  3145. '(:foreground default :background default :scale 1.0
  3146. :html-foreground "Black" :html-background "Transparent"
  3147. :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  3148. "Options for creating images from LaTeX fragments.
  3149. This is a property list with the following properties:
  3150. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  3151. `default' means use the foreground of the default face.
  3152. `auto' means use the foreground from the text face.
  3153. :background the background color, or \"Transparent\".
  3154. `default' means use the background of the default face.
  3155. `auto' means use the background from the text face.
  3156. :scale a scaling factor for the size of the images, to get more pixels
  3157. :html-foreground, :html-background, :html-scale
  3158. the same numbers for HTML export.
  3159. :matchers a list indicating which matchers should be used to
  3160. find LaTeX fragments. Valid members of this list are:
  3161. \"begin\" find environments
  3162. \"$1\" find single characters surrounded by $.$
  3163. \"$\" find math expressions surrounded by $...$
  3164. \"$$\" find math expressions surrounded by $$....$$
  3165. \"\\(\" find math expressions surrounded by \\(...\\)
  3166. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  3167. :group 'org-latex
  3168. :type 'plist)
  3169. (defcustom org-format-latex-signal-error t
  3170. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  3171. When nil, just push out a message."
  3172. :group 'org-latex
  3173. :version "24.1"
  3174. :type 'boolean)
  3175. (defcustom org-latex-to-mathml-jar-file nil
  3176. "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
  3177. Use this to specify additional executable file say a jar file.
  3178. When using MathToWeb as the converter, specify the full-path to
  3179. your mathtoweb.jar file."
  3180. :group 'org-latex
  3181. :version "24.1"
  3182. :type '(choice
  3183. (const :tag "None" nil)
  3184. (file :tag "JAR file" :must-match t)))
  3185. (defcustom org-latex-to-mathml-convert-command nil
  3186. "Command to convert LaTeX fragments to MathML.
  3187. Replace format-specifiers in the command as noted below and use
  3188. `shell-command' to convert LaTeX to MathML.
  3189. %j: Executable file in fully expanded form as specified by
  3190. `org-latex-to-mathml-jar-file'.
  3191. %I: Input LaTeX file in fully expanded form
  3192. %o: Output MathML file
  3193. This command is used by `org-create-math-formula'.
  3194. When using MathToWeb as the converter, set this to
  3195. \"java -jar %j -unicode -force -df %o %I\"."
  3196. :group 'org-latex
  3197. :version "24.1"
  3198. :type '(choice
  3199. (const :tag "None" nil)
  3200. (string :tag "\nShell command")))
  3201. (defcustom org-latex-create-formula-image-program 'dvipng
  3202. "Program to convert LaTeX fragments with.
  3203. dvipng Process the LaTeX fragments to dvi file, then convert
  3204. dvi files to png files using dvipng.
  3205. This will also include processing of non-math environments.
  3206. imagemagick Convert the LaTeX fragments to pdf files and use imagemagick
  3207. to convert pdf files to png files"
  3208. :group 'org-latex
  3209. :version "24.1"
  3210. :type '(choice
  3211. (const :tag "dvipng" dvipng)
  3212. (const :tag "imagemagick" imagemagick)))
  3213. (defcustom org-latex-preview-ltxpng-directory "ltxpng/"
  3214. "Path to store latex preview images.
  3215. A relative path here creates many directories relative to the
  3216. processed org files paths. An absolute path puts all preview
  3217. images at the same place."
  3218. :group 'org-latex
  3219. :version "24.3"
  3220. :type 'string)
  3221. (defun org-format-latex-mathml-available-p ()
  3222. "Return t if `org-latex-to-mathml-convert-command' is usable."
  3223. (save-match-data
  3224. (when (and (boundp 'org-latex-to-mathml-convert-command)
  3225. org-latex-to-mathml-convert-command)
  3226. (let ((executable (car (split-string
  3227. org-latex-to-mathml-convert-command))))
  3228. (when (executable-find executable)
  3229. (if (string-match
  3230. "%j" org-latex-to-mathml-convert-command)
  3231. (file-readable-p org-latex-to-mathml-jar-file)
  3232. t))))))
  3233. (defcustom org-format-latex-header "\\documentclass{article}
  3234. \\usepackage[usenames]{color}
  3235. \\usepackage{amsmath}
  3236. \\usepackage[mathscr]{eucal}
  3237. \\pagestyle{empty} % do not remove
  3238. \[PACKAGES]
  3239. \[DEFAULT-PACKAGES]
  3240. % The settings below are copied from fullpage.sty
  3241. \\setlength{\\textwidth}{\\paperwidth}
  3242. \\addtolength{\\textwidth}{-3cm}
  3243. \\setlength{\\oddsidemargin}{1.5cm}
  3244. \\addtolength{\\oddsidemargin}{-2.54cm}
  3245. \\setlength{\\evensidemargin}{\\oddsidemargin}
  3246. \\setlength{\\textheight}{\\paperheight}
  3247. \\addtolength{\\textheight}{-\\headheight}
  3248. \\addtolength{\\textheight}{-\\headsep}
  3249. \\addtolength{\\textheight}{-\\footskip}
  3250. \\addtolength{\\textheight}{-3cm}
  3251. \\setlength{\\topmargin}{1.5cm}
  3252. \\addtolength{\\topmargin}{-2.54cm}"
  3253. "The document header used for processing LaTeX fragments.
  3254. It is imperative that this header make sure that no page number
  3255. appears on the page. The package defined in the variables
  3256. `org-latex-default-packages-alist' and `org-latex-packages-alist'
  3257. will either replace the placeholder \"[PACKAGES]\" in this
  3258. header, or they will be appended."
  3259. :group 'org-latex
  3260. :type 'string)
  3261. (defun org-set-packages-alist (var val)
  3262. "Set the packages alist and make sure it has 3 elements per entry."
  3263. (set var (mapcar (lambda (x)
  3264. (if (and (consp x) (= (length x) 2))
  3265. (list (car x) (nth 1 x) t)
  3266. x))
  3267. val)))
  3268. (defun org-get-packages-alist (var)
  3269. "Get the packages alist and make sure it has 3 elements per entry."
  3270. (mapcar (lambda (x)
  3271. (if (and (consp x) (= (length x) 2))
  3272. (list (car x) (nth 1 x) t)
  3273. x))
  3274. (default-value var)))
  3275. (defcustom org-latex-default-packages-alist
  3276. '(("AUTO" "inputenc" t)
  3277. ("T1" "fontenc" t)
  3278. ("" "fixltx2e" nil)
  3279. ("" "graphicx" t)
  3280. ("" "longtable" nil)
  3281. ("" "float" nil)
  3282. ("" "wrapfig" nil)
  3283. ("" "soul" t)
  3284. ("" "textcomp" t)
  3285. ("" "marvosym" t)
  3286. ("" "wasysym" t)
  3287. ("" "latexsym" t)
  3288. ("" "amssymb" t)
  3289. ("" "hyperref" nil)
  3290. "\\tolerance=1000")
  3291. "Alist of default packages to be inserted in the header.
  3292. Change this only if one of the packages here causes an
  3293. incompatibility with another package you are using.
  3294. The packages in this list are needed by one part or another of
  3295. Org mode to function properly:
  3296. - inputenc, fontenc: for basic font and character selection
  3297. - textcomp, marvosymb, wasysym, latexsym, amssym: for various
  3298. symbols used for interpreting the entities in `org-entities'.
  3299. You can skip some of these packages if you don't use any of the
  3300. symbols in it.
  3301. - graphicx: for including images
  3302. - float, wrapfig: for figure placement
  3303. - longtable: for long tables
  3304. - hyperref: for cross references
  3305. Therefore you should not modify this variable unless you know
  3306. what you are doing. The one reason to change it anyway is that
  3307. you might be loading some other package that conflicts with one
  3308. of the default packages. Each cell is of the format
  3309. \( \"options\" \"package\" snippet-flag). If SNIPPET-FLAG is t,
  3310. the package also needs to be included when compiling LaTeX
  3311. snippets into images for inclusion into non-LaTeX output."
  3312. :group 'org-latex
  3313. :group 'org-export-latex
  3314. :set 'org-set-packages-alist
  3315. :get 'org-get-packages-alist
  3316. :version "24.1"
  3317. :type '(repeat
  3318. (choice
  3319. (list :tag "options/package pair"
  3320. (string :tag "options")
  3321. (string :tag "package")
  3322. (boolean :tag "Snippet"))
  3323. (string :tag "A line of LaTeX"))))
  3324. (defcustom org-latex-packages-alist nil
  3325. "Alist of packages to be inserted in every LaTeX header.
  3326. These will be inserted after `org-latex-default-packages-alist'.
  3327. Each cell is of the format:
  3328. \(\"options\" \"package\" snippet-flag)
  3329. SNIPPET-FLAG, when t, indicates that this package is also needed
  3330. when turning LaTeX snippets into images for inclusion into
  3331. non-LaTeX output.
  3332. Make sure that you only list packages here which:
  3333. - you want in every file
  3334. - do not conflict with the setup in `org-format-latex-header'.
  3335. - do not conflict with the default packages in
  3336. `org-latex-default-packages-alist'."
  3337. :group 'org-latex
  3338. :group 'org-export-latex
  3339. :set 'org-set-packages-alist
  3340. :get 'org-get-packages-alist
  3341. :type '(repeat
  3342. (choice
  3343. (list :tag "options/package pair"
  3344. (string :tag "options")
  3345. (string :tag "package")
  3346. (boolean :tag "Snippet"))
  3347. (string :tag "A line of LaTeX"))))
  3348. (defgroup org-appearance nil
  3349. "Settings for Org-mode appearance."
  3350. :tag "Org Appearance"
  3351. :group 'org)
  3352. (defcustom org-level-color-stars-only nil
  3353. "Non-nil means fontify only the stars in each headline.
  3354. When nil, the entire headline is fontified.
  3355. Changing it requires restart of `font-lock-mode' to become effective
  3356. also in regions already fontified."
  3357. :group 'org-appearance
  3358. :type 'boolean)
  3359. (defcustom org-hide-leading-stars nil
  3360. "Non-nil means hide the first N-1 stars in a headline.
  3361. This works by using the face `org-hide' for these stars. This
  3362. face is white for a light background, and black for a dark
  3363. background. You may have to customize the face `org-hide' to
  3364. make this work.
  3365. Changing it requires restart of `font-lock-mode' to become effective
  3366. also in regions already fontified.
  3367. You may also set this on a per-file basis by adding one of the following
  3368. lines to the buffer:
  3369. #+STARTUP: hidestars
  3370. #+STARTUP: showstars"
  3371. :group 'org-appearance
  3372. :type 'boolean)
  3373. (defcustom org-hidden-keywords nil
  3374. "List of symbols corresponding to keywords to be hidden the org buffer.
  3375. For example, a value '(title) for this list will make the document's title
  3376. appear in the buffer without the initial #+TITLE: keyword."
  3377. :group 'org-appearance
  3378. :version "24.1"
  3379. :type '(set (const :tag "#+AUTHOR" author)
  3380. (const :tag "#+DATE" date)
  3381. (const :tag "#+EMAIL" email)
  3382. (const :tag "#+TITLE" title)))
  3383. (defcustom org-custom-properties nil
  3384. "List of properties (as strings) with a special meaning.
  3385. The default use of these custom properties is to let the user
  3386. hide them with `org-toggle-custom-properties-visibility'."
  3387. :group 'org-properties
  3388. :group 'org-appearance
  3389. :version "24.3"
  3390. :type '(repeat (string :tag "Property Name")))
  3391. (defcustom org-fontify-done-headline nil
  3392. "Non-nil means change the face of a headline if it is marked DONE.
  3393. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3394. When this is non-nil, the headline after the keyword is set to the
  3395. `org-headline-done' as an additional indication."
  3396. :group 'org-appearance
  3397. :type 'boolean)
  3398. (defcustom org-fontify-emphasized-text t
  3399. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3400. Changing this variable requires a restart of Emacs to take effect."
  3401. :group 'org-appearance
  3402. :type 'boolean)
  3403. (defcustom org-fontify-whole-heading-line nil
  3404. "Non-nil means fontify the whole line for headings.
  3405. This is useful when setting a background color for the
  3406. org-level-* faces."
  3407. :group 'org-appearance
  3408. :type 'boolean)
  3409. (defcustom org-highlight-latex-and-related nil
  3410. "Non-nil means highlight LaTeX related syntax in the buffer.
  3411. When non nil, the value should be a list containing any of the
  3412. following symbols:
  3413. `latex' Highlight LaTeX snippets and environments.
  3414. `script' Highlight subscript and superscript.
  3415. `entities' Highlight entities."
  3416. :group 'org-appearance
  3417. :version "24.4"
  3418. :package-version '(Org . "8.0")
  3419. :type '(choice
  3420. (const :tag "No highlighting" nil)
  3421. (set :greedy t :tag "Highlight"
  3422. (const :tag "LaTeX snippets and environments" latex)
  3423. (const :tag "Subscript and superscript" script)
  3424. (const :tag "Entities" entities))))
  3425. (defcustom org-hide-emphasis-markers nil
  3426. "Non-nil mean font-lock should hide the emphasis marker characters."
  3427. :group 'org-appearance
  3428. :type 'boolean)
  3429. (defcustom org-pretty-entities nil
  3430. "Non-nil means show entities as UTF8 characters.
  3431. When nil, the \\name form remains in the buffer."
  3432. :group 'org-appearance
  3433. :version "24.1"
  3434. :type 'boolean)
  3435. (defcustom org-pretty-entities-include-sub-superscripts t
  3436. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  3437. :group 'org-appearance
  3438. :version "24.1"
  3439. :type 'boolean)
  3440. (defvar org-emph-re nil
  3441. "Regular expression for matching emphasis.
  3442. After a match, the match groups contain these elements:
  3443. 0 The match of the full regular expression, including the characters
  3444. before and after the proper match
  3445. 1 The character before the proper match, or empty at beginning of line
  3446. 2 The proper match, including the leading and trailing markers
  3447. 3 The leading marker like * or /, indicating the type of highlighting
  3448. 4 The text between the emphasis markers, not including the markers
  3449. 5 The character after the match, empty at the end of a line")
  3450. (defvar org-verbatim-re nil
  3451. "Regular expression for matching verbatim text.")
  3452. (defvar org-emphasis-regexp-components) ; defined just below
  3453. (defvar org-emphasis-alist) ; defined just below
  3454. (defun org-set-emph-re (var val)
  3455. "Set variable and compute the emphasis regular expression."
  3456. (set var val)
  3457. (when (and (boundp 'org-emphasis-alist)
  3458. (boundp 'org-emphasis-regexp-components)
  3459. org-emphasis-alist org-emphasis-regexp-components)
  3460. (let* ((e org-emphasis-regexp-components)
  3461. (pre (car e))
  3462. (post (nth 1 e))
  3463. (border (nth 2 e))
  3464. (body (nth 3 e))
  3465. (nl (nth 4 e))
  3466. (body1 (concat body "*?"))
  3467. (markers (mapconcat 'car org-emphasis-alist ""))
  3468. (vmarkers (mapconcat
  3469. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  3470. org-emphasis-alist "")))
  3471. ;; make sure special characters appear at the right position in the class
  3472. (if (string-match "\\^" markers)
  3473. (setq markers (concat (replace-match "" t t markers) "^")))
  3474. (if (string-match "-" markers)
  3475. (setq markers (concat (replace-match "" t t markers) "-")))
  3476. (if (string-match "\\^" vmarkers)
  3477. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3478. (if (string-match "-" vmarkers)
  3479. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3480. (if (> nl 0)
  3481. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3482. (int-to-string nl) "\\}")))
  3483. ;; Make the regexp
  3484. (setq org-emph-re
  3485. (concat "\\([" pre "]\\|^\\)"
  3486. "\\("
  3487. "\\([" markers "]\\)"
  3488. "\\("
  3489. "[^" border "]\\|"
  3490. "[^" border "]"
  3491. body1
  3492. "[^" border "]"
  3493. "\\)"
  3494. "\\3\\)"
  3495. "\\([" post "]\\|$\\)"))
  3496. (setq org-verbatim-re
  3497. (concat "\\([" pre "]\\|^\\)"
  3498. "\\("
  3499. "\\([" vmarkers "]\\)"
  3500. "\\("
  3501. "[^" border "]\\|"
  3502. "[^" border "]"
  3503. body1
  3504. "[^" border "]"
  3505. "\\)"
  3506. "\\3\\)"
  3507. "\\([" post "]\\|$\\)")))))
  3508. (defcustom org-emphasis-regexp-components
  3509. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  3510. "Components used to build the regular expression for emphasis.
  3511. This is a list with five entries. Terminology: In an emphasis string
  3512. like \" *strong word* \", we call the initial space PREMATCH, the final
  3513. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3514. and \"trong wor\" is the body. The different components in this variable
  3515. specify what is allowed/forbidden in each part:
  3516. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3517. post Chars allowed as postmatch. End of line will be allowed too.
  3518. border The chars *forbidden* as border characters.
  3519. body-regexp A regexp like \".\" to match a body character. Don't use
  3520. non-shy groups here, and don't allow newline here.
  3521. newline The maximum number of newlines allowed in an emphasis exp.
  3522. Use customize to modify this, or restart Emacs after changing it."
  3523. :group 'org-appearance
  3524. :set 'org-set-emph-re
  3525. :type '(list
  3526. (sexp :tag "Allowed chars in pre ")
  3527. (sexp :tag "Allowed chars in post ")
  3528. (sexp :tag "Forbidden chars in border ")
  3529. (sexp :tag "Regexp for body ")
  3530. (integer :tag "number of newlines allowed")
  3531. (option (boolean :tag "Please ignore this button"))))
  3532. (defcustom org-emphasis-alist
  3533. `(("*" bold "<b>" "</b>")
  3534. ("/" italic "<i>" "</i>")
  3535. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  3536. ("=" org-code "<code>" "</code>" verbatim)
  3537. ("~" org-verbatim "<code>" "</code>" verbatim)
  3538. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  3539. "<del>" "</del>")
  3540. )
  3541. "Special syntax for emphasized text.
  3542. Text starting and ending with a special character will be emphasized, for
  3543. example *bold*, _underlined_ and /italic/. This variable sets the marker
  3544. characters, the face to be used by font-lock for highlighting in Org-mode
  3545. Emacs buffers, and the HTML tags to be used for this.
  3546. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  3547. Use customize to modify this, or restart Emacs after changing it."
  3548. :group 'org-appearance
  3549. :set 'org-set-emph-re
  3550. :type '(repeat
  3551. (list
  3552. (string :tag "Marker character")
  3553. (choice
  3554. (face :tag "Font-lock-face")
  3555. (plist :tag "Face property list"))
  3556. (string :tag "HTML start tag")
  3557. (string :tag "HTML end tag")
  3558. (option (const verbatim)))))
  3559. (defvar org-protecting-blocks
  3560. '("src" "example" "latex" "ascii" "html" "ditaa" "dot" "r" "R")
  3561. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3562. This is needed for font-lock setup.")
  3563. ;;; Miscellaneous options
  3564. (defgroup org-completion nil
  3565. "Completion in Org-mode."
  3566. :tag "Org Completion"
  3567. :group 'org)
  3568. (defcustom org-completion-use-ido nil
  3569. "Non-nil means use ido completion wherever possible.
  3570. Note that `ido-mode' must be active for this variable to be relevant.
  3571. If you decide to turn this variable on, you might well want to turn off
  3572. `org-outline-path-complete-in-steps'.
  3573. See also `org-completion-use-iswitchb'."
  3574. :group 'org-completion
  3575. :type 'boolean)
  3576. (defcustom org-completion-use-iswitchb nil
  3577. "Non-nil means use iswitchb completion wherever possible.
  3578. Note that `iswitchb-mode' must be active for this variable to be relevant.
  3579. If you decide to turn this variable on, you might well want to turn off
  3580. `org-outline-path-complete-in-steps'.
  3581. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  3582. :group 'org-completion
  3583. :type 'boolean)
  3584. (defcustom org-completion-fallback-command 'hippie-expand
  3585. "The expansion command called by \\[pcomplete] in normal context.
  3586. Normal means, no org-mode-specific context."
  3587. :group 'org-completion
  3588. :type 'function)
  3589. ;;; Functions and variables from their packages
  3590. ;; Declared here to avoid compiler warnings
  3591. ;; XEmacs only
  3592. (defvar outline-mode-menu-heading)
  3593. (defvar outline-mode-menu-show)
  3594. (defvar outline-mode-menu-hide)
  3595. (defvar zmacs-regions) ; XEmacs regions
  3596. ;; Emacs only
  3597. (defvar mark-active)
  3598. ;; Various packages
  3599. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  3600. (declare-function calendar-forward-day "cal-move" (arg))
  3601. (declare-function calendar-goto-date "cal-move" (date))
  3602. (declare-function calendar-goto-today "cal-move" ())
  3603. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3604. (defvar calc-embedded-close-formula)
  3605. (defvar calc-embedded-open-formula)
  3606. (declare-function cdlatex-tab "ext:cdlatex" ())
  3607. (declare-function cdlatex-compute-tables "ext:cdlatex" ())
  3608. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3609. (defvar font-lock-unfontify-region-function)
  3610. (declare-function iswitchb-read-buffer "iswitchb"
  3611. (prompt &optional default require-match start matches-set))
  3612. (defvar iswitchb-temp-buflist)
  3613. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3614. (defvar org-agenda-tags-todo-honor-ignore-options)
  3615. (declare-function org-agenda-skip "org-agenda" ())
  3616. (declare-function
  3617. org-agenda-format-item "org-agenda"
  3618. (extra txt &optional level category tags dotime noprefix remove-re habitp))
  3619. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3620. (declare-function org-agenda-change-all-lines "org-agenda"
  3621. (newhead hdmarker &optional fixface just-this))
  3622. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3623. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3624. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  3625. (beg end))
  3626. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3627. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3628. "org-agenda" (&optional end))
  3629. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3630. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  3631. (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
  3632. (declare-function org-inlinetask-goto-end "org-inlinetask" ())
  3633. (declare-function org-indent-mode "org-indent" (&optional arg))
  3634. (declare-function parse-time-string "parse-time" (string))
  3635. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3636. (declare-function orgtbl-send-table "org-table" (&optional maybe))
  3637. (defvar remember-data-file)
  3638. (defvar texmathp-why)
  3639. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3640. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3641. (defvar org-latex-regexps)
  3642. ;;; Autoload and prepare some org modules
  3643. ;; Some table stuff that needs to be defined here, because it is used
  3644. ;; by the functions setting up org-mode or checking for table context.
  3645. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  3646. "Detect an org-type or table-type table.")
  3647. (defconst org-table-line-regexp "^[ \t]*|"
  3648. "Detect an org-type table line.")
  3649. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  3650. "Detect an org-type table line.")
  3651. (defconst org-table-hline-regexp "^[ \t]*|-"
  3652. "Detect an org-type table hline.")
  3653. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  3654. "Detect a table-type table hline.")
  3655. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  3656. "Detect the first line outside a table when searching from within it.
  3657. This works for both table types.")
  3658. ;; Autoload the functions in org-table.el that are needed by functions here.
  3659. (eval-and-compile
  3660. (org-autoload "org-table"
  3661. '(org-table-begin org-table-blank-field org-table-end)))
  3662. ;;;###autoload
  3663. (defun turn-on-orgtbl ()
  3664. "Unconditionally turn on `orgtbl-mode'."
  3665. (require 'org-table)
  3666. (orgtbl-mode 1))
  3667. (defun org-at-table-p (&optional table-type)
  3668. "Return t if the cursor is inside an org-type table.
  3669. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  3670. (if org-enable-table-editor
  3671. (save-excursion
  3672. (beginning-of-line 1)
  3673. (looking-at (if table-type org-table-any-line-regexp
  3674. org-table-line-regexp)))
  3675. nil))
  3676. (defsubst org-table-p () (org-at-table-p))
  3677. (defun org-at-table.el-p ()
  3678. "Return t if and only if we are at a table.el table."
  3679. (and (org-at-table-p 'any)
  3680. (save-excursion
  3681. (goto-char (org-table-begin 'any))
  3682. (looking-at org-table1-hline-regexp))))
  3683. (defun org-table-recognize-table.el ()
  3684. "If there is a table.el table nearby, recognize it and move into it."
  3685. (if org-table-tab-recognizes-table.el
  3686. (if (org-at-table.el-p)
  3687. (progn
  3688. (beginning-of-line 1)
  3689. (if (looking-at org-table-dataline-regexp)
  3690. nil
  3691. (if (looking-at org-table1-hline-regexp)
  3692. (progn
  3693. (beginning-of-line 2)
  3694. (if (looking-at org-table-any-border-regexp)
  3695. (beginning-of-line -1)))))
  3696. (if (re-search-forward "|" (org-table-end t) t)
  3697. (progn
  3698. (require 'table)
  3699. (if (table--at-cell-p (point))
  3700. t
  3701. (message "recognizing table.el table...")
  3702. (table-recognize-table)
  3703. (message "recognizing table.el table...done")))
  3704. (error "This should not happen"))
  3705. t)
  3706. nil)
  3707. nil))
  3708. (defun org-at-table-hline-p ()
  3709. "Return t if the cursor is inside a hline in a table."
  3710. (if org-enable-table-editor
  3711. (save-excursion
  3712. (beginning-of-line 1)
  3713. (looking-at org-table-hline-regexp))
  3714. nil))
  3715. (defvar org-table-clean-did-remove-column nil)
  3716. (defun org-table-map-tables (function &optional quietly)
  3717. "Apply FUNCTION to the start of all tables in the buffer."
  3718. (save-excursion
  3719. (save-restriction
  3720. (widen)
  3721. (goto-char (point-min))
  3722. (while (re-search-forward org-table-any-line-regexp nil t)
  3723. (unless quietly
  3724. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
  3725. (beginning-of-line 1)
  3726. (when (and (looking-at org-table-line-regexp)
  3727. ;; Exclude tables in src/example/verbatim/clocktable blocks
  3728. (not (org-in-block-p '("src" "example" "verbatim" "clocktable"))))
  3729. (save-excursion (funcall function))
  3730. (or (looking-at org-table-line-regexp)
  3731. (forward-char 1)))
  3732. (re-search-forward org-table-any-border-regexp nil 1))))
  3733. (unless quietly (message "Mapping tables: done")))
  3734. ;; Declare and autoload functions from ox.el and al.
  3735. (declare-function org-export-get-environment "ox"
  3736. (&optional backend subtreep ext-plist))
  3737. (declare-function org-latex-guess-inputenc "ox-latex" (header))
  3738. ;; Declare and autoload functions from org-agenda.el
  3739. (eval-and-compile
  3740. (org-autoload "org-agenda"
  3741. '(org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  3742. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock" (beg end))
  3743. (declare-function org-clock-update-mode-line "org-clock" ())
  3744. (declare-function org-resolve-clocks "org-clock"
  3745. (&optional also-non-dangling-p prompt last-valid))
  3746. (defvar org-clock-start-time)
  3747. (defvar org-clock-marker (make-marker)
  3748. "Marker recording the last clock-in.")
  3749. (defvar org-clock-hd-marker (make-marker)
  3750. "Marker recording the last clock-in, but the headline position.")
  3751. (defvar org-clock-heading ""
  3752. "The heading of the current clock entry.")
  3753. (defun org-clock-is-active ()
  3754. "Return non-nil if clock is currently running.
  3755. The return value is actually the clock marker."
  3756. (marker-buffer org-clock-marker))
  3757. (eval-and-compile
  3758. (org-autoload "org-clock" '(org-clock-remove-overlays
  3759. org-clock-update-time-maybe
  3760. org-clocktable-shift)))
  3761. (defun org-check-running-clock ()
  3762. "Check if the current buffer contains the running clock.
  3763. If yes, offer to stop it and to save the buffer with the changes."
  3764. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3765. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3766. (buffer-name))))
  3767. (org-clock-out)
  3768. (when (y-or-n-p "Save changed buffer?")
  3769. (save-buffer))))
  3770. (defun org-clocktable-try-shift (dir n)
  3771. "Check if this line starts a clock table, if yes, shift the time block."
  3772. (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
  3773. (org-clocktable-shift dir n)))
  3774. ;;;###autoload
  3775. (defun org-clock-persistence-insinuate ()
  3776. "Set up hooks for clock persistence."
  3777. (require 'org-clock)
  3778. (add-hook 'org-mode-hook 'org-clock-load)
  3779. (add-hook 'kill-emacs-hook 'org-clock-save))
  3780. ;; Define the variable already here, to make sure we have it.
  3781. (defvar org-indent-mode nil
  3782. "Non-nil if Org-Indent mode is enabled.
  3783. Use the command `org-indent-mode' to change this variable.")
  3784. ;; Autoload archiving code
  3785. ;; The stuff that is needed for cycling and tags has to be defined here.
  3786. (defgroup org-archive nil
  3787. "Options concerning archiving in Org-mode."
  3788. :tag "Org Archive"
  3789. :group 'org-structure)
  3790. (defcustom org-archive-location "%s_archive::"
  3791. "The location where subtrees should be archived.
  3792. The value of this variable is a string, consisting of two parts,
  3793. separated by a double-colon. The first part is a filename and
  3794. the second part is a headline.
  3795. When the filename is omitted, archiving happens in the same file.
  3796. %s in the filename will be replaced by the current file
  3797. name (without the directory part). Archiving to a different file
  3798. is useful to keep archived entries from contributing to the
  3799. Org-mode Agenda.
  3800. The archived entries will be filed as subtrees of the specified
  3801. headline. When the headline is omitted, the subtrees are simply
  3802. filed away at the end of the file, as top-level entries. Also in
  3803. the heading you can use %s to represent the file name, this can be
  3804. useful when using the same archive for a number of different files.
  3805. Here are a few examples:
  3806. \"%s_archive::\"
  3807. If the current file is Projects.org, archive in file
  3808. Projects.org_archive, as top-level trees. This is the default.
  3809. \"::* Archived Tasks\"
  3810. Archive in the current file, under the top-level headline
  3811. \"* Archived Tasks\".
  3812. \"~/org/archive.org::\"
  3813. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  3814. \"~/org/archive.org::* From %s\"
  3815. Archive in file ~/org/archive.org (absolute path), under headlines
  3816. \"From FILENAME\" where file name is the current file name.
  3817. \"~/org/datetree.org::datetree/* Finished Tasks\"
  3818. The \"datetree/\" string is special, signifying to archive
  3819. items to the datetree. Items are placed in either the CLOSED
  3820. date of the item, or the current date if there is no CLOSED date.
  3821. The heading will be a subentry to the current date. There doesn't
  3822. need to be a heading, but there always needs to be a slash after
  3823. datetree. For example, to store archived items directly in the
  3824. datetree, use \"~/org/datetree.org::datetree/\".
  3825. \"basement::** Finished Tasks\"
  3826. Archive in file ./basement (relative path), as level 3 trees
  3827. below the level 2 heading \"** Finished Tasks\".
  3828. You may set this option on a per-file basis by adding to the buffer a
  3829. line like
  3830. #+ARCHIVE: basement::** Finished Tasks
  3831. You may also define it locally for a subtree by setting an ARCHIVE property
  3832. in the entry. If such a property is found in an entry, or anywhere up
  3833. the hierarchy, it will be used."
  3834. :group 'org-archive
  3835. :type 'string)
  3836. (defcustom org-archive-tag "ARCHIVE"
  3837. "The tag that marks a subtree as archived.
  3838. An archived subtree does not open during visibility cycling, and does
  3839. not contribute to the agenda listings.
  3840. After changing this, font-lock must be restarted in the relevant buffers to
  3841. get the proper fontification."
  3842. :group 'org-archive
  3843. :group 'org-keywords
  3844. :type 'string)
  3845. (defcustom org-agenda-skip-archived-trees t
  3846. "Non-nil means the agenda will skip any items located in archived trees.
  3847. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  3848. variable is no longer recommended, you should leave it at the value t.
  3849. Instead, use the key `v' to cycle the archives-mode in the agenda."
  3850. :group 'org-archive
  3851. :group 'org-agenda-skip
  3852. :type 'boolean)
  3853. (defcustom org-columns-skip-archived-trees t
  3854. "Non-nil means ignore archived trees when creating column view."
  3855. :group 'org-archive
  3856. :group 'org-properties
  3857. :type 'boolean)
  3858. (defcustom org-cycle-open-archived-trees nil
  3859. "Non-nil means `org-cycle' will open archived trees.
  3860. An archived tree is a tree marked with the tag ARCHIVE.
  3861. When nil, archived trees will stay folded. You can still open them with
  3862. normal outline commands like `show-all', but not with the cycling commands."
  3863. :group 'org-archive
  3864. :group 'org-cycle
  3865. :type 'boolean)
  3866. (defcustom org-sparse-tree-open-archived-trees nil
  3867. "Non-nil means sparse tree construction shows matches in archived trees.
  3868. When nil, matches in these trees are highlighted, but the trees are kept in
  3869. collapsed state."
  3870. :group 'org-archive
  3871. :group 'org-sparse-trees
  3872. :type 'boolean)
  3873. (defcustom org-sparse-tree-default-date-type 'scheduled-or-deadline
  3874. "The default date type when building a sparse tree.
  3875. When this is nil, a date is a scheduled or a deadline timestamp.
  3876. Otherwise, these types are allowed:
  3877. all: all timestamps
  3878. active: only active timestamps (<...>)
  3879. inactive: only inactive timestamps (<...)
  3880. scheduled: only scheduled timestamps
  3881. deadline: only deadline timestamps"
  3882. :type '(choice (const :tag "Scheduled or deadline" 'scheduled-or-deadline)
  3883. (const :tag "All timestamps" all)
  3884. (const :tag "Only active timestamps" active)
  3885. (const :tag "Only inactive timestamps" inactive)
  3886. (const :tag "Only scheduled timestamps" scheduled)
  3887. (const :tag "Only deadline timestamps" deadline))
  3888. :version "24.3"
  3889. :group 'org-sparse-trees)
  3890. (defun org-cycle-hide-archived-subtrees (state)
  3891. "Re-hide all archived subtrees after a visibility state change."
  3892. (when (and (not org-cycle-open-archived-trees)
  3893. (not (memq state '(overview folded))))
  3894. (save-excursion
  3895. (let* ((globalp (memq state '(contents all)))
  3896. (beg (if globalp (point-min) (point)))
  3897. (end (if globalp (point-max) (org-end-of-subtree t))))
  3898. (org-hide-archived-subtrees beg end)
  3899. (goto-char beg)
  3900. (if (looking-at (concat ".*:" org-archive-tag ":"))
  3901. (message "%s" (substitute-command-keys
  3902. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  3903. (defun org-force-cycle-archived ()
  3904. "Cycle subtree even if it is archived."
  3905. (interactive)
  3906. (setq this-command 'org-cycle)
  3907. (let ((org-cycle-open-archived-trees t))
  3908. (call-interactively 'org-cycle)))
  3909. (defun org-hide-archived-subtrees (beg end)
  3910. "Re-hide all archived subtrees after a visibility state change."
  3911. (save-excursion
  3912. (let* ((re (concat ":" org-archive-tag ":")))
  3913. (goto-char beg)
  3914. (while (re-search-forward re end t)
  3915. (when (org-at-heading-p)
  3916. (org-flag-subtree t)
  3917. (org-end-of-subtree t))))))
  3918. (declare-function outline-end-of-heading "outline" ())
  3919. (declare-function outline-flag-region "outline" (from to flag))
  3920. (defun org-flag-subtree (flag)
  3921. (save-excursion
  3922. (org-back-to-heading t)
  3923. (outline-end-of-heading)
  3924. (outline-flag-region (point)
  3925. (progn (org-end-of-subtree t) (point))
  3926. flag)))
  3927. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  3928. (eval-and-compile
  3929. (org-autoload "org-archive"
  3930. '(org-add-archive-files)))
  3931. ;; Autoload Column View Code
  3932. (declare-function org-columns-number-to-string "org-colview" (n fmt &optional printf))
  3933. (declare-function org-columns-get-format-and-top-level "org-colview" ())
  3934. (declare-function org-columns-compute "org-colview" (property))
  3935. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  3936. '(org-columns-number-to-string
  3937. org-columns-get-format-and-top-level
  3938. org-columns-compute
  3939. org-columns-remove-overlays))
  3940. ;; Autoload ID code
  3941. (declare-function org-id-store-link "org-id")
  3942. (declare-function org-id-locations-load "org-id")
  3943. (declare-function org-id-locations-save "org-id")
  3944. (defvar org-id-track-globally)
  3945. (org-autoload "org-id"
  3946. '(org-id-new
  3947. org-id-copy
  3948. org-id-get-with-outline-path-completion
  3949. org-id-get-with-outline-drilling))
  3950. ;;; Variables for pre-computed regular expressions, all buffer local
  3951. (defvar org-drawer-regexp "^[ \t]*:PROPERTIES:[ \t]*$"
  3952. "Matches first line of a hidden block.")
  3953. (make-variable-buffer-local 'org-drawer-regexp)
  3954. (defvar org-todo-regexp nil
  3955. "Matches any of the TODO state keywords.")
  3956. (make-variable-buffer-local 'org-todo-regexp)
  3957. (defvar org-not-done-regexp nil
  3958. "Matches any of the TODO state keywords except the last one.")
  3959. (make-variable-buffer-local 'org-not-done-regexp)
  3960. (defvar org-not-done-heading-regexp nil
  3961. "Matches a TODO headline that is not done.")
  3962. (make-variable-buffer-local 'org-not-done-regexp)
  3963. (defvar org-todo-line-regexp nil
  3964. "Matches a headline and puts TODO state into group 2 if present.")
  3965. (make-variable-buffer-local 'org-todo-line-regexp)
  3966. (defvar org-complex-heading-regexp nil
  3967. "Matches a headline and puts everything into groups:
  3968. group 1: the stars
  3969. group 2: The todo keyword, maybe
  3970. group 3: Priority cookie
  3971. group 4: True headline
  3972. group 5: Tags")
  3973. (make-variable-buffer-local 'org-complex-heading-regexp)
  3974. (defvar org-complex-heading-regexp-format nil
  3975. "Printf format to make regexp to match an exact headline.
  3976. This regexp will match the headline of any node which has the
  3977. exact headline text that is put into the format, but may have any
  3978. TODO state, priority and tags.")
  3979. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  3980. (defvar org-todo-line-tags-regexp nil
  3981. "Matches a headline and puts TODO state into group 2 if present.
  3982. Also put tags into group 4 if tags are present.")
  3983. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3984. (defvar org-ds-keyword-length 12
  3985. "Maximum length of the DEADLINE and SCHEDULED keywords.")
  3986. (make-variable-buffer-local 'org-ds-keyword-length)
  3987. (defvar org-deadline-regexp nil
  3988. "Matches the DEADLINE keyword.")
  3989. (make-variable-buffer-local 'org-deadline-regexp)
  3990. (defvar org-deadline-time-regexp nil
  3991. "Matches the DEADLINE keyword together with a time stamp.")
  3992. (make-variable-buffer-local 'org-deadline-time-regexp)
  3993. (defvar org-deadline-time-hour-regexp nil
  3994. "Matches the DEADLINE keyword together with a time-and-hour stamp.")
  3995. (make-variable-buffer-local 'org-deadline-time-hour-regexp)
  3996. (defvar org-deadline-line-regexp nil
  3997. "Matches the DEADLINE keyword and the rest of the line.")
  3998. (make-variable-buffer-local 'org-deadline-line-regexp)
  3999. (defvar org-scheduled-regexp nil
  4000. "Matches the SCHEDULED keyword.")
  4001. (make-variable-buffer-local 'org-scheduled-regexp)
  4002. (defvar org-scheduled-time-regexp nil
  4003. "Matches the SCHEDULED keyword together with a time stamp.")
  4004. (make-variable-buffer-local 'org-scheduled-time-regexp)
  4005. (defvar org-scheduled-time-hour-regexp nil
  4006. "Matches the SCHEDULED keyword together with a time-and-hour stamp.")
  4007. (make-variable-buffer-local 'org-scheduled-time-hour-regexp)
  4008. (defvar org-closed-time-regexp nil
  4009. "Matches the CLOSED keyword together with a time stamp.")
  4010. (make-variable-buffer-local 'org-closed-time-regexp)
  4011. (defvar org-keyword-time-regexp nil
  4012. "Matches any of the 4 keywords, together with the time stamp.")
  4013. (make-variable-buffer-local 'org-keyword-time-regexp)
  4014. (defvar org-keyword-time-not-clock-regexp nil
  4015. "Matches any of the 3 keywords, together with the time stamp.")
  4016. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  4017. (defvar org-maybe-keyword-time-regexp nil
  4018. "Matches a timestamp, possibly preceded by a keyword.")
  4019. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  4020. (defvar org-all-time-keywords nil
  4021. "List of time keywords.")
  4022. (make-variable-buffer-local 'org-all-time-keywords)
  4023. (defconst org-plain-time-of-day-regexp
  4024. (concat
  4025. "\\(\\<[012]?[0-9]"
  4026. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4027. "\\(--?"
  4028. "\\(\\<[012]?[0-9]"
  4029. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4030. "\\)?")
  4031. "Regular expression to match a plain time or time range.
  4032. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4033. groups carry important information:
  4034. 0 the full match
  4035. 1 the first time, range or not
  4036. 8 the second time, if it is a range.")
  4037. (defconst org-plain-time-extension-regexp
  4038. (concat
  4039. "\\(\\<[012]?[0-9]"
  4040. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4041. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  4042. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  4043. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4044. groups carry important information:
  4045. 0 the full match
  4046. 7 hours of duration
  4047. 9 minutes of duration")
  4048. (defconst org-stamp-time-of-day-regexp
  4049. (concat
  4050. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  4051. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  4052. "\\(--?"
  4053. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  4054. "Regular expression to match a timestamp time or time range.
  4055. After a match, the following groups carry important information:
  4056. 0 the full match
  4057. 1 date plus weekday, for back referencing to make sure both times are on the same day
  4058. 2 the first time, range or not
  4059. 4 the second time, if it is a range.")
  4060. (defconst org-startup-options
  4061. '(("fold" org-startup-folded t)
  4062. ("overview" org-startup-folded t)
  4063. ("nofold" org-startup-folded nil)
  4064. ("showall" org-startup-folded nil)
  4065. ("showeverything" org-startup-folded showeverything)
  4066. ("content" org-startup-folded content)
  4067. ("indent" org-startup-indented t)
  4068. ("noindent" org-startup-indented nil)
  4069. ("hidestars" org-hide-leading-stars t)
  4070. ("showstars" org-hide-leading-stars nil)
  4071. ("odd" org-odd-levels-only t)
  4072. ("oddeven" org-odd-levels-only nil)
  4073. ("align" org-startup-align-all-tables t)
  4074. ("noalign" org-startup-align-all-tables nil)
  4075. ("inlineimages" org-startup-with-inline-images t)
  4076. ("noinlineimages" org-startup-with-inline-images nil)
  4077. ("latexpreview" org-startup-with-latex-preview t)
  4078. ("nolatexpreview" org-startup-with-latex-preview nil)
  4079. ("customtime" org-display-custom-times t)
  4080. ("logdone" org-log-done time)
  4081. ("lognotedone" org-log-done note)
  4082. ("nologdone" org-log-done nil)
  4083. ("lognoteclock-out" org-log-note-clock-out t)
  4084. ("nolognoteclock-out" org-log-note-clock-out nil)
  4085. ("logrepeat" org-log-repeat state)
  4086. ("lognoterepeat" org-log-repeat note)
  4087. ("logdrawer" org-log-into-drawer t)
  4088. ("nologdrawer" org-log-into-drawer nil)
  4089. ("logstatesreversed" org-log-states-order-reversed t)
  4090. ("nologstatesreversed" org-log-states-order-reversed nil)
  4091. ("nologrepeat" org-log-repeat nil)
  4092. ("logreschedule" org-log-reschedule time)
  4093. ("lognotereschedule" org-log-reschedule note)
  4094. ("nologreschedule" org-log-reschedule nil)
  4095. ("logredeadline" org-log-redeadline time)
  4096. ("lognoteredeadline" org-log-redeadline note)
  4097. ("nologredeadline" org-log-redeadline nil)
  4098. ("logrefile" org-log-refile time)
  4099. ("lognoterefile" org-log-refile note)
  4100. ("nologrefile" org-log-refile nil)
  4101. ("fninline" org-footnote-define-inline t)
  4102. ("nofninline" org-footnote-define-inline nil)
  4103. ("fnlocal" org-footnote-section nil)
  4104. ("fnauto" org-footnote-auto-label t)
  4105. ("fnprompt" org-footnote-auto-label nil)
  4106. ("fnconfirm" org-footnote-auto-label confirm)
  4107. ("fnplain" org-footnote-auto-label plain)
  4108. ("fnadjust" org-footnote-auto-adjust t)
  4109. ("nofnadjust" org-footnote-auto-adjust nil)
  4110. ("constcgs" constants-unit-system cgs)
  4111. ("constSI" constants-unit-system SI)
  4112. ("noptag" org-tag-persistent-alist nil)
  4113. ("hideblocks" org-hide-block-startup t)
  4114. ("nohideblocks" org-hide-block-startup nil)
  4115. ("beamer" org-startup-with-beamer-mode t)
  4116. ("entitiespretty" org-pretty-entities t)
  4117. ("entitiesplain" org-pretty-entities nil))
  4118. "Variable associated with STARTUP options for org-mode.
  4119. Each element is a list of three items: the startup options (as written
  4120. in the #+STARTUP line), the corresponding variable, and the value to set
  4121. this variable to if the option is found. An optional forth element PUSH
  4122. means to push this value onto the list in the variable.")
  4123. (defun org-update-property-plist (key val props)
  4124. "Update PROPS with KEY and VAL."
  4125. (let* ((appending (string= "+" (substring key (- (length key) 1))))
  4126. (key (if appending (substring key 0 (- (length key) 1)) key))
  4127. (remainder (org-remove-if (lambda (p) (string= (car p) key)) props))
  4128. (previous (cdr (assoc key props))))
  4129. (if appending
  4130. (cons (cons key (if previous (concat previous " " val) val)) remainder)
  4131. (cons (cons key val) remainder))))
  4132. (defconst org-block-regexp
  4133. "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
  4134. "Regular expression for hiding blocks.")
  4135. (defconst org-heading-keyword-regexp-format
  4136. "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  4137. "Printf format for a regexp matching a headline with some keyword.
  4138. This regexp will match the headline of any node which has the
  4139. exact keyword that is put into the format. The keyword isn't in
  4140. any group by default, but the stars and the body are.")
  4141. (defconst org-heading-keyword-maybe-regexp-format
  4142. "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
  4143. "Printf format for a regexp matching a headline, possibly with some keyword.
  4144. This regexp can match any headline with the specified keyword, or
  4145. without a keyword. The keyword isn't in any group by default,
  4146. but the stars and the body are.")
  4147. (defun org-set-regexps-and-options ()
  4148. "Precompute regular expressions for current buffer."
  4149. (when (derived-mode-p 'org-mode)
  4150. (org-set-local 'org-todo-kwd-alist nil)
  4151. (org-set-local 'org-todo-key-alist nil)
  4152. (org-set-local 'org-todo-key-trigger nil)
  4153. (org-set-local 'org-todo-keywords-1 nil)
  4154. (org-set-local 'org-done-keywords nil)
  4155. (org-set-local 'org-todo-heads nil)
  4156. (org-set-local 'org-todo-sets nil)
  4157. (org-set-local 'org-todo-log-states nil)
  4158. (org-set-local 'org-file-properties nil)
  4159. (org-set-local 'org-file-tags nil)
  4160. (let ((re (org-make-options-regexp
  4161. '("CATEGORY" "TODO" "COLUMNS" "STARTUP" "ARCHIVE" "FILETAGS"
  4162. "TAGS" "LINK" "PRIORITIES" "CONSTANTS" "PROPERTY" "DRAWERS"
  4163. "SETUPFILE" "OPTIONS")
  4164. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  4165. (splitre "[ \t]+")
  4166. (scripts org-use-sub-superscripts)
  4167. kwds kws0 kwsa key log value cat arch tags const links hw dws
  4168. tail sep kws1 prio props ftags drawers ext-setup-or-nil setup-contents
  4169. (start 0))
  4170. (save-excursion
  4171. (save-restriction
  4172. (widen)
  4173. (goto-char (point-min))
  4174. (while (or (and ext-setup-or-nil
  4175. (string-match re ext-setup-or-nil start)
  4176. (setq start (match-end 0)))
  4177. (and (setq ext-setup-or-nil nil start 0)
  4178. (re-search-forward re nil t)))
  4179. (setq key (upcase (match-string 1 ext-setup-or-nil))
  4180. value (org-match-string-no-properties 2 ext-setup-or-nil))
  4181. (if (stringp value) (setq value (org-trim value)))
  4182. (cond
  4183. ((equal key "CATEGORY")
  4184. (setq cat value))
  4185. ((member key '("SEQ_TODO" "TODO"))
  4186. (push (cons 'sequence (org-split-string value splitre)) kwds))
  4187. ((equal key "TYP_TODO")
  4188. (push (cons 'type (org-split-string value splitre)) kwds))
  4189. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  4190. ;; general TODO-like setup
  4191. (push (cons (intern (downcase (match-string 1 key)))
  4192. (org-split-string value splitre)) kwds))
  4193. ((equal key "TAGS")
  4194. (setq tags (append tags (if tags '("\\n") nil)
  4195. (org-split-string value splitre))))
  4196. ((equal key "COLUMNS")
  4197. (org-set-local 'org-columns-default-format value))
  4198. ((equal key "LINK")
  4199. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  4200. (push (cons (match-string 1 value)
  4201. (org-trim (match-string 2 value)))
  4202. links)))
  4203. ((equal key "PRIORITIES")
  4204. (setq prio (org-split-string value " +")))
  4205. ((equal key "PROPERTY")
  4206. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4207. (setq props (org-update-property-plist (match-string 1 value)
  4208. (match-string 2 value)
  4209. props))))
  4210. ((equal key "FILETAGS")
  4211. (when (string-match "\\S-" value)
  4212. (setq ftags
  4213. (append
  4214. ftags
  4215. (apply 'append
  4216. (mapcar (lambda (x) (org-split-string x ":"))
  4217. (org-split-string value)))))))
  4218. ((equal key "DRAWERS")
  4219. (setq drawers (delete-dups (append org-drawers (org-split-string value splitre)))))
  4220. ((equal key "CONSTANTS")
  4221. (setq const (append const (org-split-string value splitre))))
  4222. ((equal key "STARTUP")
  4223. (let ((opts (org-split-string value splitre))
  4224. l var val)
  4225. (while (setq l (pop opts))
  4226. (when (setq l (assoc l org-startup-options))
  4227. (setq var (nth 1 l) val (nth 2 l))
  4228. (if (not (nth 3 l))
  4229. (set (make-local-variable var) val)
  4230. (if (not (listp (symbol-value var)))
  4231. (set (make-local-variable var) nil))
  4232. (set (make-local-variable var) (symbol-value var))
  4233. (add-to-list var val))))))
  4234. ((equal key "ARCHIVE")
  4235. (setq arch value)
  4236. (remove-text-properties 0 (length arch)
  4237. '(face t fontified t) arch))
  4238. ((equal key "OPTIONS")
  4239. (if (string-match "\\([ \t]\\|\\`\\)\\^:\\(t\\|nil\\|{}\\)" value)
  4240. (setq scripts (read (match-string 2 value)))))
  4241. ((equal key "SETUPFILE")
  4242. (setq setup-contents (org-file-contents
  4243. (expand-file-name
  4244. (org-remove-double-quotes value))
  4245. 'noerror))
  4246. (if (not ext-setup-or-nil)
  4247. (setq ext-setup-or-nil setup-contents start 0)
  4248. (setq ext-setup-or-nil
  4249. (concat (substring ext-setup-or-nil 0 start)
  4250. "\n" setup-contents "\n"
  4251. (substring ext-setup-or-nil start)))))))
  4252. ;; search for property blocks
  4253. (goto-char (point-min))
  4254. (while (re-search-forward org-block-regexp nil t)
  4255. (when (equal "PROPERTY" (upcase (match-string 1)))
  4256. (setq value (replace-regexp-in-string
  4257. "[\n\r]" " " (match-string 4)))
  4258. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4259. (setq props (org-update-property-plist (match-string 1 value)
  4260. (match-string 2 value)
  4261. props)))))))
  4262. (org-set-local 'org-use-sub-superscripts scripts)
  4263. (when cat
  4264. (org-set-local 'org-category (intern cat))
  4265. (push (cons "CATEGORY" cat) props))
  4266. (when prio
  4267. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  4268. (setq prio (mapcar 'string-to-char prio))
  4269. (org-set-local 'org-highest-priority (nth 0 prio))
  4270. (org-set-local 'org-lowest-priority (nth 1 prio))
  4271. (org-set-local 'org-default-priority (nth 2 prio)))
  4272. (and props (org-set-local 'org-file-properties (nreverse props)))
  4273. (and ftags (org-set-local 'org-file-tags
  4274. (mapcar 'org-add-prop-inherited ftags)))
  4275. (and drawers (org-set-local 'org-drawers drawers))
  4276. (and arch (org-set-local 'org-archive-location arch))
  4277. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  4278. ;; Process the TODO keywords
  4279. (unless kwds
  4280. ;; Use the global values as if they had been given locally.
  4281. (setq kwds (default-value 'org-todo-keywords))
  4282. (if (stringp (car kwds))
  4283. (setq kwds (list (cons org-todo-interpretation
  4284. (default-value 'org-todo-keywords)))))
  4285. (setq kwds (reverse kwds)))
  4286. (setq kwds (nreverse kwds))
  4287. (let (inter kws kw)
  4288. (while (setq kws (pop kwds))
  4289. (let ((kws (or
  4290. (run-hook-with-args-until-success
  4291. 'org-todo-setup-filter-hook kws)
  4292. kws)))
  4293. (setq inter (pop kws) sep (member "|" kws)
  4294. kws0 (delete "|" (copy-sequence kws))
  4295. kwsa nil
  4296. kws1 (mapcar
  4297. (lambda (x)
  4298. ;; 1 2
  4299. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  4300. (progn
  4301. (setq kw (match-string 1 x)
  4302. key (and (match-end 2) (match-string 2 x))
  4303. log (org-extract-log-state-settings x))
  4304. (push (cons kw (and key (string-to-char key))) kwsa)
  4305. (and log (push log org-todo-log-states))
  4306. kw)
  4307. (error "Invalid TODO keyword %s" x)))
  4308. kws0)
  4309. kwsa (if kwsa (append '((:startgroup))
  4310. (nreverse kwsa)
  4311. '((:endgroup))))
  4312. hw (car kws1)
  4313. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  4314. tail (list inter hw (car dws) (org-last dws))))
  4315. (add-to-list 'org-todo-heads hw 'append)
  4316. (push kws1 org-todo-sets)
  4317. (setq org-done-keywords (append org-done-keywords dws nil))
  4318. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  4319. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  4320. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  4321. (setq org-todo-sets (nreverse org-todo-sets)
  4322. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  4323. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  4324. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  4325. ;; Process the constants
  4326. (when const
  4327. (let (e cst)
  4328. (while (setq e (pop const))
  4329. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  4330. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  4331. (setq org-table-formula-constants-local cst)))
  4332. ;; Process the tags.
  4333. (when tags
  4334. (let (e tgs)
  4335. (while (setq e (pop tags))
  4336. (cond
  4337. ((equal e "{") (push '(:startgroup) tgs))
  4338. ((equal e "}") (push '(:endgroup) tgs))
  4339. ((equal e "\\n") (push '(:newline) tgs))
  4340. ((string-match (org-re "^\\([[:alnum:]_@#%]+\\)(\\(.\\))$") e)
  4341. (push (cons (match-string 1 e)
  4342. (string-to-char (match-string 2 e)))
  4343. tgs))
  4344. (t (push (list e) tgs))))
  4345. (org-set-local 'org-tag-alist nil)
  4346. (while (setq e (pop tgs))
  4347. (or (and (stringp (car e))
  4348. (assoc (car e) org-tag-alist))
  4349. (push e org-tag-alist)))))
  4350. ;; Compute the regular expressions and other local variables.
  4351. ;; Using `org-outline-regexp-bol' would complicate them much,
  4352. ;; because of the fixed white space at the end of that string.
  4353. (if (not org-done-keywords)
  4354. (setq org-done-keywords (and org-todo-keywords-1
  4355. (list (org-last org-todo-keywords-1)))))
  4356. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  4357. (length org-scheduled-string)
  4358. (length org-clock-string)
  4359. (length org-closed-string)))
  4360. org-drawer-regexp
  4361. (concat "^[ \t]*:\\("
  4362. (mapconcat 'regexp-quote org-drawers "\\|")
  4363. "\\):[ \t]*$")
  4364. org-not-done-keywords
  4365. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  4366. org-todo-regexp
  4367. (concat "\\("
  4368. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4369. "\\)")
  4370. org-not-done-regexp
  4371. (concat "\\("
  4372. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  4373. "\\)")
  4374. org-not-done-heading-regexp
  4375. (format org-heading-keyword-regexp-format org-not-done-regexp)
  4376. org-todo-line-regexp
  4377. (format org-heading-keyword-maybe-regexp-format org-todo-regexp)
  4378. org-complex-heading-regexp
  4379. (concat "^\\(\\*+\\)"
  4380. "\\(?: +" org-todo-regexp "\\)?"
  4381. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4382. "\\(?: +\\(.*?\\)\\)??"
  4383. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?")
  4384. "[ \t]*$")
  4385. org-complex-heading-regexp-format
  4386. (concat "^\\(\\*+\\)"
  4387. "\\(?: +" org-todo-regexp "\\)?"
  4388. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4389. "\\(?: +"
  4390. ;; Stats cookies can be stuck to body.
  4391. "\\(?:\\[[0-9%%/]+\\] *\\)?"
  4392. "\\(%s\\)"
  4393. "\\(?: *\\[[0-9%%/]+\\]\\)?"
  4394. "\\)"
  4395. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?")
  4396. "[ \t]*$")
  4397. org-todo-line-tags-regexp
  4398. (concat "^\\(\\*+\\)"
  4399. "\\(?: +" org-todo-regexp "\\)?"
  4400. "\\(?: +\\(.*?\\)\\)??"
  4401. (org-re "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?")
  4402. "[ \t]*$")
  4403. org-deadline-regexp (concat "\\<" org-deadline-string)
  4404. org-deadline-time-regexp
  4405. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  4406. org-deadline-time-hour-regexp
  4407. (concat "\\<" org-deadline-string
  4408. " *<\\(.+[0-9]\\{1,2\\}:[0-9]\\{2\\}[^>]*\\)>")
  4409. org-deadline-line-regexp
  4410. (concat "\\<\\(" org-deadline-string "\\).*")
  4411. org-scheduled-regexp
  4412. (concat "\\<" org-scheduled-string)
  4413. org-scheduled-time-regexp
  4414. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  4415. org-scheduled-time-hour-regexp
  4416. (concat "\\<" org-scheduled-string
  4417. " *<\\(.+[0-9]\\{1,2\\}:[0-9]\\{2\\}[^>]*\\)>")
  4418. org-closed-time-regexp
  4419. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  4420. org-keyword-time-regexp
  4421. (concat "\\<\\(" org-scheduled-string
  4422. "\\|" org-deadline-string
  4423. "\\|" org-closed-string
  4424. "\\|" org-clock-string "\\)"
  4425. " *[[<]\\([^]>]+\\)[]>]")
  4426. org-keyword-time-not-clock-regexp
  4427. (concat "\\<\\(" org-scheduled-string
  4428. "\\|" org-deadline-string
  4429. "\\|" org-closed-string
  4430. "\\)"
  4431. " *[[<]\\([^]>]+\\)[]>]")
  4432. org-maybe-keyword-time-regexp
  4433. (concat "\\(\\<\\(" org-scheduled-string
  4434. "\\|" org-deadline-string
  4435. "\\|" org-closed-string
  4436. "\\|" org-clock-string "\\)\\)?"
  4437. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  4438. org-all-time-keywords
  4439. (mapcar (lambda (w) (substring w 0 -1))
  4440. (list org-scheduled-string org-deadline-string
  4441. org-clock-string org-closed-string)))
  4442. (org-compute-latex-and-related-regexp)
  4443. (org-set-font-lock-defaults))))
  4444. (defun org-file-contents (file &optional noerror)
  4445. "Return the contents of FILE, as a string."
  4446. (if (or (not file)
  4447. (not (file-readable-p file)))
  4448. (if noerror
  4449. (progn
  4450. (message "Cannot read file \"%s\"" file)
  4451. (ding) (sit-for 2)
  4452. "")
  4453. (error "Cannot read file \"%s\"" file))
  4454. (with-temp-buffer
  4455. (insert-file-contents file)
  4456. (buffer-string))))
  4457. (defun org-extract-log-state-settings (x)
  4458. "Extract the log state setting from a TODO keyword string.
  4459. This will extract info from a string like \"WAIT(w@/!)\"."
  4460. (let (kw key log1 log2)
  4461. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4462. (setq kw (match-string 1 x)
  4463. key (and (match-end 2) (match-string 2 x))
  4464. log1 (and (match-end 3) (match-string 3 x))
  4465. log2 (and (match-end 4) (match-string 4 x)))
  4466. (and (or log1 log2)
  4467. (list kw
  4468. (and log1 (if (equal log1 "!") 'time 'note))
  4469. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4470. (defun org-remove-keyword-keys (list)
  4471. "Remove a pair of parenthesis at the end of each string in LIST."
  4472. (mapcar (lambda (x)
  4473. (if (string-match "(.*)$" x)
  4474. (substring x 0 (match-beginning 0))
  4475. x))
  4476. list))
  4477. (defun org-assign-fast-keys (alist)
  4478. "Assign fast keys to a keyword-key alist.
  4479. Respect keys that are already there."
  4480. (let (new e (alt ?0))
  4481. (while (setq e (pop alist))
  4482. (if (or (memq (car e) '(:newline :endgroup :startgroup))
  4483. (cdr e)) ;; Key already assigned.
  4484. (push e new)
  4485. (let ((clist (string-to-list (downcase (car e))))
  4486. (used (append new alist)))
  4487. (when (= (car clist) ?@)
  4488. (pop clist))
  4489. (while (and clist (rassoc (car clist) used))
  4490. (pop clist))
  4491. (unless clist
  4492. (while (rassoc alt used)
  4493. (incf alt)))
  4494. (push (cons (car e) (or (car clist) alt)) new))))
  4495. (nreverse new)))
  4496. ;;; Some variables used in various places
  4497. (defvar org-window-configuration nil
  4498. "Used in various places to store a window configuration.")
  4499. (defvar org-selected-window nil
  4500. "Used in various places to store a window configuration.")
  4501. (defvar org-finish-function nil
  4502. "Function to be called when `C-c C-c' is used.
  4503. This is for getting out of special buffers like capture.")
  4504. ;; FIXME: Occasionally check by commenting these, to make sure
  4505. ;; no other functions uses these, forgetting to let-bind them.
  4506. (org-no-warnings (defvar entry)) ;; unprefixed, from calendar.el
  4507. (defvar org-last-state)
  4508. (org-no-warnings (defvar date)) ;; unprefixed, from calendar.el
  4509. ;; Defined somewhere in this file, but used before definition.
  4510. (defvar org-entities) ;; defined in org-entities.el
  4511. (defvar org-struct-menu)
  4512. (defvar org-org-menu)
  4513. (defvar org-tbl-menu)
  4514. ;;;; Define the Org-mode
  4515. ;; We use a before-change function to check if a table might need
  4516. ;; an update.
  4517. (defvar org-table-may-need-update t
  4518. "Indicates that a table might need an update.
  4519. This variable is set by `org-before-change-function'.
  4520. `org-table-align' sets it back to nil.")
  4521. (defun org-before-change-function (beg end)
  4522. "Every change indicates that a table might need an update."
  4523. (setq org-table-may-need-update t))
  4524. (defvar org-mode-map)
  4525. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4526. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4527. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4528. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4529. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4530. (defvar org-table-buffer-is-an nil)
  4531. (defvar bidi-paragraph-direction)
  4532. (defvar buffer-face-mode-face)
  4533. (require 'outline)
  4534. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4535. (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"))
  4536. (require 'noutline "noutline" 'noerror) ;; stock XEmacs does not have it
  4537. ;; Other stuff we need.
  4538. (require 'time-date)
  4539. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  4540. (require 'easymenu)
  4541. (require 'overlay)
  4542. ;; (require 'org-macs) moved higher up in the file before it is first used
  4543. (require 'org-entities)
  4544. ;; (require 'org-compat) moved higher up in the file before it is first used
  4545. (require 'org-faces)
  4546. (require 'org-list)
  4547. (require 'org-pcomplete)
  4548. (require 'org-src)
  4549. (require 'org-footnote)
  4550. (require 'org-macro)
  4551. ;; babel
  4552. (require 'ob)
  4553. ;;;###autoload
  4554. (define-derived-mode org-mode outline-mode "Org"
  4555. "Outline-based notes management and organizer, alias
  4556. \"Carsten's outline-mode for keeping track of everything.\"
  4557. Org-mode develops organizational tasks around a NOTES file which
  4558. contains information about projects as plain text. Org-mode is
  4559. implemented on top of outline-mode, which is ideal to keep the content
  4560. of large files well structured. It supports ToDo items, deadlines and
  4561. time stamps, which magically appear in the diary listing of the Emacs
  4562. calendar. Tables are easily created with a built-in table editor.
  4563. Plain text URL-like links connect to websites, emails (VM), Usenet
  4564. messages (Gnus), BBDB entries, and any files related to the project.
  4565. For printing and sharing of notes, an Org-mode file (or a part of it)
  4566. can be exported as a structured ASCII or HTML file.
  4567. The following commands are available:
  4568. \\{org-mode-map}"
  4569. ;; Get rid of Outline menus, they are not needed
  4570. ;; Need to do this here because define-derived-mode sets up
  4571. ;; the keymap so late. Still, it is a waste to call this each time
  4572. ;; we switch another buffer into org-mode.
  4573. (if (featurep 'xemacs)
  4574. (when (boundp 'outline-mode-menu-heading)
  4575. ;; Assume this is Greg's port, it uses easymenu
  4576. (easy-menu-remove outline-mode-menu-heading)
  4577. (easy-menu-remove outline-mode-menu-show)
  4578. (easy-menu-remove outline-mode-menu-hide))
  4579. (define-key org-mode-map [menu-bar headings] 'undefined)
  4580. (define-key org-mode-map [menu-bar hide] 'undefined)
  4581. (define-key org-mode-map [menu-bar show] 'undefined))
  4582. (org-load-modules-maybe)
  4583. (easy-menu-add org-org-menu)
  4584. (easy-menu-add org-tbl-menu)
  4585. (org-install-agenda-files-menu)
  4586. (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4587. (add-to-invisibility-spec '(org-cwidth))
  4588. (add-to-invisibility-spec '(org-hide-block . t))
  4589. (when (featurep 'xemacs)
  4590. (org-set-local 'line-move-ignore-invisible t))
  4591. (org-set-local 'outline-regexp org-outline-regexp)
  4592. (org-set-local 'outline-level 'org-outline-level)
  4593. (setq bidi-paragraph-direction 'left-to-right)
  4594. ;; FIXME Circumvent a bug in outline.el (Emacs <24.4)
  4595. (set (make-local-variable 'paragraph-start) " \\|[ \t]*$\\|\\*+ ")
  4596. (when (and org-ellipsis
  4597. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4598. (fboundp 'make-glyph-code))
  4599. (unless org-display-table
  4600. (setq org-display-table (make-display-table)))
  4601. (set-display-table-slot
  4602. org-display-table 4
  4603. (vconcat (mapcar
  4604. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4605. org-ellipsis)))
  4606. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4607. (setq buffer-display-table org-display-table))
  4608. (org-set-regexps-and-options)
  4609. (when (and org-tag-faces (not org-tags-special-faces-re))
  4610. ;; tag faces set outside customize.... force initialization.
  4611. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4612. ;; Calc embedded
  4613. (org-set-local 'calc-embedded-open-mode "# ")
  4614. ;; Modify a few syntax entries
  4615. (modify-syntax-entry ?@ "w")
  4616. (modify-syntax-entry ?\" "\"")
  4617. (if org-startup-truncated (setq truncate-lines t))
  4618. (when org-startup-indented (require 'org-indent) (org-indent-mode 1))
  4619. (org-set-local 'font-lock-unfontify-region-function
  4620. 'org-unfontify-region)
  4621. ;; Activate before-change-function
  4622. (org-set-local 'org-table-may-need-update t)
  4623. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4624. 'local)
  4625. ;; Check for running clock before killing a buffer
  4626. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4627. ;; Initialize macros templates.
  4628. (org-macro-initialize-templates)
  4629. ;; Initialize radio targets.
  4630. (org-update-radio-target-regexp)
  4631. ;; Indentation.
  4632. (org-set-local 'indent-line-function 'org-indent-line)
  4633. (org-set-local 'indent-region-function 'org-indent-region)
  4634. ;; Filling and auto-filling.
  4635. (org-setup-filling)
  4636. ;; Comments.
  4637. (org-setup-comments-handling)
  4638. ;; Beginning/end of defun
  4639. (org-set-local 'beginning-of-defun-function 'org-backward-element)
  4640. (org-set-local 'end-of-defun-function 'org-forward-element)
  4641. ;; Next error for sparse trees
  4642. (org-set-local 'next-error-function 'org-occur-next-match)
  4643. ;; Make sure dependence stuff works reliably, even for users who set it
  4644. ;; too late :-(
  4645. (if org-enforce-todo-dependencies
  4646. (add-hook 'org-blocker-hook
  4647. 'org-block-todo-from-children-or-siblings-or-parent)
  4648. (remove-hook 'org-blocker-hook
  4649. 'org-block-todo-from-children-or-siblings-or-parent))
  4650. (if org-enforce-todo-checkbox-dependencies
  4651. (add-hook 'org-blocker-hook
  4652. 'org-block-todo-from-checkboxes)
  4653. (remove-hook 'org-blocker-hook
  4654. 'org-block-todo-from-checkboxes))
  4655. ;; Align options lines
  4656. (org-set-local
  4657. 'align-mode-rules-list
  4658. '((org-in-buffer-settings
  4659. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4660. (modes . '(org-mode)))))
  4661. ;; Imenu
  4662. (org-set-local 'imenu-create-index-function
  4663. 'org-imenu-get-tree)
  4664. ;; Make isearch reveal context
  4665. (if (or (featurep 'xemacs)
  4666. (not (boundp 'outline-isearch-open-invisible-function)))
  4667. ;; Emacs 21 and XEmacs make use of the hook
  4668. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4669. ;; Emacs 22 deals with this through a special variable
  4670. (org-set-local 'outline-isearch-open-invisible-function
  4671. (lambda (&rest ignore) (org-show-context 'isearch)))
  4672. (org-add-hook 'isearch-mode-end-hook 'org-fix-ellipsis-at-bol 'append 'local))
  4673. ;; Setup the pcomplete hooks
  4674. (set (make-local-variable 'pcomplete-command-completion-function)
  4675. 'org-pcomplete-initial)
  4676. (set (make-local-variable 'pcomplete-command-name-function)
  4677. 'org-command-at-point)
  4678. (set (make-local-variable 'pcomplete-default-completion-function)
  4679. 'ignore)
  4680. (set (make-local-variable 'pcomplete-parse-arguments-function)
  4681. 'org-parse-arguments)
  4682. (set (make-local-variable 'pcomplete-termination-string) "")
  4683. (when (>= emacs-major-version 23)
  4684. (set (make-local-variable 'buffer-face-mode-face) 'org-default))
  4685. ;; If empty file that did not turn on org-mode automatically, make it to.
  4686. (if (and org-insert-mode-line-in-empty-file
  4687. (org-called-interactively-p 'any)
  4688. (= (point-min) (point-max)))
  4689. (insert "# -*- mode: org -*-\n\n"))
  4690. (unless org-inhibit-startup
  4691. (org-unmodified
  4692. (and org-startup-with-beamer-mode (org-beamer-mode))
  4693. (when org-startup-align-all-tables
  4694. (org-table-map-tables 'org-table-align 'quietly))
  4695. (when org-startup-with-inline-images
  4696. (org-display-inline-images))
  4697. (when org-startup-with-latex-preview
  4698. (org-preview-latex-fragment))
  4699. (unless org-inhibit-startup-visibility-stuff
  4700. (org-set-startup-visibility))))
  4701. ;; Try to set org-hide correctly
  4702. (set-face-foreground 'org-hide (org-find-invisible-foreground)))
  4703. ;; Update `customize-package-emacs-version-alist'
  4704. (add-to-list 'customize-package-emacs-version-alist
  4705. '(Org ("6.21b" . "23.1") ("6.33x" . "23.2")
  4706. ("7.8.11" . "24.1") ("7.9.4" . "24.3")
  4707. ("8.0" . "24.4")))
  4708. (defvar org-mode-transpose-word-syntax-table
  4709. (let ((st (make-syntax-table)))
  4710. (mapc (lambda(c) (modify-syntax-entry
  4711. (string-to-char (car c)) "w p" st))
  4712. org-emphasis-alist)
  4713. st))
  4714. (when (fboundp 'abbrev-table-put)
  4715. (abbrev-table-put org-mode-abbrev-table
  4716. :parents (list text-mode-abbrev-table)))
  4717. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4718. (defsubst org-fix-ellipsis-at-bol ()
  4719. (save-excursion (goto-char (window-start)) (recenter 0)))
  4720. (defun org-find-invisible-foreground ()
  4721. (let ((candidates (remove
  4722. "unspecified-bg"
  4723. (nconc
  4724. (list (face-background 'default)
  4725. (face-background 'org-default))
  4726. (mapcar
  4727. (lambda (alist)
  4728. (when (boundp alist)
  4729. (cdr (assoc 'background-color (symbol-value alist)))))
  4730. '(default-frame-alist initial-frame-alist window-system-default-frame-alist))
  4731. (list (face-foreground 'org-hide))))))
  4732. (car (remove nil candidates))))
  4733. (defun org-current-time (&optional rounding-minutes past)
  4734. "Current time, possibly rounded to ROUNDING-MINUTES.
  4735. When ROUNDING-MINUTES is not an integer, fall back on the car of
  4736. `org-time-stamp-rounding-minutes'. When PAST is non-nil, ensure
  4737. the rounding returns a past time."
  4738. (let ((r (or (and (integerp rounding-minutes) rounding-minutes)
  4739. (car org-time-stamp-rounding-minutes)))
  4740. (time (decode-time)) res)
  4741. (if (< r 1)
  4742. (current-time)
  4743. (setq res
  4744. (apply 'encode-time
  4745. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4746. (nthcdr 2 time))))
  4747. (if (and past (< (org-float-time (time-subtract (current-time) res)) 0))
  4748. (seconds-to-time (- (org-float-time res) (* r 60)))
  4749. res))))
  4750. (defun org-today ()
  4751. "Return today date, considering `org-extend-today-until'."
  4752. (time-to-days
  4753. (time-subtract (current-time)
  4754. (list 0 (* 3600 org-extend-today-until) 0))))
  4755. ;;;; Font-Lock stuff, including the activators
  4756. (defvar org-mouse-map (make-sparse-keymap))
  4757. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4758. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4759. (when org-mouse-1-follows-link
  4760. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4761. (when org-tab-follows-link
  4762. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4763. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4764. (require 'font-lock)
  4765. (defconst org-non-link-chars "]\t\n\r<>")
  4766. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  4767. "shell" "elisp" "doi" "message"))
  4768. (defvar org-link-types-re nil
  4769. "Matches a link that has a url-like prefix like \"http:\"")
  4770. (defvar org-link-re-with-space nil
  4771. "Matches a link with spaces, optional angular brackets around it.")
  4772. (defvar org-link-re-with-space2 nil
  4773. "Matches a link with spaces, optional angular brackets around it.")
  4774. (defvar org-link-re-with-space3 nil
  4775. "Matches a link with spaces, only for internal part in bracket links.")
  4776. (defvar org-angle-link-re nil
  4777. "Matches link with angular brackets, spaces are allowed.")
  4778. (defvar org-plain-link-re nil
  4779. "Matches plain link, without spaces.")
  4780. (defvar org-bracket-link-regexp nil
  4781. "Matches a link in double brackets.")
  4782. (defvar org-bracket-link-analytic-regexp nil
  4783. "Regular expression used to analyze links.
  4784. Here is what the match groups contain after a match:
  4785. 1: http:
  4786. 2: http
  4787. 3: path
  4788. 4: [desc]
  4789. 5: desc")
  4790. (defvar org-bracket-link-analytic-regexp++ nil
  4791. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4792. (defvar org-any-link-re nil
  4793. "Regular expression matching any link.")
  4794. (defconst org-match-sexp-depth 3
  4795. "Number of stacked braces for sub/superscript matching.")
  4796. (defun org-create-multibrace-regexp (left right n)
  4797. "Create a regular expression which will match a balanced sexp.
  4798. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4799. as single character strings.
  4800. The regexp returned will match the entire expression including the
  4801. delimiters. It will also define a single group which contains the
  4802. match except for the outermost delimiters. The maximum depth of
  4803. stacked delimiters is N. Escaping delimiters is not possible."
  4804. (let* ((nothing (concat "[^" left right "]*?"))
  4805. (or "\\|")
  4806. (re nothing)
  4807. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4808. (while (> n 1)
  4809. (setq n (1- n)
  4810. re (concat re or next)
  4811. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4812. (concat left "\\(" re "\\)" right)))
  4813. (defvar org-match-substring-regexp
  4814. (concat
  4815. "\\(\\S-\\)\\([_^]\\)\\("
  4816. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4817. "\\|"
  4818. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  4819. "\\|"
  4820. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  4821. "The regular expression matching a sub- or superscript.")
  4822. (defvar org-match-substring-with-braces-regexp
  4823. (concat
  4824. "\\(\\S-\\)\\([_^]\\)\\("
  4825. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4826. "\\)")
  4827. "The regular expression matching a sub- or superscript, forcing braces.")
  4828. (defun org-make-link-regexps ()
  4829. "Update the link regular expressions.
  4830. This should be called after the variable `org-link-types' has changed."
  4831. (setq org-link-types-re
  4832. (concat
  4833. "\\`\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):")
  4834. org-link-re-with-space
  4835. (concat
  4836. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4837. "\\([^" org-non-link-chars " ]"
  4838. "[^" org-non-link-chars "]*"
  4839. "[^" org-non-link-chars " ]\\)>?")
  4840. org-link-re-with-space2
  4841. (concat
  4842. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4843. "\\([^" org-non-link-chars " ]"
  4844. "[^\t\n\r]*"
  4845. "[^" org-non-link-chars " ]\\)>?")
  4846. org-link-re-with-space3
  4847. (concat
  4848. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4849. "\\([^" org-non-link-chars " ]"
  4850. "[^\t\n\r]*\\)")
  4851. org-angle-link-re
  4852. (concat
  4853. "<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4854. "\\([^" org-non-link-chars " ]"
  4855. "[^" org-non-link-chars "]*"
  4856. "\\)>")
  4857. org-plain-link-re
  4858. (concat
  4859. "\\<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4860. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  4861. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4862. org-bracket-link-regexp
  4863. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4864. org-bracket-link-analytic-regexp
  4865. (concat
  4866. "\\[\\["
  4867. "\\(\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):\\)?"
  4868. "\\([^]]+\\)"
  4869. "\\]"
  4870. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4871. "\\]")
  4872. org-bracket-link-analytic-regexp++
  4873. (concat
  4874. "\\[\\["
  4875. "\\(\\(" (mapconcat 'regexp-quote (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  4876. "\\([^]]+\\)"
  4877. "\\]"
  4878. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4879. "\\]")
  4880. org-any-link-re
  4881. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4882. org-angle-link-re "\\)\\|\\("
  4883. org-plain-link-re "\\)")))
  4884. (org-make-link-regexps)
  4885. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
  4886. "Regular expression for fast time stamp matching.")
  4887. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
  4888. "Regular expression for fast time stamp matching.")
  4889. (defconst org-ts-regexp0
  4890. "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\( +[^]+0-9>\r\n -]+\\)?\\( +\\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4891. "Regular expression matching time strings for analysis.
  4892. This one does not require the space after the date, so it can be used
  4893. on a string that terminates immediately after the date.")
  4894. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4895. "Regular expression matching time strings for analysis.")
  4896. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  4897. "Regular expression matching time stamps, with groups.")
  4898. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  4899. "Regular expression matching time stamps (also [..]), with groups.")
  4900. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  4901. "Regular expression matching a time stamp range.")
  4902. (defconst org-tr-regexp-both
  4903. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  4904. "Regular expression matching a time stamp range.")
  4905. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  4906. org-ts-regexp "\\)?")
  4907. "Regular expression matching a time stamp or time stamp range.")
  4908. (defconst org-tsr-regexp-both
  4909. (concat org-ts-regexp-both "\\(--?-?"
  4910. org-ts-regexp-both "\\)?")
  4911. "Regular expression matching a time stamp or time stamp range.
  4912. The time stamps may be either active or inactive.")
  4913. (defvar org-emph-face nil)
  4914. (defun org-do-emphasis-faces (limit)
  4915. "Run through the buffer and add overlays to emphasized strings."
  4916. (let (rtn a)
  4917. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  4918. (if (not (= (char-after (match-beginning 3))
  4919. (char-after (match-beginning 4))))
  4920. (progn
  4921. (setq rtn t)
  4922. (setq a (assoc (match-string 3) org-emphasis-alist))
  4923. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  4924. 'face
  4925. (nth 1 a))
  4926. (and (nth 4 a)
  4927. (org-remove-flyspell-overlays-in
  4928. (match-beginning 0) (match-end 0)))
  4929. (add-text-properties (match-beginning 2) (match-end 2)
  4930. '(font-lock-multiline t org-emphasis t))
  4931. (when org-hide-emphasis-markers
  4932. (add-text-properties (match-end 4) (match-beginning 5)
  4933. '(invisible org-link))
  4934. (add-text-properties (match-beginning 3) (match-end 3)
  4935. '(invisible org-link)))))
  4936. (backward-char 1))
  4937. rtn))
  4938. (defun org-emphasize (&optional char)
  4939. "Insert or change an emphasis, i.e. a font like bold or italic.
  4940. If there is an active region, change that region to a new emphasis.
  4941. If there is no region, just insert the marker characters and position
  4942. the cursor between them.
  4943. CHAR should be either the marker character, or the first character of the
  4944. HTML tag associated with that emphasis. If CHAR is a space, the means
  4945. to remove the emphasis of the selected region.
  4946. If char is not given (for example in an interactive call) it
  4947. will be prompted for."
  4948. (interactive)
  4949. (let ((eal org-emphasis-alist) e det
  4950. (erc org-emphasis-regexp-components)
  4951. (prompt "")
  4952. (string "") beg end move tag c s)
  4953. (if (org-region-active-p)
  4954. (setq beg (region-beginning) end (region-end)
  4955. string (buffer-substring beg end))
  4956. (setq move t))
  4957. (while (setq e (pop eal))
  4958. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  4959. c (aref tag 0))
  4960. (push (cons c (string-to-char (car e))) det)
  4961. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  4962. (substring tag 1)))))
  4963. (setq det (nreverse det))
  4964. (unless char
  4965. (message "%s" (concat "Emphasis marker or tag:" prompt))
  4966. (setq char (read-char-exclusive)))
  4967. (setq char (or (cdr (assoc char det)) char))
  4968. (if (equal char ?\ )
  4969. (setq s "" move nil)
  4970. (unless (assoc (char-to-string char) org-emphasis-alist)
  4971. (error "No such emphasis marker: \"%c\"" char))
  4972. (setq s (char-to-string char)))
  4973. (while (and (> (length string) 1)
  4974. (equal (substring string 0 1) (substring string -1))
  4975. (assoc (substring string 0 1) org-emphasis-alist))
  4976. (setq string (substring string 1 -1)))
  4977. (setq string (concat s string s))
  4978. (if beg (delete-region beg end))
  4979. (unless (or (bolp)
  4980. (string-match (concat "[" (nth 0 erc) "\n]")
  4981. (char-to-string (char-before (point)))))
  4982. (insert " "))
  4983. (unless (or (eobp)
  4984. (string-match (concat "[" (nth 1 erc) "\n]")
  4985. (char-to-string (char-after (point)))))
  4986. (insert " ") (backward-char 1))
  4987. (insert string)
  4988. (and move (backward-char 1))))
  4989. (defconst org-nonsticky-props
  4990. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text htmlize-link))
  4991. (defsubst org-rear-nonsticky-at (pos)
  4992. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  4993. (defun org-activate-plain-links (limit)
  4994. "Run through the buffer and add overlays to links."
  4995. (let (f hl)
  4996. (when (and (re-search-forward (concat org-plain-link-re) limit t)
  4997. (not (org-in-src-block-p)))
  4998. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4999. (setq f (get-text-property (match-beginning 0) 'face))
  5000. (setq hl (org-match-string-no-properties 0))
  5001. (if (or (eq f 'org-tag)
  5002. (and (listp f) (memq 'org-tag f)))
  5003. nil
  5004. (add-text-properties (match-beginning 0) (match-end 0)
  5005. (list 'mouse-face 'highlight
  5006. 'face 'org-link
  5007. 'htmlize-link `(:uri ,hl)
  5008. 'keymap org-mouse-map))
  5009. (org-rear-nonsticky-at (match-end 0)))
  5010. t)))
  5011. (defun org-activate-code (limit)
  5012. (if (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t)
  5013. (progn
  5014. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5015. (remove-text-properties (match-beginning 0) (match-end 0)
  5016. '(display t invisible t intangible t))
  5017. t)))
  5018. (defcustom org-src-fontify-natively nil
  5019. "When non-nil, fontify code in code blocks."
  5020. :type 'boolean
  5021. :version "24.1"
  5022. :group 'org-appearance
  5023. :group 'org-babel)
  5024. (defcustom org-allow-promoting-top-level-subtree nil
  5025. "When non-nil, allow promoting a top level subtree.
  5026. The leading star of the top level headline will be replaced
  5027. by a #."
  5028. :type 'boolean
  5029. :version "24.1"
  5030. :group 'org-appearance)
  5031. (defun org-fontify-meta-lines-and-blocks (limit)
  5032. (condition-case nil
  5033. (org-fontify-meta-lines-and-blocks-1 limit)
  5034. (error (message "org-mode fontification error"))))
  5035. (defun org-fontify-meta-lines-and-blocks-1 (limit)
  5036. "Fontify #+ lines and blocks, in the correct ways."
  5037. (let ((case-fold-search t))
  5038. (if (re-search-forward
  5039. "^\\([ \t]*#\\(\\(\\+[a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  5040. limit t)
  5041. (let ((beg (match-beginning 0))
  5042. (block-start (match-end 0))
  5043. (block-end nil)
  5044. (lang (match-string 7))
  5045. (beg1 (line-beginning-position 2))
  5046. (dc1 (downcase (match-string 2)))
  5047. (dc3 (downcase (match-string 3)))
  5048. end end1 quoting block-type ovl)
  5049. (cond
  5050. ((member dc1 '("+html:" "+ascii:" "+latex:"))
  5051. ;; a single line of backend-specific content
  5052. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5053. (remove-text-properties (match-beginning 0) (match-end 0)
  5054. '(display t invisible t intangible t))
  5055. (add-text-properties (match-beginning 1) (match-end 3)
  5056. '(font-lock-fontified t face org-meta-line))
  5057. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  5058. '(font-lock-fontified t face org-block))
  5059. ; for backend-specific code
  5060. t)
  5061. ((and (match-end 4) (equal dc3 "+begin"))
  5062. ;; Truly a block
  5063. (setq block-type (downcase (match-string 5))
  5064. quoting (member block-type org-protecting-blocks))
  5065. (when (re-search-forward
  5066. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  5067. nil t) ;; on purpose, we look further than LIMIT
  5068. (setq end (min (point-max) (match-end 0))
  5069. end1 (min (point-max) (1- (match-beginning 0))))
  5070. (setq block-end (match-beginning 0))
  5071. (when quoting
  5072. (remove-text-properties beg end
  5073. '(display t invisible t intangible t)))
  5074. (add-text-properties
  5075. beg end
  5076. '(font-lock-fontified t font-lock-multiline t))
  5077. (add-text-properties beg beg1 '(face org-meta-line))
  5078. (add-text-properties end1 (min (point-max) (1+ end))
  5079. '(face org-meta-line)) ; for end_src
  5080. (cond
  5081. ((and lang (not (string= lang "")) org-src-fontify-natively)
  5082. (org-src-font-lock-fontify-block lang block-start block-end)
  5083. ;; remove old background overlays
  5084. (mapc (lambda (ov)
  5085. (if (eq (overlay-get ov 'face) 'org-block-background)
  5086. (delete-overlay ov)))
  5087. (overlays-at (/ (+ beg1 block-end) 2)))
  5088. ;; add a background overlay
  5089. (setq ovl (make-overlay beg1 block-end))
  5090. (overlay-put ovl 'face 'org-block-background)
  5091. (overlay-put ovl 'evaporate t)) ;; make it go away when empty
  5092. (quoting
  5093. (add-text-properties beg1 (min (point-max) (1+ end1))
  5094. '(face org-block))) ; end of source block
  5095. ((not org-fontify-quote-and-verse-blocks))
  5096. ((string= block-type "quote")
  5097. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-quote)))
  5098. ((string= block-type "verse")
  5099. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-verse))))
  5100. (add-text-properties beg beg1 '(face org-block-begin-line))
  5101. (add-text-properties (min (point-max) (1+ end)) (min (point-max) (1+ end1))
  5102. '(face org-block-end-line))
  5103. t))
  5104. ((member dc1 '("+title:" "+author:" "+email:" "+date:"))
  5105. (add-text-properties
  5106. beg (match-end 3)
  5107. (if (member (intern (substring dc1 0 -1)) org-hidden-keywords)
  5108. '(font-lock-fontified t invisible t)
  5109. '(font-lock-fontified t face org-document-info-keyword)))
  5110. (add-text-properties
  5111. (match-beginning 6) (min (point-max) (1+ (match-end 6)))
  5112. (if (string-equal dc1 "+title:")
  5113. '(font-lock-fontified t face org-document-title)
  5114. '(font-lock-fontified t face org-document-info))))
  5115. ((or (equal dc1 "+results")
  5116. (member dc1 '("+begin:" "+end:" "+caption:" "+label:"
  5117. "+orgtbl:" "+tblfm:" "+tblname:" "+results:"
  5118. "+call:" "+header:" "+headers:" "+name:"))
  5119. (and (match-end 4) (equal dc3 "+attr")))
  5120. (add-text-properties
  5121. beg (match-end 0)
  5122. '(font-lock-fontified t face org-meta-line))
  5123. t)
  5124. ((member dc3 '(" " ""))
  5125. (add-text-properties
  5126. beg (match-end 0)
  5127. '(font-lock-fontified t face font-lock-comment-face)))
  5128. ((not (member (char-after beg) '(?\ ?\t)))
  5129. ;; just any other in-buffer setting, but not indented
  5130. (add-text-properties
  5131. beg (match-end 0)
  5132. '(font-lock-fontified t face org-meta-line))
  5133. t)
  5134. (t nil))))))
  5135. (defun org-activate-angle-links (limit)
  5136. "Run through the buffer and add overlays to links."
  5137. (if (and (re-search-forward org-angle-link-re limit t)
  5138. (not (org-in-src-block-p)))
  5139. (progn
  5140. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5141. (add-text-properties (match-beginning 0) (match-end 0)
  5142. (list 'mouse-face 'highlight
  5143. 'keymap org-mouse-map))
  5144. (org-rear-nonsticky-at (match-end 0))
  5145. t)))
  5146. (defun org-activate-footnote-links (limit)
  5147. "Run through the buffer and add overlays to footnotes."
  5148. (let ((fn (org-footnote-next-reference-or-definition limit)))
  5149. (when fn
  5150. (let ((beg (nth 1 fn)) (end (nth 2 fn)))
  5151. (org-remove-flyspell-overlays-in beg end)
  5152. (add-text-properties beg end
  5153. (list 'mouse-face 'highlight
  5154. 'keymap org-mouse-map
  5155. 'help-echo
  5156. (if (= (point-at-bol) beg)
  5157. "Footnote definition"
  5158. "Footnote reference")
  5159. 'font-lock-fontified t
  5160. 'font-lock-multiline t
  5161. 'face 'org-footnote))))))
  5162. (defun org-activate-bracket-links (limit)
  5163. "Run through the buffer and add overlays to bracketed links."
  5164. (if (and (re-search-forward org-bracket-link-regexp limit t)
  5165. (not (org-in-src-block-p)))
  5166. (let* ((hl (org-match-string-no-properties 1))
  5167. (help (concat "LINK: " hl))
  5168. ;; FIXME: Above we should remove the escapes. But that
  5169. ;; requires another match, protecting match data, a lot
  5170. ;; of overhead for font-lock.
  5171. (ip (org-maybe-intangible
  5172. (list 'invisible 'org-link
  5173. 'keymap org-mouse-map 'mouse-face 'highlight
  5174. 'font-lock-multiline t 'help-echo help
  5175. 'htmlize-link `(:uri ,hl))))
  5176. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  5177. 'font-lock-multiline t 'help-echo help
  5178. 'htmlize-link `(:uri ,hl))))
  5179. ;; We need to remove the invisible property here. Table narrowing
  5180. ;; may have made some of this invisible.
  5181. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5182. (remove-text-properties (match-beginning 0) (match-end 0)
  5183. '(invisible nil))
  5184. (if (match-end 3)
  5185. (progn
  5186. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  5187. (org-rear-nonsticky-at (match-beginning 3))
  5188. (add-text-properties (match-beginning 3) (match-end 3) vp)
  5189. (org-rear-nonsticky-at (match-end 3))
  5190. (add-text-properties (match-end 3) (match-end 0) ip)
  5191. (org-rear-nonsticky-at (match-end 0)))
  5192. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  5193. (org-rear-nonsticky-at (match-beginning 1))
  5194. (add-text-properties (match-beginning 1) (match-end 1) vp)
  5195. (org-rear-nonsticky-at (match-end 1))
  5196. (add-text-properties (match-end 1) (match-end 0) ip)
  5197. (org-rear-nonsticky-at (match-end 0)))
  5198. t)))
  5199. (defun org-activate-dates (limit)
  5200. "Run through the buffer and add overlays to dates."
  5201. (if (and (re-search-forward org-tsr-regexp-both limit t)
  5202. (not (equal (char-before (match-beginning 0)) 91)))
  5203. (progn
  5204. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5205. (add-text-properties (match-beginning 0) (match-end 0)
  5206. (list 'mouse-face 'highlight
  5207. 'keymap org-mouse-map))
  5208. (org-rear-nonsticky-at (match-end 0))
  5209. (when org-display-custom-times
  5210. (if (match-end 3)
  5211. (org-display-custom-time (match-beginning 3) (match-end 3)))
  5212. (org-display-custom-time (match-beginning 1) (match-end 1)))
  5213. t)))
  5214. (defvar org-target-link-regexp nil
  5215. "Regular expression matching radio targets in plain text.")
  5216. (make-variable-buffer-local 'org-target-link-regexp)
  5217. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  5218. "Regular expression matching a link target.")
  5219. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  5220. "Regular expression matching a radio target.")
  5221. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  5222. "Regular expression matching any target.")
  5223. (defun org-activate-target-links (limit)
  5224. "Run through the buffer and add overlays to target matches."
  5225. (when org-target-link-regexp
  5226. (let ((case-fold-search t))
  5227. (if (re-search-forward org-target-link-regexp limit t)
  5228. (progn
  5229. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5230. (add-text-properties (match-beginning 0) (match-end 0)
  5231. (list 'mouse-face 'highlight
  5232. 'keymap org-mouse-map
  5233. 'help-echo "Radio target link"
  5234. 'org-linked-text t))
  5235. (org-rear-nonsticky-at (match-end 0))
  5236. t)))))
  5237. (defun org-update-radio-target-regexp ()
  5238. "Find all radio targets in this file and update the regular expression."
  5239. (interactive)
  5240. (when (memq 'radio org-activate-links)
  5241. (setq org-target-link-regexp
  5242. (org-make-target-link-regexp (org-all-targets 'radio)))
  5243. (org-restart-font-lock)))
  5244. (defun org-hide-wide-columns (limit)
  5245. (let (s e)
  5246. (setq s (text-property-any (point) (or limit (point-max))
  5247. 'org-cwidth t))
  5248. (when s
  5249. (setq e (next-single-property-change s 'org-cwidth))
  5250. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  5251. (goto-char e)
  5252. t)))
  5253. (defvar org-latex-and-related-regexp nil
  5254. "Regular expression for highlighting LaTeX, entities and sub/superscript.")
  5255. (defvar org-match-substring-regexp)
  5256. (defvar org-match-substring-with-braces-regexp)
  5257. (defun org-compute-latex-and-related-regexp ()
  5258. "Compute regular expression for LaTeX, entities and sub/superscript.
  5259. Result depends on variable `org-highlight-latex-and-related'."
  5260. (org-set-local
  5261. 'org-latex-and-related-regexp
  5262. (let* ((re-sub
  5263. (cond ((not (memq 'script org-highlight-latex-and-related)) nil)
  5264. ((eq org-use-sub-superscripts '{})
  5265. (list org-match-substring-with-braces-regexp))
  5266. (org-use-sub-superscripts (list org-match-substring-regexp))))
  5267. (re-latex
  5268. (when (memq 'latex org-highlight-latex-and-related)
  5269. (let ((matchers (plist-get org-format-latex-options :matchers)))
  5270. (delq nil
  5271. (mapcar (lambda (x)
  5272. (and (member (car x) matchers) (nth 1 x)))
  5273. org-latex-regexps)))))
  5274. (re-entities
  5275. (when (memq 'entities org-highlight-latex-and-related)
  5276. (list "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))))
  5277. (mapconcat 'identity (append re-latex re-entities re-sub) "\\|"))))
  5278. (defun org-do-latex-and-related (limit)
  5279. "Highlight LaTeX snippets and environments, entities and sub/superscript.
  5280. LIMIT bounds the search for syntax to highlight. Stop at first
  5281. highlighted object, if any. Return t if some highlighting was
  5282. done, nil otherwise."
  5283. (when (org-string-nw-p org-latex-and-related-regexp)
  5284. (catch 'found
  5285. (while (re-search-forward org-latex-and-related-regexp limit t)
  5286. (unless (memq (car-safe (get-text-property (1+ (match-beginning 0))
  5287. 'face))
  5288. '(org-code org-verbatim underline))
  5289. (let ((offset (if (memq (char-after (1+ (match-beginning 0)))
  5290. '(?_ ?^))
  5291. 1
  5292. 0)))
  5293. (font-lock-prepend-text-property
  5294. (+ offset (match-beginning 0)) (match-end 0)
  5295. 'face 'org-latex-and-related)
  5296. (add-text-properties (+ offset (match-beginning 0)) (match-end 0)
  5297. '(font-lock-multiline t)))
  5298. (throw 'found t)))
  5299. nil)))
  5300. (defun org-restart-font-lock ()
  5301. "Restart `font-lock-mode', to force refontification."
  5302. (when (and (boundp 'font-lock-mode) font-lock-mode)
  5303. (font-lock-mode -1)
  5304. (font-lock-mode 1)))
  5305. (defun org-all-targets (&optional radio)
  5306. "Return a list of all targets in this file.
  5307. When optional argument RADIO is non-nil, only find radio
  5308. targets."
  5309. (let ((re (if radio org-radio-target-regexp org-target-regexp)) rtn)
  5310. (save-excursion
  5311. (goto-char (point-min))
  5312. (while (re-search-forward re nil t)
  5313. ;; Make sure point is really within the object.
  5314. (backward-char)
  5315. (let ((obj (org-element-context)))
  5316. (when (memq (org-element-type obj) '(radio-target target))
  5317. (add-to-list 'rtn (downcase (org-element-property :value obj))))))
  5318. rtn)))
  5319. (defun org-make-target-link-regexp (targets)
  5320. "Make regular expression matching all strings in TARGETS.
  5321. The regular expression finds the targets also if there is a line break
  5322. between words."
  5323. (and targets
  5324. (concat
  5325. "\\<\\("
  5326. (mapconcat
  5327. (lambda (x)
  5328. (setq x (regexp-quote x))
  5329. (while (string-match " +" x)
  5330. (setq x (replace-match "\\s-+" t t x)))
  5331. x)
  5332. targets
  5333. "\\|")
  5334. "\\)\\>")))
  5335. (defun org-activate-tags (limit)
  5336. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t)
  5337. (progn
  5338. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5339. (add-text-properties (match-beginning 1) (match-end 1)
  5340. (list 'mouse-face 'highlight
  5341. 'keymap org-mouse-map))
  5342. (org-rear-nonsticky-at (match-end 1))
  5343. t)))
  5344. (defun org-outline-level ()
  5345. "Compute the outline level of the heading at point.
  5346. If this is called at a normal headline, the level is the number of stars.
  5347. Use `org-reduced-level' to remove the effect of `org-odd-levels'."
  5348. (save-excursion
  5349. (if (not (condition-case nil
  5350. (org-back-to-heading t)
  5351. (error nil)))
  5352. 0
  5353. (looking-at org-outline-regexp)
  5354. (1- (- (match-end 0) (match-beginning 0))))))
  5355. (defvar org-font-lock-keywords nil)
  5356. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\+?\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  5357. "Regular expression matching a property line.")
  5358. (defvar org-font-lock-hook nil
  5359. "Functions to be called for special font lock stuff.")
  5360. (defvar org-font-lock-set-keywords-hook nil
  5361. "Functions that can manipulate `org-font-lock-extra-keywords'.
  5362. This is called after `org-font-lock-extra-keywords' is defined, but before
  5363. it is installed to be used by font lock. This can be useful if something
  5364. needs to be inserted at a specific position in the font-lock sequence.")
  5365. (defun org-font-lock-hook (limit)
  5366. "Run `org-font-lock-hook' within LIMIT."
  5367. (run-hook-with-args 'org-font-lock-hook limit))
  5368. (defun org-set-font-lock-defaults ()
  5369. "Set font lock defaults for the current buffer."
  5370. (let* ((em org-fontify-emphasized-text)
  5371. (lk org-activate-links)
  5372. (org-font-lock-extra-keywords
  5373. (list
  5374. ;; Call the hook
  5375. '(org-font-lock-hook)
  5376. ;; Headlines
  5377. `(,(if org-fontify-whole-heading-line
  5378. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  5379. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  5380. (1 (org-get-level-face 1))
  5381. (2 (org-get-level-face 2))
  5382. (3 (org-get-level-face 3)))
  5383. ;; Table lines
  5384. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  5385. (1 'org-table t))
  5386. ;; Table internals
  5387. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  5388. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  5389. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  5390. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  5391. ;; Drawers
  5392. (list org-drawer-regexp '(0 'org-special-keyword t))
  5393. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  5394. ;; Properties
  5395. (list org-property-re
  5396. '(1 'org-special-keyword t)
  5397. '(3 'org-property-value t))
  5398. ;; Links
  5399. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  5400. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  5401. (if (memq 'plain lk) '(org-activate-plain-links))
  5402. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  5403. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  5404. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  5405. (if (memq 'footnote lk) '(org-activate-footnote-links))
  5406. ;; Targets.
  5407. (list org-any-target-regexp '(0 'org-target t))
  5408. ;; Diary sexps.
  5409. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  5410. '(org-hide-wide-columns (0 nil append))
  5411. ;; TODO keyword
  5412. (list (format org-heading-keyword-regexp-format
  5413. org-todo-regexp)
  5414. '(2 (org-get-todo-face 2) t))
  5415. ;; DONE
  5416. (if org-fontify-done-headline
  5417. (list (format org-heading-keyword-regexp-format
  5418. (concat
  5419. "\\(?:"
  5420. (mapconcat 'regexp-quote org-done-keywords "\\|")
  5421. "\\)"))
  5422. '(2 'org-headline-done t))
  5423. nil)
  5424. ;; Priorities
  5425. '(org-font-lock-add-priority-faces)
  5426. ;; Tags
  5427. '(org-font-lock-add-tag-faces)
  5428. ;; Special keywords
  5429. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  5430. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  5431. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  5432. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  5433. ;; Emphasis
  5434. (if em
  5435. (if (featurep 'xemacs)
  5436. '(org-do-emphasis-faces (0 nil append))
  5437. '(org-do-emphasis-faces)))
  5438. ;; Checkboxes
  5439. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  5440. 1 'org-checkbox prepend)
  5441. (if (cdr (assq 'checkbox org-list-automatic-rules))
  5442. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5443. (0 (org-get-checkbox-statistics-face) t)))
  5444. ;; Description list items
  5445. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  5446. 1 'org-list-dt prepend)
  5447. ;; ARCHIVEd headings
  5448. (list (concat
  5449. org-outline-regexp-bol
  5450. "\\(.*:" org-archive-tag ":.*\\)")
  5451. '(1 'org-archived prepend))
  5452. ;; Specials
  5453. '(org-do-latex-and-related)
  5454. '(org-fontify-entities)
  5455. '(org-raise-scripts)
  5456. ;; Code
  5457. '(org-activate-code (1 'org-code t))
  5458. ;; COMMENT
  5459. (list (format org-heading-keyword-regexp-format
  5460. (concat "\\("
  5461. org-comment-string "\\|" org-quote-string
  5462. "\\)"))
  5463. '(2 'org-special-keyword t))
  5464. ;; Blocks and meta lines
  5465. '(org-fontify-meta-lines-and-blocks)
  5466. )))
  5467. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5468. (run-hooks 'org-font-lock-set-keywords-hook)
  5469. ;; Now set the full font-lock-keywords
  5470. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  5471. (org-set-local 'font-lock-defaults
  5472. '(org-font-lock-keywords t nil nil backward-paragraph))
  5473. (kill-local-variable 'font-lock-keywords) nil))
  5474. (defun org-toggle-pretty-entities ()
  5475. "Toggle the composition display of entities as UTF8 characters."
  5476. (interactive)
  5477. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  5478. (org-restart-font-lock)
  5479. (if org-pretty-entities
  5480. (message "Entities are displayed as UTF8 characters")
  5481. (save-restriction
  5482. (widen)
  5483. (org-decompose-region (point-min) (point-max))
  5484. (message "Entities are displayed plain"))))
  5485. (defvar org-custom-properties-overlays nil
  5486. "List of overlays used for custom properties.")
  5487. (make-variable-buffer-local 'org-custom-properties-overlays)
  5488. (defun org-toggle-custom-properties-visibility ()
  5489. "Display or hide properties in `org-custom-properties'."
  5490. (interactive)
  5491. (if org-custom-properties-overlays
  5492. (progn (mapc 'delete-overlay org-custom-properties-overlays)
  5493. (setq org-custom-properties-overlays nil))
  5494. (unless (not org-custom-properties)
  5495. (save-excursion
  5496. (save-restriction
  5497. (widen)
  5498. (goto-char (point-min))
  5499. (while (re-search-forward org-property-re nil t)
  5500. (mapc (lambda(p)
  5501. (when (equal p (substring (match-string 1) 1 -1))
  5502. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5503. (overlay-put o 'invisible t)
  5504. (overlay-put o 'org-custom-property t)
  5505. (push o org-custom-properties-overlays))))
  5506. org-custom-properties)))))))
  5507. (defun org-fontify-entities (limit)
  5508. "Find an entity to fontify."
  5509. (let (ee)
  5510. (when org-pretty-entities
  5511. (catch 'match
  5512. (while (re-search-forward
  5513. "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]\n]\\)"
  5514. limit t)
  5515. (if (and (not (org-in-indented-comment-line))
  5516. (setq ee (org-entity-get (match-string 1)))
  5517. (= (length (nth 6 ee)) 1))
  5518. (let*
  5519. ((end (if (equal (match-string 2) "{}")
  5520. (match-end 2)
  5521. (match-end 1))))
  5522. (add-text-properties
  5523. (match-beginning 0) end
  5524. (list 'font-lock-fontified t))
  5525. (compose-region (match-beginning 0) end
  5526. (nth 6 ee) nil)
  5527. (backward-char 1)
  5528. (throw 'match t))))
  5529. nil))))
  5530. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5531. "Fontify string S like in Org-mode."
  5532. (with-temp-buffer
  5533. (insert s)
  5534. (let ((org-odd-levels-only odd-levels))
  5535. (org-mode)
  5536. (font-lock-fontify-buffer)
  5537. (buffer-string))))
  5538. (defvar org-m nil)
  5539. (defvar org-l nil)
  5540. (defvar org-f nil)
  5541. (defun org-get-level-face (n)
  5542. "Get the right face for match N in font-lock matching of headlines."
  5543. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5544. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5545. (if org-cycle-level-faces
  5546. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5547. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5548. (cond
  5549. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5550. ((eq n 2) org-f)
  5551. (t (if org-level-color-stars-only nil org-f))))
  5552. (defun org-get-todo-face (kwd)
  5553. "Get the right face for a TODO keyword KWD.
  5554. If KWD is a number, get the corresponding match group."
  5555. (if (numberp kwd) (setq kwd (match-string kwd)))
  5556. (or (org-face-from-face-or-color
  5557. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5558. (and (member kwd org-done-keywords) 'org-done)
  5559. 'org-todo))
  5560. (defun org-face-from-face-or-color (context inherit face-or-color)
  5561. "Create a face list that inherits INHERIT, but sets the foreground color.
  5562. When FACE-OR-COLOR is not a string, just return it."
  5563. (if (stringp face-or-color)
  5564. (list :inherit inherit
  5565. (cdr (assoc context org-faces-easy-properties))
  5566. face-or-color)
  5567. face-or-color))
  5568. (defun org-font-lock-add-tag-faces (limit)
  5569. "Add the special tag faces."
  5570. (when (and org-tag-faces org-tags-special-faces-re)
  5571. (while (re-search-forward org-tags-special-faces-re limit t)
  5572. (add-text-properties (match-beginning 1) (match-end 1)
  5573. (list 'face (org-get-tag-face 1)
  5574. 'font-lock-fontified t))
  5575. (backward-char 1))))
  5576. (defun org-font-lock-add-priority-faces (limit)
  5577. "Add the special priority faces."
  5578. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  5579. (when (save-match-data (org-at-heading-p))
  5580. (add-text-properties
  5581. (match-beginning 0) (match-end 0)
  5582. (list 'face (or (org-face-from-face-or-color
  5583. 'priority 'org-priority
  5584. (cdr (assoc (char-after (match-beginning 1))
  5585. org-priority-faces)))
  5586. 'org-priority)
  5587. 'font-lock-fontified t)))))
  5588. (defun org-get-tag-face (kwd)
  5589. "Get the right face for a TODO keyword KWD.
  5590. If KWD is a number, get the corresponding match group."
  5591. (if (numberp kwd) (setq kwd (match-string kwd)))
  5592. (or (org-face-from-face-or-color
  5593. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  5594. 'org-tag))
  5595. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5596. "Remove fontification and activation overlays from links."
  5597. (font-lock-default-unfontify-region beg end)
  5598. (let* ((buffer-undo-list t)
  5599. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5600. (inhibit-modification-hooks t)
  5601. deactivate-mark buffer-file-name buffer-file-truename)
  5602. (org-decompose-region beg end)
  5603. (remove-text-properties beg end
  5604. '(mouse-face t keymap t org-linked-text t
  5605. invisible t intangible t
  5606. org-no-flyspell t org-emphasis t))
  5607. (org-remove-font-lock-display-properties beg end)))
  5608. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5609. ((raise 0.3) (height 0.7))
  5610. ((raise -0.5))
  5611. ((raise 0.5)))
  5612. "Display properties for showing superscripts and subscripts.")
  5613. (defun org-remove-font-lock-display-properties (beg end)
  5614. "Remove specific display properties that have been added by font lock.
  5615. The will remove the raise properties that are used to show superscripts
  5616. and subscripts."
  5617. (let (next prop)
  5618. (while (< beg end)
  5619. (setq next (next-single-property-change beg 'display nil end)
  5620. prop (get-text-property beg 'display))
  5621. (if (member prop org-script-display)
  5622. (put-text-property beg next 'display nil))
  5623. (setq beg next))))
  5624. (defun org-raise-scripts (limit)
  5625. "Add raise properties to sub/superscripts."
  5626. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts)
  5627. (if (re-search-forward
  5628. (if (eq org-use-sub-superscripts t)
  5629. org-match-substring-regexp
  5630. org-match-substring-with-braces-regexp)
  5631. limit t)
  5632. (let* ((pos (point)) table-p comment-p
  5633. (mpos (match-beginning 3))
  5634. (emph-p (get-text-property mpos 'org-emphasis))
  5635. (link-p (get-text-property mpos 'mouse-face))
  5636. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5637. (goto-char (point-at-bol))
  5638. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5639. comment-p (org-looking-at-p "[ \t]*#"))
  5640. (goto-char pos)
  5641. ;; FIXME: Should we go back one character here, for a_b^c
  5642. ;; (goto-char (1- pos)) ;????????????????????
  5643. (if (or comment-p emph-p link-p keyw-p)
  5644. t
  5645. (put-text-property (match-beginning 3) (match-end 0)
  5646. 'display
  5647. (if (equal (char-after (match-beginning 2)) ?^)
  5648. (nth (if table-p 3 1) org-script-display)
  5649. (nth (if table-p 2 0) org-script-display)))
  5650. (add-text-properties (match-beginning 2) (match-end 2)
  5651. (list 'invisible t
  5652. 'org-dwidth t 'org-dwidth-n 1))
  5653. (if (and (eq (char-after (match-beginning 3)) ?{)
  5654. (eq (char-before (match-end 3)) ?}))
  5655. (progn
  5656. (add-text-properties
  5657. (match-beginning 3) (1+ (match-beginning 3))
  5658. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5659. (add-text-properties
  5660. (1- (match-end 3)) (match-end 3)
  5661. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))))
  5662. t)))))
  5663. ;;;; Visibility cycling, including org-goto and indirect buffer
  5664. ;;; Cycling
  5665. (defvar org-cycle-global-status nil)
  5666. (make-variable-buffer-local 'org-cycle-global-status)
  5667. (put 'org-cycle-global-status 'org-state t)
  5668. (defvar org-cycle-subtree-status nil)
  5669. (make-variable-buffer-local 'org-cycle-subtree-status)
  5670. (put 'org-cycle-subtree-status 'org-state t)
  5671. (defvar org-inlinetask-min-level)
  5672. ;;;###autoload
  5673. (defun org-cycle (&optional arg)
  5674. "TAB-action and visibility cycling for Org-mode.
  5675. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5676. is outline visibility cycling, but it also invokes other actions
  5677. in special contexts.
  5678. - When this function is called with a prefix argument, rotate the entire
  5679. buffer through 3 states (global cycling)
  5680. 1. OVERVIEW: Show only top-level headlines.
  5681. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5682. 3. SHOW ALL: Show everything.
  5683. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  5684. determined by the variable `org-startup-folded', and by any VISIBILITY
  5685. properties in the buffer.
  5686. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  5687. including any drawers.
  5688. - When inside a table, re-align the table and move to the next field.
  5689. - When point is at the beginning of a headline, rotate the subtree started
  5690. by this line through 3 different states (local cycling)
  5691. 1. FOLDED: Only the main headline is shown.
  5692. 2. CHILDREN: The main headline and the direct children are shown.
  5693. From this state, you can move to one of the children
  5694. and zoom in further.
  5695. 3. SUBTREE: Show the entire subtree, including body text.
  5696. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5697. - When point is at the beginning of an empty headline and the variable
  5698. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5699. of the headline by demoting and promoting it to likely levels. This
  5700. speeds up creation document structure by pressing TAB once or several
  5701. times right after creating a new headline.
  5702. - When there is a numeric prefix, go up to a heading with level ARG, do
  5703. a `show-subtree' and return to the previous cursor position. If ARG
  5704. is negative, go up that many levels.
  5705. - When point is not at the beginning of a headline, execute the global
  5706. binding for TAB, which is re-indenting the line. See the option
  5707. `org-cycle-emulate-tab' for details.
  5708. - Special case: if point is at the beginning of the buffer and there is
  5709. no headline in line 1, this function will act as if called with prefix arg
  5710. (C-u TAB, same as S-TAB) also when called without prefix arg.
  5711. But only if also the variable `org-cycle-global-at-bob' is t."
  5712. (interactive "P")
  5713. (org-load-modules-maybe)
  5714. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5715. (and org-cycle-level-after-item/entry-creation
  5716. (or (org-cycle-level)
  5717. (org-cycle-item-indentation))))
  5718. (let* (message-log-max ; Don't populate the *Messages* buffer
  5719. (limit-level
  5720. (or org-cycle-max-level
  5721. (and (boundp 'org-inlinetask-min-level)
  5722. org-inlinetask-min-level
  5723. (1- org-inlinetask-min-level))))
  5724. (nstars (and limit-level
  5725. (if org-odd-levels-only
  5726. (and limit-level (1- (* limit-level 2)))
  5727. limit-level)))
  5728. (org-outline-regexp
  5729. (if (not (derived-mode-p 'org-mode))
  5730. outline-regexp
  5731. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
  5732. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5733. (not (looking-at org-outline-regexp))))
  5734. (org-cycle-hook
  5735. (if bob-special
  5736. (delq 'org-optimize-window-after-visibility-change
  5737. (copy-sequence org-cycle-hook))
  5738. org-cycle-hook))
  5739. (pos (point)))
  5740. (if (or bob-special (equal arg '(4)))
  5741. ;; special case: use global cycling
  5742. (setq arg t))
  5743. (cond
  5744. ((equal arg '(16))
  5745. (setq last-command 'dummy)
  5746. (org-set-startup-visibility)
  5747. (message "Startup visibility, plus VISIBILITY properties"))
  5748. ((equal arg '(64))
  5749. (show-all)
  5750. (message "Entire buffer visible, including drawers"))
  5751. ;; Table: enter it or move to the next field.
  5752. ((org-at-table-p 'any)
  5753. (if (org-at-table.el-p)
  5754. (message "Use C-c ' to edit table.el tables")
  5755. (if arg (org-table-edit-field t)
  5756. (org-table-justify-field-maybe)
  5757. (call-interactively 'org-table-next-field))))
  5758. ((run-hook-with-args-until-success
  5759. 'org-tab-after-check-for-table-hook))
  5760. ;; Global cycling: delegate to `org-cycle-internal-global'.
  5761. ((eq arg t) (org-cycle-internal-global))
  5762. ;; Drawers: delegate to `org-flag-drawer'.
  5763. ((and org-drawers org-drawer-regexp
  5764. (save-excursion
  5765. (beginning-of-line 1)
  5766. (looking-at org-drawer-regexp)))
  5767. (org-flag-drawer ; toggle block visibility
  5768. (not (get-char-property (match-end 0) 'invisible))))
  5769. ;; Show-subtree, ARG levels up from here.
  5770. ((integerp arg)
  5771. (save-excursion
  5772. (org-back-to-heading)
  5773. (outline-up-heading (if (< arg 0) (- arg)
  5774. (- (funcall outline-level) arg)))
  5775. (org-show-subtree)))
  5776. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  5777. ((and (featurep 'org-inlinetask)
  5778. (org-inlinetask-at-task-p)
  5779. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5780. (org-inlinetask-toggle-visibility))
  5781. ((org-try-cdlatex-tab))
  5782. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  5783. ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
  5784. (save-excursion (beginning-of-line 1)
  5785. (looking-at org-outline-regexp)))
  5786. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5787. (org-cycle-internal-local))
  5788. ;; From there: TAB emulation and template completion.
  5789. (buffer-read-only (org-back-to-heading))
  5790. ((run-hook-with-args-until-success
  5791. 'org-tab-after-check-for-cycling-hook))
  5792. ((org-try-structure-completion))
  5793. ((run-hook-with-args-until-success
  5794. 'org-tab-before-tab-emulation-hook))
  5795. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5796. (or (not (bolp))
  5797. (not (looking-at org-outline-regexp))))
  5798. (call-interactively (global-key-binding "\t")))
  5799. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5800. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5801. (or (and (eq org-cycle-emulate-tab 'white)
  5802. (= (match-end 0) (point-at-eol)))
  5803. (and (eq org-cycle-emulate-tab 'whitestart)
  5804. (>= (match-end 0) pos))))
  5805. t
  5806. (eq org-cycle-emulate-tab t))
  5807. (call-interactively (global-key-binding "\t")))
  5808. (t (save-excursion
  5809. (org-back-to-heading)
  5810. (org-cycle)))))))
  5811. (defun org-cycle-internal-global ()
  5812. "Do the global cycling action."
  5813. ;; Hack to avoid display of messages for .org attachments in Gnus
  5814. (let (message-log-max ; Don't populate the *Messages* buffer
  5815. (ga (string-match "\\*fontification" (buffer-name))))
  5816. (cond
  5817. ((and (eq last-command this-command)
  5818. (eq org-cycle-global-status 'overview))
  5819. ;; We just created the overview - now do table of contents
  5820. ;; This can be slow in very large buffers, so indicate action
  5821. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  5822. (unless ga (message "CONTENTS..."))
  5823. (org-content)
  5824. (unless ga (message "CONTENTS...done"))
  5825. (setq org-cycle-global-status 'contents)
  5826. (run-hook-with-args 'org-cycle-hook 'contents))
  5827. ((and (eq last-command this-command)
  5828. (eq org-cycle-global-status 'contents))
  5829. ;; We just showed the table of contents - now show everything
  5830. (run-hook-with-args 'org-pre-cycle-hook 'all)
  5831. (show-all)
  5832. (unless ga (message "SHOW ALL"))
  5833. (setq org-cycle-global-status 'all)
  5834. (run-hook-with-args 'org-cycle-hook 'all))
  5835. (t
  5836. ;; Default action: go to overview
  5837. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  5838. (org-overview)
  5839. (unless ga (message "OVERVIEW"))
  5840. (setq org-cycle-global-status 'overview)
  5841. (run-hook-with-args 'org-cycle-hook 'overview)))))
  5842. (defun org-cycle-internal-local ()
  5843. "Do the local cycling action."
  5844. (let (message-log-max ; Don't populate the *Messages* buffer
  5845. (goal-column 0) eoh eol eos has-children children-skipped struct)
  5846. ;; First, determine end of headline (EOH), end of subtree or item
  5847. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  5848. (save-excursion
  5849. (if (org-at-item-p)
  5850. (progn
  5851. (beginning-of-line)
  5852. (setq struct (org-list-struct))
  5853. (setq eoh (point-at-eol))
  5854. (setq eos (org-list-get-item-end-before-blank (point) struct))
  5855. (setq has-children (org-list-has-child-p (point) struct)))
  5856. (org-back-to-heading)
  5857. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  5858. (setq eos (save-excursion (1- (org-end-of-subtree t t))))
  5859. (setq has-children
  5860. (or (save-excursion
  5861. (let ((level (funcall outline-level)))
  5862. (outline-next-heading)
  5863. (and (org-at-heading-p t)
  5864. (> (funcall outline-level) level))))
  5865. (save-excursion
  5866. (org-list-search-forward (org-item-beginning-re) eos t)))))
  5867. ;; Determine end invisible part of buffer (EOL)
  5868. (beginning-of-line 2)
  5869. ;; XEmacs doesn't have `next-single-char-property-change'
  5870. (if (featurep 'xemacs)
  5871. (while (and (not (eobp)) ;; this is like `next-line'
  5872. (get-char-property (1- (point)) 'invisible))
  5873. (beginning-of-line 2))
  5874. (while (and (not (eobp)) ;; this is like `next-line'
  5875. (get-char-property (1- (point)) 'invisible))
  5876. (goto-char (next-single-char-property-change (point) 'invisible))
  5877. (and (eolp) (beginning-of-line 2))))
  5878. (setq eol (point)))
  5879. ;; Find out what to do next and set `this-command'
  5880. (cond
  5881. ((= eos eoh)
  5882. ;; Nothing is hidden behind this heading
  5883. (unless (org-before-first-heading-p)
  5884. (run-hook-with-args 'org-pre-cycle-hook 'empty))
  5885. (message "EMPTY ENTRY")
  5886. (setq org-cycle-subtree-status nil)
  5887. (save-excursion
  5888. (goto-char eos)
  5889. (outline-next-heading)
  5890. (if (outline-invisible-p) (org-flag-heading nil))))
  5891. ((and (or (>= eol eos)
  5892. (not (string-match "\\S-" (buffer-substring eol eos))))
  5893. (or has-children
  5894. (not (setq children-skipped
  5895. org-cycle-skip-children-state-if-no-children))))
  5896. ;; Entire subtree is hidden in one line: children view
  5897. (unless (org-before-first-heading-p)
  5898. (run-hook-with-args 'org-pre-cycle-hook 'children))
  5899. (if (org-at-item-p)
  5900. (org-list-set-item-visibility (point-at-bol) struct 'children)
  5901. (org-show-entry)
  5902. (org-with-limited-levels (show-children))
  5903. ;; FIXME: This slows down the func way too much.
  5904. ;; How keep drawers hidden in subtree anyway?
  5905. ;; (when (memq 'org-cycle-hide-drawers org-cycle-hook)
  5906. ;; (org-cycle-hide-drawers 'subtree))
  5907. ;; Fold every list in subtree to top-level items.
  5908. (when (eq org-cycle-include-plain-lists 'integrate)
  5909. (save-excursion
  5910. (org-back-to-heading)
  5911. (while (org-list-search-forward (org-item-beginning-re) eos t)
  5912. (beginning-of-line 1)
  5913. (let* ((struct (org-list-struct))
  5914. (prevs (org-list-prevs-alist struct))
  5915. (end (org-list-get-bottom-point struct)))
  5916. (mapc (lambda (e) (org-list-set-item-visibility e struct 'folded))
  5917. (org-list-get-all-items (point) struct prevs))
  5918. (goto-char end))))))
  5919. (message "CHILDREN")
  5920. (save-excursion
  5921. (goto-char eos)
  5922. (outline-next-heading)
  5923. (if (outline-invisible-p) (org-flag-heading nil)))
  5924. (setq org-cycle-subtree-status 'children)
  5925. (unless (org-before-first-heading-p)
  5926. (run-hook-with-args 'org-cycle-hook 'children)))
  5927. ((or children-skipped
  5928. (and (eq last-command this-command)
  5929. (eq org-cycle-subtree-status 'children)))
  5930. ;; We just showed the children, or no children are there,
  5931. ;; now show everything.
  5932. (unless (org-before-first-heading-p)
  5933. (run-hook-with-args 'org-pre-cycle-hook 'subtree))
  5934. (outline-flag-region eoh eos nil)
  5935. (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  5936. (setq org-cycle-subtree-status 'subtree)
  5937. (unless (org-before-first-heading-p)
  5938. (run-hook-with-args 'org-cycle-hook 'subtree)))
  5939. (t
  5940. ;; Default action: hide the subtree.
  5941. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  5942. (outline-flag-region eoh eos t)
  5943. (message "FOLDED")
  5944. (setq org-cycle-subtree-status 'folded)
  5945. (unless (org-before-first-heading-p)
  5946. (run-hook-with-args 'org-cycle-hook 'folded))))))
  5947. ;;;###autoload
  5948. (defun org-global-cycle (&optional arg)
  5949. "Cycle the global visibility. For details see `org-cycle'.
  5950. With \\[universal-argument] prefix arg, switch to startup visibility.
  5951. With a numeric prefix, show all headlines up to that level."
  5952. (interactive "P")
  5953. (let ((org-cycle-include-plain-lists
  5954. (if (derived-mode-p 'org-mode) org-cycle-include-plain-lists nil)))
  5955. (cond
  5956. ((integerp arg)
  5957. (show-all)
  5958. (hide-sublevels arg)
  5959. (setq org-cycle-global-status 'contents))
  5960. ((equal arg '(4))
  5961. (org-set-startup-visibility)
  5962. (message "Startup visibility, plus VISIBILITY properties."))
  5963. (t
  5964. (org-cycle '(4))))))
  5965. (defun org-set-startup-visibility ()
  5966. "Set the visibility required by startup options and properties."
  5967. (cond
  5968. ((eq org-startup-folded t)
  5969. (org-cycle '(4)))
  5970. ((eq org-startup-folded 'content)
  5971. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  5972. (org-cycle '(4)) (org-cycle '(4)))))
  5973. (unless (eq org-startup-folded 'showeverything)
  5974. (if org-hide-block-startup (org-hide-block-all))
  5975. (org-set-visibility-according-to-property 'no-cleanup)
  5976. (org-cycle-hide-archived-subtrees 'all)
  5977. (org-cycle-hide-drawers 'all)
  5978. (org-cycle-show-empty-lines t)))
  5979. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  5980. "Switch subtree visibilities according to :VISIBILITY: property."
  5981. (interactive)
  5982. (let (org-show-entry-below state)
  5983. (save-excursion
  5984. (goto-char (point-min))
  5985. (while (re-search-forward
  5986. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  5987. nil t)
  5988. (setq state (match-string 1))
  5989. (save-excursion
  5990. (org-back-to-heading t)
  5991. (hide-subtree)
  5992. (org-reveal)
  5993. (cond
  5994. ((equal state '("fold" "folded"))
  5995. (hide-subtree))
  5996. ((equal state "children")
  5997. (org-show-hidden-entry)
  5998. (show-children))
  5999. ((equal state "content")
  6000. (save-excursion
  6001. (save-restriction
  6002. (org-narrow-to-subtree)
  6003. (org-content))))
  6004. ((member state '("all" "showall"))
  6005. (show-subtree)))))
  6006. (unless no-cleanup
  6007. (org-cycle-hide-archived-subtrees 'all)
  6008. (org-cycle-hide-drawers 'all)
  6009. (org-cycle-show-empty-lines 'all)))))
  6010. ;; This function uses outline-regexp instead of the more fundamental
  6011. ;; org-outline-regexp so that org-cycle-global works outside of Org
  6012. ;; buffers, where outline-regexp is needed.
  6013. (defun org-overview ()
  6014. "Switch to overview mode, showing only top-level headlines.
  6015. Really, this shows all headlines with level equal or greater than the level
  6016. of the first headline in the buffer. This is important, because if the
  6017. first headline is not level one, then (hide-sublevels 1) gives confusing
  6018. results."
  6019. (interactive)
  6020. (let ((l (org-current-line))
  6021. (level (save-excursion
  6022. (goto-char (point-min))
  6023. (if (re-search-forward (concat "^" outline-regexp) nil t)
  6024. (progn
  6025. (goto-char (match-beginning 0))
  6026. (funcall outline-level))))))
  6027. (and level (hide-sublevels level))
  6028. (recenter '(4))
  6029. (org-goto-line l)))
  6030. (defun org-content (&optional arg)
  6031. "Show all headlines in the buffer, like a table of contents.
  6032. With numerical argument N, show content up to level N."
  6033. (interactive "P")
  6034. (save-excursion
  6035. ;; Visit all headings and show their offspring
  6036. (and (integerp arg) (org-overview))
  6037. (goto-char (point-max))
  6038. (catch 'exit
  6039. (while (and (progn (condition-case nil
  6040. (outline-previous-visible-heading 1)
  6041. (error (goto-char (point-min))))
  6042. t)
  6043. (looking-at org-outline-regexp))
  6044. (if (integerp arg)
  6045. (show-children (1- arg))
  6046. (show-branches))
  6047. (if (bobp) (throw 'exit nil))))))
  6048. (defun org-optimize-window-after-visibility-change (state)
  6049. "Adjust the window after a change in outline visibility.
  6050. This function is the default value of the hook `org-cycle-hook'."
  6051. (when (get-buffer-window (current-buffer))
  6052. (cond
  6053. ((eq state 'content) nil)
  6054. ((eq state 'all) nil)
  6055. ((eq state 'folded) nil)
  6056. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  6057. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  6058. (defun org-remove-empty-overlays-at (pos)
  6059. "Remove outline overlays that do not contain non-white stuff."
  6060. (mapc
  6061. (lambda (o)
  6062. (and (eq 'outline (overlay-get o 'invisible))
  6063. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  6064. (overlay-end o))))
  6065. (delete-overlay o)))
  6066. (overlays-at pos)))
  6067. (defun org-clean-visibility-after-subtree-move ()
  6068. "Fix visibility issues after moving a subtree."
  6069. ;; First, find a reasonable region to look at:
  6070. ;; Start two siblings above, end three below
  6071. (let* ((beg (save-excursion
  6072. (and (org-get-last-sibling)
  6073. (org-get-last-sibling))
  6074. (point)))
  6075. (end (save-excursion
  6076. (and (org-get-next-sibling)
  6077. (org-get-next-sibling)
  6078. (org-get-next-sibling))
  6079. (if (org-at-heading-p)
  6080. (point-at-eol)
  6081. (point))))
  6082. (level (looking-at "\\*+"))
  6083. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  6084. (save-excursion
  6085. (save-restriction
  6086. (narrow-to-region beg end)
  6087. (when re
  6088. ;; Properly fold already folded siblings
  6089. (goto-char (point-min))
  6090. (while (re-search-forward re nil t)
  6091. (if (and (not (outline-invisible-p))
  6092. (save-excursion
  6093. (goto-char (point-at-eol)) (outline-invisible-p)))
  6094. (hide-entry))))
  6095. (org-cycle-show-empty-lines 'overview)
  6096. (org-cycle-hide-drawers 'overview)))))
  6097. (defun org-cycle-show-empty-lines (state)
  6098. "Show empty lines above all visible headlines.
  6099. The region to be covered depends on STATE when called through
  6100. `org-cycle-hook'. Lisp program can use t for STATE to get the
  6101. entire buffer covered. Note that an empty line is only shown if there
  6102. are at least `org-cycle-separator-lines' empty lines before the headline."
  6103. (when (not (= org-cycle-separator-lines 0))
  6104. (save-excursion
  6105. (let* ((n (abs org-cycle-separator-lines))
  6106. (re (cond
  6107. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  6108. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  6109. (t (let ((ns (number-to-string (- n 2))))
  6110. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  6111. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  6112. beg end b e)
  6113. (cond
  6114. ((memq state '(overview contents t))
  6115. (setq beg (point-min) end (point-max)))
  6116. ((memq state '(children folded))
  6117. (setq beg (point) end (progn (org-end-of-subtree t t)
  6118. (beginning-of-line 2)
  6119. (point)))))
  6120. (when beg
  6121. (goto-char beg)
  6122. (while (re-search-forward re end t)
  6123. (unless (get-char-property (match-end 1) 'invisible)
  6124. (setq e (match-end 1))
  6125. (if (< org-cycle-separator-lines 0)
  6126. (setq b (save-excursion
  6127. (goto-char (match-beginning 0))
  6128. (org-back-over-empty-lines)
  6129. (if (save-excursion
  6130. (goto-char (max (point-min) (1- (point))))
  6131. (org-at-heading-p))
  6132. (1- (point))
  6133. (point))))
  6134. (setq b (match-beginning 1)))
  6135. (outline-flag-region b e nil)))))))
  6136. ;; Never hide empty lines at the end of the file.
  6137. (save-excursion
  6138. (goto-char (point-max))
  6139. (outline-previous-heading)
  6140. (outline-end-of-heading)
  6141. (if (and (looking-at "[ \t\n]+")
  6142. (= (match-end 0) (point-max)))
  6143. (outline-flag-region (point) (match-end 0) nil))))
  6144. (defun org-show-empty-lines-in-parent ()
  6145. "Move to the parent and re-show empty lines before visible headlines."
  6146. (save-excursion
  6147. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  6148. (org-cycle-show-empty-lines context))))
  6149. (defun org-files-list ()
  6150. "Return `org-agenda-files' list, plus all open org-mode files.
  6151. This is useful for operations that need to scan all of a user's
  6152. open and agenda-wise Org files."
  6153. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  6154. (dolist (buf (buffer-list))
  6155. (with-current-buffer buf
  6156. (if (and (derived-mode-p 'org-mode) (buffer-file-name))
  6157. (let ((file (expand-file-name (buffer-file-name))))
  6158. (unless (member file files)
  6159. (push file files))))))
  6160. files))
  6161. (defsubst org-entry-beginning-position ()
  6162. "Return the beginning position of the current entry."
  6163. (save-excursion (outline-back-to-heading t) (point)))
  6164. (defsubst org-entry-end-position ()
  6165. "Return the end position of the current entry."
  6166. (save-excursion (outline-next-heading) (point)))
  6167. (defun org-cycle-hide-drawers (state)
  6168. "Re-hide all drawers after a visibility state change."
  6169. (when (and (derived-mode-p 'org-mode)
  6170. (not (memq state '(overview folded contents))))
  6171. (save-excursion
  6172. (let* ((globalp (memq state '(contents all)))
  6173. (beg (if globalp (point-min) (point)))
  6174. (end (if globalp (point-max)
  6175. (if (eq state 'children)
  6176. (save-excursion (outline-next-heading) (point))
  6177. (org-end-of-subtree t)))))
  6178. (goto-char beg)
  6179. (while (re-search-forward org-drawer-regexp end t)
  6180. (org-flag-drawer t))))))
  6181. (defun org-cycle-hide-inline-tasks (state)
  6182. "Re-hide inline task when switching to 'contents visibility state."
  6183. (when (and (eq state 'contents)
  6184. (boundp 'org-inlinetask-min-level)
  6185. org-inlinetask-min-level)
  6186. (hide-sublevels (1- org-inlinetask-min-level))))
  6187. (defun org-flag-drawer (flag)
  6188. "When FLAG is non-nil, hide the drawer we are within.
  6189. Otherwise make it visible."
  6190. (save-excursion
  6191. (beginning-of-line 1)
  6192. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  6193. (let ((b (match-end 0)))
  6194. (if (re-search-forward
  6195. "^[ \t]*:END:"
  6196. (save-excursion (outline-next-heading) (point)) t)
  6197. (outline-flag-region b (point-at-eol) flag)
  6198. (error ":END: line missing at position %s" b))))))
  6199. (defun org-subtree-end-visible-p ()
  6200. "Is the end of the current subtree visible?"
  6201. (pos-visible-in-window-p
  6202. (save-excursion (org-end-of-subtree t) (point))))
  6203. (defun org-first-headline-recenter (&optional N)
  6204. "Move cursor to the first headline and recenter the headline.
  6205. Optional argument N means put the headline into the Nth line of the window."
  6206. (goto-char (point-min))
  6207. (when (re-search-forward (concat "^\\(" org-outline-regexp "\\)") nil t)
  6208. (beginning-of-line)
  6209. (recenter (prefix-numeric-value N))))
  6210. ;;; Saving and restoring visibility
  6211. (defun org-outline-overlay-data (&optional use-markers)
  6212. "Return a list of the locations of all outline overlays.
  6213. These are overlays with the `invisible' property value `outline'.
  6214. The return value is a list of cons cells, with start and stop
  6215. positions for each overlay.
  6216. If USE-MARKERS is set, return the positions as markers."
  6217. (let (beg end)
  6218. (save-excursion
  6219. (save-restriction
  6220. (widen)
  6221. (delq nil
  6222. (mapcar (lambda (o)
  6223. (when (eq (overlay-get o 'invisible) 'outline)
  6224. (setq beg (overlay-start o)
  6225. end (overlay-end o))
  6226. (and beg end (> end beg)
  6227. (if use-markers
  6228. (cons (move-marker (make-marker) beg)
  6229. (move-marker (make-marker) end))
  6230. (cons beg end)))))
  6231. (overlays-in (point-min) (point-max))))))))
  6232. (defun org-set-outline-overlay-data (data)
  6233. "Create visibility overlays for all positions in DATA.
  6234. DATA should have been made by `org-outline-overlay-data'."
  6235. (let (o)
  6236. (save-excursion
  6237. (save-restriction
  6238. (widen)
  6239. (show-all)
  6240. (mapc (lambda (c)
  6241. (outline-flag-region (car c) (cdr c) t))
  6242. data)))))
  6243. ;;; Folding of blocks
  6244. (defvar org-hide-block-overlays nil
  6245. "Overlays hiding blocks.")
  6246. (make-variable-buffer-local 'org-hide-block-overlays)
  6247. (defun org-block-map (function &optional start end)
  6248. "Call FUNCTION at the head of all source blocks in the current buffer.
  6249. Optional arguments START and END can be used to limit the range."
  6250. (let ((start (or start (point-min)))
  6251. (end (or end (point-max))))
  6252. (save-excursion
  6253. (goto-char start)
  6254. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  6255. (save-excursion
  6256. (save-match-data
  6257. (goto-char (match-beginning 0))
  6258. (funcall function)))))))
  6259. (defun org-hide-block-toggle-all ()
  6260. "Toggle the visibility of all blocks in the current buffer."
  6261. (org-block-map #'org-hide-block-toggle))
  6262. (defun org-hide-block-all ()
  6263. "Fold all blocks in the current buffer."
  6264. (interactive)
  6265. (org-show-block-all)
  6266. (org-block-map #'org-hide-block-toggle-maybe))
  6267. (defun org-show-block-all ()
  6268. "Unfold all blocks in the current buffer."
  6269. (interactive)
  6270. (mapc 'delete-overlay org-hide-block-overlays)
  6271. (setq org-hide-block-overlays nil))
  6272. (defun org-hide-block-toggle-maybe ()
  6273. "Toggle visibility of block at point."
  6274. (interactive)
  6275. (let ((case-fold-search t))
  6276. (if (save-excursion
  6277. (beginning-of-line 1)
  6278. (looking-at org-block-regexp))
  6279. (progn (org-hide-block-toggle)
  6280. t) ;; to signal that we took action
  6281. nil))) ;; to signal that we did not
  6282. (defun org-hide-block-toggle (&optional force)
  6283. "Toggle the visibility of the current block."
  6284. (interactive)
  6285. (save-excursion
  6286. (beginning-of-line)
  6287. (if (re-search-forward org-block-regexp nil t)
  6288. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  6289. (end (match-end 0)) ;; end of entire body
  6290. ov)
  6291. (if (memq t (mapcar (lambda (overlay)
  6292. (eq (overlay-get overlay 'invisible)
  6293. 'org-hide-block))
  6294. (overlays-at start)))
  6295. (if (or (not force) (eq force 'off))
  6296. (mapc (lambda (ov)
  6297. (when (member ov org-hide-block-overlays)
  6298. (setq org-hide-block-overlays
  6299. (delq ov org-hide-block-overlays)))
  6300. (when (eq (overlay-get ov 'invisible)
  6301. 'org-hide-block)
  6302. (delete-overlay ov)))
  6303. (overlays-at start)))
  6304. (setq ov (make-overlay start end))
  6305. (overlay-put ov 'invisible 'org-hide-block)
  6306. ;; make the block accessible to isearch
  6307. (overlay-put
  6308. ov 'isearch-open-invisible
  6309. (lambda (ov)
  6310. (when (member ov org-hide-block-overlays)
  6311. (setq org-hide-block-overlays
  6312. (delq ov org-hide-block-overlays)))
  6313. (when (eq (overlay-get ov 'invisible)
  6314. 'org-hide-block)
  6315. (delete-overlay ov))))
  6316. (push ov org-hide-block-overlays)))
  6317. (error "Not looking at a source block"))))
  6318. ;; org-tab-after-check-for-cycling-hook
  6319. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  6320. ;; Remove overlays when changing major mode
  6321. (add-hook 'org-mode-hook
  6322. (lambda () (org-add-hook 'change-major-mode-hook
  6323. 'org-show-block-all 'append 'local)))
  6324. ;;; Org-goto
  6325. (defvar org-goto-window-configuration nil)
  6326. (defvar org-goto-marker nil)
  6327. (defvar org-goto-map)
  6328. (defun org-goto-map ()
  6329. "Set the keymap `org-goto'."
  6330. (setq org-goto-map
  6331. (let ((map (make-sparse-keymap)))
  6332. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command
  6333. mouse-drag-region universal-argument org-occur))
  6334. cmd)
  6335. (while (setq cmd (pop cmds))
  6336. (substitute-key-definition cmd cmd map global-map)))
  6337. (suppress-keymap map)
  6338. (org-defkey map "\C-m" 'org-goto-ret)
  6339. (org-defkey map [(return)] 'org-goto-ret)
  6340. (org-defkey map [(left)] 'org-goto-left)
  6341. (org-defkey map [(right)] 'org-goto-right)
  6342. (org-defkey map [(control ?g)] 'org-goto-quit)
  6343. (org-defkey map "\C-i" 'org-cycle)
  6344. (org-defkey map [(tab)] 'org-cycle)
  6345. (org-defkey map [(down)] 'outline-next-visible-heading)
  6346. (org-defkey map [(up)] 'outline-previous-visible-heading)
  6347. (if org-goto-auto-isearch
  6348. (if (fboundp 'define-key-after)
  6349. (define-key-after map [t] 'org-goto-local-auto-isearch)
  6350. nil)
  6351. (org-defkey map "q" 'org-goto-quit)
  6352. (org-defkey map "n" 'outline-next-visible-heading)
  6353. (org-defkey map "p" 'outline-previous-visible-heading)
  6354. (org-defkey map "f" 'outline-forward-same-level)
  6355. (org-defkey map "b" 'outline-backward-same-level)
  6356. (org-defkey map "u" 'outline-up-heading))
  6357. (org-defkey map "/" 'org-occur)
  6358. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  6359. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  6360. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  6361. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  6362. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  6363. map)))
  6364. (defconst org-goto-help
  6365. "Browse buffer copy, to find location or copy text.%s
  6366. RET=jump to location C-g=quit and return to previous location
  6367. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  6368. (defvar org-goto-start-pos) ; dynamically scoped parameter
  6369. ;; FIXME: Docstring does not mention both interfaces
  6370. (defun org-goto (&optional alternative-interface)
  6371. "Look up a different location in the current file, keeping current visibility.
  6372. When you want look-up or go to a different location in a
  6373. document, the fastest way is often to fold the entire buffer and
  6374. then dive into the tree. This method has the disadvantage, that
  6375. the previous location will be folded, which may not be what you
  6376. want.
  6377. This command works around this by showing a copy of the current
  6378. buffer in an indirect buffer, in overview mode. You can dive
  6379. into the tree in that copy, use org-occur and incremental search
  6380. to find a location. When pressing RET or `Q', the command
  6381. returns to the original buffer in which the visibility is still
  6382. unchanged. After RET it will also jump to the location selected
  6383. in the indirect buffer and expose the headline hierarchy above.
  6384. With a prefix argument, use the alternative interface: e.g. if
  6385. `org-goto-interface' is 'outline use 'outline-path-completion."
  6386. (interactive "P")
  6387. (org-goto-map)
  6388. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  6389. (org-refile-use-outline-path t)
  6390. (org-refile-target-verify-function nil)
  6391. (interface
  6392. (if (not alternative-interface)
  6393. org-goto-interface
  6394. (if (eq org-goto-interface 'outline)
  6395. 'outline-path-completion
  6396. 'outline)))
  6397. (org-goto-start-pos (point))
  6398. (selected-point
  6399. (if (eq interface 'outline)
  6400. (car (org-get-location (current-buffer) org-goto-help))
  6401. (let ((pa (org-refile-get-location "Goto" nil nil t)))
  6402. (org-refile-check-position pa)
  6403. (nth 3 pa)))))
  6404. (if selected-point
  6405. (progn
  6406. (org-mark-ring-push org-goto-start-pos)
  6407. (goto-char selected-point)
  6408. (if (or (outline-invisible-p) (org-invisible-p2))
  6409. (org-show-context 'org-goto)))
  6410. (message "Quit"))))
  6411. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  6412. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  6413. (defvar org-goto-local-auto-isearch-map) ; defined below
  6414. (defun org-get-location (buf help)
  6415. "Let the user select a location in the Org-mode buffer BUF.
  6416. This function uses a recursive edit. It returns the selected position
  6417. or nil."
  6418. (org-no-popups
  6419. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  6420. (isearch-hide-immediately nil)
  6421. (isearch-search-fun-function
  6422. (lambda () 'org-goto-local-search-headings))
  6423. (org-goto-selected-point org-goto-exit-command))
  6424. (save-excursion
  6425. (save-window-excursion
  6426. (delete-other-windows)
  6427. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  6428. (org-pop-to-buffer-same-window
  6429. (condition-case nil
  6430. (make-indirect-buffer (current-buffer) "*org-goto*")
  6431. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  6432. (with-output-to-temp-buffer "*Org Help*"
  6433. (princ (format help (if org-goto-auto-isearch
  6434. " Just type for auto-isearch."
  6435. " n/p/f/b/u to navigate, q to quit."))))
  6436. (org-fit-window-to-buffer (get-buffer-window "*Org Help*"))
  6437. (setq buffer-read-only nil)
  6438. (let ((org-startup-truncated t)
  6439. (org-startup-folded nil)
  6440. (org-startup-align-all-tables nil))
  6441. (org-mode)
  6442. (org-overview))
  6443. (setq buffer-read-only t)
  6444. (if (and (boundp 'org-goto-start-pos)
  6445. (integer-or-marker-p org-goto-start-pos))
  6446. (let ((org-show-hierarchy-above t)
  6447. (org-show-siblings t)
  6448. (org-show-following-heading t))
  6449. (goto-char org-goto-start-pos)
  6450. (and (outline-invisible-p) (org-show-context)))
  6451. (goto-char (point-min)))
  6452. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  6453. (message "Select location and press RET")
  6454. (use-local-map org-goto-map)
  6455. (recursive-edit)))
  6456. (kill-buffer "*org-goto*")
  6457. (cons org-goto-selected-point org-goto-exit-command))))
  6458. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  6459. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  6460. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  6461. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  6462. (defun org-goto-local-search-headings (string bound noerror)
  6463. "Search and make sure that any matches are in headlines."
  6464. (catch 'return
  6465. (while (if isearch-forward
  6466. (search-forward string bound noerror)
  6467. (search-backward string bound noerror))
  6468. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  6469. (and (member :headline context)
  6470. (not (member :tags context))))
  6471. (throw 'return (point))))))
  6472. (defun org-goto-local-auto-isearch ()
  6473. "Start isearch."
  6474. (interactive)
  6475. (goto-char (point-min))
  6476. (let ((keys (this-command-keys)))
  6477. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  6478. (isearch-mode t)
  6479. (isearch-process-search-char (string-to-char keys)))))
  6480. (defun org-goto-ret (&optional arg)
  6481. "Finish `org-goto' by going to the new location."
  6482. (interactive "P")
  6483. (setq org-goto-selected-point (point)
  6484. org-goto-exit-command 'return)
  6485. (throw 'exit nil))
  6486. (defun org-goto-left ()
  6487. "Finish `org-goto' by going to the new location."
  6488. (interactive)
  6489. (if (org-at-heading-p)
  6490. (progn
  6491. (beginning-of-line 1)
  6492. (setq org-goto-selected-point (point)
  6493. org-goto-exit-command 'left)
  6494. (throw 'exit nil))
  6495. (error "Not on a heading")))
  6496. (defun org-goto-right ()
  6497. "Finish `org-goto' by going to the new location."
  6498. (interactive)
  6499. (if (org-at-heading-p)
  6500. (progn
  6501. (setq org-goto-selected-point (point)
  6502. org-goto-exit-command 'right)
  6503. (throw 'exit nil))
  6504. (error "Not on a heading")))
  6505. (defun org-goto-quit ()
  6506. "Finish `org-goto' without cursor motion."
  6507. (interactive)
  6508. (setq org-goto-selected-point nil)
  6509. (setq org-goto-exit-command 'quit)
  6510. (throw 'exit nil))
  6511. ;;; Indirect buffer display of subtrees
  6512. (defvar org-indirect-dedicated-frame nil
  6513. "This is the frame being used for indirect tree display.")
  6514. (defvar org-last-indirect-buffer nil)
  6515. (defun org-tree-to-indirect-buffer (&optional arg)
  6516. "Create indirect buffer and narrow it to current subtree.
  6517. With a numerical prefix ARG, go up to this level and then take that tree.
  6518. If ARG is negative, go up that many levels.
  6519. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  6520. indirect buffer previously made with this command, to avoid proliferation of
  6521. indirect buffers. However, when you call the command with a \
  6522. \\[universal-argument] prefix, or
  6523. when `org-indirect-buffer-display' is `new-frame', the last buffer
  6524. is kept so that you can work with several indirect buffers at the same time.
  6525. If `org-indirect-buffer-display' is `dedicated-frame', the \
  6526. \\[universal-argument] prefix also
  6527. requests that a new frame be made for the new buffer, so that the dedicated
  6528. frame is not changed."
  6529. (interactive "P")
  6530. (let ((cbuf (current-buffer))
  6531. (cwin (selected-window))
  6532. (pos (point))
  6533. beg end level heading ibuf)
  6534. (save-excursion
  6535. (org-back-to-heading t)
  6536. (when (numberp arg)
  6537. (setq level (org-outline-level))
  6538. (if (< arg 0) (setq arg (+ level arg)))
  6539. (while (> (setq level (org-outline-level)) arg)
  6540. (org-up-heading-safe)))
  6541. (setq beg (point)
  6542. heading (org-get-heading))
  6543. (org-end-of-subtree t t)
  6544. (if (org-at-heading-p) (backward-char 1))
  6545. (setq end (point)))
  6546. (if (and (buffer-live-p org-last-indirect-buffer)
  6547. (not (eq org-indirect-buffer-display 'new-frame))
  6548. (not arg))
  6549. (kill-buffer org-last-indirect-buffer))
  6550. (setq ibuf (org-get-indirect-buffer cbuf)
  6551. org-last-indirect-buffer ibuf)
  6552. (cond
  6553. ((or (eq org-indirect-buffer-display 'new-frame)
  6554. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  6555. (select-frame (make-frame))
  6556. (delete-other-windows)
  6557. (org-pop-to-buffer-same-window ibuf)
  6558. (org-set-frame-title heading))
  6559. ((eq org-indirect-buffer-display 'dedicated-frame)
  6560. (raise-frame
  6561. (select-frame (or (and org-indirect-dedicated-frame
  6562. (frame-live-p org-indirect-dedicated-frame)
  6563. org-indirect-dedicated-frame)
  6564. (setq org-indirect-dedicated-frame (make-frame)))))
  6565. (delete-other-windows)
  6566. (org-pop-to-buffer-same-window ibuf)
  6567. (org-set-frame-title (concat "Indirect: " heading)))
  6568. ((eq org-indirect-buffer-display 'current-window)
  6569. (org-pop-to-buffer-same-window ibuf))
  6570. ((eq org-indirect-buffer-display 'other-window)
  6571. (pop-to-buffer ibuf))
  6572. (t (error "Invalid value")))
  6573. (if (featurep 'xemacs)
  6574. (save-excursion (org-mode) (turn-on-font-lock)))
  6575. (narrow-to-region beg end)
  6576. (show-all)
  6577. (goto-char pos)
  6578. (run-hook-with-args 'org-cycle-hook 'all)
  6579. (and (window-live-p cwin) (select-window cwin))))
  6580. (defun org-get-indirect-buffer (&optional buffer)
  6581. (setq buffer (or buffer (current-buffer)))
  6582. (let ((n 1) (base (buffer-name buffer)) bname)
  6583. (while (buffer-live-p
  6584. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  6585. (setq n (1+ n)))
  6586. (condition-case nil
  6587. (make-indirect-buffer buffer bname 'clone)
  6588. (error (make-indirect-buffer buffer bname)))))
  6589. (defun org-set-frame-title (title)
  6590. "Set the title of the current frame to the string TITLE."
  6591. ;; FIXME: how to name a single frame in XEmacs???
  6592. (unless (featurep 'xemacs)
  6593. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  6594. ;;;; Structure editing
  6595. ;;; Inserting headlines
  6596. (defun org-previous-line-empty-p (&optional next)
  6597. "Is the previous line a blank line?
  6598. When NEXT is non-nil, check the next line instead."
  6599. (save-excursion
  6600. (and (not (bobp))
  6601. (or (beginning-of-line (if next 2 0)) t)
  6602. (save-match-data
  6603. (looking-at "[ \t]*$")))))
  6604. (defun org-insert-heading (&optional arg invisible-ok)
  6605. "Insert a new heading or item with same depth at point.
  6606. If point is in a plain list and ARG is nil, create a new list item.
  6607. With one universal prefix argument, insert a heading even in lists.
  6608. With two universal prefix arguments, insert the heading at the end
  6609. of the parent subtree.
  6610. If point is at the beginning of a headline, insert a sibling before
  6611. the current headline. If point is not at the beginning, split the line
  6612. and create a new headline with the text in the current line after point
  6613. \(see `org-M-RET-may-split-line' on how to modify this behavior).
  6614. When INVISIBLE-OK is set, stop at invisible headlines when going back.
  6615. This is important for non-interactive uses of the command."
  6616. (interactive "P")
  6617. (cond
  6618. ((or (= (buffer-size) 0)
  6619. (and (not (save-excursion
  6620. (and (ignore-errors (org-back-to-heading invisible-ok))
  6621. (org-at-heading-p))))
  6622. (or arg (not (org-in-item-p)))))
  6623. (insert
  6624. (if (org-previous-line-empty-p) "" "\n")
  6625. (if (org-in-src-block-p) ",* " "* "))
  6626. (run-hooks 'org-insert-heading-hook))
  6627. ((or arg (not (org-insert-item
  6628. (save-excursion
  6629. (beginning-of-line)
  6630. (looking-at org-list-full-item-re)
  6631. (match-string 3)))))
  6632. (let (begn endn)
  6633. (when (org-buffer-narrowed-p)
  6634. (setq begn (point-min) endn (point-max))
  6635. (widen))
  6636. (let* ((empty-line-p nil)
  6637. (eops (equal arg '(16))) ; insert at end of parent subtree
  6638. (org-insert-heading-respect-content
  6639. (or (not (null arg)) org-insert-heading-respect-content))
  6640. (level nil)
  6641. (on-heading (org-at-heading-p))
  6642. (head (save-excursion
  6643. (condition-case nil
  6644. (progn
  6645. (org-back-to-heading invisible-ok)
  6646. (when (and (not on-heading)
  6647. (featurep 'org-inlinetask)
  6648. (integerp org-inlinetask-min-level)
  6649. (>= (length (match-string 0))
  6650. org-inlinetask-min-level))
  6651. ;; Find a heading level before the inline task
  6652. (while (and (setq level (org-up-heading-safe))
  6653. (>= level org-inlinetask-min-level)))
  6654. (if (org-at-heading-p)
  6655. (org-back-to-heading invisible-ok)
  6656. (error "This should not happen")))
  6657. (unless (and (save-excursion
  6658. (save-match-data
  6659. (org-backward-heading-same-level 1 invisible-ok))
  6660. (= (point) (match-beginning 0)))
  6661. (not (org-previous-line-empty-p t)))
  6662. (setq empty-line-p (org-previous-line-empty-p)))
  6663. (match-string 0))
  6664. (error "* "))))
  6665. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6666. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  6667. pos hide-previous previous-pos)
  6668. (if ;; At the beginning of a heading, open a new line for insertiong
  6669. (and (bolp) (org-at-heading-p)
  6670. (not eops)
  6671. (or (bobp)
  6672. (save-excursion (backward-char 1) (not (outline-invisible-p)))))
  6673. (open-line (if blank 2 1))
  6674. (save-excursion
  6675. (setq previous-pos (point-at-bol))
  6676. (end-of-line)
  6677. (setq hide-previous (outline-invisible-p)))
  6678. (and org-insert-heading-respect-content
  6679. (save-excursion
  6680. (while (outline-invisible-p)
  6681. (org-show-subtree)
  6682. (org-up-heading-safe))))
  6683. (let ((split
  6684. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  6685. (save-excursion
  6686. (let ((p (point)))
  6687. (goto-char (point-at-bol))
  6688. (and (looking-at org-complex-heading-regexp)
  6689. (match-beginning 4)
  6690. (> p (match-beginning 4)))))))
  6691. tags pos)
  6692. (cond
  6693. ;; Insert a new line, possibly at end of parent subtree
  6694. (org-insert-heading-respect-content
  6695. (if (not eops)
  6696. (progn
  6697. (org-end-of-subtree nil t)
  6698. (and (looking-at "^\\*") (backward-char 1))
  6699. (while (and (not (bobp))
  6700. ;; Don't delete spaces in empty headlines
  6701. (not (looking-back org-outline-regexp))
  6702. (member (char-before) '(?\ ?\t ?\n)))
  6703. (backward-delete-char 1)))
  6704. (let ((p (point)))
  6705. (org-up-heading-safe)
  6706. (if (= p (point))
  6707. (goto-char (point-max))
  6708. (org-end-of-subtree nil t))))
  6709. (when (featurep 'org-inlinetask)
  6710. (while (and (not (eobp))
  6711. (looking-at "\\(\\*+\\)[ \t]+")
  6712. (>= (length (match-string 1))
  6713. org-inlinetask-min-level))
  6714. (org-end-of-subtree nil t)))
  6715. (or (bolp) (newline))
  6716. (or (org-previous-line-empty-p)
  6717. (and blank (newline)))
  6718. (if (or empty-line-p eops) (open-line 1)))
  6719. ;; Insert a headling containing text after point
  6720. ((org-at-heading-p)
  6721. (when hide-previous
  6722. (show-children)
  6723. (org-show-entry))
  6724. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?[ \t]*$")
  6725. (setq tags (and (match-end 2) (match-string 2)))
  6726. (and (match-end 1)
  6727. (delete-region (match-beginning 1) (match-end 1)))
  6728. (setq pos (point-at-bol))
  6729. (or split (end-of-line 1))
  6730. (delete-horizontal-space)
  6731. (if (string-match "\\`\\*+\\'"
  6732. (buffer-substring (point-at-bol) (point)))
  6733. (insert " "))
  6734. (newline (if blank 2 1))
  6735. (when tags
  6736. (save-excursion
  6737. (goto-char pos)
  6738. (end-of-line 1)
  6739. (insert " " tags)
  6740. (org-set-tags nil 'align))))
  6741. (t
  6742. (or split (end-of-line 1))
  6743. (newline (if blank 2 1))))))
  6744. (insert head) (just-one-space)
  6745. (setq pos (point))
  6746. (end-of-line 1)
  6747. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  6748. (when (and org-insert-heading-respect-content hide-previous)
  6749. (save-excursion
  6750. (goto-char previous-pos)
  6751. (hide-subtree)))
  6752. (when (and begn endn)
  6753. (narrow-to-region (min (point) begn) (max (point) endn)))
  6754. (run-hooks 'org-insert-heading-hook))))))
  6755. (defun org-get-heading (&optional no-tags no-todo)
  6756. "Return the heading of the current entry, without the stars.
  6757. When NO-TAGS is non-nil, don't include tags.
  6758. When NO-TODO is non-nil, don't include TODO keywords."
  6759. (save-excursion
  6760. (org-back-to-heading t)
  6761. (cond
  6762. ((and no-tags no-todo)
  6763. (looking-at org-complex-heading-regexp)
  6764. (match-string 4))
  6765. (no-tags
  6766. (looking-at (concat org-outline-regexp
  6767. "\\(.*?\\)"
  6768. "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
  6769. (match-string 1))
  6770. (no-todo
  6771. (looking-at org-todo-line-regexp)
  6772. (match-string 3))
  6773. (t (looking-at org-heading-regexp)
  6774. (match-string 2)))))
  6775. (defvar orgstruct-mode) ; defined below
  6776. (defun org-heading-components ()
  6777. "Return the components of the current heading.
  6778. This is a list with the following elements:
  6779. - the level as an integer
  6780. - the reduced level, different if `org-odd-levels-only' is set.
  6781. - the TODO keyword, or nil
  6782. - the priority character, like ?A, or nil if no priority is given
  6783. - the headline text itself, or the tags string if no headline text
  6784. - the tags string, or nil."
  6785. (save-excursion
  6786. (org-back-to-heading t)
  6787. (if (let (case-fold-search)
  6788. (looking-at
  6789. (if orgstruct-mode
  6790. org-heading-regexp
  6791. org-complex-heading-regexp)))
  6792. (if orgstruct-mode
  6793. (list (length (match-string 1))
  6794. (org-reduced-level (length (match-string 1)))
  6795. nil
  6796. nil
  6797. (match-string 2)
  6798. nil)
  6799. (list (length (match-string 1))
  6800. (org-reduced-level (length (match-string 1)))
  6801. (org-match-string-no-properties 2)
  6802. (and (match-end 3) (aref (match-string 3) 2))
  6803. (org-match-string-no-properties 4)
  6804. (org-match-string-no-properties 5))))))
  6805. (defun org-get-entry ()
  6806. "Get the entry text, after heading, entire subtree."
  6807. (save-excursion
  6808. (org-back-to-heading t)
  6809. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  6810. (defun org-insert-heading-after-current ()
  6811. "Insert a new heading with same level as current, after current subtree."
  6812. (interactive)
  6813. (org-back-to-heading)
  6814. (org-insert-heading)
  6815. (org-move-subtree-down)
  6816. (end-of-line 1))
  6817. (defun org-insert-heading-respect-content (&optional arg invisible-ok)
  6818. "Insert heading with `org-insert-heading-respect-content' set to t."
  6819. (interactive "P")
  6820. (let ((org-insert-heading-respect-content t))
  6821. (org-insert-heading arg invisible-ok)))
  6822. (defun org-insert-todo-heading-respect-content (&optional force-state)
  6823. "Insert TODO heading with `org-insert-heading-respect-content' set to t."
  6824. (interactive "P")
  6825. (let ((org-insert-heading-respect-content t))
  6826. (org-insert-todo-heading force-state t)))
  6827. (defun org-insert-todo-heading (arg &optional force-heading)
  6828. "Insert a new heading with the same level and TODO state as current heading.
  6829. If the heading has no TODO state, or if the state is DONE, use the first
  6830. state (TODO by default). Also one prefix arg, force first state. With two
  6831. prefix args, force inserting at the end of the parent subtree."
  6832. (interactive "P")
  6833. (when (or force-heading (not (org-insert-item 'checkbox)))
  6834. (org-insert-heading (or (and (equal arg '(16)) '(16))
  6835. force-heading))
  6836. (save-excursion
  6837. (org-back-to-heading)
  6838. (outline-previous-heading)
  6839. (looking-at org-todo-line-regexp))
  6840. (let*
  6841. ((new-mark-x
  6842. (if (or arg
  6843. (not (match-beginning 2))
  6844. (member (match-string 2) org-done-keywords))
  6845. (car org-todo-keywords-1)
  6846. (match-string 2)))
  6847. (new-mark
  6848. (or
  6849. (run-hook-with-args-until-success
  6850. 'org-todo-get-default-hook new-mark-x nil)
  6851. new-mark-x)))
  6852. (beginning-of-line 1)
  6853. (and (looking-at org-outline-regexp) (goto-char (match-end 0))
  6854. (if org-treat-insert-todo-heading-as-state-change
  6855. (org-todo new-mark)
  6856. (insert new-mark " "))))
  6857. (when org-provide-todo-statistics
  6858. (org-update-parent-todo-statistics))))
  6859. (defun org-insert-subheading (arg)
  6860. "Insert a new subheading and demote it.
  6861. Works for outline headings and for plain lists alike."
  6862. (interactive "P")
  6863. (org-insert-heading arg)
  6864. (cond
  6865. ((org-at-heading-p) (org-do-demote))
  6866. ((org-at-item-p) (org-indent-item))))
  6867. (defun org-insert-todo-subheading (arg)
  6868. "Insert a new subheading with TODO keyword or checkbox and demote it.
  6869. Works for outline headings and for plain lists alike."
  6870. (interactive "P")
  6871. (org-insert-todo-heading arg)
  6872. (cond
  6873. ((org-at-heading-p) (org-do-demote))
  6874. ((org-at-item-p) (org-indent-item))))
  6875. ;;; Promotion and Demotion
  6876. (defvar org-after-demote-entry-hook nil
  6877. "Hook run after an entry has been demoted.
  6878. The cursor will be at the beginning of the entry.
  6879. When a subtree is being demoted, the hook will be called for each node.")
  6880. (defvar org-after-promote-entry-hook nil
  6881. "Hook run after an entry has been promoted.
  6882. The cursor will be at the beginning of the entry.
  6883. When a subtree is being promoted, the hook will be called for each node.")
  6884. (defun org-promote-subtree ()
  6885. "Promote the entire subtree.
  6886. See also `org-promote'."
  6887. (interactive)
  6888. (save-excursion
  6889. (org-with-limited-levels (org-map-tree 'org-promote)))
  6890. (org-fix-position-after-promote))
  6891. (defun org-demote-subtree ()
  6892. "Demote the entire subtree. See `org-demote'.
  6893. See also `org-promote'."
  6894. (interactive)
  6895. (save-excursion
  6896. (org-with-limited-levels (org-map-tree 'org-demote)))
  6897. (org-fix-position-after-promote))
  6898. (defun org-do-promote ()
  6899. "Promote the current heading higher up the tree.
  6900. If the region is active in `transient-mark-mode', promote all headings
  6901. in the region."
  6902. (interactive)
  6903. (save-excursion
  6904. (if (org-region-active-p)
  6905. (org-map-region 'org-promote (region-beginning) (region-end))
  6906. (org-promote)))
  6907. (org-fix-position-after-promote))
  6908. (defun org-do-demote ()
  6909. "Demote the current heading lower down the tree.
  6910. If the region is active in `transient-mark-mode', demote all headings
  6911. in the region."
  6912. (interactive)
  6913. (save-excursion
  6914. (if (org-region-active-p)
  6915. (org-map-region 'org-demote (region-beginning) (region-end))
  6916. (org-demote)))
  6917. (org-fix-position-after-promote))
  6918. (defun org-fix-position-after-promote ()
  6919. "Make sure that after pro/demotion cursor position is right."
  6920. (let ((pos (point)))
  6921. (when (save-excursion
  6922. (beginning-of-line 1)
  6923. (looking-at org-todo-line-regexp)
  6924. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  6925. (cond ((eobp) (insert " "))
  6926. ((eolp) (insert " "))
  6927. ((equal (char-after) ?\ ) (forward-char 1))))))
  6928. (defun org-current-level ()
  6929. "Return the level of the current entry, or nil if before the first headline.
  6930. The level is the number of stars at the beginning of the headline."
  6931. (save-excursion
  6932. (org-with-limited-levels
  6933. (if (ignore-errors (org-back-to-heading t))
  6934. (funcall outline-level)))))
  6935. (defun org-get-previous-line-level ()
  6936. "Return the outline depth of the last headline before the current line.
  6937. Returns 0 for the first headline in the buffer, and nil if before the
  6938. first headline."
  6939. (let ((current-level (org-current-level))
  6940. (prev-level (when (> (line-number-at-pos) 1)
  6941. (save-excursion
  6942. (beginning-of-line 0)
  6943. (org-current-level)))))
  6944. (cond ((null current-level) nil) ; Before first headline
  6945. ((null prev-level) 0) ; At first headline
  6946. (prev-level))))
  6947. (defun org-reduced-level (l)
  6948. "Compute the effective level of a heading.
  6949. This takes into account the setting of `org-odd-levels-only'."
  6950. (cond
  6951. ((zerop l) 0)
  6952. (org-odd-levels-only (1+ (floor (/ l 2))))
  6953. (t l)))
  6954. (defun org-level-increment ()
  6955. "Return the number of stars that will be added or removed at a
  6956. time to headlines when structure editing, based on the value of
  6957. `org-odd-levels-only'."
  6958. (if org-odd-levels-only 2 1))
  6959. (defun org-get-valid-level (level &optional change)
  6960. "Rectify a level change under the influence of `org-odd-levels-only'
  6961. LEVEL is a current level, CHANGE is by how much the level should be
  6962. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  6963. even level numbers will become the next higher odd number."
  6964. (if org-odd-levels-only
  6965. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  6966. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  6967. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  6968. (max 1 (+ level (or change 0)))))
  6969. (if (boundp 'define-obsolete-function-alias)
  6970. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  6971. (define-obsolete-function-alias 'org-get-legal-level
  6972. 'org-get-valid-level)
  6973. (define-obsolete-function-alias 'org-get-legal-level
  6974. 'org-get-valid-level "23.1")))
  6975. (defvar org-called-with-limited-levels nil) ;; Dynamically bound in
  6976. ;; ̀org-with-limited-levels'
  6977. (defun org-promote ()
  6978. "Promote the current heading higher up the tree.
  6979. If the region is active in `transient-mark-mode', promote all headings
  6980. in the region."
  6981. (org-back-to-heading t)
  6982. (let* ((level (save-match-data (funcall outline-level)))
  6983. (after-change-functions (remove 'flyspell-after-change-function
  6984. after-change-functions))
  6985. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  6986. (diff (abs (- level (length up-head) -1))))
  6987. (cond ((and (= level 1) org-called-with-limited-levels
  6988. org-allow-promoting-top-level-subtree)
  6989. (replace-match "# " nil t))
  6990. ((= level 1)
  6991. (error "Cannot promote to level 0. UNDO to recover if necessary"))
  6992. (t (replace-match up-head nil t)))
  6993. ;; Fixup tag positioning
  6994. (unless (= level 1)
  6995. (and org-auto-align-tags (org-set-tags nil t))
  6996. (if org-adapt-indentation (org-fixup-indentation (- diff))))
  6997. (run-hooks 'org-after-promote-entry-hook)))
  6998. (defun org-demote ()
  6999. "Demote the current heading lower down the tree.
  7000. If the region is active in `transient-mark-mode', demote all headings
  7001. in the region."
  7002. (org-back-to-heading t)
  7003. (let* ((level (save-match-data (funcall outline-level)))
  7004. (after-change-functions (remove 'flyspell-after-change-function
  7005. after-change-functions))
  7006. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  7007. (diff (abs (- level (length down-head) -1))))
  7008. (replace-match down-head nil t)
  7009. ;; Fixup tag positioning
  7010. (and org-auto-align-tags (org-set-tags nil t))
  7011. (if org-adapt-indentation (org-fixup-indentation diff))
  7012. (run-hooks 'org-after-demote-entry-hook)))
  7013. (defun org-cycle-level ()
  7014. "Cycle the level of an empty headline through possible states.
  7015. This goes first to child, then to parent, level, then up the hierarchy.
  7016. After top level, it switches back to sibling level."
  7017. (interactive)
  7018. (let ((org-adapt-indentation nil))
  7019. (when (org-point-at-end-of-empty-headline)
  7020. (setq this-command 'org-cycle-level) ; Only needed for caching
  7021. (let ((cur-level (org-current-level))
  7022. (prev-level (org-get-previous-line-level)))
  7023. (cond
  7024. ;; If first headline in file, promote to top-level.
  7025. ((= prev-level 0)
  7026. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7027. do (org-do-promote)))
  7028. ;; If same level as prev, demote one.
  7029. ((= prev-level cur-level)
  7030. (org-do-demote))
  7031. ;; If parent is top-level, promote to top level if not already.
  7032. ((= prev-level 1)
  7033. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7034. do (org-do-promote)))
  7035. ;; If top-level, return to prev-level.
  7036. ((= cur-level 1)
  7037. (loop repeat (/ (- prev-level 1) (org-level-increment))
  7038. do (org-do-demote)))
  7039. ;; If less than prev-level, promote one.
  7040. ((< cur-level prev-level)
  7041. (org-do-promote))
  7042. ;; If deeper than prev-level, promote until higher than
  7043. ;; prev-level.
  7044. ((> cur-level prev-level)
  7045. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  7046. do (org-do-promote))))
  7047. t))))
  7048. (defun org-map-tree (fun)
  7049. "Call FUN for every heading underneath the current one."
  7050. (org-back-to-heading)
  7051. (let ((level (funcall outline-level)))
  7052. (save-excursion
  7053. (funcall fun)
  7054. (while (and (progn
  7055. (outline-next-heading)
  7056. (> (funcall outline-level) level))
  7057. (not (eobp)))
  7058. (funcall fun)))))
  7059. (defun org-map-region (fun beg end)
  7060. "Call FUN for every heading between BEG and END."
  7061. (let ((org-ignore-region t))
  7062. (save-excursion
  7063. (setq end (copy-marker end))
  7064. (goto-char beg)
  7065. (if (and (re-search-forward org-outline-regexp-bol nil t)
  7066. (< (point) end))
  7067. (funcall fun))
  7068. (while (and (progn
  7069. (outline-next-heading)
  7070. (< (point) end))
  7071. (not (eobp)))
  7072. (funcall fun)))))
  7073. (defvar org-property-end-re) ; silence byte-compiler
  7074. (defun org-fixup-indentation (diff)
  7075. "Change the indentation in the current entry by DIFF.
  7076. However, if any line in the current entry has no indentation, or if it
  7077. would end up with no indentation after the change, nothing at all is done."
  7078. (save-excursion
  7079. (let ((end (save-excursion (outline-next-heading)
  7080. (point-marker)))
  7081. (prohibit (if (> diff 0)
  7082. "^\\S-"
  7083. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  7084. col)
  7085. (unless (save-excursion (end-of-line 1)
  7086. (re-search-forward prohibit end t))
  7087. (while (and (< (point) end)
  7088. (re-search-forward "^[ \t]+" end t))
  7089. (goto-char (match-end 0))
  7090. (setq col (current-column))
  7091. (if (< diff 0) (replace-match ""))
  7092. (org-indent-to-column (+ diff col))))
  7093. (move-marker end nil))))
  7094. (defun org-convert-to-odd-levels ()
  7095. "Convert an org-mode file with all levels allowed to one with odd levels.
  7096. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  7097. level 5 etc."
  7098. (interactive)
  7099. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  7100. (let ((outline-level 'org-outline-level)
  7101. (org-odd-levels-only nil) n)
  7102. (save-excursion
  7103. (goto-char (point-min))
  7104. (while (re-search-forward "^\\*\\*+ " nil t)
  7105. (setq n (- (length (match-string 0)) 2))
  7106. (while (>= (setq n (1- n)) 0)
  7107. (org-demote))
  7108. (end-of-line 1))))))
  7109. (defun org-convert-to-oddeven-levels ()
  7110. "Convert an org-mode file with only odd levels to one with odd/even levels.
  7111. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  7112. file contains a section with an even level, conversion would
  7113. destroy the structure of the file. An error is signaled in this
  7114. case."
  7115. (interactive)
  7116. (goto-char (point-min))
  7117. ;; First check if there are no even levels
  7118. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  7119. (org-show-context t)
  7120. (error "Not all levels are odd in this file. Conversion not possible"))
  7121. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  7122. (let ((outline-regexp org-outline-regexp)
  7123. (outline-level 'org-outline-level)
  7124. (org-odd-levels-only nil) n)
  7125. (save-excursion
  7126. (goto-char (point-min))
  7127. (while (re-search-forward "^\\*\\*+ " nil t)
  7128. (setq n (/ (1- (length (match-string 0))) 2))
  7129. (while (>= (setq n (1- n)) 0)
  7130. (org-promote))
  7131. (end-of-line 1))))))
  7132. (defun org-tr-level (n)
  7133. "Make N odd if required."
  7134. (if org-odd-levels-only (1+ (/ n 2)) n))
  7135. ;;; Vertical tree motion, cutting and pasting of subtrees
  7136. (defun org-move-subtree-up (&optional arg)
  7137. "Move the current subtree up past ARG headlines of the same level."
  7138. (interactive "p")
  7139. (org-move-subtree-down (- (prefix-numeric-value arg))))
  7140. (defun org-move-subtree-down (&optional arg)
  7141. "Move the current subtree down past ARG headlines of the same level."
  7142. (interactive "p")
  7143. (setq arg (prefix-numeric-value arg))
  7144. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  7145. 'org-get-last-sibling))
  7146. (ins-point (make-marker))
  7147. (cnt (abs arg))
  7148. (col (current-column))
  7149. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  7150. ;; Select the tree
  7151. (org-back-to-heading)
  7152. (setq beg0 (point))
  7153. (save-excursion
  7154. (setq ne-beg (org-back-over-empty-lines))
  7155. (setq beg (point)))
  7156. (save-match-data
  7157. (save-excursion (outline-end-of-heading)
  7158. (setq folded (outline-invisible-p)))
  7159. (outline-end-of-subtree))
  7160. (outline-next-heading)
  7161. (setq ne-end (org-back-over-empty-lines))
  7162. (setq end (point))
  7163. (goto-char beg0)
  7164. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  7165. ;; include less whitespace
  7166. (save-excursion
  7167. (goto-char beg)
  7168. (forward-line (- ne-beg ne-end))
  7169. (setq beg (point))))
  7170. ;; Find insertion point, with error handling
  7171. (while (> cnt 0)
  7172. (or (and (funcall movfunc) (looking-at org-outline-regexp))
  7173. (progn (goto-char beg0)
  7174. (user-error "Cannot move past superior level or buffer limit")))
  7175. (setq cnt (1- cnt)))
  7176. (if (> arg 0)
  7177. ;; Moving forward - still need to move over subtree
  7178. (progn (org-end-of-subtree t t)
  7179. (save-excursion
  7180. (org-back-over-empty-lines)
  7181. (or (bolp) (newline)))))
  7182. (setq ne-ins (org-back-over-empty-lines))
  7183. (move-marker ins-point (point))
  7184. (setq txt (buffer-substring beg end))
  7185. (org-save-markers-in-region beg end)
  7186. (delete-region beg end)
  7187. (org-remove-empty-overlays-at beg)
  7188. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  7189. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  7190. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  7191. (let ((bbb (point)))
  7192. (insert-before-markers txt)
  7193. (org-reinstall-markers-in-region bbb)
  7194. (move-marker ins-point bbb))
  7195. (or (bolp) (insert "\n"))
  7196. (setq ins-end (point))
  7197. (goto-char ins-point)
  7198. (org-skip-whitespace)
  7199. (when (and (< arg 0)
  7200. (org-first-sibling-p)
  7201. (> ne-ins ne-beg))
  7202. ;; Move whitespace back to beginning
  7203. (save-excursion
  7204. (goto-char ins-end)
  7205. (let ((kill-whole-line t))
  7206. (kill-line (- ne-ins ne-beg)) (point)))
  7207. (insert (make-string (- ne-ins ne-beg) ?\n)))
  7208. (move-marker ins-point nil)
  7209. (if folded
  7210. (hide-subtree)
  7211. (org-show-entry)
  7212. (show-children)
  7213. (org-cycle-hide-drawers 'children))
  7214. (org-clean-visibility-after-subtree-move)
  7215. ;; move back to the initial column we were at
  7216. (move-to-column col)))
  7217. (defvar org-subtree-clip ""
  7218. "Clipboard for cut and paste of subtrees.
  7219. This is actually only a copy of the kill, because we use the normal kill
  7220. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  7221. (defvar org-subtree-clip-folded nil
  7222. "Was the last copied subtree folded?
  7223. This is used to fold the tree back after pasting.")
  7224. (defun org-cut-subtree (&optional n)
  7225. "Cut the current subtree into the clipboard.
  7226. With prefix arg N, cut this many sequential subtrees.
  7227. This is a short-hand for marking the subtree and then cutting it."
  7228. (interactive "p")
  7229. (org-copy-subtree n 'cut))
  7230. (defun org-copy-subtree (&optional n cut force-store-markers nosubtrees)
  7231. "Cut the current subtree into the clipboard.
  7232. With prefix arg N, cut this many sequential subtrees.
  7233. This is a short-hand for marking the subtree and then copying it.
  7234. If CUT is non-nil, actually cut the subtree.
  7235. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  7236. of some markers in the region, even if CUT is non-nil. This is
  7237. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  7238. (interactive "p")
  7239. (let (beg end folded (beg0 (point)))
  7240. (if (org-called-interactively-p 'any)
  7241. (org-back-to-heading nil) ; take what looks like a subtree
  7242. (org-back-to-heading t)) ; take what is really there
  7243. (setq beg (point))
  7244. (skip-chars-forward " \t\r\n")
  7245. (save-match-data
  7246. (if nosubtrees
  7247. (outline-next-heading)
  7248. (save-excursion (outline-end-of-heading)
  7249. (setq folded (outline-invisible-p)))
  7250. (condition-case nil
  7251. (org-forward-heading-same-level (1- n) t)
  7252. (error nil))
  7253. (org-end-of-subtree t t)))
  7254. (setq end (point))
  7255. (goto-char beg0)
  7256. (when (> end beg)
  7257. (setq org-subtree-clip-folded folded)
  7258. (when (or cut force-store-markers)
  7259. (org-save-markers-in-region beg end))
  7260. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  7261. (setq org-subtree-clip (current-kill 0))
  7262. (message "%s: Subtree(s) with %d characters"
  7263. (if cut "Cut" "Copied")
  7264. (length org-subtree-clip)))))
  7265. (defun org-paste-subtree (&optional level tree for-yank)
  7266. "Paste the clipboard as a subtree, with modification of headline level.
  7267. The entire subtree is promoted or demoted in order to match a new headline
  7268. level.
  7269. If the cursor is at the beginning of a headline, the same level as
  7270. that headline is used to paste the tree.
  7271. If not, the new level is derived from the *visible* headings
  7272. before and after the insertion point, and taken to be the inferior headline
  7273. level of the two. So if the previous visible heading is level 3 and the
  7274. next is level 4 (or vice versa), level 4 will be used for insertion.
  7275. This makes sure that the subtree remains an independent subtree and does
  7276. not swallow low level entries.
  7277. You can also force a different level, either by using a numeric prefix
  7278. argument, or by inserting the heading marker by hand. For example, if the
  7279. cursor is after \"*****\", then the tree will be shifted to level 5.
  7280. If optional TREE is given, use this text instead of the kill ring.
  7281. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  7282. move back over whitespace before inserting, and move point to the end of
  7283. the inserted text when done."
  7284. (interactive "P")
  7285. (setq tree (or tree (and kill-ring (current-kill 0))))
  7286. (unless (org-kill-is-subtree-p tree)
  7287. (error "%s"
  7288. (substitute-command-keys
  7289. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  7290. (org-with-limited-levels
  7291. (let* ((visp (not (outline-invisible-p)))
  7292. (txt tree)
  7293. (^re_ "\\(\\*+\\)[ \t]*")
  7294. (old-level (if (string-match org-outline-regexp-bol txt)
  7295. (- (match-end 0) (match-beginning 0) 1)
  7296. -1))
  7297. (force-level (cond (level (prefix-numeric-value level))
  7298. ((and (looking-at "[ \t]*$")
  7299. (string-match
  7300. "^\\*+$" (buffer-substring
  7301. (point-at-bol) (point))))
  7302. (- (match-end 1) (match-beginning 1)))
  7303. ((and (bolp)
  7304. (looking-at org-outline-regexp))
  7305. (- (match-end 0) (point) 1))))
  7306. (previous-level (save-excursion
  7307. (condition-case nil
  7308. (progn
  7309. (outline-previous-visible-heading 1)
  7310. (if (looking-at ^re_)
  7311. (- (match-end 0) (match-beginning 0) 1)
  7312. 1))
  7313. (error 1))))
  7314. (next-level (save-excursion
  7315. (condition-case nil
  7316. (progn
  7317. (or (looking-at org-outline-regexp)
  7318. (outline-next-visible-heading 1))
  7319. (if (looking-at ^re_)
  7320. (- (match-end 0) (match-beginning 0) 1)
  7321. 1))
  7322. (error 1))))
  7323. (new-level (or force-level (max previous-level next-level)))
  7324. (shift (if (or (= old-level -1)
  7325. (= new-level -1)
  7326. (= old-level new-level))
  7327. 0
  7328. (- new-level old-level)))
  7329. (delta (if (> shift 0) -1 1))
  7330. (func (if (> shift 0) 'org-demote 'org-promote))
  7331. (org-odd-levels-only nil)
  7332. beg end newend)
  7333. ;; Remove the forced level indicator
  7334. (if force-level
  7335. (delete-region (point-at-bol) (point)))
  7336. ;; Paste
  7337. (beginning-of-line (if (bolp) 1 2))
  7338. (setq beg (point))
  7339. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  7340. (insert-before-markers txt)
  7341. (unless (string-match "\n\\'" txt) (insert "\n"))
  7342. (setq newend (point))
  7343. (org-reinstall-markers-in-region beg)
  7344. (setq end (point))
  7345. (goto-char beg)
  7346. (skip-chars-forward " \t\n\r")
  7347. (setq beg (point))
  7348. (if (and (outline-invisible-p) visp)
  7349. (save-excursion (outline-show-heading)))
  7350. ;; Shift if necessary
  7351. (unless (= shift 0)
  7352. (save-restriction
  7353. (narrow-to-region beg end)
  7354. (while (not (= shift 0))
  7355. (org-map-region func (point-min) (point-max))
  7356. (setq shift (+ delta shift)))
  7357. (goto-char (point-min))
  7358. (setq newend (point-max))))
  7359. (when (or (org-called-interactively-p 'interactive) for-yank)
  7360. (message "Clipboard pasted as level %d subtree" new-level))
  7361. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  7362. kill-ring
  7363. (eq org-subtree-clip (current-kill 0))
  7364. org-subtree-clip-folded)
  7365. ;; The tree was folded before it was killed/copied
  7366. (hide-subtree))
  7367. (and for-yank (goto-char newend)))))
  7368. (defun org-kill-is-subtree-p (&optional txt)
  7369. "Check if the current kill is an outline subtree, or a set of trees.
  7370. Returns nil if kill does not start with a headline, or if the first
  7371. headline level is not the largest headline level in the tree.
  7372. So this will actually accept several entries of equal levels as well,
  7373. which is OK for `org-paste-subtree'.
  7374. If optional TXT is given, check this string instead of the current kill."
  7375. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  7376. (re (org-get-limited-outline-regexp))
  7377. (^re (concat "^" re))
  7378. (start-level (and kill
  7379. (string-match
  7380. (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
  7381. kill)
  7382. (- (match-end 2) (match-beginning 2) 1)))
  7383. (start (1+ (or (match-beginning 2) -1))))
  7384. (if (not start-level)
  7385. (progn
  7386. nil) ;; does not even start with a heading
  7387. (catch 'exit
  7388. (while (setq start (string-match ^re kill (1+ start)))
  7389. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  7390. (throw 'exit nil)))
  7391. t))))
  7392. (defvar org-markers-to-move nil
  7393. "Markers that should be moved with a cut-and-paste operation.
  7394. Those markers are stored together with their positions relative to
  7395. the start of the region.")
  7396. (defun org-save-markers-in-region (beg end)
  7397. "Check markers in region.
  7398. If these markers are between BEG and END, record their position relative
  7399. to BEG, so that after moving the block of text, we can put the markers back
  7400. into place.
  7401. This function gets called just before an entry or tree gets cut from the
  7402. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  7403. called immediately, to move the markers with the entries."
  7404. (setq org-markers-to-move nil)
  7405. (when (featurep 'org-clock)
  7406. (org-clock-save-markers-for-cut-and-paste beg end))
  7407. (when (featurep 'org-agenda)
  7408. (org-agenda-save-markers-for-cut-and-paste beg end)))
  7409. (defun org-check-and-save-marker (marker beg end)
  7410. "Check if MARKER is between BEG and END.
  7411. If yes, remember the marker and the distance to BEG."
  7412. (when (and (marker-buffer marker)
  7413. (equal (marker-buffer marker) (current-buffer)))
  7414. (if (and (>= marker beg) (< marker end))
  7415. (push (cons marker (- marker beg)) org-markers-to-move))))
  7416. (defun org-reinstall-markers-in-region (beg)
  7417. "Move all remembered markers to their position relative to BEG."
  7418. (mapc (lambda (x)
  7419. (move-marker (car x) (+ beg (cdr x))))
  7420. org-markers-to-move)
  7421. (setq org-markers-to-move nil))
  7422. (defun org-narrow-to-subtree ()
  7423. "Narrow buffer to the current subtree."
  7424. (interactive)
  7425. (save-excursion
  7426. (save-match-data
  7427. (org-with-limited-levels
  7428. (narrow-to-region
  7429. (progn (org-back-to-heading t) (point))
  7430. (progn (org-end-of-subtree t t)
  7431. (if (and (org-at-heading-p) (not (eobp))) (backward-char 1))
  7432. (point)))))))
  7433. (defun org-narrow-to-block ()
  7434. "Narrow buffer to the current block."
  7435. (interactive)
  7436. (let* ((case-fold-search t)
  7437. (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
  7438. "^[ \t]*#\\+end_.*")))
  7439. (if blockp
  7440. (narrow-to-region (car blockp) (cdr blockp))
  7441. (error "Not in a block"))))
  7442. (eval-when-compile
  7443. (defvar org-property-drawer-re))
  7444. (defvar org-property-start-re) ;; defined below
  7445. (defun org-clone-subtree-with-time-shift (n &optional shift)
  7446. "Clone the task (subtree) at point N times.
  7447. The clones will be inserted as siblings.
  7448. In interactive use, the user will be prompted for the number of
  7449. clones to be produced, and for a time SHIFT, which may be a
  7450. repeater as used in time stamps, for example `+3d'.
  7451. When a valid repeater is given and the entry contains any time
  7452. stamps, the clones will become a sequence in time, with time
  7453. stamps in the subtree shifted for each clone produced. If SHIFT
  7454. is nil or the empty string, time stamps will be left alone. The
  7455. ID property of the original subtree is removed.
  7456. If the original subtree did contain time stamps with a repeater,
  7457. the following will happen:
  7458. - the repeater will be removed in each clone
  7459. - an additional clone will be produced, with the current, unshifted
  7460. date(s) in the entry.
  7461. - the original entry will be placed *after* all the clones, with
  7462. repeater intact.
  7463. - the start days in the repeater in the original entry will be shifted
  7464. to past the last clone.
  7465. In this way you can spell out a number of instances of a repeating task,
  7466. and still retain the repeater to cover future instances of the task."
  7467. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  7468. (let (beg end template task idprop
  7469. shift-n shift-what doshift nmin nmax (n-no-remove -1)
  7470. (drawer-re org-drawer-regexp))
  7471. (if (not (and (integerp n) (> n 0)))
  7472. (error "Invalid number of replications %s" n))
  7473. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  7474. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([hdwmy]\\)[ \t]*\\'"
  7475. shift)))
  7476. (error "Invalid shift specification %s" shift))
  7477. (when doshift
  7478. (setq shift-n (string-to-number (match-string 1 shift))
  7479. shift-what (cdr (assoc (match-string 2 shift)
  7480. '(("d" . day) ("w" . week)
  7481. ("m" . month) ("y" . year))))))
  7482. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  7483. (setq nmin 1 nmax n)
  7484. (org-back-to-heading t)
  7485. (setq beg (point))
  7486. (setq idprop (org-entry-get nil "ID"))
  7487. (org-end-of-subtree t t)
  7488. (or (bolp) (insert "\n"))
  7489. (setq end (point))
  7490. (setq template (buffer-substring beg end))
  7491. (when (and doshift
  7492. (string-match "<[^<>\n]+ [.+]?\\+[0-9]+[hdwmy][^<>\n]*>" template))
  7493. (delete-region beg end)
  7494. (setq end beg)
  7495. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  7496. (goto-char end)
  7497. (loop for n from nmin to nmax do
  7498. ;; prepare clone
  7499. (with-temp-buffer
  7500. (insert template)
  7501. (org-mode)
  7502. (goto-char (point-min))
  7503. (org-show-subtree)
  7504. (and idprop (if org-clone-delete-id
  7505. (org-entry-delete nil "ID")
  7506. (org-id-get-create t)))
  7507. (unless (= n 0)
  7508. (while (re-search-forward "^[ \t]*CLOCK:.*$" nil t)
  7509. (kill-whole-line))
  7510. (goto-char (point-min))
  7511. (while (re-search-forward drawer-re nil t)
  7512. (mapc (lambda (d)
  7513. (org-remove-empty-drawer-at d (point))) org-drawers)))
  7514. (goto-char (point-min))
  7515. (when doshift
  7516. (while (re-search-forward org-ts-regexp-both nil t)
  7517. (org-timestamp-change (* n shift-n) shift-what))
  7518. (unless (= n n-no-remove)
  7519. (goto-char (point-min))
  7520. (while (re-search-forward org-ts-regexp nil t)
  7521. (save-excursion
  7522. (goto-char (match-beginning 0))
  7523. (if (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[hdwmy]\\)")
  7524. (delete-region (match-beginning 1) (match-end 1)))))))
  7525. (setq task (buffer-string)))
  7526. (insert task))
  7527. (goto-char beg)))
  7528. ;;; Outline Sorting
  7529. (defun org-sort (with-case)
  7530. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  7531. Optional argument WITH-CASE means sort case-sensitively."
  7532. (interactive "P")
  7533. (cond
  7534. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  7535. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  7536. (t
  7537. (org-call-with-arg 'org-sort-entries with-case))))
  7538. (defun org-sort-remove-invisible (s)
  7539. "Remove invisible links from string S."
  7540. (remove-text-properties 0 (length s) org-rm-props s)
  7541. (while (string-match org-bracket-link-regexp s)
  7542. (setq s (replace-match (if (match-end 2)
  7543. (match-string 3 s)
  7544. (match-string 1 s)) t t s)))
  7545. (let ((st (format " %s " s)))
  7546. (while (string-match org-emph-re st)
  7547. (setq st (replace-match (format " %s " (match-string 4 st)) t t st)))
  7548. (setq s (substring st 1 -1)))
  7549. s)
  7550. (defvar org-priority-regexp) ; defined later in the file
  7551. (defvar org-after-sorting-entries-or-items-hook nil
  7552. "Hook that is run after a bunch of entries or items have been sorted.
  7553. When children are sorted, the cursor is in the parent line when this
  7554. hook gets called. When a region or a plain list is sorted, the cursor
  7555. will be in the first entry of the sorted region/list.")
  7556. (defun org-sort-entries
  7557. (&optional with-case sorting-type getkey-func compare-func property)
  7558. "Sort entries on a certain level of an outline tree.
  7559. If there is an active region, the entries in the region are sorted.
  7560. Else, if the cursor is before the first entry, sort the top-level items.
  7561. Else, the children of the entry at point are sorted.
  7562. Sorting can be alphabetically, numerically, by date/time as given by
  7563. a time stamp, by a property, by priority order, or by a custom function.
  7564. The command prompts for the sorting type unless it has been given to the
  7565. function through the SORTING-TYPE argument, which needs to be a character,
  7566. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?o ?O ?r ?R ?f ?F). Here is the
  7567. precise meaning of each character:
  7568. n Numerically, by converting the beginning of the entry/item to a number.
  7569. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  7570. o By order of TODO keywords.
  7571. t By date/time, either the first active time stamp in the entry, or, if
  7572. none exist, by the first inactive one.
  7573. s By the scheduled date/time.
  7574. d By deadline date/time.
  7575. c By creation time, which is assumed to be the first inactive time stamp
  7576. at the beginning of a line.
  7577. p By priority according to the cookie.
  7578. r By the value of a property.
  7579. Capital letters will reverse the sort order.
  7580. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  7581. called with point at the beginning of the record. It must return either
  7582. a string or a number that should serve as the sorting key for that record.
  7583. Comparing entries ignores case by default. However, with an optional argument
  7584. WITH-CASE, the sorting considers case as well.
  7585. Sorting is done against the visible part of the headlines, it ignores hidden
  7586. links."
  7587. (interactive "P")
  7588. (let ((case-func (if with-case 'identity 'downcase))
  7589. (cmstr
  7590. ;; The clock marker is lost when using `sort-subr', let's
  7591. ;; store the clocking string.
  7592. (when (equal (marker-buffer org-clock-marker) (current-buffer))
  7593. (save-excursion
  7594. (goto-char org-clock-marker)
  7595. (looking-back "^.*") (match-string-no-properties 0))))
  7596. start beg end stars re re2
  7597. txt what tmp)
  7598. ;; Find beginning and end of region to sort
  7599. (cond
  7600. ((org-region-active-p)
  7601. ;; we will sort the region
  7602. (setq end (region-end)
  7603. what "region")
  7604. (goto-char (region-beginning))
  7605. (if (not (org-at-heading-p)) (outline-next-heading))
  7606. (setq start (point)))
  7607. ((or (org-at-heading-p)
  7608. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  7609. ;; we will sort the children of the current headline
  7610. (org-back-to-heading)
  7611. (setq start (point)
  7612. end (progn (org-end-of-subtree t t)
  7613. (or (bolp) (insert "\n"))
  7614. (org-back-over-empty-lines)
  7615. (point))
  7616. what "children")
  7617. (goto-char start)
  7618. (show-subtree)
  7619. (outline-next-heading))
  7620. (t
  7621. ;; we will sort the top-level entries in this file
  7622. (goto-char (point-min))
  7623. (or (org-at-heading-p) (outline-next-heading))
  7624. (setq start (point))
  7625. (goto-char (point-max))
  7626. (beginning-of-line 1)
  7627. (when (looking-at ".*?\\S-")
  7628. ;; File ends in a non-white line
  7629. (end-of-line 1)
  7630. (insert "\n"))
  7631. (setq end (point-max))
  7632. (setq what "top-level")
  7633. (goto-char start)
  7634. (show-all)))
  7635. (setq beg (point))
  7636. (if (>= beg end) (error "Nothing to sort"))
  7637. (looking-at "\\(\\*+\\)")
  7638. (setq stars (match-string 1)
  7639. re (concat "^" (regexp-quote stars) " +")
  7640. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  7641. txt (buffer-substring beg end))
  7642. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  7643. (if (and (not (equal stars "*")) (string-match re2 txt))
  7644. (error "Region to sort contains a level above the first entry"))
  7645. (unless sorting-type
  7646. (message
  7647. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  7648. [t]ime [s]cheduled [d]eadline [c]reated
  7649. A/N/P/R/O/F/T/S/D/C means reversed:"
  7650. what)
  7651. (setq sorting-type (read-char-exclusive))
  7652. (unless getkey-func
  7653. (and (= (downcase sorting-type) ?f)
  7654. (setq getkey-func
  7655. (org-icompleting-read "Sort using function: "
  7656. obarray 'fboundp t nil nil))
  7657. (setq getkey-func (intern getkey-func))))
  7658. (and (= (downcase sorting-type) ?r)
  7659. (not property)
  7660. (setq property
  7661. (org-icompleting-read "Property: "
  7662. (mapcar 'list (org-buffer-property-keys t))
  7663. nil t))))
  7664. (message "Sorting entries...")
  7665. (save-restriction
  7666. (narrow-to-region start end)
  7667. (let ((dcst (downcase sorting-type))
  7668. (case-fold-search nil)
  7669. (now (current-time)))
  7670. (sort-subr
  7671. (/= dcst sorting-type)
  7672. ;; This function moves to the beginning character of the "record" to
  7673. ;; be sorted.
  7674. (lambda nil
  7675. (if (re-search-forward re nil t)
  7676. (goto-char (match-beginning 0))
  7677. (goto-char (point-max))))
  7678. ;; This function moves to the last character of the "record" being
  7679. ;; sorted.
  7680. (lambda nil
  7681. (save-match-data
  7682. (condition-case nil
  7683. (outline-forward-same-level 1)
  7684. (error
  7685. (goto-char (point-max))))))
  7686. ;; This function returns the value that gets sorted against.
  7687. (lambda nil
  7688. (cond
  7689. ((= dcst ?n)
  7690. (if (looking-at org-complex-heading-regexp)
  7691. (string-to-number (org-sort-remove-invisible (match-string 4)))
  7692. nil))
  7693. ((= dcst ?a)
  7694. (if (looking-at org-complex-heading-regexp)
  7695. (funcall case-func (org-sort-remove-invisible (match-string 4)))
  7696. nil))
  7697. ((= dcst ?t)
  7698. (let ((end (save-excursion (outline-next-heading) (point))))
  7699. (if (or (re-search-forward org-ts-regexp end t)
  7700. (re-search-forward org-ts-regexp-both end t))
  7701. (org-time-string-to-seconds (match-string 0))
  7702. (org-float-time now))))
  7703. ((= dcst ?c)
  7704. (let ((end (save-excursion (outline-next-heading) (point))))
  7705. (if (re-search-forward
  7706. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  7707. end t)
  7708. (org-time-string-to-seconds (match-string 0))
  7709. (org-float-time now))))
  7710. ((= dcst ?s)
  7711. (let ((end (save-excursion (outline-next-heading) (point))))
  7712. (if (re-search-forward org-scheduled-time-regexp end t)
  7713. (org-time-string-to-seconds (match-string 1))
  7714. (org-float-time now))))
  7715. ((= dcst ?d)
  7716. (let ((end (save-excursion (outline-next-heading) (point))))
  7717. (if (re-search-forward org-deadline-time-regexp end t)
  7718. (org-time-string-to-seconds (match-string 1))
  7719. (org-float-time now))))
  7720. ((= dcst ?p)
  7721. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  7722. (string-to-char (match-string 2))
  7723. org-default-priority))
  7724. ((= dcst ?r)
  7725. (or (org-entry-get nil property) ""))
  7726. ((= dcst ?o)
  7727. (if (looking-at org-complex-heading-regexp)
  7728. (- 9999 (length (member (match-string 2)
  7729. org-todo-keywords-1)))))
  7730. ((= dcst ?f)
  7731. (if getkey-func
  7732. (progn
  7733. (setq tmp (funcall getkey-func))
  7734. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  7735. tmp)
  7736. (error "Invalid key function `%s'" getkey-func)))
  7737. (t (error "Invalid sorting type `%c'" sorting-type))))
  7738. nil
  7739. (cond
  7740. ((= dcst ?a) 'string<)
  7741. ((= dcst ?f) compare-func)
  7742. ((member dcst '(?p ?t ?s ?d ?c)) '<)))))
  7743. (run-hooks 'org-after-sorting-entries-or-items-hook)
  7744. ;; Reset the clock marker if needed
  7745. (when cmstr
  7746. (save-excursion
  7747. (goto-char start)
  7748. (search-forward cmstr nil t)
  7749. (move-marker org-clock-marker (point))))
  7750. (message "Sorting entries...done")))
  7751. (defun org-do-sort (table what &optional with-case sorting-type)
  7752. "Sort TABLE of WHAT according to SORTING-TYPE.
  7753. The user will be prompted for the SORTING-TYPE if the call to this
  7754. function does not specify it. WHAT is only for the prompt, to indicate
  7755. what is being sorted. The sorting key will be extracted from
  7756. the car of the elements of the table.
  7757. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  7758. (unless sorting-type
  7759. (message
  7760. "Sort %s: [a]lphabetic, [n]umeric, [t]ime. A/N/T means reversed:"
  7761. what)
  7762. (setq sorting-type (read-char-exclusive)))
  7763. (let ((dcst (downcase sorting-type))
  7764. extractfun comparefun)
  7765. ;; Define the appropriate functions
  7766. (cond
  7767. ((= dcst ?n)
  7768. (setq extractfun 'string-to-number
  7769. comparefun (if (= dcst sorting-type) '< '>)))
  7770. ((= dcst ?a)
  7771. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  7772. (lambda(x) (downcase (org-sort-remove-invisible x))))
  7773. comparefun (if (= dcst sorting-type)
  7774. 'string<
  7775. (lambda (a b) (and (not (string< a b))
  7776. (not (string= a b)))))))
  7777. ((= dcst ?t)
  7778. (setq extractfun
  7779. (lambda (x)
  7780. (if (or (string-match org-ts-regexp x)
  7781. (string-match org-ts-regexp-both x))
  7782. (org-float-time
  7783. (org-time-string-to-time (match-string 0 x)))
  7784. 0))
  7785. comparefun (if (= dcst sorting-type) '< '>)))
  7786. (t (error "Invalid sorting type `%c'" sorting-type)))
  7787. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  7788. table)
  7789. (lambda (a b) (funcall comparefun (car a) (car b))))))
  7790. ;;; The orgstruct minor mode
  7791. ;; Define a minor mode which can be used in other modes in order to
  7792. ;; integrate the org-mode structure editing commands.
  7793. ;; This is really a hack, because the org-mode structure commands use
  7794. ;; keys which normally belong to the major mode. Here is how it
  7795. ;; works: The minor mode defines all the keys necessary to operate the
  7796. ;; structure commands, but wraps the commands into a function which
  7797. ;; tests if the cursor is currently at a headline or a plain list
  7798. ;; item. If that is the case, the structure command is used,
  7799. ;; temporarily setting many Org-mode variables like regular
  7800. ;; expressions for filling etc. However, when any of those keys is
  7801. ;; used at a different location, function uses `key-binding' to look
  7802. ;; up if the key has an associated command in another currently active
  7803. ;; keymap (minor modes, major mode, global), and executes that
  7804. ;; command. There might be problems if any of the keys is otherwise
  7805. ;; used as a prefix key.
  7806. (defcustom orgstruct-heading-prefix-regexp nil
  7807. "Regexp that matches the custom prefix of Org headlines in
  7808. orgstruct(++)-mode."
  7809. :group 'org
  7810. :version "24.4"
  7811. :package-version '(Org . "8.0")
  7812. :type 'string)
  7813. ;;;###autoload(put 'orgstruct-heading-prefix-regexp 'safe-local-variable 'stringp)
  7814. (defcustom orgstruct-setup-hook nil
  7815. "Hook run after orgstruct-mode-map is filled."
  7816. :group 'org
  7817. :version "24.4"
  7818. :package-version '(Org . "8.0")
  7819. :type 'hook)
  7820. (defvar orgstruct-initialized nil)
  7821. (defvar org-local-vars nil
  7822. "List of local variables, for use by `orgstruct-mode'.")
  7823. ;;;###autoload
  7824. (define-minor-mode orgstruct-mode
  7825. "Toggle the minor mode `orgstruct-mode'.
  7826. This mode is for using Org-mode structure commands in other
  7827. modes. The following keys behave as if Org-mode were active, if
  7828. the cursor is on a headline, or on a plain list item (both as
  7829. defined by Org-mode)."
  7830. nil " OrgStruct" (make-sparse-keymap)
  7831. (when orgstruct-mode
  7832. (org-load-modules-maybe)
  7833. (unless orgstruct-initialized
  7834. (orgstruct-setup)
  7835. (setq orgstruct-initialized t))))
  7836. ;;;###autoload
  7837. (defun turn-on-orgstruct ()
  7838. "Unconditionally turn on `orgstruct-mode'."
  7839. (orgstruct-mode 1))
  7840. (defvar org-fb-vars nil)
  7841. (make-variable-buffer-local 'org-fb-vars)
  7842. (defun orgstruct++-mode (&optional arg)
  7843. "Toggle `orgstruct-mode', the enhanced version of it.
  7844. In addition to setting orgstruct-mode, this also exports all
  7845. indentation and autofilling variables from org-mode into the
  7846. buffer. It will also recognize item context in multiline items."
  7847. (interactive "P")
  7848. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  7849. (if (< arg 1)
  7850. (progn (orgstruct-mode -1)
  7851. (mapc (lambda(v)
  7852. (org-set-local (car v)
  7853. (if (eq (car-safe (cadr v)) 'quote) (cadadr v) (cadr v))))
  7854. org-fb-vars))
  7855. (orgstruct-mode 1)
  7856. (setq org-fb-vars nil)
  7857. (let (var val)
  7858. (mapc
  7859. (lambda (x)
  7860. (when (string-match
  7861. "^\\(paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|fill-prefix\\|indent-\\)"
  7862. (symbol-name (car x)))
  7863. (setq var (car x) val (nth 1 x))
  7864. (push (list var `(quote ,(eval var))) org-fb-vars)
  7865. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  7866. org-local-vars)
  7867. (org-set-local 'orgstruct-is-++ t))))
  7868. (defvar orgstruct-is-++ nil
  7869. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  7870. (make-variable-buffer-local 'orgstruct-is-++)
  7871. ;;;###autoload
  7872. (defun turn-on-orgstruct++ ()
  7873. "Unconditionally turn on `orgstruct++-mode'."
  7874. (orgstruct++-mode 1))
  7875. (defun orgstruct-error ()
  7876. "Error when there is no default binding for a structure key."
  7877. (interactive)
  7878. (funcall (if (fboundp 'user-error)
  7879. 'user-error
  7880. 'error)
  7881. "This key has no function outside structure elements"))
  7882. (defun orgstruct-setup ()
  7883. "Setup orgstruct keymap."
  7884. (dolist (cell '((org-demote . t)
  7885. (org-metaleft . t)
  7886. (org-metaright . t)
  7887. (org-promote . t)
  7888. (org-shiftmetaleft . t)
  7889. (org-shiftmetaright . t)
  7890. org-backward-element
  7891. org-backward-heading-same-level
  7892. org-ctrl-c-ret
  7893. org-ctrl-c-minus
  7894. org-ctrl-c-star
  7895. org-cycle
  7896. org-forward-heading-same-level
  7897. org-insert-heading
  7898. org-insert-heading-respect-content
  7899. org-kill-note-or-show-branches
  7900. org-mark-subtree
  7901. org-meta-return
  7902. org-metadown
  7903. org-metaup
  7904. org-narrow-to-subtree
  7905. org-promote-subtree
  7906. org-reveal
  7907. org-shiftdown
  7908. org-shiftleft
  7909. org-shiftmetadown
  7910. org-shiftmetaup
  7911. org-shiftright
  7912. org-shifttab
  7913. org-shifttab
  7914. org-shiftup
  7915. org-show-subtree
  7916. org-sort
  7917. org-up-element
  7918. outline-demote
  7919. outline-next-visible-heading
  7920. outline-previous-visible-heading
  7921. outline-promote
  7922. outline-up-heading
  7923. show-children))
  7924. (let ((f (or (car-safe cell) cell))
  7925. (disable-when-heading-prefix (cdr-safe cell)))
  7926. (when (fboundp f)
  7927. (dolist (binding (nconc (where-is-internal f org-mode-map)
  7928. (where-is-internal f outline-mode-map)))
  7929. ;; TODO use local-function-key-map
  7930. (dolist (rep '(("<tab>" . "TAB")
  7931. ("<return>" . "RET")
  7932. ("<escape>" . "ESC")
  7933. ("<delete>" . "DEL")))
  7934. (setq binding (read-kbd-macro (replace-regexp-in-string
  7935. (regexp-quote (car rep))
  7936. (cdr rep)
  7937. (key-description binding)))))
  7938. (let ((key (lookup-key orgstruct-mode-map binding)))
  7939. (when (or (not key) (numberp key))
  7940. (condition-case nil
  7941. (org-defkey orgstruct-mode-map
  7942. binding
  7943. (orgstruct-make-binding f binding disable-when-heading-prefix))
  7944. (error nil))))))))
  7945. (run-hooks 'orgstruct-setup-hook))
  7946. (defun orgstruct-make-binding (fun key disable-when-heading-prefix)
  7947. "Create a function for binding in the structure minor mode.
  7948. FUN is the command to call inside a table. KEY is the key that
  7949. should be checked in for a command to execute outside of tables.
  7950. Non-nil DISABLE-WHEN-HEADING-PREFIX means to disable the command
  7951. if `orgstruct-heading-prefix-regexp' is non-nil."
  7952. (let ((name (concat "orgstruct-hijacker-" (symbol-name fun))))
  7953. (let ((nname name)
  7954. (i 0))
  7955. (while (fboundp (intern nname))
  7956. (setq nname (format "%s-%d" name (setq i (1+ i)))))
  7957. (setq name (intern nname)))
  7958. (eval
  7959. (let ((bindings '((org-heading-regexp
  7960. (concat "^"
  7961. orgstruct-heading-prefix-regexp
  7962. "\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ ]*$"))
  7963. (org-outline-regexp
  7964. (concat orgstruct-heading-prefix-regexp "\\*+ "))
  7965. (org-outline-regexp-bol
  7966. (concat "^" org-outline-regexp))
  7967. (outline-regexp org-outline-regexp)
  7968. (outline-heading-end-regexp "\n")
  7969. (outline-level 'org-outline-level)
  7970. (outline-heading-alist))))
  7971. `(defun ,name (arg)
  7972. ,(concat "In Structure, run `" (symbol-name fun) "'.\n"
  7973. "Outside of structure, run the binding of `"
  7974. (key-description key) "'."
  7975. (when disable-when-heading-prefix
  7976. (concat
  7977. "\nIf `orgstruct-heading-prefix-regexp' is non-nil, this command will always fall\n"
  7978. "back to the default binding due to limitations of Org's implementation of\n"
  7979. "`" (symbol-name fun) "'.")))
  7980. (interactive "p")
  7981. (let* ((disable
  7982. ,(when disable-when-heading-prefix
  7983. '(and orgstruct-heading-prefix-regexp
  7984. (not (string= orgstruct-heading-prefix-regexp "")))))
  7985. (fallback
  7986. (or disable
  7987. (not
  7988. (let* ,bindings
  7989. (org-context-p 'headline 'item
  7990. ,(when (memq fun '(org-insert-heading))
  7991. '(when orgstruct-is-++
  7992. 'item-body))))))))
  7993. (if fallback
  7994. (let* ((orgstruct-mode)
  7995. (binding (key-binding ,key)))
  7996. (if (keymapp binding)
  7997. (set-temporary-overlay-map binding)
  7998. (let ((func (or binding
  7999. (unless disable
  8000. 'orgstruct-error))))
  8001. (when func
  8002. (call-interactively func)))))
  8003. (org-run-like-in-org-mode
  8004. (lambda ()
  8005. (interactive)
  8006. (let* ,bindings
  8007. (call-interactively ',fun)))))))))
  8008. name))
  8009. (defun org-contextualize-keys (alist contexts)
  8010. "Return valid elements in ALIST depending on CONTEXTS.
  8011. `org-agenda-custom-commands' or `org-capture-templates' are the
  8012. values used for ALIST, and `org-agenda-custom-commands-contexts'
  8013. or `org-capture-templates-contexts' are the associated contexts
  8014. definitions."
  8015. (let ((contexts
  8016. ;; normalize contexts
  8017. (mapcar
  8018. (lambda(c) (cond ((listp (cadr c))
  8019. (list (car c) (car c) (cadr c)))
  8020. ((string= "" (cadr c))
  8021. (list (car c) (car c) (caddr c)))
  8022. (t c))) contexts))
  8023. (a alist) c r s)
  8024. ;; loop over all commands or templates
  8025. (while (setq c (pop a))
  8026. (let (vrules repl)
  8027. (cond
  8028. ((not (assoc (car c) contexts))
  8029. (push c r))
  8030. ((and (assoc (car c) contexts)
  8031. (setq vrules (org-contextualize-validate-key
  8032. (car c) contexts)))
  8033. (mapc (lambda (vr)
  8034. (when (not (equal (car vr) (cadr vr)))
  8035. (setq repl vr))) vrules)
  8036. (if (not repl) (push c r)
  8037. (push (cadr repl) s)
  8038. (push
  8039. (cons (car c)
  8040. (cdr (or (assoc (cadr repl) alist)
  8041. (error "Undefined key `%s' as contextual replacement for `%s'"
  8042. (cadr repl) (car c)))))
  8043. r))))))
  8044. ;; Return limited ALIST, possibly with keys modified, and deduplicated
  8045. (delq
  8046. nil
  8047. (delete-dups
  8048. (mapcar (lambda (x)
  8049. (let ((tpl (car x)))
  8050. (when (not (delq
  8051. nil
  8052. (mapcar (lambda(y)
  8053. (equal y tpl)) s))) x)))
  8054. (reverse r))))))
  8055. (defun org-contextualize-validate-key (key contexts)
  8056. "Check CONTEXTS for agenda or capture KEY."
  8057. (let (r rr res)
  8058. (while (setq r (pop contexts))
  8059. (mapc
  8060. (lambda (rr)
  8061. (when
  8062. (and (equal key (car r))
  8063. (if (functionp rr) (funcall rr)
  8064. (or (and (eq (car rr) 'in-file)
  8065. (buffer-file-name)
  8066. (string-match (cdr rr) (buffer-file-name)))
  8067. (and (eq (car rr) 'in-mode)
  8068. (string-match (cdr rr) (symbol-name major-mode)))
  8069. (and (eq (car rr) 'in-buffer)
  8070. (string-match (cdr rr) (buffer-name)))
  8071. (when (and (eq (car rr) 'not-in-file)
  8072. (buffer-file-name))
  8073. (not (string-match (cdr rr) (buffer-file-name))))
  8074. (when (eq (car rr) 'not-in-mode)
  8075. (not (string-match (cdr rr) (symbol-name major-mode))))
  8076. (when (eq (car rr) 'not-in-buffer)
  8077. (not (string-match (cdr rr) (buffer-name)))))))
  8078. (push r res)))
  8079. (car (last r))))
  8080. (delete-dups (delq nil res))))
  8081. (defun org-context-p (&rest contexts)
  8082. "Check if local context is any of CONTEXTS.
  8083. Possible values in the list of contexts are `table', `headline', and `item'."
  8084. (let ((pos (point)))
  8085. (goto-char (point-at-bol))
  8086. (prog1 (or (and (memq 'table contexts)
  8087. (looking-at "[ \t]*|"))
  8088. (and (memq 'headline contexts)
  8089. (looking-at org-outline-regexp))
  8090. (and (memq 'item contexts)
  8091. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  8092. (and (memq 'item-body contexts)
  8093. (org-in-item-p)))
  8094. (goto-char pos))))
  8095. (defun org-get-local-variables ()
  8096. "Return a list of all local variables in an Org mode buffer."
  8097. (let (varlist)
  8098. (with-current-buffer (get-buffer-create "*Org tmp*")
  8099. (erase-buffer)
  8100. (org-mode)
  8101. (setq varlist (buffer-local-variables)))
  8102. (kill-buffer "*Org tmp*")
  8103. (delq nil
  8104. (mapcar
  8105. (lambda (x)
  8106. (setq x
  8107. (if (symbolp x)
  8108. (list x)
  8109. (list (car x) (cdr x))))
  8110. (if (and (not (get (car x) 'org-state))
  8111. (string-match
  8112. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|indent-\\)"
  8113. (symbol-name (car x))))
  8114. x nil))
  8115. varlist))))
  8116. (defun org-clone-local-variables (from-buffer &optional regexp)
  8117. "Clone local variables from FROM-BUFFER.
  8118. Optional argument REGEXP selects variables to clone."
  8119. (mapc
  8120. (lambda (pair)
  8121. (and (symbolp (car pair))
  8122. (or (null regexp)
  8123. (string-match regexp (symbol-name (car pair))))
  8124. (set (make-local-variable (car pair))
  8125. (cdr pair))))
  8126. (buffer-local-variables from-buffer)))
  8127. ;;;###autoload
  8128. (defun org-run-like-in-org-mode (cmd)
  8129. "Run a command, pretending that the current buffer is in Org-mode.
  8130. This will temporarily bind local variables that are typically bound in
  8131. Org-mode to the values they have in Org-mode, and then interactively
  8132. call CMD."
  8133. (org-load-modules-maybe)
  8134. (unless org-local-vars
  8135. (setq org-local-vars (org-get-local-variables)))
  8136. (let (binds)
  8137. (dolist (var org-local-vars)
  8138. (when (or (not (boundp (car var)))
  8139. (eq (symbol-value (car var))
  8140. (default-value (car var))))
  8141. (push (list (car var) `(quote ,(cadr var))) binds)))
  8142. (eval `(let ,binds
  8143. (call-interactively (quote ,cmd))))))
  8144. ;;;; Archiving
  8145. (defun org-get-category (&optional pos force-refresh)
  8146. "Get the category applying to position POS."
  8147. (save-match-data
  8148. (if force-refresh (org-refresh-category-properties))
  8149. (let ((pos (or pos (point))))
  8150. (or (get-text-property pos 'org-category)
  8151. (progn (org-refresh-category-properties)
  8152. (get-text-property pos 'org-category))))))
  8153. (defun org-refresh-category-properties ()
  8154. "Refresh category text properties in the buffer."
  8155. (let ((case-fold-search t)
  8156. (inhibit-read-only t)
  8157. (def-cat (cond
  8158. ((null org-category)
  8159. (if buffer-file-name
  8160. (file-name-sans-extension
  8161. (file-name-nondirectory buffer-file-name))
  8162. "???"))
  8163. ((symbolp org-category) (symbol-name org-category))
  8164. (t org-category)))
  8165. beg end cat pos optionp)
  8166. (org-with-silent-modifications
  8167. (save-excursion
  8168. (save-restriction
  8169. (widen)
  8170. (goto-char (point-min))
  8171. (put-text-property (point) (point-max) 'org-category def-cat)
  8172. (while (re-search-forward
  8173. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  8174. (setq pos (match-end 0)
  8175. optionp (equal (char-after (match-beginning 0)) ?#)
  8176. cat (org-trim (match-string 2)))
  8177. (if optionp
  8178. (setq beg (point-at-bol) end (point-max))
  8179. (org-back-to-heading t)
  8180. (setq beg (point) end (org-end-of-subtree t t)))
  8181. (put-text-property beg end 'org-category cat)
  8182. (put-text-property beg end 'org-category-position beg)
  8183. (goto-char pos)))))))
  8184. (defun org-refresh-properties (dprop tprop)
  8185. "Refresh buffer text properties.
  8186. DPROP is the drawer property and TPROP is the corresponding text
  8187. property to set."
  8188. (let ((case-fold-search t)
  8189. (inhibit-read-only t) p)
  8190. (org-with-silent-modifications
  8191. (save-excursion
  8192. (save-restriction
  8193. (widen)
  8194. (goto-char (point-min))
  8195. (while (re-search-forward (concat "^[ \t]*:" dprop ": +\\(.*\\)[ \t]*$") nil t)
  8196. (setq p (org-match-string-no-properties 1))
  8197. (save-excursion
  8198. (org-back-to-heading t)
  8199. (put-text-property
  8200. (point-at-bol) (point-at-eol) tprop p))))))))
  8201. ;;;; Link Stuff
  8202. ;;; Link abbreviations
  8203. (defun org-link-expand-abbrev (link)
  8204. "Apply replacements as defined in `org-link-abbrev-alist'."
  8205. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  8206. (let* ((key (match-string 1 link))
  8207. (as (or (assoc key org-link-abbrev-alist-local)
  8208. (assoc key org-link-abbrev-alist)))
  8209. (tag (and (match-end 2) (match-string 3 link)))
  8210. rpl)
  8211. (if (not as)
  8212. link
  8213. (setq rpl (cdr as))
  8214. (cond
  8215. ((symbolp rpl) (funcall rpl tag))
  8216. ((string-match "%(\\([^)]+\\))" rpl)
  8217. (replace-match (funcall (intern-soft (match-string 1 rpl)) tag) t t rpl))
  8218. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  8219. ((string-match "%h" rpl)
  8220. (replace-match (url-hexify-string (or tag "")) t t rpl))
  8221. (t (concat rpl tag)))))
  8222. link))
  8223. ;;; Storing and inserting links
  8224. (defvar org-insert-link-history nil
  8225. "Minibuffer history for links inserted with `org-insert-link'.")
  8226. (defvar org-stored-links nil
  8227. "Contains the links stored with `org-store-link'.")
  8228. (defvar org-store-link-plist nil
  8229. "Plist with info about the most recently link created with `org-store-link'.")
  8230. (defvar org-link-protocols nil
  8231. "Link protocols added to Org-mode using `org-add-link-type'.")
  8232. (defvar org-store-link-functions nil
  8233. "List of functions that are called to create and store a link.
  8234. Each function will be called in turn until one returns a non-nil
  8235. value. Each function should check if it is responsible for creating
  8236. this link (for example by looking at the major mode).
  8237. If not, it must exit and return nil.
  8238. If yes, it should return a non-nil value after a calling
  8239. `org-store-link-props' with a list of properties and values.
  8240. Special properties are:
  8241. :type The link prefix, like \"http\". This must be given.
  8242. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  8243. This is obligatory as well.
  8244. :description Optional default description for the second pair
  8245. of brackets in an Org-mode link. The user can still change
  8246. this when inserting this link into an Org-mode buffer.
  8247. In addition to these, any additional properties can be specified
  8248. and then used in capture templates.")
  8249. (defun org-add-link-type (type &optional follow export)
  8250. "Add TYPE to the list of `org-link-types'.
  8251. Re-compute all regular expressions depending on `org-link-types'
  8252. FOLLOW and EXPORT are two functions.
  8253. FOLLOW should take the link path as the single argument and do whatever
  8254. is necessary to follow the link, for example find a file or display
  8255. a mail message.
  8256. EXPORT should format the link path for export to one of the export formats.
  8257. It should be a function accepting three arguments:
  8258. path the path of the link, the text after the prefix (like \"http:\")
  8259. desc the description of the link, if any, or a description added by
  8260. org-export-normalize-links if there is none
  8261. format the export format, a symbol like `html' or `latex' or `ascii'..
  8262. The function may use the FORMAT information to return different values
  8263. depending on the format. The return value will be put literally into
  8264. the exported file. If the return value is nil, this means Org should
  8265. do what it normally does with links which do not have EXPORT defined.
  8266. Org-mode has a built-in default for exporting links. If you are happy with
  8267. this default, there is no need to define an export function for the link
  8268. type. For a simple example of an export function, see `org-bbdb.el'."
  8269. (add-to-list 'org-link-types type t)
  8270. (org-make-link-regexps)
  8271. (if (assoc type org-link-protocols)
  8272. (setcdr (assoc type org-link-protocols) (list follow export))
  8273. (push (list type follow export) org-link-protocols)))
  8274. (defvar org-agenda-buffer-name) ; Defined in org-agenda.el
  8275. (defvar org-id-link-to-org-use-id) ; Defined in org-id.el
  8276. ;;;###autoload
  8277. (defun org-store-link (arg)
  8278. "\\<org-mode-map>Store an org-link to the current location.
  8279. This link is added to `org-stored-links' and can later be inserted
  8280. into an org-buffer with \\[org-insert-link].
  8281. For some link types, a prefix arg is interpreted.
  8282. For links to Usenet articles, arg negates `org-gnus-prefer-web-links'.
  8283. For file links, arg negates `org-context-in-file-links'.
  8284. A double prefix arg force skipping storing functions that are not
  8285. part of Org's core."
  8286. (interactive "P")
  8287. (org-load-modules-maybe)
  8288. (setq org-store-link-plist nil) ; reset
  8289. (org-with-limited-levels
  8290. (let (link cpltxt desc description search txt custom-id agenda-link sfuns sfunsn)
  8291. (cond
  8292. ((and (not (equal arg '(16)))
  8293. (setq sfuns
  8294. (delq
  8295. nil (mapcar (lambda (f) (let (fs) (if (funcall f) (push f fs))))
  8296. org-store-link-functions))
  8297. sfunsn (mapcar (lambda (fu) (symbol-name (car fu))) sfuns))
  8298. (or (and (cdr sfuns)
  8299. (funcall (intern
  8300. (completing-read "Which function for creating the link? "
  8301. sfunsn t (car sfunsn)))))
  8302. (funcall (caar sfuns)))
  8303. (setq link (plist-get org-store-link-plist :link)
  8304. desc (or (plist-get org-store-link-plist :description) link))))
  8305. ((org-src-edit-buffer-p)
  8306. (let (label gc)
  8307. (while (or (not label)
  8308. (save-excursion
  8309. (save-restriction
  8310. (widen)
  8311. (goto-char (point-min))
  8312. (re-search-forward
  8313. (regexp-quote (format org-coderef-label-format label))
  8314. nil t))))
  8315. (when label (message "Label exists already") (sit-for 2))
  8316. (setq label (read-string "Code line label: " label)))
  8317. (end-of-line 1)
  8318. (setq link (format org-coderef-label-format label))
  8319. (setq gc (- 79 (length link)))
  8320. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  8321. (insert link)
  8322. (setq link (concat "(" label ")") desc nil)))
  8323. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  8324. ;; We are in the agenda, link to referenced location
  8325. (let ((m (or (get-text-property (point) 'org-hd-marker)
  8326. (get-text-property (point) 'org-marker))))
  8327. (when m
  8328. (org-with-point-at m
  8329. (setq agenda-link
  8330. (if (org-called-interactively-p 'any)
  8331. (call-interactively 'org-store-link)
  8332. (org-store-link nil)))))))
  8333. ((eq major-mode 'calendar-mode)
  8334. (let ((cd (calendar-cursor-to-date)))
  8335. (setq link
  8336. (format-time-string
  8337. (car org-time-stamp-formats)
  8338. (apply 'encode-time
  8339. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  8340. nil nil nil))))
  8341. (org-store-link-props :type "calendar" :date cd)))
  8342. ((eq major-mode 'help-mode)
  8343. (setq link (concat "help:" (save-excursion
  8344. (goto-char (point-min))
  8345. (looking-at "^[^ ]+")
  8346. (match-string 0))))
  8347. (org-store-link-props :type "help"))
  8348. ((eq major-mode 'w3-mode)
  8349. (setq cpltxt (if (and (buffer-name)
  8350. (not (string-match "Untitled" (buffer-name))))
  8351. (buffer-name)
  8352. (url-view-url t))
  8353. link (url-view-url t))
  8354. (org-store-link-props :type "w3" :url (url-view-url t)))
  8355. ((setq search (run-hook-with-args-until-success
  8356. 'org-create-file-search-functions))
  8357. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  8358. "::" search))
  8359. (setq cpltxt (or description link)))
  8360. ((eq major-mode 'image-mode)
  8361. (setq cpltxt (concat "file:"
  8362. (abbreviate-file-name buffer-file-name))
  8363. link cpltxt)
  8364. (org-store-link-props :type "image" :file buffer-file-name))
  8365. ((eq major-mode 'dired-mode)
  8366. ;; link to the file in the current line
  8367. (let ((file (dired-get-filename nil t)))
  8368. (setq file (if file
  8369. (abbreviate-file-name
  8370. (expand-file-name (dired-get-filename nil t)))
  8371. ;; otherwise, no file so use current directory.
  8372. default-directory))
  8373. (setq cpltxt (concat "file:" file)
  8374. link cpltxt)))
  8375. ((and (buffer-file-name (buffer-base-buffer)) (derived-mode-p 'org-mode))
  8376. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  8377. (cond
  8378. ((org-in-regexp "<<\\(.*?\\)>>")
  8379. (setq cpltxt
  8380. (concat "file:"
  8381. (abbreviate-file-name
  8382. (buffer-file-name (buffer-base-buffer)))
  8383. "::" (match-string 1))
  8384. link cpltxt))
  8385. ((and (featurep 'org-id)
  8386. (or (eq org-id-link-to-org-use-id t)
  8387. (and (org-called-interactively-p 'any)
  8388. (or (eq org-id-link-to-org-use-id 'create-if-interactive)
  8389. (and (eq org-id-link-to-org-use-id
  8390. 'create-if-interactive-and-no-custom-id)
  8391. (not custom-id))))
  8392. (and org-id-link-to-org-use-id (org-entry-get nil "ID"))))
  8393. ;; We can make a link using the ID.
  8394. (setq link (condition-case nil
  8395. (prog1 (org-id-store-link)
  8396. (setq desc (plist-get org-store-link-plist :description)))
  8397. (error
  8398. ;; probably before first headline, link to file only
  8399. (concat "file:"
  8400. (abbreviate-file-name
  8401. (buffer-file-name (buffer-base-buffer))))))))
  8402. (t
  8403. ;; Just link to current headline
  8404. (setq cpltxt (concat "file:"
  8405. (abbreviate-file-name
  8406. (buffer-file-name (buffer-base-buffer)))))
  8407. ;; Add a context search string
  8408. (when (org-xor org-context-in-file-links arg)
  8409. (let* ((ee (org-element-at-point))
  8410. (et (org-element-type ee))
  8411. (ev (plist-get (cadr ee) :value)))
  8412. (setq txt (cond
  8413. ((org-at-heading-p) nil)
  8414. ((eq et 'keyword) ev)
  8415. ((org-region-active-p)
  8416. (buffer-substring (region-beginning) (region-end)))))
  8417. (when (or (null txt) (string-match "\\S-" txt))
  8418. (setq cpltxt
  8419. (concat cpltxt "::"
  8420. (condition-case nil
  8421. (org-make-org-heading-search-string txt)
  8422. (error "")))
  8423. desc (or (and (eq et 'keyword) ev)
  8424. (nth 4 (ignore-errors (org-heading-components)))
  8425. "NONE")))))
  8426. (if (string-match "::\\'" cpltxt)
  8427. (setq cpltxt (substring cpltxt 0 -2)))
  8428. (setq link cpltxt))))
  8429. ((buffer-file-name (buffer-base-buffer))
  8430. ;; Just link to this file here.
  8431. (setq cpltxt (concat "file:"
  8432. (abbreviate-file-name
  8433. (buffer-file-name (buffer-base-buffer)))))
  8434. ;; Add a context string
  8435. (when (org-xor org-context-in-file-links arg)
  8436. (setq txt (if (org-region-active-p)
  8437. (buffer-substring (region-beginning) (region-end))
  8438. (buffer-substring (point-at-bol) (point-at-eol))))
  8439. ;; Only use search option if there is some text.
  8440. (when (string-match "\\S-" txt)
  8441. (setq cpltxt
  8442. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  8443. desc "NONE")))
  8444. (setq link cpltxt))
  8445. ((org-called-interactively-p 'interactive)
  8446. (user-error "No method for storing a link from this buffer"))
  8447. (t (setq link nil)))
  8448. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  8449. (setq link (or link cpltxt)
  8450. desc (or desc cpltxt))
  8451. (cond ((equal desc "NONE") (setq desc nil))
  8452. ((string-match org-bracket-link-regexp desc)
  8453. (setq desc (replace-regexp-in-string
  8454. org-bracket-link-regexp
  8455. (concat "\\3" (if (equal (length (match-string 0 desc))
  8456. (length desc)) "*" "")) desc))))
  8457. (if (and (or (org-called-interactively-p 'any) executing-kbd-macro) link)
  8458. (progn
  8459. (setq org-stored-links
  8460. (cons (list link desc) org-stored-links))
  8461. (message "Stored: %s" (or desc link))
  8462. (when custom-id
  8463. (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
  8464. "::#" custom-id))
  8465. (setq org-stored-links
  8466. (cons (list link desc) org-stored-links))))
  8467. (or agenda-link (and link (org-make-link-string link desc)))))))
  8468. (defun org-store-link-props (&rest plist)
  8469. "Store link properties, extract names and addresses."
  8470. (let (x adr)
  8471. (when (setq x (plist-get plist :from))
  8472. (setq adr (mail-extract-address-components x))
  8473. (setq plist (plist-put plist :fromname (car adr)))
  8474. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  8475. (when (setq x (plist-get plist :to))
  8476. (setq adr (mail-extract-address-components x))
  8477. (setq plist (plist-put plist :toname (car adr)))
  8478. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  8479. (let ((from (plist-get plist :from))
  8480. (to (plist-get plist :to)))
  8481. (when (and from to org-from-is-user-regexp)
  8482. (setq plist
  8483. (plist-put plist :fromto
  8484. (if (string-match org-from-is-user-regexp from)
  8485. (concat "to %t")
  8486. (concat "from %f"))))))
  8487. (setq org-store-link-plist plist))
  8488. (defun org-add-link-props (&rest plist)
  8489. "Add these properties to the link property list."
  8490. (let (key value)
  8491. (while plist
  8492. (setq key (pop plist) value (pop plist))
  8493. (setq org-store-link-plist
  8494. (plist-put org-store-link-plist key value)))))
  8495. (defun org-email-link-description (&optional fmt)
  8496. "Return the description part of an email link.
  8497. This takes information from `org-store-link-plist' and formats it
  8498. according to FMT (default from `org-email-link-description-format')."
  8499. (setq fmt (or fmt org-email-link-description-format))
  8500. (let* ((p org-store-link-plist)
  8501. (to (plist-get p :toaddress))
  8502. (from (plist-get p :fromaddress))
  8503. (table
  8504. (list
  8505. (cons "%c" (plist-get p :fromto))
  8506. (cons "%F" (plist-get p :from))
  8507. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  8508. (cons "%T" (plist-get p :to))
  8509. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  8510. (cons "%s" (plist-get p :subject))
  8511. (cons "%d" (plist-get p :date))
  8512. (cons "%m" (plist-get p :message-id)))))
  8513. (when (string-match "%c" fmt)
  8514. ;; Check if the user wrote this message
  8515. (if (and org-from-is-user-regexp from to
  8516. (save-match-data (string-match org-from-is-user-regexp from)))
  8517. (setq fmt (replace-match "to %t" t t fmt))
  8518. (setq fmt (replace-match "from %f" t t fmt))))
  8519. (org-replace-escapes fmt table)))
  8520. (defun org-make-org-heading-search-string (&optional string)
  8521. "Make search string for the current headline or STRING."
  8522. (let ((s (or string
  8523. (and (derived-mode-p 'org-mode)
  8524. (save-excursion
  8525. (org-back-to-heading t)
  8526. (org-element-property :raw-value (org-element-at-point))))))
  8527. (lines org-context-in-file-links))
  8528. (or string (setq s (concat "*" s))) ; Add * for headlines
  8529. (setq s (replace-regexp-in-string "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" "" s))
  8530. (when (and string (integerp lines) (> lines 0))
  8531. (let ((slines (org-split-string s "\n")))
  8532. (when (< lines (length slines))
  8533. (setq s (mapconcat
  8534. 'identity
  8535. (reverse (nthcdr (- (length slines) lines)
  8536. (reverse slines))) "\n")))))
  8537. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  8538. (defun org-make-link-string (link &optional description)
  8539. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  8540. (unless (string-match "\\S-" link)
  8541. (error "Empty link"))
  8542. (when (and description
  8543. (stringp description)
  8544. (not (string-match "\\S-" description)))
  8545. (setq description nil))
  8546. (when (stringp description)
  8547. ;; Remove brackets from the description, they are fatal.
  8548. (while (string-match "\\[" description)
  8549. (setq description (replace-match "{" t t description)))
  8550. (while (string-match "\\]" description)
  8551. (setq description (replace-match "}" t t description))))
  8552. (when (equal link description)
  8553. ;; No description needed, it is identical
  8554. (setq description nil))
  8555. (when (and (not description)
  8556. (not (string-match (org-image-file-name-regexp) link))
  8557. (not (equal link (org-link-escape link))))
  8558. (setq description (org-extract-attributes link)))
  8559. (setq link
  8560. (cond ((string-match (org-image-file-name-regexp) link) link)
  8561. ((string-match org-link-types-re link)
  8562. (concat (match-string 1 link)
  8563. (org-link-escape (substring link (match-end 1)))))
  8564. (t (org-link-escape link))))
  8565. (concat "[[" link "]"
  8566. (if description (concat "[" description "]") "")
  8567. "]"))
  8568. (defconst org-link-escape-chars
  8569. '(?\ ?\[ ?\] ?\; ?\= ?\+)
  8570. "List of characters that should be escaped in link.
  8571. This is the list that is used for internal purposes.")
  8572. (defconst org-link-escape-chars-browser
  8573. '(?\ )
  8574. "List of escapes for characters that are problematic in links.
  8575. This is the list that is used before handing over to the browser.")
  8576. (defun org-link-escape (text &optional table merge)
  8577. "Return percent escaped representation of TEXT.
  8578. TEXT is a string with the text to escape.
  8579. Optional argument TABLE is a list with characters that should be
  8580. escaped. When nil, `org-link-escape-chars' is used.
  8581. If optional argument MERGE is set, merge TABLE into
  8582. `org-link-escape-chars'."
  8583. (cond
  8584. ((and table merge)
  8585. (mapc (lambda (defchr)
  8586. (unless (member defchr table)
  8587. (setq table (cons defchr table)))) org-link-escape-chars))
  8588. ((null table)
  8589. (setq table org-link-escape-chars)))
  8590. (mapconcat
  8591. (lambda (char)
  8592. (if (or (member char table)
  8593. (and (or (< char 32) (= char 37) (> char 126))
  8594. org-url-hexify-p))
  8595. (mapconcat (lambda (sequence-element)
  8596. (format "%%%.2X" sequence-element))
  8597. (or (encode-coding-char char 'utf-8)
  8598. (error "Unable to percent escape character: %s"
  8599. (char-to-string char))) "")
  8600. (char-to-string char))) text ""))
  8601. (defun org-link-unescape (str)
  8602. "Unhex hexified Unicode strings as returned from the JavaScript function
  8603. encodeURIComponent. E.g. `%C3%B6' is the german o-Umlaut."
  8604. (unless (and (null str) (string= "" str))
  8605. (let ((pos 0) (case-fold-search t) unhexed)
  8606. (while (setq pos (string-match "\\(%[0-9a-f][0-9a-f]\\)+" str pos))
  8607. (setq unhexed (org-link-unescape-compound (match-string 0 str)))
  8608. (setq str (replace-match unhexed t t str))
  8609. (setq pos (+ pos (length unhexed))))))
  8610. str)
  8611. (defun org-link-unescape-compound (hex)
  8612. "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German o-Umlaut.
  8613. Note: this function also decodes single byte encodings like
  8614. `%E1' (a-acute) if not followed by another `%[A-F0-9]{2}' group."
  8615. (save-match-data
  8616. (let* ((bytes (cdr (split-string hex "%")))
  8617. (ret "")
  8618. (eat 0)
  8619. (sum 0))
  8620. (while bytes
  8621. (let* ((val (string-to-number (pop bytes) 16))
  8622. (shift-xor
  8623. (if (= 0 eat)
  8624. (cond
  8625. ((>= val 252) (cons 6 252))
  8626. ((>= val 248) (cons 5 248))
  8627. ((>= val 240) (cons 4 240))
  8628. ((>= val 224) (cons 3 224))
  8629. ((>= val 192) (cons 2 192))
  8630. (t (cons 0 0)))
  8631. (cons 6 128))))
  8632. (if (>= val 192) (setq eat (car shift-xor)))
  8633. (setq val (logxor val (cdr shift-xor)))
  8634. (setq sum (+ (lsh sum (car shift-xor)) val))
  8635. (if (> eat 0) (setq eat (- eat 1)))
  8636. (cond
  8637. ((= 0 eat) ;multi byte
  8638. (setq ret (concat ret (org-char-to-string sum)))
  8639. (setq sum 0))
  8640. ((not bytes) ; single byte(s)
  8641. (setq ret (org-link-unescape-single-byte-sequence hex))))
  8642. )) ;; end (while bytes
  8643. ret )))
  8644. (defun org-link-unescape-single-byte-sequence (hex)
  8645. "Unhexify hex-encoded single byte character sequences."
  8646. (mapconcat (lambda (byte)
  8647. (char-to-string (string-to-number byte 16)))
  8648. (cdr (split-string hex "%")) ""))
  8649. (defun org-xor (a b)
  8650. "Exclusive or."
  8651. (if a (not b) b))
  8652. (defun org-fixup-message-id-for-http (s)
  8653. "Replace special characters in a message id, so it can be used in an http query."
  8654. (when (string-match "%" s)
  8655. (setq s (mapconcat (lambda (c)
  8656. (if (eq c ?%)
  8657. "%25"
  8658. (char-to-string c)))
  8659. s "")))
  8660. (while (string-match "<" s)
  8661. (setq s (replace-match "%3C" t t s)))
  8662. (while (string-match ">" s)
  8663. (setq s (replace-match "%3E" t t s)))
  8664. (while (string-match "@" s)
  8665. (setq s (replace-match "%40" t t s)))
  8666. s)
  8667. (defun org-link-prettify (link)
  8668. "Return a human-readable representation of LINK.
  8669. The car of LINK must be a raw link the cdr of LINK must be either
  8670. a link description or nil."
  8671. (let ((desc (or (cadr link) "<no description>")))
  8672. (concat (format "%-45s" (substring desc 0 (min (length desc) 40)))
  8673. "<" (car link) ">")))
  8674. ;;;###autoload
  8675. (defun org-insert-link-global ()
  8676. "Insert a link like Org-mode does.
  8677. This command can be called in any mode to insert a link in Org-mode syntax."
  8678. (interactive)
  8679. (org-load-modules-maybe)
  8680. (org-run-like-in-org-mode 'org-insert-link))
  8681. (defun org-insert-all-links (&optional keep)
  8682. "Insert all links in `org-stored-links'."
  8683. (interactive "P")
  8684. (let ((links (copy-sequence org-stored-links)) l)
  8685. (while (setq l (if keep (pop links) (pop org-stored-links)))
  8686. (insert "- ")
  8687. (org-insert-link nil (car l) (cadr l))
  8688. (insert "\n"))))
  8689. (defun org-link-fontify-links-to-this-file ()
  8690. "Fontify links to the current file in `org-stored-links'."
  8691. (let ((f (buffer-file-name)) a b)
  8692. (setq a (mapcar (lambda(l)
  8693. (let ((ll (car l)))
  8694. (when (and (string-match "^file:\\(.+\\)::" ll)
  8695. (equal f (expand-file-name (match-string 1 ll))))
  8696. ll)))
  8697. org-stored-links))
  8698. (when (featurep 'org-id)
  8699. (setq b (mapcar (lambda(l)
  8700. (let ((ll (car l)))
  8701. (when (and (string-match "^id:\\(.+\\)$" ll)
  8702. (equal f (expand-file-name
  8703. (or (org-id-find-id-file
  8704. (match-string 1 ll)) ""))))
  8705. ll)))
  8706. org-stored-links)))
  8707. (mapcar (lambda(l)
  8708. (put-text-property 0 (length l) 'face 'font-lock-comment-face l))
  8709. (delq nil (append a b)))))
  8710. (defvar org-link-links-in-this-file nil)
  8711. (defun org-insert-link (&optional complete-file link-location default-description)
  8712. "Insert a link. At the prompt, enter the link.
  8713. Completion can be used to insert any of the link protocol prefixes like
  8714. http or ftp in use.
  8715. The history can be used to select a link previously stored with
  8716. `org-store-link'. When the empty string is entered (i.e. if you just
  8717. press RET at the prompt), the link defaults to the most recently
  8718. stored link. As SPC triggers completion in the minibuffer, you need to
  8719. use M-SPC or C-q SPC to force the insertion of a space character.
  8720. You will also be prompted for a description, and if one is given, it will
  8721. be displayed in the buffer instead of the link.
  8722. If there is already a link at point, this command will allow you to edit link
  8723. and description parts.
  8724. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  8725. be selected using completion. The path to the file will be relative to the
  8726. current directory if the file is in the current directory or a subdirectory.
  8727. Otherwise, the link will be the absolute path as completed in the minibuffer
  8728. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  8729. option `org-link-file-path-type'.
  8730. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  8731. the current directory or below.
  8732. With three \\[universal-argument] prefixes, negate the meaning of
  8733. `org-keep-stored-link-after-insertion'.
  8734. If `org-make-link-description-function' is non-nil, this function will be
  8735. called with the link target, and the result will be the default
  8736. link description.
  8737. If the LINK-LOCATION parameter is non-nil, this value will be
  8738. used as the link location instead of reading one interactively.
  8739. If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
  8740. be used as the default description."
  8741. (interactive "P")
  8742. (let* ((wcf (current-window-configuration))
  8743. (origbuf (current-buffer))
  8744. (region (if (org-region-active-p)
  8745. (buffer-substring (region-beginning) (region-end))))
  8746. (remove (and region (list (region-beginning) (region-end))))
  8747. (desc region)
  8748. tmphist ; byte-compile incorrectly complains about this
  8749. (link link-location)
  8750. (abbrevs org-link-abbrev-alist-local)
  8751. entry file all-prefixes auto-desc)
  8752. (cond
  8753. (link-location) ; specified by arg, just use it.
  8754. ((org-in-regexp org-bracket-link-regexp 1)
  8755. ;; We do have a link at point, and we are going to edit it.
  8756. (setq remove (list (match-beginning 0) (match-end 0)))
  8757. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  8758. (setq link (read-string "Link: "
  8759. (org-link-unescape
  8760. (org-match-string-no-properties 1)))))
  8761. ((or (org-in-regexp org-angle-link-re)
  8762. (org-in-regexp org-plain-link-re))
  8763. ;; Convert to bracket link
  8764. (setq remove (list (match-beginning 0) (match-end 0))
  8765. link (read-string "Link: "
  8766. (org-remove-angle-brackets (match-string 0)))))
  8767. ((member complete-file '((4) (16)))
  8768. ;; Completing read for file names.
  8769. (setq link (org-file-complete-link complete-file)))
  8770. (t
  8771. ;; Read link, with completion for stored links.
  8772. (org-link-fontify-links-to-this-file)
  8773. (org-switch-to-buffer-other-window "*Org Links*")
  8774. (with-current-buffer "*Org Links*"
  8775. (erase-buffer)
  8776. (insert "Insert a link.
  8777. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  8778. (when org-stored-links
  8779. (insert "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  8780. (insert (mapconcat 'org-link-prettify
  8781. (reverse org-stored-links) "\n")))
  8782. (goto-char (point-min)))
  8783. (let ((cw (selected-window)))
  8784. (select-window (get-buffer-window "*Org Links*" 'visible))
  8785. (with-current-buffer "*Org Links*" (setq truncate-lines t))
  8786. (unless (pos-visible-in-window-p (point-max))
  8787. (org-fit-window-to-buffer))
  8788. (and (window-live-p cw) (select-window cw)))
  8789. ;; Fake a link history, containing the stored links.
  8790. (setq tmphist (append (mapcar 'car org-stored-links)
  8791. org-insert-link-history))
  8792. (setq all-prefixes (append (mapcar 'car abbrevs)
  8793. (mapcar 'car org-link-abbrev-alist)
  8794. org-link-types))
  8795. (unwind-protect
  8796. (progn
  8797. (setq link
  8798. (org-completing-read
  8799. "Link: "
  8800. (append
  8801. (mapcar (lambda (x) (concat x ":"))
  8802. all-prefixes)
  8803. (mapcar 'car org-stored-links))
  8804. nil nil nil
  8805. 'tmphist
  8806. (caar org-stored-links)))
  8807. (if (not (string-match "\\S-" link))
  8808. (error "No link selected"))
  8809. (mapc (lambda(l)
  8810. (when (equal link (cadr l)) (setq link (car l) auto-desc t)))
  8811. org-stored-links)
  8812. (if (or (member link all-prefixes)
  8813. (and (equal ":" (substring link -1))
  8814. (member (substring link 0 -1) all-prefixes)
  8815. (setq link (substring link 0 -1))))
  8816. (setq link (with-current-buffer origbuf
  8817. (org-link-try-special-completion link)))))
  8818. (set-window-configuration wcf)
  8819. (kill-buffer "*Org Links*"))
  8820. (setq entry (assoc link org-stored-links))
  8821. (or entry (push link org-insert-link-history))
  8822. (setq desc (or desc (nth 1 entry)))))
  8823. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  8824. (not org-keep-stored-link-after-insertion))
  8825. (setq org-stored-links (delq (assoc link org-stored-links)
  8826. org-stored-links)))
  8827. (if (string-match org-plain-link-re link)
  8828. ;; URL-like link, normalize the use of angular brackets.
  8829. (setq link (org-remove-angle-brackets link)))
  8830. ;; Check if we are linking to the current file with a search
  8831. ;; option If yes, simplify the link by using only the search
  8832. ;; option.
  8833. (when (and buffer-file-name
  8834. (string-match "^file:\\(.+?\\)::\\(.+\\)" link))
  8835. (let* ((path (match-string 1 link))
  8836. (case-fold-search nil)
  8837. (search (match-string 2 link)))
  8838. (save-match-data
  8839. (if (equal (file-truename buffer-file-name) (file-truename path))
  8840. ;; We are linking to this same file, with a search option
  8841. (setq link search)))))
  8842. ;; Check if we can/should use a relative path. If yes, simplify the link
  8843. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  8844. (let* ((type (match-string 1 link))
  8845. (path (match-string 2 link))
  8846. (origpath path)
  8847. (case-fold-search nil))
  8848. (cond
  8849. ((or (eq org-link-file-path-type 'absolute)
  8850. (equal complete-file '(16)))
  8851. (setq path (abbreviate-file-name (expand-file-name path))))
  8852. ((eq org-link-file-path-type 'noabbrev)
  8853. (setq path (expand-file-name path)))
  8854. ((eq org-link-file-path-type 'relative)
  8855. (setq path (file-relative-name path)))
  8856. (t
  8857. (save-match-data
  8858. (if (string-match (concat "^" (regexp-quote
  8859. (expand-file-name
  8860. (file-name-as-directory
  8861. default-directory))))
  8862. (expand-file-name path))
  8863. ;; We are linking a file with relative path name.
  8864. (setq path (substring (expand-file-name path)
  8865. (match-end 0)))
  8866. (setq path (abbreviate-file-name (expand-file-name path)))))))
  8867. (setq link (concat type path))
  8868. (if (equal desc origpath)
  8869. (setq desc path))))
  8870. (if org-make-link-description-function
  8871. (setq desc
  8872. (or (condition-case nil
  8873. (funcall org-make-link-description-function link desc)
  8874. (error (progn (message "Can't get link description from `%s'"
  8875. (symbol-name org-make-link-description-function))
  8876. (sit-for 2) nil)))
  8877. (read-string "Description: " default-description)))
  8878. (if default-description (setq desc default-description)
  8879. (setq desc (or (and auto-desc desc)
  8880. (read-string "Description: " desc)))))
  8881. (unless (string-match "\\S-" desc) (setq desc nil))
  8882. (if remove (apply 'delete-region remove))
  8883. (insert (org-make-link-string link desc))))
  8884. (defun org-link-try-special-completion (type)
  8885. "If there is completion support for link type TYPE, offer it."
  8886. (let ((fun (intern (concat "org-" type "-complete-link"))))
  8887. (if (functionp fun)
  8888. (funcall fun)
  8889. (read-string "Link (no completion support): " (concat type ":")))))
  8890. (defun org-file-complete-link (&optional arg)
  8891. "Create a file link using completion."
  8892. (let (file link)
  8893. (setq file (org-iread-file-name "File: "))
  8894. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  8895. (pwd1 (file-name-as-directory (abbreviate-file-name
  8896. (expand-file-name ".")))))
  8897. (cond
  8898. ((equal arg '(16))
  8899. (setq link (concat
  8900. "file:"
  8901. (abbreviate-file-name (expand-file-name file)))))
  8902. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  8903. (setq link (concat "file:" (match-string 1 file))))
  8904. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  8905. (expand-file-name file))
  8906. (setq link (concat
  8907. "file:" (match-string 1 (expand-file-name file)))))
  8908. (t (setq link (concat "file:" file)))))
  8909. link))
  8910. (defun org-iread-file-name (&rest args)
  8911. "Read-file-name using `ido-mode' speedup if available.
  8912. ARGS are arguments that may be passed to `ido-read-file-name' or `read-file-name'.
  8913. See `read-file-name' for a description of parameters."
  8914. (org-without-partial-completion
  8915. (if (and org-completion-use-ido
  8916. (fboundp 'ido-read-file-name)
  8917. (boundp 'ido-mode) ido-mode
  8918. (listp (second args)))
  8919. (let ((ido-enter-matching-directory nil))
  8920. (apply 'ido-read-file-name args))
  8921. (apply 'read-file-name args))))
  8922. (defun org-completing-read (&rest args)
  8923. "Completing-read with SPACE being a normal character."
  8924. (let ((enable-recursive-minibuffers t)
  8925. (minibuffer-local-completion-map
  8926. (copy-keymap minibuffer-local-completion-map)))
  8927. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  8928. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  8929. (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
  8930. (apply 'org-icompleting-read args)))
  8931. (defun org-completing-read-no-i (&rest args)
  8932. (let (org-completion-use-ido org-completion-use-iswitchb)
  8933. (apply 'org-completing-read args)))
  8934. (defun org-iswitchb-completing-read (prompt choices &rest args)
  8935. "Use iswitch as a completing-read replacement to choose from choices.
  8936. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  8937. from."
  8938. (let* ((iswitchb-use-virtual-buffers nil)
  8939. (iswitchb-make-buflist-hook
  8940. (lambda ()
  8941. (setq iswitchb-temp-buflist choices))))
  8942. (iswitchb-read-buffer prompt)))
  8943. (defun org-icompleting-read (&rest args)
  8944. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  8945. (org-without-partial-completion
  8946. (if (and org-completion-use-ido
  8947. (fboundp 'ido-completing-read)
  8948. (boundp 'ido-mode) ido-mode
  8949. (listp (second args)))
  8950. (let ((ido-enter-matching-directory nil))
  8951. (apply 'ido-completing-read (concat (car args))
  8952. (if (consp (car (nth 1 args)))
  8953. (mapcar 'car (nth 1 args))
  8954. (nth 1 args))
  8955. (cddr args)))
  8956. (if (and org-completion-use-iswitchb
  8957. (boundp 'iswitchb-mode) iswitchb-mode
  8958. (listp (second args)))
  8959. (apply 'org-iswitchb-completing-read (concat (car args))
  8960. (if (consp (car (nth 1 args)))
  8961. (mapcar 'car (nth 1 args))
  8962. (nth 1 args))
  8963. (cddr args))
  8964. (apply 'completing-read args)))))
  8965. (defun org-extract-attributes (s)
  8966. "Extract the attributes cookie from a string and set as text property."
  8967. (let (a attr (start 0) key value)
  8968. (save-match-data
  8969. (when (string-match "{{\\([^}]+\\)}}$" s)
  8970. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  8971. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  8972. (setq key (match-string 1 a) value (match-string 2 a)
  8973. start (match-end 0)
  8974. attr (plist-put attr (intern key) value))))
  8975. (org-add-props s nil 'org-attr attr))
  8976. s))
  8977. (defun org-extract-attributes-from-string (tag)
  8978. (let (key value attr)
  8979. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  8980. (setq key (match-string 1 tag) value (match-string 2 tag)
  8981. tag (replace-match "" t t tag)
  8982. attr (plist-put attr (intern key) value)))
  8983. (cons tag attr)))
  8984. (defun org-attributes-to-string (plist)
  8985. "Format a property list into an HTML attribute list."
  8986. (let ((s "") key value)
  8987. (while plist
  8988. (setq key (pop plist) value (pop plist))
  8989. (and value
  8990. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  8991. s))
  8992. ;;; Opening/following a link
  8993. (defvar org-link-search-failed nil)
  8994. (defvar org-open-link-functions nil
  8995. "Hook for functions finding a plain text link.
  8996. These functions must take a single argument, the link content.
  8997. They will be called for links that look like [[link text][description]]
  8998. when LINK TEXT does not have a protocol like \"http:\" and does not look
  8999. like a filename (e.g. \"./blue.png\").
  9000. These functions will be called *before* Org attempts to resolve the
  9001. link by doing text searches in the current buffer - so if you want a
  9002. link \"[[target]]\" to still find \"<<target>>\", your function should
  9003. handle this as a special case.
  9004. When the function does handle the link, it must return a non-nil value.
  9005. If it decides that it is not responsible for this link, it must return
  9006. nil to indicate that that Org-mode can continue with other options
  9007. like exact and fuzzy text search.")
  9008. (defun org-next-link (&optional search-backward)
  9009. "Move forward to the next link.
  9010. If the link is in hidden text, expose it."
  9011. (interactive "P")
  9012. (when (and org-link-search-failed (eq this-command last-command))
  9013. (goto-char (point-min))
  9014. (message "Link search wrapped back to beginning of buffer"))
  9015. (setq org-link-search-failed nil)
  9016. (let* ((pos (point))
  9017. (ct (org-context))
  9018. (a (assoc :link ct))
  9019. (srch-fun (if search-backward 're-search-backward 're-search-forward)))
  9020. (cond (a (goto-char (nth (if search-backward 1 2) a)))
  9021. ((looking-at org-any-link-re)
  9022. ;; Don't stay stuck at link without an org-link face
  9023. (forward-char (if search-backward -1 1))))
  9024. (if (funcall srch-fun org-any-link-re nil t)
  9025. (progn
  9026. (goto-char (match-beginning 0))
  9027. (if (outline-invisible-p) (org-show-context)))
  9028. (goto-char pos)
  9029. (setq org-link-search-failed t)
  9030. (message "No further link found"))))
  9031. (defun org-previous-link ()
  9032. "Move backward to the previous link.
  9033. If the link is in hidden text, expose it."
  9034. (interactive)
  9035. (funcall 'org-next-link t))
  9036. (defun org-translate-link (s)
  9037. "Translate a link string if a translation function has been defined."
  9038. (if (and org-link-translation-function
  9039. (fboundp org-link-translation-function)
  9040. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  9041. (progn
  9042. (setq s (funcall org-link-translation-function
  9043. (match-string 1 s) (match-string 2 s)))
  9044. (concat (car s) ":" (cdr s)))
  9045. s))
  9046. (defun org-translate-link-from-planner (type path)
  9047. "Translate a link from Emacs Planner syntax so that Org can follow it.
  9048. This is still an experimental function, your mileage may vary."
  9049. (cond
  9050. ((member type '("http" "https" "news" "ftp"))
  9051. ;; standard Internet links are the same.
  9052. nil)
  9053. ((and (equal type "irc") (string-match "^//" path))
  9054. ;; Planner has two / at the beginning of an irc link, we have 1.
  9055. ;; We should have zero, actually....
  9056. (setq path (substring path 1)))
  9057. ((and (equal type "lisp") (string-match "^/" path))
  9058. ;; Planner has a slash, we do not.
  9059. (setq type "elisp" path (substring path 1)))
  9060. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  9061. ;; A typical message link. Planner has the id after the final slash,
  9062. ;; we separate it with a hash mark
  9063. (setq path (concat (match-string 1 path) "#"
  9064. (org-remove-angle-brackets (match-string 2 path)))))
  9065. )
  9066. (cons type path))
  9067. (defun org-find-file-at-mouse (ev)
  9068. "Open file link or URL at mouse."
  9069. (interactive "e")
  9070. (mouse-set-point ev)
  9071. (org-open-at-point 'in-emacs))
  9072. (defun org-open-at-mouse (ev)
  9073. "Open file link or URL at mouse.
  9074. See the docstring of `org-open-file' for details."
  9075. (interactive "e")
  9076. (mouse-set-point ev)
  9077. (if (eq major-mode 'org-agenda-mode)
  9078. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  9079. (org-open-at-point))
  9080. (defvar org-window-config-before-follow-link nil
  9081. "The window configuration before following a link.
  9082. This is saved in case the need arises to restore it.")
  9083. (defvar org-open-link-marker (make-marker)
  9084. "Marker pointing to the location where `org-open-at-point; was called.")
  9085. ;;;###autoload
  9086. (defun org-open-at-point-global ()
  9087. "Follow a link like Org-mode does.
  9088. This command can be called in any mode to follow a link that has
  9089. Org-mode syntax."
  9090. (interactive)
  9091. (org-run-like-in-org-mode 'org-open-at-point))
  9092. ;;;###autoload
  9093. (defun org-open-link-from-string (s &optional arg reference-buffer)
  9094. "Open a link in the string S, as if it was in Org-mode."
  9095. (interactive "sLink: \nP")
  9096. (let ((reference-buffer (or reference-buffer (current-buffer))))
  9097. (with-temp-buffer
  9098. (let ((org-inhibit-startup (not reference-buffer)))
  9099. (org-mode)
  9100. (insert s)
  9101. (goto-char (point-min))
  9102. (when reference-buffer
  9103. (setq org-link-abbrev-alist-local
  9104. (with-current-buffer reference-buffer
  9105. org-link-abbrev-alist-local)))
  9106. (org-open-at-point arg reference-buffer)))))
  9107. (defvar org-open-at-point-functions nil
  9108. "Hook that is run when following a link at point.
  9109. Functions in this hook must return t if they identify and follow
  9110. a link at point. If they don't find anything interesting at point,
  9111. they must return nil.")
  9112. (defvar clean-buffer-list-kill-buffer-names) ; Defined in midnight.el
  9113. (defun org-open-at-point (&optional arg reference-buffer)
  9114. "Open link at or after point.
  9115. If there is no link at point, this function will search forward up to
  9116. the end of the current line.
  9117. Normally, files will be opened by an appropriate application. If the
  9118. optional prefix argument ARG is non-nil, Emacs will visit the file.
  9119. With a double prefix argument, try to open outside of Emacs, in the
  9120. application the system uses for this file type."
  9121. (interactive "P")
  9122. ;; if in a code block, then open the block's results
  9123. (unless (call-interactively #'org-babel-open-src-block-result)
  9124. (org-load-modules-maybe)
  9125. (move-marker org-open-link-marker (point))
  9126. (setq org-window-config-before-follow-link (current-window-configuration))
  9127. (org-remove-occur-highlights nil nil t)
  9128. (cond
  9129. ((and (org-at-heading-p)
  9130. (not (org-at-timestamp-p t))
  9131. (not (org-in-regexp
  9132. (concat org-plain-link-re "\\|"
  9133. org-bracket-link-regexp "\\|"
  9134. org-angle-link-re "\\|"
  9135. "[ \t]:[^ \t\n]+:[ \t]*$")))
  9136. (not (get-text-property (point) 'org-linked-text)))
  9137. (or (let* ((lkall (org-offer-links-in-entry (current-buffer) (point) arg))
  9138. (lk0 (car lkall))
  9139. (lk (if (stringp lk0) (list lk0) lk0))
  9140. (lkend (cdr lkall)))
  9141. (mapcar (lambda(l)
  9142. (search-forward l nil lkend)
  9143. (goto-char (match-beginning 0))
  9144. (org-open-at-point))
  9145. lk))
  9146. (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
  9147. ((run-hook-with-args-until-success 'org-open-at-point-functions))
  9148. ((and (org-at-timestamp-p t)
  9149. (not (org-in-regexp org-bracket-link-regexp)))
  9150. (org-follow-timestamp-link))
  9151. ((and (or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  9152. (not (org-in-regexp org-any-link-re)))
  9153. (org-footnote-action))
  9154. (t
  9155. (let (type path link line search (pos (point)))
  9156. (catch 'match
  9157. (save-excursion
  9158. (skip-chars-forward "^]\n\r")
  9159. (when (org-in-regexp org-bracket-link-regexp 1)
  9160. (setq link (org-extract-attributes
  9161. (org-link-unescape (org-match-string-no-properties 1))))
  9162. (while (string-match " *\n *" link)
  9163. (setq link (replace-match " " t t link)))
  9164. (setq link (org-link-expand-abbrev link))
  9165. (cond
  9166. ((or (file-name-absolute-p link)
  9167. (string-match "^\\.\\.?/" link))
  9168. (setq type "file" path link))
  9169. ((string-match org-link-re-with-space3 link)
  9170. (setq type (match-string 1 link) path (match-string 2 link)))
  9171. ((string-match "^help:+\\(.+\\)" link)
  9172. (setq type "help" path (match-string 1 link)))
  9173. (t (setq type "thisfile" path link)))
  9174. (throw 'match t)))
  9175. (when (get-text-property (point) 'org-linked-text)
  9176. (setq type "thisfile"
  9177. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  9178. (1+ (point)) (point))
  9179. path (buffer-substring
  9180. (or (previous-single-property-change pos 'org-linked-text)
  9181. (point-min))
  9182. (or (next-single-property-change pos 'org-linked-text)
  9183. (point-max))))
  9184. (throw 'match t))
  9185. (save-excursion
  9186. (when (or (org-in-regexp org-angle-link-re)
  9187. (let ((match (org-in-regexp org-plain-link-re)))
  9188. ;; Check a plain link is not within a bracket link
  9189. (and match
  9190. (save-excursion
  9191. (progn
  9192. (goto-char (car match))
  9193. (not (org-in-regexp org-bracket-link-regexp))))))
  9194. (let ((line_ending (save-excursion (end-of-line) (point))))
  9195. ;; We are in a line before a plain or bracket link
  9196. (or (re-search-forward org-plain-link-re line_ending t)
  9197. (re-search-forward org-bracket-link-regexp line_ending t))))
  9198. (setq type (match-string 1)
  9199. path (org-link-unescape (match-string 2)))
  9200. (throw 'match t)))
  9201. (save-excursion
  9202. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@#%:]+\\):[ \t]*$"))
  9203. (setq type "tags"
  9204. path (match-string 1))
  9205. (while (string-match ":" path)
  9206. (setq path (replace-match "+" t t path)))
  9207. (throw 'match t)))
  9208. (when (org-in-regexp "<\\([^><\n]+\\)>")
  9209. (setq type "tree-match"
  9210. path (match-string 1))
  9211. (throw 'match t)))
  9212. (unless path
  9213. (user-error "No link found"))
  9214. ;; switch back to reference buffer
  9215. ;; needed when if called in a temporary buffer through
  9216. ;; org-open-link-from-string
  9217. (with-current-buffer (or reference-buffer (current-buffer))
  9218. ;; Remove any trailing spaces in path
  9219. (if (string-match " +\\'" path)
  9220. (setq path (replace-match "" t t path)))
  9221. (if (and org-link-translation-function
  9222. (fboundp org-link-translation-function))
  9223. ;; Check if we need to translate the link
  9224. (let ((tmp (funcall org-link-translation-function type path)))
  9225. (setq type (car tmp) path (cdr tmp))))
  9226. (cond
  9227. ((assoc type org-link-protocols)
  9228. (funcall (nth 1 (assoc type org-link-protocols)) path))
  9229. ((equal type "help")
  9230. (let ((f-or-v (intern path)))
  9231. (cond ((fboundp f-or-v)
  9232. (describe-function f-or-v))
  9233. ((boundp f-or-v)
  9234. (describe-variable f-or-v))
  9235. (t (error "Not a known function or variable")))))
  9236. ((equal type "mailto")
  9237. (let ((cmd (car org-link-mailto-program))
  9238. (args (cdr org-link-mailto-program)) args1
  9239. (address path) (subject "") a)
  9240. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  9241. (setq address (match-string 1 path)
  9242. subject (org-link-escape (match-string 2 path))))
  9243. (while args
  9244. (cond
  9245. ((not (stringp (car args))) (push (pop args) args1))
  9246. (t (setq a (pop args))
  9247. (if (string-match "%a" a)
  9248. (setq a (replace-match address t t a)))
  9249. (if (string-match "%s" a)
  9250. (setq a (replace-match subject t t a)))
  9251. (push a args1))))
  9252. (apply cmd (nreverse args1))))
  9253. ((member type '("http" "https" "ftp" "news"))
  9254. (browse-url (concat type ":" (if (org-string-match-p "[[:nonascii:] ]" path)
  9255. (org-link-escape
  9256. path org-link-escape-chars-browser)
  9257. path))))
  9258. ((string= type "doi")
  9259. (browse-url (concat org-doi-server-url (if (org-string-match-p "[[:nonascii:] ]" path)
  9260. (org-link-escape
  9261. path org-link-escape-chars-browser)
  9262. path))))
  9263. ((member type '("message"))
  9264. (browse-url (concat type ":" path)))
  9265. ((string= type "tags")
  9266. (org-tags-view arg path))
  9267. ((string= type "tree-match")
  9268. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  9269. ((string= type "file")
  9270. (if (string-match "::\\([0-9]+\\)\\'" path)
  9271. (setq line (string-to-number (match-string 1 path))
  9272. path (substring path 0 (match-beginning 0)))
  9273. (if (string-match "::\\(.+\\)\\'" path)
  9274. (setq search (match-string 1 path)
  9275. path (substring path 0 (match-beginning 0)))))
  9276. (if (string-match "[*?{]" (file-name-nondirectory path))
  9277. (dired path)
  9278. (org-open-file path arg line search)))
  9279. ((string= type "shell")
  9280. (let ((buf (generate-new-buffer "*Org Shell Output"))
  9281. (cmd path))
  9282. (if (or (and (not (string= org-confirm-shell-link-not-regexp ""))
  9283. (string-match org-confirm-shell-link-not-regexp cmd))
  9284. (not org-confirm-shell-link-function)
  9285. (funcall org-confirm-shell-link-function
  9286. (format "Execute \"%s\" in shell? "
  9287. (org-add-props cmd nil
  9288. 'face 'org-warning))))
  9289. (progn
  9290. (message "Executing %s" cmd)
  9291. (shell-command cmd buf)
  9292. (if (featurep 'midnight)
  9293. (setq clean-buffer-list-kill-buffer-names
  9294. (cons buf clean-buffer-list-kill-buffer-names))))
  9295. (error "Abort"))))
  9296. ((string= type "elisp")
  9297. (let ((cmd path))
  9298. (if (or (and (not (string= org-confirm-elisp-link-not-regexp ""))
  9299. (string-match org-confirm-elisp-link-not-regexp cmd))
  9300. (not org-confirm-elisp-link-function)
  9301. (funcall org-confirm-elisp-link-function
  9302. (format "Execute \"%s\" as elisp? "
  9303. (org-add-props cmd nil
  9304. 'face 'org-warning))))
  9305. (message "%s => %s" cmd
  9306. (if (equal (string-to-char cmd) ?\()
  9307. (eval (read cmd))
  9308. (call-interactively (read cmd))))
  9309. (error "Abort"))))
  9310. ((and (string= type "thisfile")
  9311. (run-hook-with-args-until-success
  9312. 'org-open-link-functions path)))
  9313. ((string= type "thisfile")
  9314. (if arg
  9315. (switch-to-buffer-other-window
  9316. (org-get-buffer-for-internal-link (current-buffer)))
  9317. (org-mark-ring-push))
  9318. (let ((cmd `(org-link-search
  9319. ,path
  9320. ,(cond ((equal arg '(4)) ''occur)
  9321. ((equal arg '(16)) ''org-occur))
  9322. ,pos)))
  9323. (condition-case nil (let ((org-link-search-inhibit-query t))
  9324. (eval cmd))
  9325. (error (progn (widen) (eval cmd))))))
  9326. (t (browse-url-at-point)))))))
  9327. (move-marker org-open-link-marker nil)
  9328. (run-hook-with-args 'org-follow-link-hook)))
  9329. (defun org-offer-links-in-entry (buffer marker &optional nth zero)
  9330. "Offer links in the current entry and return the selected link.
  9331. If there is only one link, return it.
  9332. If NTH is an integer, return the NTH link found.
  9333. If ZERO is a string, check also this string for a link, and if
  9334. there is one, return it."
  9335. (with-current-buffer buffer
  9336. (save-excursion
  9337. (save-restriction
  9338. (widen)
  9339. (goto-char marker)
  9340. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  9341. "\\(" org-angle-link-re "\\)\\|"
  9342. "\\(" org-plain-link-re "\\)"))
  9343. (cnt ?0)
  9344. (in-emacs (if (integerp nth) nil nth))
  9345. have-zero end links link c)
  9346. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  9347. (push (match-string 0 zero) links)
  9348. (setq cnt (1- cnt) have-zero t))
  9349. (save-excursion
  9350. (org-back-to-heading t)
  9351. (setq end (save-excursion (outline-next-heading) (point)))
  9352. (while (re-search-forward re end t)
  9353. (push (match-string 0) links))
  9354. (setq links (org-uniquify (reverse links))))
  9355. (cond
  9356. ((null links)
  9357. (message "No links"))
  9358. ((equal (length links) 1)
  9359. (setq link (car links)))
  9360. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  9361. (setq link (nth (if have-zero nth (1- nth)) links)))
  9362. (t ; we have to select a link
  9363. (save-excursion
  9364. (save-window-excursion
  9365. (delete-other-windows)
  9366. (with-output-to-temp-buffer "*Select Link*"
  9367. (mapc (lambda (l)
  9368. (if (not (string-match org-bracket-link-regexp l))
  9369. (princ (format "[%c] %s\n" (incf cnt)
  9370. (org-remove-angle-brackets l)))
  9371. (if (match-end 3)
  9372. (princ (format "[%c] %s (%s)\n" (incf cnt)
  9373. (match-string 3 l) (match-string 1 l)))
  9374. (princ (format "[%c] %s\n" (incf cnt)
  9375. (match-string 1 l))))))
  9376. links))
  9377. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  9378. (message "Select link to open, RET to open all:")
  9379. (setq c (read-char-exclusive))
  9380. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  9381. (when (equal c ?q) (error "Abort"))
  9382. (if (equal c ?\C-m)
  9383. (setq link links)
  9384. (setq nth (- c ?0))
  9385. (if have-zero (setq nth (1+ nth)))
  9386. (unless (and (integerp nth) (>= (length links) nth))
  9387. (error "Invalid link selection"))
  9388. (setq link (nth (1- nth) links)))))
  9389. (cons link end))))))
  9390. ;; Add special file links that specify the way of opening
  9391. (org-add-link-type "file+sys" 'org-open-file-with-system)
  9392. (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
  9393. (defun org-open-file-with-system (path)
  9394. "Open file at PATH using the system way of opening it."
  9395. (org-open-file path 'system))
  9396. (defun org-open-file-with-emacs (path)
  9397. "Open file at PATH in Emacs."
  9398. (org-open-file path 'emacs))
  9399. ;;; File search
  9400. (defvar org-create-file-search-functions nil
  9401. "List of functions to construct the right search string for a file link.
  9402. These functions are called in turn with point at the location to
  9403. which the link should point.
  9404. A function in the hook should first test if it would like to
  9405. handle this file type, for example by checking the `major-mode'
  9406. or the file extension. If it decides not to handle this file, it
  9407. should just return nil to give other functions a chance. If it
  9408. does handle the file, it must return the search string to be used
  9409. when following the link. The search string will be part of the
  9410. file link, given after a double colon, and `org-open-at-point'
  9411. will automatically search for it. If special measures must be
  9412. taken to make the search successful, another function should be
  9413. added to the companion hook `org-execute-file-search-functions',
  9414. which see.
  9415. A function in this hook may also use `setq' to set the variable
  9416. `description' to provide a suggestion for the descriptive text to
  9417. be used for this link when it gets inserted into an Org-mode
  9418. buffer with \\[org-insert-link].")
  9419. (defvar org-execute-file-search-functions nil
  9420. "List of functions to execute a file search triggered by a link.
  9421. Functions added to this hook must accept a single argument, the
  9422. search string that was part of the file link, the part after the
  9423. double colon. The function must first check if it would like to
  9424. handle this search, for example by checking the `major-mode' or
  9425. the file extension. If it decides not to handle this search, it
  9426. should just return nil to give other functions a chance. If it
  9427. does handle the search, it must return a non-nil value to keep
  9428. other functions from trying.
  9429. Each function can access the current prefix argument through the
  9430. variable `current-prefix-argument'. Note that a single prefix is
  9431. used to force opening a link in Emacs, so it may be good to only
  9432. use a numeric or double prefix to guide the search function.
  9433. In case this is needed, a function in this hook can also restore
  9434. the window configuration before `org-open-at-point' was called using:
  9435. (set-window-configuration org-window-config-before-follow-link)")
  9436. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  9437. (defun org-link-search (s &optional type avoid-pos stealth)
  9438. "Search for a link search option.
  9439. If S is surrounded by forward slashes, it is interpreted as a
  9440. regular expression. In org-mode files, this will create an `org-occur'
  9441. sparse tree. In ordinary files, `occur' will be used to list matches.
  9442. If the current buffer is in `dired-mode', grep will be used to search
  9443. in all files. If AVOID-POS is given, ignore matches near that position.
  9444. When optional argument STEALTH is non-nil, do not modify
  9445. visibility around point, thus ignoring
  9446. `org-show-hierarchy-above', `org-show-following-heading' and
  9447. `org-show-siblings' variables."
  9448. (let ((case-fold-search t)
  9449. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  9450. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  9451. (append '(("") (" ") ("\t") ("\n"))
  9452. org-emphasis-alist)
  9453. "\\|") "\\)"))
  9454. (pos (point))
  9455. (pre nil) (post nil)
  9456. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  9457. (cond
  9458. ;; First check if there are any special search functions
  9459. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  9460. ;; Now try the builtin stuff
  9461. ((and (equal (string-to-char s0) ?#)
  9462. (> (length s0) 1)
  9463. (save-excursion
  9464. (goto-char (point-min))
  9465. (and
  9466. (re-search-forward
  9467. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  9468. (setq type 'dedicated
  9469. pos (match-beginning 0))))
  9470. ;; There is an exact target for this
  9471. (goto-char pos)
  9472. (org-back-to-heading t)))
  9473. ((save-excursion
  9474. (goto-char (point-min))
  9475. (and
  9476. (re-search-forward
  9477. (concat "<<" (regexp-quote s0) ">>") nil t)
  9478. (setq type 'dedicated
  9479. pos (match-beginning 0))))
  9480. ;; There is an exact target for this
  9481. (goto-char pos))
  9482. ((save-excursion
  9483. (goto-char (point-min))
  9484. (and
  9485. (re-search-forward
  9486. (format "^[ \t]*#\\+TARGET: %s" (regexp-quote s0)) nil t)
  9487. (setq type 'dedicated pos (match-beginning 0))))
  9488. ;; Found an invisible target.
  9489. (goto-char pos))
  9490. ((save-excursion
  9491. (goto-char (point-min))
  9492. (and
  9493. (re-search-forward
  9494. (format "^[ \t]*#\\+NAME: %s" (regexp-quote s0)) nil t)
  9495. (setq type 'dedicated pos (match-beginning 0))))
  9496. ;; Found an element with a matching #+name affiliated keyword.
  9497. (goto-char pos))
  9498. ((and (string-match "^(\\(.*\\))$" s0)
  9499. (save-excursion
  9500. (goto-char (point-min))
  9501. (and
  9502. (re-search-forward
  9503. (concat "[^[]" (regexp-quote
  9504. (format org-coderef-label-format
  9505. (match-string 1 s0))))
  9506. nil t)
  9507. (setq type 'dedicated
  9508. pos (1+ (match-beginning 0))))))
  9509. ;; There is a coderef target for this
  9510. (goto-char pos))
  9511. ((string-match "^/\\(.*\\)/$" s)
  9512. ;; A regular expression
  9513. (cond
  9514. ((derived-mode-p 'org-mode)
  9515. (org-occur (match-string 1 s)))
  9516. (t (org-do-occur (match-string 1 s)))))
  9517. ((and (derived-mode-p 'org-mode) org-link-search-must-match-exact-headline)
  9518. (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
  9519. (goto-char (point-min))
  9520. (cond
  9521. ((let (case-fold-search)
  9522. (re-search-forward (format org-complex-heading-regexp-format
  9523. (regexp-quote s))
  9524. nil t))
  9525. ;; OK, found a match
  9526. (setq type 'dedicated)
  9527. (goto-char (match-beginning 0)))
  9528. ((and (not org-link-search-inhibit-query)
  9529. (eq org-link-search-must-match-exact-headline 'query-to-create)
  9530. (y-or-n-p "No match - create this as a new heading? "))
  9531. (goto-char (point-max))
  9532. (or (bolp) (newline))
  9533. (insert "* " s "\n")
  9534. (beginning-of-line 0))
  9535. (t
  9536. (goto-char pos)
  9537. (error "No match"))))
  9538. (t
  9539. ;; A normal search string
  9540. (when (equal (string-to-char s) ?*)
  9541. ;; Anchor on headlines, post may include tags.
  9542. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  9543. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
  9544. s (substring s 1)))
  9545. (remove-text-properties
  9546. 0 (length s)
  9547. '(face nil mouse-face nil keymap nil fontified nil) s)
  9548. ;; Make a series of regular expressions to find a match
  9549. (setq words (org-split-string s "[ \n\r\t]+")
  9550. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  9551. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  9552. "\\)" markers)
  9553. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  9554. re2a (concat "[ \t\r\n]" re2a_)
  9555. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  9556. re4 (concat "[^a-zA-Z_]" re4_)
  9557. re1 (concat pre re2 post)
  9558. re3 (concat pre (if pre re4_ re4) post)
  9559. re5 (concat pre ".*" re4)
  9560. re2 (concat pre re2)
  9561. re2a (concat pre (if pre re2a_ re2a))
  9562. re4 (concat pre (if pre re4_ re4))
  9563. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  9564. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  9565. re5 "\\)"
  9566. ))
  9567. (cond
  9568. ((eq type 'org-occur) (org-occur reall))
  9569. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  9570. (t (goto-char (point-min))
  9571. (setq type 'fuzzy)
  9572. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  9573. (org-search-not-self 1 re1 nil t)
  9574. (org-search-not-self 1 re2 nil t)
  9575. (org-search-not-self 1 re2a nil t)
  9576. (org-search-not-self 1 re3 nil t)
  9577. (org-search-not-self 1 re4 nil t)
  9578. (org-search-not-self 1 re5 nil t)
  9579. )
  9580. (goto-char (match-beginning 1))
  9581. (goto-char pos)
  9582. (error "No match"))))))
  9583. (and (derived-mode-p 'org-mode)
  9584. (not stealth)
  9585. (org-show-context 'link-search))
  9586. type))
  9587. (defun org-search-not-self (group &rest args)
  9588. "Execute `re-search-forward', but only accept matches that do not
  9589. enclose the position of `org-open-link-marker'."
  9590. (let ((m org-open-link-marker))
  9591. (catch 'exit
  9592. (while (apply 're-search-forward args)
  9593. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  9594. (goto-char (match-end group))
  9595. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  9596. (> (match-beginning 0) (marker-position m))
  9597. (< (match-end 0) (marker-position m)))
  9598. (save-match-data
  9599. (or (not (org-in-regexp
  9600. org-bracket-link-analytic-regexp 1))
  9601. (not (match-end 4)) ; no description
  9602. (and (<= (match-beginning 4) (point))
  9603. (>= (match-end 4) (point))))))
  9604. (throw 'exit (point))))))))
  9605. (defun org-get-buffer-for-internal-link (buffer)
  9606. "Return a buffer to be used for displaying the link target of internal links."
  9607. (cond
  9608. ((not org-display-internal-link-with-indirect-buffer)
  9609. buffer)
  9610. ((string-match "(Clone)$" (buffer-name buffer))
  9611. (message "Buffer is already a clone, not making another one")
  9612. ;; we also do not modify visibility in this case
  9613. buffer)
  9614. (t ; make a new indirect buffer for displaying the link
  9615. (let* ((bn (buffer-name buffer))
  9616. (ibn (concat bn "(Clone)"))
  9617. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  9618. (with-current-buffer ib (org-overview))
  9619. ib))))
  9620. (defun org-do-occur (regexp &optional cleanup)
  9621. "Call the Emacs command `occur'.
  9622. If CLEANUP is non-nil, remove the printout of the regular expression
  9623. in the *Occur* buffer. This is useful if the regex is long and not useful
  9624. to read."
  9625. (occur regexp)
  9626. (when cleanup
  9627. (let ((cwin (selected-window)) win beg end)
  9628. (when (setq win (get-buffer-window "*Occur*"))
  9629. (select-window win))
  9630. (goto-char (point-min))
  9631. (when (re-search-forward "match[a-z]+" nil t)
  9632. (setq beg (match-end 0))
  9633. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  9634. (setq end (1- (match-beginning 0)))))
  9635. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  9636. (goto-char (point-min))
  9637. (select-window cwin))))
  9638. ;;; The mark ring for links jumps
  9639. (defvar org-mark-ring nil
  9640. "Mark ring for positions before jumps in Org-mode.")
  9641. (defvar org-mark-ring-last-goto nil
  9642. "Last position in the mark ring used to go back.")
  9643. ;; Fill and close the ring
  9644. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  9645. (loop for i from 1 to org-mark-ring-length do
  9646. (push (make-marker) org-mark-ring))
  9647. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  9648. org-mark-ring)
  9649. (defun org-mark-ring-push (&optional pos buffer)
  9650. "Put the current position or POS into the mark ring and rotate it."
  9651. (interactive)
  9652. (setq pos (or pos (point)))
  9653. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  9654. (move-marker (car org-mark-ring)
  9655. (or pos (point))
  9656. (or buffer (current-buffer)))
  9657. (message "%s"
  9658. (substitute-command-keys
  9659. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  9660. (defun org-mark-ring-goto (&optional n)
  9661. "Jump to the previous position in the mark ring.
  9662. With prefix arg N, jump back that many stored positions. When
  9663. called several times in succession, walk through the entire ring.
  9664. Org-mode commands jumping to a different position in the current file,
  9665. or to another Org-mode file, automatically push the old position
  9666. onto the ring."
  9667. (interactive "p")
  9668. (let (p m)
  9669. (if (eq last-command this-command)
  9670. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  9671. (setq p org-mark-ring))
  9672. (setq org-mark-ring-last-goto p)
  9673. (setq m (car p))
  9674. (org-pop-to-buffer-same-window (marker-buffer m))
  9675. (goto-char m)
  9676. (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  9677. (defun org-remove-angle-brackets (s)
  9678. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  9679. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  9680. s)
  9681. (defun org-add-angle-brackets (s)
  9682. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  9683. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  9684. s)
  9685. (defun org-remove-double-quotes (s)
  9686. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  9687. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  9688. s)
  9689. ;;; Following specific links
  9690. (defun org-follow-timestamp-link ()
  9691. "Open an agenda view for the time-stamp date/range at point."
  9692. (cond
  9693. ((org-at-date-range-p t)
  9694. (let ((org-agenda-start-on-weekday)
  9695. (t1 (match-string 1))
  9696. (t2 (match-string 2)) tt1 tt2)
  9697. (setq tt1 (time-to-days (org-time-string-to-time t1))
  9698. tt2 (time-to-days (org-time-string-to-time t2)))
  9699. (let ((org-agenda-buffer-tmp-name
  9700. (format "*Org Agenda(a:%s)"
  9701. (concat (substring t1 0 10) "--" (substring t2 0 10)))))
  9702. (org-agenda-list nil tt1 (1+ (- tt2 tt1))))))
  9703. ((org-at-timestamp-p t)
  9704. (let ((org-agenda-buffer-tmp-name
  9705. (format "*Org Agenda(a:%s)" (substring (match-string 1) 0 10))))
  9706. (org-agenda-list nil (time-to-days (org-time-string-to-time
  9707. (substring (match-string 1) 0 10)))
  9708. 1)))
  9709. (t (error "This should not happen"))))
  9710. ;;; Following file links
  9711. (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
  9712. (declare-function mailcap-extension-to-mime "mailcap" (extn))
  9713. (declare-function mailcap-mime-info
  9714. "mailcap" (string &optional request no-decode))
  9715. (defvar org-wait nil)
  9716. (defun org-open-file (path &optional in-emacs line search)
  9717. "Open the file at PATH.
  9718. First, this expands any special file name abbreviations. Then the
  9719. configuration variable `org-file-apps' is checked if it contains an
  9720. entry for this file type, and if yes, the corresponding command is launched.
  9721. If no application is found, Emacs simply visits the file.
  9722. With optional prefix argument IN-EMACS, Emacs will visit the file.
  9723. With a double \\[universal-argument] \\[universal-argument] \
  9724. prefix arg, Org tries to avoid opening in Emacs
  9725. and to use an external application to visit the file.
  9726. Optional LINE specifies a line to go to, optional SEARCH a string
  9727. to search for. If LINE or SEARCH is given, the file will be
  9728. opened in Emacs, unless an entry from org-file-apps that makes
  9729. use of groups in a regexp matches.
  9730. If you want to change the way frames are used when following a
  9731. link, please customize `org-link-frame-setup'.
  9732. If the file does not exist, an error is thrown."
  9733. (let* ((file (if (equal path "")
  9734. buffer-file-name
  9735. (substitute-in-file-name (expand-file-name path))))
  9736. (file-apps (append org-file-apps (org-default-apps)))
  9737. (apps (org-remove-if
  9738. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9739. (apps-dlink (org-remove-if-not
  9740. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9741. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  9742. (dirp (if remp nil (file-directory-p file)))
  9743. (file (if (and dirp org-open-directory-means-index-dot-org)
  9744. (concat (file-name-as-directory file) "index.org")
  9745. file))
  9746. (a-m-a-p (assq 'auto-mode apps))
  9747. (dfile (downcase file))
  9748. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  9749. (link (cond ((and (eq line nil)
  9750. (eq search nil))
  9751. file)
  9752. (line
  9753. (concat file "::" (number-to-string line)))
  9754. (search
  9755. (concat file "::" search))))
  9756. (dlink (downcase link))
  9757. (old-buffer (current-buffer))
  9758. (old-pos (point))
  9759. (old-mode major-mode)
  9760. ext cmd link-match-data)
  9761. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  9762. (setq ext (match-string 1 dfile))
  9763. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  9764. (setq ext (match-string 1 dfile))))
  9765. (cond
  9766. ((member in-emacs '((16) system))
  9767. (setq cmd (cdr (assoc 'system apps))))
  9768. (in-emacs (setq cmd 'emacs))
  9769. (t
  9770. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  9771. (and dirp (cdr (assoc 'directory apps)))
  9772. ; first, try matching against apps-dlink
  9773. ; if we get a match here, store the match data for later
  9774. (let ((match (assoc-default dlink apps-dlink
  9775. 'string-match)))
  9776. (if match
  9777. (progn (setq link-match-data (match-data))
  9778. match)
  9779. (progn (setq in-emacs (or in-emacs line search))
  9780. nil))) ; if we have no match in apps-dlink,
  9781. ; always open the file in emacs if line or search
  9782. ; is given (for backwards compatibility)
  9783. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  9784. 'string-match)
  9785. (cdr (assoc ext apps))
  9786. (cdr (assoc t apps))))))
  9787. (when (eq cmd 'system)
  9788. (setq cmd (cdr (assoc 'system apps))))
  9789. (when (eq cmd 'default)
  9790. (setq cmd (cdr (assoc t apps))))
  9791. (when (eq cmd 'mailcap)
  9792. (require 'mailcap)
  9793. (mailcap-parse-mailcaps)
  9794. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  9795. (command (mailcap-mime-info mime-type)))
  9796. (if (stringp command)
  9797. (setq cmd command)
  9798. (setq cmd 'emacs))))
  9799. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  9800. (not (file-exists-p file))
  9801. (not org-open-non-existing-files))
  9802. (error "No such file: %s" file))
  9803. (cond
  9804. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  9805. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  9806. (while (string-match "['\"]%s['\"]" cmd)
  9807. (setq cmd (replace-match "%s" t t cmd)))
  9808. (while (string-match "%s" cmd)
  9809. (setq cmd (replace-match
  9810. (save-match-data
  9811. (shell-quote-argument
  9812. (convert-standard-filename file)))
  9813. t t cmd)))
  9814. ;; Replace "%1", "%2" etc. in command with group matches from regex
  9815. (save-match-data
  9816. (let ((match-index 1)
  9817. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  9818. (set-match-data link-match-data)
  9819. (while (<= match-index number-of-groups)
  9820. (let ((regex (concat "%" (number-to-string match-index)))
  9821. (replace-with (match-string match-index dlink)))
  9822. (while (string-match regex cmd)
  9823. (setq cmd (replace-match replace-with t t cmd))))
  9824. (setq match-index (+ match-index 1)))))
  9825. (save-window-excursion
  9826. (message "Running %s...done" cmd)
  9827. (start-process-shell-command cmd nil cmd)
  9828. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))))
  9829. ((or (stringp cmd)
  9830. (eq cmd 'emacs))
  9831. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  9832. (widen)
  9833. (if line (org-goto-line line)
  9834. (if search (org-link-search search))))
  9835. ((consp cmd)
  9836. (let ((file (convert-standard-filename file)))
  9837. (save-match-data
  9838. (set-match-data link-match-data)
  9839. (eval cmd))))
  9840. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  9841. (and (derived-mode-p 'org-mode) (eq old-mode 'org-mode)
  9842. (or (not (equal old-buffer (current-buffer)))
  9843. (not (equal old-pos (point))))
  9844. (org-mark-ring-push old-pos old-buffer))))
  9845. (defun org-file-apps-entry-match-against-dlink-p (entry)
  9846. "This function returns non-nil if `entry' uses a regular
  9847. expression which should be matched against the whole link by
  9848. org-open-file.
  9849. It assumes that is the case when the entry uses a regular
  9850. expression which has at least one grouping construct and the
  9851. action is either a lisp form or a command string containing
  9852. '%1', i.e. using at least one subexpression match as a
  9853. parameter."
  9854. (let ((selector (car entry))
  9855. (action (cdr entry)))
  9856. (if (stringp selector)
  9857. (and (> (regexp-opt-depth selector) 0)
  9858. (or (and (stringp action)
  9859. (string-match "%[0-9]" action))
  9860. (consp action)))
  9861. nil)))
  9862. (defun org-default-apps ()
  9863. "Return the default applications for this operating system."
  9864. (cond
  9865. ((eq system-type 'darwin)
  9866. org-file-apps-defaults-macosx)
  9867. ((eq system-type 'windows-nt)
  9868. org-file-apps-defaults-windowsnt)
  9869. (t org-file-apps-defaults-gnu)))
  9870. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  9871. "Convert extensions to regular expressions in the cars of LIST.
  9872. Also, weed out any non-string entries, because the return value is used
  9873. only for regexp matching.
  9874. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  9875. point to the symbol `emacs', indicating that the file should
  9876. be opened in Emacs."
  9877. (append
  9878. (delq nil
  9879. (mapcar (lambda (x)
  9880. (if (not (stringp (car x)))
  9881. nil
  9882. (if (string-match "\\W" (car x))
  9883. x
  9884. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  9885. list))
  9886. (if add-auto-mode
  9887. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  9888. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  9889. (defun org-file-remote-p (file)
  9890. "Test whether FILE specifies a location on a remote system.
  9891. Return non-nil if the location is indeed remote.
  9892. For example, the filename \"/user@host:/foo\" specifies a location
  9893. on the system \"/user@host:\"."
  9894. (cond ((fboundp 'file-remote-p)
  9895. (file-remote-p file))
  9896. ((fboundp 'tramp-handle-file-remote-p)
  9897. (tramp-handle-file-remote-p file))
  9898. ((and (boundp 'ange-ftp-name-format)
  9899. (string-match (car ange-ftp-name-format) file))
  9900. t)))
  9901. ;;;; Refiling
  9902. (defun org-get-org-file ()
  9903. "Read a filename, with default directory `org-directory'."
  9904. (let ((default (or org-default-notes-file remember-data-file)))
  9905. (read-file-name (format "File name [%s]: " default)
  9906. (file-name-as-directory org-directory)
  9907. default)))
  9908. (defun org-notes-order-reversed-p ()
  9909. "Check if the current file should receive notes in reversed order."
  9910. (cond
  9911. ((not org-reverse-note-order) nil)
  9912. ((eq t org-reverse-note-order) t)
  9913. ((not (listp org-reverse-note-order)) nil)
  9914. (t (catch 'exit
  9915. (let ((all org-reverse-note-order)
  9916. entry)
  9917. (while (setq entry (pop all))
  9918. (if (string-match (car entry) buffer-file-name)
  9919. (throw 'exit (cdr entry))))
  9920. nil)))))
  9921. (defvar org-refile-target-table nil
  9922. "The list of refile targets, created by `org-refile'.")
  9923. (defvar org-agenda-new-buffers nil
  9924. "Buffers created to visit agenda files.")
  9925. (defvar org-refile-cache nil
  9926. "Cache for refile targets.")
  9927. (defvar org-refile-markers nil
  9928. "All the markers used for caching refile locations.")
  9929. (defun org-refile-marker (pos)
  9930. "Get a new refile marker, but only if caching is in use."
  9931. (if (not org-refile-use-cache)
  9932. pos
  9933. (let ((m (make-marker)))
  9934. (move-marker m pos)
  9935. (push m org-refile-markers)
  9936. m)))
  9937. (defun org-refile-cache-clear ()
  9938. "Clear the refile cache and disable all the markers."
  9939. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  9940. (setq org-refile-markers nil)
  9941. (setq org-refile-cache nil)
  9942. (message "Refile cache has been cleared"))
  9943. (defun org-refile-cache-check-set (set)
  9944. "Check if all the markers in the cache still have live buffers."
  9945. (let (marker)
  9946. (catch 'exit
  9947. (while (and set (setq marker (nth 3 (pop set))))
  9948. ;; if org-refile-use-outline-path is 'file, marker may be nil
  9949. (when (and marker (null (marker-buffer marker)))
  9950. (message "not found") (sit-for 3)
  9951. (throw 'exit nil)))
  9952. t)))
  9953. (defun org-refile-cache-put (set &rest identifiers)
  9954. "Push the refile targets SET into the cache, under IDENTIFIERS."
  9955. (let* ((key (sha1 (prin1-to-string identifiers)))
  9956. (entry (assoc key org-refile-cache)))
  9957. (if entry
  9958. (setcdr entry set)
  9959. (push (cons key set) org-refile-cache))))
  9960. (defun org-refile-cache-get (&rest identifiers)
  9961. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  9962. (cond
  9963. ((not org-refile-cache) nil)
  9964. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  9965. (t
  9966. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  9967. org-refile-cache))))
  9968. (and set (org-refile-cache-check-set set) set)))))
  9969. (defun org-refile-get-targets (&optional default-buffer excluded-entries)
  9970. "Produce a table with refile targets."
  9971. (let ((case-fold-search nil)
  9972. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  9973. (entries (or org-refile-targets '((nil . (:level . 1)))))
  9974. targets tgs txt re files f desc descre fast-path-p level pos0)
  9975. (message "Getting targets...")
  9976. (with-current-buffer (or default-buffer (current-buffer))
  9977. (while (setq entry (pop entries))
  9978. (setq files (car entry) desc (cdr entry))
  9979. (setq fast-path-p nil)
  9980. (cond
  9981. ((null files) (setq files (list (current-buffer))))
  9982. ((eq files 'org-agenda-files)
  9983. (setq files (org-agenda-files 'unrestricted)))
  9984. ((and (symbolp files) (fboundp files))
  9985. (setq files (funcall files)))
  9986. ((and (symbolp files) (boundp files))
  9987. (setq files (symbol-value files))))
  9988. (if (stringp files) (setq files (list files)))
  9989. (cond
  9990. ((eq (car desc) :tag)
  9991. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  9992. ((eq (car desc) :todo)
  9993. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  9994. ((eq (car desc) :regexp)
  9995. (setq descre (cdr desc)))
  9996. ((eq (car desc) :level)
  9997. (setq descre (concat "^\\*\\{" (number-to-string
  9998. (if org-odd-levels-only
  9999. (1- (* 2 (cdr desc)))
  10000. (cdr desc)))
  10001. "\\}[ \t]")))
  10002. ((eq (car desc) :maxlevel)
  10003. (setq fast-path-p t)
  10004. (setq descre (concat "^\\*\\{1," (number-to-string
  10005. (if org-odd-levels-only
  10006. (1- (* 2 (cdr desc)))
  10007. (cdr desc)))
  10008. "\\}[ \t]")))
  10009. (t (error "Bad refiling target description %s" desc)))
  10010. (while (setq f (pop files))
  10011. (with-current-buffer
  10012. (if (bufferp f) f (org-get-agenda-file-buffer f))
  10013. (or
  10014. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  10015. (progn
  10016. (if (bufferp f) (setq f (buffer-file-name
  10017. (buffer-base-buffer f))))
  10018. (setq f (and f (expand-file-name f)))
  10019. (if (eq org-refile-use-outline-path 'file)
  10020. (push (list (file-name-nondirectory f) f nil nil) tgs))
  10021. (save-excursion
  10022. (save-restriction
  10023. (widen)
  10024. (goto-char (point-min))
  10025. (while (re-search-forward descre nil t)
  10026. (goto-char (setq pos0 (point-at-bol)))
  10027. (catch 'next
  10028. (when org-refile-target-verify-function
  10029. (save-match-data
  10030. (or (funcall org-refile-target-verify-function)
  10031. (throw 'next t))))
  10032. (when (and (looking-at org-complex-heading-regexp)
  10033. (not (member (match-string 4) excluded-entries))
  10034. (match-string 4))
  10035. (setq level (org-reduced-level
  10036. (- (match-end 1) (match-beginning 1)))
  10037. txt (org-link-display-format (match-string 4))
  10038. txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
  10039. re (format org-complex-heading-regexp-format
  10040. (regexp-quote (match-string 4))))
  10041. (when org-refile-use-outline-path
  10042. (setq txt (mapconcat
  10043. 'org-protect-slash
  10044. (append
  10045. (if (eq org-refile-use-outline-path
  10046. 'file)
  10047. (list (file-name-nondirectory
  10048. (buffer-file-name
  10049. (buffer-base-buffer))))
  10050. (if (eq org-refile-use-outline-path
  10051. 'full-file-path)
  10052. (list (buffer-file-name
  10053. (buffer-base-buffer)))))
  10054. (org-get-outline-path fast-path-p
  10055. level txt)
  10056. (list txt))
  10057. "/")))
  10058. (push (list txt f re (org-refile-marker (point)))
  10059. tgs)))
  10060. (when (= (point) pos0)
  10061. ;; verification function has not moved point
  10062. (goto-char (point-at-eol))))))))
  10063. (when org-refile-use-cache
  10064. (org-refile-cache-put tgs (buffer-file-name) descre))
  10065. (setq targets (append tgs targets))
  10066. ))))
  10067. (message "Getting targets...done")
  10068. (nreverse targets)))
  10069. (defun org-protect-slash (s)
  10070. (while (string-match "/" s)
  10071. (setq s (replace-match "\\" t t s)))
  10072. s)
  10073. (defvar org-olpa (make-vector 20 nil))
  10074. (defun org-get-outline-path (&optional fastp level heading)
  10075. "Return the outline path to the current entry, as a list.
  10076. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  10077. routine which makes outline path derivations for an entire file,
  10078. avoiding backtracing. Refile target collection makes use of that."
  10079. (if fastp
  10080. (progn
  10081. (if (> level 19)
  10082. (error "Outline path failure, more than 19 levels"))
  10083. (loop for i from level upto 19 do
  10084. (aset org-olpa i nil))
  10085. (prog1
  10086. (delq nil (append org-olpa nil))
  10087. (aset org-olpa level heading)))
  10088. (let (rtn case-fold-search)
  10089. (save-excursion
  10090. (save-restriction
  10091. (widen)
  10092. (while (org-up-heading-safe)
  10093. (when (looking-at org-complex-heading-regexp)
  10094. (push (org-match-string-no-properties 4) rtn)))
  10095. rtn)))))
  10096. (defun org-format-outline-path (path &optional width prefix separator)
  10097. "Format the outline path PATH for display.
  10098. WIDTH is the maximum number of characters that is available.
  10099. PREFIX is a prefix to be included in the returned string,
  10100. such as the file name.
  10101. SEPARATOR is inserted between the different parts of the path,
  10102. the default is \"/\"."
  10103. (setq width (or width 79))
  10104. (if prefix (setq width (- width (length prefix))))
  10105. (if (not path)
  10106. (or prefix "")
  10107. (let* ((nsteps (length path))
  10108. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  10109. (maxwidth (if (<= total-width width)
  10110. 10000 ;; everything fits
  10111. ;; we need to shorten the level headings
  10112. (/ (- width nsteps) nsteps)))
  10113. (org-odd-levels-only nil)
  10114. (n 0)
  10115. (total (1+ (length prefix))))
  10116. (setq maxwidth (max maxwidth 10))
  10117. (concat prefix
  10118. (if prefix (or separator "/"))
  10119. (mapconcat
  10120. (lambda (h)
  10121. (setq n (1+ n))
  10122. (if (and (= n nsteps) (< maxwidth 10000))
  10123. (setq maxwidth (- total-width total)))
  10124. (if (< (length h) maxwidth)
  10125. (progn (setq total (+ total (length h) 1)) h)
  10126. (setq h (substring h 0 (- maxwidth 2))
  10127. total (+ total maxwidth 1))
  10128. (if (string-match "[ \t]+\\'" h)
  10129. (setq h (substring h 0 (match-beginning 0))))
  10130. (setq h (concat h "..")))
  10131. (org-add-props h nil 'face
  10132. (nth (% (1- n) org-n-level-faces)
  10133. org-level-faces))
  10134. h)
  10135. path (or separator "/"))))))
  10136. (defun org-display-outline-path (&optional file current separator just-return-string)
  10137. "Display the current outline path in the echo area.
  10138. If FILE is non-nil, prepend the output with the file name.
  10139. If CURRENT is non-nil, append the current heading to the output.
  10140. SEPARATOR is passed through to `org-format-outline-path'. It separates
  10141. the different parts of the path and defaults to \"/\".
  10142. If JUST-RETURN-STRING is non-nil, return a string, don't display a message."
  10143. (interactive "P")
  10144. (let* (case-fold-search
  10145. message-log-max ; Don't populate the *Messages* buffer
  10146. (bfn (buffer-file-name (buffer-base-buffer)))
  10147. (path (and (derived-mode-p 'org-mode) (org-get-outline-path)))
  10148. res)
  10149. (if current (setq path (append path
  10150. (save-excursion
  10151. (org-back-to-heading t)
  10152. (if (looking-at org-complex-heading-regexp)
  10153. (list (match-string 4)))))))
  10154. (setq res
  10155. (org-format-outline-path
  10156. path
  10157. (1- (frame-width))
  10158. (and file bfn (concat (file-name-nondirectory bfn) separator))
  10159. separator))
  10160. (if just-return-string
  10161. (org-no-properties res)
  10162. (message "%s" res))))
  10163. (defvar org-refile-history nil
  10164. "History for refiling operations.")
  10165. (defvar org-after-refile-insert-hook nil
  10166. "Hook run after `org-refile' has inserted its stuff at the new location.
  10167. Note that this is still *before* the stuff will be removed from
  10168. the *old* location.")
  10169. (defvar org-capture-last-stored-marker)
  10170. (defvar org-refile-keep nil
  10171. "Non-nil means `org-refile' will copy instead of refile.")
  10172. (defun org-copy ()
  10173. "Like `org-refile', but copy."
  10174. (interactive)
  10175. (let ((org-refile-keep t))
  10176. (funcall 'org-refile nil nil nil "Copy")))
  10177. (defun org-refile (&optional goto default-buffer rfloc msg)
  10178. "Move the entry or entries at point to another heading.
  10179. The list of target headings is compiled using the information in
  10180. `org-refile-targets', which see.
  10181. At the target location, the entry is filed as a subitem of the target
  10182. heading. Depending on `org-reverse-note-order', the new subitem will
  10183. either be the first or the last subitem.
  10184. If there is an active region, all entries in that region will be moved.
  10185. However, the region must fulfill the requirement that the first heading
  10186. is the first one sets the top-level of the moved text - at most siblings
  10187. below it are allowed.
  10188. With prefix arg GOTO, the command will only visit the target location
  10189. and not actually move anything.
  10190. With a double prefix arg \\[universal-argument] \\[universal-argument], \
  10191. go to the location where the last refiling operation has put the subtree.
  10192. With a prefix argument of `2', refile to the running clock.
  10193. RFLOC can be a refile location obtained in a different way.
  10194. MSG is a string to replace \"Refile\" in the default prompt with
  10195. another verb. E.g. `org-copy' sets this parameter to \"Copy\".
  10196. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  10197. If you are using target caching (see `org-refile-use-cache'),
  10198. you have to clear the target cache in order to find new targets.
  10199. This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
  10200. prefix argument (`C-u C-u C-u C-c C-w')."
  10201. (interactive "P")
  10202. (if (member goto '(0 (64)))
  10203. (org-refile-cache-clear)
  10204. (let* ((actionmsg (or msg "Refile"))
  10205. (cbuf (current-buffer))
  10206. (regionp (org-region-active-p))
  10207. (region-start (and regionp (region-beginning)))
  10208. (region-end (and regionp (region-end)))
  10209. (region-length (and regionp (- region-end region-start)))
  10210. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  10211. pos it nbuf file re level reversed)
  10212. (setq last-command nil)
  10213. (when regionp
  10214. (goto-char region-start)
  10215. (or (bolp) (goto-char (point-at-bol)))
  10216. (setq region-start (point))
  10217. (unless (or (org-kill-is-subtree-p
  10218. (buffer-substring region-start region-end))
  10219. (prog1 org-refile-active-region-within-subtree
  10220. (org-toggle-heading)))
  10221. (error "The region is not a (sequence of) subtree(s)")))
  10222. (if (equal goto '(16))
  10223. (org-refile-goto-last-stored)
  10224. (when (or
  10225. (and (equal goto 2)
  10226. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  10227. (prog1
  10228. (setq it (list (or org-clock-heading "running clock")
  10229. (buffer-file-name
  10230. (marker-buffer org-clock-hd-marker))
  10231. ""
  10232. (marker-position org-clock-hd-marker)))
  10233. (setq goto nil)))
  10234. (setq it (or rfloc
  10235. (let (heading-text)
  10236. (save-excursion
  10237. (unless goto
  10238. (org-back-to-heading t)
  10239. (setq heading-text
  10240. (nth 4 (org-heading-components))))
  10241. (org-refile-get-location
  10242. (cond (goto "Goto")
  10243. (regionp (concat actionmsg " region to"))
  10244. (t (concat actionmsg " subtree \""
  10245. heading-text "\" to")))
  10246. default-buffer
  10247. (and (not (equal '(4) goto))
  10248. org-refile-allow-creating-parent-nodes)
  10249. goto))))))
  10250. (setq file (nth 1 it)
  10251. re (nth 2 it)
  10252. pos (nth 3 it))
  10253. (if (and (not goto)
  10254. pos
  10255. (equal (buffer-file-name) file)
  10256. (if regionp
  10257. (and (>= pos region-start)
  10258. (<= pos region-end))
  10259. (and (>= pos (point))
  10260. (< pos (save-excursion
  10261. (org-end-of-subtree t t))))))
  10262. (error "Cannot refile to position inside the tree or region"))
  10263. (setq nbuf (or (find-buffer-visiting file)
  10264. (find-file-noselect file)))
  10265. (if goto
  10266. (progn
  10267. (org-pop-to-buffer-same-window nbuf)
  10268. (goto-char pos)
  10269. (org-show-context 'org-goto))
  10270. (if regionp
  10271. (progn
  10272. (org-kill-new (buffer-substring region-start region-end))
  10273. (org-save-markers-in-region region-start region-end))
  10274. (org-copy-subtree 1 nil t))
  10275. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  10276. (find-file-noselect file)))
  10277. (setq reversed (org-notes-order-reversed-p))
  10278. (save-excursion
  10279. (save-restriction
  10280. (widen)
  10281. (if pos
  10282. (progn
  10283. (goto-char pos)
  10284. (looking-at org-outline-regexp)
  10285. (setq level (org-get-valid-level (funcall outline-level) 1))
  10286. (goto-char
  10287. (if reversed
  10288. (or (outline-next-heading) (point-max))
  10289. (or (save-excursion (org-get-next-sibling))
  10290. (org-end-of-subtree t t)
  10291. (point-max)))))
  10292. (setq level 1)
  10293. (if (not reversed)
  10294. (goto-char (point-max))
  10295. (goto-char (point-min))
  10296. (or (outline-next-heading) (goto-char (point-max)))))
  10297. (if (not (bolp)) (newline))
  10298. (org-paste-subtree level)
  10299. (when org-log-refile
  10300. (org-add-log-setup 'refile nil nil 'findpos
  10301. org-log-refile)
  10302. (unless (eq org-log-refile 'note)
  10303. (save-excursion (org-add-log-note))))
  10304. (and org-auto-align-tags
  10305. (let ((org-loop-over-headlines-in-active-region nil))
  10306. (org-set-tags nil t)))
  10307. (with-demoted-errors
  10308. (bookmark-set "org-refile-last-stored"))
  10309. ;; If we are refiling for capture, make sure that the
  10310. ;; last-capture pointers point here
  10311. (when (org-bound-and-true-p org-refile-for-capture)
  10312. (with-demoted-errors
  10313. (bookmark-set "org-capture-last-stored-marker"))
  10314. (move-marker org-capture-last-stored-marker (point)))
  10315. (if (fboundp 'deactivate-mark) (deactivate-mark))
  10316. (run-hooks 'org-after-refile-insert-hook))))
  10317. (unless org-refile-keep
  10318. (if regionp
  10319. (delete-region (point) (+ (point) region-length))
  10320. (org-cut-subtree)))
  10321. (when (featurep 'org-inlinetask)
  10322. (org-inlinetask-remove-END-maybe))
  10323. (setq org-markers-to-move nil)
  10324. (message (concat actionmsg " to \"%s\" in file %s: done") (car it) file)))))))
  10325. (defun org-refile-goto-last-stored ()
  10326. "Go to the location where the last refile was stored."
  10327. (interactive)
  10328. (bookmark-jump "org-refile-last-stored")
  10329. (message "This is the location of the last refile"))
  10330. (defun org-refile-get-location (&optional prompt default-buffer new-nodes
  10331. no-exclude)
  10332. "Prompt the user for a refile location, using PROMPT.
  10333. PROMPT should not be suffixed with a colon and a space, because
  10334. this function appends the default value from
  10335. `org-refile-history' automatically, if that is not empty.
  10336. When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
  10337. this is used for the GOTO interface."
  10338. (let ((org-refile-targets org-refile-targets)
  10339. (org-refile-use-outline-path org-refile-use-outline-path)
  10340. excluded-entries)
  10341. (when (and (derived-mode-p 'org-mode)
  10342. (not org-refile-use-cache)
  10343. (not no-exclude))
  10344. (org-map-tree
  10345. (lambda()
  10346. (setq excluded-entries
  10347. (append excluded-entries (list (org-get-heading t t)))))))
  10348. (setq org-refile-target-table
  10349. (org-refile-get-targets default-buffer excluded-entries)))
  10350. (unless org-refile-target-table
  10351. (error "No refile targets"))
  10352. (let* ((cbuf (current-buffer))
  10353. (partial-completion-mode nil)
  10354. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  10355. (cfunc (if (and org-refile-use-outline-path
  10356. org-outline-path-complete-in-steps)
  10357. 'org-olpath-completing-read
  10358. 'org-icompleting-read))
  10359. (extra (if org-refile-use-outline-path "/" ""))
  10360. (cbnex (concat (buffer-name) extra))
  10361. (filename (and cfn (expand-file-name cfn)))
  10362. (tbl (mapcar
  10363. (lambda (x)
  10364. (if (and (not (member org-refile-use-outline-path
  10365. '(file full-file-path)))
  10366. (not (equal filename (nth 1 x))))
  10367. (cons (concat (car x) extra " ("
  10368. (file-name-nondirectory (nth 1 x)) ")")
  10369. (cdr x))
  10370. (cons (concat (car x) extra) (cdr x))))
  10371. org-refile-target-table))
  10372. (completion-ignore-case t)
  10373. cdef
  10374. (prompt (concat prompt
  10375. (or (and (car org-refile-history)
  10376. (concat " (default " (car org-refile-history) ")"))
  10377. (and (assoc cbnex tbl) (setq cdef cbnex)
  10378. (concat " (default " cbnex ")"))) ": "))
  10379. pa answ parent-target child parent old-hist)
  10380. (setq old-hist org-refile-history)
  10381. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  10382. nil 'org-refile-history (or cdef (car org-refile-history))))
  10383. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  10384. (org-refile-check-position pa)
  10385. (if pa
  10386. (progn
  10387. (when (or (not org-refile-history)
  10388. (not (eq old-hist org-refile-history))
  10389. (not (equal (car pa) (car org-refile-history))))
  10390. (setq org-refile-history
  10391. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  10392. org-refile-history
  10393. (cdr org-refile-history))))
  10394. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  10395. (pop org-refile-history)))
  10396. pa)
  10397. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  10398. (progn
  10399. (setq parent (match-string 1 answ)
  10400. child (match-string 2 answ))
  10401. (setq parent-target (or (assoc parent tbl)
  10402. (assoc (concat parent "/") tbl)))
  10403. (when (and parent-target
  10404. (or (eq new-nodes t)
  10405. (and (eq new-nodes 'confirm)
  10406. (y-or-n-p (format "Create new node \"%s\"? "
  10407. child)))))
  10408. (org-refile-new-child parent-target child)))
  10409. (error "Invalid target location")))))
  10410. (declare-function org-string-nw-p "org-macs" (s))
  10411. (defun org-refile-check-position (refile-pointer)
  10412. "Check if the refile pointer matches the headline to which it points."
  10413. (let* ((file (nth 1 refile-pointer))
  10414. (re (nth 2 refile-pointer))
  10415. (pos (nth 3 refile-pointer))
  10416. buffer)
  10417. (if (and (not (markerp pos)) (not file))
  10418. (error "Please save the buffer to a file before refiling")
  10419. (when (org-string-nw-p re)
  10420. (setq buffer (if (markerp pos)
  10421. (marker-buffer pos)
  10422. (or (find-buffer-visiting file)
  10423. (find-file-noselect file))))
  10424. (with-current-buffer buffer
  10425. (save-excursion
  10426. (save-restriction
  10427. (widen)
  10428. (goto-char pos)
  10429. (beginning-of-line 1)
  10430. (unless (org-looking-at-p re)
  10431. (error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling")))))))))
  10432. (defun org-refile-new-child (parent-target child)
  10433. "Use refile target PARENT-TARGET to add new CHILD below it."
  10434. (unless parent-target
  10435. (error "Cannot find parent for new node"))
  10436. (let ((file (nth 1 parent-target))
  10437. (pos (nth 3 parent-target))
  10438. level)
  10439. (with-current-buffer (or (find-buffer-visiting file)
  10440. (find-file-noselect file))
  10441. (save-excursion
  10442. (save-restriction
  10443. (widen)
  10444. (if pos
  10445. (goto-char pos)
  10446. (goto-char (point-max))
  10447. (if (not (bolp)) (newline)))
  10448. (when (looking-at org-outline-regexp)
  10449. (setq level (funcall outline-level))
  10450. (org-end-of-subtree t t))
  10451. (org-back-over-empty-lines)
  10452. (insert "\n" (make-string
  10453. (if pos (org-get-valid-level level 1) 1) ?*)
  10454. " " child "\n")
  10455. (beginning-of-line 0)
  10456. (list (concat (car parent-target) "/" child) file "" (point)))))))
  10457. (defun org-olpath-completing-read (prompt collection &rest args)
  10458. "Read an outline path like a file name."
  10459. (let ((thetable collection)
  10460. (org-completion-use-ido nil) ; does not work with ido.
  10461. (org-completion-use-iswitchb nil)) ; or iswitchb
  10462. (apply
  10463. 'org-icompleting-read prompt
  10464. (lambda (string predicate &optional flag)
  10465. (let (rtn r f (l (length string)))
  10466. (cond
  10467. ((eq flag nil)
  10468. ;; try completion
  10469. (try-completion string thetable))
  10470. ((eq flag t)
  10471. ;; all-completions
  10472. (setq rtn (all-completions string thetable predicate))
  10473. (mapcar
  10474. (lambda (x)
  10475. (setq r (substring x l))
  10476. (if (string-match " ([^)]*)$" x)
  10477. (setq f (match-string 0 x))
  10478. (setq f ""))
  10479. (if (string-match "/" r)
  10480. (concat string (substring r 0 (match-end 0)) f)
  10481. x))
  10482. rtn))
  10483. ((eq flag 'lambda)
  10484. ;; exact match?
  10485. (assoc string thetable)))))
  10486. args)))
  10487. ;;;; Dynamic blocks
  10488. (defun org-find-dblock (name)
  10489. "Find the first dynamic block with name NAME in the buffer.
  10490. If not found, stay at current position and return nil."
  10491. (let ((case-fold-search t) pos)
  10492. (save-excursion
  10493. (goto-char (point-min))
  10494. (setq pos (and (re-search-forward
  10495. (concat "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+" name "\\>") nil t)
  10496. (match-beginning 0))))
  10497. (if pos (goto-char pos))
  10498. pos))
  10499. (defconst org-dblock-start-re
  10500. "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  10501. "Matches the start line of a dynamic block, with parameters.")
  10502. (defconst org-dblock-end-re "^[ \t]*#\\+\\(?:END\\|end\\)\\([: \t\r\n]\\|$\\)"
  10503. "Matches the end of a dynamic block.")
  10504. (defun org-create-dblock (plist)
  10505. "Create a dynamic block section, with parameters taken from PLIST.
  10506. PLIST must contain a :name entry which is used as name of the block."
  10507. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  10508. (end-of-line 1)
  10509. (newline))
  10510. (let ((col (current-column))
  10511. (name (plist-get plist :name)))
  10512. (insert "#+BEGIN: " name)
  10513. (while plist
  10514. (if (eq (car plist) :name)
  10515. (setq plist (cddr plist))
  10516. (insert " " (prin1-to-string (pop plist)))))
  10517. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  10518. (beginning-of-line -2)))
  10519. (defun org-prepare-dblock ()
  10520. "Prepare dynamic block for refresh.
  10521. This empties the block, puts the cursor at the insert position and returns
  10522. the property list including an extra property :name with the block name."
  10523. (unless (looking-at org-dblock-start-re)
  10524. (error "Not at a dynamic block"))
  10525. (let* ((begdel (1+ (match-end 0)))
  10526. (name (org-no-properties (match-string 1)))
  10527. (params (append (list :name name)
  10528. (read (concat "(" (match-string 3) ")")))))
  10529. (save-excursion
  10530. (beginning-of-line 1)
  10531. (skip-chars-forward " \t")
  10532. (setq params (plist-put params :indentation-column (current-column))))
  10533. (unless (re-search-forward org-dblock-end-re nil t)
  10534. (error "Dynamic block not terminated"))
  10535. (setq params
  10536. (append params
  10537. (list :content (buffer-substring
  10538. begdel (match-beginning 0)))))
  10539. (delete-region begdel (match-beginning 0))
  10540. (goto-char begdel)
  10541. (open-line 1)
  10542. params))
  10543. (defun org-map-dblocks (&optional command)
  10544. "Apply COMMAND to all dynamic blocks in the current buffer.
  10545. If COMMAND is not given, use `org-update-dblock'."
  10546. (let ((cmd (or command 'org-update-dblock)))
  10547. (save-excursion
  10548. (goto-char (point-min))
  10549. (while (re-search-forward org-dblock-start-re nil t)
  10550. (goto-char (match-beginning 0))
  10551. (save-excursion
  10552. (condition-case nil
  10553. (funcall cmd)
  10554. (error (message "Error during update of dynamic block"))))
  10555. (unless (re-search-forward org-dblock-end-re nil t)
  10556. (error "Dynamic block not terminated"))))))
  10557. (defun org-dblock-update (&optional arg)
  10558. "User command for updating dynamic blocks.
  10559. Update the dynamic block at point. With prefix ARG, update all dynamic
  10560. blocks in the buffer."
  10561. (interactive "P")
  10562. (if arg
  10563. (org-update-all-dblocks)
  10564. (or (looking-at org-dblock-start-re)
  10565. (org-beginning-of-dblock))
  10566. (org-update-dblock)))
  10567. (defun org-update-dblock ()
  10568. "Update the dynamic block at point.
  10569. This means to empty the block, parse for parameters and then call
  10570. the correct writing function."
  10571. (interactive)
  10572. (save-window-excursion
  10573. (let* ((pos (point))
  10574. (line (org-current-line))
  10575. (params (org-prepare-dblock))
  10576. (name (plist-get params :name))
  10577. (indent (plist-get params :indentation-column))
  10578. (cmd (intern (concat "org-dblock-write:" name))))
  10579. (message "Updating dynamic block `%s' at line %d..." name line)
  10580. (funcall cmd params)
  10581. (message "Updating dynamic block `%s' at line %d...done" name line)
  10582. (goto-char pos)
  10583. (when (and indent (> indent 0))
  10584. (setq indent (make-string indent ?\ ))
  10585. (save-excursion
  10586. (org-beginning-of-dblock)
  10587. (forward-line 1)
  10588. (while (not (looking-at org-dblock-end-re))
  10589. (insert indent)
  10590. (beginning-of-line 2))
  10591. (when (looking-at org-dblock-end-re)
  10592. (and (looking-at "[ \t]+")
  10593. (replace-match ""))
  10594. (insert indent)))))))
  10595. (defun org-beginning-of-dblock ()
  10596. "Find the beginning of the dynamic block at point.
  10597. Error if there is no such block at point."
  10598. (let ((pos (point))
  10599. beg)
  10600. (end-of-line 1)
  10601. (if (and (re-search-backward org-dblock-start-re nil t)
  10602. (setq beg (match-beginning 0))
  10603. (re-search-forward org-dblock-end-re nil t)
  10604. (> (match-end 0) pos))
  10605. (goto-char beg)
  10606. (goto-char pos)
  10607. (error "Not in a dynamic block"))))
  10608. (defun org-update-all-dblocks ()
  10609. "Update all dynamic blocks in the buffer.
  10610. This function can be used in a hook."
  10611. (interactive)
  10612. (when (derived-mode-p 'org-mode)
  10613. (org-map-dblocks 'org-update-dblock)))
  10614. ;;;; Completion
  10615. (defun org-get-export-keywords ()
  10616. "Return a list of all currently understood export keywords.
  10617. Export keywords include options, block names, attributes and
  10618. keywords relative to each registered export back-end."
  10619. (delq nil
  10620. (let (keywords)
  10621. (mapc
  10622. (lambda (back-end)
  10623. (let ((props (cdr back-end)))
  10624. ;; Back-end name (for keywords, like #+LATEX:)
  10625. (push (upcase (symbol-name (car back-end))) keywords)
  10626. ;; Back-end options.
  10627. (mapc (lambda (option) (push (cadr option) keywords))
  10628. (plist-get (cdr back-end) :options-alist))))
  10629. (org-bound-and-true-p org-export-registered-backends))
  10630. keywords)))
  10631. (defconst org-options-keywords
  10632. '("ARCHIVE:" "AUTHOR:" "BIND:" "CATEGORY:" "COLUMNS:" "CREATOR:" "DATE:"
  10633. "DESCRIPTION:" "DRAWERS:" "EMAIL:" "EXCLUDE_TAGS:" "FILETAGS:" "INCLUDE:"
  10634. "INDEX:" "KEYWORDS:" "LANGUAGE:" "MACRO:" "OPTIONS:" "PROPERTY:"
  10635. "PRIORITIES:" "SELECT_TAGS:" "SEQ_TODO:" "SETUPFILE:" "STARTUP:" "TAGS:"
  10636. "TITLE:" "TODO:" "TYP_TODO:" "SELECT_TAGS:" "EXCLUDE_TAGS:" "INFOJS_OPT:"))
  10637. (defcustom org-structure-template-alist
  10638. '(("s" "#+BEGIN_SRC ?\n\n#+END_SRC"
  10639. "<src lang=\"?\">\n\n</src>")
  10640. ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE"
  10641. "<example>\n?\n</example>")
  10642. ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE"
  10643. "<quote>\n?\n</quote>")
  10644. ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE"
  10645. "<verse>\n?\n</verse>")
  10646. ("V" "#+BEGIN_VERBATIM\n?\n#+END_VERBATIM"
  10647. "<verbatim>\n?\n</verbatim>")
  10648. ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER"
  10649. "<center>\n?\n</center>")
  10650. ("l" "#+BEGIN_LaTeX\n?\n#+END_LaTeX"
  10651. "<literal style=\"latex\">\n?\n</literal>")
  10652. ("L" "#+LaTeX: "
  10653. "<literal style=\"latex\">?</literal>")
  10654. ("h" "#+BEGIN_HTML\n?\n#+END_HTML"
  10655. "<literal style=\"html\">\n?\n</literal>")
  10656. ("H" "#+HTML: "
  10657. "<literal style=\"html\">?</literal>")
  10658. ("a" "#+BEGIN_ASCII\n?\n#+END_ASCII")
  10659. ("A" "#+ASCII: ")
  10660. ("i" "#+INDEX: ?"
  10661. "#+INDEX: ?")
  10662. ("I" "#+INCLUDE: %file ?"
  10663. "<include file=%file markup=\"?\">"))
  10664. "Structure completion elements.
  10665. This is a list of abbreviation keys and values. The value gets inserted
  10666. if you type `<' followed by the key and then press the completion key,
  10667. usually `M-TAB'. %file will be replaced by a file name after prompting
  10668. for the file using completion. The cursor will be placed at the position
  10669. of the `?` in the template.
  10670. There are two templates for each key, the first uses the original Org syntax,
  10671. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  10672. the default when the /org-mtags.el/ module has been loaded. See also the
  10673. variable `org-mtags-prefer-muse-templates'."
  10674. :group 'org-completion
  10675. :type '(repeat
  10676. (string :tag "Key")
  10677. (string :tag "Template")
  10678. (string :tag "Muse Template")))
  10679. (defun org-try-structure-completion ()
  10680. "Try to complete a structure template before point.
  10681. This looks for strings like \"<e\" on an otherwise empty line and
  10682. expands them."
  10683. (let ((l (buffer-substring (point-at-bol) (point)))
  10684. a)
  10685. (when (and (looking-at "[ \t]*$")
  10686. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  10687. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  10688. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  10689. (match-beginning 1)) a)
  10690. t)))
  10691. (defun org-complete-expand-structure-template (start cell)
  10692. "Expand a structure template."
  10693. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  10694. (rpl (nth (if musep 2 1) cell))
  10695. (ind ""))
  10696. (delete-region start (point))
  10697. (when (string-match "\\`#\\+" rpl)
  10698. (cond
  10699. ((bolp))
  10700. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  10701. (setq ind (buffer-substring (point-at-bol) (point))))
  10702. (t (newline))))
  10703. (setq start (point))
  10704. (if (string-match "%file" rpl)
  10705. (setq rpl (replace-match
  10706. (concat
  10707. "\""
  10708. (save-match-data
  10709. (abbreviate-file-name (read-file-name "Include file: ")))
  10710. "\"")
  10711. t t rpl)))
  10712. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  10713. (concat "\n" ind)))
  10714. (insert rpl)
  10715. (if (re-search-backward "\\?" start t) (delete-char 1))))
  10716. ;;;; TODO, DEADLINE, Comments
  10717. (defun org-toggle-comment ()
  10718. "Change the COMMENT state of an entry."
  10719. (interactive)
  10720. (save-excursion
  10721. (org-back-to-heading)
  10722. (let (case-fold-search)
  10723. (cond
  10724. ((looking-at (format org-heading-keyword-regexp-format
  10725. org-comment-string))
  10726. (goto-char (match-end 1))
  10727. (looking-at (concat " +" org-comment-string))
  10728. (replace-match "" t t)
  10729. (when (eolp) (insert " ")))
  10730. ((looking-at org-outline-regexp)
  10731. (goto-char (match-end 0))
  10732. (insert org-comment-string " "))))))
  10733. (defvar org-last-todo-state-is-todo nil
  10734. "This is non-nil when the last TODO state change led to a TODO state.
  10735. If the last change removed the TODO tag or switched to DONE, then
  10736. this is nil.")
  10737. (defvar org-setting-tags nil) ; dynamically skipped
  10738. (defvar org-todo-setup-filter-hook nil
  10739. "Hook for functions that pre-filter todo specs.
  10740. Each function takes a todo spec and returns either nil or the spec
  10741. transformed into canonical form." )
  10742. (defvar org-todo-get-default-hook nil
  10743. "Hook for functions that get a default item for todo.
  10744. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  10745. nil or a string to be used for the todo mark." )
  10746. (defvar org-agenda-headline-snapshot-before-repeat)
  10747. (defun org-current-effective-time ()
  10748. "Return current time adjusted for `org-extend-today-until' variable."
  10749. (let* ((ct (org-current-time))
  10750. (dct (decode-time ct))
  10751. (ct1
  10752. (cond
  10753. (org-use-last-clock-out-time-as-effective-time
  10754. (or (org-clock-get-last-clock-out-time) ct))
  10755. ((and org-use-effective-time (< (nth 2 dct) org-extend-today-until))
  10756. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct)))
  10757. (t ct))))
  10758. ct1))
  10759. (defun org-todo-yesterday (&optional arg)
  10760. "Like `org-todo' but the time of change will be 23:59 of yesterday."
  10761. (interactive "P")
  10762. (if (eq major-mode 'org-agenda-mode)
  10763. (apply 'org-agenda-todo-yesterday arg)
  10764. (let* ((hour (third (decode-time
  10765. (org-current-time))))
  10766. (org-extend-today-until (1+ hour)))
  10767. (org-todo arg))))
  10768. (defvar org-block-entry-blocking ""
  10769. "First entry preventing the TODO state change.")
  10770. (defun org-todo (&optional arg)
  10771. "Change the TODO state of an item.
  10772. The state of an item is given by a keyword at the start of the heading,
  10773. like
  10774. *** TODO Write paper
  10775. *** DONE Call mom
  10776. The different keywords are specified in the variable `org-todo-keywords'.
  10777. By default the available states are \"TODO\" and \"DONE\".
  10778. So for this example: when the item starts with TODO, it is changed to DONE.
  10779. When it starts with DONE, the DONE is removed. And when neither TODO nor
  10780. DONE are present, add TODO at the beginning of the heading.
  10781. With \\[universal-argument] prefix arg, use completion to determine the new \
  10782. state.
  10783. With numeric prefix arg, switch to that state.
  10784. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  10785. keywords (nextset).
  10786. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  10787. With a numeric prefix arg of 0, inhibit note taking for the change.
  10788. For calling through lisp, arg is also interpreted in the following way:
  10789. 'none -> empty state
  10790. \"\"(empty string) -> switch to empty state
  10791. 'done -> switch to DONE
  10792. 'nextset -> switch to the next set of keywords
  10793. 'previousset -> switch to the previous set of keywords
  10794. \"WAITING\" -> switch to the specified keyword, but only if it
  10795. really is a member of `org-todo-keywords'."
  10796. (interactive "P")
  10797. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  10798. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  10799. 'region-start-level 'region))
  10800. org-loop-over-headlines-in-active-region)
  10801. (org-map-entries
  10802. `(org-todo ,arg)
  10803. org-loop-over-headlines-in-active-region
  10804. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  10805. (if (equal arg '(16)) (setq arg 'nextset))
  10806. (let ((org-blocker-hook org-blocker-hook)
  10807. commentp
  10808. case-fold-search)
  10809. (when (equal arg '(64))
  10810. (setq arg nil org-blocker-hook nil))
  10811. (when (and org-blocker-hook
  10812. (or org-inhibit-blocking
  10813. (org-entry-get nil "NOBLOCKING")))
  10814. (setq org-blocker-hook nil))
  10815. (save-excursion
  10816. (catch 'exit
  10817. (org-back-to-heading t)
  10818. (when (looking-at (concat "^\\*+ " org-comment-string))
  10819. (org-toggle-comment)
  10820. (setq commentp t))
  10821. (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
  10822. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
  10823. (looking-at "\\(?: *\\|[ \t]*$\\)"))
  10824. (let* ((match-data (match-data))
  10825. (startpos (point-at-bol))
  10826. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  10827. (org-log-done org-log-done)
  10828. (org-log-repeat org-log-repeat)
  10829. (org-todo-log-states org-todo-log-states)
  10830. (org-inhibit-logging
  10831. (if (equal arg 0)
  10832. (progn (setq arg nil) 'note) org-inhibit-logging))
  10833. (this (match-string 1))
  10834. (hl-pos (match-beginning 0))
  10835. (head (org-get-todo-sequence-head this))
  10836. (ass (assoc head org-todo-kwd-alist))
  10837. (interpret (nth 1 ass))
  10838. (done-word (nth 3 ass))
  10839. (final-done-word (nth 4 ass))
  10840. (org-last-state (or this ""))
  10841. (completion-ignore-case t)
  10842. (member (member this org-todo-keywords-1))
  10843. (tail (cdr member))
  10844. (org-state (cond
  10845. ((and org-todo-key-trigger
  10846. (or (and (equal arg '(4))
  10847. (eq org-use-fast-todo-selection 'prefix))
  10848. (and (not arg) org-use-fast-todo-selection
  10849. (not (eq org-use-fast-todo-selection
  10850. 'prefix)))))
  10851. ;; Use fast selection
  10852. (org-fast-todo-selection))
  10853. ((and (equal arg '(4))
  10854. (or (not org-use-fast-todo-selection)
  10855. (not org-todo-key-trigger)))
  10856. ;; Read a state with completion
  10857. (org-icompleting-read
  10858. "State: " (mapcar (lambda(x) (list x))
  10859. org-todo-keywords-1)
  10860. nil t))
  10861. ((eq arg 'right)
  10862. (if this
  10863. (if tail (car tail) nil)
  10864. (car org-todo-keywords-1)))
  10865. ((eq arg 'left)
  10866. (if (equal member org-todo-keywords-1)
  10867. nil
  10868. (if this
  10869. (nth (- (length org-todo-keywords-1)
  10870. (length tail) 2)
  10871. org-todo-keywords-1)
  10872. (org-last org-todo-keywords-1))))
  10873. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  10874. (setq arg nil))) ; hack to fall back to cycling
  10875. (arg
  10876. ;; user or caller requests a specific state
  10877. (cond
  10878. ((equal arg "") nil)
  10879. ((eq arg 'none) nil)
  10880. ((eq arg 'done) (or done-word (car org-done-keywords)))
  10881. ((eq arg 'nextset)
  10882. (or (car (cdr (member head org-todo-heads)))
  10883. (car org-todo-heads)))
  10884. ((eq arg 'previousset)
  10885. (let ((org-todo-heads (reverse org-todo-heads)))
  10886. (or (car (cdr (member head org-todo-heads)))
  10887. (car org-todo-heads))))
  10888. ((car (member arg org-todo-keywords-1)))
  10889. ((stringp arg)
  10890. (error "State `%s' not valid in this file" arg))
  10891. ((nth (1- (prefix-numeric-value arg))
  10892. org-todo-keywords-1))))
  10893. ((null member) (or head (car org-todo-keywords-1)))
  10894. ((equal this final-done-word) nil) ;; -> make empty
  10895. ((null tail) nil) ;; -> first entry
  10896. ((memq interpret '(type priority))
  10897. (if (eq this-command last-command)
  10898. (car tail)
  10899. (if (> (length tail) 0)
  10900. (or done-word (car org-done-keywords))
  10901. nil)))
  10902. (t
  10903. (car tail))))
  10904. (org-state (or
  10905. (run-hook-with-args-until-success
  10906. 'org-todo-get-default-hook org-state org-last-state)
  10907. org-state))
  10908. (next (if org-state (concat " " org-state " ") " "))
  10909. (change-plist (list :type 'todo-state-change :from this :to org-state
  10910. :position startpos))
  10911. dolog now-done-p)
  10912. (when org-blocker-hook
  10913. (setq org-last-todo-state-is-todo
  10914. (not (member this org-done-keywords)))
  10915. (unless (save-excursion
  10916. (save-match-data
  10917. (org-with-wide-buffer
  10918. (run-hook-with-args-until-failure
  10919. 'org-blocker-hook change-plist))))
  10920. (if (org-called-interactively-p 'interactive)
  10921. (user-error "TODO state change from %s to %s blocked (by \"%s\")"
  10922. this org-state org-block-entry-blocking)
  10923. ;; fail silently
  10924. (message "TODO state change from %s to %s blocked (by \"%s\")"
  10925. this org-state org-block-entry-blocking)
  10926. (throw 'exit nil))))
  10927. (store-match-data match-data)
  10928. (replace-match next t t)
  10929. (unless (pos-visible-in-window-p hl-pos)
  10930. (message "TODO state changed to %s" (org-trim next)))
  10931. (unless head
  10932. (setq head (org-get-todo-sequence-head org-state)
  10933. ass (assoc head org-todo-kwd-alist)
  10934. interpret (nth 1 ass)
  10935. done-word (nth 3 ass)
  10936. final-done-word (nth 4 ass)))
  10937. (when (memq arg '(nextset previousset))
  10938. (message "Keyword-Set %d/%d: %s"
  10939. (- (length org-todo-sets) -1
  10940. (length (memq (assoc org-state org-todo-sets) org-todo-sets)))
  10941. (length org-todo-sets)
  10942. (mapconcat 'identity (assoc org-state org-todo-sets) " ")))
  10943. (setq org-last-todo-state-is-todo
  10944. (not (member org-state org-done-keywords)))
  10945. (setq now-done-p (and (member org-state org-done-keywords)
  10946. (not (member this org-done-keywords))))
  10947. (and logging (org-local-logging logging))
  10948. (when (and (or org-todo-log-states org-log-done)
  10949. (not (eq org-inhibit-logging t))
  10950. (not (memq arg '(nextset previousset))))
  10951. ;; we need to look at recording a time and note
  10952. (setq dolog (or (nth 1 (assoc org-state org-todo-log-states))
  10953. (nth 2 (assoc this org-todo-log-states))))
  10954. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  10955. (setq dolog 'time))
  10956. (when (and org-state
  10957. (member org-state org-not-done-keywords)
  10958. (not (member this org-not-done-keywords)))
  10959. ;; This is now a todo state and was not one before
  10960. ;; If there was a CLOSED time stamp, get rid of it.
  10961. (org-add-planning-info nil nil 'closed))
  10962. (when (and now-done-p org-log-done)
  10963. ;; It is now done, and it was not done before
  10964. (org-add-planning-info 'closed (org-current-effective-time))
  10965. (if (and (not dolog) (eq 'note org-log-done))
  10966. (org-add-log-setup 'done org-state this 'findpos 'note)))
  10967. (when (and org-state dolog)
  10968. ;; This is a non-nil state, and we need to log it
  10969. (org-add-log-setup 'state org-state this 'findpos dolog)))
  10970. ;; Fixup tag positioning
  10971. (org-todo-trigger-tag-changes org-state)
  10972. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  10973. (when org-provide-todo-statistics
  10974. (org-update-parent-todo-statistics))
  10975. (run-hooks 'org-after-todo-state-change-hook)
  10976. (if (and arg (not (member org-state org-done-keywords)))
  10977. (setq head (org-get-todo-sequence-head org-state)))
  10978. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  10979. ;; Do we need to trigger a repeat?
  10980. (when now-done-p
  10981. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  10982. ;; This is for the agenda, take a snapshot of the headline.
  10983. (save-match-data
  10984. (setq org-agenda-headline-snapshot-before-repeat
  10985. (org-get-heading))))
  10986. (org-auto-repeat-maybe org-state))
  10987. ;; Fixup cursor location if close to the keyword
  10988. (if (and (outline-on-heading-p)
  10989. (not (bolp))
  10990. (save-excursion (beginning-of-line 1)
  10991. (looking-at org-todo-line-regexp))
  10992. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  10993. (progn
  10994. (goto-char (or (match-end 2) (match-end 1)))
  10995. (and (looking-at " ") (just-one-space))))
  10996. (when org-trigger-hook
  10997. (save-excursion
  10998. (run-hook-with-args 'org-trigger-hook change-plist)))
  10999. (when commentp (org-toggle-comment))))))))
  11000. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  11001. "Block turning an entry into a TODO, using the hierarchy.
  11002. This checks whether the current task should be blocked from state
  11003. changes. Such blocking occurs when:
  11004. 1. The task has children which are not all in a completed state.
  11005. 2. A task has a parent with the property :ORDERED:, and there
  11006. are siblings prior to the current task with incomplete
  11007. status.
  11008. 3. The parent of the task is blocked because it has siblings that should
  11009. be done first, or is child of a block grandparent TODO entry."
  11010. (if (not org-enforce-todo-dependencies)
  11011. t ; if locally turned off don't block
  11012. (catch 'dont-block
  11013. ;; If this is not a todo state change, or if this entry is already DONE,
  11014. ;; do not block
  11015. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11016. (member (plist-get change-plist :from)
  11017. (cons 'done org-done-keywords))
  11018. (member (plist-get change-plist :to)
  11019. (cons 'todo org-not-done-keywords))
  11020. (not (plist-get change-plist :to)))
  11021. (throw 'dont-block t))
  11022. ;; If this task has children, and any are undone, it's blocked
  11023. (save-excursion
  11024. (org-back-to-heading t)
  11025. (let ((this-level (funcall outline-level)))
  11026. (outline-next-heading)
  11027. (let ((child-level (funcall outline-level)))
  11028. (while (and (not (eobp))
  11029. (> child-level this-level))
  11030. ;; this todo has children, check whether they are all
  11031. ;; completed
  11032. (if (and (not (org-entry-is-done-p))
  11033. (org-entry-is-todo-p))
  11034. (progn (setq org-block-entry-blocking (org-get-heading))
  11035. (throw 'dont-block nil)))
  11036. (outline-next-heading)
  11037. (setq child-level (funcall outline-level))))))
  11038. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  11039. ;; any previous siblings are undone, it's blocked
  11040. (save-excursion
  11041. (org-back-to-heading t)
  11042. (let* ((pos (point))
  11043. (parent-pos (and (org-up-heading-safe) (point))))
  11044. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  11045. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11046. (forward-line 1)
  11047. (re-search-forward org-not-done-heading-regexp pos t))
  11048. (setq org-block-entry-blocking (match-string 0))
  11049. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  11050. ;; Search further up the hierarchy, to see if an ancestor is blocked
  11051. (while t
  11052. (goto-char parent-pos)
  11053. (if (not (looking-at org-not-done-heading-regexp))
  11054. (throw 'dont-block t)) ; do not block, parent is not a TODO
  11055. (setq pos (point))
  11056. (setq parent-pos (and (org-up-heading-safe) (point)))
  11057. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  11058. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11059. (forward-line 1)
  11060. (re-search-forward org-not-done-heading-regexp pos t)
  11061. (setq org-block-entry-blocking (org-get-heading)))
  11062. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  11063. (defcustom org-track-ordered-property-with-tag nil
  11064. "Should the ORDERED property also be shown as a tag?
  11065. The ORDERED property decides if an entry should require subtasks to be
  11066. completed in sequence. Since a property is not very visible, setting
  11067. this option means that toggling the ORDERED property with the command
  11068. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  11069. not relevant for the behavior, but it makes things more visible.
  11070. Note that toggling the tag with tags commands will not change the property
  11071. and therefore not influence behavior!
  11072. This can be t, meaning the tag ORDERED should be used, It can also be a
  11073. string to select a different tag for this task."
  11074. :group 'org-todo
  11075. :type '(choice
  11076. (const :tag "No tracking" nil)
  11077. (const :tag "Track with ORDERED tag" t)
  11078. (string :tag "Use other tag")))
  11079. (defun org-toggle-ordered-property ()
  11080. "Toggle the ORDERED property of the current entry.
  11081. For better visibility, you can track the value of this property with a tag.
  11082. See variable `org-track-ordered-property-with-tag'."
  11083. (interactive)
  11084. (let* ((t1 org-track-ordered-property-with-tag)
  11085. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  11086. (save-excursion
  11087. (org-back-to-heading)
  11088. (if (org-entry-get nil "ORDERED")
  11089. (progn
  11090. (org-delete-property "ORDERED" "PROPERTIES")
  11091. (and tag (org-toggle-tag tag 'off))
  11092. (message "Subtasks can be completed in arbitrary order"))
  11093. (org-entry-put nil "ORDERED" "t")
  11094. (and tag (org-toggle-tag tag 'on))
  11095. (message "Subtasks must be completed in sequence")))))
  11096. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  11097. (defun org-block-todo-from-checkboxes (change-plist)
  11098. "Block turning an entry into a TODO, using checkboxes.
  11099. This checks whether the current task should be blocked from state
  11100. changes because there are unchecked boxes in this entry."
  11101. (if (not org-enforce-todo-checkbox-dependencies)
  11102. t ; if locally turned off don't block
  11103. (catch 'dont-block
  11104. ;; If this is not a todo state change, or if this entry is already DONE,
  11105. ;; do not block
  11106. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11107. (member (plist-get change-plist :from)
  11108. (cons 'done org-done-keywords))
  11109. (member (plist-get change-plist :to)
  11110. (cons 'todo org-not-done-keywords))
  11111. (not (plist-get change-plist :to)))
  11112. (throw 'dont-block t))
  11113. ;; If this task has checkboxes that are not checked, it's blocked
  11114. (save-excursion
  11115. (org-back-to-heading t)
  11116. (let ((beg (point)) end)
  11117. (outline-next-heading)
  11118. (setq end (point))
  11119. (goto-char beg)
  11120. (if (org-list-search-forward
  11121. (concat (org-item-beginning-re)
  11122. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
  11123. "\\[[- ]\\]")
  11124. end t)
  11125. (progn
  11126. (if (boundp 'org-blocked-by-checkboxes)
  11127. (setq org-blocked-by-checkboxes t))
  11128. (throw 'dont-block nil)))))
  11129. t))) ; do not block
  11130. (defun org-entry-blocked-p ()
  11131. "Is the current entry blocked?"
  11132. (org-with-silent-modifications
  11133. (if (org-entry-get nil "NOBLOCKING")
  11134. nil ;; Never block this entry
  11135. (not (run-hook-with-args-until-failure
  11136. 'org-blocker-hook
  11137. (list :type 'todo-state-change
  11138. :position (point)
  11139. :from 'todo
  11140. :to 'done))))))
  11141. (defun org-update-statistics-cookies (all)
  11142. "Update the statistics cookie, either from TODO or from checkboxes.
  11143. This should be called with the cursor in a line with a statistics cookie."
  11144. (interactive "P")
  11145. (if all
  11146. (progn
  11147. (org-update-checkbox-count 'all)
  11148. (org-map-entries 'org-update-parent-todo-statistics))
  11149. (if (not (org-at-heading-p))
  11150. (org-update-checkbox-count)
  11151. (let ((pos (point-marker))
  11152. end l1 l2)
  11153. (ignore-errors (org-back-to-heading t))
  11154. (if (not (org-at-heading-p))
  11155. (org-update-checkbox-count)
  11156. (setq l1 (org-outline-level))
  11157. (setq end (save-excursion
  11158. (outline-next-heading)
  11159. (if (org-at-heading-p) (setq l2 (org-outline-level)))
  11160. (point)))
  11161. (if (and (save-excursion
  11162. (re-search-forward
  11163. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  11164. (not (save-excursion (re-search-forward
  11165. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  11166. (org-update-checkbox-count)
  11167. (if (and l2 (> l2 l1))
  11168. (progn
  11169. (goto-char end)
  11170. (org-update-parent-todo-statistics))
  11171. (goto-char pos)
  11172. (beginning-of-line 1)
  11173. (while (re-search-forward
  11174. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  11175. (point-at-eol) t)
  11176. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  11177. (goto-char pos)
  11178. (move-marker pos nil)))))
  11179. (defvar org-entry-property-inherited-from) ;; defined below
  11180. (defun org-update-parent-todo-statistics ()
  11181. "Update any statistics cookie in the parent of the current headline.
  11182. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  11183. the entire subtree and this will travel up the hierarchy and update
  11184. statistics everywhere."
  11185. (let* ((prop (save-excursion (org-up-heading-safe)
  11186. (org-entry-get nil "COOKIE_DATA" 'inherit)))
  11187. (recursive (or (not org-hierarchical-todo-statistics)
  11188. (and prop (string-match "\\<recursive\\>" prop))))
  11189. (lim (or (and prop (marker-position org-entry-property-inherited-from))
  11190. 0))
  11191. (first t)
  11192. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  11193. level ltoggle l1 new ndel
  11194. (cnt-all 0) (cnt-done 0) is-percent kwd
  11195. checkbox-beg ov ovs ove cookie-present)
  11196. (catch 'exit
  11197. (save-excursion
  11198. (beginning-of-line 1)
  11199. (setq ltoggle (funcall outline-level))
  11200. ;; Three situations are to consider:
  11201. ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
  11202. ;; to the top-level ancestor on the headline;
  11203. ;; 2. If parent has "recursive" property, repeat up to the
  11204. ;; headline setting that property, taking inheritance into
  11205. ;; account;
  11206. ;; 3. Else, move up to direct parent and proceed only once.
  11207. (while (and (setq level (org-up-heading-safe))
  11208. (or recursive first)
  11209. (>= (point) lim))
  11210. (setq first nil cookie-present nil)
  11211. (unless (and level
  11212. (not (string-match
  11213. "\\<checkbox\\>"
  11214. (downcase (or (org-entry-get nil "COOKIE_DATA")
  11215. "")))))
  11216. (throw 'exit nil))
  11217. (while (re-search-forward box-re (point-at-eol) t)
  11218. (setq cnt-all 0 cnt-done 0 cookie-present t)
  11219. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  11220. (save-match-data
  11221. (unless (outline-next-heading) (throw 'exit nil))
  11222. (while (and (looking-at org-complex-heading-regexp)
  11223. (> (setq l1 (length (match-string 1))) level))
  11224. (setq kwd (and (or recursive (= l1 ltoggle))
  11225. (match-string 2)))
  11226. (if (or (eq org-provide-todo-statistics 'all-headlines)
  11227. (and (listp org-provide-todo-statistics)
  11228. (or (member kwd org-provide-todo-statistics)
  11229. (member kwd org-done-keywords))))
  11230. (setq cnt-all (1+ cnt-all))
  11231. (if (eq org-provide-todo-statistics t)
  11232. (and kwd (setq cnt-all (1+ cnt-all)))))
  11233. (and (member kwd org-done-keywords)
  11234. (setq cnt-done (1+ cnt-done)))
  11235. (outline-next-heading)))
  11236. (setq new
  11237. (if is-percent
  11238. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  11239. (format "[%d/%d]" cnt-done cnt-all))
  11240. ndel (- (match-end 0) checkbox-beg))
  11241. ;; handle overlays when updating cookie from column view
  11242. (when (setq ov (car (overlays-at checkbox-beg)))
  11243. (setq ovs (overlay-start ov) ove (overlay-end ov))
  11244. (delete-overlay ov))
  11245. (goto-char checkbox-beg)
  11246. (insert new)
  11247. (delete-region (point) (+ (point) ndel))
  11248. (when org-auto-align-tags (org-fix-tags-on-the-fly))
  11249. (when ov (move-overlay ov ovs ove)))
  11250. (when cookie-present
  11251. (run-hook-with-args 'org-after-todo-statistics-hook
  11252. cnt-done (- cnt-all cnt-done))))))
  11253. (run-hooks 'org-todo-statistics-hook)))
  11254. (defvar org-after-todo-statistics-hook nil
  11255. "Hook that is called after a TODO statistics cookie has been updated.
  11256. Each function is called with two arguments: the number of not-done entries
  11257. and the number of done entries.
  11258. For example, the following function, when added to this hook, will switch
  11259. an entry to DONE when all children are done, and back to TODO when new
  11260. entries are set to a TODO status. Note that this hook is only called
  11261. when there is a statistics cookie in the headline!
  11262. (defun org-summary-todo (n-done n-not-done)
  11263. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  11264. (let (org-log-done org-log-states) ; turn off logging
  11265. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  11266. ")
  11267. (defvar org-todo-statistics-hook nil
  11268. "Hook that is run whenever Org thinks TODO statistics should be updated.
  11269. This hook runs even if there is no statistics cookie present, in which case
  11270. `org-after-todo-statistics-hook' would not run.")
  11271. (defun org-todo-trigger-tag-changes (state)
  11272. "Apply the changes defined in `org-todo-state-tags-triggers'."
  11273. (let ((l org-todo-state-tags-triggers)
  11274. changes)
  11275. (when (or (not state) (equal state ""))
  11276. (setq changes (append changes (cdr (assoc "" l)))))
  11277. (when (and (stringp state) (> (length state) 0))
  11278. (setq changes (append changes (cdr (assoc state l)))))
  11279. (when (member state org-not-done-keywords)
  11280. (setq changes (append changes (cdr (assoc 'todo l)))))
  11281. (when (member state org-done-keywords)
  11282. (setq changes (append changes (cdr (assoc 'done l)))))
  11283. (dolist (c changes)
  11284. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  11285. (defun org-local-logging (value)
  11286. "Get logging settings from a property VALUE."
  11287. (let* (words w a)
  11288. ;; directly set the variables, they are already local.
  11289. (setq org-log-done nil
  11290. org-log-repeat nil
  11291. org-todo-log-states nil)
  11292. (setq words (org-split-string value))
  11293. (while (setq w (pop words))
  11294. (cond
  11295. ((setq a (assoc w org-startup-options))
  11296. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  11297. (set (nth 1 a) (nth 2 a))))
  11298. ((setq a (org-extract-log-state-settings w))
  11299. (and (member (car a) org-todo-keywords-1)
  11300. (push a org-todo-log-states)))))))
  11301. (defun org-get-todo-sequence-head (kwd)
  11302. "Return the head of the TODO sequence to which KWD belongs.
  11303. If KWD is not set, check if there is a text property remembering the
  11304. right sequence."
  11305. (let (p)
  11306. (cond
  11307. ((not kwd)
  11308. (or (get-text-property (point-at-bol) 'org-todo-head)
  11309. (progn
  11310. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  11311. nil (point-at-eol)))
  11312. (get-text-property p 'org-todo-head))))
  11313. ((not (member kwd org-todo-keywords-1))
  11314. (car org-todo-keywords-1))
  11315. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  11316. (defun org-fast-todo-selection ()
  11317. "Fast TODO keyword selection with single keys.
  11318. Returns the new TODO keyword, or nil if no state change should occur."
  11319. (let* ((fulltable org-todo-key-alist)
  11320. (done-keywords org-done-keywords) ;; needed for the faces.
  11321. (maxlen (apply 'max (mapcar
  11322. (lambda (x)
  11323. (if (stringp (car x)) (string-width (car x)) 0))
  11324. fulltable)))
  11325. (expert nil)
  11326. (fwidth (+ maxlen 3 1 3))
  11327. (ncol (/ (- (window-width) 4) fwidth))
  11328. tg cnt e c tbl
  11329. groups ingroup)
  11330. (save-excursion
  11331. (save-window-excursion
  11332. (if expert
  11333. (set-buffer (get-buffer-create " *Org todo*"))
  11334. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  11335. (erase-buffer)
  11336. (org-set-local 'org-done-keywords done-keywords)
  11337. (setq tbl fulltable cnt 0)
  11338. (while (setq e (pop tbl))
  11339. (cond
  11340. ((equal e '(:startgroup))
  11341. (push '() groups) (setq ingroup t)
  11342. (when (not (= cnt 0))
  11343. (setq cnt 0)
  11344. (insert "\n"))
  11345. (insert "{ "))
  11346. ((equal e '(:endgroup))
  11347. (setq ingroup nil cnt 0)
  11348. (insert "}\n"))
  11349. ((equal e '(:newline))
  11350. (when (not (= cnt 0))
  11351. (setq cnt 0)
  11352. (insert "\n")
  11353. (setq e (car tbl))
  11354. (while (equal (car tbl) '(:newline))
  11355. (insert "\n")
  11356. (setq tbl (cdr tbl)))))
  11357. (t
  11358. (setq tg (car e) c (cdr e))
  11359. (if ingroup (push tg (car groups)))
  11360. (setq tg (org-add-props tg nil 'face
  11361. (org-get-todo-face tg)))
  11362. (if (and (= cnt 0) (not ingroup)) (insert " "))
  11363. (insert "[" c "] " tg (make-string
  11364. (- fwidth 4 (length tg)) ?\ ))
  11365. (when (= (setq cnt (1+ cnt)) ncol)
  11366. (insert "\n")
  11367. (if ingroup (insert " "))
  11368. (setq cnt 0)))))
  11369. (insert "\n")
  11370. (goto-char (point-min))
  11371. (if (not expert) (org-fit-window-to-buffer))
  11372. (message "[a-z..]:Set [SPC]:clear")
  11373. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  11374. (cond
  11375. ((or (= c ?\C-g)
  11376. (and (= c ?q) (not (rassoc c fulltable))))
  11377. (setq quit-flag t))
  11378. ((= c ?\ ) nil)
  11379. ((setq e (rassoc c fulltable) tg (car e))
  11380. tg)
  11381. (t (setq quit-flag t)))))))
  11382. (defun org-entry-is-todo-p ()
  11383. (member (org-get-todo-state) org-not-done-keywords))
  11384. (defun org-entry-is-done-p ()
  11385. (member (org-get-todo-state) org-done-keywords))
  11386. (defun org-get-todo-state ()
  11387. (save-excursion
  11388. (org-back-to-heading t)
  11389. (and (looking-at org-todo-line-regexp)
  11390. (match-end 2)
  11391. (match-string 2))))
  11392. (defun org-at-date-range-p (&optional inactive-ok)
  11393. "Is the cursor inside a date range?"
  11394. (interactive)
  11395. (save-excursion
  11396. (catch 'exit
  11397. (let ((pos (point)))
  11398. (skip-chars-backward "^[<\r\n")
  11399. (skip-chars-backward "<[")
  11400. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11401. (>= (match-end 0) pos)
  11402. (throw 'exit t))
  11403. (skip-chars-backward "^<[\r\n")
  11404. (skip-chars-backward "<[")
  11405. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11406. (>= (match-end 0) pos)
  11407. (throw 'exit t)))
  11408. nil)))
  11409. (defun org-get-repeat (&optional tagline)
  11410. "Check if there is a deadline/schedule with repeater in this entry."
  11411. (save-match-data
  11412. (save-excursion
  11413. (org-back-to-heading t)
  11414. (and (re-search-forward (if tagline
  11415. (concat tagline "\\s-*" org-repeat-re)
  11416. org-repeat-re)
  11417. (org-entry-end-position) t)
  11418. (match-string-no-properties 1)))))
  11419. (defvar org-last-changed-timestamp)
  11420. (defvar org-last-inserted-timestamp)
  11421. (defvar org-log-post-message)
  11422. (defvar org-log-note-purpose)
  11423. (defvar org-log-note-how)
  11424. (defvar org-log-note-extra)
  11425. (defun org-auto-repeat-maybe (done-word)
  11426. "Check if the current headline contains a repeated deadline/schedule.
  11427. If yes, set TODO state back to what it was and change the base date
  11428. of repeating deadline/scheduled time stamps to new date.
  11429. This function is run automatically after each state change to a DONE state."
  11430. ;; last-state is dynamically scoped into this function
  11431. (let* ((repeat (org-get-repeat))
  11432. (aa (assoc org-last-state org-todo-kwd-alist))
  11433. (interpret (nth 1 aa))
  11434. (head (nth 2 aa))
  11435. (whata '(("h" . hour) ("d" . day) ("m" . month) ("y" . year)))
  11436. (msg "Entry repeats: ")
  11437. (org-log-done nil)
  11438. (org-todo-log-states nil)
  11439. re type n what ts time to-state)
  11440. (when repeat
  11441. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  11442. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  11443. org-todo-repeat-to-state))
  11444. (unless (and to-state (member to-state org-todo-keywords-1))
  11445. (setq to-state (if (eq interpret 'type) org-last-state head)))
  11446. (org-todo to-state)
  11447. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  11448. (org-entry-put nil "LAST_REPEAT" (format-time-string
  11449. (org-time-stamp-format t t))))
  11450. (when org-log-repeat
  11451. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  11452. (memq 'org-add-log-note post-command-hook))
  11453. ;; OK, we are already setup for some record
  11454. (if (eq org-log-repeat 'note)
  11455. ;; make sure we take a note, not only a time stamp
  11456. (setq org-log-note-how 'note))
  11457. ;; Set up for taking a record
  11458. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  11459. org-last-state
  11460. 'findpos org-log-repeat)))
  11461. (org-back-to-heading t)
  11462. (org-add-planning-info nil nil 'closed)
  11463. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11464. org-deadline-time-regexp "\\)\\|\\("
  11465. org-ts-regexp "\\)"))
  11466. (while (re-search-forward
  11467. re (save-excursion (outline-next-heading) (point)) t)
  11468. (setq type (if (match-end 1) org-scheduled-string
  11469. (if (match-end 3) org-deadline-string "Plain:"))
  11470. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  11471. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts)
  11472. (setq n (string-to-number (match-string 2 ts))
  11473. what (match-string 3 ts))
  11474. (if (equal what "w") (setq n (* n 7) what "d"))
  11475. (if (and (equal what "h") (not (string-match "[0-9]\\{1,2\\}:[0-9]\\{2\\}" ts)))
  11476. (error "Cannot repeat in Repeat in %d hour(s) because no hour has been set" n))
  11477. ;; Preparation, see if we need to modify the start date for the change
  11478. (when (match-end 1)
  11479. (setq time (save-match-data (org-time-string-to-time ts)))
  11480. (cond
  11481. ((equal (match-string 1 ts) ".")
  11482. ;; Shift starting date to today
  11483. (org-timestamp-change
  11484. (- (org-today) (time-to-days time))
  11485. 'day))
  11486. ((equal (match-string 1 ts) "+")
  11487. (let ((nshiftmax 10) (nshift 0))
  11488. (while (or (= nshift 0)
  11489. (<= (time-to-days time)
  11490. (time-to-days (current-time))))
  11491. (when (= (incf nshift) nshiftmax)
  11492. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  11493. (error "Abort")))
  11494. (org-timestamp-change n (cdr (assoc what whata)))
  11495. (org-at-timestamp-p t)
  11496. (setq ts (match-string 1))
  11497. (setq time (save-match-data (org-time-string-to-time ts)))))
  11498. (org-timestamp-change (- n) (cdr (assoc what whata)))
  11499. ;; rematch, so that we have everything in place for the real shift
  11500. (org-at-timestamp-p t)
  11501. (setq ts (match-string 1))
  11502. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts))))
  11503. (save-excursion (org-timestamp-change n (cdr (assoc what whata)) nil t))
  11504. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  11505. (setq org-log-post-message msg)
  11506. (message "%s" msg))))
  11507. (defun org-show-todo-tree (arg)
  11508. "Make a compact tree which shows all headlines marked with TODO.
  11509. The tree will show the lines where the regexp matches, and all higher
  11510. headlines above the match.
  11511. With a \\[universal-argument] prefix, prompt for a regexp to match.
  11512. With a numeric prefix N, construct a sparse tree for the Nth element
  11513. of `org-todo-keywords-1'."
  11514. (interactive "P")
  11515. (let ((case-fold-search nil)
  11516. (kwd-re
  11517. (cond ((null arg) org-not-done-regexp)
  11518. ((equal arg '(4))
  11519. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  11520. (mapcar 'list org-todo-keywords-1))))
  11521. (concat "\\("
  11522. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  11523. "\\)\\>")))
  11524. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  11525. (regexp-quote (nth (1- (prefix-numeric-value arg))
  11526. org-todo-keywords-1)))
  11527. (t (error "Invalid prefix argument: %s" arg)))))
  11528. (message "%d TODO entries found"
  11529. (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
  11530. (defun org-deadline (arg &optional time)
  11531. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  11532. With one universal prefix argument, remove any deadline from the item.
  11533. With two universal prefix arguments, prompt for a warning delay.
  11534. With argument TIME, set the deadline at the corresponding date. TIME
  11535. can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11536. (interactive "P")
  11537. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11538. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11539. 'region-start-level 'region))
  11540. org-loop-over-headlines-in-active-region)
  11541. (org-map-entries
  11542. `(org-deadline ',arg ,time)
  11543. org-loop-over-headlines-in-active-region
  11544. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11545. (let* ((old-date (org-entry-get nil "DEADLINE"))
  11546. (old-date-time (if old-date (org-time-string-to-time old-date)))
  11547. (repeater (and old-date
  11548. (string-match
  11549. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11550. old-date)
  11551. (match-string 1 old-date))))
  11552. (cond
  11553. ((equal arg '(4))
  11554. (when (and old-date org-log-redeadline)
  11555. (org-add-log-setup 'deldeadline nil old-date 'findpos
  11556. org-log-redeadline))
  11557. (org-remove-timestamp-with-keyword org-deadline-string)
  11558. (message "Item no longer has a deadline."))
  11559. ((equal arg '(16))
  11560. (save-excursion
  11561. (if (re-search-forward
  11562. org-deadline-time-regexp
  11563. (save-excursion (outline-next-heading) (point)) t)
  11564. (let* ((rpl0 (match-string 1))
  11565. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  11566. (replace-match
  11567. (concat org-deadline-string
  11568. " <" rpl
  11569. (format " -%dd"
  11570. (abs
  11571. (- (time-to-days
  11572. (save-match-data
  11573. (org-read-date nil t nil "Warn starting from" old-date-time)))
  11574. (time-to-days old-date-time))))
  11575. ">") t t))
  11576. (user-error "No deadline information to update"))))
  11577. (t
  11578. (org-add-planning-info 'deadline time 'closed)
  11579. (when (and old-date org-log-redeadline
  11580. (not (equal old-date
  11581. (substring org-last-inserted-timestamp 1 -1))))
  11582. (org-add-log-setup 'redeadline nil old-date 'findpos
  11583. org-log-redeadline))
  11584. (when repeater
  11585. (save-excursion
  11586. (org-back-to-heading t)
  11587. (when (re-search-forward (concat org-deadline-string " "
  11588. org-last-inserted-timestamp)
  11589. (save-excursion
  11590. (outline-next-heading) (point)) t)
  11591. (goto-char (1- (match-end 0)))
  11592. (insert " " repeater)
  11593. (setq org-last-inserted-timestamp
  11594. (concat (substring org-last-inserted-timestamp 0 -1)
  11595. " " repeater
  11596. (substring org-last-inserted-timestamp -1))))))
  11597. (message "Deadline on %s" org-last-inserted-timestamp))))))
  11598. (defun org-schedule (arg &optional time)
  11599. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  11600. With one universal prefix argument, remove any scheduling date from the item.
  11601. With two universal prefix arguments, prompt for a delay cookie.
  11602. With argument TIME, scheduled at the corresponding date. TIME can
  11603. either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11604. (interactive "P")
  11605. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11606. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11607. 'region-start-level 'region))
  11608. org-loop-over-headlines-in-active-region)
  11609. (org-map-entries
  11610. `(org-schedule ',arg ,time)
  11611. org-loop-over-headlines-in-active-region
  11612. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11613. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  11614. (old-date-time (if old-date (org-time-string-to-time old-date)))
  11615. (repeater (and old-date
  11616. (string-match
  11617. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11618. old-date)
  11619. (match-string 1 old-date))))
  11620. (cond
  11621. ((equal arg '(4))
  11622. (progn
  11623. (when (and old-date org-log-reschedule)
  11624. (org-add-log-setup 'delschedule nil old-date 'findpos
  11625. org-log-reschedule))
  11626. (org-remove-timestamp-with-keyword org-scheduled-string)
  11627. (message "Item is no longer scheduled.")))
  11628. ((equal arg '(16))
  11629. (save-excursion
  11630. (if (re-search-forward
  11631. org-scheduled-time-regexp
  11632. (save-excursion (outline-next-heading) (point)) t)
  11633. (let* ((rpl0 (match-string 1))
  11634. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  11635. (replace-match
  11636. (concat org-scheduled-string
  11637. " <" rpl
  11638. (format " -%dd"
  11639. (abs
  11640. (- (time-to-days
  11641. (save-match-data
  11642. (org-read-date nil t nil "Delay until" old-date-time)))
  11643. (time-to-days old-date-time))))
  11644. ">") t t))
  11645. (user-error "No scheduled information to update"))))
  11646. (t
  11647. (org-add-planning-info 'scheduled time 'closed)
  11648. (when (and old-date org-log-reschedule
  11649. (not (equal old-date
  11650. (substring org-last-inserted-timestamp 1 -1))))
  11651. (org-add-log-setup 'reschedule nil old-date 'findpos
  11652. org-log-reschedule))
  11653. (when repeater
  11654. (save-excursion
  11655. (org-back-to-heading t)
  11656. (when (re-search-forward (concat org-scheduled-string " "
  11657. org-last-inserted-timestamp)
  11658. (save-excursion
  11659. (outline-next-heading) (point)) t)
  11660. (goto-char (1- (match-end 0)))
  11661. (insert " " repeater)
  11662. (setq org-last-inserted-timestamp
  11663. (concat (substring org-last-inserted-timestamp 0 -1)
  11664. " " repeater
  11665. (substring org-last-inserted-timestamp -1))))))
  11666. (message "Scheduled to %s" org-last-inserted-timestamp))))))
  11667. (defun org-get-scheduled-time (pom &optional inherit)
  11668. "Get the scheduled time as a time tuple, of a format suitable
  11669. for calling org-schedule with, or if there is no scheduling,
  11670. returns nil."
  11671. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  11672. (when time
  11673. (apply 'encode-time (org-parse-time-string time)))))
  11674. (defun org-get-deadline-time (pom &optional inherit)
  11675. "Get the deadline as a time tuple, of a format suitable for
  11676. calling org-deadline with, or if there is no scheduling, returns
  11677. nil."
  11678. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  11679. (when time
  11680. (apply 'encode-time (org-parse-time-string time)))))
  11681. (defun org-remove-timestamp-with-keyword (keyword)
  11682. "Remove all time stamps with KEYWORD in the current entry."
  11683. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  11684. beg)
  11685. (save-excursion
  11686. (org-back-to-heading t)
  11687. (setq beg (point))
  11688. (outline-next-heading)
  11689. (while (re-search-backward re beg t)
  11690. (replace-match "")
  11691. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  11692. (equal (char-before) ?\ ))
  11693. (backward-delete-char 1)
  11694. (if (string-match "^[ \t]*$" (buffer-substring
  11695. (point-at-bol) (point-at-eol)))
  11696. (delete-region (point-at-bol)
  11697. (min (point-max) (1+ (point-at-eol))))))))))
  11698. (defun org-add-planning-info (what &optional time &rest remove)
  11699. "Insert new timestamp with keyword in the line directly after the headline.
  11700. WHAT indicates what kind of time stamp to add. TIME indicates the time to use.
  11701. If non is given, the user is prompted for a date.
  11702. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  11703. be removed."
  11704. (interactive)
  11705. (let (org-time-was-given org-end-time-was-given ts
  11706. end default-time default-input)
  11707. (catch 'exit
  11708. (when (and (memq what '(scheduled deadline))
  11709. (or (not time)
  11710. (and (stringp time)
  11711. (string-match "^[-+]+[0-9]" time))))
  11712. ;; Try to get a default date/time from existing timestamp
  11713. (save-excursion
  11714. (org-back-to-heading t)
  11715. (setq end (save-excursion (outline-next-heading) (point)))
  11716. (when (re-search-forward (if (eq what 'scheduled)
  11717. org-scheduled-time-regexp
  11718. org-deadline-time-regexp)
  11719. end t)
  11720. (setq ts (match-string 1)
  11721. default-time
  11722. (apply 'encode-time (org-parse-time-string ts))
  11723. default-input (and ts (org-get-compact-tod ts))))))
  11724. (when what
  11725. (setq time
  11726. (if (stringp time)
  11727. ;; This is a string (relative or absolute), set proper date
  11728. (apply 'encode-time
  11729. (org-read-date-analyze
  11730. time default-time (decode-time default-time)))
  11731. ;; If necessary, get the time from the user
  11732. (or time (org-read-date nil 'to-time nil nil
  11733. default-time default-input)))))
  11734. (when (and org-insert-labeled-timestamps-at-point
  11735. (member what '(scheduled deadline)))
  11736. (insert
  11737. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  11738. (org-insert-time-stamp time org-time-was-given
  11739. nil nil nil (list org-end-time-was-given))
  11740. (setq what nil))
  11741. (save-excursion
  11742. (save-restriction
  11743. (let (col list elt ts buffer-invisibility-spec)
  11744. (org-back-to-heading t)
  11745. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"))
  11746. (goto-char (match-end 1))
  11747. (setq col (current-column))
  11748. (goto-char (match-end 0))
  11749. (if (eobp) (insert "\n") (forward-char 1))
  11750. (when (and (not what)
  11751. (not (looking-at
  11752. (concat "[ \t]*"
  11753. org-keyword-time-not-clock-regexp))))
  11754. ;; Nothing to add, nothing to remove...... :-)
  11755. (throw 'exit nil))
  11756. (if (and (not (looking-at org-outline-regexp))
  11757. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  11758. "[^\r\n]*"))
  11759. (not (equal (match-string 1) org-clock-string)))
  11760. (narrow-to-region (match-beginning 0) (match-end 0))
  11761. (insert-before-markers "\n")
  11762. (backward-char 1)
  11763. (narrow-to-region (point) (point))
  11764. (and org-adapt-indentation (org-indent-to-column col)))
  11765. ;; Check if we have to remove something.
  11766. (setq list (cons what remove))
  11767. (while list
  11768. (setq elt (pop list))
  11769. (when (or (and (eq elt 'scheduled)
  11770. (re-search-forward org-scheduled-time-regexp nil t))
  11771. (and (eq elt 'deadline)
  11772. (re-search-forward org-deadline-time-regexp nil t))
  11773. (and (eq elt 'closed)
  11774. (re-search-forward org-closed-time-regexp nil t)))
  11775. (replace-match "")
  11776. (if (looking-at "--+<[^>]+>") (replace-match ""))))
  11777. (and (looking-at "[ \t]+") (replace-match ""))
  11778. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  11779. (when what
  11780. (insert
  11781. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  11782. (cond ((eq what 'scheduled) org-scheduled-string)
  11783. ((eq what 'deadline) org-deadline-string)
  11784. ((eq what 'closed) org-closed-string))
  11785. " ")
  11786. (setq ts (org-insert-time-stamp
  11787. time
  11788. (or org-time-was-given
  11789. (and (eq what 'closed) org-log-done-with-time))
  11790. (eq what 'closed)
  11791. nil nil (list org-end-time-was-given)))
  11792. (insert
  11793. (if (not (or (bolp) (eq (char-before) ?\ )
  11794. (memq (char-after) '(32 10))
  11795. (eobp))) " " ""))
  11796. (end-of-line 1))
  11797. (goto-char (point-min))
  11798. (widen)
  11799. (if (and (looking-at "[ \t]*\n")
  11800. (equal (char-before) ?\n))
  11801. (delete-region (1- (point)) (point-at-eol)))
  11802. ts))))))
  11803. (defvar org-log-note-marker (make-marker))
  11804. (defvar org-log-note-purpose nil)
  11805. (defvar org-log-note-state nil)
  11806. (defvar org-log-note-previous-state nil)
  11807. (defvar org-log-note-how nil)
  11808. (defvar org-log-note-extra nil)
  11809. (defvar org-log-note-window-configuration nil)
  11810. (defvar org-log-note-return-to (make-marker))
  11811. (defvar org-log-note-effective-time nil
  11812. "Remembered current time so that dynamically scoped
  11813. `org-extend-today-until' affects tha timestamps in state change
  11814. log")
  11815. (defvar org-log-post-message nil
  11816. "Message to be displayed after a log note has been stored.
  11817. The auto-repeater uses this.")
  11818. (defun org-add-note ()
  11819. "Add a note to the current entry.
  11820. This is done in the same way as adding a state change note."
  11821. (interactive)
  11822. (org-add-log-setup 'note nil nil 'findpos nil))
  11823. (defvar org-property-end-re)
  11824. (defun org-add-log-setup (&optional purpose state prev-state
  11825. findpos how extra)
  11826. "Set up the post command hook to take a note.
  11827. If this is about to TODO state change, the new state is expected in STATE.
  11828. When FINDPOS is non-nil, find the correct position for the note in
  11829. the current entry. If not, assume that it can be inserted at point.
  11830. HOW is an indicator what kind of note should be created.
  11831. EXTRA is additional text that will be inserted into the notes buffer."
  11832. (let* ((org-log-into-drawer (org-log-into-drawer))
  11833. (drawer (cond ((stringp org-log-into-drawer)
  11834. org-log-into-drawer)
  11835. (org-log-into-drawer "LOGBOOK"))))
  11836. (save-restriction
  11837. (save-excursion
  11838. (when findpos
  11839. (org-back-to-heading t)
  11840. (narrow-to-region (point) (save-excursion
  11841. (outline-next-heading) (point)))
  11842. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"
  11843. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  11844. "[^\r\n]*\\)?"))
  11845. (goto-char (match-end 0))
  11846. (cond
  11847. (drawer
  11848. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  11849. nil t)
  11850. (progn
  11851. (goto-char (match-end 0))
  11852. (or org-log-states-order-reversed
  11853. (and (re-search-forward org-property-end-re nil t)
  11854. (goto-char (1- (match-beginning 0))))))
  11855. (insert "\n:" drawer ":\n:END:")
  11856. (beginning-of-line 0)
  11857. (org-indent-line)
  11858. (beginning-of-line 2)
  11859. (org-indent-line)
  11860. (end-of-line 0)))
  11861. ((and org-log-state-notes-insert-after-drawers
  11862. (save-excursion
  11863. (forward-line) (looking-at org-drawer-regexp)))
  11864. (forward-line)
  11865. (while (looking-at org-drawer-regexp)
  11866. (goto-char (match-end 0))
  11867. (re-search-forward org-property-end-re (point-max) t)
  11868. (forward-line))
  11869. (forward-line -1)))
  11870. (unless org-log-states-order-reversed
  11871. (and (= (char-after) ?\n) (forward-char 1))
  11872. (org-skip-over-state-notes)
  11873. (skip-chars-backward " \t\n\r")))
  11874. (move-marker org-log-note-marker (point))
  11875. (setq org-log-note-purpose purpose
  11876. org-log-note-state state
  11877. org-log-note-previous-state prev-state
  11878. org-log-note-how how
  11879. org-log-note-extra extra
  11880. org-log-note-effective-time (org-current-effective-time))
  11881. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  11882. (defun org-skip-over-state-notes ()
  11883. "Skip past the list of State notes in an entry."
  11884. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  11885. (when (ignore-errors (goto-char (org-in-item-p)))
  11886. (let* ((struct (org-list-struct))
  11887. (prevs (org-list-prevs-alist struct)))
  11888. (while (looking-at "[ \t]*- State")
  11889. (goto-char (or (org-list-get-next-item (point) struct prevs)
  11890. (org-list-get-item-end (point) struct)))))))
  11891. (defun org-add-log-note (&optional purpose)
  11892. "Pop up a window for taking a note, and add this note later at point."
  11893. (remove-hook 'post-command-hook 'org-add-log-note)
  11894. (setq org-log-note-window-configuration (current-window-configuration))
  11895. (delete-other-windows)
  11896. (move-marker org-log-note-return-to (point))
  11897. (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
  11898. (goto-char org-log-note-marker)
  11899. (org-switch-to-buffer-other-window "*Org Note*")
  11900. (erase-buffer)
  11901. (if (memq org-log-note-how '(time state))
  11902. (let (current-prefix-arg) (org-store-log-note))
  11903. (let ((org-inhibit-startup t)) (org-mode))
  11904. (insert (format "# Insert note for %s.
  11905. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  11906. (cond
  11907. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  11908. ((eq org-log-note-purpose 'done) "closed todo item")
  11909. ((eq org-log-note-purpose 'state)
  11910. (format "state change from \"%s\" to \"%s\""
  11911. (or org-log-note-previous-state "")
  11912. (or org-log-note-state "")))
  11913. ((eq org-log-note-purpose 'reschedule)
  11914. "rescheduling")
  11915. ((eq org-log-note-purpose 'delschedule)
  11916. "no longer scheduled")
  11917. ((eq org-log-note-purpose 'redeadline)
  11918. "changing deadline")
  11919. ((eq org-log-note-purpose 'deldeadline)
  11920. "removing deadline")
  11921. ((eq org-log-note-purpose 'refile)
  11922. "refiling")
  11923. ((eq org-log-note-purpose 'note)
  11924. "this entry")
  11925. (t (error "This should not happen")))))
  11926. (if org-log-note-extra (insert org-log-note-extra))
  11927. (org-set-local 'org-finish-function 'org-store-log-note)
  11928. (run-hooks 'org-log-buffer-setup-hook)))
  11929. (defvar org-note-abort nil) ; dynamically scoped
  11930. (defun org-store-log-note ()
  11931. "Finish taking a log note, and insert it to where it belongs."
  11932. (let ((txt (buffer-string)))
  11933. (kill-buffer (current-buffer))
  11934. (let ((note (cdr (assq org-log-note-purpose org-log-note-headings)))
  11935. lines ind bul)
  11936. (while (string-match "\\`# .*\n[ \t\n]*" txt)
  11937. (setq txt (replace-match "" t t txt)))
  11938. (if (string-match "\\s-+\\'" txt)
  11939. (setq txt (replace-match "" t t txt)))
  11940. (setq lines (org-split-string txt "\n"))
  11941. (when (and note (string-match "\\S-" note))
  11942. (setq note
  11943. (org-replace-escapes
  11944. note
  11945. (list (cons "%u" (user-login-name))
  11946. (cons "%U" user-full-name)
  11947. (cons "%t" (format-time-string
  11948. (org-time-stamp-format 'long 'inactive)
  11949. org-log-note-effective-time))
  11950. (cons "%T" (format-time-string
  11951. (org-time-stamp-format 'long nil)
  11952. org-log-note-effective-time))
  11953. (cons "%d" (format-time-string
  11954. (org-time-stamp-format nil 'inactive)
  11955. org-log-note-effective-time))
  11956. (cons "%D" (format-time-string
  11957. (org-time-stamp-format nil nil)
  11958. org-log-note-effective-time))
  11959. (cons "%s" (if org-log-note-state
  11960. (concat "\"" org-log-note-state "\"")
  11961. ""))
  11962. (cons "%S" (if org-log-note-previous-state
  11963. (concat "\"" org-log-note-previous-state "\"")
  11964. "\"\"")))))
  11965. (if lines (setq note (concat note " \\\\")))
  11966. (push note lines))
  11967. (when (or current-prefix-arg org-note-abort)
  11968. (when org-log-into-drawer
  11969. (org-remove-empty-drawer-at
  11970. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  11971. org-log-note-marker))
  11972. (setq lines nil))
  11973. (when lines
  11974. (with-current-buffer (marker-buffer org-log-note-marker)
  11975. (save-excursion
  11976. (goto-char org-log-note-marker)
  11977. (move-marker org-log-note-marker nil)
  11978. (end-of-line 1)
  11979. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  11980. (setq ind (save-excursion
  11981. (if (ignore-errors (goto-char (org-in-item-p)))
  11982. (let ((struct (org-list-struct)))
  11983. (org-list-get-ind
  11984. (org-list-get-top-point struct) struct))
  11985. (skip-chars-backward " \r\t\n")
  11986. (cond
  11987. ((and (org-at-heading-p)
  11988. org-adapt-indentation)
  11989. (1+ (org-current-level)))
  11990. ((org-at-heading-p) 0)
  11991. (t (org-get-indentation))))))
  11992. (setq bul (org-list-bullet-string "-"))
  11993. (org-indent-line-to ind)
  11994. (insert bul (pop lines))
  11995. (let ((ind-body (+ (length bul) ind)))
  11996. (while lines
  11997. (insert "\n")
  11998. (org-indent-line-to ind-body)
  11999. (insert (pop lines))))
  12000. (message "Note stored")
  12001. (org-back-to-heading t)
  12002. (org-cycle-hide-drawers 'children))))))
  12003. (set-window-configuration org-log-note-window-configuration)
  12004. (with-current-buffer (marker-buffer org-log-note-return-to)
  12005. (goto-char org-log-note-return-to))
  12006. (move-marker org-log-note-return-to nil)
  12007. (and org-log-post-message (message "%s" org-log-post-message)))
  12008. (defun org-remove-empty-drawer-at (drawer pos)
  12009. "Remove an empty drawer DRAWER at position POS.
  12010. POS may also be a marker."
  12011. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  12012. (save-excursion
  12013. (save-restriction
  12014. (widen)
  12015. (goto-char pos)
  12016. (if (org-in-regexp
  12017. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  12018. (replace-match ""))))))
  12019. (defvar org-ts-type nil)
  12020. (defun org-sparse-tree (&optional arg type)
  12021. "Create a sparse tree, prompt for the details.
  12022. This command can create sparse trees. You first need to select the type
  12023. of match used to create the tree:
  12024. t Show all TODO entries.
  12025. T Show entries with a specific TODO keyword.
  12026. m Show entries selected by a tags/property match.
  12027. p Enter a property name and its value (both with completion on existing
  12028. names/values) and show entries with that property.
  12029. r Show entries matching a regular expression (`/' can be used as well).
  12030. b Show deadlines and scheduled items before a date.
  12031. a Show deadlines and scheduled items after a date.
  12032. d Show deadlines due within `org-deadline-warning-days'.
  12033. D Show deadlines and scheduled items between a date range."
  12034. (interactive "P")
  12035. (let (ans kwd value ts-type)
  12036. (setq type (or type org-sparse-tree-default-date-type))
  12037. (setq org-ts-type type)
  12038. (message "Sparse tree: [/]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"
  12039. (cond ((eq type 'all) "all timestamps")
  12040. ((eq type 'scheduled) "only scheduled")
  12041. ((eq type 'deadline) "only deadline")
  12042. ((eq type 'active) "only active timestamps")
  12043. ((eq type 'inactive) "only inactive timestamps")
  12044. ((eq type 'scheduled-or-deadline) "scheduled/deadline")
  12045. (t "scheduled/deadline")))
  12046. (setq ans (read-char-exclusive))
  12047. (cond
  12048. ((equal ans ?c)
  12049. (org-sparse-tree arg (cadr (member type '(scheduled-or-deadline all scheduled deadline active inactive)))))
  12050. ((equal ans ?d)
  12051. (call-interactively 'org-check-deadlines))
  12052. ((equal ans ?b)
  12053. (call-interactively 'org-check-before-date))
  12054. ((equal ans ?a)
  12055. (call-interactively 'org-check-after-date))
  12056. ((equal ans ?D)
  12057. (call-interactively 'org-check-dates-range))
  12058. ((equal ans ?t)
  12059. (call-interactively 'org-show-todo-tree))
  12060. ((equal ans ?T)
  12061. (org-show-todo-tree '(4)))
  12062. ((member ans '(?T ?m))
  12063. (call-interactively 'org-match-sparse-tree))
  12064. ((member ans '(?p ?P))
  12065. (setq kwd (org-icompleting-read "Property: "
  12066. (mapcar 'list (org-buffer-property-keys))))
  12067. (setq value (org-icompleting-read "Value: "
  12068. (mapcar 'list (org-property-values kwd))))
  12069. (unless (string-match "\\`{.*}\\'" value)
  12070. (setq value (concat "\"" value "\"")))
  12071. (org-match-sparse-tree arg (concat kwd "=" value)))
  12072. ((member ans '(?r ?R ?/))
  12073. (call-interactively 'org-occur))
  12074. (t (error "No such sparse tree command \"%c\"" ans)))))
  12075. (defvar org-occur-highlights nil
  12076. "List of overlays used for occur matches.")
  12077. (make-variable-buffer-local 'org-occur-highlights)
  12078. (defvar org-occur-parameters nil
  12079. "Parameters of the active org-occur calls.
  12080. This is a list, each call to org-occur pushes as cons cell,
  12081. containing the regular expression and the callback, onto the list.
  12082. The list can contain several entries if `org-occur' has been called
  12083. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  12084. will only contain one set of parameters. When the highlights are
  12085. removed (for example with `C-c C-c', or with the next edit (depending
  12086. on `org-remove-highlights-with-change'), this variable is emptied
  12087. as well.")
  12088. (make-variable-buffer-local 'org-occur-parameters)
  12089. (defun org-occur (regexp &optional keep-previous callback)
  12090. "Make a compact tree which shows all matches of REGEXP.
  12091. The tree will show the lines where the regexp matches, and all higher
  12092. headlines above the match. It will also show the heading after the match,
  12093. to make sure editing the matching entry is easy.
  12094. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  12095. call to `org-occur' will be kept, to allow stacking of calls to this
  12096. command.
  12097. If CALLBACK is non-nil, it is a function which is called to confirm
  12098. that the match should indeed be shown."
  12099. (interactive "sRegexp: \nP")
  12100. (when (equal regexp "")
  12101. (error "Regexp cannot be empty"))
  12102. (unless keep-previous
  12103. (org-remove-occur-highlights nil nil t))
  12104. (push (cons regexp callback) org-occur-parameters)
  12105. (let ((cnt 0))
  12106. (save-excursion
  12107. (goto-char (point-min))
  12108. (if (or (not keep-previous) ; do not want to keep
  12109. (not org-occur-highlights)) ; no previous matches
  12110. ;; hide everything
  12111. (org-overview))
  12112. (while (re-search-forward regexp nil t)
  12113. (when (or (not callback)
  12114. (save-match-data (funcall callback)))
  12115. (setq cnt (1+ cnt))
  12116. (when org-highlight-sparse-tree-matches
  12117. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  12118. (org-show-context 'occur-tree))))
  12119. (when org-remove-highlights-with-change
  12120. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  12121. nil 'local))
  12122. (unless org-sparse-tree-open-archived-trees
  12123. (org-hide-archived-subtrees (point-min) (point-max)))
  12124. (run-hooks 'org-occur-hook)
  12125. (if (org-called-interactively-p 'interactive)
  12126. (message "%d match(es) for regexp %s" cnt regexp))
  12127. cnt))
  12128. (defun org-occur-next-match (&optional n reset)
  12129. "Function for `next-error-function' to find sparse tree matches.
  12130. N is the number of matches to move, when negative move backwards.
  12131. RESET is entirely ignored - this function always goes back to the
  12132. starting point when no match is found."
  12133. (let* ((limit (if (< n 0) (point-min) (point-max)))
  12134. (search-func (if (< n 0)
  12135. 'previous-single-char-property-change
  12136. 'next-single-char-property-change))
  12137. (n (abs n))
  12138. (pos (point))
  12139. p1)
  12140. (catch 'exit
  12141. (while (setq p1 (funcall search-func (point) 'org-type))
  12142. (when (equal p1 limit)
  12143. (goto-char pos)
  12144. (error "No more matches"))
  12145. (when (equal (get-char-property p1 'org-type) 'org-occur)
  12146. (setq n (1- n))
  12147. (when (= n 0)
  12148. (goto-char p1)
  12149. (throw 'exit (point))))
  12150. (goto-char p1))
  12151. (goto-char p1)
  12152. (error "No more matches"))))
  12153. (defun org-show-context (&optional key)
  12154. "Make sure point and context are visible.
  12155. How much context is shown depends upon the variables
  12156. `org-show-hierarchy-above', `org-show-following-heading',
  12157. `org-show-entry-below' and `org-show-siblings'."
  12158. (let ((heading-p (org-at-heading-p t))
  12159. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  12160. (following-p (org-get-alist-option org-show-following-heading key))
  12161. (entry-p (org-get-alist-option org-show-entry-below key))
  12162. (siblings-p (org-get-alist-option org-show-siblings key)))
  12163. ;; Show heading or entry text
  12164. (if (and heading-p (not entry-p))
  12165. (org-flag-heading nil) ; only show the heading
  12166. (and (or entry-p (outline-invisible-p) (org-invisible-p2))
  12167. (org-show-hidden-entry))) ; show entire entry
  12168. (when following-p
  12169. ;; Show next sibling, or heading below text
  12170. (save-excursion
  12171. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  12172. (org-flag-heading nil))))
  12173. (when siblings-p (org-show-siblings))
  12174. (when hierarchy-p
  12175. ;; show all higher headings, possibly with siblings
  12176. (save-excursion
  12177. (while (and (condition-case nil
  12178. (progn (org-up-heading-all 1) t)
  12179. (error nil))
  12180. (not (bobp)))
  12181. (org-flag-heading nil)
  12182. (when siblings-p (org-show-siblings)))))
  12183. (unless (eq key 'agenda) (org-fix-ellipsis-at-bol))))
  12184. (defvar org-reveal-start-hook nil
  12185. "Hook run before revealing a location.")
  12186. (defun org-reveal (&optional siblings)
  12187. "Show current entry, hierarchy above it, and the following headline.
  12188. This can be used to show a consistent set of context around locations
  12189. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  12190. not t for the search context.
  12191. With optional argument SIBLINGS, on each level of the hierarchy all
  12192. siblings are shown. This repairs the tree structure to what it would
  12193. look like when opened with hierarchical calls to `org-cycle'.
  12194. With double optional argument \\[universal-argument] \\[universal-argument], \
  12195. go to the parent and show the
  12196. entire tree."
  12197. (interactive "P")
  12198. (run-hooks 'org-reveal-start-hook)
  12199. (let ((org-show-hierarchy-above t)
  12200. (org-show-following-heading t)
  12201. (org-show-siblings (if siblings t org-show-siblings)))
  12202. (org-show-context nil))
  12203. (when (equal siblings '(16))
  12204. (save-excursion
  12205. (when (org-up-heading-safe)
  12206. (org-show-subtree)
  12207. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  12208. (defun org-highlight-new-match (beg end)
  12209. "Highlight from BEG to END and mark the highlight is an occur headline."
  12210. (let ((ov (make-overlay beg end)))
  12211. (overlay-put ov 'face 'secondary-selection)
  12212. (overlay-put ov 'org-type 'org-occur)
  12213. (push ov org-occur-highlights)))
  12214. (defun org-remove-occur-highlights (&optional beg end noremove)
  12215. "Remove the occur highlights from the buffer.
  12216. BEG and END are ignored. If NOREMOVE is nil, remove this function
  12217. from the `before-change-functions' in the current buffer."
  12218. (interactive)
  12219. (unless org-inhibit-highlight-removal
  12220. (mapc 'delete-overlay org-occur-highlights)
  12221. (setq org-occur-highlights nil)
  12222. (setq org-occur-parameters nil)
  12223. (unless noremove
  12224. (remove-hook 'before-change-functions
  12225. 'org-remove-occur-highlights 'local))))
  12226. ;;;; Priorities
  12227. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  12228. "Regular expression matching the priority indicator.")
  12229. (defvar org-remove-priority-next-time nil)
  12230. (defun org-priority-up ()
  12231. "Increase the priority of the current item."
  12232. (interactive)
  12233. (org-priority 'up))
  12234. (defun org-priority-down ()
  12235. "Decrease the priority of the current item."
  12236. (interactive)
  12237. (org-priority 'down))
  12238. (defun org-priority (&optional action show)
  12239. "Change the priority of an item.
  12240. ACTION can be `set', `up', `down', or a character."
  12241. (interactive "P")
  12242. (if (equal action '(4))
  12243. (org-show-priority)
  12244. (unless org-enable-priority-commands
  12245. (error "Priority commands are disabled"))
  12246. (setq action (or action 'set))
  12247. (let (current new news have remove)
  12248. (save-excursion
  12249. (org-back-to-heading t)
  12250. (if (looking-at org-priority-regexp)
  12251. (setq current (string-to-char (match-string 2))
  12252. have t))
  12253. (cond
  12254. ((eq action 'remove)
  12255. (setq remove t new ?\ ))
  12256. ((or (eq action 'set)
  12257. (if (featurep 'xemacs) (characterp action) (integerp action)))
  12258. (if (not (eq action 'set))
  12259. (setq new action)
  12260. (message "Priority %c-%c, SPC to remove: "
  12261. org-highest-priority org-lowest-priority)
  12262. (save-match-data
  12263. (setq new (read-char-exclusive))))
  12264. (if (and (= (upcase org-highest-priority) org-highest-priority)
  12265. (= (upcase org-lowest-priority) org-lowest-priority))
  12266. (setq new (upcase new)))
  12267. (cond ((equal new ?\ ) (setq remove t))
  12268. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  12269. (error "Priority must be between `%c' and `%c'"
  12270. org-highest-priority org-lowest-priority))))
  12271. ((eq action 'up)
  12272. (setq new (if have
  12273. (1- current) ; normal cycling
  12274. ;; last priority was empty
  12275. (if (eq last-command this-command)
  12276. org-lowest-priority ; wrap around empty to lowest
  12277. ;; default
  12278. (if org-priority-start-cycle-with-default
  12279. org-default-priority
  12280. (1- org-default-priority))))))
  12281. ((eq action 'down)
  12282. (setq new (if have
  12283. (1+ current) ; normal cycling
  12284. ;; last priority was empty
  12285. (if (eq last-command this-command)
  12286. org-highest-priority ; wrap around empty to highest
  12287. ;; default
  12288. (if org-priority-start-cycle-with-default
  12289. org-default-priority
  12290. (1+ org-default-priority))))))
  12291. (t (error "Invalid action")))
  12292. (if (or (< (upcase new) org-highest-priority)
  12293. (> (upcase new) org-lowest-priority))
  12294. (if (and (memq action '(up down))
  12295. (not have) (not (eq last-command this-command)))
  12296. ;; `new' is from default priority
  12297. (error
  12298. "The default can not be set, see `org-default-priority' why")
  12299. ;; normal cycling: `new' is beyond highest/lowest priority
  12300. ;; and is wrapped around to the empty priority
  12301. (setq remove t)))
  12302. (setq news (format "%c" new))
  12303. (if have
  12304. (if remove
  12305. (replace-match "" t t nil 1)
  12306. (replace-match news t t nil 2))
  12307. (if remove
  12308. (error "No priority cookie found in line")
  12309. (let ((case-fold-search nil))
  12310. (looking-at org-todo-line-regexp))
  12311. (if (match-end 2)
  12312. (progn
  12313. (goto-char (match-end 2))
  12314. (insert " [#" news "]"))
  12315. (goto-char (match-beginning 3))
  12316. (insert "[#" news "] "))))
  12317. (org-preserve-lc (org-set-tags nil 'align)))
  12318. (if remove
  12319. (message "Priority removed")
  12320. (message "Priority of current item set to %s" news)))))
  12321. (defun org-show-priority ()
  12322. "Show the priority of the current item.
  12323. This priority is composed of the main priority given with the [#A] cookies,
  12324. and by additional input from the age of a schedules or deadline entry."
  12325. (interactive)
  12326. (let ((pri (if (eq major-mode 'org-agenda-mode)
  12327. (org-get-at-bol 'priority)
  12328. (save-excursion
  12329. (save-match-data
  12330. (beginning-of-line)
  12331. (and (looking-at org-heading-regexp)
  12332. (org-get-priority (match-string 0))))))))
  12333. (message "Priority is %d" (if pri pri -1000))))
  12334. (defun org-get-priority (s)
  12335. "Find priority cookie and return priority."
  12336. (save-match-data
  12337. (if (functionp org-get-priority-function)
  12338. (funcall org-get-priority-function)
  12339. (if (not (string-match org-priority-regexp s))
  12340. (* 1000 (- org-lowest-priority org-default-priority))
  12341. (* 1000 (- org-lowest-priority
  12342. (string-to-char (match-string 2 s))))))))
  12343. ;;;; Tags
  12344. (defvar org-agenda-archives-mode)
  12345. (defvar org-map-continue-from nil
  12346. "Position from where mapping should continue.
  12347. Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
  12348. (defvar org-scanner-tags nil
  12349. "The current tag list while the tags scanner is running.")
  12350. (defvar org-trust-scanner-tags nil
  12351. "Should `org-get-tags-at' use the tags for the scanner.
  12352. This is for internal dynamical scoping only.
  12353. When this is non-nil, the function `org-get-tags-at' will return the value
  12354. of `org-scanner-tags' instead of building the list by itself. This
  12355. can lead to large speed-ups when the tags scanner is used in a file with
  12356. many entries, and when the list of tags is retrieved, for example to
  12357. obtain a list of properties. Building the tags list for each entry in such
  12358. a file becomes an N^2 operation - but with this variable set, it scales
  12359. as N.")
  12360. (defun org-scan-tags (action matcher todo-only &optional start-level)
  12361. "Sca headline tags with inheritance and produce output ACTION.
  12362. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  12363. or `agenda' to produce an entry list for an agenda view. It can also be
  12364. a Lisp form or a function that should be called at each matched headline, in
  12365. this case the return value is a list of all return values from these calls.
  12366. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  12367. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  12368. only lines with a not-done TODO keyword are included in the output.
  12369. This should be the same variable that was scoped into
  12370. and set by `org-make-tags-matcher' when it constructed MATCHER.
  12371. START-LEVEL can be a string with asterisks, reducing the scope to
  12372. headlines matching this string."
  12373. (require 'org-agenda)
  12374. (let* ((re (concat "^"
  12375. (if start-level
  12376. ;; Get the correct level to match
  12377. (concat "\\*\\{" (number-to-string start-level) "\\} ")
  12378. org-outline-regexp)
  12379. " *\\(\\<\\("
  12380. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  12381. (org-re "\\)\\>\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  12382. (props (list 'face 'default
  12383. 'done-face 'org-agenda-done
  12384. 'undone-face 'default
  12385. 'mouse-face 'highlight
  12386. 'org-not-done-regexp org-not-done-regexp
  12387. 'org-todo-regexp org-todo-regexp
  12388. 'org-complex-heading-regexp org-complex-heading-regexp
  12389. 'help-echo
  12390. (format "mouse-2 or RET jump to org file %s"
  12391. (abbreviate-file-name
  12392. (or (buffer-file-name (buffer-base-buffer))
  12393. (buffer-name (buffer-base-buffer)))))))
  12394. (case-fold-search nil)
  12395. (org-map-continue-from nil)
  12396. lspos tags tags-list
  12397. (tags-alist (list (cons 0 org-file-tags)))
  12398. (llast 0) rtn rtn1 level category i txt
  12399. todo marker entry priority)
  12400. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  12401. (setq action (list 'lambda nil action)))
  12402. (save-excursion
  12403. (goto-char (point-min))
  12404. (when (eq action 'sparse-tree)
  12405. (org-overview)
  12406. (org-remove-occur-highlights))
  12407. (while (re-search-forward re nil t)
  12408. (setq org-map-continue-from nil)
  12409. (catch :skip
  12410. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  12411. tags (if (match-end 4) (org-match-string-no-properties 4)))
  12412. (goto-char (setq lspos (match-beginning 0)))
  12413. (setq level (org-reduced-level (org-outline-level))
  12414. category (org-get-category))
  12415. (setq i llast llast level)
  12416. ;; remove tag lists from same and sublevels
  12417. (while (>= i level)
  12418. (when (setq entry (assoc i tags-alist))
  12419. (setq tags-alist (delete entry tags-alist)))
  12420. (setq i (1- i)))
  12421. ;; add the next tags
  12422. (when tags
  12423. (setq tags (org-split-string tags ":")
  12424. tags-alist
  12425. (cons (cons level tags) tags-alist)))
  12426. ;; compile tags for current headline
  12427. (setq tags-list
  12428. (if org-use-tag-inheritance
  12429. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  12430. tags)
  12431. org-scanner-tags tags-list)
  12432. (when org-use-tag-inheritance
  12433. (setcdr (car tags-alist)
  12434. (mapcar (lambda (x)
  12435. (setq x (copy-sequence x))
  12436. (org-add-prop-inherited x))
  12437. (cdar tags-alist))))
  12438. (when (and tags org-use-tag-inheritance
  12439. (or (not (eq t org-use-tag-inheritance))
  12440. org-tags-exclude-from-inheritance))
  12441. ;; selective inheritance, remove uninherited ones
  12442. (setcdr (car tags-alist)
  12443. (org-remove-uninherited-tags (cdar tags-alist))))
  12444. (when (and
  12445. ;; eval matcher only when the todo condition is OK
  12446. (and (or (not todo-only) (member todo org-not-done-keywords))
  12447. (let ((case-fold-search t) (org-trust-scanner-tags t))
  12448. (eval matcher)))
  12449. ;; Call the skipper, but return t if it does not skip,
  12450. ;; so that the `and' form continues evaluating
  12451. (progn
  12452. (unless (eq action 'sparse-tree) (org-agenda-skip))
  12453. t)
  12454. ;; Check if timestamps are deselecting this entry
  12455. (or (not todo-only)
  12456. (and (member todo org-not-done-keywords)
  12457. (or (not org-agenda-tags-todo-honor-ignore-options)
  12458. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item))))))
  12459. ;; select this headline
  12460. (cond
  12461. ((eq action 'sparse-tree)
  12462. (and org-highlight-sparse-tree-matches
  12463. (org-get-heading) (match-end 0)
  12464. (org-highlight-new-match
  12465. (match-beginning 1) (match-end 1)))
  12466. (org-show-context 'tags-tree))
  12467. ((eq action 'agenda)
  12468. (setq txt (org-agenda-format-item
  12469. ""
  12470. (concat
  12471. (if (eq org-tags-match-list-sublevels 'indented)
  12472. (make-string (1- level) ?.) "")
  12473. (org-get-heading))
  12474. level category
  12475. tags-list)
  12476. priority (org-get-priority txt))
  12477. (goto-char lspos)
  12478. (setq marker (org-agenda-new-marker))
  12479. (org-add-props txt props
  12480. 'org-marker marker 'org-hd-marker marker 'org-category category
  12481. 'todo-state todo
  12482. 'priority priority 'type "tagsmatch")
  12483. (push txt rtn))
  12484. ((functionp action)
  12485. (setq org-map-continue-from nil)
  12486. (save-excursion
  12487. (setq rtn1 (funcall action))
  12488. (push rtn1 rtn)))
  12489. (t (error "Invalid action")))
  12490. ;; if we are to skip sublevels, jump to end of subtree
  12491. (unless org-tags-match-list-sublevels
  12492. (org-end-of-subtree t)
  12493. (backward-char 1))))
  12494. ;; Get the correct position from where to continue
  12495. (if org-map-continue-from
  12496. (goto-char org-map-continue-from)
  12497. (and (= (point) lspos) (end-of-line 1)))))
  12498. (when (and (eq action 'sparse-tree)
  12499. (not org-sparse-tree-open-archived-trees))
  12500. (org-hide-archived-subtrees (point-min) (point-max)))
  12501. (nreverse rtn)))
  12502. (defun org-remove-uninherited-tags (tags)
  12503. "Remove all tags that are not inherited from the list TAGS."
  12504. (cond
  12505. ((eq org-use-tag-inheritance t)
  12506. (if org-tags-exclude-from-inheritance
  12507. (org-delete-all org-tags-exclude-from-inheritance tags)
  12508. tags))
  12509. ((not org-use-tag-inheritance) nil)
  12510. ((stringp org-use-tag-inheritance)
  12511. (delq nil (mapcar
  12512. (lambda (x)
  12513. (if (and (string-match org-use-tag-inheritance x)
  12514. (not (member x org-tags-exclude-from-inheritance)))
  12515. x nil))
  12516. tags)))
  12517. ((listp org-use-tag-inheritance)
  12518. (delq nil (mapcar
  12519. (lambda (x)
  12520. (if (member x org-use-tag-inheritance) x nil))
  12521. tags)))))
  12522. (defun org-match-sparse-tree (&optional todo-only match)
  12523. "Create a sparse tree according to tags string MATCH.
  12524. MATCH can contain positive and negative selection of tags, like
  12525. \"+WORK+URGENT-WITHBOSS\".
  12526. If optional argument TODO-ONLY is non-nil, only select lines that are
  12527. also TODO lines."
  12528. (interactive "P")
  12529. (org-agenda-prepare-buffers (list (current-buffer)))
  12530. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  12531. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  12532. (defvar org-cached-props nil)
  12533. (defun org-cached-entry-get (pom property)
  12534. (if (or (eq t org-use-property-inheritance)
  12535. (and (stringp org-use-property-inheritance)
  12536. (string-match org-use-property-inheritance property))
  12537. (and (listp org-use-property-inheritance)
  12538. (member property org-use-property-inheritance)))
  12539. ;; Caching is not possible, check it directly
  12540. (org-entry-get pom property 'inherit)
  12541. ;; Get all properties, so that we can do complicated checks easily
  12542. (cdr (assoc property (or org-cached-props
  12543. (setq org-cached-props
  12544. (org-entry-properties pom)))))))
  12545. (defun org-global-tags-completion-table (&optional files)
  12546. "Return the list of all tags in all agenda buffer/files.
  12547. Optional FILES argument is a list of files which can be used
  12548. instead of the agenda files."
  12549. (save-excursion
  12550. (org-uniquify
  12551. (delq nil
  12552. (apply 'append
  12553. (mapcar
  12554. (lambda (file)
  12555. (set-buffer (find-file-noselect file))
  12556. (append (org-get-buffer-tags)
  12557. (mapcar (lambda (x) (if (stringp (car-safe x))
  12558. (list (car-safe x)) nil))
  12559. org-tag-alist)))
  12560. (if (and files (car files))
  12561. files
  12562. (org-agenda-files))))))))
  12563. (defun org-make-tags-matcher (match)
  12564. "Create the TAGS/TODO matcher form for the selection string MATCH.
  12565. The variable `todo-only' is scoped dynamically into this function.
  12566. It will be set to t if the matcher restricts matching to TODO entries,
  12567. otherwise will not be touched.
  12568. Returns a cons of the selection string MATCH and the constructed
  12569. lisp form implementing the matcher. The matcher is to be evaluated
  12570. at an Org entry, with point on the headline, and returns t if the
  12571. entry matches the selection string MATCH. The returned lisp form
  12572. references two variables with information about the entry, which
  12573. must be bound around the form's evaluation: todo, the TODO keyword
  12574. at the entry (or nil of none); and tags-list, the list of all tags
  12575. at the entry including inherited ones. Additionally, the category
  12576. of the entry (if any) must be specified as the text property
  12577. 'org-category on the headline.
  12578. See also `org-scan-tags'.
  12579. "
  12580. (declare (special todo-only))
  12581. (unless (boundp 'todo-only)
  12582. (error "org-make-tags-matcher expects todo-only to be scoped in"))
  12583. (unless match
  12584. ;; Get a new match request, with completion
  12585. (let ((org-last-tags-completion-table
  12586. (org-global-tags-completion-table)))
  12587. (setq match (org-completing-read-no-i
  12588. "Match: " 'org-tags-completion-function nil nil nil
  12589. 'org-tags-history))))
  12590. ;; Parse the string and create a lisp form
  12591. (let ((match0 match)
  12592. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  12593. minus tag mm
  12594. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  12595. orterms term orlist re-p str-p level-p level-op time-p
  12596. prop-p pn pv po gv rest)
  12597. (if (string-match "/+" match)
  12598. ;; match contains also a todo-matching request
  12599. (progn
  12600. (setq tagsmatch (substring match 0 (match-beginning 0))
  12601. todomatch (substring match (match-end 0)))
  12602. (if (string-match "^!" todomatch)
  12603. (setq todo-only t todomatch (substring todomatch 1)))
  12604. (if (string-match "^\\s-*$" todomatch)
  12605. (setq todomatch nil)))
  12606. ;; only matching tags
  12607. (setq tagsmatch match todomatch nil))
  12608. ;; Make the tags matcher
  12609. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  12610. (setq tagsmatcher t)
  12611. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  12612. (while (setq term (pop orterms))
  12613. (while (and (equal (substring term -1) "\\") orterms)
  12614. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  12615. (while (string-match re term)
  12616. (setq rest (substring term (match-end 0))
  12617. minus (and (match-end 1)
  12618. (equal (match-string 1 term) "-"))
  12619. tag (save-match-data (replace-regexp-in-string
  12620. "\\\\-" "-"
  12621. (match-string 2 term)))
  12622. re-p (equal (string-to-char tag) ?{)
  12623. level-p (match-end 4)
  12624. prop-p (match-end 5)
  12625. mm (cond
  12626. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  12627. (level-p
  12628. (setq level-op (org-op-to-function (match-string 3 term)))
  12629. `(,level-op level ,(string-to-number
  12630. (match-string 4 term))))
  12631. (prop-p
  12632. (setq pn (match-string 5 term)
  12633. po (match-string 6 term)
  12634. pv (match-string 7 term)
  12635. re-p (equal (string-to-char pv) ?{)
  12636. str-p (equal (string-to-char pv) ?\")
  12637. time-p (save-match-data
  12638. (string-match "^\"[[<].*[]>]\"$" pv))
  12639. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  12640. (if time-p (setq pv (org-matcher-time pv)))
  12641. (setq po (org-op-to-function po (if time-p 'time str-p)))
  12642. (cond
  12643. ((equal pn "CATEGORY")
  12644. (setq gv '(get-text-property (point) 'org-category)))
  12645. ((equal pn "TODO")
  12646. (setq gv 'todo))
  12647. (t
  12648. (setq gv `(org-cached-entry-get nil ,pn))))
  12649. (if re-p
  12650. (if (eq po 'org<>)
  12651. `(not (string-match ,pv (or ,gv "")))
  12652. `(string-match ,pv (or ,gv "")))
  12653. (if str-p
  12654. `(,po (or ,gv "") ,pv)
  12655. `(,po (string-to-number (or ,gv ""))
  12656. ,(string-to-number pv) ))))
  12657. (t `(member ,tag tags-list)))
  12658. mm (if minus (list 'not mm) mm)
  12659. term rest)
  12660. (push mm tagsmatcher))
  12661. (push (if (> (length tagsmatcher) 1)
  12662. (cons 'and tagsmatcher)
  12663. (car tagsmatcher))
  12664. orlist)
  12665. (setq tagsmatcher nil))
  12666. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  12667. (setq tagsmatcher
  12668. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  12669. ;; Make the todo matcher
  12670. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  12671. (setq todomatcher t)
  12672. (setq orterms (org-split-string todomatch "|") orlist nil)
  12673. (while (setq term (pop orterms))
  12674. (while (string-match re term)
  12675. (setq minus (and (match-end 1)
  12676. (equal (match-string 1 term) "-"))
  12677. kwd (match-string 2 term)
  12678. re-p (equal (string-to-char kwd) ?{)
  12679. term (substring term (match-end 0))
  12680. mm (if re-p
  12681. `(string-match ,(substring kwd 1 -1) todo)
  12682. (list 'equal 'todo kwd))
  12683. mm (if minus (list 'not mm) mm))
  12684. (push mm todomatcher))
  12685. (push (if (> (length todomatcher) 1)
  12686. (cons 'and todomatcher)
  12687. (car todomatcher))
  12688. orlist)
  12689. (setq todomatcher nil))
  12690. (setq todomatcher (if (> (length orlist) 1)
  12691. (cons 'or orlist) (car orlist))))
  12692. ;; Return the string and lisp forms of the matcher
  12693. (setq matcher (if todomatcher
  12694. (list 'and tagsmatcher todomatcher)
  12695. tagsmatcher))
  12696. (when todo-only
  12697. (setq matcher (list 'and '(member todo org-not-done-keywords)
  12698. matcher)))
  12699. (cons match0 matcher)))
  12700. (defun org-op-to-function (op &optional stringp)
  12701. "Turn an operator into the appropriate function."
  12702. (setq op
  12703. (cond
  12704. ((equal op "<" ) '(< string< org-time<))
  12705. ((equal op ">" ) '(> org-string> org-time>))
  12706. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  12707. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  12708. ((member op '("=" "==")) '(= string= org-time=))
  12709. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  12710. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  12711. (defun org<> (a b) (not (= a b)))
  12712. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  12713. (defun org-string>= (a b) (not (string< a b)))
  12714. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  12715. (defun org-string<> (a b) (not (string= a b)))
  12716. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  12717. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  12718. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  12719. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  12720. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  12721. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  12722. (defun org-2ft (s)
  12723. "Convert S to a floating point time.
  12724. If S is already a number, just return it. If it is a string, parse
  12725. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  12726. (cond
  12727. ((numberp s) s)
  12728. ((stringp s)
  12729. (condition-case nil
  12730. (float-time (apply 'encode-time (org-parse-time-string s)))
  12731. (error 0.)))
  12732. (t 0.)))
  12733. (defun org-time-today ()
  12734. "Time in seconds today at 0:00.
  12735. Returns the float number of seconds since the beginning of the
  12736. epoch to the beginning of today (00:00)."
  12737. (float-time (apply 'encode-time
  12738. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  12739. (defun org-matcher-time (s)
  12740. "Interpret a time comparison value."
  12741. (save-match-data
  12742. (cond
  12743. ((string= s "<now>") (float-time))
  12744. ((string= s "<today>") (org-time-today))
  12745. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  12746. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  12747. ((string-match "^<\\([-+][0-9]+\\)\\([hdwmy]\\)>$" s)
  12748. (+ (org-time-today)
  12749. (* (string-to-number (match-string 1 s))
  12750. (cdr (assoc (match-string 2 s)
  12751. '(("d" . 86400.0) ("w" . 604800.0)
  12752. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  12753. (t (org-2ft s)))))
  12754. (defun org-match-any-p (re list)
  12755. "Does re match any element of list?"
  12756. (setq list (mapcar (lambda (x) (string-match re x)) list))
  12757. (delq nil list))
  12758. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  12759. (defvar org-tags-overlay (make-overlay 1 1))
  12760. (org-detach-overlay org-tags-overlay)
  12761. (defun org-get-local-tags-at (&optional pos)
  12762. "Get a list of tags defined in the current headline."
  12763. (org-get-tags-at pos 'local))
  12764. (defun org-get-local-tags ()
  12765. "Get a list of tags defined in the current headline."
  12766. (org-get-tags-at nil 'local))
  12767. (defun org-get-tags-at (&optional pos local)
  12768. "Get a list of all headline tags applicable at POS.
  12769. POS defaults to point. If tags are inherited, the list contains
  12770. the targets in the same sequence as the headlines appear, i.e.
  12771. the tags of the current headline come last.
  12772. When LOCAL is non-nil, only return tags from the current headline,
  12773. ignore inherited ones."
  12774. (interactive)
  12775. (if (and org-trust-scanner-tags
  12776. (or (not pos) (equal pos (point)))
  12777. (not local))
  12778. org-scanner-tags
  12779. (let (tags ltags lastpos parent)
  12780. (save-excursion
  12781. (save-restriction
  12782. (widen)
  12783. (goto-char (or pos (point)))
  12784. (save-match-data
  12785. (catch 'done
  12786. (condition-case nil
  12787. (progn
  12788. (org-back-to-heading t)
  12789. (while (not (equal lastpos (point)))
  12790. (setq lastpos (point))
  12791. (when (looking-at
  12792. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  12793. (setq ltags (org-split-string
  12794. (org-match-string-no-properties 1) ":"))
  12795. (when parent
  12796. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  12797. (setq tags (append
  12798. (if parent
  12799. (org-remove-uninherited-tags ltags)
  12800. ltags)
  12801. tags)))
  12802. (or org-use-tag-inheritance (throw 'done t))
  12803. (if local (throw 'done t))
  12804. (or (org-up-heading-safe) (error nil))
  12805. (setq parent t)))
  12806. (error nil)))))
  12807. (if local
  12808. tags
  12809. (reverse (delete-dups
  12810. (reverse (append
  12811. (org-remove-uninherited-tags
  12812. org-file-tags) tags)))))))))
  12813. (defun org-add-prop-inherited (s)
  12814. (add-text-properties 0 (length s) '(inherited t) s)
  12815. s)
  12816. (defun org-toggle-tag (tag &optional onoff)
  12817. "Toggle the tag TAG for the current line.
  12818. If ONOFF is `on' or `off', don't toggle but set to this state."
  12819. (let (res current)
  12820. (save-excursion
  12821. (org-back-to-heading t)
  12822. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  12823. (point-at-eol) t)
  12824. (progn
  12825. (setq current (match-string 1))
  12826. (replace-match ""))
  12827. (setq current ""))
  12828. (setq current (nreverse (org-split-string current ":")))
  12829. (cond
  12830. ((eq onoff 'on)
  12831. (setq res t)
  12832. (or (member tag current) (push tag current)))
  12833. ((eq onoff 'off)
  12834. (or (not (member tag current)) (setq current (delete tag current))))
  12835. (t (if (member tag current)
  12836. (setq current (delete tag current))
  12837. (setq res t)
  12838. (push tag current))))
  12839. (end-of-line 1)
  12840. (if current
  12841. (progn
  12842. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  12843. (org-set-tags nil t))
  12844. (delete-horizontal-space))
  12845. (run-hooks 'org-after-tags-change-hook))
  12846. res))
  12847. (defun org-align-tags-here (to-col)
  12848. ;; Assumes that this is a headline
  12849. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  12850. (beginning-of-line 1)
  12851. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12852. (< pos (match-beginning 2)))
  12853. (progn
  12854. (setq tags-l (- (match-end 2) (match-beginning 2)))
  12855. (goto-char (match-beginning 1))
  12856. (insert " ")
  12857. (delete-region (point) (1+ (match-beginning 2)))
  12858. (setq ncol (max (current-column)
  12859. (1+ col)
  12860. (if (> to-col 0)
  12861. to-col
  12862. (- (abs to-col) tags-l))))
  12863. (setq p (point))
  12864. (insert (make-string (- ncol (current-column)) ?\ ))
  12865. (setq ncol (current-column))
  12866. (when indent-tabs-mode (tabify p (point-at-eol)))
  12867. (org-move-to-column (min ncol col) t))
  12868. (goto-char pos))))
  12869. (defun org-set-tags-command (&optional arg just-align)
  12870. "Call the set-tags command for the current entry."
  12871. (interactive "P")
  12872. (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
  12873. (org-set-tags arg just-align)
  12874. (save-excursion
  12875. (unless (and (org-region-active-p)
  12876. org-loop-over-headlines-in-active-region)
  12877. (org-back-to-heading t))
  12878. (org-set-tags arg just-align))))
  12879. (defun org-set-tags-to (data)
  12880. "Set the tags of the current entry to DATA, replacing the current tags.
  12881. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  12882. If DATA is nil or the empty string, any tags will be removed."
  12883. (interactive "sTags: ")
  12884. (setq data
  12885. (cond
  12886. ((eq data nil) "")
  12887. ((equal data "") "")
  12888. ((stringp data)
  12889. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  12890. ":"))
  12891. ((listp data)
  12892. (concat ":" (mapconcat 'identity data ":") ":"))))
  12893. (when data
  12894. (save-excursion
  12895. (org-back-to-heading t)
  12896. (when (looking-at org-complex-heading-regexp)
  12897. (if (match-end 5)
  12898. (progn
  12899. (goto-char (match-beginning 5))
  12900. (insert data)
  12901. (delete-region (point) (point-at-eol))
  12902. (org-set-tags nil 'align))
  12903. (goto-char (point-at-eol))
  12904. (insert " " data)
  12905. (org-set-tags nil 'align)))
  12906. (beginning-of-line 1)
  12907. (if (looking-at ".*?\\([ \t]+\\)$")
  12908. (delete-region (match-beginning 1) (match-end 1))))))
  12909. (defun org-align-all-tags ()
  12910. "Align the tags i all headings."
  12911. (interactive)
  12912. (save-excursion
  12913. (or (ignore-errors (org-back-to-heading t))
  12914. (outline-next-heading))
  12915. (if (org-at-heading-p)
  12916. (org-set-tags t)
  12917. (message "No headings"))))
  12918. (defvar org-indent-indentation-per-level)
  12919. (defun org-set-tags (&optional arg just-align)
  12920. "Set the tags for the current headline.
  12921. With prefix ARG, realign all tags in headings in the current buffer."
  12922. (interactive "P")
  12923. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  12924. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  12925. 'region-start-level 'region))
  12926. org-loop-over-headlines-in-active-region)
  12927. (org-map-entries
  12928. ;; We don't use ARG and JUST-ALIGN here these args are not
  12929. ;; useful when looping over headlines
  12930. `(org-set-tags)
  12931. org-loop-over-headlines-in-active-region
  12932. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  12933. (let* ((re org-outline-regexp-bol)
  12934. (current (unless arg (org-get-tags-string)))
  12935. (col (current-column))
  12936. (org-setting-tags t)
  12937. table current-tags inherited-tags ; computed below when needed
  12938. tags p0 c0 c1 rpl di tc level)
  12939. (if arg
  12940. (save-excursion
  12941. (goto-char (point-min))
  12942. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  12943. (while (re-search-forward re nil t)
  12944. (org-set-tags nil t)
  12945. (end-of-line 1)))
  12946. (message "All tags realigned to column %d" org-tags-column))
  12947. (if just-align
  12948. (setq tags current)
  12949. ;; Get a new set of tags from the user
  12950. (save-excursion
  12951. (setq table (append org-tag-persistent-alist
  12952. (or org-tag-alist (org-get-buffer-tags))
  12953. (and
  12954. org-complete-tags-always-offer-all-agenda-tags
  12955. (org-global-tags-completion-table
  12956. (org-agenda-files))))
  12957. org-last-tags-completion-table table
  12958. current-tags (org-split-string current ":")
  12959. inherited-tags (nreverse
  12960. (nthcdr (length current-tags)
  12961. (nreverse (org-get-tags-at))))
  12962. tags
  12963. (if (or (eq t org-use-fast-tag-selection)
  12964. (and org-use-fast-tag-selection
  12965. (delq nil (mapcar 'cdr table))))
  12966. (org-fast-tag-selection
  12967. current-tags inherited-tags table
  12968. (if org-fast-tag-selection-include-todo
  12969. org-todo-key-alist))
  12970. (let ((org-add-colon-after-tag-completion (< 1 (length table))))
  12971. (org-trim
  12972. (org-icompleting-read "Tags: "
  12973. 'org-tags-completion-function
  12974. nil nil current 'org-tags-history))))))
  12975. (while (string-match "[-+&]+" tags)
  12976. ;; No boolean logic, just a list
  12977. (setq tags (replace-match ":" t t tags))))
  12978. (setq tags (replace-regexp-in-string "[,]" ":" tags))
  12979. (if org-tags-sort-function
  12980. (setq tags (mapconcat 'identity
  12981. (sort (org-split-string
  12982. tags (org-re "[^[:alnum:]_@#%]+"))
  12983. org-tags-sort-function) ":")))
  12984. (if (string-match "\\`[\t ]*\\'" tags)
  12985. (setq tags "")
  12986. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  12987. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  12988. ;; Insert new tags at the correct column
  12989. (beginning-of-line 1)
  12990. (setq level (or (and (looking-at org-outline-regexp)
  12991. (- (match-end 0) (point) 1))
  12992. 1))
  12993. (cond
  12994. ((and (equal current "") (equal tags "")))
  12995. ((re-search-forward
  12996. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  12997. (point-at-eol) t)
  12998. (if (equal tags "")
  12999. (setq rpl "")
  13000. (goto-char (match-beginning 0))
  13001. (setq c0 (current-column)
  13002. ;; compute offset for the case of org-indent-mode active
  13003. di (if org-indent-mode
  13004. (* (1- org-indent-indentation-per-level) (1- level))
  13005. 0)
  13006. p0 (if (equal (char-before) ?*) (1+ (point)) (point))
  13007. tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
  13008. c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (length tags))))
  13009. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  13010. (replace-match rpl t t)
  13011. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  13012. tags)
  13013. (t (error "Tags alignment failed")))
  13014. (org-move-to-column col)
  13015. (unless just-align
  13016. (run-hooks 'org-after-tags-change-hook))))))
  13017. (defun org-change-tag-in-region (beg end tag off)
  13018. "Add or remove TAG for each entry in the region.
  13019. This works in the agenda, and also in an org-mode buffer."
  13020. (interactive
  13021. (list (region-beginning) (region-end)
  13022. (let ((org-last-tags-completion-table
  13023. (if (derived-mode-p 'org-mode)
  13024. (org-get-buffer-tags)
  13025. (org-global-tags-completion-table))))
  13026. (org-icompleting-read
  13027. "Tag: " 'org-tags-completion-function nil nil nil
  13028. 'org-tags-history))
  13029. (progn
  13030. (message "[s]et or [r]emove? ")
  13031. (equal (read-char-exclusive) ?r))))
  13032. (if (fboundp 'deactivate-mark) (deactivate-mark))
  13033. (let ((agendap (equal major-mode 'org-agenda-mode))
  13034. l1 l2 m buf pos newhead (cnt 0))
  13035. (goto-char end)
  13036. (setq l2 (1- (org-current-line)))
  13037. (goto-char beg)
  13038. (setq l1 (org-current-line))
  13039. (loop for l from l1 to l2 do
  13040. (org-goto-line l)
  13041. (setq m (get-text-property (point) 'org-hd-marker))
  13042. (when (or (and (derived-mode-p 'org-mode) (org-at-heading-p))
  13043. (and agendap m))
  13044. (setq buf (if agendap (marker-buffer m) (current-buffer))
  13045. pos (if agendap m (point)))
  13046. (with-current-buffer buf
  13047. (save-excursion
  13048. (save-restriction
  13049. (goto-char pos)
  13050. (setq cnt (1+ cnt))
  13051. (org-toggle-tag tag (if off 'off 'on))
  13052. (setq newhead (org-get-heading)))))
  13053. (and agendap (org-agenda-change-all-lines newhead m))))
  13054. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  13055. (defun org-tags-completion-function (string predicate &optional flag)
  13056. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  13057. (confirm (lambda (x) (stringp (car x)))))
  13058. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  13059. (setq s1 (match-string 1 string)
  13060. s2 (match-string 2 string))
  13061. (setq s1 "" s2 string))
  13062. (cond
  13063. ((eq flag nil)
  13064. ;; try completion
  13065. (setq rtn (try-completion s2 ctable confirm))
  13066. (if (stringp rtn)
  13067. (setq rtn
  13068. (concat s1 s2 (substring rtn (length s2))
  13069. (if (and org-add-colon-after-tag-completion
  13070. (assoc rtn ctable))
  13071. ":" ""))))
  13072. rtn)
  13073. ((eq flag t)
  13074. ;; all-completions
  13075. (all-completions s2 ctable confirm)
  13076. )
  13077. ((eq flag 'lambda)
  13078. ;; exact match?
  13079. (assoc s2 ctable)))
  13080. ))
  13081. (defun org-fast-tag-insert (kwd tags face &optional end)
  13082. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  13083. (insert (format "%-12s" (concat kwd ":"))
  13084. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  13085. (or end "")))
  13086. (defun org-fast-tag-show-exit (flag)
  13087. (save-excursion
  13088. (org-goto-line 3)
  13089. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  13090. (replace-match ""))
  13091. (when flag
  13092. (end-of-line 1)
  13093. (org-move-to-column (- (window-width) 19) t)
  13094. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  13095. (defun org-set-current-tags-overlay (current prefix)
  13096. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  13097. (if (featurep 'xemacs)
  13098. (org-overlay-display org-tags-overlay (concat prefix s)
  13099. 'secondary-selection)
  13100. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  13101. (org-overlay-display org-tags-overlay (concat prefix s)))))
  13102. (defvar org-last-tag-selection-key nil)
  13103. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  13104. "Fast tag selection with single keys.
  13105. CURRENT is the current list of tags in the headline, INHERITED is the
  13106. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  13107. possibly with grouping information. TODO-TABLE is a similar table with
  13108. TODO keywords, should these have keys assigned to them.
  13109. If the keys are nil, a-z are automatically assigned.
  13110. Returns the new tags string, or nil to not change the current settings."
  13111. (let* ((fulltable (append table todo-table))
  13112. (maxlen (apply 'max (mapcar
  13113. (lambda (x)
  13114. (if (stringp (car x)) (string-width (car x)) 0))
  13115. fulltable)))
  13116. (buf (current-buffer))
  13117. (expert (eq org-fast-tag-selection-single-key 'expert))
  13118. (buffer-tags nil)
  13119. (fwidth (+ maxlen 3 1 3))
  13120. (ncol (/ (- (window-width) 4) fwidth))
  13121. (i-face 'org-done)
  13122. (c-face 'org-todo)
  13123. tg cnt e c char c1 c2 ntable tbl rtn
  13124. ov-start ov-end ov-prefix
  13125. (exit-after-next org-fast-tag-selection-single-key)
  13126. (done-keywords org-done-keywords)
  13127. groups ingroup)
  13128. (save-excursion
  13129. (beginning-of-line 1)
  13130. (if (looking-at
  13131. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13132. (setq ov-start (match-beginning 1)
  13133. ov-end (match-end 1)
  13134. ov-prefix "")
  13135. (setq ov-start (1- (point-at-eol))
  13136. ov-end (1+ ov-start))
  13137. (skip-chars-forward "^\n\r")
  13138. (setq ov-prefix
  13139. (concat
  13140. (buffer-substring (1- (point)) (point))
  13141. (if (> (current-column) org-tags-column)
  13142. " "
  13143. (make-string (- org-tags-column (current-column)) ?\ ))))))
  13144. (move-overlay org-tags-overlay ov-start ov-end)
  13145. (save-window-excursion
  13146. (if expert
  13147. (set-buffer (get-buffer-create " *Org tags*"))
  13148. (delete-other-windows)
  13149. (split-window-vertically)
  13150. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  13151. (erase-buffer)
  13152. (org-set-local 'org-done-keywords done-keywords)
  13153. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  13154. (org-fast-tag-insert "Current" current c-face "\n\n")
  13155. (org-fast-tag-show-exit exit-after-next)
  13156. (org-set-current-tags-overlay current ov-prefix)
  13157. (setq tbl fulltable char ?a cnt 0)
  13158. (while (setq e (pop tbl))
  13159. (cond
  13160. ((equal (car e) :startgroup)
  13161. (push '() groups) (setq ingroup t)
  13162. (when (not (= cnt 0))
  13163. (setq cnt 0)
  13164. (insert "\n"))
  13165. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  13166. ((equal (car e) :endgroup)
  13167. (setq ingroup nil cnt 0)
  13168. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  13169. ((equal e '(:newline))
  13170. (when (not (= cnt 0))
  13171. (setq cnt 0)
  13172. (insert "\n")
  13173. (setq e (car tbl))
  13174. (while (equal (car tbl) '(:newline))
  13175. (insert "\n")
  13176. (setq tbl (cdr tbl)))))
  13177. (t
  13178. (setq tg (copy-sequence (car e)) c2 nil)
  13179. (if (cdr e)
  13180. (setq c (cdr e))
  13181. ;; automatically assign a character.
  13182. (setq c1 (string-to-char
  13183. (downcase (substring
  13184. tg (if (= (string-to-char tg) ?@) 1 0)))))
  13185. (if (or (rassoc c1 ntable) (rassoc c1 table))
  13186. (while (or (rassoc char ntable) (rassoc char table))
  13187. (setq char (1+ char)))
  13188. (setq c2 c1))
  13189. (setq c (or c2 char)))
  13190. (if ingroup (push tg (car groups)))
  13191. (setq tg (org-add-props tg nil 'face
  13192. (cond
  13193. ((not (assoc tg table))
  13194. (org-get-todo-face tg))
  13195. ((member tg current) c-face)
  13196. ((member tg inherited) i-face))))
  13197. (if (and (= cnt 0) (not ingroup)) (insert " "))
  13198. (insert "[" c "] " tg (make-string
  13199. (- fwidth 4 (length tg)) ?\ ))
  13200. (push (cons tg c) ntable)
  13201. (when (= (setq cnt (1+ cnt)) ncol)
  13202. (insert "\n")
  13203. (if ingroup (insert " "))
  13204. (setq cnt 0)))))
  13205. (setq ntable (nreverse ntable))
  13206. (insert "\n")
  13207. (goto-char (point-min))
  13208. (if (not expert) (org-fit-window-to-buffer))
  13209. (setq rtn
  13210. (catch 'exit
  13211. (while t
  13212. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  13213. (if (not groups) "no " "")
  13214. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  13215. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  13216. (setq org-last-tag-selection-key c)
  13217. (cond
  13218. ((= c ?\r) (throw 'exit t))
  13219. ((= c ?!)
  13220. (setq groups (not groups))
  13221. (goto-char (point-min))
  13222. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  13223. ((= c ?\C-c)
  13224. (if (not expert)
  13225. (org-fast-tag-show-exit
  13226. (setq exit-after-next (not exit-after-next)))
  13227. (setq expert nil)
  13228. (delete-other-windows)
  13229. (set-window-buffer (split-window-vertically) " *Org tags*")
  13230. (org-switch-to-buffer-other-window " *Org tags*")
  13231. (org-fit-window-to-buffer)))
  13232. ((or (= c ?\C-g)
  13233. (and (= c ?q) (not (rassoc c ntable))))
  13234. (org-detach-overlay org-tags-overlay)
  13235. (setq quit-flag t))
  13236. ((= c ?\ )
  13237. (setq current nil)
  13238. (if exit-after-next (setq exit-after-next 'now)))
  13239. ((= c ?\t)
  13240. (condition-case nil
  13241. (setq tg (org-icompleting-read
  13242. "Tag: "
  13243. (or buffer-tags
  13244. (with-current-buffer buf
  13245. (org-get-buffer-tags)))))
  13246. (quit (setq tg "")))
  13247. (when (string-match "\\S-" tg)
  13248. (add-to-list 'buffer-tags (list tg))
  13249. (if (member tg current)
  13250. (setq current (delete tg current))
  13251. (push tg current)))
  13252. (if exit-after-next (setq exit-after-next 'now)))
  13253. ((setq e (rassoc c todo-table) tg (car e))
  13254. (with-current-buffer buf
  13255. (save-excursion (org-todo tg)))
  13256. (if exit-after-next (setq exit-after-next 'now)))
  13257. ((setq e (rassoc c ntable) tg (car e))
  13258. (if (member tg current)
  13259. (setq current (delete tg current))
  13260. (loop for g in groups do
  13261. (if (member tg g)
  13262. (mapc (lambda (x)
  13263. (setq current (delete x current)))
  13264. g)))
  13265. (push tg current))
  13266. (if exit-after-next (setq exit-after-next 'now))))
  13267. ;; Create a sorted list
  13268. (setq current
  13269. (sort current
  13270. (lambda (a b)
  13271. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  13272. (if (eq exit-after-next 'now) (throw 'exit t))
  13273. (goto-char (point-min))
  13274. (beginning-of-line 2)
  13275. (delete-region (point) (point-at-eol))
  13276. (org-fast-tag-insert "Current" current c-face)
  13277. (org-set-current-tags-overlay current ov-prefix)
  13278. (while (re-search-forward
  13279. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  13280. (setq tg (match-string 1))
  13281. (add-text-properties
  13282. (match-beginning 1) (match-end 1)
  13283. (list 'face
  13284. (cond
  13285. ((member tg current) c-face)
  13286. ((member tg inherited) i-face)
  13287. (t (get-text-property (match-beginning 1) 'face))))))
  13288. (goto-char (point-min)))))
  13289. (org-detach-overlay org-tags-overlay)
  13290. (if rtn
  13291. (mapconcat 'identity current ":")
  13292. nil))))
  13293. (defun org-get-tags-string ()
  13294. "Get the TAGS string in the current headline."
  13295. (unless (org-at-heading-p t)
  13296. (error "Not on a heading"))
  13297. (save-excursion
  13298. (beginning-of-line 1)
  13299. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13300. (org-match-string-no-properties 1)
  13301. "")))
  13302. (defun org-get-tags ()
  13303. "Get the list of tags specified in the current headline."
  13304. (org-split-string (org-get-tags-string) ":"))
  13305. (defun org-get-buffer-tags ()
  13306. "Get a table of all tags used in the buffer, for completion."
  13307. (let (tags)
  13308. (save-excursion
  13309. (goto-char (point-min))
  13310. (while (re-search-forward
  13311. (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
  13312. (when (equal (char-after (point-at-bol 0)) ?*)
  13313. (mapc (lambda (x) (add-to-list 'tags x))
  13314. (org-split-string (org-match-string-no-properties 1) ":")))))
  13315. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  13316. (mapcar 'list tags)))
  13317. ;;;; The mapping API
  13318. (defun org-map-entries (func &optional match scope &rest skip)
  13319. "Call FUNC at each headline selected by MATCH in SCOPE.
  13320. FUNC is a function or a lisp form. The function will be called without
  13321. arguments, with the cursor positioned at the beginning of the headline.
  13322. The return values of all calls to the function will be collected and
  13323. returned as a list.
  13324. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  13325. does not need to preserve point. After evaluation, the cursor will be
  13326. moved to the end of the line (presumably of the headline of the
  13327. processed entry) and search continues from there. Under some
  13328. circumstances, this may not produce the wanted results. For example,
  13329. if you have removed (e.g. archived) the current (sub)tree it could
  13330. mean that the next entry will be skipped entirely. In such cases, you
  13331. can specify the position from where search should continue by making
  13332. FUNC set the variable `org-map-continue-from' to the desired buffer
  13333. position.
  13334. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  13335. Only headlines that are matched by this query will be considered during
  13336. the iteration. When MATCH is nil or t, all headlines will be
  13337. visited by the iteration.
  13338. SCOPE determines the scope of this command. It can be any of:
  13339. nil The current buffer, respecting the restriction if any
  13340. tree The subtree started with the entry at point
  13341. region The entries within the active region, if any
  13342. region-start-level
  13343. The entries within the active region, but only those at
  13344. the same level than the first one.
  13345. file The current buffer, without restriction
  13346. file-with-archives
  13347. The current buffer, and any archives associated with it
  13348. agenda All agenda files
  13349. agenda-with-archives
  13350. All agenda files with any archive files associated with them
  13351. \(file1 file2 ...)
  13352. If this is a list, all files in the list will be scanned
  13353. The remaining args are treated as settings for the skipping facilities of
  13354. the scanner. The following items can be given here:
  13355. archive skip trees with the archive tag
  13356. comment skip trees with the COMMENT keyword
  13357. function or Emacs Lisp form:
  13358. will be used as value for `org-agenda-skip-function', so
  13359. whenever the function returns a position, FUNC will not be
  13360. called for that entry and search will continue from the
  13361. position returned
  13362. If your function needs to retrieve the tags including inherited tags
  13363. at the *current* entry, you can use the value of the variable
  13364. `org-scanner-tags' which will be much faster than getting the value
  13365. with `org-get-tags-at'. If your function gets properties with
  13366. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  13367. to t around the call to `org-entry-properties' to get the same speedup.
  13368. Note that if your function moves around to retrieve tags and properties at
  13369. a *different* entry, you cannot use these techniques."
  13370. (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
  13371. (not (org-region-active-p)))
  13372. (let* ((org-agenda-archives-mode nil) ; just to make sure
  13373. (org-agenda-skip-archived-trees (memq 'archive skip))
  13374. (org-agenda-skip-comment-trees (memq 'comment skip))
  13375. (org-agenda-skip-function
  13376. (car (org-delete-all '(comment archive) skip)))
  13377. (org-tags-match-list-sublevels t)
  13378. (start-level (eq scope 'region-start-level))
  13379. matcher file res
  13380. org-todo-keywords-for-agenda
  13381. org-done-keywords-for-agenda
  13382. org-todo-keyword-alist-for-agenda
  13383. org-drawers-for-agenda
  13384. org-tag-alist-for-agenda
  13385. todo-only)
  13386. (cond
  13387. ((eq match t) (setq matcher t))
  13388. ((eq match nil) (setq matcher t))
  13389. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  13390. (save-excursion
  13391. (save-restriction
  13392. (cond ((eq scope 'tree)
  13393. (org-back-to-heading t)
  13394. (org-narrow-to-subtree)
  13395. (setq scope nil))
  13396. ((and (or (eq scope 'region) (eq scope 'region-start-level))
  13397. (org-region-active-p))
  13398. ;; If needed, set start-level to a string like "2"
  13399. (when start-level
  13400. (save-excursion
  13401. (goto-char (region-beginning))
  13402. (unless (org-at-heading-p) (outline-next-heading))
  13403. (setq start-level (org-current-level))))
  13404. (narrow-to-region (region-beginning)
  13405. (save-excursion
  13406. (goto-char (region-end))
  13407. (unless (and (bolp) (org-at-heading-p))
  13408. (outline-next-heading))
  13409. (point)))
  13410. (setq scope nil)))
  13411. (if (not scope)
  13412. (progn
  13413. (org-agenda-prepare-buffers
  13414. (list (buffer-file-name (current-buffer))))
  13415. (setq res (org-scan-tags func matcher todo-only start-level)))
  13416. ;; Get the right scope
  13417. (cond
  13418. ((and scope (listp scope) (symbolp (car scope)))
  13419. (setq scope (eval scope)))
  13420. ((eq scope 'agenda)
  13421. (setq scope (org-agenda-files t)))
  13422. ((eq scope 'agenda-with-archives)
  13423. (setq scope (org-agenda-files t))
  13424. (setq scope (org-add-archive-files scope)))
  13425. ((eq scope 'file)
  13426. (setq scope (list (buffer-file-name))))
  13427. ((eq scope 'file-with-archives)
  13428. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  13429. (org-agenda-prepare-buffers scope)
  13430. (while (setq file (pop scope))
  13431. (with-current-buffer (org-find-base-buffer-visiting file)
  13432. (save-excursion
  13433. (save-restriction
  13434. (widen)
  13435. (goto-char (point-min))
  13436. (setq res (append res (org-scan-tags func matcher todo-only))))))))))
  13437. res)))
  13438. ;;;; Properties
  13439. ;;; Setting and retrieving properties
  13440. (defconst org-special-properties
  13441. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  13442. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED" "FILE" "CLOCKSUM" "CLOCKSUM_T")
  13443. "The special properties valid in Org-mode.
  13444. These are properties that are not defined in the property drawer,
  13445. but in some other way.")
  13446. (defconst org-default-properties
  13447. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  13448. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  13449. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  13450. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  13451. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  13452. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  13453. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  13454. "Some properties that are used by Org-mode for various purposes.
  13455. Being in this list makes sure that they are offered for completion.")
  13456. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  13457. "Regular expression matching the first line of a property drawer.")
  13458. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  13459. "Regular expression matching the last line of a property drawer.")
  13460. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  13461. "Regular expression matching the first line of a property drawer.")
  13462. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  13463. "Regular expression matching the first line of a property drawer.")
  13464. (defconst org-property-drawer-re
  13465. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  13466. org-property-end-re "\\)\n?")
  13467. "Matches an entire property drawer.")
  13468. (defconst org-clock-drawer-re
  13469. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  13470. org-property-end-re "\\)\n?")
  13471. "Matches an entire clock drawer.")
  13472. (defsubst org-re-property (property)
  13473. "Return a regexp matching a PROPERTY line.
  13474. Match group 1 will be set to the value."
  13475. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)"))
  13476. (defsubst org-re-property-keyword (property)
  13477. "Return a regexp matching a PROPERTY line, possibly with no
  13478. value for the property."
  13479. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)?"))
  13480. (defun org-property-action ()
  13481. "Do an action on properties."
  13482. (interactive)
  13483. (let (c)
  13484. (org-at-property-p)
  13485. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  13486. (setq c (read-char-exclusive))
  13487. (cond
  13488. ((equal c ?s)
  13489. (call-interactively 'org-set-property))
  13490. ((equal c ?d)
  13491. (call-interactively 'org-delete-property))
  13492. ((equal c ?D)
  13493. (call-interactively 'org-delete-property-globally))
  13494. ((equal c ?c)
  13495. (call-interactively 'org-compute-property-at-point))
  13496. (t (error "No such property action %c" c)))))
  13497. (defun org-inc-effort ()
  13498. "Increment the value of the effort property in the current entry."
  13499. (interactive)
  13500. (org-set-effort nil t))
  13501. (defvar org-clock-effort) ;; Defined in org-clock.el
  13502. (defvar org-clock-current-task) ;; Defined in org-clock.el
  13503. (defun org-set-effort (&optional value increment)
  13504. "Set the effort property of the current entry.
  13505. With numerical prefix arg, use the nth allowed value, 0 stands for the
  13506. 10th allowed value.
  13507. When INCREMENT is non-nil, set the property to the next allowed value."
  13508. (interactive "P")
  13509. (if (equal value 0) (setq value 10))
  13510. (let* ((completion-ignore-case t)
  13511. (prop org-effort-property)
  13512. (cur (org-entry-get nil prop))
  13513. (allowed (org-property-get-allowed-values nil prop 'table))
  13514. (existing (mapcar 'list (org-property-values prop)))
  13515. (heading (nth 4 (org-heading-components)))
  13516. rpl
  13517. (val (cond
  13518. ((stringp value) value)
  13519. ((and allowed (integerp value))
  13520. (or (car (nth (1- value) allowed))
  13521. (car (org-last allowed))))
  13522. ((and allowed increment)
  13523. (or (caadr (member (list cur) allowed))
  13524. (error "Allowed effort values are not set")))
  13525. (allowed
  13526. (message "Select 1-9,0, [RET%s]: %s"
  13527. (if cur (concat "=" cur) "")
  13528. (mapconcat 'car allowed " "))
  13529. (setq rpl (read-char-exclusive))
  13530. (if (equal rpl ?\r)
  13531. cur
  13532. (setq rpl (- rpl ?0))
  13533. (if (equal rpl 0) (setq rpl 10))
  13534. (if (and (> rpl 0) (<= rpl (length allowed)))
  13535. (car (nth (1- rpl) allowed))
  13536. (org-completing-read "Effort: " allowed nil))))
  13537. (t
  13538. (let (org-completion-use-ido org-completion-use-iswitchb)
  13539. (org-completing-read
  13540. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  13541. (concat "[" cur "]") "")
  13542. ": ")
  13543. existing nil nil "" nil cur))))))
  13544. (unless (equal (org-entry-get nil prop) val)
  13545. (org-entry-put nil prop val))
  13546. (save-excursion
  13547. (org-back-to-heading t)
  13548. (put-text-property (point-at-bol) (point-at-eol) 'org-effort val))
  13549. (when (string= heading org-clock-current-task)
  13550. (setq org-clock-effort (get-text-property (point-at-bol) 'org-effort))
  13551. (org-clock-update-mode-line))
  13552. (message "%s is now %s" prop val)))
  13553. (defun org-at-property-p ()
  13554. "Is cursor inside a property drawer?"
  13555. (save-excursion
  13556. (beginning-of-line 1)
  13557. (when (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))
  13558. (save-match-data ;; Used by calling procedures
  13559. (let ((p (point))
  13560. (range (unless (org-before-first-heading-p)
  13561. (org-get-property-block))))
  13562. (and range (<= (car range) p) (< p (cdr range))))))))
  13563. (defun org-get-property-block (&optional beg end force)
  13564. "Return the (beg . end) range of the body of the property drawer.
  13565. BEG and END are the beginning and end of the current subtree, or of
  13566. the part before the first headline. If they are not given, they will
  13567. be found. If the drawer does not exist and FORCE is non-nil, create
  13568. the drawer."
  13569. (catch 'exit
  13570. (save-excursion
  13571. (let* ((beg (or beg (and (org-before-first-heading-p) (point-min))
  13572. (progn (org-back-to-heading t) (point))))
  13573. (end (or end (and (not (outline-next-heading)) (point-max))
  13574. (point))))
  13575. (goto-char beg)
  13576. (if (re-search-forward org-property-start-re end t)
  13577. (setq beg (1+ (match-end 0)))
  13578. (if force
  13579. (save-excursion
  13580. (org-insert-property-drawer)
  13581. (setq end (progn (outline-next-heading) (point))))
  13582. (throw 'exit nil))
  13583. (goto-char beg)
  13584. (if (re-search-forward org-property-start-re end t)
  13585. (setq beg (1+ (match-end 0)))))
  13586. (if (re-search-forward org-property-end-re end t)
  13587. (setq end (match-beginning 0))
  13588. (or force (throw 'exit nil))
  13589. (goto-char beg)
  13590. (setq end beg)
  13591. (org-indent-line)
  13592. (insert ":END:\n"))
  13593. (cons beg end)))))
  13594. (defun org-entry-properties (&optional pom which specific)
  13595. "Get all properties of the entry at point-or-marker POM.
  13596. This includes the TODO keyword, the tags, time strings for deadline,
  13597. scheduled, and clocking, and any additional properties defined in the
  13598. entry. The return value is an alist, keys may occur multiple times
  13599. if the property key was used several times.
  13600. POM may also be nil, in which case the current entry is used.
  13601. If WHICH is nil or `all', get all properties. If WHICH is
  13602. `special' or `standard', only get that subclass. If WHICH
  13603. is a string only get exactly this property. SPECIFIC can be a string, the
  13604. specific property we are interested in. Specifying it can speed
  13605. things up because then unnecessary parsing is avoided."
  13606. (setq which (or which 'all))
  13607. (org-with-point-at pom
  13608. (let ((clockstr (substring org-clock-string 0 -1))
  13609. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
  13610. (case-fold-search nil)
  13611. beg end range props sum-props key key1 value string clocksum clocksumt)
  13612. (save-excursion
  13613. (when (condition-case nil
  13614. (and (derived-mode-p 'org-mode) (org-back-to-heading t))
  13615. (error nil))
  13616. (setq beg (point))
  13617. (setq sum-props (get-text-property (point) 'org-summaries))
  13618. (setq clocksum (get-text-property (point) :org-clock-minutes)
  13619. clocksumt (get-text-property (point) :org-clock-minutes-today))
  13620. (outline-next-heading)
  13621. (setq end (point))
  13622. (when (memq which '(all special))
  13623. ;; Get the special properties, like TODO and tags
  13624. (goto-char beg)
  13625. (when (and (or (not specific) (string= specific "TODO"))
  13626. (looking-at org-todo-line-regexp) (match-end 2))
  13627. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  13628. (when (and (or (not specific) (string= specific "PRIORITY"))
  13629. (looking-at org-priority-regexp))
  13630. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  13631. (when (or (not specific) (string= specific "FILE"))
  13632. (push (cons "FILE" buffer-file-name) props))
  13633. (when (and (or (not specific) (string= specific "TAGS"))
  13634. (setq value (org-get-tags-string))
  13635. (string-match "\\S-" value))
  13636. (push (cons "TAGS" value) props))
  13637. (when (and (or (not specific) (string= specific "ALLTAGS"))
  13638. (setq value (org-get-tags-at)))
  13639. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
  13640. ":"))
  13641. props))
  13642. (when (or (not specific) (string= specific "BLOCKED"))
  13643. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
  13644. (when (or (not specific)
  13645. (member specific
  13646. '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
  13647. "TIMESTAMP" "TIMESTAMP_IA")))
  13648. (catch 'match
  13649. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  13650. (setq key (if (match-end 1)
  13651. (substring (org-match-string-no-properties 1)
  13652. 0 -1))
  13653. string (if (equal key clockstr)
  13654. (org-trim
  13655. (buffer-substring-no-properties
  13656. (match-beginning 3) (goto-char
  13657. (point-at-eol))))
  13658. (substring (org-match-string-no-properties 3)
  13659. 1 -1)))
  13660. ;; Get the correct property name from the key. This is
  13661. ;; necessary if the user has configured time keywords.
  13662. (setq key1 (concat key ":"))
  13663. (cond
  13664. ((not key)
  13665. (setq key
  13666. (if (= (char-after (match-beginning 3)) ?\[)
  13667. "TIMESTAMP_IA" "TIMESTAMP")))
  13668. ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
  13669. ((equal key1 org-deadline-string) (setq key "DEADLINE"))
  13670. ((equal key1 org-closed-string) (setq key "CLOSED"))
  13671. ((equal key1 org-clock-string) (setq key "CLOCK")))
  13672. (if (and specific (equal key specific) (not (equal key "CLOCK")))
  13673. (progn
  13674. (push (cons key string) props)
  13675. ;; no need to search further if match is found
  13676. (throw 'match t))
  13677. (when (or (equal key "CLOCK") (not (assoc key props)))
  13678. (push (cons key string) props)))))))
  13679. (when (memq which '(all standard))
  13680. ;; Get the standard properties, like :PROP: ...
  13681. (setq range (org-get-property-block beg end))
  13682. (when range
  13683. (goto-char (car range))
  13684. (while (re-search-forward
  13685. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  13686. (cdr range) t)
  13687. (setq key (org-match-string-no-properties 1)
  13688. value (org-trim (or (org-match-string-no-properties 2) "")))
  13689. (unless (member key excluded)
  13690. (push (cons key (or value "")) props)))))
  13691. (if clocksum
  13692. (push (cons "CLOCKSUM"
  13693. (org-columns-number-to-string (/ (float clocksum) 60.)
  13694. 'add_times))
  13695. props))
  13696. (if clocksumt
  13697. (push (cons "CLOCKSUM_T"
  13698. (org-columns-number-to-string (/ (float clocksumt) 60.)
  13699. 'add_times))
  13700. props))
  13701. (unless (assoc "CATEGORY" props)
  13702. (push (cons "CATEGORY" (org-get-category)) props))
  13703. (append sum-props (nreverse props)))))))
  13704. (defun org-entry-get (pom property &optional inherit literal-nil)
  13705. "Get value of PROPERTY for entry or content at point-or-marker POM.
  13706. If INHERIT is non-nil and the entry does not have the property,
  13707. then also check higher levels of the hierarchy.
  13708. If INHERIT is the symbol `selective', use inheritance only if the setting
  13709. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  13710. If the property is present but empty, the return value is the empty string.
  13711. If the property is not present at all, nil is returned.
  13712. If LITERAL-NIL is set, return the string value \"nil\" as a string,
  13713. do not interpret it as the list atom nil. This is used for inheritance
  13714. when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
  13715. (org-with-point-at pom
  13716. (if (and inherit (if (eq inherit 'selective)
  13717. (org-property-inherit-p property)
  13718. t))
  13719. (org-entry-get-with-inheritance property literal-nil)
  13720. (if (member property org-special-properties)
  13721. ;; We need a special property. Use `org-entry-properties' to
  13722. ;; retrieve it, but specify the wanted property
  13723. (cdr (assoc property (org-entry-properties nil 'special property)))
  13724. (let ((range (org-get-property-block)))
  13725. (when (and range (not (eq (car range) (cdr range))))
  13726. (let* ((props (list (or (assoc property org-file-properties)
  13727. (assoc property org-global-properties)
  13728. (assoc property org-global-properties-fixed))))
  13729. (ap (lambda (key)
  13730. (when (re-search-forward
  13731. (org-re-property key) (cdr range) t)
  13732. (setq props
  13733. (org-update-property-plist
  13734. key
  13735. (if (match-end 1)
  13736. (org-match-string-no-properties 1) "")
  13737. props)))))
  13738. val)
  13739. (goto-char (car range))
  13740. (funcall ap property)
  13741. (goto-char (car range))
  13742. (while (funcall ap (concat property "+")))
  13743. (setq val (cdr (assoc property props)))
  13744. (when val (if literal-nil val (org-not-nil val))))))))))
  13745. (defun org-property-or-variable-value (var &optional inherit)
  13746. "Check if there is a property fixing the value of VAR.
  13747. If yes, return this value. If not, return the current value of the variable."
  13748. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  13749. (if (and prop (stringp prop) (string-match "\\S-" prop))
  13750. (read prop)
  13751. (symbol-value var))))
  13752. (defun org-entry-delete (pom property &optional delete-empty-drawer)
  13753. "Delete the property PROPERTY from entry at point-or-marker POM.
  13754. When optional argument DELETE-EMPTY-DRAWER is a string, it defines
  13755. an empty drawer to delete."
  13756. (org-with-point-at pom
  13757. (if (member property org-special-properties)
  13758. nil ; cannot delete these properties.
  13759. (let ((range (org-get-property-block)))
  13760. (if (and range
  13761. (goto-char (car range))
  13762. (re-search-forward
  13763. (org-re-property property)
  13764. (cdr range) t))
  13765. (progn
  13766. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  13767. (and delete-empty-drawer
  13768. (org-remove-empty-drawer-at
  13769. delete-empty-drawer (car range)))
  13770. t)
  13771. nil)))))
  13772. ;; Multi-values properties are properties that contain multiple values
  13773. ;; These values are assumed to be single words, separated by whitespace.
  13774. (defun org-entry-add-to-multivalued-property (pom property value)
  13775. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  13776. (let* ((old (org-entry-get pom property))
  13777. (values (and old (org-split-string old "[ \t]"))))
  13778. (setq value (org-entry-protect-space value))
  13779. (unless (member value values)
  13780. (setq values (cons value values))
  13781. (org-entry-put pom property
  13782. (mapconcat 'identity values " ")))))
  13783. (defun org-entry-remove-from-multivalued-property (pom property value)
  13784. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  13785. (let* ((old (org-entry-get pom property))
  13786. (values (and old (org-split-string old "[ \t]"))))
  13787. (setq value (org-entry-protect-space value))
  13788. (when (member value values)
  13789. (setq values (delete value values))
  13790. (org-entry-put pom property
  13791. (mapconcat 'identity values " ")))))
  13792. (defun org-entry-member-in-multivalued-property (pom property value)
  13793. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  13794. (let* ((old (org-entry-get pom property))
  13795. (values (and old (org-split-string old "[ \t]"))))
  13796. (setq value (org-entry-protect-space value))
  13797. (member value values)))
  13798. (defun org-entry-get-multivalued-property (pom property)
  13799. "Return a list of values in a multivalued property."
  13800. (let* ((value (org-entry-get pom property))
  13801. (values (and value (org-split-string value "[ \t]"))))
  13802. (mapcar 'org-entry-restore-space values)))
  13803. (defun org-entry-put-multivalued-property (pom property &rest values)
  13804. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  13805. VALUES should be a list of strings. Spaces will be protected."
  13806. (org-entry-put pom property
  13807. (mapconcat 'org-entry-protect-space values " "))
  13808. (let* ((value (org-entry-get pom property))
  13809. (values (and value (org-split-string value "[ \t]"))))
  13810. (mapcar 'org-entry-restore-space values)))
  13811. (defun org-entry-protect-space (s)
  13812. "Protect spaces and newline in string S."
  13813. (while (string-match " " s)
  13814. (setq s (replace-match "%20" t t s)))
  13815. (while (string-match "\n" s)
  13816. (setq s (replace-match "%0A" t t s)))
  13817. s)
  13818. (defun org-entry-restore-space (s)
  13819. "Restore spaces and newline in string S."
  13820. (while (string-match "%20" s)
  13821. (setq s (replace-match " " t t s)))
  13822. (while (string-match "%0A" s)
  13823. (setq s (replace-match "\n" t t s)))
  13824. s)
  13825. (defvar org-entry-property-inherited-from (make-marker)
  13826. "Marker pointing to the entry from where a property was inherited.
  13827. Each call to `org-entry-get-with-inheritance' will set this marker to the
  13828. location of the entry where the inheritance search matched. If there was
  13829. no match, the marker will point nowhere.
  13830. Note that also `org-entry-get' calls this function, if the INHERIT flag
  13831. is set.")
  13832. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  13833. "Get PROPERTY of entry or content at point, search higher levels if needed.
  13834. The search will stop at the first ancestor which has the property defined.
  13835. If the value found is \"nil\", return nil to show that the property
  13836. should be considered as undefined (this is the meaning of nil here).
  13837. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  13838. (move-marker org-entry-property-inherited-from nil)
  13839. (let (tmp)
  13840. (save-excursion
  13841. (save-restriction
  13842. (widen)
  13843. (catch 'ex
  13844. (while t
  13845. (when (setq tmp (org-entry-get nil property nil 'literal-nil))
  13846. (or (ignore-errors (org-back-to-heading t))
  13847. (goto-char (point-min)))
  13848. (move-marker org-entry-property-inherited-from (point))
  13849. (throw 'ex tmp))
  13850. (or (ignore-errors (org-up-heading-safe))
  13851. (throw 'ex nil))))))
  13852. (setq tmp (or tmp
  13853. (cdr (assoc property org-file-properties))
  13854. (cdr (assoc property org-global-properties))
  13855. (cdr (assoc property org-global-properties-fixed))))
  13856. (if literal-nil tmp (org-not-nil tmp))))
  13857. (defvar org-property-changed-functions nil
  13858. "Hook called when the value of a property has changed.
  13859. Each hook function should accept two arguments, the name of the property
  13860. and the new value.")
  13861. (defun org-entry-put (pom property value)
  13862. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  13863. (org-with-point-at pom
  13864. (org-back-to-heading t)
  13865. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  13866. range)
  13867. (cond
  13868. ((equal property "TODO")
  13869. (when (and (stringp value) (string-match "\\S-" value)
  13870. (not (member value org-todo-keywords-1)))
  13871. (error "\"%s\" is not a valid TODO state" value))
  13872. (if (or (not value)
  13873. (not (string-match "\\S-" value)))
  13874. (setq value 'none))
  13875. (org-todo value)
  13876. (org-set-tags nil 'align))
  13877. ((equal property "PRIORITY")
  13878. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  13879. (string-to-char value) ?\ ))
  13880. (org-set-tags nil 'align))
  13881. ((equal property "CLOCKSUM")
  13882. (if (not (re-search-forward
  13883. (concat org-clock-string ".*\\]--\\(\\[[^]]+\\]\\)") nil t))
  13884. (error "Cannot find a clock log")
  13885. (goto-char (- (match-end 1) 2))
  13886. (cond
  13887. ((eq value 'earlier) (org-timestamp-down))
  13888. ((eq value 'later) (org-timestamp-up)))
  13889. (org-clock-sum-current-item)))
  13890. ((equal property "SCHEDULED")
  13891. (if (re-search-forward org-scheduled-time-regexp end t)
  13892. (cond
  13893. ((eq value 'earlier) (org-timestamp-change -1 'day))
  13894. ((eq value 'later) (org-timestamp-change 1 'day))
  13895. (t (call-interactively 'org-schedule)))
  13896. (call-interactively 'org-schedule)))
  13897. ((equal property "DEADLINE")
  13898. (if (re-search-forward org-deadline-time-regexp end t)
  13899. (cond
  13900. ((eq value 'earlier) (org-timestamp-change -1 'day))
  13901. ((eq value 'later) (org-timestamp-change 1 'day))
  13902. (t (call-interactively 'org-deadline)))
  13903. (call-interactively 'org-deadline)))
  13904. ((member property org-special-properties)
  13905. (error "The %s property can not yet be set with `org-entry-put'"
  13906. property))
  13907. (t ; a non-special property
  13908. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  13909. (setq range (org-get-property-block beg end 'force))
  13910. (goto-char (car range))
  13911. (if (re-search-forward
  13912. (org-re-property-keyword property) (cdr range) t)
  13913. (progn
  13914. (delete-region (match-beginning 0) (match-end 0))
  13915. (goto-char (match-beginning 0)))
  13916. (goto-char (cdr range))
  13917. (insert "\n")
  13918. (backward-char 1)
  13919. (org-indent-line))
  13920. (insert ":" property ":")
  13921. (and value (insert " " value))
  13922. (org-indent-line)))))
  13923. (run-hook-with-args 'org-property-changed-functions property value)))
  13924. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  13925. "Get all property keys in the current buffer.
  13926. With INCLUDE-SPECIALS, also list the special properties that reflect things
  13927. like tags and TODO state.
  13928. With INCLUDE-DEFAULTS, also include properties that has special meaning
  13929. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING
  13930. and others.
  13931. With INCLUDE-COLUMNS, also include property names given in COLUMN
  13932. formats in the current buffer."
  13933. (let (rtn range cfmt s p)
  13934. (save-excursion
  13935. (save-restriction
  13936. (widen)
  13937. (goto-char (point-min))
  13938. (while (re-search-forward org-property-start-re nil t)
  13939. (setq range (org-get-property-block))
  13940. (goto-char (car range))
  13941. (while (re-search-forward
  13942. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  13943. (cdr range) t)
  13944. (add-to-list 'rtn (org-match-string-no-properties 1)))
  13945. (outline-next-heading))))
  13946. (when include-specials
  13947. (setq rtn (append org-special-properties rtn)))
  13948. (when include-defaults
  13949. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  13950. (add-to-list 'rtn org-effort-property))
  13951. (when include-columns
  13952. (save-excursion
  13953. (save-restriction
  13954. (widen)
  13955. (goto-char (point-min))
  13956. (while (re-search-forward
  13957. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  13958. nil t)
  13959. (setq cfmt (match-string 2) s 0)
  13960. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  13961. cfmt s)
  13962. (setq s (match-end 0)
  13963. p (match-string 1 cfmt))
  13964. (unless (or (equal p "ITEM")
  13965. (member p org-special-properties))
  13966. (add-to-list 'rtn (match-string 1 cfmt))))))))
  13967. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  13968. (defun org-property-values (key)
  13969. "Return a list of all values of property KEY in the current buffer."
  13970. (save-excursion
  13971. (save-restriction
  13972. (widen)
  13973. (goto-char (point-min))
  13974. (let ((re (org-re-property key))
  13975. values)
  13976. (while (re-search-forward re nil t)
  13977. (add-to-list 'values (org-trim (match-string 1))))
  13978. (delete "" values)))))
  13979. (defun org-insert-property-drawer ()
  13980. "Insert a property drawer into the current entry."
  13981. (org-back-to-heading t)
  13982. (looking-at org-outline-regexp)
  13983. (let ((indent (if org-adapt-indentation
  13984. (- (match-end 0) (match-beginning 0))
  13985. 0))
  13986. (beg (point))
  13987. (re (concat "^[ \t]*" org-keyword-time-regexp))
  13988. end hiddenp)
  13989. (outline-next-heading)
  13990. (setq end (point))
  13991. (goto-char beg)
  13992. (while (re-search-forward re end t))
  13993. (setq hiddenp (outline-invisible-p))
  13994. (end-of-line 1)
  13995. (and (equal (char-after) ?\n) (forward-char 1))
  13996. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  13997. (if (member (match-string 1) '("CLOCK:" ":END:"))
  13998. ;; just skip this line
  13999. (beginning-of-line 2)
  14000. ;; Drawer start, find the end
  14001. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  14002. (beginning-of-line 1)))
  14003. (org-skip-over-state-notes)
  14004. (skip-chars-backward " \t\n\r")
  14005. (if (eq (char-before) ?*) (forward-char 1))
  14006. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  14007. (beginning-of-line 0)
  14008. (org-indent-to-column indent)
  14009. (beginning-of-line 2)
  14010. (org-indent-to-column indent)
  14011. (beginning-of-line 0)
  14012. (if hiddenp
  14013. (save-excursion
  14014. (org-back-to-heading t)
  14015. (hide-entry))
  14016. (org-flag-drawer t))))
  14017. (defun org-insert-drawer (&optional arg drawer)
  14018. "Insert a drawer at point.
  14019. Optional argument DRAWER, when non-nil, is a string representing
  14020. drawer's name. Otherwise, the user is prompted for a name.
  14021. If a region is active, insert the drawer around that region
  14022. instead.
  14023. Point is left between drawer's boundaries."
  14024. (interactive "P")
  14025. (let* ((logbook (if (stringp org-log-into-drawer) org-log-into-drawer
  14026. "LOGBOOK"))
  14027. ;; SYSTEM-DRAWERS is a list of drawer names that are used
  14028. ;; internally by Org. They are meant to be inserted
  14029. ;; automatically.
  14030. (system-drawers `("CLOCK" ,logbook "PROPERTIES"))
  14031. ;; Remove system drawers from list. Note: For some reason,
  14032. ;; `org-completing-read' ignores the predicate while
  14033. ;; `completing-read' handles it fine.
  14034. (drawer (if arg "PROPERTIES"
  14035. (or drawer
  14036. (completing-read
  14037. "Drawer: " org-drawers
  14038. (lambda (d) (not (member d system-drawers))))))))
  14039. (cond
  14040. ;; With C-u, fall back on `org-insert-property-drawer'
  14041. (arg (org-insert-property-drawer))
  14042. ;; With an active region, insert a drawer at point.
  14043. ((not (org-region-active-p))
  14044. (progn
  14045. (unless (bolp) (insert "\n"))
  14046. (insert (format ":%s:\n\n:END:\n" drawer))
  14047. (forward-line -2)))
  14048. ;; Otherwise, insert the drawer at point
  14049. (t
  14050. (let ((rbeg (region-beginning))
  14051. (rend (copy-marker (region-end))))
  14052. (unwind-protect
  14053. (progn
  14054. (goto-char rbeg)
  14055. (beginning-of-line)
  14056. (when (save-excursion
  14057. (re-search-forward org-outline-regexp-bol rend t))
  14058. (error "Drawers cannot contain headlines"))
  14059. ;; Position point at the beginning of the first
  14060. ;; non-blank line in region. Insert drawer's opening
  14061. ;; there, then indent it.
  14062. (org-skip-whitespace)
  14063. (beginning-of-line)
  14064. (insert ":" drawer ":\n")
  14065. (forward-line -1)
  14066. (indent-for-tab-command)
  14067. ;; Move point to the beginning of the first blank line
  14068. ;; after the last non-blank line in region. Insert
  14069. ;; drawer's closing, then indent it.
  14070. (goto-char rend)
  14071. (skip-chars-backward " \r\t\n")
  14072. (insert "\n:END:")
  14073. (deactivate-mark t)
  14074. (indent-for-tab-command)
  14075. (unless (eolp) (insert "\n")))
  14076. ;; Clear marker, whatever the outcome of insertion is.
  14077. (set-marker rend nil)))))))
  14078. (defvar org-property-set-functions-alist nil
  14079. "Property set function alist.
  14080. Each entry should have the following format:
  14081. (PROPERTY . READ-FUNCTION)
  14082. The read function will be called with the same argument as
  14083. `org-completing-read'.")
  14084. (defun org-set-property-function (property)
  14085. "Get the function that should be used to set PROPERTY.
  14086. This is computed according to `org-property-set-functions-alist'."
  14087. (or (cdr (assoc property org-property-set-functions-alist))
  14088. 'org-completing-read))
  14089. (defun org-read-property-value (property)
  14090. "Read PROPERTY value from user."
  14091. (let* ((completion-ignore-case t)
  14092. (allowed (org-property-get-allowed-values nil property 'table))
  14093. (cur (org-entry-get nil property))
  14094. (prompt (concat property " value"
  14095. (if (and cur (string-match "\\S-" cur))
  14096. (concat " [" cur "]") "") ": "))
  14097. (set-function (org-set-property-function property))
  14098. (val (if allowed
  14099. (funcall set-function prompt allowed nil
  14100. (not (get-text-property 0 'org-unrestricted
  14101. (caar allowed))))
  14102. (let (org-completion-use-ido org-completion-use-iswitchb)
  14103. (funcall set-function prompt
  14104. (mapcar 'list (org-property-values property))
  14105. nil nil "" nil cur)))))
  14106. (if (equal val "")
  14107. cur
  14108. val)))
  14109. (defvar org-last-set-property nil)
  14110. (defvar org-last-set-property-value nil)
  14111. (defun org-read-property-name ()
  14112. "Read a property name."
  14113. (let* ((completion-ignore-case t)
  14114. (keys (org-buffer-property-keys nil t t))
  14115. (default-prop (or (save-excursion
  14116. (save-match-data
  14117. (beginning-of-line)
  14118. (and (looking-at "^\\s-*:\\([^:\n]+\\):")
  14119. (null (string= (match-string 1) "END"))
  14120. (match-string 1))))
  14121. org-last-set-property))
  14122. (property (org-icompleting-read
  14123. (concat "Property"
  14124. (if default-prop (concat " [" default-prop "]") "")
  14125. ": ")
  14126. (mapcar 'list keys)
  14127. nil nil nil nil
  14128. default-prop)))
  14129. (if (member property keys)
  14130. property
  14131. (or (cdr (assoc (downcase property)
  14132. (mapcar (lambda (x) (cons (downcase x) x))
  14133. keys)))
  14134. property))))
  14135. (defun org-set-property-and-value (use-last)
  14136. "Allow to set [PROPERTY]: [value] direction from prompt.
  14137. When use-default, don't even ask, just use the last
  14138. \"[PROPERTY]: [value]\" string from the history."
  14139. (interactive "P")
  14140. (let* ((completion-ignore-case t)
  14141. (pv (or (and use-last org-last-set-property-value)
  14142. (org-completing-read
  14143. "Enter a \"[Property]: [value]\" pair: "
  14144. nil nil nil nil nil
  14145. org-last-set-property-value)))
  14146. prop val)
  14147. (when (string-match "^[ \t]*\\([^:]+\\):[ \t]*\\(.*\\)[ \t]*$" pv)
  14148. (setq prop (match-string 1 pv)
  14149. val (match-string 2 pv))
  14150. (org-set-property prop val))))
  14151. (defun org-set-property (property value)
  14152. "In the current entry, set PROPERTY to VALUE.
  14153. When called interactively, this will prompt for a property name, offering
  14154. completion on existing and default properties. And then it will prompt
  14155. for a value, offering completion either on allowed values (via an inherited
  14156. xxx_ALL property) or on existing values in other instances of this property
  14157. in the current file."
  14158. (interactive (list nil nil))
  14159. (let* ((property (or property (org-read-property-name)))
  14160. (value (or value (org-read-property-value property)))
  14161. (fn (cdr (assoc property org-properties-postprocess-alist))))
  14162. (setq org-last-set-property property)
  14163. (setq org-last-set-property-value (concat property ": " value))
  14164. ;; Possibly postprocess the inserted value:
  14165. (when fn (setq value (funcall fn value)))
  14166. (unless (equal (org-entry-get nil property) value)
  14167. (org-entry-put nil property value))))
  14168. (defun org-delete-property (property &optional delete-empty-drawer)
  14169. "In the current entry, delete PROPERTY.
  14170. When optional argument DELETE-EMPTY-DRAWER is a string, it defines
  14171. an empty drawer to delete."
  14172. (interactive
  14173. (let* ((completion-ignore-case t)
  14174. (prop (org-icompleting-read "Property: "
  14175. (org-entry-properties nil 'standard))))
  14176. (list prop)))
  14177. (message "Property %s %s" property
  14178. (if (org-entry-delete nil property delete-empty-drawer)
  14179. "deleted"
  14180. "was not present in the entry")))
  14181. (defun org-delete-property-globally (property)
  14182. "Remove PROPERTY globally, from all entries."
  14183. (interactive
  14184. (let* ((completion-ignore-case t)
  14185. (prop (org-icompleting-read
  14186. "Globally remove property: "
  14187. (mapcar 'list (org-buffer-property-keys)))))
  14188. (list prop)))
  14189. (save-excursion
  14190. (save-restriction
  14191. (widen)
  14192. (goto-char (point-min))
  14193. (let ((cnt 0))
  14194. (while (re-search-forward
  14195. (org-re-property property)
  14196. nil t)
  14197. (setq cnt (1+ cnt))
  14198. (delete-region (match-beginning 0) (1+ (point-at-eol))))
  14199. (message "Property \"%s\" removed from %d entries" property cnt)))))
  14200. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  14201. (defun org-compute-property-at-point ()
  14202. "Compute the property at point.
  14203. This looks for an enclosing column format, extracts the operator and
  14204. then applies it to the property in the column format's scope."
  14205. (interactive)
  14206. (unless (org-at-property-p)
  14207. (error "Not at a property"))
  14208. (let ((prop (org-match-string-no-properties 2)))
  14209. (org-columns-get-format-and-top-level)
  14210. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  14211. (error "No operator defined for property %s" prop))
  14212. (org-columns-compute prop)))
  14213. (defvar org-property-allowed-value-functions nil
  14214. "Hook for functions supplying allowed values for a specific property.
  14215. The functions must take a single argument, the name of the property, and
  14216. return a flat list of allowed values. If \":ETC\" is one of
  14217. the values, this means that these values are intended as defaults for
  14218. completion, but that other values should be allowed too.
  14219. The functions must return nil if they are not responsible for this
  14220. property.")
  14221. (defun org-property-get-allowed-values (pom property &optional table)
  14222. "Get allowed values for the property PROPERTY.
  14223. When TABLE is non-nil, return an alist that can directly be used for
  14224. completion."
  14225. (let (vals)
  14226. (cond
  14227. ((equal property "TODO")
  14228. (setq vals (org-with-point-at pom
  14229. (append org-todo-keywords-1 '("")))))
  14230. ((equal property "PRIORITY")
  14231. (let ((n org-lowest-priority))
  14232. (while (>= n org-highest-priority)
  14233. (push (char-to-string n) vals)
  14234. (setq n (1- n)))))
  14235. ((member property org-special-properties))
  14236. ((setq vals (run-hook-with-args-until-success
  14237. 'org-property-allowed-value-functions property)))
  14238. (t
  14239. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  14240. (when (and vals (string-match "\\S-" vals))
  14241. (setq vals (car (read-from-string (concat "(" vals ")"))))
  14242. (setq vals (mapcar (lambda (x)
  14243. (cond ((stringp x) x)
  14244. ((numberp x) (number-to-string x))
  14245. ((symbolp x) (symbol-name x))
  14246. (t "???")))
  14247. vals)))))
  14248. (when (member ":ETC" vals)
  14249. (setq vals (remove ":ETC" vals))
  14250. (org-add-props (car vals) '(org-unrestricted t)))
  14251. (if table (mapcar 'list vals) vals)))
  14252. (defun org-property-previous-allowed-value (&optional previous)
  14253. "Switch to the next allowed value for this property."
  14254. (interactive)
  14255. (org-property-next-allowed-value t))
  14256. (defun org-property-next-allowed-value (&optional previous)
  14257. "Switch to the next allowed value for this property."
  14258. (interactive)
  14259. (unless (org-at-property-p)
  14260. (error "Not at a property"))
  14261. (let* ((prop (car (save-match-data (org-split-string (match-string 1) ":"))))
  14262. (key (match-string 2))
  14263. (value (match-string 3))
  14264. (allowed (or (org-property-get-allowed-values (point) key)
  14265. (and (member value '("[ ]" "[-]" "[X]"))
  14266. '("[ ]" "[X]"))))
  14267. (heading (save-match-data (nth 4 (org-heading-components))))
  14268. nval)
  14269. (unless allowed
  14270. (error "Allowed values for this property have not been defined"))
  14271. (if previous (setq allowed (reverse allowed)))
  14272. (if (member value allowed)
  14273. (setq nval (car (cdr (member value allowed)))))
  14274. (setq nval (or nval (car allowed)))
  14275. (if (equal nval value)
  14276. (error "Only one allowed value for this property"))
  14277. (org-at-property-p)
  14278. (replace-match (concat " :" key ": " nval) t t)
  14279. (org-indent-line)
  14280. (beginning-of-line 1)
  14281. (skip-chars-forward " \t")
  14282. (when (equal prop org-effort-property)
  14283. (save-excursion
  14284. (org-back-to-heading t)
  14285. (put-text-property (point-at-bol) (point-at-eol) 'org-effort nval))
  14286. (when (string= org-clock-current-task heading)
  14287. (setq org-clock-effort nval)
  14288. (org-clock-update-mode-line)))
  14289. (run-hook-with-args 'org-property-changed-functions key nval)))
  14290. (defun org-find-olp (path &optional this-buffer)
  14291. "Return a marker pointing to the entry at outline path OLP.
  14292. If anything goes wrong, throw an error.
  14293. You can wrap this call to catch the error like this:
  14294. (condition-case msg
  14295. (org-mobile-locate-entry (match-string 4))
  14296. (error (nth 1 msg)))
  14297. The return value will then be either a string with the error message,
  14298. or a marker if everything is OK.
  14299. If THIS-BUFFER is set, the outline path does not contain a file,
  14300. only headings."
  14301. (let* ((file (if this-buffer buffer-file-name (pop path)))
  14302. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  14303. (level 1)
  14304. (lmin 1)
  14305. (lmax 1)
  14306. limit re end found pos heading cnt flevel)
  14307. (unless buffer (error "File not found :%s" file))
  14308. (with-current-buffer buffer
  14309. (save-excursion
  14310. (save-restriction
  14311. (widen)
  14312. (setq limit (point-max))
  14313. (goto-char (point-min))
  14314. (while (setq heading (pop path))
  14315. (setq re (format org-complex-heading-regexp-format
  14316. (regexp-quote heading)))
  14317. (setq cnt 0 pos (point))
  14318. (while (re-search-forward re end t)
  14319. (setq level (- (match-end 1) (match-beginning 1)))
  14320. (if (and (>= level lmin) (<= level lmax))
  14321. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  14322. (when (= cnt 0) (error "Heading not found on level %d: %s"
  14323. lmax heading))
  14324. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  14325. lmax heading))
  14326. (goto-char found)
  14327. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  14328. (setq end (save-excursion (org-end-of-subtree t t))))
  14329. (when (org-at-heading-p)
  14330. (point-marker)))))))
  14331. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  14332. "Find node HEADING in BUFFER.
  14333. Return a marker to the heading if it was found, or nil if not.
  14334. If POS-ONLY is set, return just the position instead of a marker.
  14335. The heading text must match exact, but it may have a TODO keyword,
  14336. a priority cookie and tags in the standard locations."
  14337. (with-current-buffer (or buffer (current-buffer))
  14338. (save-excursion
  14339. (save-restriction
  14340. (widen)
  14341. (goto-char (point-min))
  14342. (let (case-fold-search)
  14343. (if (re-search-forward
  14344. (format org-complex-heading-regexp-format
  14345. (regexp-quote heading)) nil t)
  14346. (if pos-only
  14347. (match-beginning 0)
  14348. (move-marker (make-marker) (match-beginning 0)))))))))
  14349. (defun org-find-exact-heading-in-directory (heading &optional dir)
  14350. "Find Org node headline HEADING in all .org files in directory DIR.
  14351. When the target headline is found, return a marker to this location."
  14352. (let ((files (directory-files (or dir default-directory)
  14353. nil "\\`[^.#].*\\.org\\'"))
  14354. file visiting m buffer)
  14355. (catch 'found
  14356. (while (setq file (pop files))
  14357. (message "trying %s" file)
  14358. (setq visiting (org-find-base-buffer-visiting file))
  14359. (setq buffer (or visiting (find-file-noselect file)))
  14360. (setq m (org-find-exact-headline-in-buffer
  14361. heading buffer))
  14362. (when (and (not m) (not visiting)) (kill-buffer buffer))
  14363. (and m (throw 'found m))))))
  14364. (defun org-find-entry-with-id (ident)
  14365. "Locate the entry that contains the ID property with exact value IDENT.
  14366. IDENT can be a string, a symbol or a number, this function will search for
  14367. the string representation of it.
  14368. Return the position where this entry starts, or nil if there is no such entry."
  14369. (interactive "sID: ")
  14370. (let ((id (cond
  14371. ((stringp ident) ident)
  14372. ((symbol-name ident) (symbol-name ident))
  14373. ((numberp ident) (number-to-string ident))
  14374. (t (error "IDENT %s must be a string, symbol or number" ident))))
  14375. (case-fold-search nil))
  14376. (save-excursion
  14377. (save-restriction
  14378. (widen)
  14379. (goto-char (point-min))
  14380. (when (re-search-forward
  14381. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  14382. nil t)
  14383. (org-back-to-heading t)
  14384. (point))))))
  14385. ;;;; Timestamps
  14386. (defvar org-last-changed-timestamp nil)
  14387. (defvar org-last-inserted-timestamp nil
  14388. "The last time stamp inserted with `org-insert-time-stamp'.")
  14389. (defvar org-time-was-given) ; dynamically scoped parameter
  14390. (defvar org-end-time-was-given) ; dynamically scoped parameter
  14391. (defvar org-ts-what) ; dynamically scoped parameter
  14392. (defun org-time-stamp (arg &optional inactive)
  14393. "Prompt for a date/time and insert a time stamp.
  14394. If the user specifies a time like HH:MM or if this command is
  14395. called with at least one prefix argument, the time stamp contains
  14396. the date and the time. Otherwise, only the date is be included.
  14397. All parts of a date not specified by the user is filled in from
  14398. the current date/time. So if you just press return without
  14399. typing anything, the time stamp will represent the current
  14400. date/time.
  14401. If there is already a timestamp at the cursor, it will be
  14402. modified.
  14403. With two universal prefix arguments, insert an active timestamp
  14404. with the current time without prompting the user."
  14405. (interactive "P")
  14406. (let* ((ts nil)
  14407. (default-time
  14408. ;; Default time is either today, or, when entering a range,
  14409. ;; the range start.
  14410. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  14411. (save-excursion
  14412. (re-search-backward
  14413. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  14414. (- (point) 20) t)))
  14415. (apply 'encode-time (org-parse-time-string (match-string 1)))
  14416. (current-time)))
  14417. (default-input (and ts (org-get-compact-tod ts)))
  14418. (repeater (save-excursion
  14419. (save-match-data
  14420. (beginning-of-line)
  14421. (when (re-search-forward
  14422. "\\([.+-]+[0-9]+[hdwmy] ?\\)+" ;;\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  14423. (save-excursion (progn (end-of-line) (point))) t)
  14424. (match-string 0)))))
  14425. org-time-was-given org-end-time-was-given time)
  14426. (cond
  14427. ((and (org-at-timestamp-p t)
  14428. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  14429. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  14430. (insert "--")
  14431. (setq time (let ((this-command this-command))
  14432. (org-read-date arg 'totime nil nil
  14433. default-time default-input inactive)))
  14434. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  14435. ((org-at-timestamp-p t)
  14436. (setq time (let ((this-command this-command))
  14437. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  14438. (when (org-at-timestamp-p t) ; just to get the match data
  14439. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  14440. (replace-match "")
  14441. (setq org-last-changed-timestamp
  14442. (org-insert-time-stamp
  14443. time (or org-time-was-given arg)
  14444. inactive nil nil (list org-end-time-was-given)))
  14445. (when repeater (goto-char (1- (point))) (insert " " repeater)
  14446. (setq org-last-changed-timestamp
  14447. (concat (substring org-last-inserted-timestamp 0 -1)
  14448. " " repeater ">"))))
  14449. (message "Timestamp updated"))
  14450. ((equal arg '(16))
  14451. (org-insert-time-stamp (current-time) t))
  14452. (t
  14453. (setq time (let ((this-command this-command))
  14454. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  14455. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  14456. nil nil (list org-end-time-was-given))))))
  14457. ;; FIXME: can we use this for something else, like computing time differences?
  14458. (defun org-get-compact-tod (s)
  14459. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  14460. (let* ((t1 (match-string 1 s))
  14461. (h1 (string-to-number (match-string 2 s)))
  14462. (m1 (string-to-number (match-string 3 s)))
  14463. (t2 (and (match-end 4) (match-string 5 s)))
  14464. (h2 (and t2 (string-to-number (match-string 6 s))))
  14465. (m2 (and t2 (string-to-number (match-string 7 s))))
  14466. dh dm)
  14467. (if (not t2)
  14468. t1
  14469. (setq dh (- h2 h1) dm (- m2 m1))
  14470. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  14471. (concat t1 "+" (number-to-string dh)
  14472. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  14473. (defun org-time-stamp-inactive (&optional arg)
  14474. "Insert an inactive time stamp.
  14475. An inactive time stamp is enclosed in square brackets instead of angle
  14476. brackets. It is inactive in the sense that it does not trigger agenda entries,
  14477. does not link to the calendar and cannot be changed with the S-cursor keys.
  14478. So these are more for recording a certain time/date."
  14479. (interactive "P")
  14480. (org-time-stamp arg 'inactive))
  14481. (defvar org-date-ovl (make-overlay 1 1))
  14482. (overlay-put org-date-ovl 'face 'org-date-selected)
  14483. (org-detach-overlay org-date-ovl)
  14484. (defvar org-ans1) ; dynamically scoped parameter
  14485. (defvar org-ans2) ; dynamically scoped parameter
  14486. (defvar org-plain-time-of-day-regexp) ; defined below
  14487. (defvar org-overriding-default-time nil) ; dynamically scoped
  14488. (defvar org-read-date-overlay nil)
  14489. (defvar org-dcst nil) ; dynamically scoped
  14490. (defvar org-read-date-history nil)
  14491. (defvar org-read-date-final-answer nil)
  14492. (defvar org-read-date-analyze-futurep nil)
  14493. (defvar org-read-date-analyze-forced-year nil)
  14494. (defvar org-read-date-inactive)
  14495. (defvar org-read-date-minibuffer-local-map
  14496. (let ((map (make-sparse-keymap)))
  14497. (set-keymap-parent map minibuffer-local-map)
  14498. (org-defkey map (kbd ".")
  14499. (lambda () (interactive)
  14500. (org-eval-in-calendar '(calendar-goto-today))))
  14501. (org-defkey map [(meta shift left)]
  14502. (lambda () (interactive)
  14503. (org-eval-in-calendar '(calendar-backward-month 1))))
  14504. (org-defkey map [(meta shift right)]
  14505. (lambda () (interactive)
  14506. (org-eval-in-calendar '(calendar-forward-month 1))))
  14507. (org-defkey map [(meta shift up)]
  14508. (lambda () (interactive)
  14509. (org-eval-in-calendar '(calendar-backward-year 1))))
  14510. (org-defkey map [(meta shift down)]
  14511. (lambda () (interactive)
  14512. (org-eval-in-calendar '(calendar-forward-year 1))))
  14513. (org-defkey map [?\e (shift left)]
  14514. (lambda () (interactive)
  14515. (org-eval-in-calendar '(calendar-backward-month 1))))
  14516. (org-defkey map [?\e (shift right)]
  14517. (lambda () (interactive)
  14518. (org-eval-in-calendar '(calendar-forward-month 1))))
  14519. (org-defkey map [?\e (shift up)]
  14520. (lambda () (interactive)
  14521. (org-eval-in-calendar '(calendar-backward-year 1))))
  14522. (org-defkey map [?\e (shift down)]
  14523. (lambda () (interactive)
  14524. (org-eval-in-calendar '(calendar-forward-year 1))))
  14525. (org-defkey map [(shift up)]
  14526. (lambda () (interactive)
  14527. (org-eval-in-calendar '(calendar-backward-week 1))))
  14528. (org-defkey map [(shift down)]
  14529. (lambda () (interactive)
  14530. (org-eval-in-calendar '(calendar-forward-week 1))))
  14531. (org-defkey map [(shift left)]
  14532. (lambda () (interactive)
  14533. (org-eval-in-calendar '(calendar-backward-day 1))))
  14534. (org-defkey map [(shift right)]
  14535. (lambda () (interactive)
  14536. (org-eval-in-calendar '(calendar-forward-day 1))))
  14537. (org-defkey map "!"
  14538. (lambda () (interactive)
  14539. (org-eval-in-calendar '(diary-view-entries))
  14540. (message "")))
  14541. (org-defkey map ">"
  14542. (lambda () (interactive)
  14543. (org-eval-in-calendar '(scroll-calendar-left 1))))
  14544. (org-defkey map "<"
  14545. (lambda () (interactive)
  14546. (org-eval-in-calendar '(scroll-calendar-right 1))))
  14547. (org-defkey map "\C-v"
  14548. (lambda () (interactive)
  14549. (org-eval-in-calendar
  14550. '(calendar-scroll-left-three-months 1))))
  14551. (org-defkey map "\M-v"
  14552. (lambda () (interactive)
  14553. (org-eval-in-calendar
  14554. '(calendar-scroll-right-three-months 1))))
  14555. map)
  14556. "Keymap for minibuffer commands when using `org-read-date'.")
  14557. (defun org-read-date (&optional org-with-time to-time from-string prompt
  14558. default-time default-input inactive)
  14559. "Read a date, possibly a time, and make things smooth for the user.
  14560. The prompt will suggest to enter an ISO date, but you can also enter anything
  14561. which will at least partially be understood by `parse-time-string'.
  14562. Unrecognized parts of the date will default to the current day, month, year,
  14563. hour and minute. If this command is called to replace a timestamp at point,
  14564. or to enter the second timestamp of a range, the default time is taken
  14565. from the existing stamp. Furthermore, the command prefers the future,
  14566. so if you are giving a date where the year is not given, and the day-month
  14567. combination is already past in the current year, it will assume you
  14568. mean next year. For details, see the manual. A few examples:
  14569. 3-2-5 --> 2003-02-05
  14570. feb 15 --> currentyear-02-15
  14571. 2/15 --> currentyear-02-15
  14572. sep 12 9 --> 2009-09-12
  14573. 12:45 --> today 12:45
  14574. 22 sept 0:34 --> currentyear-09-22 0:34
  14575. 12 --> currentyear-currentmonth-12
  14576. Fri --> nearest Friday (today or later)
  14577. etc.
  14578. Furthermore you can specify a relative date by giving, as the *first* thing
  14579. in the input: a plus/minus sign, a number and a letter [hdwmy] to indicate
  14580. change in days weeks, months, years.
  14581. With a single plus or minus, the date is relative to today. With a double
  14582. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  14583. +4d --> four days from today
  14584. +4 --> same as above
  14585. +2w --> two weeks from today
  14586. ++5 --> five days from default date
  14587. The function understands only English month and weekday abbreviations.
  14588. While prompting, a calendar is popped up - you can also select the
  14589. date with the mouse (button 1). The calendar shows a period of three
  14590. months. To scroll it to other months, use the keys `>' and `<'.
  14591. If you don't like the calendar, turn it off with
  14592. \(setq org-read-date-popup-calendar nil)
  14593. With optional argument TO-TIME, the date will immediately be converted
  14594. to an internal time.
  14595. With an optional argument ORG-WITH-TIME, the prompt will suggest to
  14596. also insert a time. Note that when ORG-WITH-TIME is not set, you can
  14597. still enter a time, and this function will inform the calling routine
  14598. about this change. The calling routine may then choose to change the
  14599. format used to insert the time stamp into the buffer to include the time.
  14600. With optional argument FROM-STRING, read from this string instead from
  14601. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  14602. the time/date that is used for everything that is not specified by the
  14603. user."
  14604. (require 'parse-time)
  14605. (let* ((org-time-stamp-rounding-minutes
  14606. (if (equal org-with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  14607. (org-dcst org-display-custom-times)
  14608. (ct (org-current-time))
  14609. (org-def (or org-overriding-default-time default-time ct))
  14610. (org-defdecode (decode-time org-def))
  14611. (dummy (progn
  14612. (when (< (nth 2 org-defdecode) org-extend-today-until)
  14613. (setcar (nthcdr 2 org-defdecode) -1)
  14614. (setcar (nthcdr 1 org-defdecode) 59)
  14615. (setq org-def (apply 'encode-time org-defdecode)
  14616. org-defdecode (decode-time org-def)))))
  14617. (mouse-autoselect-window nil) ; Don't let the mouse jump
  14618. (calendar-frame-setup nil)
  14619. (calendar-setup nil)
  14620. (calendar-move-hook nil)
  14621. (calendar-view-diary-initially-flag nil)
  14622. (calendar-view-holidays-initially-flag nil)
  14623. (timestr (format-time-string
  14624. (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") org-def))
  14625. (prompt (concat (if prompt (concat prompt " ") "")
  14626. (format "Date+time [%s]: " timestr)))
  14627. ans (org-ans0 "") org-ans1 org-ans2 final)
  14628. (cond
  14629. (from-string (setq ans from-string))
  14630. (org-read-date-popup-calendar
  14631. (save-excursion
  14632. (save-window-excursion
  14633. (calendar)
  14634. (org-eval-in-calendar '(setq cursor-type nil) t)
  14635. (unwind-protect
  14636. (progn
  14637. (calendar-forward-day (- (time-to-days org-def)
  14638. (calendar-absolute-from-gregorian
  14639. (calendar-current-date))))
  14640. (org-eval-in-calendar nil t)
  14641. (let* ((old-map (current-local-map))
  14642. (map (copy-keymap calendar-mode-map))
  14643. (minibuffer-local-map
  14644. (copy-keymap org-read-date-minibuffer-local-map)))
  14645. (org-defkey map (kbd "RET") 'org-calendar-select)
  14646. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  14647. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  14648. (unwind-protect
  14649. (progn
  14650. (use-local-map map)
  14651. (setq org-read-date-inactive inactive)
  14652. (add-hook 'post-command-hook 'org-read-date-display)
  14653. (setq org-ans0 (read-string prompt default-input
  14654. 'org-read-date-history nil))
  14655. ;; org-ans0: from prompt
  14656. ;; org-ans1: from mouse click
  14657. ;; org-ans2: from calendar motion
  14658. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  14659. (remove-hook 'post-command-hook 'org-read-date-display)
  14660. (use-local-map old-map)
  14661. (when org-read-date-overlay
  14662. (delete-overlay org-read-date-overlay)
  14663. (setq org-read-date-overlay nil)))))
  14664. (bury-buffer "*Calendar*")))))
  14665. (t ; Naked prompt only
  14666. (unwind-protect
  14667. (setq ans (read-string prompt default-input
  14668. 'org-read-date-history timestr))
  14669. (when org-read-date-overlay
  14670. (delete-overlay org-read-date-overlay)
  14671. (setq org-read-date-overlay nil)))))
  14672. (setq final (org-read-date-analyze ans org-def org-defdecode))
  14673. (when org-read-date-analyze-forced-year
  14674. (message "Year was forced into %s"
  14675. (if org-read-date-force-compatible-dates
  14676. "compatible range (1970-2037)"
  14677. "range representable on this machine"))
  14678. (ding))
  14679. ;; One round trip to get rid of 34th of August and stuff like that....
  14680. (setq final (decode-time (apply 'encode-time final)))
  14681. (setq org-read-date-final-answer ans)
  14682. (if to-time
  14683. (apply 'encode-time final)
  14684. (if (and (boundp 'org-time-was-given) org-time-was-given)
  14685. (format "%04d-%02d-%02d %02d:%02d"
  14686. (nth 5 final) (nth 4 final) (nth 3 final)
  14687. (nth 2 final) (nth 1 final))
  14688. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  14689. (defvar org-def)
  14690. (defvar org-defdecode)
  14691. (defvar org-with-time)
  14692. (defun org-read-date-display ()
  14693. "Display the current date prompt interpretation in the minibuffer."
  14694. (when org-read-date-display-live
  14695. (when org-read-date-overlay
  14696. (delete-overlay org-read-date-overlay))
  14697. (when (minibufferp (current-buffer))
  14698. (save-excursion
  14699. (end-of-line 1)
  14700. (while (not (equal (buffer-substring
  14701. (max (point-min) (- (point) 4)) (point))
  14702. " "))
  14703. (insert " ")))
  14704. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  14705. " " (or org-ans1 org-ans2)))
  14706. (org-end-time-was-given nil)
  14707. (f (org-read-date-analyze ans org-def org-defdecode))
  14708. (fmts (if org-dcst
  14709. org-time-stamp-custom-formats
  14710. org-time-stamp-formats))
  14711. (fmt (if (or org-with-time
  14712. (and (boundp 'org-time-was-given) org-time-was-given))
  14713. (cdr fmts)
  14714. (car fmts)))
  14715. (txt (format-time-string fmt (apply 'encode-time f)))
  14716. (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
  14717. (txt (concat "=> " txt)))
  14718. (when (and org-end-time-was-given
  14719. (string-match org-plain-time-of-day-regexp txt))
  14720. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  14721. org-end-time-was-given
  14722. (substring txt (match-end 0)))))
  14723. (when org-read-date-analyze-futurep
  14724. (setq txt (concat txt " (=>F)")))
  14725. (setq org-read-date-overlay
  14726. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  14727. (org-overlay-display org-read-date-overlay txt 'secondary-selection)))))
  14728. (defun org-read-date-analyze (ans org-def org-defdecode)
  14729. "Analyze the combined answer of the date prompt."
  14730. ;; FIXME: cleanup and comment
  14731. (let ((nowdecode (decode-time (current-time)))
  14732. delta deltan deltaw deltadef year month day
  14733. hour minute second wday pm h2 m2 tl wday1
  14734. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  14735. (setq org-read-date-analyze-futurep nil
  14736. org-read-date-analyze-forced-year nil)
  14737. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  14738. (setq ans "+0"))
  14739. (when (setq delta (org-read-date-get-relative ans (current-time) org-def))
  14740. (setq ans (replace-match "" t t ans)
  14741. deltan (car delta)
  14742. deltaw (nth 1 delta)
  14743. deltadef (nth 2 delta)))
  14744. ;; Check if there is an iso week date in there. If yes, store the
  14745. ;; info and postpone interpreting it until the rest of the parsing
  14746. ;; is done.
  14747. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  14748. (setq iso-year (if (match-end 1)
  14749. (org-small-year-to-year
  14750. (string-to-number (match-string 1 ans))))
  14751. iso-weekday (if (match-end 3)
  14752. (string-to-number (match-string 3 ans)))
  14753. iso-week (string-to-number (match-string 2 ans)))
  14754. (setq ans (replace-match "" t t ans)))
  14755. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  14756. (when (string-match
  14757. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  14758. (setq year (if (match-end 2)
  14759. (string-to-number (match-string 2 ans))
  14760. (progn (setq kill-year t)
  14761. (string-to-number (format-time-string "%Y"))))
  14762. month (string-to-number (match-string 3 ans))
  14763. day (string-to-number (match-string 4 ans)))
  14764. (if (< year 100) (setq year (+ 2000 year)))
  14765. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  14766. t nil ans)))
  14767. ;; Help matching dotted european dates
  14768. (when (string-match
  14769. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\.\\( ?[1-9][0-9]\\{3\\}\\)?" ans)
  14770. (setq year (if (match-end 3) (string-to-number (match-string 3 ans))
  14771. (setq kill-year t)
  14772. (string-to-number (format-time-string "%Y")))
  14773. day (string-to-number (match-string 1 ans))
  14774. month (string-to-number (match-string 2 ans))
  14775. ans (replace-match (format "%04d-%02d-%02d" year month day)
  14776. t nil ans)))
  14777. ;; Help matching american dates, like 5/30 or 5/30/7
  14778. (when (string-match
  14779. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  14780. (setq year (if (match-end 4)
  14781. (string-to-number (match-string 4 ans))
  14782. (progn (setq kill-year t)
  14783. (string-to-number (format-time-string "%Y"))))
  14784. month (string-to-number (match-string 1 ans))
  14785. day (string-to-number (match-string 2 ans)))
  14786. (if (< year 100) (setq year (+ 2000 year)))
  14787. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  14788. t nil ans)))
  14789. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  14790. ;; If there is a time with am/pm, and *no* time without it, we convert
  14791. ;; so that matching will be successful.
  14792. (loop for i from 1 to 2 do ; twice, for end time as well
  14793. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  14794. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  14795. (setq hour (string-to-number (match-string 1 ans))
  14796. minute (if (match-end 3)
  14797. (string-to-number (match-string 3 ans))
  14798. 0)
  14799. pm (equal ?p
  14800. (string-to-char (downcase (match-string 4 ans)))))
  14801. (if (and (= hour 12) (not pm))
  14802. (setq hour 0)
  14803. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  14804. (setq ans (replace-match (format "%02d:%02d" hour minute)
  14805. t t ans))))
  14806. ;; Check if a time range is given as a duration
  14807. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  14808. (setq hour (string-to-number (match-string 1 ans))
  14809. h2 (+ hour (string-to-number (match-string 3 ans)))
  14810. minute (string-to-number (match-string 2 ans))
  14811. m2 (+ minute (if (match-end 5) (string-to-number
  14812. (match-string 5 ans))0)))
  14813. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  14814. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  14815. t t ans)))
  14816. ;; Check if there is a time range
  14817. (when (boundp 'org-end-time-was-given)
  14818. (setq org-time-was-given nil)
  14819. (when (and (string-match org-plain-time-of-day-regexp ans)
  14820. (match-end 8))
  14821. (setq org-end-time-was-given (match-string 8 ans))
  14822. (setq ans (concat (substring ans 0 (match-beginning 7))
  14823. (substring ans (match-end 7))))))
  14824. (setq tl (parse-time-string ans)
  14825. day (or (nth 3 tl) (nth 3 org-defdecode))
  14826. month (or (nth 4 tl)
  14827. (if (and org-read-date-prefer-future
  14828. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  14829. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  14830. (nth 4 org-defdecode)))
  14831. year (or (and (not kill-year) (nth 5 tl))
  14832. (if (and org-read-date-prefer-future
  14833. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  14834. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  14835. (nth 5 org-defdecode)))
  14836. hour (or (nth 2 tl) (nth 2 org-defdecode))
  14837. minute (or (nth 1 tl) (nth 1 org-defdecode))
  14838. second (or (nth 0 tl) 0)
  14839. wday (nth 6 tl))
  14840. (when (and (eq org-read-date-prefer-future 'time)
  14841. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  14842. (equal day (nth 3 nowdecode))
  14843. (equal month (nth 4 nowdecode))
  14844. (equal year (nth 5 nowdecode))
  14845. (nth 2 tl)
  14846. (or (< (nth 2 tl) (nth 2 nowdecode))
  14847. (and (= (nth 2 tl) (nth 2 nowdecode))
  14848. (nth 1 tl)
  14849. (< (nth 1 tl) (nth 1 nowdecode)))))
  14850. (setq day (1+ day)
  14851. futurep t))
  14852. ;; Special date definitions below
  14853. (cond
  14854. (iso-week
  14855. ;; There was an iso week
  14856. (require 'cal-iso)
  14857. (setq futurep nil)
  14858. (setq year (or iso-year year)
  14859. day (or iso-weekday wday 1)
  14860. wday nil ; to make sure that the trigger below does not match
  14861. iso-date (calendar-gregorian-from-absolute
  14862. (calendar-absolute-from-iso
  14863. (list iso-week day year))))
  14864. ; FIXME: Should we also push ISO weeks into the future?
  14865. ; (when (and org-read-date-prefer-future
  14866. ; (not iso-year)
  14867. ; (< (calendar-absolute-from-gregorian iso-date)
  14868. ; (time-to-days (current-time))))
  14869. ; (setq year (1+ year)
  14870. ; iso-date (calendar-gregorian-from-absolute
  14871. ; (calendar-absolute-from-iso
  14872. ; (list iso-week day year)))))
  14873. (setq month (car iso-date)
  14874. year (nth 2 iso-date)
  14875. day (nth 1 iso-date)))
  14876. (deltan
  14877. (setq futurep nil)
  14878. (unless deltadef
  14879. (let ((now (decode-time (current-time))))
  14880. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  14881. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  14882. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  14883. ((equal deltaw "m") (setq month (+ month deltan)))
  14884. ((equal deltaw "y") (setq year (+ year deltan)))))
  14885. ((and wday (not (nth 3 tl)))
  14886. ;; Weekday was given, but no day, so pick that day in the week
  14887. ;; on or after the derived date.
  14888. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  14889. (unless (equal wday wday1)
  14890. (setq day (+ day (% (- wday wday1 -7) 7))))))
  14891. (if (and (boundp 'org-time-was-given)
  14892. (nth 2 tl))
  14893. (setq org-time-was-given t))
  14894. (if (< year 100) (setq year (+ 2000 year)))
  14895. ;; Check of the date is representable
  14896. (if org-read-date-force-compatible-dates
  14897. (progn
  14898. (if (< year 1970)
  14899. (setq year 1970 org-read-date-analyze-forced-year t))
  14900. (if (> year 2037)
  14901. (setq year 2037 org-read-date-analyze-forced-year t)))
  14902. (condition-case nil
  14903. (ignore (encode-time second minute hour day month year))
  14904. (error
  14905. (setq year (nth 5 org-defdecode))
  14906. (setq org-read-date-analyze-forced-year t))))
  14907. (setq org-read-date-analyze-futurep futurep)
  14908. (list second minute hour day month year)))
  14909. (defvar parse-time-weekdays)
  14910. (defun org-read-date-get-relative (s today default)
  14911. "Check string S for special relative date string.
  14912. TODAY and DEFAULT are internal times, for today and for a default.
  14913. Return shift list (N what def-flag)
  14914. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  14915. N is the number of WHATs to shift.
  14916. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  14917. the DEFAULT date rather than TODAY."
  14918. (require 'parse-time)
  14919. (when (and
  14920. (string-match
  14921. (concat
  14922. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  14923. "\\([0-9]+\\)?"
  14924. "\\([hdwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  14925. "\\([ \t]\\|$\\)") s)
  14926. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  14927. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  14928. (string-to-char (substring (match-string 1 s) -1))
  14929. ?+))
  14930. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  14931. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  14932. (what (if (match-end 3) (match-string 3 s) "d"))
  14933. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  14934. (date (if rel default today))
  14935. (wday (nth 6 (decode-time date)))
  14936. delta)
  14937. (if wday1
  14938. (progn
  14939. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  14940. (if (= dir ?-) (setq delta (- delta 7)))
  14941. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  14942. (list delta "d" rel))
  14943. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  14944. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  14945. "Turn a user-specified date into the internal representation.
  14946. The internal representation needed by the calendar is (month day year).
  14947. This is a wrapper to handle the brain-dead convention in calendar that
  14948. user function argument order change dependent on argument order."
  14949. (if (boundp 'calendar-date-style)
  14950. (cond
  14951. ((eq calendar-date-style 'american)
  14952. (list arg1 arg2 arg3))
  14953. ((eq calendar-date-style 'european)
  14954. (list arg2 arg1 arg3))
  14955. ((eq calendar-date-style 'iso)
  14956. (list arg2 arg3 arg1)))
  14957. (org-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  14958. (if (org-bound-and-true-p european-calendar-style)
  14959. (list arg2 arg1 arg3)
  14960. (list arg1 arg2 arg3)))))
  14961. (defun org-eval-in-calendar (form &optional keepdate)
  14962. "Eval FORM in the calendar window and return to current window.
  14963. When KEEPDATE is non-nil, update `org-ans2' from the cursor date,
  14964. otherwise stick to the current value of `org-ans2'."
  14965. (let ((sf (selected-frame))
  14966. (sw (selected-window)))
  14967. (select-window (get-buffer-window "*Calendar*" t))
  14968. (eval form)
  14969. (when (and (not keepdate) (calendar-cursor-to-date))
  14970. (let* ((date (calendar-cursor-to-date))
  14971. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14972. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  14973. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  14974. (select-window sw)
  14975. (org-select-frame-set-input-focus sf)))
  14976. (defun org-calendar-select ()
  14977. "Return to `org-read-date' with the date currently selected.
  14978. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  14979. (interactive)
  14980. (when (calendar-cursor-to-date)
  14981. (let* ((date (calendar-cursor-to-date))
  14982. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14983. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  14984. (if (active-minibuffer-window) (exit-minibuffer))))
  14985. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  14986. "Insert a date stamp for the date given by the internal TIME.
  14987. WITH-HM means use the stamp format that includes the time of the day.
  14988. INACTIVE means use square brackets instead of angular ones, so that the
  14989. stamp will not contribute to the agenda.
  14990. PRE and POST are optional strings to be inserted before and after the
  14991. stamp.
  14992. The command returns the inserted time stamp."
  14993. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  14994. stamp)
  14995. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  14996. (insert-before-markers (or pre ""))
  14997. (when (listp extra)
  14998. (setq extra (car extra))
  14999. (if (and (stringp extra)
  15000. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  15001. (setq extra (format "-%02d:%02d"
  15002. (string-to-number (match-string 1 extra))
  15003. (string-to-number (match-string 2 extra))))
  15004. (setq extra nil)))
  15005. (when extra
  15006. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  15007. (insert-before-markers (setq stamp (format-time-string fmt time)))
  15008. (insert-before-markers (or post ""))
  15009. (setq org-last-inserted-timestamp stamp)))
  15010. (defun org-toggle-time-stamp-overlays ()
  15011. "Toggle the use of custom time stamp formats."
  15012. (interactive)
  15013. (setq org-display-custom-times (not org-display-custom-times))
  15014. (unless org-display-custom-times
  15015. (let ((p (point-min)) (bmp (buffer-modified-p)))
  15016. (while (setq p (next-single-property-change p 'display))
  15017. (if (and (get-text-property p 'display)
  15018. (eq (get-text-property p 'face) 'org-date))
  15019. (remove-text-properties
  15020. p (setq p (next-single-property-change p 'display))
  15021. '(display t))))
  15022. (set-buffer-modified-p bmp)))
  15023. (if (featurep 'xemacs)
  15024. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  15025. (org-restart-font-lock)
  15026. (setq org-table-may-need-update t)
  15027. (if org-display-custom-times
  15028. (message "Time stamps are overlaid with custom format")
  15029. (message "Time stamp overlays removed")))
  15030. (defun org-display-custom-time (beg end)
  15031. "Overlay modified time stamp format over timestamp between BEG and END."
  15032. (let* ((ts (buffer-substring beg end))
  15033. t1 w1 with-hm tf time str w2 (off 0))
  15034. (save-match-data
  15035. (setq t1 (org-parse-time-string ts t))
  15036. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)?\\'" ts)
  15037. (setq off (- (match-end 0) (match-beginning 0)))))
  15038. (setq end (- end off))
  15039. (setq w1 (- end beg)
  15040. with-hm (and (nth 1 t1) (nth 2 t1))
  15041. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  15042. time (org-fix-decoded-time t1)
  15043. str (org-add-props
  15044. (format-time-string
  15045. (substring tf 1 -1) (apply 'encode-time time))
  15046. nil 'mouse-face 'highlight)
  15047. w2 (length str))
  15048. (if (not (= w2 w1))
  15049. (add-text-properties (1+ beg) (+ 2 beg)
  15050. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  15051. (if (featurep 'xemacs)
  15052. (progn
  15053. (put-text-property beg end 'invisible t)
  15054. (put-text-property beg end 'end-glyph (make-glyph str)))
  15055. (put-text-property beg end 'display str))))
  15056. (defun org-translate-time (string)
  15057. "Translate all timestamps in STRING to custom format.
  15058. But do this only if the variable `org-display-custom-times' is set."
  15059. (when org-display-custom-times
  15060. (save-match-data
  15061. (let* ((start 0)
  15062. (re org-ts-regexp-both)
  15063. t1 with-hm inactive tf time str beg end)
  15064. (while (setq start (string-match re string start))
  15065. (setq beg (match-beginning 0)
  15066. end (match-end 0)
  15067. t1 (save-match-data
  15068. (org-parse-time-string (substring string beg end) t))
  15069. with-hm (and (nth 1 t1) (nth 2 t1))
  15070. inactive (equal (substring string beg (1+ beg)) "[")
  15071. tf (funcall (if with-hm 'cdr 'car)
  15072. org-time-stamp-custom-formats)
  15073. time (org-fix-decoded-time t1)
  15074. str (format-time-string
  15075. (concat
  15076. (if inactive "[" "<") (substring tf 1 -1)
  15077. (if inactive "]" ">"))
  15078. (apply 'encode-time time))
  15079. string (replace-match str t t string)
  15080. start (+ start (length str)))))))
  15081. string)
  15082. (defun org-fix-decoded-time (time)
  15083. "Set 0 instead of nil for the first 6 elements of time.
  15084. Don't touch the rest."
  15085. (let ((n 0))
  15086. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  15087. (define-obsolete-function-alias 'org-days-to-time 'org-time-stamp-to-now "24.4")
  15088. (defun org-time-stamp-to-now (timestamp-string &optional seconds)
  15089. "Difference between TIMESTAMP-STRING and now in days.
  15090. If SECONDS is non-nil, return the difference in seconds."
  15091. (let ((fdiff (if seconds 'org-float-time 'time-to-days)))
  15092. (- (funcall fdiff (org-time-string-to-time timestamp-string))
  15093. (funcall fdiff (current-time)))))
  15094. (defun org-deadline-close (timestamp-string &optional ndays)
  15095. "Is the time in TIMESTAMP-STRING close to the current date?"
  15096. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  15097. (and (< (org-time-stamp-to-now timestamp-string) ndays)
  15098. (not (org-entry-is-done-p))))
  15099. (defun org-get-wdays (ts &optional delay zero-delay)
  15100. "Get the deadline lead time appropriate for timestring TS.
  15101. When DELAY is non-nil, get the delay time for scheduled items
  15102. instead of the deadline lead time. When ZERO-DELAY is non-nil
  15103. and `org-scheduled-delay-days' is 0, enforce 0 as the delay,
  15104. don't try to find the delay cookie in the scheduled timestamp."
  15105. (let ((tv (if delay org-scheduled-delay-days
  15106. org-deadline-warning-days)))
  15107. (cond
  15108. ((or (and delay (< tv 0))
  15109. (and delay zero-delay (<= tv 0))
  15110. (and (not delay) (<= tv 0)))
  15111. ;; Enforce this value no matter what
  15112. (- tv))
  15113. ((string-match "-\\([0-9]+\\)\\([hdwmy]\\)\\(\\'\\|>\\| \\)" ts)
  15114. ;; lead time is specified.
  15115. (floor (* (string-to-number (match-string 1 ts))
  15116. (cdr (assoc (match-string 2 ts)
  15117. '(("d" . 1) ("w" . 7)
  15118. ("m" . 30.4) ("y" . 365.25)
  15119. ("h" . 0.041667)))))))
  15120. ;; go for the default.
  15121. (t tv))))
  15122. (defun org-calendar-select-mouse (ev)
  15123. "Return to `org-read-date' with the date currently selected.
  15124. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15125. (interactive "e")
  15126. (mouse-set-point ev)
  15127. (when (calendar-cursor-to-date)
  15128. (let* ((date (calendar-cursor-to-date))
  15129. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15130. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15131. (if (active-minibuffer-window) (exit-minibuffer))))
  15132. (defun org-check-deadlines (ndays)
  15133. "Check if there are any deadlines due or past due.
  15134. A deadline is considered due if it happens within `org-deadline-warning-days'
  15135. days from today's date. If the deadline appears in an entry marked DONE,
  15136. it is not shown. The prefix arg NDAYS can be used to test that many
  15137. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  15138. (interactive "P")
  15139. (let* ((org-warn-days
  15140. (cond
  15141. ((equal ndays '(4)) 100000)
  15142. (ndays (prefix-numeric-value ndays))
  15143. (t (abs org-deadline-warning-days))))
  15144. (case-fold-search nil)
  15145. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15146. (callback
  15147. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  15148. (message "%d deadlines past-due or due within %d days"
  15149. (org-occur regexp nil callback)
  15150. org-warn-days)))
  15151. (defsubst org-re-timestamp (type)
  15152. "Return a regexp for timestamp TYPE.
  15153. Allowed values for TYPE are:
  15154. all: all timestamps
  15155. active: only active timestamps (<...>)
  15156. inactive: only inactive timestamps ([...])
  15157. scheduled: only scheduled timestamps
  15158. deadline: only deadline timestamps
  15159. When TYPE is nil, fall back on returning a regexp that matches
  15160. both scheduled and deadline timestamps."
  15161. (cond ((eq type 'all) "\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}\\(?: +[^]+0-9> \n -]+\\)?\\(?: +[0-9]\\{1,2\\}:[0-9]\\{2\\}\\)?\\)")
  15162. ((eq type 'active) org-ts-regexp)
  15163. ((eq type 'inactive) "\\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^ \n>]*?\\)\\]")
  15164. ((eq type 'scheduled) (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>"))
  15165. ((eq type 'deadline) (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15166. ((eq type 'scheduled-or-deadline)
  15167. (concat "\\<\\(?:" org-deadline-string "\\|" org-scheduled-string "\\) *<\\([^>]+\\)>"))))
  15168. (defun org-check-before-date (date)
  15169. "Check if there are deadlines or scheduled entries before DATE."
  15170. (interactive (list (org-read-date)))
  15171. (let ((case-fold-search nil)
  15172. (regexp (org-re-timestamp org-ts-type))
  15173. (callback
  15174. (lambda () (time-less-p
  15175. (org-time-string-to-time (match-string 1))
  15176. (org-time-string-to-time date)))))
  15177. (message "%d entries before %s"
  15178. (org-occur regexp nil callback) date)))
  15179. (defun org-check-after-date (date)
  15180. "Check if there are deadlines or scheduled entries after DATE."
  15181. (interactive (list (org-read-date)))
  15182. (let ((case-fold-search nil)
  15183. (regexp (org-re-timestamp org-ts-type))
  15184. (callback
  15185. (lambda () (not
  15186. (time-less-p
  15187. (org-time-string-to-time (match-string 1))
  15188. (org-time-string-to-time date))))))
  15189. (message "%d entries after %s"
  15190. (org-occur regexp nil callback) date)))
  15191. (defun org-check-dates-range (start-date end-date)
  15192. "Check for deadlines/scheduled entries between START-DATE and END-DATE."
  15193. (interactive (list (org-read-date nil nil nil "Range starts")
  15194. (org-read-date nil nil nil "Range end")))
  15195. (let ((case-fold-search nil)
  15196. (regexp (org-re-timestamp org-ts-type))
  15197. (callback
  15198. (lambda ()
  15199. (let ((match (match-string 1)))
  15200. (and
  15201. (not (time-less-p
  15202. (org-time-string-to-time match)
  15203. (org-time-string-to-time start-date)))
  15204. (time-less-p
  15205. (org-time-string-to-time match)
  15206. (org-time-string-to-time end-date)))))))
  15207. (message "%d entries between %s and %s"
  15208. (org-occur regexp nil callback) start-date end-date)))
  15209. (defun org-evaluate-time-range (&optional to-buffer)
  15210. "Evaluate a time range by computing the difference between start and end.
  15211. Normally the result is just printed in the echo area, but with prefix arg
  15212. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  15213. If the time range is actually in a table, the result is inserted into the
  15214. next column.
  15215. For time difference computation, a year is assumed to be exactly 365
  15216. days in order to avoid rounding problems."
  15217. (interactive "P")
  15218. (or
  15219. (org-clock-update-time-maybe)
  15220. (save-excursion
  15221. (unless (org-at-date-range-p t)
  15222. (goto-char (point-at-bol))
  15223. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  15224. (if (not (org-at-date-range-p t))
  15225. (error "Not at a time-stamp range, and none found in current line")))
  15226. (let* ((ts1 (match-string 1))
  15227. (ts2 (match-string 2))
  15228. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  15229. (match-end (match-end 0))
  15230. (time1 (org-time-string-to-time ts1))
  15231. (time2 (org-time-string-to-time ts2))
  15232. (t1 (org-float-time time1))
  15233. (t2 (org-float-time time2))
  15234. (diff (abs (- t2 t1)))
  15235. (negative (< (- t2 t1) 0))
  15236. ;; (ys (floor (* 365 24 60 60)))
  15237. (ds (* 24 60 60))
  15238. (hs (* 60 60))
  15239. (fy "%dy %dd %02d:%02d")
  15240. (fy1 "%dy %dd")
  15241. (fd "%dd %02d:%02d")
  15242. (fd1 "%dd")
  15243. (fh "%02d:%02d")
  15244. y d h m align)
  15245. (if havetime
  15246. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  15247. y 0
  15248. d (floor (/ diff ds)) diff (mod diff ds)
  15249. h (floor (/ diff hs)) diff (mod diff hs)
  15250. m (floor (/ diff 60)))
  15251. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  15252. y 0
  15253. d (floor (+ (/ diff ds) 0.5))
  15254. h 0 m 0))
  15255. (if (not to-buffer)
  15256. (message "%s" (org-make-tdiff-string y d h m))
  15257. (if (org-at-table-p)
  15258. (progn
  15259. (goto-char match-end)
  15260. (setq align t)
  15261. (and (looking-at " *|") (goto-char (match-end 0))))
  15262. (goto-char match-end))
  15263. (if (looking-at
  15264. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  15265. (replace-match ""))
  15266. (if negative (insert " -"))
  15267. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  15268. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  15269. (insert " " (format fh h m))))
  15270. (if align (org-table-align))
  15271. (message "Time difference inserted")))))
  15272. (defun org-make-tdiff-string (y d h m)
  15273. (let ((fmt "")
  15274. (l nil))
  15275. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  15276. l (push y l)))
  15277. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  15278. l (push d l)))
  15279. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  15280. l (push h l)))
  15281. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  15282. l (push m l)))
  15283. (apply 'format fmt (nreverse l))))
  15284. (defun org-time-string-to-time (s &optional buffer pos)
  15285. "Convert a timestamp string into internal time."
  15286. (condition-case errdata
  15287. (apply 'encode-time (org-parse-time-string s))
  15288. (error (error "Bad timestamp `%s'%s\nError was: %s"
  15289. s (if (not (and buffer pos))
  15290. ""
  15291. (format " at %d in buffer `%s'" pos buffer))
  15292. (cdr errdata)))))
  15293. (defun org-time-string-to-seconds (s)
  15294. "Convert a timestamp string to a number of seconds."
  15295. (org-float-time (org-time-string-to-time s)))
  15296. (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
  15297. "Convert a time stamp to an absolute day number.
  15298. If there is a specifier for a cyclic time stamp, get the closest
  15299. date to DAYNR.
  15300. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  15301. The variable `date' is bound by the calendar when this is called."
  15302. (cond
  15303. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  15304. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  15305. daynr
  15306. (+ daynr 1000)))
  15307. ((and daynr (string-match "\\+[0-9]+[hdwmy]" s))
  15308. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  15309. (time-to-days (current-time))) (match-string 0 s)
  15310. prefer show-all))
  15311. (t (time-to-days
  15312. (condition-case errdata
  15313. (apply 'encode-time (org-parse-time-string s))
  15314. (error (error "Bad timestamp `%s'%s\nError was: %s"
  15315. s (if (not (and buffer pos))
  15316. ""
  15317. (format " at %d in buffer `%s'" pos buffer))
  15318. (cdr errdata))))))))
  15319. (defun org-days-to-iso-week (days)
  15320. "Return the iso week number."
  15321. (require 'cal-iso)
  15322. (car (calendar-iso-from-absolute days)))
  15323. (defun org-small-year-to-year (year)
  15324. "Convert 2-digit years into 4-digit years.
  15325. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  15326. The year 2000 cannot be abbreviated. Any year larger than 99
  15327. is returned unchanged."
  15328. (if (< year 38)
  15329. (setq year (+ 2000 year))
  15330. (if (< year 100)
  15331. (setq year (+ 1900 year))))
  15332. year)
  15333. (defun org-time-from-absolute (d)
  15334. "Return the time corresponding to date D.
  15335. D may be an absolute day number, or a calendar-type list (month day year)."
  15336. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  15337. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  15338. (defun org-calendar-holiday ()
  15339. "List of holidays, for Diary display in Org-mode."
  15340. (require 'holidays)
  15341. (let ((hl (funcall
  15342. (if (fboundp 'calendar-check-holidays)
  15343. 'calendar-check-holidays 'check-calendar-holidays) date)))
  15344. (if hl (mapconcat 'identity hl "; "))))
  15345. (defun org-diary-sexp-entry (sexp entry date)
  15346. "Process a SEXP diary ENTRY for DATE."
  15347. (require 'diary-lib)
  15348. (let ((result (if calendar-debug-sexp
  15349. (let ((stack-trace-on-error t))
  15350. (eval (car (read-from-string sexp))))
  15351. (condition-case nil
  15352. (eval (car (read-from-string sexp)))
  15353. (error
  15354. (beep)
  15355. (message "Bad sexp at line %d in %s: %s"
  15356. (org-current-line)
  15357. (buffer-file-name) sexp)
  15358. (sleep-for 2))))))
  15359. (cond ((stringp result) (split-string result "; "))
  15360. ((and (consp result)
  15361. (not (consp (cdr result)))
  15362. (stringp (cdr result))) (cdr result))
  15363. ((and (consp result)
  15364. (stringp (car result))) result)
  15365. (result entry))))
  15366. (defun org-diary-to-ical-string (frombuf)
  15367. "Get iCalendar entries from diary entries in buffer FROMBUF.
  15368. This uses the icalendar.el library."
  15369. (let* ((tmpdir (if (featurep 'xemacs)
  15370. (temp-directory)
  15371. temporary-file-directory))
  15372. (tmpfile (make-temp-name
  15373. (expand-file-name "orgics" tmpdir)))
  15374. buf rtn b e)
  15375. (with-current-buffer frombuf
  15376. (icalendar-export-region (point-min) (point-max) tmpfile)
  15377. (setq buf (find-buffer-visiting tmpfile))
  15378. (set-buffer buf)
  15379. (goto-char (point-min))
  15380. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  15381. (setq b (match-beginning 0)))
  15382. (goto-char (point-max))
  15383. (if (re-search-backward "^END:VEVENT" nil t)
  15384. (setq e (match-end 0)))
  15385. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  15386. (kill-buffer buf)
  15387. (delete-file tmpfile)
  15388. rtn))
  15389. (defun org-closest-date (start current change prefer show-all)
  15390. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  15391. When PREFER is `past', return a date that is either CURRENT or past.
  15392. When PREFER is `future', return a date that is either CURRENT or future.
  15393. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  15394. ;; Make the proper lists from the dates
  15395. (catch 'exit
  15396. (let ((a1 '(("h" . hour)
  15397. ("d" . day)
  15398. ("w" . week)
  15399. ("m" . month)
  15400. ("y" . year)))
  15401. (shour (nth 2 (org-parse-time-string start)))
  15402. dn dw sday cday n1 n2 n0
  15403. d m y y1 y2 date1 date2 nmonths nm ny m2)
  15404. (setq start (org-date-to-gregorian start)
  15405. current (org-date-to-gregorian
  15406. (if show-all
  15407. current
  15408. (time-to-days (current-time))))
  15409. sday (calendar-absolute-from-gregorian start)
  15410. cday (calendar-absolute-from-gregorian current))
  15411. (if (<= cday sday) (throw 'exit sday))
  15412. (if (string-match "\\(\\+[0-9]+\\)\\([hdwmy]\\)" change)
  15413. (setq dn (string-to-number (match-string 1 change))
  15414. dw (cdr (assoc (match-string 2 change) a1)))
  15415. (error "Invalid change specifier: %s" change))
  15416. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  15417. (cond
  15418. ((eq dw 'hour)
  15419. (let ((missing-hours
  15420. (mod (+ (- (* 24 (- cday sday)) shour) org-extend-today-until)
  15421. dn)))
  15422. (setq n1 (if (zerop missing-hours) cday
  15423. (- cday (1+ (floor (/ missing-hours 24)))))
  15424. n2 (+ cday (floor (/ (- dn missing-hours) 24))))))
  15425. ((eq dw 'day)
  15426. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  15427. n2 (+ n1 dn)))
  15428. ((eq dw 'year)
  15429. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  15430. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  15431. (setq date1 (list m d y1)
  15432. n1 (calendar-absolute-from-gregorian date1)
  15433. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  15434. n2 (calendar-absolute-from-gregorian date2)))
  15435. ((eq dw 'month)
  15436. ;; approx number of month between the two dates
  15437. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  15438. ;; How often does dn fit in there?
  15439. (setq d (nth 1 start) m (car start) y (nth 2 start)
  15440. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  15441. m (+ m nm)
  15442. ny (floor (/ m 12))
  15443. y (+ y ny)
  15444. m (- m (* ny 12)))
  15445. (while (> m 12) (setq m (- m 12) y (1+ y)))
  15446. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  15447. (setq m2 (+ m dn) y2 y)
  15448. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  15449. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  15450. (while (<= n2 cday)
  15451. (setq n1 n2 m m2 y y2)
  15452. (setq m2 (+ m dn) y2 y)
  15453. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  15454. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  15455. ;; Make sure n1 is the earlier date
  15456. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  15457. (if show-all
  15458. (cond
  15459. ((eq prefer 'past) (if (= cday n2) n2 n1))
  15460. ((eq prefer 'future) (if (= cday n1) n1 n2))
  15461. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  15462. (cond
  15463. ((eq prefer 'past) (if (= cday n2) n2 n1))
  15464. ((eq prefer 'future) (if (= cday n1) n1 n2))
  15465. (t (if (= cday n1) n1 n2)))))))
  15466. (defun org-date-to-gregorian (date)
  15467. "Turn any specification of DATE into a Gregorian date for the calendar."
  15468. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  15469. ((and (listp date) (= (length date) 3)) date)
  15470. ((stringp date)
  15471. (setq date (org-parse-time-string date))
  15472. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  15473. ((listp date)
  15474. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  15475. (defun org-parse-time-string (s &optional nodefault)
  15476. "Parse the standard Org-mode time string.
  15477. This should be a lot faster than the normal `parse-time-string'.
  15478. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  15479. hour and minute fields will be nil if not given."
  15480. (cond ((string-match org-ts-regexp0 s)
  15481. (list 0
  15482. (if (or (match-beginning 8) (not nodefault))
  15483. (string-to-number (or (match-string 8 s) "0")))
  15484. (if (or (match-beginning 7) (not nodefault))
  15485. (string-to-number (or (match-string 7 s) "0")))
  15486. (string-to-number (match-string 4 s))
  15487. (string-to-number (match-string 3 s))
  15488. (string-to-number (match-string 2 s))
  15489. nil nil nil))
  15490. ((string-match "^<[^>]+>$" s)
  15491. (decode-time (seconds-to-time (org-matcher-time s))))
  15492. (t (error "Not a standard Org-mode time string: %s" s))))
  15493. (defun org-timestamp-up (&optional arg)
  15494. "Increase the date item at the cursor by one.
  15495. If the cursor is on the year, change the year. If it is on the month,
  15496. the day or the time, change that.
  15497. With prefix ARG, change by that many units."
  15498. (interactive "p")
  15499. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  15500. (defun org-timestamp-down (&optional arg)
  15501. "Decrease the date item at the cursor by one.
  15502. If the cursor is on the year, change the year. If it is on the month,
  15503. the day or the time, change that.
  15504. With prefix ARG, change by that many units."
  15505. (interactive "p")
  15506. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  15507. (defun org-timestamp-up-day (&optional arg)
  15508. "Increase the date in the time stamp by one day.
  15509. With prefix ARG, change that many days."
  15510. (interactive "p")
  15511. (if (and (not (org-at-timestamp-p t))
  15512. (org-at-heading-p))
  15513. (org-todo 'up)
  15514. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  15515. (defun org-timestamp-down-day (&optional arg)
  15516. "Decrease the date in the time stamp by one day.
  15517. With prefix ARG, change that many days."
  15518. (interactive "p")
  15519. (if (and (not (org-at-timestamp-p t))
  15520. (org-at-heading-p))
  15521. (org-todo 'down)
  15522. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  15523. (defun org-at-timestamp-p (&optional inactive-ok)
  15524. "Determine if the cursor is in or at a timestamp."
  15525. (interactive)
  15526. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  15527. (pos (point))
  15528. (ans (or (looking-at tsr)
  15529. (save-excursion
  15530. (skip-chars-backward "^[<\n\r\t")
  15531. (if (> (point) (point-min)) (backward-char 1))
  15532. (and (looking-at tsr)
  15533. (> (- (match-end 0) pos) -1))))))
  15534. (and ans
  15535. (boundp 'org-ts-what)
  15536. (setq org-ts-what
  15537. (cond
  15538. ((= pos (match-beginning 0)) 'bracket)
  15539. ;; Point is considered to be "on the bracket" whether
  15540. ;; it's really on it or right after it.
  15541. ((= pos (1- (match-end 0))) 'bracket)
  15542. ((= pos (match-end 0)) 'after)
  15543. ((org-pos-in-match-range pos 2) 'year)
  15544. ((org-pos-in-match-range pos 3) 'month)
  15545. ((org-pos-in-match-range pos 7) 'hour)
  15546. ((org-pos-in-match-range pos 8) 'minute)
  15547. ((or (org-pos-in-match-range pos 4)
  15548. (org-pos-in-match-range pos 5)) 'day)
  15549. ((and (> pos (or (match-end 8) (match-end 5)))
  15550. (< pos (match-end 0)))
  15551. (- pos (or (match-end 8) (match-end 5))))
  15552. (t 'day))))
  15553. ans))
  15554. (defun org-toggle-timestamp-type ()
  15555. "Toggle the type (<active> or [inactive]) of a time stamp."
  15556. (interactive)
  15557. (when (org-at-timestamp-p t)
  15558. (let ((beg (match-beginning 0)) (end (match-end 0))
  15559. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  15560. (save-excursion
  15561. (goto-char beg)
  15562. (while (re-search-forward "[][<>]" end t)
  15563. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  15564. t t)))
  15565. (message "Timestamp is now %sactive"
  15566. (if (equal (char-after beg) ?<) "" "in")))))
  15567. (defun org-at-clock-log-p nil
  15568. "Is the cursor on the clock log line?"
  15569. (save-excursion
  15570. (move-beginning-of-line 1)
  15571. (looking-at "^[ \t]*CLOCK:")))
  15572. (defvar org-clock-history) ; defined in org-clock.el
  15573. (defvar org-clock-adjust-closest nil) ; defined in org-clock.el
  15574. (defun org-timestamp-change (n &optional what updown suppress-tmp-delay)
  15575. "Change the date in the time stamp at point.
  15576. The date will be changed by N times WHAT. WHAT can be `day', `month',
  15577. `year', `minute', `second'. If WHAT is not given, the cursor position
  15578. in the timestamp determines what will be changed.
  15579. When SUPPRESS-TMP-DELAY is non-nil, suppress delays like \"--2d\"."
  15580. (let ((origin (point)) origin-cat
  15581. with-hm inactive
  15582. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  15583. org-ts-what
  15584. extra rem
  15585. ts time time0 fixnext clrgx)
  15586. (if (not (org-at-timestamp-p t))
  15587. (error "Not at a timestamp"))
  15588. (if (and (not what) (eq org-ts-what 'bracket))
  15589. (org-toggle-timestamp-type)
  15590. ;; Point isn't on brackets. Remember the part of the time-stamp
  15591. ;; the point was in. Indeed, size of time-stamps may change,
  15592. ;; but point must be kept in the same category nonetheless.
  15593. (setq origin-cat org-ts-what)
  15594. (if (and (not what) (not (eq org-ts-what 'day))
  15595. org-display-custom-times
  15596. (get-text-property (point) 'display)
  15597. (not (get-text-property (1- (point)) 'display)))
  15598. (setq org-ts-what 'day))
  15599. (setq org-ts-what (or what org-ts-what)
  15600. inactive (= (char-after (match-beginning 0)) ?\[)
  15601. ts (match-string 0))
  15602. (replace-match "")
  15603. (when (string-match
  15604. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?-?[-+][0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)*\\)[]>]"
  15605. ts)
  15606. (setq extra (match-string 1 ts))
  15607. (if suppress-tmp-delay
  15608. (setq extra (replace-regexp-in-string " --[0-9]+[hdwmy]" "" extra))))
  15609. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  15610. (setq with-hm t))
  15611. (setq time0 (org-parse-time-string ts))
  15612. (when (and updown
  15613. (eq org-ts-what 'minute)
  15614. (not current-prefix-arg))
  15615. ;; This looks like s-up and s-down. Change by one rounding step.
  15616. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  15617. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  15618. (setcar (cdr time0) (+ (nth 1 time0)
  15619. (if (> n 0) (- rem) (- dm rem))))))
  15620. (setq time
  15621. (encode-time (or (car time0) 0)
  15622. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  15623. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  15624. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  15625. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  15626. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  15627. (nthcdr 6 time0)))
  15628. (when (and (member org-ts-what '(hour minute))
  15629. extra
  15630. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  15631. (setq extra (org-modify-ts-extra
  15632. extra
  15633. (if (eq org-ts-what 'hour) 2 5)
  15634. n dm)))
  15635. (when (integerp org-ts-what)
  15636. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  15637. (if (eq what 'calendar)
  15638. (let ((cal-date (org-get-date-from-calendar)))
  15639. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  15640. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  15641. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  15642. (setcar time0 (or (car time0) 0))
  15643. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  15644. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  15645. (setq time (apply 'encode-time time0))))
  15646. ;; Insert the new time-stamp, and ensure point stays in the same
  15647. ;; category as before (i.e. not after the last position in that
  15648. ;; category).
  15649. (let ((pos (point)))
  15650. ;; Stay before inserted string. `save-excursion' is of no use.
  15651. (setq org-last-changed-timestamp
  15652. (org-insert-time-stamp time with-hm inactive nil nil extra))
  15653. (goto-char pos))
  15654. (save-match-data
  15655. (looking-at org-ts-regexp3)
  15656. (goto-char (cond
  15657. ;; `day' category ends before `hour' if any, or at
  15658. ;; the end of the day name.
  15659. ((eq origin-cat 'day)
  15660. (min (or (match-beginning 7) (1- (match-end 5))) origin))
  15661. ((eq origin-cat 'hour) (min (match-end 7) origin))
  15662. ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
  15663. ((integerp origin-cat) (min (1- (match-end 0)) origin))
  15664. ;; `year' and `month' have both fixed size: point
  15665. ;; couldn't have moved into another part.
  15666. (t origin))))
  15667. ;; Update clock if on a CLOCK line.
  15668. (org-clock-update-time-maybe)
  15669. ;; Maybe adjust the closest clock in `org-clock-history'
  15670. (when org-clock-adjust-closest
  15671. (if (not (and (org-at-clock-log-p)
  15672. (< 1 (length (delq nil (mapcar (lambda(m) (marker-position m))
  15673. org-clock-history))))))
  15674. (message "No clock to adjust")
  15675. (cond ((save-excursion ; fix previous clock?
  15676. (re-search-backward org-ts-regexp0 nil t)
  15677. (org-looking-back (concat org-clock-string " \\[")))
  15678. (setq fixnext 1 clrgx (concat org-ts-regexp0 "\\] =>.*$")))
  15679. ((save-excursion ; fix next clock?
  15680. (re-search-backward org-ts-regexp0 nil t)
  15681. (looking-at (concat org-ts-regexp0 "\\] =>")))
  15682. (setq fixnext -1 clrgx (concat org-clock-string " \\[" org-ts-regexp0))))
  15683. (save-window-excursion
  15684. ;; Find closest clock to point, adjust the previous/next one in history
  15685. (let* ((p (save-excursion (org-back-to-heading t)))
  15686. (cl (mapcar (lambda(c) (abs (- (marker-position c) p))) org-clock-history))
  15687. (clfixnth
  15688. (+ fixnext (- (length cl) (or (length (member (apply #'min cl) cl)) 100))))
  15689. (clfixpos (if (> 0 clfixnth) nil (nth clfixnth org-clock-history))))
  15690. (if (not clfixpos)
  15691. (message "No clock to adjust")
  15692. (save-excursion
  15693. (org-goto-marker-or-bmk clfixpos)
  15694. (org-show-subtree)
  15695. (when (re-search-forward clrgx nil t)
  15696. (goto-char (match-beginning 1))
  15697. (let (org-clock-adjust-closest)
  15698. (org-timestamp-change n org-ts-what updown))
  15699. (message "Clock adjusted in %s for heading: %s"
  15700. (file-name-nondirectory (buffer-file-name))
  15701. (org-get-heading t t)))))))))
  15702. ;; Try to recenter the calendar window, if any.
  15703. (if (and org-calendar-follow-timestamp-change
  15704. (get-buffer-window "*Calendar*" t)
  15705. (memq org-ts-what '(day month year)))
  15706. (org-recenter-calendar (time-to-days time))))))
  15707. (defun org-modify-ts-extra (s pos n dm)
  15708. "Change the different parts of the lead-time and repeat fields in timestamp."
  15709. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  15710. ng h m new rem)
  15711. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  15712. (cond
  15713. ((or (org-pos-in-match-range pos 2)
  15714. (org-pos-in-match-range pos 3))
  15715. (setq m (string-to-number (match-string 3 s))
  15716. h (string-to-number (match-string 2 s)))
  15717. (if (org-pos-in-match-range pos 2)
  15718. (setq h (+ h n))
  15719. (setq n (* dm (org-no-warnings (signum n))))
  15720. (when (not (= 0 (setq rem (% m dm))))
  15721. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  15722. (setq m (+ m n)))
  15723. (if (< m 0) (setq m (+ m 60) h (1- h)))
  15724. (if (> m 59) (setq m (- m 60) h (1+ h)))
  15725. (setq h (min 24 (max 0 h)))
  15726. (setq ng 1 new (format "-%02d:%02d" h m)))
  15727. ((org-pos-in-match-range pos 6)
  15728. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  15729. ((org-pos-in-match-range pos 5)
  15730. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  15731. ((org-pos-in-match-range pos 9)
  15732. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  15733. ((org-pos-in-match-range pos 8)
  15734. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  15735. (when ng
  15736. (setq s (concat
  15737. (substring s 0 (match-beginning ng))
  15738. new
  15739. (substring s (match-end ng))))))
  15740. s))
  15741. (defun org-recenter-calendar (date)
  15742. "If the calendar is visible, recenter it to DATE."
  15743. (let ((cwin (get-buffer-window "*Calendar*" t)))
  15744. (when cwin
  15745. (let ((calendar-move-hook nil))
  15746. (with-selected-window cwin
  15747. (calendar-goto-date (if (listp date) date
  15748. (calendar-gregorian-from-absolute date))))))))
  15749. (defun org-goto-calendar (&optional arg)
  15750. "Go to the Emacs calendar at the current date.
  15751. If there is a time stamp in the current line, go to that date.
  15752. A prefix ARG can be used to force the current date."
  15753. (interactive "P")
  15754. (let ((tsr org-ts-regexp) diff
  15755. (calendar-move-hook nil)
  15756. (calendar-view-holidays-initially-flag nil)
  15757. (calendar-view-diary-initially-flag nil))
  15758. (if (or (org-at-timestamp-p)
  15759. (save-excursion
  15760. (beginning-of-line 1)
  15761. (looking-at (concat ".*" tsr))))
  15762. (let ((d1 (time-to-days (current-time)))
  15763. (d2 (time-to-days
  15764. (org-time-string-to-time (match-string 1)))))
  15765. (setq diff (- d2 d1))))
  15766. (calendar)
  15767. (calendar-goto-today)
  15768. (if (and diff (not arg)) (calendar-forward-day diff))))
  15769. (defun org-get-date-from-calendar ()
  15770. "Return a list (month day year) of date at point in calendar."
  15771. (with-current-buffer "*Calendar*"
  15772. (save-match-data
  15773. (calendar-cursor-to-date))))
  15774. (defun org-date-from-calendar ()
  15775. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  15776. If there is already a time stamp at the cursor position, update it."
  15777. (interactive)
  15778. (if (org-at-timestamp-p t)
  15779. (org-timestamp-change 0 'calendar)
  15780. (let ((cal-date (org-get-date-from-calendar)))
  15781. (org-insert-time-stamp
  15782. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  15783. (defcustom org-effort-durations
  15784. `(("h" . 60)
  15785. ("d" . ,(* 60 8))
  15786. ("w" . ,(* 60 8 5))
  15787. ("m" . ,(* 60 8 5 4))
  15788. ("y" . ,(* 60 8 5 40)))
  15789. "Conversion factor to minutes for an effort modifier.
  15790. Each entry has the form (MODIFIER . MINUTES).
  15791. In an effort string, a number followed by MODIFIER is multiplied
  15792. by the specified number of MINUTES to obtain an effort in
  15793. minutes.
  15794. For example, if the value of this variable is ((\"hours\" . 60)), then an
  15795. effort string \"2hours\" is equivalent to 120 minutes."
  15796. :group 'org-agenda
  15797. :version "24.1"
  15798. :type '(alist :key-type (string :tag "Modifier")
  15799. :value-type (number :tag "Minutes")))
  15800. (defun org-minutes-to-clocksum-string (m)
  15801. "Format number of minutes as a clocksum string.
  15802. The format is determined by `org-time-clocksum-format',
  15803. `org-time-clocksum-use-fractional' and
  15804. `org-time-clocksum-fractional-format' and
  15805. `org-time-clocksum-use-effort-durations'."
  15806. (let ((clocksum "") h d w mo y fmt n)
  15807. (setq h (if org-time-clocksum-use-effort-durations
  15808. (cdr (assoc "h" org-effort-durations)) 60)
  15809. d (if org-time-clocksum-use-effort-durations
  15810. (/ (cdr (assoc "d" org-effort-durations)) h) 24)
  15811. w (if org-time-clocksum-use-effort-durations
  15812. (/ (cdr (assoc "w" org-effort-durations)) (* d h)) 7)
  15813. mo (if org-time-clocksum-use-effort-durations
  15814. (/ (cdr (assoc "m" org-effort-durations)) (* d h)) 30)
  15815. y (if org-time-clocksum-use-effort-durations
  15816. (/ (cdr (assoc "y" org-effort-durations)) (* d h)) 365))
  15817. ;; fractional format
  15818. (if org-time-clocksum-use-fractional
  15819. (cond
  15820. ;; single format string
  15821. ((stringp org-time-clocksum-fractional-format)
  15822. (format org-time-clocksum-fractional-format (/ m (float h))))
  15823. ;; choice of fractional formats for different time units
  15824. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :years))
  15825. (> (/ (truncate m) (* y d h)) 0))
  15826. (format fmt (/ m (* y d (float h)))))
  15827. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :months))
  15828. (> (/ (truncate m) (* mo d h)) 0))
  15829. (format fmt (/ m (* mo d (float h)))))
  15830. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  15831. (> (/ (truncate m) (* w d h)) 0))
  15832. (format fmt (/ m (* w d (float h)))))
  15833. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :days))
  15834. (> (/ (truncate m) (* d h)) 0))
  15835. (format fmt (/ m (* d (float h)))))
  15836. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  15837. (> (/ (truncate m) h) 0))
  15838. (format fmt (/ m (float h))))
  15839. ((setq fmt (plist-get org-time-clocksum-fractional-format :minutes))
  15840. (format fmt m))
  15841. ;; fall back to smallest time unit with a format
  15842. ((setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  15843. (format fmt (/ m (float h))))
  15844. ((setq fmt (plist-get org-time-clocksum-fractional-format :days))
  15845. (format fmt (/ m (* d (float h)))))
  15846. ((setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  15847. (format fmt (/ m (* w d (float h)))))
  15848. ((setq fmt (plist-get org-time-clocksum-fractional-format :months))
  15849. (format fmt (/ m (* mo d (float h)))))
  15850. ((setq fmt (plist-get org-time-clocksum-fractional-format :years))
  15851. (format fmt (/ m (* y d (float h))))))
  15852. ;; standard (non-fractional) format, with single format string
  15853. (if (stringp org-time-clocksum-format)
  15854. (format org-time-clocksum-format (setq n (/ m h)) (- m (* h n)))
  15855. ;; separate formats components
  15856. (and (setq fmt (plist-get org-time-clocksum-format :years))
  15857. (or (> (setq n (/ (truncate m) (* y d h))) 0)
  15858. (plist-get org-time-clocksum-format :require-years))
  15859. (setq clocksum (concat clocksum (format fmt n))
  15860. m (- m (* n y d h))))
  15861. (and (setq fmt (plist-get org-time-clocksum-format :months))
  15862. (or (> (setq n (/ (truncate m) (* mo d h))) 0)
  15863. (plist-get org-time-clocksum-format :require-months))
  15864. (setq clocksum (concat clocksum (format fmt n))
  15865. m (- m (* n mo d h))))
  15866. (and (setq fmt (plist-get org-time-clocksum-format :weeks))
  15867. (or (> (setq n (/ (truncate m) (* w d h))) 0)
  15868. (plist-get org-time-clocksum-format :require-weeks))
  15869. (setq clocksum (concat clocksum (format fmt n))
  15870. m (- m (* n w d h))))
  15871. (and (setq fmt (plist-get org-time-clocksum-format :days))
  15872. (or (> (setq n (/ (truncate m) (* d h))) 0)
  15873. (plist-get org-time-clocksum-format :require-days))
  15874. (setq clocksum (concat clocksum (format fmt n))
  15875. m (- m (* n d h))))
  15876. (and (setq fmt (plist-get org-time-clocksum-format :hours))
  15877. (or (> (setq n (/ (truncate m) h)) 0)
  15878. (plist-get org-time-clocksum-format :require-hours))
  15879. (setq clocksum (concat clocksum (format fmt n))
  15880. m (- m (* n h))))
  15881. (and (setq fmt (plist-get org-time-clocksum-format :minutes))
  15882. (or (> m 0) (plist-get org-time-clocksum-format :require-minutes))
  15883. (setq clocksum (concat clocksum (format fmt m))))
  15884. ;; return formatted time duration
  15885. clocksum))))
  15886. (defalias 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string)
  15887. (make-obsolete 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string
  15888. "Org mode version 8.0")
  15889. (defun org-hours-to-clocksum-string (n)
  15890. (org-minutes-to-clocksum-string (* n 60)))
  15891. (defun org-hh:mm-string-to-minutes (s)
  15892. "Convert a string H:MM to a number of minutes.
  15893. If the string is just a number, interpret it as minutes.
  15894. In fact, the first hh:mm or number in the string will be taken,
  15895. there can be extra stuff in the string.
  15896. If no number is found, the return value is 0."
  15897. (cond
  15898. ((integerp s) s)
  15899. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  15900. (+ (* (string-to-number (match-string 1 s)) 60)
  15901. (string-to-number (match-string 2 s))))
  15902. ((string-match "\\([0-9]+\\)" s)
  15903. (string-to-number (match-string 1 s)))
  15904. (t 0)))
  15905. (defcustom org-image-actual-width t
  15906. "Should we use the actual width of images when inlining them?
  15907. When set to `t', always use the image width.
  15908. When set to a number, use imagemagick (when available) to set
  15909. the image's width to this value.
  15910. When set to a number in a list, try to get the width from the
  15911. #+ATTR.* keyword if it matches a width specification like
  15912. width=\"[0-9]+\", and fall back on that number if none is found.
  15913. When set to nil, try to get the width from an #+ATTR.* keyword
  15914. and fall back on the original width if none is found.
  15915. This requires Emacs >= 24.1, build with imagemagick support."
  15916. :group 'org-appearance
  15917. :version "24.4"
  15918. :package-version '(Org . "8.0")
  15919. :type '(choice
  15920. (const :tag "Use the image width" t)
  15921. (integer :tag "Use a number of pixels")
  15922. (list :tag "Use #+ATTR* or a number of pixels" (integer))
  15923. (const :tag "Use #+ATTR* or don't resize" nil)))
  15924. (defcustom org-agenda-inhibit-startup nil
  15925. "Inhibit startup when preparing agenda buffers.
  15926. When this variable is `t' (the default), the initialization of
  15927. the Org agenda buffers is inhibited: e.g. the visibility state
  15928. is not set, the tables are not re-aligned, etc."
  15929. :type 'boolean
  15930. :version "24.3"
  15931. :group 'org-agenda)
  15932. (defun org-duration-string-to-minutes (s &optional output-to-string)
  15933. "Convert a duration string S to minutes.
  15934. A bare number is interpreted as minutes, modifiers can be set by
  15935. customizing `org-effort-durations' (which see).
  15936. Entries containing a colon are interpreted as H:MM by
  15937. `org-hh:mm-string-to-minutes'."
  15938. (let ((result 0)
  15939. (re (concat "\\([0-9.]+\\) *\\("
  15940. (regexp-opt (mapcar 'car org-effort-durations))
  15941. "\\)")))
  15942. (while (string-match re s)
  15943. (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  15944. (string-to-number (match-string 1 s))))
  15945. (setq s (replace-match "" nil t s)))
  15946. (setq result (floor result))
  15947. (incf result (org-hh:mm-string-to-minutes s))
  15948. (if output-to-string (number-to-string result) result)))
  15949. ;;;; Files
  15950. (defun org-save-all-org-buffers ()
  15951. "Save all Org-mode buffers without user confirmation."
  15952. (interactive)
  15953. (message "Saving all Org-mode buffers...")
  15954. (save-some-buffers t (lambda () (derived-mode-p 'org-mode)))
  15955. (when (featurep 'org-id) (org-id-locations-save))
  15956. (message "Saving all Org-mode buffers... done"))
  15957. (defun org-revert-all-org-buffers ()
  15958. "Revert all Org-mode buffers.
  15959. Prompt for confirmation when there are unsaved changes.
  15960. Be sure you know what you are doing before letting this function
  15961. overwrite your changes.
  15962. This function is useful in a setup where one tracks org files
  15963. with a version control system, to revert on one machine after pulling
  15964. changes from another. I believe the procedure must be like this:
  15965. 1. M-x org-save-all-org-buffers
  15966. 2. Pull changes from the other machine, resolve conflicts
  15967. 3. M-x org-revert-all-org-buffers"
  15968. (interactive)
  15969. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  15970. (error "Abort"))
  15971. (save-excursion
  15972. (save-window-excursion
  15973. (mapc
  15974. (lambda (b)
  15975. (when (and (with-current-buffer b (derived-mode-p 'org-mode))
  15976. (with-current-buffer b buffer-file-name))
  15977. (org-pop-to-buffer-same-window b)
  15978. (revert-buffer t 'no-confirm)))
  15979. (buffer-list))
  15980. (when (and (featurep 'org-id) org-id-track-globally)
  15981. (org-id-locations-load)))))
  15982. ;;;; Agenda files
  15983. ;;;###autoload
  15984. (defun org-switchb (&optional arg)
  15985. "Switch between Org buffers.
  15986. With one prefix argument, restrict available buffers to files.
  15987. With two prefix arguments, restrict available buffers to agenda files.
  15988. Defaults to `iswitchb' for buffer name completion.
  15989. Set `org-completion-use-ido' to make it use ido instead."
  15990. (interactive "P")
  15991. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  15992. ((equal arg '(16)) (org-buffer-list 'agenda))
  15993. (t (org-buffer-list))))
  15994. (org-completion-use-iswitchb org-completion-use-iswitchb)
  15995. (org-completion-use-ido org-completion-use-ido))
  15996. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  15997. (setq org-completion-use-iswitchb t))
  15998. (org-pop-to-buffer-same-window
  15999. (org-icompleting-read "Org buffer: "
  16000. (mapcar 'list (mapcar 'buffer-name blist))
  16001. nil t))))
  16002. ;;; Define some older names previously used for this functionality
  16003. ;;;###autoload
  16004. (defalias 'org-ido-switchb 'org-switchb)
  16005. ;;;###autoload
  16006. (defalias 'org-iswitchb 'org-switchb)
  16007. (defun org-buffer-list (&optional predicate exclude-tmp)
  16008. "Return a list of Org buffers.
  16009. PREDICATE can be `export', `files' or `agenda'.
  16010. export restrict the list to Export buffers.
  16011. files restrict the list to buffers visiting Org files.
  16012. agenda restrict the list to buffers visiting agenda files.
  16013. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  16014. (let* ((bfn nil)
  16015. (agenda-files (and (eq predicate 'agenda)
  16016. (mapcar 'file-truename (org-agenda-files t))))
  16017. (filter
  16018. (cond
  16019. ((eq predicate 'files)
  16020. (lambda (b) (with-current-buffer b (derived-mode-p 'org-mode))))
  16021. ((eq predicate 'export)
  16022. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  16023. ((eq predicate 'agenda)
  16024. (lambda (b)
  16025. (with-current-buffer b
  16026. (and (derived-mode-p 'org-mode)
  16027. (setq bfn (buffer-file-name b))
  16028. (member (file-truename bfn) agenda-files)))))
  16029. (t (lambda (b) (with-current-buffer b
  16030. (or (derived-mode-p 'org-mode)
  16031. (string-match "\*Org .*Export"
  16032. (buffer-name b)))))))))
  16033. (delq nil
  16034. (mapcar
  16035. (lambda(b)
  16036. (if (and (funcall filter b)
  16037. (or (not exclude-tmp)
  16038. (not (string-match "tmp" (buffer-name b)))))
  16039. b
  16040. nil))
  16041. (buffer-list)))))
  16042. (defun org-agenda-files (&optional unrestricted archives)
  16043. "Get the list of agenda files.
  16044. Optional UNRESTRICTED means return the full list even if a restriction
  16045. is currently in place.
  16046. When ARCHIVES is t, include all archive files that are really being
  16047. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  16048. `org-agenda-archives-mode' is t."
  16049. (let ((files
  16050. (cond
  16051. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  16052. ((stringp org-agenda-files) (org-read-agenda-file-list))
  16053. ((listp org-agenda-files) org-agenda-files)
  16054. (t (error "Invalid value of `org-agenda-files'")))))
  16055. (setq files (apply 'append
  16056. (mapcar (lambda (f)
  16057. (if (file-directory-p f)
  16058. (directory-files
  16059. f t org-agenda-file-regexp)
  16060. (list f)))
  16061. files)))
  16062. (when org-agenda-skip-unavailable-files
  16063. (setq files (delq nil
  16064. (mapcar (function
  16065. (lambda (file)
  16066. (and (file-readable-p file) file)))
  16067. files))))
  16068. (when (or (eq archives t)
  16069. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  16070. (setq files (org-add-archive-files files)))
  16071. files))
  16072. (defun org-agenda-file-p (&optional file)
  16073. "Return non-nil, if FILE is an agenda file.
  16074. If FILE is omitted, use the file associated with the current
  16075. buffer."
  16076. (member (or file (buffer-file-name))
  16077. (org-agenda-files t)))
  16078. (defun org-edit-agenda-file-list ()
  16079. "Edit the list of agenda files.
  16080. Depending on setup, this either uses customize to edit the variable
  16081. `org-agenda-files', or it visits the file that is holding the list. In the
  16082. latter case, the buffer is set up in a way that saving it automatically kills
  16083. the buffer and restores the previous window configuration."
  16084. (interactive)
  16085. (if (stringp org-agenda-files)
  16086. (let ((cw (current-window-configuration)))
  16087. (find-file org-agenda-files)
  16088. (org-set-local 'org-window-configuration cw)
  16089. (org-add-hook 'after-save-hook
  16090. (lambda ()
  16091. (set-window-configuration
  16092. (prog1 org-window-configuration
  16093. (kill-buffer (current-buffer))))
  16094. (org-install-agenda-files-menu)
  16095. (message "New agenda file list installed"))
  16096. nil 'local)
  16097. (message "%s" (substitute-command-keys
  16098. "Edit list and finish with \\[save-buffer]")))
  16099. (customize-variable 'org-agenda-files)))
  16100. (defun org-store-new-agenda-file-list (list)
  16101. "Set new value for the agenda file list and save it correctly."
  16102. (if (stringp org-agenda-files)
  16103. (let ((fe (org-read-agenda-file-list t)) b u)
  16104. (while (setq b (find-buffer-visiting org-agenda-files))
  16105. (kill-buffer b))
  16106. (with-temp-file org-agenda-files
  16107. (insert
  16108. (mapconcat
  16109. (lambda (f) ;; Keep un-expanded entries.
  16110. (if (setq u (assoc f fe))
  16111. (cdr u)
  16112. f))
  16113. list "\n")
  16114. "\n")))
  16115. (let ((org-mode-hook nil) (org-inhibit-startup t)
  16116. (org-insert-mode-line-in-empty-file nil))
  16117. (setq org-agenda-files list)
  16118. (customize-save-variable 'org-agenda-files org-agenda-files))))
  16119. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  16120. "Read the list of agenda files from a file.
  16121. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  16122. filenames, used by `org-store-new-agenda-file-list' to write back
  16123. un-expanded file names."
  16124. (when (file-directory-p org-agenda-files)
  16125. (error "`org-agenda-files' cannot be a single directory"))
  16126. (when (stringp org-agenda-files)
  16127. (with-temp-buffer
  16128. (insert-file-contents org-agenda-files)
  16129. (mapcar
  16130. (lambda (f)
  16131. (let ((e (expand-file-name (substitute-in-file-name f)
  16132. org-directory)))
  16133. (if pair-with-expansion
  16134. (cons e f)
  16135. e)))
  16136. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  16137. ;;;###autoload
  16138. (defun org-cycle-agenda-files ()
  16139. "Cycle through the files in `org-agenda-files'.
  16140. If the current buffer visits an agenda file, find the next one in the list.
  16141. If the current buffer does not, find the first agenda file."
  16142. (interactive)
  16143. (let* ((fs (org-agenda-files t))
  16144. (files (append fs (list (car fs))))
  16145. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  16146. file)
  16147. (unless files (error "No agenda files"))
  16148. (catch 'exit
  16149. (while (setq file (pop files))
  16150. (if (equal (file-truename file) tcf)
  16151. (when (car files)
  16152. (find-file (car files))
  16153. (throw 'exit t))))
  16154. (find-file (car fs)))
  16155. (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
  16156. (defun org-agenda-file-to-front (&optional to-end)
  16157. "Move/add the current file to the top of the agenda file list.
  16158. If the file is not present in the list, it is added to the front. If it is
  16159. present, it is moved there. With optional argument TO-END, add/move to the
  16160. end of the list."
  16161. (interactive "P")
  16162. (let ((org-agenda-skip-unavailable-files nil)
  16163. (file-alist (mapcar (lambda (x)
  16164. (cons (file-truename x) x))
  16165. (org-agenda-files t)))
  16166. (ctf (file-truename
  16167. (or buffer-file-name
  16168. (error "Please save the current buffer to a file"))))
  16169. x had)
  16170. (setq x (assoc ctf file-alist) had x)
  16171. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  16172. (if to-end
  16173. (setq file-alist (append (delq x file-alist) (list x)))
  16174. (setq file-alist (cons x (delq x file-alist))))
  16175. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  16176. (org-install-agenda-files-menu)
  16177. (message "File %s to %s of agenda file list"
  16178. (if had "moved" "added") (if to-end "end" "front"))))
  16179. (defun org-remove-file (&optional file)
  16180. "Remove current file from the list of files in variable `org-agenda-files'.
  16181. These are the files which are being checked for agenda entries.
  16182. Optional argument FILE means use this file instead of the current."
  16183. (interactive)
  16184. (let* ((org-agenda-skip-unavailable-files nil)
  16185. (file (or file buffer-file-name
  16186. (error "Current buffer does not visit a file")))
  16187. (true-file (file-truename file))
  16188. (afile (abbreviate-file-name file))
  16189. (files (delq nil (mapcar
  16190. (lambda (x)
  16191. (if (equal true-file
  16192. (file-truename x))
  16193. nil x))
  16194. (org-agenda-files t)))))
  16195. (if (not (= (length files) (length (org-agenda-files t))))
  16196. (progn
  16197. (org-store-new-agenda-file-list files)
  16198. (org-install-agenda-files-menu)
  16199. (message "Removed file: %s" afile))
  16200. (message "File was not in list: %s (not removed)" afile))))
  16201. (defun org-file-menu-entry (file)
  16202. (vector file (list 'find-file file) t))
  16203. (defun org-check-agenda-file (file)
  16204. "Make sure FILE exists. If not, ask user what to do."
  16205. (when (not (file-exists-p file))
  16206. (message "Non-existent agenda file %s. [R]emove from list or [A]bort?"
  16207. (abbreviate-file-name file))
  16208. (let ((r (downcase (read-char-exclusive))))
  16209. (cond
  16210. ((equal r ?r)
  16211. (org-remove-file file)
  16212. (throw 'nextfile t))
  16213. (t (error "Abort"))))))
  16214. (defun org-get-agenda-file-buffer (file)
  16215. "Get a buffer visiting FILE. If the buffer needs to be created, add
  16216. it to the list of buffers which might be released later."
  16217. (let ((buf (org-find-base-buffer-visiting file)))
  16218. (if buf
  16219. buf ; just return it
  16220. ;; Make a new buffer and remember it
  16221. (setq buf (find-file-noselect file))
  16222. (if buf (push buf org-agenda-new-buffers))
  16223. buf)))
  16224. (defun org-release-buffers (blist)
  16225. "Release all buffers in list, asking the user for confirmation when needed.
  16226. When a buffer is unmodified, it is just killed. When modified, it is saved
  16227. \(if the user agrees) and then killed."
  16228. (let (buf file)
  16229. (while (setq buf (pop blist))
  16230. (setq file (buffer-file-name buf))
  16231. (when (and (buffer-modified-p buf)
  16232. file
  16233. (y-or-n-p (format "Save file %s? " file)))
  16234. (with-current-buffer buf (save-buffer)))
  16235. (kill-buffer buf))))
  16236. (defun org-agenda-prepare-buffers (files)
  16237. "Create buffers for all agenda files, protect archived trees and comments."
  16238. (interactive)
  16239. (let ((pa '(:org-archived t))
  16240. (pc '(:org-comment t))
  16241. (pall '(:org-archived t :org-comment t))
  16242. (inhibit-read-only t)
  16243. (org-inhibit-startup org-agenda-inhibit-startup)
  16244. (rea (concat ":" org-archive-tag ":"))
  16245. file re)
  16246. (save-excursion
  16247. (save-restriction
  16248. (while (setq file (pop files))
  16249. (catch 'nextfile
  16250. (if (bufferp file)
  16251. (set-buffer file)
  16252. (org-check-agenda-file file)
  16253. (set-buffer (org-get-agenda-file-buffer file)))
  16254. (widen)
  16255. (org-refresh-category-properties)
  16256. (org-refresh-properties org-effort-property 'org-effort)
  16257. (org-refresh-properties "APPT_WARNTIME" 'org-appt-warntime)
  16258. (setq org-todo-keywords-for-agenda
  16259. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  16260. (setq org-done-keywords-for-agenda
  16261. (append org-done-keywords-for-agenda org-done-keywords))
  16262. (setq org-todo-keyword-alist-for-agenda
  16263. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  16264. (setq org-drawers-for-agenda
  16265. (append org-drawers-for-agenda org-drawers))
  16266. (setq org-tag-alist-for-agenda
  16267. (append org-tag-alist-for-agenda org-tag-alist))
  16268. (org-with-silent-modifications
  16269. (save-excursion
  16270. (remove-text-properties (point-min) (point-max) pall)
  16271. (when org-agenda-skip-archived-trees
  16272. (goto-char (point-min))
  16273. (while (re-search-forward rea nil t)
  16274. (if (org-at-heading-p t)
  16275. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  16276. (goto-char (point-min))
  16277. (setq re (format org-heading-keyword-regexp-format
  16278. org-comment-string))
  16279. (while (re-search-forward re nil t)
  16280. (add-text-properties
  16281. (match-beginning 0) (org-end-of-subtree t) pc))))))))
  16282. (setq org-todo-keywords-for-agenda
  16283. (org-uniquify org-todo-keywords-for-agenda))
  16284. (setq org-todo-keyword-alist-for-agenda
  16285. (org-uniquify org-todo-keyword-alist-for-agenda)
  16286. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  16287. ;;;; CDLaTeX minor mode
  16288. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  16289. "Keymap for the minor `org-cdlatex-mode'.")
  16290. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  16291. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  16292. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  16293. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  16294. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  16295. (defvar org-cdlatex-texmathp-advice-is-done nil
  16296. "Flag remembering if we have applied the advice to texmathp already.")
  16297. (define-minor-mode org-cdlatex-mode
  16298. "Toggle the minor `org-cdlatex-mode'.
  16299. This mode supports entering LaTeX environment and math in LaTeX fragments
  16300. in Org-mode.
  16301. \\{org-cdlatex-mode-map}"
  16302. nil " OCDL" nil
  16303. (when org-cdlatex-mode
  16304. (require 'cdlatex)
  16305. (run-hooks 'cdlatex-mode-hook)
  16306. (cdlatex-compute-tables))
  16307. (unless org-cdlatex-texmathp-advice-is-done
  16308. (setq org-cdlatex-texmathp-advice-is-done t)
  16309. (defadvice texmathp (around org-math-always-on activate)
  16310. "Always return t in org-mode buffers.
  16311. This is because we want to insert math symbols without dollars even outside
  16312. the LaTeX math segments. If Orgmode thinks that point is actually inside
  16313. an embedded LaTeX fragment, let texmathp do its job.
  16314. \\[org-cdlatex-mode-map]"
  16315. (interactive)
  16316. (let (p)
  16317. (cond
  16318. ((not (derived-mode-p 'org-mode)) ad-do-it)
  16319. ((eq this-command 'cdlatex-math-symbol)
  16320. (setq ad-return-value t
  16321. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  16322. (t
  16323. (let ((p (org-inside-LaTeX-fragment-p)))
  16324. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  16325. (setq ad-return-value t
  16326. texmathp-why '("Org-mode embedded math" . 0))
  16327. (if p ad-do-it)))))))))
  16328. (defun turn-on-org-cdlatex ()
  16329. "Unconditionally turn on `org-cdlatex-mode'."
  16330. (org-cdlatex-mode 1))
  16331. (defun org-try-cdlatex-tab ()
  16332. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  16333. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  16334. - inside a LaTeX fragment, or
  16335. - after the first word in a line, where an abbreviation expansion could
  16336. insert a LaTeX environment."
  16337. (when org-cdlatex-mode
  16338. (cond
  16339. ;; Before any word on the line: No expansion possible.
  16340. ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
  16341. ;; Just after first word on the line: Expand it. Make sure it
  16342. ;; cannot happen on headlines, though.
  16343. ((save-excursion
  16344. (skip-chars-backward "a-zA-Z0-9*")
  16345. (skip-chars-backward " \t")
  16346. (and (bolp) (not (org-at-heading-p))))
  16347. (cdlatex-tab) t)
  16348. ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
  16349. (defun org-cdlatex-underscore-caret (&optional arg)
  16350. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  16351. Revert to the normal definition outside of these fragments."
  16352. (interactive "P")
  16353. (if (org-inside-LaTeX-fragment-p)
  16354. (call-interactively 'cdlatex-sub-superscript)
  16355. (let (org-cdlatex-mode)
  16356. (call-interactively (key-binding (vector last-input-event))))))
  16357. (defun org-cdlatex-math-modify (&optional arg)
  16358. "Execute `cdlatex-math-modify' in LaTeX fragments.
  16359. Revert to the normal definition outside of these fragments."
  16360. (interactive "P")
  16361. (if (org-inside-LaTeX-fragment-p)
  16362. (call-interactively 'cdlatex-math-modify)
  16363. (let (org-cdlatex-mode)
  16364. (call-interactively (key-binding (vector last-input-event))))))
  16365. ;;;; LaTeX fragments
  16366. (defvar org-latex-regexps
  16367. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  16368. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  16369. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  16370. ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  16371. ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  16372. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  16373. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  16374. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  16375. "Regular expressions for matching embedded LaTeX.")
  16376. (defun org-inside-LaTeX-fragment-p ()
  16377. "Test if point is inside a LaTeX fragment.
  16378. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  16379. sequence appearing also before point.
  16380. Even though the matchers for math are configurable, this function assumes
  16381. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  16382. delimiters are skipped when they have been removed by customization.
  16383. The return value is nil, or a cons cell with the delimiter and the
  16384. position of this delimiter.
  16385. This function does a reasonably good job, but can locally be fooled by
  16386. for example currency specifications. For example it will assume being in
  16387. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  16388. fragments that are properly closed, but during editing, we have to live
  16389. with the uncertainty caused by missing closing delimiters. This function
  16390. looks only before point, not after."
  16391. (catch 'exit
  16392. (let ((pos (point))
  16393. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  16394. (lim (progn
  16395. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  16396. (point)))
  16397. dd-on str (start 0) m re)
  16398. (goto-char pos)
  16399. (when dodollar
  16400. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  16401. re (nth 1 (assoc "$" org-latex-regexps)))
  16402. (while (string-match re str start)
  16403. (cond
  16404. ((= (match-end 0) (length str))
  16405. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  16406. ((= (match-end 0) (- (length str) 5))
  16407. (throw 'exit nil))
  16408. (t (setq start (match-end 0))))))
  16409. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  16410. (goto-char pos)
  16411. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  16412. (and (match-beginning 2) (throw 'exit nil))
  16413. ;; count $$
  16414. (while (re-search-backward "\\$\\$" lim t)
  16415. (setq dd-on (not dd-on)))
  16416. (goto-char pos)
  16417. (if dd-on (cons "$$" m))))))
  16418. (defun org-inside-latex-macro-p ()
  16419. "Is point inside a LaTeX macro or its arguments?"
  16420. (save-match-data
  16421. (org-in-regexp
  16422. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  16423. (defvar org-latex-fragment-image-overlays nil
  16424. "List of overlays carrying the images of latex fragments.")
  16425. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  16426. (defun org-remove-latex-fragment-image-overlays ()
  16427. "Remove all overlays with LaTeX fragment images in current buffer."
  16428. (mapc 'delete-overlay org-latex-fragment-image-overlays)
  16429. (setq org-latex-fragment-image-overlays nil))
  16430. (defun org-preview-latex-fragment (&optional subtree)
  16431. "Preview the LaTeX fragment at point, or all locally or globally.
  16432. If the cursor is in a LaTeX fragment, create the image and overlay
  16433. it over the source code. If there is no fragment at point, display
  16434. all fragments in the current text, from one headline to the next. With
  16435. prefix SUBTREE, display all fragments in the current subtree. With a
  16436. double prefix arg \\[universal-argument] \\[universal-argument], or when \
  16437. the cursor is before the first headline,
  16438. display all fragments in the buffer.
  16439. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  16440. (interactive "P")
  16441. (unless buffer-file-name
  16442. (error "Can't preview LaTeX fragment in a non-file buffer"))
  16443. (org-remove-latex-fragment-image-overlays)
  16444. (save-excursion
  16445. (save-restriction
  16446. (let (beg end at msg)
  16447. (cond
  16448. ((or (equal subtree '(16))
  16449. (not (save-excursion
  16450. (re-search-backward org-outline-regexp-bol nil t))))
  16451. (setq beg (point-min) end (point-max)
  16452. msg "Creating images for buffer...%s"))
  16453. ((equal subtree '(4))
  16454. (org-back-to-heading)
  16455. (setq beg (point) end (org-end-of-subtree t)
  16456. msg "Creating images for subtree...%s"))
  16457. (t
  16458. (if (setq at (org-inside-LaTeX-fragment-p))
  16459. (goto-char (max (point-min) (- (cdr at) 2)))
  16460. (org-back-to-heading))
  16461. (setq beg (point) end (progn (outline-next-heading) (point))
  16462. msg (if at "Creating image...%s"
  16463. "Creating images for entry...%s"))))
  16464. (message msg "")
  16465. (narrow-to-region beg end)
  16466. (goto-char beg)
  16467. (org-format-latex
  16468. (concat org-latex-preview-ltxpng-directory (file-name-sans-extension
  16469. (file-name-nondirectory
  16470. buffer-file-name)))
  16471. default-directory 'overlays msg at 'forbuffer
  16472. org-latex-create-formula-image-program)
  16473. (message msg "done. Use `C-c C-c' to remove images.")))))
  16474. (defun org-format-latex (prefix &optional dir overlays msg at
  16475. forbuffer processing-type)
  16476. "Replace LaTeX fragments with links to an image, and produce images.
  16477. Some of the options can be changed using the variable
  16478. `org-format-latex-options'."
  16479. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  16480. (let* ((prefixnodir (file-name-nondirectory prefix))
  16481. (absprefix (expand-file-name prefix dir))
  16482. (todir (file-name-directory absprefix))
  16483. (opt org-format-latex-options)
  16484. (optnew org-format-latex-options)
  16485. (matchers (plist-get opt :matchers))
  16486. (re-list org-latex-regexps)
  16487. (cnt 0) txt hash link beg end re e checkdir
  16488. string
  16489. m n block-type block linkfile movefile ov)
  16490. ;; Check the different regular expressions
  16491. (while (setq e (pop re-list))
  16492. (setq m (car e) re (nth 1 e) n (nth 2 e) block-type (nth 3 e)
  16493. block (if block-type "\n\n" ""))
  16494. (when (member m matchers)
  16495. (goto-char (point-min))
  16496. (while (re-search-forward re nil t)
  16497. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  16498. (or (not overlays)
  16499. (not (eq (get-char-property (match-beginning n)
  16500. 'org-overlay-type)
  16501. 'org-latex-overlay))))
  16502. (cond
  16503. ((eq processing-type 'verbatim))
  16504. ((eq processing-type 'mathjax)
  16505. ;; Prepare for MathJax processing.
  16506. (setq string (match-string n))
  16507. (when (member m '("$" "$1"))
  16508. (save-excursion
  16509. (delete-region (match-beginning n) (match-end n))
  16510. (goto-char (match-beginning n))
  16511. (insert (concat "\\(" (substring string 1 -1) "\\)")))))
  16512. ((or (eq processing-type 'dvipng)
  16513. (eq processing-type 'imagemagick))
  16514. ;; Process to an image.
  16515. (setq txt (match-string n)
  16516. beg (match-beginning n) end (match-end n)
  16517. cnt (1+ cnt))
  16518. (let ((face (face-at-point))
  16519. (fg (plist-get opt :foreground))
  16520. (bg (plist-get opt :background))
  16521. ;; Ensure full list is printed.
  16522. print-length print-level)
  16523. (when forbuffer
  16524. ;; Get the colors from the face at point.
  16525. (goto-char beg)
  16526. (when (eq fg 'auto)
  16527. (setq fg (face-attribute face :foreground nil 'default)))
  16528. (when (eq bg 'auto)
  16529. (setq bg (face-attribute face :background nil 'default)))
  16530. (setq optnew (copy-sequence opt))
  16531. (plist-put optnew :foreground fg)
  16532. (plist-put optnew :background bg))
  16533. (setq hash (sha1 (prin1-to-string
  16534. (list org-format-latex-header
  16535. org-latex-default-packages-alist
  16536. org-latex-packages-alist
  16537. org-format-latex-options
  16538. forbuffer txt fg bg)))
  16539. linkfile (format "%s_%s.png" prefix hash)
  16540. movefile (format "%s_%s.png" absprefix hash)))
  16541. (setq link (concat block "[[file:" linkfile "]]" block))
  16542. (if msg (message msg cnt))
  16543. (goto-char beg)
  16544. (unless checkdir ; Ensure the directory exists.
  16545. (setq checkdir t)
  16546. (or (file-directory-p todir) (make-directory todir t)))
  16547. (unless (file-exists-p movefile)
  16548. (org-create-formula-image
  16549. txt movefile optnew forbuffer processing-type))
  16550. (if overlays
  16551. (progn
  16552. (mapc (lambda (o)
  16553. (if (eq (overlay-get o 'org-overlay-type)
  16554. 'org-latex-overlay)
  16555. (delete-overlay o)))
  16556. (overlays-in beg end))
  16557. (setq ov (make-overlay beg end))
  16558. (overlay-put ov 'org-overlay-type 'org-latex-overlay)
  16559. (if (featurep 'xemacs)
  16560. (progn
  16561. (overlay-put ov 'invisible t)
  16562. (overlay-put
  16563. ov 'end-glyph
  16564. (make-glyph (vector 'png :file movefile))))
  16565. (overlay-put
  16566. ov 'display
  16567. (list 'image :type 'png :file movefile :ascent 'center)))
  16568. (push ov org-latex-fragment-image-overlays)
  16569. (goto-char end))
  16570. (delete-region beg end)
  16571. (insert (org-add-props link
  16572. (list 'org-latex-src
  16573. (replace-regexp-in-string
  16574. "\"" "" txt)
  16575. 'org-latex-src-embed-type
  16576. (if block-type 'paragraph 'character))))))
  16577. ((eq processing-type 'mathml)
  16578. ;; Process to MathML
  16579. (unless (save-match-data (org-format-latex-mathml-available-p))
  16580. (error "LaTeX to MathML converter not configured"))
  16581. (setq txt (match-string n)
  16582. beg (match-beginning n) end (match-end n)
  16583. cnt (1+ cnt))
  16584. (if msg (message msg cnt))
  16585. (goto-char beg)
  16586. (delete-region beg end)
  16587. (insert (org-format-latex-as-mathml
  16588. txt block-type prefix dir)))
  16589. (t
  16590. (error "Unknown conversion type %s for latex fragments"
  16591. processing-type)))))))))
  16592. (defun org-create-math-formula (latex-frag &optional mathml-file)
  16593. "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
  16594. Use `org-latex-to-mathml-convert-command'. If the conversion is
  16595. sucessful, return the portion between \"<math...> </math>\"
  16596. elements otherwise return nil. When MATHML-FILE is specified,
  16597. write the results in to that file. When invoked as an
  16598. interactive command, prompt for LATEX-FRAG, with initial value
  16599. set to the current active region and echo the results for user
  16600. inspection."
  16601. (interactive (list (let ((frag (when (org-region-active-p)
  16602. (buffer-substring-no-properties
  16603. (region-beginning) (region-end)))))
  16604. (read-string "LaTeX Fragment: " frag nil frag))))
  16605. (unless latex-frag (error "Invalid latex-frag"))
  16606. (let* ((tmp-in-file (file-relative-name
  16607. (make-temp-name (expand-file-name "ltxmathml-in"))))
  16608. (ignore (write-region latex-frag nil tmp-in-file))
  16609. (tmp-out-file (file-relative-name
  16610. (make-temp-name (expand-file-name "ltxmathml-out"))))
  16611. (cmd (format-spec
  16612. org-latex-to-mathml-convert-command
  16613. `((?j . ,(shell-quote-argument
  16614. (expand-file-name org-latex-to-mathml-jar-file)))
  16615. (?I . ,(shell-quote-argument tmp-in-file))
  16616. (?o . ,(shell-quote-argument tmp-out-file)))))
  16617. mathml shell-command-output)
  16618. (when (org-called-interactively-p 'any)
  16619. (unless (org-format-latex-mathml-available-p)
  16620. (error "LaTeX to MathML converter not configured")))
  16621. (message "Running %s" cmd)
  16622. (setq shell-command-output (shell-command-to-string cmd))
  16623. (setq mathml
  16624. (when (file-readable-p tmp-out-file)
  16625. (with-current-buffer (find-file-noselect tmp-out-file t)
  16626. (goto-char (point-min))
  16627. (when (re-search-forward
  16628. (concat
  16629. (regexp-quote
  16630. "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">")
  16631. "\\(.\\|\n\\)*"
  16632. (regexp-quote "</math>")) nil t)
  16633. (prog1 (match-string 0) (kill-buffer))))))
  16634. (cond
  16635. (mathml
  16636. (setq mathml
  16637. (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
  16638. (when mathml-file
  16639. (write-region mathml nil mathml-file))
  16640. (when (org-called-interactively-p 'any)
  16641. (message mathml)))
  16642. ((message "LaTeX to MathML conversion failed")
  16643. (message shell-command-output)))
  16644. (delete-file tmp-in-file)
  16645. (when (file-exists-p tmp-out-file)
  16646. (delete-file tmp-out-file))
  16647. mathml))
  16648. (defun org-format-latex-as-mathml (latex-frag latex-frag-type
  16649. prefix &optional dir)
  16650. "Use `org-create-math-formula' but check local cache first."
  16651. (let* ((absprefix (expand-file-name prefix dir))
  16652. (print-length nil) (print-level nil)
  16653. (formula-id (concat
  16654. "formula-"
  16655. (sha1
  16656. (prin1-to-string
  16657. (list latex-frag
  16658. org-latex-to-mathml-convert-command)))))
  16659. (formula-cache (format "%s-%s.mathml" absprefix formula-id))
  16660. (formula-cache-dir (file-name-directory formula-cache)))
  16661. (unless (file-directory-p formula-cache-dir)
  16662. (make-directory formula-cache-dir t))
  16663. (unless (file-exists-p formula-cache)
  16664. (org-create-math-formula latex-frag formula-cache))
  16665. (if (file-exists-p formula-cache)
  16666. ;; Successful conversion. Return the link to MathML file.
  16667. (org-add-props
  16668. (format "[[file:%s]]" (file-relative-name formula-cache dir))
  16669. (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
  16670. 'org-latex-src-embed-type (if latex-frag-type
  16671. 'paragraph 'character)))
  16672. ;; Failed conversion. Return the LaTeX fragment verbatim
  16673. latex-frag)))
  16674. (defun org-create-formula-image (string tofile options buffer &optional type)
  16675. "Create an image from LaTeX source using dvipng or convert.
  16676. This function calls either `org-create-formula-image-with-dvipng'
  16677. or `org-create-formula-image-with-imagemagick' depending on the
  16678. value of `org-latex-create-formula-image-program' or on the value
  16679. of the optional TYPE variable.
  16680. Note: ultimately these two function should be combined as they
  16681. share a good deal of logic."
  16682. (org-check-external-command
  16683. "latex" "needed to convert LaTeX fragments to images")
  16684. (funcall
  16685. (case (or type org-latex-create-formula-image-program)
  16686. ('dvipng
  16687. (org-check-external-command
  16688. "dvipng" "needed to convert LaTeX fragments to images")
  16689. #'org-create-formula-image-with-dvipng)
  16690. ('imagemagick
  16691. (org-check-external-command
  16692. "convert" "you need to install imagemagick")
  16693. #'org-create-formula-image-with-imagemagick)
  16694. (t (error
  16695. "invalid value of `org-latex-create-formula-image-program'")))
  16696. string tofile options buffer))
  16697. (declare-function org-export--get-global-options "ox" (&optional backend))
  16698. (declare-function org-export--get-inbuffer-options "ox" (&optional backend))
  16699. (defun org-create-formula--latex-header ()
  16700. "Return LaTeX header appropriate for previewing a LaTeX snippet."
  16701. (org-latex-guess-inputenc
  16702. (org-splice-latex-header
  16703. org-format-latex-header
  16704. org-latex-default-packages-alist
  16705. org-latex-packages-alist t
  16706. (plist-get
  16707. (org-combine-plists
  16708. (org-export--get-global-options 'latex)
  16709. (org-export--get-inbuffer-options 'latex))
  16710. :latex-header))))
  16711. ;; This function borrows from Ganesh Swami's latex2png.el
  16712. (defun org-create-formula-image-with-dvipng (string tofile options buffer)
  16713. "This calls dvipng."
  16714. (require 'ox-latex)
  16715. (let* ((tmpdir (if (featurep 'xemacs)
  16716. (temp-directory)
  16717. temporary-file-directory))
  16718. (texfilebase (make-temp-name
  16719. (expand-file-name "orgtex" tmpdir)))
  16720. (texfile (concat texfilebase ".tex"))
  16721. (dvifile (concat texfilebase ".dvi"))
  16722. (pngfile (concat texfilebase ".png"))
  16723. (fnh (if (featurep 'xemacs)
  16724. (font-height (face-font 'default))
  16725. (face-attribute 'default :height nil)))
  16726. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  16727. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  16728. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  16729. "Black"))
  16730. (bg (or (plist-get options (if buffer :background :html-background))
  16731. "Transparent")))
  16732. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground))
  16733. (unless (string= fg "Transparent") (setq fg (org-dvipng-color-format fg))))
  16734. (if (eq bg 'default) (setq bg (org-dvipng-color :background))
  16735. (unless (string= bg "Transparent") (setq bg (org-dvipng-color-format bg))))
  16736. (let ((latex-header (org-create-formula--latex-header)))
  16737. (with-temp-file texfile
  16738. (insert latex-header)
  16739. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")))
  16740. (let ((dir default-directory))
  16741. (condition-case nil
  16742. (progn
  16743. (cd tmpdir)
  16744. (call-process "latex" nil nil nil texfile))
  16745. (error nil))
  16746. (cd dir))
  16747. (if (not (file-exists-p dvifile))
  16748. (progn (message "Failed to create dvi file from %s" texfile) nil)
  16749. (condition-case nil
  16750. (if (featurep 'xemacs)
  16751. (call-process "dvipng" nil nil nil
  16752. "-fg" fg "-bg" bg
  16753. "-T" "tight"
  16754. "-o" pngfile
  16755. dvifile)
  16756. (call-process "dvipng" nil nil nil
  16757. "-fg" fg "-bg" bg
  16758. "-D" dpi
  16759. ;;"-x" scale "-y" scale
  16760. "-T" "tight"
  16761. "-o" pngfile
  16762. dvifile))
  16763. (error nil))
  16764. (if (not (file-exists-p pngfile))
  16765. (if org-format-latex-signal-error
  16766. (error "Failed to create png file from %s" texfile)
  16767. (message "Failed to create png file from %s" texfile)
  16768. nil)
  16769. ;; Use the requested file name and clean up
  16770. (copy-file pngfile tofile 'replace)
  16771. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png" ".out") do
  16772. (if (file-exists-p (concat texfilebase e))
  16773. (delete-file (concat texfilebase e))))
  16774. pngfile))))
  16775. (declare-function org-latex-compile "ox-latex" (texfile &optional snippet))
  16776. (defun org-create-formula-image-with-imagemagick (string tofile options buffer)
  16777. "This calls convert, which is included into imagemagick."
  16778. (require 'ox-latex)
  16779. (let* ((tmpdir (if (featurep 'xemacs)
  16780. (temp-directory)
  16781. temporary-file-directory))
  16782. (texfilebase (make-temp-name
  16783. (expand-file-name "orgtex" tmpdir)))
  16784. (texfile (concat texfilebase ".tex"))
  16785. (pdffile (concat texfilebase ".pdf"))
  16786. (pngfile (concat texfilebase ".png"))
  16787. (fnh (if (featurep 'xemacs)
  16788. (font-height (face-font 'default))
  16789. (face-attribute 'default :height nil)))
  16790. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  16791. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  16792. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  16793. "black"))
  16794. (bg (or (plist-get options (if buffer :background :html-background))
  16795. "white")))
  16796. (if (eq fg 'default) (setq fg (org-latex-color :foreground))
  16797. (setq fg (org-latex-color-format fg)))
  16798. (if (eq bg 'default) (setq bg (org-latex-color :background))
  16799. (setq bg (org-latex-color-format
  16800. (if (string= bg "Transparent") "white" bg))))
  16801. (let ((latex-header (org-create-formula--latex-header)))
  16802. (with-temp-file texfile
  16803. (insert latex-header)
  16804. (insert "\n\\begin{document}\n"
  16805. "\\definecolor{fg}{rgb}{" fg "}\n"
  16806. "\\definecolor{bg}{rgb}{" bg "}\n"
  16807. "\n\\pagecolor{bg}\n"
  16808. "\n{\\color{fg}\n"
  16809. string
  16810. "\n}\n"
  16811. "\n\\end{document}\n")))
  16812. (org-latex-compile texfile t)
  16813. (if (not (file-exists-p pdffile))
  16814. (progn (message "Failed to create pdf file from %s" texfile) nil)
  16815. (condition-case nil
  16816. (if (featurep 'xemacs)
  16817. (call-process "convert" nil nil nil
  16818. "-density" "96"
  16819. "-trim"
  16820. "-antialias"
  16821. pdffile
  16822. "-quality" "100"
  16823. ;; "-sharpen" "0x1.0"
  16824. pngfile)
  16825. (call-process "convert" nil nil nil
  16826. "-density" dpi
  16827. "-trim"
  16828. "-antialias"
  16829. pdffile
  16830. "-quality" "100"
  16831. ;; "-sharpen" "0x1.0"
  16832. pngfile))
  16833. (error nil))
  16834. (if (not (file-exists-p pngfile))
  16835. (if org-format-latex-signal-error
  16836. (error "Failed to create png file from %s" texfile)
  16837. (message "Failed to create png file from %s" texfile)
  16838. nil)
  16839. ;; Use the requested file name and clean up
  16840. (copy-file pngfile tofile 'replace)
  16841. (loop for e in '(".pdf" ".tex" ".aux" ".log" ".png") do
  16842. (if (file-exists-p (concat texfilebase e))
  16843. (delete-file (concat texfilebase e))))
  16844. pngfile))))
  16845. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  16846. "Fill a LaTeX header template TPL.
  16847. In the template, the following place holders will be recognized:
  16848. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  16849. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  16850. [PACKAGES] \\usepackage statements for PKG
  16851. [NO-PACKAGES] do not include PKG
  16852. [EXTRA] the string EXTRA
  16853. [NO-EXTRA] do not include EXTRA
  16854. For backward compatibility, if both the positive and the negative place
  16855. holder is missing, the positive one (without the \"NO-\") will be
  16856. assumed to be present at the end of the template.
  16857. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  16858. EXTRA is a string.
  16859. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  16860. (let (rpl (end ""))
  16861. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  16862. (setq rpl (if (or (match-end 1) (not def-pkg))
  16863. "" (org-latex-packages-to-string def-pkg snippets-p t))
  16864. tpl (replace-match rpl t t tpl))
  16865. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  16866. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  16867. (setq rpl (if (or (match-end 1) (not pkg))
  16868. "" (org-latex-packages-to-string pkg snippets-p t))
  16869. tpl (replace-match rpl t t tpl))
  16870. (if pkg (setq end
  16871. (concat end "\n"
  16872. (org-latex-packages-to-string pkg snippets-p)))))
  16873. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  16874. (setq rpl (if (or (match-end 1) (not extra))
  16875. "" (concat extra "\n"))
  16876. tpl (replace-match rpl t t tpl))
  16877. (if (and extra (string-match "\\S-" extra))
  16878. (setq end (concat end "\n" extra))))
  16879. (if (string-match "\\S-" end)
  16880. (concat tpl "\n" end)
  16881. tpl)))
  16882. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  16883. "Turn an alist of packages into a string with the \\usepackage macros."
  16884. (setq pkg (mapconcat (lambda(p)
  16885. (cond
  16886. ((stringp p) p)
  16887. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  16888. (format "%% Package %s omitted" (cadr p)))
  16889. ((equal "" (car p))
  16890. (format "\\usepackage{%s}" (cadr p)))
  16891. (t
  16892. (format "\\usepackage[%s]{%s}"
  16893. (car p) (cadr p)))))
  16894. pkg
  16895. "\n"))
  16896. (if newline (concat pkg "\n") pkg))
  16897. (defun org-dvipng-color (attr)
  16898. "Return a RGB color specification for dvipng."
  16899. (apply 'format "rgb %s %s %s"
  16900. (mapcar 'org-normalize-color
  16901. (if (featurep 'xemacs)
  16902. (color-rgb-components
  16903. (face-property 'default
  16904. (cond ((eq attr :foreground) 'foreground)
  16905. ((eq attr :background) 'background))))
  16906. (color-values (face-attribute 'default attr nil))))))
  16907. (defun org-dvipng-color-format (color-name)
  16908. "Convert COLOR-NAME to a RGB color value for dvipng."
  16909. (apply 'format "rgb %s %s %s"
  16910. (mapcar 'org-normalize-color
  16911. (color-values color-name))))
  16912. (defun org-latex-color (attr)
  16913. "Return a RGB color for the LaTeX color package."
  16914. (apply 'format "%s,%s,%s"
  16915. (mapcar 'org-normalize-color
  16916. (if (featurep 'xemacs)
  16917. (color-rgb-components
  16918. (face-property 'default
  16919. (cond ((eq attr :foreground) 'foreground)
  16920. ((eq attr :background) 'background))))
  16921. (color-values (face-attribute 'default attr nil))))))
  16922. (defun org-latex-color-format (color-name)
  16923. "Convert COLOR-NAME to a RGB color value."
  16924. (apply 'format "%s,%s,%s"
  16925. (mapcar 'org-normalize-color
  16926. (color-values color-name))))
  16927. (defun org-normalize-color (value)
  16928. "Return string to be used as color value for an RGB component."
  16929. (format "%g" (/ value 65535.0)))
  16930. ;; Image display
  16931. (defvar org-inline-image-overlays nil)
  16932. (make-variable-buffer-local 'org-inline-image-overlays)
  16933. (defun org-toggle-inline-images (&optional include-linked)
  16934. "Toggle the display of inline images.
  16935. INCLUDE-LINKED is passed to `org-display-inline-images'."
  16936. (interactive "P")
  16937. (if org-inline-image-overlays
  16938. (progn
  16939. (org-remove-inline-images)
  16940. (message "Inline image display turned off"))
  16941. (org-display-inline-images include-linked)
  16942. (if (and (org-called-interactively-p)
  16943. org-inline-image-overlays)
  16944. (message "%d images displayed inline"
  16945. (length org-inline-image-overlays))
  16946. (message "No images to display inline"))))
  16947. (defun org-redisplay-inline-images ()
  16948. "Refresh the display of inline images."
  16949. (interactive)
  16950. (if (not org-inline-image-overlays)
  16951. (org-toggle-inline-images)
  16952. (org-toggle-inline-images)
  16953. (org-toggle-inline-images)))
  16954. (defun org-display-inline-images (&optional include-linked refresh beg end)
  16955. "Display inline images.
  16956. Normally only links without a description part are inlined, because this
  16957. is how it will work for export. When INCLUDE-LINKED is set, also links
  16958. with a description part will be inlined. This can be nice for a quick
  16959. look at those images, but it does not reflect what exported files will look
  16960. like.
  16961. When REFRESH is set, refresh existing images between BEG and END.
  16962. This will create new image displays only if necessary.
  16963. BEG and END default to the buffer boundaries."
  16964. (interactive "P")
  16965. (unless refresh
  16966. (org-remove-inline-images)
  16967. (if (fboundp 'clear-image-cache) (clear-image-cache)))
  16968. (save-excursion
  16969. (save-restriction
  16970. (widen)
  16971. (setq beg (or beg (point-min)) end (or end (point-max)))
  16972. (goto-char beg)
  16973. (let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([^]\n]+?"
  16974. (substring (org-image-file-name-regexp) 0 -2)
  16975. "\\)\\]" (if include-linked "" "\\]")))
  16976. old file ov img type attrwidth width)
  16977. (while (re-search-forward re end t)
  16978. (setq old (get-char-property-and-overlay (match-beginning 1)
  16979. 'org-image-overlay)
  16980. file (expand-file-name
  16981. (concat (or (match-string 3) "") (match-string 4))))
  16982. (when (image-type-available-p 'imagemagick)
  16983. (setq attrwidth (if (or (listp org-image-actual-width)
  16984. (null org-image-actual-width))
  16985. (save-excursion
  16986. (save-match-data
  16987. (when (re-search-backward
  16988. "#\\+ATTR.*width=\"\\([^\"]+\\)\""
  16989. (save-excursion
  16990. (re-search-backward "^[ \t]*$\\|\\`" nil t)) t)
  16991. (string-to-number (match-string 1))))))
  16992. width (cond ((eq org-image-actual-width t) nil)
  16993. ((null org-image-actual-width) attrwidth)
  16994. ((numberp org-image-actual-width)
  16995. org-image-actual-width)
  16996. ((listp org-image-actual-width)
  16997. (or attrwidth (car org-image-actual-width))))
  16998. type (if width 'imagemagick)))
  16999. (when (file-exists-p file)
  17000. (if (and (car-safe old) refresh)
  17001. (image-refresh (overlay-get (cdr old) 'display))
  17002. (setq img (save-match-data (create-image file type nil :width width)))
  17003. (when img
  17004. (setq ov (make-overlay (match-beginning 0) (match-end 0)))
  17005. (overlay-put ov 'display img)
  17006. (overlay-put ov 'face 'default)
  17007. (overlay-put ov 'org-image-overlay t)
  17008. (overlay-put ov 'modification-hooks
  17009. (list 'org-display-inline-remove-overlay))
  17010. (push ov org-inline-image-overlays)))))))))
  17011. (define-obsolete-function-alias
  17012. 'org-display-inline-modification-hook 'org-display-inline-remove-overlay "24.3")
  17013. (defun org-display-inline-remove-overlay (ov after beg end &optional len)
  17014. "Remove inline-display overlay if a corresponding region is modified."
  17015. (let ((inhibit-modification-hooks t))
  17016. (when (and ov after)
  17017. (delete ov org-inline-image-overlays)
  17018. (delete-overlay ov))))
  17019. (defun org-remove-inline-images ()
  17020. "Remove inline display of images."
  17021. (interactive)
  17022. (mapc 'delete-overlay org-inline-image-overlays)
  17023. (setq org-inline-image-overlays nil))
  17024. ;;;; Key bindings
  17025. ;; Outline functions from `outline-mode-prefix-map'
  17026. ;; that can be remapped in Org:
  17027. (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
  17028. (define-key org-mode-map [remap show-subtree] 'org-show-subtree)
  17029. (define-key org-mode-map [remap outline-forward-same-level]
  17030. 'org-forward-heading-same-level)
  17031. (define-key org-mode-map [remap outline-backward-same-level]
  17032. 'org-backward-heading-same-level)
  17033. (define-key org-mode-map [remap show-branches]
  17034. 'org-kill-note-or-show-branches)
  17035. (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
  17036. (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
  17037. (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
  17038. ;; Outline functions from `outline-mode-prefix-map' that can not
  17039. ;; be remapped in Org:
  17040. ;;
  17041. ;; - the column "key binding" shows whether the Outline function is still
  17042. ;; available in Org mode on the same key that it has been bound to in
  17043. ;; Outline mode:
  17044. ;; - "overridden": key used for a different functionality in Org mode
  17045. ;; - else: key still bound to the same Outline function in Org mode
  17046. ;;
  17047. ;; | Outline function | key binding | Org replacement |
  17048. ;; |------------------------------------+-------------+-----------------------|
  17049. ;; | `outline-next-visible-heading' | `C-c C-n' | still same function |
  17050. ;; | `outline-previous-visible-heading' | `C-c C-p' | still same function |
  17051. ;; | `outline-up-heading' | `C-c C-u' | still same function |
  17052. ;; | `outline-move-subtree-up' | overridden | better: org-shiftup |
  17053. ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
  17054. ;; | `show-entry' | overridden | no replacement |
  17055. ;; | `show-children' | `C-c C-i' | visibility cycling |
  17056. ;; | `show-branches' | `C-c C-k' | still same function |
  17057. ;; | `show-subtree' | overridden | visibility cycling |
  17058. ;; | `show-all' | overridden | no replacement |
  17059. ;; | `hide-subtree' | overridden | visibility cycling |
  17060. ;; | `hide-body' | overridden | no replacement |
  17061. ;; | `hide-entry' | overridden | visibility cycling |
  17062. ;; | `hide-leaves' | overridden | no replacement |
  17063. ;; | `hide-sublevels' | overridden | no replacement |
  17064. ;; | `hide-other' | overridden | no replacement |
  17065. ;; Make `C-c C-x' a prefix key
  17066. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  17067. ;; TAB key with modifiers
  17068. (org-defkey org-mode-map "\C-i" 'org-cycle)
  17069. (org-defkey org-mode-map [(tab)] 'org-cycle)
  17070. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  17071. (org-defkey org-mode-map "\M-\t" 'pcomplete)
  17072. ;; The following line is necessary under Suse GNU/Linux
  17073. (unless (featurep 'xemacs)
  17074. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  17075. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  17076. (define-key org-mode-map [backtab] 'org-shifttab)
  17077. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  17078. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  17079. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  17080. ;; Cursor keys with modifiers
  17081. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  17082. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  17083. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  17084. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  17085. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  17086. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  17087. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  17088. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  17089. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  17090. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  17091. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  17092. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  17093. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  17094. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  17095. (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
  17096. (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
  17097. ;; Babel keys
  17098. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  17099. (mapc (lambda (pair)
  17100. (define-key org-babel-map (car pair) (cdr pair)))
  17101. org-babel-key-bindings)
  17102. ;;; Extra keys for tty access.
  17103. ;; We only set them when really needed because otherwise the
  17104. ;; menus don't show the simple keys
  17105. (when (or org-use-extra-keys
  17106. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  17107. (not window-system))
  17108. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  17109. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  17110. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  17111. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  17112. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  17113. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  17114. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  17115. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  17116. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  17117. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  17118. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  17119. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  17120. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  17121. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  17122. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  17123. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  17124. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  17125. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  17126. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  17127. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  17128. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  17129. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  17130. (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
  17131. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  17132. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  17133. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  17134. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  17135. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  17136. ;; All the other keys
  17137. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  17138. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  17139. (if (boundp 'narrow-map)
  17140. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  17141. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  17142. (if (boundp 'narrow-map)
  17143. (org-defkey narrow-map "b" 'org-narrow-to-block)
  17144. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  17145. (if (boundp 'narrow-map)
  17146. (org-defkey narrow-map "e" 'org-narrow-to-element)
  17147. (org-defkey org-mode-map "\C-xne" 'org-narrow-to-element))
  17148. (org-defkey org-mode-map "\C-\M-t" 'org-transpose-element)
  17149. (org-defkey org-mode-map "\M-}" 'org-forward-element)
  17150. (org-defkey org-mode-map "\M-{" 'org-backward-element)
  17151. (org-defkey org-mode-map "\C-c\C-^" 'org-up-element)
  17152. (org-defkey org-mode-map "\C-c\C-_" 'org-down-element)
  17153. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-heading-same-level)
  17154. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-heading-same-level)
  17155. (org-defkey org-mode-map "\C-c\M-f" 'org-next-block)
  17156. (org-defkey org-mode-map "\C-c\M-b" 'org-previous-block)
  17157. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  17158. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  17159. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  17160. (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
  17161. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  17162. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  17163. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  17164. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  17165. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  17166. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  17167. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  17168. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  17169. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  17170. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  17171. (org-defkey org-mode-map "\C-c\M-w" 'org-copy)
  17172. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  17173. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  17174. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  17175. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  17176. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  17177. (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
  17178. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  17179. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  17180. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  17181. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  17182. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  17183. (org-defkey org-mode-map "\C-c\C-\M-l" 'org-insert-all-links)
  17184. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  17185. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  17186. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  17187. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  17188. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  17189. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  17190. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  17191. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  17192. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  17193. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  17194. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  17195. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  17196. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  17197. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  17198. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  17199. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  17200. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  17201. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  17202. (org-defkey org-mode-map "\C-c^" 'org-sort)
  17203. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  17204. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  17205. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  17206. (org-defkey org-mode-map "\C-m" 'org-return)
  17207. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  17208. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  17209. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  17210. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  17211. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  17212. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  17213. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  17214. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  17215. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  17216. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  17217. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  17218. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  17219. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  17220. (org-defkey org-mode-map "\C-c\C-e" 'org-export-dispatch)
  17221. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  17222. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  17223. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  17224. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  17225. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  17226. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  17227. (org-defkey org-mode-map "\M-h" 'org-mark-element)
  17228. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  17229. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  17230. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  17231. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  17232. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  17233. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  17234. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  17235. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-in-last)
  17236. (org-defkey org-mode-map "\C-c\C-x\C-z" 'org-resolve-clocks)
  17237. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  17238. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  17239. (org-defkey org-mode-map "\C-c\C-x\C-q" 'org-clock-cancel)
  17240. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  17241. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  17242. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  17243. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  17244. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  17245. (org-defkey org-mode-map "\C-c\C-x\C-\M-v" 'org-redisplay-inline-images)
  17246. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  17247. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  17248. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  17249. (org-defkey org-mode-map "\C-c\C-xP" 'org-set-property-and-value)
  17250. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  17251. (org-defkey org-mode-map "\C-c\C-xE" 'org-inc-effort)
  17252. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  17253. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  17254. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  17255. (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
  17256. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  17257. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  17258. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  17259. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  17260. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  17261. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  17262. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  17263. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  17264. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  17265. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  17266. (when (featurep 'xemacs)
  17267. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  17268. (defconst org-speed-commands-default
  17269. '(
  17270. ("Outline Navigation")
  17271. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  17272. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  17273. ("f" . (org-speed-move-safe 'org-forward-heading-same-level))
  17274. ("b" . (org-speed-move-safe 'org-backward-heading-same-level))
  17275. ("F" . org-next-block)
  17276. ("B" . org-previous-block)
  17277. ("u" . (org-speed-move-safe 'outline-up-heading))
  17278. ("j" . org-goto)
  17279. ("g" . (org-refile t))
  17280. ("Outline Visibility")
  17281. ("c" . org-cycle)
  17282. ("C" . org-shifttab)
  17283. (" " . org-display-outline-path)
  17284. ("=" . org-columns)
  17285. ("Outline Structure Editing")
  17286. ("U" . org-shiftmetaup)
  17287. ("D" . org-shiftmetadown)
  17288. ("r" . org-metaright)
  17289. ("l" . org-metaleft)
  17290. ("R" . org-shiftmetaright)
  17291. ("L" . org-shiftmetaleft)
  17292. ("i" . (progn (forward-char 1) (call-interactively
  17293. 'org-insert-heading-respect-content)))
  17294. ("^" . org-sort)
  17295. ("w" . org-refile)
  17296. ("a" . org-archive-subtree-default-with-confirmation)
  17297. ("@" . org-mark-subtree)
  17298. ("#" . org-toggle-comment)
  17299. ("Clock Commands")
  17300. ("I" . org-clock-in)
  17301. ("O" . org-clock-out)
  17302. ("Meta Data Editing")
  17303. ("t" . org-todo)
  17304. ("," . (org-priority))
  17305. ("0" . (org-priority ?\ ))
  17306. ("1" . (org-priority ?A))
  17307. ("2" . (org-priority ?B))
  17308. ("3" . (org-priority ?C))
  17309. (":" . org-set-tags-command)
  17310. ("e" . org-set-effort)
  17311. ("E" . org-inc-effort)
  17312. ("W" . (lambda(m) (interactive "sMinutes before warning: ")
  17313. (org-entry-put (point) "APPT_WARNTIME" m)))
  17314. ("Agenda Views etc")
  17315. ("v" . org-agenda)
  17316. ("/" . org-sparse-tree)
  17317. ("Misc")
  17318. ("o" . org-open-at-point)
  17319. ("?" . org-speed-command-help)
  17320. ("<" . (org-agenda-set-restriction-lock 'subtree))
  17321. (">" . (org-agenda-remove-restriction-lock))
  17322. )
  17323. "The default speed commands.")
  17324. (defun org-print-speed-command (e)
  17325. (if (> (length (car e)) 1)
  17326. (progn
  17327. (princ "\n")
  17328. (princ (car e))
  17329. (princ "\n")
  17330. (princ (make-string (length (car e)) ?-))
  17331. (princ "\n"))
  17332. (princ (car e))
  17333. (princ " ")
  17334. (if (symbolp (cdr e))
  17335. (princ (symbol-name (cdr e)))
  17336. (prin1 (cdr e)))
  17337. (princ "\n")))
  17338. (defun org-speed-command-help ()
  17339. "Show the available speed commands."
  17340. (interactive)
  17341. (if (not org-use-speed-commands)
  17342. (error "Speed commands are not activated, customize `org-use-speed-commands'")
  17343. (with-output-to-temp-buffer "*Help*"
  17344. (princ "User-defined Speed commands\n===========================\n")
  17345. (mapc 'org-print-speed-command org-speed-commands-user)
  17346. (princ "\n")
  17347. (princ "Built-in Speed commands\n=======================\n")
  17348. (mapc 'org-print-speed-command org-speed-commands-default))
  17349. (with-current-buffer "*Help*"
  17350. (setq truncate-lines t))))
  17351. (defun org-speed-move-safe (cmd)
  17352. "Execute CMD, but make sure that the cursor always ends up in a headline.
  17353. If not, return to the original position and throw an error."
  17354. (interactive)
  17355. (let ((pos (point)))
  17356. (call-interactively cmd)
  17357. (unless (and (bolp) (org-at-heading-p))
  17358. (goto-char pos)
  17359. (error "Boundary reached while executing %s" cmd))))
  17360. (defvar org-self-insert-command-undo-counter 0)
  17361. (defvar org-table-auto-blank-field) ; defined in org-table.el
  17362. (defvar org-speed-command nil)
  17363. (define-obsolete-function-alias
  17364. 'org-speed-command-default-hook 'org-speed-command-activate "24.3")
  17365. (defun org-speed-command-activate (keys)
  17366. "Hook for activating single-letter speed commands.
  17367. `org-speed-commands-default' specifies a minimal command set.
  17368. Use `org-speed-commands-user' for further customization."
  17369. (when (or (and (bolp) (looking-at org-outline-regexp))
  17370. (and (functionp org-use-speed-commands)
  17371. (funcall org-use-speed-commands)))
  17372. (cdr (assoc keys (append org-speed-commands-user
  17373. org-speed-commands-default)))))
  17374. (define-obsolete-function-alias
  17375. 'org-babel-speed-command-hook 'org-babel-speed-command-activate "24.3")
  17376. (defun org-babel-speed-command-activate (keys)
  17377. "Hook for activating single-letter code block commands."
  17378. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  17379. (cdr (assoc keys org-babel-key-bindings))))
  17380. (defcustom org-speed-command-hook
  17381. '(org-speed-command-default-hook org-babel-speed-command-hook)
  17382. "Hook for activating speed commands at strategic locations.
  17383. Hook functions are called in sequence until a valid handler is
  17384. found.
  17385. Each hook takes a single argument, a user-pressed command key
  17386. which is also a `self-insert-command' from the global map.
  17387. Within the hook, examine the cursor position and the command key
  17388. and return nil or a valid handler as appropriate. Handler could
  17389. be one of an interactive command, a function, or a form.
  17390. Set `org-use-speed-commands' to non-nil value to enable this
  17391. hook. The default setting is `org-speed-command-activate'."
  17392. :group 'org-structure
  17393. :version "24.1"
  17394. :type 'hook)
  17395. (defun org-self-insert-command (N)
  17396. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  17397. If the cursor is in a table looking at whitespace, the whitespace is
  17398. overwritten, and the table is not marked as requiring realignment."
  17399. (interactive "p")
  17400. (org-check-before-invisible-edit 'insert)
  17401. (cond
  17402. ((and org-use-speed-commands
  17403. (setq org-speed-command
  17404. (run-hook-with-args-until-success
  17405. 'org-speed-command-hook (this-command-keys))))
  17406. (cond
  17407. ((commandp org-speed-command)
  17408. (setq this-command org-speed-command)
  17409. (call-interactively org-speed-command))
  17410. ((functionp org-speed-command)
  17411. (funcall org-speed-command))
  17412. ((and org-speed-command (listp org-speed-command))
  17413. (eval org-speed-command))
  17414. (t (let (org-use-speed-commands)
  17415. (call-interactively 'org-self-insert-command)))))
  17416. ((and
  17417. (org-table-p)
  17418. (progn
  17419. ;; check if we blank the field, and if that triggers align
  17420. (and (featurep 'org-table) org-table-auto-blank-field
  17421. (member last-command
  17422. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  17423. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  17424. ;; got extra space, this field does not determine column width
  17425. (let (org-table-may-need-update) (org-table-blank-field))
  17426. ;; no extra space, this field may determine column width
  17427. (org-table-blank-field)))
  17428. t)
  17429. (eq N 1)
  17430. (looking-at "[^|\n]* |"))
  17431. (let (org-table-may-need-update)
  17432. (goto-char (1- (match-end 0)))
  17433. (backward-delete-char 1)
  17434. (goto-char (match-beginning 0))
  17435. (self-insert-command N)))
  17436. (t
  17437. (setq org-table-may-need-update t)
  17438. (self-insert-command N)
  17439. (org-fix-tags-on-the-fly)
  17440. (if org-self-insert-cluster-for-undo
  17441. (if (not (eq last-command 'org-self-insert-command))
  17442. (setq org-self-insert-command-undo-counter 1)
  17443. (if (>= org-self-insert-command-undo-counter 20)
  17444. (setq org-self-insert-command-undo-counter 1)
  17445. (and (> org-self-insert-command-undo-counter 0)
  17446. buffer-undo-list (listp buffer-undo-list)
  17447. (not (cadr buffer-undo-list)) ; remove nil entry
  17448. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  17449. (setq org-self-insert-command-undo-counter
  17450. (1+ org-self-insert-command-undo-counter))))))))
  17451. (defun org-check-before-invisible-edit (kind)
  17452. "Check is editing if kind KIND would be dangerous with invisible text around.
  17453. The detailed reaction depends on the user option `org-catch-invisible-edits'."
  17454. ;; First, try to get out of here as quickly as possible, to reduce overhead
  17455. (if (and org-catch-invisible-edits
  17456. (or (not (boundp 'visible-mode)) (not visible-mode))
  17457. (or (get-char-property (point) 'invisible)
  17458. (get-char-property (max (point-min) (1- (point))) 'invisible)))
  17459. ;; OK, we need to take a closer look
  17460. (let* ((invisible-at-point (get-char-property (point) 'invisible))
  17461. (invisible-before-point (if (bobp) nil (get-char-property
  17462. (1- (point)) 'invisible)))
  17463. (border-and-ok-direction
  17464. (or
  17465. ;; Check if we are acting predictably before invisible text
  17466. (and invisible-at-point (not invisible-before-point)
  17467. (memq kind '(insert delete-backward)))
  17468. ;; Check if we are acting predictably after invisible text
  17469. ;; This works not well, and I have turned it off. It seems
  17470. ;; better to always show and stop after invisible text.
  17471. ;; (and (not invisible-at-point) invisible-before-point
  17472. ;; (memq kind '(insert delete)))
  17473. )))
  17474. (when (or (memq invisible-at-point '(outline org-hide-block t))
  17475. (memq invisible-before-point '(outline org-hide-block t)))
  17476. (if (eq org-catch-invisible-edits 'error)
  17477. (error "Editing in invisible areas is prohibited - make visible first"))
  17478. (if (and org-custom-properties-overlays
  17479. (y-or-n-p "Display invisible properties in this buffer? "))
  17480. (org-toggle-custom-properties-visibility)
  17481. ;; Make the area visible
  17482. (save-excursion
  17483. (if invisible-before-point
  17484. (goto-char (previous-single-char-property-change
  17485. (point) 'invisible)))
  17486. (org-cycle))
  17487. (cond
  17488. ((eq org-catch-invisible-edits 'show)
  17489. ;; That's it, we do the edit after showing
  17490. (message
  17491. "Unfolding invisible region around point before editing")
  17492. (sit-for 1))
  17493. ((and (eq org-catch-invisible-edits 'smart)
  17494. border-and-ok-direction)
  17495. (message "Unfolding invisible region around point before editing"))
  17496. (t
  17497. ;; Don't do the edit, make the user repeat it in full visibility
  17498. (error "Edit in invisible region aborted, repeat to confirm with text visible"))))))))
  17499. (defun org-fix-tags-on-the-fly ()
  17500. (when (and (equal (char-after (point-at-bol)) ?*)
  17501. (org-at-heading-p))
  17502. (org-align-tags-here org-tags-column)))
  17503. (defun org-delete-backward-char (N)
  17504. "Like `delete-backward-char', insert whitespace at field end in tables.
  17505. When deleting backwards, in tables this function will insert whitespace in
  17506. front of the next \"|\" separator, to keep the table aligned. The table will
  17507. still be marked for re-alignment if the field did fill the entire column,
  17508. because, in this case the deletion might narrow the column."
  17509. (interactive "p")
  17510. (save-match-data
  17511. (org-check-before-invisible-edit 'delete-backward)
  17512. (if (and (org-table-p)
  17513. (eq N 1)
  17514. (string-match "|" (buffer-substring (point-at-bol) (point)))
  17515. (looking-at ".*?|"))
  17516. (let ((pos (point))
  17517. (noalign (looking-at "[^|\n\r]* |"))
  17518. (c org-table-may-need-update))
  17519. (backward-delete-char N)
  17520. (if (not overwrite-mode)
  17521. (progn
  17522. (skip-chars-forward "^|")
  17523. (insert " ")
  17524. (goto-char (1- pos))))
  17525. ;; noalign: if there were two spaces at the end, this field
  17526. ;; does not determine the width of the column.
  17527. (if noalign (setq org-table-may-need-update c)))
  17528. (backward-delete-char N)
  17529. (org-fix-tags-on-the-fly))))
  17530. (defun org-delete-char (N)
  17531. "Like `delete-char', but insert whitespace at field end in tables.
  17532. When deleting characters, in tables this function will insert whitespace in
  17533. front of the next \"|\" separator, to keep the table aligned. The table will
  17534. still be marked for re-alignment if the field did fill the entire column,
  17535. because, in this case the deletion might narrow the column."
  17536. (interactive "p")
  17537. (save-match-data
  17538. (org-check-before-invisible-edit 'delete)
  17539. (if (and (org-table-p)
  17540. (not (bolp))
  17541. (not (= (char-after) ?|))
  17542. (eq N 1))
  17543. (if (looking-at ".*?|")
  17544. (let ((pos (point))
  17545. (noalign (looking-at "[^|\n\r]* |"))
  17546. (c org-table-may-need-update))
  17547. (replace-match (concat
  17548. (substring (match-string 0) 1 -1)
  17549. " |"))
  17550. (goto-char pos)
  17551. ;; noalign: if there were two spaces at the end, this field
  17552. ;; does not determine the width of the column.
  17553. (if noalign (setq org-table-may-need-update c)))
  17554. (delete-char N))
  17555. (delete-char N)
  17556. (org-fix-tags-on-the-fly))))
  17557. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  17558. (put 'org-self-insert-command 'delete-selection t)
  17559. (put 'orgtbl-self-insert-command 'delete-selection t)
  17560. (put 'org-delete-char 'delete-selection 'supersede)
  17561. (put 'org-delete-backward-char 'delete-selection 'supersede)
  17562. (put 'org-yank 'delete-selection 'yank)
  17563. ;; Make `flyspell-mode' delay after some commands
  17564. (put 'org-self-insert-command 'flyspell-delayed t)
  17565. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  17566. (put 'org-delete-char 'flyspell-delayed t)
  17567. (put 'org-delete-backward-char 'flyspell-delayed t)
  17568. ;; Make pabbrev-mode expand after org-mode commands
  17569. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  17570. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  17571. ;; How to do this: Measure non-white length of current string
  17572. ;; If equal to column width, we should realign.
  17573. (defun org-remap (map &rest commands)
  17574. "In MAP, remap the functions given in COMMANDS.
  17575. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  17576. (let (new old)
  17577. (while commands
  17578. (setq old (pop commands) new (pop commands))
  17579. (if (fboundp 'command-remapping)
  17580. (org-defkey map (vector 'remap old) new)
  17581. (substitute-key-definition old new map global-map)))))
  17582. (defun org-transpose-words ()
  17583. "Transpose words for Org.
  17584. This uses the `org-mode-transpose-word-syntax-table' syntax
  17585. table, which interprets characters in `org-emphasis-alist' as
  17586. word constituants."
  17587. (interactive)
  17588. (with-syntax-table org-mode-transpose-word-syntax-table
  17589. (call-interactively 'transpose-words)))
  17590. (org-remap org-mode-map 'transpose-words 'org-transpose-words)
  17591. (when (eq org-enable-table-editor 'optimized)
  17592. ;; If the user wants maximum table support, we need to hijack
  17593. ;; some standard editing functions
  17594. (org-remap org-mode-map
  17595. 'self-insert-command 'org-self-insert-command
  17596. 'delete-char 'org-delete-char
  17597. 'delete-backward-char 'org-delete-backward-char)
  17598. (org-defkey org-mode-map "|" 'org-force-self-insert))
  17599. (defvar org-ctrl-c-ctrl-c-hook nil
  17600. "Hook for functions attaching themselves to `C-c C-c'.
  17601. This can be used to add additional functionality to the C-c C-c
  17602. key which executes context-dependent commands. This hook is run
  17603. before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
  17604. run after the last test.
  17605. Each function will be called with no arguments. The function
  17606. must check if the context is appropriate for it to act. If yes,
  17607. it should do its thing and then return a non-nil value. If the
  17608. context is wrong, just do nothing and return nil.")
  17609. (defvar org-ctrl-c-ctrl-c-final-hook nil
  17610. "Hook for functions attaching themselves to `C-c C-c'.
  17611. This can be used to add additional functionality to the C-c C-c
  17612. key which executes context-dependent commands. This hook is run
  17613. after any other test, while `org-ctrl-c-ctrl-c-hook' is run
  17614. before the first test.
  17615. Each function will be called with no arguments. The function
  17616. must check if the context is appropriate for it to act. If yes,
  17617. it should do its thing and then return a non-nil value. If the
  17618. context is wrong, just do nothing and return nil.")
  17619. (defvar org-tab-first-hook nil
  17620. "Hook for functions to attach themselves to TAB.
  17621. See `org-ctrl-c-ctrl-c-hook' for more information.
  17622. This hook runs as the first action when TAB is pressed, even before
  17623. `org-cycle' messes around with the `outline-regexp' to cater for
  17624. inline tasks and plain list item folding.
  17625. If any function in this hook returns t, any other actions that
  17626. would have been caused by TAB (such as table field motion or visibility
  17627. cycling) will not occur.")
  17628. (defvar org-tab-after-check-for-table-hook nil
  17629. "Hook for functions to attach themselves to TAB.
  17630. See `org-ctrl-c-ctrl-c-hook' for more information.
  17631. This hook runs after it has been established that the cursor is not in a
  17632. table, but before checking if the cursor is in a headline or if global cycling
  17633. should be done.
  17634. If any function in this hook returns t, not other actions like visibility
  17635. cycling will be done.")
  17636. (defvar org-tab-after-check-for-cycling-hook nil
  17637. "Hook for functions to attach themselves to TAB.
  17638. See `org-ctrl-c-ctrl-c-hook' for more information.
  17639. This hook runs after it has been established that not table field motion and
  17640. not visibility should be done because of current context. This is probably
  17641. the place where a package like yasnippets can hook in.")
  17642. (defvar org-tab-before-tab-emulation-hook nil
  17643. "Hook for functions to attach themselves to TAB.
  17644. See `org-ctrl-c-ctrl-c-hook' for more information.
  17645. This hook runs after every other options for TAB have been exhausted, but
  17646. before indentation and \t insertion takes place.")
  17647. (defvar org-metaleft-hook nil
  17648. "Hook for functions attaching themselves to `M-left'.
  17649. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17650. (defvar org-metaright-hook nil
  17651. "Hook for functions attaching themselves to `M-right'.
  17652. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17653. (defvar org-metaup-hook nil
  17654. "Hook for functions attaching themselves to `M-up'.
  17655. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17656. (defvar org-metadown-hook nil
  17657. "Hook for functions attaching themselves to `M-down'.
  17658. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17659. (defvar org-shiftmetaleft-hook nil
  17660. "Hook for functions attaching themselves to `M-S-left'.
  17661. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17662. (defvar org-shiftmetaright-hook nil
  17663. "Hook for functions attaching themselves to `M-S-right'.
  17664. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17665. (defvar org-shiftmetaup-hook nil
  17666. "Hook for functions attaching themselves to `M-S-up'.
  17667. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17668. (defvar org-shiftmetadown-hook nil
  17669. "Hook for functions attaching themselves to `M-S-down'.
  17670. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17671. (defvar org-metareturn-hook nil
  17672. "Hook for functions attaching themselves to `M-RET'.
  17673. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17674. (defvar org-shiftup-hook nil
  17675. "Hook for functions attaching themselves to `S-up'.
  17676. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17677. (defvar org-shiftup-final-hook nil
  17678. "Hook for functions attaching themselves to `S-up'.
  17679. This one runs after all other options except shift-select have been excluded.
  17680. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17681. (defvar org-shiftdown-hook nil
  17682. "Hook for functions attaching themselves to `S-down'.
  17683. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17684. (defvar org-shiftdown-final-hook nil
  17685. "Hook for functions attaching themselves to `S-down'.
  17686. This one runs after all other options except shift-select have been excluded.
  17687. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17688. (defvar org-shiftleft-hook nil
  17689. "Hook for functions attaching themselves to `S-left'.
  17690. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17691. (defvar org-shiftleft-final-hook nil
  17692. "Hook for functions attaching themselves to `S-left'.
  17693. This one runs after all other options except shift-select have been excluded.
  17694. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17695. (defvar org-shiftright-hook nil
  17696. "Hook for functions attaching themselves to `S-right'.
  17697. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17698. (defvar org-shiftright-final-hook nil
  17699. "Hook for functions attaching themselves to `S-right'.
  17700. This one runs after all other options except shift-select have been excluded.
  17701. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17702. (defun org-modifier-cursor-error ()
  17703. "Throw an error, a modified cursor command was applied in wrong context."
  17704. (error "This command is active in special context like tables, headlines or items"))
  17705. (defun org-shiftselect-error ()
  17706. "Throw an error because Shift-Cursor command was applied in wrong context."
  17707. (if (and (boundp 'shift-select-mode) shift-select-mode)
  17708. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  17709. (error "This command works only in special context like headlines or timestamps")))
  17710. (defun org-call-for-shift-select (cmd)
  17711. (let ((this-command-keys-shift-translated t))
  17712. (call-interactively cmd)))
  17713. (defun org-shifttab (&optional arg)
  17714. "Global visibility cycling or move to previous table field.
  17715. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  17716. on context.
  17717. See the individual commands for more information."
  17718. (interactive "P")
  17719. (cond
  17720. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  17721. ((integerp arg)
  17722. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  17723. (message "Content view to level: %d" arg)
  17724. (org-content (prefix-numeric-value arg2))
  17725. (setq org-cycle-global-status 'overview)))
  17726. (t (call-interactively 'org-global-cycle))))
  17727. (defun org-shiftmetaleft ()
  17728. "Promote subtree or delete table column.
  17729. Calls `org-promote-subtree', `org-outdent-item-tree', or
  17730. `org-table-delete-column', depending on context. See the
  17731. individual commands for more information."
  17732. (interactive)
  17733. (cond
  17734. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  17735. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  17736. ((org-at-heading-p) (call-interactively 'org-promote-subtree))
  17737. ((if (not (org-region-active-p)) (org-at-item-p)
  17738. (save-excursion (goto-char (region-beginning))
  17739. (org-at-item-p)))
  17740. (call-interactively 'org-outdent-item-tree))
  17741. (t (org-modifier-cursor-error))))
  17742. (defun org-shiftmetaright ()
  17743. "Demote subtree or insert table column.
  17744. Calls `org-demote-subtree', `org-indent-item-tree', or
  17745. `org-table-insert-column', depending on context. See the
  17746. individual commands for more information."
  17747. (interactive)
  17748. (cond
  17749. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  17750. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  17751. ((org-at-heading-p) (call-interactively 'org-demote-subtree))
  17752. ((if (not (org-region-active-p)) (org-at-item-p)
  17753. (save-excursion (goto-char (region-beginning))
  17754. (org-at-item-p)))
  17755. (call-interactively 'org-indent-item-tree))
  17756. (t (org-modifier-cursor-error))))
  17757. (defun org-shiftmetaup (&optional arg)
  17758. "Move subtree up or kill table row.
  17759. Calls `org-move-subtree-up' or `org-table-kill-row' or
  17760. `org-move-item-up' or `org-timestamp-up', depending on context.
  17761. See the individual commands for more information."
  17762. (interactive "P")
  17763. (cond
  17764. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  17765. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  17766. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  17767. ((org-at-item-p) (call-interactively 'org-move-item-up))
  17768. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  17769. (call-interactively 'org-timestamp-up)))
  17770. (t (call-interactively 'org-drag-line-backward))))
  17771. (defun org-shiftmetadown (&optional arg)
  17772. "Move subtree down or insert table row.
  17773. Calls `org-move-subtree-down' or `org-table-insert-row' or
  17774. `org-move-item-down' or `org-timestamp-up', depending on context.
  17775. See the individual commands for more information."
  17776. (interactive "P")
  17777. (cond
  17778. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  17779. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  17780. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  17781. ((org-at-item-p) (call-interactively 'org-move-item-down))
  17782. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  17783. (call-interactively 'org-timestamp-down)))
  17784. (t (call-interactively 'org-drag-line-forward))))
  17785. (defsubst org-hidden-tree-error ()
  17786. (error
  17787. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  17788. (defun org-metaleft (&optional arg)
  17789. "Promote heading or move table column to left.
  17790. Calls `org-do-promote' or `org-table-move-column', depending on context.
  17791. With no specific context, calls the Emacs default `backward-word'.
  17792. See the individual commands for more information."
  17793. (interactive "P")
  17794. (cond
  17795. ((run-hook-with-args-until-success 'org-metaleft-hook))
  17796. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  17797. ((org-with-limited-levels
  17798. (or (org-at-heading-p)
  17799. (and (org-region-active-p)
  17800. (save-excursion
  17801. (goto-char (region-beginning))
  17802. (org-at-heading-p)))))
  17803. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  17804. (call-interactively 'org-do-promote))
  17805. ;; At an inline task.
  17806. ((org-at-heading-p)
  17807. (call-interactively 'org-inlinetask-promote))
  17808. ((or (org-at-item-p)
  17809. (and (org-region-active-p)
  17810. (save-excursion
  17811. (goto-char (region-beginning))
  17812. (org-at-item-p))))
  17813. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  17814. (call-interactively 'org-outdent-item))
  17815. (t (call-interactively 'backward-word))))
  17816. (defun org-metaright (&optional arg)
  17817. "Demote a subtree, a list item or move table column to right.
  17818. In front of a drawer or a block keyword, indent it correctly.
  17819. With no specific context, calls the Emacs default `forward-word'.
  17820. See the individual commands for more information."
  17821. (interactive "P")
  17822. (cond
  17823. ((run-hook-with-args-until-success 'org-metaright-hook))
  17824. ((org-at-table-p) (call-interactively 'org-table-move-column))
  17825. ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
  17826. ((org-at-block-p) (call-interactively 'org-indent-block))
  17827. ((org-with-limited-levels
  17828. (or (org-at-heading-p)
  17829. (and (org-region-active-p)
  17830. (save-excursion
  17831. (goto-char (region-beginning))
  17832. (org-at-heading-p)))))
  17833. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  17834. (call-interactively 'org-do-demote))
  17835. ;; At an inline task.
  17836. ((org-at-heading-p)
  17837. (call-interactively 'org-inlinetask-demote))
  17838. ((or (org-at-item-p)
  17839. (and (org-region-active-p)
  17840. (save-excursion
  17841. (goto-char (region-beginning))
  17842. (org-at-item-p))))
  17843. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  17844. (call-interactively 'org-indent-item))
  17845. (t (call-interactively 'forward-word))))
  17846. (defun org-check-for-hidden (what)
  17847. "Check if there are hidden headlines/items in the current visual line.
  17848. WHAT can be either `headlines' or `items'. If the current line is
  17849. an outline or item heading and it has a folded subtree below it,
  17850. this function returns t, nil otherwise."
  17851. (let ((re (cond
  17852. ((eq what 'headlines) org-outline-regexp-bol)
  17853. ((eq what 'items) (org-item-beginning-re))
  17854. (t (error "This should not happen"))))
  17855. beg end)
  17856. (save-excursion
  17857. (catch 'exit
  17858. (unless (org-region-active-p)
  17859. (setq beg (point-at-bol))
  17860. (beginning-of-line 2)
  17861. (while (and (not (eobp)) ;; this is like `next-line'
  17862. (get-char-property (1- (point)) 'invisible))
  17863. (beginning-of-line 2))
  17864. (setq end (point))
  17865. (goto-char beg)
  17866. (goto-char (point-at-eol))
  17867. (setq end (max end (point)))
  17868. (while (re-search-forward re end t)
  17869. (if (get-char-property (match-beginning 0) 'invisible)
  17870. (throw 'exit t))))
  17871. nil))))
  17872. (defun org-metaup (&optional arg)
  17873. "Move subtree up or move table row up.
  17874. Calls `org-move-subtree-up' or `org-table-move-row' or
  17875. `org-move-item-up', depending on context. See the individual commands
  17876. for more information."
  17877. (interactive "P")
  17878. (cond
  17879. ((run-hook-with-args-until-success 'org-metaup-hook))
  17880. ((org-region-active-p)
  17881. (let* ((a (min (region-beginning) (region-end)))
  17882. (b (1- (max (region-beginning) (region-end))))
  17883. (c (save-excursion (goto-char a)
  17884. (move-beginning-of-line 0)))
  17885. (d (save-excursion (goto-char a)
  17886. (move-end-of-line 0) (point))))
  17887. (transpose-regions a b c d)
  17888. (goto-char c)))
  17889. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  17890. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  17891. ((org-at-item-p) (call-interactively 'org-move-item-up))
  17892. (t (org-drag-element-backward))))
  17893. (defun org-metadown (&optional arg)
  17894. "Move subtree down or move table row down.
  17895. Calls `org-move-subtree-down' or `org-table-move-row' or
  17896. `org-move-item-down', depending on context. See the individual
  17897. commands for more information."
  17898. (interactive "P")
  17899. (cond
  17900. ((run-hook-with-args-until-success 'org-metadown-hook))
  17901. ((org-region-active-p)
  17902. (let* ((a (min (region-beginning) (region-end)))
  17903. (b (max (region-beginning) (region-end)))
  17904. (c (save-excursion (goto-char b)
  17905. (move-beginning-of-line 1)))
  17906. (d (save-excursion (goto-char b)
  17907. (move-end-of-line 1) (1+ (point)))))
  17908. (transpose-regions a b c d)
  17909. (goto-char d)))
  17910. ((org-at-table-p) (call-interactively 'org-table-move-row))
  17911. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  17912. ((org-at-item-p) (call-interactively 'org-move-item-down))
  17913. (t (org-drag-element-forward))))
  17914. (defun org-shiftup (&optional arg)
  17915. "Increase item in timestamp or increase priority of current headline.
  17916. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  17917. depending on context. See the individual commands for more information."
  17918. (interactive "P")
  17919. (cond
  17920. ((run-hook-with-args-until-success 'org-shiftup-hook))
  17921. ((and org-support-shift-select (org-region-active-p))
  17922. (org-call-for-shift-select 'previous-line))
  17923. ((org-at-timestamp-p t)
  17924. (call-interactively (if org-edit-timestamp-down-means-later
  17925. 'org-timestamp-down 'org-timestamp-up)))
  17926. ((and (not (eq org-support-shift-select 'always))
  17927. org-enable-priority-commands
  17928. (org-at-heading-p))
  17929. (call-interactively 'org-priority-up))
  17930. ((and (not org-support-shift-select) (org-at-item-p))
  17931. (call-interactively 'org-previous-item))
  17932. ((org-clocktable-try-shift 'up arg))
  17933. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  17934. (org-support-shift-select
  17935. (org-call-for-shift-select 'previous-line))
  17936. (t (org-shiftselect-error))))
  17937. (defun org-shiftdown (&optional arg)
  17938. "Decrease item in timestamp or decrease priority of current headline.
  17939. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  17940. depending on context. See the individual commands for more information."
  17941. (interactive "P")
  17942. (cond
  17943. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  17944. ((and org-support-shift-select (org-region-active-p))
  17945. (org-call-for-shift-select 'next-line))
  17946. ((org-at-timestamp-p t)
  17947. (call-interactively (if org-edit-timestamp-down-means-later
  17948. 'org-timestamp-up 'org-timestamp-down)))
  17949. ((and (not (eq org-support-shift-select 'always))
  17950. org-enable-priority-commands
  17951. (org-at-heading-p))
  17952. (call-interactively 'org-priority-down))
  17953. ((and (not org-support-shift-select) (org-at-item-p))
  17954. (call-interactively 'org-next-item))
  17955. ((org-clocktable-try-shift 'down arg))
  17956. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  17957. (org-support-shift-select
  17958. (org-call-for-shift-select 'next-line))
  17959. (t (org-shiftselect-error))))
  17960. (defun org-shiftright (&optional arg)
  17961. "Cycle the thing at point or in the current line, depending on context.
  17962. Depending on context, this does one of the following:
  17963. - switch a timestamp at point one day into the future
  17964. - on a headline, switch to the next TODO keyword.
  17965. - on an item, switch entire list to the next bullet type
  17966. - on a property line, switch to the next allowed value
  17967. - on a clocktable definition line, move time block into the future"
  17968. (interactive "P")
  17969. (cond
  17970. ((run-hook-with-args-until-success 'org-shiftright-hook))
  17971. ((and org-support-shift-select (org-region-active-p))
  17972. (org-call-for-shift-select 'forward-char))
  17973. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  17974. ((and (not (eq org-support-shift-select 'always))
  17975. (org-at-heading-p))
  17976. (let ((org-inhibit-logging
  17977. (not org-treat-S-cursor-todo-selection-as-state-change))
  17978. (org-inhibit-blocking
  17979. (not org-treat-S-cursor-todo-selection-as-state-change)))
  17980. (org-call-with-arg 'org-todo 'right)))
  17981. ((or (and org-support-shift-select
  17982. (not (eq org-support-shift-select 'always))
  17983. (org-at-item-bullet-p))
  17984. (and (not org-support-shift-select) (org-at-item-p)))
  17985. (org-call-with-arg 'org-cycle-list-bullet nil))
  17986. ((and (not (eq org-support-shift-select 'always))
  17987. (org-at-property-p))
  17988. (call-interactively 'org-property-next-allowed-value))
  17989. ((org-clocktable-try-shift 'right arg))
  17990. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  17991. (org-support-shift-select
  17992. (org-call-for-shift-select 'forward-char))
  17993. (t (org-shiftselect-error))))
  17994. (defun org-shiftleft (&optional arg)
  17995. "Cycle the thing at point or in the current line, depending on context.
  17996. Depending on context, this does one of the following:
  17997. - switch a timestamp at point one day into the past
  17998. - on a headline, switch to the previous TODO keyword.
  17999. - on an item, switch entire list to the previous bullet type
  18000. - on a property line, switch to the previous allowed value
  18001. - on a clocktable definition line, move time block into the past"
  18002. (interactive "P")
  18003. (cond
  18004. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  18005. ((and org-support-shift-select (org-region-active-p))
  18006. (org-call-for-shift-select 'backward-char))
  18007. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  18008. ((and (not (eq org-support-shift-select 'always))
  18009. (org-at-heading-p))
  18010. (let ((org-inhibit-logging
  18011. (not org-treat-S-cursor-todo-selection-as-state-change))
  18012. (org-inhibit-blocking
  18013. (not org-treat-S-cursor-todo-selection-as-state-change)))
  18014. (org-call-with-arg 'org-todo 'left)))
  18015. ((or (and org-support-shift-select
  18016. (not (eq org-support-shift-select 'always))
  18017. (org-at-item-bullet-p))
  18018. (and (not org-support-shift-select) (org-at-item-p)))
  18019. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  18020. ((and (not (eq org-support-shift-select 'always))
  18021. (org-at-property-p))
  18022. (call-interactively 'org-property-previous-allowed-value))
  18023. ((org-clocktable-try-shift 'left arg))
  18024. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  18025. (org-support-shift-select
  18026. (org-call-for-shift-select 'backward-char))
  18027. (t (org-shiftselect-error))))
  18028. (defun org-shiftcontrolright ()
  18029. "Switch to next TODO set."
  18030. (interactive)
  18031. (cond
  18032. ((and org-support-shift-select (org-region-active-p))
  18033. (org-call-for-shift-select 'forward-word))
  18034. ((and (not (eq org-support-shift-select 'always))
  18035. (org-at-heading-p))
  18036. (org-call-with-arg 'org-todo 'nextset))
  18037. (org-support-shift-select
  18038. (org-call-for-shift-select 'forward-word))
  18039. (t (org-shiftselect-error))))
  18040. (defun org-shiftcontrolleft ()
  18041. "Switch to previous TODO set."
  18042. (interactive)
  18043. (cond
  18044. ((and org-support-shift-select (org-region-active-p))
  18045. (org-call-for-shift-select 'backward-word))
  18046. ((and (not (eq org-support-shift-select 'always))
  18047. (org-at-heading-p))
  18048. (org-call-with-arg 'org-todo 'previousset))
  18049. (org-support-shift-select
  18050. (org-call-for-shift-select 'backward-word))
  18051. (t (org-shiftselect-error))))
  18052. (defun org-shiftcontrolup (&optional n)
  18053. "Change timestamps synchronously up in CLOCK log lines.
  18054. Optional argument N tells to change by that many units."
  18055. (interactive "P")
  18056. (cond ((and (not org-support-shift-select)
  18057. (org-at-clock-log-p)
  18058. (org-at-timestamp-p t))
  18059. (org-clock-timestamps-up n))
  18060. (t (org-shiftselect-error))))
  18061. (defun org-shiftcontroldown (&optional n)
  18062. "Change timestamps synchronously down in CLOCK log lines.
  18063. Optional argument N tells to change by that many units."
  18064. (interactive "P")
  18065. (cond ((and (not org-support-shift-select)
  18066. (org-at-clock-log-p)
  18067. (org-at-timestamp-p t))
  18068. (org-clock-timestamps-down n))
  18069. (t (org-shiftselect-error))))
  18070. (defun org-ctrl-c-ret ()
  18071. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  18072. (interactive)
  18073. (cond
  18074. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  18075. (t (call-interactively 'org-insert-heading))))
  18076. (defun org-find-visible ()
  18077. (let ((s (point)))
  18078. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  18079. (get-char-property s 'invisible)))
  18080. s))
  18081. (defun org-find-invisible ()
  18082. (let ((s (point)))
  18083. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  18084. (not (get-char-property s 'invisible))))
  18085. s))
  18086. (defun org-copy-visible (beg end)
  18087. "Copy the visible parts of the region."
  18088. (interactive "r")
  18089. (let (snippets s)
  18090. (save-excursion
  18091. (save-restriction
  18092. (narrow-to-region beg end)
  18093. (setq s (goto-char (point-min)))
  18094. (while (not (= (point) (point-max)))
  18095. (goto-char (org-find-invisible))
  18096. (push (buffer-substring s (point)) snippets)
  18097. (setq s (goto-char (org-find-visible))))))
  18098. (kill-new (apply 'concat (nreverse snippets)))))
  18099. (defun org-copy-special ()
  18100. "Copy region in table or copy current subtree.
  18101. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  18102. See the individual commands for more information."
  18103. (interactive)
  18104. (call-interactively
  18105. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  18106. (defun org-cut-special ()
  18107. "Cut region in table or cut current subtree.
  18108. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  18109. See the individual commands for more information."
  18110. (interactive)
  18111. (call-interactively
  18112. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  18113. (defun org-paste-special (arg)
  18114. "Paste rectangular region into table, or past subtree relative to level.
  18115. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  18116. See the individual commands for more information."
  18117. (interactive "P")
  18118. (if (org-at-table-p)
  18119. (org-table-paste-rectangle)
  18120. (org-paste-subtree arg)))
  18121. (defsubst org-in-fixed-width-region-p ()
  18122. "Is point in a fixed-width region?"
  18123. (save-match-data
  18124. (eq 'fixed-width (org-element-type (org-element-at-point)))))
  18125. (defun org-edit-special (&optional arg)
  18126. "Call a special editor for the element at point.
  18127. When at a table, call the formula editor with `org-table-edit-formulas'.
  18128. When in a source code block, call `org-edit-src-code'.
  18129. When in a fixed-width region, call `org-edit-fixed-width-region'.
  18130. When at an #+INCLUDE keyword, visit the included file.
  18131. On a link, call `ffap' to visit the link at point.
  18132. Otherwise, return a user error."
  18133. (interactive)
  18134. (let ((element (org-element-at-point)))
  18135. (assert (not buffer-read-only) nil
  18136. "Buffer is read-only: %s" (buffer-name))
  18137. (case (org-element-type element)
  18138. (src-block
  18139. (if (not arg) (org-edit-src-code)
  18140. (let* ((info (org-babel-get-src-block-info))
  18141. (lang (nth 0 info))
  18142. (params (nth 2 info))
  18143. (session (cdr (assq :session params))))
  18144. (if (not session) (org-edit-src-code)
  18145. ;; At a src-block with a session and function called with
  18146. ;; an ARG: switch to the buffer related to the inferior
  18147. ;; process.
  18148. (funcall (intern (concat "org-babel-prep-session:" lang))
  18149. session params)))))
  18150. (keyword
  18151. (if (member (org-element-property :key element) '("INCLUDE" "SETUPFILE"))
  18152. (find-file
  18153. (org-remove-double-quotes
  18154. (car (org-split-string (org-element-property :value element)))))
  18155. (user-error "No special environment to edit here")))
  18156. (table
  18157. (if (eq (org-element-property :type element) 'table.el)
  18158. (org-edit-src-code)
  18159. (call-interactively 'org-table-edit-formulas)))
  18160. ;; Only Org tables contain `table-row' type elements.
  18161. (table-row (call-interactively 'org-table-edit-formulas))
  18162. ((example-block export-block) (org-edit-src-code))
  18163. (fixed-width (org-edit-fixed-width-region))
  18164. (otherwise
  18165. ;; No notable element at point. Though, we may be at a link,
  18166. ;; which is an object. Thus, scan deeper.
  18167. (if (eq (org-element-type (org-element-context element)) 'link)
  18168. (call-interactively 'ffap)
  18169. (user-error "No special environment to edit here"))))))
  18170. (defvar org-table-coordinate-overlays) ; defined in org-table.el
  18171. (defun org-ctrl-c-ctrl-c (&optional arg)
  18172. "Set tags in headline, or update according to changed information at point.
  18173. This command does many different things, depending on context:
  18174. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  18175. this is what we do.
  18176. - If the cursor is on a statistics cookie, update it.
  18177. - If the cursor is in a headline, prompt for tags and insert them
  18178. into the current line, aligned to `org-tags-column'. When called
  18179. with prefix arg, realign all tags in the current buffer.
  18180. - If the cursor is in one of the special #+KEYWORD lines, this
  18181. triggers scanning the buffer for these lines and updating the
  18182. information.
  18183. - If the cursor is inside a table, realign the table. This command
  18184. works even if the automatic table editor has been turned off.
  18185. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  18186. the entire table.
  18187. - If the cursor is at a footnote reference or definition, jump to
  18188. the corresponding definition or references, respectively.
  18189. - If the cursor is a the beginning of a dynamic block, update it.
  18190. - If the current buffer is a capture buffer, close note and file it.
  18191. - If the cursor is on a <<<target>>>, update radio targets and
  18192. corresponding links in this buffer.
  18193. - If the cursor is on a numbered item in a plain list, renumber the
  18194. ordered list.
  18195. - If the cursor is on a checkbox, toggle it.
  18196. - If the cursor is on a code block, evaluate it. The variable
  18197. `org-confirm-babel-evaluate' can be used to control prompting
  18198. before code block evaluation, by default every code block
  18199. evaluation requires confirmation. Code block evaluation can be
  18200. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  18201. (interactive "P")
  18202. (cond
  18203. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  18204. org-occur-highlights
  18205. org-latex-fragment-image-overlays)
  18206. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  18207. (org-remove-occur-highlights)
  18208. (org-remove-latex-fragment-image-overlays)
  18209. (message "Temporary highlights/overlays removed from current buffer"))
  18210. ((and (local-variable-p 'org-finish-function (current-buffer))
  18211. (fboundp org-finish-function))
  18212. (funcall org-finish-function))
  18213. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  18214. (t
  18215. (let* ((context (org-element-context)) (type (org-element-type context)))
  18216. ;; Test if point is within a blank line.
  18217. (if (save-excursion (beginning-of-line) (looking-at "[ \t]*$"))
  18218. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  18219. (user-error "C-c C-c can do nothing useful at this location"))
  18220. ;; For convenience: at the first line of a paragraph on the
  18221. ;; same line as an item, apply function on that item instead.
  18222. (when (eq type 'paragraph)
  18223. (let ((parent (org-element-property :parent context)))
  18224. (when (and (eq (org-element-type parent) 'item)
  18225. (= (point-at-bol) (org-element-property :begin parent)))
  18226. (setq context parent type 'item))))
  18227. ;; Act according to type of element or object at point.
  18228. (case type
  18229. (clock (org-clock-update-time-maybe))
  18230. (dynamic-block
  18231. (save-excursion
  18232. (goto-char (org-element-property :post-affiliated context))
  18233. (org-update-dblock)))
  18234. (footnote-definition
  18235. (goto-char (org-element-property :post-affiliated context))
  18236. (call-interactively 'org-footnote-action))
  18237. (footnote-reference (call-interactively 'org-footnote-action))
  18238. ((headline inlinetask)
  18239. (save-excursion (goto-char (org-element-property :begin context))
  18240. (call-interactively 'org-set-tags)))
  18241. (item
  18242. ;; At an item: a double C-u set checkbox to "[-]"
  18243. ;; unconditionally, whereas a single one will toggle its
  18244. ;; presence. Without an universal argument, if the item
  18245. ;; has a checkbox, toggle it. Otherwise repair the list.
  18246. (let* ((box (org-element-property :checkbox context))
  18247. (struct (org-element-property :structure context))
  18248. (old-struct (copy-tree struct))
  18249. (parents (org-list-parents-alist struct))
  18250. (prevs (org-list-prevs-alist struct))
  18251. (orderedp (org-not-nil (org-entry-get nil "ORDERED"))))
  18252. (org-list-set-checkbox
  18253. (org-element-property :begin context) struct
  18254. (cond ((equal arg '(16)) "[-]")
  18255. ((and (not box) (equal arg '(4))) "[ ]")
  18256. ((or (not box) (equal arg '(4))) nil)
  18257. ((eq box 'on) "[ ]")
  18258. (t "[X]")))
  18259. ;; Mimic `org-list-write-struct' but with grabbing
  18260. ;; a return value from `org-list-struct-fix-box'.
  18261. (org-list-struct-fix-ind struct parents 2)
  18262. (org-list-struct-fix-item-end struct)
  18263. (org-list-struct-fix-bul struct prevs)
  18264. (org-list-struct-fix-ind struct parents)
  18265. (let ((block-item
  18266. (org-list-struct-fix-box struct parents prevs orderedp)))
  18267. (if (and box (equal struct old-struct))
  18268. (if (equal arg '(16))
  18269. (message "Checkboxes already reset")
  18270. (user-error "Cannot toggle this checkbox: %s"
  18271. (if (eq box 'on)
  18272. "all subitems checked"
  18273. "unchecked subitems")))
  18274. (org-list-struct-apply-struct struct old-struct)
  18275. (org-update-checkbox-count-maybe))
  18276. (when block-item
  18277. (message "Checkboxes were removed due to empty box at line %d"
  18278. (org-current-line block-item))))))
  18279. (keyword
  18280. (let ((org-inhibit-startup-visibility-stuff t)
  18281. (org-startup-align-all-tables nil))
  18282. (when (boundp 'org-table-coordinate-overlays)
  18283. (mapc 'delete-overlay org-table-coordinate-overlays)
  18284. (setq org-table-coordinate-overlays nil))
  18285. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  18286. (message "Local setup has been refreshed"))
  18287. (plain-list
  18288. ;; At a plain list, with a double C-u argument, set
  18289. ;; checkboxes of each item to "[-]", whereas a single one
  18290. ;; will toggle their presence according to the state of the
  18291. ;; first item in the list. Without an argument, repair the
  18292. ;; list.
  18293. (let* ((begin (org-element-property :contents-begin context))
  18294. (struct (org-element-property :structure context))
  18295. (old-struct (copy-tree struct))
  18296. (first-box (save-excursion
  18297. (goto-char begin)
  18298. (looking-at org-list-full-item-re)
  18299. (match-string-no-properties 3)))
  18300. (new-box (cond ((equal arg '(16)) "[-]")
  18301. ((equal arg '(4)) (unless first-box "[ ]"))
  18302. ((equal first-box "[X]") "[ ]")
  18303. (t "[X]"))))
  18304. (cond
  18305. (arg
  18306. (mapc (lambda (pos) (org-list-set-checkbox pos struct new-box))
  18307. (org-list-get-all-items
  18308. begin struct (org-list-prevs-alist struct))))
  18309. ((and first-box (eq (point) begin))
  18310. ;; For convenience, when point is at bol on the first
  18311. ;; item of the list and no argument is provided, simply
  18312. ;; toggle checkbox of that item, if any.
  18313. (org-list-set-checkbox begin struct new-box)))
  18314. (org-list-write-struct
  18315. struct (org-list-parents-alist struct) old-struct)
  18316. (org-update-checkbox-count-maybe)
  18317. (save-excursion (goto-char begin) (org-list-send-list 'maybe))))
  18318. ((property-drawer node-property)
  18319. (call-interactively 'org-property-action))
  18320. ((radio-target target)
  18321. (call-interactively 'org-update-radio-target-regexp))
  18322. (statistics-cookie
  18323. (call-interactively 'org-update-statistics-cookies))
  18324. ((table table-cell table-row)
  18325. ;; At a table, recalculate every field and align it. Also
  18326. ;; send the table if necessary. If the table has
  18327. ;; a `table.el' type, just give up. At a table row or
  18328. ;; cell, maybe recalculate line but always align table.
  18329. (if (eq (org-element-property :type context) 'table.el)
  18330. (message "Use C-c ' to edit table.el tables")
  18331. (let ((org-enable-table-editor t))
  18332. (if (or (eq type 'table)
  18333. ;; Check if point is at a TBLFM line.
  18334. (and (eq type 'table-row)
  18335. (= (point) (org-element-property :end context))))
  18336. (save-excursion
  18337. (goto-char (org-element-property :contents-begin context))
  18338. (org-call-with-arg 'org-table-recalculate (or arg t))
  18339. (orgtbl-send-table 'maybe))
  18340. (org-table-maybe-eval-formula)
  18341. (cond (arg (call-interactively 'org-table-recalculate))
  18342. ((org-table-maybe-recalculate-line))
  18343. (t (org-table-align)))))))
  18344. (timestamp (org-timestamp-change 0 'day))
  18345. (otherwise
  18346. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  18347. (user-error
  18348. "C-c C-c can do nothing useful at this location")))))))))
  18349. (defun org-mode-restart ()
  18350. "Restart Org-mode, to scan again for special lines.
  18351. Also updates the keyword regular expressions."
  18352. (interactive)
  18353. (org-mode)
  18354. (message "Org-mode restarted"))
  18355. (defun org-kill-note-or-show-branches ()
  18356. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  18357. (interactive)
  18358. (if (not org-finish-function)
  18359. (progn
  18360. (hide-subtree)
  18361. (call-interactively 'show-branches))
  18362. (let ((org-note-abort t))
  18363. (funcall org-finish-function))))
  18364. (defun org-return (&optional indent)
  18365. "Goto next table row or insert a newline.
  18366. Calls `org-table-next-row' or `newline', depending on context.
  18367. See the individual commands for more information."
  18368. (interactive)
  18369. (let (org-ts-what)
  18370. (cond
  18371. ((or (bobp) (org-in-src-block-p))
  18372. (if indent (newline-and-indent) (newline)))
  18373. ((org-at-table-p)
  18374. (org-table-justify-field-maybe)
  18375. (call-interactively 'org-table-next-row))
  18376. ;; when `newline-and-indent' is called within a list, make sure
  18377. ;; text moved stays inside the item.
  18378. ((and (org-in-item-p) indent)
  18379. (if (and (org-at-item-p) (>= (point) (match-end 0)))
  18380. (progn
  18381. (save-match-data (newline))
  18382. (org-indent-line-to (length (match-string 0))))
  18383. (let ((ind (org-get-indentation)))
  18384. (newline)
  18385. (if (org-looking-back org-list-end-re)
  18386. (org-indent-line)
  18387. (org-indent-line-to ind)))))
  18388. ((and org-return-follows-link
  18389. (org-at-timestamp-p t)
  18390. (not (eq org-ts-what 'after)))
  18391. (org-follow-timestamp-link))
  18392. ((and org-return-follows-link
  18393. (let ((tprop (get-text-property (point) 'face)))
  18394. (or (eq tprop 'org-link)
  18395. (and (listp tprop) (memq 'org-link tprop)))))
  18396. (call-interactively 'org-open-at-point))
  18397. ((and (org-at-heading-p)
  18398. (looking-at
  18399. (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
  18400. (org-show-entry)
  18401. (end-of-line 1)
  18402. (newline))
  18403. (t (if indent (newline-and-indent) (newline))))))
  18404. (defun org-return-indent ()
  18405. "Goto next table row or insert a newline and indent.
  18406. Calls `org-table-next-row' or `newline-and-indent', depending on
  18407. context. See the individual commands for more information."
  18408. (interactive)
  18409. (org-return t))
  18410. (defun org-ctrl-c-star ()
  18411. "Compute table, or change heading status of lines.
  18412. Calls `org-table-recalculate' or `org-toggle-heading',
  18413. depending on context."
  18414. (interactive)
  18415. (cond
  18416. ((org-at-table-p)
  18417. (call-interactively 'org-table-recalculate))
  18418. (t
  18419. ;; Convert all lines in region to list items
  18420. (call-interactively 'org-toggle-heading))))
  18421. (defun org-ctrl-c-minus ()
  18422. "Insert separator line in table or modify bullet status of line.
  18423. Also turns a plain line or a region of lines into list items.
  18424. Calls `org-table-insert-hline', `org-toggle-item', or
  18425. `org-cycle-list-bullet', depending on context."
  18426. (interactive)
  18427. (cond
  18428. ((org-at-table-p)
  18429. (call-interactively 'org-table-insert-hline))
  18430. ((org-region-active-p)
  18431. (call-interactively 'org-toggle-item))
  18432. ((org-in-item-p)
  18433. (call-interactively 'org-cycle-list-bullet))
  18434. (t
  18435. (call-interactively 'org-toggle-item))))
  18436. (defun org-toggle-item (arg)
  18437. "Convert headings or normal lines to items, items to normal lines.
  18438. If there is no active region, only the current line is considered.
  18439. If the first non blank line in the region is a headline, convert
  18440. all headlines to items, shifting text accordingly.
  18441. If it is an item, convert all items to normal lines.
  18442. If it is normal text, change region into a list of items.
  18443. With a prefix argument ARG, change the region in a single item."
  18444. (interactive "P")
  18445. (let ((shift-text
  18446. (function
  18447. ;; Shift text in current section to IND, from point to END.
  18448. ;; The function leaves point to END line.
  18449. (lambda (ind end)
  18450. (let ((min-i 1000) (end (copy-marker end)))
  18451. ;; First determine the minimum indentation (MIN-I) of
  18452. ;; the text.
  18453. (save-excursion
  18454. (catch 'exit
  18455. (while (< (point) end)
  18456. (let ((i (org-get-indentation)))
  18457. (cond
  18458. ;; Skip blank lines and inline tasks.
  18459. ((looking-at "^[ \t]*$"))
  18460. ((looking-at org-outline-regexp-bol))
  18461. ;; We can't find less than 0 indentation.
  18462. ((zerop i) (throw 'exit (setq min-i 0)))
  18463. ((< i min-i) (setq min-i i))))
  18464. (forward-line))))
  18465. ;; Then indent each line so that a line indented to
  18466. ;; MIN-I becomes indented to IND. Ignore blank lines
  18467. ;; and inline tasks in the process.
  18468. (let ((delta (- ind min-i)))
  18469. (while (< (point) end)
  18470. (unless (or (looking-at "^[ \t]*$")
  18471. (looking-at org-outline-regexp-bol))
  18472. (org-indent-line-to (+ (org-get-indentation) delta)))
  18473. (forward-line)))))))
  18474. (skip-blanks
  18475. (function
  18476. ;; Return beginning of first non-blank line, starting from
  18477. ;; line at POS.
  18478. (lambda (pos)
  18479. (save-excursion
  18480. (goto-char pos)
  18481. (skip-chars-forward " \r\t\n")
  18482. (point-at-bol)))))
  18483. beg end)
  18484. ;; Determine boundaries of changes.
  18485. (if (org-region-active-p)
  18486. (setq beg (funcall skip-blanks (region-beginning))
  18487. end (copy-marker (region-end)))
  18488. (setq beg (funcall skip-blanks (point-at-bol))
  18489. end (copy-marker (point-at-eol))))
  18490. ;; Depending on the starting line, choose an action on the text
  18491. ;; between BEG and END.
  18492. (org-with-limited-levels
  18493. (save-excursion
  18494. (goto-char beg)
  18495. (cond
  18496. ;; Case 1. Start at an item: de-itemize. Note that it only
  18497. ;; happens when a region is active: `org-ctrl-c-minus'
  18498. ;; would call `org-cycle-list-bullet' otherwise.
  18499. ((org-at-item-p)
  18500. (while (< (point) end)
  18501. (when (org-at-item-p)
  18502. (skip-chars-forward " \t")
  18503. (delete-region (point) (match-end 0)))
  18504. (forward-line)))
  18505. ;; Case 2. Start at an heading: convert to items.
  18506. ((org-at-heading-p)
  18507. (let* ((bul (org-list-bullet-string "-"))
  18508. (bul-len (length bul))
  18509. ;; Indentation of the first heading. It should be
  18510. ;; relative to the indentation of its parent, if any.
  18511. (start-ind (save-excursion
  18512. (cond
  18513. ((not org-adapt-indentation) 0)
  18514. ((not (outline-previous-heading)) 0)
  18515. (t (length (match-string 0))))))
  18516. ;; Level of first heading. Further headings will be
  18517. ;; compared to it to determine hierarchy in the list.
  18518. (ref-level (org-reduced-level (org-outline-level))))
  18519. (while (< (point) end)
  18520. (let* ((level (org-reduced-level (org-outline-level)))
  18521. (delta (max 0 (- level ref-level))))
  18522. ;; If current headline is less indented than the first
  18523. ;; one, set it as reference, in order to preserve
  18524. ;; subtrees.
  18525. (when (< level ref-level) (setq ref-level level))
  18526. (replace-match bul t t)
  18527. (org-indent-line-to (+ start-ind (* delta bul-len)))
  18528. ;; Ensure all text down to END (or SECTION-END) belongs
  18529. ;; to the newly created item.
  18530. (let ((section-end (save-excursion
  18531. (or (outline-next-heading) (point)))))
  18532. (forward-line)
  18533. (funcall shift-text
  18534. (+ start-ind (* (1+ delta) bul-len))
  18535. (min end section-end)))))))
  18536. ;; Case 3. Normal line with ARG: make the first line of region
  18537. ;; an item, and shift indentation of others lines to
  18538. ;; set them as item's body.
  18539. (arg (let* ((bul (org-list-bullet-string "-"))
  18540. (bul-len (length bul))
  18541. (ref-ind (org-get-indentation)))
  18542. (skip-chars-forward " \t")
  18543. (insert bul)
  18544. (forward-line)
  18545. (while (< (point) end)
  18546. ;; Ensure that lines less indented than first one
  18547. ;; still get included in item body.
  18548. (funcall shift-text
  18549. (+ ref-ind bul-len)
  18550. (min end (save-excursion (or (outline-next-heading)
  18551. (point)))))
  18552. (forward-line))))
  18553. ;; Case 4. Normal line without ARG: turn each non-item line
  18554. ;; into an item.
  18555. (t
  18556. (while (< (point) end)
  18557. (unless (or (org-at-heading-p) (org-at-item-p))
  18558. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  18559. (replace-match
  18560. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  18561. (forward-line))))))))
  18562. (defun org-toggle-heading (&optional nstars)
  18563. "Convert headings to normal text, or items or text to headings.
  18564. If there is no active region, only convert the current line.
  18565. With a \\[universal-argument] prefix, convert the whole list at
  18566. point into heading.
  18567. In a region:
  18568. - If the first non blank line is a headline, remove the stars
  18569. from all headlines in the region.
  18570. - If it is a normal line, turn each and every normal line (i.e.,
  18571. not an heading or an item) in the region into headings. If you
  18572. want to convert only the first line of this region, use one
  18573. universal prefix argument.
  18574. - If it is a plain list item, turn all plain list items into headings.
  18575. When converting a line into a heading, the number of stars is chosen
  18576. such that the lines become children of the current entry. However,
  18577. when a numeric prefix argument is given, its value determines the
  18578. number of stars to add."
  18579. (interactive "P")
  18580. (let ((skip-blanks
  18581. (function
  18582. ;; Return beginning of first non-blank line, starting from
  18583. ;; line at POS.
  18584. (lambda (pos)
  18585. (save-excursion
  18586. (goto-char pos)
  18587. (while (org-at-comment-p) (forward-line))
  18588. (skip-chars-forward " \r\t\n")
  18589. (point-at-bol)))))
  18590. beg end toggled)
  18591. ;; Determine boundaries of changes. If a universal prefix has
  18592. ;; been given, put the list in a region. If region ends at a bol,
  18593. ;; do not consider the last line to be in the region.
  18594. (when (and current-prefix-arg (org-at-item-p))
  18595. (if (listp current-prefix-arg) (setq current-prefix-arg 1))
  18596. (org-mark-element))
  18597. (if (org-region-active-p)
  18598. (setq beg (funcall skip-blanks (region-beginning))
  18599. end (copy-marker (save-excursion
  18600. (goto-char (region-end))
  18601. (if (bolp) (point) (point-at-eol)))))
  18602. (setq beg (funcall skip-blanks (point-at-bol))
  18603. end (copy-marker (point-at-eol))))
  18604. ;; Ensure inline tasks don't count as headings.
  18605. (org-with-limited-levels
  18606. (save-excursion
  18607. (goto-char beg)
  18608. (cond
  18609. ;; Case 1. Started at an heading: de-star headings.
  18610. ((org-at-heading-p)
  18611. (while (< (point) end)
  18612. (when (org-at-heading-p t)
  18613. (looking-at org-outline-regexp) (replace-match "")
  18614. (setq toggled t))
  18615. (forward-line)))
  18616. ;; Case 2. Started at an item: change items into headlines.
  18617. ;; One star will be added by `org-list-to-subtree'.
  18618. ((org-at-item-p)
  18619. (let* ((stars (make-string
  18620. ;; subtract the star that will be added again by
  18621. ;; `org-list-to-subtree'
  18622. (if (numberp nstars) (1- nstars)
  18623. (or (org-current-level) 0))
  18624. ?*))
  18625. (add-stars
  18626. (cond (nstars "") ; stars from prefix only
  18627. ((equal stars "") "") ; before first heading
  18628. (org-odd-levels-only "*") ; inside heading, odd
  18629. (t "")))) ; inside heading, oddeven
  18630. (while (< (point) end)
  18631. (when (org-at-item-p)
  18632. ;; Pay attention to cases when region ends before list.
  18633. (let* ((struct (org-list-struct))
  18634. (list-end (min (org-list-get-bottom-point struct) (1+ end))))
  18635. (save-restriction
  18636. (narrow-to-region (point) list-end)
  18637. (insert
  18638. (org-list-to-subtree
  18639. (org-list-parse-list t)
  18640. '(:istart (concat stars add-stars (funcall get-stars depth))
  18641. :icount (concat stars add-stars (funcall get-stars depth)))))))
  18642. (setq toggled t))
  18643. (forward-line))))
  18644. ;; Case 3. Started at normal text: make every line an heading,
  18645. ;; skipping headlines and items.
  18646. (t (let* ((stars
  18647. (make-string
  18648. (if (numberp nstars) nstars (or (org-current-level) 0)) ?*))
  18649. (add-stars
  18650. (cond (nstars "") ; stars from prefix only
  18651. ((equal stars "") "*") ; before first heading
  18652. (org-odd-levels-only "**") ; inside heading, odd
  18653. (t "*"))) ; inside heading, oddeven
  18654. (rpl (concat stars add-stars " "))
  18655. (lend (if (listp nstars) (save-excursion (end-of-line) (point)))))
  18656. (while (< (point) (if (equal nstars '(4)) lend end))
  18657. (when (and (not (or (org-at-heading-p) (org-at-item-p) (org-at-comment-p)))
  18658. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  18659. (replace-match (concat rpl (match-string 2))) (setq toggled t))
  18660. (forward-line)))))))
  18661. (unless toggled (message "Cannot toggle heading from here"))))
  18662. (defun org-meta-return (&optional arg)
  18663. "Insert a new heading or wrap a region in a table.
  18664. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  18665. See the individual commands for more information."
  18666. (interactive "P")
  18667. (cond
  18668. ((run-hook-with-args-until-success 'org-metareturn-hook))
  18669. ((or (org-at-drawer-p) (org-at-property-p))
  18670. (newline-and-indent))
  18671. ((org-at-table-p)
  18672. (call-interactively 'org-table-wrap-region))
  18673. (t (call-interactively 'org-insert-heading))))
  18674. ;;; Menu entries
  18675. (defsubst org-in-subtree-not-table-p ()
  18676. "Are we in a subtree and not in a table?"
  18677. (and (not (org-before-first-heading-p))
  18678. (not (org-at-table-p))))
  18679. ;; Define the Org-mode menus
  18680. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  18681. '("Tbl"
  18682. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  18683. ["Next Field" org-cycle (org-at-table-p)]
  18684. ["Previous Field" org-shifttab (org-at-table-p)]
  18685. ["Next Row" org-return (org-at-table-p)]
  18686. "--"
  18687. ["Blank Field" org-table-blank-field (org-at-table-p)]
  18688. ["Edit Field" org-table-edit-field (org-at-table-p)]
  18689. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  18690. "--"
  18691. ("Column"
  18692. ["Move Column Left" org-metaleft (org-at-table-p)]
  18693. ["Move Column Right" org-metaright (org-at-table-p)]
  18694. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  18695. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  18696. ("Row"
  18697. ["Move Row Up" org-metaup (org-at-table-p)]
  18698. ["Move Row Down" org-metadown (org-at-table-p)]
  18699. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  18700. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  18701. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  18702. "--"
  18703. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  18704. ("Rectangle"
  18705. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  18706. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  18707. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  18708. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  18709. "--"
  18710. ("Calculate"
  18711. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  18712. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  18713. ["Edit Formulas" org-edit-special (org-at-table-p)]
  18714. "--"
  18715. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  18716. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  18717. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  18718. "--"
  18719. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  18720. "--"
  18721. ["Sum Column/Rectangle" org-table-sum
  18722. (or (org-at-table-p) (org-region-active-p))]
  18723. ["Which Column?" org-table-current-column (org-at-table-p)])
  18724. ["Debug Formulas"
  18725. org-table-toggle-formula-debugger
  18726. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  18727. ["Show Col/Row Numbers"
  18728. org-table-toggle-coordinate-overlays
  18729. :style toggle
  18730. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  18731. "--"
  18732. ["Create" org-table-create (and (not (org-at-table-p))
  18733. org-enable-table-editor)]
  18734. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  18735. ["Import from File" org-table-import (not (org-at-table-p))]
  18736. ["Export to File" org-table-export (org-at-table-p)]
  18737. "--"
  18738. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  18739. (easy-menu-define org-org-menu org-mode-map "Org menu"
  18740. '("Org"
  18741. ("Show/Hide"
  18742. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  18743. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  18744. ["Sparse Tree..." org-sparse-tree t]
  18745. ["Reveal Context" org-reveal t]
  18746. ["Show All" show-all t]
  18747. "--"
  18748. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  18749. "--"
  18750. ["New Heading" org-insert-heading t]
  18751. ("Navigate Headings"
  18752. ["Up" outline-up-heading t]
  18753. ["Next" outline-next-visible-heading t]
  18754. ["Previous" outline-previous-visible-heading t]
  18755. ["Next Same Level" outline-forward-same-level t]
  18756. ["Previous Same Level" outline-backward-same-level t]
  18757. "--"
  18758. ["Jump" org-goto t])
  18759. ("Edit Structure"
  18760. ["Refile Subtree" org-refile (org-in-subtree-not-table-p)]
  18761. "--"
  18762. ["Move Subtree Up" org-shiftmetaup (org-in-subtree-not-table-p)]
  18763. ["Move Subtree Down" org-shiftmetadown (org-in-subtree-not-table-p)]
  18764. "--"
  18765. ["Copy Subtree" org-copy-special (org-in-subtree-not-table-p)]
  18766. ["Cut Subtree" org-cut-special (org-in-subtree-not-table-p)]
  18767. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  18768. "--"
  18769. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  18770. "--"
  18771. ["Copy visible text" org-copy-visible t]
  18772. "--"
  18773. ["Promote Heading" org-metaleft (org-in-subtree-not-table-p)]
  18774. ["Promote Subtree" org-shiftmetaleft (org-in-subtree-not-table-p)]
  18775. ["Demote Heading" org-metaright (org-in-subtree-not-table-p)]
  18776. ["Demote Subtree" org-shiftmetaright (org-in-subtree-not-table-p)]
  18777. "--"
  18778. ["Sort Region/Children" org-sort t]
  18779. "--"
  18780. ["Convert to odd levels" org-convert-to-odd-levels t]
  18781. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  18782. ("Editing"
  18783. ["Emphasis..." org-emphasize t]
  18784. ["Edit Source Example" org-edit-special t]
  18785. "--"
  18786. ["Footnote new/jump" org-footnote-action t]
  18787. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  18788. ("Archive"
  18789. ["Archive (default method)" org-archive-subtree-default (org-in-subtree-not-table-p)]
  18790. "--"
  18791. ["Move Subtree to Archive file" org-advertized-archive-subtree (org-in-subtree-not-table-p)]
  18792. ["Toggle ARCHIVE tag" org-toggle-archive-tag (org-in-subtree-not-table-p)]
  18793. ["Move subtree to Archive sibling" org-archive-to-archive-sibling (org-in-subtree-not-table-p)]
  18794. )
  18795. "--"
  18796. ("Hyperlinks"
  18797. ["Store Link (Global)" org-store-link t]
  18798. ["Find existing link to here" org-occur-link-in-agenda-files t]
  18799. ["Insert Link" org-insert-link t]
  18800. ["Follow Link" org-open-at-point t]
  18801. "--"
  18802. ["Next link" org-next-link t]
  18803. ["Previous link" org-previous-link t]
  18804. "--"
  18805. ["Descriptive Links"
  18806. org-toggle-link-display
  18807. :style radio
  18808. :selected org-descriptive-links
  18809. ]
  18810. ["Literal Links"
  18811. org-toggle-link-display
  18812. :style radio
  18813. :selected (not org-descriptive-links)])
  18814. "--"
  18815. ("TODO Lists"
  18816. ["TODO/DONE/-" org-todo t]
  18817. ("Select keyword"
  18818. ["Next keyword" org-shiftright (org-at-heading-p)]
  18819. ["Previous keyword" org-shiftleft (org-at-heading-p)]
  18820. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  18821. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
  18822. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
  18823. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  18824. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  18825. "--"
  18826. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  18827. :selected org-enforce-todo-dependencies :style toggle :active t]
  18828. "Settings for tree at point"
  18829. ["Do Children sequentially" org-toggle-ordered-property :style radio
  18830. :selected (org-entry-get nil "ORDERED")
  18831. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  18832. ["Do Children parallel" org-toggle-ordered-property :style radio
  18833. :selected (not (org-entry-get nil "ORDERED"))
  18834. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  18835. "--"
  18836. ["Set Priority" org-priority t]
  18837. ["Priority Up" org-shiftup t]
  18838. ["Priority Down" org-shiftdown t]
  18839. "--"
  18840. ["Get news from all feeds" org-feed-update-all t]
  18841. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  18842. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  18843. ("TAGS and Properties"
  18844. ["Set Tags" org-set-tags-command (not (org-before-first-heading-p))]
  18845. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  18846. "--"
  18847. ["Set property" org-set-property (not (org-before-first-heading-p))]
  18848. ["Column view of properties" org-columns t]
  18849. ["Insert Column View DBlock" org-insert-columns-dblock t])
  18850. ("Dates and Scheduling"
  18851. ["Timestamp" org-time-stamp (not (org-before-first-heading-p))]
  18852. ["Timestamp (inactive)" org-time-stamp-inactive (not (org-before-first-heading-p))]
  18853. ("Change Date"
  18854. ["1 Day Later" org-shiftright (org-at-timestamp-p)]
  18855. ["1 Day Earlier" org-shiftleft (org-at-timestamp-p)]
  18856. ["1 ... Later" org-shiftup (org-at-timestamp-p)]
  18857. ["1 ... Earlier" org-shiftdown (org-at-timestamp-p)])
  18858. ["Compute Time Range" org-evaluate-time-range t]
  18859. ["Schedule Item" org-schedule (not (org-before-first-heading-p))]
  18860. ["Deadline" org-deadline (not (org-before-first-heading-p))]
  18861. "--"
  18862. ["Custom time format" org-toggle-time-stamp-overlays
  18863. :style radio :selected org-display-custom-times]
  18864. "--"
  18865. ["Goto Calendar" org-goto-calendar t]
  18866. ["Date from Calendar" org-date-from-calendar t]
  18867. "--"
  18868. ["Start/Restart Timer" org-timer-start t]
  18869. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  18870. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  18871. ["Insert Timer String" org-timer t]
  18872. ["Insert Timer Item" org-timer-item t])
  18873. ("Logging work"
  18874. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  18875. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  18876. ["Clock out" org-clock-out t]
  18877. ["Clock cancel" org-clock-cancel t]
  18878. "--"
  18879. ["Mark as default task" org-clock-mark-default-task t]
  18880. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  18881. ["Goto running clock" org-clock-goto t]
  18882. "--"
  18883. ["Display times" org-clock-display t]
  18884. ["Create clock table" org-clock-report t]
  18885. "--"
  18886. ["Record DONE time"
  18887. (progn (setq org-log-done (not org-log-done))
  18888. (message "Switching to %s will %s record a timestamp"
  18889. (car org-done-keywords)
  18890. (if org-log-done "automatically" "not")))
  18891. :style toggle :selected org-log-done])
  18892. "--"
  18893. ["Agenda Command..." org-agenda t]
  18894. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  18895. ("File List for Agenda")
  18896. ("Special views current file"
  18897. ["TODO Tree" org-show-todo-tree t]
  18898. ["Check Deadlines" org-check-deadlines t]
  18899. ["Timeline" org-timeline t]
  18900. ["Tags/Property tree" org-match-sparse-tree t])
  18901. "--"
  18902. ["Export/Publish..." org-export-dispatch t]
  18903. ("LaTeX"
  18904. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  18905. :selected org-cdlatex-mode]
  18906. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  18907. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  18908. ["Modify math symbol" org-cdlatex-math-modify
  18909. (org-inside-LaTeX-fragment-p)]
  18910. ["Insert citation" org-reftex-citation t]
  18911. "--"
  18912. ["Template for BEAMER" (org-beamer-insert-options-template) t])
  18913. "--"
  18914. ("MobileOrg"
  18915. ["Push Files and Views" org-mobile-push t]
  18916. ["Get Captured and Flagged" org-mobile-pull t]
  18917. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  18918. "--"
  18919. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  18920. "--"
  18921. ("Documentation"
  18922. ["Show Version" org-version t]
  18923. ["Info Documentation" org-info t])
  18924. ("Customize"
  18925. ["Browse Org Group" org-customize t]
  18926. "--"
  18927. ["Expand This Menu" org-create-customize-menu
  18928. (fboundp 'customize-menu-create)])
  18929. ["Send bug report" org-submit-bug-report t]
  18930. "--"
  18931. ("Refresh/Reload"
  18932. ["Refresh setup current buffer" org-mode-restart t]
  18933. ["Reload Org (after update)" org-reload t]
  18934. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x !"])
  18935. ))
  18936. (defun org-info (&optional node)
  18937. "Read documentation for Org-mode in the info system.
  18938. With optional NODE, go directly to that node."
  18939. (interactive)
  18940. (info (format "(org)%s" (or node ""))))
  18941. ;;;###autoload
  18942. (defun org-submit-bug-report ()
  18943. "Submit a bug report on Org-mode via mail.
  18944. Don't hesitate to report any problems or inaccurate documentation.
  18945. If you don't have setup sending mail from (X)Emacs, please copy the
  18946. output buffer into your mail program, as it gives us important
  18947. information about your Org-mode version and configuration."
  18948. (interactive)
  18949. (require 'reporter)
  18950. (org-load-modules-maybe)
  18951. (org-require-autoloaded-modules)
  18952. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  18953. (reporter-submit-bug-report
  18954. "emacs-orgmode@gnu.org"
  18955. (org-version nil 'full)
  18956. (let (list)
  18957. (save-window-excursion
  18958. (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
  18959. (delete-other-windows)
  18960. (erase-buffer)
  18961. (insert "You are about to submit a bug report to the Org-mode mailing list.
  18962. We would like to add your full Org-mode and Outline configuration to the
  18963. bug report. This greatly simplifies the work of the maintainer and
  18964. other experts on the mailing list.
  18965. HOWEVER, some variables you have customized may contain private
  18966. information. The names of customers, colleagues, or friends, might
  18967. appear in the form of file names, tags, todo states, or search strings.
  18968. If you answer yes to the prompt, you might want to check and remove
  18969. such private information before sending the email.")
  18970. (add-text-properties (point-min) (point-max) '(face org-warning))
  18971. (when (yes-or-no-p "Include your Org-mode configuration ")
  18972. (mapatoms
  18973. (lambda (v)
  18974. (and (boundp v)
  18975. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  18976. (or (and (symbol-value v)
  18977. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  18978. (and
  18979. (get v 'custom-type) (get v 'standard-value)
  18980. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  18981. (push v list)))))
  18982. (kill-buffer (get-buffer "*Warn about privacy*"))
  18983. list))
  18984. nil nil
  18985. "Remember to cover the basics, that is, what you expected to happen and
  18986. what in fact did happen. You don't know how to make a good report? See
  18987. http://orgmode.org/manual/Feedback.html#Feedback
  18988. Your bug report will be posted to the Org-mode mailing list.
  18989. ------------------------------------------------------------------------")
  18990. (save-excursion
  18991. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  18992. (replace-match "\\1Bug: \\3 [\\2]")))))
  18993. (defun org-install-agenda-files-menu ()
  18994. (let ((bl (buffer-list)))
  18995. (save-excursion
  18996. (while bl
  18997. (set-buffer (pop bl))
  18998. (if (derived-mode-p 'org-mode) (setq bl nil)))
  18999. (when (derived-mode-p 'org-mode)
  19000. (easy-menu-change
  19001. '("Org") "File List for Agenda"
  19002. (append
  19003. (list
  19004. ["Edit File List" (org-edit-agenda-file-list) t]
  19005. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  19006. ["Remove Current File from List" org-remove-file t]
  19007. ["Cycle through agenda files" org-cycle-agenda-files t]
  19008. ["Occur in all agenda files" org-occur-in-agenda-files t]
  19009. "--")
  19010. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  19011. ;;;; Documentation
  19012. (defun org-require-autoloaded-modules ()
  19013. (interactive)
  19014. (mapc 'require
  19015. '(org-agenda org-archive org-attach org-clock org-colview org-id
  19016. org-remember org-table org-timer)))
  19017. ;;;###autoload
  19018. (defun org-reload (&optional uncompiled)
  19019. "Reload all org lisp files.
  19020. With prefix arg UNCOMPILED, load the uncompiled versions."
  19021. (interactive "P")
  19022. (require 'loadhist)
  19023. (let* ((org-dir (org-find-library-dir "org"))
  19024. (contrib-dir (or (org-find-library-dir "org-contribdir") org-dir))
  19025. (feature-re "^\\(org\\|ob\\|ox\\)\\(-.*\\)?")
  19026. (remove-re (mapconcat 'identity
  19027. (mapcar (lambda (f) (concat "^" f "$"))
  19028. (list (if (featurep 'xemacs)
  19029. "org-colview"
  19030. "org-colview-xemacs")
  19031. "org" "org-loaddefs" "org-version"))
  19032. "\\|"))
  19033. (feats (delete-dups
  19034. (mapcar 'file-name-sans-extension
  19035. (mapcar 'file-name-nondirectory
  19036. (delq nil
  19037. (mapcar 'feature-file
  19038. features))))))
  19039. (lfeat (append
  19040. (sort
  19041. (setq feats
  19042. (delq nil (mapcar
  19043. (lambda (f)
  19044. (if (and (string-match feature-re f)
  19045. (not (string-match remove-re f)))
  19046. f nil))
  19047. feats)))
  19048. 'string-lessp)
  19049. (list "org-version" "org")))
  19050. (load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  19051. (load-suffixes (if uncompiled (reverse load-suffixes) load-suffixes))
  19052. load-uncore load-misses)
  19053. (setq load-misses
  19054. (delq 't
  19055. (mapcar (lambda (f)
  19056. (or (org-load-noerror-mustsuffix (concat org-dir f))
  19057. (and (string= org-dir contrib-dir)
  19058. (org-load-noerror-mustsuffix (concat contrib-dir f)))
  19059. (and (org-load-noerror-mustsuffix (concat (org-find-library-dir f) f))
  19060. (add-to-list 'load-uncore f 'append)
  19061. 't)
  19062. f))
  19063. lfeat)))
  19064. (if load-uncore
  19065. (message "The following feature%s found in load-path, please check if that's correct:\n%s"
  19066. (if (> (length load-uncore) 1) "s were" " was") load-uncore))
  19067. (if load-misses
  19068. (message "Some error occured while reloading Org feature%s\n%s\nPlease check *Messages*!\n%s"
  19069. (if (> (length load-misses) 1) "s" "") load-misses (org-version nil 'full))
  19070. (message "Successfully reloaded Org\n%s" (org-version nil 'full)))))
  19071. ;;;###autoload
  19072. (defun org-customize ()
  19073. "Call the customize function with org as argument."
  19074. (interactive)
  19075. (org-load-modules-maybe)
  19076. (org-require-autoloaded-modules)
  19077. (customize-browse 'org))
  19078. (defun org-create-customize-menu ()
  19079. "Create a full customization menu for Org-mode, insert it into the menu."
  19080. (interactive)
  19081. (org-load-modules-maybe)
  19082. (org-require-autoloaded-modules)
  19083. (if (fboundp 'customize-menu-create)
  19084. (progn
  19085. (easy-menu-change
  19086. '("Org") "Customize"
  19087. `(["Browse Org group" org-customize t]
  19088. "--"
  19089. ,(customize-menu-create 'org)
  19090. ["Set" Custom-set t]
  19091. ["Save" Custom-save t]
  19092. ["Reset to Current" Custom-reset-current t]
  19093. ["Reset to Saved" Custom-reset-saved t]
  19094. ["Reset to Standard Settings" Custom-reset-standard t]))
  19095. (message "\"Org\"-menu now contains full customization menu"))
  19096. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  19097. ;;;; Miscellaneous stuff
  19098. ;;; Generally useful functions
  19099. (defun org-get-at-bol (property)
  19100. "Get text property PROPERTY at beginning of line."
  19101. (get-text-property (point-at-bol) property))
  19102. (defun org-find-text-property-in-string (prop s)
  19103. "Return the first non-nil value of property PROP in string S."
  19104. (or (get-text-property 0 prop s)
  19105. (get-text-property (or (next-single-property-change 0 prop s) 0)
  19106. prop s)))
  19107. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  19108. "Display the given MESSAGE as a warning."
  19109. (if (fboundp 'display-warning)
  19110. (display-warning 'org message
  19111. (if (featurep 'xemacs) 'warning :warning))
  19112. (let ((buf (get-buffer-create "*Org warnings*")))
  19113. (with-current-buffer buf
  19114. (goto-char (point-max))
  19115. (insert "Warning (Org): " message)
  19116. (unless (bolp)
  19117. (newline)))
  19118. (display-buffer buf)
  19119. (sit-for 0))))
  19120. (defun org-eval (form)
  19121. "Eval FORM and return result."
  19122. (condition-case error
  19123. (eval form)
  19124. (error (format "%%![Error: %s]" error))))
  19125. (defun org-in-clocktable-p ()
  19126. "Check if the cursor is in a clocktable."
  19127. (let ((pos (point)) start)
  19128. (save-excursion
  19129. (end-of-line 1)
  19130. (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
  19131. (setq start (match-beginning 0))
  19132. (re-search-forward "^[ \t]*#\\+END:.*" nil t)
  19133. (>= (match-end 0) pos)
  19134. start))))
  19135. (defun org-in-commented-line ()
  19136. "Is point in a line starting with `#'?"
  19137. (equal (char-after (point-at-bol)) ?#))
  19138. (defun org-in-indented-comment-line ()
  19139. "Is point in a line starting with `#' after some white space?"
  19140. (save-excursion
  19141. (save-match-data
  19142. (goto-char (point-at-bol))
  19143. (looking-at "[ \t]*#"))))
  19144. (defun org-in-verbatim-emphasis ()
  19145. (save-match-data
  19146. (and (org-in-regexp org-emph-re 2)
  19147. (>= (point) (match-beginning 3))
  19148. (<= (point) (match-end 4))
  19149. (member (match-string 3) '("=" "~")))))
  19150. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  19151. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  19152. (if (and marker (marker-buffer marker)
  19153. (buffer-live-p (marker-buffer marker)))
  19154. (progn
  19155. (org-pop-to-buffer-same-window (marker-buffer marker))
  19156. (if (or (> marker (point-max)) (< marker (point-min)))
  19157. (widen))
  19158. (goto-char marker)
  19159. (org-show-context 'org-goto))
  19160. (if bookmark
  19161. (bookmark-jump bookmark)
  19162. (error "Cannot find location"))))
  19163. (defun org-quote-csv-field (s)
  19164. "Quote field for inclusion in CSV material."
  19165. (if (string-match "[\",]" s)
  19166. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  19167. s))
  19168. (defun org-force-self-insert (N)
  19169. "Needed to enforce self-insert under remapping."
  19170. (interactive "p")
  19171. (self-insert-command N))
  19172. (defun org-string-width (s)
  19173. "Compute width of string, ignoring invisible characters.
  19174. This ignores character with invisibility property `org-link', and also
  19175. characters with property `org-cwidth', because these will become invisible
  19176. upon the next fontification round."
  19177. (let (b l)
  19178. (when (or (eq t buffer-invisibility-spec)
  19179. (assq 'org-link buffer-invisibility-spec))
  19180. (while (setq b (text-property-any 0 (length s)
  19181. 'invisible 'org-link s))
  19182. (setq s (concat (substring s 0 b)
  19183. (substring s (or (next-single-property-change
  19184. b 'invisible s) (length s)))))))
  19185. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  19186. (setq s (concat (substring s 0 b)
  19187. (substring s (or (next-single-property-change
  19188. b 'org-cwidth s) (length s))))))
  19189. (setq l (string-width s) b -1)
  19190. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  19191. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  19192. l))
  19193. (defun org-shorten-string (s maxlength)
  19194. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  19195. If the string is shorter or has length MAXLENGTH, just return the
  19196. original string. If it is longer, the functions finds a space in the
  19197. string, breaks this string off at that locations and adds three dots
  19198. as ellipsis. Including the ellipsis, the string will not be longer
  19199. than MAXLENGTH. If finding a good breaking point in the string does
  19200. not work, the string is just chopped off in the middle of a word
  19201. if necessary."
  19202. (if (<= (length s) maxlength)
  19203. s
  19204. (let* ((n (max (- maxlength 4) 1))
  19205. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  19206. (if (string-match re s)
  19207. (concat (match-string 1 s) "...")
  19208. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  19209. (defun org-get-indentation (&optional line)
  19210. "Get the indentation of the current line, interpreting tabs.
  19211. When LINE is given, assume it represents a line and compute its indentation."
  19212. (if line
  19213. (if (string-match "^ *" (org-remove-tabs line))
  19214. (match-end 0))
  19215. (save-excursion
  19216. (beginning-of-line 1)
  19217. (skip-chars-forward " \t")
  19218. (current-column))))
  19219. (defun org-get-string-indentation (s)
  19220. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  19221. (let ((n -1) (i 0) (w tab-width) c)
  19222. (catch 'exit
  19223. (while (< (setq n (1+ n)) (length s))
  19224. (setq c (aref s n))
  19225. (cond ((= c ?\ ) (setq i (1+ i)))
  19226. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  19227. (t (throw 'exit t)))))
  19228. i))
  19229. (defun org-remove-tabs (s &optional width)
  19230. "Replace tabulators in S with spaces.
  19231. Assumes that s is a single line, starting in column 0."
  19232. (setq width (or width tab-width))
  19233. (while (string-match "\t" s)
  19234. (setq s (replace-match
  19235. (make-string
  19236. (- (* width (/ (+ (match-beginning 0) width) width))
  19237. (match-beginning 0)) ?\ )
  19238. t t s)))
  19239. s)
  19240. (defun org-fix-indentation (line ind)
  19241. "Fix indentation in LINE.
  19242. IND is a cons cell with target and minimum indentation.
  19243. If the current indentation in LINE is smaller than the minimum,
  19244. leave it alone. If it is larger than ind, set it to the target."
  19245. (let* ((l (org-remove-tabs line))
  19246. (i (org-get-indentation l))
  19247. (i1 (car ind)) (i2 (cdr ind)))
  19248. (if (>= i i2) (setq l (substring line i2)))
  19249. (if (> i1 0)
  19250. (concat (make-string i1 ?\ ) l)
  19251. l)))
  19252. (defun org-remove-indentation (code &optional n)
  19253. "Remove the maximum common indentation from the lines in CODE.
  19254. N may optionally be the number of spaces to remove."
  19255. (with-temp-buffer
  19256. (insert code)
  19257. (org-do-remove-indentation n)
  19258. (buffer-string)))
  19259. (defun org-do-remove-indentation (&optional n)
  19260. "Remove the maximum common indentation from the buffer."
  19261. (untabify (point-min) (point-max))
  19262. (let ((min 10000) re)
  19263. (if n
  19264. (setq min n)
  19265. (goto-char (point-min))
  19266. (while (re-search-forward "^ *[^ \n]" nil t)
  19267. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  19268. (unless (or (= min 0) (= min 10000))
  19269. (setq re (format "^ \\{%d\\}" min))
  19270. (goto-char (point-min))
  19271. (while (re-search-forward re nil t)
  19272. (replace-match "")
  19273. (end-of-line 1))
  19274. min)))
  19275. (defun org-fill-template (template alist)
  19276. "Find each %key of ALIST in TEMPLATE and replace it."
  19277. (let ((case-fold-search nil)
  19278. entry key value)
  19279. (setq alist (sort (copy-sequence alist)
  19280. (lambda (a b) (< (length (car a)) (length (car b))))))
  19281. (while (setq entry (pop alist))
  19282. (setq template
  19283. (replace-regexp-in-string
  19284. (concat "%" (regexp-quote (car entry)))
  19285. (or (cdr entry) "") template t t)))
  19286. template))
  19287. (defun org-base-buffer (buffer)
  19288. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  19289. (if (not buffer)
  19290. buffer
  19291. (or (buffer-base-buffer buffer)
  19292. buffer)))
  19293. (defun org-trim (s)
  19294. "Remove whitespace at beginning and end of string."
  19295. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  19296. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  19297. s)
  19298. (defun org-wrap (string &optional width lines)
  19299. "Wrap string to either a number of lines, or a width in characters.
  19300. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  19301. that costs. If there is a word longer than WIDTH, the text is actually
  19302. wrapped to the length of that word.
  19303. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  19304. many lines, whatever width that takes.
  19305. The return value is a list of lines, without newlines at the end."
  19306. (let* ((words (org-split-string string "[ \t\n]+"))
  19307. (maxword (apply 'max (mapcar 'org-string-width words)))
  19308. w ll)
  19309. (cond (width
  19310. (org-do-wrap words (max maxword width)))
  19311. (lines
  19312. (setq w maxword)
  19313. (setq ll (org-do-wrap words maxword))
  19314. (if (<= (length ll) lines)
  19315. ll
  19316. (setq ll words)
  19317. (while (> (length ll) lines)
  19318. (setq w (1+ w))
  19319. (setq ll (org-do-wrap words w)))
  19320. ll))
  19321. (t (error "Cannot wrap this")))))
  19322. (defun org-do-wrap (words width)
  19323. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  19324. (let (lines line)
  19325. (while words
  19326. (setq line (pop words))
  19327. (while (and words (< (+ (length line) (length (car words))) width))
  19328. (setq line (concat line " " (pop words))))
  19329. (setq lines (push line lines)))
  19330. (nreverse lines)))
  19331. (defun org-split-string (string &optional separators)
  19332. "Splits STRING into substrings at SEPARATORS.
  19333. No empty strings are returned if there are matches at the beginning
  19334. and end of string."
  19335. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  19336. (start 0)
  19337. notfirst
  19338. (list nil))
  19339. (while (and (string-match rexp string
  19340. (if (and notfirst
  19341. (= start (match-beginning 0))
  19342. (< start (length string)))
  19343. (1+ start) start))
  19344. (< (match-beginning 0) (length string)))
  19345. (setq notfirst t)
  19346. (or (eq (match-beginning 0) 0)
  19347. (and (eq (match-beginning 0) (match-end 0))
  19348. (eq (match-beginning 0) start))
  19349. (setq list
  19350. (cons (substring string start (match-beginning 0))
  19351. list)))
  19352. (setq start (match-end 0)))
  19353. (or (eq start (length string))
  19354. (setq list
  19355. (cons (substring string start)
  19356. list)))
  19357. (nreverse list)))
  19358. (defun org-quote-vert (s)
  19359. "Replace \"|\" with \"\\vert\"."
  19360. (while (string-match "|" s)
  19361. (setq s (replace-match "\\vert" t t s)))
  19362. s)
  19363. (defun org-uuidgen-p (s)
  19364. "Is S an ID created by UUIDGEN?"
  19365. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  19366. (defun org-in-src-block-p (&optional inside)
  19367. "Whether point is in a code source block.
  19368. When INSIDE is non-nil, don't consider we are within a src block
  19369. when point is at #+BEGIN_SRC or #+END_SRC."
  19370. (let ((case-fold-search t) ov)
  19371. (or (and (setq ov (overlays-at (point)))
  19372. (memq 'org-block-background
  19373. (overlay-properties (car ov))))
  19374. (and (not inside)
  19375. (save-match-data
  19376. (save-excursion
  19377. (beginning-of-line)
  19378. (looking-at ".*#\\+\\(begin\\|end\\)_src")))))))
  19379. (defun org-context ()
  19380. "Return a list of contexts of the current cursor position.
  19381. If several contexts apply, all are returned.
  19382. Each context entry is a list with a symbol naming the context, and
  19383. two positions indicating start and end of the context. Possible
  19384. contexts are:
  19385. :headline anywhere in a headline
  19386. :headline-stars on the leading stars in a headline
  19387. :todo-keyword on a TODO keyword (including DONE) in a headline
  19388. :tags on the TAGS in a headline
  19389. :priority on the priority cookie in a headline
  19390. :item on the first line of a plain list item
  19391. :item-bullet on the bullet/number of a plain list item
  19392. :checkbox on the checkbox in a plain list item
  19393. :table in an org-mode table
  19394. :table-special on a special filed in a table
  19395. :table-table in a table.el table
  19396. :clocktable in a clocktable
  19397. :src-block in a source block
  19398. :link on a hyperlink
  19399. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT, QUOTE.
  19400. :target on a <<target>>
  19401. :radio-target on a <<<radio-target>>>
  19402. :latex-fragment on a LaTeX fragment
  19403. :latex-preview on a LaTeX fragment with overlaid preview image
  19404. This function expects the position to be visible because it uses font-lock
  19405. faces as a help to recognize the following contexts: :table-special, :link,
  19406. and :keyword."
  19407. (let* ((f (get-text-property (point) 'face))
  19408. (faces (if (listp f) f (list f)))
  19409. (case-fold-search t)
  19410. (p (point)) clist o)
  19411. ;; First the large context
  19412. (cond
  19413. ((org-at-heading-p t)
  19414. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  19415. (when (progn
  19416. (beginning-of-line 1)
  19417. (looking-at org-todo-line-tags-regexp))
  19418. (push (org-point-in-group p 1 :headline-stars) clist)
  19419. (push (org-point-in-group p 2 :todo-keyword) clist)
  19420. (push (org-point-in-group p 4 :tags) clist))
  19421. (goto-char p)
  19422. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  19423. (if (looking-at "\\[#[A-Z0-9]\\]")
  19424. (push (org-point-in-group p 0 :priority) clist)))
  19425. ((org-at-item-p)
  19426. (push (org-point-in-group p 2 :item-bullet) clist)
  19427. (push (list :item (point-at-bol)
  19428. (save-excursion (org-end-of-item) (point)))
  19429. clist)
  19430. (and (org-at-item-checkbox-p)
  19431. (push (org-point-in-group p 0 :checkbox) clist)))
  19432. ((org-at-table-p)
  19433. (push (list :table (org-table-begin) (org-table-end)) clist)
  19434. (if (memq 'org-formula faces)
  19435. (push (list :table-special
  19436. (previous-single-property-change p 'face)
  19437. (next-single-property-change p 'face)) clist)))
  19438. ((org-at-table-p 'any)
  19439. (push (list :table-table) clist)))
  19440. (goto-char p)
  19441. (let ((case-fold-search t))
  19442. ;; New the "medium" contexts: clocktables, source blocks
  19443. (cond ((org-in-clocktable-p)
  19444. (push (list :clocktable
  19445. (and (or (looking-at "#\\+BEGIN: clocktable")
  19446. (search-backward "#+BEGIN: clocktable" nil t))
  19447. (match-beginning 0))
  19448. (and (re-search-forward "#\\+END:?" nil t)
  19449. (match-end 0))) clist))
  19450. ((org-in-src-block-p)
  19451. (push (list :src-block
  19452. (and (or (looking-at "#\\+BEGIN_SRC")
  19453. (search-backward "#+BEGIN_SRC" nil t))
  19454. (match-beginning 0))
  19455. (and (search-forward "#+END_SRC" nil t)
  19456. (match-beginning 0))) clist))))
  19457. (goto-char p)
  19458. ;; Now the small context
  19459. (cond
  19460. ((org-at-timestamp-p)
  19461. (push (org-point-in-group p 0 :timestamp) clist))
  19462. ((memq 'org-link faces)
  19463. (push (list :link
  19464. (previous-single-property-change p 'face)
  19465. (next-single-property-change p 'face)) clist))
  19466. ((memq 'org-special-keyword faces)
  19467. (push (list :keyword
  19468. (previous-single-property-change p 'face)
  19469. (next-single-property-change p 'face)) clist))
  19470. ((org-at-target-p)
  19471. (push (org-point-in-group p 0 :target) clist)
  19472. (goto-char (1- (match-beginning 0)))
  19473. (if (looking-at org-radio-target-regexp)
  19474. (push (org-point-in-group p 0 :radio-target) clist))
  19475. (goto-char p))
  19476. ((setq o (car (delq nil
  19477. (mapcar
  19478. (lambda (x)
  19479. (if (memq x org-latex-fragment-image-overlays) x))
  19480. (overlays-at (point))))))
  19481. (push (list :latex-fragment
  19482. (overlay-start o) (overlay-end o)) clist)
  19483. (push (list :latex-preview
  19484. (overlay-start o) (overlay-end o)) clist))
  19485. ((org-inside-LaTeX-fragment-p)
  19486. ;; FIXME: positions wrong.
  19487. (push (list :latex-fragment (point) (point)) clist)))
  19488. (setq clist (nreverse (delq nil clist)))
  19489. clist))
  19490. ;; FIXME: Compare with at-regexp-p Do we need both?
  19491. (defun org-in-regexp (re &optional nlines visually)
  19492. "Check if point is inside a match of regexp.
  19493. Normally only the current line is checked, but you can include NLINES extra
  19494. lines both before and after point into the search.
  19495. If VISUALLY is set, require that the cursor is not after the match but
  19496. really on, so that the block visually is on the match."
  19497. (catch 'exit
  19498. (let ((pos (point))
  19499. (eol (point-at-eol (+ 1 (or nlines 0))))
  19500. (inc (if visually 1 0)))
  19501. (save-excursion
  19502. (beginning-of-line (- 1 (or nlines 0)))
  19503. (while (re-search-forward re eol t)
  19504. (if (and (<= (match-beginning 0) pos)
  19505. (>= (+ inc (match-end 0)) pos))
  19506. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  19507. (defun org-at-regexp-p (regexp)
  19508. "Is point inside a match of REGEXP in the current line?"
  19509. (catch 'exit
  19510. (save-excursion
  19511. (let ((pos (point)) (end (point-at-eol)))
  19512. (beginning-of-line 1)
  19513. (while (re-search-forward regexp end t)
  19514. (if (and (<= (match-beginning 0) pos)
  19515. (>= (match-end 0) pos))
  19516. (throw 'exit t)))
  19517. nil))))
  19518. (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
  19519. "Non-nil when point is between matches of START-RE and END-RE.
  19520. Also return a non-nil value when point is on one of the matches.
  19521. Optional arguments LIM-UP and LIM-DOWN bound the search; they are
  19522. buffer positions. Default values are the positions of headlines
  19523. surrounding the point.
  19524. The functions returns a cons cell whose car (resp. cdr) is the
  19525. position before START-RE (resp. after END-RE)."
  19526. (save-match-data
  19527. (let ((pos (point))
  19528. (limit-up (or lim-up (save-excursion (outline-previous-heading))))
  19529. (limit-down (or lim-down (save-excursion (outline-next-heading))))
  19530. beg end)
  19531. (save-excursion
  19532. ;; Point is on a block when on START-RE or if START-RE can be
  19533. ;; found before it...
  19534. (and (or (org-at-regexp-p start-re)
  19535. (re-search-backward start-re limit-up t))
  19536. (setq beg (match-beginning 0))
  19537. ;; ... and END-RE after it...
  19538. (goto-char (match-end 0))
  19539. (re-search-forward end-re limit-down t)
  19540. (> (setq end (match-end 0)) pos)
  19541. ;; ... without another START-RE in-between.
  19542. (goto-char (match-beginning 0))
  19543. (not (re-search-backward start-re (1+ beg) t))
  19544. ;; Return value.
  19545. (cons beg end))))))
  19546. (defun org-in-block-p (names)
  19547. "Non-nil when point belongs to a block whose name belongs to NAMES.
  19548. NAMES is a list of strings containing names of blocks.
  19549. Return first block name matched, or nil. Beware that in case of
  19550. nested blocks, the returned name may not belong to the closest
  19551. block from point."
  19552. (save-match-data
  19553. (catch 'exit
  19554. (let ((case-fold-search t)
  19555. (lim-up (save-excursion (outline-previous-heading)))
  19556. (lim-down (save-excursion (outline-next-heading))))
  19557. (mapc (lambda (name)
  19558. (let ((n (regexp-quote name)))
  19559. (when (org-between-regexps-p
  19560. (concat "^[ \t]*#\\+begin_" n)
  19561. (concat "^[ \t]*#\\+end_" n)
  19562. lim-up lim-down)
  19563. (throw 'exit n))))
  19564. names))
  19565. nil)))
  19566. (defun org-occur-in-agenda-files (regexp &optional nlines)
  19567. "Call `multi-occur' with buffers for all agenda files."
  19568. (interactive "sOrg-files matching: \np")
  19569. (let* ((files (org-agenda-files))
  19570. (tnames (mapcar 'file-truename files))
  19571. (extra org-agenda-text-search-extra-files)
  19572. f)
  19573. (when (eq (car extra) 'agenda-archives)
  19574. (setq extra (cdr extra))
  19575. (setq files (org-add-archive-files files)))
  19576. (while (setq f (pop extra))
  19577. (unless (member (file-truename f) tnames)
  19578. (add-to-list 'files f 'append)
  19579. (add-to-list 'tnames (file-truename f) 'append)))
  19580. (multi-occur
  19581. (mapcar (lambda (x)
  19582. (with-current-buffer
  19583. (or (get-file-buffer x) (find-file-noselect x))
  19584. (widen)
  19585. (current-buffer)))
  19586. files)
  19587. regexp)))
  19588. (if (boundp 'occur-mode-find-occurrence-hook)
  19589. ;; Emacs 23
  19590. (add-hook 'occur-mode-find-occurrence-hook
  19591. (lambda ()
  19592. (when (derived-mode-p 'org-mode)
  19593. (org-reveal))))
  19594. ;; Emacs 22
  19595. (defadvice occur-mode-goto-occurrence
  19596. (after org-occur-reveal activate)
  19597. (and (derived-mode-p 'org-mode) (org-reveal)))
  19598. (defadvice occur-mode-goto-occurrence-other-window
  19599. (after org-occur-reveal activate)
  19600. (and (derived-mode-p 'org-mode) (org-reveal)))
  19601. (defadvice occur-mode-display-occurrence
  19602. (after org-occur-reveal activate)
  19603. (when (derived-mode-p 'org-mode)
  19604. (let ((pos (occur-mode-find-occurrence)))
  19605. (with-current-buffer (marker-buffer pos)
  19606. (save-excursion
  19607. (goto-char pos)
  19608. (org-reveal)))))))
  19609. (defun org-occur-link-in-agenda-files ()
  19610. "Create a link and search for it in the agendas.
  19611. The link is not stored in `org-stored-links', it is just created
  19612. for the search purpose."
  19613. (interactive)
  19614. (let ((link (condition-case nil
  19615. (org-store-link nil)
  19616. (error "Unable to create a link to here"))))
  19617. (org-occur-in-agenda-files (regexp-quote link))))
  19618. (defun org-reverse-string (string)
  19619. "Return the reverse of STRING."
  19620. (apply 'string (reverse (string-to-list string))))
  19621. (defun org-uniquify (list)
  19622. "Remove duplicate elements from LIST."
  19623. (let (res)
  19624. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  19625. res))
  19626. (defun org-delete-all (elts list)
  19627. "Remove all elements in ELTS from LIST."
  19628. (while elts
  19629. (setq list (delete (pop elts) list)))
  19630. list)
  19631. (defun org-count (cl-item cl-seq)
  19632. "Count the number of occurrences of ITEM in SEQ.
  19633. Taken from `count' in cl-seq.el with all keyword arguments removed."
  19634. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  19635. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  19636. (while (< cl-start cl-end)
  19637. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  19638. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  19639. (setq cl-start (1+ cl-start)))
  19640. cl-count))
  19641. (defun org-remove-if (predicate seq)
  19642. "Remove everything from SEQ that fulfills PREDICATE."
  19643. (let (res e)
  19644. (while seq
  19645. (setq e (pop seq))
  19646. (if (not (funcall predicate e)) (push e res)))
  19647. (nreverse res)))
  19648. (defun org-remove-if-not (predicate seq)
  19649. "Remove everything from SEQ that does not fulfill PREDICATE."
  19650. (let (res e)
  19651. (while seq
  19652. (setq e (pop seq))
  19653. (if (funcall predicate e) (push e res)))
  19654. (nreverse res)))
  19655. (defun org-reduce (cl-func cl-seq &rest cl-keys)
  19656. "Reduce two-argument FUNCTION across SEQ.
  19657. Taken from `reduce' in cl-seq.el with all keyword arguments but
  19658. \":initial-value\" removed."
  19659. (let ((cl-accum (cond ((memq :initial-value cl-keys)
  19660. (cadr (memq :initial-value cl-keys)))
  19661. (cl-seq (pop cl-seq))
  19662. (t (funcall cl-func)))))
  19663. (while cl-seq
  19664. (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
  19665. cl-accum))
  19666. (defun org-back-over-empty-lines ()
  19667. "Move backwards over whitespace, to the beginning of the first empty line.
  19668. Returns the number of empty lines passed."
  19669. (let ((pos (point)))
  19670. (if (cdr (assoc 'heading org-blank-before-new-entry))
  19671. (skip-chars-backward " \t\n\r")
  19672. (unless (eobp)
  19673. (forward-line -1)))
  19674. (beginning-of-line 2)
  19675. (goto-char (min (point) pos))
  19676. (count-lines (point) pos)))
  19677. (defun org-skip-whitespace ()
  19678. (skip-chars-forward " \t\n\r"))
  19679. (defun org-point-in-group (point group &optional context)
  19680. "Check if POINT is in match-group GROUP.
  19681. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  19682. match. If the match group does not exist or point is not inside it,
  19683. return nil."
  19684. (and (match-beginning group)
  19685. (>= point (match-beginning group))
  19686. (<= point (match-end group))
  19687. (if context
  19688. (list context (match-beginning group) (match-end group))
  19689. t)))
  19690. (defun org-switch-to-buffer-other-window (&rest args)
  19691. "Switch to buffer in a second window on the current frame.
  19692. In particular, do not allow pop-up frames.
  19693. Returns the newly created buffer."
  19694. (org-no-popups
  19695. (apply 'switch-to-buffer-other-window args)))
  19696. (defun org-combine-plists (&rest plists)
  19697. "Create a single property list from all plists in PLISTS.
  19698. The process starts by copying the first list, and then setting properties
  19699. from the other lists. Settings in the last list are the most significant
  19700. ones and overrule settings in the other lists."
  19701. (let ((rtn (copy-sequence (pop plists)))
  19702. p v ls)
  19703. (while plists
  19704. (setq ls (pop plists))
  19705. (while ls
  19706. (setq p (pop ls) v (pop ls))
  19707. (setq rtn (plist-put rtn p v))))
  19708. rtn))
  19709. (defun org-replace-escapes (string table)
  19710. "Replace %-escapes in STRING with values in TABLE.
  19711. TABLE is an association list with keys like \"%a\" and string values.
  19712. The sequences in STRING may contain normal field width and padding information,
  19713. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  19714. so values can contain further %-escapes if they are define later in TABLE."
  19715. (let ((tbl (copy-alist table))
  19716. (case-fold-search nil)
  19717. (pchg 0)
  19718. e re rpl)
  19719. (while (setq e (pop tbl))
  19720. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  19721. (when (and (cdr e) (string-match re (cdr e)))
  19722. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  19723. (safe "SREF"))
  19724. (add-text-properties 0 3 (list 'sref sref) safe)
  19725. (setcdr e (replace-match safe t t (cdr e)))))
  19726. (while (string-match re string)
  19727. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  19728. (cdr e)))
  19729. (setq string (replace-match rpl t t string))))
  19730. (while (setq pchg (next-property-change pchg string))
  19731. (let ((sref (get-text-property pchg 'sref string)))
  19732. (when (and sref (string-match "SREF" string pchg))
  19733. (setq string (replace-match sref t t string)))))
  19734. string))
  19735. (defun org-sublist (list start end)
  19736. "Return a section of LIST, from START to END.
  19737. Counting starts at 1."
  19738. (let (rtn (c start))
  19739. (setq list (nthcdr (1- start) list))
  19740. (while (and list (<= c end))
  19741. (push (pop list) rtn)
  19742. (setq c (1+ c)))
  19743. (nreverse rtn)))
  19744. (defun org-find-base-buffer-visiting (file)
  19745. "Like `find-buffer-visiting' but always return the base buffer and
  19746. not an indirect buffer."
  19747. (let ((buf (or (get-file-buffer file)
  19748. (find-buffer-visiting file))))
  19749. (if buf
  19750. (or (buffer-base-buffer buf) buf)
  19751. nil)))
  19752. (defun org-image-file-name-regexp (&optional extensions)
  19753. "Return regexp matching the file names of images.
  19754. If EXTENSIONS is given, only match these."
  19755. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  19756. (image-file-name-regexp)
  19757. (let ((image-file-name-extensions
  19758. (or extensions
  19759. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  19760. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  19761. (concat "\\."
  19762. (regexp-opt (nconc (mapcar 'upcase
  19763. image-file-name-extensions)
  19764. image-file-name-extensions)
  19765. t)
  19766. "\\'"))))
  19767. (defun org-file-image-p (file &optional extensions)
  19768. "Return non-nil if FILE is an image."
  19769. (save-match-data
  19770. (string-match (org-image-file-name-regexp extensions) file)))
  19771. (defun org-get-cursor-date (&optional with-time)
  19772. "Return the date at cursor in as a time.
  19773. This works in the calendar and in the agenda, anywhere else it just
  19774. returns the current time.
  19775. If WITH-TIME is non-nil, returns the time of the event at point (in
  19776. the agenda) or the current time of the day."
  19777. (let (date day defd tp tm hod mod)
  19778. (when with-time
  19779. (setq tp (get-text-property (point) 'time))
  19780. (when (and tp (string-match "\\([0-9][0-9]\\):\\([0-9][0-9]\\)" tp))
  19781. (setq hod (string-to-number (match-string 1 tp))
  19782. mod (string-to-number (match-string 2 tp))))
  19783. (or tp (setq hod (nth 2 (decode-time (current-time)))
  19784. mod (nth 1 (decode-time (current-time))))))
  19785. (cond
  19786. ((eq major-mode 'calendar-mode)
  19787. (setq date (calendar-cursor-to-date)
  19788. defd (encode-time 0 (or mod 0) (or hod 0)
  19789. (nth 1 date) (nth 0 date) (nth 2 date))))
  19790. ((eq major-mode 'org-agenda-mode)
  19791. (setq day (get-text-property (point) 'day))
  19792. (if day
  19793. (setq date (calendar-gregorian-from-absolute day)
  19794. defd (encode-time 0 (or mod 0) (or hod 0)
  19795. (nth 1 date) (nth 0 date) (nth 2 date))))))
  19796. (or defd (current-time))))
  19797. (defun org-mark-subtree (&optional up)
  19798. "Mark the current subtree.
  19799. This puts point at the start of the current subtree, and mark at
  19800. the end. If a numeric prefix UP is given, move up into the
  19801. hierarchy of headlines by UP levels before marking the subtree."
  19802. (interactive "P")
  19803. (org-with-limited-levels
  19804. (cond ((org-at-heading-p) (beginning-of-line))
  19805. ((org-before-first-heading-p) (error "Not in a subtree"))
  19806. (t (outline-previous-visible-heading 1))))
  19807. (when up (while (and (> up 0) (org-up-heading-safe)) (decf up)))
  19808. (if (org-called-interactively-p 'any)
  19809. (call-interactively 'org-mark-element)
  19810. (org-mark-element)))
  19811. ;;; Indentation
  19812. (defun org-indent-line ()
  19813. "Indent line depending on context."
  19814. (interactive)
  19815. (let* ((pos (point))
  19816. (itemp (org-at-item-p))
  19817. (case-fold-search t)
  19818. (org-drawer-regexp (or org-drawer-regexp "\000"))
  19819. (inline-task-p (and (featurep 'org-inlinetask)
  19820. (org-inlinetask-in-task-p)))
  19821. (inline-re (and inline-task-p
  19822. (org-inlinetask-outline-regexp)))
  19823. column)
  19824. (if (and orgstruct-is-++ (eq pos (point)))
  19825. (let ((indent-line-function (cadadr (assoc 'indent-line-function org-fb-vars))))
  19826. (indent-according-to-mode))
  19827. (beginning-of-line 1)
  19828. (cond
  19829. ;; Headings
  19830. ((looking-at org-outline-regexp) (setq column 0))
  19831. ;; Footnote definition
  19832. ((looking-at org-footnote-definition-re) (setq column 0))
  19833. ;; Literal examples
  19834. ((looking-at "[ \t]*:\\( \\|$\\)")
  19835. (setq column (org-get-indentation))) ; do nothing
  19836. ;; Lists
  19837. ((ignore-errors (goto-char (org-in-item-p)))
  19838. (setq column (if itemp
  19839. (org-get-indentation)
  19840. (org-list-item-body-column (point))))
  19841. (goto-char pos))
  19842. ;; Drawers
  19843. ((and (looking-at "[ \t]*:END:")
  19844. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  19845. (save-excursion
  19846. (goto-char (1- (match-beginning 1)))
  19847. (setq column (current-column))))
  19848. ;; Special blocks
  19849. ((and (looking-at "[ \t]*#\\+end_\\([a-z]+\\)")
  19850. (save-excursion
  19851. (re-search-backward
  19852. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  19853. (setq column (org-get-indentation (match-string 0))))
  19854. ((and (not (looking-at "[ \t]*#\\+begin_"))
  19855. (org-between-regexps-p "^[ \t]*#\\+begin_" "[ \t]*#\\+end_"))
  19856. (save-excursion
  19857. (re-search-backward "^[ \t]*#\\+begin_\\([a-z]+\\)" nil t))
  19858. (setq column
  19859. (cond ((equal (downcase (match-string 1)) "src")
  19860. ;; src blocks: let `org-edit-src-exit' handle them
  19861. (org-get-indentation))
  19862. ((equal (downcase (match-string 1)) "example")
  19863. (max (org-get-indentation)
  19864. (org-get-indentation (match-string 0))))
  19865. (t
  19866. (org-get-indentation (match-string 0))))))
  19867. ;; This line has nothing special, look at the previous relevant
  19868. ;; line to compute indentation
  19869. (t
  19870. (beginning-of-line 0)
  19871. (while (and (not (bobp))
  19872. (not (looking-at org-table-line-regexp))
  19873. (not (looking-at org-drawer-regexp))
  19874. ;; When point started in an inline task, do not move
  19875. ;; above task starting line.
  19876. (not (and inline-task-p (looking-at inline-re)))
  19877. ;; Skip drawers, blocks, empty lines, verbatim,
  19878. ;; comments, tables, footnotes definitions, lists,
  19879. ;; inline tasks.
  19880. (or (and (looking-at "[ \t]*:END:")
  19881. (re-search-backward org-drawer-regexp nil t))
  19882. (and (looking-at "[ \t]*#\\+end_")
  19883. (re-search-backward "[ \t]*#\\+begin_"nil t))
  19884. (looking-at "[ \t]*[\n:#|]")
  19885. (looking-at org-footnote-definition-re)
  19886. (and (ignore-errors (goto-char (org-in-item-p)))
  19887. (goto-char
  19888. (org-list-get-top-point (org-list-struct))))
  19889. (and (not inline-task-p)
  19890. (featurep 'org-inlinetask)
  19891. (org-inlinetask-in-task-p)
  19892. (or (org-inlinetask-goto-beginning) t))))
  19893. (beginning-of-line 0))
  19894. (cond
  19895. ;; There was an heading above.
  19896. ((looking-at "\\*+[ \t]+")
  19897. (if (not org-adapt-indentation)
  19898. (setq column 0)
  19899. (goto-char (match-end 0))
  19900. (setq column (current-column))))
  19901. ;; A drawer had started and is unfinished
  19902. ((looking-at org-drawer-regexp)
  19903. (goto-char (1- (match-beginning 1)))
  19904. (setq column (current-column)))
  19905. ;; Else, nothing noticeable found: get indentation and go on.
  19906. (t (setq column (org-get-indentation))))))
  19907. ;; Now apply indentation and move cursor accordingly
  19908. (goto-char pos)
  19909. (if (<= (current-column) (current-indentation))
  19910. (org-indent-line-to column)
  19911. (save-excursion (org-indent-line-to column)))
  19912. ;; Special polishing for properties, see `org-property-format'
  19913. (setq column (current-column))
  19914. (beginning-of-line 1)
  19915. (if (looking-at
  19916. "\\([ \t]*\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  19917. (replace-match (concat (match-string 1)
  19918. (format org-property-format
  19919. (match-string 2) (match-string 3)))
  19920. t t))
  19921. (org-move-to-column column))))
  19922. (defun org-indent-drawer ()
  19923. "Indent the drawer at point."
  19924. (interactive)
  19925. (let ((p (point))
  19926. (e (and (save-excursion (re-search-forward ":END:" nil t))
  19927. (match-end 0)))
  19928. (folded
  19929. (save-excursion
  19930. (end-of-line)
  19931. (when (overlays-at (point))
  19932. (member 'invisible (overlay-properties
  19933. (car (overlays-at (point)))))))))
  19934. (when folded (org-cycle))
  19935. (indent-for-tab-command)
  19936. (while (and (move-beginning-of-line 2) (< (point) e))
  19937. (indent-for-tab-command))
  19938. (goto-char p)
  19939. (when folded (org-cycle)))
  19940. (message "Drawer at point indented"))
  19941. (defun org-indent-block ()
  19942. "Indent the block at point."
  19943. (interactive)
  19944. (let ((p (point))
  19945. (case-fold-search t)
  19946. (e (and (save-excursion (re-search-forward "#\\+end_?\\(?:[a-z]+\\)?" nil t))
  19947. (match-end 0)))
  19948. (folded
  19949. (save-excursion
  19950. (end-of-line)
  19951. (when (overlays-at (point))
  19952. (member 'invisible (overlay-properties
  19953. (car (overlays-at (point)))))))))
  19954. (when folded (org-cycle))
  19955. (indent-for-tab-command)
  19956. (while (and (move-beginning-of-line 2) (< (point) e))
  19957. (indent-for-tab-command))
  19958. (goto-char p)
  19959. (when folded (org-cycle)))
  19960. (message "Block at point indented"))
  19961. (defun org-indent-region (start end)
  19962. "Indent region."
  19963. (interactive "r")
  19964. (save-excursion
  19965. (let ((line-end (org-current-line end)))
  19966. (goto-char start)
  19967. (while (< (org-current-line) line-end)
  19968. (cond ((org-in-src-block-p) (org-src-native-tab-command-maybe))
  19969. (t (call-interactively 'org-indent-line)))
  19970. (move-beginning-of-line 2)))))
  19971. ;;; Filling
  19972. ;; We use our own fill-paragraph and auto-fill functions.
  19973. ;; `org-fill-paragraph' relies on adaptive filling and context
  19974. ;; checking. Appropriate `fill-prefix' is computed with
  19975. ;; `org-adaptive-fill-function'.
  19976. ;; `org-auto-fill-function' takes care of auto-filling. It calls
  19977. ;; `do-auto-fill' only on valid areas with `fill-prefix' shadowed with
  19978. ;; `org-adaptive-fill-function' value. Internally,
  19979. ;; `org-comment-line-break-function' breaks the line.
  19980. ;; `org-setup-filling' installs filling and auto-filling related
  19981. ;; variables during `org-mode' initialization.
  19982. (defun org-setup-filling ()
  19983. (interactive)
  19984. ;; Prevent auto-fill from inserting unwanted new items.
  19985. (when (boundp 'fill-nobreak-predicate)
  19986. (org-set-local
  19987. 'fill-nobreak-predicate
  19988. (org-uniquify
  19989. (append fill-nobreak-predicate
  19990. '(org-fill-paragraph-separate-nobreak-p
  19991. org-fill-line-break-nobreak-p
  19992. org-fill-paragraph-with-timestamp-nobreak-p)))))
  19993. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  19994. (org-set-local 'auto-fill-inhibit-regexp nil)
  19995. (org-set-local 'adaptive-fill-function 'org-adaptive-fill-function)
  19996. (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
  19997. (org-set-local 'comment-line-break-function 'org-comment-line-break-function))
  19998. (defvar org-element-paragraph-separate) ; org-element.el
  19999. (defun org-fill-paragraph-separate-nobreak-p ()
  20000. "Non-nil when a line break at point would insert a new item."
  20001. (looking-at (substring org-element-paragraph-separate 1)))
  20002. (defun org-fill-line-break-nobreak-p ()
  20003. "Non-nil when a line break at point would create an Org line break."
  20004. (save-excursion
  20005. (skip-chars-backward "[ \t]")
  20006. (skip-chars-backward "\\\\")
  20007. (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
  20008. (defun org-fill-paragraph-with-timestamp-nobreak-p ()
  20009. "Non-nil when a line break at point would insert a new item."
  20010. (and (org-at-timestamp-p t)
  20011. (not (looking-at org-ts-regexp-both))))
  20012. (declare-function message-in-body-p "message" ())
  20013. (defvar orgtbl-line-start-regexp) ; From org-table.el
  20014. (defun org-adaptive-fill-function ()
  20015. "Compute a fill prefix for the current line.
  20016. Return fill prefix, as a string, or nil if current line isn't
  20017. meant to be filled."
  20018. (let (prefix)
  20019. (catch 'exit
  20020. (when (derived-mode-p 'message-mode)
  20021. (save-excursion
  20022. (beginning-of-line)
  20023. (cond ((or (not (message-in-body-p))
  20024. (looking-at orgtbl-line-start-regexp))
  20025. (throw 'exit nil))
  20026. ((looking-at message-cite-prefix-regexp)
  20027. (throw 'exit (match-string-no-properties 0)))
  20028. ((looking-at org-outline-regexp)
  20029. (throw 'exit (make-string (length (match-string 0)) ? ))))))
  20030. (org-with-wide-buffer
  20031. (let* ((p (line-beginning-position))
  20032. (element (save-excursion (beginning-of-line)
  20033. (org-element-at-point)))
  20034. (type (org-element-type element))
  20035. (post-affiliated (org-element-property :post-affiliated element)))
  20036. (unless (and post-affiliated (< p post-affiliated))
  20037. (case type
  20038. (comment (looking-at "[ \t]*# ?") (match-string 0))
  20039. (footnote-definition "")
  20040. ((item plain-list)
  20041. (make-string (org-list-item-body-column
  20042. (or post-affiliated
  20043. (org-element-property :begin element)))
  20044. ? ))
  20045. (paragraph
  20046. ;; Fill prefix is usually the same as the current line,
  20047. ;; except if the paragraph is at the beginning of an item.
  20048. (let ((parent (org-element-property :parent element)))
  20049. (cond ((eq (org-element-type parent) 'item)
  20050. (make-string (org-list-item-body-column
  20051. (org-element-property :begin parent))
  20052. ? ))
  20053. ((save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  20054. (match-string 0))
  20055. (t ""))))
  20056. (comment-block
  20057. ;; Only fill contents if P is within block boundaries.
  20058. (let* ((cbeg (save-excursion (goto-char post-affiliated)
  20059. (forward-line)
  20060. (point)))
  20061. (cend (save-excursion
  20062. (goto-char (org-element-property :end element))
  20063. (skip-chars-backward " \r\t\n")
  20064. (line-beginning-position))))
  20065. (when (and (>= p cbeg) (< p cend))
  20066. (if (save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  20067. (match-string 0)
  20068. "")))))))))))
  20069. (declare-function message-goto-body "message" ())
  20070. (defvar message-cite-prefix-regexp) ; From message.el
  20071. (defun org-fill-paragraph (&optional justify)
  20072. "Fill element at point, when applicable.
  20073. This function only applies to comment blocks, comments, example
  20074. blocks and paragraphs. Also, as a special case, re-align table
  20075. when point is at one.
  20076. If JUSTIFY is non-nil (interactively, with prefix argument),
  20077. justify as well. If `sentence-end-double-space' is non-nil, then
  20078. period followed by one space does not end a sentence, so don't
  20079. break a line there. The variable `fill-column' controls the
  20080. width for filling.
  20081. For convenience, when point is at a plain list, an item or
  20082. a footnote definition, try to fill the first paragraph within."
  20083. (interactive)
  20084. (if (and (derived-mode-p 'message-mode)
  20085. (or (not (message-in-body-p))
  20086. (save-excursion (move-beginning-of-line 1)
  20087. (looking-at message-cite-prefix-regexp))))
  20088. ;; First ensure filling is correct in message-mode.
  20089. (let ((fill-paragraph-function
  20090. (cadadr (assoc 'fill-paragraph-function org-fb-vars)))
  20091. (fill-prefix (cadadr (assoc 'fill-prefix org-fb-vars)))
  20092. (paragraph-start (cadadr (assoc 'paragraph-start org-fb-vars)))
  20093. (paragraph-separate
  20094. (cadadr (assoc 'paragraph-separate org-fb-vars))))
  20095. (fill-paragraph nil))
  20096. (with-syntax-table org-mode-transpose-word-syntax-table
  20097. ;; Move to end of line in order to get the first paragraph
  20098. ;; within a plain list or a footnote definition.
  20099. (let ((element (save-excursion (end-of-line) (org-element-at-point))))
  20100. ;; First check if point is in a blank line at the beginning of
  20101. ;; the buffer. In that case, ignore filling.
  20102. (case (org-element-type element)
  20103. ;; Use major mode filling function is src blocks.
  20104. (src-block (org-babel-do-key-sequence-in-edit-buffer (kbd "M-q")))
  20105. ;; Align Org tables, leave table.el tables as-is.
  20106. (table-row (org-table-align) t)
  20107. (table
  20108. (when (eq (org-element-property :type element) 'org)
  20109. (org-table-align))
  20110. t)
  20111. (paragraph
  20112. ;; Paragraphs may contain `line-break' type objects.
  20113. (let ((beg (max (point-min)
  20114. (org-element-property :contents-begin element)))
  20115. (end (min (point-max)
  20116. (org-element-property :contents-end element))))
  20117. ;; Do nothing if point is at an affiliated keyword.
  20118. (if (< (line-end-position) beg) t
  20119. (when (derived-mode-p 'message-mode)
  20120. ;; In `message-mode', do not fill following citation
  20121. ;; in current paragraph nor text before message body.
  20122. (let ((body-start (save-excursion (message-goto-body))))
  20123. (when body-start (setq beg (max body-start beg))))
  20124. (when (save-excursion
  20125. (re-search-forward
  20126. (concat "^" message-cite-prefix-regexp) end t))
  20127. (setq end (match-beginning 0))))
  20128. ;; Fill paragraph, taking line breaks into account.
  20129. ;; For that, slice the paragraph using line breaks as
  20130. ;; separators, and fill the parts in reverse order to
  20131. ;; avoid messing with markers.
  20132. (save-excursion
  20133. (goto-char end)
  20134. (mapc
  20135. (lambda (pos)
  20136. (fill-region-as-paragraph pos (point) justify)
  20137. (goto-char pos))
  20138. ;; Find the list of ending positions for line breaks
  20139. ;; in the current paragraph. Add paragraph
  20140. ;; beginning to include first slice.
  20141. (nreverse
  20142. (cons beg
  20143. (org-element-map
  20144. (org-element--parse-objects
  20145. beg end nil (org-element-restriction 'paragraph))
  20146. 'line-break
  20147. (lambda (lb) (org-element-property :end lb)))))))
  20148. t)))
  20149. ;; Contents of `comment-block' type elements should be
  20150. ;; filled as plain text, but only if point is within block
  20151. ;; markers.
  20152. (comment-block
  20153. (let* ((case-fold-search t)
  20154. (beg (save-excursion
  20155. (goto-char (org-element-property :begin element))
  20156. (re-search-forward "^[ \t]*#\\+begin_comment" nil t)
  20157. (forward-line)
  20158. (point)))
  20159. (end (save-excursion
  20160. (goto-char (org-element-property :end element))
  20161. (re-search-backward "^[ \t]*#\\+end_comment" nil t)
  20162. (line-beginning-position))))
  20163. (when (and (>= (point) beg) (< (point) end))
  20164. (fill-region-as-paragraph
  20165. (save-excursion
  20166. (end-of-line)
  20167. (re-search-backward "^[ \t]*$" beg 'move)
  20168. (line-beginning-position))
  20169. (save-excursion
  20170. (beginning-of-line)
  20171. (re-search-forward "^[ \t]*$" end 'move)
  20172. (line-beginning-position))
  20173. justify)))
  20174. t)
  20175. ;; Fill comments.
  20176. (comment (fill-comment-paragraph justify))
  20177. ;; Ignore every other element.
  20178. (otherwise t))))))
  20179. (defun org-auto-fill-function ()
  20180. "Auto-fill function."
  20181. ;; Check if auto-filling is meaningful.
  20182. (let ((fc (current-fill-column)))
  20183. (when (and fc (> (current-column) fc))
  20184. (let* ((fill-prefix (org-adaptive-fill-function))
  20185. ;; Enforce empty fill prefix, if required. Otherwise, it
  20186. ;; will be computed again.
  20187. (adaptive-fill-mode (not (equal fill-prefix ""))))
  20188. (when fill-prefix (do-auto-fill))))))
  20189. (defun org-comment-line-break-function (&optional soft)
  20190. "Break line at point and indent, continuing comment if within one.
  20191. The inserted newline is marked hard if variable
  20192. `use-hard-newlines' is true, unless optional argument SOFT is
  20193. non-nil."
  20194. (if soft (insert-and-inherit ?\n) (newline 1))
  20195. (save-excursion (forward-char -1) (delete-horizontal-space))
  20196. (delete-horizontal-space)
  20197. (indent-to-left-margin)
  20198. (insert-before-markers-and-inherit fill-prefix))
  20199. ;;; Comments
  20200. ;; Org comments syntax is quite complex. It requires the entire line
  20201. ;; to be just a comment. Also, even with the right syntax at the
  20202. ;; beginning of line, some some elements (i.e. verse-block or
  20203. ;; example-block) don't accept comments. Usual Emacs comment commands
  20204. ;; cannot cope with those requirements. Therefore, Org replaces them.
  20205. ;; Org still relies on `comment-dwim', but cannot trust
  20206. ;; `comment-only-p'. So, `comment-region-function' and
  20207. ;; `uncomment-region-function' both point
  20208. ;; to`org-comment-or-uncomment-region'. Eventually,
  20209. ;; `org-insert-comment' takes care of insertion of comments at the
  20210. ;; beginning of line.
  20211. ;; `org-setup-comments-handling' install comments related variables
  20212. ;; during `org-mode' initialization.
  20213. (defun org-setup-comments-handling ()
  20214. (interactive)
  20215. (org-set-local 'comment-use-syntax nil)
  20216. (org-set-local 'comment-start "# ")
  20217. (org-set-local 'comment-start-skip "^\\s-*#\\(?: \\|$\\)")
  20218. (org-set-local 'comment-insert-comment-function 'org-insert-comment)
  20219. (org-set-local 'comment-region-function 'org-comment-or-uncomment-region)
  20220. (org-set-local 'uncomment-region-function 'org-comment-or-uncomment-region))
  20221. (defun org-insert-comment ()
  20222. "Insert an empty comment above current line.
  20223. If the line is empty, insert comment at its beginning."
  20224. (beginning-of-line)
  20225. (if (looking-at "\\s-*$") (replace-match "") (open-line 1))
  20226. (org-indent-line)
  20227. (insert "# "))
  20228. (defvar comment-empty-lines) ; From newcomment.el.
  20229. (defun org-comment-or-uncomment-region (beg end &rest ignore)
  20230. "Comment or uncomment each non-blank line in the region.
  20231. Uncomment each non-blank line between BEG and END if it only
  20232. contains commented lines. Otherwise, comment them."
  20233. (save-restriction
  20234. ;; Restrict region
  20235. (narrow-to-region (save-excursion (goto-char beg)
  20236. (skip-chars-forward " \r\t\n" end)
  20237. (line-beginning-position))
  20238. (save-excursion (goto-char end)
  20239. (skip-chars-backward " \r\t\n" beg)
  20240. (line-end-position)))
  20241. (let ((uncommentp
  20242. ;; UNCOMMENTP is non-nil when every non blank line between
  20243. ;; BEG and END is a comment.
  20244. (save-excursion
  20245. (goto-char (point-min))
  20246. (while (and (not (eobp))
  20247. (let ((element (org-element-at-point)))
  20248. (and (eq (org-element-type element) 'comment)
  20249. (goto-char (min (point-max)
  20250. (org-element-property
  20251. :end element)))))))
  20252. (eobp))))
  20253. (if uncommentp
  20254. ;; Only blank lines and comments in region: uncomment it.
  20255. (save-excursion
  20256. (goto-char (point-min))
  20257. (while (not (eobp))
  20258. (when (looking-at "[ \t]*\\(#\\(?: \\|$\\)\\)")
  20259. (replace-match "" nil nil nil 1))
  20260. (forward-line)))
  20261. ;; Comment each line in region.
  20262. (let ((min-indent (point-max)))
  20263. ;; First find the minimum indentation across all lines.
  20264. (save-excursion
  20265. (goto-char (point-min))
  20266. (while (and (not (eobp)) (not (zerop min-indent)))
  20267. (unless (looking-at "[ \t]*$")
  20268. (setq min-indent (min min-indent (current-indentation))))
  20269. (forward-line)))
  20270. ;; Then loop over all lines.
  20271. (save-excursion
  20272. (goto-char (point-min))
  20273. (while (not (eobp))
  20274. (unless (and (not comment-empty-lines) (looking-at "[ \t]*$"))
  20275. (org-move-to-column min-indent t)
  20276. (insert comment-start))
  20277. (forward-line))))))))
  20278. ;;; Planning
  20279. ;; This section contains tools to operate on timestamp objects, as
  20280. ;; returned by, e.g. `org-element-context'.
  20281. (defun org-timestamp-has-time-p (timestamp)
  20282. "Non-nil when TIMESTAMP has a time specified."
  20283. (org-element-property :hour-start timestamp))
  20284. (defun org-timestamp-format (timestamp format &optional end utc)
  20285. "Format a TIMESTAMP element into a string.
  20286. FORMAT is a format specifier to be passed to
  20287. `format-time-string'.
  20288. When optional argument END is non-nil, use end of date-range or
  20289. time-range, if possible.
  20290. When optional argument UTC is non-nil, time will be expressed as
  20291. Universal Time."
  20292. (format-time-string
  20293. format
  20294. (apply 'encode-time
  20295. (cons 0
  20296. (mapcar
  20297. (lambda (prop) (or (org-element-property prop timestamp) 0))
  20298. (if end '(:minute-end :hour-end :day-end :month-end :year-end)
  20299. '(:minute-start :hour-start :day-start :month-start
  20300. :year-start)))))
  20301. utc))
  20302. (defun org-timestamp-split-range (timestamp &optional end)
  20303. "Extract a timestamp object from a date or time range.
  20304. TIMESTAMP is a timestamp object. END, when non-nil, means extract
  20305. the end of the range. Otherwise, extract its start.
  20306. Return a new timestamp object sharing the same parent as
  20307. TIMESTAMP."
  20308. (let ((type (org-element-property :type timestamp)))
  20309. (if (memq type '(active inactive diary)) timestamp
  20310. (let ((split-ts (list 'timestamp (copy-sequence (nth 1 timestamp)))))
  20311. ;; Set new type.
  20312. (org-element-put-property
  20313. split-ts :type (if (eq type 'active-range) 'active 'inactive))
  20314. ;; Copy start properties over end properties if END is
  20315. ;; non-nil. Otherwise, copy end properties over `start' ones.
  20316. (let ((p-alist '((:minute-start . :minute-end)
  20317. (:hour-start . :hour-end)
  20318. (:day-start . :day-end)
  20319. (:month-start . :month-end)
  20320. (:year-start . :year-end))))
  20321. (dolist (p-cell p-alist)
  20322. (org-element-put-property
  20323. split-ts
  20324. (funcall (if end 'car 'cdr) p-cell)
  20325. (org-element-property
  20326. (funcall (if end 'cdr 'car) p-cell) split-ts)))
  20327. ;; Eventually refresh `:raw-value'.
  20328. (org-element-put-property split-ts :raw-value nil)
  20329. (org-element-put-property
  20330. split-ts :raw-value (org-element-interpret-data split-ts)))))))
  20331. (defun org-timestamp-translate (timestamp &optional boundary)
  20332. "Apply `org-translate-time' on a TIMESTAMP object.
  20333. When optional argument BOUNDARY is non-nil, it is either the
  20334. symbol `start' or `end'. In this case, only translate the
  20335. starting or ending part of TIMESTAMP if it is a date or time
  20336. range. Otherwise, translate both parts."
  20337. (if (and (not boundary)
  20338. (memq (org-element-property :type timestamp)
  20339. '(active-range inactive-range)))
  20340. (concat
  20341. (org-translate-time
  20342. (org-element-property :raw-value
  20343. (org-timestamp-split-range timestamp)))
  20344. "--"
  20345. (org-translate-time
  20346. (org-element-property :raw-value
  20347. (org-timestamp-split-range timestamp t))))
  20348. (org-translate-time
  20349. (org-element-property
  20350. :raw-value
  20351. (if (not boundary) timestamp
  20352. (org-timestamp-split-range timestamp (eq boundary 'end)))))))
  20353. ;;; Other stuff.
  20354. (defun org-toggle-fixed-width-section (arg)
  20355. "Toggle the fixed-width export.
  20356. If there is no active region, the QUOTE keyword at the current headline is
  20357. inserted or removed. When present, it causes the text between this headline
  20358. and the next to be exported as fixed-width text, and unmodified.
  20359. If there is an active region, this command adds or removes a colon as the
  20360. first character of this line. If the first character of a line is a colon,
  20361. this line is also exported in fixed-width font."
  20362. (interactive "P")
  20363. (let* ((cc 0)
  20364. (regionp (org-region-active-p))
  20365. (beg (if regionp (region-beginning) (point)))
  20366. (end (if regionp (region-end)))
  20367. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  20368. (case-fold-search nil)
  20369. (re "[ \t]*\\(:\\(?: \\|$\\)\\)")
  20370. off)
  20371. (if regionp
  20372. (save-excursion
  20373. (goto-char beg)
  20374. (setq cc (current-column))
  20375. (beginning-of-line 1)
  20376. (setq off (looking-at re))
  20377. (while (> nlines 0)
  20378. (setq nlines (1- nlines))
  20379. (beginning-of-line 1)
  20380. (cond
  20381. (arg
  20382. (org-move-to-column cc t)
  20383. (insert ": \n")
  20384. (forward-line -1))
  20385. ((and off (looking-at re))
  20386. (replace-match "" t t nil 1))
  20387. ((not off) (org-move-to-column cc t) (insert ": ")))
  20388. (forward-line 1)))
  20389. (save-excursion
  20390. (org-back-to-heading)
  20391. (cond
  20392. ((looking-at (format org-heading-keyword-regexp-format
  20393. org-quote-string))
  20394. (goto-char (match-end 1))
  20395. (looking-at (concat " +" org-quote-string))
  20396. (replace-match "" t t)
  20397. (when (eolp) (insert " ")))
  20398. ((looking-at org-outline-regexp)
  20399. (goto-char (match-end 0))
  20400. (insert org-quote-string " ")))))))
  20401. (defun org-reftex-citation ()
  20402. "Use reftex-citation to insert a citation into the buffer.
  20403. This looks for a line like
  20404. #+BIBLIOGRAPHY: foo plain option:-d
  20405. and derives from it that foo.bib is the bibliography file relevant
  20406. for this document. It then installs the necessary environment for RefTeX
  20407. to work in this buffer and calls `reftex-citation' to insert a citation
  20408. into the buffer.
  20409. Export of such citations to both LaTeX and HTML is handled by the contributed
  20410. package org-exp-bibtex by Taru Karttunen."
  20411. (interactive)
  20412. (let ((reftex-docstruct-symbol 'rds)
  20413. (reftex-cite-format "\\cite{%l}")
  20414. rds bib)
  20415. (save-excursion
  20416. (save-restriction
  20417. (widen)
  20418. (let ((case-fold-search t)
  20419. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  20420. (if (not (save-excursion
  20421. (or (re-search-forward re nil t)
  20422. (re-search-backward re nil t))))
  20423. (error "No bibliography defined in file")
  20424. (setq bib (concat (match-string 1) ".bib")
  20425. rds (list (list 'bib bib)))))))
  20426. (call-interactively 'reftex-citation)))
  20427. ;;;; Functions extending outline functionality
  20428. (defun org-beginning-of-line (&optional arg)
  20429. "Go to the beginning of the current line. If that is invisible, continue
  20430. to a visible line beginning. This makes the function of C-a more intuitive.
  20431. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  20432. first attempt, and only move to after the tags when the cursor is already
  20433. beyond the end of the headline."
  20434. (interactive "P")
  20435. (let ((pos (point))
  20436. (special (if (consp org-special-ctrl-a/e)
  20437. (car org-special-ctrl-a/e)
  20438. org-special-ctrl-a/e))
  20439. refpos)
  20440. (if (org-bound-and-true-p visual-line-mode)
  20441. (beginning-of-visual-line 1)
  20442. (beginning-of-line 1))
  20443. (if (and arg (fboundp 'move-beginning-of-line))
  20444. (call-interactively 'move-beginning-of-line)
  20445. (if (bobp)
  20446. nil
  20447. (backward-char 1)
  20448. (if (org-truely-invisible-p)
  20449. (while (and (not (bobp)) (org-truely-invisible-p))
  20450. (backward-char 1)
  20451. (beginning-of-line 1))
  20452. (forward-char 1))))
  20453. (when special
  20454. (cond
  20455. ((and (looking-at org-complex-heading-regexp)
  20456. (= (char-after (match-end 1)) ?\ ))
  20457. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  20458. (point-at-eol)))
  20459. (goto-char
  20460. (if (eq special t)
  20461. (cond ((> pos refpos) refpos)
  20462. ((= pos (point)) refpos)
  20463. (t (point)))
  20464. (cond ((> pos (point)) (point))
  20465. ((not (eq last-command this-command)) (point))
  20466. (t refpos)))))
  20467. ((org-at-item-p)
  20468. ;; Being at an item and not looking at an the item means point
  20469. ;; was previously moved to beginning of a visual line, which
  20470. ;; doesn't contain the item. Therefore, do nothing special,
  20471. ;; just stay here.
  20472. (when (looking-at org-list-full-item-re)
  20473. ;; Set special position at first white space character after
  20474. ;; bullet, and check-box, if any.
  20475. (let ((after-bullet
  20476. (let ((box (match-end 3)))
  20477. (if (not box) (match-end 1)
  20478. (let ((after (char-after box)))
  20479. (if (and after (= after ? )) (1+ box) box))))))
  20480. ;; Special case: Move point to special position when
  20481. ;; currently after it or at beginning of line.
  20482. (if (eq special t)
  20483. (when (or (> pos after-bullet) (= (point) pos))
  20484. (goto-char after-bullet))
  20485. ;; Reversed case: Move point to special position when
  20486. ;; point was already at beginning of line and command is
  20487. ;; repeated.
  20488. (when (and (= (point) pos) (eq last-command this-command))
  20489. (goto-char after-bullet))))))))
  20490. (org-no-warnings
  20491. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  20492. (defun org-end-of-line (&optional arg)
  20493. "Go to the end of the line.
  20494. If this is a headline, and `org-special-ctrl-a/e' is set, ignore
  20495. tags on the first attempt, and only move to after the tags when
  20496. the cursor is already beyond the end of the headline."
  20497. (interactive "P")
  20498. (let ((special (if (consp org-special-ctrl-a/e) (cdr org-special-ctrl-a/e)
  20499. org-special-ctrl-a/e))
  20500. (move-fun (cond ((org-bound-and-true-p visual-line-mode)
  20501. 'end-of-visual-line)
  20502. ((fboundp 'move-end-of-line) 'move-end-of-line)
  20503. (t 'end-of-line))))
  20504. (if (or (not special) arg) (call-interactively move-fun)
  20505. (let* ((element (save-excursion (beginning-of-line)
  20506. (org-element-at-point)))
  20507. (type (org-element-type element)))
  20508. (cond
  20509. ((memq type '(headline inlinetask))
  20510. (let ((pos (point)))
  20511. (beginning-of-line 1)
  20512. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  20513. (if (eq special t)
  20514. (if (or (< pos (match-beginning 1)) (= pos (match-end 0)))
  20515. (goto-char (match-beginning 1))
  20516. (goto-char (match-end 0)))
  20517. (if (or (< pos (match-end 0))
  20518. (not (eq this-command last-command)))
  20519. (goto-char (match-end 0))
  20520. (goto-char (match-beginning 1))))
  20521. (call-interactively move-fun))))
  20522. ((org-element-property :hiddenp element)
  20523. ;; If element is hidden, `move-end-of-line' would put point
  20524. ;; after it. Use `end-of-line' to stay on current line.
  20525. (call-interactively 'end-of-line))
  20526. (t (call-interactively move-fun)))))
  20527. (org-no-warnings (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  20528. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  20529. (define-key org-mode-map "\C-e" 'org-end-of-line)
  20530. (defun org-backward-sentence (&optional arg)
  20531. "Go to beginning of sentence, or beginning of table field.
  20532. This will call `backward-sentence' or `org-table-beginning-of-field',
  20533. depending on context."
  20534. (interactive "P")
  20535. (cond
  20536. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  20537. (t (call-interactively 'backward-sentence))))
  20538. (defun org-forward-sentence (&optional arg)
  20539. "Go to end of sentence, or end of table field.
  20540. This will call `forward-sentence' or `org-table-end-of-field',
  20541. depending on context."
  20542. (interactive "P")
  20543. (cond
  20544. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  20545. (t (call-interactively 'forward-sentence))))
  20546. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  20547. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  20548. (defun org-kill-line (&optional arg)
  20549. "Kill line, to tags or end of line."
  20550. (interactive "P")
  20551. (cond
  20552. ((or (not org-special-ctrl-k)
  20553. (bolp)
  20554. (not (org-at-heading-p)))
  20555. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  20556. org-ctrl-k-protect-subtree)
  20557. (if (or (eq org-ctrl-k-protect-subtree 'error)
  20558. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  20559. (error "C-k aborted - would kill hidden subtree")))
  20560. (call-interactively
  20561. (if (org-bound-and-true-p visual-line-mode) 'kill-visual-line 'kill-line)))
  20562. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  20563. (kill-region (point) (match-beginning 1))
  20564. (org-set-tags nil t))
  20565. (t (kill-region (point) (point-at-eol)))))
  20566. (define-key org-mode-map "\C-k" 'org-kill-line)
  20567. (defun org-yank (&optional arg)
  20568. "Yank. If the kill is a subtree, treat it specially.
  20569. This command will look at the current kill and check if is a single
  20570. subtree, or a series of subtrees[1]. If it passes the test, and if the
  20571. cursor is at the beginning of a line or after the stars of a currently
  20572. empty headline, then the yank is handled specially. How exactly depends
  20573. on the value of the following variables, both set by default.
  20574. org-yank-folded-subtrees
  20575. When set, the subtree(s) will be folded after insertion, but only
  20576. if doing so would now swallow text after the yanked text.
  20577. org-yank-adjusted-subtrees
  20578. When set, the subtree will be promoted or demoted in order to
  20579. fit into the local outline tree structure, which means that the level
  20580. will be adjusted so that it becomes the smaller one of the two
  20581. *visible* surrounding headings.
  20582. Any prefix to this command will cause `yank' to be called directly with
  20583. no special treatment. In particular, a simple \\[universal-argument] prefix \
  20584. will just
  20585. plainly yank the text as it is.
  20586. \[1] The test checks if the first non-white line is a heading
  20587. and if there are no other headings with fewer stars."
  20588. (interactive "P")
  20589. (org-yank-generic 'yank arg))
  20590. (defun org-yank-generic (command arg)
  20591. "Perform some yank-like command.
  20592. This function implements the behavior described in the `org-yank'
  20593. documentation. However, it has been generalized to work for any
  20594. interactive command with similar behavior."
  20595. ;; pretend to be command COMMAND
  20596. (setq this-command command)
  20597. (if arg
  20598. (call-interactively command)
  20599. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  20600. (and (org-kill-is-subtree-p)
  20601. (or (bolp)
  20602. (and (looking-at "[ \t]*$")
  20603. (string-match
  20604. "\\`\\*+\\'"
  20605. (buffer-substring (point-at-bol) (point)))))))
  20606. swallowp)
  20607. (cond
  20608. ((and subtreep org-yank-folded-subtrees)
  20609. (let ((beg (point))
  20610. end)
  20611. (if (and subtreep org-yank-adjusted-subtrees)
  20612. (org-paste-subtree nil nil 'for-yank)
  20613. (call-interactively command))
  20614. (setq end (point))
  20615. (goto-char beg)
  20616. (when (and (bolp) subtreep
  20617. (not (setq swallowp
  20618. (org-yank-folding-would-swallow-text beg end))))
  20619. (org-with-limited-levels
  20620. (or (looking-at org-outline-regexp)
  20621. (re-search-forward org-outline-regexp-bol end t))
  20622. (while (and (< (point) end) (looking-at org-outline-regexp))
  20623. (hide-subtree)
  20624. (org-cycle-show-empty-lines 'folded)
  20625. (condition-case nil
  20626. (outline-forward-same-level 1)
  20627. (error (goto-char end))))))
  20628. (when swallowp
  20629. (message
  20630. "Inserted text not folded because that would swallow text"))
  20631. (goto-char end)
  20632. (skip-chars-forward " \t\n\r")
  20633. (beginning-of-line 1)
  20634. (push-mark beg 'nomsg)))
  20635. ((and subtreep org-yank-adjusted-subtrees)
  20636. (let ((beg (point-at-bol)))
  20637. (org-paste-subtree nil nil 'for-yank)
  20638. (push-mark beg 'nomsg)))
  20639. (t
  20640. (call-interactively command))))))
  20641. (defun org-yank-folding-would-swallow-text (beg end)
  20642. "Would hide-subtree at BEG swallow any text after END?"
  20643. (let (level)
  20644. (org-with-limited-levels
  20645. (save-excursion
  20646. (goto-char beg)
  20647. (when (or (looking-at org-outline-regexp)
  20648. (re-search-forward org-outline-regexp-bol end t))
  20649. (setq level (org-outline-level)))
  20650. (goto-char end)
  20651. (skip-chars-forward " \t\r\n\v\f")
  20652. (if (or (eobp)
  20653. (and (bolp) (looking-at org-outline-regexp)
  20654. (<= (org-outline-level) level)))
  20655. nil ; Nothing would be swallowed
  20656. t))))) ; something would swallow
  20657. (define-key org-mode-map "\C-y" 'org-yank)
  20658. (defun org-truely-invisible-p ()
  20659. "Check if point is at a character currently not visible.
  20660. This version does not only check the character property, but also
  20661. `visible-mode'."
  20662. ;; Early versions of noutline don't have `outline-invisible-p'.
  20663. (if (org-bound-and-true-p visible-mode)
  20664. nil
  20665. (outline-invisible-p)))
  20666. (defun org-invisible-p2 ()
  20667. "Check if point is at a character currently not visible."
  20668. (save-excursion
  20669. (if (and (eolp) (not (bobp))) (backward-char 1))
  20670. ;; Early versions of noutline don't have `outline-invisible-p'.
  20671. (outline-invisible-p)))
  20672. (defun org-back-to-heading (&optional invisible-ok)
  20673. "Call `outline-back-to-heading', but provide a better error message."
  20674. (condition-case nil
  20675. (outline-back-to-heading invisible-ok)
  20676. (error (error "Before first headline at position %d in buffer %s"
  20677. (point) (current-buffer)))))
  20678. (defun org-before-first-heading-p ()
  20679. "Before first heading?"
  20680. (save-excursion
  20681. (end-of-line)
  20682. (null (re-search-backward org-outline-regexp-bol nil t))))
  20683. (defun org-at-heading-p (&optional ignored)
  20684. (outline-on-heading-p t))
  20685. ;; Compatibility alias with Org versions < 7.8.03
  20686. (defalias 'org-on-heading-p 'org-at-heading-p)
  20687. (defun org-at-comment-p nil
  20688. "Is cursor in a line starting with a # character?"
  20689. (save-excursion
  20690. (beginning-of-line)
  20691. (looking-at "^#")))
  20692. (defun org-at-drawer-p nil
  20693. "Is cursor at a drawer keyword?"
  20694. (save-excursion
  20695. (move-beginning-of-line 1)
  20696. (looking-at org-drawer-regexp)))
  20697. (defun org-at-block-p nil
  20698. "Is cursor at a block keyword?"
  20699. (save-excursion
  20700. (move-beginning-of-line 1)
  20701. (looking-at org-block-regexp)))
  20702. (defun org-point-at-end-of-empty-headline ()
  20703. "If point is at the end of an empty headline, return t, else nil.
  20704. If the heading only contains a TODO keyword, it is still still considered
  20705. empty."
  20706. (and (looking-at "[ \t]*$")
  20707. (when org-todo-line-regexp
  20708. (save-excursion
  20709. (beginning-of-line 1)
  20710. (let ((case-fold-search nil))
  20711. (looking-at org-todo-line-regexp)
  20712. (string= (match-string 3) ""))))))
  20713. (defun org-at-heading-or-item-p ()
  20714. (or (org-at-heading-p) (org-at-item-p)))
  20715. (defun org-at-target-p ()
  20716. (or (org-in-regexp org-radio-target-regexp)
  20717. (org-in-regexp org-target-regexp)))
  20718. ;; Compatibility alias with Org versions < 7.8.03
  20719. (defalias 'org-on-target-p 'org-at-target-p)
  20720. (defun org-up-heading-all (arg)
  20721. "Move to the heading line of which the present line is a subheading.
  20722. This function considers both visible and invisible heading lines.
  20723. With argument, move up ARG levels."
  20724. (if (fboundp 'outline-up-heading-all)
  20725. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  20726. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  20727. (defun org-up-heading-safe ()
  20728. "Move to the heading line of which the present line is a subheading.
  20729. This version will not throw an error. It will return the level of the
  20730. headline found, or nil if no higher level is found.
  20731. Also, this function will be a lot faster than `outline-up-heading',
  20732. because it relies on stars being the outline starters. This can really
  20733. make a significant difference in outlines with very many siblings."
  20734. (let (start-level re)
  20735. (org-back-to-heading t)
  20736. (setq start-level (funcall outline-level))
  20737. (if (equal start-level 1)
  20738. nil
  20739. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  20740. (if (re-search-backward re nil t)
  20741. (funcall outline-level)))))
  20742. (defun org-first-sibling-p ()
  20743. "Is this heading the first child of its parents?"
  20744. (interactive)
  20745. (let ((re org-outline-regexp-bol)
  20746. level l)
  20747. (unless (org-at-heading-p t)
  20748. (error "Not at a heading"))
  20749. (setq level (funcall outline-level))
  20750. (save-excursion
  20751. (if (not (re-search-backward re nil t))
  20752. t
  20753. (setq l (funcall outline-level))
  20754. (< l level)))))
  20755. (defun org-goto-sibling (&optional previous)
  20756. "Goto the next sibling, even if it is invisible.
  20757. When PREVIOUS is set, go to the previous sibling instead. Returns t
  20758. when a sibling was found. When none is found, return nil and don't
  20759. move point."
  20760. (let ((fun (if previous 're-search-backward 're-search-forward))
  20761. (pos (point))
  20762. (re org-outline-regexp-bol)
  20763. level l)
  20764. (when (condition-case nil (org-back-to-heading t) (error nil))
  20765. (setq level (funcall outline-level))
  20766. (catch 'exit
  20767. (or previous (forward-char 1))
  20768. (while (funcall fun re nil t)
  20769. (setq l (funcall outline-level))
  20770. (when (< l level) (goto-char pos) (throw 'exit nil))
  20771. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  20772. (goto-char pos)
  20773. nil))))
  20774. (defun org-show-siblings ()
  20775. "Show all siblings of the current headline."
  20776. (save-excursion
  20777. (while (org-goto-sibling) (org-flag-heading nil)))
  20778. (save-excursion
  20779. (while (org-goto-sibling 'previous)
  20780. (org-flag-heading nil))))
  20781. (defun org-goto-first-child ()
  20782. "Goto the first child, even if it is invisible.
  20783. Return t when a child was found. Otherwise don't move point and
  20784. return nil."
  20785. (let (level (pos (point)) (re org-outline-regexp-bol))
  20786. (when (condition-case nil (org-back-to-heading t) (error nil))
  20787. (setq level (outline-level))
  20788. (forward-char 1)
  20789. (if (and (re-search-forward re nil t) (> (outline-level) level))
  20790. (progn (goto-char (match-beginning 0)) t)
  20791. (goto-char pos) nil))))
  20792. (defun org-show-hidden-entry ()
  20793. "Show an entry where even the heading is hidden."
  20794. (save-excursion
  20795. (org-show-entry)))
  20796. (defun org-flag-heading (flag &optional entry)
  20797. "Flag the current heading. FLAG non-nil means make invisible.
  20798. When ENTRY is non-nil, show the entire entry."
  20799. (save-excursion
  20800. (org-back-to-heading t)
  20801. ;; Check if we should show the entire entry
  20802. (if entry
  20803. (progn
  20804. (org-show-entry)
  20805. (save-excursion
  20806. (and (outline-next-heading)
  20807. (org-flag-heading nil))))
  20808. (outline-flag-region (max (point-min) (1- (point)))
  20809. (save-excursion (outline-end-of-heading) (point))
  20810. flag))))
  20811. (defun org-get-next-sibling ()
  20812. "Move to next heading of the same level, and return point.
  20813. If there is no such heading, return nil.
  20814. This is like outline-next-sibling, but invisible headings are ok."
  20815. (let ((level (funcall outline-level)))
  20816. (outline-next-heading)
  20817. (while (and (not (eobp)) (> (funcall outline-level) level))
  20818. (outline-next-heading))
  20819. (if (or (eobp) (< (funcall outline-level) level))
  20820. nil
  20821. (point))))
  20822. (defun org-get-last-sibling ()
  20823. "Move to previous heading of the same level, and return point.
  20824. If there is no such heading, return nil."
  20825. (let ((opoint (point))
  20826. (level (funcall outline-level)))
  20827. (outline-previous-heading)
  20828. (when (and (/= (point) opoint) (outline-on-heading-p t))
  20829. (while (and (> (funcall outline-level) level)
  20830. (not (bobp)))
  20831. (outline-previous-heading))
  20832. (if (< (funcall outline-level) level)
  20833. nil
  20834. (point)))))
  20835. (defun org-end-of-subtree (&optional invisible-ok to-heading)
  20836. "Goto to the end of a subtree."
  20837. ;; This contains an exact copy of the original function, but it uses
  20838. ;; `org-back-to-heading', to make it work also in invisible
  20839. ;; trees. And is uses an invisible-ok argument.
  20840. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  20841. ;; Furthermore, when used inside Org, finding the end of a large subtree
  20842. ;; with many children and grandchildren etc, this can be much faster
  20843. ;; than the outline version.
  20844. (org-back-to-heading invisible-ok)
  20845. (let ((first t)
  20846. (level (funcall outline-level)))
  20847. (if (and (derived-mode-p 'org-mode) (< level 1000))
  20848. ;; A true heading (not a plain list item), in Org-mode
  20849. ;; This means we can easily find the end by looking
  20850. ;; only for the right number of stars. Using a regexp to do
  20851. ;; this is so much faster than using a Lisp loop.
  20852. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  20853. (forward-char 1)
  20854. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  20855. ;; something else, do it the slow way
  20856. (while (and (not (eobp))
  20857. (or first (> (funcall outline-level) level)))
  20858. (setq first nil)
  20859. (outline-next-heading)))
  20860. (unless to-heading
  20861. (if (memq (preceding-char) '(?\n ?\^M))
  20862. (progn
  20863. ;; Go to end of line before heading
  20864. (forward-char -1)
  20865. (if (memq (preceding-char) '(?\n ?\^M))
  20866. ;; leave blank line before heading
  20867. (forward-char -1))))))
  20868. (point))
  20869. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  20870. "Use Org version in org-mode, for dramatic speed-up."
  20871. (if (derived-mode-p 'org-mode)
  20872. (progn
  20873. (org-end-of-subtree nil t)
  20874. (unless (eobp) (backward-char 1)))
  20875. ad-do-it))
  20876. (defun org-end-of-meta-data-and-drawers ()
  20877. "Jump to the first text after meta data and drawers in the current entry.
  20878. This will move over empty lines, lines with planning time stamps,
  20879. clocking lines, and drawers."
  20880. (org-back-to-heading t)
  20881. (let ((end (save-excursion (outline-next-heading) (point)))
  20882. (re (concat "\\(" org-drawer-regexp "\\)"
  20883. "\\|" "[ \t]*" org-keyword-time-regexp)))
  20884. (forward-line 1)
  20885. (while (re-search-forward re end t)
  20886. (if (not (match-end 1))
  20887. ;; empty or planning line
  20888. (forward-line 1)
  20889. ;; a drawer, find the end
  20890. (re-search-forward "^[ \t]*:END:" end 'move)
  20891. (forward-line 1)))
  20892. (and (re-search-forward "[^\n]" nil t) (backward-char 1))
  20893. (point)))
  20894. (defun org-forward-heading-same-level (arg &optional invisible-ok)
  20895. "Move forward to the ARG'th subheading at same level as this one.
  20896. Stop at the first and last subheadings of a superior heading.
  20897. Normally this only looks at visible headings, but when INVISIBLE-OK is
  20898. non-nil it will also look at invisible ones."
  20899. (interactive "p")
  20900. (if (not (ignore-errors (org-back-to-heading invisible-ok)))
  20901. (if (and arg (< arg 0))
  20902. (goto-char (point-min))
  20903. (outline-next-heading))
  20904. (org-at-heading-p)
  20905. (let ((level (- (match-end 0) (match-beginning 0) 1))
  20906. (f (if (and arg (< arg 0))
  20907. 're-search-backward
  20908. 're-search-forward))
  20909. (count (if arg (abs arg) 1))
  20910. (result (point)))
  20911. (forward-char (if (and arg (< arg 0)) -1 1))
  20912. (while (and (> count 0)
  20913. (funcall f org-outline-regexp-bol nil 'move))
  20914. (let ((l (- (match-end 0) (match-beginning 0) 1)))
  20915. (cond ((< l level) (setq count 0))
  20916. ((and (= l level)
  20917. (or invisible-ok
  20918. (progn
  20919. (goto-char (line-beginning-position))
  20920. (not (outline-invisible-p)))))
  20921. (setq count (1- count))
  20922. (when (eq l level)
  20923. (setq result (point)))))))
  20924. (goto-char result))
  20925. (beginning-of-line 1)))
  20926. (defun org-backward-heading-same-level (arg &optional invisible-ok)
  20927. "Move backward to the ARG'th subheading at same level as this one.
  20928. Stop at the first and last subheadings of a superior heading."
  20929. (interactive "p")
  20930. (org-forward-heading-same-level (if arg (- arg) -1) invisible-ok))
  20931. (defun org-next-block (arg &optional backward block-regexp)
  20932. "Jump to the next block.
  20933. With a prefix argument ARG, jump forward ARG many source blocks.
  20934. When BACKWARD is non-nil, jump to the previous block.
  20935. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  20936. (interactive "p")
  20937. (let ((re (or block-regexp org-block-regexp))
  20938. (re-search-fn (or (and backward 're-search-backward)
  20939. 're-search-forward)))
  20940. (if (looking-at re) (forward-char 1))
  20941. (condition-case nil
  20942. (funcall re-search-fn re nil nil arg)
  20943. (error (error "No %s code blocks" (if backward "previous" "further" ))))
  20944. (goto-char (match-beginning 0)) (org-show-context)))
  20945. (defun org-previous-block (arg &optional block-regexp)
  20946. "Jump to the previous block.
  20947. With a prefix argument ARG, jump backward ARG many source blocks.
  20948. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  20949. (interactive "p")
  20950. (org-next-block arg t block-regexp))
  20951. (defun org-forward-element ()
  20952. "Move forward by one element.
  20953. Move to the next element at the same level, when possible."
  20954. (interactive)
  20955. (cond ((eobp) (user-error "Cannot move further down"))
  20956. ((org-with-limited-levels (org-at-heading-p))
  20957. (let ((origin (point)))
  20958. (goto-char (org-end-of-subtree nil t))
  20959. (unless (org-with-limited-levels (org-at-heading-p))
  20960. (goto-char origin)
  20961. (user-error "Cannot move further down"))))
  20962. (t
  20963. (let* ((elem (org-element-at-point))
  20964. (end (org-element-property :end elem))
  20965. (parent (org-element-property :parent elem)))
  20966. (if (and parent (= (org-element-property :contents-end parent) end))
  20967. (goto-char (org-element-property :end parent))
  20968. (goto-char end))))))
  20969. (defun org-backward-element ()
  20970. "Move backward by one element.
  20971. Move to the previous element at the same level, when possible."
  20972. (interactive)
  20973. (cond ((bobp) (user-error "Cannot move further up"))
  20974. ((org-with-limited-levels (org-at-heading-p))
  20975. ;; At a headline, move to the previous one, if any, or stay
  20976. ;; here.
  20977. (let ((origin (point)))
  20978. (org-with-limited-levels (org-backward-heading-same-level 1))
  20979. ;; When current headline has no sibling above, move to its
  20980. ;; parent.
  20981. (when (= (point) origin)
  20982. (or (org-with-limited-levels (org-up-heading-safe))
  20983. (progn (goto-char origin)
  20984. (user-error "Cannot move further up"))))))
  20985. (t
  20986. (let* ((trail (org-element-at-point 'keep-trail))
  20987. (elem (car trail))
  20988. (prev-elem (nth 1 trail))
  20989. (beg (org-element-property :begin elem)))
  20990. (cond
  20991. ;; Move to beginning of current element if point isn't
  20992. ;; there already.
  20993. ((/= (point) beg) (goto-char beg))
  20994. (prev-elem (goto-char (org-element-property :begin prev-elem)))
  20995. ((org-before-first-heading-p) (goto-char (point-min)))
  20996. (t (org-back-to-heading)))))))
  20997. (defun org-up-element ()
  20998. "Move to upper element."
  20999. (interactive)
  21000. (if (org-with-limited-levels (org-at-heading-p))
  21001. (unless (org-up-heading-safe) (error "No surrounding element"))
  21002. (let* ((elem (org-element-at-point))
  21003. (parent (org-element-property :parent elem)))
  21004. (if parent (goto-char (org-element-property :begin parent))
  21005. (if (org-with-limited-levels (org-before-first-heading-p))
  21006. (error "No surrounding element")
  21007. (org-with-limited-levels (org-back-to-heading)))))))
  21008. (defvar org-element-greater-elements)
  21009. (defun org-down-element ()
  21010. "Move to inner element."
  21011. (interactive)
  21012. (let ((element (org-element-at-point)))
  21013. (cond
  21014. ((memq (org-element-type element) '(plain-list table))
  21015. (goto-char (org-element-property :contents-begin element))
  21016. (forward-char))
  21017. ((memq (org-element-type element) org-element-greater-elements)
  21018. ;; If contents are hidden, first disclose them.
  21019. (when (org-element-property :hiddenp element) (org-cycle))
  21020. (goto-char (or (org-element-property :contents-begin element)
  21021. (error "No content for this element"))))
  21022. (t (error "No inner element")))))
  21023. (defun org-drag-element-backward ()
  21024. "Move backward element at point."
  21025. (interactive)
  21026. (if (org-with-limited-levels (org-at-heading-p)) (org-move-subtree-up)
  21027. (let* ((trail (org-element-at-point 'keep-trail))
  21028. (elem (car trail))
  21029. (prev-elem (nth 1 trail)))
  21030. ;; Error out if no previous element or previous element is
  21031. ;; a parent of the current one.
  21032. (if (or (not prev-elem) (org-element-nested-p elem prev-elem))
  21033. (error "Cannot drag element backward")
  21034. (let ((pos (point)))
  21035. (org-element-swap-A-B prev-elem elem)
  21036. (goto-char (+ (org-element-property :begin prev-elem)
  21037. (- pos (org-element-property :begin elem)))))))))
  21038. (defun org-drag-element-forward ()
  21039. "Move forward element at point."
  21040. (interactive)
  21041. (let* ((pos (point))
  21042. (elem (org-element-at-point)))
  21043. (when (= (point-max) (org-element-property :end elem))
  21044. (error "Cannot drag element forward"))
  21045. (goto-char (org-element-property :end elem))
  21046. (let ((next-elem (org-element-at-point)))
  21047. (when (or (org-element-nested-p elem next-elem)
  21048. (and (eq (org-element-type next-elem) 'headline)
  21049. (not (eq (org-element-type elem) 'headline))))
  21050. (goto-char pos)
  21051. (error "Cannot drag element forward"))
  21052. ;; Compute new position of point: it's shifted by NEXT-ELEM
  21053. ;; body's length (without final blanks) and by the length of
  21054. ;; blanks between ELEM and NEXT-ELEM.
  21055. (let ((size-next (- (save-excursion
  21056. (goto-char (org-element-property :end next-elem))
  21057. (skip-chars-backward " \r\t\n")
  21058. (forward-line)
  21059. ;; Small correction if buffer doesn't end
  21060. ;; with a newline character.
  21061. (if (and (eolp) (not (bolp))) (1+ (point)) (point)))
  21062. (org-element-property :begin next-elem)))
  21063. (size-blank (- (org-element-property :end elem)
  21064. (save-excursion
  21065. (goto-char (org-element-property :end elem))
  21066. (skip-chars-backward " \r\t\n")
  21067. (forward-line)
  21068. (point)))))
  21069. (org-element-swap-A-B elem next-elem)
  21070. (goto-char (+ pos size-next size-blank))))))
  21071. (defun org-drag-line-forward (arg)
  21072. "Drag the line at point ARG lines forward."
  21073. (interactive "p")
  21074. (dotimes (n (abs arg))
  21075. (let ((c (current-column)))
  21076. (if (< 0 arg)
  21077. (progn
  21078. (beginning-of-line 2)
  21079. (transpose-lines 1)
  21080. (beginning-of-line 0))
  21081. (transpose-lines 1)
  21082. (beginning-of-line -1))
  21083. (org-move-to-column c))))
  21084. (defun org-drag-line-backward (arg)
  21085. "Drag the line at point ARG lines backward."
  21086. (interactive "p")
  21087. (org-drag-line-forward (- arg)))
  21088. (defun org-mark-element ()
  21089. "Put point at beginning of this element, mark at end.
  21090. Interactively, if this command is repeated or (in Transient Mark
  21091. mode) if the mark is active, it marks the next element after the
  21092. ones already marked."
  21093. (interactive)
  21094. (let (deactivate-mark)
  21095. (if (and (org-called-interactively-p 'any)
  21096. (or (and (eq last-command this-command) (mark t))
  21097. (and transient-mark-mode mark-active)))
  21098. (set-mark
  21099. (save-excursion
  21100. (goto-char (mark))
  21101. (goto-char (org-element-property :end (org-element-at-point)))))
  21102. (let ((element (org-element-at-point)))
  21103. (end-of-line)
  21104. (push-mark (org-element-property :end element) t t)
  21105. (goto-char (org-element-property :begin element))))))
  21106. (defun org-narrow-to-element ()
  21107. "Narrow buffer to current element."
  21108. (interactive)
  21109. (let ((elem (org-element-at-point)))
  21110. (cond
  21111. ((eq (car elem) 'headline)
  21112. (narrow-to-region
  21113. (org-element-property :begin elem)
  21114. (org-element-property :end elem)))
  21115. ((memq (car elem) org-element-greater-elements)
  21116. (narrow-to-region
  21117. (org-element-property :contents-begin elem)
  21118. (org-element-property :contents-end elem)))
  21119. (t
  21120. (narrow-to-region
  21121. (org-element-property :begin elem)
  21122. (org-element-property :end elem))))))
  21123. (defun org-transpose-element ()
  21124. "Transpose current and previous elements, keeping blank lines between.
  21125. Point is moved after both elements."
  21126. (interactive)
  21127. (org-skip-whitespace)
  21128. (let ((end (org-element-property :end (org-element-at-point))))
  21129. (org-drag-element-backward)
  21130. (goto-char end)))
  21131. (defun org-unindent-buffer ()
  21132. "Un-indent the visible part of the buffer.
  21133. Relative indentation (between items, inside blocks, etc.) isn't
  21134. modified."
  21135. (interactive)
  21136. (unless (eq major-mode 'org-mode)
  21137. (error "Cannot un-indent a buffer not in Org mode"))
  21138. (let* ((parse-tree (org-element-parse-buffer 'greater-element))
  21139. unindent-tree ; For byte-compiler.
  21140. (unindent-tree
  21141. (function
  21142. (lambda (contents)
  21143. (mapc
  21144. (lambda (element)
  21145. (if (memq (org-element-type element) '(headline section))
  21146. (funcall unindent-tree (org-element-contents element))
  21147. (save-excursion
  21148. (save-restriction
  21149. (narrow-to-region
  21150. (org-element-property :begin element)
  21151. (org-element-property :end element))
  21152. (org-do-remove-indentation)))))
  21153. (reverse contents))))))
  21154. (funcall unindent-tree (org-element-contents parse-tree))))
  21155. (defun org-show-subtree ()
  21156. "Show everything after this heading at deeper levels."
  21157. (interactive)
  21158. (outline-flag-region
  21159. (point)
  21160. (save-excursion
  21161. (org-end-of-subtree t t))
  21162. nil))
  21163. (defun org-show-entry ()
  21164. "Show the body directly following this heading.
  21165. Show the heading too, if it is currently invisible."
  21166. (interactive)
  21167. (save-excursion
  21168. (condition-case nil
  21169. (progn
  21170. (org-back-to-heading t)
  21171. (outline-flag-region
  21172. (max (point-min) (1- (point)))
  21173. (save-excursion
  21174. (if (re-search-forward
  21175. (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
  21176. (match-beginning 1)
  21177. (point-max)))
  21178. nil)
  21179. (org-cycle-hide-drawers 'children))
  21180. (error nil))))
  21181. (defun org-make-options-regexp (kwds &optional extra)
  21182. "Make a regular expression for keyword lines."
  21183. (concat
  21184. "^#\\+\\("
  21185. (mapconcat 'regexp-quote kwds "\\|")
  21186. (if extra (concat "\\|" extra))
  21187. "\\):[ \t]*\\(.*\\)"))
  21188. ;; Make isearch reveal the necessary context
  21189. (defun org-isearch-end ()
  21190. "Reveal context after isearch exits."
  21191. (when isearch-success ; only if search was successful
  21192. (if (featurep 'xemacs)
  21193. ;; Under XEmacs, the hook is run in the correct place,
  21194. ;; we directly show the context.
  21195. (org-show-context 'isearch)
  21196. ;; In Emacs the hook runs *before* restoring the overlays.
  21197. ;; So we have to use a one-time post-command-hook to do this.
  21198. ;; (Emacs 22 has a special variable, see function `org-mode')
  21199. (unless (and (boundp 'isearch-mode-end-hook-quit)
  21200. isearch-mode-end-hook-quit)
  21201. ;; Only when the isearch was not quitted.
  21202. (org-add-hook 'post-command-hook 'org-isearch-post-command
  21203. 'append 'local)))
  21204. (org-fix-ellipsis-at-bol)))
  21205. (defun org-isearch-post-command ()
  21206. "Remove self from hook, and show context."
  21207. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  21208. (org-show-context 'isearch))
  21209. ;;;; Integration with and fixes for other packages
  21210. ;;; Imenu support
  21211. (defvar org-imenu-markers nil
  21212. "All markers currently used by Imenu.")
  21213. (make-variable-buffer-local 'org-imenu-markers)
  21214. (defun org-imenu-new-marker (&optional pos)
  21215. "Return a new marker for use by Imenu, and remember the marker."
  21216. (let ((m (make-marker)))
  21217. (move-marker m (or pos (point)))
  21218. (push m org-imenu-markers)
  21219. m))
  21220. (defun org-imenu-get-tree ()
  21221. "Produce the index for Imenu."
  21222. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  21223. (setq org-imenu-markers nil)
  21224. (let* ((n org-imenu-depth)
  21225. (re (concat "^" (org-get-limited-outline-regexp)))
  21226. (subs (make-vector (1+ n) nil))
  21227. (last-level 0)
  21228. m level head0 head)
  21229. (save-excursion
  21230. (save-restriction
  21231. (widen)
  21232. (goto-char (point-max))
  21233. (while (re-search-backward re nil t)
  21234. (setq level (org-reduced-level (funcall outline-level)))
  21235. (when (and (<= level n)
  21236. (looking-at org-complex-heading-regexp)
  21237. (setq head0 (org-match-string-no-properties 4)))
  21238. (setq head (org-link-display-format head0)
  21239. m (org-imenu-new-marker))
  21240. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  21241. (if (>= level last-level)
  21242. (push (cons head m) (aref subs level))
  21243. (push (cons head (aref subs (1+ level))) (aref subs level))
  21244. (loop for i from (1+ level) to n do (aset subs i nil)))
  21245. (setq last-level level)))))
  21246. (aref subs 1)))
  21247. (eval-after-load "imenu"
  21248. '(progn
  21249. (add-hook 'imenu-after-jump-hook
  21250. (lambda ()
  21251. (if (derived-mode-p 'org-mode)
  21252. (org-show-context 'org-goto))))))
  21253. (defun org-link-display-format (link)
  21254. "Replace a link with either the description, or the link target
  21255. if no description is present"
  21256. (save-match-data
  21257. (if (string-match org-bracket-link-analytic-regexp link)
  21258. (replace-match (if (match-end 5)
  21259. (match-string 5 link)
  21260. (concat (match-string 1 link)
  21261. (match-string 3 link)))
  21262. nil t link)
  21263. link)))
  21264. (defun org-toggle-link-display ()
  21265. "Toggle the literal or descriptive display of links."
  21266. (interactive)
  21267. (if org-descriptive-links
  21268. (progn (org-remove-from-invisibility-spec '(org-link))
  21269. (org-restart-font-lock)
  21270. (setq org-descriptive-links nil))
  21271. (progn (add-to-invisibility-spec '(org-link))
  21272. (org-restart-font-lock)
  21273. (setq org-descriptive-links t))))
  21274. ;; Speedbar support
  21275. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  21276. "Overlay marking the agenda restriction line in speedbar.")
  21277. (overlay-put org-speedbar-restriction-lock-overlay
  21278. 'face 'org-agenda-restriction-lock)
  21279. (overlay-put org-speedbar-restriction-lock-overlay
  21280. 'help-echo "Agendas are currently limited to this item.")
  21281. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  21282. (defun org-speedbar-set-agenda-restriction ()
  21283. "Restrict future agenda commands to the location at point in speedbar.
  21284. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  21285. (interactive)
  21286. (require 'org-agenda)
  21287. (let (p m tp np dir txt)
  21288. (cond
  21289. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  21290. 'org-imenu t))
  21291. (setq m (get-text-property p 'org-imenu-marker))
  21292. (with-current-buffer (marker-buffer m)
  21293. (goto-char m)
  21294. (org-agenda-set-restriction-lock 'subtree)))
  21295. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  21296. 'speedbar-function 'speedbar-find-file))
  21297. (setq tp (previous-single-property-change
  21298. (1+ p) 'speedbar-function)
  21299. np (next-single-property-change
  21300. tp 'speedbar-function)
  21301. dir (speedbar-line-directory)
  21302. txt (buffer-substring-no-properties (or tp (point-min))
  21303. (or np (point-max))))
  21304. (with-current-buffer (find-file-noselect
  21305. (let ((default-directory dir))
  21306. (expand-file-name txt)))
  21307. (unless (derived-mode-p 'org-mode)
  21308. (error "Cannot restrict to non-Org-mode file"))
  21309. (org-agenda-set-restriction-lock 'file)))
  21310. (t (error "Don't know how to restrict Org-mode's agenda")))
  21311. (move-overlay org-speedbar-restriction-lock-overlay
  21312. (point-at-bol) (point-at-eol))
  21313. (setq current-prefix-arg nil)
  21314. (org-agenda-maybe-redo)))
  21315. (eval-after-load "speedbar"
  21316. '(progn
  21317. (speedbar-add-supported-extension ".org")
  21318. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  21319. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  21320. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  21321. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  21322. (add-hook 'speedbar-visiting-tag-hook
  21323. (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
  21324. ;;; Fixes and Hacks for problems with other packages
  21325. ;; Make flyspell not check words in links, to not mess up our keymap
  21326. (defvar org-element-affiliated-keywords) ; From org-element.el
  21327. (defvar org-element-block-name-alist) ; From org-element.el
  21328. (defun org-mode-flyspell-verify ()
  21329. "Don't let flyspell put overlays at active buttons, or on
  21330. {todo,all-time,additional-option-like}-keywords."
  21331. (let ((pos (max (1- (point)) (point-min)))
  21332. (word (thing-at-point 'word)))
  21333. (and (not (get-text-property pos 'keymap))
  21334. (not (get-text-property pos 'org-no-flyspell))
  21335. (not (member word org-todo-keywords-1))
  21336. (not (member word org-all-time-keywords))
  21337. (not (member word org-options-keywords))
  21338. (not (member word (mapcar 'car org-startup-options)))
  21339. (not (member-ignore-case word org-element-affiliated-keywords))
  21340. (not (member-ignore-case word (org-get-export-keywords)))
  21341. (not (member-ignore-case
  21342. word (mapcar 'car org-element-block-name-alist)))
  21343. (not (member-ignore-case word '("BEGIN" "END" "ATTR"))))))
  21344. (defun org-remove-flyspell-overlays-in (beg end)
  21345. "Remove flyspell overlays in region."
  21346. (and (org-bound-and-true-p flyspell-mode)
  21347. (fboundp 'flyspell-delete-region-overlays)
  21348. (flyspell-delete-region-overlays beg end))
  21349. (add-text-properties beg end '(org-no-flyspell t)))
  21350. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  21351. (eval-after-load "bookmark"
  21352. '(if (boundp 'bookmark-after-jump-hook)
  21353. ;; We can use the hook
  21354. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  21355. ;; Hook not available, use advice
  21356. (defadvice bookmark-jump (after org-make-visible activate)
  21357. "Make the position visible."
  21358. (org-bookmark-jump-unhide))))
  21359. ;; Make sure saveplace shows the location if it was hidden
  21360. (eval-after-load "saveplace"
  21361. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  21362. "Make the position visible."
  21363. (org-bookmark-jump-unhide)))
  21364. ;; Make sure ecb shows the location if it was hidden
  21365. (eval-after-load "ecb"
  21366. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  21367. "Make hierarchy visible when jumping into location from ECB tree buffer."
  21368. (if (derived-mode-p 'org-mode)
  21369. (org-show-context))))
  21370. (defun org-bookmark-jump-unhide ()
  21371. "Unhide the current position, to show the bookmark location."
  21372. (and (derived-mode-p 'org-mode)
  21373. (or (outline-invisible-p)
  21374. (save-excursion (goto-char (max (point-min) (1- (point))))
  21375. (outline-invisible-p)))
  21376. (org-show-context 'bookmark-jump)))
  21377. ;; Make session.el ignore our circular variable
  21378. (eval-after-load "session"
  21379. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  21380. ;;;; Experimental code
  21381. (defun org-closed-in-range ()
  21382. "Sparse tree of items closed in a certain time range.
  21383. Still experimental, may disappear in the future."
  21384. (interactive)
  21385. ;; Get the time interval from the user.
  21386. (let* ((time1 (org-float-time
  21387. (org-read-date nil 'to-time nil "Starting date: ")))
  21388. (time2 (org-float-time
  21389. (org-read-date nil 'to-time nil "End date:")))
  21390. ;; callback function
  21391. (callback (lambda ()
  21392. (let ((time
  21393. (org-float-time
  21394. (apply 'encode-time
  21395. (org-parse-time-string
  21396. (match-string 1))))))
  21397. ;; check if time in interval
  21398. (and (>= time time1) (<= time time2))))))
  21399. ;; make tree, check each match with the callback
  21400. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  21401. ;;;; Finish up
  21402. (provide 'org)
  21403. (run-hooks 'org-load-hook)
  21404. ;;; org.el ends here