org.el 881 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281
  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. (unless (boundp 'calendar-view-holidays-initially-flag)
  94. (org-defvaralias 'calendar-view-holidays-initially-flag
  95. 'view-calendar-holidays-initially))
  96. (unless (boundp 'calendar-view-diary-initially-flag)
  97. (org-defvaralias 'calendar-view-diary-initially-flag
  98. 'view-diary-entries-initially))
  99. (unless (boundp 'diary-fancy-buffer)
  100. (org-defvaralias 'diary-fancy-buffer 'fancy-diary-buffer))
  101. (declare-function org-inlinetask-at-task-p "org-inlinetask" ())
  102. (declare-function org-inlinetask-outline-regexp "org-inlinetask" ())
  103. (declare-function org-inlinetask-toggle-visibility "org-inlinetask" ())
  104. (declare-function org-pop-to-buffer-same-window "org-compat" (&optional buffer-or-name norecord label))
  105. (declare-function org-clock-get-last-clock-out-time "org-clock" ())
  106. (declare-function org-clock-timestamps-up "org-clock" (&optional n))
  107. (declare-function org-clock-timestamps-down "org-clock" (&optional n))
  108. (declare-function org-clock-sum-current-item "org-clock" (&optional tstart))
  109. (declare-function orgtbl-mode "org-table" (&optional arg))
  110. (declare-function org-clock-out "org-clock" (&optional switch-to-state fail-quietly at-time))
  111. (declare-function org-beamer-mode "ox-beamer" ())
  112. (declare-function org-table-edit-field "org-table" (arg))
  113. (declare-function org-table-justify-field-maybe "org-table" (&optional new))
  114. (declare-function org-id-get-create "org-id" (&optional force))
  115. (declare-function org-id-find-id-file "org-id" (id))
  116. (declare-function org-tags-view "org-agenda" (&optional todo-only match))
  117. (declare-function org-agenda-list "org-agenda" (&optional arg start-day span))
  118. (declare-function org-table-align "org-table" ())
  119. (declare-function org-table-paste-rectangle "org-table" ())
  120. (declare-function org-table-maybe-eval-formula "org-table" ())
  121. (declare-function org-table-maybe-recalculate-line "org-table" ())
  122. (declare-function org-element--parse-objects "org-element"
  123. (beg end acc restriction))
  124. (declare-function org-element-at-point "org-element" (&optional keep-trail))
  125. (declare-function org-element-contents "org-element" (element))
  126. (declare-function org-element-context "org-element" (&optional element))
  127. (declare-function org-element-interpret-data "org-element"
  128. (data &optional parent))
  129. (declare-function org-element-map "org-element"
  130. (data types fun &optional info first-match no-recursion))
  131. (declare-function org-element-nested-p "org-element" (elem-a elem-b))
  132. (declare-function org-element-parse-buffer "org-element"
  133. (&optional granularity visible-only))
  134. (declare-function org-element-property "org-element" (property element))
  135. (declare-function org-element-put-property "org-element"
  136. (element property value))
  137. (declare-function org-element-swap-A-B "org-element" (elem-a elem-b))
  138. (declare-function org-element--parse-objects "org-element"
  139. (beg end acc restriction))
  140. (declare-function org-element-parse-buffer "org-element"
  141. (&optional granularity visible-only))
  142. (declare-function org-element-restriction "org-element" (element))
  143. (declare-function org-element-type "org-element" (element))
  144. ;; load languages based on value of `org-babel-load-languages'
  145. (defvar org-babel-load-languages)
  146. ;;;###autoload
  147. (defun org-babel-do-load-languages (sym value)
  148. "Load the languages defined in `org-babel-load-languages'."
  149. (set-default sym value)
  150. (mapc (lambda (pair)
  151. (let ((active (cdr pair)) (lang (symbol-name (car pair))))
  152. (if active
  153. (progn
  154. (require (intern (concat "ob-" lang))))
  155. (progn
  156. (funcall 'fmakunbound
  157. (intern (concat "org-babel-execute:" lang)))
  158. (funcall 'fmakunbound
  159. (intern (concat "org-babel-expand-body:" lang)))))))
  160. org-babel-load-languages))
  161. (defcustom org-babel-load-languages '((emacs-lisp . t))
  162. "Languages which can be evaluated in Org-mode buffers.
  163. This list can be used to load support for any of the languages
  164. below, note that each language will depend on a different set of
  165. system executables and/or Emacs modes. When a language is
  166. \"loaded\", then code blocks in that language can be evaluated
  167. with `org-babel-execute-src-block' bound by default to C-c
  168. C-c (note the `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can
  169. be set to remove code block evaluation from the C-c C-c
  170. keybinding. By default only Emacs Lisp (which has no
  171. requirements) is loaded."
  172. :group 'org-babel
  173. :set 'org-babel-do-load-languages
  174. :version "24.1"
  175. :type '(alist :tag "Babel Languages"
  176. :key-type
  177. (choice
  178. (const :tag "Awk" awk)
  179. (const :tag "C" C)
  180. (const :tag "R" R)
  181. (const :tag "Asymptote" asymptote)
  182. (const :tag "Calc" calc)
  183. (const :tag "Clojure" clojure)
  184. (const :tag "CSS" css)
  185. (const :tag "Ditaa" ditaa)
  186. (const :tag "Dot" dot)
  187. (const :tag "Emacs Lisp" emacs-lisp)
  188. (const :tag "Fortran" fortran)
  189. (const :tag "Gnuplot" gnuplot)
  190. (const :tag "Haskell" haskell)
  191. (const :tag "IO" io)
  192. (const :tag "Java" java)
  193. (const :tag "Javascript" js)
  194. (const :tag "LaTeX" latex)
  195. (const :tag "Ledger" ledger)
  196. (const :tag "Lilypond" lilypond)
  197. (const :tag "Lisp" lisp)
  198. (const :tag "Makefile" makefile)
  199. (const :tag "Maxima" maxima)
  200. (const :tag "Matlab" matlab)
  201. (const :tag "Mscgen" mscgen)
  202. (const :tag "Ocaml" ocaml)
  203. (const :tag "Octave" octave)
  204. (const :tag "Org" org)
  205. (const :tag "Perl" perl)
  206. (const :tag "Pico Lisp" picolisp)
  207. (const :tag "PlantUML" plantuml)
  208. (const :tag "Python" python)
  209. (const :tag "Ruby" ruby)
  210. (const :tag "Sass" sass)
  211. (const :tag "Scala" scala)
  212. (const :tag "Scheme" scheme)
  213. (const :tag "Screen" screen)
  214. (const :tag "Shell Script" sh)
  215. (const :tag "Shen" shen)
  216. (const :tag "Sql" sql)
  217. (const :tag "Sqlite" sqlite))
  218. :value-type (boolean :tag "Activate" :value t)))
  219. ;;;; Customization variables
  220. (defcustom org-clone-delete-id nil
  221. "Remove ID property of clones of a subtree.
  222. When non-nil, clones of a subtree don't inherit the ID property.
  223. Otherwise they inherit the ID property with a new unique
  224. identifier."
  225. :type 'boolean
  226. :version "24.1"
  227. :group 'org-id)
  228. ;;; Version
  229. (org-check-version)
  230. ;;;###autoload
  231. (defun org-version (&optional here full message)
  232. "Show the org-mode version in the echo area.
  233. With prefix argument HERE, insert it at point.
  234. When FULL is non-nil, use a verbose version string.
  235. When MESSAGE is non-nil, display a message with the version."
  236. (interactive "P")
  237. (let* ((org-dir (ignore-errors (org-find-library-dir "org")))
  238. (save-load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  239. (load-suffixes (list ".el"))
  240. (org-install-dir (ignore-errors (org-find-library-dir "org-loaddefs")))
  241. (org-trash (or
  242. (and (fboundp 'org-release) (fboundp 'org-git-version))
  243. (org-load-noerror-mustsuffix (concat org-dir "org-version"))))
  244. (load-suffixes save-load-suffixes)
  245. (org-version (org-release))
  246. (git-version (org-git-version))
  247. (version (format "Org-mode version %s (%s @ %s)"
  248. org-version
  249. git-version
  250. (if org-install-dir
  251. (if (string= org-dir org-install-dir)
  252. org-install-dir
  253. (concat "mixed installation! " org-install-dir " and " org-dir))
  254. "org-loaddefs.el can not be found!")))
  255. (_version (if full version org-version)))
  256. (if (org-called-interactively-p 'interactive)
  257. (if here
  258. (insert version)
  259. (message version))
  260. (if message (message _version))
  261. _version)))
  262. (defconst org-version (org-version))
  263. ;;; Compatibility constants
  264. ;;; The custom variables
  265. (defgroup org nil
  266. "Outline-based notes management and organizer."
  267. :tag "Org"
  268. :group 'outlines
  269. :group 'calendar)
  270. (defcustom org-mode-hook nil
  271. "Mode hook for Org-mode, run after the mode was turned on."
  272. :group 'org
  273. :type 'hook)
  274. (defcustom org-load-hook nil
  275. "Hook that is run after org.el has been loaded."
  276. :group 'org
  277. :type 'hook)
  278. (defcustom org-log-buffer-setup-hook nil
  279. "Hook that is run after an Org log buffer is created."
  280. :group 'org
  281. :version "24.1"
  282. :type 'hook)
  283. (defvar org-modules) ; defined below
  284. (defvar org-modules-loaded nil
  285. "Have the modules been loaded already?")
  286. (defun org-load-modules-maybe (&optional force)
  287. "Load all extensions listed in `org-modules'."
  288. (when (or force (not org-modules-loaded))
  289. (mapc (lambda (ext)
  290. (condition-case nil (require ext)
  291. (error (message "Problems while trying to load feature `%s'" ext))))
  292. org-modules)
  293. (setq org-modules-loaded t)))
  294. (defun org-set-modules (var value)
  295. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  296. (set var value)
  297. (when (featurep 'org)
  298. (org-load-modules-maybe 'force)))
  299. (defcustom org-modules '(org-bbdb org-bibtex org-docview org-gnus org-info org-irc org-mhe org-rmail)
  300. "Modules that should always be loaded together with org.el.
  301. If a description starts with <C>, the file is not part of Emacs
  302. and loading it will require that you have downloaded and properly
  303. installed the Org mode distribution.
  304. You can also use this system to load external packages (i.e. neither Org
  305. core modules, nor modules from the CONTRIB directory). Just add symbols
  306. to the end of the list. If the package is called org-xyz.el, then you need
  307. to add the symbol `xyz', and the package must have a call to:
  308. \(provide 'org-xyz)
  309. For export specific modules, see also `org-export-backends'."
  310. :group 'org
  311. :set 'org-set-modules
  312. :version "24.4"
  313. :package-version '(Org . "8.0")
  314. :type
  315. '(set :greedy t
  316. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  317. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  318. (const :tag " crypt: Encryption of subtrees" org-crypt)
  319. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  320. (const :tag " docview: Links to doc-view buffers" org-docview)
  321. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  322. (const :tag " id: Global IDs for identifying entries" org-id)
  323. (const :tag " info: Links to Info nodes" org-info)
  324. (const :tag " habit: Track your consistency with habits" org-habit)
  325. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  326. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  327. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  328. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  329. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  330. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  331. (const :tag " mouse: Additional mouse support" org-mouse)
  332. (const :tag "C vm: Links to VM folders/messages" org-vm)
  333. (const :tag "C wl: Links to Wanderlust folders/messages" org-wl)
  334. (const :tag "C w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  335. (const :tag "C mew: Links to Mew folders/messages" org-mew)
  336. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  337. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  338. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  339. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  340. (const :tag "C collector: Collect properties into tables" org-collector)
  341. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  342. (const :tag "C drill: Flashcards and spaced repetition for Org-mode" org-drill)
  343. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  344. (const :tag "C eshell Support for links to working directories in eshell" org-eshell)
  345. (const :tag "C eval: Include command output as text" org-eval)
  346. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  347. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  348. (const :tag "C exp-bibtex: Export citations using BibTeX" org-exp-bibtex)
  349. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  350. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  351. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  352. (const :tag "C jira: Add a jira:ticket protocol to Org-mode" org-jira)
  353. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  354. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  355. (const :tag "C notmuch: Provide org links to notmuch searches or messages" org-notmuch)
  356. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  357. (const :tag "C mac-link-grabber Grab links and URLs from various Mac applications" org-mac-link-grabber)
  358. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  359. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  360. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  361. (const :tag "C registry: A registry for Org-mode links" org-registry)
  362. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  363. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  364. (const :tag "C secretary: Team management with org-mode" org-secretary)
  365. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  366. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  367. (const :tag "C track: Keep up with Org-mode development" org-track)
  368. (const :tag "C velocity Something like Notational Velocity for Org" org-velocity)
  369. (const :tag "C wikinodes: CamelCase wiki-like links" org-wikinodes)
  370. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  371. (defvar org-export-registered-backends) ; From ox.el
  372. (declare-function org-export-derived-backend-p "ox" (backend &rest backends))
  373. (defcustom org-export-backends '(ascii html icalendar latex)
  374. "List of export back-ends that should be always available.
  375. If a description starts with <C>, the file is not part of Emacs
  376. and loading it will require that you have downloaded and properly
  377. installed the Org mode distribution.
  378. Unlike to `org-modules', libraries in this list will not be
  379. loaded along with Org, but only once the export framework is
  380. needed.
  381. This variable needs to be set before org.el is loaded. If you
  382. need to make a change while Emacs is running, use the customize
  383. interface or run the following code, where VALUE stands for the
  384. new value of the variable, after updating it:
  385. \(progn
  386. \(setq org-export-registered-backends
  387. \(org-remove-if-not
  388. \(lambda (backend)
  389. \(or (memq backend val)
  390. \(catch 'parentp
  391. \(mapc
  392. \(lambda (b)
  393. \(and (org-export-derived-backend-p b (car backend))
  394. \(throw 'parentp t)))
  395. val)
  396. nil)))
  397. org-export-registered-backends))
  398. \(let ((new-list (mapcar 'car org-export-registered-backends)))
  399. \(dolist (backend val)
  400. \(cond
  401. \((not (load (format \"ox-%s\" backend) t t))
  402. \(message \"Problems while trying to load export back-end `%s'\"
  403. backend))
  404. \((not (memq backend new-list)) (push backend new-list))))
  405. \(set-default var new-list)))
  406. Adding a back-end to this list will also pull the back-end it
  407. depends on, if any."
  408. :group 'org
  409. :group 'org-export
  410. :version "24.4"
  411. :package-version '(Org . "8.0")
  412. :initialize 'custom-initialize-set
  413. :set (lambda (var val)
  414. (if (not (featurep 'ox)) (set-default var val)
  415. ;; Any back-end not required anymore (not present in VAL and not
  416. ;; a parent of any back-end in the new value) is removed from the
  417. ;; list of registered back-ends.
  418. (setq org-export-registered-backends
  419. (org-remove-if-not
  420. (lambda (backend)
  421. (or (memq backend val)
  422. (catch 'parentp
  423. (mapc
  424. (lambda (b)
  425. (and (org-export-derived-backend-p b (car backend))
  426. (throw 'parentp t)))
  427. val)
  428. nil)))
  429. org-export-registered-backends))
  430. ;; Now build NEW-LIST of both new back-ends and required
  431. ;; parents.
  432. (let ((new-list (mapcar 'car org-export-registered-backends)))
  433. (dolist (backend val)
  434. (cond
  435. ((not (load (format "ox-%s" backend) t t))
  436. (message "Problems while trying to load export back-end `%s'"
  437. backend))
  438. ((not (memq backend new-list)) (push backend new-list))))
  439. ;; Set VAR to that list with fixed dependencies.
  440. (set-default var new-list))))
  441. :type '(set :greedy t
  442. (const :tag " ascii Export buffer to ASCII format" ascii)
  443. (const :tag " beamer Export buffer to Beamer presentation" beamer)
  444. (const :tag " html Export buffer to HTML format" html)
  445. (const :tag " icalendar Export buffer to iCalendar format" icalendar)
  446. (const :tag " latex Export buffer to LaTeX format" latex)
  447. (const :tag " man Export buffer to MAN format" man)
  448. (const :tag " md Export buffer to Markdown format" md)
  449. (const :tag " odt Export buffer to ODT format" odt)
  450. (const :tag " texinfo Export buffer to Texinfo format" texinfo)
  451. (const :tag "C confluence Export buffer to Confluence Wiki format" confluence)
  452. (const :tag "C freemind Export buffer to Freemind mindmap format" freemind)
  453. (const :tag "C groff Export buffer to Groff format" groff)
  454. (const :tag "C koma-letter Export buffer to KOMA Scrlttrl2 format" koma-letter)
  455. (const :tag "C taskjuggler Export buffer to TaskJuggler format" taskjuggler)))
  456. (eval-after-load 'ox
  457. '(mapc
  458. (lambda (backend)
  459. (condition-case nil (require (intern (format "ox-%s" backend)))
  460. (error (message "Problems while trying to load export back-end `%s'"
  461. backend))))
  462. org-export-backends))
  463. (defcustom org-support-shift-select nil
  464. "Non-nil means make shift-cursor commands select text when possible.
  465. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
  466. start selecting a region, or enlarge regions started in this way.
  467. In Org-mode, in special contexts, these same keys are used for
  468. other purposes, important enough to compete with shift selection.
  469. Org tries to balance these needs by supporting `shift-select-mode'
  470. outside these special contexts, under control of this variable.
  471. The default of this variable is nil, to avoid confusing behavior. Shifted
  472. cursor keys will then execute Org commands in the following contexts:
  473. - on a headline, changing TODO state (left/right) and priority (up/down)
  474. - on a time stamp, changing the time
  475. - in a plain list item, changing the bullet type
  476. - in a property definition line, switching between allowed values
  477. - in the BEGIN line of a clock table (changing the time block).
  478. Outside these contexts, the commands will throw an error.
  479. When this variable is t and the cursor is not in a special
  480. context, Org-mode will support shift-selection for making and
  481. enlarging regions. To make this more effective, the bullet
  482. cycling will no longer happen anywhere in an item line, but only
  483. if the cursor is exactly on the bullet.
  484. If you set this variable to the symbol `always', then the keys
  485. will not be special in headlines, property lines, and item lines,
  486. to make shift selection work there as well. If this is what you
  487. want, you can use the following alternative commands: `C-c C-t'
  488. and `C-c ,' to change TODO state and priority, `C-u C-u C-c C-t'
  489. can be used to switch TODO sets, `C-c -' to cycle item bullet
  490. types, and properties can be edited by hand or in column view.
  491. However, when the cursor is on a timestamp, shift-cursor commands
  492. will still edit the time stamp - this is just too good to give up.
  493. XEmacs user should have this variable set to nil, because
  494. `shift-select-mode' is in Emacs 23 or later only."
  495. :group 'org
  496. :type '(choice
  497. (const :tag "Never" nil)
  498. (const :tag "When outside special context" t)
  499. (const :tag "Everywhere except timestamps" always)))
  500. (defcustom org-loop-over-headlines-in-active-region nil
  501. "Shall some commands act upon headlines in the active region?
  502. When set to `t', some commands will be performed in all headlines
  503. within the active region.
  504. When set to `start-level', some commands will be performed in all
  505. headlines within the active region, provided that these headlines
  506. are of the same level than the first one.
  507. When set to a string, those commands will be performed on the
  508. matching headlines within the active region. Such string must be
  509. a tags/property/todo match as it is used in the agenda tags view.
  510. The list of commands is: `org-schedule', `org-deadline',
  511. `org-todo', `org-archive-subtree', `org-archive-set-tag' and
  512. `org-archive-to-archive-sibling'. The archiving commands skip
  513. already archived entries."
  514. :type '(choice (const :tag "Don't loop" nil)
  515. (const :tag "All headlines in active region" t)
  516. (const :tag "In active region, headlines at the same level than the first one" 'start-level)
  517. (string :tag "Tags/Property/Todo matcher"))
  518. :version "24.1"
  519. :group 'org-todo
  520. :group 'org-archive)
  521. (defgroup org-startup nil
  522. "Options concerning startup of Org-mode."
  523. :tag "Org Startup"
  524. :group 'org)
  525. (defcustom org-startup-folded t
  526. "Non-nil means entering Org-mode will switch to OVERVIEW.
  527. This can also be configured on a per-file basis by adding one of
  528. the following lines anywhere in the buffer:
  529. #+STARTUP: fold (or `overview', this is equivalent)
  530. #+STARTUP: nofold (or `showall', this is equivalent)
  531. #+STARTUP: content
  532. #+STARTUP: showeverything
  533. By default, this option is ignored when Org opens agenda files
  534. for the first time. If you want the agenda to honor the startup
  535. option, set `org-agenda-inhibit-startup' to nil."
  536. :group 'org-startup
  537. :type '(choice
  538. (const :tag "nofold: show all" nil)
  539. (const :tag "fold: overview" t)
  540. (const :tag "content: all headlines" content)
  541. (const :tag "show everything, even drawers" showeverything)))
  542. (defcustom org-startup-truncated t
  543. "Non-nil means entering Org-mode will set `truncate-lines'.
  544. This is useful since some lines containing links can be very long and
  545. uninteresting. Also tables look terrible when wrapped."
  546. :group 'org-startup
  547. :type 'boolean)
  548. (defcustom org-startup-indented nil
  549. "Non-nil means turn on `org-indent-mode' on startup.
  550. This can also be configured on a per-file basis by adding one of
  551. the following lines anywhere in the buffer:
  552. #+STARTUP: indent
  553. #+STARTUP: noindent"
  554. :group 'org-structure
  555. :type '(choice
  556. (const :tag "Not" nil)
  557. (const :tag "Globally (slow on startup in large files)" t)))
  558. (defcustom org-use-sub-superscripts t
  559. "Non-nil means interpret \"_\" and \"^\" for display.
  560. When this option is turned on, you can use TeX-like syntax for sub- and
  561. superscripts. Several characters after \"_\" or \"^\" will be
  562. considered as a single item - so grouping with {} is normally not
  563. needed. For example, the following things will be parsed as single
  564. sub- or superscripts.
  565. 10^24 or 10^tau several digits will be considered 1 item.
  566. 10^-12 or 10^-tau a leading sign with digits or a word
  567. x^2-y^3 will be read as x^2 - y^3, because items are
  568. terminated by almost any nonword/nondigit char.
  569. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  570. Still, ambiguity is possible - so when in doubt use {} to enclose
  571. the sub/superscript. If you set this variable to the symbol
  572. `{}', the braces are *required* in order to trigger
  573. interpretations as sub/superscript. This can be helpful in
  574. documents that need \"_\" frequently in plain text."
  575. :group 'org-startup
  576. :version "24.1"
  577. :type '(choice
  578. (const :tag "Always interpret" t)
  579. (const :tag "Only with braces" {})
  580. (const :tag "Never interpret" nil)))
  581. (defcustom org-startup-with-beamer-mode nil
  582. "Non-nil means turn on `org-beamer-mode' on startup.
  583. This can also be configured on a per-file basis by adding one of
  584. the following lines anywhere in the buffer:
  585. #+STARTUP: beamer"
  586. :group 'org-startup
  587. :version "24.1"
  588. :type 'boolean)
  589. (defcustom org-startup-align-all-tables nil
  590. "Non-nil means align all tables when visiting a file.
  591. This is useful when the column width in tables is forced with <N> cookies
  592. in table fields. Such tables will look correct only after the first re-align.
  593. This can also be configured on a per-file basis by adding one of
  594. the following lines anywhere in the buffer:
  595. #+STARTUP: align
  596. #+STARTUP: noalign"
  597. :group 'org-startup
  598. :type 'boolean)
  599. (defcustom org-startup-with-inline-images nil
  600. "Non-nil means show inline images when loading a new Org file.
  601. This can also be configured on a per-file basis by adding one of
  602. the following lines anywhere in the buffer:
  603. #+STARTUP: inlineimages
  604. #+STARTUP: noinlineimages"
  605. :group 'org-startup
  606. :version "24.1"
  607. :type 'boolean)
  608. (defcustom org-startup-with-latex-preview nil
  609. "Non-nil means preview LaTeX fragments when loading a new Org file.
  610. This can also be configured on a per-file basis by adding one of
  611. the followinglines anywhere in the buffer:
  612. #+STARTUP: latexpreview
  613. #+STARTUP: nolatexpreview"
  614. :group 'org-startup
  615. :version "24.4"
  616. :package-version '(Org . "8.0")
  617. :type 'boolean)
  618. (defcustom org-insert-mode-line-in-empty-file nil
  619. "Non-nil means insert the first line setting Org-mode in empty files.
  620. When the function `org-mode' is called interactively in an empty file, this
  621. normally means that the file name does not automatically trigger Org-mode.
  622. To ensure that the file will always be in Org-mode in the future, a
  623. line enforcing Org-mode will be inserted into the buffer, if this option
  624. has been set."
  625. :group 'org-startup
  626. :type 'boolean)
  627. (defcustom org-replace-disputed-keys nil
  628. "Non-nil means use alternative key bindings for some keys.
  629. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  630. These keys are also used by other packages like shift-selection-mode'
  631. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  632. If you want to use Org-mode together with one of these other modes,
  633. or more generally if you would like to move some Org-mode commands to
  634. other keys, set this variable and configure the keys with the variable
  635. `org-disputed-keys'.
  636. This option is only relevant at load-time of Org-mode, and must be set
  637. *before* org.el is loaded. Changing it requires a restart of Emacs to
  638. become effective."
  639. :group 'org-startup
  640. :type 'boolean)
  641. (defcustom org-use-extra-keys nil
  642. "Non-nil means use extra key sequence definitions for certain commands.
  643. This happens automatically if you run XEmacs or if `window-system'
  644. is nil. This variable lets you do the same manually. You must
  645. set it before loading org.
  646. Example: on Carbon Emacs 22 running graphically, with an external
  647. keyboard on a Powerbook, the default way of setting M-left might
  648. not work for either Alt or ESC. Setting this variable will make
  649. it work for ESC."
  650. :group 'org-startup
  651. :type 'boolean)
  652. (org-defvaralias 'org-CUA-compatible 'org-replace-disputed-keys)
  653. (defcustom org-disputed-keys
  654. '(([(shift up)] . [(meta p)])
  655. ([(shift down)] . [(meta n)])
  656. ([(shift left)] . [(meta -)])
  657. ([(shift right)] . [(meta +)])
  658. ([(control shift right)] . [(meta shift +)])
  659. ([(control shift left)] . [(meta shift -)]))
  660. "Keys for which Org-mode and other modes compete.
  661. This is an alist, cars are the default keys, second element specifies
  662. the alternative to use when `org-replace-disputed-keys' is t.
  663. Keys can be specified in any syntax supported by `define-key'.
  664. The value of this option takes effect only at Org-mode's startup,
  665. therefore you'll have to restart Emacs to apply it after changing."
  666. :group 'org-startup
  667. :type 'alist)
  668. (defun org-key (key)
  669. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  670. Or return the original if not disputed.
  671. Also apply the translations defined in `org-xemacs-key-equivalents'."
  672. (when org-replace-disputed-keys
  673. (let* ((nkey (key-description key))
  674. (x (org-find-if (lambda (x)
  675. (equal (key-description (car x)) nkey))
  676. org-disputed-keys)))
  677. (setq key (if x (cdr x) key))))
  678. (when (featurep 'xemacs)
  679. (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
  680. key)
  681. (defun org-find-if (predicate seq)
  682. (catch 'exit
  683. (while seq
  684. (if (funcall predicate (car seq))
  685. (throw 'exit (car seq))
  686. (pop seq)))))
  687. (defun org-defkey (keymap key def)
  688. "Define a key, possibly translated, as returned by `org-key'."
  689. (define-key keymap (org-key key) def))
  690. (defcustom org-ellipsis nil
  691. "The ellipsis to use in the Org-mode outline.
  692. When nil, just use the standard three dots. When a string, use that instead,
  693. When a face, use the standard 3 dots, but with the specified face.
  694. The change affects only Org-mode (which will then use its own display table).
  695. Changing this requires executing `M-x org-mode' in a buffer to become
  696. effective."
  697. :group 'org-startup
  698. :type '(choice (const :tag "Default" nil)
  699. (face :tag "Face" :value org-warning)
  700. (string :tag "String" :value "...#")))
  701. (defvar org-display-table nil
  702. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  703. (defgroup org-keywords nil
  704. "Keywords in Org-mode."
  705. :tag "Org Keywords"
  706. :group 'org)
  707. (defcustom org-deadline-string "DEADLINE:"
  708. "String to mark deadline entries.
  709. A deadline is this string, followed by a time stamp. Should be a word,
  710. terminated by a colon. You can insert a schedule keyword and
  711. a timestamp with \\[org-deadline].
  712. Changes become only effective after restarting Emacs."
  713. :group 'org-keywords
  714. :type 'string)
  715. (defcustom org-scheduled-string "SCHEDULED:"
  716. "String to mark scheduled TODO entries.
  717. A schedule is this string, followed by a time stamp. Should be a word,
  718. terminated by a colon. You can insert a schedule keyword and
  719. a timestamp with \\[org-schedule].
  720. Changes become only effective after restarting Emacs."
  721. :group 'org-keywords
  722. :type 'string)
  723. (defcustom org-closed-string "CLOSED:"
  724. "String used as the prefix for timestamps logging closing a TODO entry."
  725. :group 'org-keywords
  726. :type 'string)
  727. (defcustom org-clock-string "CLOCK:"
  728. "String used as prefix for timestamps clocking work hours on an item."
  729. :group 'org-keywords
  730. :type 'string)
  731. (defconst org-planning-or-clock-line-re (concat "^[ \t]*\\("
  732. org-scheduled-string "\\|"
  733. org-deadline-string "\\|"
  734. org-closed-string "\\|"
  735. org-clock-string "\\)")
  736. "Matches a line with planning or clock info.")
  737. (defcustom org-comment-string "COMMENT"
  738. "Entries starting with this keyword will never be exported.
  739. An entry can be toggled between COMMENT and normal with
  740. \\[org-toggle-comment].
  741. Changes become only effective after restarting Emacs."
  742. :group 'org-keywords
  743. :type 'string)
  744. (defcustom org-quote-string "QUOTE"
  745. "Entries starting with this keyword will be exported in fixed-width font.
  746. Quoting applies only to the text in the entry following the headline, and does
  747. not extend beyond the next headline, even if that is lower level.
  748. An entry can be toggled between QUOTE and normal with
  749. \\[org-toggle-fixed-width-section]."
  750. :group 'org-keywords
  751. :type 'string)
  752. (defconst org-repeat-re
  753. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)"
  754. "Regular expression for specifying repeated events.
  755. After a match, group 1 contains the repeat expression.")
  756. (defgroup org-structure nil
  757. "Options concerning the general structure of Org-mode files."
  758. :tag "Org Structure"
  759. :group 'org)
  760. (defgroup org-reveal-location nil
  761. "Options about how to make context of a location visible."
  762. :tag "Org Reveal Location"
  763. :group 'org-structure)
  764. (defconst org-context-choice
  765. '(choice
  766. (const :tag "Always" t)
  767. (const :tag "Never" nil)
  768. (repeat :greedy t :tag "Individual contexts"
  769. (cons
  770. (choice :tag "Context"
  771. (const agenda)
  772. (const org-goto)
  773. (const occur-tree)
  774. (const tags-tree)
  775. (const link-search)
  776. (const mark-goto)
  777. (const bookmark-jump)
  778. (const isearch)
  779. (const default))
  780. (boolean))))
  781. "Contexts for the reveal options.")
  782. (defcustom org-show-hierarchy-above '((default . t))
  783. "Non-nil means show full hierarchy when revealing a location.
  784. Org-mode often shows locations in an org-mode file which might have
  785. been invisible before. When this is set, the hierarchy of headings
  786. above the exposed location is shown.
  787. Turning this off for example for sparse trees makes them very compact.
  788. Instead of t, this can also be an alist specifying this option for different
  789. contexts. Valid contexts are
  790. agenda when exposing an entry from the agenda
  791. org-goto when using the command `org-goto' on key C-c C-j
  792. occur-tree when using the command `org-occur' on key C-c /
  793. tags-tree when constructing a sparse tree based on tags matches
  794. link-search when exposing search matches associated with a link
  795. mark-goto when exposing the jump goal of a mark
  796. bookmark-jump when exposing a bookmark location
  797. isearch when exiting from an incremental search
  798. default default for all contexts not set explicitly"
  799. :group 'org-reveal-location
  800. :type org-context-choice)
  801. (defcustom org-show-following-heading '((default . nil))
  802. "Non-nil means show following heading when revealing a location.
  803. Org-mode often shows locations in an org-mode file which might have
  804. been invisible before. When this is set, the heading following the
  805. match is shown.
  806. Turning this off for example for sparse trees makes them very compact,
  807. but makes it harder to edit the location of the match. In such a case,
  808. use the command \\[org-reveal] to show more context.
  809. Instead of t, this can also be an alist specifying this option for different
  810. contexts. See `org-show-hierarchy-above' for valid contexts."
  811. :group 'org-reveal-location
  812. :type org-context-choice)
  813. (defcustom org-show-siblings '((default . nil) (isearch t))
  814. "Non-nil means show all sibling heading when revealing a location.
  815. Org-mode often shows locations in an org-mode file which might have
  816. been invisible before. When this is set, the sibling of the current entry
  817. heading are all made visible. If `org-show-hierarchy-above' is t,
  818. the same happens on each level of the hierarchy above the current entry.
  819. By default this is on for the isearch context, off for all other contexts.
  820. Turning this off for example for sparse trees makes them very compact,
  821. but makes it harder to edit the location of the match. In such a case,
  822. use the command \\[org-reveal] to show more context.
  823. Instead of t, this can also be an alist specifying this option for different
  824. contexts. See `org-show-hierarchy-above' for valid contexts."
  825. :group 'org-reveal-location
  826. :type org-context-choice)
  827. (defcustom org-show-entry-below '((default . nil))
  828. "Non-nil means show the entry below a headline when revealing a location.
  829. Org-mode often shows locations in an org-mode file which might have
  830. been invisible before. When this is set, the text below the headline that is
  831. exposed is also shown.
  832. By default this is off for all contexts.
  833. Instead of t, this can also be an alist specifying this option for different
  834. contexts. See `org-show-hierarchy-above' for valid contexts."
  835. :group 'org-reveal-location
  836. :type org-context-choice)
  837. (defcustom org-indirect-buffer-display 'other-window
  838. "How should indirect tree buffers be displayed?
  839. This applies to indirect buffers created with the commands
  840. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  841. Valid values are:
  842. current-window Display in the current window
  843. other-window Just display in another window.
  844. dedicated-frame Create one new frame, and re-use it each time.
  845. new-frame Make a new frame each time. Note that in this case
  846. previously-made indirect buffers are kept, and you need to
  847. kill these buffers yourself."
  848. :group 'org-structure
  849. :group 'org-agenda-windows
  850. :type '(choice
  851. (const :tag "In current window" current-window)
  852. (const :tag "In current frame, other window" other-window)
  853. (const :tag "Each time a new frame" new-frame)
  854. (const :tag "One dedicated frame" dedicated-frame)))
  855. (defcustom org-use-speed-commands nil
  856. "Non-nil means activate single letter commands at beginning of a headline.
  857. This may also be a function to test for appropriate locations where speed
  858. commands should be active."
  859. :group 'org-structure
  860. :type '(choice
  861. (const :tag "Never" nil)
  862. (const :tag "At beginning of headline stars" t)
  863. (function)))
  864. (defcustom org-speed-commands-user nil
  865. "Alist of additional speed commands.
  866. This list will be checked before `org-speed-commands-default'
  867. when the variable `org-use-speed-commands' is non-nil
  868. and when the cursor is at the beginning of a headline.
  869. The car if each entry is a string with a single letter, which must
  870. be assigned to `self-insert-command' in the global map.
  871. The cdr is either a command to be called interactively, a function
  872. to be called, or a form to be evaluated.
  873. An entry that is just a list with a single string will be interpreted
  874. as a descriptive headline that will be added when listing the speed
  875. commands in the Help buffer using the `?' speed command."
  876. :group 'org-structure
  877. :type '(repeat :value ("k" . ignore)
  878. (choice :value ("k" . ignore)
  879. (list :tag "Descriptive Headline" (string :tag "Headline"))
  880. (cons :tag "Letter and Command"
  881. (string :tag "Command letter")
  882. (choice
  883. (function)
  884. (sexp))))))
  885. (defgroup org-cycle nil
  886. "Options concerning visibility cycling in Org-mode."
  887. :tag "Org Cycle"
  888. :group 'org-structure)
  889. (defcustom org-cycle-skip-children-state-if-no-children t
  890. "Non-nil means skip CHILDREN state in entries that don't have any."
  891. :group 'org-cycle
  892. :type 'boolean)
  893. (defcustom org-cycle-max-level nil
  894. "Maximum level which should still be subject to visibility cycling.
  895. Levels higher than this will, for cycling, be treated as text, not a headline.
  896. When `org-odd-levels-only' is set, a value of N in this variable actually
  897. means 2N-1 stars as the limiting headline.
  898. When nil, cycle all levels.
  899. Note that the limiting level of cycling is also influenced by
  900. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  901. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  902. than its value."
  903. :group 'org-cycle
  904. :type '(choice
  905. (const :tag "No limit" nil)
  906. (integer :tag "Maximum level")))
  907. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK" "RESULTS")
  908. "Names of drawers. Drawers are not opened by cycling on the headline above.
  909. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  910. this:
  911. :DRAWERNAME:
  912. .....
  913. :END:
  914. The drawer \"PROPERTIES\" is special for capturing properties through
  915. the property API.
  916. Drawers can be defined on the per-file basis with a line like:
  917. #+DRAWERS: HIDDEN STATE PROPERTIES"
  918. :group 'org-structure
  919. :group 'org-cycle
  920. :type '(repeat (string :tag "Drawer Name")))
  921. (defcustom org-hide-block-startup nil
  922. "Non-nil means entering Org-mode will fold all blocks.
  923. This can also be set in on a per-file basis with
  924. #+STARTUP: hideblocks
  925. #+STARTUP: showblocks"
  926. :group 'org-startup
  927. :group 'org-cycle
  928. :type 'boolean)
  929. (defcustom org-cycle-global-at-bob nil
  930. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  931. This makes it possible to do global cycling without having to use S-TAB or
  932. \\[universal-argument] TAB. For this special case to work, the first line
  933. of the buffer must not be a headline -- it may be empty or some other text.
  934. When used in this way, `org-cycle-hook' is disabled temporarily to make
  935. sure the cursor stays at the beginning of the buffer. When this option is
  936. nil, don't do anything special at the beginning of the buffer."
  937. :group 'org-cycle
  938. :type 'boolean)
  939. (defcustom org-cycle-level-after-item/entry-creation t
  940. "Non-nil means cycle entry level or item indentation in new empty entries.
  941. When the cursor is at the end of an empty headline, i.e., with only stars
  942. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  943. and then all possible ancestor states, before returning to the original state.
  944. This makes data entry extremely fast: M-RET to create a new headline,
  945. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  946. When the cursor is at the end of an empty plain list item, one TAB will
  947. make it a subitem, two or more tabs will back up to make this an item
  948. higher up in the item hierarchy."
  949. :group 'org-cycle
  950. :type 'boolean)
  951. (defcustom org-cycle-emulate-tab t
  952. "Where should `org-cycle' emulate TAB.
  953. nil Never
  954. white Only in completely white lines
  955. whitestart Only at the beginning of lines, before the first non-white char
  956. t Everywhere except in headlines
  957. exc-hl-bol Everywhere except at the start of a headline
  958. If TAB is used in a place where it does not emulate TAB, the current subtree
  959. visibility is cycled."
  960. :group 'org-cycle
  961. :type '(choice (const :tag "Never" nil)
  962. (const :tag "Only in completely white lines" white)
  963. (const :tag "Before first char in a line" whitestart)
  964. (const :tag "Everywhere except in headlines" t)
  965. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  966. ))
  967. (defcustom org-cycle-separator-lines 2
  968. "Number of empty lines needed to keep an empty line between collapsed trees.
  969. If you leave an empty line between the end of a subtree and the following
  970. headline, this empty line is hidden when the subtree is folded.
  971. Org-mode will leave (exactly) one empty line visible if the number of
  972. empty lines is equal or larger to the number given in this variable.
  973. So the default 2 means at least 2 empty lines after the end of a subtree
  974. are needed to produce free space between a collapsed subtree and the
  975. following headline.
  976. If the number is negative, and the number of empty lines is at least -N,
  977. all empty lines are shown.
  978. Special case: when 0, never leave empty lines in collapsed view."
  979. :group 'org-cycle
  980. :type 'integer)
  981. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  982. (defcustom org-pre-cycle-hook nil
  983. "Hook that is run before visibility cycling is happening.
  984. The function(s) in this hook must accept a single argument which indicates
  985. the new state that will be set right after running this hook. The
  986. argument is a symbol. Before a global state change, it can have the values
  987. `overview', `content', or `all'. Before a local state change, it can have
  988. the values `folded', `children', or `subtree'."
  989. :group 'org-cycle
  990. :type 'hook)
  991. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  992. org-cycle-hide-drawers
  993. org-cycle-hide-inline-tasks
  994. org-cycle-show-empty-lines
  995. org-optimize-window-after-visibility-change)
  996. "Hook that is run after `org-cycle' has changed the buffer visibility.
  997. The function(s) in this hook must accept a single argument which indicates
  998. the new state that was set by the most recent `org-cycle' command. The
  999. argument is a symbol. After a global state change, it can have the values
  1000. `overview', `contents', or `all'. After a local state change, it can have
  1001. the values `folded', `children', or `subtree'."
  1002. :group 'org-cycle
  1003. :type 'hook)
  1004. (defgroup org-edit-structure nil
  1005. "Options concerning structure editing in Org-mode."
  1006. :tag "Org Edit Structure"
  1007. :group 'org-structure)
  1008. (defcustom org-odd-levels-only nil
  1009. "Non-nil means skip even levels and only use odd levels for the outline.
  1010. This has the effect that two stars are being added/taken away in
  1011. promotion/demotion commands. It also influences how levels are
  1012. handled by the exporters.
  1013. Changing it requires restart of `font-lock-mode' to become effective
  1014. for fontification also in regions already fontified.
  1015. You may also set this on a per-file basis by adding one of the following
  1016. lines to the buffer:
  1017. #+STARTUP: odd
  1018. #+STARTUP: oddeven"
  1019. :group 'org-edit-structure
  1020. :group 'org-appearance
  1021. :type 'boolean)
  1022. (defcustom org-adapt-indentation t
  1023. "Non-nil means adapt indentation to outline node level.
  1024. When this variable is set, Org assumes that you write outlines by
  1025. indenting text in each node to align with the headline (after the stars).
  1026. The following issues are influenced by this variable:
  1027. - When this is set and the *entire* text in an entry is indented, the
  1028. indentation is increased by one space in a demotion command, and
  1029. decreased by one in a promotion command. If any line in the entry
  1030. body starts with text at column 0, indentation is not changed at all.
  1031. - Property drawers and planning information is inserted indented when
  1032. this variable s set. When nil, they will not be indented.
  1033. - TAB indents a line relative to context. The lines below a headline
  1034. will be indented when this variable is set.
  1035. Note that this is all about true indentation, by adding and removing
  1036. space characters. See also `org-indent.el' which does level-dependent
  1037. indentation in a virtual way, i.e. at display time in Emacs."
  1038. :group 'org-edit-structure
  1039. :type 'boolean)
  1040. (defcustom org-special-ctrl-a/e nil
  1041. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  1042. When t, `C-a' will bring back the cursor to the beginning of the
  1043. headline text, i.e. after the stars and after a possible TODO
  1044. keyword. In an item, this will be the position after bullet and
  1045. check-box, if any. When the cursor is already at that position,
  1046. another `C-a' will bring it to the beginning of the line.
  1047. `C-e' will jump to the end of the headline, ignoring the presence
  1048. of tags in the headline. A second `C-e' will then jump to the
  1049. true end of the line, after any tags. This also means that, when
  1050. this variable is non-nil, `C-e' also will never jump beyond the
  1051. end of the heading of a folded section, i.e. not after the
  1052. ellipses.
  1053. When set to the symbol `reversed', the first `C-a' or `C-e' works
  1054. normally, going to the true line boundary first. Only a directly
  1055. following, identical keypress will bring the cursor to the
  1056. special positions.
  1057. This may also be a cons cell where the behavior for `C-a' and
  1058. `C-e' is set separately."
  1059. :group 'org-edit-structure
  1060. :type '(choice
  1061. (const :tag "off" nil)
  1062. (const :tag "on: after stars/bullet and before tags first" t)
  1063. (const :tag "reversed: true line boundary first" reversed)
  1064. (cons :tag "Set C-a and C-e separately"
  1065. (choice :tag "Special C-a"
  1066. (const :tag "off" nil)
  1067. (const :tag "on: after stars/bullet first" t)
  1068. (const :tag "reversed: before stars/bullet first" reversed))
  1069. (choice :tag "Special C-e"
  1070. (const :tag "off" nil)
  1071. (const :tag "on: before tags first" t)
  1072. (const :tag "reversed: after tags first" reversed)))))
  1073. (org-defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e)
  1074. (defcustom org-special-ctrl-k nil
  1075. "Non-nil means `C-k' will behave specially in headlines.
  1076. When nil, `C-k' will call the default `kill-line' command.
  1077. When t, the following will happen while the cursor is in the headline:
  1078. - When the cursor is at the beginning of a headline, kill the entire
  1079. line and possible the folded subtree below the line.
  1080. - When in the middle of the headline text, kill the headline up to the tags.
  1081. - When after the headline text, kill the tags."
  1082. :group 'org-edit-structure
  1083. :type 'boolean)
  1084. (defcustom org-ctrl-k-protect-subtree nil
  1085. "Non-nil means, do not delete a hidden subtree with C-k.
  1086. When set to the symbol `error', simply throw an error when C-k is
  1087. used to kill (part-of) a headline that has hidden text behind it.
  1088. Any other non-nil value will result in a query to the user, if it is
  1089. OK to kill that hidden subtree. When nil, kill without remorse."
  1090. :group 'org-edit-structure
  1091. :version "24.1"
  1092. :type '(choice
  1093. (const :tag "Do not protect hidden subtrees" nil)
  1094. (const :tag "Protect hidden subtrees with a security query" t)
  1095. (const :tag "Never kill a hidden subtree with C-k" error)))
  1096. (defcustom org-catch-invisible-edits nil
  1097. "Check if in invisible region before inserting or deleting a character.
  1098. Valid values are:
  1099. nil Do not check, so just do invisible edits.
  1100. error Throw an error and do nothing.
  1101. show Make point visible, and do the requested edit.
  1102. show-and-error Make point visible, then throw an error and abort the edit.
  1103. smart Make point visible, and do insertion/deletion if it is
  1104. adjacent to visible text and the change feels predictable.
  1105. Never delete a previously invisible character or add in the
  1106. middle or right after an invisible region. Basically, this
  1107. allows insertion and backward-delete right before ellipses.
  1108. FIXME: maybe in this case we should not even show?"
  1109. :group 'org-edit-structure
  1110. :version "24.1"
  1111. :type '(choice
  1112. (const :tag "Do not check" nil)
  1113. (const :tag "Throw error when trying to edit" error)
  1114. (const :tag "Unhide, but do not do the edit" show-and-error)
  1115. (const :tag "Show invisible part and do the edit" show)
  1116. (const :tag "Be smart and do the right thing" smart)))
  1117. (defcustom org-yank-folded-subtrees t
  1118. "Non-nil means when yanking subtrees, fold them.
  1119. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  1120. it starts with a heading and all other headings in it are either children
  1121. or siblings, then fold all the subtrees. However, do this only if no
  1122. text after the yank would be swallowed into a folded tree by this action."
  1123. :group 'org-edit-structure
  1124. :type 'boolean)
  1125. (defcustom org-yank-adjusted-subtrees nil
  1126. "Non-nil means when yanking subtrees, adjust the level.
  1127. With this setting, `org-paste-subtree' is used to insert the subtree, see
  1128. this function for details."
  1129. :group 'org-edit-structure
  1130. :type 'boolean)
  1131. (defcustom org-M-RET-may-split-line '((default . t))
  1132. "Non-nil means M-RET will split the line at the cursor position.
  1133. When nil, it will go to the end of the line before making a
  1134. new line.
  1135. You may also set this option in a different way for different
  1136. contexts. Valid contexts are:
  1137. headline when creating a new headline
  1138. item when creating a new item
  1139. table in a table field
  1140. default the value to be used for all contexts not explicitly
  1141. customized"
  1142. :group 'org-structure
  1143. :group 'org-table
  1144. :type '(choice
  1145. (const :tag "Always" t)
  1146. (const :tag "Never" nil)
  1147. (repeat :greedy t :tag "Individual contexts"
  1148. (cons
  1149. (choice :tag "Context"
  1150. (const headline)
  1151. (const item)
  1152. (const table)
  1153. (const default))
  1154. (boolean)))))
  1155. (defcustom org-insert-heading-respect-content nil
  1156. "Non-nil means insert new headings after the current subtree.
  1157. When nil, the new heading is created directly after the current line.
  1158. The commands \\[org-insert-heading-respect-content] and
  1159. \\[org-insert-todo-heading-respect-content] turn this variable on
  1160. for the duration of the command."
  1161. :group 'org-structure
  1162. :type 'boolean)
  1163. (defcustom org-blank-before-new-entry '((heading . auto)
  1164. (plain-list-item . auto))
  1165. "Should `org-insert-heading' leave a blank line before new heading/item?
  1166. The value is an alist, with `heading' and `plain-list-item' as CAR,
  1167. and a boolean flag as CDR. The cdr may also be the symbol `auto', in
  1168. which case Org will look at the surrounding headings/items and try to
  1169. make an intelligent decision whether to insert a blank line or not.
  1170. For plain lists, if the variable `org-empty-line-terminates-plain-lists' is
  1171. set, the setting here is ignored and no empty line is inserted, to avoid
  1172. breaking the list structure."
  1173. :group 'org-edit-structure
  1174. :type '(list
  1175. (cons (const heading)
  1176. (choice (const :tag "Never" nil)
  1177. (const :tag "Always" t)
  1178. (const :tag "Auto" auto)))
  1179. (cons (const plain-list-item)
  1180. (choice (const :tag "Never" nil)
  1181. (const :tag "Always" t)
  1182. (const :tag "Auto" auto)))))
  1183. (defcustom org-insert-heading-hook nil
  1184. "Hook being run after inserting a new heading."
  1185. :group 'org-edit-structure
  1186. :type 'hook)
  1187. (defcustom org-enable-fixed-width-editor t
  1188. "Non-nil means lines starting with \":\" are treated as fixed-width.
  1189. This currently only means they are never auto-wrapped.
  1190. When nil, such lines will be treated like ordinary lines.
  1191. See also the QUOTE keyword."
  1192. :group 'org-edit-structure
  1193. :type 'boolean)
  1194. (defcustom org-goto-auto-isearch t
  1195. "Non-nil means typing characters in `org-goto' starts incremental search.
  1196. When nil, you can use these keybindings to navigate the buffer:
  1197. q Quit the org-goto interface
  1198. n Go to the next visible heading
  1199. p Go to the previous visible heading
  1200. f Go one heading forward on same level
  1201. b Go one heading backward on same level
  1202. u Go one heading up"
  1203. :group 'org-edit-structure
  1204. :type 'boolean)
  1205. (defgroup org-sparse-trees nil
  1206. "Options concerning sparse trees in Org-mode."
  1207. :tag "Org Sparse Trees"
  1208. :group 'org-structure)
  1209. (defcustom org-highlight-sparse-tree-matches t
  1210. "Non-nil means highlight all matches that define a sparse tree.
  1211. The highlights will automatically disappear the next time the buffer is
  1212. changed by an edit command."
  1213. :group 'org-sparse-trees
  1214. :type 'boolean)
  1215. (defcustom org-remove-highlights-with-change t
  1216. "Non-nil means any change to the buffer will remove temporary highlights.
  1217. Such highlights are created by `org-occur' and `org-clock-display'.
  1218. When nil, `C-c C-c needs to be used to get rid of the highlights.
  1219. The highlights created by `org-preview-latex-fragment' always need
  1220. `C-c C-c' to be removed."
  1221. :group 'org-sparse-trees
  1222. :group 'org-time
  1223. :type 'boolean)
  1224. (defcustom org-occur-hook '(org-first-headline-recenter)
  1225. "Hook that is run after `org-occur' has constructed a sparse tree.
  1226. This can be used to recenter the window to show as much of the structure
  1227. as possible."
  1228. :group 'org-sparse-trees
  1229. :type 'hook)
  1230. (defgroup org-imenu-and-speedbar nil
  1231. "Options concerning imenu and speedbar in Org-mode."
  1232. :tag "Org Imenu and Speedbar"
  1233. :group 'org-structure)
  1234. (defcustom org-imenu-depth 2
  1235. "The maximum level for Imenu access to Org-mode headlines.
  1236. This also applied for speedbar access."
  1237. :group 'org-imenu-and-speedbar
  1238. :type 'integer)
  1239. (defgroup org-table nil
  1240. "Options concerning tables in Org-mode."
  1241. :tag "Org Table"
  1242. :group 'org)
  1243. (defcustom org-enable-table-editor 'optimized
  1244. "Non-nil means lines starting with \"|\" are handled by the table editor.
  1245. When nil, such lines will be treated like ordinary lines.
  1246. When equal to the symbol `optimized', the table editor will be optimized to
  1247. do the following:
  1248. - Automatic overwrite mode in front of whitespace in table fields.
  1249. This makes the structure of the table stay in tact as long as the edited
  1250. field does not exceed the column width.
  1251. - Minimize the number of realigns. Normally, the table is aligned each time
  1252. TAB or RET are pressed to move to another field. With optimization this
  1253. happens only if changes to a field might have changed the column width.
  1254. Optimization requires replacing the functions `self-insert-command',
  1255. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  1256. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  1257. very good at guessing when a re-align will be necessary, but you can always
  1258. force one with \\[org-ctrl-c-ctrl-c].
  1259. If you would like to use the optimized version in Org-mode, but the
  1260. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  1261. This variable can be used to turn on and off the table editor during a session,
  1262. but in order to toggle optimization, a restart is required.
  1263. See also the variable `org-table-auto-blank-field'."
  1264. :group 'org-table
  1265. :type '(choice
  1266. (const :tag "off" nil)
  1267. (const :tag "on" t)
  1268. (const :tag "on, optimized" optimized)))
  1269. (defcustom org-self-insert-cluster-for-undo (or (featurep 'xemacs)
  1270. (version<= emacs-version "24.1"))
  1271. "Non-nil means cluster self-insert commands for undo when possible.
  1272. If this is set, then, like in the Emacs command loop, 20 consecutive
  1273. characters will be undone together.
  1274. This is configurable, because there is some impact on typing performance."
  1275. :group 'org-table
  1276. :type 'boolean)
  1277. (defcustom org-table-tab-recognizes-table.el t
  1278. "Non-nil means TAB will automatically notice a table.el table.
  1279. When it sees such a table, it moves point into it and - if necessary -
  1280. calls `table-recognize-table'."
  1281. :group 'org-table-editing
  1282. :type 'boolean)
  1283. (defgroup org-link nil
  1284. "Options concerning links in Org-mode."
  1285. :tag "Org Link"
  1286. :group 'org)
  1287. (defvar org-link-abbrev-alist-local nil
  1288. "Buffer-local version of `org-link-abbrev-alist', which see.
  1289. The value of this is taken from the #+LINK lines.")
  1290. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  1291. (defcustom org-link-abbrev-alist nil
  1292. "Alist of link abbreviations.
  1293. The car of each element is a string, to be replaced at the start of a link.
  1294. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1295. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  1296. [[linkkey:tag][description]]
  1297. The 'linkkey' must be a word word, starting with a letter, followed
  1298. by letters, numbers, '-' or '_'.
  1299. If REPLACE is a string, the tag will simply be appended to create the link.
  1300. If the string contains \"%s\", the tag will be inserted there. If the string
  1301. contains \"%h\", it will cause a url-encoded version of the tag to be inserted
  1302. at that point (see the function `url-hexify-string'). If the string contains
  1303. the specifier \"%(my-function)\", then the custom function `my-function' will
  1304. be invoked: this function takes the tag as its only argument and must return
  1305. a string.
  1306. REPLACE may also be a function that will be called with the tag as the
  1307. only argument to create the link, which should be returned as a string.
  1308. See the manual for examples."
  1309. :group 'org-link
  1310. :type '(repeat
  1311. (cons
  1312. (string :tag "Protocol")
  1313. (choice
  1314. (string :tag "Format")
  1315. (function)))))
  1316. (defcustom org-descriptive-links t
  1317. "Non-nil means Org will display descriptive links.
  1318. E.g. [[http://orgmode.org][Org website]] will be displayed as
  1319. \"Org Website\", hiding the link itself and just displaying its
  1320. description. When set to `nil', Org will display the full links
  1321. literally.
  1322. You can interactively set the value of this variable by calling
  1323. `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
  1324. :group 'org-link
  1325. :type 'boolean)
  1326. (defcustom org-link-file-path-type 'adaptive
  1327. "How the path name in file links should be stored.
  1328. Valid values are:
  1329. relative Relative to the current directory, i.e. the directory of the file
  1330. into which the link is being inserted.
  1331. absolute Absolute path, if possible with ~ for home directory.
  1332. noabbrev Absolute path, no abbreviation of home directory.
  1333. adaptive Use relative path for files in the current directory and sub-
  1334. directories of it. For other files, use an absolute path."
  1335. :group 'org-link
  1336. :type '(choice
  1337. (const relative)
  1338. (const absolute)
  1339. (const noabbrev)
  1340. (const adaptive)))
  1341. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  1342. "Types of links that should be activated in Org-mode files.
  1343. This is a list of symbols, each leading to the activation of a certain link
  1344. type. In principle, it does not hurt to turn on most link types - there may
  1345. be a small gain when turning off unused link types. The types are:
  1346. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1347. angle Links in angular brackets that may contain whitespace like
  1348. <bbdb:Carsten Dominik>.
  1349. plain Plain links in normal text, no whitespace, like http://google.com.
  1350. radio Text that is matched by a radio target, see manual for details.
  1351. tag Tag settings in a headline (link to tag search).
  1352. date Time stamps (link to calendar).
  1353. footnote Footnote labels.
  1354. Changing this variable requires a restart of Emacs to become effective."
  1355. :group 'org-link
  1356. :type '(set :greedy t
  1357. (const :tag "Double bracket links" bracket)
  1358. (const :tag "Angular bracket links" angle)
  1359. (const :tag "Plain text links" plain)
  1360. (const :tag "Radio target matches" radio)
  1361. (const :tag "Tags" tag)
  1362. (const :tag "Timestamps" date)
  1363. (const :tag "Footnotes" footnote)))
  1364. (defcustom org-make-link-description-function nil
  1365. "Function to use for generating link descriptions from links.
  1366. When nil, the link location will be used. This function must take
  1367. two parameters: the first one is the link, the second one is the
  1368. description generated by `org-insert-link'. The function should
  1369. return the description to use."
  1370. :group 'org-link
  1371. :type 'function)
  1372. (defgroup org-link-store nil
  1373. "Options concerning storing links in Org-mode."
  1374. :tag "Org Store Link"
  1375. :group 'org-link)
  1376. (defcustom org-url-hexify-p t
  1377. "When non-nil, hexify URL when creating a link."
  1378. :type 'boolean
  1379. :version "24.3"
  1380. :group 'org-link-store)
  1381. (defcustom org-email-link-description-format "Email %c: %.30s"
  1382. "Format of the description part of a link to an email or usenet message.
  1383. The following %-escapes will be replaced by corresponding information:
  1384. %F full \"From\" field
  1385. %f name, taken from \"From\" field, address if no name
  1386. %T full \"To\" field
  1387. %t first name in \"To\" field, address if no name
  1388. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1389. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1390. %s subject
  1391. %d date
  1392. %m message-id.
  1393. You may use normal field width specification between the % and the letter.
  1394. This is for example useful to limit the length of the subject.
  1395. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1396. :group 'org-link-store
  1397. :type 'string)
  1398. (defcustom org-from-is-user-regexp
  1399. (let (r1 r2)
  1400. (when (and user-mail-address (not (string= user-mail-address "")))
  1401. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1402. (when (and user-full-name (not (string= user-full-name "")))
  1403. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1404. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1405. "Regexp matched against the \"From:\" header of an email or usenet message.
  1406. It should match if the message is from the user him/herself."
  1407. :group 'org-link-store
  1408. :type 'regexp)
  1409. (defcustom org-context-in-file-links t
  1410. "Non-nil means file links from `org-store-link' contain context.
  1411. A search string will be added to the file name with :: as separator and
  1412. used to find the context when the link is activated by the command
  1413. `org-open-at-point'. When this option is t, the entire active region
  1414. will be placed in the search string of the file link. If set to a
  1415. positive integer, only the first n lines of context will be stored.
  1416. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1417. negates this setting for the duration of the command."
  1418. :group 'org-link-store
  1419. :type '(choice boolean integer))
  1420. (defcustom org-keep-stored-link-after-insertion nil
  1421. "Non-nil means keep link in list for entire session.
  1422. The command `org-store-link' adds a link pointing to the current
  1423. location to an internal list. These links accumulate during a session.
  1424. The command `org-insert-link' can be used to insert links into any
  1425. Org-mode file (offering completion for all stored links). When this
  1426. option is nil, every link which has been inserted once using \\[org-insert-link]
  1427. will be removed from the list, to make completing the unused links
  1428. more efficient."
  1429. :group 'org-link-store
  1430. :type 'boolean)
  1431. (defgroup org-link-follow nil
  1432. "Options concerning following links in Org-mode."
  1433. :tag "Org Follow Link"
  1434. :group 'org-link)
  1435. (defcustom org-link-translation-function nil
  1436. "Function to translate links with different syntax to Org syntax.
  1437. This can be used to translate links created for example by the Planner
  1438. or emacs-wiki packages to Org syntax.
  1439. The function must accept two parameters, a TYPE containing the link
  1440. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1441. which is everything after the link protocol. It should return a cons
  1442. with possibly modified values of type and path.
  1443. Org contains a function for this, so if you set this variable to
  1444. `org-translate-link-from-planner', you should be able follow many
  1445. links created by planner."
  1446. :group 'org-link-follow
  1447. :type 'function)
  1448. (defcustom org-follow-link-hook nil
  1449. "Hook that is run after a link has been followed."
  1450. :group 'org-link-follow
  1451. :type 'hook)
  1452. (defcustom org-tab-follows-link nil
  1453. "Non-nil means on links TAB will follow the link.
  1454. Needs to be set before org.el is loaded.
  1455. This really should not be used, it does not make sense, and the
  1456. implementation is bad."
  1457. :group 'org-link-follow
  1458. :type 'boolean)
  1459. (defcustom org-return-follows-link nil
  1460. "Non-nil means on links RET will follow the link.
  1461. In tables, the special behavior of RET has precedence."
  1462. :group 'org-link-follow
  1463. :type 'boolean)
  1464. (defcustom org-mouse-1-follows-link
  1465. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1466. "Non-nil means mouse-1 on a link will follow the link.
  1467. A longer mouse click will still set point. Does not work on XEmacs.
  1468. Needs to be set before org.el is loaded."
  1469. :group 'org-link-follow
  1470. :type 'boolean)
  1471. (defcustom org-mark-ring-length 4
  1472. "Number of different positions to be recorded in the ring.
  1473. Changing this requires a restart of Emacs to work correctly."
  1474. :group 'org-link-follow
  1475. :type 'integer)
  1476. (defcustom org-link-search-must-match-exact-headline 'query-to-create
  1477. "Non-nil means internal links in Org files must exactly match a headline.
  1478. When nil, the link search tries to match a phrase with all words
  1479. in the search text."
  1480. :group 'org-link-follow
  1481. :version "24.1"
  1482. :type '(choice
  1483. (const :tag "Use fuzzy text search" nil)
  1484. (const :tag "Match only exact headline" t)
  1485. (const :tag "Match exact headline or query to create it"
  1486. query-to-create)))
  1487. (defcustom org-link-frame-setup
  1488. '((vm . vm-visit-folder-other-frame)
  1489. (vm-imap . vm-visit-imap-folder-other-frame)
  1490. (gnus . org-gnus-no-new-news)
  1491. (file . find-file-other-window)
  1492. (wl . wl-other-frame))
  1493. "Setup the frame configuration for following links.
  1494. When following a link with Emacs, it may often be useful to display
  1495. this link in another window or frame. This variable can be used to
  1496. set this up for the different types of links.
  1497. For VM, use any of
  1498. `vm-visit-folder'
  1499. `vm-visit-folder-other-window'
  1500. `vm-visit-folder-other-frame'
  1501. For Gnus, use any of
  1502. `gnus'
  1503. `gnus-other-frame'
  1504. `org-gnus-no-new-news'
  1505. For FILE, use any of
  1506. `find-file'
  1507. `find-file-other-window'
  1508. `find-file-other-frame'
  1509. For Wanderlust use any of
  1510. `wl'
  1511. `wl-other-frame'
  1512. For the calendar, use the variable `calendar-setup'.
  1513. For BBDB, it is currently only possible to display the matches in
  1514. another window."
  1515. :group 'org-link-follow
  1516. :type '(list
  1517. (cons (const vm)
  1518. (choice
  1519. (const vm-visit-folder)
  1520. (const vm-visit-folder-other-window)
  1521. (const vm-visit-folder-other-frame)))
  1522. (cons (const gnus)
  1523. (choice
  1524. (const gnus)
  1525. (const gnus-other-frame)
  1526. (const org-gnus-no-new-news)))
  1527. (cons (const file)
  1528. (choice
  1529. (const find-file)
  1530. (const find-file-other-window)
  1531. (const find-file-other-frame)))
  1532. (cons (const wl)
  1533. (choice
  1534. (const wl)
  1535. (const wl-other-frame)))))
  1536. (defcustom org-display-internal-link-with-indirect-buffer nil
  1537. "Non-nil means use indirect buffer to display infile links.
  1538. Activating internal links (from one location in a file to another location
  1539. in the same file) normally just jumps to the location. When the link is
  1540. activated with a \\[universal-argument] prefix (or with mouse-3), the link \
  1541. is displayed in
  1542. another window. When this option is set, the other window actually displays
  1543. an indirect buffer clone of the current buffer, to avoid any visibility
  1544. changes to the current buffer."
  1545. :group 'org-link-follow
  1546. :type 'boolean)
  1547. (defcustom org-open-non-existing-files nil
  1548. "Non-nil means `org-open-file' will open non-existing files.
  1549. When nil, an error will be generated.
  1550. This variable applies only to external applications because they
  1551. might choke on non-existing files. If the link is to a file that
  1552. will be opened in Emacs, the variable is ignored."
  1553. :group 'org-link-follow
  1554. :type 'boolean)
  1555. (defcustom org-open-directory-means-index-dot-org nil
  1556. "Non-nil means a link to a directory really means to index.org.
  1557. When nil, following a directory link will run dired or open a finder/explorer
  1558. window on that directory."
  1559. :group 'org-link-follow
  1560. :type 'boolean)
  1561. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1562. "Function and arguments to call for following mailto links.
  1563. This is a list with the first element being a Lisp function, and the
  1564. remaining elements being arguments to the function. In string arguments,
  1565. %a will be replaced by the address, and %s will be replaced by the subject
  1566. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1567. :group 'org-link-follow
  1568. :type '(choice
  1569. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1570. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1571. (const :tag "message-mail" (message-mail "%a" "%s"))
  1572. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1573. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1574. "Non-nil means ask for confirmation before executing shell links.
  1575. Shell links can be dangerous: just think about a link
  1576. [[shell:rm -rf ~/*][Google Search]]
  1577. This link would show up in your Org-mode document as \"Google Search\",
  1578. but really it would remove your entire home directory.
  1579. Therefore we advise against setting this variable to nil.
  1580. Just change it to `y-or-n-p' if you want to confirm with a
  1581. single keystroke rather than having to type \"yes\"."
  1582. :group 'org-link-follow
  1583. :type '(choice
  1584. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1585. (const :tag "with y-or-n (faster)" y-or-n-p)
  1586. (const :tag "no confirmation (dangerous)" nil)))
  1587. (put 'org-confirm-shell-link-function
  1588. 'safe-local-variable
  1589. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1590. (defcustom org-confirm-shell-link-not-regexp ""
  1591. "A regexp to skip confirmation for shell links."
  1592. :group 'org-link-follow
  1593. :version "24.1"
  1594. :type 'regexp)
  1595. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1596. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1597. Elisp links can be dangerous: just think about a link
  1598. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1599. This link would show up in your Org-mode document as \"Google Search\",
  1600. but really it would remove your entire home directory.
  1601. Therefore we advise against setting this variable to nil.
  1602. Just change it to `y-or-n-p' if you want to confirm with a
  1603. single keystroke rather than having to type \"yes\"."
  1604. :group 'org-link-follow
  1605. :type '(choice
  1606. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1607. (const :tag "with y-or-n (faster)" y-or-n-p)
  1608. (const :tag "no confirmation (dangerous)" nil)))
  1609. (put 'org-confirm-shell-link-function
  1610. 'safe-local-variable
  1611. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1612. (defcustom org-confirm-elisp-link-not-regexp ""
  1613. "A regexp to skip confirmation for Elisp links."
  1614. :group 'org-link-follow
  1615. :version "24.1"
  1616. :type 'regexp)
  1617. (defconst org-file-apps-defaults-gnu
  1618. '((remote . emacs)
  1619. (system . mailcap)
  1620. (t . mailcap))
  1621. "Default file applications on a UNIX or GNU/Linux system.
  1622. See `org-file-apps'.")
  1623. (defconst org-file-apps-defaults-macosx
  1624. '((remote . emacs)
  1625. (t . "open %s")
  1626. (system . "open %s")
  1627. ("ps.gz" . "gv %s")
  1628. ("eps.gz" . "gv %s")
  1629. ("dvi" . "xdvi %s")
  1630. ("fig" . "xfig %s"))
  1631. "Default file applications on a MacOS X system.
  1632. The system \"open\" is known as a default, but we use X11 applications
  1633. for some files for which the OS does not have a good default.
  1634. See `org-file-apps'.")
  1635. (defconst org-file-apps-defaults-windowsnt
  1636. (list
  1637. '(remote . emacs)
  1638. (cons t
  1639. (list (if (featurep 'xemacs)
  1640. 'mswindows-shell-execute
  1641. 'w32-shell-execute)
  1642. "open" 'file))
  1643. (cons 'system
  1644. (list (if (featurep 'xemacs)
  1645. 'mswindows-shell-execute
  1646. 'w32-shell-execute)
  1647. "open" 'file)))
  1648. "Default file applications on a Windows NT system.
  1649. The system \"open\" is used for most files.
  1650. See `org-file-apps'.")
  1651. (defcustom org-file-apps
  1652. '(
  1653. (auto-mode . emacs)
  1654. ("\\.mm\\'" . default)
  1655. ("\\.x?html?\\'" . default)
  1656. ("\\.pdf\\'" . default)
  1657. )
  1658. "External applications for opening `file:path' items in a document.
  1659. Org-mode uses system defaults for different file types, but
  1660. you can use this variable to set the application for a given file
  1661. extension. The entries in this list are cons cells where the car identifies
  1662. files and the cdr the corresponding command. Possible values for the
  1663. file identifier are
  1664. \"string\" A string as a file identifier can be interpreted in different
  1665. ways, depending on its contents:
  1666. - Alphanumeric characters only:
  1667. Match links with this file extension.
  1668. Example: (\"pdf\" . \"evince %s\")
  1669. to open PDFs with evince.
  1670. - Regular expression: Match links where the
  1671. filename matches the regexp. If you want to
  1672. use groups here, use shy groups.
  1673. Example: (\"\\.x?html\\'\" . \"firefox %s\")
  1674. (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1675. to open *.html and *.xhtml with firefox.
  1676. - Regular expression which contains (non-shy) groups:
  1677. Match links where the whole link, including \"::\", and
  1678. anything after that, matches the regexp.
  1679. In a custom command string, %1, %2, etc. are replaced with
  1680. the parts of the link that were matched by the groups.
  1681. For backwards compatibility, if a command string is given
  1682. that does not use any of the group matches, this case is
  1683. handled identically to the second one (i.e. match against
  1684. file name only).
  1685. In a custom lisp form, you can access the group matches with
  1686. (match-string n link).
  1687. Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
  1688. to open [[file:document.pdf::5]] with evince at page 5.
  1689. `directory' Matches a directory
  1690. `remote' Matches a remote file, accessible through tramp or efs.
  1691. Remote files most likely should be visited through Emacs
  1692. because external applications cannot handle such paths.
  1693. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1694. so all files Emacs knows how to handle. Using this with
  1695. command `emacs' will open most files in Emacs. Beware that this
  1696. will also open html files inside Emacs, unless you add
  1697. (\"html\" . default) to the list as well.
  1698. t Default for files not matched by any of the other options.
  1699. `system' The system command to open files, like `open' on Windows
  1700. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1701. that will be selected if you call `C-c C-o' with a double
  1702. \\[universal-argument] \\[universal-argument] prefix.
  1703. Possible values for the command are:
  1704. `emacs' The file will be visited by the current Emacs process.
  1705. `default' Use the default application for this file type, which is the
  1706. association for t in the list, most likely in the system-specific
  1707. part.
  1708. This can be used to overrule an unwanted setting in the
  1709. system-specific variable.
  1710. `system' Use the system command for opening files, like \"open\".
  1711. This command is specified by the entry whose car is `system'.
  1712. Most likely, the system-specific version of this variable
  1713. does define this command, but you can overrule/replace it
  1714. here.
  1715. string A command to be executed by a shell; %s will be replaced
  1716. by the path to the file.
  1717. sexp A Lisp form which will be evaluated. The file path will
  1718. be available in the Lisp variable `file'.
  1719. For more examples, see the system specific constants
  1720. `org-file-apps-defaults-macosx'
  1721. `org-file-apps-defaults-windowsnt'
  1722. `org-file-apps-defaults-gnu'."
  1723. :group 'org-link-follow
  1724. :type '(repeat
  1725. (cons (choice :value ""
  1726. (string :tag "Extension")
  1727. (const :tag "System command to open files" system)
  1728. (const :tag "Default for unrecognized files" t)
  1729. (const :tag "Remote file" remote)
  1730. (const :tag "Links to a directory" directory)
  1731. (const :tag "Any files that have Emacs modes"
  1732. auto-mode))
  1733. (choice :value ""
  1734. (const :tag "Visit with Emacs" emacs)
  1735. (const :tag "Use default" default)
  1736. (const :tag "Use the system command" system)
  1737. (string :tag "Command")
  1738. (sexp :tag "Lisp form")))))
  1739. (defcustom org-doi-server-url "http://dx.doi.org/"
  1740. "The URL of the DOI server."
  1741. :type 'string
  1742. :version "24.3"
  1743. :group 'org-link-follow)
  1744. (defgroup org-refile nil
  1745. "Options concerning refiling entries in Org-mode."
  1746. :tag "Org Refile"
  1747. :group 'org)
  1748. (defcustom org-directory "~/org"
  1749. "Directory with org files.
  1750. This is just a default location to look for Org files. There is no need
  1751. at all to put your files into this directory. It is only used in the
  1752. following situations:
  1753. 1. When a capture template specifies a target file that is not an
  1754. absolute path. The path will then be interpreted relative to
  1755. `org-directory'
  1756. 2. When a capture note is filed away in an interactive way (when exiting the
  1757. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1758. with `org-directory' as the default path."
  1759. :group 'org-refile
  1760. :group 'org-remember
  1761. :group 'org-capture
  1762. :type 'directory)
  1763. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1764. "Default target for storing notes.
  1765. Used as a fall back file for org-remember.el and org-capture.el, for
  1766. templates that do not specify a target file."
  1767. :group 'org-refile
  1768. :group 'org-remember
  1769. :group 'org-capture
  1770. :type '(choice
  1771. (const :tag "Default from remember-data-file" nil)
  1772. file))
  1773. (defcustom org-goto-interface 'outline
  1774. "The default interface to be used for `org-goto'.
  1775. Allowed values are:
  1776. outline The interface shows an outline of the relevant file
  1777. and the correct heading is found by moving through
  1778. the outline or by searching with incremental search.
  1779. outline-path-completion Headlines in the current buffer are offered via
  1780. completion. This is the interface also used by
  1781. the refile command."
  1782. :group 'org-refile
  1783. :type '(choice
  1784. (const :tag "Outline" outline)
  1785. (const :tag "Outline-path-completion" outline-path-completion)))
  1786. (defcustom org-goto-max-level 5
  1787. "Maximum target level when running `org-goto' with refile interface."
  1788. :group 'org-refile
  1789. :type 'integer)
  1790. (defcustom org-reverse-note-order nil
  1791. "Non-nil means store new notes at the beginning of a file or entry.
  1792. When nil, new notes will be filed to the end of a file or entry.
  1793. This can also be a list with cons cells of regular expressions that
  1794. are matched against file names, and values."
  1795. :group 'org-remember
  1796. :group 'org-capture
  1797. :group 'org-refile
  1798. :type '(choice
  1799. (const :tag "Reverse always" t)
  1800. (const :tag "Reverse never" nil)
  1801. (repeat :tag "By file name regexp"
  1802. (cons regexp boolean))))
  1803. (defcustom org-log-refile nil
  1804. "Information to record when a task is refiled.
  1805. Possible values are:
  1806. nil Don't add anything
  1807. time Add a time stamp to the task
  1808. note Prompt for a note and add it with template `org-log-note-headings'
  1809. This option can also be set with on a per-file-basis with
  1810. #+STARTUP: nologrefile
  1811. #+STARTUP: logrefile
  1812. #+STARTUP: lognoterefile
  1813. You can have local logging settings for a subtree by setting the LOGGING
  1814. property to one or more of these keywords.
  1815. When bulk-refiling from the agenda, the value `note' is forbidden and
  1816. will temporarily be changed to `time'."
  1817. :group 'org-refile
  1818. :group 'org-progress
  1819. :version "24.1"
  1820. :type '(choice
  1821. (const :tag "No logging" nil)
  1822. (const :tag "Record timestamp" time)
  1823. (const :tag "Record timestamp with note." note)))
  1824. (defcustom org-refile-targets nil
  1825. "Targets for refiling entries with \\[org-refile].
  1826. This is a list of cons cells. Each cell contains:
  1827. - a specification of the files to be considered, either a list of files,
  1828. or a symbol whose function or variable value will be used to retrieve
  1829. a file name or a list of file names. If you use `org-agenda-files' for
  1830. that, all agenda files will be scanned for targets. Nil means consider
  1831. headings in the current buffer.
  1832. - A specification of how to find candidate refile targets. This may be
  1833. any of:
  1834. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1835. This tag has to be present in all target headlines, inheritance will
  1836. not be considered.
  1837. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1838. todo keyword.
  1839. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1840. headlines that are refiling targets.
  1841. - a cons cell (:level . N). Any headline of level N is considered a target.
  1842. Note that, when `org-odd-levels-only' is set, level corresponds to
  1843. order in hierarchy, not to the number of stars.
  1844. - a cons cell (:maxlevel . N). Any headline with level <= N is 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. Each element of this list generates a set of possible targets.
  1848. The union of these sets is presented (with completion) to
  1849. the user by `org-refile'.
  1850. You can set the variable `org-refile-target-verify-function' to a function
  1851. to verify each headline found by the simple criteria above.
  1852. When this variable is nil, all top-level headlines in the current buffer
  1853. are used, equivalent to the value `((nil . (:level . 1))'."
  1854. :group 'org-refile
  1855. :type '(repeat
  1856. (cons
  1857. (choice :value org-agenda-files
  1858. (const :tag "All agenda files" org-agenda-files)
  1859. (const :tag "Current buffer" nil)
  1860. (function) (variable) (file))
  1861. (choice :tag "Identify target headline by"
  1862. (cons :tag "Specific tag" (const :value :tag) (string))
  1863. (cons :tag "TODO keyword" (const :value :todo) (string))
  1864. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1865. (cons :tag "Level number" (const :value :level) (integer))
  1866. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1867. (defcustom org-refile-target-verify-function nil
  1868. "Function to verify if the headline at point should be a refile target.
  1869. The function will be called without arguments, with point at the
  1870. beginning of the headline. It should return t and leave point
  1871. where it is if the headline is a valid target for refiling.
  1872. If the target should not be selected, the function must return nil.
  1873. In addition to this, it may move point to a place from where the search
  1874. should be continued. For example, the function may decide that the entire
  1875. subtree of the current entry should be excluded and move point to the end
  1876. of the subtree."
  1877. :group 'org-refile
  1878. :type 'function)
  1879. (defcustom org-refile-use-cache nil
  1880. "Non-nil means cache refile targets to speed up the process.
  1881. The cache for a particular file will be updated automatically when
  1882. the buffer has been killed, or when any of the marker used for flagging
  1883. refile targets no longer points at a live buffer.
  1884. If you have added new entries to a buffer that might themselves be targets,
  1885. you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
  1886. find that easier, `C-u C-u C-u C-c C-w'."
  1887. :group 'org-refile
  1888. :version "24.1"
  1889. :type 'boolean)
  1890. (defcustom org-refile-use-outline-path nil
  1891. "Non-nil means provide refile targets as paths.
  1892. So a level 3 headline will be available as level1/level2/level3.
  1893. When the value is `file', also include the file name (without directory)
  1894. into the path. In this case, you can also stop the completion after
  1895. the file name, to get entries inserted as top level in the file.
  1896. When `full-file-path', include the full file path."
  1897. :group 'org-refile
  1898. :type '(choice
  1899. (const :tag "Not" nil)
  1900. (const :tag "Yes" t)
  1901. (const :tag "Start with file name" file)
  1902. (const :tag "Start with full file path" full-file-path)))
  1903. (defcustom org-outline-path-complete-in-steps t
  1904. "Non-nil means complete the outline path in hierarchical steps.
  1905. When Org-mode uses the refile interface to select an outline path
  1906. \(see variable `org-refile-use-outline-path'), the completion of
  1907. the path can be done is a single go, or if can be done in steps down
  1908. the headline hierarchy. Going in steps is probably the best if you
  1909. do not use a special completion package like `ido' or `icicles'.
  1910. However, when using these packages, going in one step can be very
  1911. fast, while still showing the whole path to the entry."
  1912. :group 'org-refile
  1913. :type 'boolean)
  1914. (defcustom org-refile-allow-creating-parent-nodes nil
  1915. "Non-nil means allow to create new nodes as refile targets.
  1916. New nodes are then created by adding \"/new node name\" to the completion
  1917. of an existing node. When the value of this variable is `confirm',
  1918. new node creation must be confirmed by the user (recommended)
  1919. When nil, the completion must match an existing entry.
  1920. Note that, if the new heading is not seen by the criteria
  1921. listed in `org-refile-targets', multiple instances of the same
  1922. heading would be created by trying again to file under the new
  1923. heading."
  1924. :group 'org-refile
  1925. :type '(choice
  1926. (const :tag "Never" nil)
  1927. (const :tag "Always" t)
  1928. (const :tag "Prompt for confirmation" confirm)))
  1929. (defcustom org-refile-active-region-within-subtree nil
  1930. "Non-nil means also refile active region within a subtree.
  1931. By default `org-refile' doesn't allow refiling regions if they
  1932. don't contain a set of subtrees, but it might be convenient to
  1933. do so sometimes: in that case, the first line of the region is
  1934. converted to a headline before refiling."
  1935. :group 'org-refile
  1936. :version "24.1"
  1937. :type 'boolean)
  1938. (defgroup org-todo nil
  1939. "Options concerning TODO items in Org-mode."
  1940. :tag "Org TODO"
  1941. :group 'org)
  1942. (defgroup org-progress nil
  1943. "Options concerning Progress logging in Org-mode."
  1944. :tag "Org Progress"
  1945. :group 'org-time)
  1946. (defvar org-todo-interpretation-widgets
  1947. '((:tag "Sequence (cycling hits every state)" sequence)
  1948. (:tag "Type (cycling directly to DONE)" type))
  1949. "The available interpretation symbols for customizing `org-todo-keywords'.
  1950. Interested libraries should add to this list.")
  1951. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1952. "List of TODO entry keyword sequences and their interpretation.
  1953. \\<org-mode-map>This is a list of sequences.
  1954. Each sequence starts with a symbol, either `sequence' or `type',
  1955. indicating if the keywords should be interpreted as a sequence of
  1956. action steps, or as different types of TODO items. The first
  1957. keywords are states requiring action - these states will select a headline
  1958. for inclusion into the global TODO list Org-mode produces. If one of
  1959. the \"keywords\" is the vertical bar, \"|\", the remaining keywords
  1960. signify that no further action is necessary. If \"|\" is not found,
  1961. the last keyword is treated as the only DONE state of the sequence.
  1962. The command \\[org-todo] cycles an entry through these states, and one
  1963. additional state where no keyword is present. For details about this
  1964. cycling, see the manual.
  1965. TODO keywords and interpretation can also be set on a per-file basis with
  1966. the special #+SEQ_TODO and #+TYP_TODO lines.
  1967. Each keyword can optionally specify a character for fast state selection
  1968. \(in combination with the variable `org-use-fast-todo-selection')
  1969. and specifiers for state change logging, using the same syntax that
  1970. is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says that
  1971. the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1972. indicates to record a time stamp each time this state is selected.
  1973. Each keyword may also specify if a timestamp or a note should be
  1974. recorded when entering or leaving the state, by adding additional
  1975. characters in the parenthesis after the keyword. This looks like this:
  1976. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1977. record only the time of the state change. With X and Y being either
  1978. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1979. Y when leaving the state if and only if the *target* state does not
  1980. define X. You may omit any of the fast-selection key or X or /Y,
  1981. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1982. For backward compatibility, this variable may also be just a list
  1983. of keywords. In this case the interpretation (sequence or type) will be
  1984. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1985. :group 'org-todo
  1986. :group 'org-keywords
  1987. :type '(choice
  1988. (repeat :tag "Old syntax, just keywords"
  1989. (string :tag "Keyword"))
  1990. (repeat :tag "New syntax"
  1991. (cons
  1992. (choice
  1993. :tag "Interpretation"
  1994. ;;Quick and dirty way to see
  1995. ;;`org-todo-interpretations'. This takes the
  1996. ;;place of item arguments
  1997. :convert-widget
  1998. (lambda (widget)
  1999. (widget-put widget
  2000. :args (mapcar
  2001. #'(lambda (x)
  2002. (widget-convert
  2003. (cons 'const x)))
  2004. org-todo-interpretation-widgets))
  2005. widget))
  2006. (repeat
  2007. (string :tag "Keyword"))))))
  2008. (defvar org-todo-keywords-1 nil
  2009. "All TODO and DONE keywords active in a buffer.")
  2010. (make-variable-buffer-local 'org-todo-keywords-1)
  2011. (defvar org-todo-keywords-for-agenda nil)
  2012. (defvar org-done-keywords-for-agenda nil)
  2013. (defvar org-drawers-for-agenda nil)
  2014. (defvar org-todo-keyword-alist-for-agenda nil)
  2015. (defvar org-tag-alist-for-agenda nil)
  2016. (defvar org-agenda-contributing-files nil)
  2017. (defvar org-not-done-keywords nil)
  2018. (make-variable-buffer-local 'org-not-done-keywords)
  2019. (defvar org-done-keywords nil)
  2020. (make-variable-buffer-local 'org-done-keywords)
  2021. (defvar org-todo-heads nil)
  2022. (make-variable-buffer-local 'org-todo-heads)
  2023. (defvar org-todo-sets nil)
  2024. (make-variable-buffer-local 'org-todo-sets)
  2025. (defvar org-todo-log-states nil)
  2026. (make-variable-buffer-local 'org-todo-log-states)
  2027. (defvar org-todo-kwd-alist nil)
  2028. (make-variable-buffer-local 'org-todo-kwd-alist)
  2029. (defvar org-todo-key-alist nil)
  2030. (make-variable-buffer-local 'org-todo-key-alist)
  2031. (defvar org-todo-key-trigger nil)
  2032. (make-variable-buffer-local 'org-todo-key-trigger)
  2033. (defcustom org-todo-interpretation 'sequence
  2034. "Controls how TODO keywords are interpreted.
  2035. This variable is in principle obsolete and is only used for
  2036. backward compatibility, if the interpretation of todo keywords is
  2037. not given already in `org-todo-keywords'. See that variable for
  2038. more information."
  2039. :group 'org-todo
  2040. :group 'org-keywords
  2041. :type '(choice (const sequence)
  2042. (const type)))
  2043. (defcustom org-use-fast-todo-selection t
  2044. "Non-nil means use the fast todo selection scheme with C-c C-t.
  2045. This variable describes if and under what circumstances the cycling
  2046. mechanism for TODO keywords will be replaced by a single-key, direct
  2047. selection scheme.
  2048. When nil, fast selection is never used.
  2049. When the symbol `prefix', it will be used when `org-todo' is called
  2050. with a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and
  2051. `C-u t' in an agenda buffer.
  2052. When t, fast selection is used by default. In this case, the prefix
  2053. argument forces cycling instead.
  2054. In all cases, the special interface is only used if access keys have
  2055. actually been assigned by the user, i.e. if keywords in the configuration
  2056. are followed by a letter in parenthesis, like TODO(t)."
  2057. :group 'org-todo
  2058. :type '(choice
  2059. (const :tag "Never" nil)
  2060. (const :tag "By default" t)
  2061. (const :tag "Only with C-u C-c C-t" prefix)))
  2062. (defcustom org-provide-todo-statistics t
  2063. "Non-nil means update todo statistics after insert and toggle.
  2064. ALL-HEADLINES means update todo statistics by including headlines
  2065. with no TODO keyword as well, counting them as not done.
  2066. A list of TODO keywords means the same, but skip keywords that are
  2067. not in this list.
  2068. When this is set, todo statistics is updated in the parent of the
  2069. current entry each time a todo state is changed."
  2070. :group 'org-todo
  2071. :type '(choice
  2072. (const :tag "Yes, only for TODO entries" t)
  2073. (const :tag "Yes, including all entries" 'all-headlines)
  2074. (repeat :tag "Yes, for TODOs in this list"
  2075. (string :tag "TODO keyword"))
  2076. (other :tag "No TODO statistics" nil)))
  2077. (defcustom org-hierarchical-todo-statistics t
  2078. "Non-nil means TODO statistics covers just direct children.
  2079. When nil, all entries in the subtree are considered.
  2080. This has only an effect if `org-provide-todo-statistics' is set.
  2081. To set this to nil for only a single subtree, use a COOKIE_DATA
  2082. property and include the word \"recursive\" into the value."
  2083. :group 'org-todo
  2084. :type 'boolean)
  2085. (defcustom org-after-todo-state-change-hook nil
  2086. "Hook which is run after the state of a TODO item was changed.
  2087. The new state (a string with a TODO keyword, or nil) is available in the
  2088. Lisp variable `org-state'."
  2089. :group 'org-todo
  2090. :type 'hook)
  2091. (defvar org-blocker-hook nil
  2092. "Hook for functions that are allowed to block a state change.
  2093. Functions in this hook should not modify the buffer.
  2094. Each function gets as its single argument a property list,
  2095. see `org-trigger-hook' for more information about this list.
  2096. If any of the functions in this hook returns nil, the state change
  2097. is blocked.")
  2098. (defvar org-trigger-hook nil
  2099. "Hook for functions that are triggered by a state change.
  2100. Each function gets as its single argument a property list with at
  2101. least the following elements:
  2102. (:type type-of-change :position pos-at-entry-start
  2103. :from old-state :to new-state)
  2104. Depending on the type, more properties may be present.
  2105. This mechanism is currently implemented for:
  2106. TODO state changes
  2107. ------------------
  2108. :type todo-state-change
  2109. :from previous state (keyword as a string), or nil, or a symbol
  2110. 'todo' or 'done', to indicate the general type of state.
  2111. :to new state, like in :from")
  2112. (defcustom org-enforce-todo-dependencies nil
  2113. "Non-nil means undone TODO entries will block switching the parent to DONE.
  2114. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  2115. be blocked if any prior sibling is not yet done.
  2116. Finally, if the parent is blocked because of ordered siblings of its own,
  2117. the child will also be blocked."
  2118. :set (lambda (var val)
  2119. (set var val)
  2120. (if val
  2121. (add-hook 'org-blocker-hook
  2122. 'org-block-todo-from-children-or-siblings-or-parent)
  2123. (remove-hook 'org-blocker-hook
  2124. 'org-block-todo-from-children-or-siblings-or-parent)))
  2125. :group 'org-todo
  2126. :type 'boolean)
  2127. (defcustom org-enforce-todo-checkbox-dependencies nil
  2128. "Non-nil means unchecked boxes will block switching the parent to DONE.
  2129. When this is nil, checkboxes have no influence on switching TODO states.
  2130. When non-nil, you first need to check off all check boxes before the TODO
  2131. entry can be switched to DONE.
  2132. This variable needs to be set before org.el is loaded, and you need to
  2133. restart Emacs after a change to make the change effective. The only way
  2134. to change is while Emacs is running is through the customize interface."
  2135. :set (lambda (var val)
  2136. (set var val)
  2137. (if val
  2138. (add-hook 'org-blocker-hook
  2139. 'org-block-todo-from-checkboxes)
  2140. (remove-hook 'org-blocker-hook
  2141. 'org-block-todo-from-checkboxes)))
  2142. :group 'org-todo
  2143. :type 'boolean)
  2144. (defcustom org-treat-insert-todo-heading-as-state-change nil
  2145. "Non-nil means inserting a TODO heading is treated as state change.
  2146. So when the command \\[org-insert-todo-heading] is used, state change
  2147. logging will apply if appropriate. When nil, the new TODO item will
  2148. be inserted directly, and no logging will take place."
  2149. :group 'org-todo
  2150. :type 'boolean)
  2151. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  2152. "Non-nil means switching TODO states with S-cursor counts as state change.
  2153. This is the default behavior. However, setting this to nil allows a
  2154. convenient way to select a TODO state and bypass any logging associated
  2155. with that."
  2156. :group 'org-todo
  2157. :type 'boolean)
  2158. (defcustom org-todo-state-tags-triggers nil
  2159. "Tag changes that should be triggered by TODO state changes.
  2160. This is a list. Each entry is
  2161. (state-change (tag . flag) .......)
  2162. State-change can be a string with a state, and empty string to indicate the
  2163. state that has no TODO keyword, or it can be one of the symbols `todo'
  2164. or `done', meaning any not-done or done state, respectively."
  2165. :group 'org-todo
  2166. :group 'org-tags
  2167. :type '(repeat
  2168. (cons (choice :tag "When changing to"
  2169. (const :tag "Not-done state" todo)
  2170. (const :tag "Done state" done)
  2171. (string :tag "State"))
  2172. (repeat
  2173. (cons :tag "Tag action"
  2174. (string :tag "Tag")
  2175. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  2176. (defcustom org-log-done nil
  2177. "Information to record when a task moves to the DONE state.
  2178. Possible values are:
  2179. nil Don't add anything, just change the keyword
  2180. time Add a time stamp to the task
  2181. note Prompt for a note and add it with template `org-log-note-headings'
  2182. This option can also be set with on a per-file-basis with
  2183. #+STARTUP: nologdone
  2184. #+STARTUP: logdone
  2185. #+STARTUP: lognotedone
  2186. You can have local logging settings for a subtree by setting the LOGGING
  2187. property to one or more of these keywords."
  2188. :group 'org-todo
  2189. :group 'org-progress
  2190. :type '(choice
  2191. (const :tag "No logging" nil)
  2192. (const :tag "Record CLOSED timestamp" time)
  2193. (const :tag "Record CLOSED timestamp with note." note)))
  2194. ;; Normalize old uses of org-log-done.
  2195. (cond
  2196. ((eq org-log-done t) (setq org-log-done 'time))
  2197. ((and (listp org-log-done) (memq 'done org-log-done))
  2198. (setq org-log-done 'note)))
  2199. (defcustom org-log-reschedule nil
  2200. "Information to record when the scheduling date of a tasks is modified.
  2201. Possible values are:
  2202. nil Don't add anything, just change the date
  2203. time Add a time stamp to the task
  2204. note Prompt for a note and add it with template `org-log-note-headings'
  2205. This option can also be set with on a per-file-basis with
  2206. #+STARTUP: nologreschedule
  2207. #+STARTUP: logreschedule
  2208. #+STARTUP: lognotereschedule"
  2209. :group 'org-todo
  2210. :group 'org-progress
  2211. :type '(choice
  2212. (const :tag "No logging" nil)
  2213. (const :tag "Record timestamp" time)
  2214. (const :tag "Record timestamp with note." note)))
  2215. (defcustom org-log-redeadline nil
  2216. "Information to record when the deadline date of a tasks is modified.
  2217. Possible values are:
  2218. nil Don't add anything, just change the date
  2219. time Add a time stamp to the task
  2220. note Prompt for a note and add it with template `org-log-note-headings'
  2221. This option can also be set with on a per-file-basis with
  2222. #+STARTUP: nologredeadline
  2223. #+STARTUP: logredeadline
  2224. #+STARTUP: lognoteredeadline
  2225. You can have local logging settings for a subtree by setting the LOGGING
  2226. property to one or more of these keywords."
  2227. :group 'org-todo
  2228. :group 'org-progress
  2229. :type '(choice
  2230. (const :tag "No logging" nil)
  2231. (const :tag "Record timestamp" time)
  2232. (const :tag "Record timestamp with note." note)))
  2233. (defcustom org-log-note-clock-out nil
  2234. "Non-nil means record a note when clocking out of an item.
  2235. This can also be configured on a per-file basis by adding one of
  2236. the following lines anywhere in the buffer:
  2237. #+STARTUP: lognoteclock-out
  2238. #+STARTUP: nolognoteclock-out"
  2239. :group 'org-todo
  2240. :group 'org-progress
  2241. :type 'boolean)
  2242. (defcustom org-log-done-with-time t
  2243. "Non-nil means the CLOSED time stamp will contain date and time.
  2244. When nil, only the date will be recorded."
  2245. :group 'org-progress
  2246. :type 'boolean)
  2247. (defcustom org-log-note-headings
  2248. '((done . "CLOSING NOTE %t")
  2249. (state . "State %-12s from %-12S %t")
  2250. (note . "Note taken on %t")
  2251. (reschedule . "Rescheduled from %S on %t")
  2252. (delschedule . "Not scheduled, was %S on %t")
  2253. (redeadline . "New deadline from %S on %t")
  2254. (deldeadline . "Removed deadline, was %S on %t")
  2255. (refile . "Refiled on %t")
  2256. (clock-out . ""))
  2257. "Headings for notes added to entries.
  2258. The value is an alist, with the car being a symbol indicating the note
  2259. context, and the cdr is the heading to be used. The heading may also be the
  2260. empty string.
  2261. %t in the heading will be replaced by a time stamp.
  2262. %T will be an active time stamp instead the default inactive one
  2263. %d will be replaced by a short-format time stamp.
  2264. %D will be replaced by an active short-format time stamp.
  2265. %s will be replaced by the new TODO state, in double quotes.
  2266. %S will be replaced by the old TODO state, in double quotes.
  2267. %u will be replaced by the user name.
  2268. %U will be replaced by the full user name.
  2269. In fact, it is not a good idea to change the `state' entry, because
  2270. agenda log mode depends on the format of these entries."
  2271. :group 'org-todo
  2272. :group 'org-progress
  2273. :type '(list :greedy t
  2274. (cons (const :tag "Heading when closing an item" done) string)
  2275. (cons (const :tag
  2276. "Heading when changing todo state (todo sequence only)"
  2277. state) string)
  2278. (cons (const :tag "Heading when just taking a note" note) string)
  2279. (cons (const :tag "Heading when clocking out" clock-out) string)
  2280. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2281. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2282. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2283. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2284. (cons (const :tag "Heading when refiling" refile) string)))
  2285. (unless (assq 'note org-log-note-headings)
  2286. (push '(note . "%t") org-log-note-headings))
  2287. (defcustom org-log-into-drawer nil
  2288. "Non-nil means insert state change notes and time stamps into a drawer.
  2289. When nil, state changes notes will be inserted after the headline and
  2290. any scheduling and clock lines, but not inside a drawer.
  2291. The value of this variable should be the name of the drawer to use.
  2292. LOGBOOK is proposed as the default drawer for this purpose, you can
  2293. also set this to a string to define the drawer of your choice.
  2294. A value of t is also allowed, representing \"LOGBOOK\".
  2295. A value of t or nil can also be set with on a per-file-basis with
  2296. #+STARTUP: logdrawer
  2297. #+STARTUP: nologdrawer
  2298. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2299. will be ignored.
  2300. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2301. a subtree."
  2302. :group 'org-todo
  2303. :group 'org-progress
  2304. :type '(choice
  2305. (const :tag "Not into a drawer" nil)
  2306. (const :tag "LOGBOOK" t)
  2307. (string :tag "Other")))
  2308. (org-defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer)
  2309. (defun org-log-into-drawer ()
  2310. "Return the value of `org-log-into-drawer', but let properties overrule.
  2311. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  2312. used instead of the default value."
  2313. (let ((p (org-entry-get nil "LOG_INTO_DRAWER" 'inherit t)))
  2314. (cond
  2315. ((not p) org-log-into-drawer)
  2316. ((equal p "nil") nil)
  2317. ((equal p "t") "LOGBOOK")
  2318. (t p))))
  2319. (defcustom org-log-state-notes-insert-after-drawers nil
  2320. "Non-nil means insert state change notes after any drawers in entry.
  2321. Only the drawers that *immediately* follow the headline and the
  2322. deadline/scheduled line are skipped.
  2323. When nil, insert notes right after the heading and perhaps the line
  2324. with deadline/scheduling if present.
  2325. This variable will have no effect if `org-log-into-drawer' is
  2326. set."
  2327. :group 'org-todo
  2328. :group 'org-progress
  2329. :type 'boolean)
  2330. (defcustom org-log-states-order-reversed t
  2331. "Non-nil means the latest state note will be directly after heading.
  2332. When nil, the state change notes will be ordered according to time.
  2333. This option can also be set with on a per-file-basis with
  2334. #+STARTUP: logstatesreversed
  2335. #+STARTUP: nologstatesreversed"
  2336. :group 'org-todo
  2337. :group 'org-progress
  2338. :type 'boolean)
  2339. (defcustom org-todo-repeat-to-state nil
  2340. "The TODO state to which a repeater should return the repeating task.
  2341. By default this is the first task in a TODO sequence, or the previous state
  2342. in a TODO_TYP set. But you can specify another task here.
  2343. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2344. :group 'org-todo
  2345. :version "24.1"
  2346. :type '(choice (const :tag "Head of sequence" nil)
  2347. (string :tag "Specific state")))
  2348. (defcustom org-log-repeat 'time
  2349. "Non-nil means record moving through the DONE state when triggering repeat.
  2350. An auto-repeating task is immediately switched back to TODO when
  2351. marked DONE. If you are not logging state changes (by adding \"@\"
  2352. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2353. record a closing note, there will be no record of the task moving
  2354. through DONE. This variable forces taking a note anyway.
  2355. nil Don't force a record
  2356. time Record a time stamp
  2357. note Prompt for a note and add it with template `org-log-note-headings'
  2358. This option can also be set with on a per-file-basis with
  2359. #+STARTUP: nologrepeat
  2360. #+STARTUP: logrepeat
  2361. #+STARTUP: lognoterepeat
  2362. You can have local logging settings for a subtree by setting the LOGGING
  2363. property to one or more of these keywords."
  2364. :group 'org-todo
  2365. :group 'org-progress
  2366. :type '(choice
  2367. (const :tag "Don't force a record" nil)
  2368. (const :tag "Force recording the DONE state" time)
  2369. (const :tag "Force recording a note with the DONE state" note)))
  2370. (defgroup org-priorities nil
  2371. "Priorities in Org-mode."
  2372. :tag "Org Priorities"
  2373. :group 'org-todo)
  2374. (defcustom org-enable-priority-commands t
  2375. "Non-nil means priority commands are active.
  2376. When nil, these commands will be disabled, so that you never accidentally
  2377. set a priority."
  2378. :group 'org-priorities
  2379. :type 'boolean)
  2380. (defcustom org-highest-priority ?A
  2381. "The highest priority of TODO items. A character like ?A, ?B etc.
  2382. Must have a smaller ASCII number than `org-lowest-priority'."
  2383. :group 'org-priorities
  2384. :type 'character)
  2385. (defcustom org-lowest-priority ?C
  2386. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2387. Must have a larger ASCII number than `org-highest-priority'."
  2388. :group 'org-priorities
  2389. :type 'character)
  2390. (defcustom org-default-priority ?B
  2391. "The default priority of TODO items.
  2392. This is the priority an item gets if no explicit priority is given.
  2393. When starting to cycle on an empty priority the first step in the cycle
  2394. depends on `org-priority-start-cycle-with-default'. The resulting first
  2395. step priority must not exceed the range from `org-highest-priority' to
  2396. `org-lowest-priority' which means that `org-default-priority' has to be
  2397. in this range exclusive or inclusive the range boundaries. Else the
  2398. first step refuses to set the default and the second will fall back
  2399. to (depending on the command used) the highest or lowest priority."
  2400. :group 'org-priorities
  2401. :type 'character)
  2402. (defcustom org-priority-start-cycle-with-default t
  2403. "Non-nil means start with default priority when starting to cycle.
  2404. When this is nil, the first step in the cycle will be (depending on the
  2405. command used) one higher or lower than the default priority.
  2406. See also `org-default-priority'."
  2407. :group 'org-priorities
  2408. :type 'boolean)
  2409. (defcustom org-get-priority-function nil
  2410. "Function to extract the priority from a string.
  2411. The string is normally the headline. If this is nil Org computes the
  2412. priority from the priority cookie like [#A] in the headline. It returns
  2413. an integer, increasing by 1000 for each priority level.
  2414. The user can set a different function here, which should take a string
  2415. as an argument and return the numeric priority."
  2416. :group 'org-priorities
  2417. :version "24.1"
  2418. :type 'function)
  2419. (defgroup org-time nil
  2420. "Options concerning time stamps and deadlines in Org-mode."
  2421. :tag "Org Time"
  2422. :group 'org)
  2423. (defcustom org-insert-labeled-timestamps-at-point nil
  2424. "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
  2425. When nil, these labeled time stamps are forces into the second line of an
  2426. entry, just after the headline. When scheduling from the global TODO list,
  2427. the time stamp will always be forced into the second line."
  2428. :group 'org-time
  2429. :type 'boolean)
  2430. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  2431. "Formats for `format-time-string' which are used for time stamps.
  2432. It is not recommended to change this constant.")
  2433. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2434. "Number of minutes to round time stamps to.
  2435. These are two values, the first applies when first creating a time stamp.
  2436. The second applies when changing it with the commands `S-up' and `S-down'.
  2437. When changing the time stamp, this means that it will change in steps
  2438. of N minutes, as given by the second value.
  2439. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2440. numbers should be factors of 60, so for example 5, 10, 15.
  2441. When this is larger than 1, you can still force an exact time stamp by using
  2442. a double prefix argument to a time stamp command like `C-c .' or `C-c !',
  2443. and by using a prefix arg to `S-up/down' to specify the exact number
  2444. of minutes to shift."
  2445. :group 'org-time
  2446. :get #'(lambda (var) ; Make sure both elements are there
  2447. (if (integerp (default-value var))
  2448. (list (default-value var) 5)
  2449. (default-value var)))
  2450. :type '(list
  2451. (integer :tag "when inserting times")
  2452. (integer :tag "when modifying times")))
  2453. ;; Normalize old customizations of this variable.
  2454. (when (integerp org-time-stamp-rounding-minutes)
  2455. (setq org-time-stamp-rounding-minutes
  2456. (list org-time-stamp-rounding-minutes
  2457. org-time-stamp-rounding-minutes)))
  2458. (defcustom org-display-custom-times nil
  2459. "Non-nil means overlay custom formats over all time stamps.
  2460. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2461. To turn this on on a per-file basis, insert anywhere in the file:
  2462. #+STARTUP: customtime"
  2463. :group 'org-time
  2464. :set 'set-default
  2465. :type 'sexp)
  2466. (make-variable-buffer-local 'org-display-custom-times)
  2467. (defcustom org-time-stamp-custom-formats
  2468. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2469. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2470. These are overlaid over the default ISO format if the variable
  2471. `org-display-custom-times' is set. Time like %H:%M should be at the
  2472. end of the second format. The custom formats are also honored by export
  2473. commands, if custom time display is turned on at the time of export."
  2474. :group 'org-time
  2475. :type 'sexp)
  2476. (defun org-time-stamp-format (&optional long inactive)
  2477. "Get the right format for a time string."
  2478. (let ((f (if long (cdr org-time-stamp-formats)
  2479. (car org-time-stamp-formats))))
  2480. (if inactive
  2481. (concat "[" (substring f 1 -1) "]")
  2482. f)))
  2483. (defcustom org-time-clocksum-format
  2484. '(:days "%dd " :hours "%d" :require-hours t :minutes ":%02d" :require-minutes t)
  2485. "The format string used when creating CLOCKSUM lines.
  2486. This is also used when Org mode generates a time duration.
  2487. The value can be a single format string containing two
  2488. %-sequences, which will be filled with the number of hours and
  2489. minutes in that order.
  2490. Alternatively, the value can be a plist associating any of the
  2491. keys :years, :months, :weeks, :days, :hours or :minutes with
  2492. format strings. The time duration is formatted using only the
  2493. time components that are needed and concatenating the results.
  2494. If a time unit in absent, it falls back to the next smallest
  2495. unit.
  2496. The keys :require-years, :require-months, :require-days,
  2497. :require-weeks, :require-hours, :require-minutes are also
  2498. meaningful. A non-nil value for these keys indicates that the
  2499. corresponding time component should always be included, even if
  2500. its value is 0.
  2501. For example,
  2502. \(:days \"%dd\" :hours \"%d\" :require-hours t :minutes \":%02d\"
  2503. :require-minutes t)
  2504. means durations longer than a day will be expressed in days,
  2505. hours and minutes, and durations less than a day will always be
  2506. expressed in hours and minutes (even for durations less than an
  2507. hour).
  2508. The value
  2509. \(:days \"%dd\" :minutes \"%dm\")
  2510. means durations longer than a day will be expressed in days and
  2511. minutes, and durations less than a day will be expressed entirely
  2512. in minutes (even for durations longer than an hour)."
  2513. :group 'org-time
  2514. :group 'org-clock
  2515. :version "24.4"
  2516. :package-version '(Org . "8.0")
  2517. :type '(choice (string :tag "Format string")
  2518. (set :tag "Plist"
  2519. (group :inline t (const :tag "Years" :years)
  2520. (string :tag "Format string"))
  2521. (group :inline t
  2522. (const :tag "Always show years" :require-years)
  2523. (const t))
  2524. (group :inline t (const :tag "Months" :months)
  2525. (string :tag "Format string"))
  2526. (group :inline t
  2527. (const :tag "Always show months" :require-months)
  2528. (const t))
  2529. (group :inline t (const :tag "Weeks" :weeks)
  2530. (string :tag "Format string"))
  2531. (group :inline t
  2532. (const :tag "Always show weeks" :require-weeks)
  2533. (const t))
  2534. (group :inline t (const :tag "Days" :days)
  2535. (string :tag "Format string"))
  2536. (group :inline t
  2537. (const :tag "Always show days" :require-days)
  2538. (const t))
  2539. (group :inline t (const :tag "Hours" :hours)
  2540. (string :tag "Format string"))
  2541. (group :inline t
  2542. (const :tag "Always show hours" :require-hours)
  2543. (const t))
  2544. (group :inline t (const :tag "Minutes" :minutes)
  2545. (string :tag "Format string"))
  2546. (group :inline t
  2547. (const :tag "Always show minutes" :require-minutes)
  2548. (const t)))))
  2549. (defcustom org-time-clocksum-use-fractional nil
  2550. "When non-nil, \\[org-clock-display] uses fractional times.
  2551. See `org-time-clocksum-format' for more on time clock formats."
  2552. :group 'org-time
  2553. :group 'org-clock
  2554. :version "24.3"
  2555. :type 'boolean)
  2556. (defcustom org-time-clocksum-use-effort-durations nil
  2557. "When non-nil, \\[org-clock-display] uses effort durations.
  2558. E.g. by default, one day is considered to be a 8 hours effort,
  2559. so a task that has been clocked for 16 hours will be displayed
  2560. as during 2 days in the clock display or in the clocktable.
  2561. See `org-effort-durations' on how to set effort durations
  2562. and `org-time-clocksum-format' for more on time clock formats."
  2563. :group 'org-time
  2564. :group 'org-clock
  2565. :version "24.4"
  2566. :package-version '(Org . "8.0")
  2567. :type 'boolean)
  2568. (defcustom org-time-clocksum-fractional-format "%.2f"
  2569. "The format string used when creating CLOCKSUM lines,
  2570. or when Org mode generates a time duration, if
  2571. `org-time-clocksum-use-fractional' is enabled.
  2572. The value can be a single format string containing one
  2573. %-sequence, which will be filled with the number of hours as
  2574. a float.
  2575. Alternatively, the value can be a plist associating any of the
  2576. keys :years, :months, :weeks, :days, :hours or :minutes with
  2577. a format string. The time duration is formatted using the
  2578. largest time unit which gives a non-zero integer part. If all
  2579. specified formats have zero integer part, the smallest time unit
  2580. is used."
  2581. :group 'org-time
  2582. :type '(choice (string :tag "Format string")
  2583. (set (group :inline t (const :tag "Years" :years)
  2584. (string :tag "Format string"))
  2585. (group :inline t (const :tag "Months" :months)
  2586. (string :tag "Format string"))
  2587. (group :inline t (const :tag "Weeks" :weeks)
  2588. (string :tag "Format string"))
  2589. (group :inline t (const :tag "Days" :days)
  2590. (string :tag "Format string"))
  2591. (group :inline t (const :tag "Hours" :hours)
  2592. (string :tag "Format string"))
  2593. (group :inline t (const :tag "Minutes" :minutes)
  2594. (string :tag "Format string")))))
  2595. (defcustom org-deadline-warning-days 14
  2596. "Number of days before expiration during which a deadline becomes active.
  2597. This variable governs the display in sparse trees and in the agenda.
  2598. When 0 or negative, it means use this number (the absolute value of it)
  2599. even if a deadline has a different individual lead time specified.
  2600. Custom commands can set this variable in the options section."
  2601. :group 'org-time
  2602. :group 'org-agenda-daily/weekly
  2603. :type 'integer)
  2604. (defcustom org-scheduled-delay-days 0
  2605. "Number of days before a scheduled item becomes active.
  2606. This variable governs the display in sparse trees and in the agenda.
  2607. The default value (i.e. 0) means: don't delay scheduled item.
  2608. When negative, it means use this number (the absolute value of it)
  2609. even if a scheduled item has a different individual delay time
  2610. specified.
  2611. Custom commands can set this variable in the options section."
  2612. :group 'org-time
  2613. :group 'org-agenda-daily/weekly
  2614. :version "24.4"
  2615. :package-version '(Org . "8.0")
  2616. :type 'integer)
  2617. (defcustom org-read-date-prefer-future t
  2618. "Non-nil means assume future for incomplete date input from user.
  2619. This affects the following situations:
  2620. 1. The user gives a month but not a year.
  2621. For example, if it is April and you enter \"feb 2\", this will be read
  2622. as Feb 2, *next* year. \"May 5\", however, will be this year.
  2623. 2. The user gives a day, but no month.
  2624. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2625. read this as the third of *next* month. However, if you enter \"17\",
  2626. it will be considered as *this* month.
  2627. If you set this variable to the symbol `time', then also the following
  2628. will work:
  2629. 3. If the user gives a time.
  2630. If the time is before now, it will be interpreted as tomorrow.
  2631. Currently none of this works for ISO week specifications.
  2632. When this option is nil, the current day, month and year will always be
  2633. used as defaults.
  2634. See also `org-agenda-jump-prefer-future'."
  2635. :group 'org-time
  2636. :type '(choice
  2637. (const :tag "Never" nil)
  2638. (const :tag "Check month and day" t)
  2639. (const :tag "Check month, day, and time" time)))
  2640. (defcustom org-agenda-jump-prefer-future 'org-read-date-prefer-future
  2641. "Should the agenda jump command prefer the future for incomplete dates?
  2642. The default is to do the same as configured in `org-read-date-prefer-future'.
  2643. But you can also set a deviating value here.
  2644. This may t or nil, or the symbol `org-read-date-prefer-future'."
  2645. :group 'org-agenda
  2646. :group 'org-time
  2647. :version "24.1"
  2648. :type '(choice
  2649. (const :tag "Use org-read-date-prefer-future"
  2650. org-read-date-prefer-future)
  2651. (const :tag "Never" nil)
  2652. (const :tag "Always" t)))
  2653. (defcustom org-read-date-force-compatible-dates t
  2654. "Should date/time prompt force dates that are guaranteed to work in Emacs?
  2655. Depending on the system Emacs is running on, certain dates cannot
  2656. be represented with the type used internally to represent time.
  2657. Dates between 1970-1-1 and 2038-1-1 can always be represented
  2658. correctly. Some systems allow for earlier dates, some for later,
  2659. some for both. One way to find out it to insert any date into an
  2660. Org buffer, putting the cursor on the year and hitting S-up and
  2661. S-down to test the range.
  2662. When this variable is set to t, the date/time prompt will not let
  2663. you specify dates outside the 1970-2037 range, so it is certain that
  2664. these dates will work in whatever version of Emacs you are
  2665. running, and also that you can move a file from one Emacs implementation
  2666. to another. WHenever Org is forcing the year for you, it will display
  2667. a message and beep.
  2668. When this variable is nil, Org will check if the date is
  2669. representable in the specific Emacs implementation you are using.
  2670. If not, it will force a year, usually the current year, and beep
  2671. to remind you. Currently this setting is not recommended because
  2672. the likelihood that you will open your Org files in an Emacs that
  2673. has limited date range is not negligible.
  2674. A workaround for this problem is to use diary sexp dates for time
  2675. stamps outside of this range."
  2676. :group 'org-time
  2677. :version "24.1"
  2678. :type 'boolean)
  2679. (defcustom org-read-date-display-live t
  2680. "Non-nil means display current interpretation of date prompt live.
  2681. This display will be in an overlay, in the minibuffer."
  2682. :group 'org-time
  2683. :type 'boolean)
  2684. (defcustom org-read-date-popup-calendar t
  2685. "Non-nil means pop up a calendar when prompting for a date.
  2686. In the calendar, the date can be selected with mouse-1. However, the
  2687. minibuffer will also be active, and you can simply enter the date as well.
  2688. When nil, only the minibuffer will be available."
  2689. :group 'org-time
  2690. :type 'boolean)
  2691. (org-defvaralias 'org-popup-calendar-for-date-prompt
  2692. 'org-read-date-popup-calendar)
  2693. (make-obsolete-variable
  2694. 'org-read-date-minibuffer-setup-hook
  2695. "Set `org-read-date-minibuffer-local-map' instead." "24.4")
  2696. (defcustom org-read-date-minibuffer-setup-hook nil
  2697. "Hook to be used to set up keys for the date/time interface.
  2698. Add key definitions to `minibuffer-local-map', which will be a
  2699. temporary copy.
  2700. WARNING: This option is obsolete, you should use
  2701. `org-read-date-minibuffer-local-map' to set up keys."
  2702. :group 'org-time
  2703. :type 'hook)
  2704. (defcustom org-extend-today-until 0
  2705. "The hour when your day really ends. Must be an integer.
  2706. This has influence for the following applications:
  2707. - When switching the agenda to \"today\". It it is still earlier than
  2708. the time given here, the day recognized as TODAY is actually yesterday.
  2709. - When a date is read from the user and it is still before the time given
  2710. here, the current date and time will be assumed to be yesterday, 23:59.
  2711. Also, timestamps inserted in capture templates follow this rule.
  2712. IMPORTANT: This is a feature whose implementation is and likely will
  2713. remain incomplete. Really, it is only here because past midnight seems to
  2714. be the favorite working time of John Wiegley :-)"
  2715. :group 'org-time
  2716. :type 'integer)
  2717. (defcustom org-use-effective-time nil
  2718. "If non-nil, consider `org-extend-today-until' when creating timestamps.
  2719. For example, if `org-extend-today-until' is 8, and it's 4am, then the
  2720. \"effective time\" of any timestamps between midnight and 8am will be
  2721. 23:59 of the previous day."
  2722. :group 'org-time
  2723. :version "24.1"
  2724. :type 'boolean)
  2725. (defcustom org-use-last-clock-out-time-as-effective-time nil
  2726. "When non-nil, use the last clock out time for `org-todo'.
  2727. Note that this option has precedence over the combined use of
  2728. `org-use-effective-time' and `org-extend-today-until'."
  2729. :group 'org-time
  2730. :version "24.4"
  2731. :package-version '(Org . "8.0")
  2732. :type 'boolean)
  2733. (defcustom org-edit-timestamp-down-means-later nil
  2734. "Non-nil means S-down will increase the time in a time stamp.
  2735. When nil, S-up will increase."
  2736. :group 'org-time
  2737. :type 'boolean)
  2738. (defcustom org-calendar-follow-timestamp-change t
  2739. "Non-nil means make the calendar window follow timestamp changes.
  2740. When a timestamp is modified and the calendar window is visible, it will be
  2741. moved to the new date."
  2742. :group 'org-time
  2743. :type 'boolean)
  2744. (defgroup org-tags nil
  2745. "Options concerning tags in Org-mode."
  2746. :tag "Org Tags"
  2747. :group 'org)
  2748. (defcustom org-tag-alist nil
  2749. "List of tags allowed in Org-mode files.
  2750. When this list is nil, Org-mode will base TAG input on what is already in the
  2751. buffer.
  2752. The value of this variable is an alist, the car of each entry must be a
  2753. keyword as a string, the cdr may be a character that is used to select
  2754. that tag through the fast-tag-selection interface.
  2755. See the manual for details."
  2756. :group 'org-tags
  2757. :type '(repeat
  2758. (choice
  2759. (cons (string :tag "Tag name")
  2760. (character :tag "Access char"))
  2761. (list :tag "Start radio group"
  2762. (const :startgroup)
  2763. (option (string :tag "Group description")))
  2764. (list :tag "End radio group"
  2765. (const :endgroup)
  2766. (option (string :tag "Group description")))
  2767. (const :tag "New line" (:newline)))))
  2768. (defcustom org-tag-persistent-alist nil
  2769. "List of tags that will always appear in all Org-mode files.
  2770. This is in addition to any in buffer settings or customizations
  2771. of `org-tag-alist'.
  2772. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  2773. The value of this variable is an alist, the car of each entry must be a
  2774. keyword as a string, the cdr may be a character that is used to select
  2775. that tag through the fast-tag-selection interface.
  2776. See the manual for details.
  2777. To disable these tags on a per-file basis, insert anywhere in the file:
  2778. #+STARTUP: noptag"
  2779. :group 'org-tags
  2780. :type '(repeat
  2781. (choice
  2782. (cons (string :tag "Tag name")
  2783. (character :tag "Access char"))
  2784. (const :tag "Start radio group" (:startgroup))
  2785. (const :tag "End radio group" (:endgroup))
  2786. (const :tag "New line" (:newline)))))
  2787. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  2788. "If non-nil, always offer completion for all tags of all agenda files.
  2789. Instead of customizing this variable directly, you might want to
  2790. set it locally for capture buffers, because there no list of
  2791. tags in that file can be created dynamically (there are none).
  2792. (add-hook 'org-capture-mode-hook
  2793. (lambda ()
  2794. (set (make-local-variable
  2795. 'org-complete-tags-always-offer-all-agenda-tags)
  2796. t)))"
  2797. :group 'org-tags
  2798. :version "24.1"
  2799. :type 'boolean)
  2800. (defvar org-file-tags nil
  2801. "List of tags that can be inherited by all entries in the file.
  2802. The tags will be inherited if the variable `org-use-tag-inheritance'
  2803. says they should be.
  2804. This variable is populated from #+FILETAGS lines.")
  2805. (defcustom org-use-fast-tag-selection 'auto
  2806. "Non-nil means use fast tag selection scheme.
  2807. This is a special interface to select and deselect tags with single keys.
  2808. When nil, fast selection is never used.
  2809. When the symbol `auto', fast selection is used if and only if selection
  2810. characters for tags have been configured, either through the variable
  2811. `org-tag-alist' or through a #+TAGS line in the buffer.
  2812. When t, fast selection is always used and selection keys are assigned
  2813. automatically if necessary."
  2814. :group 'org-tags
  2815. :type '(choice
  2816. (const :tag "Always" t)
  2817. (const :tag "Never" nil)
  2818. (const :tag "When selection characters are configured" 'auto)))
  2819. (defcustom org-fast-tag-selection-single-key nil
  2820. "Non-nil means fast tag selection exits after first change.
  2821. When nil, you have to press RET to exit it.
  2822. During fast tag selection, you can toggle this flag with `C-c'.
  2823. This variable can also have the value `expert'. In this case, the window
  2824. displaying the tags menu is not even shown, until you press C-c again."
  2825. :group 'org-tags
  2826. :type '(choice
  2827. (const :tag "No" nil)
  2828. (const :tag "Yes" t)
  2829. (const :tag "Expert" expert)))
  2830. (defvar org-fast-tag-selection-include-todo nil
  2831. "Non-nil means fast tags selection interface will also offer TODO states.
  2832. This is an undocumented feature, you should not rely on it.")
  2833. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2834. "The column to which tags should be indented in a headline.
  2835. If this number is positive, it specifies the column. If it is negative,
  2836. it means that the tags should be flushright to that column. For example,
  2837. -80 works well for a normal 80 character screen.
  2838. When 0, place tags directly after headline text, with only one space in
  2839. between."
  2840. :group 'org-tags
  2841. :type 'integer)
  2842. (defcustom org-auto-align-tags t
  2843. "Non-nil keeps tags aligned when modifying headlines.
  2844. Some operations (i.e. demoting) change the length of a headline and
  2845. therefore shift the tags around. With this option turned on, after
  2846. each such operation the tags are again aligned to `org-tags-column'."
  2847. :group 'org-tags
  2848. :type 'boolean)
  2849. (defcustom org-use-tag-inheritance t
  2850. "Non-nil means tags in levels apply also for sublevels.
  2851. When nil, only the tags directly given in a specific line apply there.
  2852. This may also be a list of tags that should be inherited, or a regexp that
  2853. matches tags that should be inherited. Additional control is possible
  2854. with the variable `org-tags-exclude-from-inheritance' which gives an
  2855. explicit list of tags to be excluded from inheritance, even if the value of
  2856. `org-use-tag-inheritance' would select it for inheritance.
  2857. If this option is t, a match early-on in a tree can lead to a large
  2858. number of matches in the subtree when constructing the agenda or creating
  2859. a sparse tree. If you only want to see the first match in a tree during
  2860. a search, check out the variable `org-tags-match-list-sublevels'."
  2861. :group 'org-tags
  2862. :type '(choice
  2863. (const :tag "Not" nil)
  2864. (const :tag "Always" t)
  2865. (repeat :tag "Specific tags" (string :tag "Tag"))
  2866. (regexp :tag "Tags matched by regexp")))
  2867. (defcustom org-tags-exclude-from-inheritance nil
  2868. "List of tags that should never be inherited.
  2869. This is a way to exclude a few tags from inheritance. For way to do
  2870. the opposite, to actively allow inheritance for selected tags,
  2871. see the variable `org-use-tag-inheritance'."
  2872. :group 'org-tags
  2873. :type '(repeat (string :tag "Tag")))
  2874. (defun org-tag-inherit-p (tag)
  2875. "Check if TAG is one that should be inherited."
  2876. (cond
  2877. ((member tag org-tags-exclude-from-inheritance) nil)
  2878. ((eq org-use-tag-inheritance t) t)
  2879. ((not org-use-tag-inheritance) nil)
  2880. ((stringp org-use-tag-inheritance)
  2881. (string-match org-use-tag-inheritance tag))
  2882. ((listp org-use-tag-inheritance)
  2883. (member tag org-use-tag-inheritance))
  2884. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2885. (defcustom org-tags-match-list-sublevels t
  2886. "Non-nil means list also sublevels of headlines matching a search.
  2887. This variable applies to tags/property searches, and also to stuck
  2888. projects because this search is based on a tags match as well.
  2889. When set to the symbol `indented', sublevels are indented with
  2890. leading dots.
  2891. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2892. the sublevels of a headline matching a tag search often also match
  2893. the same search. Listing all of them can create very long lists.
  2894. Setting this variable to nil causes subtrees of a match to be skipped.
  2895. This variable is semi-obsolete and probably should always be true. It
  2896. is better to limit inheritance to certain tags using the variables
  2897. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2898. :group 'org-tags
  2899. :type '(choice
  2900. (const :tag "No, don't list them" nil)
  2901. (const :tag "Yes, do list them" t)
  2902. (const :tag "List them, indented with leading dots" indented)))
  2903. (defcustom org-tags-sort-function nil
  2904. "When set, tags are sorted using this function as a comparator."
  2905. :group 'org-tags
  2906. :type '(choice
  2907. (const :tag "No sorting" nil)
  2908. (const :tag "Alphabetical" string<)
  2909. (const :tag "Reverse alphabetical" string>)
  2910. (function :tag "Custom function" nil)))
  2911. (defvar org-tags-history nil
  2912. "History of minibuffer reads for tags.")
  2913. (defvar org-last-tags-completion-table nil
  2914. "The last used completion table for tags.")
  2915. (defvar org-after-tags-change-hook nil
  2916. "Hook that is run after the tags in a line have changed.")
  2917. (defgroup org-properties nil
  2918. "Options concerning properties in Org-mode."
  2919. :tag "Org Properties"
  2920. :group 'org)
  2921. (defcustom org-property-format "%-10s %s"
  2922. "How property key/value pairs should be formatted by `indent-line'.
  2923. When `indent-line' hits a property definition, it will format the line
  2924. according to this format, mainly to make sure that the values are
  2925. lined-up with respect to each other."
  2926. :group 'org-properties
  2927. :type 'string)
  2928. (defcustom org-properties-postprocess-alist nil
  2929. "Alist of properties and functions to adjust inserted values.
  2930. Elements of this alist must be of the form
  2931. ([string] [function])
  2932. where [string] must be a property name and [function] must be a
  2933. lambda expression: this lambda expression must take one argument,
  2934. the value to adjust, and return the new value as a string.
  2935. For example, this element will allow the property \"Remaining\"
  2936. to be updated wrt the relation between the \"Effort\" property
  2937. and the clock summary:
  2938. ((\"Remaining\" (lambda(value)
  2939. (let ((clocksum (org-clock-sum-current-item))
  2940. (effort (org-duration-string-to-minutes
  2941. (org-entry-get (point) \"Effort\"))))
  2942. (org-minutes-to-clocksum-string (- effort clocksum))))))"
  2943. :group 'org-properties
  2944. :version "24.1"
  2945. :type '(alist :key-type (string :tag "Property")
  2946. :value-type (function :tag "Function")))
  2947. (defcustom org-use-property-inheritance nil
  2948. "Non-nil means properties apply also for sublevels.
  2949. This setting is chiefly used during property searches. Turning it on can
  2950. cause significant overhead when doing a search, which is why it is not
  2951. on by default.
  2952. When nil, only the properties directly given in the current entry count.
  2953. When t, every property is inherited. The value may also be a list of
  2954. properties that should have inheritance, or a regular expression matching
  2955. properties that should be inherited.
  2956. However, note that some special properties use inheritance under special
  2957. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2958. and the properties ending in \"_ALL\" when they are used as descriptor
  2959. for valid values of a property.
  2960. Note for programmers:
  2961. When querying an entry with `org-entry-get', you can control if inheritance
  2962. should be used. By default, `org-entry-get' looks only at the local
  2963. properties. You can request inheritance by setting the inherit argument
  2964. to t (to force inheritance) or to `selective' (to respect the setting
  2965. in this variable)."
  2966. :group 'org-properties
  2967. :type '(choice
  2968. (const :tag "Not" nil)
  2969. (const :tag "Always" t)
  2970. (repeat :tag "Specific properties" (string :tag "Property"))
  2971. (regexp :tag "Properties matched by regexp")))
  2972. (defun org-property-inherit-p (property)
  2973. "Check if PROPERTY is one that should be inherited."
  2974. (cond
  2975. ((eq org-use-property-inheritance t) t)
  2976. ((not org-use-property-inheritance) nil)
  2977. ((stringp org-use-property-inheritance)
  2978. (string-match org-use-property-inheritance property))
  2979. ((listp org-use-property-inheritance)
  2980. (member property org-use-property-inheritance))
  2981. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2982. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2983. "The default column format, if no other format has been defined.
  2984. This variable can be set on the per-file basis by inserting a line
  2985. #+COLUMNS: %25ITEM ....."
  2986. :group 'org-properties
  2987. :type 'string)
  2988. (defcustom org-columns-ellipses ".."
  2989. "The ellipses to be used when a field in column view is truncated.
  2990. When this is the empty string, as many characters as possible are shown,
  2991. but then there will be no visual indication that the field has been truncated.
  2992. When this is a string of length N, the last N characters of a truncated
  2993. field are replaced by this string. If the column is narrower than the
  2994. ellipses string, only part of the ellipses string will be shown."
  2995. :group 'org-properties
  2996. :type 'string)
  2997. (defcustom org-columns-modify-value-for-display-function nil
  2998. "Function that modifies values for display in column view.
  2999. For example, it can be used to cut out a certain part from a time stamp.
  3000. The function must take 2 arguments:
  3001. column-title The title of the column (*not* the property name)
  3002. value The value that should be modified.
  3003. The function should return the value that should be displayed,
  3004. or nil if the normal value should be used."
  3005. :group 'org-properties
  3006. :type 'function)
  3007. (defcustom org-effort-property "Effort"
  3008. "The property that is being used to keep track of effort estimates.
  3009. Effort estimates given in this property need to have the format H:MM."
  3010. :group 'org-properties
  3011. :group 'org-progress
  3012. :type '(string :tag "Property"))
  3013. (defconst org-global-properties-fixed
  3014. '(("VISIBILITY_ALL" . "folded children content all")
  3015. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  3016. "List of property/value pairs that can be inherited by any entry.
  3017. These are fixed values, for the preset properties. The user variable
  3018. that can be used to add to this list is `org-global-properties'.
  3019. The entries in this list are cons cells where the car is a property
  3020. name and cdr is a string with the value. If the value represents
  3021. multiple items like an \"_ALL\" property, separate the items by
  3022. spaces.")
  3023. (defcustom org-global-properties nil
  3024. "List of property/value pairs that can be inherited by any entry.
  3025. This list will be combined with the constant `org-global-properties-fixed'.
  3026. The entries in this list are cons cells where the car is a property
  3027. name and cdr is a string with the value.
  3028. You can set buffer-local values for the same purpose in the variable
  3029. `org-file-properties' this by adding lines like
  3030. #+PROPERTY: NAME VALUE"
  3031. :group 'org-properties
  3032. :type '(repeat
  3033. (cons (string :tag "Property")
  3034. (string :tag "Value"))))
  3035. (defvar org-file-properties nil
  3036. "List of property/value pairs that can be inherited by any entry.
  3037. Valid for the current buffer.
  3038. This variable is populated from #+PROPERTY lines.")
  3039. (make-variable-buffer-local 'org-file-properties)
  3040. (defgroup org-agenda nil
  3041. "Options concerning agenda views in Org-mode."
  3042. :tag "Org Agenda"
  3043. :group 'org)
  3044. (defvar org-category nil
  3045. "Variable used by org files to set a category for agenda display.
  3046. Such files should use a file variable to set it, for example
  3047. # -*- mode: org; org-category: \"ELisp\"
  3048. or contain a special line
  3049. #+CATEGORY: ELisp
  3050. If the file does not specify a category, then file's base name
  3051. is used instead.")
  3052. (make-variable-buffer-local 'org-category)
  3053. (put 'org-category 'safe-local-variable #'(lambda (x) (or (symbolp x) (stringp x))))
  3054. (defcustom org-agenda-files nil
  3055. "The files to be used for agenda display.
  3056. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  3057. \\[org-remove-file]. You can also use customize to edit the list.
  3058. If an entry is a directory, all files in that directory that are matched by
  3059. `org-agenda-file-regexp' will be part of the file list.
  3060. If the value of the variable is not a list but a single file name, then
  3061. the list of agenda files is actually stored and maintained in that file, one
  3062. agenda file per line. In this file paths can be given relative to
  3063. `org-directory'. Tilde expansion and environment variable substitution
  3064. are also made."
  3065. :group 'org-agenda
  3066. :type '(choice
  3067. (repeat :tag "List of files and directories" file)
  3068. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  3069. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  3070. "Regular expression to match files for `org-agenda-files'.
  3071. If any element in the list in that variable contains a directory instead
  3072. of a normal file, all files in that directory that are matched by this
  3073. regular expression will be included."
  3074. :group 'org-agenda
  3075. :type 'regexp)
  3076. (defcustom org-agenda-text-search-extra-files nil
  3077. "List of extra files to be searched by text search commands.
  3078. These files will be search in addition to the agenda files by the
  3079. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  3080. Note that these files will only be searched for text search commands,
  3081. not for the other agenda views like todo lists, tag searches or the weekly
  3082. agenda. This variable is intended to list notes and possibly archive files
  3083. that should also be searched by these two commands.
  3084. In fact, if the first element in the list is the symbol `agenda-archives',
  3085. than all archive files of all agenda files will be added to the search
  3086. scope."
  3087. :group 'org-agenda
  3088. :type '(set :greedy t
  3089. (const :tag "Agenda Archives" agenda-archives)
  3090. (repeat :inline t (file))))
  3091. (org-defvaralias 'org-agenda-multi-occur-extra-files
  3092. 'org-agenda-text-search-extra-files)
  3093. (defcustom org-agenda-skip-unavailable-files nil
  3094. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  3095. A nil value means to remove them, after a query, from the list."
  3096. :group 'org-agenda
  3097. :type 'boolean)
  3098. (defcustom org-calendar-to-agenda-key [?c]
  3099. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  3100. The command `org-calendar-goto-agenda' will be bound to this key. The
  3101. default is the character `c' because then `c' can be used to switch back and
  3102. forth between agenda and calendar."
  3103. :group 'org-agenda
  3104. :type 'sexp)
  3105. (defcustom org-calendar-insert-diary-entry-key [?i]
  3106. "The key to be installed in `calendar-mode-map' for adding diary entries.
  3107. This option is irrelevant until `org-agenda-diary-file' has been configured
  3108. to point to an Org-mode file. When that is the case, the command
  3109. `org-agenda-diary-entry' will be bound to the key given here, by default
  3110. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  3111. if you want to continue doing this, you need to change this to a different
  3112. key."
  3113. :group 'org-agenda
  3114. :type 'sexp)
  3115. (defcustom org-agenda-diary-file 'diary-file
  3116. "File to which to add new entries with the `i' key in agenda and calendar.
  3117. When this is the symbol `diary-file', the functionality in the Emacs
  3118. calendar will be used to add entries to the `diary-file'. But when this
  3119. points to a file, `org-agenda-diary-entry' will be used instead."
  3120. :group 'org-agenda
  3121. :type '(choice
  3122. (const :tag "The standard Emacs diary file" diary-file)
  3123. (file :tag "Special Org file diary entries")))
  3124. (eval-after-load "calendar"
  3125. '(progn
  3126. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  3127. 'org-calendar-goto-agenda)
  3128. (add-hook 'calendar-mode-hook
  3129. (lambda ()
  3130. (unless (eq org-agenda-diary-file 'diary-file)
  3131. (define-key calendar-mode-map
  3132. org-calendar-insert-diary-entry-key
  3133. 'org-agenda-diary-entry))))))
  3134. (defgroup org-latex nil
  3135. "Options for embedding LaTeX code into Org-mode."
  3136. :tag "Org LaTeX"
  3137. :group 'org)
  3138. (defcustom org-format-latex-options
  3139. '(:foreground default :background default :scale 1.0
  3140. :html-foreground "Black" :html-background "Transparent"
  3141. :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  3142. "Options for creating images from LaTeX fragments.
  3143. This is a property list with the following properties:
  3144. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  3145. `default' means use the foreground of the default face.
  3146. `auto' means use the foreground from the text face.
  3147. :background the background color, or \"Transparent\".
  3148. `default' means use the background of the default face.
  3149. `auto' means use the background from the text face.
  3150. :scale a scaling factor for the size of the images, to get more pixels
  3151. :html-foreground, :html-background, :html-scale
  3152. the same numbers for HTML export.
  3153. :matchers a list indicating which matchers should be used to
  3154. find LaTeX fragments. Valid members of this list are:
  3155. \"begin\" find environments
  3156. \"$1\" find single characters surrounded by $.$
  3157. \"$\" find math expressions surrounded by $...$
  3158. \"$$\" find math expressions surrounded by $$....$$
  3159. \"\\(\" find math expressions surrounded by \\(...\\)
  3160. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  3161. :group 'org-latex
  3162. :type 'plist)
  3163. (defcustom org-format-latex-signal-error t
  3164. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  3165. When nil, just push out a message."
  3166. :group 'org-latex
  3167. :version "24.1"
  3168. :type 'boolean)
  3169. (defcustom org-latex-to-mathml-jar-file nil
  3170. "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
  3171. Use this to specify additional executable file say a jar file.
  3172. When using MathToWeb as the converter, specify the full-path to
  3173. your mathtoweb.jar file."
  3174. :group 'org-latex
  3175. :version "24.1"
  3176. :type '(choice
  3177. (const :tag "None" nil)
  3178. (file :tag "JAR file" :must-match t)))
  3179. (defcustom org-latex-to-mathml-convert-command nil
  3180. "Command to convert LaTeX fragments to MathML.
  3181. Replace format-specifiers in the command as noted below and use
  3182. `shell-command' to convert LaTeX to MathML.
  3183. %j: Executable file in fully expanded form as specified by
  3184. `org-latex-to-mathml-jar-file'.
  3185. %I: Input LaTeX file in fully expanded form
  3186. %o: Output MathML file
  3187. This command is used by `org-create-math-formula'.
  3188. When using MathToWeb as the converter, set this to
  3189. \"java -jar %j -unicode -force -df %o %I\"."
  3190. :group 'org-latex
  3191. :version "24.1"
  3192. :type '(choice
  3193. (const :tag "None" nil)
  3194. (string :tag "\nShell command")))
  3195. (defcustom org-latex-create-formula-image-program 'dvipng
  3196. "Program to convert LaTeX fragments with.
  3197. dvipng Process the LaTeX fragments to dvi file, then convert
  3198. dvi files to png files using dvipng.
  3199. This will also include processing of non-math environments.
  3200. imagemagick Convert the LaTeX fragments to pdf files and use imagemagick
  3201. to convert pdf files to png files"
  3202. :group 'org-latex
  3203. :version "24.1"
  3204. :type '(choice
  3205. (const :tag "dvipng" dvipng)
  3206. (const :tag "imagemagick" imagemagick)))
  3207. (defcustom org-latex-preview-ltxpng-directory "ltxpng/"
  3208. "Path to store latex preview images.
  3209. A relative path here creates many directories relative to the
  3210. processed org files paths. An absolute path puts all preview
  3211. images at the same place."
  3212. :group 'org-latex
  3213. :version "24.3"
  3214. :type 'string)
  3215. (defun org-format-latex-mathml-available-p ()
  3216. "Return t if `org-latex-to-mathml-convert-command' is usable."
  3217. (save-match-data
  3218. (when (and (boundp 'org-latex-to-mathml-convert-command)
  3219. org-latex-to-mathml-convert-command)
  3220. (let ((executable (car (split-string
  3221. org-latex-to-mathml-convert-command))))
  3222. (when (executable-find executable)
  3223. (if (string-match
  3224. "%j" org-latex-to-mathml-convert-command)
  3225. (file-readable-p org-latex-to-mathml-jar-file)
  3226. t))))))
  3227. (defcustom org-format-latex-header "\\documentclass{article}
  3228. \\usepackage[usenames]{color}
  3229. \\usepackage{amsmath}
  3230. \\usepackage[mathscr]{eucal}
  3231. \\pagestyle{empty} % do not remove
  3232. \[PACKAGES]
  3233. \[DEFAULT-PACKAGES]
  3234. % The settings below are copied from fullpage.sty
  3235. \\setlength{\\textwidth}{\\paperwidth}
  3236. \\addtolength{\\textwidth}{-3cm}
  3237. \\setlength{\\oddsidemargin}{1.5cm}
  3238. \\addtolength{\\oddsidemargin}{-2.54cm}
  3239. \\setlength{\\evensidemargin}{\\oddsidemargin}
  3240. \\setlength{\\textheight}{\\paperheight}
  3241. \\addtolength{\\textheight}{-\\headheight}
  3242. \\addtolength{\\textheight}{-\\headsep}
  3243. \\addtolength{\\textheight}{-\\footskip}
  3244. \\addtolength{\\textheight}{-3cm}
  3245. \\setlength{\\topmargin}{1.5cm}
  3246. \\addtolength{\\topmargin}{-2.54cm}"
  3247. "The document header used for processing LaTeX fragments.
  3248. It is imperative that this header make sure that no page number
  3249. appears on the page. The package defined in the variables
  3250. `org-latex-default-packages-alist' and `org-latex-packages-alist'
  3251. will either replace the placeholder \"[PACKAGES]\" in this
  3252. header, or they will be appended."
  3253. :group 'org-latex
  3254. :type 'string)
  3255. (defun org-set-packages-alist (var val)
  3256. "Set the packages alist and make sure it has 3 elements per entry."
  3257. (set var (mapcar (lambda (x)
  3258. (if (and (consp x) (= (length x) 2))
  3259. (list (car x) (nth 1 x) t)
  3260. x))
  3261. val)))
  3262. (defun org-get-packages-alist (var)
  3263. "Get the packages alist and make sure it has 3 elements per entry."
  3264. (mapcar (lambda (x)
  3265. (if (and (consp x) (= (length x) 2))
  3266. (list (car x) (nth 1 x) t)
  3267. x))
  3268. (default-value var)))
  3269. (defcustom org-latex-default-packages-alist
  3270. '(("AUTO" "inputenc" t)
  3271. ("T1" "fontenc" t)
  3272. ("" "fixltx2e" nil)
  3273. ("" "graphicx" t)
  3274. ("" "longtable" nil)
  3275. ("" "float" nil)
  3276. ("" "wrapfig" nil)
  3277. ("" "soul" t)
  3278. ("" "textcomp" t)
  3279. ("" "marvosym" t)
  3280. ("" "wasysym" t)
  3281. ("" "latexsym" t)
  3282. ("" "amssymb" t)
  3283. ("" "hyperref" nil)
  3284. "\\tolerance=1000")
  3285. "Alist of default packages to be inserted in the header.
  3286. Change this only if one of the packages here causes an
  3287. incompatibility with another package you are using.
  3288. The packages in this list are needed by one part or another of
  3289. Org mode to function properly:
  3290. - inputenc, fontenc: for basic font and character selection
  3291. - textcomp, marvosymb, wasysym, latexsym, amssym: for various
  3292. symbols used for interpreting the entities in `org-entities'.
  3293. You can skip some of these packages if you don't use any of the
  3294. symbols in it.
  3295. - graphicx: for including images
  3296. - float, wrapfig: for figure placement
  3297. - longtable: for long tables
  3298. - hyperref: for cross references
  3299. Therefore you should not modify this variable unless you know
  3300. what you are doing. The one reason to change it anyway is that
  3301. you might be loading some other package that conflicts with one
  3302. of the default packages. Each cell is of the format
  3303. \( \"options\" \"package\" snippet-flag). If SNIPPET-FLAG is t,
  3304. the package also needs to be included when compiling LaTeX
  3305. snippets into images for inclusion into non-LaTeX output."
  3306. :group 'org-latex
  3307. :group 'org-export-latex
  3308. :set 'org-set-packages-alist
  3309. :get 'org-get-packages-alist
  3310. :version "24.1"
  3311. :type '(repeat
  3312. (choice
  3313. (list :tag "options/package pair"
  3314. (string :tag "options")
  3315. (string :tag "package")
  3316. (boolean :tag "Snippet"))
  3317. (string :tag "A line of LaTeX"))))
  3318. (defcustom org-latex-packages-alist nil
  3319. "Alist of packages to be inserted in every LaTeX header.
  3320. These will be inserted after `org-latex-default-packages-alist'.
  3321. Each cell is of the format:
  3322. \(\"options\" \"package\" snippet-flag)
  3323. SNIPPET-FLAG, when t, indicates that this package is also needed
  3324. when turning LaTeX snippets into images for inclusion into
  3325. non-LaTeX output.
  3326. Make sure that you only list packages here which:
  3327. - you want in every file
  3328. - do not conflict with the setup in `org-format-latex-header'.
  3329. - do not conflict with the default packages in
  3330. `org-latex-default-packages-alist'."
  3331. :group 'org-latex
  3332. :group 'org-export-latex
  3333. :set 'org-set-packages-alist
  3334. :get 'org-get-packages-alist
  3335. :type '(repeat
  3336. (choice
  3337. (list :tag "options/package pair"
  3338. (string :tag "options")
  3339. (string :tag "package")
  3340. (boolean :tag "Snippet"))
  3341. (string :tag "A line of LaTeX"))))
  3342. (defgroup org-appearance nil
  3343. "Settings for Org-mode appearance."
  3344. :tag "Org Appearance"
  3345. :group 'org)
  3346. (defcustom org-level-color-stars-only nil
  3347. "Non-nil means fontify only the stars in each headline.
  3348. When nil, the entire headline is fontified.
  3349. Changing it requires restart of `font-lock-mode' to become effective
  3350. also in regions already fontified."
  3351. :group 'org-appearance
  3352. :type 'boolean)
  3353. (defcustom org-hide-leading-stars nil
  3354. "Non-nil means hide the first N-1 stars in a headline.
  3355. This works by using the face `org-hide' for these stars. This
  3356. face is white for a light background, and black for a dark
  3357. background. You may have to customize the face `org-hide' to
  3358. make this work.
  3359. Changing it requires restart of `font-lock-mode' to become effective
  3360. also in regions already fontified.
  3361. You may also set this on a per-file basis by adding one of the following
  3362. lines to the buffer:
  3363. #+STARTUP: hidestars
  3364. #+STARTUP: showstars"
  3365. :group 'org-appearance
  3366. :type 'boolean)
  3367. (defcustom org-hidden-keywords nil
  3368. "List of symbols corresponding to keywords to be hidden the org buffer.
  3369. For example, a value '(title) for this list will make the document's title
  3370. appear in the buffer without the initial #+TITLE: keyword."
  3371. :group 'org-appearance
  3372. :version "24.1"
  3373. :type '(set (const :tag "#+AUTHOR" author)
  3374. (const :tag "#+DATE" date)
  3375. (const :tag "#+EMAIL" email)
  3376. (const :tag "#+TITLE" title)))
  3377. (defcustom org-custom-properties nil
  3378. "List of properties (as strings) with a special meaning.
  3379. The default use of these custom properties is to let the user
  3380. hide them with `org-toggle-custom-properties-visibility'."
  3381. :group 'org-properties
  3382. :group 'org-appearance
  3383. :version "24.3"
  3384. :type '(repeat (string :tag "Property Name")))
  3385. (defcustom org-fontify-done-headline nil
  3386. "Non-nil means change the face of a headline if it is marked DONE.
  3387. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3388. When this is non-nil, the headline after the keyword is set to the
  3389. `org-headline-done' as an additional indication."
  3390. :group 'org-appearance
  3391. :type 'boolean)
  3392. (defcustom org-fontify-emphasized-text t
  3393. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3394. Changing this variable requires a restart of Emacs to take effect."
  3395. :group 'org-appearance
  3396. :type 'boolean)
  3397. (defcustom org-fontify-whole-heading-line nil
  3398. "Non-nil means fontify the whole line for headings.
  3399. This is useful when setting a background color for the
  3400. org-level-* faces."
  3401. :group 'org-appearance
  3402. :type 'boolean)
  3403. (defcustom org-highlight-latex-and-related nil
  3404. "Non-nil means highlight LaTeX related syntax in the buffer.
  3405. When non nil, the value should be a list containing any of the
  3406. following symbols:
  3407. `latex' Highlight LaTeX snippets and environments.
  3408. `script' Highlight subscript and superscript.
  3409. `entities' Highlight entities."
  3410. :group 'org-appearance
  3411. :version "24.4"
  3412. :package-version '(Org . "8.0")
  3413. :type '(choice
  3414. (const :tag "No highlighting" nil)
  3415. (set :greedy t :tag "Highlight"
  3416. (const :tag "LaTeX snippets and environments" latex)
  3417. (const :tag "Subscript and superscript" script)
  3418. (const :tag "Entities" entities))))
  3419. (defcustom org-hide-emphasis-markers nil
  3420. "Non-nil mean font-lock should hide the emphasis marker characters."
  3421. :group 'org-appearance
  3422. :type 'boolean)
  3423. (defcustom org-pretty-entities nil
  3424. "Non-nil means show entities as UTF8 characters.
  3425. When nil, the \\name form remains in the buffer."
  3426. :group 'org-appearance
  3427. :version "24.1"
  3428. :type 'boolean)
  3429. (defcustom org-pretty-entities-include-sub-superscripts t
  3430. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  3431. :group 'org-appearance
  3432. :version "24.1"
  3433. :type 'boolean)
  3434. (defvar org-emph-re nil
  3435. "Regular expression for matching emphasis.
  3436. After a match, the match groups contain these elements:
  3437. 0 The match of the full regular expression, including the characters
  3438. before and after the proper match
  3439. 1 The character before the proper match, or empty at beginning of line
  3440. 2 The proper match, including the leading and trailing markers
  3441. 3 The leading marker like * or /, indicating the type of highlighting
  3442. 4 The text between the emphasis markers, not including the markers
  3443. 5 The character after the match, empty at the end of a line")
  3444. (defvar org-verbatim-re nil
  3445. "Regular expression for matching verbatim text.")
  3446. (defvar org-emphasis-regexp-components) ; defined just below
  3447. (defvar org-emphasis-alist) ; defined just below
  3448. (defun org-set-emph-re (var val)
  3449. "Set variable and compute the emphasis regular expression."
  3450. (set var val)
  3451. (when (and (boundp 'org-emphasis-alist)
  3452. (boundp 'org-emphasis-regexp-components)
  3453. org-emphasis-alist org-emphasis-regexp-components)
  3454. (let* ((e org-emphasis-regexp-components)
  3455. (pre (car e))
  3456. (post (nth 1 e))
  3457. (border (nth 2 e))
  3458. (body (nth 3 e))
  3459. (nl (nth 4 e))
  3460. (body1 (concat body "*?"))
  3461. (markers (mapconcat 'car org-emphasis-alist ""))
  3462. (vmarkers (mapconcat
  3463. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  3464. org-emphasis-alist "")))
  3465. ;; make sure special characters appear at the right position in the class
  3466. (if (string-match "\\^" markers)
  3467. (setq markers (concat (replace-match "" t t markers) "^")))
  3468. (if (string-match "-" markers)
  3469. (setq markers (concat (replace-match "" t t markers) "-")))
  3470. (if (string-match "\\^" vmarkers)
  3471. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3472. (if (string-match "-" vmarkers)
  3473. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3474. (if (> nl 0)
  3475. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3476. (int-to-string nl) "\\}")))
  3477. ;; Make the regexp
  3478. (setq org-emph-re
  3479. (concat "\\([" pre "]\\|^\\)"
  3480. "\\("
  3481. "\\([" markers "]\\)"
  3482. "\\("
  3483. "[^" border "]\\|"
  3484. "[^" border "]"
  3485. body1
  3486. "[^" border "]"
  3487. "\\)"
  3488. "\\3\\)"
  3489. "\\([" post "]\\|$\\)"))
  3490. (setq org-verbatim-re
  3491. (concat "\\([" pre "]\\|^\\)"
  3492. "\\("
  3493. "\\([" vmarkers "]\\)"
  3494. "\\("
  3495. "[^" border "]\\|"
  3496. "[^" border "]"
  3497. body1
  3498. "[^" border "]"
  3499. "\\)"
  3500. "\\3\\)"
  3501. "\\([" post "]\\|$\\)")))))
  3502. (defcustom org-emphasis-regexp-components
  3503. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  3504. "Components used to build the regular expression for emphasis.
  3505. This is a list with five entries. Terminology: In an emphasis string
  3506. like \" *strong word* \", we call the initial space PREMATCH, the final
  3507. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3508. and \"trong wor\" is the body. The different components in this variable
  3509. specify what is allowed/forbidden in each part:
  3510. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3511. post Chars allowed as postmatch. End of line will be allowed too.
  3512. border The chars *forbidden* as border characters.
  3513. body-regexp A regexp like \".\" to match a body character. Don't use
  3514. non-shy groups here, and don't allow newline here.
  3515. newline The maximum number of newlines allowed in an emphasis exp.
  3516. Use customize to modify this, or restart Emacs after changing it."
  3517. :group 'org-appearance
  3518. :set 'org-set-emph-re
  3519. :type '(list
  3520. (sexp :tag "Allowed chars in pre ")
  3521. (sexp :tag "Allowed chars in post ")
  3522. (sexp :tag "Forbidden chars in border ")
  3523. (sexp :tag "Regexp for body ")
  3524. (integer :tag "number of newlines allowed")
  3525. (option (boolean :tag "Please ignore this button"))))
  3526. (defcustom org-emphasis-alist
  3527. `(("*" bold "<b>" "</b>")
  3528. ("/" italic "<i>" "</i>")
  3529. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  3530. ("=" org-code "<code>" "</code>" verbatim)
  3531. ("~" org-verbatim "<code>" "</code>" verbatim)
  3532. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  3533. "<del>" "</del>")
  3534. )
  3535. "Special syntax for emphasized text.
  3536. Text starting and ending with a special character will be emphasized, for
  3537. example *bold*, _underlined_ and /italic/. This variable sets the marker
  3538. characters, the face to be used by font-lock for highlighting in Org-mode
  3539. Emacs buffers, and the HTML tags to be used for this.
  3540. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  3541. Use customize to modify this, or restart Emacs after changing it."
  3542. :group 'org-appearance
  3543. :set 'org-set-emph-re
  3544. :type '(repeat
  3545. (list
  3546. (string :tag "Marker character")
  3547. (choice
  3548. (face :tag "Font-lock-face")
  3549. (plist :tag "Face property list"))
  3550. (string :tag "HTML start tag")
  3551. (string :tag "HTML end tag")
  3552. (option (const verbatim)))))
  3553. (defvar org-protecting-blocks
  3554. '("src" "example" "latex" "ascii" "html" "ditaa" "dot" "r" "R")
  3555. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3556. This is needed for font-lock setup.")
  3557. ;;; Miscellaneous options
  3558. (defgroup org-completion nil
  3559. "Completion in Org-mode."
  3560. :tag "Org Completion"
  3561. :group 'org)
  3562. (defcustom org-completion-use-ido nil
  3563. "Non-nil means use ido completion wherever possible.
  3564. Note that `ido-mode' must be active for this variable to be relevant.
  3565. If you decide to turn this variable on, you might well want to turn off
  3566. `org-outline-path-complete-in-steps'.
  3567. See also `org-completion-use-iswitchb'."
  3568. :group 'org-completion
  3569. :type 'boolean)
  3570. (defcustom org-completion-use-iswitchb nil
  3571. "Non-nil means use iswitchb completion wherever possible.
  3572. Note that `iswitchb-mode' must be active for this variable to be relevant.
  3573. If you decide to turn this variable on, you might well want to turn off
  3574. `org-outline-path-complete-in-steps'.
  3575. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  3576. :group 'org-completion
  3577. :type 'boolean)
  3578. (defcustom org-completion-fallback-command 'hippie-expand
  3579. "The expansion command called by \\[pcomplete] in normal context.
  3580. Normal means, no org-mode-specific context."
  3581. :group 'org-completion
  3582. :type 'function)
  3583. ;;; Functions and variables from their packages
  3584. ;; Declared here to avoid compiler warnings
  3585. ;; XEmacs only
  3586. (defvar outline-mode-menu-heading)
  3587. (defvar outline-mode-menu-show)
  3588. (defvar outline-mode-menu-hide)
  3589. (defvar zmacs-regions) ; XEmacs regions
  3590. ;; Emacs only
  3591. (defvar mark-active)
  3592. ;; Various packages
  3593. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  3594. (declare-function calendar-forward-day "cal-move" (arg))
  3595. (declare-function calendar-goto-date "cal-move" (date))
  3596. (declare-function calendar-goto-today "cal-move" ())
  3597. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3598. (defvar calc-embedded-close-formula)
  3599. (defvar calc-embedded-open-formula)
  3600. (declare-function cdlatex-tab "ext:cdlatex" ())
  3601. (declare-function cdlatex-compute-tables "ext:cdlatex" ())
  3602. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3603. (defvar font-lock-unfontify-region-function)
  3604. (declare-function iswitchb-read-buffer "iswitchb"
  3605. (prompt &optional default require-match start matches-set))
  3606. (defvar iswitchb-temp-buflist)
  3607. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3608. (defvar org-agenda-tags-todo-honor-ignore-options)
  3609. (declare-function org-agenda-skip "org-agenda" ())
  3610. (declare-function
  3611. org-agenda-format-item "org-agenda"
  3612. (extra txt &optional level category tags dotime noprefix remove-re habitp))
  3613. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3614. (declare-function org-agenda-change-all-lines "org-agenda"
  3615. (newhead hdmarker &optional fixface just-this))
  3616. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3617. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3618. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  3619. (beg end))
  3620. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3621. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3622. "org-agenda" (&optional end))
  3623. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3624. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  3625. (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
  3626. (declare-function org-inlinetask-goto-end "org-inlinetask" ())
  3627. (declare-function org-indent-mode "org-indent" (&optional arg))
  3628. (declare-function parse-time-string "parse-time" (string))
  3629. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3630. (declare-function orgtbl-send-table "org-table" (&optional maybe))
  3631. (defvar remember-data-file)
  3632. (defvar texmathp-why)
  3633. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3634. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3635. (defvar org-latex-regexps)
  3636. ;;; Autoload and prepare some org modules
  3637. ;; Some table stuff that needs to be defined here, because it is used
  3638. ;; by the functions setting up org-mode or checking for table context.
  3639. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  3640. "Detect an org-type or table-type table.")
  3641. (defconst org-table-line-regexp "^[ \t]*|"
  3642. "Detect an org-type table line.")
  3643. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  3644. "Detect an org-type table line.")
  3645. (defconst org-table-hline-regexp "^[ \t]*|-"
  3646. "Detect an org-type table hline.")
  3647. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  3648. "Detect a table-type table hline.")
  3649. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  3650. "Detect the first line outside a table when searching from within it.
  3651. This works for both table types.")
  3652. ;; Autoload the functions in org-table.el that are needed by functions here.
  3653. (eval-and-compile
  3654. (org-autoload "org-table"
  3655. '(org-table-begin org-table-blank-field org-table-end)))
  3656. ;;;###autoload
  3657. (defun turn-on-orgtbl ()
  3658. "Unconditionally turn on `orgtbl-mode'."
  3659. (require 'org-table)
  3660. (orgtbl-mode 1))
  3661. (defun org-at-table-p (&optional table-type)
  3662. "Return t if the cursor is inside an org-type table.
  3663. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  3664. (if org-enable-table-editor
  3665. (save-excursion
  3666. (beginning-of-line 1)
  3667. (looking-at (if table-type org-table-any-line-regexp
  3668. org-table-line-regexp)))
  3669. nil))
  3670. (defsubst org-table-p () (org-at-table-p))
  3671. (defun org-at-table.el-p ()
  3672. "Return t if and only if we are at a table.el table."
  3673. (and (org-at-table-p 'any)
  3674. (save-excursion
  3675. (goto-char (org-table-begin 'any))
  3676. (looking-at org-table1-hline-regexp))))
  3677. (defun org-table-recognize-table.el ()
  3678. "If there is a table.el table nearby, recognize it and move into it."
  3679. (if org-table-tab-recognizes-table.el
  3680. (if (org-at-table.el-p)
  3681. (progn
  3682. (beginning-of-line 1)
  3683. (if (looking-at org-table-dataline-regexp)
  3684. nil
  3685. (if (looking-at org-table1-hline-regexp)
  3686. (progn
  3687. (beginning-of-line 2)
  3688. (if (looking-at org-table-any-border-regexp)
  3689. (beginning-of-line -1)))))
  3690. (if (re-search-forward "|" (org-table-end t) t)
  3691. (progn
  3692. (require 'table)
  3693. (if (table--at-cell-p (point))
  3694. t
  3695. (message "recognizing table.el table...")
  3696. (table-recognize-table)
  3697. (message "recognizing table.el table...done")))
  3698. (error "This should not happen"))
  3699. t)
  3700. nil)
  3701. nil))
  3702. (defun org-at-table-hline-p ()
  3703. "Return t if the cursor is inside a hline in a table."
  3704. (if org-enable-table-editor
  3705. (save-excursion
  3706. (beginning-of-line 1)
  3707. (looking-at org-table-hline-regexp))
  3708. nil))
  3709. (defvar org-table-clean-did-remove-column nil)
  3710. (defun org-table-map-tables (function &optional quietly)
  3711. "Apply FUNCTION to the start of all tables in the buffer."
  3712. (save-excursion
  3713. (save-restriction
  3714. (widen)
  3715. (goto-char (point-min))
  3716. (while (re-search-forward org-table-any-line-regexp nil t)
  3717. (unless quietly
  3718. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
  3719. (beginning-of-line 1)
  3720. (when (and (looking-at org-table-line-regexp)
  3721. ;; Exclude tables in src/example/verbatim/clocktable blocks
  3722. (not (org-in-block-p '("src" "example" "verbatim" "clocktable"))))
  3723. (save-excursion (funcall function))
  3724. (or (looking-at org-table-line-regexp)
  3725. (forward-char 1)))
  3726. (re-search-forward org-table-any-border-regexp nil 1))))
  3727. (unless quietly (message "Mapping tables: done")))
  3728. ;; Declare and autoload functions from ox.el and al.
  3729. (declare-function org-export-get-environment "ox"
  3730. (&optional backend subtreep ext-plist))
  3731. (declare-function org-latex-guess-inputenc "ox-latex" (header))
  3732. ;; Declare and autoload functions from org-agenda.el
  3733. (eval-and-compile
  3734. (org-autoload "org-agenda"
  3735. '(org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  3736. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock" (beg end))
  3737. (declare-function org-clock-update-mode-line "org-clock" ())
  3738. (declare-function org-resolve-clocks "org-clock"
  3739. (&optional also-non-dangling-p prompt last-valid))
  3740. (defvar org-clock-start-time)
  3741. (defvar org-clock-marker (make-marker)
  3742. "Marker recording the last clock-in.")
  3743. (defvar org-clock-hd-marker (make-marker)
  3744. "Marker recording the last clock-in, but the headline position.")
  3745. (defvar org-clock-heading ""
  3746. "The heading of the current clock entry.")
  3747. (defun org-clock-is-active ()
  3748. "Return non-nil if clock is currently running.
  3749. The return value is actually the clock marker."
  3750. (marker-buffer org-clock-marker))
  3751. (eval-and-compile
  3752. (org-autoload "org-clock" '(org-clock-remove-overlays
  3753. org-clock-update-time-maybe
  3754. org-clocktable-shift)))
  3755. (defun org-check-running-clock ()
  3756. "Check if the current buffer contains the running clock.
  3757. If yes, offer to stop it and to save the buffer with the changes."
  3758. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3759. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3760. (buffer-name))))
  3761. (org-clock-out)
  3762. (when (y-or-n-p "Save changed buffer?")
  3763. (save-buffer))))
  3764. (defun org-clocktable-try-shift (dir n)
  3765. "Check if this line starts a clock table, if yes, shift the time block."
  3766. (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
  3767. (org-clocktable-shift dir n)))
  3768. ;;;###autoload
  3769. (defun org-clock-persistence-insinuate ()
  3770. "Set up hooks for clock persistence."
  3771. (require 'org-clock)
  3772. (add-hook 'org-mode-hook 'org-clock-load)
  3773. (add-hook 'kill-emacs-hook 'org-clock-save))
  3774. ;; Define the variable already here, to make sure we have it.
  3775. (defvar org-indent-mode nil
  3776. "Non-nil if Org-Indent mode is enabled.
  3777. Use the command `org-indent-mode' to change this variable.")
  3778. ;; Autoload archiving code
  3779. ;; The stuff that is needed for cycling and tags has to be defined here.
  3780. (defgroup org-archive nil
  3781. "Options concerning archiving in Org-mode."
  3782. :tag "Org Archive"
  3783. :group 'org-structure)
  3784. (defcustom org-archive-location "%s_archive::"
  3785. "The location where subtrees should be archived.
  3786. The value of this variable is a string, consisting of two parts,
  3787. separated by a double-colon. The first part is a filename and
  3788. the second part is a headline.
  3789. When the filename is omitted, archiving happens in the same file.
  3790. %s in the filename will be replaced by the current file
  3791. name (without the directory part). Archiving to a different file
  3792. is useful to keep archived entries from contributing to the
  3793. Org-mode Agenda.
  3794. The archived entries will be filed as subtrees of the specified
  3795. headline. When the headline is omitted, the subtrees are simply
  3796. filed away at the end of the file, as top-level entries. Also in
  3797. the heading you can use %s to represent the file name, this can be
  3798. useful when using the same archive for a number of different files.
  3799. Here are a few examples:
  3800. \"%s_archive::\"
  3801. If the current file is Projects.org, archive in file
  3802. Projects.org_archive, as top-level trees. This is the default.
  3803. \"::* Archived Tasks\"
  3804. Archive in the current file, under the top-level headline
  3805. \"* Archived Tasks\".
  3806. \"~/org/archive.org::\"
  3807. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  3808. \"~/org/archive.org::* From %s\"
  3809. Archive in file ~/org/archive.org (absolute path), under headlines
  3810. \"From FILENAME\" where file name is the current file name.
  3811. \"~/org/datetree.org::datetree/* Finished Tasks\"
  3812. The \"datetree/\" string is special, signifying to archive
  3813. items to the datetree. Items are placed in either the CLOSED
  3814. date of the item, or the current date if there is no CLOSED date.
  3815. The heading will be a subentry to the current date. There doesn't
  3816. need to be a heading, but there always needs to be a slash after
  3817. datetree. For example, to store archived items directly in the
  3818. datetree, use \"~/org/datetree.org::datetree/\".
  3819. \"basement::** Finished Tasks\"
  3820. Archive in file ./basement (relative path), as level 3 trees
  3821. below the level 2 heading \"** Finished Tasks\".
  3822. You may set this option on a per-file basis by adding to the buffer a
  3823. line like
  3824. #+ARCHIVE: basement::** Finished Tasks
  3825. You may also define it locally for a subtree by setting an ARCHIVE property
  3826. in the entry. If such a property is found in an entry, or anywhere up
  3827. the hierarchy, it will be used."
  3828. :group 'org-archive
  3829. :type 'string)
  3830. (defcustom org-archive-tag "ARCHIVE"
  3831. "The tag that marks a subtree as archived.
  3832. An archived subtree does not open during visibility cycling, and does
  3833. not contribute to the agenda listings.
  3834. After changing this, font-lock must be restarted in the relevant buffers to
  3835. get the proper fontification."
  3836. :group 'org-archive
  3837. :group 'org-keywords
  3838. :type 'string)
  3839. (defcustom org-agenda-skip-archived-trees t
  3840. "Non-nil means the agenda will skip any items located in archived trees.
  3841. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  3842. variable is no longer recommended, you should leave it at the value t.
  3843. Instead, use the key `v' to cycle the archives-mode in the agenda."
  3844. :group 'org-archive
  3845. :group 'org-agenda-skip
  3846. :type 'boolean)
  3847. (defcustom org-columns-skip-archived-trees t
  3848. "Non-nil means ignore archived trees when creating column view."
  3849. :group 'org-archive
  3850. :group 'org-properties
  3851. :type 'boolean)
  3852. (defcustom org-cycle-open-archived-trees nil
  3853. "Non-nil means `org-cycle' will open archived trees.
  3854. An archived tree is a tree marked with the tag ARCHIVE.
  3855. When nil, archived trees will stay folded. You can still open them with
  3856. normal outline commands like `show-all', but not with the cycling commands."
  3857. :group 'org-archive
  3858. :group 'org-cycle
  3859. :type 'boolean)
  3860. (defcustom org-sparse-tree-open-archived-trees nil
  3861. "Non-nil means sparse tree construction shows matches in archived trees.
  3862. When nil, matches in these trees are highlighted, but the trees are kept in
  3863. collapsed state."
  3864. :group 'org-archive
  3865. :group 'org-sparse-trees
  3866. :type 'boolean)
  3867. (defcustom org-sparse-tree-default-date-type 'scheduled-or-deadline
  3868. "The default date type when building a sparse tree.
  3869. When this is nil, a date is a scheduled or a deadline timestamp.
  3870. Otherwise, these types are allowed:
  3871. all: all timestamps
  3872. active: only active timestamps (<...>)
  3873. inactive: only inactive timestamps (<...)
  3874. scheduled: only scheduled timestamps
  3875. deadline: only deadline timestamps"
  3876. :type '(choice (const :tag "Scheduled or deadline" 'scheduled-or-deadline)
  3877. (const :tag "All timestamps" all)
  3878. (const :tag "Only active timestamps" active)
  3879. (const :tag "Only inactive timestamps" inactive)
  3880. (const :tag "Only scheduled timestamps" scheduled)
  3881. (const :tag "Only deadline timestamps" deadline))
  3882. :version "24.3"
  3883. :group 'org-sparse-trees)
  3884. (defun org-cycle-hide-archived-subtrees (state)
  3885. "Re-hide all archived subtrees after a visibility state change."
  3886. (when (and (not org-cycle-open-archived-trees)
  3887. (not (memq state '(overview folded))))
  3888. (save-excursion
  3889. (let* ((globalp (memq state '(contents all)))
  3890. (beg (if globalp (point-min) (point)))
  3891. (end (if globalp (point-max) (org-end-of-subtree t))))
  3892. (org-hide-archived-subtrees beg end)
  3893. (goto-char beg)
  3894. (if (looking-at (concat ".*:" org-archive-tag ":"))
  3895. (message "%s" (substitute-command-keys
  3896. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  3897. (defun org-force-cycle-archived ()
  3898. "Cycle subtree even if it is archived."
  3899. (interactive)
  3900. (setq this-command 'org-cycle)
  3901. (let ((org-cycle-open-archived-trees t))
  3902. (call-interactively 'org-cycle)))
  3903. (defun org-hide-archived-subtrees (beg end)
  3904. "Re-hide all archived subtrees after a visibility state change."
  3905. (save-excursion
  3906. (let* ((re (concat ":" org-archive-tag ":")))
  3907. (goto-char beg)
  3908. (while (re-search-forward re end t)
  3909. (when (org-at-heading-p)
  3910. (org-flag-subtree t)
  3911. (org-end-of-subtree t))))))
  3912. (declare-function outline-end-of-heading "outline" ())
  3913. (declare-function outline-flag-region "outline" (from to flag))
  3914. (defun org-flag-subtree (flag)
  3915. (save-excursion
  3916. (org-back-to-heading t)
  3917. (outline-end-of-heading)
  3918. (outline-flag-region (point)
  3919. (progn (org-end-of-subtree t) (point))
  3920. flag)))
  3921. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  3922. (eval-and-compile
  3923. (org-autoload "org-archive"
  3924. '(org-add-archive-files)))
  3925. ;; Autoload Column View Code
  3926. (declare-function org-columns-number-to-string "org-colview" (n fmt &optional printf))
  3927. (declare-function org-columns-get-format-and-top-level "org-colview" ())
  3928. (declare-function org-columns-compute "org-colview" (property))
  3929. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  3930. '(org-columns-number-to-string
  3931. org-columns-get-format-and-top-level
  3932. org-columns-compute
  3933. org-columns-remove-overlays))
  3934. ;; Autoload ID code
  3935. (declare-function org-id-store-link "org-id")
  3936. (declare-function org-id-locations-load "org-id")
  3937. (declare-function org-id-locations-save "org-id")
  3938. (defvar org-id-track-globally)
  3939. (org-autoload "org-id"
  3940. '(org-id-new
  3941. org-id-copy
  3942. org-id-get-with-outline-path-completion
  3943. org-id-get-with-outline-drilling))
  3944. ;;; Variables for pre-computed regular expressions, all buffer local
  3945. (defvar org-drawer-regexp "^[ \t]*:PROPERTIES:[ \t]*$"
  3946. "Matches first line of a hidden block.")
  3947. (make-variable-buffer-local 'org-drawer-regexp)
  3948. (defvar org-todo-regexp nil
  3949. "Matches any of the TODO state keywords.")
  3950. (make-variable-buffer-local 'org-todo-regexp)
  3951. (defvar org-not-done-regexp nil
  3952. "Matches any of the TODO state keywords except the last one.")
  3953. (make-variable-buffer-local 'org-not-done-regexp)
  3954. (defvar org-not-done-heading-regexp nil
  3955. "Matches a TODO headline that is not done.")
  3956. (make-variable-buffer-local 'org-not-done-regexp)
  3957. (defvar org-todo-line-regexp nil
  3958. "Matches a headline and puts TODO state into group 2 if present.")
  3959. (make-variable-buffer-local 'org-todo-line-regexp)
  3960. (defvar org-complex-heading-regexp nil
  3961. "Matches a headline and puts everything into groups:
  3962. group 1: the stars
  3963. group 2: The todo keyword, maybe
  3964. group 3: Priority cookie
  3965. group 4: True headline
  3966. group 5: Tags")
  3967. (make-variable-buffer-local 'org-complex-heading-regexp)
  3968. (defvar org-complex-heading-regexp-format nil
  3969. "Printf format to make regexp to match an exact headline.
  3970. This regexp will match the headline of any node which has the
  3971. exact headline text that is put into the format, but may have any
  3972. TODO state, priority and tags.")
  3973. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  3974. (defvar org-todo-line-tags-regexp nil
  3975. "Matches a headline and puts TODO state into group 2 if present.
  3976. Also put tags into group 4 if tags are present.")
  3977. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3978. (defvar org-ds-keyword-length 12
  3979. "Maximum length of the DEADLINE and SCHEDULED keywords.")
  3980. (make-variable-buffer-local 'org-ds-keyword-length)
  3981. (defvar org-deadline-regexp nil
  3982. "Matches the DEADLINE keyword.")
  3983. (make-variable-buffer-local 'org-deadline-regexp)
  3984. (defvar org-deadline-time-regexp nil
  3985. "Matches the DEADLINE keyword together with a time stamp.")
  3986. (make-variable-buffer-local 'org-deadline-time-regexp)
  3987. (defvar org-deadline-time-hour-regexp nil
  3988. "Matches the DEADLINE keyword together with a time-and-hour stamp.")
  3989. (make-variable-buffer-local 'org-deadline-time-hour-regexp)
  3990. (defvar org-deadline-line-regexp nil
  3991. "Matches the DEADLINE keyword and the rest of the line.")
  3992. (make-variable-buffer-local 'org-deadline-line-regexp)
  3993. (defvar org-scheduled-regexp nil
  3994. "Matches the SCHEDULED keyword.")
  3995. (make-variable-buffer-local 'org-scheduled-regexp)
  3996. (defvar org-scheduled-time-regexp nil
  3997. "Matches the SCHEDULED keyword together with a time stamp.")
  3998. (make-variable-buffer-local 'org-scheduled-time-regexp)
  3999. (defvar org-scheduled-time-hour-regexp nil
  4000. "Matches the SCHEDULED keyword together with a time-and-hour stamp.")
  4001. (make-variable-buffer-local 'org-scheduled-time-hour-regexp)
  4002. (defvar org-closed-time-regexp nil
  4003. "Matches the CLOSED keyword together with a time stamp.")
  4004. (make-variable-buffer-local 'org-closed-time-regexp)
  4005. (defvar org-keyword-time-regexp nil
  4006. "Matches any of the 4 keywords, together with the time stamp.")
  4007. (make-variable-buffer-local 'org-keyword-time-regexp)
  4008. (defvar org-keyword-time-not-clock-regexp nil
  4009. "Matches any of the 3 keywords, together with the time stamp.")
  4010. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  4011. (defvar org-maybe-keyword-time-regexp nil
  4012. "Matches a timestamp, possibly preceded by a keyword.")
  4013. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  4014. (defvar org-all-time-keywords nil
  4015. "List of time keywords.")
  4016. (make-variable-buffer-local 'org-all-time-keywords)
  4017. (defconst org-plain-time-of-day-regexp
  4018. (concat
  4019. "\\(\\<[012]?[0-9]"
  4020. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4021. "\\(--?"
  4022. "\\(\\<[012]?[0-9]"
  4023. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4024. "\\)?")
  4025. "Regular expression to match a plain time or time range.
  4026. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4027. groups carry important information:
  4028. 0 the full match
  4029. 1 the first time, range or not
  4030. 8 the second time, if it is a range.")
  4031. (defconst org-plain-time-extension-regexp
  4032. (concat
  4033. "\\(\\<[012]?[0-9]"
  4034. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4035. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  4036. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  4037. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4038. groups carry important information:
  4039. 0 the full match
  4040. 7 hours of duration
  4041. 9 minutes of duration")
  4042. (defconst org-stamp-time-of-day-regexp
  4043. (concat
  4044. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  4045. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  4046. "\\(--?"
  4047. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  4048. "Regular expression to match a timestamp time or time range.
  4049. After a match, the following groups carry important information:
  4050. 0 the full match
  4051. 1 date plus weekday, for back referencing to make sure both times are on the same day
  4052. 2 the first time, range or not
  4053. 4 the second time, if it is a range.")
  4054. (defconst org-startup-options
  4055. '(("fold" org-startup-folded t)
  4056. ("overview" org-startup-folded t)
  4057. ("nofold" org-startup-folded nil)
  4058. ("showall" org-startup-folded nil)
  4059. ("showeverything" org-startup-folded showeverything)
  4060. ("content" org-startup-folded content)
  4061. ("indent" org-startup-indented t)
  4062. ("noindent" org-startup-indented nil)
  4063. ("hidestars" org-hide-leading-stars t)
  4064. ("showstars" org-hide-leading-stars nil)
  4065. ("odd" org-odd-levels-only t)
  4066. ("oddeven" org-odd-levels-only nil)
  4067. ("align" org-startup-align-all-tables t)
  4068. ("noalign" org-startup-align-all-tables nil)
  4069. ("inlineimages" org-startup-with-inline-images t)
  4070. ("noinlineimages" org-startup-with-inline-images nil)
  4071. ("latexpreview" org-startup-with-latex-preview t)
  4072. ("nolatexpreview" org-startup-with-latex-preview nil)
  4073. ("customtime" org-display-custom-times t)
  4074. ("logdone" org-log-done time)
  4075. ("lognotedone" org-log-done note)
  4076. ("nologdone" org-log-done nil)
  4077. ("lognoteclock-out" org-log-note-clock-out t)
  4078. ("nolognoteclock-out" org-log-note-clock-out nil)
  4079. ("logrepeat" org-log-repeat state)
  4080. ("lognoterepeat" org-log-repeat note)
  4081. ("logdrawer" org-log-into-drawer t)
  4082. ("nologdrawer" org-log-into-drawer nil)
  4083. ("logstatesreversed" org-log-states-order-reversed t)
  4084. ("nologstatesreversed" org-log-states-order-reversed nil)
  4085. ("nologrepeat" org-log-repeat nil)
  4086. ("logreschedule" org-log-reschedule time)
  4087. ("lognotereschedule" org-log-reschedule note)
  4088. ("nologreschedule" org-log-reschedule nil)
  4089. ("logredeadline" org-log-redeadline time)
  4090. ("lognoteredeadline" org-log-redeadline note)
  4091. ("nologredeadline" org-log-redeadline nil)
  4092. ("logrefile" org-log-refile time)
  4093. ("lognoterefile" org-log-refile note)
  4094. ("nologrefile" org-log-refile nil)
  4095. ("fninline" org-footnote-define-inline t)
  4096. ("nofninline" org-footnote-define-inline nil)
  4097. ("fnlocal" org-footnote-section nil)
  4098. ("fnauto" org-footnote-auto-label t)
  4099. ("fnprompt" org-footnote-auto-label nil)
  4100. ("fnconfirm" org-footnote-auto-label confirm)
  4101. ("fnplain" org-footnote-auto-label plain)
  4102. ("fnadjust" org-footnote-auto-adjust t)
  4103. ("nofnadjust" org-footnote-auto-adjust nil)
  4104. ("constcgs" constants-unit-system cgs)
  4105. ("constSI" constants-unit-system SI)
  4106. ("noptag" org-tag-persistent-alist nil)
  4107. ("hideblocks" org-hide-block-startup t)
  4108. ("nohideblocks" org-hide-block-startup nil)
  4109. ("beamer" org-startup-with-beamer-mode t)
  4110. ("entitiespretty" org-pretty-entities t)
  4111. ("entitiesplain" org-pretty-entities nil))
  4112. "Variable associated with STARTUP options for org-mode.
  4113. Each element is a list of three items: the startup options (as written
  4114. in the #+STARTUP line), the corresponding variable, and the value to set
  4115. this variable to if the option is found. An optional forth element PUSH
  4116. means to push this value onto the list in the variable.")
  4117. (defun org-update-property-plist (key val props)
  4118. "Update PROPS with KEY and VAL."
  4119. (let* ((appending (string= "+" (substring key (- (length key) 1))))
  4120. (key (if appending (substring key 0 (- (length key) 1)) key))
  4121. (remainder (org-remove-if (lambda (p) (string= (car p) key)) props))
  4122. (previous (cdr (assoc key props))))
  4123. (if appending
  4124. (cons (cons key (if previous (concat previous " " val) val)) remainder)
  4125. (cons (cons key val) remainder))))
  4126. (defconst org-block-regexp
  4127. "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
  4128. "Regular expression for hiding blocks.")
  4129. (defconst org-heading-keyword-regexp-format
  4130. "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  4131. "Printf format for a regexp matching a headline with some keyword.
  4132. This regexp will match the headline of any node which has the
  4133. exact keyword that is put into the format. The keyword isn't in
  4134. any group by default, but the stars and the body are.")
  4135. (defconst org-heading-keyword-maybe-regexp-format
  4136. "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
  4137. "Printf format for a regexp matching a headline, possibly with some keyword.
  4138. This regexp can match any headline with the specified keyword, or
  4139. without a keyword. The keyword isn't in any group by default,
  4140. but the stars and the body are.")
  4141. (defun org-set-regexps-and-options ()
  4142. "Precompute regular expressions for current buffer."
  4143. (when (derived-mode-p 'org-mode)
  4144. (org-set-local 'org-todo-kwd-alist nil)
  4145. (org-set-local 'org-todo-key-alist nil)
  4146. (org-set-local 'org-todo-key-trigger nil)
  4147. (org-set-local 'org-todo-keywords-1 nil)
  4148. (org-set-local 'org-done-keywords nil)
  4149. (org-set-local 'org-todo-heads nil)
  4150. (org-set-local 'org-todo-sets nil)
  4151. (org-set-local 'org-todo-log-states nil)
  4152. (org-set-local 'org-file-properties nil)
  4153. (org-set-local 'org-file-tags nil)
  4154. (let ((re (org-make-options-regexp
  4155. '("CATEGORY" "TODO" "COLUMNS" "STARTUP" "ARCHIVE" "FILETAGS"
  4156. "TAGS" "LINK" "PRIORITIES" "CONSTANTS" "PROPERTY" "DRAWERS"
  4157. "SETUPFILE" "OPTIONS")
  4158. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  4159. (splitre "[ \t]+")
  4160. (scripts org-use-sub-superscripts)
  4161. kwds kws0 kwsa key log value cat arch tags const links hw dws
  4162. tail sep kws1 prio props ftags drawers ext-setup-or-nil setup-contents
  4163. (start 0))
  4164. (save-excursion
  4165. (save-restriction
  4166. (widen)
  4167. (goto-char (point-min))
  4168. (while (or (and ext-setup-or-nil
  4169. (string-match re ext-setup-or-nil start)
  4170. (setq start (match-end 0)))
  4171. (and (setq ext-setup-or-nil nil start 0)
  4172. (re-search-forward re nil t)))
  4173. (setq key (upcase (match-string 1 ext-setup-or-nil))
  4174. value (org-match-string-no-properties 2 ext-setup-or-nil))
  4175. (if (stringp value) (setq value (org-trim value)))
  4176. (cond
  4177. ((equal key "CATEGORY")
  4178. (setq cat value))
  4179. ((member key '("SEQ_TODO" "TODO"))
  4180. (push (cons 'sequence (org-split-string value splitre)) kwds))
  4181. ((equal key "TYP_TODO")
  4182. (push (cons 'type (org-split-string value splitre)) kwds))
  4183. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  4184. ;; general TODO-like setup
  4185. (push (cons (intern (downcase (match-string 1 key)))
  4186. (org-split-string value splitre)) kwds))
  4187. ((equal key "TAGS")
  4188. (setq tags (append tags (if tags '("\\n") nil)
  4189. (org-split-string value splitre))))
  4190. ((equal key "COLUMNS")
  4191. (org-set-local 'org-columns-default-format value))
  4192. ((equal key "LINK")
  4193. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  4194. (push (cons (match-string 1 value)
  4195. (org-trim (match-string 2 value)))
  4196. links)))
  4197. ((equal key "PRIORITIES")
  4198. (setq prio (org-split-string value " +")))
  4199. ((equal key "PROPERTY")
  4200. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4201. (setq props (org-update-property-plist (match-string 1 value)
  4202. (match-string 2 value)
  4203. props))))
  4204. ((equal key "FILETAGS")
  4205. (when (string-match "\\S-" value)
  4206. (setq ftags
  4207. (append
  4208. ftags
  4209. (apply 'append
  4210. (mapcar (lambda (x) (org-split-string x ":"))
  4211. (org-split-string value)))))))
  4212. ((equal key "DRAWERS")
  4213. (setq drawers (delete-dups (append org-drawers (org-split-string value splitre)))))
  4214. ((equal key "CONSTANTS")
  4215. (setq const (append const (org-split-string value splitre))))
  4216. ((equal key "STARTUP")
  4217. (let ((opts (org-split-string value splitre))
  4218. l var val)
  4219. (while (setq l (pop opts))
  4220. (when (setq l (assoc l org-startup-options))
  4221. (setq var (nth 1 l) val (nth 2 l))
  4222. (if (not (nth 3 l))
  4223. (set (make-local-variable var) val)
  4224. (if (not (listp (symbol-value var)))
  4225. (set (make-local-variable var) nil))
  4226. (set (make-local-variable var) (symbol-value var))
  4227. (add-to-list var val))))))
  4228. ((equal key "ARCHIVE")
  4229. (setq arch value)
  4230. (remove-text-properties 0 (length arch)
  4231. '(face t fontified t) arch))
  4232. ((equal key "OPTIONS")
  4233. (if (string-match "\\([ \t]\\|\\`\\)\\^:\\(t\\|nil\\|{}\\)" value)
  4234. (setq scripts (read (match-string 2 value)))))
  4235. ((equal key "SETUPFILE")
  4236. (setq setup-contents (org-file-contents
  4237. (expand-file-name
  4238. (org-remove-double-quotes value))
  4239. 'noerror))
  4240. (if (not ext-setup-or-nil)
  4241. (setq ext-setup-or-nil setup-contents start 0)
  4242. (setq ext-setup-or-nil
  4243. (concat (substring ext-setup-or-nil 0 start)
  4244. "\n" setup-contents "\n"
  4245. (substring ext-setup-or-nil start)))))))
  4246. ;; search for property blocks
  4247. (goto-char (point-min))
  4248. (while (re-search-forward org-block-regexp nil t)
  4249. (when (equal "PROPERTY" (upcase (match-string 1)))
  4250. (setq value (replace-regexp-in-string
  4251. "[\n\r]" " " (match-string 4)))
  4252. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4253. (setq props (org-update-property-plist (match-string 1 value)
  4254. (match-string 2 value)
  4255. props)))))))
  4256. (org-set-local 'org-use-sub-superscripts scripts)
  4257. (when cat
  4258. (org-set-local 'org-category (intern cat))
  4259. (push (cons "CATEGORY" cat) props))
  4260. (when prio
  4261. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  4262. (setq prio (mapcar 'string-to-char prio))
  4263. (org-set-local 'org-highest-priority (nth 0 prio))
  4264. (org-set-local 'org-lowest-priority (nth 1 prio))
  4265. (org-set-local 'org-default-priority (nth 2 prio)))
  4266. (and props (org-set-local 'org-file-properties (nreverse props)))
  4267. (and ftags (org-set-local 'org-file-tags
  4268. (mapcar 'org-add-prop-inherited ftags)))
  4269. (and drawers (org-set-local 'org-drawers drawers))
  4270. (and arch (org-set-local 'org-archive-location arch))
  4271. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  4272. ;; Process the TODO keywords
  4273. (unless kwds
  4274. ;; Use the global values as if they had been given locally.
  4275. (setq kwds (default-value 'org-todo-keywords))
  4276. (if (stringp (car kwds))
  4277. (setq kwds (list (cons org-todo-interpretation
  4278. (default-value 'org-todo-keywords)))))
  4279. (setq kwds (reverse kwds)))
  4280. (setq kwds (nreverse kwds))
  4281. (let (inter kws kw)
  4282. (while (setq kws (pop kwds))
  4283. (let ((kws (or
  4284. (run-hook-with-args-until-success
  4285. 'org-todo-setup-filter-hook kws)
  4286. kws)))
  4287. (setq inter (pop kws) sep (member "|" kws)
  4288. kws0 (delete "|" (copy-sequence kws))
  4289. kwsa nil
  4290. kws1 (mapcar
  4291. (lambda (x)
  4292. ;; 1 2
  4293. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  4294. (progn
  4295. (setq kw (match-string 1 x)
  4296. key (and (match-end 2) (match-string 2 x))
  4297. log (org-extract-log-state-settings x))
  4298. (push (cons kw (and key (string-to-char key))) kwsa)
  4299. (and log (push log org-todo-log-states))
  4300. kw)
  4301. (error "Invalid TODO keyword %s" x)))
  4302. kws0)
  4303. kwsa (if kwsa (append '((:startgroup))
  4304. (nreverse kwsa)
  4305. '((:endgroup))))
  4306. hw (car kws1)
  4307. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  4308. tail (list inter hw (car dws) (org-last dws))))
  4309. (add-to-list 'org-todo-heads hw 'append)
  4310. (push kws1 org-todo-sets)
  4311. (setq org-done-keywords (append org-done-keywords dws nil))
  4312. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  4313. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  4314. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  4315. (setq org-todo-sets (nreverse org-todo-sets)
  4316. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  4317. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  4318. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  4319. ;; Process the constants
  4320. (when const
  4321. (let (e cst)
  4322. (while (setq e (pop const))
  4323. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  4324. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  4325. (setq org-table-formula-constants-local cst)))
  4326. ;; Process the tags.
  4327. (when tags
  4328. (let (e tgs)
  4329. (while (setq e (pop tags))
  4330. (cond
  4331. ((equal e "{") (push '(:startgroup) tgs))
  4332. ((equal e "}") (push '(:endgroup) tgs))
  4333. ((equal e "\\n") (push '(:newline) tgs))
  4334. ((string-match (org-re "^\\([[:alnum:]_@#%]+\\)(\\(.\\))$") e)
  4335. (push (cons (match-string 1 e)
  4336. (string-to-char (match-string 2 e)))
  4337. tgs))
  4338. (t (push (list e) tgs))))
  4339. (org-set-local 'org-tag-alist nil)
  4340. (while (setq e (pop tgs))
  4341. (or (and (stringp (car e))
  4342. (assoc (car e) org-tag-alist))
  4343. (push e org-tag-alist)))))
  4344. ;; Compute the regular expressions and other local variables.
  4345. ;; Using `org-outline-regexp-bol' would complicate them much,
  4346. ;; because of the fixed white space at the end of that string.
  4347. (if (not org-done-keywords)
  4348. (setq org-done-keywords (and org-todo-keywords-1
  4349. (list (org-last org-todo-keywords-1)))))
  4350. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  4351. (length org-scheduled-string)
  4352. (length org-clock-string)
  4353. (length org-closed-string)))
  4354. org-drawer-regexp
  4355. (concat "^[ \t]*:\\("
  4356. (mapconcat 'regexp-quote org-drawers "\\|")
  4357. "\\):[ \t]*$")
  4358. org-not-done-keywords
  4359. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  4360. org-todo-regexp
  4361. (concat "\\("
  4362. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4363. "\\)")
  4364. org-not-done-regexp
  4365. (concat "\\("
  4366. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  4367. "\\)")
  4368. org-not-done-heading-regexp
  4369. (format org-heading-keyword-regexp-format org-not-done-regexp)
  4370. org-todo-line-regexp
  4371. (format org-heading-keyword-maybe-regexp-format org-todo-regexp)
  4372. org-complex-heading-regexp
  4373. (concat "^\\(\\*+\\)"
  4374. "\\(?: +" org-todo-regexp "\\)?"
  4375. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4376. "\\(?: +\\(.*?\\)\\)??"
  4377. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?")
  4378. "[ \t]*$")
  4379. org-complex-heading-regexp-format
  4380. (concat "^\\(\\*+\\)"
  4381. "\\(?: +" org-todo-regexp "\\)?"
  4382. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4383. "\\(?: +"
  4384. ;; Stats cookies can be stuck to body.
  4385. "\\(?:\\[[0-9%%/]+\\] *\\)?"
  4386. "\\(%s\\)"
  4387. "\\(?: *\\[[0-9%%/]+\\]\\)?"
  4388. "\\)"
  4389. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?")
  4390. "[ \t]*$")
  4391. org-todo-line-tags-regexp
  4392. (concat "^\\(\\*+\\)"
  4393. "\\(?: +" org-todo-regexp "\\)?"
  4394. "\\(?: +\\(.*?\\)\\)??"
  4395. (org-re "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?")
  4396. "[ \t]*$")
  4397. org-deadline-regexp (concat "\\<" org-deadline-string)
  4398. org-deadline-time-regexp
  4399. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  4400. org-deadline-time-hour-regexp
  4401. (concat "\\<" org-deadline-string
  4402. " *<\\(.+[0-9]\\{1,2\\}:[0-9]\\{2\\}[^>]*\\)>")
  4403. org-deadline-line-regexp
  4404. (concat "\\<\\(" org-deadline-string "\\).*")
  4405. org-scheduled-regexp
  4406. (concat "\\<" org-scheduled-string)
  4407. org-scheduled-time-regexp
  4408. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  4409. org-scheduled-time-hour-regexp
  4410. (concat "\\<" org-scheduled-string
  4411. " *<\\(.+[0-9]\\{1,2\\}:[0-9]\\{2\\}[^>]*\\)>")
  4412. org-closed-time-regexp
  4413. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  4414. org-keyword-time-regexp
  4415. (concat "\\<\\(" org-scheduled-string
  4416. "\\|" org-deadline-string
  4417. "\\|" org-closed-string
  4418. "\\|" org-clock-string "\\)"
  4419. " *[[<]\\([^]>]+\\)[]>]")
  4420. org-keyword-time-not-clock-regexp
  4421. (concat "\\<\\(" org-scheduled-string
  4422. "\\|" org-deadline-string
  4423. "\\|" org-closed-string
  4424. "\\)"
  4425. " *[[<]\\([^]>]+\\)[]>]")
  4426. org-maybe-keyword-time-regexp
  4427. (concat "\\(\\<\\(" org-scheduled-string
  4428. "\\|" org-deadline-string
  4429. "\\|" org-closed-string
  4430. "\\|" org-clock-string "\\)\\)?"
  4431. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  4432. org-all-time-keywords
  4433. (mapcar (lambda (w) (substring w 0 -1))
  4434. (list org-scheduled-string org-deadline-string
  4435. org-clock-string org-closed-string)))
  4436. (org-compute-latex-and-related-regexp)
  4437. (org-set-font-lock-defaults))))
  4438. (defun org-file-contents (file &optional noerror)
  4439. "Return the contents of FILE, as a string."
  4440. (if (or (not file)
  4441. (not (file-readable-p file)))
  4442. (if noerror
  4443. (progn
  4444. (message "Cannot read file \"%s\"" file)
  4445. (ding) (sit-for 2)
  4446. "")
  4447. (error "Cannot read file \"%s\"" file))
  4448. (with-temp-buffer
  4449. (insert-file-contents file)
  4450. (buffer-string))))
  4451. (defun org-extract-log-state-settings (x)
  4452. "Extract the log state setting from a TODO keyword string.
  4453. This will extract info from a string like \"WAIT(w@/!)\"."
  4454. (let (kw key log1 log2)
  4455. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4456. (setq kw (match-string 1 x)
  4457. key (and (match-end 2) (match-string 2 x))
  4458. log1 (and (match-end 3) (match-string 3 x))
  4459. log2 (and (match-end 4) (match-string 4 x)))
  4460. (and (or log1 log2)
  4461. (list kw
  4462. (and log1 (if (equal log1 "!") 'time 'note))
  4463. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4464. (defun org-remove-keyword-keys (list)
  4465. "Remove a pair of parenthesis at the end of each string in LIST."
  4466. (mapcar (lambda (x)
  4467. (if (string-match "(.*)$" x)
  4468. (substring x 0 (match-beginning 0))
  4469. x))
  4470. list))
  4471. (defun org-assign-fast-keys (alist)
  4472. "Assign fast keys to a keyword-key alist.
  4473. Respect keys that are already there."
  4474. (let (new e (alt ?0))
  4475. (while (setq e (pop alist))
  4476. (if (or (memq (car e) '(:newline :endgroup :startgroup))
  4477. (cdr e)) ;; Key already assigned.
  4478. (push e new)
  4479. (let ((clist (string-to-list (downcase (car e))))
  4480. (used (append new alist)))
  4481. (when (= (car clist) ?@)
  4482. (pop clist))
  4483. (while (and clist (rassoc (car clist) used))
  4484. (pop clist))
  4485. (unless clist
  4486. (while (rassoc alt used)
  4487. (incf alt)))
  4488. (push (cons (car e) (or (car clist) alt)) new))))
  4489. (nreverse new)))
  4490. ;;; Some variables used in various places
  4491. (defvar org-window-configuration nil
  4492. "Used in various places to store a window configuration.")
  4493. (defvar org-selected-window nil
  4494. "Used in various places to store a window configuration.")
  4495. (defvar org-finish-function nil
  4496. "Function to be called when `C-c C-c' is used.
  4497. This is for getting out of special buffers like capture.")
  4498. ;; FIXME: Occasionally check by commenting these, to make sure
  4499. ;; no other functions uses these, forgetting to let-bind them.
  4500. (org-no-warnings (defvar entry)) ;; unprefixed, from calendar.el
  4501. (defvar org-last-state)
  4502. (org-no-warnings (defvar date)) ;; unprefixed, from calendar.el
  4503. ;; Defined somewhere in this file, but used before definition.
  4504. (defvar org-entities) ;; defined in org-entities.el
  4505. (defvar org-struct-menu)
  4506. (defvar org-org-menu)
  4507. (defvar org-tbl-menu)
  4508. ;;;; Define the Org-mode
  4509. ;; We use a before-change function to check if a table might need
  4510. ;; an update.
  4511. (defvar org-table-may-need-update t
  4512. "Indicates that a table might need an update.
  4513. This variable is set by `org-before-change-function'.
  4514. `org-table-align' sets it back to nil.")
  4515. (defun org-before-change-function (beg end)
  4516. "Every change indicates that a table might need an update."
  4517. (setq org-table-may-need-update t))
  4518. (defvar org-mode-map)
  4519. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4520. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4521. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4522. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4523. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4524. (defvar org-table-buffer-is-an nil)
  4525. (defvar bidi-paragraph-direction)
  4526. (defvar buffer-face-mode-face)
  4527. (require 'outline)
  4528. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4529. (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"))
  4530. (require 'noutline "noutline" 'noerror) ;; stock XEmacs does not have it
  4531. ;; Other stuff we need.
  4532. (require 'time-date)
  4533. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  4534. (require 'easymenu)
  4535. (require 'overlay)
  4536. ;; (require 'org-macs) moved higher up in the file before it is first used
  4537. (require 'org-entities)
  4538. ;; (require 'org-compat) moved higher up in the file before it is first used
  4539. (require 'org-faces)
  4540. (require 'org-list)
  4541. (require 'org-pcomplete)
  4542. (require 'org-src)
  4543. (require 'org-footnote)
  4544. (require 'org-macro)
  4545. ;; babel
  4546. (require 'ob)
  4547. ;;;###autoload
  4548. (define-derived-mode org-mode outline-mode "Org"
  4549. "Outline-based notes management and organizer, alias
  4550. \"Carsten's outline-mode for keeping track of everything.\"
  4551. Org-mode develops organizational tasks around a NOTES file which
  4552. contains information about projects as plain text. Org-mode is
  4553. implemented on top of outline-mode, which is ideal to keep the content
  4554. of large files well structured. It supports ToDo items, deadlines and
  4555. time stamps, which magically appear in the diary listing of the Emacs
  4556. calendar. Tables are easily created with a built-in table editor.
  4557. Plain text URL-like links connect to websites, emails (VM), Usenet
  4558. messages (Gnus), BBDB entries, and any files related to the project.
  4559. For printing and sharing of notes, an Org-mode file (or a part of it)
  4560. can be exported as a structured ASCII or HTML file.
  4561. The following commands are available:
  4562. \\{org-mode-map}"
  4563. ;; Get rid of Outline menus, they are not needed
  4564. ;; Need to do this here because define-derived-mode sets up
  4565. ;; the keymap so late. Still, it is a waste to call this each time
  4566. ;; we switch another buffer into org-mode.
  4567. (if (featurep 'xemacs)
  4568. (when (boundp 'outline-mode-menu-heading)
  4569. ;; Assume this is Greg's port, it uses easymenu
  4570. (easy-menu-remove outline-mode-menu-heading)
  4571. (easy-menu-remove outline-mode-menu-show)
  4572. (easy-menu-remove outline-mode-menu-hide))
  4573. (define-key org-mode-map [menu-bar headings] 'undefined)
  4574. (define-key org-mode-map [menu-bar hide] 'undefined)
  4575. (define-key org-mode-map [menu-bar show] 'undefined))
  4576. (org-load-modules-maybe)
  4577. (easy-menu-add org-org-menu)
  4578. (easy-menu-add org-tbl-menu)
  4579. (org-install-agenda-files-menu)
  4580. (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4581. (add-to-invisibility-spec '(org-cwidth))
  4582. (add-to-invisibility-spec '(org-hide-block . t))
  4583. (when (featurep 'xemacs)
  4584. (org-set-local 'line-move-ignore-invisible t))
  4585. (org-set-local 'outline-regexp org-outline-regexp)
  4586. (org-set-local 'outline-level 'org-outline-level)
  4587. (setq bidi-paragraph-direction 'left-to-right)
  4588. ;; FIXME Circumvent a bug in outline.el (Emacs <24.4)
  4589. (set (make-local-variable 'paragraph-start) " \\|[ \t]*$\\|\\*+ ")
  4590. (when (and org-ellipsis
  4591. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4592. (fboundp 'make-glyph-code))
  4593. (unless org-display-table
  4594. (setq org-display-table (make-display-table)))
  4595. (set-display-table-slot
  4596. org-display-table 4
  4597. (vconcat (mapcar
  4598. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4599. org-ellipsis)))
  4600. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4601. (setq buffer-display-table org-display-table))
  4602. (org-set-regexps-and-options)
  4603. (when (and org-tag-faces (not org-tags-special-faces-re))
  4604. ;; tag faces set outside customize.... force initialization.
  4605. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4606. ;; Calc embedded
  4607. (org-set-local 'calc-embedded-open-mode "# ")
  4608. ;; Modify a few syntax entries
  4609. (modify-syntax-entry ?@ "w")
  4610. (modify-syntax-entry ?\" "\"")
  4611. (if org-startup-truncated (setq truncate-lines t))
  4612. (when org-startup-indented (require 'org-indent) (org-indent-mode 1))
  4613. (org-set-local 'font-lock-unfontify-region-function
  4614. 'org-unfontify-region)
  4615. ;; Activate before-change-function
  4616. (org-set-local 'org-table-may-need-update t)
  4617. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4618. 'local)
  4619. ;; Check for running clock before killing a buffer
  4620. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4621. ;; Initialize macros templates.
  4622. (org-macro-initialize-templates)
  4623. ;; Initialize radio targets.
  4624. (org-update-radio-target-regexp)
  4625. ;; Indentation.
  4626. (org-set-local 'indent-line-function 'org-indent-line)
  4627. (org-set-local 'indent-region-function 'org-indent-region)
  4628. ;; Filling and auto-filling.
  4629. (org-setup-filling)
  4630. ;; Comments.
  4631. (org-setup-comments-handling)
  4632. ;; Beginning/end of defun
  4633. (org-set-local 'beginning-of-defun-function 'org-backward-element)
  4634. (org-set-local 'end-of-defun-function 'org-forward-element)
  4635. ;; Next error for sparse trees
  4636. (org-set-local 'next-error-function 'org-occur-next-match)
  4637. ;; Make sure dependence stuff works reliably, even for users who set it
  4638. ;; too late :-(
  4639. (if org-enforce-todo-dependencies
  4640. (add-hook 'org-blocker-hook
  4641. 'org-block-todo-from-children-or-siblings-or-parent)
  4642. (remove-hook 'org-blocker-hook
  4643. 'org-block-todo-from-children-or-siblings-or-parent))
  4644. (if org-enforce-todo-checkbox-dependencies
  4645. (add-hook 'org-blocker-hook
  4646. 'org-block-todo-from-checkboxes)
  4647. (remove-hook 'org-blocker-hook
  4648. 'org-block-todo-from-checkboxes))
  4649. ;; Align options lines
  4650. (org-set-local
  4651. 'align-mode-rules-list
  4652. '((org-in-buffer-settings
  4653. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4654. (modes . '(org-mode)))))
  4655. ;; Imenu
  4656. (org-set-local 'imenu-create-index-function
  4657. 'org-imenu-get-tree)
  4658. ;; Make isearch reveal context
  4659. (if (or (featurep 'xemacs)
  4660. (not (boundp 'outline-isearch-open-invisible-function)))
  4661. ;; Emacs 21 and XEmacs make use of the hook
  4662. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4663. ;; Emacs 22 deals with this through a special variable
  4664. (org-set-local 'outline-isearch-open-invisible-function
  4665. (lambda (&rest ignore) (org-show-context 'isearch)))
  4666. (org-add-hook 'isearch-mode-end-hook 'org-fix-ellipsis-at-bol 'append 'local))
  4667. ;; Setup the pcomplete hooks
  4668. (set (make-local-variable 'pcomplete-command-completion-function)
  4669. 'org-pcomplete-initial)
  4670. (set (make-local-variable 'pcomplete-command-name-function)
  4671. 'org-command-at-point)
  4672. (set (make-local-variable 'pcomplete-default-completion-function)
  4673. 'ignore)
  4674. (set (make-local-variable 'pcomplete-parse-arguments-function)
  4675. 'org-parse-arguments)
  4676. (set (make-local-variable 'pcomplete-termination-string) "")
  4677. (when (>= emacs-major-version 23)
  4678. (set (make-local-variable 'buffer-face-mode-face) 'org-default))
  4679. ;; If empty file that did not turn on org-mode automatically, make it to.
  4680. (if (and org-insert-mode-line-in-empty-file
  4681. (org-called-interactively-p 'any)
  4682. (= (point-min) (point-max)))
  4683. (insert "# -*- mode: org -*-\n\n"))
  4684. (unless org-inhibit-startup
  4685. (org-unmodified
  4686. (and org-startup-with-beamer-mode (org-beamer-mode))
  4687. (when org-startup-align-all-tables
  4688. (org-table-map-tables 'org-table-align 'quietly))
  4689. (when org-startup-with-inline-images
  4690. (org-display-inline-images))
  4691. (when org-startup-with-latex-preview
  4692. (org-preview-latex-fragment))
  4693. (unless org-inhibit-startup-visibility-stuff
  4694. (org-set-startup-visibility))))
  4695. ;; Try to set org-hide correctly
  4696. (set-face-foreground 'org-hide (org-find-invisible-foreground)))
  4697. ;; Update `customize-package-emacs-version-alist'
  4698. (add-to-list 'customize-package-emacs-version-alist
  4699. '(Org ("6.21b" . "23.1") ("6.33x" . "23.2")
  4700. ("7.8.11" . "24.1") ("7.9.4" . "24.3")
  4701. ("8.0" . "24.4")))
  4702. (defvar org-mode-transpose-word-syntax-table
  4703. (let ((st (make-syntax-table)))
  4704. (mapc (lambda(c) (modify-syntax-entry
  4705. (string-to-char (car c)) "w p" st))
  4706. org-emphasis-alist)
  4707. st))
  4708. (when (fboundp 'abbrev-table-put)
  4709. (abbrev-table-put org-mode-abbrev-table
  4710. :parents (list text-mode-abbrev-table)))
  4711. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4712. (defsubst org-fix-ellipsis-at-bol ()
  4713. (save-excursion (goto-char (window-start)) (recenter 0)))
  4714. (defun org-find-invisible-foreground ()
  4715. (let ((candidates (remove
  4716. "unspecified-bg"
  4717. (nconc
  4718. (list (face-background 'default)
  4719. (face-background 'org-default))
  4720. (mapcar
  4721. (lambda (alist)
  4722. (when (boundp alist)
  4723. (cdr (assoc 'background-color (symbol-value alist)))))
  4724. '(default-frame-alist initial-frame-alist window-system-default-frame-alist))
  4725. (list (face-foreground 'org-hide))))))
  4726. (car (remove nil candidates))))
  4727. (defun org-current-time (&optional rounding-minutes past)
  4728. "Current time, possibly rounded to ROUNDING-MINUTES.
  4729. When ROUNDING-MINUTES is not an integer, fall back on the car of
  4730. `org-time-stamp-rounding-minutes'. When PAST is non-nil, ensure
  4731. the rounding returns a past time."
  4732. (let ((r (or (and (integerp rounding-minutes) rounding-minutes)
  4733. (car org-time-stamp-rounding-minutes)))
  4734. (time (decode-time)) res)
  4735. (if (< r 1)
  4736. (current-time)
  4737. (setq res
  4738. (apply 'encode-time
  4739. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4740. (nthcdr 2 time))))
  4741. (if (and past (< (org-float-time (time-subtract (current-time) res)) 0))
  4742. (seconds-to-time (- (org-float-time res) (* r 60)))
  4743. res))))
  4744. (defun org-today ()
  4745. "Return today date, considering `org-extend-today-until'."
  4746. (time-to-days
  4747. (time-subtract (current-time)
  4748. (list 0 (* 3600 org-extend-today-until) 0))))
  4749. ;;;; Font-Lock stuff, including the activators
  4750. (defvar org-mouse-map (make-sparse-keymap))
  4751. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4752. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4753. (when org-mouse-1-follows-link
  4754. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4755. (when org-tab-follows-link
  4756. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4757. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4758. (require 'font-lock)
  4759. (defconst org-non-link-chars "]\t\n\r<>")
  4760. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  4761. "shell" "elisp" "doi" "message"))
  4762. (defvar org-link-types-re nil
  4763. "Matches a link that has a url-like prefix like \"http:\"")
  4764. (defvar org-link-re-with-space nil
  4765. "Matches a link with spaces, optional angular brackets around it.")
  4766. (defvar org-link-re-with-space2 nil
  4767. "Matches a link with spaces, optional angular brackets around it.")
  4768. (defvar org-link-re-with-space3 nil
  4769. "Matches a link with spaces, only for internal part in bracket links.")
  4770. (defvar org-angle-link-re nil
  4771. "Matches link with angular brackets, spaces are allowed.")
  4772. (defvar org-plain-link-re nil
  4773. "Matches plain link, without spaces.")
  4774. (defvar org-bracket-link-regexp nil
  4775. "Matches a link in double brackets.")
  4776. (defvar org-bracket-link-analytic-regexp nil
  4777. "Regular expression used to analyze links.
  4778. Here is what the match groups contain after a match:
  4779. 1: http:
  4780. 2: http
  4781. 3: path
  4782. 4: [desc]
  4783. 5: desc")
  4784. (defvar org-bracket-link-analytic-regexp++ nil
  4785. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4786. (defvar org-any-link-re nil
  4787. "Regular expression matching any link.")
  4788. (defconst org-match-sexp-depth 3
  4789. "Number of stacked braces for sub/superscript matching.")
  4790. (defun org-create-multibrace-regexp (left right n)
  4791. "Create a regular expression which will match a balanced sexp.
  4792. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4793. as single character strings.
  4794. The regexp returned will match the entire expression including the
  4795. delimiters. It will also define a single group which contains the
  4796. match except for the outermost delimiters. The maximum depth of
  4797. stacked delimiters is N. Escaping delimiters is not possible."
  4798. (let* ((nothing (concat "[^" left right "]*?"))
  4799. (or "\\|")
  4800. (re nothing)
  4801. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4802. (while (> n 1)
  4803. (setq n (1- n)
  4804. re (concat re or next)
  4805. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4806. (concat left "\\(" re "\\)" right)))
  4807. (defvar org-match-substring-regexp
  4808. (concat
  4809. "\\(\\S-\\)\\([_^]\\)\\("
  4810. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4811. "\\|"
  4812. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  4813. "\\|"
  4814. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  4815. "The regular expression matching a sub- or superscript.")
  4816. (defvar org-match-substring-with-braces-regexp
  4817. (concat
  4818. "\\(\\S-\\)\\([_^]\\)\\("
  4819. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4820. "\\)")
  4821. "The regular expression matching a sub- or superscript, forcing braces.")
  4822. (defun org-make-link-regexps ()
  4823. "Update the link regular expressions.
  4824. This should be called after the variable `org-link-types' has changed."
  4825. (setq org-link-types-re
  4826. (concat
  4827. "\\`\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):")
  4828. org-link-re-with-space
  4829. (concat
  4830. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4831. "\\([^" org-non-link-chars " ]"
  4832. "[^" org-non-link-chars "]*"
  4833. "[^" org-non-link-chars " ]\\)>?")
  4834. org-link-re-with-space2
  4835. (concat
  4836. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4837. "\\([^" org-non-link-chars " ]"
  4838. "[^\t\n\r]*"
  4839. "[^" org-non-link-chars " ]\\)>?")
  4840. org-link-re-with-space3
  4841. (concat
  4842. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4843. "\\([^" org-non-link-chars " ]"
  4844. "[^\t\n\r]*\\)")
  4845. org-angle-link-re
  4846. (concat
  4847. "<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4848. "\\([^" org-non-link-chars " ]"
  4849. "[^" org-non-link-chars "]*"
  4850. "\\)>")
  4851. org-plain-link-re
  4852. (concat
  4853. "\\<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4854. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  4855. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4856. org-bracket-link-regexp
  4857. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4858. org-bracket-link-analytic-regexp
  4859. (concat
  4860. "\\[\\["
  4861. "\\(\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):\\)?"
  4862. "\\([^]]+\\)"
  4863. "\\]"
  4864. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4865. "\\]")
  4866. org-bracket-link-analytic-regexp++
  4867. (concat
  4868. "\\[\\["
  4869. "\\(\\(" (mapconcat 'regexp-quote (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  4870. "\\([^]]+\\)"
  4871. "\\]"
  4872. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4873. "\\]")
  4874. org-any-link-re
  4875. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4876. org-angle-link-re "\\)\\|\\("
  4877. org-plain-link-re "\\)")))
  4878. (org-make-link-regexps)
  4879. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
  4880. "Regular expression for fast time stamp matching.")
  4881. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
  4882. "Regular expression for fast time stamp matching.")
  4883. (defconst org-ts-regexp0
  4884. "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\( +[^]+0-9>\r\n -]+\\)?\\( +\\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4885. "Regular expression matching time strings for analysis.
  4886. This one does not require the space after the date, so it can be used
  4887. on a string that terminates immediately after the date.")
  4888. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4889. "Regular expression matching time strings for analysis.")
  4890. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  4891. "Regular expression matching time stamps, with groups.")
  4892. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  4893. "Regular expression matching time stamps (also [..]), with groups.")
  4894. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  4895. "Regular expression matching a time stamp range.")
  4896. (defconst org-tr-regexp-both
  4897. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  4898. "Regular expression matching a time stamp range.")
  4899. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  4900. org-ts-regexp "\\)?")
  4901. "Regular expression matching a time stamp or time stamp range.")
  4902. (defconst org-tsr-regexp-both
  4903. (concat org-ts-regexp-both "\\(--?-?"
  4904. org-ts-regexp-both "\\)?")
  4905. "Regular expression matching a time stamp or time stamp range.
  4906. The time stamps may be either active or inactive.")
  4907. (defvar org-emph-face nil)
  4908. (defun org-do-emphasis-faces (limit)
  4909. "Run through the buffer and add overlays to emphasized strings."
  4910. (let (rtn a)
  4911. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  4912. (if (not (= (char-after (match-beginning 3))
  4913. (char-after (match-beginning 4))))
  4914. (progn
  4915. (setq rtn t)
  4916. (setq a (assoc (match-string 3) org-emphasis-alist))
  4917. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  4918. 'face
  4919. (nth 1 a))
  4920. (and (nth 4 a)
  4921. (org-remove-flyspell-overlays-in
  4922. (match-beginning 0) (match-end 0)))
  4923. (add-text-properties (match-beginning 2) (match-end 2)
  4924. '(font-lock-multiline t org-emphasis t))
  4925. (when org-hide-emphasis-markers
  4926. (add-text-properties (match-end 4) (match-beginning 5)
  4927. '(invisible org-link))
  4928. (add-text-properties (match-beginning 3) (match-end 3)
  4929. '(invisible org-link)))))
  4930. (backward-char 1))
  4931. rtn))
  4932. (defun org-emphasize (&optional char)
  4933. "Insert or change an emphasis, i.e. a font like bold or italic.
  4934. If there is an active region, change that region to a new emphasis.
  4935. If there is no region, just insert the marker characters and position
  4936. the cursor between them.
  4937. CHAR should be either the marker character, or the first character of the
  4938. HTML tag associated with that emphasis. If CHAR is a space, the means
  4939. to remove the emphasis of the selected region.
  4940. If char is not given (for example in an interactive call) it
  4941. will be prompted for."
  4942. (interactive)
  4943. (let ((eal org-emphasis-alist) e det
  4944. (erc org-emphasis-regexp-components)
  4945. (prompt "")
  4946. (string "") beg end move tag c s)
  4947. (if (org-region-active-p)
  4948. (setq beg (region-beginning) end (region-end)
  4949. string (buffer-substring beg end))
  4950. (setq move t))
  4951. (while (setq e (pop eal))
  4952. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  4953. c (aref tag 0))
  4954. (push (cons c (string-to-char (car e))) det)
  4955. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  4956. (substring tag 1)))))
  4957. (setq det (nreverse det))
  4958. (unless char
  4959. (message "%s" (concat "Emphasis marker or tag:" prompt))
  4960. (setq char (read-char-exclusive)))
  4961. (setq char (or (cdr (assoc char det)) char))
  4962. (if (equal char ?\ )
  4963. (setq s "" move nil)
  4964. (unless (assoc (char-to-string char) org-emphasis-alist)
  4965. (error "No such emphasis marker: \"%c\"" char))
  4966. (setq s (char-to-string char)))
  4967. (while (and (> (length string) 1)
  4968. (equal (substring string 0 1) (substring string -1))
  4969. (assoc (substring string 0 1) org-emphasis-alist))
  4970. (setq string (substring string 1 -1)))
  4971. (setq string (concat s string s))
  4972. (if beg (delete-region beg end))
  4973. (unless (or (bolp)
  4974. (string-match (concat "[" (nth 0 erc) "\n]")
  4975. (char-to-string (char-before (point)))))
  4976. (insert " "))
  4977. (unless (or (eobp)
  4978. (string-match (concat "[" (nth 1 erc) "\n]")
  4979. (char-to-string (char-after (point)))))
  4980. (insert " ") (backward-char 1))
  4981. (insert string)
  4982. (and move (backward-char 1))))
  4983. (defconst org-nonsticky-props
  4984. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text htmlize-link))
  4985. (defsubst org-rear-nonsticky-at (pos)
  4986. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  4987. (defun org-activate-plain-links (limit)
  4988. "Run through the buffer and add overlays to links."
  4989. (let (f hl)
  4990. (when (and (re-search-forward (concat org-plain-link-re) limit t)
  4991. (not (org-in-src-block-p)))
  4992. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4993. (setq f (get-text-property (match-beginning 0) 'face))
  4994. (setq hl (org-match-string-no-properties 0))
  4995. (if (or (eq f 'org-tag)
  4996. (and (listp f) (memq 'org-tag f)))
  4997. nil
  4998. (add-text-properties (match-beginning 0) (match-end 0)
  4999. (list 'mouse-face 'highlight
  5000. 'face 'org-link
  5001. 'htmlize-link `(:uri ,hl)
  5002. 'keymap org-mouse-map))
  5003. (org-rear-nonsticky-at (match-end 0)))
  5004. t)))
  5005. (defun org-activate-code (limit)
  5006. (if (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t)
  5007. (progn
  5008. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5009. (remove-text-properties (match-beginning 0) (match-end 0)
  5010. '(display t invisible t intangible t))
  5011. t)))
  5012. (defcustom org-src-fontify-natively nil
  5013. "When non-nil, fontify code in code blocks."
  5014. :type 'boolean
  5015. :version "24.1"
  5016. :group 'org-appearance
  5017. :group 'org-babel)
  5018. (defcustom org-allow-promoting-top-level-subtree nil
  5019. "When non-nil, allow promoting a top level subtree.
  5020. The leading star of the top level headline will be replaced
  5021. by a #."
  5022. :type 'boolean
  5023. :version "24.1"
  5024. :group 'org-appearance)
  5025. (defun org-fontify-meta-lines-and-blocks (limit)
  5026. (condition-case nil
  5027. (org-fontify-meta-lines-and-blocks-1 limit)
  5028. (error (message "org-mode fontification error"))))
  5029. (defun org-fontify-meta-lines-and-blocks-1 (limit)
  5030. "Fontify #+ lines and blocks, in the correct ways."
  5031. (let ((case-fold-search t))
  5032. (if (re-search-forward
  5033. "^\\([ \t]*#\\(\\(\\+[a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  5034. limit t)
  5035. (let ((beg (match-beginning 0))
  5036. (block-start (match-end 0))
  5037. (block-end nil)
  5038. (lang (match-string 7))
  5039. (beg1 (line-beginning-position 2))
  5040. (dc1 (downcase (match-string 2)))
  5041. (dc3 (downcase (match-string 3)))
  5042. end end1 quoting block-type ovl)
  5043. (cond
  5044. ((member dc1 '("+html:" "+ascii:" "+latex:"))
  5045. ;; a single line of backend-specific content
  5046. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5047. (remove-text-properties (match-beginning 0) (match-end 0)
  5048. '(display t invisible t intangible t))
  5049. (add-text-properties (match-beginning 1) (match-end 3)
  5050. '(font-lock-fontified t face org-meta-line))
  5051. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  5052. '(font-lock-fontified t face org-block))
  5053. ; for backend-specific code
  5054. t)
  5055. ((and (match-end 4) (equal dc3 "+begin"))
  5056. ;; Truly a block
  5057. (setq block-type (downcase (match-string 5))
  5058. quoting (member block-type org-protecting-blocks))
  5059. (when (re-search-forward
  5060. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  5061. nil t) ;; on purpose, we look further than LIMIT
  5062. (setq end (min (point-max) (match-end 0))
  5063. end1 (min (point-max) (1- (match-beginning 0))))
  5064. (setq block-end (match-beginning 0))
  5065. (when quoting
  5066. (remove-text-properties beg end
  5067. '(display t invisible t intangible t)))
  5068. (add-text-properties
  5069. beg end
  5070. '(font-lock-fontified t font-lock-multiline t))
  5071. (add-text-properties beg beg1 '(face org-meta-line))
  5072. (add-text-properties end1 (min (point-max) (1+ end))
  5073. '(face org-meta-line)) ; for end_src
  5074. (cond
  5075. ((and lang (not (string= lang "")) org-src-fontify-natively)
  5076. (org-src-font-lock-fontify-block lang block-start block-end)
  5077. ;; remove old background overlays
  5078. (mapc (lambda (ov)
  5079. (if (eq (overlay-get ov 'face) 'org-block-background)
  5080. (delete-overlay ov)))
  5081. (overlays-at (/ (+ beg1 block-end) 2)))
  5082. ;; add a background overlay
  5083. (setq ovl (make-overlay beg1 block-end))
  5084. (overlay-put ovl 'face 'org-block-background)
  5085. (overlay-put ovl 'evaporate t)) ;; make it go away when empty
  5086. (quoting
  5087. (add-text-properties beg1 (min (point-max) (1+ end1))
  5088. '(face org-block))) ; end of source block
  5089. ((not org-fontify-quote-and-verse-blocks))
  5090. ((string= block-type "quote")
  5091. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-quote)))
  5092. ((string= block-type "verse")
  5093. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-verse))))
  5094. (add-text-properties beg beg1 '(face org-block-begin-line))
  5095. (add-text-properties (min (point-max) (1+ end)) (min (point-max) (1+ end1))
  5096. '(face org-block-end-line))
  5097. t))
  5098. ((member dc1 '("+title:" "+author:" "+email:" "+date:"))
  5099. (add-text-properties
  5100. beg (match-end 3)
  5101. (if (member (intern (substring dc1 0 -1)) org-hidden-keywords)
  5102. '(font-lock-fontified t invisible t)
  5103. '(font-lock-fontified t face org-document-info-keyword)))
  5104. (add-text-properties
  5105. (match-beginning 6) (min (point-max) (1+ (match-end 6)))
  5106. (if (string-equal dc1 "+title:")
  5107. '(font-lock-fontified t face org-document-title)
  5108. '(font-lock-fontified t face org-document-info))))
  5109. ((or (equal dc1 "+results")
  5110. (member dc1 '("+begin:" "+end:" "+caption:" "+label:"
  5111. "+orgtbl:" "+tblfm:" "+tblname:" "+results:"
  5112. "+call:" "+header:" "+headers:" "+name:"))
  5113. (and (match-end 4) (equal dc3 "+attr")))
  5114. (add-text-properties
  5115. beg (match-end 0)
  5116. '(font-lock-fontified t face org-meta-line))
  5117. t)
  5118. ((member dc3 '(" " ""))
  5119. (add-text-properties
  5120. beg (match-end 0)
  5121. '(font-lock-fontified t face font-lock-comment-face)))
  5122. ((not (member (char-after beg) '(?\ ?\t)))
  5123. ;; just any other in-buffer setting, but not indented
  5124. (add-text-properties
  5125. beg (match-end 0)
  5126. '(font-lock-fontified t face org-meta-line))
  5127. t)
  5128. (t nil))))))
  5129. (defun org-activate-angle-links (limit)
  5130. "Run through the buffer and add overlays to links."
  5131. (if (and (re-search-forward org-angle-link-re limit t)
  5132. (not (org-in-src-block-p)))
  5133. (progn
  5134. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5135. (add-text-properties (match-beginning 0) (match-end 0)
  5136. (list 'mouse-face 'highlight
  5137. 'keymap org-mouse-map))
  5138. (org-rear-nonsticky-at (match-end 0))
  5139. t)))
  5140. (defun org-activate-footnote-links (limit)
  5141. "Run through the buffer and add overlays to footnotes."
  5142. (let ((fn (org-footnote-next-reference-or-definition limit)))
  5143. (when fn
  5144. (let ((beg (nth 1 fn)) (end (nth 2 fn)))
  5145. (org-remove-flyspell-overlays-in beg end)
  5146. (add-text-properties beg end
  5147. (list 'mouse-face 'highlight
  5148. 'keymap org-mouse-map
  5149. 'help-echo
  5150. (if (= (point-at-bol) beg)
  5151. "Footnote definition"
  5152. "Footnote reference")
  5153. 'font-lock-fontified t
  5154. 'font-lock-multiline t
  5155. 'face 'org-footnote))))))
  5156. (defun org-activate-bracket-links (limit)
  5157. "Run through the buffer and add overlays to bracketed links."
  5158. (if (and (re-search-forward org-bracket-link-regexp limit t)
  5159. (not (org-in-src-block-p)))
  5160. (let* ((hl (org-match-string-no-properties 1))
  5161. (help (concat "LINK: " hl))
  5162. ;; FIXME: Above we should remove the escapes. But that
  5163. ;; requires another match, protecting match data, a lot
  5164. ;; of overhead for font-lock.
  5165. (ip (org-maybe-intangible
  5166. (list 'invisible 'org-link
  5167. 'keymap org-mouse-map 'mouse-face 'highlight
  5168. 'font-lock-multiline t 'help-echo help
  5169. 'htmlize-link `(:uri ,hl))))
  5170. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  5171. 'font-lock-multiline t 'help-echo help
  5172. 'htmlize-link `(:uri ,hl))))
  5173. ;; We need to remove the invisible property here. Table narrowing
  5174. ;; may have made some of this invisible.
  5175. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5176. (remove-text-properties (match-beginning 0) (match-end 0)
  5177. '(invisible nil))
  5178. (if (match-end 3)
  5179. (progn
  5180. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  5181. (org-rear-nonsticky-at (match-beginning 3))
  5182. (add-text-properties (match-beginning 3) (match-end 3) vp)
  5183. (org-rear-nonsticky-at (match-end 3))
  5184. (add-text-properties (match-end 3) (match-end 0) ip)
  5185. (org-rear-nonsticky-at (match-end 0)))
  5186. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  5187. (org-rear-nonsticky-at (match-beginning 1))
  5188. (add-text-properties (match-beginning 1) (match-end 1) vp)
  5189. (org-rear-nonsticky-at (match-end 1))
  5190. (add-text-properties (match-end 1) (match-end 0) ip)
  5191. (org-rear-nonsticky-at (match-end 0)))
  5192. t)))
  5193. (defun org-activate-dates (limit)
  5194. "Run through the buffer and add overlays to dates."
  5195. (if (and (re-search-forward org-tsr-regexp-both limit t)
  5196. (not (equal (char-before (match-beginning 0)) 91)))
  5197. (progn
  5198. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5199. (add-text-properties (match-beginning 0) (match-end 0)
  5200. (list 'mouse-face 'highlight
  5201. 'keymap org-mouse-map))
  5202. (org-rear-nonsticky-at (match-end 0))
  5203. (when org-display-custom-times
  5204. (if (match-end 3)
  5205. (org-display-custom-time (match-beginning 3) (match-end 3)))
  5206. (org-display-custom-time (match-beginning 1) (match-end 1)))
  5207. t)))
  5208. (defvar org-target-link-regexp nil
  5209. "Regular expression matching radio targets in plain text.")
  5210. (make-variable-buffer-local 'org-target-link-regexp)
  5211. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  5212. "Regular expression matching a link target.")
  5213. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  5214. "Regular expression matching a radio target.")
  5215. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  5216. "Regular expression matching any target.")
  5217. (defun org-activate-target-links (limit)
  5218. "Run through the buffer and add overlays to target matches."
  5219. (when org-target-link-regexp
  5220. (let ((case-fold-search t))
  5221. (if (re-search-forward org-target-link-regexp limit t)
  5222. (progn
  5223. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5224. (add-text-properties (match-beginning 0) (match-end 0)
  5225. (list 'mouse-face 'highlight
  5226. 'keymap org-mouse-map
  5227. 'help-echo "Radio target link"
  5228. 'org-linked-text t))
  5229. (org-rear-nonsticky-at (match-end 0))
  5230. t)))))
  5231. (defun org-update-radio-target-regexp ()
  5232. "Find all radio targets in this file and update the regular expression."
  5233. (interactive)
  5234. (when (memq 'radio org-activate-links)
  5235. (setq org-target-link-regexp
  5236. (org-make-target-link-regexp (org-all-targets 'radio)))
  5237. (org-restart-font-lock)))
  5238. (defun org-hide-wide-columns (limit)
  5239. (let (s e)
  5240. (setq s (text-property-any (point) (or limit (point-max))
  5241. 'org-cwidth t))
  5242. (when s
  5243. (setq e (next-single-property-change s 'org-cwidth))
  5244. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  5245. (goto-char e)
  5246. t)))
  5247. (defvar org-latex-and-related-regexp nil
  5248. "Regular expression for highlighting LaTeX, entities and sub/superscript.")
  5249. (defvar org-match-substring-regexp)
  5250. (defvar org-match-substring-with-braces-regexp)
  5251. (defun org-compute-latex-and-related-regexp ()
  5252. "Compute regular expression for LaTeX, entities and sub/superscript.
  5253. Result depends on variable `org-highlight-latex-and-related'."
  5254. (org-set-local
  5255. 'org-latex-and-related-regexp
  5256. (let* ((re-sub
  5257. (cond ((not (memq 'script org-highlight-latex-and-related)) nil)
  5258. ((eq org-use-sub-superscripts '{})
  5259. (list org-match-substring-with-braces-regexp))
  5260. (org-use-sub-superscripts (list org-match-substring-regexp))))
  5261. (re-latex
  5262. (when (memq 'latex org-highlight-latex-and-related)
  5263. (let ((matchers (plist-get org-format-latex-options :matchers)))
  5264. (delq nil
  5265. (mapcar (lambda (x)
  5266. (and (member (car x) matchers) (nth 1 x)))
  5267. org-latex-regexps)))))
  5268. (re-entities
  5269. (when (memq 'entities org-highlight-latex-and-related)
  5270. (list "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))))
  5271. (mapconcat 'identity (append re-latex re-entities re-sub) "\\|"))))
  5272. (defun org-do-latex-and-related (limit)
  5273. "Highlight LaTeX snippets and environments, entities and sub/superscript.
  5274. LIMIT bounds the search for syntax to highlight. Stop at first
  5275. highlighted object, if any. Return t if some highlighting was
  5276. done, nil otherwise."
  5277. (when (org-string-nw-p org-latex-and-related-regexp)
  5278. (catch 'found
  5279. (while (re-search-forward org-latex-and-related-regexp limit t)
  5280. (unless (memq (car-safe (get-text-property (1+ (match-beginning 0))
  5281. 'face))
  5282. '(org-code org-verbatim underline))
  5283. (let ((offset (if (memq (char-after (1+ (match-beginning 0)))
  5284. '(?_ ?^))
  5285. 1
  5286. 0)))
  5287. (font-lock-prepend-text-property
  5288. (+ offset (match-beginning 0)) (match-end 0)
  5289. 'face 'org-latex-and-related)
  5290. (add-text-properties (+ offset (match-beginning 0)) (match-end 0)
  5291. '(font-lock-multiline t)))
  5292. (throw 'found t)))
  5293. nil)))
  5294. (defun org-restart-font-lock ()
  5295. "Restart `font-lock-mode', to force refontification."
  5296. (when (and (boundp 'font-lock-mode) font-lock-mode)
  5297. (font-lock-mode -1)
  5298. (font-lock-mode 1)))
  5299. (defun org-all-targets (&optional radio)
  5300. "Return a list of all targets in this file.
  5301. When optional argument RADIO is non-nil, only find radio
  5302. targets."
  5303. (let ((re (if radio org-radio-target-regexp org-target-regexp)) rtn)
  5304. (save-excursion
  5305. (goto-char (point-min))
  5306. (while (re-search-forward re nil t)
  5307. ;; Make sure point is really within the object.
  5308. (backward-char)
  5309. (let ((obj (org-element-context)))
  5310. (when (memq (org-element-type obj) '(radio-target target))
  5311. (add-to-list 'rtn (downcase (org-element-property :value obj))))))
  5312. rtn)))
  5313. (defun org-make-target-link-regexp (targets)
  5314. "Make regular expression matching all strings in TARGETS.
  5315. The regular expression finds the targets also if there is a line break
  5316. between words."
  5317. (and targets
  5318. (concat
  5319. "\\<\\("
  5320. (mapconcat
  5321. (lambda (x)
  5322. (setq x (regexp-quote x))
  5323. (while (string-match " +" x)
  5324. (setq x (replace-match "\\s-+" t t x)))
  5325. x)
  5326. targets
  5327. "\\|")
  5328. "\\)\\>")))
  5329. (defun org-activate-tags (limit)
  5330. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t)
  5331. (progn
  5332. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5333. (add-text-properties (match-beginning 1) (match-end 1)
  5334. (list 'mouse-face 'highlight
  5335. 'keymap org-mouse-map))
  5336. (org-rear-nonsticky-at (match-end 1))
  5337. t)))
  5338. (defun org-outline-level ()
  5339. "Compute the outline level of the heading at point.
  5340. If this is called at a normal headline, the level is the number of stars.
  5341. Use `org-reduced-level' to remove the effect of `org-odd-levels'."
  5342. (save-excursion
  5343. (if (not (condition-case nil
  5344. (org-back-to-heading t)
  5345. (error nil)))
  5346. 0
  5347. (looking-at org-outline-regexp)
  5348. (1- (- (match-end 0) (match-beginning 0))))))
  5349. (defvar org-font-lock-keywords nil)
  5350. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\+?\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  5351. "Regular expression matching a property line.")
  5352. (defvar org-font-lock-hook nil
  5353. "Functions to be called for special font lock stuff.")
  5354. (defvar org-font-lock-set-keywords-hook nil
  5355. "Functions that can manipulate `org-font-lock-extra-keywords'.
  5356. This is called after `org-font-lock-extra-keywords' is defined, but before
  5357. it is installed to be used by font lock. This can be useful if something
  5358. needs to be inserted at a specific position in the font-lock sequence.")
  5359. (defun org-font-lock-hook (limit)
  5360. "Run `org-font-lock-hook' within LIMIT."
  5361. (run-hook-with-args 'org-font-lock-hook limit))
  5362. (defun org-set-font-lock-defaults ()
  5363. "Set font lock defaults for the current buffer."
  5364. (let* ((em org-fontify-emphasized-text)
  5365. (lk org-activate-links)
  5366. (org-font-lock-extra-keywords
  5367. (list
  5368. ;; Call the hook
  5369. '(org-font-lock-hook)
  5370. ;; Headlines
  5371. `(,(if org-fontify-whole-heading-line
  5372. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  5373. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  5374. (1 (org-get-level-face 1))
  5375. (2 (org-get-level-face 2))
  5376. (3 (org-get-level-face 3)))
  5377. ;; Table lines
  5378. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  5379. (1 'org-table t))
  5380. ;; Table internals
  5381. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  5382. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  5383. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  5384. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  5385. ;; Drawers
  5386. (list org-drawer-regexp '(0 'org-special-keyword t))
  5387. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  5388. ;; Properties
  5389. (list org-property-re
  5390. '(1 'org-special-keyword t)
  5391. '(3 'org-property-value t))
  5392. ;; Links
  5393. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  5394. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  5395. (if (memq 'plain lk) '(org-activate-plain-links))
  5396. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  5397. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  5398. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  5399. (if (memq 'footnote lk) '(org-activate-footnote-links))
  5400. ;; Targets.
  5401. (list org-any-target-regexp '(0 'org-target t))
  5402. ;; Diary sexps.
  5403. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  5404. '(org-hide-wide-columns (0 nil append))
  5405. ;; TODO keyword
  5406. (list (format org-heading-keyword-regexp-format
  5407. org-todo-regexp)
  5408. '(2 (org-get-todo-face 2) t))
  5409. ;; DONE
  5410. (if org-fontify-done-headline
  5411. (list (format org-heading-keyword-regexp-format
  5412. (concat
  5413. "\\(?:"
  5414. (mapconcat 'regexp-quote org-done-keywords "\\|")
  5415. "\\)"))
  5416. '(2 'org-headline-done t))
  5417. nil)
  5418. ;; Priorities
  5419. '(org-font-lock-add-priority-faces)
  5420. ;; Tags
  5421. '(org-font-lock-add-tag-faces)
  5422. ;; Special keywords
  5423. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  5424. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  5425. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  5426. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  5427. ;; Emphasis
  5428. (if em
  5429. (if (featurep 'xemacs)
  5430. '(org-do-emphasis-faces (0 nil append))
  5431. '(org-do-emphasis-faces)))
  5432. ;; Checkboxes
  5433. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  5434. 1 'org-checkbox prepend)
  5435. (if (cdr (assq 'checkbox org-list-automatic-rules))
  5436. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5437. (0 (org-get-checkbox-statistics-face) t)))
  5438. ;; Description list items
  5439. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  5440. 1 'org-list-dt prepend)
  5441. ;; ARCHIVEd headings
  5442. (list (concat
  5443. org-outline-regexp-bol
  5444. "\\(.*:" org-archive-tag ":.*\\)")
  5445. '(1 'org-archived prepend))
  5446. ;; Specials
  5447. '(org-do-latex-and-related)
  5448. '(org-fontify-entities)
  5449. '(org-raise-scripts)
  5450. ;; Code
  5451. '(org-activate-code (1 'org-code t))
  5452. ;; COMMENT
  5453. (list (format org-heading-keyword-regexp-format
  5454. (concat "\\("
  5455. org-comment-string "\\|" org-quote-string
  5456. "\\)"))
  5457. '(2 'org-special-keyword t))
  5458. ;; Blocks and meta lines
  5459. '(org-fontify-meta-lines-and-blocks)
  5460. )))
  5461. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5462. (run-hooks 'org-font-lock-set-keywords-hook)
  5463. ;; Now set the full font-lock-keywords
  5464. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  5465. (org-set-local 'font-lock-defaults
  5466. '(org-font-lock-keywords t nil nil backward-paragraph))
  5467. (kill-local-variable 'font-lock-keywords) nil))
  5468. (defun org-toggle-pretty-entities ()
  5469. "Toggle the composition display of entities as UTF8 characters."
  5470. (interactive)
  5471. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  5472. (org-restart-font-lock)
  5473. (if org-pretty-entities
  5474. (message "Entities are displayed as UTF8 characters")
  5475. (save-restriction
  5476. (widen)
  5477. (org-decompose-region (point-min) (point-max))
  5478. (message "Entities are displayed plain"))))
  5479. (defvar org-custom-properties-overlays nil
  5480. "List of overlays used for custom properties.")
  5481. (make-variable-buffer-local 'org-custom-properties-overlays)
  5482. (defun org-toggle-custom-properties-visibility ()
  5483. "Display or hide properties in `org-custom-properties'."
  5484. (interactive)
  5485. (if org-custom-properties-overlays
  5486. (progn (mapc 'delete-overlay org-custom-properties-overlays)
  5487. (setq org-custom-properties-overlays nil))
  5488. (unless (not org-custom-properties)
  5489. (save-excursion
  5490. (save-restriction
  5491. (widen)
  5492. (goto-char (point-min))
  5493. (while (re-search-forward org-property-re nil t)
  5494. (mapc (lambda(p)
  5495. (when (equal p (substring (match-string 1) 1 -1))
  5496. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5497. (overlay-put o 'invisible t)
  5498. (overlay-put o 'org-custom-property t)
  5499. (push o org-custom-properties-overlays))))
  5500. org-custom-properties)))))))
  5501. (defun org-fontify-entities (limit)
  5502. "Find an entity to fontify."
  5503. (let (ee)
  5504. (when org-pretty-entities
  5505. (catch 'match
  5506. (while (re-search-forward
  5507. "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]\n]\\)"
  5508. limit t)
  5509. (if (and (not (org-in-indented-comment-line))
  5510. (setq ee (org-entity-get (match-string 1)))
  5511. (= (length (nth 6 ee)) 1))
  5512. (let*
  5513. ((end (if (equal (match-string 2) "{}")
  5514. (match-end 2)
  5515. (match-end 1))))
  5516. (add-text-properties
  5517. (match-beginning 0) end
  5518. (list 'font-lock-fontified t))
  5519. (compose-region (match-beginning 0) end
  5520. (nth 6 ee) nil)
  5521. (backward-char 1)
  5522. (throw 'match t))))
  5523. nil))))
  5524. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5525. "Fontify string S like in Org-mode."
  5526. (with-temp-buffer
  5527. (insert s)
  5528. (let ((org-odd-levels-only odd-levels))
  5529. (org-mode)
  5530. (font-lock-fontify-buffer)
  5531. (buffer-string))))
  5532. (defvar org-m nil)
  5533. (defvar org-l nil)
  5534. (defvar org-f nil)
  5535. (defun org-get-level-face (n)
  5536. "Get the right face for match N in font-lock matching of headlines."
  5537. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5538. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5539. (if org-cycle-level-faces
  5540. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5541. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5542. (cond
  5543. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5544. ((eq n 2) org-f)
  5545. (t (if org-level-color-stars-only nil org-f))))
  5546. (defun org-get-todo-face (kwd)
  5547. "Get the right face for a TODO keyword KWD.
  5548. If KWD is a number, get the corresponding match group."
  5549. (if (numberp kwd) (setq kwd (match-string kwd)))
  5550. (or (org-face-from-face-or-color
  5551. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5552. (and (member kwd org-done-keywords) 'org-done)
  5553. 'org-todo))
  5554. (defun org-face-from-face-or-color (context inherit face-or-color)
  5555. "Create a face list that inherits INHERIT, but sets the foreground color.
  5556. When FACE-OR-COLOR is not a string, just return it."
  5557. (if (stringp face-or-color)
  5558. (list :inherit inherit
  5559. (cdr (assoc context org-faces-easy-properties))
  5560. face-or-color)
  5561. face-or-color))
  5562. (defun org-font-lock-add-tag-faces (limit)
  5563. "Add the special tag faces."
  5564. (when (and org-tag-faces org-tags-special-faces-re)
  5565. (while (re-search-forward org-tags-special-faces-re limit t)
  5566. (add-text-properties (match-beginning 1) (match-end 1)
  5567. (list 'face (org-get-tag-face 1)
  5568. 'font-lock-fontified t))
  5569. (backward-char 1))))
  5570. (defun org-font-lock-add-priority-faces (limit)
  5571. "Add the special priority faces."
  5572. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  5573. (when (save-match-data (org-at-heading-p))
  5574. (add-text-properties
  5575. (match-beginning 0) (match-end 0)
  5576. (list 'face (or (org-face-from-face-or-color
  5577. 'priority 'org-priority
  5578. (cdr (assoc (char-after (match-beginning 1))
  5579. org-priority-faces)))
  5580. 'org-priority)
  5581. 'font-lock-fontified t)))))
  5582. (defun org-get-tag-face (kwd)
  5583. "Get the right face for a TODO keyword KWD.
  5584. If KWD is a number, get the corresponding match group."
  5585. (if (numberp kwd) (setq kwd (match-string kwd)))
  5586. (or (org-face-from-face-or-color
  5587. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  5588. 'org-tag))
  5589. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5590. "Remove fontification and activation overlays from links."
  5591. (font-lock-default-unfontify-region beg end)
  5592. (let* ((buffer-undo-list t)
  5593. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5594. (inhibit-modification-hooks t)
  5595. deactivate-mark buffer-file-name buffer-file-truename)
  5596. (org-decompose-region beg end)
  5597. (remove-text-properties beg end
  5598. '(mouse-face t keymap t org-linked-text t
  5599. invisible t intangible t
  5600. org-no-flyspell t org-emphasis t))
  5601. (org-remove-font-lock-display-properties beg end)))
  5602. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5603. ((raise 0.3) (height 0.7))
  5604. ((raise -0.5))
  5605. ((raise 0.5)))
  5606. "Display properties for showing superscripts and subscripts.")
  5607. (defun org-remove-font-lock-display-properties (beg end)
  5608. "Remove specific display properties that have been added by font lock.
  5609. The will remove the raise properties that are used to show superscripts
  5610. and subscripts."
  5611. (let (next prop)
  5612. (while (< beg end)
  5613. (setq next (next-single-property-change beg 'display nil end)
  5614. prop (get-text-property beg 'display))
  5615. (if (member prop org-script-display)
  5616. (put-text-property beg next 'display nil))
  5617. (setq beg next))))
  5618. (defun org-raise-scripts (limit)
  5619. "Add raise properties to sub/superscripts."
  5620. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts)
  5621. (if (re-search-forward
  5622. (if (eq org-use-sub-superscripts t)
  5623. org-match-substring-regexp
  5624. org-match-substring-with-braces-regexp)
  5625. limit t)
  5626. (let* ((pos (point)) table-p comment-p
  5627. (mpos (match-beginning 3))
  5628. (emph-p (get-text-property mpos 'org-emphasis))
  5629. (link-p (get-text-property mpos 'mouse-face))
  5630. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5631. (goto-char (point-at-bol))
  5632. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5633. comment-p (org-looking-at-p "[ \t]*#"))
  5634. (goto-char pos)
  5635. ;; FIXME: Should we go back one character here, for a_b^c
  5636. ;; (goto-char (1- pos)) ;????????????????????
  5637. (if (or comment-p emph-p link-p keyw-p)
  5638. t
  5639. (put-text-property (match-beginning 3) (match-end 0)
  5640. 'display
  5641. (if (equal (char-after (match-beginning 2)) ?^)
  5642. (nth (if table-p 3 1) org-script-display)
  5643. (nth (if table-p 2 0) org-script-display)))
  5644. (add-text-properties (match-beginning 2) (match-end 2)
  5645. (list 'invisible t
  5646. 'org-dwidth t 'org-dwidth-n 1))
  5647. (if (and (eq (char-after (match-beginning 3)) ?{)
  5648. (eq (char-before (match-end 3)) ?}))
  5649. (progn
  5650. (add-text-properties
  5651. (match-beginning 3) (1+ (match-beginning 3))
  5652. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5653. (add-text-properties
  5654. (1- (match-end 3)) (match-end 3)
  5655. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))))
  5656. t)))))
  5657. ;;;; Visibility cycling, including org-goto and indirect buffer
  5658. ;;; Cycling
  5659. (defvar org-cycle-global-status nil)
  5660. (make-variable-buffer-local 'org-cycle-global-status)
  5661. (put 'org-cycle-global-status 'org-state t)
  5662. (defvar org-cycle-subtree-status nil)
  5663. (make-variable-buffer-local 'org-cycle-subtree-status)
  5664. (put 'org-cycle-subtree-status 'org-state t)
  5665. (defvar org-inlinetask-min-level)
  5666. ;;;###autoload
  5667. (defun org-cycle (&optional arg)
  5668. "TAB-action and visibility cycling for Org-mode.
  5669. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5670. is outline visibility cycling, but it also invokes other actions
  5671. in special contexts.
  5672. - When this function is called with a prefix argument, rotate the entire
  5673. buffer through 3 states (global cycling)
  5674. 1. OVERVIEW: Show only top-level headlines.
  5675. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5676. 3. SHOW ALL: Show everything.
  5677. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  5678. determined by the variable `org-startup-folded', and by any VISIBILITY
  5679. properties in the buffer.
  5680. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  5681. including any drawers.
  5682. - When inside a table, re-align the table and move to the next field.
  5683. - When point is at the beginning of a headline, rotate the subtree started
  5684. by this line through 3 different states (local cycling)
  5685. 1. FOLDED: Only the main headline is shown.
  5686. 2. CHILDREN: The main headline and the direct children are shown.
  5687. From this state, you can move to one of the children
  5688. and zoom in further.
  5689. 3. SUBTREE: Show the entire subtree, including body text.
  5690. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5691. - When point is at the beginning of an empty headline and the variable
  5692. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5693. of the headline by demoting and promoting it to likely levels. This
  5694. speeds up creation document structure by pressing TAB once or several
  5695. times right after creating a new headline.
  5696. - When there is a numeric prefix, go up to a heading with level ARG, do
  5697. a `show-subtree' and return to the previous cursor position. If ARG
  5698. is negative, go up that many levels.
  5699. - When point is not at the beginning of a headline, execute the global
  5700. binding for TAB, which is re-indenting the line. See the option
  5701. `org-cycle-emulate-tab' for details.
  5702. - Special case: if point is at the beginning of the buffer and there is
  5703. no headline in line 1, this function will act as if called with prefix arg
  5704. (C-u TAB, same as S-TAB) also when called without prefix arg.
  5705. But only if also the variable `org-cycle-global-at-bob' is t."
  5706. (interactive "P")
  5707. (org-load-modules-maybe)
  5708. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5709. (and org-cycle-level-after-item/entry-creation
  5710. (or (org-cycle-level)
  5711. (org-cycle-item-indentation))))
  5712. (let* (message-log-max ; Don't populate the *Messages* buffer
  5713. (limit-level
  5714. (or org-cycle-max-level
  5715. (and (boundp 'org-inlinetask-min-level)
  5716. org-inlinetask-min-level
  5717. (1- org-inlinetask-min-level))))
  5718. (nstars (and limit-level
  5719. (if org-odd-levels-only
  5720. (and limit-level (1- (* limit-level 2)))
  5721. limit-level)))
  5722. (org-outline-regexp
  5723. (if (not (derived-mode-p 'org-mode))
  5724. outline-regexp
  5725. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
  5726. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5727. (not (looking-at org-outline-regexp))))
  5728. (org-cycle-hook
  5729. (if bob-special
  5730. (delq 'org-optimize-window-after-visibility-change
  5731. (copy-sequence org-cycle-hook))
  5732. org-cycle-hook))
  5733. (pos (point)))
  5734. (if (or bob-special (equal arg '(4)))
  5735. ;; special case: use global cycling
  5736. (setq arg t))
  5737. (cond
  5738. ((equal arg '(16))
  5739. (setq last-command 'dummy)
  5740. (org-set-startup-visibility)
  5741. (message "Startup visibility, plus VISIBILITY properties"))
  5742. ((equal arg '(64))
  5743. (show-all)
  5744. (message "Entire buffer visible, including drawers"))
  5745. ;; Table: enter it or move to the next field.
  5746. ((org-at-table-p 'any)
  5747. (if (org-at-table.el-p)
  5748. (message "Use C-c ' to edit table.el tables")
  5749. (if arg (org-table-edit-field t)
  5750. (org-table-justify-field-maybe)
  5751. (call-interactively 'org-table-next-field))))
  5752. ((run-hook-with-args-until-success
  5753. 'org-tab-after-check-for-table-hook))
  5754. ;; Global cycling: delegate to `org-cycle-internal-global'.
  5755. ((eq arg t) (org-cycle-internal-global))
  5756. ;; Drawers: delegate to `org-flag-drawer'.
  5757. ((and org-drawers org-drawer-regexp
  5758. (save-excursion
  5759. (beginning-of-line 1)
  5760. (looking-at org-drawer-regexp)))
  5761. (org-flag-drawer ; toggle block visibility
  5762. (not (get-char-property (match-end 0) 'invisible))))
  5763. ;; Show-subtree, ARG levels up from here.
  5764. ((integerp arg)
  5765. (save-excursion
  5766. (org-back-to-heading)
  5767. (outline-up-heading (if (< arg 0) (- arg)
  5768. (- (funcall outline-level) arg)))
  5769. (org-show-subtree)))
  5770. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  5771. ((and (featurep 'org-inlinetask)
  5772. (org-inlinetask-at-task-p)
  5773. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5774. (org-inlinetask-toggle-visibility))
  5775. ((org-try-cdlatex-tab))
  5776. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  5777. ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
  5778. (save-excursion (beginning-of-line 1)
  5779. (looking-at org-outline-regexp)))
  5780. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5781. (org-cycle-internal-local))
  5782. ;; From there: TAB emulation and template completion.
  5783. (buffer-read-only (org-back-to-heading))
  5784. ((run-hook-with-args-until-success
  5785. 'org-tab-after-check-for-cycling-hook))
  5786. ((org-try-structure-completion))
  5787. ((run-hook-with-args-until-success
  5788. 'org-tab-before-tab-emulation-hook))
  5789. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5790. (or (not (bolp))
  5791. (not (looking-at org-outline-regexp))))
  5792. (call-interactively (global-key-binding "\t")))
  5793. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5794. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5795. (or (and (eq org-cycle-emulate-tab 'white)
  5796. (= (match-end 0) (point-at-eol)))
  5797. (and (eq org-cycle-emulate-tab 'whitestart)
  5798. (>= (match-end 0) pos))))
  5799. t
  5800. (eq org-cycle-emulate-tab t))
  5801. (call-interactively (global-key-binding "\t")))
  5802. (t (save-excursion
  5803. (org-back-to-heading)
  5804. (org-cycle)))))))
  5805. (defun org-cycle-internal-global ()
  5806. "Do the global cycling action."
  5807. ;; Hack to avoid display of messages for .org attachments in Gnus
  5808. (let (message-log-max ; Don't populate the *Messages* buffer
  5809. (ga (string-match "\\*fontification" (buffer-name))))
  5810. (cond
  5811. ((and (eq last-command this-command)
  5812. (eq org-cycle-global-status 'overview))
  5813. ;; We just created the overview - now do table of contents
  5814. ;; This can be slow in very large buffers, so indicate action
  5815. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  5816. (unless ga (message "CONTENTS..."))
  5817. (org-content)
  5818. (unless ga (message "CONTENTS...done"))
  5819. (setq org-cycle-global-status 'contents)
  5820. (run-hook-with-args 'org-cycle-hook 'contents))
  5821. ((and (eq last-command this-command)
  5822. (eq org-cycle-global-status 'contents))
  5823. ;; We just showed the table of contents - now show everything
  5824. (run-hook-with-args 'org-pre-cycle-hook 'all)
  5825. (show-all)
  5826. (unless ga (message "SHOW ALL"))
  5827. (setq org-cycle-global-status 'all)
  5828. (run-hook-with-args 'org-cycle-hook 'all))
  5829. (t
  5830. ;; Default action: go to overview
  5831. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  5832. (org-overview)
  5833. (unless ga (message "OVERVIEW"))
  5834. (setq org-cycle-global-status 'overview)
  5835. (run-hook-with-args 'org-cycle-hook 'overview)))))
  5836. (defun org-cycle-internal-local ()
  5837. "Do the local cycling action."
  5838. (let (message-log-max ; Don't populate the *Messages* buffer
  5839. (goal-column 0) eoh eol eos has-children children-skipped struct)
  5840. ;; First, determine end of headline (EOH), end of subtree or item
  5841. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  5842. (save-excursion
  5843. (if (org-at-item-p)
  5844. (progn
  5845. (beginning-of-line)
  5846. (setq struct (org-list-struct))
  5847. (setq eoh (point-at-eol))
  5848. (setq eos (org-list-get-item-end-before-blank (point) struct))
  5849. (setq has-children (org-list-has-child-p (point) struct)))
  5850. (org-back-to-heading)
  5851. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  5852. (setq eos (save-excursion (1- (org-end-of-subtree t t))))
  5853. (setq has-children
  5854. (or (save-excursion
  5855. (let ((level (funcall outline-level)))
  5856. (outline-next-heading)
  5857. (and (org-at-heading-p t)
  5858. (> (funcall outline-level) level))))
  5859. (save-excursion
  5860. (org-list-search-forward (org-item-beginning-re) eos t)))))
  5861. ;; Determine end invisible part of buffer (EOL)
  5862. (beginning-of-line 2)
  5863. ;; XEmacs doesn't have `next-single-char-property-change'
  5864. (if (featurep 'xemacs)
  5865. (while (and (not (eobp)) ;; this is like `next-line'
  5866. (get-char-property (1- (point)) 'invisible))
  5867. (beginning-of-line 2))
  5868. (while (and (not (eobp)) ;; this is like `next-line'
  5869. (get-char-property (1- (point)) 'invisible))
  5870. (goto-char (next-single-char-property-change (point) 'invisible))
  5871. (and (eolp) (beginning-of-line 2))))
  5872. (setq eol (point)))
  5873. ;; Find out what to do next and set `this-command'
  5874. (cond
  5875. ((= eos eoh)
  5876. ;; Nothing is hidden behind this heading
  5877. (unless (org-before-first-heading-p)
  5878. (run-hook-with-args 'org-pre-cycle-hook 'empty))
  5879. (message "EMPTY ENTRY")
  5880. (setq org-cycle-subtree-status nil)
  5881. (save-excursion
  5882. (goto-char eos)
  5883. (outline-next-heading)
  5884. (if (outline-invisible-p) (org-flag-heading nil))))
  5885. ((and (or (>= eol eos)
  5886. (not (string-match "\\S-" (buffer-substring eol eos))))
  5887. (or has-children
  5888. (not (setq children-skipped
  5889. org-cycle-skip-children-state-if-no-children))))
  5890. ;; Entire subtree is hidden in one line: children view
  5891. (unless (org-before-first-heading-p)
  5892. (run-hook-with-args 'org-pre-cycle-hook 'children))
  5893. (if (org-at-item-p)
  5894. (org-list-set-item-visibility (point-at-bol) struct 'children)
  5895. (org-show-entry)
  5896. (org-with-limited-levels (show-children))
  5897. ;; FIXME: This slows down the func way too much.
  5898. ;; How keep drawers hidden in subtree anyway?
  5899. ;; (when (memq 'org-cycle-hide-drawers org-cycle-hook)
  5900. ;; (org-cycle-hide-drawers 'subtree))
  5901. ;; Fold every list in subtree to top-level items.
  5902. (when (eq org-cycle-include-plain-lists 'integrate)
  5903. (save-excursion
  5904. (org-back-to-heading)
  5905. (while (org-list-search-forward (org-item-beginning-re) eos t)
  5906. (beginning-of-line 1)
  5907. (let* ((struct (org-list-struct))
  5908. (prevs (org-list-prevs-alist struct))
  5909. (end (org-list-get-bottom-point struct)))
  5910. (mapc (lambda (e) (org-list-set-item-visibility e struct 'folded))
  5911. (org-list-get-all-items (point) struct prevs))
  5912. (goto-char end))))))
  5913. (message "CHILDREN")
  5914. (save-excursion
  5915. (goto-char eos)
  5916. (outline-next-heading)
  5917. (if (outline-invisible-p) (org-flag-heading nil)))
  5918. (setq org-cycle-subtree-status 'children)
  5919. (unless (org-before-first-heading-p)
  5920. (run-hook-with-args 'org-cycle-hook 'children)))
  5921. ((or children-skipped
  5922. (and (eq last-command this-command)
  5923. (eq org-cycle-subtree-status 'children)))
  5924. ;; We just showed the children, or no children are there,
  5925. ;; now show everything.
  5926. (unless (org-before-first-heading-p)
  5927. (run-hook-with-args 'org-pre-cycle-hook 'subtree))
  5928. (outline-flag-region eoh eos nil)
  5929. (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  5930. (setq org-cycle-subtree-status 'subtree)
  5931. (unless (org-before-first-heading-p)
  5932. (run-hook-with-args 'org-cycle-hook 'subtree)))
  5933. (t
  5934. ;; Default action: hide the subtree.
  5935. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  5936. (outline-flag-region eoh eos t)
  5937. (message "FOLDED")
  5938. (setq org-cycle-subtree-status 'folded)
  5939. (unless (org-before-first-heading-p)
  5940. (run-hook-with-args 'org-cycle-hook 'folded))))))
  5941. ;;;###autoload
  5942. (defun org-global-cycle (&optional arg)
  5943. "Cycle the global visibility. For details see `org-cycle'.
  5944. With \\[universal-argument] prefix arg, switch to startup visibility.
  5945. With a numeric prefix, show all headlines up to that level."
  5946. (interactive "P")
  5947. (let ((org-cycle-include-plain-lists
  5948. (if (derived-mode-p 'org-mode) org-cycle-include-plain-lists nil)))
  5949. (cond
  5950. ((integerp arg)
  5951. (show-all)
  5952. (hide-sublevels arg)
  5953. (setq org-cycle-global-status 'contents))
  5954. ((equal arg '(4))
  5955. (org-set-startup-visibility)
  5956. (message "Startup visibility, plus VISIBILITY properties."))
  5957. (t
  5958. (org-cycle '(4))))))
  5959. (defun org-set-startup-visibility ()
  5960. "Set the visibility required by startup options and properties."
  5961. (cond
  5962. ((eq org-startup-folded t)
  5963. (org-cycle '(4)))
  5964. ((eq org-startup-folded 'content)
  5965. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  5966. (org-cycle '(4)) (org-cycle '(4)))))
  5967. (unless (eq org-startup-folded 'showeverything)
  5968. (if org-hide-block-startup (org-hide-block-all))
  5969. (org-set-visibility-according-to-property 'no-cleanup)
  5970. (org-cycle-hide-archived-subtrees 'all)
  5971. (org-cycle-hide-drawers 'all)
  5972. (org-cycle-show-empty-lines t)))
  5973. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  5974. "Switch subtree visibilities according to :VISIBILITY: property."
  5975. (interactive)
  5976. (let (org-show-entry-below state)
  5977. (save-excursion
  5978. (goto-char (point-min))
  5979. (while (re-search-forward
  5980. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  5981. nil t)
  5982. (setq state (match-string 1))
  5983. (save-excursion
  5984. (org-back-to-heading t)
  5985. (hide-subtree)
  5986. (org-reveal)
  5987. (cond
  5988. ((equal state '("fold" "folded"))
  5989. (hide-subtree))
  5990. ((equal state "children")
  5991. (org-show-hidden-entry)
  5992. (show-children))
  5993. ((equal state "content")
  5994. (save-excursion
  5995. (save-restriction
  5996. (org-narrow-to-subtree)
  5997. (org-content))))
  5998. ((member state '("all" "showall"))
  5999. (show-subtree)))))
  6000. (unless no-cleanup
  6001. (org-cycle-hide-archived-subtrees 'all)
  6002. (org-cycle-hide-drawers 'all)
  6003. (org-cycle-show-empty-lines 'all)))))
  6004. ;; This function uses outline-regexp instead of the more fundamental
  6005. ;; org-outline-regexp so that org-cycle-global works outside of Org
  6006. ;; buffers, where outline-regexp is needed.
  6007. (defun org-overview ()
  6008. "Switch to overview mode, showing only top-level headlines.
  6009. Really, this shows all headlines with level equal or greater than the level
  6010. of the first headline in the buffer. This is important, because if the
  6011. first headline is not level one, then (hide-sublevels 1) gives confusing
  6012. results."
  6013. (interactive)
  6014. (let ((l (org-current-line))
  6015. (level (save-excursion
  6016. (goto-char (point-min))
  6017. (if (re-search-forward (concat "^" outline-regexp) nil t)
  6018. (progn
  6019. (goto-char (match-beginning 0))
  6020. (funcall outline-level))))))
  6021. (and level (hide-sublevels level))
  6022. (recenter '(4))
  6023. (org-goto-line l)))
  6024. (defun org-content (&optional arg)
  6025. "Show all headlines in the buffer, like a table of contents.
  6026. With numerical argument N, show content up to level N."
  6027. (interactive "P")
  6028. (save-excursion
  6029. ;; Visit all headings and show their offspring
  6030. (and (integerp arg) (org-overview))
  6031. (goto-char (point-max))
  6032. (catch 'exit
  6033. (while (and (progn (condition-case nil
  6034. (outline-previous-visible-heading 1)
  6035. (error (goto-char (point-min))))
  6036. t)
  6037. (looking-at org-outline-regexp))
  6038. (if (integerp arg)
  6039. (show-children (1- arg))
  6040. (show-branches))
  6041. (if (bobp) (throw 'exit nil))))))
  6042. (defun org-optimize-window-after-visibility-change (state)
  6043. "Adjust the window after a change in outline visibility.
  6044. This function is the default value of the hook `org-cycle-hook'."
  6045. (when (get-buffer-window (current-buffer))
  6046. (cond
  6047. ((eq state 'content) nil)
  6048. ((eq state 'all) nil)
  6049. ((eq state 'folded) nil)
  6050. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  6051. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  6052. (defun org-remove-empty-overlays-at (pos)
  6053. "Remove outline overlays that do not contain non-white stuff."
  6054. (mapc
  6055. (lambda (o)
  6056. (and (eq 'outline (overlay-get o 'invisible))
  6057. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  6058. (overlay-end o))))
  6059. (delete-overlay o)))
  6060. (overlays-at pos)))
  6061. (defun org-clean-visibility-after-subtree-move ()
  6062. "Fix visibility issues after moving a subtree."
  6063. ;; First, find a reasonable region to look at:
  6064. ;; Start two siblings above, end three below
  6065. (let* ((beg (save-excursion
  6066. (and (org-get-last-sibling)
  6067. (org-get-last-sibling))
  6068. (point)))
  6069. (end (save-excursion
  6070. (and (org-get-next-sibling)
  6071. (org-get-next-sibling)
  6072. (org-get-next-sibling))
  6073. (if (org-at-heading-p)
  6074. (point-at-eol)
  6075. (point))))
  6076. (level (looking-at "\\*+"))
  6077. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  6078. (save-excursion
  6079. (save-restriction
  6080. (narrow-to-region beg end)
  6081. (when re
  6082. ;; Properly fold already folded siblings
  6083. (goto-char (point-min))
  6084. (while (re-search-forward re nil t)
  6085. (if (and (not (outline-invisible-p))
  6086. (save-excursion
  6087. (goto-char (point-at-eol)) (outline-invisible-p)))
  6088. (hide-entry))))
  6089. (org-cycle-show-empty-lines 'overview)
  6090. (org-cycle-hide-drawers 'overview)))))
  6091. (defun org-cycle-show-empty-lines (state)
  6092. "Show empty lines above all visible headlines.
  6093. The region to be covered depends on STATE when called through
  6094. `org-cycle-hook'. Lisp program can use t for STATE to get the
  6095. entire buffer covered. Note that an empty line is only shown if there
  6096. are at least `org-cycle-separator-lines' empty lines before the headline."
  6097. (when (not (= org-cycle-separator-lines 0))
  6098. (save-excursion
  6099. (let* ((n (abs org-cycle-separator-lines))
  6100. (re (cond
  6101. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  6102. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  6103. (t (let ((ns (number-to-string (- n 2))))
  6104. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  6105. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  6106. beg end b e)
  6107. (cond
  6108. ((memq state '(overview contents t))
  6109. (setq beg (point-min) end (point-max)))
  6110. ((memq state '(children folded))
  6111. (setq beg (point) end (progn (org-end-of-subtree t t)
  6112. (beginning-of-line 2)
  6113. (point)))))
  6114. (when beg
  6115. (goto-char beg)
  6116. (while (re-search-forward re end t)
  6117. (unless (get-char-property (match-end 1) 'invisible)
  6118. (setq e (match-end 1))
  6119. (if (< org-cycle-separator-lines 0)
  6120. (setq b (save-excursion
  6121. (goto-char (match-beginning 0))
  6122. (org-back-over-empty-lines)
  6123. (if (save-excursion
  6124. (goto-char (max (point-min) (1- (point))))
  6125. (org-at-heading-p))
  6126. (1- (point))
  6127. (point))))
  6128. (setq b (match-beginning 1)))
  6129. (outline-flag-region b e nil)))))))
  6130. ;; Never hide empty lines at the end of the file.
  6131. (save-excursion
  6132. (goto-char (point-max))
  6133. (outline-previous-heading)
  6134. (outline-end-of-heading)
  6135. (if (and (looking-at "[ \t\n]+")
  6136. (= (match-end 0) (point-max)))
  6137. (outline-flag-region (point) (match-end 0) nil))))
  6138. (defun org-show-empty-lines-in-parent ()
  6139. "Move to the parent and re-show empty lines before visible headlines."
  6140. (save-excursion
  6141. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  6142. (org-cycle-show-empty-lines context))))
  6143. (defun org-files-list ()
  6144. "Return `org-agenda-files' list, plus all open org-mode files.
  6145. This is useful for operations that need to scan all of a user's
  6146. open and agenda-wise Org files."
  6147. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  6148. (dolist (buf (buffer-list))
  6149. (with-current-buffer buf
  6150. (if (and (derived-mode-p 'org-mode) (buffer-file-name))
  6151. (let ((file (expand-file-name (buffer-file-name))))
  6152. (unless (member file files)
  6153. (push file files))))))
  6154. files))
  6155. (defsubst org-entry-beginning-position ()
  6156. "Return the beginning position of the current entry."
  6157. (save-excursion (outline-back-to-heading t) (point)))
  6158. (defsubst org-entry-end-position ()
  6159. "Return the end position of the current entry."
  6160. (save-excursion (outline-next-heading) (point)))
  6161. (defun org-cycle-hide-drawers (state)
  6162. "Re-hide all drawers after a visibility state change."
  6163. (when (and (derived-mode-p 'org-mode)
  6164. (not (memq state '(overview folded contents))))
  6165. (save-excursion
  6166. (let* ((globalp (memq state '(contents all)))
  6167. (beg (if globalp (point-min) (point)))
  6168. (end (if globalp (point-max)
  6169. (if (eq state 'children)
  6170. (save-excursion (outline-next-heading) (point))
  6171. (org-end-of-subtree t)))))
  6172. (goto-char beg)
  6173. (while (re-search-forward org-drawer-regexp end t)
  6174. (org-flag-drawer t))))))
  6175. (defun org-cycle-hide-inline-tasks (state)
  6176. "Re-hide inline task when switching to 'contents visibility state."
  6177. (when (and (eq state 'contents)
  6178. (boundp 'org-inlinetask-min-level)
  6179. org-inlinetask-min-level)
  6180. (hide-sublevels (1- org-inlinetask-min-level))))
  6181. (defun org-flag-drawer (flag)
  6182. "When FLAG is non-nil, hide the drawer we are within.
  6183. Otherwise make it visible."
  6184. (save-excursion
  6185. (beginning-of-line 1)
  6186. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  6187. (let ((b (match-end 0)))
  6188. (if (re-search-forward
  6189. "^[ \t]*:END:"
  6190. (save-excursion (outline-next-heading) (point)) t)
  6191. (outline-flag-region b (point-at-eol) flag)
  6192. (error ":END: line missing at position %s" b))))))
  6193. (defun org-subtree-end-visible-p ()
  6194. "Is the end of the current subtree visible?"
  6195. (pos-visible-in-window-p
  6196. (save-excursion (org-end-of-subtree t) (point))))
  6197. (defun org-first-headline-recenter (&optional N)
  6198. "Move cursor to the first headline and recenter the headline.
  6199. Optional argument N means put the headline into the Nth line of the window."
  6200. (goto-char (point-min))
  6201. (when (re-search-forward (concat "^\\(" org-outline-regexp "\\)") nil t)
  6202. (beginning-of-line)
  6203. (recenter (prefix-numeric-value N))))
  6204. ;;; Saving and restoring visibility
  6205. (defun org-outline-overlay-data (&optional use-markers)
  6206. "Return a list of the locations of all outline overlays.
  6207. These are overlays with the `invisible' property value `outline'.
  6208. The return value is a list of cons cells, with start and stop
  6209. positions for each overlay.
  6210. If USE-MARKERS is set, return the positions as markers."
  6211. (let (beg end)
  6212. (save-excursion
  6213. (save-restriction
  6214. (widen)
  6215. (delq nil
  6216. (mapcar (lambda (o)
  6217. (when (eq (overlay-get o 'invisible) 'outline)
  6218. (setq beg (overlay-start o)
  6219. end (overlay-end o))
  6220. (and beg end (> end beg)
  6221. (if use-markers
  6222. (cons (move-marker (make-marker) beg)
  6223. (move-marker (make-marker) end))
  6224. (cons beg end)))))
  6225. (overlays-in (point-min) (point-max))))))))
  6226. (defun org-set-outline-overlay-data (data)
  6227. "Create visibility overlays for all positions in DATA.
  6228. DATA should have been made by `org-outline-overlay-data'."
  6229. (let (o)
  6230. (save-excursion
  6231. (save-restriction
  6232. (widen)
  6233. (show-all)
  6234. (mapc (lambda (c)
  6235. (outline-flag-region (car c) (cdr c) t))
  6236. data)))))
  6237. ;;; Folding of blocks
  6238. (defvar org-hide-block-overlays nil
  6239. "Overlays hiding blocks.")
  6240. (make-variable-buffer-local 'org-hide-block-overlays)
  6241. (defun org-block-map (function &optional start end)
  6242. "Call FUNCTION at the head of all source blocks in the current buffer.
  6243. Optional arguments START and END can be used to limit the range."
  6244. (let ((start (or start (point-min)))
  6245. (end (or end (point-max))))
  6246. (save-excursion
  6247. (goto-char start)
  6248. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  6249. (save-excursion
  6250. (save-match-data
  6251. (goto-char (match-beginning 0))
  6252. (funcall function)))))))
  6253. (defun org-hide-block-toggle-all ()
  6254. "Toggle the visibility of all blocks in the current buffer."
  6255. (org-block-map #'org-hide-block-toggle))
  6256. (defun org-hide-block-all ()
  6257. "Fold all blocks in the current buffer."
  6258. (interactive)
  6259. (org-show-block-all)
  6260. (org-block-map #'org-hide-block-toggle-maybe))
  6261. (defun org-show-block-all ()
  6262. "Unfold all blocks in the current buffer."
  6263. (interactive)
  6264. (mapc 'delete-overlay org-hide-block-overlays)
  6265. (setq org-hide-block-overlays nil))
  6266. (defun org-hide-block-toggle-maybe ()
  6267. "Toggle visibility of block at point."
  6268. (interactive)
  6269. (let ((case-fold-search t))
  6270. (if (save-excursion
  6271. (beginning-of-line 1)
  6272. (looking-at org-block-regexp))
  6273. (progn (org-hide-block-toggle)
  6274. t) ;; to signal that we took action
  6275. nil))) ;; to signal that we did not
  6276. (defun org-hide-block-toggle (&optional force)
  6277. "Toggle the visibility of the current block."
  6278. (interactive)
  6279. (save-excursion
  6280. (beginning-of-line)
  6281. (if (re-search-forward org-block-regexp nil t)
  6282. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  6283. (end (match-end 0)) ;; end of entire body
  6284. ov)
  6285. (if (memq t (mapcar (lambda (overlay)
  6286. (eq (overlay-get overlay 'invisible)
  6287. 'org-hide-block))
  6288. (overlays-at start)))
  6289. (if (or (not force) (eq force 'off))
  6290. (mapc (lambda (ov)
  6291. (when (member ov org-hide-block-overlays)
  6292. (setq org-hide-block-overlays
  6293. (delq ov org-hide-block-overlays)))
  6294. (when (eq (overlay-get ov 'invisible)
  6295. 'org-hide-block)
  6296. (delete-overlay ov)))
  6297. (overlays-at start)))
  6298. (setq ov (make-overlay start end))
  6299. (overlay-put ov 'invisible 'org-hide-block)
  6300. ;; make the block accessible to isearch
  6301. (overlay-put
  6302. ov 'isearch-open-invisible
  6303. (lambda (ov)
  6304. (when (member ov org-hide-block-overlays)
  6305. (setq org-hide-block-overlays
  6306. (delq ov org-hide-block-overlays)))
  6307. (when (eq (overlay-get ov 'invisible)
  6308. 'org-hide-block)
  6309. (delete-overlay ov))))
  6310. (push ov org-hide-block-overlays)))
  6311. (error "Not looking at a source block"))))
  6312. ;; org-tab-after-check-for-cycling-hook
  6313. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  6314. ;; Remove overlays when changing major mode
  6315. (add-hook 'org-mode-hook
  6316. (lambda () (org-add-hook 'change-major-mode-hook
  6317. 'org-show-block-all 'append 'local)))
  6318. ;;; Org-goto
  6319. (defvar org-goto-window-configuration nil)
  6320. (defvar org-goto-marker nil)
  6321. (defvar org-goto-map)
  6322. (defun org-goto-map ()
  6323. "Set the keymap `org-goto'."
  6324. (setq org-goto-map
  6325. (let ((map (make-sparse-keymap)))
  6326. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command
  6327. mouse-drag-region universal-argument org-occur))
  6328. cmd)
  6329. (while (setq cmd (pop cmds))
  6330. (substitute-key-definition cmd cmd map global-map)))
  6331. (suppress-keymap map)
  6332. (org-defkey map "\C-m" 'org-goto-ret)
  6333. (org-defkey map [(return)] 'org-goto-ret)
  6334. (org-defkey map [(left)] 'org-goto-left)
  6335. (org-defkey map [(right)] 'org-goto-right)
  6336. (org-defkey map [(control ?g)] 'org-goto-quit)
  6337. (org-defkey map "\C-i" 'org-cycle)
  6338. (org-defkey map [(tab)] 'org-cycle)
  6339. (org-defkey map [(down)] 'outline-next-visible-heading)
  6340. (org-defkey map [(up)] 'outline-previous-visible-heading)
  6341. (if org-goto-auto-isearch
  6342. (if (fboundp 'define-key-after)
  6343. (define-key-after map [t] 'org-goto-local-auto-isearch)
  6344. nil)
  6345. (org-defkey map "q" 'org-goto-quit)
  6346. (org-defkey map "n" 'outline-next-visible-heading)
  6347. (org-defkey map "p" 'outline-previous-visible-heading)
  6348. (org-defkey map "f" 'outline-forward-same-level)
  6349. (org-defkey map "b" 'outline-backward-same-level)
  6350. (org-defkey map "u" 'outline-up-heading))
  6351. (org-defkey map "/" 'org-occur)
  6352. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  6353. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  6354. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  6355. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  6356. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  6357. map)))
  6358. (defconst org-goto-help
  6359. "Browse buffer copy, to find location or copy text.%s
  6360. RET=jump to location C-g=quit and return to previous location
  6361. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  6362. (defvar org-goto-start-pos) ; dynamically scoped parameter
  6363. ;; FIXME: Docstring does not mention both interfaces
  6364. (defun org-goto (&optional alternative-interface)
  6365. "Look up a different location in the current file, keeping current visibility.
  6366. When you want look-up or go to a different location in a
  6367. document, the fastest way is often to fold the entire buffer and
  6368. then dive into the tree. This method has the disadvantage, that
  6369. the previous location will be folded, which may not be what you
  6370. want.
  6371. This command works around this by showing a copy of the current
  6372. buffer in an indirect buffer, in overview mode. You can dive
  6373. into the tree in that copy, use org-occur and incremental search
  6374. to find a location. When pressing RET or `Q', the command
  6375. returns to the original buffer in which the visibility is still
  6376. unchanged. After RET it will also jump to the location selected
  6377. in the indirect buffer and expose the headline hierarchy above.
  6378. With a prefix argument, use the alternative interface: e.g. if
  6379. `org-goto-interface' is 'outline use 'outline-path-completion."
  6380. (interactive "P")
  6381. (org-goto-map)
  6382. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  6383. (org-refile-use-outline-path t)
  6384. (org-refile-target-verify-function nil)
  6385. (interface
  6386. (if (not alternative-interface)
  6387. org-goto-interface
  6388. (if (eq org-goto-interface 'outline)
  6389. 'outline-path-completion
  6390. 'outline)))
  6391. (org-goto-start-pos (point))
  6392. (selected-point
  6393. (if (eq interface 'outline)
  6394. (car (org-get-location (current-buffer) org-goto-help))
  6395. (let ((pa (org-refile-get-location "Goto" nil nil t)))
  6396. (org-refile-check-position pa)
  6397. (nth 3 pa)))))
  6398. (if selected-point
  6399. (progn
  6400. (org-mark-ring-push org-goto-start-pos)
  6401. (goto-char selected-point)
  6402. (if (or (outline-invisible-p) (org-invisible-p2))
  6403. (org-show-context 'org-goto)))
  6404. (message "Quit"))))
  6405. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  6406. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  6407. (defvar org-goto-local-auto-isearch-map) ; defined below
  6408. (defun org-get-location (buf help)
  6409. "Let the user select a location in the Org-mode buffer BUF.
  6410. This function uses a recursive edit. It returns the selected position
  6411. or nil."
  6412. (org-no-popups
  6413. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  6414. (isearch-hide-immediately nil)
  6415. (isearch-search-fun-function
  6416. (lambda () 'org-goto-local-search-headings))
  6417. (org-goto-selected-point org-goto-exit-command))
  6418. (save-excursion
  6419. (save-window-excursion
  6420. (delete-other-windows)
  6421. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  6422. (org-pop-to-buffer-same-window
  6423. (condition-case nil
  6424. (make-indirect-buffer (current-buffer) "*org-goto*")
  6425. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  6426. (with-output-to-temp-buffer "*Org Help*"
  6427. (princ (format help (if org-goto-auto-isearch
  6428. " Just type for auto-isearch."
  6429. " n/p/f/b/u to navigate, q to quit."))))
  6430. (org-fit-window-to-buffer (get-buffer-window "*Org Help*"))
  6431. (setq buffer-read-only nil)
  6432. (let ((org-startup-truncated t)
  6433. (org-startup-folded nil)
  6434. (org-startup-align-all-tables nil))
  6435. (org-mode)
  6436. (org-overview))
  6437. (setq buffer-read-only t)
  6438. (if (and (boundp 'org-goto-start-pos)
  6439. (integer-or-marker-p org-goto-start-pos))
  6440. (let ((org-show-hierarchy-above t)
  6441. (org-show-siblings t)
  6442. (org-show-following-heading t))
  6443. (goto-char org-goto-start-pos)
  6444. (and (outline-invisible-p) (org-show-context)))
  6445. (goto-char (point-min)))
  6446. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  6447. (message "Select location and press RET")
  6448. (use-local-map org-goto-map)
  6449. (recursive-edit)))
  6450. (kill-buffer "*org-goto*")
  6451. (cons org-goto-selected-point org-goto-exit-command))))
  6452. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  6453. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  6454. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  6455. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  6456. (defun org-goto-local-search-headings (string bound noerror)
  6457. "Search and make sure that any matches are in headlines."
  6458. (catch 'return
  6459. (while (if isearch-forward
  6460. (search-forward string bound noerror)
  6461. (search-backward string bound noerror))
  6462. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  6463. (and (member :headline context)
  6464. (not (member :tags context))))
  6465. (throw 'return (point))))))
  6466. (defun org-goto-local-auto-isearch ()
  6467. "Start isearch."
  6468. (interactive)
  6469. (goto-char (point-min))
  6470. (let ((keys (this-command-keys)))
  6471. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  6472. (isearch-mode t)
  6473. (isearch-process-search-char (string-to-char keys)))))
  6474. (defun org-goto-ret (&optional arg)
  6475. "Finish `org-goto' by going to the new location."
  6476. (interactive "P")
  6477. (setq org-goto-selected-point (point)
  6478. org-goto-exit-command 'return)
  6479. (throw 'exit nil))
  6480. (defun org-goto-left ()
  6481. "Finish `org-goto' by going to the new location."
  6482. (interactive)
  6483. (if (org-at-heading-p)
  6484. (progn
  6485. (beginning-of-line 1)
  6486. (setq org-goto-selected-point (point)
  6487. org-goto-exit-command 'left)
  6488. (throw 'exit nil))
  6489. (error "Not on a heading")))
  6490. (defun org-goto-right ()
  6491. "Finish `org-goto' by going to the new location."
  6492. (interactive)
  6493. (if (org-at-heading-p)
  6494. (progn
  6495. (setq org-goto-selected-point (point)
  6496. org-goto-exit-command 'right)
  6497. (throw 'exit nil))
  6498. (error "Not on a heading")))
  6499. (defun org-goto-quit ()
  6500. "Finish `org-goto' without cursor motion."
  6501. (interactive)
  6502. (setq org-goto-selected-point nil)
  6503. (setq org-goto-exit-command 'quit)
  6504. (throw 'exit nil))
  6505. ;;; Indirect buffer display of subtrees
  6506. (defvar org-indirect-dedicated-frame nil
  6507. "This is the frame being used for indirect tree display.")
  6508. (defvar org-last-indirect-buffer nil)
  6509. (defun org-tree-to-indirect-buffer (&optional arg)
  6510. "Create indirect buffer and narrow it to current subtree.
  6511. With a numerical prefix ARG, go up to this level and then take that tree.
  6512. If ARG is negative, go up that many levels.
  6513. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  6514. indirect buffer previously made with this command, to avoid proliferation of
  6515. indirect buffers. However, when you call the command with a \
  6516. \\[universal-argument] prefix, or
  6517. when `org-indirect-buffer-display' is `new-frame', the last buffer
  6518. is kept so that you can work with several indirect buffers at the same time.
  6519. If `org-indirect-buffer-display' is `dedicated-frame', the \
  6520. \\[universal-argument] prefix also
  6521. requests that a new frame be made for the new buffer, so that the dedicated
  6522. frame is not changed."
  6523. (interactive "P")
  6524. (let ((cbuf (current-buffer))
  6525. (cwin (selected-window))
  6526. (pos (point))
  6527. beg end level heading ibuf)
  6528. (save-excursion
  6529. (org-back-to-heading t)
  6530. (when (numberp arg)
  6531. (setq level (org-outline-level))
  6532. (if (< arg 0) (setq arg (+ level arg)))
  6533. (while (> (setq level (org-outline-level)) arg)
  6534. (org-up-heading-safe)))
  6535. (setq beg (point)
  6536. heading (org-get-heading))
  6537. (org-end-of-subtree t t)
  6538. (if (org-at-heading-p) (backward-char 1))
  6539. (setq end (point)))
  6540. (if (and (buffer-live-p org-last-indirect-buffer)
  6541. (not (eq org-indirect-buffer-display 'new-frame))
  6542. (not arg))
  6543. (kill-buffer org-last-indirect-buffer))
  6544. (setq ibuf (org-get-indirect-buffer cbuf)
  6545. org-last-indirect-buffer ibuf)
  6546. (cond
  6547. ((or (eq org-indirect-buffer-display 'new-frame)
  6548. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  6549. (select-frame (make-frame))
  6550. (delete-other-windows)
  6551. (org-pop-to-buffer-same-window ibuf)
  6552. (org-set-frame-title heading))
  6553. ((eq org-indirect-buffer-display 'dedicated-frame)
  6554. (raise-frame
  6555. (select-frame (or (and org-indirect-dedicated-frame
  6556. (frame-live-p org-indirect-dedicated-frame)
  6557. org-indirect-dedicated-frame)
  6558. (setq org-indirect-dedicated-frame (make-frame)))))
  6559. (delete-other-windows)
  6560. (org-pop-to-buffer-same-window ibuf)
  6561. (org-set-frame-title (concat "Indirect: " heading)))
  6562. ((eq org-indirect-buffer-display 'current-window)
  6563. (org-pop-to-buffer-same-window ibuf))
  6564. ((eq org-indirect-buffer-display 'other-window)
  6565. (pop-to-buffer ibuf))
  6566. (t (error "Invalid value")))
  6567. (if (featurep 'xemacs)
  6568. (save-excursion (org-mode) (turn-on-font-lock)))
  6569. (narrow-to-region beg end)
  6570. (show-all)
  6571. (goto-char pos)
  6572. (run-hook-with-args 'org-cycle-hook 'all)
  6573. (and (window-live-p cwin) (select-window cwin))))
  6574. (defun org-get-indirect-buffer (&optional buffer)
  6575. (setq buffer (or buffer (current-buffer)))
  6576. (let ((n 1) (base (buffer-name buffer)) bname)
  6577. (while (buffer-live-p
  6578. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  6579. (setq n (1+ n)))
  6580. (condition-case nil
  6581. (make-indirect-buffer buffer bname 'clone)
  6582. (error (make-indirect-buffer buffer bname)))))
  6583. (defun org-set-frame-title (title)
  6584. "Set the title of the current frame to the string TITLE."
  6585. ;; FIXME: how to name a single frame in XEmacs???
  6586. (unless (featurep 'xemacs)
  6587. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  6588. ;;;; Structure editing
  6589. ;;; Inserting headlines
  6590. (defun org-previous-line-empty-p (&optional next)
  6591. "Is the previous line a blank line?
  6592. When NEXT is non-nil, check the next line instead."
  6593. (save-excursion
  6594. (and (not (bobp))
  6595. (or (beginning-of-line (if next 2 0)) t)
  6596. (save-match-data
  6597. (looking-at "[ \t]*$")))))
  6598. (defun org-insert-heading (&optional arg invisible-ok)
  6599. "Insert a new heading or item with same depth at point.
  6600. If point is in a plain list and ARG is nil, create a new list item.
  6601. With one universal prefix argument, insert a heading even in lists.
  6602. With two universal prefix arguments, insert the heading at the end
  6603. of the parent subtree.
  6604. If point is at the beginning of a headline, insert a sibling before
  6605. the current headline. If point is not at the beginning, split the line
  6606. and create a new headline with the text in the current line after point
  6607. \(see `org-M-RET-may-split-line' on how to modify this behavior).
  6608. When INVISIBLE-OK is set, stop at invisible headlines when going back.
  6609. This is important for non-interactive uses of the command."
  6610. (interactive "P")
  6611. (cond
  6612. ((or (= (buffer-size) 0)
  6613. (and (not (save-excursion
  6614. (and (ignore-errors (org-back-to-heading invisible-ok))
  6615. (org-at-heading-p))))
  6616. (or arg (not (org-in-item-p)))))
  6617. (insert
  6618. (if (org-previous-line-empty-p) "" "\n")
  6619. (if (org-in-src-block-p) ",* " "* "))
  6620. (run-hooks 'org-insert-heading-hook))
  6621. ((or arg (not (org-insert-item
  6622. (save-excursion
  6623. (beginning-of-line)
  6624. (looking-at org-list-full-item-re)
  6625. (match-string 3)))))
  6626. (let (begn endn)
  6627. (when (org-buffer-narrowed-p)
  6628. (setq begn (point-min) endn (point-max))
  6629. (widen))
  6630. (let* ((empty-line-p nil)
  6631. (eops (equal arg '(16))) ; insert at end of parent subtree
  6632. (org-insert-heading-respect-content
  6633. (or (not (null arg)) org-insert-heading-respect-content))
  6634. (level nil)
  6635. (on-heading (org-at-heading-p))
  6636. (head (save-excursion
  6637. (condition-case nil
  6638. (progn
  6639. (org-back-to-heading invisible-ok)
  6640. (when (and (not on-heading)
  6641. (featurep 'org-inlinetask)
  6642. (integerp org-inlinetask-min-level)
  6643. (>= (length (match-string 0))
  6644. org-inlinetask-min-level))
  6645. ;; Find a heading level before the inline task
  6646. (while (and (setq level (org-up-heading-safe))
  6647. (>= level org-inlinetask-min-level)))
  6648. (if (org-at-heading-p)
  6649. (org-back-to-heading invisible-ok)
  6650. (error "This should not happen")))
  6651. (unless (and (save-excursion
  6652. (save-match-data
  6653. (org-backward-heading-same-level 1 invisible-ok))
  6654. (= (point) (match-beginning 0)))
  6655. (not (org-previous-line-empty-p t)))
  6656. (setq empty-line-p (org-previous-line-empty-p)))
  6657. (match-string 0))
  6658. (error "* "))))
  6659. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6660. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  6661. pos hide-previous previous-pos)
  6662. (if ;; At the beginning of a heading, open a new line for insertiong
  6663. (and (bolp) (org-at-heading-p)
  6664. (not eops)
  6665. (or (bobp)
  6666. (save-excursion (backward-char 1) (not (outline-invisible-p)))))
  6667. (open-line (if blank 2 1))
  6668. (save-excursion
  6669. (setq previous-pos (point-at-bol))
  6670. (end-of-line)
  6671. (setq hide-previous (outline-invisible-p)))
  6672. (and org-insert-heading-respect-content
  6673. (save-excursion
  6674. (while (outline-invisible-p)
  6675. (org-show-subtree)
  6676. (org-up-heading-safe))))
  6677. (let ((split
  6678. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  6679. (save-excursion
  6680. (let ((p (point)))
  6681. (goto-char (point-at-bol))
  6682. (and (looking-at org-complex-heading-regexp)
  6683. (match-beginning 4)
  6684. (> p (match-beginning 4)))))))
  6685. tags pos)
  6686. (cond
  6687. ;; Insert a new line, possibly at end of parent subtree
  6688. (org-insert-heading-respect-content
  6689. (if (not eops)
  6690. (progn
  6691. (org-end-of-subtree nil t)
  6692. (and (looking-at "^\\*") (backward-char 1))
  6693. (while (and (not (bobp))
  6694. ;; Don't delete spaces in empty headlines
  6695. (not (looking-back org-outline-regexp))
  6696. (member (char-before) '(?\ ?\t ?\n)))
  6697. (backward-delete-char 1)))
  6698. (let ((p (point)))
  6699. (org-up-heading-safe)
  6700. (if (= p (point))
  6701. (goto-char (point-max))
  6702. (org-end-of-subtree nil t))))
  6703. (when (featurep 'org-inlinetask)
  6704. (while (and (not (eobp))
  6705. (looking-at "\\(\\*+\\)[ \t]+")
  6706. (>= (length (match-string 1))
  6707. org-inlinetask-min-level))
  6708. (org-end-of-subtree nil t)))
  6709. (or (bolp) (newline))
  6710. (or (org-previous-line-empty-p)
  6711. (and blank (newline)))
  6712. (if (or empty-line-p eops) (open-line 1)))
  6713. ;; Insert a headling containing text after point
  6714. ((org-at-heading-p)
  6715. (when hide-previous
  6716. (show-children)
  6717. (org-show-entry))
  6718. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?[ \t]*$")
  6719. (setq tags (and (match-end 2) (match-string 2)))
  6720. (and (match-end 1)
  6721. (delete-region (match-beginning 1) (match-end 1)))
  6722. (setq pos (point-at-bol))
  6723. (or split (end-of-line 1))
  6724. (delete-horizontal-space)
  6725. (if (string-match "\\`\\*+\\'"
  6726. (buffer-substring (point-at-bol) (point)))
  6727. (insert " "))
  6728. (newline (if blank 2 1))
  6729. (when tags
  6730. (save-excursion
  6731. (goto-char pos)
  6732. (end-of-line 1)
  6733. (insert " " tags)
  6734. (org-set-tags nil 'align))))
  6735. (t
  6736. (or split (end-of-line 1))
  6737. (newline (if blank 2 1))))))
  6738. (insert head) (just-one-space)
  6739. (setq pos (point))
  6740. (end-of-line 1)
  6741. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  6742. (when (and org-insert-heading-respect-content hide-previous)
  6743. (save-excursion
  6744. (goto-char previous-pos)
  6745. (hide-subtree)))
  6746. (when (and begn endn)
  6747. (narrow-to-region (min (point) begn) (max (point) endn)))
  6748. (run-hooks 'org-insert-heading-hook))))))
  6749. (defun org-get-heading (&optional no-tags no-todo)
  6750. "Return the heading of the current entry, without the stars.
  6751. When NO-TAGS is non-nil, don't include tags.
  6752. When NO-TODO is non-nil, don't include TODO keywords."
  6753. (save-excursion
  6754. (org-back-to-heading t)
  6755. (cond
  6756. ((and no-tags no-todo)
  6757. (looking-at org-complex-heading-regexp)
  6758. (match-string 4))
  6759. (no-tags
  6760. (looking-at (concat org-outline-regexp
  6761. "\\(.*?\\)"
  6762. "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
  6763. (match-string 1))
  6764. (no-todo
  6765. (looking-at org-todo-line-regexp)
  6766. (match-string 3))
  6767. (t (looking-at org-heading-regexp)
  6768. (match-string 2)))))
  6769. (defvar orgstruct-mode) ; defined below
  6770. (defun org-heading-components ()
  6771. "Return the components of the current heading.
  6772. This is a list with the following elements:
  6773. - the level as an integer
  6774. - the reduced level, different if `org-odd-levels-only' is set.
  6775. - the TODO keyword, or nil
  6776. - the priority character, like ?A, or nil if no priority is given
  6777. - the headline text itself, or the tags string if no headline text
  6778. - the tags string, or nil."
  6779. (save-excursion
  6780. (org-back-to-heading t)
  6781. (if (let (case-fold-search)
  6782. (looking-at
  6783. (if orgstruct-mode
  6784. org-heading-regexp
  6785. org-complex-heading-regexp)))
  6786. (if orgstruct-mode
  6787. (list (length (match-string 1))
  6788. (org-reduced-level (length (match-string 1)))
  6789. nil
  6790. nil
  6791. (match-string 2)
  6792. nil)
  6793. (list (length (match-string 1))
  6794. (org-reduced-level (length (match-string 1)))
  6795. (org-match-string-no-properties 2)
  6796. (and (match-end 3) (aref (match-string 3) 2))
  6797. (org-match-string-no-properties 4)
  6798. (org-match-string-no-properties 5))))))
  6799. (defun org-get-entry ()
  6800. "Get the entry text, after heading, entire subtree."
  6801. (save-excursion
  6802. (org-back-to-heading t)
  6803. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  6804. (defun org-insert-heading-after-current ()
  6805. "Insert a new heading with same level as current, after current subtree."
  6806. (interactive)
  6807. (org-back-to-heading)
  6808. (org-insert-heading)
  6809. (org-move-subtree-down)
  6810. (end-of-line 1))
  6811. (defun org-insert-heading-respect-content (&optional arg invisible-ok)
  6812. "Insert heading with `org-insert-heading-respect-content' set to t."
  6813. (interactive "P")
  6814. (let ((org-insert-heading-respect-content t))
  6815. (org-insert-heading arg invisible-ok)))
  6816. (defun org-insert-todo-heading-respect-content (&optional force-state)
  6817. "Insert TODO heading with `org-insert-heading-respect-content' set to t."
  6818. (interactive "P")
  6819. (let ((org-insert-heading-respect-content t))
  6820. (org-insert-todo-heading force-state t)))
  6821. (defun org-insert-todo-heading (arg &optional force-heading)
  6822. "Insert a new heading with the same level and TODO state as current heading.
  6823. If the heading has no TODO state, or if the state is DONE, use the first
  6824. state (TODO by default). Also one prefix arg, force first state. With two
  6825. prefix args, force inserting at the end of the parent subtree."
  6826. (interactive "P")
  6827. (when (or force-heading (not (org-insert-item 'checkbox)))
  6828. (org-insert-heading (or (and (equal arg '(16)) '(16))
  6829. force-heading))
  6830. (save-excursion
  6831. (org-back-to-heading)
  6832. (outline-previous-heading)
  6833. (looking-at org-todo-line-regexp))
  6834. (let*
  6835. ((new-mark-x
  6836. (if (or arg
  6837. (not (match-beginning 2))
  6838. (member (match-string 2) org-done-keywords))
  6839. (car org-todo-keywords-1)
  6840. (match-string 2)))
  6841. (new-mark
  6842. (or
  6843. (run-hook-with-args-until-success
  6844. 'org-todo-get-default-hook new-mark-x nil)
  6845. new-mark-x)))
  6846. (beginning-of-line 1)
  6847. (and (looking-at org-outline-regexp) (goto-char (match-end 0))
  6848. (if org-treat-insert-todo-heading-as-state-change
  6849. (org-todo new-mark)
  6850. (insert new-mark " "))))
  6851. (when org-provide-todo-statistics
  6852. (org-update-parent-todo-statistics))))
  6853. (defun org-insert-subheading (arg)
  6854. "Insert a new subheading and demote it.
  6855. Works for outline headings and for plain lists alike."
  6856. (interactive "P")
  6857. (org-insert-heading arg)
  6858. (cond
  6859. ((org-at-heading-p) (org-do-demote))
  6860. ((org-at-item-p) (org-indent-item))))
  6861. (defun org-insert-todo-subheading (arg)
  6862. "Insert a new subheading with TODO keyword or checkbox and demote it.
  6863. Works for outline headings and for plain lists alike."
  6864. (interactive "P")
  6865. (org-insert-todo-heading arg)
  6866. (cond
  6867. ((org-at-heading-p) (org-do-demote))
  6868. ((org-at-item-p) (org-indent-item))))
  6869. ;;; Promotion and Demotion
  6870. (defvar org-after-demote-entry-hook nil
  6871. "Hook run after an entry has been demoted.
  6872. The cursor will be at the beginning of the entry.
  6873. When a subtree is being demoted, the hook will be called for each node.")
  6874. (defvar org-after-promote-entry-hook nil
  6875. "Hook run after an entry has been promoted.
  6876. The cursor will be at the beginning of the entry.
  6877. When a subtree is being promoted, the hook will be called for each node.")
  6878. (defun org-promote-subtree ()
  6879. "Promote the entire subtree.
  6880. See also `org-promote'."
  6881. (interactive)
  6882. (save-excursion
  6883. (org-with-limited-levels (org-map-tree 'org-promote)))
  6884. (org-fix-position-after-promote))
  6885. (defun org-demote-subtree ()
  6886. "Demote the entire subtree. See `org-demote'.
  6887. See also `org-promote'."
  6888. (interactive)
  6889. (save-excursion
  6890. (org-with-limited-levels (org-map-tree 'org-demote)))
  6891. (org-fix-position-after-promote))
  6892. (defun org-do-promote ()
  6893. "Promote the current heading higher up the tree.
  6894. If the region is active in `transient-mark-mode', promote all headings
  6895. in the region."
  6896. (interactive)
  6897. (save-excursion
  6898. (if (org-region-active-p)
  6899. (org-map-region 'org-promote (region-beginning) (region-end))
  6900. (org-promote)))
  6901. (org-fix-position-after-promote))
  6902. (defun org-do-demote ()
  6903. "Demote the current heading lower down the tree.
  6904. If the region is active in `transient-mark-mode', demote all headings
  6905. in the region."
  6906. (interactive)
  6907. (save-excursion
  6908. (if (org-region-active-p)
  6909. (org-map-region 'org-demote (region-beginning) (region-end))
  6910. (org-demote)))
  6911. (org-fix-position-after-promote))
  6912. (defun org-fix-position-after-promote ()
  6913. "Make sure that after pro/demotion cursor position is right."
  6914. (let ((pos (point)))
  6915. (when (save-excursion
  6916. (beginning-of-line 1)
  6917. (looking-at org-todo-line-regexp)
  6918. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  6919. (cond ((eobp) (insert " "))
  6920. ((eolp) (insert " "))
  6921. ((equal (char-after) ?\ ) (forward-char 1))))))
  6922. (defun org-current-level ()
  6923. "Return the level of the current entry, or nil if before the first headline.
  6924. The level is the number of stars at the beginning of the headline."
  6925. (save-excursion
  6926. (org-with-limited-levels
  6927. (if (ignore-errors (org-back-to-heading t))
  6928. (funcall outline-level)))))
  6929. (defun org-get-previous-line-level ()
  6930. "Return the outline depth of the last headline before the current line.
  6931. Returns 0 for the first headline in the buffer, and nil if before the
  6932. first headline."
  6933. (let ((current-level (org-current-level))
  6934. (prev-level (when (> (line-number-at-pos) 1)
  6935. (save-excursion
  6936. (beginning-of-line 0)
  6937. (org-current-level)))))
  6938. (cond ((null current-level) nil) ; Before first headline
  6939. ((null prev-level) 0) ; At first headline
  6940. (prev-level))))
  6941. (defun org-reduced-level (l)
  6942. "Compute the effective level of a heading.
  6943. This takes into account the setting of `org-odd-levels-only'."
  6944. (cond
  6945. ((zerop l) 0)
  6946. (org-odd-levels-only (1+ (floor (/ l 2))))
  6947. (t l)))
  6948. (defun org-level-increment ()
  6949. "Return the number of stars that will be added or removed at a
  6950. time to headlines when structure editing, based on the value of
  6951. `org-odd-levels-only'."
  6952. (if org-odd-levels-only 2 1))
  6953. (defun org-get-valid-level (level &optional change)
  6954. "Rectify a level change under the influence of `org-odd-levels-only'
  6955. LEVEL is a current level, CHANGE is by how much the level should be
  6956. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  6957. even level numbers will become the next higher odd number."
  6958. (if org-odd-levels-only
  6959. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  6960. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  6961. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  6962. (max 1 (+ level (or change 0)))))
  6963. (if (boundp 'define-obsolete-function-alias)
  6964. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  6965. (define-obsolete-function-alias 'org-get-legal-level
  6966. 'org-get-valid-level)
  6967. (define-obsolete-function-alias 'org-get-legal-level
  6968. 'org-get-valid-level "23.1")))
  6969. (defvar org-called-with-limited-levels nil) ;; Dynamically bound in
  6970. ;; ̀org-with-limited-levels'
  6971. (defun org-promote ()
  6972. "Promote the current heading higher up the tree.
  6973. If the region is active in `transient-mark-mode', promote all headings
  6974. in the region."
  6975. (org-back-to-heading t)
  6976. (let* ((level (save-match-data (funcall outline-level)))
  6977. (after-change-functions (remove 'flyspell-after-change-function
  6978. after-change-functions))
  6979. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  6980. (diff (abs (- level (length up-head) -1))))
  6981. (cond ((and (= level 1) org-called-with-limited-levels
  6982. org-allow-promoting-top-level-subtree)
  6983. (replace-match "# " nil t))
  6984. ((= level 1)
  6985. (error "Cannot promote to level 0. UNDO to recover if necessary"))
  6986. (t (replace-match up-head nil t)))
  6987. ;; Fixup tag positioning
  6988. (unless (= level 1)
  6989. (and org-auto-align-tags (org-set-tags nil t))
  6990. (if org-adapt-indentation (org-fixup-indentation (- diff))))
  6991. (run-hooks 'org-after-promote-entry-hook)))
  6992. (defun org-demote ()
  6993. "Demote the current heading lower down the tree.
  6994. If the region is active in `transient-mark-mode', demote all headings
  6995. in the region."
  6996. (org-back-to-heading t)
  6997. (let* ((level (save-match-data (funcall outline-level)))
  6998. (after-change-functions (remove 'flyspell-after-change-function
  6999. after-change-functions))
  7000. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  7001. (diff (abs (- level (length down-head) -1))))
  7002. (replace-match down-head nil t)
  7003. ;; Fixup tag positioning
  7004. (and org-auto-align-tags (org-set-tags nil t))
  7005. (if org-adapt-indentation (org-fixup-indentation diff))
  7006. (run-hooks 'org-after-demote-entry-hook)))
  7007. (defun org-cycle-level ()
  7008. "Cycle the level of an empty headline through possible states.
  7009. This goes first to child, then to parent, level, then up the hierarchy.
  7010. After top level, it switches back to sibling level."
  7011. (interactive)
  7012. (let ((org-adapt-indentation nil))
  7013. (when (org-point-at-end-of-empty-headline)
  7014. (setq this-command 'org-cycle-level) ; Only needed for caching
  7015. (let ((cur-level (org-current-level))
  7016. (prev-level (org-get-previous-line-level)))
  7017. (cond
  7018. ;; If first headline in file, promote to top-level.
  7019. ((= prev-level 0)
  7020. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7021. do (org-do-promote)))
  7022. ;; If same level as prev, demote one.
  7023. ((= prev-level cur-level)
  7024. (org-do-demote))
  7025. ;; If parent is top-level, promote to top level if not already.
  7026. ((= prev-level 1)
  7027. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7028. do (org-do-promote)))
  7029. ;; If top-level, return to prev-level.
  7030. ((= cur-level 1)
  7031. (loop repeat (/ (- prev-level 1) (org-level-increment))
  7032. do (org-do-demote)))
  7033. ;; If less than prev-level, promote one.
  7034. ((< cur-level prev-level)
  7035. (org-do-promote))
  7036. ;; If deeper than prev-level, promote until higher than
  7037. ;; prev-level.
  7038. ((> cur-level prev-level)
  7039. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  7040. do (org-do-promote))))
  7041. t))))
  7042. (defun org-map-tree (fun)
  7043. "Call FUN for every heading underneath the current one."
  7044. (org-back-to-heading)
  7045. (let ((level (funcall outline-level)))
  7046. (save-excursion
  7047. (funcall fun)
  7048. (while (and (progn
  7049. (outline-next-heading)
  7050. (> (funcall outline-level) level))
  7051. (not (eobp)))
  7052. (funcall fun)))))
  7053. (defun org-map-region (fun beg end)
  7054. "Call FUN for every heading between BEG and END."
  7055. (let ((org-ignore-region t))
  7056. (save-excursion
  7057. (setq end (copy-marker end))
  7058. (goto-char beg)
  7059. (if (and (re-search-forward org-outline-regexp-bol nil t)
  7060. (< (point) end))
  7061. (funcall fun))
  7062. (while (and (progn
  7063. (outline-next-heading)
  7064. (< (point) end))
  7065. (not (eobp)))
  7066. (funcall fun)))))
  7067. (defvar org-property-end-re) ; silence byte-compiler
  7068. (defun org-fixup-indentation (diff)
  7069. "Change the indentation in the current entry by DIFF.
  7070. However, if any line in the current entry has no indentation, or if it
  7071. would end up with no indentation after the change, nothing at all is done."
  7072. (save-excursion
  7073. (let ((end (save-excursion (outline-next-heading)
  7074. (point-marker)))
  7075. (prohibit (if (> diff 0)
  7076. "^\\S-"
  7077. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  7078. col)
  7079. (unless (save-excursion (end-of-line 1)
  7080. (re-search-forward prohibit end t))
  7081. (while (and (< (point) end)
  7082. (re-search-forward "^[ \t]+" end t))
  7083. (goto-char (match-end 0))
  7084. (setq col (current-column))
  7085. (if (< diff 0) (replace-match ""))
  7086. (org-indent-to-column (+ diff col))))
  7087. (move-marker end nil))))
  7088. (defun org-convert-to-odd-levels ()
  7089. "Convert an org-mode file with all levels allowed to one with odd levels.
  7090. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  7091. level 5 etc."
  7092. (interactive)
  7093. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  7094. (let ((outline-level 'org-outline-level)
  7095. (org-odd-levels-only nil) n)
  7096. (save-excursion
  7097. (goto-char (point-min))
  7098. (while (re-search-forward "^\\*\\*+ " nil t)
  7099. (setq n (- (length (match-string 0)) 2))
  7100. (while (>= (setq n (1- n)) 0)
  7101. (org-demote))
  7102. (end-of-line 1))))))
  7103. (defun org-convert-to-oddeven-levels ()
  7104. "Convert an org-mode file with only odd levels to one with odd/even levels.
  7105. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  7106. file contains a section with an even level, conversion would
  7107. destroy the structure of the file. An error is signaled in this
  7108. case."
  7109. (interactive)
  7110. (goto-char (point-min))
  7111. ;; First check if there are no even levels
  7112. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  7113. (org-show-context t)
  7114. (error "Not all levels are odd in this file. Conversion not possible"))
  7115. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  7116. (let ((outline-regexp org-outline-regexp)
  7117. (outline-level 'org-outline-level)
  7118. (org-odd-levels-only nil) n)
  7119. (save-excursion
  7120. (goto-char (point-min))
  7121. (while (re-search-forward "^\\*\\*+ " nil t)
  7122. (setq n (/ (1- (length (match-string 0))) 2))
  7123. (while (>= (setq n (1- n)) 0)
  7124. (org-promote))
  7125. (end-of-line 1))))))
  7126. (defun org-tr-level (n)
  7127. "Make N odd if required."
  7128. (if org-odd-levels-only (1+ (/ n 2)) n))
  7129. ;;; Vertical tree motion, cutting and pasting of subtrees
  7130. (defun org-move-subtree-up (&optional arg)
  7131. "Move the current subtree up past ARG headlines of the same level."
  7132. (interactive "p")
  7133. (org-move-subtree-down (- (prefix-numeric-value arg))))
  7134. (defun org-move-subtree-down (&optional arg)
  7135. "Move the current subtree down past ARG headlines of the same level."
  7136. (interactive "p")
  7137. (setq arg (prefix-numeric-value arg))
  7138. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  7139. 'org-get-last-sibling))
  7140. (ins-point (make-marker))
  7141. (cnt (abs arg))
  7142. (col (current-column))
  7143. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  7144. ;; Select the tree
  7145. (org-back-to-heading)
  7146. (setq beg0 (point))
  7147. (save-excursion
  7148. (setq ne-beg (org-back-over-empty-lines))
  7149. (setq beg (point)))
  7150. (save-match-data
  7151. (save-excursion (outline-end-of-heading)
  7152. (setq folded (outline-invisible-p)))
  7153. (outline-end-of-subtree))
  7154. (outline-next-heading)
  7155. (setq ne-end (org-back-over-empty-lines))
  7156. (setq end (point))
  7157. (goto-char beg0)
  7158. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  7159. ;; include less whitespace
  7160. (save-excursion
  7161. (goto-char beg)
  7162. (forward-line (- ne-beg ne-end))
  7163. (setq beg (point))))
  7164. ;; Find insertion point, with error handling
  7165. (while (> cnt 0)
  7166. (or (and (funcall movfunc) (looking-at org-outline-regexp))
  7167. (progn (goto-char beg0)
  7168. (user-error "Cannot move past superior level or buffer limit")))
  7169. (setq cnt (1- cnt)))
  7170. (if (> arg 0)
  7171. ;; Moving forward - still need to move over subtree
  7172. (progn (org-end-of-subtree t t)
  7173. (save-excursion
  7174. (org-back-over-empty-lines)
  7175. (or (bolp) (newline)))))
  7176. (setq ne-ins (org-back-over-empty-lines))
  7177. (move-marker ins-point (point))
  7178. (setq txt (buffer-substring beg end))
  7179. (org-save-markers-in-region beg end)
  7180. (delete-region beg end)
  7181. (org-remove-empty-overlays-at beg)
  7182. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  7183. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  7184. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  7185. (let ((bbb (point)))
  7186. (insert-before-markers txt)
  7187. (org-reinstall-markers-in-region bbb)
  7188. (move-marker ins-point bbb))
  7189. (or (bolp) (insert "\n"))
  7190. (setq ins-end (point))
  7191. (goto-char ins-point)
  7192. (org-skip-whitespace)
  7193. (when (and (< arg 0)
  7194. (org-first-sibling-p)
  7195. (> ne-ins ne-beg))
  7196. ;; Move whitespace back to beginning
  7197. (save-excursion
  7198. (goto-char ins-end)
  7199. (let ((kill-whole-line t))
  7200. (kill-line (- ne-ins ne-beg)) (point)))
  7201. (insert (make-string (- ne-ins ne-beg) ?\n)))
  7202. (move-marker ins-point nil)
  7203. (if folded
  7204. (hide-subtree)
  7205. (org-show-entry)
  7206. (show-children)
  7207. (org-cycle-hide-drawers 'children))
  7208. (org-clean-visibility-after-subtree-move)
  7209. ;; move back to the initial column we were at
  7210. (move-to-column col)))
  7211. (defvar org-subtree-clip ""
  7212. "Clipboard for cut and paste of subtrees.
  7213. This is actually only a copy of the kill, because we use the normal kill
  7214. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  7215. (defvar org-subtree-clip-folded nil
  7216. "Was the last copied subtree folded?
  7217. This is used to fold the tree back after pasting.")
  7218. (defun org-cut-subtree (&optional n)
  7219. "Cut the current subtree into the clipboard.
  7220. With prefix arg N, cut this many sequential subtrees.
  7221. This is a short-hand for marking the subtree and then cutting it."
  7222. (interactive "p")
  7223. (org-copy-subtree n 'cut))
  7224. (defun org-copy-subtree (&optional n cut force-store-markers nosubtrees)
  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 copying it.
  7228. If CUT is non-nil, actually cut the subtree.
  7229. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  7230. of some markers in the region, even if CUT is non-nil. This is
  7231. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  7232. (interactive "p")
  7233. (let (beg end folded (beg0 (point)))
  7234. (if (org-called-interactively-p 'any)
  7235. (org-back-to-heading nil) ; take what looks like a subtree
  7236. (org-back-to-heading t)) ; take what is really there
  7237. (setq beg (point))
  7238. (skip-chars-forward " \t\r\n")
  7239. (save-match-data
  7240. (if nosubtrees
  7241. (outline-next-heading)
  7242. (save-excursion (outline-end-of-heading)
  7243. (setq folded (outline-invisible-p)))
  7244. (condition-case nil
  7245. (org-forward-heading-same-level (1- n) t)
  7246. (error nil))
  7247. (org-end-of-subtree t t)))
  7248. (setq end (point))
  7249. (goto-char beg0)
  7250. (when (> end beg)
  7251. (setq org-subtree-clip-folded folded)
  7252. (when (or cut force-store-markers)
  7253. (org-save-markers-in-region beg end))
  7254. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  7255. (setq org-subtree-clip (current-kill 0))
  7256. (message "%s: Subtree(s) with %d characters"
  7257. (if cut "Cut" "Copied")
  7258. (length org-subtree-clip)))))
  7259. (defun org-paste-subtree (&optional level tree for-yank)
  7260. "Paste the clipboard as a subtree, with modification of headline level.
  7261. The entire subtree is promoted or demoted in order to match a new headline
  7262. level.
  7263. If the cursor is at the beginning of a headline, the same level as
  7264. that headline is used to paste the tree.
  7265. If not, the new level is derived from the *visible* headings
  7266. before and after the insertion point, and taken to be the inferior headline
  7267. level of the two. So if the previous visible heading is level 3 and the
  7268. next is level 4 (or vice versa), level 4 will be used for insertion.
  7269. This makes sure that the subtree remains an independent subtree and does
  7270. not swallow low level entries.
  7271. You can also force a different level, either by using a numeric prefix
  7272. argument, or by inserting the heading marker by hand. For example, if the
  7273. cursor is after \"*****\", then the tree will be shifted to level 5.
  7274. If optional TREE is given, use this text instead of the kill ring.
  7275. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  7276. move back over whitespace before inserting, and move point to the end of
  7277. the inserted text when done."
  7278. (interactive "P")
  7279. (setq tree (or tree (and kill-ring (current-kill 0))))
  7280. (unless (org-kill-is-subtree-p tree)
  7281. (error "%s"
  7282. (substitute-command-keys
  7283. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  7284. (org-with-limited-levels
  7285. (let* ((visp (not (outline-invisible-p)))
  7286. (txt tree)
  7287. (^re_ "\\(\\*+\\)[ \t]*")
  7288. (old-level (if (string-match org-outline-regexp-bol txt)
  7289. (- (match-end 0) (match-beginning 0) 1)
  7290. -1))
  7291. (force-level (cond (level (prefix-numeric-value level))
  7292. ((and (looking-at "[ \t]*$")
  7293. (string-match
  7294. "^\\*+$" (buffer-substring
  7295. (point-at-bol) (point))))
  7296. (- (match-end 1) (match-beginning 1)))
  7297. ((and (bolp)
  7298. (looking-at org-outline-regexp))
  7299. (- (match-end 0) (point) 1))))
  7300. (previous-level (save-excursion
  7301. (condition-case nil
  7302. (progn
  7303. (outline-previous-visible-heading 1)
  7304. (if (looking-at ^re_)
  7305. (- (match-end 0) (match-beginning 0) 1)
  7306. 1))
  7307. (error 1))))
  7308. (next-level (save-excursion
  7309. (condition-case nil
  7310. (progn
  7311. (or (looking-at org-outline-regexp)
  7312. (outline-next-visible-heading 1))
  7313. (if (looking-at ^re_)
  7314. (- (match-end 0) (match-beginning 0) 1)
  7315. 1))
  7316. (error 1))))
  7317. (new-level (or force-level (max previous-level next-level)))
  7318. (shift (if (or (= old-level -1)
  7319. (= new-level -1)
  7320. (= old-level new-level))
  7321. 0
  7322. (- new-level old-level)))
  7323. (delta (if (> shift 0) -1 1))
  7324. (func (if (> shift 0) 'org-demote 'org-promote))
  7325. (org-odd-levels-only nil)
  7326. beg end newend)
  7327. ;; Remove the forced level indicator
  7328. (if force-level
  7329. (delete-region (point-at-bol) (point)))
  7330. ;; Paste
  7331. (beginning-of-line (if (bolp) 1 2))
  7332. (setq beg (point))
  7333. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  7334. (insert-before-markers txt)
  7335. (unless (string-match "\n\\'" txt) (insert "\n"))
  7336. (setq newend (point))
  7337. (org-reinstall-markers-in-region beg)
  7338. (setq end (point))
  7339. (goto-char beg)
  7340. (skip-chars-forward " \t\n\r")
  7341. (setq beg (point))
  7342. (if (and (outline-invisible-p) visp)
  7343. (save-excursion (outline-show-heading)))
  7344. ;; Shift if necessary
  7345. (unless (= shift 0)
  7346. (save-restriction
  7347. (narrow-to-region beg end)
  7348. (while (not (= shift 0))
  7349. (org-map-region func (point-min) (point-max))
  7350. (setq shift (+ delta shift)))
  7351. (goto-char (point-min))
  7352. (setq newend (point-max))))
  7353. (when (or (org-called-interactively-p 'interactive) for-yank)
  7354. (message "Clipboard pasted as level %d subtree" new-level))
  7355. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  7356. kill-ring
  7357. (eq org-subtree-clip (current-kill 0))
  7358. org-subtree-clip-folded)
  7359. ;; The tree was folded before it was killed/copied
  7360. (hide-subtree))
  7361. (and for-yank (goto-char newend)))))
  7362. (defun org-kill-is-subtree-p (&optional txt)
  7363. "Check if the current kill is an outline subtree, or a set of trees.
  7364. Returns nil if kill does not start with a headline, or if the first
  7365. headline level is not the largest headline level in the tree.
  7366. So this will actually accept several entries of equal levels as well,
  7367. which is OK for `org-paste-subtree'.
  7368. If optional TXT is given, check this string instead of the current kill."
  7369. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  7370. (re (org-get-limited-outline-regexp))
  7371. (^re (concat "^" re))
  7372. (start-level (and kill
  7373. (string-match
  7374. (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
  7375. kill)
  7376. (- (match-end 2) (match-beginning 2) 1)))
  7377. (start (1+ (or (match-beginning 2) -1))))
  7378. (if (not start-level)
  7379. (progn
  7380. nil) ;; does not even start with a heading
  7381. (catch 'exit
  7382. (while (setq start (string-match ^re kill (1+ start)))
  7383. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  7384. (throw 'exit nil)))
  7385. t))))
  7386. (defvar org-markers-to-move nil
  7387. "Markers that should be moved with a cut-and-paste operation.
  7388. Those markers are stored together with their positions relative to
  7389. the start of the region.")
  7390. (defun org-save-markers-in-region (beg end)
  7391. "Check markers in region.
  7392. If these markers are between BEG and END, record their position relative
  7393. to BEG, so that after moving the block of text, we can put the markers back
  7394. into place.
  7395. This function gets called just before an entry or tree gets cut from the
  7396. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  7397. called immediately, to move the markers with the entries."
  7398. (setq org-markers-to-move nil)
  7399. (when (featurep 'org-clock)
  7400. (org-clock-save-markers-for-cut-and-paste beg end))
  7401. (when (featurep 'org-agenda)
  7402. (org-agenda-save-markers-for-cut-and-paste beg end)))
  7403. (defun org-check-and-save-marker (marker beg end)
  7404. "Check if MARKER is between BEG and END.
  7405. If yes, remember the marker and the distance to BEG."
  7406. (when (and (marker-buffer marker)
  7407. (equal (marker-buffer marker) (current-buffer)))
  7408. (if (and (>= marker beg) (< marker end))
  7409. (push (cons marker (- marker beg)) org-markers-to-move))))
  7410. (defun org-reinstall-markers-in-region (beg)
  7411. "Move all remembered markers to their position relative to BEG."
  7412. (mapc (lambda (x)
  7413. (move-marker (car x) (+ beg (cdr x))))
  7414. org-markers-to-move)
  7415. (setq org-markers-to-move nil))
  7416. (defun org-narrow-to-subtree ()
  7417. "Narrow buffer to the current subtree."
  7418. (interactive)
  7419. (save-excursion
  7420. (save-match-data
  7421. (org-with-limited-levels
  7422. (narrow-to-region
  7423. (progn (org-back-to-heading t) (point))
  7424. (progn (org-end-of-subtree t t)
  7425. (if (and (org-at-heading-p) (not (eobp))) (backward-char 1))
  7426. (point)))))))
  7427. (defun org-narrow-to-block ()
  7428. "Narrow buffer to the current block."
  7429. (interactive)
  7430. (let* ((case-fold-search t)
  7431. (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
  7432. "^[ \t]*#\\+end_.*")))
  7433. (if blockp
  7434. (narrow-to-region (car blockp) (cdr blockp))
  7435. (error "Not in a block"))))
  7436. (eval-when-compile
  7437. (defvar org-property-drawer-re))
  7438. (defvar org-property-start-re) ;; defined below
  7439. (defun org-clone-subtree-with-time-shift (n &optional shift)
  7440. "Clone the task (subtree) at point N times.
  7441. The clones will be inserted as siblings.
  7442. In interactive use, the user will be prompted for the number of
  7443. clones to be produced, and for a time SHIFT, which may be a
  7444. repeater as used in time stamps, for example `+3d'.
  7445. When a valid repeater is given and the entry contains any time
  7446. stamps, the clones will become a sequence in time, with time
  7447. stamps in the subtree shifted for each clone produced. If SHIFT
  7448. is nil or the empty string, time stamps will be left alone. The
  7449. ID property of the original subtree is removed.
  7450. If the original subtree did contain time stamps with a repeater,
  7451. the following will happen:
  7452. - the repeater will be removed in each clone
  7453. - an additional clone will be produced, with the current, unshifted
  7454. date(s) in the entry.
  7455. - the original entry will be placed *after* all the clones, with
  7456. repeater intact.
  7457. - the start days in the repeater in the original entry will be shifted
  7458. to past the last clone.
  7459. In this way you can spell out a number of instances of a repeating task,
  7460. and still retain the repeater to cover future instances of the task."
  7461. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  7462. (let (beg end template task idprop
  7463. shift-n shift-what doshift nmin nmax (n-no-remove -1)
  7464. (drawer-re org-drawer-regexp))
  7465. (if (not (and (integerp n) (> n 0)))
  7466. (error "Invalid number of replications %s" n))
  7467. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  7468. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([hdwmy]\\)[ \t]*\\'"
  7469. shift)))
  7470. (error "Invalid shift specification %s" shift))
  7471. (when doshift
  7472. (setq shift-n (string-to-number (match-string 1 shift))
  7473. shift-what (cdr (assoc (match-string 2 shift)
  7474. '(("d" . day) ("w" . week)
  7475. ("m" . month) ("y" . year))))))
  7476. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  7477. (setq nmin 1 nmax n)
  7478. (org-back-to-heading t)
  7479. (setq beg (point))
  7480. (setq idprop (org-entry-get nil "ID"))
  7481. (org-end-of-subtree t t)
  7482. (or (bolp) (insert "\n"))
  7483. (setq end (point))
  7484. (setq template (buffer-substring beg end))
  7485. (when (and doshift
  7486. (string-match "<[^<>\n]+ [.+]?\\+[0-9]+[hdwmy][^<>\n]*>" template))
  7487. (delete-region beg end)
  7488. (setq end beg)
  7489. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  7490. (goto-char end)
  7491. (loop for n from nmin to nmax do
  7492. ;; prepare clone
  7493. (with-temp-buffer
  7494. (insert template)
  7495. (org-mode)
  7496. (goto-char (point-min))
  7497. (org-show-subtree)
  7498. (and idprop (if org-clone-delete-id
  7499. (org-entry-delete nil "ID")
  7500. (org-id-get-create t)))
  7501. (unless (= n 0)
  7502. (while (re-search-forward "^[ \t]*CLOCK:.*$" nil t)
  7503. (kill-whole-line))
  7504. (goto-char (point-min))
  7505. (while (re-search-forward drawer-re nil t)
  7506. (mapc (lambda (d)
  7507. (org-remove-empty-drawer-at d (point))) org-drawers)))
  7508. (goto-char (point-min))
  7509. (when doshift
  7510. (while (re-search-forward org-ts-regexp-both nil t)
  7511. (org-timestamp-change (* n shift-n) shift-what))
  7512. (unless (= n n-no-remove)
  7513. (goto-char (point-min))
  7514. (while (re-search-forward org-ts-regexp nil t)
  7515. (save-excursion
  7516. (goto-char (match-beginning 0))
  7517. (if (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[hdwmy]\\)")
  7518. (delete-region (match-beginning 1) (match-end 1)))))))
  7519. (setq task (buffer-string)))
  7520. (insert task))
  7521. (goto-char beg)))
  7522. ;;; Outline Sorting
  7523. (defun org-sort (with-case)
  7524. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  7525. Optional argument WITH-CASE means sort case-sensitively."
  7526. (interactive "P")
  7527. (cond
  7528. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  7529. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  7530. (t
  7531. (org-call-with-arg 'org-sort-entries with-case))))
  7532. (defun org-sort-remove-invisible (s)
  7533. "Remove invisible links from string S."
  7534. (remove-text-properties 0 (length s) org-rm-props s)
  7535. (while (string-match org-bracket-link-regexp s)
  7536. (setq s (replace-match (if (match-end 2)
  7537. (match-string 3 s)
  7538. (match-string 1 s)) t t s)))
  7539. (let ((st (format " %s " s)))
  7540. (while (string-match org-emph-re st)
  7541. (setq st (replace-match (format " %s " (match-string 4 st)) t t st)))
  7542. (setq s (substring st 1 -1)))
  7543. s)
  7544. (defvar org-priority-regexp) ; defined later in the file
  7545. (defvar org-after-sorting-entries-or-items-hook nil
  7546. "Hook that is run after a bunch of entries or items have been sorted.
  7547. When children are sorted, the cursor is in the parent line when this
  7548. hook gets called. When a region or a plain list is sorted, the cursor
  7549. will be in the first entry of the sorted region/list.")
  7550. (defun org-sort-entries
  7551. (&optional with-case sorting-type getkey-func compare-func property)
  7552. "Sort entries on a certain level of an outline tree.
  7553. If there is an active region, the entries in the region are sorted.
  7554. Else, if the cursor is before the first entry, sort the top-level items.
  7555. Else, the children of the entry at point are sorted.
  7556. Sorting can be alphabetically, numerically, by date/time as given by
  7557. a time stamp, by a property, by priority order, or by a custom function.
  7558. The command prompts for the sorting type unless it has been given to the
  7559. function through the SORTING-TYPE argument, which needs to be a character,
  7560. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?o ?O ?r ?R ?f ?F). Here is the
  7561. precise meaning of each character:
  7562. n Numerically, by converting the beginning of the entry/item to a number.
  7563. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  7564. o By order of TODO keywords.
  7565. t By date/time, either the first active time stamp in the entry, or, if
  7566. none exist, by the first inactive one.
  7567. s By the scheduled date/time.
  7568. d By deadline date/time.
  7569. c By creation time, which is assumed to be the first inactive time stamp
  7570. at the beginning of a line.
  7571. p By priority according to the cookie.
  7572. r By the value of a property.
  7573. Capital letters will reverse the sort order.
  7574. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  7575. called with point at the beginning of the record. It must return either
  7576. a string or a number that should serve as the sorting key for that record.
  7577. Comparing entries ignores case by default. However, with an optional argument
  7578. WITH-CASE, the sorting considers case as well.
  7579. Sorting is done against the visible part of the headlines, it ignores hidden
  7580. links."
  7581. (interactive "P")
  7582. (let ((case-func (if with-case 'identity 'downcase))
  7583. (cmstr
  7584. ;; The clock marker is lost when using `sort-subr', let's
  7585. ;; store the clocking string.
  7586. (when (equal (marker-buffer org-clock-marker) (current-buffer))
  7587. (save-excursion
  7588. (goto-char org-clock-marker)
  7589. (looking-back "^.*") (match-string-no-properties 0))))
  7590. start beg end stars re re2
  7591. txt what tmp)
  7592. ;; Find beginning and end of region to sort
  7593. (cond
  7594. ((org-region-active-p)
  7595. ;; we will sort the region
  7596. (setq end (region-end)
  7597. what "region")
  7598. (goto-char (region-beginning))
  7599. (if (not (org-at-heading-p)) (outline-next-heading))
  7600. (setq start (point)))
  7601. ((or (org-at-heading-p)
  7602. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  7603. ;; we will sort the children of the current headline
  7604. (org-back-to-heading)
  7605. (setq start (point)
  7606. end (progn (org-end-of-subtree t t)
  7607. (or (bolp) (insert "\n"))
  7608. (org-back-over-empty-lines)
  7609. (point))
  7610. what "children")
  7611. (goto-char start)
  7612. (show-subtree)
  7613. (outline-next-heading))
  7614. (t
  7615. ;; we will sort the top-level entries in this file
  7616. (goto-char (point-min))
  7617. (or (org-at-heading-p) (outline-next-heading))
  7618. (setq start (point))
  7619. (goto-char (point-max))
  7620. (beginning-of-line 1)
  7621. (when (looking-at ".*?\\S-")
  7622. ;; File ends in a non-white line
  7623. (end-of-line 1)
  7624. (insert "\n"))
  7625. (setq end (point-max))
  7626. (setq what "top-level")
  7627. (goto-char start)
  7628. (show-all)))
  7629. (setq beg (point))
  7630. (if (>= beg end) (error "Nothing to sort"))
  7631. (looking-at "\\(\\*+\\)")
  7632. (setq stars (match-string 1)
  7633. re (concat "^" (regexp-quote stars) " +")
  7634. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  7635. txt (buffer-substring beg end))
  7636. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  7637. (if (and (not (equal stars "*")) (string-match re2 txt))
  7638. (error "Region to sort contains a level above the first entry"))
  7639. (unless sorting-type
  7640. (message
  7641. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  7642. [t]ime [s]cheduled [d]eadline [c]reated
  7643. A/N/P/R/O/F/T/S/D/C means reversed:"
  7644. what)
  7645. (setq sorting-type (read-char-exclusive))
  7646. (unless getkey-func
  7647. (and (= (downcase sorting-type) ?f)
  7648. (setq getkey-func
  7649. (org-icompleting-read "Sort using function: "
  7650. obarray 'fboundp t nil nil))
  7651. (setq getkey-func (intern getkey-func))))
  7652. (and (= (downcase sorting-type) ?r)
  7653. (not property)
  7654. (setq property
  7655. (org-icompleting-read "Property: "
  7656. (mapcar 'list (org-buffer-property-keys t))
  7657. nil t))))
  7658. (message "Sorting entries...")
  7659. (save-restriction
  7660. (narrow-to-region start end)
  7661. (let ((dcst (downcase sorting-type))
  7662. (case-fold-search nil)
  7663. (now (current-time)))
  7664. (sort-subr
  7665. (/= dcst sorting-type)
  7666. ;; This function moves to the beginning character of the "record" to
  7667. ;; be sorted.
  7668. (lambda nil
  7669. (if (re-search-forward re nil t)
  7670. (goto-char (match-beginning 0))
  7671. (goto-char (point-max))))
  7672. ;; This function moves to the last character of the "record" being
  7673. ;; sorted.
  7674. (lambda nil
  7675. (save-match-data
  7676. (condition-case nil
  7677. (outline-forward-same-level 1)
  7678. (error
  7679. (goto-char (point-max))))))
  7680. ;; This function returns the value that gets sorted against.
  7681. (lambda nil
  7682. (cond
  7683. ((= dcst ?n)
  7684. (if (looking-at org-complex-heading-regexp)
  7685. (string-to-number (org-sort-remove-invisible (match-string 4)))
  7686. nil))
  7687. ((= dcst ?a)
  7688. (if (looking-at org-complex-heading-regexp)
  7689. (funcall case-func (org-sort-remove-invisible (match-string 4)))
  7690. nil))
  7691. ((= dcst ?t)
  7692. (let ((end (save-excursion (outline-next-heading) (point))))
  7693. (if (or (re-search-forward org-ts-regexp end t)
  7694. (re-search-forward org-ts-regexp-both end t))
  7695. (org-time-string-to-seconds (match-string 0))
  7696. (org-float-time now))))
  7697. ((= dcst ?c)
  7698. (let ((end (save-excursion (outline-next-heading) (point))))
  7699. (if (re-search-forward
  7700. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  7701. end t)
  7702. (org-time-string-to-seconds (match-string 0))
  7703. (org-float-time now))))
  7704. ((= dcst ?s)
  7705. (let ((end (save-excursion (outline-next-heading) (point))))
  7706. (if (re-search-forward org-scheduled-time-regexp end t)
  7707. (org-time-string-to-seconds (match-string 1))
  7708. (org-float-time now))))
  7709. ((= dcst ?d)
  7710. (let ((end (save-excursion (outline-next-heading) (point))))
  7711. (if (re-search-forward org-deadline-time-regexp end t)
  7712. (org-time-string-to-seconds (match-string 1))
  7713. (org-float-time now))))
  7714. ((= dcst ?p)
  7715. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  7716. (string-to-char (match-string 2))
  7717. org-default-priority))
  7718. ((= dcst ?r)
  7719. (or (org-entry-get nil property) ""))
  7720. ((= dcst ?o)
  7721. (if (looking-at org-complex-heading-regexp)
  7722. (- 9999 (length (member (match-string 2)
  7723. org-todo-keywords-1)))))
  7724. ((= dcst ?f)
  7725. (if getkey-func
  7726. (progn
  7727. (setq tmp (funcall getkey-func))
  7728. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  7729. tmp)
  7730. (error "Invalid key function `%s'" getkey-func)))
  7731. (t (error "Invalid sorting type `%c'" sorting-type))))
  7732. nil
  7733. (cond
  7734. ((= dcst ?a) 'string<)
  7735. ((= dcst ?f) compare-func)
  7736. ((member dcst '(?p ?t ?s ?d ?c)) '<)))))
  7737. (run-hooks 'org-after-sorting-entries-or-items-hook)
  7738. ;; Reset the clock marker if needed
  7739. (when cmstr
  7740. (save-excursion
  7741. (goto-char start)
  7742. (search-forward cmstr nil t)
  7743. (move-marker org-clock-marker (point))))
  7744. (message "Sorting entries...done")))
  7745. (defun org-do-sort (table what &optional with-case sorting-type)
  7746. "Sort TABLE of WHAT according to SORTING-TYPE.
  7747. The user will be prompted for the SORTING-TYPE if the call to this
  7748. function does not specify it. WHAT is only for the prompt, to indicate
  7749. what is being sorted. The sorting key will be extracted from
  7750. the car of the elements of the table.
  7751. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  7752. (unless sorting-type
  7753. (message
  7754. "Sort %s: [a]lphabetic, [n]umeric, [t]ime. A/N/T means reversed:"
  7755. what)
  7756. (setq sorting-type (read-char-exclusive)))
  7757. (let ((dcst (downcase sorting-type))
  7758. extractfun comparefun)
  7759. ;; Define the appropriate functions
  7760. (cond
  7761. ((= dcst ?n)
  7762. (setq extractfun 'string-to-number
  7763. comparefun (if (= dcst sorting-type) '< '>)))
  7764. ((= dcst ?a)
  7765. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  7766. (lambda(x) (downcase (org-sort-remove-invisible x))))
  7767. comparefun (if (= dcst sorting-type)
  7768. 'string<
  7769. (lambda (a b) (and (not (string< a b))
  7770. (not (string= a b)))))))
  7771. ((= dcst ?t)
  7772. (setq extractfun
  7773. (lambda (x)
  7774. (if (or (string-match org-ts-regexp x)
  7775. (string-match org-ts-regexp-both x))
  7776. (org-float-time
  7777. (org-time-string-to-time (match-string 0 x)))
  7778. 0))
  7779. comparefun (if (= dcst sorting-type) '< '>)))
  7780. (t (error "Invalid sorting type `%c'" sorting-type)))
  7781. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  7782. table)
  7783. (lambda (a b) (funcall comparefun (car a) (car b))))))
  7784. ;;; The orgstruct minor mode
  7785. ;; Define a minor mode which can be used in other modes in order to
  7786. ;; integrate the org-mode structure editing commands.
  7787. ;; This is really a hack, because the org-mode structure commands use
  7788. ;; keys which normally belong to the major mode. Here is how it
  7789. ;; works: The minor mode defines all the keys necessary to operate the
  7790. ;; structure commands, but wraps the commands into a function which
  7791. ;; tests if the cursor is currently at a headline or a plain list
  7792. ;; item. If that is the case, the structure command is used,
  7793. ;; temporarily setting many Org-mode variables like regular
  7794. ;; expressions for filling etc. However, when any of those keys is
  7795. ;; used at a different location, function uses `key-binding' to look
  7796. ;; up if the key has an associated command in another currently active
  7797. ;; keymap (minor modes, major mode, global), and executes that
  7798. ;; command. There might be problems if any of the keys is otherwise
  7799. ;; used as a prefix key.
  7800. (defcustom orgstruct-heading-prefix-regexp nil
  7801. "Regexp that matches the custom prefix of Org headlines in
  7802. orgstruct(++)-mode."
  7803. :group 'org
  7804. :version "24.4"
  7805. :package-version '(Org . "8.0")
  7806. :type 'string)
  7807. ;;;###autoload(put 'orgstruct-heading-prefix-regexp 'safe-local-variable 'stringp)
  7808. (defcustom orgstruct-setup-hook nil
  7809. "Hook run after orgstruct-mode-map is filled."
  7810. :group 'org
  7811. :version "24.4"
  7812. :package-version '(Org . "8.0")
  7813. :type 'hook)
  7814. (defvar orgstruct-initialized nil)
  7815. (defvar org-local-vars nil
  7816. "List of local variables, for use by `orgstruct-mode'.")
  7817. ;;;###autoload
  7818. (define-minor-mode orgstruct-mode
  7819. "Toggle the minor mode `orgstruct-mode'.
  7820. This mode is for using Org-mode structure commands in other
  7821. modes. The following keys behave as if Org-mode were active, if
  7822. the cursor is on a headline, or on a plain list item (both as
  7823. defined by Org-mode)."
  7824. nil " OrgStruct" (make-sparse-keymap)
  7825. (when orgstruct-mode
  7826. (org-load-modules-maybe)
  7827. (unless orgstruct-initialized
  7828. (orgstruct-setup)
  7829. (setq orgstruct-initialized t))))
  7830. ;;;###autoload
  7831. (defun turn-on-orgstruct ()
  7832. "Unconditionally turn on `orgstruct-mode'."
  7833. (orgstruct-mode 1))
  7834. (defvar org-fb-vars nil)
  7835. (make-variable-buffer-local 'org-fb-vars)
  7836. (defun orgstruct++-mode (&optional arg)
  7837. "Toggle `orgstruct-mode', the enhanced version of it.
  7838. In addition to setting orgstruct-mode, this also exports all
  7839. indentation and autofilling variables from org-mode into the
  7840. buffer. It will also recognize item context in multiline items."
  7841. (interactive "P")
  7842. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  7843. (if (< arg 1)
  7844. (progn (orgstruct-mode -1)
  7845. (mapc (lambda(v)
  7846. (org-set-local (car v)
  7847. (if (eq (car-safe (cadr v)) 'quote) (cadadr v) (cadr v))))
  7848. org-fb-vars))
  7849. (orgstruct-mode 1)
  7850. (setq org-fb-vars nil)
  7851. (let (var val)
  7852. (mapc
  7853. (lambda (x)
  7854. (when (string-match
  7855. "^\\(paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|fill-prefix\\|indent-\\)"
  7856. (symbol-name (car x)))
  7857. (setq var (car x) val (nth 1 x))
  7858. (push (list var `(quote ,(eval var))) org-fb-vars)
  7859. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  7860. org-local-vars)
  7861. (org-set-local 'orgstruct-is-++ t))))
  7862. (defvar orgstruct-is-++ nil
  7863. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  7864. (make-variable-buffer-local 'orgstruct-is-++)
  7865. ;;;###autoload
  7866. (defun turn-on-orgstruct++ ()
  7867. "Unconditionally turn on `orgstruct++-mode'."
  7868. (orgstruct++-mode 1))
  7869. (defun orgstruct-error ()
  7870. "Error when there is no default binding for a structure key."
  7871. (interactive)
  7872. (funcall (if (fboundp 'user-error)
  7873. 'user-error
  7874. 'error)
  7875. "This key has no function outside structure elements"))
  7876. (defun orgstruct-setup ()
  7877. "Setup orgstruct keymap."
  7878. (dolist (cell '((org-demote . t)
  7879. (org-metaleft . t)
  7880. (org-metaright . t)
  7881. (org-promote . t)
  7882. (org-shiftmetaleft . t)
  7883. (org-shiftmetaright . t)
  7884. org-backward-element
  7885. org-backward-heading-same-level
  7886. org-ctrl-c-ret
  7887. org-ctrl-c-minus
  7888. org-ctrl-c-star
  7889. org-cycle
  7890. org-forward-heading-same-level
  7891. org-insert-heading
  7892. org-insert-heading-respect-content
  7893. org-kill-note-or-show-branches
  7894. org-mark-subtree
  7895. org-meta-return
  7896. org-metadown
  7897. org-metaup
  7898. org-narrow-to-subtree
  7899. org-promote-subtree
  7900. org-reveal
  7901. org-shiftdown
  7902. org-shiftleft
  7903. org-shiftmetadown
  7904. org-shiftmetaup
  7905. org-shiftright
  7906. org-shifttab
  7907. org-shifttab
  7908. org-shiftup
  7909. org-show-subtree
  7910. org-sort
  7911. org-up-element
  7912. outline-demote
  7913. outline-next-visible-heading
  7914. outline-previous-visible-heading
  7915. outline-promote
  7916. outline-up-heading
  7917. show-children))
  7918. (let ((f (or (car-safe cell) cell))
  7919. (disable-when-heading-prefix (cdr-safe cell)))
  7920. (when (fboundp f)
  7921. (dolist (binding (nconc (where-is-internal f org-mode-map)
  7922. (where-is-internal f outline-mode-map)))
  7923. ;; TODO use local-function-key-map
  7924. (dolist (rep '(("<tab>" . "TAB")
  7925. ("<return>" . "RET")
  7926. ("<escape>" . "ESC")
  7927. ("<delete>" . "DEL")))
  7928. (setq binding (read-kbd-macro (replace-regexp-in-string
  7929. (regexp-quote (car rep))
  7930. (cdr rep)
  7931. (key-description binding)))))
  7932. (let ((key (lookup-key orgstruct-mode-map binding)))
  7933. (when (or (not key) (numberp key))
  7934. (condition-case nil
  7935. (org-defkey orgstruct-mode-map
  7936. binding
  7937. (orgstruct-make-binding f binding disable-when-heading-prefix))
  7938. (error nil))))))))
  7939. (run-hooks 'orgstruct-setup-hook))
  7940. (defun orgstruct-make-binding (fun key disable-when-heading-prefix)
  7941. "Create a function for binding in the structure minor mode.
  7942. FUN is the command to call inside a table. KEY is the key that
  7943. should be checked in for a command to execute outside of tables.
  7944. Non-nil DISABLE-WHEN-HEADING-PREFIX means to disable the command
  7945. if `orgstruct-heading-prefix-regexp' is non-nil."
  7946. (let ((name (concat "orgstruct-hijacker-" (symbol-name fun))))
  7947. (let ((nname name)
  7948. (i 0))
  7949. (while (fboundp (intern nname))
  7950. (setq nname (format "%s-%d" name (setq i (1+ i)))))
  7951. (setq name (intern nname)))
  7952. (eval
  7953. (let ((bindings '((org-heading-regexp
  7954. (concat "^"
  7955. orgstruct-heading-prefix-regexp
  7956. "\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ ]*$"))
  7957. (org-outline-regexp
  7958. (concat orgstruct-heading-prefix-regexp "\\*+ "))
  7959. (org-outline-regexp-bol
  7960. (concat "^" org-outline-regexp))
  7961. (outline-regexp org-outline-regexp)
  7962. (outline-heading-end-regexp "\n")
  7963. (outline-level 'org-outline-level)
  7964. (outline-heading-alist))))
  7965. `(defun ,name (arg)
  7966. ,(concat "In Structure, run `" (symbol-name fun) "'.\n"
  7967. "Outside of structure, run the binding of `"
  7968. (key-description key) "'."
  7969. (when disable-when-heading-prefix
  7970. (concat
  7971. "\nIf `orgstruct-heading-prefix-regexp' is non-nil, this command will always fall\n"
  7972. "back to the default binding due to limitations of Org's implementation of\n"
  7973. "`" (symbol-name fun) "'.")))
  7974. (interactive "p")
  7975. (let* ((disable
  7976. ,(when disable-when-heading-prefix
  7977. '(and orgstruct-heading-prefix-regexp
  7978. (not (string= orgstruct-heading-prefix-regexp "")))))
  7979. (fallback
  7980. (or disable
  7981. (not
  7982. (let* ,bindings
  7983. (org-context-p 'headline 'item
  7984. ,(when (memq fun '(org-insert-heading))
  7985. '(when orgstruct-is-++
  7986. 'item-body))))))))
  7987. (if fallback
  7988. (let* ((orgstruct-mode)
  7989. (binding (key-binding ,key)))
  7990. (if (keymapp binding)
  7991. (set-temporary-overlay-map binding)
  7992. (let ((func (or binding
  7993. (unless disable
  7994. 'orgstruct-error))))
  7995. (when func
  7996. (call-interactively func)))))
  7997. (org-run-like-in-org-mode
  7998. (lambda ()
  7999. (interactive)
  8000. (let* ,bindings
  8001. (call-interactively ',fun)))))))))
  8002. name))
  8003. (defun org-contextualize-keys (alist contexts)
  8004. "Return valid elements in ALIST depending on CONTEXTS.
  8005. `org-agenda-custom-commands' or `org-capture-templates' are the
  8006. values used for ALIST, and `org-agenda-custom-commands-contexts'
  8007. or `org-capture-templates-contexts' are the associated contexts
  8008. definitions."
  8009. (let ((contexts
  8010. ;; normalize contexts
  8011. (mapcar
  8012. (lambda(c) (cond ((listp (cadr c))
  8013. (list (car c) (car c) (cadr c)))
  8014. ((string= "" (cadr c))
  8015. (list (car c) (car c) (caddr c)))
  8016. (t c))) contexts))
  8017. (a alist) c r s)
  8018. ;; loop over all commands or templates
  8019. (while (setq c (pop a))
  8020. (let (vrules repl)
  8021. (cond
  8022. ((not (assoc (car c) contexts))
  8023. (push c r))
  8024. ((and (assoc (car c) contexts)
  8025. (setq vrules (org-contextualize-validate-key
  8026. (car c) contexts)))
  8027. (mapc (lambda (vr)
  8028. (when (not (equal (car vr) (cadr vr)))
  8029. (setq repl vr))) vrules)
  8030. (if (not repl) (push c r)
  8031. (push (cadr repl) s)
  8032. (push
  8033. (cons (car c)
  8034. (cdr (or (assoc (cadr repl) alist)
  8035. (error "Undefined key `%s' as contextual replacement for `%s'"
  8036. (cadr repl) (car c)))))
  8037. r))))))
  8038. ;; Return limited ALIST, possibly with keys modified, and deduplicated
  8039. (delq
  8040. nil
  8041. (delete-dups
  8042. (mapcar (lambda (x)
  8043. (let ((tpl (car x)))
  8044. (when (not (delq
  8045. nil
  8046. (mapcar (lambda(y)
  8047. (equal y tpl)) s))) x)))
  8048. (reverse r))))))
  8049. (defun org-contextualize-validate-key (key contexts)
  8050. "Check CONTEXTS for agenda or capture KEY."
  8051. (let (r rr res)
  8052. (while (setq r (pop contexts))
  8053. (mapc
  8054. (lambda (rr)
  8055. (when
  8056. (and (equal key (car r))
  8057. (if (functionp rr) (funcall rr)
  8058. (or (and (eq (car rr) 'in-file)
  8059. (buffer-file-name)
  8060. (string-match (cdr rr) (buffer-file-name)))
  8061. (and (eq (car rr) 'in-mode)
  8062. (string-match (cdr rr) (symbol-name major-mode)))
  8063. (and (eq (car rr) 'in-buffer)
  8064. (string-match (cdr rr) (buffer-name)))
  8065. (when (and (eq (car rr) 'not-in-file)
  8066. (buffer-file-name))
  8067. (not (string-match (cdr rr) (buffer-file-name))))
  8068. (when (eq (car rr) 'not-in-mode)
  8069. (not (string-match (cdr rr) (symbol-name major-mode))))
  8070. (when (eq (car rr) 'not-in-buffer)
  8071. (not (string-match (cdr rr) (buffer-name)))))))
  8072. (push r res)))
  8073. (car (last r))))
  8074. (delete-dups (delq nil res))))
  8075. (defun org-context-p (&rest contexts)
  8076. "Check if local context is any of CONTEXTS.
  8077. Possible values in the list of contexts are `table', `headline', and `item'."
  8078. (let ((pos (point)))
  8079. (goto-char (point-at-bol))
  8080. (prog1 (or (and (memq 'table contexts)
  8081. (looking-at "[ \t]*|"))
  8082. (and (memq 'headline contexts)
  8083. (looking-at org-outline-regexp))
  8084. (and (memq 'item contexts)
  8085. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  8086. (and (memq 'item-body contexts)
  8087. (org-in-item-p)))
  8088. (goto-char pos))))
  8089. (defun org-get-local-variables ()
  8090. "Return a list of all local variables in an Org mode buffer."
  8091. (let (varlist)
  8092. (with-current-buffer (get-buffer-create "*Org tmp*")
  8093. (erase-buffer)
  8094. (org-mode)
  8095. (setq varlist (buffer-local-variables)))
  8096. (kill-buffer "*Org tmp*")
  8097. (delq nil
  8098. (mapcar
  8099. (lambda (x)
  8100. (setq x
  8101. (if (symbolp x)
  8102. (list x)
  8103. (list (car x) (cdr x))))
  8104. (if (and (not (get (car x) 'org-state))
  8105. (string-match
  8106. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|indent-\\)"
  8107. (symbol-name (car x))))
  8108. x nil))
  8109. varlist))))
  8110. (defun org-clone-local-variables (from-buffer &optional regexp)
  8111. "Clone local variables from FROM-BUFFER.
  8112. Optional argument REGEXP selects variables to clone."
  8113. (mapc
  8114. (lambda (pair)
  8115. (and (symbolp (car pair))
  8116. (or (null regexp)
  8117. (string-match regexp (symbol-name (car pair))))
  8118. (set (make-local-variable (car pair))
  8119. (cdr pair))))
  8120. (buffer-local-variables from-buffer)))
  8121. ;;;###autoload
  8122. (defun org-run-like-in-org-mode (cmd)
  8123. "Run a command, pretending that the current buffer is in Org-mode.
  8124. This will temporarily bind local variables that are typically bound in
  8125. Org-mode to the values they have in Org-mode, and then interactively
  8126. call CMD."
  8127. (org-load-modules-maybe)
  8128. (unless org-local-vars
  8129. (setq org-local-vars (org-get-local-variables)))
  8130. (let (binds)
  8131. (dolist (var org-local-vars)
  8132. (when (or (not (boundp (car var)))
  8133. (eq (symbol-value (car var))
  8134. (default-value (car var))))
  8135. (push (list (car var) `(quote ,(cadr var))) binds)))
  8136. (eval `(let ,binds
  8137. (call-interactively (quote ,cmd))))))
  8138. ;;;; Archiving
  8139. (defun org-get-category (&optional pos force-refresh)
  8140. "Get the category applying to position POS."
  8141. (save-match-data
  8142. (if force-refresh (org-refresh-category-properties))
  8143. (let ((pos (or pos (point))))
  8144. (or (get-text-property pos 'org-category)
  8145. (progn (org-refresh-category-properties)
  8146. (get-text-property pos 'org-category))))))
  8147. (defun org-refresh-category-properties ()
  8148. "Refresh category text properties in the buffer."
  8149. (let ((case-fold-search t)
  8150. (inhibit-read-only t)
  8151. (def-cat (cond
  8152. ((null org-category)
  8153. (if buffer-file-name
  8154. (file-name-sans-extension
  8155. (file-name-nondirectory buffer-file-name))
  8156. "???"))
  8157. ((symbolp org-category) (symbol-name org-category))
  8158. (t org-category)))
  8159. beg end cat pos optionp)
  8160. (org-with-silent-modifications
  8161. (save-excursion
  8162. (save-restriction
  8163. (widen)
  8164. (goto-char (point-min))
  8165. (put-text-property (point) (point-max) 'org-category def-cat)
  8166. (while (re-search-forward
  8167. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  8168. (setq pos (match-end 0)
  8169. optionp (equal (char-after (match-beginning 0)) ?#)
  8170. cat (org-trim (match-string 2)))
  8171. (if optionp
  8172. (setq beg (point-at-bol) end (point-max))
  8173. (org-back-to-heading t)
  8174. (setq beg (point) end (org-end-of-subtree t t)))
  8175. (put-text-property beg end 'org-category cat)
  8176. (put-text-property beg end 'org-category-position beg)
  8177. (goto-char pos)))))))
  8178. (defun org-refresh-properties (dprop tprop)
  8179. "Refresh buffer text properties.
  8180. DPROP is the drawer property and TPROP is the corresponding text
  8181. property to set."
  8182. (let ((case-fold-search t)
  8183. (inhibit-read-only t) p)
  8184. (org-with-silent-modifications
  8185. (save-excursion
  8186. (save-restriction
  8187. (widen)
  8188. (goto-char (point-min))
  8189. (while (re-search-forward (concat "^[ \t]*:" dprop ": +\\(.*\\)[ \t]*$") nil t)
  8190. (setq p (org-match-string-no-properties 1))
  8191. (save-excursion
  8192. (org-back-to-heading t)
  8193. (put-text-property
  8194. (point-at-bol) (point-at-eol) tprop p))))))))
  8195. ;;;; Link Stuff
  8196. ;;; Link abbreviations
  8197. (defun org-link-expand-abbrev (link)
  8198. "Apply replacements as defined in `org-link-abbrev-alist'."
  8199. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  8200. (let* ((key (match-string 1 link))
  8201. (as (or (assoc key org-link-abbrev-alist-local)
  8202. (assoc key org-link-abbrev-alist)))
  8203. (tag (and (match-end 2) (match-string 3 link)))
  8204. rpl)
  8205. (if (not as)
  8206. link
  8207. (setq rpl (cdr as))
  8208. (cond
  8209. ((symbolp rpl) (funcall rpl tag))
  8210. ((string-match "%(\\([^)]+\\))" rpl)
  8211. (replace-match (funcall (intern-soft (match-string 1 rpl)) tag) t t rpl))
  8212. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  8213. ((string-match "%h" rpl)
  8214. (replace-match (url-hexify-string (or tag "")) t t rpl))
  8215. (t (concat rpl tag)))))
  8216. link))
  8217. ;;; Storing and inserting links
  8218. (defvar org-insert-link-history nil
  8219. "Minibuffer history for links inserted with `org-insert-link'.")
  8220. (defvar org-stored-links nil
  8221. "Contains the links stored with `org-store-link'.")
  8222. (defvar org-store-link-plist nil
  8223. "Plist with info about the most recently link created with `org-store-link'.")
  8224. (defvar org-link-protocols nil
  8225. "Link protocols added to Org-mode using `org-add-link-type'.")
  8226. (defvar org-store-link-functions nil
  8227. "List of functions that are called to create and store a link.
  8228. Each function will be called in turn until one returns a non-nil
  8229. value. Each function should check if it is responsible for creating
  8230. this link (for example by looking at the major mode).
  8231. If not, it must exit and return nil.
  8232. If yes, it should return a non-nil value after a calling
  8233. `org-store-link-props' with a list of properties and values.
  8234. Special properties are:
  8235. :type The link prefix, like \"http\". This must be given.
  8236. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  8237. This is obligatory as well.
  8238. :description Optional default description for the second pair
  8239. of brackets in an Org-mode link. The user can still change
  8240. this when inserting this link into an Org-mode buffer.
  8241. In addition to these, any additional properties can be specified
  8242. and then used in capture templates.")
  8243. (defun org-add-link-type (type &optional follow export)
  8244. "Add TYPE to the list of `org-link-types'.
  8245. Re-compute all regular expressions depending on `org-link-types'
  8246. FOLLOW and EXPORT are two functions.
  8247. FOLLOW should take the link path as the single argument and do whatever
  8248. is necessary to follow the link, for example find a file or display
  8249. a mail message.
  8250. EXPORT should format the link path for export to one of the export formats.
  8251. It should be a function accepting three arguments:
  8252. path the path of the link, the text after the prefix (like \"http:\")
  8253. desc the description of the link, if any, or a description added by
  8254. org-export-normalize-links if there is none
  8255. format the export format, a symbol like `html' or `latex' or `ascii'..
  8256. The function may use the FORMAT information to return different values
  8257. depending on the format. The return value will be put literally into
  8258. the exported file. If the return value is nil, this means Org should
  8259. do what it normally does with links which do not have EXPORT defined.
  8260. Org-mode has a built-in default for exporting links. If you are happy with
  8261. this default, there is no need to define an export function for the link
  8262. type. For a simple example of an export function, see `org-bbdb.el'."
  8263. (add-to-list 'org-link-types type t)
  8264. (org-make-link-regexps)
  8265. (if (assoc type org-link-protocols)
  8266. (setcdr (assoc type org-link-protocols) (list follow export))
  8267. (push (list type follow export) org-link-protocols)))
  8268. (defvar org-agenda-buffer-name) ; Defined in org-agenda.el
  8269. (defvar org-id-link-to-org-use-id) ; Defined in org-id.el
  8270. ;;;###autoload
  8271. (defun org-store-link (arg)
  8272. "\\<org-mode-map>Store an org-link to the current location.
  8273. This link is added to `org-stored-links' and can later be inserted
  8274. into an org-buffer with \\[org-insert-link].
  8275. For some link types, a prefix arg is interpreted.
  8276. For links to Usenet articles, arg negates `org-gnus-prefer-web-links'.
  8277. For file links, arg negates `org-context-in-file-links'.
  8278. A double prefix arg force skipping storing functions that are not
  8279. part of Org's core."
  8280. (interactive "P")
  8281. (org-load-modules-maybe)
  8282. (setq org-store-link-plist nil) ; reset
  8283. (org-with-limited-levels
  8284. (let (link cpltxt desc description search txt custom-id agenda-link sfuns sfunsn)
  8285. (cond
  8286. ((and (not (equal arg '(16)))
  8287. (setq sfuns
  8288. (delq
  8289. nil (mapcar (lambda (f) (let (fs) (if (funcall f) (push f fs))))
  8290. org-store-link-functions))
  8291. sfunsn (mapcar (lambda (fu) (symbol-name (car fu))) sfuns))
  8292. (or (and (cdr sfuns)
  8293. (funcall (intern
  8294. (completing-read "Which function for creating the link? "
  8295. sfunsn t (car sfunsn)))))
  8296. (funcall (caar sfuns)))
  8297. (setq link (plist-get org-store-link-plist :link)
  8298. desc (or (plist-get org-store-link-plist :description) link))))
  8299. ((org-src-edit-buffer-p)
  8300. (let (label gc)
  8301. (while (or (not label)
  8302. (save-excursion
  8303. (save-restriction
  8304. (widen)
  8305. (goto-char (point-min))
  8306. (re-search-forward
  8307. (regexp-quote (format org-coderef-label-format label))
  8308. nil t))))
  8309. (when label (message "Label exists already") (sit-for 2))
  8310. (setq label (read-string "Code line label: " label)))
  8311. (end-of-line 1)
  8312. (setq link (format org-coderef-label-format label))
  8313. (setq gc (- 79 (length link)))
  8314. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  8315. (insert link)
  8316. (setq link (concat "(" label ")") desc nil)))
  8317. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  8318. ;; We are in the agenda, link to referenced location
  8319. (let ((m (or (get-text-property (point) 'org-hd-marker)
  8320. (get-text-property (point) 'org-marker))))
  8321. (when m
  8322. (org-with-point-at m
  8323. (setq agenda-link
  8324. (if (org-called-interactively-p 'any)
  8325. (call-interactively 'org-store-link)
  8326. (org-store-link nil)))))))
  8327. ((eq major-mode 'calendar-mode)
  8328. (let ((cd (calendar-cursor-to-date)))
  8329. (setq link
  8330. (format-time-string
  8331. (car org-time-stamp-formats)
  8332. (apply 'encode-time
  8333. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  8334. nil nil nil))))
  8335. (org-store-link-props :type "calendar" :date cd)))
  8336. ((eq major-mode 'help-mode)
  8337. (setq link (concat "help:" (save-excursion
  8338. (goto-char (point-min))
  8339. (looking-at "^[^ ]+")
  8340. (match-string 0))))
  8341. (org-store-link-props :type "help"))
  8342. ((eq major-mode 'w3-mode)
  8343. (setq cpltxt (if (and (buffer-name)
  8344. (not (string-match "Untitled" (buffer-name))))
  8345. (buffer-name)
  8346. (url-view-url t))
  8347. link (url-view-url t))
  8348. (org-store-link-props :type "w3" :url (url-view-url t)))
  8349. ((setq search (run-hook-with-args-until-success
  8350. 'org-create-file-search-functions))
  8351. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  8352. "::" search))
  8353. (setq cpltxt (or description link)))
  8354. ((eq major-mode 'image-mode)
  8355. (setq cpltxt (concat "file:"
  8356. (abbreviate-file-name buffer-file-name))
  8357. link cpltxt)
  8358. (org-store-link-props :type "image" :file buffer-file-name))
  8359. ((eq major-mode 'dired-mode)
  8360. ;; link to the file in the current line
  8361. (let ((file (dired-get-filename nil t)))
  8362. (setq file (if file
  8363. (abbreviate-file-name
  8364. (expand-file-name (dired-get-filename nil t)))
  8365. ;; otherwise, no file so use current directory.
  8366. default-directory))
  8367. (setq cpltxt (concat "file:" file)
  8368. link cpltxt)))
  8369. ((and (buffer-file-name (buffer-base-buffer)) (derived-mode-p 'org-mode))
  8370. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  8371. (cond
  8372. ((org-in-regexp "<<\\(.*?\\)>>")
  8373. (setq cpltxt
  8374. (concat "file:"
  8375. (abbreviate-file-name
  8376. (buffer-file-name (buffer-base-buffer)))
  8377. "::" (match-string 1))
  8378. link cpltxt))
  8379. ((and (featurep 'org-id)
  8380. (or (eq org-id-link-to-org-use-id t)
  8381. (and (org-called-interactively-p 'any)
  8382. (or (eq org-id-link-to-org-use-id 'create-if-interactive)
  8383. (and (eq org-id-link-to-org-use-id
  8384. 'create-if-interactive-and-no-custom-id)
  8385. (not custom-id))))
  8386. (and org-id-link-to-org-use-id (org-entry-get nil "ID"))))
  8387. ;; We can make a link using the ID.
  8388. (setq link (condition-case nil
  8389. (prog1 (org-id-store-link)
  8390. (setq desc (plist-get org-store-link-plist :description)))
  8391. (error
  8392. ;; probably before first headline, link to file only
  8393. (concat "file:"
  8394. (abbreviate-file-name
  8395. (buffer-file-name (buffer-base-buffer))))))))
  8396. (t
  8397. ;; Just link to current headline
  8398. (setq cpltxt (concat "file:"
  8399. (abbreviate-file-name
  8400. (buffer-file-name (buffer-base-buffer)))))
  8401. ;; Add a context search string
  8402. (when (org-xor org-context-in-file-links arg)
  8403. (let* ((ee (org-element-at-point))
  8404. (et (org-element-type ee))
  8405. (ev (plist-get (cadr ee) :value)))
  8406. (setq txt (cond
  8407. ((org-at-heading-p) nil)
  8408. ((eq et 'keyword) ev)
  8409. ((org-region-active-p)
  8410. (buffer-substring (region-beginning) (region-end)))))
  8411. (when (or (null txt) (string-match "\\S-" txt))
  8412. (setq cpltxt
  8413. (concat cpltxt "::"
  8414. (condition-case nil
  8415. (org-make-org-heading-search-string txt)
  8416. (error "")))
  8417. desc (or (and (eq et 'keyword) ev)
  8418. (nth 4 (ignore-errors (org-heading-components)))
  8419. "NONE")))))
  8420. (if (string-match "::\\'" cpltxt)
  8421. (setq cpltxt (substring cpltxt 0 -2)))
  8422. (setq link cpltxt))))
  8423. ((buffer-file-name (buffer-base-buffer))
  8424. ;; Just link to this file here.
  8425. (setq cpltxt (concat "file:"
  8426. (abbreviate-file-name
  8427. (buffer-file-name (buffer-base-buffer)))))
  8428. ;; Add a context string
  8429. (when (org-xor org-context-in-file-links arg)
  8430. (setq txt (if (org-region-active-p)
  8431. (buffer-substring (region-beginning) (region-end))
  8432. (buffer-substring (point-at-bol) (point-at-eol))))
  8433. ;; Only use search option if there is some text.
  8434. (when (string-match "\\S-" txt)
  8435. (setq cpltxt
  8436. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  8437. desc "NONE")))
  8438. (setq link cpltxt))
  8439. ((org-called-interactively-p 'interactive)
  8440. (user-error "No method for storing a link from this buffer"))
  8441. (t (setq link nil)))
  8442. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  8443. (setq link (or link cpltxt)
  8444. desc (or desc cpltxt))
  8445. (cond ((equal desc "NONE") (setq desc nil))
  8446. ((string-match org-bracket-link-regexp desc)
  8447. (setq desc (replace-regexp-in-string
  8448. org-bracket-link-regexp
  8449. (concat "\\3" (if (equal (length (match-string 0 desc))
  8450. (length desc)) "*" "")) desc))))
  8451. (if (and (or (org-called-interactively-p 'any) executing-kbd-macro) link)
  8452. (progn
  8453. (setq org-stored-links
  8454. (cons (list link desc) org-stored-links))
  8455. (message "Stored: %s" (or desc link))
  8456. (when custom-id
  8457. (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
  8458. "::#" custom-id))
  8459. (setq org-stored-links
  8460. (cons (list link desc) org-stored-links))))
  8461. (or agenda-link (and link (org-make-link-string link desc)))))))
  8462. (defun org-store-link-props (&rest plist)
  8463. "Store link properties, extract names and addresses."
  8464. (let (x adr)
  8465. (when (setq x (plist-get plist :from))
  8466. (setq adr (mail-extract-address-components x))
  8467. (setq plist (plist-put plist :fromname (car adr)))
  8468. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  8469. (when (setq x (plist-get plist :to))
  8470. (setq adr (mail-extract-address-components x))
  8471. (setq plist (plist-put plist :toname (car adr)))
  8472. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  8473. (let ((from (plist-get plist :from))
  8474. (to (plist-get plist :to)))
  8475. (when (and from to org-from-is-user-regexp)
  8476. (setq plist
  8477. (plist-put plist :fromto
  8478. (if (string-match org-from-is-user-regexp from)
  8479. (concat "to %t")
  8480. (concat "from %f"))))))
  8481. (setq org-store-link-plist plist))
  8482. (defun org-add-link-props (&rest plist)
  8483. "Add these properties to the link property list."
  8484. (let (key value)
  8485. (while plist
  8486. (setq key (pop plist) value (pop plist))
  8487. (setq org-store-link-plist
  8488. (plist-put org-store-link-plist key value)))))
  8489. (defun org-email-link-description (&optional fmt)
  8490. "Return the description part of an email link.
  8491. This takes information from `org-store-link-plist' and formats it
  8492. according to FMT (default from `org-email-link-description-format')."
  8493. (setq fmt (or fmt org-email-link-description-format))
  8494. (let* ((p org-store-link-plist)
  8495. (to (plist-get p :toaddress))
  8496. (from (plist-get p :fromaddress))
  8497. (table
  8498. (list
  8499. (cons "%c" (plist-get p :fromto))
  8500. (cons "%F" (plist-get p :from))
  8501. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  8502. (cons "%T" (plist-get p :to))
  8503. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  8504. (cons "%s" (plist-get p :subject))
  8505. (cons "%d" (plist-get p :date))
  8506. (cons "%m" (plist-get p :message-id)))))
  8507. (when (string-match "%c" fmt)
  8508. ;; Check if the user wrote this message
  8509. (if (and org-from-is-user-regexp from to
  8510. (save-match-data (string-match org-from-is-user-regexp from)))
  8511. (setq fmt (replace-match "to %t" t t fmt))
  8512. (setq fmt (replace-match "from %f" t t fmt))))
  8513. (org-replace-escapes fmt table)))
  8514. (defun org-make-org-heading-search-string (&optional string)
  8515. "Make search string for the current headline or STRING."
  8516. (let ((s (or string
  8517. (and (derived-mode-p 'org-mode)
  8518. (save-excursion
  8519. (org-back-to-heading t)
  8520. (org-element-property :raw-value (org-element-at-point))))))
  8521. (lines org-context-in-file-links))
  8522. (or string (setq s (concat "*" s))) ; Add * for headlines
  8523. (setq s (replace-regexp-in-string "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" "" s))
  8524. (when (and string (integerp lines) (> lines 0))
  8525. (let ((slines (org-split-string s "\n")))
  8526. (when (< lines (length slines))
  8527. (setq s (mapconcat
  8528. 'identity
  8529. (reverse (nthcdr (- (length slines) lines)
  8530. (reverse slines))) "\n")))))
  8531. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  8532. (defun org-make-link-string (link &optional description)
  8533. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  8534. (unless (string-match "\\S-" link)
  8535. (error "Empty link"))
  8536. (when (and description
  8537. (stringp description)
  8538. (not (string-match "\\S-" description)))
  8539. (setq description nil))
  8540. (when (stringp description)
  8541. ;; Remove brackets from the description, they are fatal.
  8542. (while (string-match "\\[" description)
  8543. (setq description (replace-match "{" t t description)))
  8544. (while (string-match "\\]" description)
  8545. (setq description (replace-match "}" t t description))))
  8546. (when (equal link description)
  8547. ;; No description needed, it is identical
  8548. (setq description nil))
  8549. (when (and (not description)
  8550. (not (string-match (org-image-file-name-regexp) link))
  8551. (not (equal link (org-link-escape link))))
  8552. (setq description (org-extract-attributes link)))
  8553. (setq link
  8554. (cond ((string-match (org-image-file-name-regexp) link) link)
  8555. ((string-match org-link-types-re link)
  8556. (concat (match-string 1 link)
  8557. (org-link-escape (substring link (match-end 1)))))
  8558. (t (org-link-escape link))))
  8559. (concat "[[" link "]"
  8560. (if description (concat "[" description "]") "")
  8561. "]"))
  8562. (defconst org-link-escape-chars
  8563. '(?\ ?\[ ?\] ?\; ?\= ?\+)
  8564. "List of characters that should be escaped in link.
  8565. This is the list that is used for internal purposes.")
  8566. (defconst org-link-escape-chars-browser
  8567. '(?\ )
  8568. "List of escapes for characters that are problematic in links.
  8569. This is the list that is used before handing over to the browser.")
  8570. (defun org-link-escape (text &optional table merge)
  8571. "Return percent escaped representation of TEXT.
  8572. TEXT is a string with the text to escape.
  8573. Optional argument TABLE is a list with characters that should be
  8574. escaped. When nil, `org-link-escape-chars' is used.
  8575. If optional argument MERGE is set, merge TABLE into
  8576. `org-link-escape-chars'."
  8577. (cond
  8578. ((and table merge)
  8579. (mapc (lambda (defchr)
  8580. (unless (member defchr table)
  8581. (setq table (cons defchr table)))) org-link-escape-chars))
  8582. ((null table)
  8583. (setq table org-link-escape-chars)))
  8584. (mapconcat
  8585. (lambda (char)
  8586. (if (or (member char table)
  8587. (and (or (< char 32) (= char 37) (> char 126))
  8588. org-url-hexify-p))
  8589. (mapconcat (lambda (sequence-element)
  8590. (format "%%%.2X" sequence-element))
  8591. (or (encode-coding-char char 'utf-8)
  8592. (error "Unable to percent escape character: %s"
  8593. (char-to-string char))) "")
  8594. (char-to-string char))) text ""))
  8595. (defun org-link-unescape (str)
  8596. "Unhex hexified Unicode strings as returned from the JavaScript function
  8597. encodeURIComponent. E.g. `%C3%B6' is the german o-Umlaut."
  8598. (unless (and (null str) (string= "" str))
  8599. (let ((pos 0) (case-fold-search t) unhexed)
  8600. (while (setq pos (string-match "\\(%[0-9a-f][0-9a-f]\\)+" str pos))
  8601. (setq unhexed (org-link-unescape-compound (match-string 0 str)))
  8602. (setq str (replace-match unhexed t t str))
  8603. (setq pos (+ pos (length unhexed))))))
  8604. str)
  8605. (defun org-link-unescape-compound (hex)
  8606. "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German o-Umlaut.
  8607. Note: this function also decodes single byte encodings like
  8608. `%E1' (a-acute) if not followed by another `%[A-F0-9]{2}' group."
  8609. (save-match-data
  8610. (let* ((bytes (cdr (split-string hex "%")))
  8611. (ret "")
  8612. (eat 0)
  8613. (sum 0))
  8614. (while bytes
  8615. (let* ((val (string-to-number (pop bytes) 16))
  8616. (shift-xor
  8617. (if (= 0 eat)
  8618. (cond
  8619. ((>= val 252) (cons 6 252))
  8620. ((>= val 248) (cons 5 248))
  8621. ((>= val 240) (cons 4 240))
  8622. ((>= val 224) (cons 3 224))
  8623. ((>= val 192) (cons 2 192))
  8624. (t (cons 0 0)))
  8625. (cons 6 128))))
  8626. (if (>= val 192) (setq eat (car shift-xor)))
  8627. (setq val (logxor val (cdr shift-xor)))
  8628. (setq sum (+ (lsh sum (car shift-xor)) val))
  8629. (if (> eat 0) (setq eat (- eat 1)))
  8630. (cond
  8631. ((= 0 eat) ;multi byte
  8632. (setq ret (concat ret (org-char-to-string sum)))
  8633. (setq sum 0))
  8634. ((not bytes) ; single byte(s)
  8635. (setq ret (org-link-unescape-single-byte-sequence hex))))
  8636. )) ;; end (while bytes
  8637. ret )))
  8638. (defun org-link-unescape-single-byte-sequence (hex)
  8639. "Unhexify hex-encoded single byte character sequences."
  8640. (mapconcat (lambda (byte)
  8641. (char-to-string (string-to-number byte 16)))
  8642. (cdr (split-string hex "%")) ""))
  8643. (defun org-xor (a b)
  8644. "Exclusive or."
  8645. (if a (not b) b))
  8646. (defun org-fixup-message-id-for-http (s)
  8647. "Replace special characters in a message id, so it can be used in an http query."
  8648. (when (string-match "%" s)
  8649. (setq s (mapconcat (lambda (c)
  8650. (if (eq c ?%)
  8651. "%25"
  8652. (char-to-string c)))
  8653. s "")))
  8654. (while (string-match "<" s)
  8655. (setq s (replace-match "%3C" t t s)))
  8656. (while (string-match ">" s)
  8657. (setq s (replace-match "%3E" t t s)))
  8658. (while (string-match "@" s)
  8659. (setq s (replace-match "%40" t t s)))
  8660. s)
  8661. (defun org-link-prettify (link)
  8662. "Return a human-readable representation of LINK.
  8663. The car of LINK must be a raw link the cdr of LINK must be either
  8664. a link description or nil."
  8665. (let ((desc (or (cadr link) "<no description>")))
  8666. (concat (format "%-45s" (substring desc 0 (min (length desc) 40)))
  8667. "<" (car link) ">")))
  8668. ;;;###autoload
  8669. (defun org-insert-link-global ()
  8670. "Insert a link like Org-mode does.
  8671. This command can be called in any mode to insert a link in Org-mode syntax."
  8672. (interactive)
  8673. (org-load-modules-maybe)
  8674. (org-run-like-in-org-mode 'org-insert-link))
  8675. (defun org-insert-all-links (&optional keep)
  8676. "Insert all links in `org-stored-links'."
  8677. (interactive "P")
  8678. (let ((links (copy-sequence org-stored-links)) l)
  8679. (while (setq l (if keep (pop links) (pop org-stored-links)))
  8680. (insert "- ")
  8681. (org-insert-link nil (car l) (cadr l))
  8682. (insert "\n"))))
  8683. (defun org-link-fontify-links-to-this-file ()
  8684. "Fontify links to the current file in `org-stored-links'."
  8685. (let ((f (buffer-file-name)) a b)
  8686. (setq a (mapcar (lambda(l)
  8687. (let ((ll (car l)))
  8688. (when (and (string-match "^file:\\(.+\\)::" ll)
  8689. (equal f (expand-file-name (match-string 1 ll))))
  8690. ll)))
  8691. org-stored-links))
  8692. (when (featurep 'org-id)
  8693. (setq b (mapcar (lambda(l)
  8694. (let ((ll (car l)))
  8695. (when (and (string-match "^id:\\(.+\\)$" ll)
  8696. (equal f (expand-file-name
  8697. (or (org-id-find-id-file
  8698. (match-string 1 ll)) ""))))
  8699. ll)))
  8700. org-stored-links)))
  8701. (mapcar (lambda(l)
  8702. (put-text-property 0 (length l) 'face 'font-lock-comment-face l))
  8703. (delq nil (append a b)))))
  8704. (defvar org-link-links-in-this-file nil)
  8705. (defun org-insert-link (&optional complete-file link-location default-description)
  8706. "Insert a link. At the prompt, enter the link.
  8707. Completion can be used to insert any of the link protocol prefixes like
  8708. http or ftp in use.
  8709. The history can be used to select a link previously stored with
  8710. `org-store-link'. When the empty string is entered (i.e. if you just
  8711. press RET at the prompt), the link defaults to the most recently
  8712. stored link. As SPC triggers completion in the minibuffer, you need to
  8713. use M-SPC or C-q SPC to force the insertion of a space character.
  8714. You will also be prompted for a description, and if one is given, it will
  8715. be displayed in the buffer instead of the link.
  8716. If there is already a link at point, this command will allow you to edit link
  8717. and description parts.
  8718. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  8719. be selected using completion. The path to the file will be relative to the
  8720. current directory if the file is in the current directory or a subdirectory.
  8721. Otherwise, the link will be the absolute path as completed in the minibuffer
  8722. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  8723. option `org-link-file-path-type'.
  8724. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  8725. the current directory or below.
  8726. With three \\[universal-argument] prefixes, negate the meaning of
  8727. `org-keep-stored-link-after-insertion'.
  8728. If `org-make-link-description-function' is non-nil, this function will be
  8729. called with the link target, and the result will be the default
  8730. link description.
  8731. If the LINK-LOCATION parameter is non-nil, this value will be
  8732. used as the link location instead of reading one interactively.
  8733. If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
  8734. be used as the default description."
  8735. (interactive "P")
  8736. (let* ((wcf (current-window-configuration))
  8737. (origbuf (current-buffer))
  8738. (region (if (org-region-active-p)
  8739. (buffer-substring (region-beginning) (region-end))))
  8740. (remove (and region (list (region-beginning) (region-end))))
  8741. (desc region)
  8742. tmphist ; byte-compile incorrectly complains about this
  8743. (link link-location)
  8744. (abbrevs org-link-abbrev-alist-local)
  8745. entry file all-prefixes auto-desc)
  8746. (cond
  8747. (link-location) ; specified by arg, just use it.
  8748. ((org-in-regexp org-bracket-link-regexp 1)
  8749. ;; We do have a link at point, and we are going to edit it.
  8750. (setq remove (list (match-beginning 0) (match-end 0)))
  8751. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  8752. (setq link (read-string "Link: "
  8753. (org-link-unescape
  8754. (org-match-string-no-properties 1)))))
  8755. ((or (org-in-regexp org-angle-link-re)
  8756. (org-in-regexp org-plain-link-re))
  8757. ;; Convert to bracket link
  8758. (setq remove (list (match-beginning 0) (match-end 0))
  8759. link (read-string "Link: "
  8760. (org-remove-angle-brackets (match-string 0)))))
  8761. ((member complete-file '((4) (16)))
  8762. ;; Completing read for file names.
  8763. (setq link (org-file-complete-link complete-file)))
  8764. (t
  8765. ;; Read link, with completion for stored links.
  8766. (org-link-fontify-links-to-this-file)
  8767. (org-switch-to-buffer-other-window "*Org Links*")
  8768. (with-current-buffer "*Org Links*"
  8769. (erase-buffer)
  8770. (insert "Insert a link.
  8771. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  8772. (when org-stored-links
  8773. (insert "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  8774. (insert (mapconcat 'org-link-prettify
  8775. (reverse org-stored-links) "\n")))
  8776. (goto-char (point-min)))
  8777. (let ((cw (selected-window)))
  8778. (select-window (get-buffer-window "*Org Links*" 'visible))
  8779. (with-current-buffer "*Org Links*" (setq truncate-lines t))
  8780. (unless (pos-visible-in-window-p (point-max))
  8781. (org-fit-window-to-buffer))
  8782. (and (window-live-p cw) (select-window cw)))
  8783. ;; Fake a link history, containing the stored links.
  8784. (setq tmphist (append (mapcar 'car org-stored-links)
  8785. org-insert-link-history))
  8786. (setq all-prefixes (append (mapcar 'car abbrevs)
  8787. (mapcar 'car org-link-abbrev-alist)
  8788. org-link-types))
  8789. (unwind-protect
  8790. (progn
  8791. (setq link
  8792. (org-completing-read
  8793. "Link: "
  8794. (append
  8795. (mapcar (lambda (x) (concat x ":"))
  8796. all-prefixes)
  8797. (mapcar 'car org-stored-links))
  8798. nil nil nil
  8799. 'tmphist
  8800. (caar org-stored-links)))
  8801. (if (not (string-match "\\S-" link))
  8802. (error "No link selected"))
  8803. (mapc (lambda(l)
  8804. (when (equal link (cadr l)) (setq link (car l) auto-desc t)))
  8805. org-stored-links)
  8806. (if (or (member link all-prefixes)
  8807. (and (equal ":" (substring link -1))
  8808. (member (substring link 0 -1) all-prefixes)
  8809. (setq link (substring link 0 -1))))
  8810. (setq link (with-current-buffer origbuf
  8811. (org-link-try-special-completion link)))))
  8812. (set-window-configuration wcf)
  8813. (kill-buffer "*Org Links*"))
  8814. (setq entry (assoc link org-stored-links))
  8815. (or entry (push link org-insert-link-history))
  8816. (setq desc (or desc (nth 1 entry)))))
  8817. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  8818. (not org-keep-stored-link-after-insertion))
  8819. (setq org-stored-links (delq (assoc link org-stored-links)
  8820. org-stored-links)))
  8821. (if (string-match org-plain-link-re link)
  8822. ;; URL-like link, normalize the use of angular brackets.
  8823. (setq link (org-remove-angle-brackets link)))
  8824. ;; Check if we are linking to the current file with a search
  8825. ;; option If yes, simplify the link by using only the search
  8826. ;; option.
  8827. (when (and buffer-file-name
  8828. (string-match "^file:\\(.+?\\)::\\(.+\\)" link))
  8829. (let* ((path (match-string 1 link))
  8830. (case-fold-search nil)
  8831. (search (match-string 2 link)))
  8832. (save-match-data
  8833. (if (equal (file-truename buffer-file-name) (file-truename path))
  8834. ;; We are linking to this same file, with a search option
  8835. (setq link search)))))
  8836. ;; Check if we can/should use a relative path. If yes, simplify the link
  8837. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  8838. (let* ((type (match-string 1 link))
  8839. (path (match-string 2 link))
  8840. (origpath path)
  8841. (case-fold-search nil))
  8842. (cond
  8843. ((or (eq org-link-file-path-type 'absolute)
  8844. (equal complete-file '(16)))
  8845. (setq path (abbreviate-file-name (expand-file-name path))))
  8846. ((eq org-link-file-path-type 'noabbrev)
  8847. (setq path (expand-file-name path)))
  8848. ((eq org-link-file-path-type 'relative)
  8849. (setq path (file-relative-name path)))
  8850. (t
  8851. (save-match-data
  8852. (if (string-match (concat "^" (regexp-quote
  8853. (expand-file-name
  8854. (file-name-as-directory
  8855. default-directory))))
  8856. (expand-file-name path))
  8857. ;; We are linking a file with relative path name.
  8858. (setq path (substring (expand-file-name path)
  8859. (match-end 0)))
  8860. (setq path (abbreviate-file-name (expand-file-name path)))))))
  8861. (setq link (concat type path))
  8862. (if (equal desc origpath)
  8863. (setq desc path))))
  8864. (if org-make-link-description-function
  8865. (setq desc
  8866. (or (condition-case nil
  8867. (funcall org-make-link-description-function link desc)
  8868. (error (progn (message "Can't get link description from `%s'"
  8869. (symbol-name org-make-link-description-function))
  8870. (sit-for 2) nil)))
  8871. (read-string "Description: " default-description)))
  8872. (if default-description (setq desc default-description)
  8873. (setq desc (or (and auto-desc desc)
  8874. (read-string "Description: " desc)))))
  8875. (unless (string-match "\\S-" desc) (setq desc nil))
  8876. (if remove (apply 'delete-region remove))
  8877. (insert (org-make-link-string link desc))))
  8878. (defun org-link-try-special-completion (type)
  8879. "If there is completion support for link type TYPE, offer it."
  8880. (let ((fun (intern (concat "org-" type "-complete-link"))))
  8881. (if (functionp fun)
  8882. (funcall fun)
  8883. (read-string "Link (no completion support): " (concat type ":")))))
  8884. (defun org-file-complete-link (&optional arg)
  8885. "Create a file link using completion."
  8886. (let (file link)
  8887. (setq file (org-iread-file-name "File: "))
  8888. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  8889. (pwd1 (file-name-as-directory (abbreviate-file-name
  8890. (expand-file-name ".")))))
  8891. (cond
  8892. ((equal arg '(16))
  8893. (setq link (concat
  8894. "file:"
  8895. (abbreviate-file-name (expand-file-name file)))))
  8896. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  8897. (setq link (concat "file:" (match-string 1 file))))
  8898. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  8899. (expand-file-name file))
  8900. (setq link (concat
  8901. "file:" (match-string 1 (expand-file-name file)))))
  8902. (t (setq link (concat "file:" file)))))
  8903. link))
  8904. (defun org-iread-file-name (&rest args)
  8905. "Read-file-name using `ido-mode' speedup if available.
  8906. ARGS are arguments that may be passed to `ido-read-file-name' or `read-file-name'.
  8907. See `read-file-name' for a description of parameters."
  8908. (org-without-partial-completion
  8909. (if (and org-completion-use-ido
  8910. (fboundp 'ido-read-file-name)
  8911. (boundp 'ido-mode) ido-mode
  8912. (listp (second args)))
  8913. (let ((ido-enter-matching-directory nil))
  8914. (apply 'ido-read-file-name args))
  8915. (apply 'read-file-name args))))
  8916. (defun org-completing-read (&rest args)
  8917. "Completing-read with SPACE being a normal character."
  8918. (let ((enable-recursive-minibuffers t)
  8919. (minibuffer-local-completion-map
  8920. (copy-keymap minibuffer-local-completion-map)))
  8921. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  8922. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  8923. (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
  8924. (apply 'org-icompleting-read args)))
  8925. (defun org-completing-read-no-i (&rest args)
  8926. (let (org-completion-use-ido org-completion-use-iswitchb)
  8927. (apply 'org-completing-read args)))
  8928. (defun org-iswitchb-completing-read (prompt choices &rest args)
  8929. "Use iswitch as a completing-read replacement to choose from choices.
  8930. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  8931. from."
  8932. (let* ((iswitchb-use-virtual-buffers nil)
  8933. (iswitchb-make-buflist-hook
  8934. (lambda ()
  8935. (setq iswitchb-temp-buflist choices))))
  8936. (iswitchb-read-buffer prompt)))
  8937. (defun org-icompleting-read (&rest args)
  8938. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  8939. (org-without-partial-completion
  8940. (if (and org-completion-use-ido
  8941. (fboundp 'ido-completing-read)
  8942. (boundp 'ido-mode) ido-mode
  8943. (listp (second args)))
  8944. (let ((ido-enter-matching-directory nil))
  8945. (apply 'ido-completing-read (concat (car args))
  8946. (if (consp (car (nth 1 args)))
  8947. (mapcar 'car (nth 1 args))
  8948. (nth 1 args))
  8949. (cddr args)))
  8950. (if (and org-completion-use-iswitchb
  8951. (boundp 'iswitchb-mode) iswitchb-mode
  8952. (listp (second args)))
  8953. (apply 'org-iswitchb-completing-read (concat (car args))
  8954. (if (consp (car (nth 1 args)))
  8955. (mapcar 'car (nth 1 args))
  8956. (nth 1 args))
  8957. (cddr args))
  8958. (apply 'completing-read args)))))
  8959. (defun org-extract-attributes (s)
  8960. "Extract the attributes cookie from a string and set as text property."
  8961. (let (a attr (start 0) key value)
  8962. (save-match-data
  8963. (when (string-match "{{\\([^}]+\\)}}$" s)
  8964. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  8965. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  8966. (setq key (match-string 1 a) value (match-string 2 a)
  8967. start (match-end 0)
  8968. attr (plist-put attr (intern key) value))))
  8969. (org-add-props s nil 'org-attr attr))
  8970. s))
  8971. (defun org-extract-attributes-from-string (tag)
  8972. (let (key value attr)
  8973. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  8974. (setq key (match-string 1 tag) value (match-string 2 tag)
  8975. tag (replace-match "" t t tag)
  8976. attr (plist-put attr (intern key) value)))
  8977. (cons tag attr)))
  8978. (defun org-attributes-to-string (plist)
  8979. "Format a property list into an HTML attribute list."
  8980. (let ((s "") key value)
  8981. (while plist
  8982. (setq key (pop plist) value (pop plist))
  8983. (and value
  8984. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  8985. s))
  8986. ;;; Opening/following a link
  8987. (defvar org-link-search-failed nil)
  8988. (defvar org-open-link-functions nil
  8989. "Hook for functions finding a plain text link.
  8990. These functions must take a single argument, the link content.
  8991. They will be called for links that look like [[link text][description]]
  8992. when LINK TEXT does not have a protocol like \"http:\" and does not look
  8993. like a filename (e.g. \"./blue.png\").
  8994. These functions will be called *before* Org attempts to resolve the
  8995. link by doing text searches in the current buffer - so if you want a
  8996. link \"[[target]]\" to still find \"<<target>>\", your function should
  8997. handle this as a special case.
  8998. When the function does handle the link, it must return a non-nil value.
  8999. If it decides that it is not responsible for this link, it must return
  9000. nil to indicate that that Org-mode can continue with other options
  9001. like exact and fuzzy text search.")
  9002. (defun org-next-link (&optional search-backward)
  9003. "Move forward to the next link.
  9004. If the link is in hidden text, expose it."
  9005. (interactive "P")
  9006. (when (and org-link-search-failed (eq this-command last-command))
  9007. (goto-char (point-min))
  9008. (message "Link search wrapped back to beginning of buffer"))
  9009. (setq org-link-search-failed nil)
  9010. (let* ((pos (point))
  9011. (ct (org-context))
  9012. (a (assoc :link ct))
  9013. (srch-fun (if search-backward 're-search-backward 're-search-forward)))
  9014. (cond (a (goto-char (nth (if search-backward 1 2) a)))
  9015. ((looking-at org-any-link-re)
  9016. ;; Don't stay stuck at link without an org-link face
  9017. (forward-char (if search-backward -1 1))))
  9018. (if (funcall srch-fun org-any-link-re nil t)
  9019. (progn
  9020. (goto-char (match-beginning 0))
  9021. (if (outline-invisible-p) (org-show-context)))
  9022. (goto-char pos)
  9023. (setq org-link-search-failed t)
  9024. (message "No further link found"))))
  9025. (defun org-previous-link ()
  9026. "Move backward to the previous link.
  9027. If the link is in hidden text, expose it."
  9028. (interactive)
  9029. (funcall 'org-next-link t))
  9030. (defun org-translate-link (s)
  9031. "Translate a link string if a translation function has been defined."
  9032. (if (and org-link-translation-function
  9033. (fboundp org-link-translation-function)
  9034. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  9035. (progn
  9036. (setq s (funcall org-link-translation-function
  9037. (match-string 1 s) (match-string 2 s)))
  9038. (concat (car s) ":" (cdr s)))
  9039. s))
  9040. (defun org-translate-link-from-planner (type path)
  9041. "Translate a link from Emacs Planner syntax so that Org can follow it.
  9042. This is still an experimental function, your mileage may vary."
  9043. (cond
  9044. ((member type '("http" "https" "news" "ftp"))
  9045. ;; standard Internet links are the same.
  9046. nil)
  9047. ((and (equal type "irc") (string-match "^//" path))
  9048. ;; Planner has two / at the beginning of an irc link, we have 1.
  9049. ;; We should have zero, actually....
  9050. (setq path (substring path 1)))
  9051. ((and (equal type "lisp") (string-match "^/" path))
  9052. ;; Planner has a slash, we do not.
  9053. (setq type "elisp" path (substring path 1)))
  9054. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  9055. ;; A typical message link. Planner has the id after the final slash,
  9056. ;; we separate it with a hash mark
  9057. (setq path (concat (match-string 1 path) "#"
  9058. (org-remove-angle-brackets (match-string 2 path)))))
  9059. )
  9060. (cons type path))
  9061. (defun org-find-file-at-mouse (ev)
  9062. "Open file link or URL at mouse."
  9063. (interactive "e")
  9064. (mouse-set-point ev)
  9065. (org-open-at-point 'in-emacs))
  9066. (defun org-open-at-mouse (ev)
  9067. "Open file link or URL at mouse.
  9068. See the docstring of `org-open-file' for details."
  9069. (interactive "e")
  9070. (mouse-set-point ev)
  9071. (if (eq major-mode 'org-agenda-mode)
  9072. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  9073. (org-open-at-point))
  9074. (defvar org-window-config-before-follow-link nil
  9075. "The window configuration before following a link.
  9076. This is saved in case the need arises to restore it.")
  9077. (defvar org-open-link-marker (make-marker)
  9078. "Marker pointing to the location where `org-open-at-point; was called.")
  9079. ;;;###autoload
  9080. (defun org-open-at-point-global ()
  9081. "Follow a link like Org-mode does.
  9082. This command can be called in any mode to follow a link that has
  9083. Org-mode syntax."
  9084. (interactive)
  9085. (org-run-like-in-org-mode 'org-open-at-point))
  9086. ;;;###autoload
  9087. (defun org-open-link-from-string (s &optional arg reference-buffer)
  9088. "Open a link in the string S, as if it was in Org-mode."
  9089. (interactive "sLink: \nP")
  9090. (let ((reference-buffer (or reference-buffer (current-buffer))))
  9091. (with-temp-buffer
  9092. (let ((org-inhibit-startup (not reference-buffer)))
  9093. (org-mode)
  9094. (insert s)
  9095. (goto-char (point-min))
  9096. (when reference-buffer
  9097. (setq org-link-abbrev-alist-local
  9098. (with-current-buffer reference-buffer
  9099. org-link-abbrev-alist-local)))
  9100. (org-open-at-point arg reference-buffer)))))
  9101. (defvar org-open-at-point-functions nil
  9102. "Hook that is run when following a link at point.
  9103. Functions in this hook must return t if they identify and follow
  9104. a link at point. If they don't find anything interesting at point,
  9105. they must return nil.")
  9106. (defvar clean-buffer-list-kill-buffer-names) ; Defined in midnight.el
  9107. (defun org-open-at-point (&optional arg reference-buffer)
  9108. "Open link at or after point.
  9109. If there is no link at point, this function will search forward up to
  9110. the end of the current line.
  9111. Normally, files will be opened by an appropriate application. If the
  9112. optional prefix argument ARG is non-nil, Emacs will visit the file.
  9113. With a double prefix argument, try to open outside of Emacs, in the
  9114. application the system uses for this file type."
  9115. (interactive "P")
  9116. ;; if in a code block, then open the block's results
  9117. (unless (call-interactively #'org-babel-open-src-block-result)
  9118. (org-load-modules-maybe)
  9119. (move-marker org-open-link-marker (point))
  9120. (setq org-window-config-before-follow-link (current-window-configuration))
  9121. (org-remove-occur-highlights nil nil t)
  9122. (cond
  9123. ((and (org-at-heading-p)
  9124. (not (org-at-timestamp-p t))
  9125. (not (org-in-regexp
  9126. (concat org-plain-link-re "\\|"
  9127. org-bracket-link-regexp "\\|"
  9128. org-angle-link-re "\\|"
  9129. "[ \t]:[^ \t\n]+:[ \t]*$")))
  9130. (not (get-text-property (point) 'org-linked-text)))
  9131. (or (let* ((lkall (org-offer-links-in-entry (current-buffer) (point) arg))
  9132. (lk0 (car lkall))
  9133. (lk (if (stringp lk0) (list lk0) lk0))
  9134. (lkend (cdr lkall)))
  9135. (mapcar (lambda(l)
  9136. (search-forward l nil lkend)
  9137. (goto-char (match-beginning 0))
  9138. (org-open-at-point))
  9139. lk))
  9140. (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
  9141. ((run-hook-with-args-until-success 'org-open-at-point-functions))
  9142. ((and (org-at-timestamp-p t)
  9143. (not (org-in-regexp org-bracket-link-regexp)))
  9144. (org-follow-timestamp-link))
  9145. ((and (or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  9146. (not (org-in-regexp org-any-link-re)))
  9147. (org-footnote-action))
  9148. (t
  9149. (let (type path link line search (pos (point)))
  9150. (catch 'match
  9151. (save-excursion
  9152. (skip-chars-forward "^]\n\r")
  9153. (when (org-in-regexp org-bracket-link-regexp 1)
  9154. (setq link (org-extract-attributes
  9155. (org-link-unescape (org-match-string-no-properties 1))))
  9156. (while (string-match " *\n *" link)
  9157. (setq link (replace-match " " t t link)))
  9158. (setq link (org-link-expand-abbrev link))
  9159. (cond
  9160. ((or (file-name-absolute-p link)
  9161. (string-match "^\\.\\.?/" link))
  9162. (setq type "file" path link))
  9163. ((string-match org-link-re-with-space3 link)
  9164. (setq type (match-string 1 link) path (match-string 2 link)))
  9165. ((string-match "^help:+\\(.+\\)" link)
  9166. (setq type "help" path (match-string 1 link)))
  9167. (t (setq type "thisfile" path link)))
  9168. (throw 'match t)))
  9169. (when (get-text-property (point) 'org-linked-text)
  9170. (setq type "thisfile"
  9171. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  9172. (1+ (point)) (point))
  9173. path (buffer-substring
  9174. (or (previous-single-property-change pos 'org-linked-text)
  9175. (point-min))
  9176. (or (next-single-property-change pos 'org-linked-text)
  9177. (point-max))))
  9178. (throw 'match t))
  9179. (save-excursion
  9180. (when (or (org-in-regexp org-angle-link-re)
  9181. (let ((match (org-in-regexp org-plain-link-re)))
  9182. ;; Check a plain link is not within a bracket link
  9183. (and match
  9184. (save-excursion
  9185. (progn
  9186. (goto-char (car match))
  9187. (not (org-in-regexp org-bracket-link-regexp))))))
  9188. (let ((line_ending (save-excursion (end-of-line) (point))))
  9189. ;; We are in a line before a plain or bracket link
  9190. (or (re-search-forward org-plain-link-re line_ending t)
  9191. (re-search-forward org-bracket-link-regexp line_ending t))))
  9192. (setq type (match-string 1)
  9193. path (org-link-unescape (match-string 2)))
  9194. (throw 'match t)))
  9195. (save-excursion
  9196. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@#%:]+\\):[ \t]*$"))
  9197. (setq type "tags"
  9198. path (match-string 1))
  9199. (while (string-match ":" path)
  9200. (setq path (replace-match "+" t t path)))
  9201. (throw 'match t)))
  9202. (when (org-in-regexp "<\\([^><\n]+\\)>")
  9203. (setq type "tree-match"
  9204. path (match-string 1))
  9205. (throw 'match t)))
  9206. (unless path
  9207. (user-error "No link found"))
  9208. ;; switch back to reference buffer
  9209. ;; needed when if called in a temporary buffer through
  9210. ;; org-open-link-from-string
  9211. (with-current-buffer (or reference-buffer (current-buffer))
  9212. ;; Remove any trailing spaces in path
  9213. (if (string-match " +\\'" path)
  9214. (setq path (replace-match "" t t path)))
  9215. (if (and org-link-translation-function
  9216. (fboundp org-link-translation-function))
  9217. ;; Check if we need to translate the link
  9218. (let ((tmp (funcall org-link-translation-function type path)))
  9219. (setq type (car tmp) path (cdr tmp))))
  9220. (cond
  9221. ((assoc type org-link-protocols)
  9222. (funcall (nth 1 (assoc type org-link-protocols)) path))
  9223. ((equal type "help")
  9224. (let ((f-or-v (intern path)))
  9225. (cond ((fboundp f-or-v)
  9226. (describe-function f-or-v))
  9227. ((boundp f-or-v)
  9228. (describe-variable f-or-v))
  9229. (t (error "Not a known function or variable")))))
  9230. ((equal type "mailto")
  9231. (let ((cmd (car org-link-mailto-program))
  9232. (args (cdr org-link-mailto-program)) args1
  9233. (address path) (subject "") a)
  9234. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  9235. (setq address (match-string 1 path)
  9236. subject (org-link-escape (match-string 2 path))))
  9237. (while args
  9238. (cond
  9239. ((not (stringp (car args))) (push (pop args) args1))
  9240. (t (setq a (pop args))
  9241. (if (string-match "%a" a)
  9242. (setq a (replace-match address t t a)))
  9243. (if (string-match "%s" a)
  9244. (setq a (replace-match subject t t a)))
  9245. (push a args1))))
  9246. (apply cmd (nreverse args1))))
  9247. ((member type '("http" "https" "ftp" "news"))
  9248. (browse-url (concat type ":" (if (org-string-match-p "[[:nonascii:] ]" path)
  9249. (org-link-escape
  9250. path org-link-escape-chars-browser)
  9251. path))))
  9252. ((string= type "doi")
  9253. (browse-url (concat org-doi-server-url (if (org-string-match-p "[[:nonascii:] ]" path)
  9254. (org-link-escape
  9255. path org-link-escape-chars-browser)
  9256. path))))
  9257. ((member type '("message"))
  9258. (browse-url (concat type ":" path)))
  9259. ((string= type "tags")
  9260. (org-tags-view arg path))
  9261. ((string= type "tree-match")
  9262. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  9263. ((string= type "file")
  9264. (if (string-match "::\\([0-9]+\\)\\'" path)
  9265. (setq line (string-to-number (match-string 1 path))
  9266. path (substring path 0 (match-beginning 0)))
  9267. (if (string-match "::\\(.+\\)\\'" path)
  9268. (setq search (match-string 1 path)
  9269. path (substring path 0 (match-beginning 0)))))
  9270. (if (string-match "[*?{]" (file-name-nondirectory path))
  9271. (dired path)
  9272. (org-open-file path arg line search)))
  9273. ((string= type "shell")
  9274. (let ((buf (generate-new-buffer "*Org Shell Output"))
  9275. (cmd path))
  9276. (if (or (and (not (string= org-confirm-shell-link-not-regexp ""))
  9277. (string-match org-confirm-shell-link-not-regexp cmd))
  9278. (not org-confirm-shell-link-function)
  9279. (funcall org-confirm-shell-link-function
  9280. (format "Execute \"%s\" in shell? "
  9281. (org-add-props cmd nil
  9282. 'face 'org-warning))))
  9283. (progn
  9284. (message "Executing %s" cmd)
  9285. (shell-command cmd buf)
  9286. (if (featurep 'midnight)
  9287. (setq clean-buffer-list-kill-buffer-names
  9288. (cons buf clean-buffer-list-kill-buffer-names))))
  9289. (error "Abort"))))
  9290. ((string= type "elisp")
  9291. (let ((cmd path))
  9292. (if (or (and (not (string= org-confirm-elisp-link-not-regexp ""))
  9293. (string-match org-confirm-elisp-link-not-regexp cmd))
  9294. (not org-confirm-elisp-link-function)
  9295. (funcall org-confirm-elisp-link-function
  9296. (format "Execute \"%s\" as elisp? "
  9297. (org-add-props cmd nil
  9298. 'face 'org-warning))))
  9299. (message "%s => %s" cmd
  9300. (if (equal (string-to-char cmd) ?\()
  9301. (eval (read cmd))
  9302. (call-interactively (read cmd))))
  9303. (error "Abort"))))
  9304. ((and (string= type "thisfile")
  9305. (run-hook-with-args-until-success
  9306. 'org-open-link-functions path)))
  9307. ((string= type "thisfile")
  9308. (if arg
  9309. (switch-to-buffer-other-window
  9310. (org-get-buffer-for-internal-link (current-buffer)))
  9311. (org-mark-ring-push))
  9312. (let ((cmd `(org-link-search
  9313. ,path
  9314. ,(cond ((equal arg '(4)) ''occur)
  9315. ((equal arg '(16)) ''org-occur))
  9316. ,pos)))
  9317. (condition-case nil (let ((org-link-search-inhibit-query t))
  9318. (eval cmd))
  9319. (error (progn (widen) (eval cmd))))))
  9320. (t (browse-url-at-point)))))))
  9321. (move-marker org-open-link-marker nil)
  9322. (run-hook-with-args 'org-follow-link-hook)))
  9323. (defun org-offer-links-in-entry (buffer marker &optional nth zero)
  9324. "Offer links in the current entry and return the selected link.
  9325. If there is only one link, return it.
  9326. If NTH is an integer, return the NTH link found.
  9327. If ZERO is a string, check also this string for a link, and if
  9328. there is one, return it."
  9329. (with-current-buffer buffer
  9330. (save-excursion
  9331. (save-restriction
  9332. (widen)
  9333. (goto-char marker)
  9334. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  9335. "\\(" org-angle-link-re "\\)\\|"
  9336. "\\(" org-plain-link-re "\\)"))
  9337. (cnt ?0)
  9338. (in-emacs (if (integerp nth) nil nth))
  9339. have-zero end links link c)
  9340. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  9341. (push (match-string 0 zero) links)
  9342. (setq cnt (1- cnt) have-zero t))
  9343. (save-excursion
  9344. (org-back-to-heading t)
  9345. (setq end (save-excursion (outline-next-heading) (point)))
  9346. (while (re-search-forward re end t)
  9347. (push (match-string 0) links))
  9348. (setq links (org-uniquify (reverse links))))
  9349. (cond
  9350. ((null links)
  9351. (message "No links"))
  9352. ((equal (length links) 1)
  9353. (setq link (car links)))
  9354. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  9355. (setq link (nth (if have-zero nth (1- nth)) links)))
  9356. (t ; we have to select a link
  9357. (save-excursion
  9358. (save-window-excursion
  9359. (delete-other-windows)
  9360. (with-output-to-temp-buffer "*Select Link*"
  9361. (mapc (lambda (l)
  9362. (if (not (string-match org-bracket-link-regexp l))
  9363. (princ (format "[%c] %s\n" (incf cnt)
  9364. (org-remove-angle-brackets l)))
  9365. (if (match-end 3)
  9366. (princ (format "[%c] %s (%s)\n" (incf cnt)
  9367. (match-string 3 l) (match-string 1 l)))
  9368. (princ (format "[%c] %s\n" (incf cnt)
  9369. (match-string 1 l))))))
  9370. links))
  9371. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  9372. (message "Select link to open, RET to open all:")
  9373. (setq c (read-char-exclusive))
  9374. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  9375. (when (equal c ?q) (error "Abort"))
  9376. (if (equal c ?\C-m)
  9377. (setq link links)
  9378. (setq nth (- c ?0))
  9379. (if have-zero (setq nth (1+ nth)))
  9380. (unless (and (integerp nth) (>= (length links) nth))
  9381. (error "Invalid link selection"))
  9382. (setq link (nth (1- nth) links)))))
  9383. (cons link end))))))
  9384. ;; Add special file links that specify the way of opening
  9385. (org-add-link-type "file+sys" 'org-open-file-with-system)
  9386. (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
  9387. (defun org-open-file-with-system (path)
  9388. "Open file at PATH using the system way of opening it."
  9389. (org-open-file path 'system))
  9390. (defun org-open-file-with-emacs (path)
  9391. "Open file at PATH in Emacs."
  9392. (org-open-file path 'emacs))
  9393. ;;; File search
  9394. (defvar org-create-file-search-functions nil
  9395. "List of functions to construct the right search string for a file link.
  9396. These functions are called in turn with point at the location to
  9397. which the link should point.
  9398. A function in the hook should first test if it would like to
  9399. handle this file type, for example by checking the `major-mode'
  9400. or the file extension. If it decides not to handle this file, it
  9401. should just return nil to give other functions a chance. If it
  9402. does handle the file, it must return the search string to be used
  9403. when following the link. The search string will be part of the
  9404. file link, given after a double colon, and `org-open-at-point'
  9405. will automatically search for it. If special measures must be
  9406. taken to make the search successful, another function should be
  9407. added to the companion hook `org-execute-file-search-functions',
  9408. which see.
  9409. A function in this hook may also use `setq' to set the variable
  9410. `description' to provide a suggestion for the descriptive text to
  9411. be used for this link when it gets inserted into an Org-mode
  9412. buffer with \\[org-insert-link].")
  9413. (defvar org-execute-file-search-functions nil
  9414. "List of functions to execute a file search triggered by a link.
  9415. Functions added to this hook must accept a single argument, the
  9416. search string that was part of the file link, the part after the
  9417. double colon. The function must first check if it would like to
  9418. handle this search, for example by checking the `major-mode' or
  9419. the file extension. If it decides not to handle this search, it
  9420. should just return nil to give other functions a chance. If it
  9421. does handle the search, it must return a non-nil value to keep
  9422. other functions from trying.
  9423. Each function can access the current prefix argument through the
  9424. variable `current-prefix-argument'. Note that a single prefix is
  9425. used to force opening a link in Emacs, so it may be good to only
  9426. use a numeric or double prefix to guide the search function.
  9427. In case this is needed, a function in this hook can also restore
  9428. the window configuration before `org-open-at-point' was called using:
  9429. (set-window-configuration org-window-config-before-follow-link)")
  9430. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  9431. (defun org-link-search (s &optional type avoid-pos stealth)
  9432. "Search for a link search option.
  9433. If S is surrounded by forward slashes, it is interpreted as a
  9434. regular expression. In org-mode files, this will create an `org-occur'
  9435. sparse tree. In ordinary files, `occur' will be used to list matches.
  9436. If the current buffer is in `dired-mode', grep will be used to search
  9437. in all files. If AVOID-POS is given, ignore matches near that position.
  9438. When optional argument STEALTH is non-nil, do not modify
  9439. visibility around point, thus ignoring
  9440. `org-show-hierarchy-above', `org-show-following-heading' and
  9441. `org-show-siblings' variables."
  9442. (let ((case-fold-search t)
  9443. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  9444. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  9445. (append '(("") (" ") ("\t") ("\n"))
  9446. org-emphasis-alist)
  9447. "\\|") "\\)"))
  9448. (pos (point))
  9449. (pre nil) (post nil)
  9450. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  9451. (cond
  9452. ;; First check if there are any special search functions
  9453. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  9454. ;; Now try the builtin stuff
  9455. ((and (equal (string-to-char s0) ?#)
  9456. (> (length s0) 1)
  9457. (save-excursion
  9458. (goto-char (point-min))
  9459. (and
  9460. (re-search-forward
  9461. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  9462. (setq type 'dedicated
  9463. pos (match-beginning 0))))
  9464. ;; There is an exact target for this
  9465. (goto-char pos)
  9466. (org-back-to-heading t)))
  9467. ((save-excursion
  9468. (goto-char (point-min))
  9469. (and
  9470. (re-search-forward
  9471. (concat "<<" (regexp-quote s0) ">>") nil t)
  9472. (setq type 'dedicated
  9473. pos (match-beginning 0))))
  9474. ;; There is an exact target for this
  9475. (goto-char pos))
  9476. ((save-excursion
  9477. (goto-char (point-min))
  9478. (and
  9479. (re-search-forward
  9480. (format "^[ \t]*#\\+TARGET: %s" (regexp-quote s0)) nil t)
  9481. (setq type 'dedicated pos (match-beginning 0))))
  9482. ;; Found an invisible target.
  9483. (goto-char pos))
  9484. ((save-excursion
  9485. (goto-char (point-min))
  9486. (and
  9487. (re-search-forward
  9488. (format "^[ \t]*#\\+NAME: %s" (regexp-quote s0)) nil t)
  9489. (setq type 'dedicated pos (match-beginning 0))))
  9490. ;; Found an element with a matching #+name affiliated keyword.
  9491. (goto-char pos))
  9492. ((and (string-match "^(\\(.*\\))$" s0)
  9493. (save-excursion
  9494. (goto-char (point-min))
  9495. (and
  9496. (re-search-forward
  9497. (concat "[^[]" (regexp-quote
  9498. (format org-coderef-label-format
  9499. (match-string 1 s0))))
  9500. nil t)
  9501. (setq type 'dedicated
  9502. pos (1+ (match-beginning 0))))))
  9503. ;; There is a coderef target for this
  9504. (goto-char pos))
  9505. ((string-match "^/\\(.*\\)/$" s)
  9506. ;; A regular expression
  9507. (cond
  9508. ((derived-mode-p 'org-mode)
  9509. (org-occur (match-string 1 s)))
  9510. (t (org-do-occur (match-string 1 s)))))
  9511. ((and (derived-mode-p 'org-mode) org-link-search-must-match-exact-headline)
  9512. (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
  9513. (goto-char (point-min))
  9514. (cond
  9515. ((let (case-fold-search)
  9516. (re-search-forward (format org-complex-heading-regexp-format
  9517. (regexp-quote s))
  9518. nil t))
  9519. ;; OK, found a match
  9520. (setq type 'dedicated)
  9521. (goto-char (match-beginning 0)))
  9522. ((and (not org-link-search-inhibit-query)
  9523. (eq org-link-search-must-match-exact-headline 'query-to-create)
  9524. (y-or-n-p "No match - create this as a new heading? "))
  9525. (goto-char (point-max))
  9526. (or (bolp) (newline))
  9527. (insert "* " s "\n")
  9528. (beginning-of-line 0))
  9529. (t
  9530. (goto-char pos)
  9531. (error "No match"))))
  9532. (t
  9533. ;; A normal search string
  9534. (when (equal (string-to-char s) ?*)
  9535. ;; Anchor on headlines, post may include tags.
  9536. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  9537. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
  9538. s (substring s 1)))
  9539. (remove-text-properties
  9540. 0 (length s)
  9541. '(face nil mouse-face nil keymap nil fontified nil) s)
  9542. ;; Make a series of regular expressions to find a match
  9543. (setq words (org-split-string s "[ \n\r\t]+")
  9544. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  9545. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  9546. "\\)" markers)
  9547. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  9548. re2a (concat "[ \t\r\n]" re2a_)
  9549. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  9550. re4 (concat "[^a-zA-Z_]" re4_)
  9551. re1 (concat pre re2 post)
  9552. re3 (concat pre (if pre re4_ re4) post)
  9553. re5 (concat pre ".*" re4)
  9554. re2 (concat pre re2)
  9555. re2a (concat pre (if pre re2a_ re2a))
  9556. re4 (concat pre (if pre re4_ re4))
  9557. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  9558. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  9559. re5 "\\)"
  9560. ))
  9561. (cond
  9562. ((eq type 'org-occur) (org-occur reall))
  9563. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  9564. (t (goto-char (point-min))
  9565. (setq type 'fuzzy)
  9566. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  9567. (org-search-not-self 1 re1 nil t)
  9568. (org-search-not-self 1 re2 nil t)
  9569. (org-search-not-self 1 re2a nil t)
  9570. (org-search-not-self 1 re3 nil t)
  9571. (org-search-not-self 1 re4 nil t)
  9572. (org-search-not-self 1 re5 nil t)
  9573. )
  9574. (goto-char (match-beginning 1))
  9575. (goto-char pos)
  9576. (error "No match"))))))
  9577. (and (derived-mode-p 'org-mode)
  9578. (not stealth)
  9579. (org-show-context 'link-search))
  9580. type))
  9581. (defun org-search-not-self (group &rest args)
  9582. "Execute `re-search-forward', but only accept matches that do not
  9583. enclose the position of `org-open-link-marker'."
  9584. (let ((m org-open-link-marker))
  9585. (catch 'exit
  9586. (while (apply 're-search-forward args)
  9587. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  9588. (goto-char (match-end group))
  9589. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  9590. (> (match-beginning 0) (marker-position m))
  9591. (< (match-end 0) (marker-position m)))
  9592. (save-match-data
  9593. (or (not (org-in-regexp
  9594. org-bracket-link-analytic-regexp 1))
  9595. (not (match-end 4)) ; no description
  9596. (and (<= (match-beginning 4) (point))
  9597. (>= (match-end 4) (point))))))
  9598. (throw 'exit (point))))))))
  9599. (defun org-get-buffer-for-internal-link (buffer)
  9600. "Return a buffer to be used for displaying the link target of internal links."
  9601. (cond
  9602. ((not org-display-internal-link-with-indirect-buffer)
  9603. buffer)
  9604. ((string-match "(Clone)$" (buffer-name buffer))
  9605. (message "Buffer is already a clone, not making another one")
  9606. ;; we also do not modify visibility in this case
  9607. buffer)
  9608. (t ; make a new indirect buffer for displaying the link
  9609. (let* ((bn (buffer-name buffer))
  9610. (ibn (concat bn "(Clone)"))
  9611. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  9612. (with-current-buffer ib (org-overview))
  9613. ib))))
  9614. (defun org-do-occur (regexp &optional cleanup)
  9615. "Call the Emacs command `occur'.
  9616. If CLEANUP is non-nil, remove the printout of the regular expression
  9617. in the *Occur* buffer. This is useful if the regex is long and not useful
  9618. to read."
  9619. (occur regexp)
  9620. (when cleanup
  9621. (let ((cwin (selected-window)) win beg end)
  9622. (when (setq win (get-buffer-window "*Occur*"))
  9623. (select-window win))
  9624. (goto-char (point-min))
  9625. (when (re-search-forward "match[a-z]+" nil t)
  9626. (setq beg (match-end 0))
  9627. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  9628. (setq end (1- (match-beginning 0)))))
  9629. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  9630. (goto-char (point-min))
  9631. (select-window cwin))))
  9632. ;;; The mark ring for links jumps
  9633. (defvar org-mark-ring nil
  9634. "Mark ring for positions before jumps in Org-mode.")
  9635. (defvar org-mark-ring-last-goto nil
  9636. "Last position in the mark ring used to go back.")
  9637. ;; Fill and close the ring
  9638. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  9639. (loop for i from 1 to org-mark-ring-length do
  9640. (push (make-marker) org-mark-ring))
  9641. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  9642. org-mark-ring)
  9643. (defun org-mark-ring-push (&optional pos buffer)
  9644. "Put the current position or POS into the mark ring and rotate it."
  9645. (interactive)
  9646. (setq pos (or pos (point)))
  9647. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  9648. (move-marker (car org-mark-ring)
  9649. (or pos (point))
  9650. (or buffer (current-buffer)))
  9651. (message "%s"
  9652. (substitute-command-keys
  9653. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  9654. (defun org-mark-ring-goto (&optional n)
  9655. "Jump to the previous position in the mark ring.
  9656. With prefix arg N, jump back that many stored positions. When
  9657. called several times in succession, walk through the entire ring.
  9658. Org-mode commands jumping to a different position in the current file,
  9659. or to another Org-mode file, automatically push the old position
  9660. onto the ring."
  9661. (interactive "p")
  9662. (let (p m)
  9663. (if (eq last-command this-command)
  9664. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  9665. (setq p org-mark-ring))
  9666. (setq org-mark-ring-last-goto p)
  9667. (setq m (car p))
  9668. (org-pop-to-buffer-same-window (marker-buffer m))
  9669. (goto-char m)
  9670. (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  9671. (defun org-remove-angle-brackets (s)
  9672. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  9673. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  9674. s)
  9675. (defun org-add-angle-brackets (s)
  9676. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  9677. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  9678. s)
  9679. (defun org-remove-double-quotes (s)
  9680. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  9681. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  9682. s)
  9683. ;;; Following specific links
  9684. (defun org-follow-timestamp-link ()
  9685. "Open an agenda view for the time-stamp date/range at point."
  9686. (cond
  9687. ((org-at-date-range-p t)
  9688. (let ((org-agenda-start-on-weekday)
  9689. (t1 (match-string 1))
  9690. (t2 (match-string 2)) tt1 tt2)
  9691. (setq tt1 (time-to-days (org-time-string-to-time t1))
  9692. tt2 (time-to-days (org-time-string-to-time t2)))
  9693. (let ((org-agenda-buffer-tmp-name
  9694. (format "*Org Agenda(a:%s)"
  9695. (concat (substring t1 0 10) "--" (substring t2 0 10)))))
  9696. (org-agenda-list nil tt1 (1+ (- tt2 tt1))))))
  9697. ((org-at-timestamp-p t)
  9698. (let ((org-agenda-buffer-tmp-name
  9699. (format "*Org Agenda(a:%s)" (substring (match-string 1) 0 10))))
  9700. (org-agenda-list nil (time-to-days (org-time-string-to-time
  9701. (substring (match-string 1) 0 10)))
  9702. 1)))
  9703. (t (error "This should not happen"))))
  9704. ;;; Following file links
  9705. (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
  9706. (declare-function mailcap-extension-to-mime "mailcap" (extn))
  9707. (declare-function mailcap-mime-info
  9708. "mailcap" (string &optional request no-decode))
  9709. (defvar org-wait nil)
  9710. (defun org-open-file (path &optional in-emacs line search)
  9711. "Open the file at PATH.
  9712. First, this expands any special file name abbreviations. Then the
  9713. configuration variable `org-file-apps' is checked if it contains an
  9714. entry for this file type, and if yes, the corresponding command is launched.
  9715. If no application is found, Emacs simply visits the file.
  9716. With optional prefix argument IN-EMACS, Emacs will visit the file.
  9717. With a double \\[universal-argument] \\[universal-argument] \
  9718. prefix arg, Org tries to avoid opening in Emacs
  9719. and to use an external application to visit the file.
  9720. Optional LINE specifies a line to go to, optional SEARCH a string
  9721. to search for. If LINE or SEARCH is given, the file will be
  9722. opened in Emacs, unless an entry from org-file-apps that makes
  9723. use of groups in a regexp matches.
  9724. If you want to change the way frames are used when following a
  9725. link, please customize `org-link-frame-setup'.
  9726. If the file does not exist, an error is thrown."
  9727. (let* ((file (if (equal path "")
  9728. buffer-file-name
  9729. (substitute-in-file-name (expand-file-name path))))
  9730. (file-apps (append org-file-apps (org-default-apps)))
  9731. (apps (org-remove-if
  9732. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9733. (apps-dlink (org-remove-if-not
  9734. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9735. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  9736. (dirp (if remp nil (file-directory-p file)))
  9737. (file (if (and dirp org-open-directory-means-index-dot-org)
  9738. (concat (file-name-as-directory file) "index.org")
  9739. file))
  9740. (a-m-a-p (assq 'auto-mode apps))
  9741. (dfile (downcase file))
  9742. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  9743. (link (cond ((and (eq line nil)
  9744. (eq search nil))
  9745. file)
  9746. (line
  9747. (concat file "::" (number-to-string line)))
  9748. (search
  9749. (concat file "::" search))))
  9750. (dlink (downcase link))
  9751. (old-buffer (current-buffer))
  9752. (old-pos (point))
  9753. (old-mode major-mode)
  9754. ext cmd link-match-data)
  9755. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  9756. (setq ext (match-string 1 dfile))
  9757. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  9758. (setq ext (match-string 1 dfile))))
  9759. (cond
  9760. ((member in-emacs '((16) system))
  9761. (setq cmd (cdr (assoc 'system apps))))
  9762. (in-emacs (setq cmd 'emacs))
  9763. (t
  9764. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  9765. (and dirp (cdr (assoc 'directory apps)))
  9766. ; first, try matching against apps-dlink
  9767. ; if we get a match here, store the match data for later
  9768. (let ((match (assoc-default dlink apps-dlink
  9769. 'string-match)))
  9770. (if match
  9771. (progn (setq link-match-data (match-data))
  9772. match)
  9773. (progn (setq in-emacs (or in-emacs line search))
  9774. nil))) ; if we have no match in apps-dlink,
  9775. ; always open the file in emacs if line or search
  9776. ; is given (for backwards compatibility)
  9777. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  9778. 'string-match)
  9779. (cdr (assoc ext apps))
  9780. (cdr (assoc t apps))))))
  9781. (when (eq cmd 'system)
  9782. (setq cmd (cdr (assoc 'system apps))))
  9783. (when (eq cmd 'default)
  9784. (setq cmd (cdr (assoc t apps))))
  9785. (when (eq cmd 'mailcap)
  9786. (require 'mailcap)
  9787. (mailcap-parse-mailcaps)
  9788. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  9789. (command (mailcap-mime-info mime-type)))
  9790. (if (stringp command)
  9791. (setq cmd command)
  9792. (setq cmd 'emacs))))
  9793. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  9794. (not (file-exists-p file))
  9795. (not org-open-non-existing-files))
  9796. (error "No such file: %s" file))
  9797. (cond
  9798. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  9799. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  9800. (while (string-match "['\"]%s['\"]" cmd)
  9801. (setq cmd (replace-match "%s" t t cmd)))
  9802. (while (string-match "%s" cmd)
  9803. (setq cmd (replace-match
  9804. (save-match-data
  9805. (shell-quote-argument
  9806. (convert-standard-filename file)))
  9807. t t cmd)))
  9808. ;; Replace "%1", "%2" etc. in command with group matches from regex
  9809. (save-match-data
  9810. (let ((match-index 1)
  9811. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  9812. (set-match-data link-match-data)
  9813. (while (<= match-index number-of-groups)
  9814. (let ((regex (concat "%" (number-to-string match-index)))
  9815. (replace-with (match-string match-index dlink)))
  9816. (while (string-match regex cmd)
  9817. (setq cmd (replace-match replace-with t t cmd))))
  9818. (setq match-index (+ match-index 1)))))
  9819. (save-window-excursion
  9820. (message "Running %s...done" cmd)
  9821. (start-process-shell-command cmd nil cmd)
  9822. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))))
  9823. ((or (stringp cmd)
  9824. (eq cmd 'emacs))
  9825. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  9826. (widen)
  9827. (if line (org-goto-line line)
  9828. (if search (org-link-search search))))
  9829. ((consp cmd)
  9830. (let ((file (convert-standard-filename file)))
  9831. (save-match-data
  9832. (set-match-data link-match-data)
  9833. (eval cmd))))
  9834. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  9835. (and (derived-mode-p 'org-mode) (eq old-mode 'org-mode)
  9836. (or (not (equal old-buffer (current-buffer)))
  9837. (not (equal old-pos (point))))
  9838. (org-mark-ring-push old-pos old-buffer))))
  9839. (defun org-file-apps-entry-match-against-dlink-p (entry)
  9840. "This function returns non-nil if `entry' uses a regular
  9841. expression which should be matched against the whole link by
  9842. org-open-file.
  9843. It assumes that is the case when the entry uses a regular
  9844. expression which has at least one grouping construct and the
  9845. action is either a lisp form or a command string containing
  9846. '%1', i.e. using at least one subexpression match as a
  9847. parameter."
  9848. (let ((selector (car entry))
  9849. (action (cdr entry)))
  9850. (if (stringp selector)
  9851. (and (> (regexp-opt-depth selector) 0)
  9852. (or (and (stringp action)
  9853. (string-match "%[0-9]" action))
  9854. (consp action)))
  9855. nil)))
  9856. (defun org-default-apps ()
  9857. "Return the default applications for this operating system."
  9858. (cond
  9859. ((eq system-type 'darwin)
  9860. org-file-apps-defaults-macosx)
  9861. ((eq system-type 'windows-nt)
  9862. org-file-apps-defaults-windowsnt)
  9863. (t org-file-apps-defaults-gnu)))
  9864. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  9865. "Convert extensions to regular expressions in the cars of LIST.
  9866. Also, weed out any non-string entries, because the return value is used
  9867. only for regexp matching.
  9868. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  9869. point to the symbol `emacs', indicating that the file should
  9870. be opened in Emacs."
  9871. (append
  9872. (delq nil
  9873. (mapcar (lambda (x)
  9874. (if (not (stringp (car x)))
  9875. nil
  9876. (if (string-match "\\W" (car x))
  9877. x
  9878. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  9879. list))
  9880. (if add-auto-mode
  9881. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  9882. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  9883. (defun org-file-remote-p (file)
  9884. "Test whether FILE specifies a location on a remote system.
  9885. Return non-nil if the location is indeed remote.
  9886. For example, the filename \"/user@host:/foo\" specifies a location
  9887. on the system \"/user@host:\"."
  9888. (cond ((fboundp 'file-remote-p)
  9889. (file-remote-p file))
  9890. ((fboundp 'tramp-handle-file-remote-p)
  9891. (tramp-handle-file-remote-p file))
  9892. ((and (boundp 'ange-ftp-name-format)
  9893. (string-match (car ange-ftp-name-format) file))
  9894. t)))
  9895. ;;;; Refiling
  9896. (defun org-get-org-file ()
  9897. "Read a filename, with default directory `org-directory'."
  9898. (let ((default (or org-default-notes-file remember-data-file)))
  9899. (read-file-name (format "File name [%s]: " default)
  9900. (file-name-as-directory org-directory)
  9901. default)))
  9902. (defun org-notes-order-reversed-p ()
  9903. "Check if the current file should receive notes in reversed order."
  9904. (cond
  9905. ((not org-reverse-note-order) nil)
  9906. ((eq t org-reverse-note-order) t)
  9907. ((not (listp org-reverse-note-order)) nil)
  9908. (t (catch 'exit
  9909. (let ((all org-reverse-note-order)
  9910. entry)
  9911. (while (setq entry (pop all))
  9912. (if (string-match (car entry) buffer-file-name)
  9913. (throw 'exit (cdr entry))))
  9914. nil)))))
  9915. (defvar org-refile-target-table nil
  9916. "The list of refile targets, created by `org-refile'.")
  9917. (defvar org-agenda-new-buffers nil
  9918. "Buffers created to visit agenda files.")
  9919. (defvar org-refile-cache nil
  9920. "Cache for refile targets.")
  9921. (defvar org-refile-markers nil
  9922. "All the markers used for caching refile locations.")
  9923. (defun org-refile-marker (pos)
  9924. "Get a new refile marker, but only if caching is in use."
  9925. (if (not org-refile-use-cache)
  9926. pos
  9927. (let ((m (make-marker)))
  9928. (move-marker m pos)
  9929. (push m org-refile-markers)
  9930. m)))
  9931. (defun org-refile-cache-clear ()
  9932. "Clear the refile cache and disable all the markers."
  9933. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  9934. (setq org-refile-markers nil)
  9935. (setq org-refile-cache nil)
  9936. (message "Refile cache has been cleared"))
  9937. (defun org-refile-cache-check-set (set)
  9938. "Check if all the markers in the cache still have live buffers."
  9939. (let (marker)
  9940. (catch 'exit
  9941. (while (and set (setq marker (nth 3 (pop set))))
  9942. ;; if org-refile-use-outline-path is 'file, marker may be nil
  9943. (when (and marker (null (marker-buffer marker)))
  9944. (message "not found") (sit-for 3)
  9945. (throw 'exit nil)))
  9946. t)))
  9947. (defun org-refile-cache-put (set &rest identifiers)
  9948. "Push the refile targets SET into the cache, under IDENTIFIERS."
  9949. (let* ((key (sha1 (prin1-to-string identifiers)))
  9950. (entry (assoc key org-refile-cache)))
  9951. (if entry
  9952. (setcdr entry set)
  9953. (push (cons key set) org-refile-cache))))
  9954. (defun org-refile-cache-get (&rest identifiers)
  9955. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  9956. (cond
  9957. ((not org-refile-cache) nil)
  9958. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  9959. (t
  9960. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  9961. org-refile-cache))))
  9962. (and set (org-refile-cache-check-set set) set)))))
  9963. (defun org-refile-get-targets (&optional default-buffer excluded-entries)
  9964. "Produce a table with refile targets."
  9965. (let ((case-fold-search nil)
  9966. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  9967. (entries (or org-refile-targets '((nil . (:level . 1)))))
  9968. targets tgs txt re files f desc descre fast-path-p level pos0)
  9969. (message "Getting targets...")
  9970. (with-current-buffer (or default-buffer (current-buffer))
  9971. (while (setq entry (pop entries))
  9972. (setq files (car entry) desc (cdr entry))
  9973. (setq fast-path-p nil)
  9974. (cond
  9975. ((null files) (setq files (list (current-buffer))))
  9976. ((eq files 'org-agenda-files)
  9977. (setq files (org-agenda-files 'unrestricted)))
  9978. ((and (symbolp files) (fboundp files))
  9979. (setq files (funcall files)))
  9980. ((and (symbolp files) (boundp files))
  9981. (setq files (symbol-value files))))
  9982. (if (stringp files) (setq files (list files)))
  9983. (cond
  9984. ((eq (car desc) :tag)
  9985. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  9986. ((eq (car desc) :todo)
  9987. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  9988. ((eq (car desc) :regexp)
  9989. (setq descre (cdr desc)))
  9990. ((eq (car desc) :level)
  9991. (setq descre (concat "^\\*\\{" (number-to-string
  9992. (if org-odd-levels-only
  9993. (1- (* 2 (cdr desc)))
  9994. (cdr desc)))
  9995. "\\}[ \t]")))
  9996. ((eq (car desc) :maxlevel)
  9997. (setq fast-path-p t)
  9998. (setq descre (concat "^\\*\\{1," (number-to-string
  9999. (if org-odd-levels-only
  10000. (1- (* 2 (cdr desc)))
  10001. (cdr desc)))
  10002. "\\}[ \t]")))
  10003. (t (error "Bad refiling target description %s" desc)))
  10004. (while (setq f (pop files))
  10005. (with-current-buffer
  10006. (if (bufferp f) f (org-get-agenda-file-buffer f))
  10007. (or
  10008. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  10009. (progn
  10010. (if (bufferp f) (setq f (buffer-file-name
  10011. (buffer-base-buffer f))))
  10012. (setq f (and f (expand-file-name f)))
  10013. (if (eq org-refile-use-outline-path 'file)
  10014. (push (list (file-name-nondirectory f) f nil nil) tgs))
  10015. (save-excursion
  10016. (save-restriction
  10017. (widen)
  10018. (goto-char (point-min))
  10019. (while (re-search-forward descre nil t)
  10020. (goto-char (setq pos0 (point-at-bol)))
  10021. (catch 'next
  10022. (when org-refile-target-verify-function
  10023. (save-match-data
  10024. (or (funcall org-refile-target-verify-function)
  10025. (throw 'next t))))
  10026. (when (and (looking-at org-complex-heading-regexp)
  10027. (not (member (match-string 4) excluded-entries))
  10028. (match-string 4))
  10029. (setq level (org-reduced-level
  10030. (- (match-end 1) (match-beginning 1)))
  10031. txt (org-link-display-format (match-string 4))
  10032. txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
  10033. re (format org-complex-heading-regexp-format
  10034. (regexp-quote (match-string 4))))
  10035. (when org-refile-use-outline-path
  10036. (setq txt (mapconcat
  10037. 'org-protect-slash
  10038. (append
  10039. (if (eq org-refile-use-outline-path
  10040. 'file)
  10041. (list (file-name-nondirectory
  10042. (buffer-file-name
  10043. (buffer-base-buffer))))
  10044. (if (eq org-refile-use-outline-path
  10045. 'full-file-path)
  10046. (list (buffer-file-name
  10047. (buffer-base-buffer)))))
  10048. (org-get-outline-path fast-path-p
  10049. level txt)
  10050. (list txt))
  10051. "/")))
  10052. (push (list txt f re (org-refile-marker (point)))
  10053. tgs)))
  10054. (when (= (point) pos0)
  10055. ;; verification function has not moved point
  10056. (goto-char (point-at-eol))))))))
  10057. (when org-refile-use-cache
  10058. (org-refile-cache-put tgs (buffer-file-name) descre))
  10059. (setq targets (append tgs targets))
  10060. ))))
  10061. (message "Getting targets...done")
  10062. (nreverse targets)))
  10063. (defun org-protect-slash (s)
  10064. (while (string-match "/" s)
  10065. (setq s (replace-match "\\" t t s)))
  10066. s)
  10067. (defvar org-olpa (make-vector 20 nil))
  10068. (defun org-get-outline-path (&optional fastp level heading)
  10069. "Return the outline path to the current entry, as a list.
  10070. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  10071. routine which makes outline path derivations for an entire file,
  10072. avoiding backtracing. Refile target collection makes use of that."
  10073. (if fastp
  10074. (progn
  10075. (if (> level 19)
  10076. (error "Outline path failure, more than 19 levels"))
  10077. (loop for i from level upto 19 do
  10078. (aset org-olpa i nil))
  10079. (prog1
  10080. (delq nil (append org-olpa nil))
  10081. (aset org-olpa level heading)))
  10082. (let (rtn case-fold-search)
  10083. (save-excursion
  10084. (save-restriction
  10085. (widen)
  10086. (while (org-up-heading-safe)
  10087. (when (looking-at org-complex-heading-regexp)
  10088. (push (org-match-string-no-properties 4) rtn)))
  10089. rtn)))))
  10090. (defun org-format-outline-path (path &optional width prefix separator)
  10091. "Format the outline path PATH for display.
  10092. WIDTH is the maximum number of characters that is available.
  10093. PREFIX is a prefix to be included in the returned string,
  10094. such as the file name.
  10095. SEPARATOR is inserted between the different parts of the path,
  10096. the default is \"/\"."
  10097. (setq width (or width 79))
  10098. (if prefix (setq width (- width (length prefix))))
  10099. (if (not path)
  10100. (or prefix "")
  10101. (let* ((nsteps (length path))
  10102. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  10103. (maxwidth (if (<= total-width width)
  10104. 10000 ;; everything fits
  10105. ;; we need to shorten the level headings
  10106. (/ (- width nsteps) nsteps)))
  10107. (org-odd-levels-only nil)
  10108. (n 0)
  10109. (total (1+ (length prefix))))
  10110. (setq maxwidth (max maxwidth 10))
  10111. (concat prefix
  10112. (if prefix (or separator "/"))
  10113. (mapconcat
  10114. (lambda (h)
  10115. (setq n (1+ n))
  10116. (if (and (= n nsteps) (< maxwidth 10000))
  10117. (setq maxwidth (- total-width total)))
  10118. (if (< (length h) maxwidth)
  10119. (progn (setq total (+ total (length h) 1)) h)
  10120. (setq h (substring h 0 (- maxwidth 2))
  10121. total (+ total maxwidth 1))
  10122. (if (string-match "[ \t]+\\'" h)
  10123. (setq h (substring h 0 (match-beginning 0))))
  10124. (setq h (concat h "..")))
  10125. (org-add-props h nil 'face
  10126. (nth (% (1- n) org-n-level-faces)
  10127. org-level-faces))
  10128. h)
  10129. path (or separator "/"))))))
  10130. (defun org-display-outline-path (&optional file current separator just-return-string)
  10131. "Display the current outline path in the echo area.
  10132. If FILE is non-nil, prepend the output with the file name.
  10133. If CURRENT is non-nil, append the current heading to the output.
  10134. SEPARATOR is passed through to `org-format-outline-path'. It separates
  10135. the different parts of the path and defaults to \"/\".
  10136. If JUST-RETURN-STRING is non-nil, return a string, don't display a message."
  10137. (interactive "P")
  10138. (let* (case-fold-search
  10139. message-log-max ; Don't populate the *Messages* buffer
  10140. (bfn (buffer-file-name (buffer-base-buffer)))
  10141. (path (and (derived-mode-p 'org-mode) (org-get-outline-path)))
  10142. res)
  10143. (if current (setq path (append path
  10144. (save-excursion
  10145. (org-back-to-heading t)
  10146. (if (looking-at org-complex-heading-regexp)
  10147. (list (match-string 4)))))))
  10148. (setq res
  10149. (org-format-outline-path
  10150. path
  10151. (1- (frame-width))
  10152. (and file bfn (concat (file-name-nondirectory bfn) separator))
  10153. separator))
  10154. (if just-return-string
  10155. (org-no-properties res)
  10156. (message "%s" res))))
  10157. (defvar org-refile-history nil
  10158. "History for refiling operations.")
  10159. (defvar org-after-refile-insert-hook nil
  10160. "Hook run after `org-refile' has inserted its stuff at the new location.
  10161. Note that this is still *before* the stuff will be removed from
  10162. the *old* location.")
  10163. (defvar org-capture-last-stored-marker)
  10164. (defvar org-refile-keep nil
  10165. "Non-nil means `org-refile' will copy instead of refile.")
  10166. (defun org-copy ()
  10167. "Like `org-refile', but copy."
  10168. (interactive)
  10169. (let ((org-refile-keep t))
  10170. (funcall 'org-refile nil nil nil "Copy")))
  10171. (defun org-refile (&optional goto default-buffer rfloc msg)
  10172. "Move the entry or entries at point to another heading.
  10173. The list of target headings is compiled using the information in
  10174. `org-refile-targets', which see.
  10175. At the target location, the entry is filed as a subitem of the target
  10176. heading. Depending on `org-reverse-note-order', the new subitem will
  10177. either be the first or the last subitem.
  10178. If there is an active region, all entries in that region will be moved.
  10179. However, the region must fulfill the requirement that the first heading
  10180. is the first one sets the top-level of the moved text - at most siblings
  10181. below it are allowed.
  10182. With prefix arg GOTO, the command will only visit the target location
  10183. and not actually move anything.
  10184. With a double prefix arg \\[universal-argument] \\[universal-argument], \
  10185. go to the location where the last refiling operation has put the subtree.
  10186. With a prefix argument of `2', refile to the running clock.
  10187. RFLOC can be a refile location obtained in a different way.
  10188. MSG is a string to replace \"Refile\" in the default prompt with
  10189. another verb. E.g. `org-copy' sets this parameter to \"Copy\".
  10190. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  10191. If you are using target caching (see `org-refile-use-cache'),
  10192. you have to clear the target cache in order to find new targets.
  10193. This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
  10194. prefix argument (`C-u C-u C-u C-c C-w')."
  10195. (interactive "P")
  10196. (if (member goto '(0 (64)))
  10197. (org-refile-cache-clear)
  10198. (let* ((actionmsg (or msg "Refile"))
  10199. (cbuf (current-buffer))
  10200. (regionp (org-region-active-p))
  10201. (region-start (and regionp (region-beginning)))
  10202. (region-end (and regionp (region-end)))
  10203. (region-length (and regionp (- region-end region-start)))
  10204. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  10205. pos it nbuf file re level reversed)
  10206. (setq last-command nil)
  10207. (when regionp
  10208. (goto-char region-start)
  10209. (or (bolp) (goto-char (point-at-bol)))
  10210. (setq region-start (point))
  10211. (unless (or (org-kill-is-subtree-p
  10212. (buffer-substring region-start region-end))
  10213. (prog1 org-refile-active-region-within-subtree
  10214. (org-toggle-heading)))
  10215. (error "The region is not a (sequence of) subtree(s)")))
  10216. (if (equal goto '(16))
  10217. (org-refile-goto-last-stored)
  10218. (when (or
  10219. (and (equal goto 2)
  10220. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  10221. (prog1
  10222. (setq it (list (or org-clock-heading "running clock")
  10223. (buffer-file-name
  10224. (marker-buffer org-clock-hd-marker))
  10225. ""
  10226. (marker-position org-clock-hd-marker)))
  10227. (setq goto nil)))
  10228. (setq it (or rfloc
  10229. (let (heading-text)
  10230. (save-excursion
  10231. (unless goto
  10232. (org-back-to-heading t)
  10233. (setq heading-text
  10234. (nth 4 (org-heading-components))))
  10235. (org-refile-get-location
  10236. (cond (goto "Goto")
  10237. (regionp (concat actionmsg " region to"))
  10238. (t (concat actionmsg " subtree \""
  10239. heading-text "\" to")))
  10240. default-buffer
  10241. (and (not (equal '(4) goto))
  10242. org-refile-allow-creating-parent-nodes)
  10243. goto))))))
  10244. (setq file (nth 1 it)
  10245. re (nth 2 it)
  10246. pos (nth 3 it))
  10247. (if (and (not goto)
  10248. pos
  10249. (equal (buffer-file-name) file)
  10250. (if regionp
  10251. (and (>= pos region-start)
  10252. (<= pos region-end))
  10253. (and (>= pos (point))
  10254. (< pos (save-excursion
  10255. (org-end-of-subtree t t))))))
  10256. (error "Cannot refile to position inside the tree or region"))
  10257. (setq nbuf (or (find-buffer-visiting file)
  10258. (find-file-noselect file)))
  10259. (if goto
  10260. (progn
  10261. (org-pop-to-buffer-same-window nbuf)
  10262. (goto-char pos)
  10263. (org-show-context 'org-goto))
  10264. (if regionp
  10265. (progn
  10266. (org-kill-new (buffer-substring region-start region-end))
  10267. (org-save-markers-in-region region-start region-end))
  10268. (org-copy-subtree 1 nil t))
  10269. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  10270. (find-file-noselect file)))
  10271. (setq reversed (org-notes-order-reversed-p))
  10272. (save-excursion
  10273. (save-restriction
  10274. (widen)
  10275. (if pos
  10276. (progn
  10277. (goto-char pos)
  10278. (looking-at org-outline-regexp)
  10279. (setq level (org-get-valid-level (funcall outline-level) 1))
  10280. (goto-char
  10281. (if reversed
  10282. (or (outline-next-heading) (point-max))
  10283. (or (save-excursion (org-get-next-sibling))
  10284. (org-end-of-subtree t t)
  10285. (point-max)))))
  10286. (setq level 1)
  10287. (if (not reversed)
  10288. (goto-char (point-max))
  10289. (goto-char (point-min))
  10290. (or (outline-next-heading) (goto-char (point-max)))))
  10291. (if (not (bolp)) (newline))
  10292. (org-paste-subtree level)
  10293. (when org-log-refile
  10294. (org-add-log-setup 'refile nil nil 'findpos
  10295. org-log-refile)
  10296. (unless (eq org-log-refile 'note)
  10297. (save-excursion (org-add-log-note))))
  10298. (and org-auto-align-tags
  10299. (let ((org-loop-over-headlines-in-active-region nil))
  10300. (org-set-tags nil t)))
  10301. (with-demoted-errors
  10302. (bookmark-set "org-refile-last-stored"))
  10303. ;; If we are refiling for capture, make sure that the
  10304. ;; last-capture pointers point here
  10305. (when (org-bound-and-true-p org-refile-for-capture)
  10306. (with-demoted-errors
  10307. (bookmark-set "org-capture-last-stored-marker"))
  10308. (move-marker org-capture-last-stored-marker (point)))
  10309. (if (fboundp 'deactivate-mark) (deactivate-mark))
  10310. (run-hooks 'org-after-refile-insert-hook))))
  10311. (unless org-refile-keep
  10312. (if regionp
  10313. (delete-region (point) (+ (point) region-length))
  10314. (org-cut-subtree)))
  10315. (when (featurep 'org-inlinetask)
  10316. (org-inlinetask-remove-END-maybe))
  10317. (setq org-markers-to-move nil)
  10318. (message (concat actionmsg " to \"%s\" in file %s: done") (car it) file)))))))
  10319. (defun org-refile-goto-last-stored ()
  10320. "Go to the location where the last refile was stored."
  10321. (interactive)
  10322. (bookmark-jump "org-refile-last-stored")
  10323. (message "This is the location of the last refile"))
  10324. (defun org-refile-get-location (&optional prompt default-buffer new-nodes
  10325. no-exclude)
  10326. "Prompt the user for a refile location, using PROMPT.
  10327. PROMPT should not be suffixed with a colon and a space, because
  10328. this function appends the default value from
  10329. `org-refile-history' automatically, if that is not empty.
  10330. When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
  10331. this is used for the GOTO interface."
  10332. (let ((org-refile-targets org-refile-targets)
  10333. (org-refile-use-outline-path org-refile-use-outline-path)
  10334. excluded-entries)
  10335. (when (and (derived-mode-p 'org-mode)
  10336. (not org-refile-use-cache)
  10337. (not no-exclude))
  10338. (org-map-tree
  10339. (lambda()
  10340. (setq excluded-entries
  10341. (append excluded-entries (list (org-get-heading t t)))))))
  10342. (setq org-refile-target-table
  10343. (org-refile-get-targets default-buffer excluded-entries)))
  10344. (unless org-refile-target-table
  10345. (error "No refile targets"))
  10346. (let* ((cbuf (current-buffer))
  10347. (partial-completion-mode nil)
  10348. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  10349. (cfunc (if (and org-refile-use-outline-path
  10350. org-outline-path-complete-in-steps)
  10351. 'org-olpath-completing-read
  10352. 'org-icompleting-read))
  10353. (extra (if org-refile-use-outline-path "/" ""))
  10354. (cbnex (concat (buffer-name) extra))
  10355. (filename (and cfn (expand-file-name cfn)))
  10356. (tbl (mapcar
  10357. (lambda (x)
  10358. (if (and (not (member org-refile-use-outline-path
  10359. '(file full-file-path)))
  10360. (not (equal filename (nth 1 x))))
  10361. (cons (concat (car x) extra " ("
  10362. (file-name-nondirectory (nth 1 x)) ")")
  10363. (cdr x))
  10364. (cons (concat (car x) extra) (cdr x))))
  10365. org-refile-target-table))
  10366. (completion-ignore-case t)
  10367. cdef
  10368. (prompt (concat prompt
  10369. (or (and (car org-refile-history)
  10370. (concat " (default " (car org-refile-history) ")"))
  10371. (and (assoc cbnex tbl) (setq cdef cbnex)
  10372. (concat " (default " cbnex ")"))) ": "))
  10373. pa answ parent-target child parent old-hist)
  10374. (setq old-hist org-refile-history)
  10375. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  10376. nil 'org-refile-history (or cdef (car org-refile-history))))
  10377. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  10378. (org-refile-check-position pa)
  10379. (if pa
  10380. (progn
  10381. (when (or (not org-refile-history)
  10382. (not (eq old-hist org-refile-history))
  10383. (not (equal (car pa) (car org-refile-history))))
  10384. (setq org-refile-history
  10385. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  10386. org-refile-history
  10387. (cdr org-refile-history))))
  10388. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  10389. (pop org-refile-history)))
  10390. pa)
  10391. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  10392. (progn
  10393. (setq parent (match-string 1 answ)
  10394. child (match-string 2 answ))
  10395. (setq parent-target (or (assoc parent tbl)
  10396. (assoc (concat parent "/") tbl)))
  10397. (when (and parent-target
  10398. (or (eq new-nodes t)
  10399. (and (eq new-nodes 'confirm)
  10400. (y-or-n-p (format "Create new node \"%s\"? "
  10401. child)))))
  10402. (org-refile-new-child parent-target child)))
  10403. (error "Invalid target location")))))
  10404. (declare-function org-string-nw-p "org-macs" (s))
  10405. (defun org-refile-check-position (refile-pointer)
  10406. "Check if the refile pointer matches the headline to which it points."
  10407. (let* ((file (nth 1 refile-pointer))
  10408. (re (nth 2 refile-pointer))
  10409. (pos (nth 3 refile-pointer))
  10410. buffer)
  10411. (if (and (not (markerp pos)) (not file))
  10412. (error "Please save the buffer to a file before refiling")
  10413. (when (org-string-nw-p re)
  10414. (setq buffer (if (markerp pos)
  10415. (marker-buffer pos)
  10416. (or (find-buffer-visiting file)
  10417. (find-file-noselect file))))
  10418. (with-current-buffer buffer
  10419. (save-excursion
  10420. (save-restriction
  10421. (widen)
  10422. (goto-char pos)
  10423. (beginning-of-line 1)
  10424. (unless (org-looking-at-p re)
  10425. (error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling")))))))))
  10426. (defun org-refile-new-child (parent-target child)
  10427. "Use refile target PARENT-TARGET to add new CHILD below it."
  10428. (unless parent-target
  10429. (error "Cannot find parent for new node"))
  10430. (let ((file (nth 1 parent-target))
  10431. (pos (nth 3 parent-target))
  10432. level)
  10433. (with-current-buffer (or (find-buffer-visiting file)
  10434. (find-file-noselect file))
  10435. (save-excursion
  10436. (save-restriction
  10437. (widen)
  10438. (if pos
  10439. (goto-char pos)
  10440. (goto-char (point-max))
  10441. (if (not (bolp)) (newline)))
  10442. (when (looking-at org-outline-regexp)
  10443. (setq level (funcall outline-level))
  10444. (org-end-of-subtree t t))
  10445. (org-back-over-empty-lines)
  10446. (insert "\n" (make-string
  10447. (if pos (org-get-valid-level level 1) 1) ?*)
  10448. " " child "\n")
  10449. (beginning-of-line 0)
  10450. (list (concat (car parent-target) "/" child) file "" (point)))))))
  10451. (defun org-olpath-completing-read (prompt collection &rest args)
  10452. "Read an outline path like a file name."
  10453. (let ((thetable collection)
  10454. (org-completion-use-ido nil) ; does not work with ido.
  10455. (org-completion-use-iswitchb nil)) ; or iswitchb
  10456. (apply
  10457. 'org-icompleting-read prompt
  10458. (lambda (string predicate &optional flag)
  10459. (let (rtn r f (l (length string)))
  10460. (cond
  10461. ((eq flag nil)
  10462. ;; try completion
  10463. (try-completion string thetable))
  10464. ((eq flag t)
  10465. ;; all-completions
  10466. (setq rtn (all-completions string thetable predicate))
  10467. (mapcar
  10468. (lambda (x)
  10469. (setq r (substring x l))
  10470. (if (string-match " ([^)]*)$" x)
  10471. (setq f (match-string 0 x))
  10472. (setq f ""))
  10473. (if (string-match "/" r)
  10474. (concat string (substring r 0 (match-end 0)) f)
  10475. x))
  10476. rtn))
  10477. ((eq flag 'lambda)
  10478. ;; exact match?
  10479. (assoc string thetable)))))
  10480. args)))
  10481. ;;;; Dynamic blocks
  10482. (defun org-find-dblock (name)
  10483. "Find the first dynamic block with name NAME in the buffer.
  10484. If not found, stay at current position and return nil."
  10485. (let ((case-fold-search t) pos)
  10486. (save-excursion
  10487. (goto-char (point-min))
  10488. (setq pos (and (re-search-forward
  10489. (concat "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+" name "\\>") nil t)
  10490. (match-beginning 0))))
  10491. (if pos (goto-char pos))
  10492. pos))
  10493. (defconst org-dblock-start-re
  10494. "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  10495. "Matches the start line of a dynamic block, with parameters.")
  10496. (defconst org-dblock-end-re "^[ \t]*#\\+\\(?:END\\|end\\)\\([: \t\r\n]\\|$\\)"
  10497. "Matches the end of a dynamic block.")
  10498. (defun org-create-dblock (plist)
  10499. "Create a dynamic block section, with parameters taken from PLIST.
  10500. PLIST must contain a :name entry which is used as name of the block."
  10501. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  10502. (end-of-line 1)
  10503. (newline))
  10504. (let ((col (current-column))
  10505. (name (plist-get plist :name)))
  10506. (insert "#+BEGIN: " name)
  10507. (while plist
  10508. (if (eq (car plist) :name)
  10509. (setq plist (cddr plist))
  10510. (insert " " (prin1-to-string (pop plist)))))
  10511. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  10512. (beginning-of-line -2)))
  10513. (defun org-prepare-dblock ()
  10514. "Prepare dynamic block for refresh.
  10515. This empties the block, puts the cursor at the insert position and returns
  10516. the property list including an extra property :name with the block name."
  10517. (unless (looking-at org-dblock-start-re)
  10518. (error "Not at a dynamic block"))
  10519. (let* ((begdel (1+ (match-end 0)))
  10520. (name (org-no-properties (match-string 1)))
  10521. (params (append (list :name name)
  10522. (read (concat "(" (match-string 3) ")")))))
  10523. (save-excursion
  10524. (beginning-of-line 1)
  10525. (skip-chars-forward " \t")
  10526. (setq params (plist-put params :indentation-column (current-column))))
  10527. (unless (re-search-forward org-dblock-end-re nil t)
  10528. (error "Dynamic block not terminated"))
  10529. (setq params
  10530. (append params
  10531. (list :content (buffer-substring
  10532. begdel (match-beginning 0)))))
  10533. (delete-region begdel (match-beginning 0))
  10534. (goto-char begdel)
  10535. (open-line 1)
  10536. params))
  10537. (defun org-map-dblocks (&optional command)
  10538. "Apply COMMAND to all dynamic blocks in the current buffer.
  10539. If COMMAND is not given, use `org-update-dblock'."
  10540. (let ((cmd (or command 'org-update-dblock)))
  10541. (save-excursion
  10542. (goto-char (point-min))
  10543. (while (re-search-forward org-dblock-start-re nil t)
  10544. (goto-char (match-beginning 0))
  10545. (save-excursion
  10546. (condition-case nil
  10547. (funcall cmd)
  10548. (error (message "Error during update of dynamic block"))))
  10549. (unless (re-search-forward org-dblock-end-re nil t)
  10550. (error "Dynamic block not terminated"))))))
  10551. (defun org-dblock-update (&optional arg)
  10552. "User command for updating dynamic blocks.
  10553. Update the dynamic block at point. With prefix ARG, update all dynamic
  10554. blocks in the buffer."
  10555. (interactive "P")
  10556. (if arg
  10557. (org-update-all-dblocks)
  10558. (or (looking-at org-dblock-start-re)
  10559. (org-beginning-of-dblock))
  10560. (org-update-dblock)))
  10561. (defun org-update-dblock ()
  10562. "Update the dynamic block at point.
  10563. This means to empty the block, parse for parameters and then call
  10564. the correct writing function."
  10565. (interactive)
  10566. (save-window-excursion
  10567. (let* ((pos (point))
  10568. (line (org-current-line))
  10569. (params (org-prepare-dblock))
  10570. (name (plist-get params :name))
  10571. (indent (plist-get params :indentation-column))
  10572. (cmd (intern (concat "org-dblock-write:" name))))
  10573. (message "Updating dynamic block `%s' at line %d..." name line)
  10574. (funcall cmd params)
  10575. (message "Updating dynamic block `%s' at line %d...done" name line)
  10576. (goto-char pos)
  10577. (when (and indent (> indent 0))
  10578. (setq indent (make-string indent ?\ ))
  10579. (save-excursion
  10580. (org-beginning-of-dblock)
  10581. (forward-line 1)
  10582. (while (not (looking-at org-dblock-end-re))
  10583. (insert indent)
  10584. (beginning-of-line 2))
  10585. (when (looking-at org-dblock-end-re)
  10586. (and (looking-at "[ \t]+")
  10587. (replace-match ""))
  10588. (insert indent)))))))
  10589. (defun org-beginning-of-dblock ()
  10590. "Find the beginning of the dynamic block at point.
  10591. Error if there is no such block at point."
  10592. (let ((pos (point))
  10593. beg)
  10594. (end-of-line 1)
  10595. (if (and (re-search-backward org-dblock-start-re nil t)
  10596. (setq beg (match-beginning 0))
  10597. (re-search-forward org-dblock-end-re nil t)
  10598. (> (match-end 0) pos))
  10599. (goto-char beg)
  10600. (goto-char pos)
  10601. (error "Not in a dynamic block"))))
  10602. (defun org-update-all-dblocks ()
  10603. "Update all dynamic blocks in the buffer.
  10604. This function can be used in a hook."
  10605. (interactive)
  10606. (when (derived-mode-p 'org-mode)
  10607. (org-map-dblocks 'org-update-dblock)))
  10608. ;;;; Completion
  10609. (defun org-get-export-keywords ()
  10610. "Return a list of all currently understood export keywords.
  10611. Export keywords include options, block names, attributes and
  10612. keywords relative to each registered export back-end."
  10613. (delq nil
  10614. (let (keywords)
  10615. (mapc
  10616. (lambda (back-end)
  10617. (let ((props (cdr back-end)))
  10618. ;; Back-end name (for keywords, like #+LATEX:)
  10619. (push (upcase (symbol-name (car back-end))) keywords)
  10620. ;; Back-end options.
  10621. (mapc (lambda (option) (push (cadr option) keywords))
  10622. (plist-get (cdr back-end) :options-alist))))
  10623. (org-bound-and-true-p org-export-registered-backends))
  10624. keywords)))
  10625. (defconst org-options-keywords
  10626. '("ARCHIVE:" "AUTHOR:" "BIND:" "CATEGORY:" "COLUMNS:" "CREATOR:" "DATE:"
  10627. "DESCRIPTION:" "DRAWERS:" "EMAIL:" "EXCLUDE_TAGS:" "FILETAGS:" "INCLUDE:"
  10628. "INDEX:" "KEYWORDS:" "LANGUAGE:" "MACRO:" "OPTIONS:" "PROPERTY:"
  10629. "PRIORITIES:" "SELECT_TAGS:" "SEQ_TODO:" "SETUPFILE:" "STARTUP:" "TAGS:"
  10630. "TITLE:" "TODO:" "TYP_TODO:" "SELECT_TAGS:" "EXCLUDE_TAGS:" "INFOJS_OPT:"))
  10631. (defcustom org-structure-template-alist
  10632. '(("s" "#+BEGIN_SRC ?\n\n#+END_SRC"
  10633. "<src lang=\"?\">\n\n</src>")
  10634. ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE"
  10635. "<example>\n?\n</example>")
  10636. ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE"
  10637. "<quote>\n?\n</quote>")
  10638. ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE"
  10639. "<verse>\n?\n</verse>")
  10640. ("V" "#+BEGIN_VERBATIM\n?\n#+END_VERBATIM"
  10641. "<verbatim>\n?\n</verbatim>")
  10642. ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER"
  10643. "<center>\n?\n</center>")
  10644. ("l" "#+BEGIN_LaTeX\n?\n#+END_LaTeX"
  10645. "<literal style=\"latex\">\n?\n</literal>")
  10646. ("L" "#+LaTeX: "
  10647. "<literal style=\"latex\">?</literal>")
  10648. ("h" "#+BEGIN_HTML\n?\n#+END_HTML"
  10649. "<literal style=\"html\">\n?\n</literal>")
  10650. ("H" "#+HTML: "
  10651. "<literal style=\"html\">?</literal>")
  10652. ("a" "#+BEGIN_ASCII\n?\n#+END_ASCII")
  10653. ("A" "#+ASCII: ")
  10654. ("i" "#+INDEX: ?"
  10655. "#+INDEX: ?")
  10656. ("I" "#+INCLUDE: %file ?"
  10657. "<include file=%file markup=\"?\">"))
  10658. "Structure completion elements.
  10659. This is a list of abbreviation keys and values. The value gets inserted
  10660. if you type `<' followed by the key and then press the completion key,
  10661. usually `M-TAB'. %file will be replaced by a file name after prompting
  10662. for the file using completion. The cursor will be placed at the position
  10663. of the `?` in the template.
  10664. There are two templates for each key, the first uses the original Org syntax,
  10665. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  10666. the default when the /org-mtags.el/ module has been loaded. See also the
  10667. variable `org-mtags-prefer-muse-templates'."
  10668. :group 'org-completion
  10669. :type '(repeat
  10670. (string :tag "Key")
  10671. (string :tag "Template")
  10672. (string :tag "Muse Template")))
  10673. (defun org-try-structure-completion ()
  10674. "Try to complete a structure template before point.
  10675. This looks for strings like \"<e\" on an otherwise empty line and
  10676. expands them."
  10677. (let ((l (buffer-substring (point-at-bol) (point)))
  10678. a)
  10679. (when (and (looking-at "[ \t]*$")
  10680. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  10681. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  10682. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  10683. (match-beginning 1)) a)
  10684. t)))
  10685. (defun org-complete-expand-structure-template (start cell)
  10686. "Expand a structure template."
  10687. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  10688. (rpl (nth (if musep 2 1) cell))
  10689. (ind ""))
  10690. (delete-region start (point))
  10691. (when (string-match "\\`#\\+" rpl)
  10692. (cond
  10693. ((bolp))
  10694. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  10695. (setq ind (buffer-substring (point-at-bol) (point))))
  10696. (t (newline))))
  10697. (setq start (point))
  10698. (if (string-match "%file" rpl)
  10699. (setq rpl (replace-match
  10700. (concat
  10701. "\""
  10702. (save-match-data
  10703. (abbreviate-file-name (read-file-name "Include file: ")))
  10704. "\"")
  10705. t t rpl)))
  10706. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  10707. (concat "\n" ind)))
  10708. (insert rpl)
  10709. (if (re-search-backward "\\?" start t) (delete-char 1))))
  10710. ;;;; TODO, DEADLINE, Comments
  10711. (defun org-toggle-comment ()
  10712. "Change the COMMENT state of an entry."
  10713. (interactive)
  10714. (save-excursion
  10715. (org-back-to-heading)
  10716. (let (case-fold-search)
  10717. (cond
  10718. ((looking-at (format org-heading-keyword-regexp-format
  10719. org-comment-string))
  10720. (goto-char (match-end 1))
  10721. (looking-at (concat " +" org-comment-string))
  10722. (replace-match "" t t)
  10723. (when (eolp) (insert " ")))
  10724. ((looking-at org-outline-regexp)
  10725. (goto-char (match-end 0))
  10726. (insert org-comment-string " "))))))
  10727. (defvar org-last-todo-state-is-todo nil
  10728. "This is non-nil when the last TODO state change led to a TODO state.
  10729. If the last change removed the TODO tag or switched to DONE, then
  10730. this is nil.")
  10731. (defvar org-setting-tags nil) ; dynamically skipped
  10732. (defvar org-todo-setup-filter-hook nil
  10733. "Hook for functions that pre-filter todo specs.
  10734. Each function takes a todo spec and returns either nil or the spec
  10735. transformed into canonical form." )
  10736. (defvar org-todo-get-default-hook nil
  10737. "Hook for functions that get a default item for todo.
  10738. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  10739. nil or a string to be used for the todo mark." )
  10740. (defvar org-agenda-headline-snapshot-before-repeat)
  10741. (defun org-current-effective-time ()
  10742. "Return current time adjusted for `org-extend-today-until' variable."
  10743. (let* ((ct (org-current-time))
  10744. (dct (decode-time ct))
  10745. (ct1
  10746. (cond
  10747. (org-use-last-clock-out-time-as-effective-time
  10748. (or (org-clock-get-last-clock-out-time) ct))
  10749. ((and org-use-effective-time (< (nth 2 dct) org-extend-today-until))
  10750. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct)))
  10751. (t ct))))
  10752. ct1))
  10753. (defun org-todo-yesterday (&optional arg)
  10754. "Like `org-todo' but the time of change will be 23:59 of yesterday."
  10755. (interactive "P")
  10756. (if (eq major-mode 'org-agenda-mode)
  10757. (apply 'org-agenda-todo-yesterday arg)
  10758. (let* ((hour (third (decode-time
  10759. (org-current-time))))
  10760. (org-extend-today-until (1+ hour)))
  10761. (org-todo arg))))
  10762. (defvar org-block-entry-blocking ""
  10763. "First entry preventing the TODO state change.")
  10764. (defun org-todo (&optional arg)
  10765. "Change the TODO state of an item.
  10766. The state of an item is given by a keyword at the start of the heading,
  10767. like
  10768. *** TODO Write paper
  10769. *** DONE Call mom
  10770. The different keywords are specified in the variable `org-todo-keywords'.
  10771. By default the available states are \"TODO\" and \"DONE\".
  10772. So for this example: when the item starts with TODO, it is changed to DONE.
  10773. When it starts with DONE, the DONE is removed. And when neither TODO nor
  10774. DONE are present, add TODO at the beginning of the heading.
  10775. With \\[universal-argument] prefix arg, use completion to determine the new \
  10776. state.
  10777. With numeric prefix arg, switch to that state.
  10778. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  10779. keywords (nextset).
  10780. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  10781. With a numeric prefix arg of 0, inhibit note taking for the change.
  10782. For calling through lisp, arg is also interpreted in the following way:
  10783. 'none -> empty state
  10784. \"\"(empty string) -> switch to empty state
  10785. 'done -> switch to DONE
  10786. 'nextset -> switch to the next set of keywords
  10787. 'previousset -> switch to the previous set of keywords
  10788. \"WAITING\" -> switch to the specified keyword, but only if it
  10789. really is a member of `org-todo-keywords'."
  10790. (interactive "P")
  10791. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  10792. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  10793. 'region-start-level 'region))
  10794. org-loop-over-headlines-in-active-region)
  10795. (org-map-entries
  10796. `(org-todo ,arg)
  10797. org-loop-over-headlines-in-active-region
  10798. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  10799. (if (equal arg '(16)) (setq arg 'nextset))
  10800. (let ((org-blocker-hook org-blocker-hook)
  10801. commentp
  10802. case-fold-search)
  10803. (when (equal arg '(64))
  10804. (setq arg nil org-blocker-hook nil))
  10805. (when (and org-blocker-hook
  10806. (or org-inhibit-blocking
  10807. (org-entry-get nil "NOBLOCKING")))
  10808. (setq org-blocker-hook nil))
  10809. (save-excursion
  10810. (catch 'exit
  10811. (org-back-to-heading t)
  10812. (when (looking-at (concat "^\\*+ " org-comment-string))
  10813. (org-toggle-comment)
  10814. (setq commentp t))
  10815. (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
  10816. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
  10817. (looking-at "\\(?: *\\|[ \t]*$\\)"))
  10818. (let* ((match-data (match-data))
  10819. (startpos (point-at-bol))
  10820. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  10821. (org-log-done org-log-done)
  10822. (org-log-repeat org-log-repeat)
  10823. (org-todo-log-states org-todo-log-states)
  10824. (org-inhibit-logging
  10825. (if (equal arg 0)
  10826. (progn (setq arg nil) 'note) org-inhibit-logging))
  10827. (this (match-string 1))
  10828. (hl-pos (match-beginning 0))
  10829. (head (org-get-todo-sequence-head this))
  10830. (ass (assoc head org-todo-kwd-alist))
  10831. (interpret (nth 1 ass))
  10832. (done-word (nth 3 ass))
  10833. (final-done-word (nth 4 ass))
  10834. (org-last-state (or this ""))
  10835. (completion-ignore-case t)
  10836. (member (member this org-todo-keywords-1))
  10837. (tail (cdr member))
  10838. (org-state (cond
  10839. ((and org-todo-key-trigger
  10840. (or (and (equal arg '(4))
  10841. (eq org-use-fast-todo-selection 'prefix))
  10842. (and (not arg) org-use-fast-todo-selection
  10843. (not (eq org-use-fast-todo-selection
  10844. 'prefix)))))
  10845. ;; Use fast selection
  10846. (org-fast-todo-selection))
  10847. ((and (equal arg '(4))
  10848. (or (not org-use-fast-todo-selection)
  10849. (not org-todo-key-trigger)))
  10850. ;; Read a state with completion
  10851. (org-icompleting-read
  10852. "State: " (mapcar (lambda(x) (list x))
  10853. org-todo-keywords-1)
  10854. nil t))
  10855. ((eq arg 'right)
  10856. (if this
  10857. (if tail (car tail) nil)
  10858. (car org-todo-keywords-1)))
  10859. ((eq arg 'left)
  10860. (if (equal member org-todo-keywords-1)
  10861. nil
  10862. (if this
  10863. (nth (- (length org-todo-keywords-1)
  10864. (length tail) 2)
  10865. org-todo-keywords-1)
  10866. (org-last org-todo-keywords-1))))
  10867. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  10868. (setq arg nil))) ; hack to fall back to cycling
  10869. (arg
  10870. ;; user or caller requests a specific state
  10871. (cond
  10872. ((equal arg "") nil)
  10873. ((eq arg 'none) nil)
  10874. ((eq arg 'done) (or done-word (car org-done-keywords)))
  10875. ((eq arg 'nextset)
  10876. (or (car (cdr (member head org-todo-heads)))
  10877. (car org-todo-heads)))
  10878. ((eq arg 'previousset)
  10879. (let ((org-todo-heads (reverse org-todo-heads)))
  10880. (or (car (cdr (member head org-todo-heads)))
  10881. (car org-todo-heads))))
  10882. ((car (member arg org-todo-keywords-1)))
  10883. ((stringp arg)
  10884. (error "State `%s' not valid in this file" arg))
  10885. ((nth (1- (prefix-numeric-value arg))
  10886. org-todo-keywords-1))))
  10887. ((null member) (or head (car org-todo-keywords-1)))
  10888. ((equal this final-done-word) nil) ;; -> make empty
  10889. ((null tail) nil) ;; -> first entry
  10890. ((memq interpret '(type priority))
  10891. (if (eq this-command last-command)
  10892. (car tail)
  10893. (if (> (length tail) 0)
  10894. (or done-word (car org-done-keywords))
  10895. nil)))
  10896. (t
  10897. (car tail))))
  10898. (org-state (or
  10899. (run-hook-with-args-until-success
  10900. 'org-todo-get-default-hook org-state org-last-state)
  10901. org-state))
  10902. (next (if org-state (concat " " org-state " ") " "))
  10903. (change-plist (list :type 'todo-state-change :from this :to org-state
  10904. :position startpos))
  10905. dolog now-done-p)
  10906. (when org-blocker-hook
  10907. (setq org-last-todo-state-is-todo
  10908. (not (member this org-done-keywords)))
  10909. (unless (save-excursion
  10910. (save-match-data
  10911. (org-with-wide-buffer
  10912. (run-hook-with-args-until-failure
  10913. 'org-blocker-hook change-plist))))
  10914. (if (org-called-interactively-p 'interactive)
  10915. (user-error "TODO state change from %s to %s blocked (by \"%s\")"
  10916. this org-state org-block-entry-blocking)
  10917. ;; fail silently
  10918. (message "TODO state change from %s to %s blocked (by \"%s\")"
  10919. this org-state org-block-entry-blocking)
  10920. (throw 'exit nil))))
  10921. (store-match-data match-data)
  10922. (replace-match next t t)
  10923. (unless (pos-visible-in-window-p hl-pos)
  10924. (message "TODO state changed to %s" (org-trim next)))
  10925. (unless head
  10926. (setq head (org-get-todo-sequence-head org-state)
  10927. ass (assoc head org-todo-kwd-alist)
  10928. interpret (nth 1 ass)
  10929. done-word (nth 3 ass)
  10930. final-done-word (nth 4 ass)))
  10931. (when (memq arg '(nextset previousset))
  10932. (message "Keyword-Set %d/%d: %s"
  10933. (- (length org-todo-sets) -1
  10934. (length (memq (assoc org-state org-todo-sets) org-todo-sets)))
  10935. (length org-todo-sets)
  10936. (mapconcat 'identity (assoc org-state org-todo-sets) " ")))
  10937. (setq org-last-todo-state-is-todo
  10938. (not (member org-state org-done-keywords)))
  10939. (setq now-done-p (and (member org-state org-done-keywords)
  10940. (not (member this org-done-keywords))))
  10941. (and logging (org-local-logging logging))
  10942. (when (and (or org-todo-log-states org-log-done)
  10943. (not (eq org-inhibit-logging t))
  10944. (not (memq arg '(nextset previousset))))
  10945. ;; we need to look at recording a time and note
  10946. (setq dolog (or (nth 1 (assoc org-state org-todo-log-states))
  10947. (nth 2 (assoc this org-todo-log-states))))
  10948. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  10949. (setq dolog 'time))
  10950. (when (and org-state
  10951. (member org-state org-not-done-keywords)
  10952. (not (member this org-not-done-keywords)))
  10953. ;; This is now a todo state and was not one before
  10954. ;; If there was a CLOSED time stamp, get rid of it.
  10955. (org-add-planning-info nil nil 'closed))
  10956. (when (and now-done-p org-log-done)
  10957. ;; It is now done, and it was not done before
  10958. (org-add-planning-info 'closed (org-current-effective-time))
  10959. (if (and (not dolog) (eq 'note org-log-done))
  10960. (org-add-log-setup 'done org-state this 'findpos 'note)))
  10961. (when (and org-state dolog)
  10962. ;; This is a non-nil state, and we need to log it
  10963. (org-add-log-setup 'state org-state this 'findpos dolog)))
  10964. ;; Fixup tag positioning
  10965. (org-todo-trigger-tag-changes org-state)
  10966. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  10967. (when org-provide-todo-statistics
  10968. (org-update-parent-todo-statistics))
  10969. (run-hooks 'org-after-todo-state-change-hook)
  10970. (if (and arg (not (member org-state org-done-keywords)))
  10971. (setq head (org-get-todo-sequence-head org-state)))
  10972. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  10973. ;; Do we need to trigger a repeat?
  10974. (when now-done-p
  10975. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  10976. ;; This is for the agenda, take a snapshot of the headline.
  10977. (save-match-data
  10978. (setq org-agenda-headline-snapshot-before-repeat
  10979. (org-get-heading))))
  10980. (org-auto-repeat-maybe org-state))
  10981. ;; Fixup cursor location if close to the keyword
  10982. (if (and (outline-on-heading-p)
  10983. (not (bolp))
  10984. (save-excursion (beginning-of-line 1)
  10985. (looking-at org-todo-line-regexp))
  10986. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  10987. (progn
  10988. (goto-char (or (match-end 2) (match-end 1)))
  10989. (and (looking-at " ") (just-one-space))))
  10990. (when org-trigger-hook
  10991. (save-excursion
  10992. (run-hook-with-args 'org-trigger-hook change-plist)))
  10993. (when commentp (org-toggle-comment))))))))
  10994. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  10995. "Block turning an entry into a TODO, using the hierarchy.
  10996. This checks whether the current task should be blocked from state
  10997. changes. Such blocking occurs when:
  10998. 1. The task has children which are not all in a completed state.
  10999. 2. A task has a parent with the property :ORDERED:, and there
  11000. are siblings prior to the current task with incomplete
  11001. status.
  11002. 3. The parent of the task is blocked because it has siblings that should
  11003. be done first, or is child of a block grandparent TODO entry."
  11004. (if (not org-enforce-todo-dependencies)
  11005. t ; if locally turned off don't block
  11006. (catch 'dont-block
  11007. ;; If this is not a todo state change, or if this entry is already DONE,
  11008. ;; do not block
  11009. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11010. (member (plist-get change-plist :from)
  11011. (cons 'done org-done-keywords))
  11012. (member (plist-get change-plist :to)
  11013. (cons 'todo org-not-done-keywords))
  11014. (not (plist-get change-plist :to)))
  11015. (throw 'dont-block t))
  11016. ;; If this task has children, and any are undone, it's blocked
  11017. (save-excursion
  11018. (org-back-to-heading t)
  11019. (let ((this-level (funcall outline-level)))
  11020. (outline-next-heading)
  11021. (let ((child-level (funcall outline-level)))
  11022. (while (and (not (eobp))
  11023. (> child-level this-level))
  11024. ;; this todo has children, check whether they are all
  11025. ;; completed
  11026. (if (and (not (org-entry-is-done-p))
  11027. (org-entry-is-todo-p))
  11028. (progn (setq org-block-entry-blocking (org-get-heading))
  11029. (throw 'dont-block nil)))
  11030. (outline-next-heading)
  11031. (setq child-level (funcall outline-level))))))
  11032. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  11033. ;; any previous siblings are undone, it's blocked
  11034. (save-excursion
  11035. (org-back-to-heading t)
  11036. (let* ((pos (point))
  11037. (parent-pos (and (org-up-heading-safe) (point))))
  11038. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  11039. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11040. (forward-line 1)
  11041. (re-search-forward org-not-done-heading-regexp pos t))
  11042. (setq org-block-entry-blocking (match-string 0))
  11043. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  11044. ;; Search further up the hierarchy, to see if an ancestor is blocked
  11045. (while t
  11046. (goto-char parent-pos)
  11047. (if (not (looking-at org-not-done-heading-regexp))
  11048. (throw 'dont-block t)) ; do not block, parent is not a TODO
  11049. (setq pos (point))
  11050. (setq parent-pos (and (org-up-heading-safe) (point)))
  11051. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  11052. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11053. (forward-line 1)
  11054. (re-search-forward org-not-done-heading-regexp pos t)
  11055. (setq org-block-entry-blocking (org-get-heading)))
  11056. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  11057. (defcustom org-track-ordered-property-with-tag nil
  11058. "Should the ORDERED property also be shown as a tag?
  11059. The ORDERED property decides if an entry should require subtasks to be
  11060. completed in sequence. Since a property is not very visible, setting
  11061. this option means that toggling the ORDERED property with the command
  11062. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  11063. not relevant for the behavior, but it makes things more visible.
  11064. Note that toggling the tag with tags commands will not change the property
  11065. and therefore not influence behavior!
  11066. This can be t, meaning the tag ORDERED should be used, It can also be a
  11067. string to select a different tag for this task."
  11068. :group 'org-todo
  11069. :type '(choice
  11070. (const :tag "No tracking" nil)
  11071. (const :tag "Track with ORDERED tag" t)
  11072. (string :tag "Use other tag")))
  11073. (defun org-toggle-ordered-property ()
  11074. "Toggle the ORDERED property of the current entry.
  11075. For better visibility, you can track the value of this property with a tag.
  11076. See variable `org-track-ordered-property-with-tag'."
  11077. (interactive)
  11078. (let* ((t1 org-track-ordered-property-with-tag)
  11079. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  11080. (save-excursion
  11081. (org-back-to-heading)
  11082. (if (org-entry-get nil "ORDERED")
  11083. (progn
  11084. (org-delete-property "ORDERED" "PROPERTIES")
  11085. (and tag (org-toggle-tag tag 'off))
  11086. (message "Subtasks can be completed in arbitrary order"))
  11087. (org-entry-put nil "ORDERED" "t")
  11088. (and tag (org-toggle-tag tag 'on))
  11089. (message "Subtasks must be completed in sequence")))))
  11090. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  11091. (defun org-block-todo-from-checkboxes (change-plist)
  11092. "Block turning an entry into a TODO, using checkboxes.
  11093. This checks whether the current task should be blocked from state
  11094. changes because there are unchecked boxes in this entry."
  11095. (if (not org-enforce-todo-checkbox-dependencies)
  11096. t ; if locally turned off don't block
  11097. (catch 'dont-block
  11098. ;; If this is not a todo state change, or if this entry is already DONE,
  11099. ;; do not block
  11100. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11101. (member (plist-get change-plist :from)
  11102. (cons 'done org-done-keywords))
  11103. (member (plist-get change-plist :to)
  11104. (cons 'todo org-not-done-keywords))
  11105. (not (plist-get change-plist :to)))
  11106. (throw 'dont-block t))
  11107. ;; If this task has checkboxes that are not checked, it's blocked
  11108. (save-excursion
  11109. (org-back-to-heading t)
  11110. (let ((beg (point)) end)
  11111. (outline-next-heading)
  11112. (setq end (point))
  11113. (goto-char beg)
  11114. (if (org-list-search-forward
  11115. (concat (org-item-beginning-re)
  11116. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
  11117. "\\[[- ]\\]")
  11118. end t)
  11119. (progn
  11120. (if (boundp 'org-blocked-by-checkboxes)
  11121. (setq org-blocked-by-checkboxes t))
  11122. (throw 'dont-block nil)))))
  11123. t))) ; do not block
  11124. (defun org-entry-blocked-p ()
  11125. "Is the current entry blocked?"
  11126. (org-with-silent-modifications
  11127. (if (org-entry-get nil "NOBLOCKING")
  11128. nil ;; Never block this entry
  11129. (not (run-hook-with-args-until-failure
  11130. 'org-blocker-hook
  11131. (list :type 'todo-state-change
  11132. :position (point)
  11133. :from 'todo
  11134. :to 'done))))))
  11135. (defun org-update-statistics-cookies (all)
  11136. "Update the statistics cookie, either from TODO or from checkboxes.
  11137. This should be called with the cursor in a line with a statistics cookie."
  11138. (interactive "P")
  11139. (if all
  11140. (progn
  11141. (org-update-checkbox-count 'all)
  11142. (org-map-entries 'org-update-parent-todo-statistics))
  11143. (if (not (org-at-heading-p))
  11144. (org-update-checkbox-count)
  11145. (let ((pos (point-marker))
  11146. end l1 l2)
  11147. (ignore-errors (org-back-to-heading t))
  11148. (if (not (org-at-heading-p))
  11149. (org-update-checkbox-count)
  11150. (setq l1 (org-outline-level))
  11151. (setq end (save-excursion
  11152. (outline-next-heading)
  11153. (if (org-at-heading-p) (setq l2 (org-outline-level)))
  11154. (point)))
  11155. (if (and (save-excursion
  11156. (re-search-forward
  11157. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  11158. (not (save-excursion (re-search-forward
  11159. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  11160. (org-update-checkbox-count)
  11161. (if (and l2 (> l2 l1))
  11162. (progn
  11163. (goto-char end)
  11164. (org-update-parent-todo-statistics))
  11165. (goto-char pos)
  11166. (beginning-of-line 1)
  11167. (while (re-search-forward
  11168. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  11169. (point-at-eol) t)
  11170. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  11171. (goto-char pos)
  11172. (move-marker pos nil)))))
  11173. (defvar org-entry-property-inherited-from) ;; defined below
  11174. (defun org-update-parent-todo-statistics ()
  11175. "Update any statistics cookie in the parent of the current headline.
  11176. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  11177. the entire subtree and this will travel up the hierarchy and update
  11178. statistics everywhere."
  11179. (let* ((prop (save-excursion (org-up-heading-safe)
  11180. (org-entry-get nil "COOKIE_DATA" 'inherit)))
  11181. (recursive (or (not org-hierarchical-todo-statistics)
  11182. (and prop (string-match "\\<recursive\\>" prop))))
  11183. (lim (or (and prop (marker-position org-entry-property-inherited-from))
  11184. 0))
  11185. (first t)
  11186. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  11187. level ltoggle l1 new ndel
  11188. (cnt-all 0) (cnt-done 0) is-percent kwd
  11189. checkbox-beg ov ovs ove cookie-present)
  11190. (catch 'exit
  11191. (save-excursion
  11192. (beginning-of-line 1)
  11193. (setq ltoggle (funcall outline-level))
  11194. ;; Three situations are to consider:
  11195. ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
  11196. ;; to the top-level ancestor on the headline;
  11197. ;; 2. If parent has "recursive" property, repeat up to the
  11198. ;; headline setting that property, taking inheritance into
  11199. ;; account;
  11200. ;; 3. Else, move up to direct parent and proceed only once.
  11201. (while (and (setq level (org-up-heading-safe))
  11202. (or recursive first)
  11203. (>= (point) lim))
  11204. (setq first nil cookie-present nil)
  11205. (unless (and level
  11206. (not (string-match
  11207. "\\<checkbox\\>"
  11208. (downcase (or (org-entry-get nil "COOKIE_DATA")
  11209. "")))))
  11210. (throw 'exit nil))
  11211. (while (re-search-forward box-re (point-at-eol) t)
  11212. (setq cnt-all 0 cnt-done 0 cookie-present t)
  11213. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  11214. (save-match-data
  11215. (unless (outline-next-heading) (throw 'exit nil))
  11216. (while (and (looking-at org-complex-heading-regexp)
  11217. (> (setq l1 (length (match-string 1))) level))
  11218. (setq kwd (and (or recursive (= l1 ltoggle))
  11219. (match-string 2)))
  11220. (if (or (eq org-provide-todo-statistics 'all-headlines)
  11221. (and (listp org-provide-todo-statistics)
  11222. (or (member kwd org-provide-todo-statistics)
  11223. (member kwd org-done-keywords))))
  11224. (setq cnt-all (1+ cnt-all))
  11225. (if (eq org-provide-todo-statistics t)
  11226. (and kwd (setq cnt-all (1+ cnt-all)))))
  11227. (and (member kwd org-done-keywords)
  11228. (setq cnt-done (1+ cnt-done)))
  11229. (outline-next-heading)))
  11230. (setq new
  11231. (if is-percent
  11232. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  11233. (format "[%d/%d]" cnt-done cnt-all))
  11234. ndel (- (match-end 0) checkbox-beg))
  11235. ;; handle overlays when updating cookie from column view
  11236. (when (setq ov (car (overlays-at checkbox-beg)))
  11237. (setq ovs (overlay-start ov) ove (overlay-end ov))
  11238. (delete-overlay ov))
  11239. (goto-char checkbox-beg)
  11240. (insert new)
  11241. (delete-region (point) (+ (point) ndel))
  11242. (when org-auto-align-tags (org-fix-tags-on-the-fly))
  11243. (when ov (move-overlay ov ovs ove)))
  11244. (when cookie-present
  11245. (run-hook-with-args 'org-after-todo-statistics-hook
  11246. cnt-done (- cnt-all cnt-done))))))
  11247. (run-hooks 'org-todo-statistics-hook)))
  11248. (defvar org-after-todo-statistics-hook nil
  11249. "Hook that is called after a TODO statistics cookie has been updated.
  11250. Each function is called with two arguments: the number of not-done entries
  11251. and the number of done entries.
  11252. For example, the following function, when added to this hook, will switch
  11253. an entry to DONE when all children are done, and back to TODO when new
  11254. entries are set to a TODO status. Note that this hook is only called
  11255. when there is a statistics cookie in the headline!
  11256. (defun org-summary-todo (n-done n-not-done)
  11257. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  11258. (let (org-log-done org-log-states) ; turn off logging
  11259. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  11260. ")
  11261. (defvar org-todo-statistics-hook nil
  11262. "Hook that is run whenever Org thinks TODO statistics should be updated.
  11263. This hook runs even if there is no statistics cookie present, in which case
  11264. `org-after-todo-statistics-hook' would not run.")
  11265. (defun org-todo-trigger-tag-changes (state)
  11266. "Apply the changes defined in `org-todo-state-tags-triggers'."
  11267. (let ((l org-todo-state-tags-triggers)
  11268. changes)
  11269. (when (or (not state) (equal state ""))
  11270. (setq changes (append changes (cdr (assoc "" l)))))
  11271. (when (and (stringp state) (> (length state) 0))
  11272. (setq changes (append changes (cdr (assoc state l)))))
  11273. (when (member state org-not-done-keywords)
  11274. (setq changes (append changes (cdr (assoc 'todo l)))))
  11275. (when (member state org-done-keywords)
  11276. (setq changes (append changes (cdr (assoc 'done l)))))
  11277. (dolist (c changes)
  11278. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  11279. (defun org-local-logging (value)
  11280. "Get logging settings from a property VALUE."
  11281. (let* (words w a)
  11282. ;; directly set the variables, they are already local.
  11283. (setq org-log-done nil
  11284. org-log-repeat nil
  11285. org-todo-log-states nil)
  11286. (setq words (org-split-string value))
  11287. (while (setq w (pop words))
  11288. (cond
  11289. ((setq a (assoc w org-startup-options))
  11290. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  11291. (set (nth 1 a) (nth 2 a))))
  11292. ((setq a (org-extract-log-state-settings w))
  11293. (and (member (car a) org-todo-keywords-1)
  11294. (push a org-todo-log-states)))))))
  11295. (defun org-get-todo-sequence-head (kwd)
  11296. "Return the head of the TODO sequence to which KWD belongs.
  11297. If KWD is not set, check if there is a text property remembering the
  11298. right sequence."
  11299. (let (p)
  11300. (cond
  11301. ((not kwd)
  11302. (or (get-text-property (point-at-bol) 'org-todo-head)
  11303. (progn
  11304. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  11305. nil (point-at-eol)))
  11306. (get-text-property p 'org-todo-head))))
  11307. ((not (member kwd org-todo-keywords-1))
  11308. (car org-todo-keywords-1))
  11309. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  11310. (defun org-fast-todo-selection ()
  11311. "Fast TODO keyword selection with single keys.
  11312. Returns the new TODO keyword, or nil if no state change should occur."
  11313. (let* ((fulltable org-todo-key-alist)
  11314. (done-keywords org-done-keywords) ;; needed for the faces.
  11315. (maxlen (apply 'max (mapcar
  11316. (lambda (x)
  11317. (if (stringp (car x)) (string-width (car x)) 0))
  11318. fulltable)))
  11319. (expert nil)
  11320. (fwidth (+ maxlen 3 1 3))
  11321. (ncol (/ (- (window-width) 4) fwidth))
  11322. tg cnt e c tbl
  11323. groups ingroup)
  11324. (save-excursion
  11325. (save-window-excursion
  11326. (if expert
  11327. (set-buffer (get-buffer-create " *Org todo*"))
  11328. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  11329. (erase-buffer)
  11330. (org-set-local 'org-done-keywords done-keywords)
  11331. (setq tbl fulltable cnt 0)
  11332. (while (setq e (pop tbl))
  11333. (cond
  11334. ((equal e '(:startgroup))
  11335. (push '() groups) (setq ingroup t)
  11336. (when (not (= cnt 0))
  11337. (setq cnt 0)
  11338. (insert "\n"))
  11339. (insert "{ "))
  11340. ((equal e '(:endgroup))
  11341. (setq ingroup nil cnt 0)
  11342. (insert "}\n"))
  11343. ((equal e '(:newline))
  11344. (when (not (= cnt 0))
  11345. (setq cnt 0)
  11346. (insert "\n")
  11347. (setq e (car tbl))
  11348. (while (equal (car tbl) '(:newline))
  11349. (insert "\n")
  11350. (setq tbl (cdr tbl)))))
  11351. (t
  11352. (setq tg (car e) c (cdr e))
  11353. (if ingroup (push tg (car groups)))
  11354. (setq tg (org-add-props tg nil 'face
  11355. (org-get-todo-face tg)))
  11356. (if (and (= cnt 0) (not ingroup)) (insert " "))
  11357. (insert "[" c "] " tg (make-string
  11358. (- fwidth 4 (length tg)) ?\ ))
  11359. (when (= (setq cnt (1+ cnt)) ncol)
  11360. (insert "\n")
  11361. (if ingroup (insert " "))
  11362. (setq cnt 0)))))
  11363. (insert "\n")
  11364. (goto-char (point-min))
  11365. (if (not expert) (org-fit-window-to-buffer))
  11366. (message "[a-z..]:Set [SPC]:clear")
  11367. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  11368. (cond
  11369. ((or (= c ?\C-g)
  11370. (and (= c ?q) (not (rassoc c fulltable))))
  11371. (setq quit-flag t))
  11372. ((= c ?\ ) nil)
  11373. ((setq e (rassoc c fulltable) tg (car e))
  11374. tg)
  11375. (t (setq quit-flag t)))))))
  11376. (defun org-entry-is-todo-p ()
  11377. (member (org-get-todo-state) org-not-done-keywords))
  11378. (defun org-entry-is-done-p ()
  11379. (member (org-get-todo-state) org-done-keywords))
  11380. (defun org-get-todo-state ()
  11381. (save-excursion
  11382. (org-back-to-heading t)
  11383. (and (looking-at org-todo-line-regexp)
  11384. (match-end 2)
  11385. (match-string 2))))
  11386. (defun org-at-date-range-p (&optional inactive-ok)
  11387. "Is the cursor inside a date range?"
  11388. (interactive)
  11389. (save-excursion
  11390. (catch 'exit
  11391. (let ((pos (point)))
  11392. (skip-chars-backward "^[<\r\n")
  11393. (skip-chars-backward "<[")
  11394. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11395. (>= (match-end 0) pos)
  11396. (throw 'exit t))
  11397. (skip-chars-backward "^<[\r\n")
  11398. (skip-chars-backward "<[")
  11399. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11400. (>= (match-end 0) pos)
  11401. (throw 'exit t)))
  11402. nil)))
  11403. (defun org-get-repeat (&optional tagline)
  11404. "Check if there is a deadline/schedule with repeater in this entry."
  11405. (save-match-data
  11406. (save-excursion
  11407. (org-back-to-heading t)
  11408. (and (re-search-forward (if tagline
  11409. (concat tagline "\\s-*" org-repeat-re)
  11410. org-repeat-re)
  11411. (org-entry-end-position) t)
  11412. (match-string-no-properties 1)))))
  11413. (defvar org-last-changed-timestamp)
  11414. (defvar org-last-inserted-timestamp)
  11415. (defvar org-log-post-message)
  11416. (defvar org-log-note-purpose)
  11417. (defvar org-log-note-how)
  11418. (defvar org-log-note-extra)
  11419. (defun org-auto-repeat-maybe (done-word)
  11420. "Check if the current headline contains a repeated deadline/schedule.
  11421. If yes, set TODO state back to what it was and change the base date
  11422. of repeating deadline/scheduled time stamps to new date.
  11423. This function is run automatically after each state change to a DONE state."
  11424. ;; last-state is dynamically scoped into this function
  11425. (let* ((repeat (org-get-repeat))
  11426. (aa (assoc org-last-state org-todo-kwd-alist))
  11427. (interpret (nth 1 aa))
  11428. (head (nth 2 aa))
  11429. (whata '(("h" . hour) ("d" . day) ("m" . month) ("y" . year)))
  11430. (msg "Entry repeats: ")
  11431. (org-log-done nil)
  11432. (org-todo-log-states nil)
  11433. re type n what ts time to-state)
  11434. (when repeat
  11435. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  11436. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  11437. org-todo-repeat-to-state))
  11438. (unless (and to-state (member to-state org-todo-keywords-1))
  11439. (setq to-state (if (eq interpret 'type) org-last-state head)))
  11440. (org-todo to-state)
  11441. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  11442. (org-entry-put nil "LAST_REPEAT" (format-time-string
  11443. (org-time-stamp-format t t))))
  11444. (when org-log-repeat
  11445. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  11446. (memq 'org-add-log-note post-command-hook))
  11447. ;; OK, we are already setup for some record
  11448. (if (eq org-log-repeat 'note)
  11449. ;; make sure we take a note, not only a time stamp
  11450. (setq org-log-note-how 'note))
  11451. ;; Set up for taking a record
  11452. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  11453. org-last-state
  11454. 'findpos org-log-repeat)))
  11455. (org-back-to-heading t)
  11456. (org-add-planning-info nil nil 'closed)
  11457. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11458. org-deadline-time-regexp "\\)\\|\\("
  11459. org-ts-regexp "\\)"))
  11460. (while (re-search-forward
  11461. re (save-excursion (outline-next-heading) (point)) t)
  11462. (setq type (if (match-end 1) org-scheduled-string
  11463. (if (match-end 3) org-deadline-string "Plain:"))
  11464. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  11465. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts)
  11466. (setq n (string-to-number (match-string 2 ts))
  11467. what (match-string 3 ts))
  11468. (if (equal what "w") (setq n (* n 7) what "d"))
  11469. (if (and (equal what "h") (not (string-match "[0-9]\\{1,2\\}:[0-9]\\{2\\}" ts)))
  11470. (error "Cannot repeat in Repeat in %d hour(s) because no hour has been set" n))
  11471. ;; Preparation, see if we need to modify the start date for the change
  11472. (when (match-end 1)
  11473. (setq time (save-match-data (org-time-string-to-time ts)))
  11474. (cond
  11475. ((equal (match-string 1 ts) ".")
  11476. ;; Shift starting date to today
  11477. (org-timestamp-change
  11478. (- (org-today) (time-to-days time))
  11479. 'day))
  11480. ((equal (match-string 1 ts) "+")
  11481. (let ((nshiftmax 10) (nshift 0))
  11482. (while (or (= nshift 0)
  11483. (<= (time-to-days time)
  11484. (time-to-days (current-time))))
  11485. (when (= (incf nshift) nshiftmax)
  11486. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  11487. (error "Abort")))
  11488. (org-timestamp-change n (cdr (assoc what whata)))
  11489. (org-at-timestamp-p t)
  11490. (setq ts (match-string 1))
  11491. (setq time (save-match-data (org-time-string-to-time ts)))))
  11492. (org-timestamp-change (- n) (cdr (assoc what whata)))
  11493. ;; rematch, so that we have everything in place for the real shift
  11494. (org-at-timestamp-p t)
  11495. (setq ts (match-string 1))
  11496. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts))))
  11497. (save-excursion (org-timestamp-change n (cdr (assoc what whata)) nil t))
  11498. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  11499. (setq org-log-post-message msg)
  11500. (message "%s" msg))))
  11501. (defun org-show-todo-tree (arg)
  11502. "Make a compact tree which shows all headlines marked with TODO.
  11503. The tree will show the lines where the regexp matches, and all higher
  11504. headlines above the match.
  11505. With a \\[universal-argument] prefix, prompt for a regexp to match.
  11506. With a numeric prefix N, construct a sparse tree for the Nth element
  11507. of `org-todo-keywords-1'."
  11508. (interactive "P")
  11509. (let ((case-fold-search nil)
  11510. (kwd-re
  11511. (cond ((null arg) org-not-done-regexp)
  11512. ((equal arg '(4))
  11513. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  11514. (mapcar 'list org-todo-keywords-1))))
  11515. (concat "\\("
  11516. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  11517. "\\)\\>")))
  11518. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  11519. (regexp-quote (nth (1- (prefix-numeric-value arg))
  11520. org-todo-keywords-1)))
  11521. (t (error "Invalid prefix argument: %s" arg)))))
  11522. (message "%d TODO entries found"
  11523. (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
  11524. (defun org-deadline (arg &optional time)
  11525. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  11526. With one universal prefix argument, remove any deadline from the item.
  11527. With two universal prefix arguments, prompt for a warning delay.
  11528. With argument TIME, set the deadline at the corresponding date. TIME
  11529. can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11530. (interactive "P")
  11531. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11532. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11533. 'region-start-level 'region))
  11534. org-loop-over-headlines-in-active-region)
  11535. (org-map-entries
  11536. `(org-deadline ',arg ,time)
  11537. org-loop-over-headlines-in-active-region
  11538. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11539. (let* ((old-date (org-entry-get nil "DEADLINE"))
  11540. (old-date-time (if old-date (org-time-string-to-time old-date)))
  11541. (repeater (and old-date
  11542. (string-match
  11543. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11544. old-date)
  11545. (match-string 1 old-date))))
  11546. (cond
  11547. ((equal arg '(4))
  11548. (when (and old-date org-log-redeadline)
  11549. (org-add-log-setup 'deldeadline nil old-date 'findpos
  11550. org-log-redeadline))
  11551. (org-remove-timestamp-with-keyword org-deadline-string)
  11552. (message "Item no longer has a deadline."))
  11553. ((equal arg '(16))
  11554. (save-excursion
  11555. (if (re-search-forward
  11556. org-deadline-time-regexp
  11557. (save-excursion (outline-next-heading) (point)) t)
  11558. (let* ((rpl0 (match-string 1))
  11559. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  11560. (replace-match
  11561. (concat org-deadline-string
  11562. " <" rpl
  11563. (format " -%dd"
  11564. (abs
  11565. (- (time-to-days
  11566. (save-match-data
  11567. (org-read-date nil t nil "Warn starting from" old-date-time)))
  11568. (time-to-days old-date-time))))
  11569. ">") t t))
  11570. (user-error "No deadline information to update"))))
  11571. (t
  11572. (org-add-planning-info 'deadline time 'closed)
  11573. (when (and old-date org-log-redeadline
  11574. (not (equal old-date
  11575. (substring org-last-inserted-timestamp 1 -1))))
  11576. (org-add-log-setup 'redeadline nil old-date 'findpos
  11577. org-log-redeadline))
  11578. (when repeater
  11579. (save-excursion
  11580. (org-back-to-heading t)
  11581. (when (re-search-forward (concat org-deadline-string " "
  11582. org-last-inserted-timestamp)
  11583. (save-excursion
  11584. (outline-next-heading) (point)) t)
  11585. (goto-char (1- (match-end 0)))
  11586. (insert " " repeater)
  11587. (setq org-last-inserted-timestamp
  11588. (concat (substring org-last-inserted-timestamp 0 -1)
  11589. " " repeater
  11590. (substring org-last-inserted-timestamp -1))))))
  11591. (message "Deadline on %s" org-last-inserted-timestamp))))))
  11592. (defun org-schedule (arg &optional time)
  11593. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  11594. With one universal prefix argument, remove any scheduling date from the item.
  11595. With two universal prefix arguments, prompt for a delay cookie.
  11596. With argument TIME, scheduled at the corresponding date. TIME can
  11597. either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11598. (interactive "P")
  11599. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11600. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11601. 'region-start-level 'region))
  11602. org-loop-over-headlines-in-active-region)
  11603. (org-map-entries
  11604. `(org-schedule ',arg ,time)
  11605. org-loop-over-headlines-in-active-region
  11606. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11607. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  11608. (old-date-time (if old-date (org-time-string-to-time old-date)))
  11609. (repeater (and old-date
  11610. (string-match
  11611. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11612. old-date)
  11613. (match-string 1 old-date))))
  11614. (cond
  11615. ((equal arg '(4))
  11616. (progn
  11617. (when (and old-date org-log-reschedule)
  11618. (org-add-log-setup 'delschedule nil old-date 'findpos
  11619. org-log-reschedule))
  11620. (org-remove-timestamp-with-keyword org-scheduled-string)
  11621. (message "Item is no longer scheduled.")))
  11622. ((equal arg '(16))
  11623. (save-excursion
  11624. (if (re-search-forward
  11625. org-scheduled-time-regexp
  11626. (save-excursion (outline-next-heading) (point)) t)
  11627. (let* ((rpl0 (match-string 1))
  11628. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  11629. (replace-match
  11630. (concat org-scheduled-string
  11631. " <" rpl
  11632. (format " -%dd"
  11633. (abs
  11634. (- (time-to-days
  11635. (save-match-data
  11636. (org-read-date nil t nil "Delay until" old-date-time)))
  11637. (time-to-days old-date-time))))
  11638. ">") t t))
  11639. (user-error "No scheduled information to update"))))
  11640. (t
  11641. (org-add-planning-info 'scheduled time 'closed)
  11642. (when (and old-date org-log-reschedule
  11643. (not (equal old-date
  11644. (substring org-last-inserted-timestamp 1 -1))))
  11645. (org-add-log-setup 'reschedule nil old-date 'findpos
  11646. org-log-reschedule))
  11647. (when repeater
  11648. (save-excursion
  11649. (org-back-to-heading t)
  11650. (when (re-search-forward (concat org-scheduled-string " "
  11651. org-last-inserted-timestamp)
  11652. (save-excursion
  11653. (outline-next-heading) (point)) t)
  11654. (goto-char (1- (match-end 0)))
  11655. (insert " " repeater)
  11656. (setq org-last-inserted-timestamp
  11657. (concat (substring org-last-inserted-timestamp 0 -1)
  11658. " " repeater
  11659. (substring org-last-inserted-timestamp -1))))))
  11660. (message "Scheduled to %s" org-last-inserted-timestamp))))))
  11661. (defun org-get-scheduled-time (pom &optional inherit)
  11662. "Get the scheduled time as a time tuple, of a format suitable
  11663. for calling org-schedule with, or if there is no scheduling,
  11664. returns nil."
  11665. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  11666. (when time
  11667. (apply 'encode-time (org-parse-time-string time)))))
  11668. (defun org-get-deadline-time (pom &optional inherit)
  11669. "Get the deadline as a time tuple, of a format suitable for
  11670. calling org-deadline with, or if there is no scheduling, returns
  11671. nil."
  11672. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  11673. (when time
  11674. (apply 'encode-time (org-parse-time-string time)))))
  11675. (defun org-remove-timestamp-with-keyword (keyword)
  11676. "Remove all time stamps with KEYWORD in the current entry."
  11677. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  11678. beg)
  11679. (save-excursion
  11680. (org-back-to-heading t)
  11681. (setq beg (point))
  11682. (outline-next-heading)
  11683. (while (re-search-backward re beg t)
  11684. (replace-match "")
  11685. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  11686. (equal (char-before) ?\ ))
  11687. (backward-delete-char 1)
  11688. (if (string-match "^[ \t]*$" (buffer-substring
  11689. (point-at-bol) (point-at-eol)))
  11690. (delete-region (point-at-bol)
  11691. (min (point-max) (1+ (point-at-eol))))))))))
  11692. (defun org-add-planning-info (what &optional time &rest remove)
  11693. "Insert new timestamp with keyword in the line directly after the headline.
  11694. WHAT indicates what kind of time stamp to add. TIME indicates the time to use.
  11695. If non is given, the user is prompted for a date.
  11696. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  11697. be removed."
  11698. (interactive)
  11699. (let (org-time-was-given org-end-time-was-given ts
  11700. end default-time default-input)
  11701. (catch 'exit
  11702. (when (and (memq what '(scheduled deadline))
  11703. (or (not time)
  11704. (and (stringp time)
  11705. (string-match "^[-+]+[0-9]" time))))
  11706. ;; Try to get a default date/time from existing timestamp
  11707. (save-excursion
  11708. (org-back-to-heading t)
  11709. (setq end (save-excursion (outline-next-heading) (point)))
  11710. (when (re-search-forward (if (eq what 'scheduled)
  11711. org-scheduled-time-regexp
  11712. org-deadline-time-regexp)
  11713. end t)
  11714. (setq ts (match-string 1)
  11715. default-time
  11716. (apply 'encode-time (org-parse-time-string ts))
  11717. default-input (and ts (org-get-compact-tod ts))))))
  11718. (when what
  11719. (setq time
  11720. (if (stringp time)
  11721. ;; This is a string (relative or absolute), set proper date
  11722. (apply 'encode-time
  11723. (org-read-date-analyze
  11724. time default-time (decode-time default-time)))
  11725. ;; If necessary, get the time from the user
  11726. (or time (org-read-date nil 'to-time nil nil
  11727. default-time default-input)))))
  11728. (when (and org-insert-labeled-timestamps-at-point
  11729. (member what '(scheduled deadline)))
  11730. (insert
  11731. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  11732. (org-insert-time-stamp time org-time-was-given
  11733. nil nil nil (list org-end-time-was-given))
  11734. (setq what nil))
  11735. (save-excursion
  11736. (save-restriction
  11737. (let (col list elt ts buffer-invisibility-spec)
  11738. (org-back-to-heading t)
  11739. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"))
  11740. (goto-char (match-end 1))
  11741. (setq col (current-column))
  11742. (goto-char (match-end 0))
  11743. (if (eobp) (insert "\n") (forward-char 1))
  11744. (when (and (not what)
  11745. (not (looking-at
  11746. (concat "[ \t]*"
  11747. org-keyword-time-not-clock-regexp))))
  11748. ;; Nothing to add, nothing to remove...... :-)
  11749. (throw 'exit nil))
  11750. (if (and (not (looking-at org-outline-regexp))
  11751. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  11752. "[^\r\n]*"))
  11753. (not (equal (match-string 1) org-clock-string)))
  11754. (narrow-to-region (match-beginning 0) (match-end 0))
  11755. (insert-before-markers "\n")
  11756. (backward-char 1)
  11757. (narrow-to-region (point) (point))
  11758. (and org-adapt-indentation (org-indent-to-column col)))
  11759. ;; Check if we have to remove something.
  11760. (setq list (cons what remove))
  11761. (while list
  11762. (setq elt (pop list))
  11763. (when (or (and (eq elt 'scheduled)
  11764. (re-search-forward org-scheduled-time-regexp nil t))
  11765. (and (eq elt 'deadline)
  11766. (re-search-forward org-deadline-time-regexp nil t))
  11767. (and (eq elt 'closed)
  11768. (re-search-forward org-closed-time-regexp nil t)))
  11769. (replace-match "")
  11770. (if (looking-at "--+<[^>]+>") (replace-match ""))))
  11771. (and (looking-at "[ \t]+") (replace-match ""))
  11772. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  11773. (when what
  11774. (insert
  11775. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  11776. (cond ((eq what 'scheduled) org-scheduled-string)
  11777. ((eq what 'deadline) org-deadline-string)
  11778. ((eq what 'closed) org-closed-string))
  11779. " ")
  11780. (setq ts (org-insert-time-stamp
  11781. time
  11782. (or org-time-was-given
  11783. (and (eq what 'closed) org-log-done-with-time))
  11784. (eq what 'closed)
  11785. nil nil (list org-end-time-was-given)))
  11786. (insert
  11787. (if (not (or (bolp) (eq (char-before) ?\ )
  11788. (memq (char-after) '(32 10))
  11789. (eobp))) " " ""))
  11790. (end-of-line 1))
  11791. (goto-char (point-min))
  11792. (widen)
  11793. (if (and (looking-at "[ \t]*\n")
  11794. (equal (char-before) ?\n))
  11795. (delete-region (1- (point)) (point-at-eol)))
  11796. ts))))))
  11797. (defvar org-log-note-marker (make-marker))
  11798. (defvar org-log-note-purpose nil)
  11799. (defvar org-log-note-state nil)
  11800. (defvar org-log-note-previous-state nil)
  11801. (defvar org-log-note-how nil)
  11802. (defvar org-log-note-extra nil)
  11803. (defvar org-log-note-window-configuration nil)
  11804. (defvar org-log-note-return-to (make-marker))
  11805. (defvar org-log-note-effective-time nil
  11806. "Remembered current time so that dynamically scoped
  11807. `org-extend-today-until' affects tha timestamps in state change
  11808. log")
  11809. (defvar org-log-post-message nil
  11810. "Message to be displayed after a log note has been stored.
  11811. The auto-repeater uses this.")
  11812. (defun org-add-note ()
  11813. "Add a note to the current entry.
  11814. This is done in the same way as adding a state change note."
  11815. (interactive)
  11816. (org-add-log-setup 'note nil nil 'findpos nil))
  11817. (defvar org-property-end-re)
  11818. (defun org-add-log-setup (&optional purpose state prev-state
  11819. findpos how extra)
  11820. "Set up the post command hook to take a note.
  11821. If this is about to TODO state change, the new state is expected in STATE.
  11822. When FINDPOS is non-nil, find the correct position for the note in
  11823. the current entry. If not, assume that it can be inserted at point.
  11824. HOW is an indicator what kind of note should be created.
  11825. EXTRA is additional text that will be inserted into the notes buffer."
  11826. (let* ((org-log-into-drawer (org-log-into-drawer))
  11827. (drawer (cond ((stringp org-log-into-drawer)
  11828. org-log-into-drawer)
  11829. (org-log-into-drawer "LOGBOOK"))))
  11830. (save-restriction
  11831. (save-excursion
  11832. (when findpos
  11833. (org-back-to-heading t)
  11834. (narrow-to-region (point) (save-excursion
  11835. (outline-next-heading) (point)))
  11836. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"
  11837. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  11838. "[^\r\n]*\\)?"))
  11839. (goto-char (match-end 0))
  11840. (cond
  11841. (drawer
  11842. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  11843. nil t)
  11844. (progn
  11845. (goto-char (match-end 0))
  11846. (or org-log-states-order-reversed
  11847. (and (re-search-forward org-property-end-re nil t)
  11848. (goto-char (1- (match-beginning 0))))))
  11849. (insert "\n:" drawer ":\n:END:")
  11850. (beginning-of-line 0)
  11851. (org-indent-line)
  11852. (beginning-of-line 2)
  11853. (org-indent-line)
  11854. (end-of-line 0)))
  11855. ((and org-log-state-notes-insert-after-drawers
  11856. (save-excursion
  11857. (forward-line) (looking-at org-drawer-regexp)))
  11858. (forward-line)
  11859. (while (looking-at org-drawer-regexp)
  11860. (goto-char (match-end 0))
  11861. (re-search-forward org-property-end-re (point-max) t)
  11862. (forward-line))
  11863. (forward-line -1)))
  11864. (unless org-log-states-order-reversed
  11865. (and (= (char-after) ?\n) (forward-char 1))
  11866. (org-skip-over-state-notes)
  11867. (skip-chars-backward " \t\n\r")))
  11868. (move-marker org-log-note-marker (point))
  11869. (setq org-log-note-purpose purpose
  11870. org-log-note-state state
  11871. org-log-note-previous-state prev-state
  11872. org-log-note-how how
  11873. org-log-note-extra extra
  11874. org-log-note-effective-time (org-current-effective-time))
  11875. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  11876. (defun org-skip-over-state-notes ()
  11877. "Skip past the list of State notes in an entry."
  11878. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  11879. (when (ignore-errors (goto-char (org-in-item-p)))
  11880. (let* ((struct (org-list-struct))
  11881. (prevs (org-list-prevs-alist struct)))
  11882. (while (looking-at "[ \t]*- State")
  11883. (goto-char (or (org-list-get-next-item (point) struct prevs)
  11884. (org-list-get-item-end (point) struct)))))))
  11885. (defun org-add-log-note (&optional purpose)
  11886. "Pop up a window for taking a note, and add this note later at point."
  11887. (remove-hook 'post-command-hook 'org-add-log-note)
  11888. (setq org-log-note-window-configuration (current-window-configuration))
  11889. (delete-other-windows)
  11890. (move-marker org-log-note-return-to (point))
  11891. (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
  11892. (goto-char org-log-note-marker)
  11893. (org-switch-to-buffer-other-window "*Org Note*")
  11894. (erase-buffer)
  11895. (if (memq org-log-note-how '(time state))
  11896. (let (current-prefix-arg) (org-store-log-note))
  11897. (let ((org-inhibit-startup t)) (org-mode))
  11898. (insert (format "# Insert note for %s.
  11899. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  11900. (cond
  11901. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  11902. ((eq org-log-note-purpose 'done) "closed todo item")
  11903. ((eq org-log-note-purpose 'state)
  11904. (format "state change from \"%s\" to \"%s\""
  11905. (or org-log-note-previous-state "")
  11906. (or org-log-note-state "")))
  11907. ((eq org-log-note-purpose 'reschedule)
  11908. "rescheduling")
  11909. ((eq org-log-note-purpose 'delschedule)
  11910. "no longer scheduled")
  11911. ((eq org-log-note-purpose 'redeadline)
  11912. "changing deadline")
  11913. ((eq org-log-note-purpose 'deldeadline)
  11914. "removing deadline")
  11915. ((eq org-log-note-purpose 'refile)
  11916. "refiling")
  11917. ((eq org-log-note-purpose 'note)
  11918. "this entry")
  11919. (t (error "This should not happen")))))
  11920. (if org-log-note-extra (insert org-log-note-extra))
  11921. (org-set-local 'org-finish-function 'org-store-log-note)
  11922. (run-hooks 'org-log-buffer-setup-hook)))
  11923. (defvar org-note-abort nil) ; dynamically scoped
  11924. (defun org-store-log-note ()
  11925. "Finish taking a log note, and insert it to where it belongs."
  11926. (let ((txt (buffer-string)))
  11927. (kill-buffer (current-buffer))
  11928. (let ((note (cdr (assq org-log-note-purpose org-log-note-headings)))
  11929. lines ind bul)
  11930. (while (string-match "\\`# .*\n[ \t\n]*" txt)
  11931. (setq txt (replace-match "" t t txt)))
  11932. (if (string-match "\\s-+\\'" txt)
  11933. (setq txt (replace-match "" t t txt)))
  11934. (setq lines (org-split-string txt "\n"))
  11935. (when (and note (string-match "\\S-" note))
  11936. (setq note
  11937. (org-replace-escapes
  11938. note
  11939. (list (cons "%u" (user-login-name))
  11940. (cons "%U" user-full-name)
  11941. (cons "%t" (format-time-string
  11942. (org-time-stamp-format 'long 'inactive)
  11943. org-log-note-effective-time))
  11944. (cons "%T" (format-time-string
  11945. (org-time-stamp-format 'long nil)
  11946. org-log-note-effective-time))
  11947. (cons "%d" (format-time-string
  11948. (org-time-stamp-format nil 'inactive)
  11949. org-log-note-effective-time))
  11950. (cons "%D" (format-time-string
  11951. (org-time-stamp-format nil nil)
  11952. org-log-note-effective-time))
  11953. (cons "%s" (if org-log-note-state
  11954. (concat "\"" org-log-note-state "\"")
  11955. ""))
  11956. (cons "%S" (if org-log-note-previous-state
  11957. (concat "\"" org-log-note-previous-state "\"")
  11958. "\"\"")))))
  11959. (if lines (setq note (concat note " \\\\")))
  11960. (push note lines))
  11961. (when (or current-prefix-arg org-note-abort)
  11962. (when org-log-into-drawer
  11963. (org-remove-empty-drawer-at
  11964. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  11965. org-log-note-marker))
  11966. (setq lines nil))
  11967. (when lines
  11968. (with-current-buffer (marker-buffer org-log-note-marker)
  11969. (save-excursion
  11970. (goto-char org-log-note-marker)
  11971. (move-marker org-log-note-marker nil)
  11972. (end-of-line 1)
  11973. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  11974. (setq ind (save-excursion
  11975. (if (ignore-errors (goto-char (org-in-item-p)))
  11976. (let ((struct (org-list-struct)))
  11977. (org-list-get-ind
  11978. (org-list-get-top-point struct) struct))
  11979. (skip-chars-backward " \r\t\n")
  11980. (cond
  11981. ((and (org-at-heading-p)
  11982. org-adapt-indentation)
  11983. (1+ (org-current-level)))
  11984. ((org-at-heading-p) 0)
  11985. (t (org-get-indentation))))))
  11986. (setq bul (org-list-bullet-string "-"))
  11987. (org-indent-line-to ind)
  11988. (insert bul (pop lines))
  11989. (let ((ind-body (+ (length bul) ind)))
  11990. (while lines
  11991. (insert "\n")
  11992. (org-indent-line-to ind-body)
  11993. (insert (pop lines))))
  11994. (message "Note stored")
  11995. (org-back-to-heading t)
  11996. (org-cycle-hide-drawers 'children))))))
  11997. (set-window-configuration org-log-note-window-configuration)
  11998. (with-current-buffer (marker-buffer org-log-note-return-to)
  11999. (goto-char org-log-note-return-to))
  12000. (move-marker org-log-note-return-to nil)
  12001. (and org-log-post-message (message "%s" org-log-post-message)))
  12002. (defun org-remove-empty-drawer-at (drawer pos)
  12003. "Remove an empty drawer DRAWER at position POS.
  12004. POS may also be a marker."
  12005. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  12006. (save-excursion
  12007. (save-restriction
  12008. (widen)
  12009. (goto-char pos)
  12010. (if (org-in-regexp
  12011. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  12012. (replace-match ""))))))
  12013. (defvar org-ts-type nil)
  12014. (defun org-sparse-tree (&optional arg type)
  12015. "Create a sparse tree, prompt for the details.
  12016. This command can create sparse trees. You first need to select the type
  12017. of match used to create the tree:
  12018. t Show all TODO entries.
  12019. T Show entries with a specific TODO keyword.
  12020. m Show entries selected by a tags/property match.
  12021. p Enter a property name and its value (both with completion on existing
  12022. names/values) and show entries with that property.
  12023. r Show entries matching a regular expression (`/' can be used as well).
  12024. b Show deadlines and scheduled items before a date.
  12025. a Show deadlines and scheduled items after a date.
  12026. d Show deadlines due within `org-deadline-warning-days'.
  12027. D Show deadlines and scheduled items between a date range."
  12028. (interactive "P")
  12029. (let (ans kwd value ts-type)
  12030. (setq type (or type org-sparse-tree-default-date-type))
  12031. (setq org-ts-type type)
  12032. (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"
  12033. (cond ((eq type 'all) "all timestamps")
  12034. ((eq type 'scheduled) "only scheduled")
  12035. ((eq type 'deadline) "only deadline")
  12036. ((eq type 'active) "only active timestamps")
  12037. ((eq type 'inactive) "only inactive timestamps")
  12038. ((eq type 'scheduled-or-deadline) "scheduled/deadline")
  12039. (t "scheduled/deadline")))
  12040. (setq ans (read-char-exclusive))
  12041. (cond
  12042. ((equal ans ?c)
  12043. (org-sparse-tree arg (cadr (member type '(scheduled-or-deadline all scheduled deadline active inactive)))))
  12044. ((equal ans ?d)
  12045. (call-interactively 'org-check-deadlines))
  12046. ((equal ans ?b)
  12047. (call-interactively 'org-check-before-date))
  12048. ((equal ans ?a)
  12049. (call-interactively 'org-check-after-date))
  12050. ((equal ans ?D)
  12051. (call-interactively 'org-check-dates-range))
  12052. ((equal ans ?t)
  12053. (call-interactively 'org-show-todo-tree))
  12054. ((equal ans ?T)
  12055. (org-show-todo-tree '(4)))
  12056. ((member ans '(?T ?m))
  12057. (call-interactively 'org-match-sparse-tree))
  12058. ((member ans '(?p ?P))
  12059. (setq kwd (org-icompleting-read "Property: "
  12060. (mapcar 'list (org-buffer-property-keys))))
  12061. (setq value (org-icompleting-read "Value: "
  12062. (mapcar 'list (org-property-values kwd))))
  12063. (unless (string-match "\\`{.*}\\'" value)
  12064. (setq value (concat "\"" value "\"")))
  12065. (org-match-sparse-tree arg (concat kwd "=" value)))
  12066. ((member ans '(?r ?R ?/))
  12067. (call-interactively 'org-occur))
  12068. (t (error "No such sparse tree command \"%c\"" ans)))))
  12069. (defvar org-occur-highlights nil
  12070. "List of overlays used for occur matches.")
  12071. (make-variable-buffer-local 'org-occur-highlights)
  12072. (defvar org-occur-parameters nil
  12073. "Parameters of the active org-occur calls.
  12074. This is a list, each call to org-occur pushes as cons cell,
  12075. containing the regular expression and the callback, onto the list.
  12076. The list can contain several entries if `org-occur' has been called
  12077. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  12078. will only contain one set of parameters. When the highlights are
  12079. removed (for example with `C-c C-c', or with the next edit (depending
  12080. on `org-remove-highlights-with-change'), this variable is emptied
  12081. as well.")
  12082. (make-variable-buffer-local 'org-occur-parameters)
  12083. (defun org-occur (regexp &optional keep-previous callback)
  12084. "Make a compact tree which shows all matches of REGEXP.
  12085. The tree will show the lines where the regexp matches, and all higher
  12086. headlines above the match. It will also show the heading after the match,
  12087. to make sure editing the matching entry is easy.
  12088. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  12089. call to `org-occur' will be kept, to allow stacking of calls to this
  12090. command.
  12091. If CALLBACK is non-nil, it is a function which is called to confirm
  12092. that the match should indeed be shown."
  12093. (interactive "sRegexp: \nP")
  12094. (when (equal regexp "")
  12095. (error "Regexp cannot be empty"))
  12096. (unless keep-previous
  12097. (org-remove-occur-highlights nil nil t))
  12098. (push (cons regexp callback) org-occur-parameters)
  12099. (let ((cnt 0))
  12100. (save-excursion
  12101. (goto-char (point-min))
  12102. (if (or (not keep-previous) ; do not want to keep
  12103. (not org-occur-highlights)) ; no previous matches
  12104. ;; hide everything
  12105. (org-overview))
  12106. (while (re-search-forward regexp nil t)
  12107. (when (or (not callback)
  12108. (save-match-data (funcall callback)))
  12109. (setq cnt (1+ cnt))
  12110. (when org-highlight-sparse-tree-matches
  12111. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  12112. (org-show-context 'occur-tree))))
  12113. (when org-remove-highlights-with-change
  12114. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  12115. nil 'local))
  12116. (unless org-sparse-tree-open-archived-trees
  12117. (org-hide-archived-subtrees (point-min) (point-max)))
  12118. (run-hooks 'org-occur-hook)
  12119. (if (org-called-interactively-p 'interactive)
  12120. (message "%d match(es) for regexp %s" cnt regexp))
  12121. cnt))
  12122. (defun org-occur-next-match (&optional n reset)
  12123. "Function for `next-error-function' to find sparse tree matches.
  12124. N is the number of matches to move, when negative move backwards.
  12125. RESET is entirely ignored - this function always goes back to the
  12126. starting point when no match is found."
  12127. (let* ((limit (if (< n 0) (point-min) (point-max)))
  12128. (search-func (if (< n 0)
  12129. 'previous-single-char-property-change
  12130. 'next-single-char-property-change))
  12131. (n (abs n))
  12132. (pos (point))
  12133. p1)
  12134. (catch 'exit
  12135. (while (setq p1 (funcall search-func (point) 'org-type))
  12136. (when (equal p1 limit)
  12137. (goto-char pos)
  12138. (error "No more matches"))
  12139. (when (equal (get-char-property p1 'org-type) 'org-occur)
  12140. (setq n (1- n))
  12141. (when (= n 0)
  12142. (goto-char p1)
  12143. (throw 'exit (point))))
  12144. (goto-char p1))
  12145. (goto-char p1)
  12146. (error "No more matches"))))
  12147. (defun org-show-context (&optional key)
  12148. "Make sure point and context are visible.
  12149. How much context is shown depends upon the variables
  12150. `org-show-hierarchy-above', `org-show-following-heading',
  12151. `org-show-entry-below' and `org-show-siblings'."
  12152. (let ((heading-p (org-at-heading-p t))
  12153. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  12154. (following-p (org-get-alist-option org-show-following-heading key))
  12155. (entry-p (org-get-alist-option org-show-entry-below key))
  12156. (siblings-p (org-get-alist-option org-show-siblings key)))
  12157. ;; Show heading or entry text
  12158. (if (and heading-p (not entry-p))
  12159. (org-flag-heading nil) ; only show the heading
  12160. (and (or entry-p (outline-invisible-p) (org-invisible-p2))
  12161. (org-show-hidden-entry))) ; show entire entry
  12162. (when following-p
  12163. ;; Show next sibling, or heading below text
  12164. (save-excursion
  12165. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  12166. (org-flag-heading nil))))
  12167. (when siblings-p (org-show-siblings))
  12168. (when hierarchy-p
  12169. ;; show all higher headings, possibly with siblings
  12170. (save-excursion
  12171. (while (and (condition-case nil
  12172. (progn (org-up-heading-all 1) t)
  12173. (error nil))
  12174. (not (bobp)))
  12175. (org-flag-heading nil)
  12176. (when siblings-p (org-show-siblings)))))
  12177. (unless (eq key 'agenda) (org-fix-ellipsis-at-bol))))
  12178. (defvar org-reveal-start-hook nil
  12179. "Hook run before revealing a location.")
  12180. (defun org-reveal (&optional siblings)
  12181. "Show current entry, hierarchy above it, and the following headline.
  12182. This can be used to show a consistent set of context around locations
  12183. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  12184. not t for the search context.
  12185. With optional argument SIBLINGS, on each level of the hierarchy all
  12186. siblings are shown. This repairs the tree structure to what it would
  12187. look like when opened with hierarchical calls to `org-cycle'.
  12188. With double optional argument \\[universal-argument] \\[universal-argument], \
  12189. go to the parent and show the
  12190. entire tree."
  12191. (interactive "P")
  12192. (run-hooks 'org-reveal-start-hook)
  12193. (let ((org-show-hierarchy-above t)
  12194. (org-show-following-heading t)
  12195. (org-show-siblings (if siblings t org-show-siblings)))
  12196. (org-show-context nil))
  12197. (when (equal siblings '(16))
  12198. (save-excursion
  12199. (when (org-up-heading-safe)
  12200. (org-show-subtree)
  12201. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  12202. (defun org-highlight-new-match (beg end)
  12203. "Highlight from BEG to END and mark the highlight is an occur headline."
  12204. (let ((ov (make-overlay beg end)))
  12205. (overlay-put ov 'face 'secondary-selection)
  12206. (overlay-put ov 'org-type 'org-occur)
  12207. (push ov org-occur-highlights)))
  12208. (defun org-remove-occur-highlights (&optional beg end noremove)
  12209. "Remove the occur highlights from the buffer.
  12210. BEG and END are ignored. If NOREMOVE is nil, remove this function
  12211. from the `before-change-functions' in the current buffer."
  12212. (interactive)
  12213. (unless org-inhibit-highlight-removal
  12214. (mapc 'delete-overlay org-occur-highlights)
  12215. (setq org-occur-highlights nil)
  12216. (setq org-occur-parameters nil)
  12217. (unless noremove
  12218. (remove-hook 'before-change-functions
  12219. 'org-remove-occur-highlights 'local))))
  12220. ;;;; Priorities
  12221. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  12222. "Regular expression matching the priority indicator.")
  12223. (defvar org-remove-priority-next-time nil)
  12224. (defun org-priority-up ()
  12225. "Increase the priority of the current item."
  12226. (interactive)
  12227. (org-priority 'up))
  12228. (defun org-priority-down ()
  12229. "Decrease the priority of the current item."
  12230. (interactive)
  12231. (org-priority 'down))
  12232. (defun org-priority (&optional action show)
  12233. "Change the priority of an item.
  12234. ACTION can be `set', `up', `down', or a character."
  12235. (interactive "P")
  12236. (if (equal action '(4))
  12237. (org-show-priority)
  12238. (unless org-enable-priority-commands
  12239. (error "Priority commands are disabled"))
  12240. (setq action (or action 'set))
  12241. (let (current new news have remove)
  12242. (save-excursion
  12243. (org-back-to-heading t)
  12244. (if (looking-at org-priority-regexp)
  12245. (setq current (string-to-char (match-string 2))
  12246. have t))
  12247. (cond
  12248. ((eq action 'remove)
  12249. (setq remove t new ?\ ))
  12250. ((or (eq action 'set)
  12251. (if (featurep 'xemacs) (characterp action) (integerp action)))
  12252. (if (not (eq action 'set))
  12253. (setq new action)
  12254. (message "Priority %c-%c, SPC to remove: "
  12255. org-highest-priority org-lowest-priority)
  12256. (save-match-data
  12257. (setq new (read-char-exclusive))))
  12258. (if (and (= (upcase org-highest-priority) org-highest-priority)
  12259. (= (upcase org-lowest-priority) org-lowest-priority))
  12260. (setq new (upcase new)))
  12261. (cond ((equal new ?\ ) (setq remove t))
  12262. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  12263. (error "Priority must be between `%c' and `%c'"
  12264. org-highest-priority org-lowest-priority))))
  12265. ((eq action 'up)
  12266. (setq new (if have
  12267. (1- current) ; normal cycling
  12268. ;; last priority was empty
  12269. (if (eq last-command this-command)
  12270. org-lowest-priority ; wrap around empty to lowest
  12271. ;; default
  12272. (if org-priority-start-cycle-with-default
  12273. org-default-priority
  12274. (1- org-default-priority))))))
  12275. ((eq action 'down)
  12276. (setq new (if have
  12277. (1+ current) ; normal cycling
  12278. ;; last priority was empty
  12279. (if (eq last-command this-command)
  12280. org-highest-priority ; wrap around empty to highest
  12281. ;; default
  12282. (if org-priority-start-cycle-with-default
  12283. org-default-priority
  12284. (1+ org-default-priority))))))
  12285. (t (error "Invalid action")))
  12286. (if (or (< (upcase new) org-highest-priority)
  12287. (> (upcase new) org-lowest-priority))
  12288. (if (and (memq action '(up down))
  12289. (not have) (not (eq last-command this-command)))
  12290. ;; `new' is from default priority
  12291. (error
  12292. "The default can not be set, see `org-default-priority' why")
  12293. ;; normal cycling: `new' is beyond highest/lowest priority
  12294. ;; and is wrapped around to the empty priority
  12295. (setq remove t)))
  12296. (setq news (format "%c" new))
  12297. (if have
  12298. (if remove
  12299. (replace-match "" t t nil 1)
  12300. (replace-match news t t nil 2))
  12301. (if remove
  12302. (error "No priority cookie found in line")
  12303. (let ((case-fold-search nil))
  12304. (looking-at org-todo-line-regexp))
  12305. (if (match-end 2)
  12306. (progn
  12307. (goto-char (match-end 2))
  12308. (insert " [#" news "]"))
  12309. (goto-char (match-beginning 3))
  12310. (insert "[#" news "] "))))
  12311. (org-preserve-lc (org-set-tags nil 'align)))
  12312. (if remove
  12313. (message "Priority removed")
  12314. (message "Priority of current item set to %s" news)))))
  12315. (defun org-show-priority ()
  12316. "Show the priority of the current item.
  12317. This priority is composed of the main priority given with the [#A] cookies,
  12318. and by additional input from the age of a schedules or deadline entry."
  12319. (interactive)
  12320. (let ((pri (if (eq major-mode 'org-agenda-mode)
  12321. (org-get-at-bol 'priority)
  12322. (save-excursion
  12323. (save-match-data
  12324. (beginning-of-line)
  12325. (and (looking-at org-heading-regexp)
  12326. (org-get-priority (match-string 0))))))))
  12327. (message "Priority is %d" (if pri pri -1000))))
  12328. (defun org-get-priority (s)
  12329. "Find priority cookie and return priority."
  12330. (save-match-data
  12331. (if (functionp org-get-priority-function)
  12332. (funcall org-get-priority-function)
  12333. (if (not (string-match org-priority-regexp s))
  12334. (* 1000 (- org-lowest-priority org-default-priority))
  12335. (* 1000 (- org-lowest-priority
  12336. (string-to-char (match-string 2 s))))))))
  12337. ;;;; Tags
  12338. (defvar org-agenda-archives-mode)
  12339. (defvar org-map-continue-from nil
  12340. "Position from where mapping should continue.
  12341. Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
  12342. (defvar org-scanner-tags nil
  12343. "The current tag list while the tags scanner is running.")
  12344. (defvar org-trust-scanner-tags nil
  12345. "Should `org-get-tags-at' use the tags for the scanner.
  12346. This is for internal dynamical scoping only.
  12347. When this is non-nil, the function `org-get-tags-at' will return the value
  12348. of `org-scanner-tags' instead of building the list by itself. This
  12349. can lead to large speed-ups when the tags scanner is used in a file with
  12350. many entries, and when the list of tags is retrieved, for example to
  12351. obtain a list of properties. Building the tags list for each entry in such
  12352. a file becomes an N^2 operation - but with this variable set, it scales
  12353. as N.")
  12354. (defun org-scan-tags (action matcher todo-only &optional start-level)
  12355. "Sca headline tags with inheritance and produce output ACTION.
  12356. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  12357. or `agenda' to produce an entry list for an agenda view. It can also be
  12358. a Lisp form or a function that should be called at each matched headline, in
  12359. this case the return value is a list of all return values from these calls.
  12360. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  12361. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  12362. only lines with a not-done TODO keyword are included in the output.
  12363. This should be the same variable that was scoped into
  12364. and set by `org-make-tags-matcher' when it constructed MATCHER.
  12365. START-LEVEL can be a string with asterisks, reducing the scope to
  12366. headlines matching this string."
  12367. (require 'org-agenda)
  12368. (let* ((re (concat "^"
  12369. (if start-level
  12370. ;; Get the correct level to match
  12371. (concat "\\*\\{" (number-to-string start-level) "\\} ")
  12372. org-outline-regexp)
  12373. " *\\(\\<\\("
  12374. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  12375. (org-re "\\)\\>\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  12376. (props (list 'face 'default
  12377. 'done-face 'org-agenda-done
  12378. 'undone-face 'default
  12379. 'mouse-face 'highlight
  12380. 'org-not-done-regexp org-not-done-regexp
  12381. 'org-todo-regexp org-todo-regexp
  12382. 'org-complex-heading-regexp org-complex-heading-regexp
  12383. 'help-echo
  12384. (format "mouse-2 or RET jump to org file %s"
  12385. (abbreviate-file-name
  12386. (or (buffer-file-name (buffer-base-buffer))
  12387. (buffer-name (buffer-base-buffer)))))))
  12388. (case-fold-search nil)
  12389. (org-map-continue-from nil)
  12390. lspos tags tags-list
  12391. (tags-alist (list (cons 0 org-file-tags)))
  12392. (llast 0) rtn rtn1 level category i txt
  12393. todo marker entry priority)
  12394. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  12395. (setq action (list 'lambda nil action)))
  12396. (save-excursion
  12397. (goto-char (point-min))
  12398. (when (eq action 'sparse-tree)
  12399. (org-overview)
  12400. (org-remove-occur-highlights))
  12401. (while (re-search-forward re nil t)
  12402. (setq org-map-continue-from nil)
  12403. (catch :skip
  12404. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  12405. tags (if (match-end 4) (org-match-string-no-properties 4)))
  12406. (goto-char (setq lspos (match-beginning 0)))
  12407. (setq level (org-reduced-level (org-outline-level))
  12408. category (org-get-category))
  12409. (setq i llast llast level)
  12410. ;; remove tag lists from same and sublevels
  12411. (while (>= i level)
  12412. (when (setq entry (assoc i tags-alist))
  12413. (setq tags-alist (delete entry tags-alist)))
  12414. (setq i (1- i)))
  12415. ;; add the next tags
  12416. (when tags
  12417. (setq tags (org-split-string tags ":")
  12418. tags-alist
  12419. (cons (cons level tags) tags-alist)))
  12420. ;; compile tags for current headline
  12421. (setq tags-list
  12422. (if org-use-tag-inheritance
  12423. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  12424. tags)
  12425. org-scanner-tags tags-list)
  12426. (when org-use-tag-inheritance
  12427. (setcdr (car tags-alist)
  12428. (mapcar (lambda (x)
  12429. (setq x (copy-sequence x))
  12430. (org-add-prop-inherited x))
  12431. (cdar tags-alist))))
  12432. (when (and tags org-use-tag-inheritance
  12433. (or (not (eq t org-use-tag-inheritance))
  12434. org-tags-exclude-from-inheritance))
  12435. ;; selective inheritance, remove uninherited ones
  12436. (setcdr (car tags-alist)
  12437. (org-remove-uninherited-tags (cdar tags-alist))))
  12438. (when (and
  12439. ;; eval matcher only when the todo condition is OK
  12440. (and (or (not todo-only) (member todo org-not-done-keywords))
  12441. (let ((case-fold-search t) (org-trust-scanner-tags t))
  12442. (eval matcher)))
  12443. ;; Call the skipper, but return t if it does not skip,
  12444. ;; so that the `and' form continues evaluating
  12445. (progn
  12446. (unless (eq action 'sparse-tree) (org-agenda-skip))
  12447. t)
  12448. ;; Check if timestamps are deselecting this entry
  12449. (or (not todo-only)
  12450. (and (member todo org-not-done-keywords)
  12451. (or (not org-agenda-tags-todo-honor-ignore-options)
  12452. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item))))))
  12453. ;; select this headline
  12454. (cond
  12455. ((eq action 'sparse-tree)
  12456. (and org-highlight-sparse-tree-matches
  12457. (org-get-heading) (match-end 0)
  12458. (org-highlight-new-match
  12459. (match-beginning 1) (match-end 1)))
  12460. (org-show-context 'tags-tree))
  12461. ((eq action 'agenda)
  12462. (setq txt (org-agenda-format-item
  12463. ""
  12464. (concat
  12465. (if (eq org-tags-match-list-sublevels 'indented)
  12466. (make-string (1- level) ?.) "")
  12467. (org-get-heading))
  12468. level category
  12469. tags-list)
  12470. priority (org-get-priority txt))
  12471. (goto-char lspos)
  12472. (setq marker (org-agenda-new-marker))
  12473. (org-add-props txt props
  12474. 'org-marker marker 'org-hd-marker marker 'org-category category
  12475. 'todo-state todo
  12476. 'priority priority 'type "tagsmatch")
  12477. (push txt rtn))
  12478. ((functionp action)
  12479. (setq org-map-continue-from nil)
  12480. (save-excursion
  12481. (setq rtn1 (funcall action))
  12482. (push rtn1 rtn)))
  12483. (t (error "Invalid action")))
  12484. ;; if we are to skip sublevels, jump to end of subtree
  12485. (unless org-tags-match-list-sublevels
  12486. (org-end-of-subtree t)
  12487. (backward-char 1))))
  12488. ;; Get the correct position from where to continue
  12489. (if org-map-continue-from
  12490. (goto-char org-map-continue-from)
  12491. (and (= (point) lspos) (end-of-line 1)))))
  12492. (when (and (eq action 'sparse-tree)
  12493. (not org-sparse-tree-open-archived-trees))
  12494. (org-hide-archived-subtrees (point-min) (point-max)))
  12495. (nreverse rtn)))
  12496. (defun org-remove-uninherited-tags (tags)
  12497. "Remove all tags that are not inherited from the list TAGS."
  12498. (cond
  12499. ((eq org-use-tag-inheritance t)
  12500. (if org-tags-exclude-from-inheritance
  12501. (org-delete-all org-tags-exclude-from-inheritance tags)
  12502. tags))
  12503. ((not org-use-tag-inheritance) nil)
  12504. ((stringp org-use-tag-inheritance)
  12505. (delq nil (mapcar
  12506. (lambda (x)
  12507. (if (and (string-match org-use-tag-inheritance x)
  12508. (not (member x org-tags-exclude-from-inheritance)))
  12509. x nil))
  12510. tags)))
  12511. ((listp org-use-tag-inheritance)
  12512. (delq nil (mapcar
  12513. (lambda (x)
  12514. (if (member x org-use-tag-inheritance) x nil))
  12515. tags)))))
  12516. (defun org-match-sparse-tree (&optional todo-only match)
  12517. "Create a sparse tree according to tags string MATCH.
  12518. MATCH can contain positive and negative selection of tags, like
  12519. \"+WORK+URGENT-WITHBOSS\".
  12520. If optional argument TODO-ONLY is non-nil, only select lines that are
  12521. also TODO lines."
  12522. (interactive "P")
  12523. (org-agenda-prepare-buffers (list (current-buffer)))
  12524. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  12525. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  12526. (defvar org-cached-props nil)
  12527. (defun org-cached-entry-get (pom property)
  12528. (if (or (eq t org-use-property-inheritance)
  12529. (and (stringp org-use-property-inheritance)
  12530. (string-match org-use-property-inheritance property))
  12531. (and (listp org-use-property-inheritance)
  12532. (member property org-use-property-inheritance)))
  12533. ;; Caching is not possible, check it directly
  12534. (org-entry-get pom property 'inherit)
  12535. ;; Get all properties, so that we can do complicated checks easily
  12536. (cdr (assoc property (or org-cached-props
  12537. (setq org-cached-props
  12538. (org-entry-properties pom)))))))
  12539. (defun org-global-tags-completion-table (&optional files)
  12540. "Return the list of all tags in all agenda buffer/files.
  12541. Optional FILES argument is a list of files which can be used
  12542. instead of the agenda files."
  12543. (save-excursion
  12544. (org-uniquify
  12545. (delq nil
  12546. (apply 'append
  12547. (mapcar
  12548. (lambda (file)
  12549. (set-buffer (find-file-noselect file))
  12550. (append (org-get-buffer-tags)
  12551. (mapcar (lambda (x) (if (stringp (car-safe x))
  12552. (list (car-safe x)) nil))
  12553. org-tag-alist)))
  12554. (if (and files (car files))
  12555. files
  12556. (org-agenda-files))))))))
  12557. (defun org-make-tags-matcher (match)
  12558. "Create the TAGS/TODO matcher form for the selection string MATCH.
  12559. The variable `todo-only' is scoped dynamically into this function.
  12560. It will be set to t if the matcher restricts matching to TODO entries,
  12561. otherwise will not be touched.
  12562. Returns a cons of the selection string MATCH and the constructed
  12563. lisp form implementing the matcher. The matcher is to be evaluated
  12564. at an Org entry, with point on the headline, and returns t if the
  12565. entry matches the selection string MATCH. The returned lisp form
  12566. references two variables with information about the entry, which
  12567. must be bound around the form's evaluation: todo, the TODO keyword
  12568. at the entry (or nil of none); and tags-list, the list of all tags
  12569. at the entry including inherited ones. Additionally, the category
  12570. of the entry (if any) must be specified as the text property
  12571. 'org-category on the headline.
  12572. See also `org-scan-tags'.
  12573. "
  12574. (declare (special todo-only))
  12575. (unless (boundp 'todo-only)
  12576. (error "org-make-tags-matcher expects todo-only to be scoped in"))
  12577. (unless match
  12578. ;; Get a new match request, with completion
  12579. (let ((org-last-tags-completion-table
  12580. (org-global-tags-completion-table)))
  12581. (setq match (org-completing-read-no-i
  12582. "Match: " 'org-tags-completion-function nil nil nil
  12583. 'org-tags-history))))
  12584. ;; Parse the string and create a lisp form
  12585. (let ((match0 match)
  12586. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  12587. minus tag mm
  12588. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  12589. orterms term orlist re-p str-p level-p level-op time-p
  12590. prop-p pn pv po gv rest)
  12591. (if (string-match "/+" match)
  12592. ;; match contains also a todo-matching request
  12593. (progn
  12594. (setq tagsmatch (substring match 0 (match-beginning 0))
  12595. todomatch (substring match (match-end 0)))
  12596. (if (string-match "^!" todomatch)
  12597. (setq todo-only t todomatch (substring todomatch 1)))
  12598. (if (string-match "^\\s-*$" todomatch)
  12599. (setq todomatch nil)))
  12600. ;; only matching tags
  12601. (setq tagsmatch match todomatch nil))
  12602. ;; Make the tags matcher
  12603. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  12604. (setq tagsmatcher t)
  12605. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  12606. (while (setq term (pop orterms))
  12607. (while (and (equal (substring term -1) "\\") orterms)
  12608. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  12609. (while (string-match re term)
  12610. (setq rest (substring term (match-end 0))
  12611. minus (and (match-end 1)
  12612. (equal (match-string 1 term) "-"))
  12613. tag (save-match-data (replace-regexp-in-string
  12614. "\\\\-" "-"
  12615. (match-string 2 term)))
  12616. re-p (equal (string-to-char tag) ?{)
  12617. level-p (match-end 4)
  12618. prop-p (match-end 5)
  12619. mm (cond
  12620. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  12621. (level-p
  12622. (setq level-op (org-op-to-function (match-string 3 term)))
  12623. `(,level-op level ,(string-to-number
  12624. (match-string 4 term))))
  12625. (prop-p
  12626. (setq pn (match-string 5 term)
  12627. po (match-string 6 term)
  12628. pv (match-string 7 term)
  12629. re-p (equal (string-to-char pv) ?{)
  12630. str-p (equal (string-to-char pv) ?\")
  12631. time-p (save-match-data
  12632. (string-match "^\"[[<].*[]>]\"$" pv))
  12633. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  12634. (if time-p (setq pv (org-matcher-time pv)))
  12635. (setq po (org-op-to-function po (if time-p 'time str-p)))
  12636. (cond
  12637. ((equal pn "CATEGORY")
  12638. (setq gv '(get-text-property (point) 'org-category)))
  12639. ((equal pn "TODO")
  12640. (setq gv 'todo))
  12641. (t
  12642. (setq gv `(org-cached-entry-get nil ,pn))))
  12643. (if re-p
  12644. (if (eq po 'org<>)
  12645. `(not (string-match ,pv (or ,gv "")))
  12646. `(string-match ,pv (or ,gv "")))
  12647. (if str-p
  12648. `(,po (or ,gv "") ,pv)
  12649. `(,po (string-to-number (or ,gv ""))
  12650. ,(string-to-number pv) ))))
  12651. (t `(member ,tag tags-list)))
  12652. mm (if minus (list 'not mm) mm)
  12653. term rest)
  12654. (push mm tagsmatcher))
  12655. (push (if (> (length tagsmatcher) 1)
  12656. (cons 'and tagsmatcher)
  12657. (car tagsmatcher))
  12658. orlist)
  12659. (setq tagsmatcher nil))
  12660. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  12661. (setq tagsmatcher
  12662. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  12663. ;; Make the todo matcher
  12664. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  12665. (setq todomatcher t)
  12666. (setq orterms (org-split-string todomatch "|") orlist nil)
  12667. (while (setq term (pop orterms))
  12668. (while (string-match re term)
  12669. (setq minus (and (match-end 1)
  12670. (equal (match-string 1 term) "-"))
  12671. kwd (match-string 2 term)
  12672. re-p (equal (string-to-char kwd) ?{)
  12673. term (substring term (match-end 0))
  12674. mm (if re-p
  12675. `(string-match ,(substring kwd 1 -1) todo)
  12676. (list 'equal 'todo kwd))
  12677. mm (if minus (list 'not mm) mm))
  12678. (push mm todomatcher))
  12679. (push (if (> (length todomatcher) 1)
  12680. (cons 'and todomatcher)
  12681. (car todomatcher))
  12682. orlist)
  12683. (setq todomatcher nil))
  12684. (setq todomatcher (if (> (length orlist) 1)
  12685. (cons 'or orlist) (car orlist))))
  12686. ;; Return the string and lisp forms of the matcher
  12687. (setq matcher (if todomatcher
  12688. (list 'and tagsmatcher todomatcher)
  12689. tagsmatcher))
  12690. (when todo-only
  12691. (setq matcher (list 'and '(member todo org-not-done-keywords)
  12692. matcher)))
  12693. (cons match0 matcher)))
  12694. (defun org-op-to-function (op &optional stringp)
  12695. "Turn an operator into the appropriate function."
  12696. (setq op
  12697. (cond
  12698. ((equal op "<" ) '(< string< org-time<))
  12699. ((equal op ">" ) '(> org-string> org-time>))
  12700. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  12701. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  12702. ((member op '("=" "==")) '(= string= org-time=))
  12703. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  12704. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  12705. (defun org<> (a b) (not (= a b)))
  12706. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  12707. (defun org-string>= (a b) (not (string< a b)))
  12708. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  12709. (defun org-string<> (a b) (not (string= a b)))
  12710. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  12711. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  12712. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  12713. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  12714. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  12715. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  12716. (defun org-2ft (s)
  12717. "Convert S to a floating point time.
  12718. If S is already a number, just return it. If it is a string, parse
  12719. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  12720. (cond
  12721. ((numberp s) s)
  12722. ((stringp s)
  12723. (condition-case nil
  12724. (float-time (apply 'encode-time (org-parse-time-string s)))
  12725. (error 0.)))
  12726. (t 0.)))
  12727. (defun org-time-today ()
  12728. "Time in seconds today at 0:00.
  12729. Returns the float number of seconds since the beginning of the
  12730. epoch to the beginning of today (00:00)."
  12731. (float-time (apply 'encode-time
  12732. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  12733. (defun org-matcher-time (s)
  12734. "Interpret a time comparison value."
  12735. (save-match-data
  12736. (cond
  12737. ((string= s "<now>") (float-time))
  12738. ((string= s "<today>") (org-time-today))
  12739. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  12740. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  12741. ((string-match "^<\\([-+][0-9]+\\)\\([hdwmy]\\)>$" s)
  12742. (+ (org-time-today)
  12743. (* (string-to-number (match-string 1 s))
  12744. (cdr (assoc (match-string 2 s)
  12745. '(("d" . 86400.0) ("w" . 604800.0)
  12746. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  12747. (t (org-2ft s)))))
  12748. (defun org-match-any-p (re list)
  12749. "Does re match any element of list?"
  12750. (setq list (mapcar (lambda (x) (string-match re x)) list))
  12751. (delq nil list))
  12752. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  12753. (defvar org-tags-overlay (make-overlay 1 1))
  12754. (org-detach-overlay org-tags-overlay)
  12755. (defun org-get-local-tags-at (&optional pos)
  12756. "Get a list of tags defined in the current headline."
  12757. (org-get-tags-at pos 'local))
  12758. (defun org-get-local-tags ()
  12759. "Get a list of tags defined in the current headline."
  12760. (org-get-tags-at nil 'local))
  12761. (defun org-get-tags-at (&optional pos local)
  12762. "Get a list of all headline tags applicable at POS.
  12763. POS defaults to point. If tags are inherited, the list contains
  12764. the targets in the same sequence as the headlines appear, i.e.
  12765. the tags of the current headline come last.
  12766. When LOCAL is non-nil, only return tags from the current headline,
  12767. ignore inherited ones."
  12768. (interactive)
  12769. (if (and org-trust-scanner-tags
  12770. (or (not pos) (equal pos (point)))
  12771. (not local))
  12772. org-scanner-tags
  12773. (let (tags ltags lastpos parent)
  12774. (save-excursion
  12775. (save-restriction
  12776. (widen)
  12777. (goto-char (or pos (point)))
  12778. (save-match-data
  12779. (catch 'done
  12780. (condition-case nil
  12781. (progn
  12782. (org-back-to-heading t)
  12783. (while (not (equal lastpos (point)))
  12784. (setq lastpos (point))
  12785. (when (looking-at
  12786. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  12787. (setq ltags (org-split-string
  12788. (org-match-string-no-properties 1) ":"))
  12789. (when parent
  12790. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  12791. (setq tags (append
  12792. (if parent
  12793. (org-remove-uninherited-tags ltags)
  12794. ltags)
  12795. tags)))
  12796. (or org-use-tag-inheritance (throw 'done t))
  12797. (if local (throw 'done t))
  12798. (or (org-up-heading-safe) (error nil))
  12799. (setq parent t)))
  12800. (error nil)))))
  12801. (if local
  12802. tags
  12803. (reverse (delete-dups
  12804. (reverse (append
  12805. (org-remove-uninherited-tags
  12806. org-file-tags) tags)))))))))
  12807. (defun org-add-prop-inherited (s)
  12808. (add-text-properties 0 (length s) '(inherited t) s)
  12809. s)
  12810. (defun org-toggle-tag (tag &optional onoff)
  12811. "Toggle the tag TAG for the current line.
  12812. If ONOFF is `on' or `off', don't toggle but set to this state."
  12813. (let (res current)
  12814. (save-excursion
  12815. (org-back-to-heading t)
  12816. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  12817. (point-at-eol) t)
  12818. (progn
  12819. (setq current (match-string 1))
  12820. (replace-match ""))
  12821. (setq current ""))
  12822. (setq current (nreverse (org-split-string current ":")))
  12823. (cond
  12824. ((eq onoff 'on)
  12825. (setq res t)
  12826. (or (member tag current) (push tag current)))
  12827. ((eq onoff 'off)
  12828. (or (not (member tag current)) (setq current (delete tag current))))
  12829. (t (if (member tag current)
  12830. (setq current (delete tag current))
  12831. (setq res t)
  12832. (push tag current))))
  12833. (end-of-line 1)
  12834. (if current
  12835. (progn
  12836. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  12837. (org-set-tags nil t))
  12838. (delete-horizontal-space))
  12839. (run-hooks 'org-after-tags-change-hook))
  12840. res))
  12841. (defun org-align-tags-here (to-col)
  12842. ;; Assumes that this is a headline
  12843. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  12844. (beginning-of-line 1)
  12845. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12846. (< pos (match-beginning 2)))
  12847. (progn
  12848. (setq tags-l (- (match-end 2) (match-beginning 2)))
  12849. (goto-char (match-beginning 1))
  12850. (insert " ")
  12851. (delete-region (point) (1+ (match-beginning 2)))
  12852. (setq ncol (max (current-column)
  12853. (1+ col)
  12854. (if (> to-col 0)
  12855. to-col
  12856. (- (abs to-col) tags-l))))
  12857. (setq p (point))
  12858. (insert (make-string (- ncol (current-column)) ?\ ))
  12859. (setq ncol (current-column))
  12860. (when indent-tabs-mode (tabify p (point-at-eol)))
  12861. (org-move-to-column (min ncol col) t))
  12862. (goto-char pos))))
  12863. (defun org-set-tags-command (&optional arg just-align)
  12864. "Call the set-tags command for the current entry."
  12865. (interactive "P")
  12866. (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
  12867. (org-set-tags arg just-align)
  12868. (save-excursion
  12869. (unless (and (org-region-active-p)
  12870. org-loop-over-headlines-in-active-region)
  12871. (org-back-to-heading t))
  12872. (org-set-tags arg just-align))))
  12873. (defun org-set-tags-to (data)
  12874. "Set the tags of the current entry to DATA, replacing the current tags.
  12875. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  12876. If DATA is nil or the empty string, any tags will be removed."
  12877. (interactive "sTags: ")
  12878. (setq data
  12879. (cond
  12880. ((eq data nil) "")
  12881. ((equal data "") "")
  12882. ((stringp data)
  12883. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  12884. ":"))
  12885. ((listp data)
  12886. (concat ":" (mapconcat 'identity data ":") ":"))))
  12887. (when data
  12888. (save-excursion
  12889. (org-back-to-heading t)
  12890. (when (looking-at org-complex-heading-regexp)
  12891. (if (match-end 5)
  12892. (progn
  12893. (goto-char (match-beginning 5))
  12894. (insert data)
  12895. (delete-region (point) (point-at-eol))
  12896. (org-set-tags nil 'align))
  12897. (goto-char (point-at-eol))
  12898. (insert " " data)
  12899. (org-set-tags nil 'align)))
  12900. (beginning-of-line 1)
  12901. (if (looking-at ".*?\\([ \t]+\\)$")
  12902. (delete-region (match-beginning 1) (match-end 1))))))
  12903. (defun org-align-all-tags ()
  12904. "Align the tags i all headings."
  12905. (interactive)
  12906. (save-excursion
  12907. (or (ignore-errors (org-back-to-heading t))
  12908. (outline-next-heading))
  12909. (if (org-at-heading-p)
  12910. (org-set-tags t)
  12911. (message "No headings"))))
  12912. (defvar org-indent-indentation-per-level)
  12913. (defun org-set-tags (&optional arg just-align)
  12914. "Set the tags for the current headline.
  12915. With prefix ARG, realign all tags in headings in the current buffer."
  12916. (interactive "P")
  12917. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  12918. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  12919. 'region-start-level 'region))
  12920. org-loop-over-headlines-in-active-region)
  12921. (org-map-entries
  12922. ;; We don't use ARG and JUST-ALIGN here these args are not
  12923. ;; useful when looping over headlines
  12924. `(org-set-tags)
  12925. org-loop-over-headlines-in-active-region
  12926. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  12927. (let* ((re org-outline-regexp-bol)
  12928. (current (unless arg (org-get-tags-string)))
  12929. (col (current-column))
  12930. (org-setting-tags t)
  12931. table current-tags inherited-tags ; computed below when needed
  12932. tags p0 c0 c1 rpl di tc level)
  12933. (if arg
  12934. (save-excursion
  12935. (goto-char (point-min))
  12936. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  12937. (while (re-search-forward re nil t)
  12938. (org-set-tags nil t)
  12939. (end-of-line 1)))
  12940. (message "All tags realigned to column %d" org-tags-column))
  12941. (if just-align
  12942. (setq tags current)
  12943. ;; Get a new set of tags from the user
  12944. (save-excursion
  12945. (setq table (append org-tag-persistent-alist
  12946. (or org-tag-alist (org-get-buffer-tags))
  12947. (and
  12948. org-complete-tags-always-offer-all-agenda-tags
  12949. (org-global-tags-completion-table
  12950. (org-agenda-files))))
  12951. org-last-tags-completion-table table
  12952. current-tags (org-split-string current ":")
  12953. inherited-tags (nreverse
  12954. (nthcdr (length current-tags)
  12955. (nreverse (org-get-tags-at))))
  12956. tags
  12957. (if (or (eq t org-use-fast-tag-selection)
  12958. (and org-use-fast-tag-selection
  12959. (delq nil (mapcar 'cdr table))))
  12960. (org-fast-tag-selection
  12961. current-tags inherited-tags table
  12962. (if org-fast-tag-selection-include-todo
  12963. org-todo-key-alist))
  12964. (let ((org-add-colon-after-tag-completion (< 1 (length table))))
  12965. (org-trim
  12966. (org-icompleting-read "Tags: "
  12967. 'org-tags-completion-function
  12968. nil nil current 'org-tags-history))))))
  12969. (while (string-match "[-+&]+" tags)
  12970. ;; No boolean logic, just a list
  12971. (setq tags (replace-match ":" t t tags))))
  12972. (setq tags (replace-regexp-in-string "[,]" ":" tags))
  12973. (if org-tags-sort-function
  12974. (setq tags (mapconcat 'identity
  12975. (sort (org-split-string
  12976. tags (org-re "[^[:alnum:]_@#%]+"))
  12977. org-tags-sort-function) ":")))
  12978. (if (string-match "\\`[\t ]*\\'" tags)
  12979. (setq tags "")
  12980. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  12981. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  12982. ;; Insert new tags at the correct column
  12983. (beginning-of-line 1)
  12984. (setq level (or (and (looking-at org-outline-regexp)
  12985. (- (match-end 0) (point) 1))
  12986. 1))
  12987. (cond
  12988. ((and (equal current "") (equal tags "")))
  12989. ((re-search-forward
  12990. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  12991. (point-at-eol) t)
  12992. (if (equal tags "")
  12993. (setq rpl "")
  12994. (goto-char (match-beginning 0))
  12995. (setq c0 (current-column)
  12996. ;; compute offset for the case of org-indent-mode active
  12997. di (if org-indent-mode
  12998. (* (1- org-indent-indentation-per-level) (1- level))
  12999. 0)
  13000. p0 (if (equal (char-before) ?*) (1+ (point)) (point))
  13001. tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
  13002. c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (length tags))))
  13003. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  13004. (replace-match rpl t t)
  13005. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  13006. tags)
  13007. (t (error "Tags alignment failed")))
  13008. (org-move-to-column col)
  13009. (unless just-align
  13010. (run-hooks 'org-after-tags-change-hook))))))
  13011. (defun org-change-tag-in-region (beg end tag off)
  13012. "Add or remove TAG for each entry in the region.
  13013. This works in the agenda, and also in an org-mode buffer."
  13014. (interactive
  13015. (list (region-beginning) (region-end)
  13016. (let ((org-last-tags-completion-table
  13017. (if (derived-mode-p 'org-mode)
  13018. (org-get-buffer-tags)
  13019. (org-global-tags-completion-table))))
  13020. (org-icompleting-read
  13021. "Tag: " 'org-tags-completion-function nil nil nil
  13022. 'org-tags-history))
  13023. (progn
  13024. (message "[s]et or [r]emove? ")
  13025. (equal (read-char-exclusive) ?r))))
  13026. (if (fboundp 'deactivate-mark) (deactivate-mark))
  13027. (let ((agendap (equal major-mode 'org-agenda-mode))
  13028. l1 l2 m buf pos newhead (cnt 0))
  13029. (goto-char end)
  13030. (setq l2 (1- (org-current-line)))
  13031. (goto-char beg)
  13032. (setq l1 (org-current-line))
  13033. (loop for l from l1 to l2 do
  13034. (org-goto-line l)
  13035. (setq m (get-text-property (point) 'org-hd-marker))
  13036. (when (or (and (derived-mode-p 'org-mode) (org-at-heading-p))
  13037. (and agendap m))
  13038. (setq buf (if agendap (marker-buffer m) (current-buffer))
  13039. pos (if agendap m (point)))
  13040. (with-current-buffer buf
  13041. (save-excursion
  13042. (save-restriction
  13043. (goto-char pos)
  13044. (setq cnt (1+ cnt))
  13045. (org-toggle-tag tag (if off 'off 'on))
  13046. (setq newhead (org-get-heading)))))
  13047. (and agendap (org-agenda-change-all-lines newhead m))))
  13048. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  13049. (defun org-tags-completion-function (string predicate &optional flag)
  13050. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  13051. (confirm (lambda (x) (stringp (car x)))))
  13052. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  13053. (setq s1 (match-string 1 string)
  13054. s2 (match-string 2 string))
  13055. (setq s1 "" s2 string))
  13056. (cond
  13057. ((eq flag nil)
  13058. ;; try completion
  13059. (setq rtn (try-completion s2 ctable confirm))
  13060. (if (stringp rtn)
  13061. (setq rtn
  13062. (concat s1 s2 (substring rtn (length s2))
  13063. (if (and org-add-colon-after-tag-completion
  13064. (assoc rtn ctable))
  13065. ":" ""))))
  13066. rtn)
  13067. ((eq flag t)
  13068. ;; all-completions
  13069. (all-completions s2 ctable confirm)
  13070. )
  13071. ((eq flag 'lambda)
  13072. ;; exact match?
  13073. (assoc s2 ctable)))
  13074. ))
  13075. (defun org-fast-tag-insert (kwd tags face &optional end)
  13076. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  13077. (insert (format "%-12s" (concat kwd ":"))
  13078. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  13079. (or end "")))
  13080. (defun org-fast-tag-show-exit (flag)
  13081. (save-excursion
  13082. (org-goto-line 3)
  13083. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  13084. (replace-match ""))
  13085. (when flag
  13086. (end-of-line 1)
  13087. (org-move-to-column (- (window-width) 19) t)
  13088. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  13089. (defun org-set-current-tags-overlay (current prefix)
  13090. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  13091. (if (featurep 'xemacs)
  13092. (org-overlay-display org-tags-overlay (concat prefix s)
  13093. 'secondary-selection)
  13094. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  13095. (org-overlay-display org-tags-overlay (concat prefix s)))))
  13096. (defvar org-last-tag-selection-key nil)
  13097. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  13098. "Fast tag selection with single keys.
  13099. CURRENT is the current list of tags in the headline, INHERITED is the
  13100. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  13101. possibly with grouping information. TODO-TABLE is a similar table with
  13102. TODO keywords, should these have keys assigned to them.
  13103. If the keys are nil, a-z are automatically assigned.
  13104. Returns the new tags string, or nil to not change the current settings."
  13105. (let* ((fulltable (append table todo-table))
  13106. (maxlen (apply 'max (mapcar
  13107. (lambda (x)
  13108. (if (stringp (car x)) (string-width (car x)) 0))
  13109. fulltable)))
  13110. (buf (current-buffer))
  13111. (expert (eq org-fast-tag-selection-single-key 'expert))
  13112. (buffer-tags nil)
  13113. (fwidth (+ maxlen 3 1 3))
  13114. (ncol (/ (- (window-width) 4) fwidth))
  13115. (i-face 'org-done)
  13116. (c-face 'org-todo)
  13117. tg cnt e c char c1 c2 ntable tbl rtn
  13118. ov-start ov-end ov-prefix
  13119. (exit-after-next org-fast-tag-selection-single-key)
  13120. (done-keywords org-done-keywords)
  13121. groups ingroup)
  13122. (save-excursion
  13123. (beginning-of-line 1)
  13124. (if (looking-at
  13125. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13126. (setq ov-start (match-beginning 1)
  13127. ov-end (match-end 1)
  13128. ov-prefix "")
  13129. (setq ov-start (1- (point-at-eol))
  13130. ov-end (1+ ov-start))
  13131. (skip-chars-forward "^\n\r")
  13132. (setq ov-prefix
  13133. (concat
  13134. (buffer-substring (1- (point)) (point))
  13135. (if (> (current-column) org-tags-column)
  13136. " "
  13137. (make-string (- org-tags-column (current-column)) ?\ ))))))
  13138. (move-overlay org-tags-overlay ov-start ov-end)
  13139. (save-window-excursion
  13140. (if expert
  13141. (set-buffer (get-buffer-create " *Org tags*"))
  13142. (delete-other-windows)
  13143. (split-window-vertically)
  13144. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  13145. (erase-buffer)
  13146. (org-set-local 'org-done-keywords done-keywords)
  13147. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  13148. (org-fast-tag-insert "Current" current c-face "\n\n")
  13149. (org-fast-tag-show-exit exit-after-next)
  13150. (org-set-current-tags-overlay current ov-prefix)
  13151. (setq tbl fulltable char ?a cnt 0)
  13152. (while (setq e (pop tbl))
  13153. (cond
  13154. ((equal (car e) :startgroup)
  13155. (push '() groups) (setq ingroup t)
  13156. (when (not (= cnt 0))
  13157. (setq cnt 0)
  13158. (insert "\n"))
  13159. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  13160. ((equal (car e) :endgroup)
  13161. (setq ingroup nil cnt 0)
  13162. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  13163. ((equal e '(:newline))
  13164. (when (not (= cnt 0))
  13165. (setq cnt 0)
  13166. (insert "\n")
  13167. (setq e (car tbl))
  13168. (while (equal (car tbl) '(:newline))
  13169. (insert "\n")
  13170. (setq tbl (cdr tbl)))))
  13171. (t
  13172. (setq tg (copy-sequence (car e)) c2 nil)
  13173. (if (cdr e)
  13174. (setq c (cdr e))
  13175. ;; automatically assign a character.
  13176. (setq c1 (string-to-char
  13177. (downcase (substring
  13178. tg (if (= (string-to-char tg) ?@) 1 0)))))
  13179. (if (or (rassoc c1 ntable) (rassoc c1 table))
  13180. (while (or (rassoc char ntable) (rassoc char table))
  13181. (setq char (1+ char)))
  13182. (setq c2 c1))
  13183. (setq c (or c2 char)))
  13184. (if ingroup (push tg (car groups)))
  13185. (setq tg (org-add-props tg nil 'face
  13186. (cond
  13187. ((not (assoc tg table))
  13188. (org-get-todo-face tg))
  13189. ((member tg current) c-face)
  13190. ((member tg inherited) i-face))))
  13191. (if (and (= cnt 0) (not ingroup)) (insert " "))
  13192. (insert "[" c "] " tg (make-string
  13193. (- fwidth 4 (length tg)) ?\ ))
  13194. (push (cons tg c) ntable)
  13195. (when (= (setq cnt (1+ cnt)) ncol)
  13196. (insert "\n")
  13197. (if ingroup (insert " "))
  13198. (setq cnt 0)))))
  13199. (setq ntable (nreverse ntable))
  13200. (insert "\n")
  13201. (goto-char (point-min))
  13202. (if (not expert) (org-fit-window-to-buffer))
  13203. (setq rtn
  13204. (catch 'exit
  13205. (while t
  13206. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  13207. (if (not groups) "no " "")
  13208. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  13209. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  13210. (setq org-last-tag-selection-key c)
  13211. (cond
  13212. ((= c ?\r) (throw 'exit t))
  13213. ((= c ?!)
  13214. (setq groups (not groups))
  13215. (goto-char (point-min))
  13216. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  13217. ((= c ?\C-c)
  13218. (if (not expert)
  13219. (org-fast-tag-show-exit
  13220. (setq exit-after-next (not exit-after-next)))
  13221. (setq expert nil)
  13222. (delete-other-windows)
  13223. (set-window-buffer (split-window-vertically) " *Org tags*")
  13224. (org-switch-to-buffer-other-window " *Org tags*")
  13225. (org-fit-window-to-buffer)))
  13226. ((or (= c ?\C-g)
  13227. (and (= c ?q) (not (rassoc c ntable))))
  13228. (org-detach-overlay org-tags-overlay)
  13229. (setq quit-flag t))
  13230. ((= c ?\ )
  13231. (setq current nil)
  13232. (if exit-after-next (setq exit-after-next 'now)))
  13233. ((= c ?\t)
  13234. (condition-case nil
  13235. (setq tg (org-icompleting-read
  13236. "Tag: "
  13237. (or buffer-tags
  13238. (with-current-buffer buf
  13239. (org-get-buffer-tags)))))
  13240. (quit (setq tg "")))
  13241. (when (string-match "\\S-" tg)
  13242. (add-to-list 'buffer-tags (list tg))
  13243. (if (member tg current)
  13244. (setq current (delete tg current))
  13245. (push tg current)))
  13246. (if exit-after-next (setq exit-after-next 'now)))
  13247. ((setq e (rassoc c todo-table) tg (car e))
  13248. (with-current-buffer buf
  13249. (save-excursion (org-todo tg)))
  13250. (if exit-after-next (setq exit-after-next 'now)))
  13251. ((setq e (rassoc c ntable) tg (car e))
  13252. (if (member tg current)
  13253. (setq current (delete tg current))
  13254. (loop for g in groups do
  13255. (if (member tg g)
  13256. (mapc (lambda (x)
  13257. (setq current (delete x current)))
  13258. g)))
  13259. (push tg current))
  13260. (if exit-after-next (setq exit-after-next 'now))))
  13261. ;; Create a sorted list
  13262. (setq current
  13263. (sort current
  13264. (lambda (a b)
  13265. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  13266. (if (eq exit-after-next 'now) (throw 'exit t))
  13267. (goto-char (point-min))
  13268. (beginning-of-line 2)
  13269. (delete-region (point) (point-at-eol))
  13270. (org-fast-tag-insert "Current" current c-face)
  13271. (org-set-current-tags-overlay current ov-prefix)
  13272. (while (re-search-forward
  13273. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  13274. (setq tg (match-string 1))
  13275. (add-text-properties
  13276. (match-beginning 1) (match-end 1)
  13277. (list 'face
  13278. (cond
  13279. ((member tg current) c-face)
  13280. ((member tg inherited) i-face)
  13281. (t (get-text-property (match-beginning 1) 'face))))))
  13282. (goto-char (point-min)))))
  13283. (org-detach-overlay org-tags-overlay)
  13284. (if rtn
  13285. (mapconcat 'identity current ":")
  13286. nil))))
  13287. (defun org-get-tags-string ()
  13288. "Get the TAGS string in the current headline."
  13289. (unless (org-at-heading-p t)
  13290. (error "Not on a heading"))
  13291. (save-excursion
  13292. (beginning-of-line 1)
  13293. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13294. (org-match-string-no-properties 1)
  13295. "")))
  13296. (defun org-get-tags ()
  13297. "Get the list of tags specified in the current headline."
  13298. (org-split-string (org-get-tags-string) ":"))
  13299. (defun org-get-buffer-tags ()
  13300. "Get a table of all tags used in the buffer, for completion."
  13301. (let (tags)
  13302. (save-excursion
  13303. (goto-char (point-min))
  13304. (while (re-search-forward
  13305. (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
  13306. (when (equal (char-after (point-at-bol 0)) ?*)
  13307. (mapc (lambda (x) (add-to-list 'tags x))
  13308. (org-split-string (org-match-string-no-properties 1) ":")))))
  13309. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  13310. (mapcar 'list tags)))
  13311. ;;;; The mapping API
  13312. (defun org-map-entries (func &optional match scope &rest skip)
  13313. "Call FUNC at each headline selected by MATCH in SCOPE.
  13314. FUNC is a function or a lisp form. The function will be called without
  13315. arguments, with the cursor positioned at the beginning of the headline.
  13316. The return values of all calls to the function will be collected and
  13317. returned as a list.
  13318. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  13319. does not need to preserve point. After evaluation, the cursor will be
  13320. moved to the end of the line (presumably of the headline of the
  13321. processed entry) and search continues from there. Under some
  13322. circumstances, this may not produce the wanted results. For example,
  13323. if you have removed (e.g. archived) the current (sub)tree it could
  13324. mean that the next entry will be skipped entirely. In such cases, you
  13325. can specify the position from where search should continue by making
  13326. FUNC set the variable `org-map-continue-from' to the desired buffer
  13327. position.
  13328. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  13329. Only headlines that are matched by this query will be considered during
  13330. the iteration. When MATCH is nil or t, all headlines will be
  13331. visited by the iteration.
  13332. SCOPE determines the scope of this command. It can be any of:
  13333. nil The current buffer, respecting the restriction if any
  13334. tree The subtree started with the entry at point
  13335. region The entries within the active region, if any
  13336. region-start-level
  13337. The entries within the active region, but only those at
  13338. the same level than the first one.
  13339. file The current buffer, without restriction
  13340. file-with-archives
  13341. The current buffer, and any archives associated with it
  13342. agenda All agenda files
  13343. agenda-with-archives
  13344. All agenda files with any archive files associated with them
  13345. \(file1 file2 ...)
  13346. If this is a list, all files in the list will be scanned
  13347. The remaining args are treated as settings for the skipping facilities of
  13348. the scanner. The following items can be given here:
  13349. archive skip trees with the archive tag
  13350. comment skip trees with the COMMENT keyword
  13351. function or Emacs Lisp form:
  13352. will be used as value for `org-agenda-skip-function', so
  13353. whenever the function returns a position, FUNC will not be
  13354. called for that entry and search will continue from the
  13355. position returned
  13356. If your function needs to retrieve the tags including inherited tags
  13357. at the *current* entry, you can use the value of the variable
  13358. `org-scanner-tags' which will be much faster than getting the value
  13359. with `org-get-tags-at'. If your function gets properties with
  13360. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  13361. to t around the call to `org-entry-properties' to get the same speedup.
  13362. Note that if your function moves around to retrieve tags and properties at
  13363. a *different* entry, you cannot use these techniques."
  13364. (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
  13365. (not (org-region-active-p)))
  13366. (let* ((org-agenda-archives-mode nil) ; just to make sure
  13367. (org-agenda-skip-archived-trees (memq 'archive skip))
  13368. (org-agenda-skip-comment-trees (memq 'comment skip))
  13369. (org-agenda-skip-function
  13370. (car (org-delete-all '(comment archive) skip)))
  13371. (org-tags-match-list-sublevels t)
  13372. (start-level (eq scope 'region-start-level))
  13373. matcher file res
  13374. org-todo-keywords-for-agenda
  13375. org-done-keywords-for-agenda
  13376. org-todo-keyword-alist-for-agenda
  13377. org-drawers-for-agenda
  13378. org-tag-alist-for-agenda
  13379. todo-only)
  13380. (cond
  13381. ((eq match t) (setq matcher t))
  13382. ((eq match nil) (setq matcher t))
  13383. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  13384. (save-excursion
  13385. (save-restriction
  13386. (cond ((eq scope 'tree)
  13387. (org-back-to-heading t)
  13388. (org-narrow-to-subtree)
  13389. (setq scope nil))
  13390. ((and (or (eq scope 'region) (eq scope 'region-start-level))
  13391. (org-region-active-p))
  13392. ;; If needed, set start-level to a string like "2"
  13393. (when start-level
  13394. (save-excursion
  13395. (goto-char (region-beginning))
  13396. (unless (org-at-heading-p) (outline-next-heading))
  13397. (setq start-level (org-current-level))))
  13398. (narrow-to-region (region-beginning)
  13399. (save-excursion
  13400. (goto-char (region-end))
  13401. (unless (and (bolp) (org-at-heading-p))
  13402. (outline-next-heading))
  13403. (point)))
  13404. (setq scope nil)))
  13405. (if (not scope)
  13406. (progn
  13407. (org-agenda-prepare-buffers
  13408. (list (buffer-file-name (current-buffer))))
  13409. (setq res (org-scan-tags func matcher todo-only start-level)))
  13410. ;; Get the right scope
  13411. (cond
  13412. ((and scope (listp scope) (symbolp (car scope)))
  13413. (setq scope (eval scope)))
  13414. ((eq scope 'agenda)
  13415. (setq scope (org-agenda-files t)))
  13416. ((eq scope 'agenda-with-archives)
  13417. (setq scope (org-agenda-files t))
  13418. (setq scope (org-add-archive-files scope)))
  13419. ((eq scope 'file)
  13420. (setq scope (list (buffer-file-name))))
  13421. ((eq scope 'file-with-archives)
  13422. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  13423. (org-agenda-prepare-buffers scope)
  13424. (while (setq file (pop scope))
  13425. (with-current-buffer (org-find-base-buffer-visiting file)
  13426. (save-excursion
  13427. (save-restriction
  13428. (widen)
  13429. (goto-char (point-min))
  13430. (setq res (append res (org-scan-tags func matcher todo-only))))))))))
  13431. res)))
  13432. ;;;; Properties
  13433. ;;; Setting and retrieving properties
  13434. (defconst org-special-properties
  13435. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  13436. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED" "FILE" "CLOCKSUM" "CLOCKSUM_T")
  13437. "The special properties valid in Org-mode.
  13438. These are properties that are not defined in the property drawer,
  13439. but in some other way.")
  13440. (defconst org-default-properties
  13441. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  13442. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  13443. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  13444. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  13445. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  13446. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  13447. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  13448. "Some properties that are used by Org-mode for various purposes.
  13449. Being in this list makes sure that they are offered for completion.")
  13450. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  13451. "Regular expression matching the first line of a property drawer.")
  13452. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  13453. "Regular expression matching the last line of a property drawer.")
  13454. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  13455. "Regular expression matching the first line of a property drawer.")
  13456. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  13457. "Regular expression matching the first line of a property drawer.")
  13458. (defconst org-property-drawer-re
  13459. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  13460. org-property-end-re "\\)\n?")
  13461. "Matches an entire property drawer.")
  13462. (defconst org-clock-drawer-re
  13463. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  13464. org-property-end-re "\\)\n?")
  13465. "Matches an entire clock drawer.")
  13466. (defsubst org-re-property (property)
  13467. "Return a regexp matching a PROPERTY line.
  13468. Match group 1 will be set to the value."
  13469. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)"))
  13470. (defsubst org-re-property-keyword (property)
  13471. "Return a regexp matching a PROPERTY line, possibly with no
  13472. value for the property."
  13473. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)?"))
  13474. (defun org-property-action ()
  13475. "Do an action on properties."
  13476. (interactive)
  13477. (let (c)
  13478. (org-at-property-p)
  13479. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  13480. (setq c (read-char-exclusive))
  13481. (cond
  13482. ((equal c ?s)
  13483. (call-interactively 'org-set-property))
  13484. ((equal c ?d)
  13485. (call-interactively 'org-delete-property))
  13486. ((equal c ?D)
  13487. (call-interactively 'org-delete-property-globally))
  13488. ((equal c ?c)
  13489. (call-interactively 'org-compute-property-at-point))
  13490. (t (error "No such property action %c" c)))))
  13491. (defun org-inc-effort ()
  13492. "Increment the value of the effort property in the current entry."
  13493. (interactive)
  13494. (org-set-effort nil t))
  13495. (defvar org-clock-effort) ;; Defined in org-clock.el
  13496. (defvar org-clock-current-task) ;; Defined in org-clock.el
  13497. (defun org-set-effort (&optional value increment)
  13498. "Set the effort property of the current entry.
  13499. With numerical prefix arg, use the nth allowed value, 0 stands for the
  13500. 10th allowed value.
  13501. When INCREMENT is non-nil, set the property to the next allowed value."
  13502. (interactive "P")
  13503. (if (equal value 0) (setq value 10))
  13504. (let* ((completion-ignore-case t)
  13505. (prop org-effort-property)
  13506. (cur (org-entry-get nil prop))
  13507. (allowed (org-property-get-allowed-values nil prop 'table))
  13508. (existing (mapcar 'list (org-property-values prop)))
  13509. (heading (nth 4 (org-heading-components)))
  13510. rpl
  13511. (val (cond
  13512. ((stringp value) value)
  13513. ((and allowed (integerp value))
  13514. (or (car (nth (1- value) allowed))
  13515. (car (org-last allowed))))
  13516. ((and allowed increment)
  13517. (or (caadr (member (list cur) allowed))
  13518. (error "Allowed effort values are not set")))
  13519. (allowed
  13520. (message "Select 1-9,0, [RET%s]: %s"
  13521. (if cur (concat "=" cur) "")
  13522. (mapconcat 'car allowed " "))
  13523. (setq rpl (read-char-exclusive))
  13524. (if (equal rpl ?\r)
  13525. cur
  13526. (setq rpl (- rpl ?0))
  13527. (if (equal rpl 0) (setq rpl 10))
  13528. (if (and (> rpl 0) (<= rpl (length allowed)))
  13529. (car (nth (1- rpl) allowed))
  13530. (org-completing-read "Effort: " allowed nil))))
  13531. (t
  13532. (let (org-completion-use-ido org-completion-use-iswitchb)
  13533. (org-completing-read
  13534. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  13535. (concat "[" cur "]") "")
  13536. ": ")
  13537. existing nil nil "" nil cur))))))
  13538. (unless (equal (org-entry-get nil prop) val)
  13539. (org-entry-put nil prop val))
  13540. (save-excursion
  13541. (org-back-to-heading t)
  13542. (put-text-property (point-at-bol) (point-at-eol) 'org-effort val))
  13543. (when (string= heading org-clock-current-task)
  13544. (setq org-clock-effort (get-text-property (point-at-bol) 'org-effort))
  13545. (org-clock-update-mode-line))
  13546. (message "%s is now %s" prop val)))
  13547. (defun org-at-property-p ()
  13548. "Is cursor inside a property drawer?"
  13549. (save-excursion
  13550. (beginning-of-line 1)
  13551. (when (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))
  13552. (save-match-data ;; Used by calling procedures
  13553. (let ((p (point))
  13554. (range (unless (org-before-first-heading-p)
  13555. (org-get-property-block))))
  13556. (and range (<= (car range) p) (< p (cdr range))))))))
  13557. (defun org-get-property-block (&optional beg end force)
  13558. "Return the (beg . end) range of the body of the property drawer.
  13559. BEG and END are the beginning and end of the current subtree, or of
  13560. the part before the first headline. If they are not given, they will
  13561. be found. If the drawer does not exist and FORCE is non-nil, create
  13562. the drawer."
  13563. (catch 'exit
  13564. (save-excursion
  13565. (let* ((beg (or beg (and (org-before-first-heading-p) (point-min))
  13566. (progn (org-back-to-heading t) (point))))
  13567. (end (or end (and (not (outline-next-heading)) (point-max))
  13568. (point))))
  13569. (goto-char beg)
  13570. (if (re-search-forward org-property-start-re end t)
  13571. (setq beg (1+ (match-end 0)))
  13572. (if force
  13573. (save-excursion
  13574. (org-insert-property-drawer)
  13575. (setq end (progn (outline-next-heading) (point))))
  13576. (throw 'exit nil))
  13577. (goto-char beg)
  13578. (if (re-search-forward org-property-start-re end t)
  13579. (setq beg (1+ (match-end 0)))))
  13580. (if (re-search-forward org-property-end-re end t)
  13581. (setq end (match-beginning 0))
  13582. (or force (throw 'exit nil))
  13583. (goto-char beg)
  13584. (setq end beg)
  13585. (org-indent-line)
  13586. (insert ":END:\n"))
  13587. (cons beg end)))))
  13588. (defun org-entry-properties (&optional pom which specific)
  13589. "Get all properties of the entry at point-or-marker POM.
  13590. This includes the TODO keyword, the tags, time strings for deadline,
  13591. scheduled, and clocking, and any additional properties defined in the
  13592. entry. The return value is an alist, keys may occur multiple times
  13593. if the property key was used several times.
  13594. POM may also be nil, in which case the current entry is used.
  13595. If WHICH is nil or `all', get all properties. If WHICH is
  13596. `special' or `standard', only get that subclass. If WHICH
  13597. is a string only get exactly this property. SPECIFIC can be a string, the
  13598. specific property we are interested in. Specifying it can speed
  13599. things up because then unnecessary parsing is avoided."
  13600. (setq which (or which 'all))
  13601. (org-with-point-at pom
  13602. (let ((clockstr (substring org-clock-string 0 -1))
  13603. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
  13604. (case-fold-search nil)
  13605. beg end range props sum-props key key1 value string clocksum clocksumt)
  13606. (save-excursion
  13607. (when (condition-case nil
  13608. (and (derived-mode-p 'org-mode) (org-back-to-heading t))
  13609. (error nil))
  13610. (setq beg (point))
  13611. (setq sum-props (get-text-property (point) 'org-summaries))
  13612. (setq clocksum (get-text-property (point) :org-clock-minutes)
  13613. clocksumt (get-text-property (point) :org-clock-minutes-today))
  13614. (outline-next-heading)
  13615. (setq end (point))
  13616. (when (memq which '(all special))
  13617. ;; Get the special properties, like TODO and tags
  13618. (goto-char beg)
  13619. (when (and (or (not specific) (string= specific "TODO"))
  13620. (looking-at org-todo-line-regexp) (match-end 2))
  13621. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  13622. (when (and (or (not specific) (string= specific "PRIORITY"))
  13623. (looking-at org-priority-regexp))
  13624. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  13625. (when (or (not specific) (string= specific "FILE"))
  13626. (push (cons "FILE" buffer-file-name) props))
  13627. (when (and (or (not specific) (string= specific "TAGS"))
  13628. (setq value (org-get-tags-string))
  13629. (string-match "\\S-" value))
  13630. (push (cons "TAGS" value) props))
  13631. (when (and (or (not specific) (string= specific "ALLTAGS"))
  13632. (setq value (org-get-tags-at)))
  13633. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
  13634. ":"))
  13635. props))
  13636. (when (or (not specific) (string= specific "BLOCKED"))
  13637. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
  13638. (when (or (not specific)
  13639. (member specific
  13640. '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
  13641. "TIMESTAMP" "TIMESTAMP_IA")))
  13642. (catch 'match
  13643. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  13644. (setq key (if (match-end 1)
  13645. (substring (org-match-string-no-properties 1)
  13646. 0 -1))
  13647. string (if (equal key clockstr)
  13648. (org-trim
  13649. (buffer-substring-no-properties
  13650. (match-beginning 3) (goto-char
  13651. (point-at-eol))))
  13652. (substring (org-match-string-no-properties 3)
  13653. 1 -1)))
  13654. ;; Get the correct property name from the key. This is
  13655. ;; necessary if the user has configured time keywords.
  13656. (setq key1 (concat key ":"))
  13657. (cond
  13658. ((not key)
  13659. (setq key
  13660. (if (= (char-after (match-beginning 3)) ?\[)
  13661. "TIMESTAMP_IA" "TIMESTAMP")))
  13662. ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
  13663. ((equal key1 org-deadline-string) (setq key "DEADLINE"))
  13664. ((equal key1 org-closed-string) (setq key "CLOSED"))
  13665. ((equal key1 org-clock-string) (setq key "CLOCK")))
  13666. (if (and specific (equal key specific) (not (equal key "CLOCK")))
  13667. (progn
  13668. (push (cons key string) props)
  13669. ;; no need to search further if match is found
  13670. (throw 'match t))
  13671. (when (or (equal key "CLOCK") (not (assoc key props)))
  13672. (push (cons key string) props)))))))
  13673. (when (memq which '(all standard))
  13674. ;; Get the standard properties, like :PROP: ...
  13675. (setq range (org-get-property-block beg end))
  13676. (when range
  13677. (goto-char (car range))
  13678. (while (re-search-forward
  13679. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  13680. (cdr range) t)
  13681. (setq key (org-match-string-no-properties 1)
  13682. value (org-trim (or (org-match-string-no-properties 2) "")))
  13683. (unless (member key excluded)
  13684. (push (cons key (or value "")) props)))))
  13685. (if clocksum
  13686. (push (cons "CLOCKSUM"
  13687. (org-columns-number-to-string (/ (float clocksum) 60.)
  13688. 'add_times))
  13689. props))
  13690. (if clocksumt
  13691. (push (cons "CLOCKSUM_T"
  13692. (org-columns-number-to-string (/ (float clocksumt) 60.)
  13693. 'add_times))
  13694. props))
  13695. (unless (assoc "CATEGORY" props)
  13696. (push (cons "CATEGORY" (org-get-category)) props))
  13697. (append sum-props (nreverse props)))))))
  13698. (defun org-entry-get (pom property &optional inherit literal-nil)
  13699. "Get value of PROPERTY for entry or content at point-or-marker POM.
  13700. If INHERIT is non-nil and the entry does not have the property,
  13701. then also check higher levels of the hierarchy.
  13702. If INHERIT is the symbol `selective', use inheritance only if the setting
  13703. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  13704. If the property is present but empty, the return value is the empty string.
  13705. If the property is not present at all, nil is returned.
  13706. If LITERAL-NIL is set, return the string value \"nil\" as a string,
  13707. do not interpret it as the list atom nil. This is used for inheritance
  13708. when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
  13709. (org-with-point-at pom
  13710. (if (and inherit (if (eq inherit 'selective)
  13711. (org-property-inherit-p property)
  13712. t))
  13713. (org-entry-get-with-inheritance property literal-nil)
  13714. (if (member property org-special-properties)
  13715. ;; We need a special property. Use `org-entry-properties' to
  13716. ;; retrieve it, but specify the wanted property
  13717. (cdr (assoc property (org-entry-properties nil 'special property)))
  13718. (let ((range (org-get-property-block)))
  13719. (when (and range (not (eq (car range) (cdr range))))
  13720. (let* ((props (list (or (assoc property org-file-properties)
  13721. (assoc property org-global-properties)
  13722. (assoc property org-global-properties-fixed))))
  13723. (ap (lambda (key)
  13724. (when (re-search-forward
  13725. (org-re-property key) (cdr range) t)
  13726. (setq props
  13727. (org-update-property-plist
  13728. key
  13729. (if (match-end 1)
  13730. (org-match-string-no-properties 1) "")
  13731. props)))))
  13732. val)
  13733. (goto-char (car range))
  13734. (funcall ap property)
  13735. (goto-char (car range))
  13736. (while (funcall ap (concat property "+")))
  13737. (setq val (cdr (assoc property props)))
  13738. (when val (if literal-nil val (org-not-nil val))))))))))
  13739. (defun org-property-or-variable-value (var &optional inherit)
  13740. "Check if there is a property fixing the value of VAR.
  13741. If yes, return this value. If not, return the current value of the variable."
  13742. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  13743. (if (and prop (stringp prop) (string-match "\\S-" prop))
  13744. (read prop)
  13745. (symbol-value var))))
  13746. (defun org-entry-delete (pom property &optional delete-empty-drawer)
  13747. "Delete the property PROPERTY from entry at point-or-marker POM.
  13748. When optional argument DELETE-EMPTY-DRAWER is a string, it defines
  13749. an empty drawer to delete."
  13750. (org-with-point-at pom
  13751. (if (member property org-special-properties)
  13752. nil ; cannot delete these properties.
  13753. (let ((range (org-get-property-block)))
  13754. (if (and range
  13755. (goto-char (car range))
  13756. (re-search-forward
  13757. (org-re-property property)
  13758. (cdr range) t))
  13759. (progn
  13760. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  13761. (and delete-empty-drawer
  13762. (org-remove-empty-drawer-at
  13763. delete-empty-drawer (car range)))
  13764. t)
  13765. nil)))))
  13766. ;; Multi-values properties are properties that contain multiple values
  13767. ;; These values are assumed to be single words, separated by whitespace.
  13768. (defun org-entry-add-to-multivalued-property (pom property value)
  13769. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  13770. (let* ((old (org-entry-get pom property))
  13771. (values (and old (org-split-string old "[ \t]"))))
  13772. (setq value (org-entry-protect-space value))
  13773. (unless (member value values)
  13774. (setq values (cons value values))
  13775. (org-entry-put pom property
  13776. (mapconcat 'identity values " ")))))
  13777. (defun org-entry-remove-from-multivalued-property (pom property value)
  13778. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  13779. (let* ((old (org-entry-get pom property))
  13780. (values (and old (org-split-string old "[ \t]"))))
  13781. (setq value (org-entry-protect-space value))
  13782. (when (member value values)
  13783. (setq values (delete value values))
  13784. (org-entry-put pom property
  13785. (mapconcat 'identity values " ")))))
  13786. (defun org-entry-member-in-multivalued-property (pom property value)
  13787. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  13788. (let* ((old (org-entry-get pom property))
  13789. (values (and old (org-split-string old "[ \t]"))))
  13790. (setq value (org-entry-protect-space value))
  13791. (member value values)))
  13792. (defun org-entry-get-multivalued-property (pom property)
  13793. "Return a list of values in a multivalued property."
  13794. (let* ((value (org-entry-get pom property))
  13795. (values (and value (org-split-string value "[ \t]"))))
  13796. (mapcar 'org-entry-restore-space values)))
  13797. (defun org-entry-put-multivalued-property (pom property &rest values)
  13798. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  13799. VALUES should be a list of strings. Spaces will be protected."
  13800. (org-entry-put pom property
  13801. (mapconcat 'org-entry-protect-space values " "))
  13802. (let* ((value (org-entry-get pom property))
  13803. (values (and value (org-split-string value "[ \t]"))))
  13804. (mapcar 'org-entry-restore-space values)))
  13805. (defun org-entry-protect-space (s)
  13806. "Protect spaces and newline in string S."
  13807. (while (string-match " " s)
  13808. (setq s (replace-match "%20" t t s)))
  13809. (while (string-match "\n" s)
  13810. (setq s (replace-match "%0A" t t s)))
  13811. s)
  13812. (defun org-entry-restore-space (s)
  13813. "Restore spaces and newline in string S."
  13814. (while (string-match "%20" s)
  13815. (setq s (replace-match " " t t s)))
  13816. (while (string-match "%0A" s)
  13817. (setq s (replace-match "\n" t t s)))
  13818. s)
  13819. (defvar org-entry-property-inherited-from (make-marker)
  13820. "Marker pointing to the entry from where a property was inherited.
  13821. Each call to `org-entry-get-with-inheritance' will set this marker to the
  13822. location of the entry where the inheritance search matched. If there was
  13823. no match, the marker will point nowhere.
  13824. Note that also `org-entry-get' calls this function, if the INHERIT flag
  13825. is set.")
  13826. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  13827. "Get PROPERTY of entry or content at point, search higher levels if needed.
  13828. The search will stop at the first ancestor which has the property defined.
  13829. If the value found is \"nil\", return nil to show that the property
  13830. should be considered as undefined (this is the meaning of nil here).
  13831. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  13832. (move-marker org-entry-property-inherited-from nil)
  13833. (let (tmp)
  13834. (save-excursion
  13835. (save-restriction
  13836. (widen)
  13837. (catch 'ex
  13838. (while t
  13839. (when (setq tmp (org-entry-get nil property nil 'literal-nil))
  13840. (or (ignore-errors (org-back-to-heading t))
  13841. (goto-char (point-min)))
  13842. (move-marker org-entry-property-inherited-from (point))
  13843. (throw 'ex tmp))
  13844. (or (ignore-errors (org-up-heading-safe))
  13845. (throw 'ex nil))))))
  13846. (setq tmp (or tmp
  13847. (cdr (assoc property org-file-properties))
  13848. (cdr (assoc property org-global-properties))
  13849. (cdr (assoc property org-global-properties-fixed))))
  13850. (if literal-nil tmp (org-not-nil tmp))))
  13851. (defvar org-property-changed-functions nil
  13852. "Hook called when the value of a property has changed.
  13853. Each hook function should accept two arguments, the name of the property
  13854. and the new value.")
  13855. (defun org-entry-put (pom property value)
  13856. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  13857. (org-with-point-at pom
  13858. (org-back-to-heading t)
  13859. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  13860. range)
  13861. (cond
  13862. ((equal property "TODO")
  13863. (when (and (stringp value) (string-match "\\S-" value)
  13864. (not (member value org-todo-keywords-1)))
  13865. (error "\"%s\" is not a valid TODO state" value))
  13866. (if (or (not value)
  13867. (not (string-match "\\S-" value)))
  13868. (setq value 'none))
  13869. (org-todo value)
  13870. (org-set-tags nil 'align))
  13871. ((equal property "PRIORITY")
  13872. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  13873. (string-to-char value) ?\ ))
  13874. (org-set-tags nil 'align))
  13875. ((equal property "CLOCKSUM")
  13876. (if (not (re-search-forward
  13877. (concat org-clock-string ".*\\]--\\(\\[[^]]+\\]\\)") nil t))
  13878. (error "Cannot find a clock log")
  13879. (goto-char (- (match-end 1) 2))
  13880. (cond
  13881. ((eq value 'earlier) (org-timestamp-down))
  13882. ((eq value 'later) (org-timestamp-up)))
  13883. (org-clock-sum-current-item)))
  13884. ((equal property "SCHEDULED")
  13885. (if (re-search-forward org-scheduled-time-regexp end t)
  13886. (cond
  13887. ((eq value 'earlier) (org-timestamp-change -1 'day))
  13888. ((eq value 'later) (org-timestamp-change 1 'day))
  13889. (t (call-interactively 'org-schedule)))
  13890. (call-interactively 'org-schedule)))
  13891. ((equal property "DEADLINE")
  13892. (if (re-search-forward org-deadline-time-regexp end t)
  13893. (cond
  13894. ((eq value 'earlier) (org-timestamp-change -1 'day))
  13895. ((eq value 'later) (org-timestamp-change 1 'day))
  13896. (t (call-interactively 'org-deadline)))
  13897. (call-interactively 'org-deadline)))
  13898. ((member property org-special-properties)
  13899. (error "The %s property can not yet be set with `org-entry-put'"
  13900. property))
  13901. (t ; a non-special property
  13902. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  13903. (setq range (org-get-property-block beg end 'force))
  13904. (goto-char (car range))
  13905. (if (re-search-forward
  13906. (org-re-property-keyword property) (cdr range) t)
  13907. (progn
  13908. (delete-region (match-beginning 0) (match-end 0))
  13909. (goto-char (match-beginning 0)))
  13910. (goto-char (cdr range))
  13911. (insert "\n")
  13912. (backward-char 1)
  13913. (org-indent-line))
  13914. (insert ":" property ":")
  13915. (and value (insert " " value))
  13916. (org-indent-line)))))
  13917. (run-hook-with-args 'org-property-changed-functions property value)))
  13918. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  13919. "Get all property keys in the current buffer.
  13920. With INCLUDE-SPECIALS, also list the special properties that reflect things
  13921. like tags and TODO state.
  13922. With INCLUDE-DEFAULTS, also include properties that has special meaning
  13923. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING
  13924. and others.
  13925. With INCLUDE-COLUMNS, also include property names given in COLUMN
  13926. formats in the current buffer."
  13927. (let (rtn range cfmt s p)
  13928. (save-excursion
  13929. (save-restriction
  13930. (widen)
  13931. (goto-char (point-min))
  13932. (while (re-search-forward org-property-start-re nil t)
  13933. (setq range (org-get-property-block))
  13934. (goto-char (car range))
  13935. (while (re-search-forward
  13936. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  13937. (cdr range) t)
  13938. (add-to-list 'rtn (org-match-string-no-properties 1)))
  13939. (outline-next-heading))))
  13940. (when include-specials
  13941. (setq rtn (append org-special-properties rtn)))
  13942. (when include-defaults
  13943. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  13944. (add-to-list 'rtn org-effort-property))
  13945. (when include-columns
  13946. (save-excursion
  13947. (save-restriction
  13948. (widen)
  13949. (goto-char (point-min))
  13950. (while (re-search-forward
  13951. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  13952. nil t)
  13953. (setq cfmt (match-string 2) s 0)
  13954. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  13955. cfmt s)
  13956. (setq s (match-end 0)
  13957. p (match-string 1 cfmt))
  13958. (unless (or (equal p "ITEM")
  13959. (member p org-special-properties))
  13960. (add-to-list 'rtn (match-string 1 cfmt))))))))
  13961. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  13962. (defun org-property-values (key)
  13963. "Return a list of all values of property KEY in the current buffer."
  13964. (save-excursion
  13965. (save-restriction
  13966. (widen)
  13967. (goto-char (point-min))
  13968. (let ((re (org-re-property key))
  13969. values)
  13970. (while (re-search-forward re nil t)
  13971. (add-to-list 'values (org-trim (match-string 1))))
  13972. (delete "" values)))))
  13973. (defun org-insert-property-drawer ()
  13974. "Insert a property drawer into the current entry."
  13975. (org-back-to-heading t)
  13976. (looking-at org-outline-regexp)
  13977. (let ((indent (if org-adapt-indentation
  13978. (- (match-end 0) (match-beginning 0))
  13979. 0))
  13980. (beg (point))
  13981. (re (concat "^[ \t]*" org-keyword-time-regexp))
  13982. end hiddenp)
  13983. (outline-next-heading)
  13984. (setq end (point))
  13985. (goto-char beg)
  13986. (while (re-search-forward re end t))
  13987. (setq hiddenp (outline-invisible-p))
  13988. (end-of-line 1)
  13989. (and (equal (char-after) ?\n) (forward-char 1))
  13990. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  13991. (if (member (match-string 1) '("CLOCK:" ":END:"))
  13992. ;; just skip this line
  13993. (beginning-of-line 2)
  13994. ;; Drawer start, find the end
  13995. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  13996. (beginning-of-line 1)))
  13997. (org-skip-over-state-notes)
  13998. (skip-chars-backward " \t\n\r")
  13999. (if (eq (char-before) ?*) (forward-char 1))
  14000. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  14001. (beginning-of-line 0)
  14002. (org-indent-to-column indent)
  14003. (beginning-of-line 2)
  14004. (org-indent-to-column indent)
  14005. (beginning-of-line 0)
  14006. (if hiddenp
  14007. (save-excursion
  14008. (org-back-to-heading t)
  14009. (hide-entry))
  14010. (org-flag-drawer t))))
  14011. (defun org-insert-drawer (&optional arg drawer)
  14012. "Insert a drawer at point.
  14013. Optional argument DRAWER, when non-nil, is a string representing
  14014. drawer's name. Otherwise, the user is prompted for a name.
  14015. If a region is active, insert the drawer around that region
  14016. instead.
  14017. Point is left between drawer's boundaries."
  14018. (interactive "P")
  14019. (let* ((logbook (if (stringp org-log-into-drawer) org-log-into-drawer
  14020. "LOGBOOK"))
  14021. ;; SYSTEM-DRAWERS is a list of drawer names that are used
  14022. ;; internally by Org. They are meant to be inserted
  14023. ;; automatically.
  14024. (system-drawers `("CLOCK" ,logbook "PROPERTIES"))
  14025. ;; Remove system drawers from list. Note: For some reason,
  14026. ;; `org-completing-read' ignores the predicate while
  14027. ;; `completing-read' handles it fine.
  14028. (drawer (if arg "PROPERTIES"
  14029. (or drawer
  14030. (completing-read
  14031. "Drawer: " org-drawers
  14032. (lambda (d) (not (member d system-drawers))))))))
  14033. (cond
  14034. ;; With C-u, fall back on `org-insert-property-drawer'
  14035. (arg (org-insert-property-drawer))
  14036. ;; With an active region, insert a drawer at point.
  14037. ((not (org-region-active-p))
  14038. (progn
  14039. (unless (bolp) (insert "\n"))
  14040. (insert (format ":%s:\n\n:END:\n" drawer))
  14041. (forward-line -2)))
  14042. ;; Otherwise, insert the drawer at point
  14043. (t
  14044. (let ((rbeg (region-beginning))
  14045. (rend (copy-marker (region-end))))
  14046. (unwind-protect
  14047. (progn
  14048. (goto-char rbeg)
  14049. (beginning-of-line)
  14050. (when (save-excursion
  14051. (re-search-forward org-outline-regexp-bol rend t))
  14052. (error "Drawers cannot contain headlines"))
  14053. ;; Position point at the beginning of the first
  14054. ;; non-blank line in region. Insert drawer's opening
  14055. ;; there, then indent it.
  14056. (org-skip-whitespace)
  14057. (beginning-of-line)
  14058. (insert ":" drawer ":\n")
  14059. (forward-line -1)
  14060. (indent-for-tab-command)
  14061. ;; Move point to the beginning of the first blank line
  14062. ;; after the last non-blank line in region. Insert
  14063. ;; drawer's closing, then indent it.
  14064. (goto-char rend)
  14065. (skip-chars-backward " \r\t\n")
  14066. (insert "\n:END:")
  14067. (deactivate-mark t)
  14068. (indent-for-tab-command)
  14069. (unless (eolp) (insert "\n")))
  14070. ;; Clear marker, whatever the outcome of insertion is.
  14071. (set-marker rend nil)))))))
  14072. (defvar org-property-set-functions-alist nil
  14073. "Property set function alist.
  14074. Each entry should have the following format:
  14075. (PROPERTY . READ-FUNCTION)
  14076. The read function will be called with the same argument as
  14077. `org-completing-read'.")
  14078. (defun org-set-property-function (property)
  14079. "Get the function that should be used to set PROPERTY.
  14080. This is computed according to `org-property-set-functions-alist'."
  14081. (or (cdr (assoc property org-property-set-functions-alist))
  14082. 'org-completing-read))
  14083. (defun org-read-property-value (property)
  14084. "Read PROPERTY value from user."
  14085. (let* ((completion-ignore-case t)
  14086. (allowed (org-property-get-allowed-values nil property 'table))
  14087. (cur (org-entry-get nil property))
  14088. (prompt (concat property " value"
  14089. (if (and cur (string-match "\\S-" cur))
  14090. (concat " [" cur "]") "") ": "))
  14091. (set-function (org-set-property-function property))
  14092. (val (if allowed
  14093. (funcall set-function prompt allowed nil
  14094. (not (get-text-property 0 'org-unrestricted
  14095. (caar allowed))))
  14096. (let (org-completion-use-ido org-completion-use-iswitchb)
  14097. (funcall set-function prompt
  14098. (mapcar 'list (org-property-values property))
  14099. nil nil "" nil cur)))))
  14100. (if (equal val "")
  14101. cur
  14102. val)))
  14103. (defvar org-last-set-property nil)
  14104. (defvar org-last-set-property-value nil)
  14105. (defun org-read-property-name ()
  14106. "Read a property name."
  14107. (let* ((completion-ignore-case t)
  14108. (keys (org-buffer-property-keys nil t t))
  14109. (default-prop (or (save-excursion
  14110. (save-match-data
  14111. (beginning-of-line)
  14112. (and (looking-at "^\\s-*:\\([^:\n]+\\):")
  14113. (null (string= (match-string 1) "END"))
  14114. (match-string 1))))
  14115. org-last-set-property))
  14116. (property (org-icompleting-read
  14117. (concat "Property"
  14118. (if default-prop (concat " [" default-prop "]") "")
  14119. ": ")
  14120. (mapcar 'list keys)
  14121. nil nil nil nil
  14122. default-prop)))
  14123. (if (member property keys)
  14124. property
  14125. (or (cdr (assoc (downcase property)
  14126. (mapcar (lambda (x) (cons (downcase x) x))
  14127. keys)))
  14128. property))))
  14129. (defun org-set-property-and-value (use-last)
  14130. "Allow to set [PROPERTY]: [value] direction from prompt.
  14131. When use-default, don't even ask, just use the last
  14132. \"[PROPERTY]: [value]\" string from the history."
  14133. (interactive "P")
  14134. (let* ((completion-ignore-case t)
  14135. (pv (or (and use-last org-last-set-property-value)
  14136. (org-completing-read
  14137. "Enter a \"[Property]: [value]\" pair: "
  14138. nil nil nil nil nil
  14139. org-last-set-property-value)))
  14140. prop val)
  14141. (when (string-match "^[ \t]*\\([^:]+\\):[ \t]*\\(.*\\)[ \t]*$" pv)
  14142. (setq prop (match-string 1 pv)
  14143. val (match-string 2 pv))
  14144. (org-set-property prop val))))
  14145. (defun org-set-property (property value)
  14146. "In the current entry, set PROPERTY to VALUE.
  14147. When called interactively, this will prompt for a property name, offering
  14148. completion on existing and default properties. And then it will prompt
  14149. for a value, offering completion either on allowed values (via an inherited
  14150. xxx_ALL property) or on existing values in other instances of this property
  14151. in the current file."
  14152. (interactive (list nil nil))
  14153. (let* ((property (or property (org-read-property-name)))
  14154. (value (or value (org-read-property-value property)))
  14155. (fn (cdr (assoc property org-properties-postprocess-alist))))
  14156. (setq org-last-set-property property)
  14157. (setq org-last-set-property-value (concat property ": " value))
  14158. ;; Possibly postprocess the inserted value:
  14159. (when fn (setq value (funcall fn value)))
  14160. (unless (equal (org-entry-get nil property) value)
  14161. (org-entry-put nil property value))))
  14162. (defun org-delete-property (property &optional delete-empty-drawer)
  14163. "In the current entry, delete PROPERTY.
  14164. When optional argument DELETE-EMPTY-DRAWER is a string, it defines
  14165. an empty drawer to delete."
  14166. (interactive
  14167. (let* ((completion-ignore-case t)
  14168. (prop (org-icompleting-read "Property: "
  14169. (org-entry-properties nil 'standard))))
  14170. (list prop)))
  14171. (message "Property %s %s" property
  14172. (if (org-entry-delete nil property delete-empty-drawer)
  14173. "deleted"
  14174. "was not present in the entry")))
  14175. (defun org-delete-property-globally (property)
  14176. "Remove PROPERTY globally, from all entries."
  14177. (interactive
  14178. (let* ((completion-ignore-case t)
  14179. (prop (org-icompleting-read
  14180. "Globally remove property: "
  14181. (mapcar 'list (org-buffer-property-keys)))))
  14182. (list prop)))
  14183. (save-excursion
  14184. (save-restriction
  14185. (widen)
  14186. (goto-char (point-min))
  14187. (let ((cnt 0))
  14188. (while (re-search-forward
  14189. (org-re-property property)
  14190. nil t)
  14191. (setq cnt (1+ cnt))
  14192. (delete-region (match-beginning 0) (1+ (point-at-eol))))
  14193. (message "Property \"%s\" removed from %d entries" property cnt)))))
  14194. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  14195. (defun org-compute-property-at-point ()
  14196. "Compute the property at point.
  14197. This looks for an enclosing column format, extracts the operator and
  14198. then applies it to the property in the column format's scope."
  14199. (interactive)
  14200. (unless (org-at-property-p)
  14201. (error "Not at a property"))
  14202. (let ((prop (org-match-string-no-properties 2)))
  14203. (org-columns-get-format-and-top-level)
  14204. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  14205. (error "No operator defined for property %s" prop))
  14206. (org-columns-compute prop)))
  14207. (defvar org-property-allowed-value-functions nil
  14208. "Hook for functions supplying allowed values for a specific property.
  14209. The functions must take a single argument, the name of the property, and
  14210. return a flat list of allowed values. If \":ETC\" is one of
  14211. the values, this means that these values are intended as defaults for
  14212. completion, but that other values should be allowed too.
  14213. The functions must return nil if they are not responsible for this
  14214. property.")
  14215. (defun org-property-get-allowed-values (pom property &optional table)
  14216. "Get allowed values for the property PROPERTY.
  14217. When TABLE is non-nil, return an alist that can directly be used for
  14218. completion."
  14219. (let (vals)
  14220. (cond
  14221. ((equal property "TODO")
  14222. (setq vals (org-with-point-at pom
  14223. (append org-todo-keywords-1 '("")))))
  14224. ((equal property "PRIORITY")
  14225. (let ((n org-lowest-priority))
  14226. (while (>= n org-highest-priority)
  14227. (push (char-to-string n) vals)
  14228. (setq n (1- n)))))
  14229. ((member property org-special-properties))
  14230. ((setq vals (run-hook-with-args-until-success
  14231. 'org-property-allowed-value-functions property)))
  14232. (t
  14233. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  14234. (when (and vals (string-match "\\S-" vals))
  14235. (setq vals (car (read-from-string (concat "(" vals ")"))))
  14236. (setq vals (mapcar (lambda (x)
  14237. (cond ((stringp x) x)
  14238. ((numberp x) (number-to-string x))
  14239. ((symbolp x) (symbol-name x))
  14240. (t "???")))
  14241. vals)))))
  14242. (when (member ":ETC" vals)
  14243. (setq vals (remove ":ETC" vals))
  14244. (org-add-props (car vals) '(org-unrestricted t)))
  14245. (if table (mapcar 'list vals) vals)))
  14246. (defun org-property-previous-allowed-value (&optional previous)
  14247. "Switch to the next allowed value for this property."
  14248. (interactive)
  14249. (org-property-next-allowed-value t))
  14250. (defun org-property-next-allowed-value (&optional previous)
  14251. "Switch to the next allowed value for this property."
  14252. (interactive)
  14253. (unless (org-at-property-p)
  14254. (error "Not at a property"))
  14255. (let* ((prop (car (save-match-data (org-split-string (match-string 1) ":"))))
  14256. (key (match-string 2))
  14257. (value (match-string 3))
  14258. (allowed (or (org-property-get-allowed-values (point) key)
  14259. (and (member value '("[ ]" "[-]" "[X]"))
  14260. '("[ ]" "[X]"))))
  14261. (heading (save-match-data (nth 4 (org-heading-components))))
  14262. nval)
  14263. (unless allowed
  14264. (error "Allowed values for this property have not been defined"))
  14265. (if previous (setq allowed (reverse allowed)))
  14266. (if (member value allowed)
  14267. (setq nval (car (cdr (member value allowed)))))
  14268. (setq nval (or nval (car allowed)))
  14269. (if (equal nval value)
  14270. (error "Only one allowed value for this property"))
  14271. (org-at-property-p)
  14272. (replace-match (concat " :" key ": " nval) t t)
  14273. (org-indent-line)
  14274. (beginning-of-line 1)
  14275. (skip-chars-forward " \t")
  14276. (when (equal prop org-effort-property)
  14277. (save-excursion
  14278. (org-back-to-heading t)
  14279. (put-text-property (point-at-bol) (point-at-eol) 'org-effort nval))
  14280. (when (string= org-clock-current-task heading)
  14281. (setq org-clock-effort nval)
  14282. (org-clock-update-mode-line)))
  14283. (run-hook-with-args 'org-property-changed-functions key nval)))
  14284. (defun org-find-olp (path &optional this-buffer)
  14285. "Return a marker pointing to the entry at outline path OLP.
  14286. If anything goes wrong, throw an error.
  14287. You can wrap this call to catch the error like this:
  14288. (condition-case msg
  14289. (org-mobile-locate-entry (match-string 4))
  14290. (error (nth 1 msg)))
  14291. The return value will then be either a string with the error message,
  14292. or a marker if everything is OK.
  14293. If THIS-BUFFER is set, the outline path does not contain a file,
  14294. only headings."
  14295. (let* ((file (if this-buffer buffer-file-name (pop path)))
  14296. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  14297. (level 1)
  14298. (lmin 1)
  14299. (lmax 1)
  14300. limit re end found pos heading cnt flevel)
  14301. (unless buffer (error "File not found :%s" file))
  14302. (with-current-buffer buffer
  14303. (save-excursion
  14304. (save-restriction
  14305. (widen)
  14306. (setq limit (point-max))
  14307. (goto-char (point-min))
  14308. (while (setq heading (pop path))
  14309. (setq re (format org-complex-heading-regexp-format
  14310. (regexp-quote heading)))
  14311. (setq cnt 0 pos (point))
  14312. (while (re-search-forward re end t)
  14313. (setq level (- (match-end 1) (match-beginning 1)))
  14314. (if (and (>= level lmin) (<= level lmax))
  14315. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  14316. (when (= cnt 0) (error "Heading not found on level %d: %s"
  14317. lmax heading))
  14318. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  14319. lmax heading))
  14320. (goto-char found)
  14321. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  14322. (setq end (save-excursion (org-end-of-subtree t t))))
  14323. (when (org-at-heading-p)
  14324. (point-marker)))))))
  14325. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  14326. "Find node HEADING in BUFFER.
  14327. Return a marker to the heading if it was found, or nil if not.
  14328. If POS-ONLY is set, return just the position instead of a marker.
  14329. The heading text must match exact, but it may have a TODO keyword,
  14330. a priority cookie and tags in the standard locations."
  14331. (with-current-buffer (or buffer (current-buffer))
  14332. (save-excursion
  14333. (save-restriction
  14334. (widen)
  14335. (goto-char (point-min))
  14336. (let (case-fold-search)
  14337. (if (re-search-forward
  14338. (format org-complex-heading-regexp-format
  14339. (regexp-quote heading)) nil t)
  14340. (if pos-only
  14341. (match-beginning 0)
  14342. (move-marker (make-marker) (match-beginning 0)))))))))
  14343. (defun org-find-exact-heading-in-directory (heading &optional dir)
  14344. "Find Org node headline HEADING in all .org files in directory DIR.
  14345. When the target headline is found, return a marker to this location."
  14346. (let ((files (directory-files (or dir default-directory)
  14347. nil "\\`[^.#].*\\.org\\'"))
  14348. file visiting m buffer)
  14349. (catch 'found
  14350. (while (setq file (pop files))
  14351. (message "trying %s" file)
  14352. (setq visiting (org-find-base-buffer-visiting file))
  14353. (setq buffer (or visiting (find-file-noselect file)))
  14354. (setq m (org-find-exact-headline-in-buffer
  14355. heading buffer))
  14356. (when (and (not m) (not visiting)) (kill-buffer buffer))
  14357. (and m (throw 'found m))))))
  14358. (defun org-find-entry-with-id (ident)
  14359. "Locate the entry that contains the ID property with exact value IDENT.
  14360. IDENT can be a string, a symbol or a number, this function will search for
  14361. the string representation of it.
  14362. Return the position where this entry starts, or nil if there is no such entry."
  14363. (interactive "sID: ")
  14364. (let ((id (cond
  14365. ((stringp ident) ident)
  14366. ((symbol-name ident) (symbol-name ident))
  14367. ((numberp ident) (number-to-string ident))
  14368. (t (error "IDENT %s must be a string, symbol or number" ident))))
  14369. (case-fold-search nil))
  14370. (save-excursion
  14371. (save-restriction
  14372. (widen)
  14373. (goto-char (point-min))
  14374. (when (re-search-forward
  14375. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  14376. nil t)
  14377. (org-back-to-heading t)
  14378. (point))))))
  14379. ;;;; Timestamps
  14380. (defvar org-last-changed-timestamp nil)
  14381. (defvar org-last-inserted-timestamp nil
  14382. "The last time stamp inserted with `org-insert-time-stamp'.")
  14383. (defvar org-time-was-given) ; dynamically scoped parameter
  14384. (defvar org-end-time-was-given) ; dynamically scoped parameter
  14385. (defvar org-ts-what) ; dynamically scoped parameter
  14386. (defun org-time-stamp (arg &optional inactive)
  14387. "Prompt for a date/time and insert a time stamp.
  14388. If the user specifies a time like HH:MM or if this command is
  14389. called with at least one prefix argument, the time stamp contains
  14390. the date and the time. Otherwise, only the date is be included.
  14391. All parts of a date not specified by the user is filled in from
  14392. the current date/time. So if you just press return without
  14393. typing anything, the time stamp will represent the current
  14394. date/time.
  14395. If there is already a timestamp at the cursor, it will be
  14396. modified.
  14397. With two universal prefix arguments, insert an active timestamp
  14398. with the current time without prompting the user."
  14399. (interactive "P")
  14400. (let* ((ts nil)
  14401. (default-time
  14402. ;; Default time is either today, or, when entering a range,
  14403. ;; the range start.
  14404. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  14405. (save-excursion
  14406. (re-search-backward
  14407. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  14408. (- (point) 20) t)))
  14409. (apply 'encode-time (org-parse-time-string (match-string 1)))
  14410. (current-time)))
  14411. (default-input (and ts (org-get-compact-tod ts)))
  14412. (repeater (save-excursion
  14413. (save-match-data
  14414. (beginning-of-line)
  14415. (when (re-search-forward
  14416. "\\([.+-]+[0-9]+[hdwmy] ?\\)+" ;;\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  14417. (save-excursion (progn (end-of-line) (point))) t)
  14418. (match-string 0)))))
  14419. org-time-was-given org-end-time-was-given time)
  14420. (cond
  14421. ((and (org-at-timestamp-p t)
  14422. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  14423. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  14424. (insert "--")
  14425. (setq time (let ((this-command this-command))
  14426. (org-read-date arg 'totime nil nil
  14427. default-time default-input inactive)))
  14428. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  14429. ((org-at-timestamp-p t)
  14430. (setq time (let ((this-command this-command))
  14431. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  14432. (when (org-at-timestamp-p t) ; just to get the match data
  14433. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  14434. (replace-match "")
  14435. (setq org-last-changed-timestamp
  14436. (org-insert-time-stamp
  14437. time (or org-time-was-given arg)
  14438. inactive nil nil (list org-end-time-was-given)))
  14439. (when repeater (goto-char (1- (point))) (insert " " repeater)
  14440. (setq org-last-changed-timestamp
  14441. (concat (substring org-last-inserted-timestamp 0 -1)
  14442. " " repeater ">"))))
  14443. (message "Timestamp updated"))
  14444. ((equal arg '(16))
  14445. (org-insert-time-stamp (current-time) t))
  14446. (t
  14447. (setq time (let ((this-command this-command))
  14448. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  14449. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  14450. nil nil (list org-end-time-was-given))))))
  14451. ;; FIXME: can we use this for something else, like computing time differences?
  14452. (defun org-get-compact-tod (s)
  14453. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  14454. (let* ((t1 (match-string 1 s))
  14455. (h1 (string-to-number (match-string 2 s)))
  14456. (m1 (string-to-number (match-string 3 s)))
  14457. (t2 (and (match-end 4) (match-string 5 s)))
  14458. (h2 (and t2 (string-to-number (match-string 6 s))))
  14459. (m2 (and t2 (string-to-number (match-string 7 s))))
  14460. dh dm)
  14461. (if (not t2)
  14462. t1
  14463. (setq dh (- h2 h1) dm (- m2 m1))
  14464. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  14465. (concat t1 "+" (number-to-string dh)
  14466. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  14467. (defun org-time-stamp-inactive (&optional arg)
  14468. "Insert an inactive time stamp.
  14469. An inactive time stamp is enclosed in square brackets instead of angle
  14470. brackets. It is inactive in the sense that it does not trigger agenda entries,
  14471. does not link to the calendar and cannot be changed with the S-cursor keys.
  14472. So these are more for recording a certain time/date."
  14473. (interactive "P")
  14474. (org-time-stamp arg 'inactive))
  14475. (defvar org-date-ovl (make-overlay 1 1))
  14476. (overlay-put org-date-ovl 'face 'org-date-selected)
  14477. (org-detach-overlay org-date-ovl)
  14478. (defvar org-ans1) ; dynamically scoped parameter
  14479. (defvar org-ans2) ; dynamically scoped parameter
  14480. (defvar org-plain-time-of-day-regexp) ; defined below
  14481. (defvar org-overriding-default-time nil) ; dynamically scoped
  14482. (defvar org-read-date-overlay nil)
  14483. (defvar org-dcst nil) ; dynamically scoped
  14484. (defvar org-read-date-history nil)
  14485. (defvar org-read-date-final-answer nil)
  14486. (defvar org-read-date-analyze-futurep nil)
  14487. (defvar org-read-date-analyze-forced-year nil)
  14488. (defvar org-read-date-inactive)
  14489. (defvar org-read-date-minibuffer-local-map
  14490. (let ((map (make-sparse-keymap)))
  14491. (set-keymap-parent map minibuffer-local-map)
  14492. (org-defkey map (kbd ".")
  14493. (lambda () (interactive)
  14494. (org-eval-in-calendar '(calendar-goto-today))))
  14495. (org-defkey map [(meta shift left)]
  14496. (lambda () (interactive)
  14497. (org-eval-in-calendar '(calendar-backward-month 1))))
  14498. (org-defkey map [(meta shift right)]
  14499. (lambda () (interactive)
  14500. (org-eval-in-calendar '(calendar-forward-month 1))))
  14501. (org-defkey map [(meta shift up)]
  14502. (lambda () (interactive)
  14503. (org-eval-in-calendar '(calendar-backward-year 1))))
  14504. (org-defkey map [(meta shift down)]
  14505. (lambda () (interactive)
  14506. (org-eval-in-calendar '(calendar-forward-year 1))))
  14507. (org-defkey map [?\e (shift left)]
  14508. (lambda () (interactive)
  14509. (org-eval-in-calendar '(calendar-backward-month 1))))
  14510. (org-defkey map [?\e (shift right)]
  14511. (lambda () (interactive)
  14512. (org-eval-in-calendar '(calendar-forward-month 1))))
  14513. (org-defkey map [?\e (shift up)]
  14514. (lambda () (interactive)
  14515. (org-eval-in-calendar '(calendar-backward-year 1))))
  14516. (org-defkey map [?\e (shift down)]
  14517. (lambda () (interactive)
  14518. (org-eval-in-calendar '(calendar-forward-year 1))))
  14519. (org-defkey map [(shift up)]
  14520. (lambda () (interactive)
  14521. (org-eval-in-calendar '(calendar-backward-week 1))))
  14522. (org-defkey map [(shift down)]
  14523. (lambda () (interactive)
  14524. (org-eval-in-calendar '(calendar-forward-week 1))))
  14525. (org-defkey map [(shift left)]
  14526. (lambda () (interactive)
  14527. (org-eval-in-calendar '(calendar-backward-day 1))))
  14528. (org-defkey map [(shift right)]
  14529. (lambda () (interactive)
  14530. (org-eval-in-calendar '(calendar-forward-day 1))))
  14531. (org-defkey map "!"
  14532. (lambda () (interactive)
  14533. (org-eval-in-calendar '(diary-view-entries))
  14534. (message "")))
  14535. (org-defkey map ">"
  14536. (lambda () (interactive)
  14537. (org-eval-in-calendar '(scroll-calendar-left 1))))
  14538. (org-defkey map "<"
  14539. (lambda () (interactive)
  14540. (org-eval-in-calendar '(scroll-calendar-right 1))))
  14541. (org-defkey map "\C-v"
  14542. (lambda () (interactive)
  14543. (org-eval-in-calendar
  14544. '(calendar-scroll-left-three-months 1))))
  14545. (org-defkey map "\M-v"
  14546. (lambda () (interactive)
  14547. (org-eval-in-calendar
  14548. '(calendar-scroll-right-three-months 1))))
  14549. map)
  14550. "Keymap for minibuffer commands when using `org-read-date'.")
  14551. (defun org-read-date (&optional org-with-time to-time from-string prompt
  14552. default-time default-input inactive)
  14553. "Read a date, possibly a time, and make things smooth for the user.
  14554. The prompt will suggest to enter an ISO date, but you can also enter anything
  14555. which will at least partially be understood by `parse-time-string'.
  14556. Unrecognized parts of the date will default to the current day, month, year,
  14557. hour and minute. If this command is called to replace a timestamp at point,
  14558. or to enter the second timestamp of a range, the default time is taken
  14559. from the existing stamp. Furthermore, the command prefers the future,
  14560. so if you are giving a date where the year is not given, and the day-month
  14561. combination is already past in the current year, it will assume you
  14562. mean next year. For details, see the manual. A few examples:
  14563. 3-2-5 --> 2003-02-05
  14564. feb 15 --> currentyear-02-15
  14565. 2/15 --> currentyear-02-15
  14566. sep 12 9 --> 2009-09-12
  14567. 12:45 --> today 12:45
  14568. 22 sept 0:34 --> currentyear-09-22 0:34
  14569. 12 --> currentyear-currentmonth-12
  14570. Fri --> nearest Friday (today or later)
  14571. etc.
  14572. Furthermore you can specify a relative date by giving, as the *first* thing
  14573. in the input: a plus/minus sign, a number and a letter [hdwmy] to indicate
  14574. change in days weeks, months, years.
  14575. With a single plus or minus, the date is relative to today. With a double
  14576. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  14577. +4d --> four days from today
  14578. +4 --> same as above
  14579. +2w --> two weeks from today
  14580. ++5 --> five days from default date
  14581. The function understands only English month and weekday abbreviations.
  14582. While prompting, a calendar is popped up - you can also select the
  14583. date with the mouse (button 1). The calendar shows a period of three
  14584. months. To scroll it to other months, use the keys `>' and `<'.
  14585. If you don't like the calendar, turn it off with
  14586. \(setq org-read-date-popup-calendar nil)
  14587. With optional argument TO-TIME, the date will immediately be converted
  14588. to an internal time.
  14589. With an optional argument ORG-WITH-TIME, the prompt will suggest to
  14590. also insert a time. Note that when ORG-WITH-TIME is not set, you can
  14591. still enter a time, and this function will inform the calling routine
  14592. about this change. The calling routine may then choose to change the
  14593. format used to insert the time stamp into the buffer to include the time.
  14594. With optional argument FROM-STRING, read from this string instead from
  14595. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  14596. the time/date that is used for everything that is not specified by the
  14597. user."
  14598. (require 'parse-time)
  14599. (let* ((org-time-stamp-rounding-minutes
  14600. (if (equal org-with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  14601. (org-dcst org-display-custom-times)
  14602. (ct (org-current-time))
  14603. (org-def (or org-overriding-default-time default-time ct))
  14604. (org-defdecode (decode-time org-def))
  14605. (dummy (progn
  14606. (when (< (nth 2 org-defdecode) org-extend-today-until)
  14607. (setcar (nthcdr 2 org-defdecode) -1)
  14608. (setcar (nthcdr 1 org-defdecode) 59)
  14609. (setq org-def (apply 'encode-time org-defdecode)
  14610. org-defdecode (decode-time org-def)))))
  14611. (mouse-autoselect-window nil) ; Don't let the mouse jump
  14612. (calendar-frame-setup nil)
  14613. (calendar-setup nil)
  14614. (calendar-move-hook nil)
  14615. (calendar-view-diary-initially-flag nil)
  14616. (calendar-view-holidays-initially-flag nil)
  14617. (timestr (format-time-string
  14618. (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") org-def))
  14619. (prompt (concat (if prompt (concat prompt " ") "")
  14620. (format "Date+time [%s]: " timestr)))
  14621. ans (org-ans0 "") org-ans1 org-ans2 final)
  14622. (cond
  14623. (from-string (setq ans from-string))
  14624. (org-read-date-popup-calendar
  14625. (save-excursion
  14626. (save-window-excursion
  14627. (calendar)
  14628. (org-eval-in-calendar '(setq cursor-type nil) t)
  14629. (unwind-protect
  14630. (progn
  14631. (calendar-forward-day (- (time-to-days org-def)
  14632. (calendar-absolute-from-gregorian
  14633. (calendar-current-date))))
  14634. (org-eval-in-calendar nil t)
  14635. (let* ((old-map (current-local-map))
  14636. (map (copy-keymap calendar-mode-map))
  14637. (minibuffer-local-map
  14638. (copy-keymap org-read-date-minibuffer-local-map)))
  14639. (org-defkey map (kbd "RET") 'org-calendar-select)
  14640. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  14641. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  14642. (unwind-protect
  14643. (progn
  14644. (use-local-map map)
  14645. (setq org-read-date-inactive inactive)
  14646. (add-hook 'post-command-hook 'org-read-date-display)
  14647. (setq org-ans0 (read-string prompt default-input
  14648. 'org-read-date-history nil))
  14649. ;; org-ans0: from prompt
  14650. ;; org-ans1: from mouse click
  14651. ;; org-ans2: from calendar motion
  14652. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  14653. (remove-hook 'post-command-hook 'org-read-date-display)
  14654. (use-local-map old-map)
  14655. (when org-read-date-overlay
  14656. (delete-overlay org-read-date-overlay)
  14657. (setq org-read-date-overlay nil)))))
  14658. (bury-buffer "*Calendar*")))))
  14659. (t ; Naked prompt only
  14660. (unwind-protect
  14661. (setq ans (read-string prompt default-input
  14662. 'org-read-date-history timestr))
  14663. (when org-read-date-overlay
  14664. (delete-overlay org-read-date-overlay)
  14665. (setq org-read-date-overlay nil)))))
  14666. (setq final (org-read-date-analyze ans org-def org-defdecode))
  14667. (when org-read-date-analyze-forced-year
  14668. (message "Year was forced into %s"
  14669. (if org-read-date-force-compatible-dates
  14670. "compatible range (1970-2037)"
  14671. "range representable on this machine"))
  14672. (ding))
  14673. ;; One round trip to get rid of 34th of August and stuff like that....
  14674. (setq final (decode-time (apply 'encode-time final)))
  14675. (setq org-read-date-final-answer ans)
  14676. (if to-time
  14677. (apply 'encode-time final)
  14678. (if (and (boundp 'org-time-was-given) org-time-was-given)
  14679. (format "%04d-%02d-%02d %02d:%02d"
  14680. (nth 5 final) (nth 4 final) (nth 3 final)
  14681. (nth 2 final) (nth 1 final))
  14682. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  14683. (defvar org-def)
  14684. (defvar org-defdecode)
  14685. (defvar org-with-time)
  14686. (defun org-read-date-display ()
  14687. "Display the current date prompt interpretation in the minibuffer."
  14688. (when org-read-date-display-live
  14689. (when org-read-date-overlay
  14690. (delete-overlay org-read-date-overlay))
  14691. (when (minibufferp (current-buffer))
  14692. (save-excursion
  14693. (end-of-line 1)
  14694. (while (not (equal (buffer-substring
  14695. (max (point-min) (- (point) 4)) (point))
  14696. " "))
  14697. (insert " ")))
  14698. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  14699. " " (or org-ans1 org-ans2)))
  14700. (org-end-time-was-given nil)
  14701. (f (org-read-date-analyze ans org-def org-defdecode))
  14702. (fmts (if org-dcst
  14703. org-time-stamp-custom-formats
  14704. org-time-stamp-formats))
  14705. (fmt (if (or org-with-time
  14706. (and (boundp 'org-time-was-given) org-time-was-given))
  14707. (cdr fmts)
  14708. (car fmts)))
  14709. (txt (format-time-string fmt (apply 'encode-time f)))
  14710. (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
  14711. (txt (concat "=> " txt)))
  14712. (when (and org-end-time-was-given
  14713. (string-match org-plain-time-of-day-regexp txt))
  14714. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  14715. org-end-time-was-given
  14716. (substring txt (match-end 0)))))
  14717. (when org-read-date-analyze-futurep
  14718. (setq txt (concat txt " (=>F)")))
  14719. (setq org-read-date-overlay
  14720. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  14721. (org-overlay-display org-read-date-overlay txt 'secondary-selection)))))
  14722. (defun org-read-date-analyze (ans org-def org-defdecode)
  14723. "Analyze the combined answer of the date prompt."
  14724. ;; FIXME: cleanup and comment
  14725. (let ((nowdecode (decode-time (current-time)))
  14726. delta deltan deltaw deltadef year month day
  14727. hour minute second wday pm h2 m2 tl wday1
  14728. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  14729. (setq org-read-date-analyze-futurep nil
  14730. org-read-date-analyze-forced-year nil)
  14731. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  14732. (setq ans "+0"))
  14733. (when (setq delta (org-read-date-get-relative ans (current-time) org-def))
  14734. (setq ans (replace-match "" t t ans)
  14735. deltan (car delta)
  14736. deltaw (nth 1 delta)
  14737. deltadef (nth 2 delta)))
  14738. ;; Check if there is an iso week date in there. If yes, store the
  14739. ;; info and postpone interpreting it until the rest of the parsing
  14740. ;; is done.
  14741. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  14742. (setq iso-year (if (match-end 1)
  14743. (org-small-year-to-year
  14744. (string-to-number (match-string 1 ans))))
  14745. iso-weekday (if (match-end 3)
  14746. (string-to-number (match-string 3 ans)))
  14747. iso-week (string-to-number (match-string 2 ans)))
  14748. (setq ans (replace-match "" t t ans)))
  14749. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  14750. (when (string-match
  14751. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  14752. (setq year (if (match-end 2)
  14753. (string-to-number (match-string 2 ans))
  14754. (progn (setq kill-year t)
  14755. (string-to-number (format-time-string "%Y"))))
  14756. month (string-to-number (match-string 3 ans))
  14757. day (string-to-number (match-string 4 ans)))
  14758. (if (< year 100) (setq year (+ 2000 year)))
  14759. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  14760. t nil ans)))
  14761. ;; Help matching dotted european dates
  14762. (when (string-match
  14763. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\.\\( ?[1-9][0-9]\\{3\\}\\)?" ans)
  14764. (setq year (if (match-end 3) (string-to-number (match-string 3 ans))
  14765. (setq kill-year t)
  14766. (string-to-number (format-time-string "%Y")))
  14767. day (string-to-number (match-string 1 ans))
  14768. month (string-to-number (match-string 2 ans))
  14769. ans (replace-match (format "%04d-%02d-%02d" year month day)
  14770. t nil ans)))
  14771. ;; Help matching american dates, like 5/30 or 5/30/7
  14772. (when (string-match
  14773. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  14774. (setq year (if (match-end 4)
  14775. (string-to-number (match-string 4 ans))
  14776. (progn (setq kill-year t)
  14777. (string-to-number (format-time-string "%Y"))))
  14778. month (string-to-number (match-string 1 ans))
  14779. day (string-to-number (match-string 2 ans)))
  14780. (if (< year 100) (setq year (+ 2000 year)))
  14781. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  14782. t nil ans)))
  14783. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  14784. ;; If there is a time with am/pm, and *no* time without it, we convert
  14785. ;; so that matching will be successful.
  14786. (loop for i from 1 to 2 do ; twice, for end time as well
  14787. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  14788. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  14789. (setq hour (string-to-number (match-string 1 ans))
  14790. minute (if (match-end 3)
  14791. (string-to-number (match-string 3 ans))
  14792. 0)
  14793. pm (equal ?p
  14794. (string-to-char (downcase (match-string 4 ans)))))
  14795. (if (and (= hour 12) (not pm))
  14796. (setq hour 0)
  14797. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  14798. (setq ans (replace-match (format "%02d:%02d" hour minute)
  14799. t t ans))))
  14800. ;; Check if a time range is given as a duration
  14801. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  14802. (setq hour (string-to-number (match-string 1 ans))
  14803. h2 (+ hour (string-to-number (match-string 3 ans)))
  14804. minute (string-to-number (match-string 2 ans))
  14805. m2 (+ minute (if (match-end 5) (string-to-number
  14806. (match-string 5 ans))0)))
  14807. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  14808. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  14809. t t ans)))
  14810. ;; Check if there is a time range
  14811. (when (boundp 'org-end-time-was-given)
  14812. (setq org-time-was-given nil)
  14813. (when (and (string-match org-plain-time-of-day-regexp ans)
  14814. (match-end 8))
  14815. (setq org-end-time-was-given (match-string 8 ans))
  14816. (setq ans (concat (substring ans 0 (match-beginning 7))
  14817. (substring ans (match-end 7))))))
  14818. (setq tl (parse-time-string ans)
  14819. day (or (nth 3 tl) (nth 3 org-defdecode))
  14820. month (or (nth 4 tl)
  14821. (if (and org-read-date-prefer-future
  14822. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  14823. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  14824. (nth 4 org-defdecode)))
  14825. year (or (and (not kill-year) (nth 5 tl))
  14826. (if (and org-read-date-prefer-future
  14827. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  14828. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  14829. (nth 5 org-defdecode)))
  14830. hour (or (nth 2 tl) (nth 2 org-defdecode))
  14831. minute (or (nth 1 tl) (nth 1 org-defdecode))
  14832. second (or (nth 0 tl) 0)
  14833. wday (nth 6 tl))
  14834. (when (and (eq org-read-date-prefer-future 'time)
  14835. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  14836. (equal day (nth 3 nowdecode))
  14837. (equal month (nth 4 nowdecode))
  14838. (equal year (nth 5 nowdecode))
  14839. (nth 2 tl)
  14840. (or (< (nth 2 tl) (nth 2 nowdecode))
  14841. (and (= (nth 2 tl) (nth 2 nowdecode))
  14842. (nth 1 tl)
  14843. (< (nth 1 tl) (nth 1 nowdecode)))))
  14844. (setq day (1+ day)
  14845. futurep t))
  14846. ;; Special date definitions below
  14847. (cond
  14848. (iso-week
  14849. ;; There was an iso week
  14850. (require 'cal-iso)
  14851. (setq futurep nil)
  14852. (setq year (or iso-year year)
  14853. day (or iso-weekday wday 1)
  14854. wday nil ; to make sure that the trigger below does not match
  14855. iso-date (calendar-gregorian-from-absolute
  14856. (calendar-absolute-from-iso
  14857. (list iso-week day year))))
  14858. ; FIXME: Should we also push ISO weeks into the future?
  14859. ; (when (and org-read-date-prefer-future
  14860. ; (not iso-year)
  14861. ; (< (calendar-absolute-from-gregorian iso-date)
  14862. ; (time-to-days (current-time))))
  14863. ; (setq year (1+ year)
  14864. ; iso-date (calendar-gregorian-from-absolute
  14865. ; (calendar-absolute-from-iso
  14866. ; (list iso-week day year)))))
  14867. (setq month (car iso-date)
  14868. year (nth 2 iso-date)
  14869. day (nth 1 iso-date)))
  14870. (deltan
  14871. (setq futurep nil)
  14872. (unless deltadef
  14873. (let ((now (decode-time (current-time))))
  14874. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  14875. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  14876. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  14877. ((equal deltaw "m") (setq month (+ month deltan)))
  14878. ((equal deltaw "y") (setq year (+ year deltan)))))
  14879. ((and wday (not (nth 3 tl)))
  14880. ;; Weekday was given, but no day, so pick that day in the week
  14881. ;; on or after the derived date.
  14882. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  14883. (unless (equal wday wday1)
  14884. (setq day (+ day (% (- wday wday1 -7) 7))))))
  14885. (if (and (boundp 'org-time-was-given)
  14886. (nth 2 tl))
  14887. (setq org-time-was-given t))
  14888. (if (< year 100) (setq year (+ 2000 year)))
  14889. ;; Check of the date is representable
  14890. (if org-read-date-force-compatible-dates
  14891. (progn
  14892. (if (< year 1970)
  14893. (setq year 1970 org-read-date-analyze-forced-year t))
  14894. (if (> year 2037)
  14895. (setq year 2037 org-read-date-analyze-forced-year t)))
  14896. (condition-case nil
  14897. (ignore (encode-time second minute hour day month year))
  14898. (error
  14899. (setq year (nth 5 org-defdecode))
  14900. (setq org-read-date-analyze-forced-year t))))
  14901. (setq org-read-date-analyze-futurep futurep)
  14902. (list second minute hour day month year)))
  14903. (defvar parse-time-weekdays)
  14904. (defun org-read-date-get-relative (s today default)
  14905. "Check string S for special relative date string.
  14906. TODAY and DEFAULT are internal times, for today and for a default.
  14907. Return shift list (N what def-flag)
  14908. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  14909. N is the number of WHATs to shift.
  14910. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  14911. the DEFAULT date rather than TODAY."
  14912. (require 'parse-time)
  14913. (when (and
  14914. (string-match
  14915. (concat
  14916. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  14917. "\\([0-9]+\\)?"
  14918. "\\([hdwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  14919. "\\([ \t]\\|$\\)") s)
  14920. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  14921. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  14922. (string-to-char (substring (match-string 1 s) -1))
  14923. ?+))
  14924. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  14925. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  14926. (what (if (match-end 3) (match-string 3 s) "d"))
  14927. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  14928. (date (if rel default today))
  14929. (wday (nth 6 (decode-time date)))
  14930. delta)
  14931. (if wday1
  14932. (progn
  14933. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  14934. (if (= dir ?-) (setq delta (- delta 7)))
  14935. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  14936. (list delta "d" rel))
  14937. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  14938. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  14939. "Turn a user-specified date into the internal representation.
  14940. The internal representation needed by the calendar is (month day year).
  14941. This is a wrapper to handle the brain-dead convention in calendar that
  14942. user function argument order change dependent on argument order."
  14943. (if (boundp 'calendar-date-style)
  14944. (cond
  14945. ((eq calendar-date-style 'american)
  14946. (list arg1 arg2 arg3))
  14947. ((eq calendar-date-style 'european)
  14948. (list arg2 arg1 arg3))
  14949. ((eq calendar-date-style 'iso)
  14950. (list arg2 arg3 arg1)))
  14951. (org-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  14952. (if (org-bound-and-true-p european-calendar-style)
  14953. (list arg2 arg1 arg3)
  14954. (list arg1 arg2 arg3)))))
  14955. (defun org-eval-in-calendar (form &optional keepdate)
  14956. "Eval FORM in the calendar window and return to current window.
  14957. When KEEPDATE is non-nil, update `org-ans2' from the cursor date,
  14958. otherwise stick to the current value of `org-ans2'."
  14959. (let ((sf (selected-frame))
  14960. (sw (selected-window)))
  14961. (select-window (get-buffer-window "*Calendar*" t))
  14962. (eval form)
  14963. (when (and (not keepdate) (calendar-cursor-to-date))
  14964. (let* ((date (calendar-cursor-to-date))
  14965. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14966. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  14967. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  14968. (select-window sw)
  14969. (org-select-frame-set-input-focus sf)))
  14970. (defun org-calendar-select ()
  14971. "Return to `org-read-date' with the date currently selected.
  14972. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  14973. (interactive)
  14974. (when (calendar-cursor-to-date)
  14975. (let* ((date (calendar-cursor-to-date))
  14976. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14977. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  14978. (if (active-minibuffer-window) (exit-minibuffer))))
  14979. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  14980. "Insert a date stamp for the date given by the internal TIME.
  14981. WITH-HM means use the stamp format that includes the time of the day.
  14982. INACTIVE means use square brackets instead of angular ones, so that the
  14983. stamp will not contribute to the agenda.
  14984. PRE and POST are optional strings to be inserted before and after the
  14985. stamp.
  14986. The command returns the inserted time stamp."
  14987. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  14988. stamp)
  14989. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  14990. (insert-before-markers (or pre ""))
  14991. (when (listp extra)
  14992. (setq extra (car extra))
  14993. (if (and (stringp extra)
  14994. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  14995. (setq extra (format "-%02d:%02d"
  14996. (string-to-number (match-string 1 extra))
  14997. (string-to-number (match-string 2 extra))))
  14998. (setq extra nil)))
  14999. (when extra
  15000. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  15001. (insert-before-markers (setq stamp (format-time-string fmt time)))
  15002. (insert-before-markers (or post ""))
  15003. (setq org-last-inserted-timestamp stamp)))
  15004. (defun org-toggle-time-stamp-overlays ()
  15005. "Toggle the use of custom time stamp formats."
  15006. (interactive)
  15007. (setq org-display-custom-times (not org-display-custom-times))
  15008. (unless org-display-custom-times
  15009. (let ((p (point-min)) (bmp (buffer-modified-p)))
  15010. (while (setq p (next-single-property-change p 'display))
  15011. (if (and (get-text-property p 'display)
  15012. (eq (get-text-property p 'face) 'org-date))
  15013. (remove-text-properties
  15014. p (setq p (next-single-property-change p 'display))
  15015. '(display t))))
  15016. (set-buffer-modified-p bmp)))
  15017. (if (featurep 'xemacs)
  15018. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  15019. (org-restart-font-lock)
  15020. (setq org-table-may-need-update t)
  15021. (if org-display-custom-times
  15022. (message "Time stamps are overlaid with custom format")
  15023. (message "Time stamp overlays removed")))
  15024. (defun org-display-custom-time (beg end)
  15025. "Overlay modified time stamp format over timestamp between BEG and END."
  15026. (let* ((ts (buffer-substring beg end))
  15027. t1 w1 with-hm tf time str w2 (off 0))
  15028. (save-match-data
  15029. (setq t1 (org-parse-time-string ts t))
  15030. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)?\\'" ts)
  15031. (setq off (- (match-end 0) (match-beginning 0)))))
  15032. (setq end (- end off))
  15033. (setq w1 (- end beg)
  15034. with-hm (and (nth 1 t1) (nth 2 t1))
  15035. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  15036. time (org-fix-decoded-time t1)
  15037. str (org-add-props
  15038. (format-time-string
  15039. (substring tf 1 -1) (apply 'encode-time time))
  15040. nil 'mouse-face 'highlight)
  15041. w2 (length str))
  15042. (if (not (= w2 w1))
  15043. (add-text-properties (1+ beg) (+ 2 beg)
  15044. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  15045. (if (featurep 'xemacs)
  15046. (progn
  15047. (put-text-property beg end 'invisible t)
  15048. (put-text-property beg end 'end-glyph (make-glyph str)))
  15049. (put-text-property beg end 'display str))))
  15050. (defun org-translate-time (string)
  15051. "Translate all timestamps in STRING to custom format.
  15052. But do this only if the variable `org-display-custom-times' is set."
  15053. (when org-display-custom-times
  15054. (save-match-data
  15055. (let* ((start 0)
  15056. (re org-ts-regexp-both)
  15057. t1 with-hm inactive tf time str beg end)
  15058. (while (setq start (string-match re string start))
  15059. (setq beg (match-beginning 0)
  15060. end (match-end 0)
  15061. t1 (save-match-data
  15062. (org-parse-time-string (substring string beg end) t))
  15063. with-hm (and (nth 1 t1) (nth 2 t1))
  15064. inactive (equal (substring string beg (1+ beg)) "[")
  15065. tf (funcall (if with-hm 'cdr 'car)
  15066. org-time-stamp-custom-formats)
  15067. time (org-fix-decoded-time t1)
  15068. str (format-time-string
  15069. (concat
  15070. (if inactive "[" "<") (substring tf 1 -1)
  15071. (if inactive "]" ">"))
  15072. (apply 'encode-time time))
  15073. string (replace-match str t t string)
  15074. start (+ start (length str)))))))
  15075. string)
  15076. (defun org-fix-decoded-time (time)
  15077. "Set 0 instead of nil for the first 6 elements of time.
  15078. Don't touch the rest."
  15079. (let ((n 0))
  15080. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  15081. (define-obsolete-function-alias 'org-days-to-time 'org-time-stamp-to-now "24.4")
  15082. (defun org-time-stamp-to-now (timestamp-string &optional seconds)
  15083. "Difference between TIMESTAMP-STRING and now in days.
  15084. If SECONDS is non-nil, return the difference in seconds."
  15085. (let ((fdiff (if seconds 'org-float-time 'time-to-days)))
  15086. (- (funcall fdiff (org-time-string-to-time timestamp-string))
  15087. (funcall fdiff (current-time)))))
  15088. (defun org-deadline-close (timestamp-string &optional ndays)
  15089. "Is the time in TIMESTAMP-STRING close to the current date?"
  15090. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  15091. (and (< (org-time-stamp-to-now timestamp-string) ndays)
  15092. (not (org-entry-is-done-p))))
  15093. (defun org-get-wdays (ts &optional delay zero-delay)
  15094. "Get the deadline lead time appropriate for timestring TS.
  15095. When DELAY is non-nil, get the delay time for scheduled items
  15096. instead of the deadline lead time. When ZERO-DELAY is non-nil
  15097. and `org-scheduled-delay-days' is 0, enforce 0 as the delay,
  15098. don't try to find the delay cookie in the scheduled timestamp."
  15099. (let ((tv (if delay org-scheduled-delay-days
  15100. org-deadline-warning-days)))
  15101. (cond
  15102. ((or (and delay (< tv 0))
  15103. (and delay zero-delay (<= tv 0))
  15104. (and (not delay) (<= tv 0)))
  15105. ;; Enforce this value no matter what
  15106. (- tv))
  15107. ((string-match "-\\([0-9]+\\)\\([hdwmy]\\)\\(\\'\\|>\\| \\)" ts)
  15108. ;; lead time is specified.
  15109. (floor (* (string-to-number (match-string 1 ts))
  15110. (cdr (assoc (match-string 2 ts)
  15111. '(("d" . 1) ("w" . 7)
  15112. ("m" . 30.4) ("y" . 365.25)
  15113. ("h" . 0.041667)))))))
  15114. ;; go for the default.
  15115. (t tv))))
  15116. (defun org-calendar-select-mouse (ev)
  15117. "Return to `org-read-date' with the date currently selected.
  15118. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15119. (interactive "e")
  15120. (mouse-set-point ev)
  15121. (when (calendar-cursor-to-date)
  15122. (let* ((date (calendar-cursor-to-date))
  15123. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15124. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15125. (if (active-minibuffer-window) (exit-minibuffer))))
  15126. (defun org-check-deadlines (ndays)
  15127. "Check if there are any deadlines due or past due.
  15128. A deadline is considered due if it happens within `org-deadline-warning-days'
  15129. days from today's date. If the deadline appears in an entry marked DONE,
  15130. it is not shown. The prefix arg NDAYS can be used to test that many
  15131. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  15132. (interactive "P")
  15133. (let* ((org-warn-days
  15134. (cond
  15135. ((equal ndays '(4)) 100000)
  15136. (ndays (prefix-numeric-value ndays))
  15137. (t (abs org-deadline-warning-days))))
  15138. (case-fold-search nil)
  15139. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15140. (callback
  15141. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  15142. (message "%d deadlines past-due or due within %d days"
  15143. (org-occur regexp nil callback)
  15144. org-warn-days)))
  15145. (defsubst org-re-timestamp (type)
  15146. "Return a regexp for timestamp TYPE.
  15147. Allowed values for TYPE are:
  15148. all: all timestamps
  15149. active: only active timestamps (<...>)
  15150. inactive: only inactive timestamps ([...])
  15151. scheduled: only scheduled timestamps
  15152. deadline: only deadline timestamps
  15153. When TYPE is nil, fall back on returning a regexp that matches
  15154. both scheduled and deadline timestamps."
  15155. (cond ((eq type 'all) "\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}\\(?: +[^]+0-9> \n -]+\\)?\\(?: +[0-9]\\{1,2\\}:[0-9]\\{2\\}\\)?\\)")
  15156. ((eq type 'active) org-ts-regexp)
  15157. ((eq type 'inactive) "\\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^ \n>]*?\\)\\]")
  15158. ((eq type 'scheduled) (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>"))
  15159. ((eq type 'deadline) (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15160. ((eq type 'scheduled-or-deadline)
  15161. (concat "\\<\\(?:" org-deadline-string "\\|" org-scheduled-string "\\) *<\\([^>]+\\)>"))))
  15162. (defun org-check-before-date (date)
  15163. "Check if there are deadlines or scheduled entries before DATE."
  15164. (interactive (list (org-read-date)))
  15165. (let ((case-fold-search nil)
  15166. (regexp (org-re-timestamp org-ts-type))
  15167. (callback
  15168. (lambda () (time-less-p
  15169. (org-time-string-to-time (match-string 1))
  15170. (org-time-string-to-time date)))))
  15171. (message "%d entries before %s"
  15172. (org-occur regexp nil callback) date)))
  15173. (defun org-check-after-date (date)
  15174. "Check if there are deadlines or scheduled entries after DATE."
  15175. (interactive (list (org-read-date)))
  15176. (let ((case-fold-search nil)
  15177. (regexp (org-re-timestamp org-ts-type))
  15178. (callback
  15179. (lambda () (not
  15180. (time-less-p
  15181. (org-time-string-to-time (match-string 1))
  15182. (org-time-string-to-time date))))))
  15183. (message "%d entries after %s"
  15184. (org-occur regexp nil callback) date)))
  15185. (defun org-check-dates-range (start-date end-date)
  15186. "Check for deadlines/scheduled entries between START-DATE and END-DATE."
  15187. (interactive (list (org-read-date nil nil nil "Range starts")
  15188. (org-read-date nil nil nil "Range end")))
  15189. (let ((case-fold-search nil)
  15190. (regexp (org-re-timestamp org-ts-type))
  15191. (callback
  15192. (lambda ()
  15193. (let ((match (match-string 1)))
  15194. (and
  15195. (not (time-less-p
  15196. (org-time-string-to-time match)
  15197. (org-time-string-to-time start-date)))
  15198. (time-less-p
  15199. (org-time-string-to-time match)
  15200. (org-time-string-to-time end-date)))))))
  15201. (message "%d entries between %s and %s"
  15202. (org-occur regexp nil callback) start-date end-date)))
  15203. (defun org-evaluate-time-range (&optional to-buffer)
  15204. "Evaluate a time range by computing the difference between start and end.
  15205. Normally the result is just printed in the echo area, but with prefix arg
  15206. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  15207. If the time range is actually in a table, the result is inserted into the
  15208. next column.
  15209. For time difference computation, a year is assumed to be exactly 365
  15210. days in order to avoid rounding problems."
  15211. (interactive "P")
  15212. (or
  15213. (org-clock-update-time-maybe)
  15214. (save-excursion
  15215. (unless (org-at-date-range-p t)
  15216. (goto-char (point-at-bol))
  15217. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  15218. (if (not (org-at-date-range-p t))
  15219. (error "Not at a time-stamp range, and none found in current line")))
  15220. (let* ((ts1 (match-string 1))
  15221. (ts2 (match-string 2))
  15222. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  15223. (match-end (match-end 0))
  15224. (time1 (org-time-string-to-time ts1))
  15225. (time2 (org-time-string-to-time ts2))
  15226. (t1 (org-float-time time1))
  15227. (t2 (org-float-time time2))
  15228. (diff (abs (- t2 t1)))
  15229. (negative (< (- t2 t1) 0))
  15230. ;; (ys (floor (* 365 24 60 60)))
  15231. (ds (* 24 60 60))
  15232. (hs (* 60 60))
  15233. (fy "%dy %dd %02d:%02d")
  15234. (fy1 "%dy %dd")
  15235. (fd "%dd %02d:%02d")
  15236. (fd1 "%dd")
  15237. (fh "%02d:%02d")
  15238. y d h m align)
  15239. (if havetime
  15240. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  15241. y 0
  15242. d (floor (/ diff ds)) diff (mod diff ds)
  15243. h (floor (/ diff hs)) diff (mod diff hs)
  15244. m (floor (/ diff 60)))
  15245. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  15246. y 0
  15247. d (floor (+ (/ diff ds) 0.5))
  15248. h 0 m 0))
  15249. (if (not to-buffer)
  15250. (message "%s" (org-make-tdiff-string y d h m))
  15251. (if (org-at-table-p)
  15252. (progn
  15253. (goto-char match-end)
  15254. (setq align t)
  15255. (and (looking-at " *|") (goto-char (match-end 0))))
  15256. (goto-char match-end))
  15257. (if (looking-at
  15258. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  15259. (replace-match ""))
  15260. (if negative (insert " -"))
  15261. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  15262. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  15263. (insert " " (format fh h m))))
  15264. (if align (org-table-align))
  15265. (message "Time difference inserted")))))
  15266. (defun org-make-tdiff-string (y d h m)
  15267. (let ((fmt "")
  15268. (l nil))
  15269. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  15270. l (push y l)))
  15271. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  15272. l (push d l)))
  15273. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  15274. l (push h l)))
  15275. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  15276. l (push m l)))
  15277. (apply 'format fmt (nreverse l))))
  15278. (defun org-time-string-to-time (s &optional buffer pos)
  15279. "Convert a timestamp string into internal time."
  15280. (condition-case errdata
  15281. (apply 'encode-time (org-parse-time-string s))
  15282. (error (error "Bad timestamp `%s'%s\nError was: %s"
  15283. s (if (not (and buffer pos))
  15284. ""
  15285. (format " at %d in buffer `%s'" pos buffer))
  15286. (cdr errdata)))))
  15287. (defun org-time-string-to-seconds (s)
  15288. "Convert a timestamp string to a number of seconds."
  15289. (org-float-time (org-time-string-to-time s)))
  15290. (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
  15291. "Convert a time stamp to an absolute day number.
  15292. If there is a specifier for a cyclic time stamp, get the closest
  15293. date to DAYNR.
  15294. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  15295. The variable `date' is bound by the calendar when this is called."
  15296. (cond
  15297. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  15298. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  15299. daynr
  15300. (+ daynr 1000)))
  15301. ((and daynr (string-match "\\+[0-9]+[hdwmy]" s))
  15302. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  15303. (time-to-days (current-time))) (match-string 0 s)
  15304. prefer show-all))
  15305. (t (time-to-days
  15306. (condition-case errdata
  15307. (apply 'encode-time (org-parse-time-string s))
  15308. (error (error "Bad timestamp `%s'%s\nError was: %s"
  15309. s (if (not (and buffer pos))
  15310. ""
  15311. (format " at %d in buffer `%s'" pos buffer))
  15312. (cdr errdata))))))))
  15313. (defun org-days-to-iso-week (days)
  15314. "Return the iso week number."
  15315. (require 'cal-iso)
  15316. (car (calendar-iso-from-absolute days)))
  15317. (defun org-small-year-to-year (year)
  15318. "Convert 2-digit years into 4-digit years.
  15319. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  15320. The year 2000 cannot be abbreviated. Any year larger than 99
  15321. is returned unchanged."
  15322. (if (< year 38)
  15323. (setq year (+ 2000 year))
  15324. (if (< year 100)
  15325. (setq year (+ 1900 year))))
  15326. year)
  15327. (defun org-time-from-absolute (d)
  15328. "Return the time corresponding to date D.
  15329. D may be an absolute day number, or a calendar-type list (month day year)."
  15330. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  15331. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  15332. (defun org-calendar-holiday ()
  15333. "List of holidays, for Diary display in Org-mode."
  15334. (require 'holidays)
  15335. (let ((hl (funcall
  15336. (if (fboundp 'calendar-check-holidays)
  15337. 'calendar-check-holidays 'check-calendar-holidays) date)))
  15338. (if hl (mapconcat 'identity hl "; "))))
  15339. (defun org-diary-sexp-entry (sexp entry date)
  15340. "Process a SEXP diary ENTRY for DATE."
  15341. (require 'diary-lib)
  15342. (let ((result (if calendar-debug-sexp
  15343. (let ((stack-trace-on-error t))
  15344. (eval (car (read-from-string sexp))))
  15345. (condition-case nil
  15346. (eval (car (read-from-string sexp)))
  15347. (error
  15348. (beep)
  15349. (message "Bad sexp at line %d in %s: %s"
  15350. (org-current-line)
  15351. (buffer-file-name) sexp)
  15352. (sleep-for 2))))))
  15353. (cond ((stringp result) (split-string result "; "))
  15354. ((and (consp result)
  15355. (not (consp (cdr result)))
  15356. (stringp (cdr result))) (cdr result))
  15357. ((and (consp result)
  15358. (stringp (car result))) result)
  15359. (result entry))))
  15360. (defun org-diary-to-ical-string (frombuf)
  15361. "Get iCalendar entries from diary entries in buffer FROMBUF.
  15362. This uses the icalendar.el library."
  15363. (let* ((tmpdir (if (featurep 'xemacs)
  15364. (temp-directory)
  15365. temporary-file-directory))
  15366. (tmpfile (make-temp-name
  15367. (expand-file-name "orgics" tmpdir)))
  15368. buf rtn b e)
  15369. (with-current-buffer frombuf
  15370. (icalendar-export-region (point-min) (point-max) tmpfile)
  15371. (setq buf (find-buffer-visiting tmpfile))
  15372. (set-buffer buf)
  15373. (goto-char (point-min))
  15374. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  15375. (setq b (match-beginning 0)))
  15376. (goto-char (point-max))
  15377. (if (re-search-backward "^END:VEVENT" nil t)
  15378. (setq e (match-end 0)))
  15379. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  15380. (kill-buffer buf)
  15381. (delete-file tmpfile)
  15382. rtn))
  15383. (defun org-closest-date (start current change prefer show-all)
  15384. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  15385. When PREFER is `past', return a date that is either CURRENT or past.
  15386. When PREFER is `future', return a date that is either CURRENT or future.
  15387. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  15388. ;; Make the proper lists from the dates
  15389. (catch 'exit
  15390. (let ((a1 '(("h" . hour)
  15391. ("d" . day)
  15392. ("w" . week)
  15393. ("m" . month)
  15394. ("y" . year)))
  15395. (shour (nth 2 (org-parse-time-string start)))
  15396. dn dw sday cday n1 n2 n0
  15397. d m y y1 y2 date1 date2 nmonths nm ny m2)
  15398. (setq start (org-date-to-gregorian start)
  15399. current (org-date-to-gregorian
  15400. (if show-all
  15401. current
  15402. (time-to-days (current-time))))
  15403. sday (calendar-absolute-from-gregorian start)
  15404. cday (calendar-absolute-from-gregorian current))
  15405. (if (<= cday sday) (throw 'exit sday))
  15406. (if (string-match "\\(\\+[0-9]+\\)\\([hdwmy]\\)" change)
  15407. (setq dn (string-to-number (match-string 1 change))
  15408. dw (cdr (assoc (match-string 2 change) a1)))
  15409. (error "Invalid change specifier: %s" change))
  15410. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  15411. (cond
  15412. ((eq dw 'hour)
  15413. (let ((missing-hours
  15414. (mod (+ (- (* 24 (- cday sday)) shour) org-extend-today-until)
  15415. dn)))
  15416. (setq n1 (if (zerop missing-hours) cday
  15417. (- cday (1+ (floor (/ missing-hours 24)))))
  15418. n2 (+ cday (floor (/ (- dn missing-hours) 24))))))
  15419. ((eq dw 'day)
  15420. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  15421. n2 (+ n1 dn)))
  15422. ((eq dw 'year)
  15423. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  15424. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  15425. (setq date1 (list m d y1)
  15426. n1 (calendar-absolute-from-gregorian date1)
  15427. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  15428. n2 (calendar-absolute-from-gregorian date2)))
  15429. ((eq dw 'month)
  15430. ;; approx number of month between the two dates
  15431. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  15432. ;; How often does dn fit in there?
  15433. (setq d (nth 1 start) m (car start) y (nth 2 start)
  15434. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  15435. m (+ m nm)
  15436. ny (floor (/ m 12))
  15437. y (+ y ny)
  15438. m (- m (* ny 12)))
  15439. (while (> m 12) (setq m (- m 12) y (1+ y)))
  15440. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  15441. (setq m2 (+ m dn) y2 y)
  15442. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  15443. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  15444. (while (<= n2 cday)
  15445. (setq n1 n2 m m2 y y2)
  15446. (setq m2 (+ m dn) y2 y)
  15447. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  15448. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  15449. ;; Make sure n1 is the earlier date
  15450. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  15451. (if show-all
  15452. (cond
  15453. ((eq prefer 'past) (if (= cday n2) n2 n1))
  15454. ((eq prefer 'future) (if (= cday n1) n1 n2))
  15455. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  15456. (cond
  15457. ((eq prefer 'past) (if (= cday n2) n2 n1))
  15458. ((eq prefer 'future) (if (= cday n1) n1 n2))
  15459. (t (if (= cday n1) n1 n2)))))))
  15460. (defun org-date-to-gregorian (date)
  15461. "Turn any specification of DATE into a Gregorian date for the calendar."
  15462. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  15463. ((and (listp date) (= (length date) 3)) date)
  15464. ((stringp date)
  15465. (setq date (org-parse-time-string date))
  15466. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  15467. ((listp date)
  15468. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  15469. (defun org-parse-time-string (s &optional nodefault)
  15470. "Parse the standard Org-mode time string.
  15471. This should be a lot faster than the normal `parse-time-string'.
  15472. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  15473. hour and minute fields will be nil if not given."
  15474. (cond ((string-match org-ts-regexp0 s)
  15475. (list 0
  15476. (if (or (match-beginning 8) (not nodefault))
  15477. (string-to-number (or (match-string 8 s) "0")))
  15478. (if (or (match-beginning 7) (not nodefault))
  15479. (string-to-number (or (match-string 7 s) "0")))
  15480. (string-to-number (match-string 4 s))
  15481. (string-to-number (match-string 3 s))
  15482. (string-to-number (match-string 2 s))
  15483. nil nil nil))
  15484. ((string-match "^<[^>]+>$" s)
  15485. (decode-time (seconds-to-time (org-matcher-time s))))
  15486. (t (error "Not a standard Org-mode time string: %s" s))))
  15487. (defun org-timestamp-up (&optional arg)
  15488. "Increase the date item at the cursor by one.
  15489. If the cursor is on the year, change the year. If it is on the month,
  15490. the day or the time, change that.
  15491. With prefix ARG, change by that many units."
  15492. (interactive "p")
  15493. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  15494. (defun org-timestamp-down (&optional arg)
  15495. "Decrease the date item at the cursor by one.
  15496. If the cursor is on the year, change the year. If it is on the month,
  15497. the day or the time, change that.
  15498. With prefix ARG, change by that many units."
  15499. (interactive "p")
  15500. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  15501. (defun org-timestamp-up-day (&optional arg)
  15502. "Increase the date in the time stamp by one day.
  15503. With prefix ARG, change that many days."
  15504. (interactive "p")
  15505. (if (and (not (org-at-timestamp-p t))
  15506. (org-at-heading-p))
  15507. (org-todo 'up)
  15508. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  15509. (defun org-timestamp-down-day (&optional arg)
  15510. "Decrease the date in the time stamp by one day.
  15511. With prefix ARG, change that many days."
  15512. (interactive "p")
  15513. (if (and (not (org-at-timestamp-p t))
  15514. (org-at-heading-p))
  15515. (org-todo 'down)
  15516. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  15517. (defun org-at-timestamp-p (&optional inactive-ok)
  15518. "Determine if the cursor is in or at a timestamp."
  15519. (interactive)
  15520. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  15521. (pos (point))
  15522. (ans (or (looking-at tsr)
  15523. (save-excursion
  15524. (skip-chars-backward "^[<\n\r\t")
  15525. (if (> (point) (point-min)) (backward-char 1))
  15526. (and (looking-at tsr)
  15527. (> (- (match-end 0) pos) -1))))))
  15528. (and ans
  15529. (boundp 'org-ts-what)
  15530. (setq org-ts-what
  15531. (cond
  15532. ((= pos (match-beginning 0)) 'bracket)
  15533. ;; Point is considered to be "on the bracket" whether
  15534. ;; it's really on it or right after it.
  15535. ((= pos (1- (match-end 0))) 'bracket)
  15536. ((= pos (match-end 0)) 'after)
  15537. ((org-pos-in-match-range pos 2) 'year)
  15538. ((org-pos-in-match-range pos 3) 'month)
  15539. ((org-pos-in-match-range pos 7) 'hour)
  15540. ((org-pos-in-match-range pos 8) 'minute)
  15541. ((or (org-pos-in-match-range pos 4)
  15542. (org-pos-in-match-range pos 5)) 'day)
  15543. ((and (> pos (or (match-end 8) (match-end 5)))
  15544. (< pos (match-end 0)))
  15545. (- pos (or (match-end 8) (match-end 5))))
  15546. (t 'day))))
  15547. ans))
  15548. (defun org-toggle-timestamp-type ()
  15549. "Toggle the type (<active> or [inactive]) of a time stamp."
  15550. (interactive)
  15551. (when (org-at-timestamp-p t)
  15552. (let ((beg (match-beginning 0)) (end (match-end 0))
  15553. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  15554. (save-excursion
  15555. (goto-char beg)
  15556. (while (re-search-forward "[][<>]" end t)
  15557. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  15558. t t)))
  15559. (message "Timestamp is now %sactive"
  15560. (if (equal (char-after beg) ?<) "" "in")))))
  15561. (defun org-at-clock-log-p nil
  15562. "Is the cursor on the clock log line?"
  15563. (save-excursion
  15564. (move-beginning-of-line 1)
  15565. (looking-at "^[ \t]*CLOCK:")))
  15566. (defvar org-clock-history) ; defined in org-clock.el
  15567. (defvar org-clock-adjust-closest nil) ; defined in org-clock.el
  15568. (defun org-timestamp-change (n &optional what updown suppress-tmp-delay)
  15569. "Change the date in the time stamp at point.
  15570. The date will be changed by N times WHAT. WHAT can be `day', `month',
  15571. `year', `minute', `second'. If WHAT is not given, the cursor position
  15572. in the timestamp determines what will be changed.
  15573. When SUPPRESS-TMP-DELAY is non-nil, suppress delays like \"--2d\"."
  15574. (let ((origin (point)) origin-cat
  15575. with-hm inactive
  15576. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  15577. org-ts-what
  15578. extra rem
  15579. ts time time0 fixnext clrgx)
  15580. (if (not (org-at-timestamp-p t))
  15581. (error "Not at a timestamp"))
  15582. (if (and (not what) (eq org-ts-what 'bracket))
  15583. (org-toggle-timestamp-type)
  15584. ;; Point isn't on brackets. Remember the part of the time-stamp
  15585. ;; the point was in. Indeed, size of time-stamps may change,
  15586. ;; but point must be kept in the same category nonetheless.
  15587. (setq origin-cat org-ts-what)
  15588. (if (and (not what) (not (eq org-ts-what 'day))
  15589. org-display-custom-times
  15590. (get-text-property (point) 'display)
  15591. (not (get-text-property (1- (point)) 'display)))
  15592. (setq org-ts-what 'day))
  15593. (setq org-ts-what (or what org-ts-what)
  15594. inactive (= (char-after (match-beginning 0)) ?\[)
  15595. ts (match-string 0))
  15596. (replace-match "")
  15597. (when (string-match
  15598. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?-?[-+][0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)*\\)[]>]"
  15599. ts)
  15600. (setq extra (match-string 1 ts))
  15601. (if suppress-tmp-delay
  15602. (setq extra (replace-regexp-in-string " --[0-9]+[hdwmy]" "" extra))))
  15603. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  15604. (setq with-hm t))
  15605. (setq time0 (org-parse-time-string ts))
  15606. (when (and updown
  15607. (eq org-ts-what 'minute)
  15608. (not current-prefix-arg))
  15609. ;; This looks like s-up and s-down. Change by one rounding step.
  15610. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  15611. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  15612. (setcar (cdr time0) (+ (nth 1 time0)
  15613. (if (> n 0) (- rem) (- dm rem))))))
  15614. (setq time
  15615. (encode-time (or (car time0) 0)
  15616. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  15617. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  15618. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  15619. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  15620. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  15621. (nthcdr 6 time0)))
  15622. (when (and (member org-ts-what '(hour minute))
  15623. extra
  15624. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  15625. (setq extra (org-modify-ts-extra
  15626. extra
  15627. (if (eq org-ts-what 'hour) 2 5)
  15628. n dm)))
  15629. (when (integerp org-ts-what)
  15630. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  15631. (if (eq what 'calendar)
  15632. (let ((cal-date (org-get-date-from-calendar)))
  15633. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  15634. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  15635. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  15636. (setcar time0 (or (car time0) 0))
  15637. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  15638. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  15639. (setq time (apply 'encode-time time0))))
  15640. ;; Insert the new time-stamp, and ensure point stays in the same
  15641. ;; category as before (i.e. not after the last position in that
  15642. ;; category).
  15643. (let ((pos (point)))
  15644. ;; Stay before inserted string. `save-excursion' is of no use.
  15645. (setq org-last-changed-timestamp
  15646. (org-insert-time-stamp time with-hm inactive nil nil extra))
  15647. (goto-char pos))
  15648. (save-match-data
  15649. (looking-at org-ts-regexp3)
  15650. (goto-char (cond
  15651. ;; `day' category ends before `hour' if any, or at
  15652. ;; the end of the day name.
  15653. ((eq origin-cat 'day)
  15654. (min (or (match-beginning 7) (1- (match-end 5))) origin))
  15655. ((eq origin-cat 'hour) (min (match-end 7) origin))
  15656. ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
  15657. ((integerp origin-cat) (min (1- (match-end 0)) origin))
  15658. ;; `year' and `month' have both fixed size: point
  15659. ;; couldn't have moved into another part.
  15660. (t origin))))
  15661. ;; Update clock if on a CLOCK line.
  15662. (org-clock-update-time-maybe)
  15663. ;; Maybe adjust the closest clock in `org-clock-history'
  15664. (when org-clock-adjust-closest
  15665. (if (not (and (org-at-clock-log-p)
  15666. (< 1 (length (delq nil (mapcar (lambda(m) (marker-position m))
  15667. org-clock-history))))))
  15668. (message "No clock to adjust")
  15669. (cond ((save-excursion ; fix previous clock?
  15670. (re-search-backward org-ts-regexp0 nil t)
  15671. (org-looking-back (concat org-clock-string " \\[")))
  15672. (setq fixnext 1 clrgx (concat org-ts-regexp0 "\\] =>.*$")))
  15673. ((save-excursion ; fix next clock?
  15674. (re-search-backward org-ts-regexp0 nil t)
  15675. (looking-at (concat org-ts-regexp0 "\\] =>")))
  15676. (setq fixnext -1 clrgx (concat org-clock-string " \\[" org-ts-regexp0))))
  15677. (save-window-excursion
  15678. ;; Find closest clock to point, adjust the previous/next one in history
  15679. (let* ((p (save-excursion (org-back-to-heading t)))
  15680. (cl (mapcar (lambda(c) (abs (- (marker-position c) p))) org-clock-history))
  15681. (clfixnth
  15682. (+ fixnext (- (length cl) (or (length (member (apply #'min cl) cl)) 100))))
  15683. (clfixpos (if (> 0 clfixnth) nil (nth clfixnth org-clock-history))))
  15684. (if (not clfixpos)
  15685. (message "No clock to adjust")
  15686. (save-excursion
  15687. (org-goto-marker-or-bmk clfixpos)
  15688. (org-show-subtree)
  15689. (when (re-search-forward clrgx nil t)
  15690. (goto-char (match-beginning 1))
  15691. (let (org-clock-adjust-closest)
  15692. (org-timestamp-change n org-ts-what updown))
  15693. (message "Clock adjusted in %s for heading: %s"
  15694. (file-name-nondirectory (buffer-file-name))
  15695. (org-get-heading t t)))))))))
  15696. ;; Try to recenter the calendar window, if any.
  15697. (if (and org-calendar-follow-timestamp-change
  15698. (get-buffer-window "*Calendar*" t)
  15699. (memq org-ts-what '(day month year)))
  15700. (org-recenter-calendar (time-to-days time))))))
  15701. (defun org-modify-ts-extra (s pos n dm)
  15702. "Change the different parts of the lead-time and repeat fields in timestamp."
  15703. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  15704. ng h m new rem)
  15705. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  15706. (cond
  15707. ((or (org-pos-in-match-range pos 2)
  15708. (org-pos-in-match-range pos 3))
  15709. (setq m (string-to-number (match-string 3 s))
  15710. h (string-to-number (match-string 2 s)))
  15711. (if (org-pos-in-match-range pos 2)
  15712. (setq h (+ h n))
  15713. (setq n (* dm (org-no-warnings (signum n))))
  15714. (when (not (= 0 (setq rem (% m dm))))
  15715. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  15716. (setq m (+ m n)))
  15717. (if (< m 0) (setq m (+ m 60) h (1- h)))
  15718. (if (> m 59) (setq m (- m 60) h (1+ h)))
  15719. (setq h (min 24 (max 0 h)))
  15720. (setq ng 1 new (format "-%02d:%02d" h m)))
  15721. ((org-pos-in-match-range pos 6)
  15722. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  15723. ((org-pos-in-match-range pos 5)
  15724. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  15725. ((org-pos-in-match-range pos 9)
  15726. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  15727. ((org-pos-in-match-range pos 8)
  15728. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  15729. (when ng
  15730. (setq s (concat
  15731. (substring s 0 (match-beginning ng))
  15732. new
  15733. (substring s (match-end ng))))))
  15734. s))
  15735. (defun org-recenter-calendar (date)
  15736. "If the calendar is visible, recenter it to DATE."
  15737. (let ((cwin (get-buffer-window "*Calendar*" t)))
  15738. (when cwin
  15739. (let ((calendar-move-hook nil))
  15740. (with-selected-window cwin
  15741. (calendar-goto-date (if (listp date) date
  15742. (calendar-gregorian-from-absolute date))))))))
  15743. (defun org-goto-calendar (&optional arg)
  15744. "Go to the Emacs calendar at the current date.
  15745. If there is a time stamp in the current line, go to that date.
  15746. A prefix ARG can be used to force the current date."
  15747. (interactive "P")
  15748. (let ((tsr org-ts-regexp) diff
  15749. (calendar-move-hook nil)
  15750. (calendar-view-holidays-initially-flag nil)
  15751. (calendar-view-diary-initially-flag nil))
  15752. (if (or (org-at-timestamp-p)
  15753. (save-excursion
  15754. (beginning-of-line 1)
  15755. (looking-at (concat ".*" tsr))))
  15756. (let ((d1 (time-to-days (current-time)))
  15757. (d2 (time-to-days
  15758. (org-time-string-to-time (match-string 1)))))
  15759. (setq diff (- d2 d1))))
  15760. (calendar)
  15761. (calendar-goto-today)
  15762. (if (and diff (not arg)) (calendar-forward-day diff))))
  15763. (defun org-get-date-from-calendar ()
  15764. "Return a list (month day year) of date at point in calendar."
  15765. (with-current-buffer "*Calendar*"
  15766. (save-match-data
  15767. (calendar-cursor-to-date))))
  15768. (defun org-date-from-calendar ()
  15769. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  15770. If there is already a time stamp at the cursor position, update it."
  15771. (interactive)
  15772. (if (org-at-timestamp-p t)
  15773. (org-timestamp-change 0 'calendar)
  15774. (let ((cal-date (org-get-date-from-calendar)))
  15775. (org-insert-time-stamp
  15776. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  15777. (defcustom org-effort-durations
  15778. `(("h" . 60)
  15779. ("d" . ,(* 60 8))
  15780. ("w" . ,(* 60 8 5))
  15781. ("m" . ,(* 60 8 5 4))
  15782. ("y" . ,(* 60 8 5 40)))
  15783. "Conversion factor to minutes for an effort modifier.
  15784. Each entry has the form (MODIFIER . MINUTES).
  15785. In an effort string, a number followed by MODIFIER is multiplied
  15786. by the specified number of MINUTES to obtain an effort in
  15787. minutes.
  15788. For example, if the value of this variable is ((\"hours\" . 60)), then an
  15789. effort string \"2hours\" is equivalent to 120 minutes."
  15790. :group 'org-agenda
  15791. :version "24.1"
  15792. :type '(alist :key-type (string :tag "Modifier")
  15793. :value-type (number :tag "Minutes")))
  15794. (defun org-minutes-to-clocksum-string (m)
  15795. "Format number of minutes as a clocksum string.
  15796. The format is determined by `org-time-clocksum-format',
  15797. `org-time-clocksum-use-fractional' and
  15798. `org-time-clocksum-fractional-format' and
  15799. `org-time-clocksum-use-effort-durations'."
  15800. (let ((clocksum "") h d w mo y fmt n)
  15801. (setq h (if org-time-clocksum-use-effort-durations
  15802. (cdr (assoc "h" org-effort-durations)) 60)
  15803. d (if org-time-clocksum-use-effort-durations
  15804. (/ (cdr (assoc "d" org-effort-durations)) h) 24)
  15805. w (if org-time-clocksum-use-effort-durations
  15806. (/ (cdr (assoc "w" org-effort-durations)) (* d h)) 7)
  15807. mo (if org-time-clocksum-use-effort-durations
  15808. (/ (cdr (assoc "m" org-effort-durations)) (* d h)) 30)
  15809. y (if org-time-clocksum-use-effort-durations
  15810. (/ (cdr (assoc "y" org-effort-durations)) (* d h)) 365))
  15811. ;; fractional format
  15812. (if org-time-clocksum-use-fractional
  15813. (cond
  15814. ;; single format string
  15815. ((stringp org-time-clocksum-fractional-format)
  15816. (format org-time-clocksum-fractional-format (/ m (float h))))
  15817. ;; choice of fractional formats for different time units
  15818. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :years))
  15819. (> (/ (truncate m) (* y d h)) 0))
  15820. (format fmt (/ m (* y d (float h)))))
  15821. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :months))
  15822. (> (/ (truncate m) (* mo d h)) 0))
  15823. (format fmt (/ m (* mo d (float h)))))
  15824. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  15825. (> (/ (truncate m) (* w d h)) 0))
  15826. (format fmt (/ m (* w d (float h)))))
  15827. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :days))
  15828. (> (/ (truncate m) (* d h)) 0))
  15829. (format fmt (/ m (* d (float h)))))
  15830. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  15831. (> (/ (truncate m) h) 0))
  15832. (format fmt (/ m (float h))))
  15833. ((setq fmt (plist-get org-time-clocksum-fractional-format :minutes))
  15834. (format fmt m))
  15835. ;; fall back to smallest time unit with a format
  15836. ((setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  15837. (format fmt (/ m (float h))))
  15838. ((setq fmt (plist-get org-time-clocksum-fractional-format :days))
  15839. (format fmt (/ m (* d (float h)))))
  15840. ((setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  15841. (format fmt (/ m (* w d (float h)))))
  15842. ((setq fmt (plist-get org-time-clocksum-fractional-format :months))
  15843. (format fmt (/ m (* mo d (float h)))))
  15844. ((setq fmt (plist-get org-time-clocksum-fractional-format :years))
  15845. (format fmt (/ m (* y d (float h))))))
  15846. ;; standard (non-fractional) format, with single format string
  15847. (if (stringp org-time-clocksum-format)
  15848. (format org-time-clocksum-format (setq n (/ m h)) (- m (* h n)))
  15849. ;; separate formats components
  15850. (and (setq fmt (plist-get org-time-clocksum-format :years))
  15851. (or (> (setq n (/ (truncate m) (* y d h))) 0)
  15852. (plist-get org-time-clocksum-format :require-years))
  15853. (setq clocksum (concat clocksum (format fmt n))
  15854. m (- m (* n y d h))))
  15855. (and (setq fmt (plist-get org-time-clocksum-format :months))
  15856. (or (> (setq n (/ (truncate m) (* mo d h))) 0)
  15857. (plist-get org-time-clocksum-format :require-months))
  15858. (setq clocksum (concat clocksum (format fmt n))
  15859. m (- m (* n mo d h))))
  15860. (and (setq fmt (plist-get org-time-clocksum-format :weeks))
  15861. (or (> (setq n (/ (truncate m) (* w d h))) 0)
  15862. (plist-get org-time-clocksum-format :require-weeks))
  15863. (setq clocksum (concat clocksum (format fmt n))
  15864. m (- m (* n w d h))))
  15865. (and (setq fmt (plist-get org-time-clocksum-format :days))
  15866. (or (> (setq n (/ (truncate m) (* d h))) 0)
  15867. (plist-get org-time-clocksum-format :require-days))
  15868. (setq clocksum (concat clocksum (format fmt n))
  15869. m (- m (* n d h))))
  15870. (and (setq fmt (plist-get org-time-clocksum-format :hours))
  15871. (or (> (setq n (/ (truncate m) h)) 0)
  15872. (plist-get org-time-clocksum-format :require-hours))
  15873. (setq clocksum (concat clocksum (format fmt n))
  15874. m (- m (* n h))))
  15875. (and (setq fmt (plist-get org-time-clocksum-format :minutes))
  15876. (or (> m 0) (plist-get org-time-clocksum-format :require-minutes))
  15877. (setq clocksum (concat clocksum (format fmt m))))
  15878. ;; return formatted time duration
  15879. clocksum))))
  15880. (defalias 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string)
  15881. (make-obsolete 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string
  15882. "Org mode version 8.0")
  15883. (defun org-hours-to-clocksum-string (n)
  15884. (org-minutes-to-clocksum-string (* n 60)))
  15885. (defun org-hh:mm-string-to-minutes (s)
  15886. "Convert a string H:MM to a number of minutes.
  15887. If the string is just a number, interpret it as minutes.
  15888. In fact, the first hh:mm or number in the string will be taken,
  15889. there can be extra stuff in the string.
  15890. If no number is found, the return value is 0."
  15891. (cond
  15892. ((integerp s) s)
  15893. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  15894. (+ (* (string-to-number (match-string 1 s)) 60)
  15895. (string-to-number (match-string 2 s))))
  15896. ((string-match "\\([0-9]+\\)" s)
  15897. (string-to-number (match-string 1 s)))
  15898. (t 0)))
  15899. (defcustom org-image-actual-width t
  15900. "Should we use the actual width of images when inlining them?
  15901. When set to `t', always use the image width.
  15902. When set to a number, use imagemagick (when available) to set
  15903. the image's width to this value.
  15904. When set to a number in a list, try to get the width from the
  15905. #+ATTR.* keyword if it matches a width specification like
  15906. width=\"[0-9]+\", and fall back on that number if none is found.
  15907. When set to nil, try to get the width from an #+ATTR.* keyword
  15908. and fall back on the original width if none is found.
  15909. This requires Emacs >= 24.1, build with imagemagick support."
  15910. :group 'org-appearance
  15911. :version "24.4"
  15912. :package-version '(Org . "8.0")
  15913. :type '(choice
  15914. (const :tag "Use the image width" t)
  15915. (integer :tag "Use a number of pixels")
  15916. (list :tag "Use #+ATTR* or a number of pixels" (integer))
  15917. (const :tag "Use #+ATTR* or don't resize" nil)))
  15918. (defcustom org-agenda-inhibit-startup nil
  15919. "Inhibit startup when preparing agenda buffers.
  15920. When this variable is `t' (the default), the initialization of
  15921. the Org agenda buffers is inhibited: e.g. the visibility state
  15922. is not set, the tables are not re-aligned, etc."
  15923. :type 'boolean
  15924. :version "24.3"
  15925. :group 'org-agenda)
  15926. (defun org-duration-string-to-minutes (s &optional output-to-string)
  15927. "Convert a duration string S to minutes.
  15928. A bare number is interpreted as minutes, modifiers can be set by
  15929. customizing `org-effort-durations' (which see).
  15930. Entries containing a colon are interpreted as H:MM by
  15931. `org-hh:mm-string-to-minutes'."
  15932. (let ((result 0)
  15933. (re (concat "\\([0-9.]+\\) *\\("
  15934. (regexp-opt (mapcar 'car org-effort-durations))
  15935. "\\)")))
  15936. (while (string-match re s)
  15937. (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  15938. (string-to-number (match-string 1 s))))
  15939. (setq s (replace-match "" nil t s)))
  15940. (setq result (floor result))
  15941. (incf result (org-hh:mm-string-to-minutes s))
  15942. (if output-to-string (number-to-string result) result)))
  15943. ;;;; Files
  15944. (defun org-save-all-org-buffers ()
  15945. "Save all Org-mode buffers without user confirmation."
  15946. (interactive)
  15947. (message "Saving all Org-mode buffers...")
  15948. (save-some-buffers t (lambda () (derived-mode-p 'org-mode)))
  15949. (when (featurep 'org-id) (org-id-locations-save))
  15950. (message "Saving all Org-mode buffers... done"))
  15951. (defun org-revert-all-org-buffers ()
  15952. "Revert all Org-mode buffers.
  15953. Prompt for confirmation when there are unsaved changes.
  15954. Be sure you know what you are doing before letting this function
  15955. overwrite your changes.
  15956. This function is useful in a setup where one tracks org files
  15957. with a version control system, to revert on one machine after pulling
  15958. changes from another. I believe the procedure must be like this:
  15959. 1. M-x org-save-all-org-buffers
  15960. 2. Pull changes from the other machine, resolve conflicts
  15961. 3. M-x org-revert-all-org-buffers"
  15962. (interactive)
  15963. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  15964. (error "Abort"))
  15965. (save-excursion
  15966. (save-window-excursion
  15967. (mapc
  15968. (lambda (b)
  15969. (when (and (with-current-buffer b (derived-mode-p 'org-mode))
  15970. (with-current-buffer b buffer-file-name))
  15971. (org-pop-to-buffer-same-window b)
  15972. (revert-buffer t 'no-confirm)))
  15973. (buffer-list))
  15974. (when (and (featurep 'org-id) org-id-track-globally)
  15975. (org-id-locations-load)))))
  15976. ;;;; Agenda files
  15977. ;;;###autoload
  15978. (defun org-switchb (&optional arg)
  15979. "Switch between Org buffers.
  15980. With one prefix argument, restrict available buffers to files.
  15981. With two prefix arguments, restrict available buffers to agenda files.
  15982. Defaults to `iswitchb' for buffer name completion.
  15983. Set `org-completion-use-ido' to make it use ido instead."
  15984. (interactive "P")
  15985. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  15986. ((equal arg '(16)) (org-buffer-list 'agenda))
  15987. (t (org-buffer-list))))
  15988. (org-completion-use-iswitchb org-completion-use-iswitchb)
  15989. (org-completion-use-ido org-completion-use-ido))
  15990. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  15991. (setq org-completion-use-iswitchb t))
  15992. (org-pop-to-buffer-same-window
  15993. (org-icompleting-read "Org buffer: "
  15994. (mapcar 'list (mapcar 'buffer-name blist))
  15995. nil t))))
  15996. ;;; Define some older names previously used for this functionality
  15997. ;;;###autoload
  15998. (defalias 'org-ido-switchb 'org-switchb)
  15999. ;;;###autoload
  16000. (defalias 'org-iswitchb 'org-switchb)
  16001. (defun org-buffer-list (&optional predicate exclude-tmp)
  16002. "Return a list of Org buffers.
  16003. PREDICATE can be `export', `files' or `agenda'.
  16004. export restrict the list to Export buffers.
  16005. files restrict the list to buffers visiting Org files.
  16006. agenda restrict the list to buffers visiting agenda files.
  16007. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  16008. (let* ((bfn nil)
  16009. (agenda-files (and (eq predicate 'agenda)
  16010. (mapcar 'file-truename (org-agenda-files t))))
  16011. (filter
  16012. (cond
  16013. ((eq predicate 'files)
  16014. (lambda (b) (with-current-buffer b (derived-mode-p 'org-mode))))
  16015. ((eq predicate 'export)
  16016. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  16017. ((eq predicate 'agenda)
  16018. (lambda (b)
  16019. (with-current-buffer b
  16020. (and (derived-mode-p 'org-mode)
  16021. (setq bfn (buffer-file-name b))
  16022. (member (file-truename bfn) agenda-files)))))
  16023. (t (lambda (b) (with-current-buffer b
  16024. (or (derived-mode-p 'org-mode)
  16025. (string-match "\*Org .*Export"
  16026. (buffer-name b)))))))))
  16027. (delq nil
  16028. (mapcar
  16029. (lambda(b)
  16030. (if (and (funcall filter b)
  16031. (or (not exclude-tmp)
  16032. (not (string-match "tmp" (buffer-name b)))))
  16033. b
  16034. nil))
  16035. (buffer-list)))))
  16036. (defun org-agenda-files (&optional unrestricted archives)
  16037. "Get the list of agenda files.
  16038. Optional UNRESTRICTED means return the full list even if a restriction
  16039. is currently in place.
  16040. When ARCHIVES is t, include all archive files that are really being
  16041. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  16042. `org-agenda-archives-mode' is t."
  16043. (let ((files
  16044. (cond
  16045. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  16046. ((stringp org-agenda-files) (org-read-agenda-file-list))
  16047. ((listp org-agenda-files) org-agenda-files)
  16048. (t (error "Invalid value of `org-agenda-files'")))))
  16049. (setq files (apply 'append
  16050. (mapcar (lambda (f)
  16051. (if (file-directory-p f)
  16052. (directory-files
  16053. f t org-agenda-file-regexp)
  16054. (list f)))
  16055. files)))
  16056. (when org-agenda-skip-unavailable-files
  16057. (setq files (delq nil
  16058. (mapcar (function
  16059. (lambda (file)
  16060. (and (file-readable-p file) file)))
  16061. files))))
  16062. (when (or (eq archives t)
  16063. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  16064. (setq files (org-add-archive-files files)))
  16065. files))
  16066. (defun org-agenda-file-p (&optional file)
  16067. "Return non-nil, if FILE is an agenda file.
  16068. If FILE is omitted, use the file associated with the current
  16069. buffer."
  16070. (member (or file (buffer-file-name))
  16071. (org-agenda-files t)))
  16072. (defun org-edit-agenda-file-list ()
  16073. "Edit the list of agenda files.
  16074. Depending on setup, this either uses customize to edit the variable
  16075. `org-agenda-files', or it visits the file that is holding the list. In the
  16076. latter case, the buffer is set up in a way that saving it automatically kills
  16077. the buffer and restores the previous window configuration."
  16078. (interactive)
  16079. (if (stringp org-agenda-files)
  16080. (let ((cw (current-window-configuration)))
  16081. (find-file org-agenda-files)
  16082. (org-set-local 'org-window-configuration cw)
  16083. (org-add-hook 'after-save-hook
  16084. (lambda ()
  16085. (set-window-configuration
  16086. (prog1 org-window-configuration
  16087. (kill-buffer (current-buffer))))
  16088. (org-install-agenda-files-menu)
  16089. (message "New agenda file list installed"))
  16090. nil 'local)
  16091. (message "%s" (substitute-command-keys
  16092. "Edit list and finish with \\[save-buffer]")))
  16093. (customize-variable 'org-agenda-files)))
  16094. (defun org-store-new-agenda-file-list (list)
  16095. "Set new value for the agenda file list and save it correctly."
  16096. (if (stringp org-agenda-files)
  16097. (let ((fe (org-read-agenda-file-list t)) b u)
  16098. (while (setq b (find-buffer-visiting org-agenda-files))
  16099. (kill-buffer b))
  16100. (with-temp-file org-agenda-files
  16101. (insert
  16102. (mapconcat
  16103. (lambda (f) ;; Keep un-expanded entries.
  16104. (if (setq u (assoc f fe))
  16105. (cdr u)
  16106. f))
  16107. list "\n")
  16108. "\n")))
  16109. (let ((org-mode-hook nil) (org-inhibit-startup t)
  16110. (org-insert-mode-line-in-empty-file nil))
  16111. (setq org-agenda-files list)
  16112. (customize-save-variable 'org-agenda-files org-agenda-files))))
  16113. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  16114. "Read the list of agenda files from a file.
  16115. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  16116. filenames, used by `org-store-new-agenda-file-list' to write back
  16117. un-expanded file names."
  16118. (when (file-directory-p org-agenda-files)
  16119. (error "`org-agenda-files' cannot be a single directory"))
  16120. (when (stringp org-agenda-files)
  16121. (with-temp-buffer
  16122. (insert-file-contents org-agenda-files)
  16123. (mapcar
  16124. (lambda (f)
  16125. (let ((e (expand-file-name (substitute-in-file-name f)
  16126. org-directory)))
  16127. (if pair-with-expansion
  16128. (cons e f)
  16129. e)))
  16130. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  16131. ;;;###autoload
  16132. (defun org-cycle-agenda-files ()
  16133. "Cycle through the files in `org-agenda-files'.
  16134. If the current buffer visits an agenda file, find the next one in the list.
  16135. If the current buffer does not, find the first agenda file."
  16136. (interactive)
  16137. (let* ((fs (org-agenda-files t))
  16138. (files (append fs (list (car fs))))
  16139. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  16140. file)
  16141. (unless files (error "No agenda files"))
  16142. (catch 'exit
  16143. (while (setq file (pop files))
  16144. (if (equal (file-truename file) tcf)
  16145. (when (car files)
  16146. (find-file (car files))
  16147. (throw 'exit t))))
  16148. (find-file (car fs)))
  16149. (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
  16150. (defun org-agenda-file-to-front (&optional to-end)
  16151. "Move/add the current file to the top of the agenda file list.
  16152. If the file is not present in the list, it is added to the front. If it is
  16153. present, it is moved there. With optional argument TO-END, add/move to the
  16154. end of the list."
  16155. (interactive "P")
  16156. (let ((org-agenda-skip-unavailable-files nil)
  16157. (file-alist (mapcar (lambda (x)
  16158. (cons (file-truename x) x))
  16159. (org-agenda-files t)))
  16160. (ctf (file-truename
  16161. (or buffer-file-name
  16162. (error "Please save the current buffer to a file"))))
  16163. x had)
  16164. (setq x (assoc ctf file-alist) had x)
  16165. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  16166. (if to-end
  16167. (setq file-alist (append (delq x file-alist) (list x)))
  16168. (setq file-alist (cons x (delq x file-alist))))
  16169. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  16170. (org-install-agenda-files-menu)
  16171. (message "File %s to %s of agenda file list"
  16172. (if had "moved" "added") (if to-end "end" "front"))))
  16173. (defun org-remove-file (&optional file)
  16174. "Remove current file from the list of files in variable `org-agenda-files'.
  16175. These are the files which are being checked for agenda entries.
  16176. Optional argument FILE means use this file instead of the current."
  16177. (interactive)
  16178. (let* ((org-agenda-skip-unavailable-files nil)
  16179. (file (or file buffer-file-name
  16180. (error "Current buffer does not visit a file")))
  16181. (true-file (file-truename file))
  16182. (afile (abbreviate-file-name file))
  16183. (files (delq nil (mapcar
  16184. (lambda (x)
  16185. (if (equal true-file
  16186. (file-truename x))
  16187. nil x))
  16188. (org-agenda-files t)))))
  16189. (if (not (= (length files) (length (org-agenda-files t))))
  16190. (progn
  16191. (org-store-new-agenda-file-list files)
  16192. (org-install-agenda-files-menu)
  16193. (message "Removed file: %s" afile))
  16194. (message "File was not in list: %s (not removed)" afile))))
  16195. (defun org-file-menu-entry (file)
  16196. (vector file (list 'find-file file) t))
  16197. (defun org-check-agenda-file (file)
  16198. "Make sure FILE exists. If not, ask user what to do."
  16199. (when (not (file-exists-p file))
  16200. (message "Non-existent agenda file %s. [R]emove from list or [A]bort?"
  16201. (abbreviate-file-name file))
  16202. (let ((r (downcase (read-char-exclusive))))
  16203. (cond
  16204. ((equal r ?r)
  16205. (org-remove-file file)
  16206. (throw 'nextfile t))
  16207. (t (error "Abort"))))))
  16208. (defun org-get-agenda-file-buffer (file)
  16209. "Get a buffer visiting FILE. If the buffer needs to be created, add
  16210. it to the list of buffers which might be released later."
  16211. (let ((buf (org-find-base-buffer-visiting file)))
  16212. (if buf
  16213. buf ; just return it
  16214. ;; Make a new buffer and remember it
  16215. (setq buf (find-file-noselect file))
  16216. (if buf (push buf org-agenda-new-buffers))
  16217. buf)))
  16218. (defun org-release-buffers (blist)
  16219. "Release all buffers in list, asking the user for confirmation when needed.
  16220. When a buffer is unmodified, it is just killed. When modified, it is saved
  16221. \(if the user agrees) and then killed."
  16222. (let (buf file)
  16223. (while (setq buf (pop blist))
  16224. (setq file (buffer-file-name buf))
  16225. (when (and (buffer-modified-p buf)
  16226. file
  16227. (y-or-n-p (format "Save file %s? " file)))
  16228. (with-current-buffer buf (save-buffer)))
  16229. (kill-buffer buf))))
  16230. (defun org-agenda-prepare-buffers (files)
  16231. "Create buffers for all agenda files, protect archived trees and comments."
  16232. (interactive)
  16233. (let ((pa '(:org-archived t))
  16234. (pc '(:org-comment t))
  16235. (pall '(:org-archived t :org-comment t))
  16236. (inhibit-read-only t)
  16237. (org-inhibit-startup org-agenda-inhibit-startup)
  16238. (rea (concat ":" org-archive-tag ":"))
  16239. file re)
  16240. (save-excursion
  16241. (save-restriction
  16242. (while (setq file (pop files))
  16243. (catch 'nextfile
  16244. (if (bufferp file)
  16245. (set-buffer file)
  16246. (org-check-agenda-file file)
  16247. (set-buffer (org-get-agenda-file-buffer file)))
  16248. (widen)
  16249. (org-refresh-category-properties)
  16250. (org-refresh-properties org-effort-property 'org-effort)
  16251. (org-refresh-properties "APPT_WARNTIME" 'org-appt-warntime)
  16252. (setq org-todo-keywords-for-agenda
  16253. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  16254. (setq org-done-keywords-for-agenda
  16255. (append org-done-keywords-for-agenda org-done-keywords))
  16256. (setq org-todo-keyword-alist-for-agenda
  16257. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  16258. (setq org-drawers-for-agenda
  16259. (append org-drawers-for-agenda org-drawers))
  16260. (setq org-tag-alist-for-agenda
  16261. (append org-tag-alist-for-agenda org-tag-alist))
  16262. (org-with-silent-modifications
  16263. (save-excursion
  16264. (remove-text-properties (point-min) (point-max) pall)
  16265. (when org-agenda-skip-archived-trees
  16266. (goto-char (point-min))
  16267. (while (re-search-forward rea nil t)
  16268. (if (org-at-heading-p t)
  16269. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  16270. (goto-char (point-min))
  16271. (setq re (format org-heading-keyword-regexp-format
  16272. org-comment-string))
  16273. (while (re-search-forward re nil t)
  16274. (add-text-properties
  16275. (match-beginning 0) (org-end-of-subtree t) pc))))))))
  16276. (setq org-todo-keywords-for-agenda
  16277. (org-uniquify org-todo-keywords-for-agenda))
  16278. (setq org-todo-keyword-alist-for-agenda
  16279. (org-uniquify org-todo-keyword-alist-for-agenda)
  16280. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  16281. ;;;; CDLaTeX minor mode
  16282. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  16283. "Keymap for the minor `org-cdlatex-mode'.")
  16284. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  16285. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  16286. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  16287. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  16288. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  16289. (defvar org-cdlatex-texmathp-advice-is-done nil
  16290. "Flag remembering if we have applied the advice to texmathp already.")
  16291. (define-minor-mode org-cdlatex-mode
  16292. "Toggle the minor `org-cdlatex-mode'.
  16293. This mode supports entering LaTeX environment and math in LaTeX fragments
  16294. in Org-mode.
  16295. \\{org-cdlatex-mode-map}"
  16296. nil " OCDL" nil
  16297. (when org-cdlatex-mode
  16298. (require 'cdlatex)
  16299. (run-hooks 'cdlatex-mode-hook)
  16300. (cdlatex-compute-tables))
  16301. (unless org-cdlatex-texmathp-advice-is-done
  16302. (setq org-cdlatex-texmathp-advice-is-done t)
  16303. (defadvice texmathp (around org-math-always-on activate)
  16304. "Always return t in org-mode buffers.
  16305. This is because we want to insert math symbols without dollars even outside
  16306. the LaTeX math segments. If Orgmode thinks that point is actually inside
  16307. an embedded LaTeX fragment, let texmathp do its job.
  16308. \\[org-cdlatex-mode-map]"
  16309. (interactive)
  16310. (let (p)
  16311. (cond
  16312. ((not (derived-mode-p 'org-mode)) ad-do-it)
  16313. ((eq this-command 'cdlatex-math-symbol)
  16314. (setq ad-return-value t
  16315. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  16316. (t
  16317. (let ((p (org-inside-LaTeX-fragment-p)))
  16318. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  16319. (setq ad-return-value t
  16320. texmathp-why '("Org-mode embedded math" . 0))
  16321. (if p ad-do-it)))))))))
  16322. (defun turn-on-org-cdlatex ()
  16323. "Unconditionally turn on `org-cdlatex-mode'."
  16324. (org-cdlatex-mode 1))
  16325. (defun org-try-cdlatex-tab ()
  16326. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  16327. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  16328. - inside a LaTeX fragment, or
  16329. - after the first word in a line, where an abbreviation expansion could
  16330. insert a LaTeX environment."
  16331. (when org-cdlatex-mode
  16332. (cond
  16333. ;; Before any word on the line: No expansion possible.
  16334. ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
  16335. ;; Just after first word on the line: Expand it. Make sure it
  16336. ;; cannot happen on headlines, though.
  16337. ((save-excursion
  16338. (skip-chars-backward "a-zA-Z0-9*")
  16339. (skip-chars-backward " \t")
  16340. (and (bolp) (not (org-at-heading-p))))
  16341. (cdlatex-tab) t)
  16342. ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
  16343. (defun org-cdlatex-underscore-caret (&optional arg)
  16344. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  16345. Revert to the normal definition outside of these fragments."
  16346. (interactive "P")
  16347. (if (org-inside-LaTeX-fragment-p)
  16348. (call-interactively 'cdlatex-sub-superscript)
  16349. (let (org-cdlatex-mode)
  16350. (call-interactively (key-binding (vector last-input-event))))))
  16351. (defun org-cdlatex-math-modify (&optional arg)
  16352. "Execute `cdlatex-math-modify' in LaTeX fragments.
  16353. Revert to the normal definition outside of these fragments."
  16354. (interactive "P")
  16355. (if (org-inside-LaTeX-fragment-p)
  16356. (call-interactively 'cdlatex-math-modify)
  16357. (let (org-cdlatex-mode)
  16358. (call-interactively (key-binding (vector last-input-event))))))
  16359. ;;;; LaTeX fragments
  16360. (defvar org-latex-regexps
  16361. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  16362. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  16363. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  16364. ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  16365. ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  16366. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  16367. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  16368. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  16369. "Regular expressions for matching embedded LaTeX.")
  16370. (defun org-inside-LaTeX-fragment-p ()
  16371. "Test if point is inside a LaTeX fragment.
  16372. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  16373. sequence appearing also before point.
  16374. Even though the matchers for math are configurable, this function assumes
  16375. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  16376. delimiters are skipped when they have been removed by customization.
  16377. The return value is nil, or a cons cell with the delimiter and the
  16378. position of this delimiter.
  16379. This function does a reasonably good job, but can locally be fooled by
  16380. for example currency specifications. For example it will assume being in
  16381. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  16382. fragments that are properly closed, but during editing, we have to live
  16383. with the uncertainty caused by missing closing delimiters. This function
  16384. looks only before point, not after."
  16385. (catch 'exit
  16386. (let ((pos (point))
  16387. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  16388. (lim (progn
  16389. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  16390. (point)))
  16391. dd-on str (start 0) m re)
  16392. (goto-char pos)
  16393. (when dodollar
  16394. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  16395. re (nth 1 (assoc "$" org-latex-regexps)))
  16396. (while (string-match re str start)
  16397. (cond
  16398. ((= (match-end 0) (length str))
  16399. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  16400. ((= (match-end 0) (- (length str) 5))
  16401. (throw 'exit nil))
  16402. (t (setq start (match-end 0))))))
  16403. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  16404. (goto-char pos)
  16405. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  16406. (and (match-beginning 2) (throw 'exit nil))
  16407. ;; count $$
  16408. (while (re-search-backward "\\$\\$" lim t)
  16409. (setq dd-on (not dd-on)))
  16410. (goto-char pos)
  16411. (if dd-on (cons "$$" m))))))
  16412. (defun org-inside-latex-macro-p ()
  16413. "Is point inside a LaTeX macro or its arguments?"
  16414. (save-match-data
  16415. (org-in-regexp
  16416. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  16417. (defvar org-latex-fragment-image-overlays nil
  16418. "List of overlays carrying the images of latex fragments.")
  16419. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  16420. (defun org-remove-latex-fragment-image-overlays ()
  16421. "Remove all overlays with LaTeX fragment images in current buffer."
  16422. (mapc 'delete-overlay org-latex-fragment-image-overlays)
  16423. (setq org-latex-fragment-image-overlays nil))
  16424. (defun org-preview-latex-fragment (&optional subtree)
  16425. "Preview the LaTeX fragment at point, or all locally or globally.
  16426. If the cursor is in a LaTeX fragment, create the image and overlay
  16427. it over the source code. If there is no fragment at point, display
  16428. all fragments in the current text, from one headline to the next. With
  16429. prefix SUBTREE, display all fragments in the current subtree. With a
  16430. double prefix arg \\[universal-argument] \\[universal-argument], or when \
  16431. the cursor is before the first headline,
  16432. display all fragments in the buffer.
  16433. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  16434. (interactive "P")
  16435. (unless buffer-file-name
  16436. (error "Can't preview LaTeX fragment in a non-file buffer"))
  16437. (org-remove-latex-fragment-image-overlays)
  16438. (save-excursion
  16439. (save-restriction
  16440. (let (beg end at msg)
  16441. (cond
  16442. ((or (equal subtree '(16))
  16443. (not (save-excursion
  16444. (re-search-backward org-outline-regexp-bol nil t))))
  16445. (setq beg (point-min) end (point-max)
  16446. msg "Creating images for buffer...%s"))
  16447. ((equal subtree '(4))
  16448. (org-back-to-heading)
  16449. (setq beg (point) end (org-end-of-subtree t)
  16450. msg "Creating images for subtree...%s"))
  16451. (t
  16452. (if (setq at (org-inside-LaTeX-fragment-p))
  16453. (goto-char (max (point-min) (- (cdr at) 2)))
  16454. (org-back-to-heading))
  16455. (setq beg (point) end (progn (outline-next-heading) (point))
  16456. msg (if at "Creating image...%s"
  16457. "Creating images for entry...%s"))))
  16458. (message msg "")
  16459. (narrow-to-region beg end)
  16460. (goto-char beg)
  16461. (org-format-latex
  16462. (concat org-latex-preview-ltxpng-directory (file-name-sans-extension
  16463. (file-name-nondirectory
  16464. buffer-file-name)))
  16465. default-directory 'overlays msg at 'forbuffer
  16466. org-latex-create-formula-image-program)
  16467. (message msg "done. Use `C-c C-c' to remove images.")))))
  16468. (defun org-format-latex (prefix &optional dir overlays msg at
  16469. forbuffer processing-type)
  16470. "Replace LaTeX fragments with links to an image, and produce images.
  16471. Some of the options can be changed using the variable
  16472. `org-format-latex-options'."
  16473. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  16474. (let* ((prefixnodir (file-name-nondirectory prefix))
  16475. (absprefix (expand-file-name prefix dir))
  16476. (todir (file-name-directory absprefix))
  16477. (opt org-format-latex-options)
  16478. (optnew org-format-latex-options)
  16479. (matchers (plist-get opt :matchers))
  16480. (re-list org-latex-regexps)
  16481. (cnt 0) txt hash link beg end re e checkdir
  16482. string
  16483. m n block-type block linkfile movefile ov)
  16484. ;; Check the different regular expressions
  16485. (while (setq e (pop re-list))
  16486. (setq m (car e) re (nth 1 e) n (nth 2 e) block-type (nth 3 e)
  16487. block (if block-type "\n\n" ""))
  16488. (when (member m matchers)
  16489. (goto-char (point-min))
  16490. (while (re-search-forward re nil t)
  16491. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  16492. (or (not overlays)
  16493. (not (eq (get-char-property (match-beginning n)
  16494. 'org-overlay-type)
  16495. 'org-latex-overlay))))
  16496. (cond
  16497. ((eq processing-type 'verbatim))
  16498. ((eq processing-type 'mathjax)
  16499. ;; Prepare for MathJax processing.
  16500. (setq string (match-string n))
  16501. (when (member m '("$" "$1"))
  16502. (save-excursion
  16503. (delete-region (match-beginning n) (match-end n))
  16504. (goto-char (match-beginning n))
  16505. (insert (concat "\\(" (substring string 1 -1) "\\)")))))
  16506. ((or (eq processing-type 'dvipng)
  16507. (eq processing-type 'imagemagick))
  16508. ;; Process to an image.
  16509. (setq txt (match-string n)
  16510. beg (match-beginning n) end (match-end n)
  16511. cnt (1+ cnt))
  16512. (let ((face (face-at-point))
  16513. (fg (plist-get opt :foreground))
  16514. (bg (plist-get opt :background))
  16515. ;; Ensure full list is printed.
  16516. print-length print-level)
  16517. (when forbuffer
  16518. ;; Get the colors from the face at point.
  16519. (goto-char beg)
  16520. (when (eq fg 'auto)
  16521. (setq fg (face-attribute face :foreground nil 'default)))
  16522. (when (eq bg 'auto)
  16523. (setq bg (face-attribute face :background nil 'default)))
  16524. (setq optnew (copy-sequence opt))
  16525. (plist-put optnew :foreground fg)
  16526. (plist-put optnew :background bg))
  16527. (setq hash (sha1 (prin1-to-string
  16528. (list org-format-latex-header
  16529. org-latex-default-packages-alist
  16530. org-latex-packages-alist
  16531. org-format-latex-options
  16532. forbuffer txt fg bg)))
  16533. linkfile (format "%s_%s.png" prefix hash)
  16534. movefile (format "%s_%s.png" absprefix hash)))
  16535. (setq link (concat block "[[file:" linkfile "]]" block))
  16536. (if msg (message msg cnt))
  16537. (goto-char beg)
  16538. (unless checkdir ; Ensure the directory exists.
  16539. (setq checkdir t)
  16540. (or (file-directory-p todir) (make-directory todir t)))
  16541. (unless (file-exists-p movefile)
  16542. (org-create-formula-image
  16543. txt movefile optnew forbuffer processing-type))
  16544. (if overlays
  16545. (progn
  16546. (mapc (lambda (o)
  16547. (if (eq (overlay-get o 'org-overlay-type)
  16548. 'org-latex-overlay)
  16549. (delete-overlay o)))
  16550. (overlays-in beg end))
  16551. (setq ov (make-overlay beg end))
  16552. (overlay-put ov 'org-overlay-type 'org-latex-overlay)
  16553. (if (featurep 'xemacs)
  16554. (progn
  16555. (overlay-put ov 'invisible t)
  16556. (overlay-put
  16557. ov 'end-glyph
  16558. (make-glyph (vector 'png :file movefile))))
  16559. (overlay-put
  16560. ov 'display
  16561. (list 'image :type 'png :file movefile :ascent 'center)))
  16562. (push ov org-latex-fragment-image-overlays)
  16563. (goto-char end))
  16564. (delete-region beg end)
  16565. (insert (org-add-props link
  16566. (list 'org-latex-src
  16567. (replace-regexp-in-string
  16568. "\"" "" txt)
  16569. 'org-latex-src-embed-type
  16570. (if block-type 'paragraph 'character))))))
  16571. ((eq processing-type 'mathml)
  16572. ;; Process to MathML
  16573. (unless (save-match-data (org-format-latex-mathml-available-p))
  16574. (error "LaTeX to MathML converter not configured"))
  16575. (setq txt (match-string n)
  16576. beg (match-beginning n) end (match-end n)
  16577. cnt (1+ cnt))
  16578. (if msg (message msg cnt))
  16579. (goto-char beg)
  16580. (delete-region beg end)
  16581. (insert (org-format-latex-as-mathml
  16582. txt block-type prefix dir)))
  16583. (t
  16584. (error "Unknown conversion type %s for latex fragments"
  16585. processing-type)))))))))
  16586. (defun org-create-math-formula (latex-frag &optional mathml-file)
  16587. "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
  16588. Use `org-latex-to-mathml-convert-command'. If the conversion is
  16589. sucessful, return the portion between \"<math...> </math>\"
  16590. elements otherwise return nil. When MATHML-FILE is specified,
  16591. write the results in to that file. When invoked as an
  16592. interactive command, prompt for LATEX-FRAG, with initial value
  16593. set to the current active region and echo the results for user
  16594. inspection."
  16595. (interactive (list (let ((frag (when (org-region-active-p)
  16596. (buffer-substring-no-properties
  16597. (region-beginning) (region-end)))))
  16598. (read-string "LaTeX Fragment: " frag nil frag))))
  16599. (unless latex-frag (error "Invalid latex-frag"))
  16600. (let* ((tmp-in-file (file-relative-name
  16601. (make-temp-name (expand-file-name "ltxmathml-in"))))
  16602. (ignore (write-region latex-frag nil tmp-in-file))
  16603. (tmp-out-file (file-relative-name
  16604. (make-temp-name (expand-file-name "ltxmathml-out"))))
  16605. (cmd (format-spec
  16606. org-latex-to-mathml-convert-command
  16607. `((?j . ,(shell-quote-argument
  16608. (expand-file-name org-latex-to-mathml-jar-file)))
  16609. (?I . ,(shell-quote-argument tmp-in-file))
  16610. (?o . ,(shell-quote-argument tmp-out-file)))))
  16611. mathml shell-command-output)
  16612. (when (org-called-interactively-p 'any)
  16613. (unless (org-format-latex-mathml-available-p)
  16614. (error "LaTeX to MathML converter not configured")))
  16615. (message "Running %s" cmd)
  16616. (setq shell-command-output (shell-command-to-string cmd))
  16617. (setq mathml
  16618. (when (file-readable-p tmp-out-file)
  16619. (with-current-buffer (find-file-noselect tmp-out-file t)
  16620. (goto-char (point-min))
  16621. (when (re-search-forward
  16622. (concat
  16623. (regexp-quote
  16624. "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">")
  16625. "\\(.\\|\n\\)*"
  16626. (regexp-quote "</math>")) nil t)
  16627. (prog1 (match-string 0) (kill-buffer))))))
  16628. (cond
  16629. (mathml
  16630. (setq mathml
  16631. (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
  16632. (when mathml-file
  16633. (write-region mathml nil mathml-file))
  16634. (when (org-called-interactively-p 'any)
  16635. (message mathml)))
  16636. ((message "LaTeX to MathML conversion failed")
  16637. (message shell-command-output)))
  16638. (delete-file tmp-in-file)
  16639. (when (file-exists-p tmp-out-file)
  16640. (delete-file tmp-out-file))
  16641. mathml))
  16642. (defun org-format-latex-as-mathml (latex-frag latex-frag-type
  16643. prefix &optional dir)
  16644. "Use `org-create-math-formula' but check local cache first."
  16645. (let* ((absprefix (expand-file-name prefix dir))
  16646. (print-length nil) (print-level nil)
  16647. (formula-id (concat
  16648. "formula-"
  16649. (sha1
  16650. (prin1-to-string
  16651. (list latex-frag
  16652. org-latex-to-mathml-convert-command)))))
  16653. (formula-cache (format "%s-%s.mathml" absprefix formula-id))
  16654. (formula-cache-dir (file-name-directory formula-cache)))
  16655. (unless (file-directory-p formula-cache-dir)
  16656. (make-directory formula-cache-dir t))
  16657. (unless (file-exists-p formula-cache)
  16658. (org-create-math-formula latex-frag formula-cache))
  16659. (if (file-exists-p formula-cache)
  16660. ;; Successful conversion. Return the link to MathML file.
  16661. (org-add-props
  16662. (format "[[file:%s]]" (file-relative-name formula-cache dir))
  16663. (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
  16664. 'org-latex-src-embed-type (if latex-frag-type
  16665. 'paragraph 'character)))
  16666. ;; Failed conversion. Return the LaTeX fragment verbatim
  16667. latex-frag)))
  16668. (defun org-create-formula-image (string tofile options buffer &optional type)
  16669. "Create an image from LaTeX source using dvipng or convert.
  16670. This function calls either `org-create-formula-image-with-dvipng'
  16671. or `org-create-formula-image-with-imagemagick' depending on the
  16672. value of `org-latex-create-formula-image-program' or on the value
  16673. of the optional TYPE variable.
  16674. Note: ultimately these two function should be combined as they
  16675. share a good deal of logic."
  16676. (org-check-external-command
  16677. "latex" "needed to convert LaTeX fragments to images")
  16678. (funcall
  16679. (case (or type org-latex-create-formula-image-program)
  16680. ('dvipng
  16681. (org-check-external-command
  16682. "dvipng" "needed to convert LaTeX fragments to images")
  16683. #'org-create-formula-image-with-dvipng)
  16684. ('imagemagick
  16685. (org-check-external-command
  16686. "convert" "you need to install imagemagick")
  16687. #'org-create-formula-image-with-imagemagick)
  16688. (t (error
  16689. "invalid value of `org-latex-create-formula-image-program'")))
  16690. string tofile options buffer))
  16691. (declare-function org-export--get-global-options "ox" (&optional backend))
  16692. (declare-function org-export--get-inbuffer-options "ox" (&optional backend))
  16693. (defun org-create-formula--latex-header ()
  16694. "Return LaTeX header appropriate for previewing a LaTeX snippet."
  16695. (org-latex-guess-inputenc
  16696. (org-splice-latex-header
  16697. org-format-latex-header
  16698. org-latex-default-packages-alist
  16699. org-latex-packages-alist t
  16700. (plist-get
  16701. (org-combine-plists
  16702. (org-export--get-global-options 'latex)
  16703. (org-export--get-inbuffer-options 'latex))
  16704. :latex-header))))
  16705. ;; This function borrows from Ganesh Swami's latex2png.el
  16706. (defun org-create-formula-image-with-dvipng (string tofile options buffer)
  16707. "This calls dvipng."
  16708. (require 'ox-latex)
  16709. (let* ((tmpdir (if (featurep 'xemacs)
  16710. (temp-directory)
  16711. temporary-file-directory))
  16712. (texfilebase (make-temp-name
  16713. (expand-file-name "orgtex" tmpdir)))
  16714. (texfile (concat texfilebase ".tex"))
  16715. (dvifile (concat texfilebase ".dvi"))
  16716. (pngfile (concat texfilebase ".png"))
  16717. (fnh (if (featurep 'xemacs)
  16718. (font-height (face-font 'default))
  16719. (face-attribute 'default :height nil)))
  16720. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  16721. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  16722. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  16723. "Black"))
  16724. (bg (or (plist-get options (if buffer :background :html-background))
  16725. "Transparent")))
  16726. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground))
  16727. (unless (string= fg "Transparent") (setq fg (org-dvipng-color-format fg))))
  16728. (if (eq bg 'default) (setq bg (org-dvipng-color :background))
  16729. (unless (string= bg "Transparent") (setq bg (org-dvipng-color-format bg))))
  16730. (let ((latex-header (org-create-formula--latex-header)))
  16731. (with-temp-file texfile
  16732. (insert latex-header)
  16733. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")))
  16734. (let ((dir default-directory))
  16735. (condition-case nil
  16736. (progn
  16737. (cd tmpdir)
  16738. (call-process "latex" nil nil nil texfile))
  16739. (error nil))
  16740. (cd dir))
  16741. (if (not (file-exists-p dvifile))
  16742. (progn (message "Failed to create dvi file from %s" texfile) nil)
  16743. (condition-case nil
  16744. (if (featurep 'xemacs)
  16745. (call-process "dvipng" nil nil nil
  16746. "-fg" fg "-bg" bg
  16747. "-T" "tight"
  16748. "-o" pngfile
  16749. dvifile)
  16750. (call-process "dvipng" nil nil nil
  16751. "-fg" fg "-bg" bg
  16752. "-D" dpi
  16753. ;;"-x" scale "-y" scale
  16754. "-T" "tight"
  16755. "-o" pngfile
  16756. dvifile))
  16757. (error nil))
  16758. (if (not (file-exists-p pngfile))
  16759. (if org-format-latex-signal-error
  16760. (error "Failed to create png file from %s" texfile)
  16761. (message "Failed to create png file from %s" texfile)
  16762. nil)
  16763. ;; Use the requested file name and clean up
  16764. (copy-file pngfile tofile 'replace)
  16765. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png" ".out") do
  16766. (if (file-exists-p (concat texfilebase e))
  16767. (delete-file (concat texfilebase e))))
  16768. pngfile))))
  16769. (declare-function org-latex-compile "ox-latex" (texfile &optional snippet))
  16770. (defun org-create-formula-image-with-imagemagick (string tofile options buffer)
  16771. "This calls convert, which is included into imagemagick."
  16772. (require 'ox-latex)
  16773. (let* ((tmpdir (if (featurep 'xemacs)
  16774. (temp-directory)
  16775. temporary-file-directory))
  16776. (texfilebase (make-temp-name
  16777. (expand-file-name "orgtex" tmpdir)))
  16778. (texfile (concat texfilebase ".tex"))
  16779. (pdffile (concat texfilebase ".pdf"))
  16780. (pngfile (concat texfilebase ".png"))
  16781. (fnh (if (featurep 'xemacs)
  16782. (font-height (face-font 'default))
  16783. (face-attribute 'default :height nil)))
  16784. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  16785. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  16786. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  16787. "black"))
  16788. (bg (or (plist-get options (if buffer :background :html-background))
  16789. "white")))
  16790. (if (eq fg 'default) (setq fg (org-latex-color :foreground))
  16791. (setq fg (org-latex-color-format fg)))
  16792. (if (eq bg 'default) (setq bg (org-latex-color :background))
  16793. (setq bg (org-latex-color-format
  16794. (if (string= bg "Transparent") "white" bg))))
  16795. (let ((latex-header (org-create-formula--latex-header)))
  16796. (with-temp-file texfile
  16797. (insert latex-header)
  16798. (insert "\n\\begin{document}\n"
  16799. "\\definecolor{fg}{rgb}{" fg "}\n"
  16800. "\\definecolor{bg}{rgb}{" bg "}\n"
  16801. "\n\\pagecolor{bg}\n"
  16802. "\n{\\color{fg}\n"
  16803. string
  16804. "\n}\n"
  16805. "\n\\end{document}\n")))
  16806. (org-latex-compile texfile t)
  16807. (if (not (file-exists-p pdffile))
  16808. (progn (message "Failed to create pdf file from %s" texfile) nil)
  16809. (condition-case nil
  16810. (if (featurep 'xemacs)
  16811. (call-process "convert" nil nil nil
  16812. "-density" "96"
  16813. "-trim"
  16814. "-antialias"
  16815. pdffile
  16816. "-quality" "100"
  16817. ;; "-sharpen" "0x1.0"
  16818. pngfile)
  16819. (call-process "convert" nil nil nil
  16820. "-density" dpi
  16821. "-trim"
  16822. "-antialias"
  16823. pdffile
  16824. "-quality" "100"
  16825. ;; "-sharpen" "0x1.0"
  16826. pngfile))
  16827. (error nil))
  16828. (if (not (file-exists-p pngfile))
  16829. (if org-format-latex-signal-error
  16830. (error "Failed to create png file from %s" texfile)
  16831. (message "Failed to create png file from %s" texfile)
  16832. nil)
  16833. ;; Use the requested file name and clean up
  16834. (copy-file pngfile tofile 'replace)
  16835. (loop for e in '(".pdf" ".tex" ".aux" ".log" ".png") do
  16836. (if (file-exists-p (concat texfilebase e))
  16837. (delete-file (concat texfilebase e))))
  16838. pngfile))))
  16839. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  16840. "Fill a LaTeX header template TPL.
  16841. In the template, the following place holders will be recognized:
  16842. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  16843. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  16844. [PACKAGES] \\usepackage statements for PKG
  16845. [NO-PACKAGES] do not include PKG
  16846. [EXTRA] the string EXTRA
  16847. [NO-EXTRA] do not include EXTRA
  16848. For backward compatibility, if both the positive and the negative place
  16849. holder is missing, the positive one (without the \"NO-\") will be
  16850. assumed to be present at the end of the template.
  16851. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  16852. EXTRA is a string.
  16853. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  16854. (let (rpl (end ""))
  16855. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  16856. (setq rpl (if (or (match-end 1) (not def-pkg))
  16857. "" (org-latex-packages-to-string def-pkg snippets-p t))
  16858. tpl (replace-match rpl t t tpl))
  16859. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  16860. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  16861. (setq rpl (if (or (match-end 1) (not pkg))
  16862. "" (org-latex-packages-to-string pkg snippets-p t))
  16863. tpl (replace-match rpl t t tpl))
  16864. (if pkg (setq end
  16865. (concat end "\n"
  16866. (org-latex-packages-to-string pkg snippets-p)))))
  16867. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  16868. (setq rpl (if (or (match-end 1) (not extra))
  16869. "" (concat extra "\n"))
  16870. tpl (replace-match rpl t t tpl))
  16871. (if (and extra (string-match "\\S-" extra))
  16872. (setq end (concat end "\n" extra))))
  16873. (if (string-match "\\S-" end)
  16874. (concat tpl "\n" end)
  16875. tpl)))
  16876. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  16877. "Turn an alist of packages into a string with the \\usepackage macros."
  16878. (setq pkg (mapconcat (lambda(p)
  16879. (cond
  16880. ((stringp p) p)
  16881. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  16882. (format "%% Package %s omitted" (cadr p)))
  16883. ((equal "" (car p))
  16884. (format "\\usepackage{%s}" (cadr p)))
  16885. (t
  16886. (format "\\usepackage[%s]{%s}"
  16887. (car p) (cadr p)))))
  16888. pkg
  16889. "\n"))
  16890. (if newline (concat pkg "\n") pkg))
  16891. (defun org-dvipng-color (attr)
  16892. "Return a RGB color specification for dvipng."
  16893. (apply 'format "rgb %s %s %s"
  16894. (mapcar 'org-normalize-color
  16895. (if (featurep 'xemacs)
  16896. (color-rgb-components
  16897. (face-property 'default
  16898. (cond ((eq attr :foreground) 'foreground)
  16899. ((eq attr :background) 'background))))
  16900. (color-values (face-attribute 'default attr nil))))))
  16901. (defun org-dvipng-color-format (color-name)
  16902. "Convert COLOR-NAME to a RGB color value for dvipng."
  16903. (apply 'format "rgb %s %s %s"
  16904. (mapcar 'org-normalize-color
  16905. (color-values color-name))))
  16906. (defun org-latex-color (attr)
  16907. "Return a RGB color for the LaTeX color package."
  16908. (apply 'format "%s,%s,%s"
  16909. (mapcar 'org-normalize-color
  16910. (if (featurep 'xemacs)
  16911. (color-rgb-components
  16912. (face-property 'default
  16913. (cond ((eq attr :foreground) 'foreground)
  16914. ((eq attr :background) 'background))))
  16915. (color-values (face-attribute 'default attr nil))))))
  16916. (defun org-latex-color-format (color-name)
  16917. "Convert COLOR-NAME to a RGB color value."
  16918. (apply 'format "%s,%s,%s"
  16919. (mapcar 'org-normalize-color
  16920. (color-values color-name))))
  16921. (defun org-normalize-color (value)
  16922. "Return string to be used as color value for an RGB component."
  16923. (format "%g" (/ value 65535.0)))
  16924. ;; Image display
  16925. (defvar org-inline-image-overlays nil)
  16926. (make-variable-buffer-local 'org-inline-image-overlays)
  16927. (defun org-toggle-inline-images (&optional include-linked)
  16928. "Toggle the display of inline images.
  16929. INCLUDE-LINKED is passed to `org-display-inline-images'."
  16930. (interactive "P")
  16931. (if org-inline-image-overlays
  16932. (progn
  16933. (org-remove-inline-images)
  16934. (message "Inline image display turned off"))
  16935. (org-display-inline-images include-linked)
  16936. (if (and (org-called-interactively-p)
  16937. org-inline-image-overlays)
  16938. (message "%d images displayed inline"
  16939. (length org-inline-image-overlays))
  16940. (message "No images to display inline"))))
  16941. (defun org-redisplay-inline-images ()
  16942. "Refresh the display of inline images."
  16943. (interactive)
  16944. (if (not org-inline-image-overlays)
  16945. (org-toggle-inline-images)
  16946. (org-toggle-inline-images)
  16947. (org-toggle-inline-images)))
  16948. (defun org-display-inline-images (&optional include-linked refresh beg end)
  16949. "Display inline images.
  16950. Normally only links without a description part are inlined, because this
  16951. is how it will work for export. When INCLUDE-LINKED is set, also links
  16952. with a description part will be inlined. This can be nice for a quick
  16953. look at those images, but it does not reflect what exported files will look
  16954. like.
  16955. When REFRESH is set, refresh existing images between BEG and END.
  16956. This will create new image displays only if necessary.
  16957. BEG and END default to the buffer boundaries."
  16958. (interactive "P")
  16959. (unless refresh
  16960. (org-remove-inline-images)
  16961. (if (fboundp 'clear-image-cache) (clear-image-cache)))
  16962. (save-excursion
  16963. (save-restriction
  16964. (widen)
  16965. (setq beg (or beg (point-min)) end (or end (point-max)))
  16966. (goto-char beg)
  16967. (let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([^]\n]+?"
  16968. (substring (org-image-file-name-regexp) 0 -2)
  16969. "\\)\\]" (if include-linked "" "\\]")))
  16970. old file ov img type attrwidth width)
  16971. (while (re-search-forward re end t)
  16972. (setq old (get-char-property-and-overlay (match-beginning 1)
  16973. 'org-image-overlay)
  16974. file (expand-file-name
  16975. (concat (or (match-string 3) "") (match-string 4))))
  16976. (when (image-type-available-p 'imagemagick)
  16977. (setq attrwidth (if (or (listp org-image-actual-width)
  16978. (null org-image-actual-width))
  16979. (save-excursion
  16980. (save-match-data
  16981. (when (re-search-backward
  16982. "#\\+ATTR.*width=\"\\([^\"]+\\)\""
  16983. (save-excursion
  16984. (re-search-backward "^[ \t]*$\\|\\`" nil t)) t)
  16985. (string-to-number (match-string 1))))))
  16986. width (cond ((eq org-image-actual-width t) nil)
  16987. ((null org-image-actual-width) attrwidth)
  16988. ((numberp org-image-actual-width)
  16989. org-image-actual-width)
  16990. ((listp org-image-actual-width)
  16991. (or attrwidth (car org-image-actual-width))))
  16992. type (if width 'imagemagick)))
  16993. (when (file-exists-p file)
  16994. (if (and (car-safe old) refresh)
  16995. (image-refresh (overlay-get (cdr old) 'display))
  16996. (setq img (save-match-data (create-image file type nil :width width)))
  16997. (when img
  16998. (setq ov (make-overlay (match-beginning 0) (match-end 0)))
  16999. (overlay-put ov 'display img)
  17000. (overlay-put ov 'face 'default)
  17001. (overlay-put ov 'org-image-overlay t)
  17002. (overlay-put ov 'modification-hooks
  17003. (list 'org-display-inline-remove-overlay))
  17004. (push ov org-inline-image-overlays)))))))))
  17005. (define-obsolete-function-alias
  17006. 'org-display-inline-modification-hook 'org-display-inline-remove-overlay "24.3")
  17007. (defun org-display-inline-remove-overlay (ov after beg end &optional len)
  17008. "Remove inline-display overlay if a corresponding region is modified."
  17009. (let ((inhibit-modification-hooks t))
  17010. (when (and ov after)
  17011. (delete ov org-inline-image-overlays)
  17012. (delete-overlay ov))))
  17013. (defun org-remove-inline-images ()
  17014. "Remove inline display of images."
  17015. (interactive)
  17016. (mapc 'delete-overlay org-inline-image-overlays)
  17017. (setq org-inline-image-overlays nil))
  17018. ;;;; Key bindings
  17019. ;; Outline functions from `outline-mode-prefix-map'
  17020. ;; that can be remapped in Org:
  17021. (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
  17022. (define-key org-mode-map [remap show-subtree] 'org-show-subtree)
  17023. (define-key org-mode-map [remap outline-forward-same-level]
  17024. 'org-forward-heading-same-level)
  17025. (define-key org-mode-map [remap outline-backward-same-level]
  17026. 'org-backward-heading-same-level)
  17027. (define-key org-mode-map [remap show-branches]
  17028. 'org-kill-note-or-show-branches)
  17029. (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
  17030. (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
  17031. (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
  17032. ;; Outline functions from `outline-mode-prefix-map' that can not
  17033. ;; be remapped in Org:
  17034. ;;
  17035. ;; - the column "key binding" shows whether the Outline function is still
  17036. ;; available in Org mode on the same key that it has been bound to in
  17037. ;; Outline mode:
  17038. ;; - "overridden": key used for a different functionality in Org mode
  17039. ;; - else: key still bound to the same Outline function in Org mode
  17040. ;;
  17041. ;; | Outline function | key binding | Org replacement |
  17042. ;; |------------------------------------+-------------+-----------------------|
  17043. ;; | `outline-next-visible-heading' | `C-c C-n' | still same function |
  17044. ;; | `outline-previous-visible-heading' | `C-c C-p' | still same function |
  17045. ;; | `outline-up-heading' | `C-c C-u' | still same function |
  17046. ;; | `outline-move-subtree-up' | overridden | better: org-shiftup |
  17047. ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
  17048. ;; | `show-entry' | overridden | no replacement |
  17049. ;; | `show-children' | `C-c C-i' | visibility cycling |
  17050. ;; | `show-branches' | `C-c C-k' | still same function |
  17051. ;; | `show-subtree' | overridden | visibility cycling |
  17052. ;; | `show-all' | overridden | no replacement |
  17053. ;; | `hide-subtree' | overridden | visibility cycling |
  17054. ;; | `hide-body' | overridden | no replacement |
  17055. ;; | `hide-entry' | overridden | visibility cycling |
  17056. ;; | `hide-leaves' | overridden | no replacement |
  17057. ;; | `hide-sublevels' | overridden | no replacement |
  17058. ;; | `hide-other' | overridden | no replacement |
  17059. ;; Make `C-c C-x' a prefix key
  17060. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  17061. ;; TAB key with modifiers
  17062. (org-defkey org-mode-map "\C-i" 'org-cycle)
  17063. (org-defkey org-mode-map [(tab)] 'org-cycle)
  17064. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  17065. (org-defkey org-mode-map "\M-\t" 'pcomplete)
  17066. ;; The following line is necessary under Suse GNU/Linux
  17067. (unless (featurep 'xemacs)
  17068. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  17069. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  17070. (define-key org-mode-map [backtab] 'org-shifttab)
  17071. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  17072. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  17073. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  17074. ;; Cursor keys with modifiers
  17075. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  17076. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  17077. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  17078. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  17079. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  17080. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  17081. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  17082. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  17083. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  17084. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  17085. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  17086. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  17087. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  17088. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  17089. (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
  17090. (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
  17091. ;; Babel keys
  17092. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  17093. (mapc (lambda (pair)
  17094. (define-key org-babel-map (car pair) (cdr pair)))
  17095. org-babel-key-bindings)
  17096. ;;; Extra keys for tty access.
  17097. ;; We only set them when really needed because otherwise the
  17098. ;; menus don't show the simple keys
  17099. (when (or org-use-extra-keys
  17100. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  17101. (not window-system))
  17102. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  17103. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  17104. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  17105. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  17106. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  17107. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  17108. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  17109. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  17110. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  17111. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  17112. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  17113. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  17114. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  17115. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  17116. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  17117. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  17118. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  17119. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  17120. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  17121. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  17122. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  17123. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  17124. (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
  17125. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  17126. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  17127. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  17128. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  17129. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  17130. ;; All the other keys
  17131. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  17132. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  17133. (if (boundp 'narrow-map)
  17134. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  17135. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  17136. (if (boundp 'narrow-map)
  17137. (org-defkey narrow-map "b" 'org-narrow-to-block)
  17138. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  17139. (if (boundp 'narrow-map)
  17140. (org-defkey narrow-map "e" 'org-narrow-to-element)
  17141. (org-defkey org-mode-map "\C-xne" 'org-narrow-to-element))
  17142. (org-defkey org-mode-map "\C-\M-t" 'org-transpose-element)
  17143. (org-defkey org-mode-map "\M-}" 'org-forward-element)
  17144. (org-defkey org-mode-map "\M-{" 'org-backward-element)
  17145. (org-defkey org-mode-map "\C-c\C-^" 'org-up-element)
  17146. (org-defkey org-mode-map "\C-c\C-_" 'org-down-element)
  17147. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-heading-same-level)
  17148. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-heading-same-level)
  17149. (org-defkey org-mode-map "\C-c\M-f" 'org-next-block)
  17150. (org-defkey org-mode-map "\C-c\M-b" 'org-previous-block)
  17151. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  17152. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  17153. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  17154. (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
  17155. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  17156. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  17157. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  17158. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  17159. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  17160. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  17161. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  17162. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  17163. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  17164. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  17165. (org-defkey org-mode-map "\C-c\M-w" 'org-copy)
  17166. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  17167. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  17168. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  17169. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  17170. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  17171. (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
  17172. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  17173. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  17174. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  17175. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  17176. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  17177. (org-defkey org-mode-map "\C-c\C-\M-l" 'org-insert-all-links)
  17178. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  17179. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  17180. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  17181. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  17182. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  17183. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  17184. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  17185. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  17186. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  17187. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  17188. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  17189. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  17190. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  17191. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  17192. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  17193. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  17194. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  17195. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  17196. (org-defkey org-mode-map "\C-c^" 'org-sort)
  17197. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  17198. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  17199. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  17200. (org-defkey org-mode-map "\C-m" 'org-return)
  17201. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  17202. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  17203. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  17204. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  17205. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  17206. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  17207. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  17208. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  17209. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  17210. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  17211. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  17212. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  17213. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  17214. (org-defkey org-mode-map "\C-c\C-e" 'org-export-dispatch)
  17215. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  17216. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  17217. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  17218. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  17219. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  17220. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  17221. (org-defkey org-mode-map "\M-h" 'org-mark-element)
  17222. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  17223. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  17224. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  17225. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  17226. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  17227. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  17228. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  17229. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-in-last)
  17230. (org-defkey org-mode-map "\C-c\C-x\C-z" 'org-resolve-clocks)
  17231. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  17232. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  17233. (org-defkey org-mode-map "\C-c\C-x\C-q" 'org-clock-cancel)
  17234. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  17235. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  17236. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  17237. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  17238. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  17239. (org-defkey org-mode-map "\C-c\C-x\C-\M-v" 'org-redisplay-inline-images)
  17240. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  17241. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  17242. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  17243. (org-defkey org-mode-map "\C-c\C-xP" 'org-set-property-and-value)
  17244. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  17245. (org-defkey org-mode-map "\C-c\C-xE" 'org-inc-effort)
  17246. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  17247. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  17248. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  17249. (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
  17250. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  17251. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  17252. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  17253. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  17254. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  17255. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  17256. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  17257. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  17258. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  17259. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  17260. (when (featurep 'xemacs)
  17261. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  17262. (defconst org-speed-commands-default
  17263. '(
  17264. ("Outline Navigation")
  17265. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  17266. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  17267. ("f" . (org-speed-move-safe 'org-forward-heading-same-level))
  17268. ("b" . (org-speed-move-safe 'org-backward-heading-same-level))
  17269. ("F" . org-next-block)
  17270. ("B" . org-previous-block)
  17271. ("u" . (org-speed-move-safe 'outline-up-heading))
  17272. ("j" . org-goto)
  17273. ("g" . (org-refile t))
  17274. ("Outline Visibility")
  17275. ("c" . org-cycle)
  17276. ("C" . org-shifttab)
  17277. (" " . org-display-outline-path)
  17278. ("=" . org-columns)
  17279. ("Outline Structure Editing")
  17280. ("U" . org-shiftmetaup)
  17281. ("D" . org-shiftmetadown)
  17282. ("r" . org-metaright)
  17283. ("l" . org-metaleft)
  17284. ("R" . org-shiftmetaright)
  17285. ("L" . org-shiftmetaleft)
  17286. ("i" . (progn (forward-char 1) (call-interactively
  17287. 'org-insert-heading-respect-content)))
  17288. ("^" . org-sort)
  17289. ("w" . org-refile)
  17290. ("a" . org-archive-subtree-default-with-confirmation)
  17291. ("@" . org-mark-subtree)
  17292. ("#" . org-toggle-comment)
  17293. ("Clock Commands")
  17294. ("I" . org-clock-in)
  17295. ("O" . org-clock-out)
  17296. ("Meta Data Editing")
  17297. ("t" . org-todo)
  17298. ("," . (org-priority))
  17299. ("0" . (org-priority ?\ ))
  17300. ("1" . (org-priority ?A))
  17301. ("2" . (org-priority ?B))
  17302. ("3" . (org-priority ?C))
  17303. (":" . org-set-tags-command)
  17304. ("e" . org-set-effort)
  17305. ("E" . org-inc-effort)
  17306. ("W" . (lambda(m) (interactive "sMinutes before warning: ")
  17307. (org-entry-put (point) "APPT_WARNTIME" m)))
  17308. ("Agenda Views etc")
  17309. ("v" . org-agenda)
  17310. ("/" . org-sparse-tree)
  17311. ("Misc")
  17312. ("o" . org-open-at-point)
  17313. ("?" . org-speed-command-help)
  17314. ("<" . (org-agenda-set-restriction-lock 'subtree))
  17315. (">" . (org-agenda-remove-restriction-lock))
  17316. )
  17317. "The default speed commands.")
  17318. (defun org-print-speed-command (e)
  17319. (if (> (length (car e)) 1)
  17320. (progn
  17321. (princ "\n")
  17322. (princ (car e))
  17323. (princ "\n")
  17324. (princ (make-string (length (car e)) ?-))
  17325. (princ "\n"))
  17326. (princ (car e))
  17327. (princ " ")
  17328. (if (symbolp (cdr e))
  17329. (princ (symbol-name (cdr e)))
  17330. (prin1 (cdr e)))
  17331. (princ "\n")))
  17332. (defun org-speed-command-help ()
  17333. "Show the available speed commands."
  17334. (interactive)
  17335. (if (not org-use-speed-commands)
  17336. (error "Speed commands are not activated, customize `org-use-speed-commands'")
  17337. (with-output-to-temp-buffer "*Help*"
  17338. (princ "User-defined Speed commands\n===========================\n")
  17339. (mapc 'org-print-speed-command org-speed-commands-user)
  17340. (princ "\n")
  17341. (princ "Built-in Speed commands\n=======================\n")
  17342. (mapc 'org-print-speed-command org-speed-commands-default))
  17343. (with-current-buffer "*Help*"
  17344. (setq truncate-lines t))))
  17345. (defun org-speed-move-safe (cmd)
  17346. "Execute CMD, but make sure that the cursor always ends up in a headline.
  17347. If not, return to the original position and throw an error."
  17348. (interactive)
  17349. (let ((pos (point)))
  17350. (call-interactively cmd)
  17351. (unless (and (bolp) (org-at-heading-p))
  17352. (goto-char pos)
  17353. (error "Boundary reached while executing %s" cmd))))
  17354. (defvar org-self-insert-command-undo-counter 0)
  17355. (defvar org-table-auto-blank-field) ; defined in org-table.el
  17356. (defvar org-speed-command nil)
  17357. (define-obsolete-function-alias
  17358. 'org-speed-command-default-hook 'org-speed-command-activate "24.3")
  17359. (defun org-speed-command-activate (keys)
  17360. "Hook for activating single-letter speed commands.
  17361. `org-speed-commands-default' specifies a minimal command set.
  17362. Use `org-speed-commands-user' for further customization."
  17363. (when (or (and (bolp) (looking-at org-outline-regexp))
  17364. (and (functionp org-use-speed-commands)
  17365. (funcall org-use-speed-commands)))
  17366. (cdr (assoc keys (append org-speed-commands-user
  17367. org-speed-commands-default)))))
  17368. (define-obsolete-function-alias
  17369. 'org-babel-speed-command-hook 'org-babel-speed-command-activate "24.3")
  17370. (defun org-babel-speed-command-activate (keys)
  17371. "Hook for activating single-letter code block commands."
  17372. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  17373. (cdr (assoc keys org-babel-key-bindings))))
  17374. (defcustom org-speed-command-hook
  17375. '(org-speed-command-default-hook org-babel-speed-command-hook)
  17376. "Hook for activating speed commands at strategic locations.
  17377. Hook functions are called in sequence until a valid handler is
  17378. found.
  17379. Each hook takes a single argument, a user-pressed command key
  17380. which is also a `self-insert-command' from the global map.
  17381. Within the hook, examine the cursor position and the command key
  17382. and return nil or a valid handler as appropriate. Handler could
  17383. be one of an interactive command, a function, or a form.
  17384. Set `org-use-speed-commands' to non-nil value to enable this
  17385. hook. The default setting is `org-speed-command-activate'."
  17386. :group 'org-structure
  17387. :version "24.1"
  17388. :type 'hook)
  17389. (defun org-self-insert-command (N)
  17390. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  17391. If the cursor is in a table looking at whitespace, the whitespace is
  17392. overwritten, and the table is not marked as requiring realignment."
  17393. (interactive "p")
  17394. (org-check-before-invisible-edit 'insert)
  17395. (cond
  17396. ((and org-use-speed-commands
  17397. (setq org-speed-command
  17398. (run-hook-with-args-until-success
  17399. 'org-speed-command-hook (this-command-keys))))
  17400. (cond
  17401. ((commandp org-speed-command)
  17402. (setq this-command org-speed-command)
  17403. (call-interactively org-speed-command))
  17404. ((functionp org-speed-command)
  17405. (funcall org-speed-command))
  17406. ((and org-speed-command (listp org-speed-command))
  17407. (eval org-speed-command))
  17408. (t (let (org-use-speed-commands)
  17409. (call-interactively 'org-self-insert-command)))))
  17410. ((and
  17411. (org-table-p)
  17412. (progn
  17413. ;; check if we blank the field, and if that triggers align
  17414. (and (featurep 'org-table) org-table-auto-blank-field
  17415. (member last-command
  17416. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  17417. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  17418. ;; got extra space, this field does not determine column width
  17419. (let (org-table-may-need-update) (org-table-blank-field))
  17420. ;; no extra space, this field may determine column width
  17421. (org-table-blank-field)))
  17422. t)
  17423. (eq N 1)
  17424. (looking-at "[^|\n]* |"))
  17425. (let (org-table-may-need-update)
  17426. (goto-char (1- (match-end 0)))
  17427. (backward-delete-char 1)
  17428. (goto-char (match-beginning 0))
  17429. (self-insert-command N)))
  17430. (t
  17431. (setq org-table-may-need-update t)
  17432. (self-insert-command N)
  17433. (org-fix-tags-on-the-fly)
  17434. (if org-self-insert-cluster-for-undo
  17435. (if (not (eq last-command 'org-self-insert-command))
  17436. (setq org-self-insert-command-undo-counter 1)
  17437. (if (>= org-self-insert-command-undo-counter 20)
  17438. (setq org-self-insert-command-undo-counter 1)
  17439. (and (> org-self-insert-command-undo-counter 0)
  17440. buffer-undo-list (listp buffer-undo-list)
  17441. (not (cadr buffer-undo-list)) ; remove nil entry
  17442. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  17443. (setq org-self-insert-command-undo-counter
  17444. (1+ org-self-insert-command-undo-counter))))))))
  17445. (defun org-check-before-invisible-edit (kind)
  17446. "Check is editing if kind KIND would be dangerous with invisible text around.
  17447. The detailed reaction depends on the user option `org-catch-invisible-edits'."
  17448. ;; First, try to get out of here as quickly as possible, to reduce overhead
  17449. (if (and org-catch-invisible-edits
  17450. (or (not (boundp 'visible-mode)) (not visible-mode))
  17451. (or (get-char-property (point) 'invisible)
  17452. (get-char-property (max (point-min) (1- (point))) 'invisible)))
  17453. ;; OK, we need to take a closer look
  17454. (let* ((invisible-at-point (get-char-property (point) 'invisible))
  17455. (invisible-before-point (if (bobp) nil (get-char-property
  17456. (1- (point)) 'invisible)))
  17457. (border-and-ok-direction
  17458. (or
  17459. ;; Check if we are acting predictably before invisible text
  17460. (and invisible-at-point (not invisible-before-point)
  17461. (memq kind '(insert delete-backward)))
  17462. ;; Check if we are acting predictably after invisible text
  17463. ;; This works not well, and I have turned it off. It seems
  17464. ;; better to always show and stop after invisible text.
  17465. ;; (and (not invisible-at-point) invisible-before-point
  17466. ;; (memq kind '(insert delete)))
  17467. )))
  17468. (when (or (memq invisible-at-point '(outline org-hide-block t))
  17469. (memq invisible-before-point '(outline org-hide-block t)))
  17470. (if (eq org-catch-invisible-edits 'error)
  17471. (error "Editing in invisible areas is prohibited - make visible first"))
  17472. (if (and org-custom-properties-overlays
  17473. (y-or-n-p "Display invisible properties in this buffer? "))
  17474. (org-toggle-custom-properties-visibility)
  17475. ;; Make the area visible
  17476. (save-excursion
  17477. (if invisible-before-point
  17478. (goto-char (previous-single-char-property-change
  17479. (point) 'invisible)))
  17480. (org-cycle))
  17481. (cond
  17482. ((eq org-catch-invisible-edits 'show)
  17483. ;; That's it, we do the edit after showing
  17484. (message
  17485. "Unfolding invisible region around point before editing")
  17486. (sit-for 1))
  17487. ((and (eq org-catch-invisible-edits 'smart)
  17488. border-and-ok-direction)
  17489. (message "Unfolding invisible region around point before editing"))
  17490. (t
  17491. ;; Don't do the edit, make the user repeat it in full visibility
  17492. (error "Edit in invisible region aborted, repeat to confirm with text visible"))))))))
  17493. (defun org-fix-tags-on-the-fly ()
  17494. (when (and (equal (char-after (point-at-bol)) ?*)
  17495. (org-at-heading-p))
  17496. (org-align-tags-here org-tags-column)))
  17497. (defun org-delete-backward-char (N)
  17498. "Like `delete-backward-char', insert whitespace at field end in tables.
  17499. When deleting backwards, in tables this function will insert whitespace in
  17500. front of the next \"|\" separator, to keep the table aligned. The table will
  17501. still be marked for re-alignment if the field did fill the entire column,
  17502. because, in this case the deletion might narrow the column."
  17503. (interactive "p")
  17504. (save-match-data
  17505. (org-check-before-invisible-edit 'delete-backward)
  17506. (if (and (org-table-p)
  17507. (eq N 1)
  17508. (string-match "|" (buffer-substring (point-at-bol) (point)))
  17509. (looking-at ".*?|"))
  17510. (let ((pos (point))
  17511. (noalign (looking-at "[^|\n\r]* |"))
  17512. (c org-table-may-need-update))
  17513. (backward-delete-char N)
  17514. (if (not overwrite-mode)
  17515. (progn
  17516. (skip-chars-forward "^|")
  17517. (insert " ")
  17518. (goto-char (1- pos))))
  17519. ;; noalign: if there were two spaces at the end, this field
  17520. ;; does not determine the width of the column.
  17521. (if noalign (setq org-table-may-need-update c)))
  17522. (backward-delete-char N)
  17523. (org-fix-tags-on-the-fly))))
  17524. (defun org-delete-char (N)
  17525. "Like `delete-char', but insert whitespace at field end in tables.
  17526. When deleting characters, in tables this function will insert whitespace in
  17527. front of the next \"|\" separator, to keep the table aligned. The table will
  17528. still be marked for re-alignment if the field did fill the entire column,
  17529. because, in this case the deletion might narrow the column."
  17530. (interactive "p")
  17531. (save-match-data
  17532. (org-check-before-invisible-edit 'delete)
  17533. (if (and (org-table-p)
  17534. (not (bolp))
  17535. (not (= (char-after) ?|))
  17536. (eq N 1))
  17537. (if (looking-at ".*?|")
  17538. (let ((pos (point))
  17539. (noalign (looking-at "[^|\n\r]* |"))
  17540. (c org-table-may-need-update))
  17541. (replace-match (concat
  17542. (substring (match-string 0) 1 -1)
  17543. " |"))
  17544. (goto-char pos)
  17545. ;; noalign: if there were two spaces at the end, this field
  17546. ;; does not determine the width of the column.
  17547. (if noalign (setq org-table-may-need-update c)))
  17548. (delete-char N))
  17549. (delete-char N)
  17550. (org-fix-tags-on-the-fly))))
  17551. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  17552. (put 'org-self-insert-command 'delete-selection t)
  17553. (put 'orgtbl-self-insert-command 'delete-selection t)
  17554. (put 'org-delete-char 'delete-selection 'supersede)
  17555. (put 'org-delete-backward-char 'delete-selection 'supersede)
  17556. (put 'org-yank 'delete-selection 'yank)
  17557. ;; Make `flyspell-mode' delay after some commands
  17558. (put 'org-self-insert-command 'flyspell-delayed t)
  17559. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  17560. (put 'org-delete-char 'flyspell-delayed t)
  17561. (put 'org-delete-backward-char 'flyspell-delayed t)
  17562. ;; Make pabbrev-mode expand after org-mode commands
  17563. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  17564. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  17565. ;; How to do this: Measure non-white length of current string
  17566. ;; If equal to column width, we should realign.
  17567. (defun org-remap (map &rest commands)
  17568. "In MAP, remap the functions given in COMMANDS.
  17569. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  17570. (let (new old)
  17571. (while commands
  17572. (setq old (pop commands) new (pop commands))
  17573. (if (fboundp 'command-remapping)
  17574. (org-defkey map (vector 'remap old) new)
  17575. (substitute-key-definition old new map global-map)))))
  17576. (defun org-transpose-words ()
  17577. "Transpose words for Org.
  17578. This uses the `org-mode-transpose-word-syntax-table' syntax
  17579. table, which interprets characters in `org-emphasis-alist' as
  17580. word constituants."
  17581. (interactive)
  17582. (with-syntax-table org-mode-transpose-word-syntax-table
  17583. (call-interactively 'transpose-words)))
  17584. (org-remap org-mode-map 'transpose-words 'org-transpose-words)
  17585. (when (eq org-enable-table-editor 'optimized)
  17586. ;; If the user wants maximum table support, we need to hijack
  17587. ;; some standard editing functions
  17588. (org-remap org-mode-map
  17589. 'self-insert-command 'org-self-insert-command
  17590. 'delete-char 'org-delete-char
  17591. 'delete-backward-char 'org-delete-backward-char)
  17592. (org-defkey org-mode-map "|" 'org-force-self-insert))
  17593. (defvar org-ctrl-c-ctrl-c-hook nil
  17594. "Hook for functions attaching themselves to `C-c C-c'.
  17595. This can be used to add additional functionality to the C-c C-c
  17596. key which executes context-dependent commands. This hook is run
  17597. before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
  17598. run after the last test.
  17599. Each function will be called with no arguments. The function
  17600. must check if the context is appropriate for it to act. If yes,
  17601. it should do its thing and then return a non-nil value. If the
  17602. context is wrong, just do nothing and return nil.")
  17603. (defvar org-ctrl-c-ctrl-c-final-hook nil
  17604. "Hook for functions attaching themselves to `C-c C-c'.
  17605. This can be used to add additional functionality to the C-c C-c
  17606. key which executes context-dependent commands. This hook is run
  17607. after any other test, while `org-ctrl-c-ctrl-c-hook' is run
  17608. before the first test.
  17609. Each function will be called with no arguments. The function
  17610. must check if the context is appropriate for it to act. If yes,
  17611. it should do its thing and then return a non-nil value. If the
  17612. context is wrong, just do nothing and return nil.")
  17613. (defvar org-tab-first-hook nil
  17614. "Hook for functions to attach themselves to TAB.
  17615. See `org-ctrl-c-ctrl-c-hook' for more information.
  17616. This hook runs as the first action when TAB is pressed, even before
  17617. `org-cycle' messes around with the `outline-regexp' to cater for
  17618. inline tasks and plain list item folding.
  17619. If any function in this hook returns t, any other actions that
  17620. would have been caused by TAB (such as table field motion or visibility
  17621. cycling) will not occur.")
  17622. (defvar org-tab-after-check-for-table-hook nil
  17623. "Hook for functions to attach themselves to TAB.
  17624. See `org-ctrl-c-ctrl-c-hook' for more information.
  17625. This hook runs after it has been established that the cursor is not in a
  17626. table, but before checking if the cursor is in a headline or if global cycling
  17627. should be done.
  17628. If any function in this hook returns t, not other actions like visibility
  17629. cycling will be done.")
  17630. (defvar org-tab-after-check-for-cycling-hook nil
  17631. "Hook for functions to attach themselves to TAB.
  17632. See `org-ctrl-c-ctrl-c-hook' for more information.
  17633. This hook runs after it has been established that not table field motion and
  17634. not visibility should be done because of current context. This is probably
  17635. the place where a package like yasnippets can hook in.")
  17636. (defvar org-tab-before-tab-emulation-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 every other options for TAB have been exhausted, but
  17640. before indentation and \t insertion takes place.")
  17641. (defvar org-metaleft-hook nil
  17642. "Hook for functions attaching themselves to `M-left'.
  17643. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17644. (defvar org-metaright-hook nil
  17645. "Hook for functions attaching themselves to `M-right'.
  17646. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17647. (defvar org-metaup-hook nil
  17648. "Hook for functions attaching themselves to `M-up'.
  17649. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17650. (defvar org-metadown-hook nil
  17651. "Hook for functions attaching themselves to `M-down'.
  17652. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17653. (defvar org-shiftmetaleft-hook nil
  17654. "Hook for functions attaching themselves to `M-S-left'.
  17655. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17656. (defvar org-shiftmetaright-hook nil
  17657. "Hook for functions attaching themselves to `M-S-right'.
  17658. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17659. (defvar org-shiftmetaup-hook nil
  17660. "Hook for functions attaching themselves to `M-S-up'.
  17661. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17662. (defvar org-shiftmetadown-hook nil
  17663. "Hook for functions attaching themselves to `M-S-down'.
  17664. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17665. (defvar org-metareturn-hook nil
  17666. "Hook for functions attaching themselves to `M-RET'.
  17667. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17668. (defvar org-shiftup-hook nil
  17669. "Hook for functions attaching themselves to `S-up'.
  17670. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17671. (defvar org-shiftup-final-hook nil
  17672. "Hook for functions attaching themselves to `S-up'.
  17673. This one runs after all other options except shift-select have been excluded.
  17674. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17675. (defvar org-shiftdown-hook nil
  17676. "Hook for functions attaching themselves to `S-down'.
  17677. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17678. (defvar org-shiftdown-final-hook nil
  17679. "Hook for functions attaching themselves to `S-down'.
  17680. This one runs after all other options except shift-select have been excluded.
  17681. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17682. (defvar org-shiftleft-hook nil
  17683. "Hook for functions attaching themselves to `S-left'.
  17684. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17685. (defvar org-shiftleft-final-hook nil
  17686. "Hook for functions attaching themselves to `S-left'.
  17687. This one runs after all other options except shift-select have been excluded.
  17688. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17689. (defvar org-shiftright-hook nil
  17690. "Hook for functions attaching themselves to `S-right'.
  17691. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17692. (defvar org-shiftright-final-hook nil
  17693. "Hook for functions attaching themselves to `S-right'.
  17694. This one runs after all other options except shift-select have been excluded.
  17695. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17696. (defun org-modifier-cursor-error ()
  17697. "Throw an error, a modified cursor command was applied in wrong context."
  17698. (error "This command is active in special context like tables, headlines or items"))
  17699. (defun org-shiftselect-error ()
  17700. "Throw an error because Shift-Cursor command was applied in wrong context."
  17701. (if (and (boundp 'shift-select-mode) shift-select-mode)
  17702. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  17703. (error "This command works only in special context like headlines or timestamps")))
  17704. (defun org-call-for-shift-select (cmd)
  17705. (let ((this-command-keys-shift-translated t))
  17706. (call-interactively cmd)))
  17707. (defun org-shifttab (&optional arg)
  17708. "Global visibility cycling or move to previous table field.
  17709. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  17710. on context.
  17711. See the individual commands for more information."
  17712. (interactive "P")
  17713. (cond
  17714. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  17715. ((integerp arg)
  17716. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  17717. (message "Content view to level: %d" arg)
  17718. (org-content (prefix-numeric-value arg2))
  17719. (setq org-cycle-global-status 'overview)))
  17720. (t (call-interactively 'org-global-cycle))))
  17721. (defun org-shiftmetaleft ()
  17722. "Promote subtree or delete table column.
  17723. Calls `org-promote-subtree', `org-outdent-item-tree', or
  17724. `org-table-delete-column', depending on context. See the
  17725. individual commands for more information."
  17726. (interactive)
  17727. (cond
  17728. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  17729. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  17730. ((org-at-heading-p) (call-interactively 'org-promote-subtree))
  17731. ((if (not (org-region-active-p)) (org-at-item-p)
  17732. (save-excursion (goto-char (region-beginning))
  17733. (org-at-item-p)))
  17734. (call-interactively 'org-outdent-item-tree))
  17735. (t (org-modifier-cursor-error))))
  17736. (defun org-shiftmetaright ()
  17737. "Demote subtree or insert table column.
  17738. Calls `org-demote-subtree', `org-indent-item-tree', or
  17739. `org-table-insert-column', depending on context. See the
  17740. individual commands for more information."
  17741. (interactive)
  17742. (cond
  17743. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  17744. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  17745. ((org-at-heading-p) (call-interactively 'org-demote-subtree))
  17746. ((if (not (org-region-active-p)) (org-at-item-p)
  17747. (save-excursion (goto-char (region-beginning))
  17748. (org-at-item-p)))
  17749. (call-interactively 'org-indent-item-tree))
  17750. (t (org-modifier-cursor-error))))
  17751. (defun org-shiftmetaup (&optional arg)
  17752. "Move subtree up or kill table row.
  17753. Calls `org-move-subtree-up' or `org-table-kill-row' or
  17754. `org-move-item-up' or `org-timestamp-up', depending on context.
  17755. See the individual commands for more information."
  17756. (interactive "P")
  17757. (cond
  17758. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  17759. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  17760. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  17761. ((org-at-item-p) (call-interactively 'org-move-item-up))
  17762. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  17763. (call-interactively 'org-timestamp-up)))
  17764. (t (call-interactively 'org-drag-line-backward))))
  17765. (defun org-shiftmetadown (&optional arg)
  17766. "Move subtree down or insert table row.
  17767. Calls `org-move-subtree-down' or `org-table-insert-row' or
  17768. `org-move-item-down' or `org-timestamp-up', depending on context.
  17769. See the individual commands for more information."
  17770. (interactive "P")
  17771. (cond
  17772. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  17773. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  17774. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  17775. ((org-at-item-p) (call-interactively 'org-move-item-down))
  17776. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  17777. (call-interactively 'org-timestamp-down)))
  17778. (t (call-interactively 'org-drag-line-forward))))
  17779. (defsubst org-hidden-tree-error ()
  17780. (error
  17781. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  17782. (defun org-metaleft (&optional arg)
  17783. "Promote heading or move table column to left.
  17784. Calls `org-do-promote' or `org-table-move-column', depending on context.
  17785. With no specific context, calls the Emacs default `backward-word'.
  17786. See the individual commands for more information."
  17787. (interactive "P")
  17788. (cond
  17789. ((run-hook-with-args-until-success 'org-metaleft-hook))
  17790. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  17791. ((org-with-limited-levels
  17792. (or (org-at-heading-p)
  17793. (and (org-region-active-p)
  17794. (save-excursion
  17795. (goto-char (region-beginning))
  17796. (org-at-heading-p)))))
  17797. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  17798. (call-interactively 'org-do-promote))
  17799. ;; At an inline task.
  17800. ((org-at-heading-p)
  17801. (call-interactively 'org-inlinetask-promote))
  17802. ((or (org-at-item-p)
  17803. (and (org-region-active-p)
  17804. (save-excursion
  17805. (goto-char (region-beginning))
  17806. (org-at-item-p))))
  17807. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  17808. (call-interactively 'org-outdent-item))
  17809. (t (call-interactively 'backward-word))))
  17810. (defun org-metaright (&optional arg)
  17811. "Demote a subtree, a list item or move table column to right.
  17812. In front of a drawer or a block keyword, indent it correctly.
  17813. With no specific context, calls the Emacs default `forward-word'.
  17814. See the individual commands for more information."
  17815. (interactive "P")
  17816. (cond
  17817. ((run-hook-with-args-until-success 'org-metaright-hook))
  17818. ((org-at-table-p) (call-interactively 'org-table-move-column))
  17819. ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
  17820. ((org-at-block-p) (call-interactively 'org-indent-block))
  17821. ((org-with-limited-levels
  17822. (or (org-at-heading-p)
  17823. (and (org-region-active-p)
  17824. (save-excursion
  17825. (goto-char (region-beginning))
  17826. (org-at-heading-p)))))
  17827. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  17828. (call-interactively 'org-do-demote))
  17829. ;; At an inline task.
  17830. ((org-at-heading-p)
  17831. (call-interactively 'org-inlinetask-demote))
  17832. ((or (org-at-item-p)
  17833. (and (org-region-active-p)
  17834. (save-excursion
  17835. (goto-char (region-beginning))
  17836. (org-at-item-p))))
  17837. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  17838. (call-interactively 'org-indent-item))
  17839. (t (call-interactively 'forward-word))))
  17840. (defun org-check-for-hidden (what)
  17841. "Check if there are hidden headlines/items in the current visual line.
  17842. WHAT can be either `headlines' or `items'. If the current line is
  17843. an outline or item heading and it has a folded subtree below it,
  17844. this function returns t, nil otherwise."
  17845. (let ((re (cond
  17846. ((eq what 'headlines) org-outline-regexp-bol)
  17847. ((eq what 'items) (org-item-beginning-re))
  17848. (t (error "This should not happen"))))
  17849. beg end)
  17850. (save-excursion
  17851. (catch 'exit
  17852. (unless (org-region-active-p)
  17853. (setq beg (point-at-bol))
  17854. (beginning-of-line 2)
  17855. (while (and (not (eobp)) ;; this is like `next-line'
  17856. (get-char-property (1- (point)) 'invisible))
  17857. (beginning-of-line 2))
  17858. (setq end (point))
  17859. (goto-char beg)
  17860. (goto-char (point-at-eol))
  17861. (setq end (max end (point)))
  17862. (while (re-search-forward re end t)
  17863. (if (get-char-property (match-beginning 0) 'invisible)
  17864. (throw 'exit t))))
  17865. nil))))
  17866. (defun org-metaup (&optional arg)
  17867. "Move subtree up or move table row up.
  17868. Calls `org-move-subtree-up' or `org-table-move-row' or
  17869. `org-move-item-up', depending on context. See the individual commands
  17870. for more information."
  17871. (interactive "P")
  17872. (cond
  17873. ((run-hook-with-args-until-success 'org-metaup-hook))
  17874. ((org-region-active-p)
  17875. (let* ((a (min (region-beginning) (region-end)))
  17876. (b (1- (max (region-beginning) (region-end))))
  17877. (c (save-excursion (goto-char a)
  17878. (move-beginning-of-line 0)))
  17879. (d (save-excursion (goto-char a)
  17880. (move-end-of-line 0) (point))))
  17881. (transpose-regions a b c d)
  17882. (goto-char c)))
  17883. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  17884. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  17885. ((org-at-item-p) (call-interactively 'org-move-item-up))
  17886. (t (org-drag-element-backward))))
  17887. (defun org-metadown (&optional arg)
  17888. "Move subtree down or move table row down.
  17889. Calls `org-move-subtree-down' or `org-table-move-row' or
  17890. `org-move-item-down', depending on context. See the individual
  17891. commands for more information."
  17892. (interactive "P")
  17893. (cond
  17894. ((run-hook-with-args-until-success 'org-metadown-hook))
  17895. ((org-region-active-p)
  17896. (let* ((a (min (region-beginning) (region-end)))
  17897. (b (max (region-beginning) (region-end)))
  17898. (c (save-excursion (goto-char b)
  17899. (move-beginning-of-line 1)))
  17900. (d (save-excursion (goto-char b)
  17901. (move-end-of-line 1) (1+ (point)))))
  17902. (transpose-regions a b c d)
  17903. (goto-char d)))
  17904. ((org-at-table-p) (call-interactively 'org-table-move-row))
  17905. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  17906. ((org-at-item-p) (call-interactively 'org-move-item-down))
  17907. (t (org-drag-element-forward))))
  17908. (defun org-shiftup (&optional arg)
  17909. "Increase item in timestamp or increase priority of current headline.
  17910. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  17911. depending on context. See the individual commands for more information."
  17912. (interactive "P")
  17913. (cond
  17914. ((run-hook-with-args-until-success 'org-shiftup-hook))
  17915. ((and org-support-shift-select (org-region-active-p))
  17916. (org-call-for-shift-select 'previous-line))
  17917. ((org-at-timestamp-p t)
  17918. (call-interactively (if org-edit-timestamp-down-means-later
  17919. 'org-timestamp-down 'org-timestamp-up)))
  17920. ((and (not (eq org-support-shift-select 'always))
  17921. org-enable-priority-commands
  17922. (org-at-heading-p))
  17923. (call-interactively 'org-priority-up))
  17924. ((and (not org-support-shift-select) (org-at-item-p))
  17925. (call-interactively 'org-previous-item))
  17926. ((org-clocktable-try-shift 'up arg))
  17927. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  17928. (org-support-shift-select
  17929. (org-call-for-shift-select 'previous-line))
  17930. (t (org-shiftselect-error))))
  17931. (defun org-shiftdown (&optional arg)
  17932. "Decrease item in timestamp or decrease priority of current headline.
  17933. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  17934. depending on context. See the individual commands for more information."
  17935. (interactive "P")
  17936. (cond
  17937. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  17938. ((and org-support-shift-select (org-region-active-p))
  17939. (org-call-for-shift-select 'next-line))
  17940. ((org-at-timestamp-p t)
  17941. (call-interactively (if org-edit-timestamp-down-means-later
  17942. 'org-timestamp-up 'org-timestamp-down)))
  17943. ((and (not (eq org-support-shift-select 'always))
  17944. org-enable-priority-commands
  17945. (org-at-heading-p))
  17946. (call-interactively 'org-priority-down))
  17947. ((and (not org-support-shift-select) (org-at-item-p))
  17948. (call-interactively 'org-next-item))
  17949. ((org-clocktable-try-shift 'down arg))
  17950. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  17951. (org-support-shift-select
  17952. (org-call-for-shift-select 'next-line))
  17953. (t (org-shiftselect-error))))
  17954. (defun org-shiftright (&optional arg)
  17955. "Cycle the thing at point or in the current line, depending on context.
  17956. Depending on context, this does one of the following:
  17957. - switch a timestamp at point one day into the future
  17958. - on a headline, switch to the next TODO keyword.
  17959. - on an item, switch entire list to the next bullet type
  17960. - on a property line, switch to the next allowed value
  17961. - on a clocktable definition line, move time block into the future"
  17962. (interactive "P")
  17963. (cond
  17964. ((run-hook-with-args-until-success 'org-shiftright-hook))
  17965. ((and org-support-shift-select (org-region-active-p))
  17966. (org-call-for-shift-select 'forward-char))
  17967. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  17968. ((and (not (eq org-support-shift-select 'always))
  17969. (org-at-heading-p))
  17970. (let ((org-inhibit-logging
  17971. (not org-treat-S-cursor-todo-selection-as-state-change))
  17972. (org-inhibit-blocking
  17973. (not org-treat-S-cursor-todo-selection-as-state-change)))
  17974. (org-call-with-arg 'org-todo 'right)))
  17975. ((or (and org-support-shift-select
  17976. (not (eq org-support-shift-select 'always))
  17977. (org-at-item-bullet-p))
  17978. (and (not org-support-shift-select) (org-at-item-p)))
  17979. (org-call-with-arg 'org-cycle-list-bullet nil))
  17980. ((and (not (eq org-support-shift-select 'always))
  17981. (org-at-property-p))
  17982. (call-interactively 'org-property-next-allowed-value))
  17983. ((org-clocktable-try-shift 'right arg))
  17984. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  17985. (org-support-shift-select
  17986. (org-call-for-shift-select 'forward-char))
  17987. (t (org-shiftselect-error))))
  17988. (defun org-shiftleft (&optional arg)
  17989. "Cycle the thing at point or in the current line, depending on context.
  17990. Depending on context, this does one of the following:
  17991. - switch a timestamp at point one day into the past
  17992. - on a headline, switch to the previous TODO keyword.
  17993. - on an item, switch entire list to the previous bullet type
  17994. - on a property line, switch to the previous allowed value
  17995. - on a clocktable definition line, move time block into the past"
  17996. (interactive "P")
  17997. (cond
  17998. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  17999. ((and org-support-shift-select (org-region-active-p))
  18000. (org-call-for-shift-select 'backward-char))
  18001. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  18002. ((and (not (eq org-support-shift-select 'always))
  18003. (org-at-heading-p))
  18004. (let ((org-inhibit-logging
  18005. (not org-treat-S-cursor-todo-selection-as-state-change))
  18006. (org-inhibit-blocking
  18007. (not org-treat-S-cursor-todo-selection-as-state-change)))
  18008. (org-call-with-arg 'org-todo 'left)))
  18009. ((or (and org-support-shift-select
  18010. (not (eq org-support-shift-select 'always))
  18011. (org-at-item-bullet-p))
  18012. (and (not org-support-shift-select) (org-at-item-p)))
  18013. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  18014. ((and (not (eq org-support-shift-select 'always))
  18015. (org-at-property-p))
  18016. (call-interactively 'org-property-previous-allowed-value))
  18017. ((org-clocktable-try-shift 'left arg))
  18018. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  18019. (org-support-shift-select
  18020. (org-call-for-shift-select 'backward-char))
  18021. (t (org-shiftselect-error))))
  18022. (defun org-shiftcontrolright ()
  18023. "Switch to next TODO set."
  18024. (interactive)
  18025. (cond
  18026. ((and org-support-shift-select (org-region-active-p))
  18027. (org-call-for-shift-select 'forward-word))
  18028. ((and (not (eq org-support-shift-select 'always))
  18029. (org-at-heading-p))
  18030. (org-call-with-arg 'org-todo 'nextset))
  18031. (org-support-shift-select
  18032. (org-call-for-shift-select 'forward-word))
  18033. (t (org-shiftselect-error))))
  18034. (defun org-shiftcontrolleft ()
  18035. "Switch to previous TODO set."
  18036. (interactive)
  18037. (cond
  18038. ((and org-support-shift-select (org-region-active-p))
  18039. (org-call-for-shift-select 'backward-word))
  18040. ((and (not (eq org-support-shift-select 'always))
  18041. (org-at-heading-p))
  18042. (org-call-with-arg 'org-todo 'previousset))
  18043. (org-support-shift-select
  18044. (org-call-for-shift-select 'backward-word))
  18045. (t (org-shiftselect-error))))
  18046. (defun org-shiftcontrolup (&optional n)
  18047. "Change timestamps synchronously up in CLOCK log lines.
  18048. Optional argument N tells to change by that many units."
  18049. (interactive "P")
  18050. (cond ((and (not org-support-shift-select)
  18051. (org-at-clock-log-p)
  18052. (org-at-timestamp-p t))
  18053. (org-clock-timestamps-up n))
  18054. (t (org-shiftselect-error))))
  18055. (defun org-shiftcontroldown (&optional n)
  18056. "Change timestamps synchronously down in CLOCK log lines.
  18057. Optional argument N tells to change by that many units."
  18058. (interactive "P")
  18059. (cond ((and (not org-support-shift-select)
  18060. (org-at-clock-log-p)
  18061. (org-at-timestamp-p t))
  18062. (org-clock-timestamps-down n))
  18063. (t (org-shiftselect-error))))
  18064. (defun org-ctrl-c-ret ()
  18065. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  18066. (interactive)
  18067. (cond
  18068. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  18069. (t (call-interactively 'org-insert-heading))))
  18070. (defun org-find-visible ()
  18071. (let ((s (point)))
  18072. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  18073. (get-char-property s 'invisible)))
  18074. s))
  18075. (defun org-find-invisible ()
  18076. (let ((s (point)))
  18077. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  18078. (not (get-char-property s 'invisible))))
  18079. s))
  18080. (defun org-copy-visible (beg end)
  18081. "Copy the visible parts of the region."
  18082. (interactive "r")
  18083. (let (snippets s)
  18084. (save-excursion
  18085. (save-restriction
  18086. (narrow-to-region beg end)
  18087. (setq s (goto-char (point-min)))
  18088. (while (not (= (point) (point-max)))
  18089. (goto-char (org-find-invisible))
  18090. (push (buffer-substring s (point)) snippets)
  18091. (setq s (goto-char (org-find-visible))))))
  18092. (kill-new (apply 'concat (nreverse snippets)))))
  18093. (defun org-copy-special ()
  18094. "Copy region in table or copy current subtree.
  18095. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  18096. See the individual commands for more information."
  18097. (interactive)
  18098. (call-interactively
  18099. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  18100. (defun org-cut-special ()
  18101. "Cut region in table or cut current subtree.
  18102. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  18103. See the individual commands for more information."
  18104. (interactive)
  18105. (call-interactively
  18106. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  18107. (defun org-paste-special (arg)
  18108. "Paste rectangular region into table, or past subtree relative to level.
  18109. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  18110. See the individual commands for more information."
  18111. (interactive "P")
  18112. (if (org-at-table-p)
  18113. (org-table-paste-rectangle)
  18114. (org-paste-subtree arg)))
  18115. (defsubst org-in-fixed-width-region-p ()
  18116. "Is point in a fixed-width region?"
  18117. (save-match-data
  18118. (eq 'fixed-width (org-element-type (org-element-at-point)))))
  18119. (defun org-edit-special (&optional arg)
  18120. "Call a special editor for the element at point.
  18121. When at a table, call the formula editor with `org-table-edit-formulas'.
  18122. When in a source code block, call `org-edit-src-code'.
  18123. When in a fixed-width region, call `org-edit-fixed-width-region'.
  18124. When at an #+INCLUDE keyword, visit the included file.
  18125. On a link, call `ffap' to visit the link at point.
  18126. Otherwise, return a user error."
  18127. (interactive)
  18128. (let ((element (org-element-at-point)))
  18129. (assert (not buffer-read-only) nil
  18130. "Buffer is read-only: %s" (buffer-name))
  18131. (case (org-element-type element)
  18132. (src-block
  18133. (if (not arg) (org-edit-src-code)
  18134. (let* ((info (org-babel-get-src-block-info))
  18135. (lang (nth 0 info))
  18136. (params (nth 2 info))
  18137. (session (cdr (assq :session params))))
  18138. (if (not session) (org-edit-src-code)
  18139. ;; At a src-block with a session and function called with
  18140. ;; an ARG: switch to the buffer related to the inferior
  18141. ;; process.
  18142. (funcall (intern (concat "org-babel-prep-session:" lang))
  18143. session params)))))
  18144. (keyword
  18145. (if (member (org-element-property :key element) '("INCLUDE" "SETUPFILE"))
  18146. (find-file
  18147. (org-remove-double-quotes
  18148. (car (org-split-string (org-element-property :value element)))))
  18149. (user-error "No special environment to edit here")))
  18150. (table
  18151. (if (eq (org-element-property :type element) 'table.el)
  18152. (org-edit-src-code)
  18153. (call-interactively 'org-table-edit-formulas)))
  18154. ;; Only Org tables contain `table-row' type elements.
  18155. (table-row (call-interactively 'org-table-edit-formulas))
  18156. ((example-block export-block) (org-edit-src-code))
  18157. (fixed-width (org-edit-fixed-width-region))
  18158. (otherwise
  18159. ;; No notable element at point. Though, we may be at a link,
  18160. ;; which is an object. Thus, scan deeper.
  18161. (if (eq (org-element-type (org-element-context element)) 'link)
  18162. (call-interactively 'ffap)
  18163. (user-error "No special environment to edit here"))))))
  18164. (defvar org-table-coordinate-overlays) ; defined in org-table.el
  18165. (defun org-ctrl-c-ctrl-c (&optional arg)
  18166. "Set tags in headline, or update according to changed information at point.
  18167. This command does many different things, depending on context:
  18168. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  18169. this is what we do.
  18170. - If the cursor is on a statistics cookie, update it.
  18171. - If the cursor is in a headline, prompt for tags and insert them
  18172. into the current line, aligned to `org-tags-column'. When called
  18173. with prefix arg, realign all tags in the current buffer.
  18174. - If the cursor is in one of the special #+KEYWORD lines, this
  18175. triggers scanning the buffer for these lines and updating the
  18176. information.
  18177. - If the cursor is inside a table, realign the table. This command
  18178. works even if the automatic table editor has been turned off.
  18179. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  18180. the entire table.
  18181. - If the cursor is at a footnote reference or definition, jump to
  18182. the corresponding definition or references, respectively.
  18183. - If the cursor is a the beginning of a dynamic block, update it.
  18184. - If the current buffer is a capture buffer, close note and file it.
  18185. - If the cursor is on a <<<target>>>, update radio targets and
  18186. corresponding links in this buffer.
  18187. - If the cursor is on a numbered item in a plain list, renumber the
  18188. ordered list.
  18189. - If the cursor is on a checkbox, toggle it.
  18190. - If the cursor is on a code block, evaluate it. The variable
  18191. `org-confirm-babel-evaluate' can be used to control prompting
  18192. before code block evaluation, by default every code block
  18193. evaluation requires confirmation. Code block evaluation can be
  18194. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  18195. (interactive "P")
  18196. (cond
  18197. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  18198. org-occur-highlights
  18199. org-latex-fragment-image-overlays)
  18200. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  18201. (org-remove-occur-highlights)
  18202. (org-remove-latex-fragment-image-overlays)
  18203. (message "Temporary highlights/overlays removed from current buffer"))
  18204. ((and (local-variable-p 'org-finish-function (current-buffer))
  18205. (fboundp org-finish-function))
  18206. (funcall org-finish-function))
  18207. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  18208. (t
  18209. (let* ((context (org-element-context)) (type (org-element-type context)))
  18210. ;; Test if point is within a blank line.
  18211. (if (save-excursion (beginning-of-line) (looking-at "[ \t]*$"))
  18212. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  18213. (user-error "C-c C-c can do nothing useful at this location"))
  18214. ;; For convenience: at the first line of a paragraph on the
  18215. ;; same line as an item, apply function on that item instead.
  18216. (when (eq type 'paragraph)
  18217. (let ((parent (org-element-property :parent context)))
  18218. (when (and (eq (org-element-type parent) 'item)
  18219. (= (point-at-bol) (org-element-property :begin parent)))
  18220. (setq context parent type 'item))))
  18221. ;; Act according to type of element or object at point.
  18222. (case type
  18223. (clock (org-clock-update-time-maybe))
  18224. (dynamic-block
  18225. (save-excursion
  18226. (goto-char (org-element-property :post-affiliated context))
  18227. (org-update-dblock)))
  18228. (footnote-definition
  18229. (goto-char (org-element-property :post-affiliated context))
  18230. (call-interactively 'org-footnote-action))
  18231. (footnote-reference (call-interactively 'org-footnote-action))
  18232. ((headline inlinetask)
  18233. (save-excursion (goto-char (org-element-property :begin context))
  18234. (call-interactively 'org-set-tags)))
  18235. (item
  18236. ;; At an item: a double C-u set checkbox to "[-]"
  18237. ;; unconditionally, whereas a single one will toggle its
  18238. ;; presence. Without an universal argument, if the item
  18239. ;; has a checkbox, toggle it. Otherwise repair the list.
  18240. (let* ((box (org-element-property :checkbox context))
  18241. (struct (org-element-property :structure context))
  18242. (old-struct (copy-tree struct))
  18243. (parents (org-list-parents-alist struct))
  18244. (prevs (org-list-prevs-alist struct))
  18245. (orderedp (org-not-nil (org-entry-get nil "ORDERED"))))
  18246. (org-list-set-checkbox
  18247. (org-element-property :begin context) struct
  18248. (cond ((equal arg '(16)) "[-]")
  18249. ((and (not box) (equal arg '(4))) "[ ]")
  18250. ((or (not box) (equal arg '(4))) nil)
  18251. ((eq box 'on) "[ ]")
  18252. (t "[X]")))
  18253. ;; Mimic `org-list-write-struct' but with grabbing
  18254. ;; a return value from `org-list-struct-fix-box'.
  18255. (org-list-struct-fix-ind struct parents 2)
  18256. (org-list-struct-fix-item-end struct)
  18257. (org-list-struct-fix-bul struct prevs)
  18258. (org-list-struct-fix-ind struct parents)
  18259. (let ((block-item
  18260. (org-list-struct-fix-box struct parents prevs orderedp)))
  18261. (if (and box (equal struct old-struct))
  18262. (if (equal arg '(16))
  18263. (message "Checkboxes already reset")
  18264. (user-error "Cannot toggle this checkbox: %s"
  18265. (if (eq box 'on)
  18266. "all subitems checked"
  18267. "unchecked subitems")))
  18268. (org-list-struct-apply-struct struct old-struct)
  18269. (org-update-checkbox-count-maybe))
  18270. (when block-item
  18271. (message "Checkboxes were removed due to empty box at line %d"
  18272. (org-current-line block-item))))))
  18273. (keyword
  18274. (let ((org-inhibit-startup-visibility-stuff t)
  18275. (org-startup-align-all-tables nil))
  18276. (when (boundp 'org-table-coordinate-overlays)
  18277. (mapc 'delete-overlay org-table-coordinate-overlays)
  18278. (setq org-table-coordinate-overlays nil))
  18279. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  18280. (message "Local setup has been refreshed"))
  18281. (plain-list
  18282. ;; At a plain list, with a double C-u argument, set
  18283. ;; checkboxes of each item to "[-]", whereas a single one
  18284. ;; will toggle their presence according to the state of the
  18285. ;; first item in the list. Without an argument, repair the
  18286. ;; list.
  18287. (let* ((begin (org-element-property :contents-begin context))
  18288. (struct (org-element-property :structure context))
  18289. (old-struct (copy-tree struct))
  18290. (first-box (save-excursion
  18291. (goto-char begin)
  18292. (looking-at org-list-full-item-re)
  18293. (match-string-no-properties 3)))
  18294. (new-box (cond ((equal arg '(16)) "[-]")
  18295. ((equal arg '(4)) (unless first-box "[ ]"))
  18296. ((equal first-box "[X]") "[ ]")
  18297. (t "[X]"))))
  18298. (cond
  18299. (arg
  18300. (mapc (lambda (pos) (org-list-set-checkbox pos struct new-box))
  18301. (org-list-get-all-items
  18302. begin struct (org-list-prevs-alist struct))))
  18303. ((and first-box (eq (point) begin))
  18304. ;; For convenience, when point is at bol on the first
  18305. ;; item of the list and no argument is provided, simply
  18306. ;; toggle checkbox of that item, if any.
  18307. (org-list-set-checkbox begin struct new-box)))
  18308. (org-list-write-struct
  18309. struct (org-list-parents-alist struct) old-struct)
  18310. (org-update-checkbox-count-maybe)
  18311. (save-excursion (goto-char begin) (org-list-send-list 'maybe))))
  18312. ((property-drawer node-property)
  18313. (call-interactively 'org-property-action))
  18314. ((radio-target target)
  18315. (call-interactively 'org-update-radio-target-regexp))
  18316. (statistics-cookie
  18317. (call-interactively 'org-update-statistics-cookies))
  18318. ((table table-cell table-row)
  18319. ;; At a table, recalculate every field and align it. Also
  18320. ;; send the table if necessary. If the table has
  18321. ;; a `table.el' type, just give up. At a table row or
  18322. ;; cell, maybe recalculate line but always align table.
  18323. (if (eq (org-element-property :type context) 'table.el)
  18324. (message "Use C-c ' to edit table.el tables")
  18325. (let ((org-enable-table-editor t))
  18326. (if (or (eq type 'table)
  18327. ;; Check if point is at a TBLFM line.
  18328. (and (eq type 'table-row)
  18329. (= (point) (org-element-property :end context))))
  18330. (save-excursion
  18331. (goto-char (org-element-property :contents-begin context))
  18332. (org-call-with-arg 'org-table-recalculate (or arg t))
  18333. (orgtbl-send-table 'maybe))
  18334. (org-table-maybe-eval-formula)
  18335. (cond (arg (call-interactively 'org-table-recalculate))
  18336. ((org-table-maybe-recalculate-line))
  18337. (t (org-table-align)))))))
  18338. (timestamp (org-timestamp-change 0 'day))
  18339. (otherwise
  18340. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  18341. (user-error
  18342. "C-c C-c can do nothing useful at this location")))))))))
  18343. (defun org-mode-restart ()
  18344. "Restart Org-mode, to scan again for special lines.
  18345. Also updates the keyword regular expressions."
  18346. (interactive)
  18347. (org-mode)
  18348. (message "Org-mode restarted"))
  18349. (defun org-kill-note-or-show-branches ()
  18350. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  18351. (interactive)
  18352. (if (not org-finish-function)
  18353. (progn
  18354. (hide-subtree)
  18355. (call-interactively 'show-branches))
  18356. (let ((org-note-abort t))
  18357. (funcall org-finish-function))))
  18358. (defun org-return (&optional indent)
  18359. "Goto next table row or insert a newline.
  18360. Calls `org-table-next-row' or `newline', depending on context.
  18361. See the individual commands for more information."
  18362. (interactive)
  18363. (let (org-ts-what)
  18364. (cond
  18365. ((or (bobp) (org-in-src-block-p))
  18366. (if indent (newline-and-indent) (newline)))
  18367. ((org-at-table-p)
  18368. (org-table-justify-field-maybe)
  18369. (call-interactively 'org-table-next-row))
  18370. ;; when `newline-and-indent' is called within a list, make sure
  18371. ;; text moved stays inside the item.
  18372. ((and (org-in-item-p) indent)
  18373. (if (and (org-at-item-p) (>= (point) (match-end 0)))
  18374. (progn
  18375. (save-match-data (newline))
  18376. (org-indent-line-to (length (match-string 0))))
  18377. (let ((ind (org-get-indentation)))
  18378. (newline)
  18379. (if (org-looking-back org-list-end-re)
  18380. (org-indent-line)
  18381. (org-indent-line-to ind)))))
  18382. ((and org-return-follows-link
  18383. (org-at-timestamp-p t)
  18384. (not (eq org-ts-what 'after)))
  18385. (org-follow-timestamp-link))
  18386. ((and org-return-follows-link
  18387. (let ((tprop (get-text-property (point) 'face)))
  18388. (or (eq tprop 'org-link)
  18389. (and (listp tprop) (memq 'org-link tprop)))))
  18390. (call-interactively 'org-open-at-point))
  18391. ((and (org-at-heading-p)
  18392. (looking-at
  18393. (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
  18394. (org-show-entry)
  18395. (end-of-line 1)
  18396. (newline))
  18397. (t (if indent (newline-and-indent) (newline))))))
  18398. (defun org-return-indent ()
  18399. "Goto next table row or insert a newline and indent.
  18400. Calls `org-table-next-row' or `newline-and-indent', depending on
  18401. context. See the individual commands for more information."
  18402. (interactive)
  18403. (org-return t))
  18404. (defun org-ctrl-c-star ()
  18405. "Compute table, or change heading status of lines.
  18406. Calls `org-table-recalculate' or `org-toggle-heading',
  18407. depending on context."
  18408. (interactive)
  18409. (cond
  18410. ((org-at-table-p)
  18411. (call-interactively 'org-table-recalculate))
  18412. (t
  18413. ;; Convert all lines in region to list items
  18414. (call-interactively 'org-toggle-heading))))
  18415. (defun org-ctrl-c-minus ()
  18416. "Insert separator line in table or modify bullet status of line.
  18417. Also turns a plain line or a region of lines into list items.
  18418. Calls `org-table-insert-hline', `org-toggle-item', or
  18419. `org-cycle-list-bullet', depending on context."
  18420. (interactive)
  18421. (cond
  18422. ((org-at-table-p)
  18423. (call-interactively 'org-table-insert-hline))
  18424. ((org-region-active-p)
  18425. (call-interactively 'org-toggle-item))
  18426. ((org-in-item-p)
  18427. (call-interactively 'org-cycle-list-bullet))
  18428. (t
  18429. (call-interactively 'org-toggle-item))))
  18430. (defun org-toggle-item (arg)
  18431. "Convert headings or normal lines to items, items to normal lines.
  18432. If there is no active region, only the current line is considered.
  18433. If the first non blank line in the region is a headline, convert
  18434. all headlines to items, shifting text accordingly.
  18435. If it is an item, convert all items to normal lines.
  18436. If it is normal text, change region into a list of items.
  18437. With a prefix argument ARG, change the region in a single item."
  18438. (interactive "P")
  18439. (let ((shift-text
  18440. (function
  18441. ;; Shift text in current section to IND, from point to END.
  18442. ;; The function leaves point to END line.
  18443. (lambda (ind end)
  18444. (let ((min-i 1000) (end (copy-marker end)))
  18445. ;; First determine the minimum indentation (MIN-I) of
  18446. ;; the text.
  18447. (save-excursion
  18448. (catch 'exit
  18449. (while (< (point) end)
  18450. (let ((i (org-get-indentation)))
  18451. (cond
  18452. ;; Skip blank lines and inline tasks.
  18453. ((looking-at "^[ \t]*$"))
  18454. ((looking-at org-outline-regexp-bol))
  18455. ;; We can't find less than 0 indentation.
  18456. ((zerop i) (throw 'exit (setq min-i 0)))
  18457. ((< i min-i) (setq min-i i))))
  18458. (forward-line))))
  18459. ;; Then indent each line so that a line indented to
  18460. ;; MIN-I becomes indented to IND. Ignore blank lines
  18461. ;; and inline tasks in the process.
  18462. (let ((delta (- ind min-i)))
  18463. (while (< (point) end)
  18464. (unless (or (looking-at "^[ \t]*$")
  18465. (looking-at org-outline-regexp-bol))
  18466. (org-indent-line-to (+ (org-get-indentation) delta)))
  18467. (forward-line)))))))
  18468. (skip-blanks
  18469. (function
  18470. ;; Return beginning of first non-blank line, starting from
  18471. ;; line at POS.
  18472. (lambda (pos)
  18473. (save-excursion
  18474. (goto-char pos)
  18475. (skip-chars-forward " \r\t\n")
  18476. (point-at-bol)))))
  18477. beg end)
  18478. ;; Determine boundaries of changes.
  18479. (if (org-region-active-p)
  18480. (setq beg (funcall skip-blanks (region-beginning))
  18481. end (copy-marker (region-end)))
  18482. (setq beg (funcall skip-blanks (point-at-bol))
  18483. end (copy-marker (point-at-eol))))
  18484. ;; Depending on the starting line, choose an action on the text
  18485. ;; between BEG and END.
  18486. (org-with-limited-levels
  18487. (save-excursion
  18488. (goto-char beg)
  18489. (cond
  18490. ;; Case 1. Start at an item: de-itemize. Note that it only
  18491. ;; happens when a region is active: `org-ctrl-c-minus'
  18492. ;; would call `org-cycle-list-bullet' otherwise.
  18493. ((org-at-item-p)
  18494. (while (< (point) end)
  18495. (when (org-at-item-p)
  18496. (skip-chars-forward " \t")
  18497. (delete-region (point) (match-end 0)))
  18498. (forward-line)))
  18499. ;; Case 2. Start at an heading: convert to items.
  18500. ((org-at-heading-p)
  18501. (let* ((bul (org-list-bullet-string "-"))
  18502. (bul-len (length bul))
  18503. ;; Indentation of the first heading. It should be
  18504. ;; relative to the indentation of its parent, if any.
  18505. (start-ind (save-excursion
  18506. (cond
  18507. ((not org-adapt-indentation) 0)
  18508. ((not (outline-previous-heading)) 0)
  18509. (t (length (match-string 0))))))
  18510. ;; Level of first heading. Further headings will be
  18511. ;; compared to it to determine hierarchy in the list.
  18512. (ref-level (org-reduced-level (org-outline-level))))
  18513. (while (< (point) end)
  18514. (let* ((level (org-reduced-level (org-outline-level)))
  18515. (delta (max 0 (- level ref-level))))
  18516. ;; If current headline is less indented than the first
  18517. ;; one, set it as reference, in order to preserve
  18518. ;; subtrees.
  18519. (when (< level ref-level) (setq ref-level level))
  18520. (replace-match bul t t)
  18521. (org-indent-line-to (+ start-ind (* delta bul-len)))
  18522. ;; Ensure all text down to END (or SECTION-END) belongs
  18523. ;; to the newly created item.
  18524. (let ((section-end (save-excursion
  18525. (or (outline-next-heading) (point)))))
  18526. (forward-line)
  18527. (funcall shift-text
  18528. (+ start-ind (* (1+ delta) bul-len))
  18529. (min end section-end)))))))
  18530. ;; Case 3. Normal line with ARG: make the first line of region
  18531. ;; an item, and shift indentation of others lines to
  18532. ;; set them as item's body.
  18533. (arg (let* ((bul (org-list-bullet-string "-"))
  18534. (bul-len (length bul))
  18535. (ref-ind (org-get-indentation)))
  18536. (skip-chars-forward " \t")
  18537. (insert bul)
  18538. (forward-line)
  18539. (while (< (point) end)
  18540. ;; Ensure that lines less indented than first one
  18541. ;; still get included in item body.
  18542. (funcall shift-text
  18543. (+ ref-ind bul-len)
  18544. (min end (save-excursion (or (outline-next-heading)
  18545. (point)))))
  18546. (forward-line))))
  18547. ;; Case 4. Normal line without ARG: turn each non-item line
  18548. ;; into an item.
  18549. (t
  18550. (while (< (point) end)
  18551. (unless (or (org-at-heading-p) (org-at-item-p))
  18552. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  18553. (replace-match
  18554. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  18555. (forward-line))))))))
  18556. (defun org-toggle-heading (&optional nstars)
  18557. "Convert headings to normal text, or items or text to headings.
  18558. If there is no active region, only convert the current line.
  18559. With a \\[universal-argument] prefix, convert the whole list at
  18560. point into heading.
  18561. In a region:
  18562. - If the first non blank line is a headline, remove the stars
  18563. from all headlines in the region.
  18564. - If it is a normal line, turn each and every normal line (i.e.,
  18565. not an heading or an item) in the region into headings. If you
  18566. want to convert only the first line of this region, use one
  18567. universal prefix argument.
  18568. - If it is a plain list item, turn all plain list items into headings.
  18569. When converting a line into a heading, the number of stars is chosen
  18570. such that the lines become children of the current entry. However,
  18571. when a numeric prefix argument is given, its value determines the
  18572. number of stars to add."
  18573. (interactive "P")
  18574. (let ((skip-blanks
  18575. (function
  18576. ;; Return beginning of first non-blank line, starting from
  18577. ;; line at POS.
  18578. (lambda (pos)
  18579. (save-excursion
  18580. (goto-char pos)
  18581. (while (org-at-comment-p) (forward-line))
  18582. (skip-chars-forward " \r\t\n")
  18583. (point-at-bol)))))
  18584. beg end toggled)
  18585. ;; Determine boundaries of changes. If a universal prefix has
  18586. ;; been given, put the list in a region. If region ends at a bol,
  18587. ;; do not consider the last line to be in the region.
  18588. (when (and current-prefix-arg (org-at-item-p))
  18589. (if (listp current-prefix-arg) (setq current-prefix-arg 1))
  18590. (org-mark-element))
  18591. (if (org-region-active-p)
  18592. (setq beg (funcall skip-blanks (region-beginning))
  18593. end (copy-marker (save-excursion
  18594. (goto-char (region-end))
  18595. (if (bolp) (point) (point-at-eol)))))
  18596. (setq beg (funcall skip-blanks (point-at-bol))
  18597. end (copy-marker (point-at-eol))))
  18598. ;; Ensure inline tasks don't count as headings.
  18599. (org-with-limited-levels
  18600. (save-excursion
  18601. (goto-char beg)
  18602. (cond
  18603. ;; Case 1. Started at an heading: de-star headings.
  18604. ((org-at-heading-p)
  18605. (while (< (point) end)
  18606. (when (org-at-heading-p t)
  18607. (looking-at org-outline-regexp) (replace-match "")
  18608. (setq toggled t))
  18609. (forward-line)))
  18610. ;; Case 2. Started at an item: change items into headlines.
  18611. ;; One star will be added by `org-list-to-subtree'.
  18612. ((org-at-item-p)
  18613. (let* ((stars (make-string
  18614. ;; subtract the star that will be added again by
  18615. ;; `org-list-to-subtree'
  18616. (if (numberp nstars) (1- nstars)
  18617. (or (org-current-level) 0))
  18618. ?*))
  18619. (add-stars
  18620. (cond (nstars "") ; stars from prefix only
  18621. ((equal stars "") "") ; before first heading
  18622. (org-odd-levels-only "*") ; inside heading, odd
  18623. (t "")))) ; inside heading, oddeven
  18624. (while (< (point) end)
  18625. (when (org-at-item-p)
  18626. ;; Pay attention to cases when region ends before list.
  18627. (let* ((struct (org-list-struct))
  18628. (list-end (min (org-list-get-bottom-point struct) (1+ end))))
  18629. (save-restriction
  18630. (narrow-to-region (point) list-end)
  18631. (insert
  18632. (org-list-to-subtree
  18633. (org-list-parse-list t)
  18634. '(:istart (concat stars add-stars (funcall get-stars depth))
  18635. :icount (concat stars add-stars (funcall get-stars depth)))))))
  18636. (setq toggled t))
  18637. (forward-line))))
  18638. ;; Case 3. Started at normal text: make every line an heading,
  18639. ;; skipping headlines and items.
  18640. (t (let* ((stars
  18641. (make-string
  18642. (if (numberp nstars) nstars (or (org-current-level) 0)) ?*))
  18643. (add-stars
  18644. (cond (nstars "") ; stars from prefix only
  18645. ((equal stars "") "*") ; before first heading
  18646. (org-odd-levels-only "**") ; inside heading, odd
  18647. (t "*"))) ; inside heading, oddeven
  18648. (rpl (concat stars add-stars " "))
  18649. (lend (if (listp nstars) (save-excursion (end-of-line) (point)))))
  18650. (while (< (point) (if (equal nstars '(4)) lend end))
  18651. (when (and (not (or (org-at-heading-p) (org-at-item-p) (org-at-comment-p)))
  18652. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  18653. (replace-match (concat rpl (match-string 2))) (setq toggled t))
  18654. (forward-line)))))))
  18655. (unless toggled (message "Cannot toggle heading from here"))))
  18656. (defun org-meta-return (&optional arg)
  18657. "Insert a new heading or wrap a region in a table.
  18658. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  18659. See the individual commands for more information."
  18660. (interactive "P")
  18661. (cond
  18662. ((run-hook-with-args-until-success 'org-metareturn-hook))
  18663. ((or (org-at-drawer-p) (org-at-property-p))
  18664. (newline-and-indent))
  18665. ((org-at-table-p)
  18666. (call-interactively 'org-table-wrap-region))
  18667. (t (call-interactively 'org-insert-heading))))
  18668. ;;; Menu entries
  18669. (defsubst org-in-subtree-not-table-p ()
  18670. "Are we in a subtree and not in a table?"
  18671. (and (not (org-before-first-heading-p))
  18672. (not (org-at-table-p))))
  18673. ;; Define the Org-mode menus
  18674. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  18675. '("Tbl"
  18676. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  18677. ["Next Field" org-cycle (org-at-table-p)]
  18678. ["Previous Field" org-shifttab (org-at-table-p)]
  18679. ["Next Row" org-return (org-at-table-p)]
  18680. "--"
  18681. ["Blank Field" org-table-blank-field (org-at-table-p)]
  18682. ["Edit Field" org-table-edit-field (org-at-table-p)]
  18683. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  18684. "--"
  18685. ("Column"
  18686. ["Move Column Left" org-metaleft (org-at-table-p)]
  18687. ["Move Column Right" org-metaright (org-at-table-p)]
  18688. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  18689. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  18690. ("Row"
  18691. ["Move Row Up" org-metaup (org-at-table-p)]
  18692. ["Move Row Down" org-metadown (org-at-table-p)]
  18693. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  18694. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  18695. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  18696. "--"
  18697. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  18698. ("Rectangle"
  18699. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  18700. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  18701. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  18702. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  18703. "--"
  18704. ("Calculate"
  18705. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  18706. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  18707. ["Edit Formulas" org-edit-special (org-at-table-p)]
  18708. "--"
  18709. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  18710. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  18711. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  18712. "--"
  18713. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  18714. "--"
  18715. ["Sum Column/Rectangle" org-table-sum
  18716. (or (org-at-table-p) (org-region-active-p))]
  18717. ["Which Column?" org-table-current-column (org-at-table-p)])
  18718. ["Debug Formulas"
  18719. org-table-toggle-formula-debugger
  18720. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  18721. ["Show Col/Row Numbers"
  18722. org-table-toggle-coordinate-overlays
  18723. :style toggle
  18724. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  18725. "--"
  18726. ["Create" org-table-create (and (not (org-at-table-p))
  18727. org-enable-table-editor)]
  18728. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  18729. ["Import from File" org-table-import (not (org-at-table-p))]
  18730. ["Export to File" org-table-export (org-at-table-p)]
  18731. "--"
  18732. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  18733. (easy-menu-define org-org-menu org-mode-map "Org menu"
  18734. '("Org"
  18735. ("Show/Hide"
  18736. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  18737. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  18738. ["Sparse Tree..." org-sparse-tree t]
  18739. ["Reveal Context" org-reveal t]
  18740. ["Show All" show-all t]
  18741. "--"
  18742. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  18743. "--"
  18744. ["New Heading" org-insert-heading t]
  18745. ("Navigate Headings"
  18746. ["Up" outline-up-heading t]
  18747. ["Next" outline-next-visible-heading t]
  18748. ["Previous" outline-previous-visible-heading t]
  18749. ["Next Same Level" outline-forward-same-level t]
  18750. ["Previous Same Level" outline-backward-same-level t]
  18751. "--"
  18752. ["Jump" org-goto t])
  18753. ("Edit Structure"
  18754. ["Refile Subtree" org-refile (org-in-subtree-not-table-p)]
  18755. "--"
  18756. ["Move Subtree Up" org-shiftmetaup (org-in-subtree-not-table-p)]
  18757. ["Move Subtree Down" org-shiftmetadown (org-in-subtree-not-table-p)]
  18758. "--"
  18759. ["Copy Subtree" org-copy-special (org-in-subtree-not-table-p)]
  18760. ["Cut Subtree" org-cut-special (org-in-subtree-not-table-p)]
  18761. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  18762. "--"
  18763. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  18764. "--"
  18765. ["Copy visible text" org-copy-visible t]
  18766. "--"
  18767. ["Promote Heading" org-metaleft (org-in-subtree-not-table-p)]
  18768. ["Promote Subtree" org-shiftmetaleft (org-in-subtree-not-table-p)]
  18769. ["Demote Heading" org-metaright (org-in-subtree-not-table-p)]
  18770. ["Demote Subtree" org-shiftmetaright (org-in-subtree-not-table-p)]
  18771. "--"
  18772. ["Sort Region/Children" org-sort t]
  18773. "--"
  18774. ["Convert to odd levels" org-convert-to-odd-levels t]
  18775. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  18776. ("Editing"
  18777. ["Emphasis..." org-emphasize t]
  18778. ["Edit Source Example" org-edit-special t]
  18779. "--"
  18780. ["Footnote new/jump" org-footnote-action t]
  18781. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  18782. ("Archive"
  18783. ["Archive (default method)" org-archive-subtree-default (org-in-subtree-not-table-p)]
  18784. "--"
  18785. ["Move Subtree to Archive file" org-advertized-archive-subtree (org-in-subtree-not-table-p)]
  18786. ["Toggle ARCHIVE tag" org-toggle-archive-tag (org-in-subtree-not-table-p)]
  18787. ["Move subtree to Archive sibling" org-archive-to-archive-sibling (org-in-subtree-not-table-p)]
  18788. )
  18789. "--"
  18790. ("Hyperlinks"
  18791. ["Store Link (Global)" org-store-link t]
  18792. ["Find existing link to here" org-occur-link-in-agenda-files t]
  18793. ["Insert Link" org-insert-link t]
  18794. ["Follow Link" org-open-at-point t]
  18795. "--"
  18796. ["Next link" org-next-link t]
  18797. ["Previous link" org-previous-link t]
  18798. "--"
  18799. ["Descriptive Links"
  18800. org-toggle-link-display
  18801. :style radio
  18802. :selected org-descriptive-links
  18803. ]
  18804. ["Literal Links"
  18805. org-toggle-link-display
  18806. :style radio
  18807. :selected (not org-descriptive-links)])
  18808. "--"
  18809. ("TODO Lists"
  18810. ["TODO/DONE/-" org-todo t]
  18811. ("Select keyword"
  18812. ["Next keyword" org-shiftright (org-at-heading-p)]
  18813. ["Previous keyword" org-shiftleft (org-at-heading-p)]
  18814. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  18815. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
  18816. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
  18817. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  18818. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  18819. "--"
  18820. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  18821. :selected org-enforce-todo-dependencies :style toggle :active t]
  18822. "Settings for tree at point"
  18823. ["Do Children sequentially" org-toggle-ordered-property :style radio
  18824. :selected (org-entry-get nil "ORDERED")
  18825. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  18826. ["Do Children parallel" org-toggle-ordered-property :style radio
  18827. :selected (not (org-entry-get nil "ORDERED"))
  18828. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  18829. "--"
  18830. ["Set Priority" org-priority t]
  18831. ["Priority Up" org-shiftup t]
  18832. ["Priority Down" org-shiftdown t]
  18833. "--"
  18834. ["Get news from all feeds" org-feed-update-all t]
  18835. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  18836. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  18837. ("TAGS and Properties"
  18838. ["Set Tags" org-set-tags-command (not (org-before-first-heading-p))]
  18839. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  18840. "--"
  18841. ["Set property" org-set-property (not (org-before-first-heading-p))]
  18842. ["Column view of properties" org-columns t]
  18843. ["Insert Column View DBlock" org-insert-columns-dblock t])
  18844. ("Dates and Scheduling"
  18845. ["Timestamp" org-time-stamp (not (org-before-first-heading-p))]
  18846. ["Timestamp (inactive)" org-time-stamp-inactive (not (org-before-first-heading-p))]
  18847. ("Change Date"
  18848. ["1 Day Later" org-shiftright (org-at-timestamp-p)]
  18849. ["1 Day Earlier" org-shiftleft (org-at-timestamp-p)]
  18850. ["1 ... Later" org-shiftup (org-at-timestamp-p)]
  18851. ["1 ... Earlier" org-shiftdown (org-at-timestamp-p)])
  18852. ["Compute Time Range" org-evaluate-time-range t]
  18853. ["Schedule Item" org-schedule (not (org-before-first-heading-p))]
  18854. ["Deadline" org-deadline (not (org-before-first-heading-p))]
  18855. "--"
  18856. ["Custom time format" org-toggle-time-stamp-overlays
  18857. :style radio :selected org-display-custom-times]
  18858. "--"
  18859. ["Goto Calendar" org-goto-calendar t]
  18860. ["Date from Calendar" org-date-from-calendar t]
  18861. "--"
  18862. ["Start/Restart Timer" org-timer-start t]
  18863. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  18864. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  18865. ["Insert Timer String" org-timer t]
  18866. ["Insert Timer Item" org-timer-item t])
  18867. ("Logging work"
  18868. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  18869. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  18870. ["Clock out" org-clock-out t]
  18871. ["Clock cancel" org-clock-cancel t]
  18872. "--"
  18873. ["Mark as default task" org-clock-mark-default-task t]
  18874. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  18875. ["Goto running clock" org-clock-goto t]
  18876. "--"
  18877. ["Display times" org-clock-display t]
  18878. ["Create clock table" org-clock-report t]
  18879. "--"
  18880. ["Record DONE time"
  18881. (progn (setq org-log-done (not org-log-done))
  18882. (message "Switching to %s will %s record a timestamp"
  18883. (car org-done-keywords)
  18884. (if org-log-done "automatically" "not")))
  18885. :style toggle :selected org-log-done])
  18886. "--"
  18887. ["Agenda Command..." org-agenda t]
  18888. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  18889. ("File List for Agenda")
  18890. ("Special views current file"
  18891. ["TODO Tree" org-show-todo-tree t]
  18892. ["Check Deadlines" org-check-deadlines t]
  18893. ["Timeline" org-timeline t]
  18894. ["Tags/Property tree" org-match-sparse-tree t])
  18895. "--"
  18896. ["Export/Publish..." org-export-dispatch t]
  18897. ("LaTeX"
  18898. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  18899. :selected org-cdlatex-mode]
  18900. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  18901. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  18902. ["Modify math symbol" org-cdlatex-math-modify
  18903. (org-inside-LaTeX-fragment-p)]
  18904. ["Insert citation" org-reftex-citation t]
  18905. "--"
  18906. ["Template for BEAMER" (org-beamer-insert-options-template) t])
  18907. "--"
  18908. ("MobileOrg"
  18909. ["Push Files and Views" org-mobile-push t]
  18910. ["Get Captured and Flagged" org-mobile-pull t]
  18911. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  18912. "--"
  18913. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  18914. "--"
  18915. ("Documentation"
  18916. ["Show Version" org-version t]
  18917. ["Info Documentation" org-info t])
  18918. ("Customize"
  18919. ["Browse Org Group" org-customize t]
  18920. "--"
  18921. ["Expand This Menu" org-create-customize-menu
  18922. (fboundp 'customize-menu-create)])
  18923. ["Send bug report" org-submit-bug-report t]
  18924. "--"
  18925. ("Refresh/Reload"
  18926. ["Refresh setup current buffer" org-mode-restart t]
  18927. ["Reload Org (after update)" org-reload t]
  18928. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x !"])
  18929. ))
  18930. (defun org-info (&optional node)
  18931. "Read documentation for Org-mode in the info system.
  18932. With optional NODE, go directly to that node."
  18933. (interactive)
  18934. (info (format "(org)%s" (or node ""))))
  18935. ;;;###autoload
  18936. (defun org-submit-bug-report ()
  18937. "Submit a bug report on Org-mode via mail.
  18938. Don't hesitate to report any problems or inaccurate documentation.
  18939. If you don't have setup sending mail from (X)Emacs, please copy the
  18940. output buffer into your mail program, as it gives us important
  18941. information about your Org-mode version and configuration."
  18942. (interactive)
  18943. (require 'reporter)
  18944. (org-load-modules-maybe)
  18945. (org-require-autoloaded-modules)
  18946. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  18947. (reporter-submit-bug-report
  18948. "emacs-orgmode@gnu.org"
  18949. (org-version nil 'full)
  18950. (let (list)
  18951. (save-window-excursion
  18952. (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
  18953. (delete-other-windows)
  18954. (erase-buffer)
  18955. (insert "You are about to submit a bug report to the Org-mode mailing list.
  18956. We would like to add your full Org-mode and Outline configuration to the
  18957. bug report. This greatly simplifies the work of the maintainer and
  18958. other experts on the mailing list.
  18959. HOWEVER, some variables you have customized may contain private
  18960. information. The names of customers, colleagues, or friends, might
  18961. appear in the form of file names, tags, todo states, or search strings.
  18962. If you answer yes to the prompt, you might want to check and remove
  18963. such private information before sending the email.")
  18964. (add-text-properties (point-min) (point-max) '(face org-warning))
  18965. (when (yes-or-no-p "Include your Org-mode configuration ")
  18966. (mapatoms
  18967. (lambda (v)
  18968. (and (boundp v)
  18969. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  18970. (or (and (symbol-value v)
  18971. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  18972. (and
  18973. (get v 'custom-type) (get v 'standard-value)
  18974. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  18975. (push v list)))))
  18976. (kill-buffer (get-buffer "*Warn about privacy*"))
  18977. list))
  18978. nil nil
  18979. "Remember to cover the basics, that is, what you expected to happen and
  18980. what in fact did happen. You don't know how to make a good report? See
  18981. http://orgmode.org/manual/Feedback.html#Feedback
  18982. Your bug report will be posted to the Org-mode mailing list.
  18983. ------------------------------------------------------------------------")
  18984. (save-excursion
  18985. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  18986. (replace-match "\\1Bug: \\3 [\\2]")))))
  18987. (defun org-install-agenda-files-menu ()
  18988. (let ((bl (buffer-list)))
  18989. (save-excursion
  18990. (while bl
  18991. (set-buffer (pop bl))
  18992. (if (derived-mode-p 'org-mode) (setq bl nil)))
  18993. (when (derived-mode-p 'org-mode)
  18994. (easy-menu-change
  18995. '("Org") "File List for Agenda"
  18996. (append
  18997. (list
  18998. ["Edit File List" (org-edit-agenda-file-list) t]
  18999. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  19000. ["Remove Current File from List" org-remove-file t]
  19001. ["Cycle through agenda files" org-cycle-agenda-files t]
  19002. ["Occur in all agenda files" org-occur-in-agenda-files t]
  19003. "--")
  19004. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  19005. ;;;; Documentation
  19006. (defun org-require-autoloaded-modules ()
  19007. (interactive)
  19008. (mapc 'require
  19009. '(org-agenda org-archive org-attach org-clock org-colview org-id
  19010. org-remember org-table org-timer)))
  19011. ;;;###autoload
  19012. (defun org-reload (&optional uncompiled)
  19013. "Reload all org lisp files.
  19014. With prefix arg UNCOMPILED, load the uncompiled versions."
  19015. (interactive "P")
  19016. (require 'loadhist)
  19017. (let* ((org-dir (org-find-library-dir "org"))
  19018. (contrib-dir (or (org-find-library-dir "org-contribdir") org-dir))
  19019. (feature-re "^\\(org\\|ob\\|ox\\)\\(-.*\\)?")
  19020. (remove-re (mapconcat 'identity
  19021. (mapcar (lambda (f) (concat "^" f "$"))
  19022. (list (if (featurep 'xemacs)
  19023. "org-colview"
  19024. "org-colview-xemacs")
  19025. "org" "org-loaddefs" "org-version"))
  19026. "\\|"))
  19027. (feats (delete-dups
  19028. (mapcar 'file-name-sans-extension
  19029. (mapcar 'file-name-nondirectory
  19030. (delq nil
  19031. (mapcar 'feature-file
  19032. features))))))
  19033. (lfeat (append
  19034. (sort
  19035. (setq feats
  19036. (delq nil (mapcar
  19037. (lambda (f)
  19038. (if (and (string-match feature-re f)
  19039. (not (string-match remove-re f)))
  19040. f nil))
  19041. feats)))
  19042. 'string-lessp)
  19043. (list "org-version" "org")))
  19044. (load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  19045. (load-suffixes (if uncompiled (reverse load-suffixes) load-suffixes))
  19046. load-uncore load-misses)
  19047. (setq load-misses
  19048. (delq 't
  19049. (mapcar (lambda (f)
  19050. (or (org-load-noerror-mustsuffix (concat org-dir f))
  19051. (and (string= org-dir contrib-dir)
  19052. (org-load-noerror-mustsuffix (concat contrib-dir f)))
  19053. (and (org-load-noerror-mustsuffix (concat (org-find-library-dir f) f))
  19054. (add-to-list 'load-uncore f 'append)
  19055. 't)
  19056. f))
  19057. lfeat)))
  19058. (if load-uncore
  19059. (message "The following feature%s found in load-path, please check if that's correct:\n%s"
  19060. (if (> (length load-uncore) 1) "s were" " was") load-uncore))
  19061. (if load-misses
  19062. (message "Some error occured while reloading Org feature%s\n%s\nPlease check *Messages*!\n%s"
  19063. (if (> (length load-misses) 1) "s" "") load-misses (org-version nil 'full))
  19064. (message "Successfully reloaded Org\n%s" (org-version nil 'full)))))
  19065. ;;;###autoload
  19066. (defun org-customize ()
  19067. "Call the customize function with org as argument."
  19068. (interactive)
  19069. (org-load-modules-maybe)
  19070. (org-require-autoloaded-modules)
  19071. (customize-browse 'org))
  19072. (defun org-create-customize-menu ()
  19073. "Create a full customization menu for Org-mode, insert it into the menu."
  19074. (interactive)
  19075. (org-load-modules-maybe)
  19076. (org-require-autoloaded-modules)
  19077. (if (fboundp 'customize-menu-create)
  19078. (progn
  19079. (easy-menu-change
  19080. '("Org") "Customize"
  19081. `(["Browse Org group" org-customize t]
  19082. "--"
  19083. ,(customize-menu-create 'org)
  19084. ["Set" Custom-set t]
  19085. ["Save" Custom-save t]
  19086. ["Reset to Current" Custom-reset-current t]
  19087. ["Reset to Saved" Custom-reset-saved t]
  19088. ["Reset to Standard Settings" Custom-reset-standard t]))
  19089. (message "\"Org\"-menu now contains full customization menu"))
  19090. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  19091. ;;;; Miscellaneous stuff
  19092. ;;; Generally useful functions
  19093. (defun org-get-at-bol (property)
  19094. "Get text property PROPERTY at beginning of line."
  19095. (get-text-property (point-at-bol) property))
  19096. (defun org-find-text-property-in-string (prop s)
  19097. "Return the first non-nil value of property PROP in string S."
  19098. (or (get-text-property 0 prop s)
  19099. (get-text-property (or (next-single-property-change 0 prop s) 0)
  19100. prop s)))
  19101. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  19102. "Display the given MESSAGE as a warning."
  19103. (if (fboundp 'display-warning)
  19104. (display-warning 'org message
  19105. (if (featurep 'xemacs) 'warning :warning))
  19106. (let ((buf (get-buffer-create "*Org warnings*")))
  19107. (with-current-buffer buf
  19108. (goto-char (point-max))
  19109. (insert "Warning (Org): " message)
  19110. (unless (bolp)
  19111. (newline)))
  19112. (display-buffer buf)
  19113. (sit-for 0))))
  19114. (defun org-eval (form)
  19115. "Eval FORM and return result."
  19116. (condition-case error
  19117. (eval form)
  19118. (error (format "%%![Error: %s]" error))))
  19119. (defun org-in-clocktable-p ()
  19120. "Check if the cursor is in a clocktable."
  19121. (let ((pos (point)) start)
  19122. (save-excursion
  19123. (end-of-line 1)
  19124. (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
  19125. (setq start (match-beginning 0))
  19126. (re-search-forward "^[ \t]*#\\+END:.*" nil t)
  19127. (>= (match-end 0) pos)
  19128. start))))
  19129. (defun org-in-commented-line ()
  19130. "Is point in a line starting with `#'?"
  19131. (equal (char-after (point-at-bol)) ?#))
  19132. (defun org-in-indented-comment-line ()
  19133. "Is point in a line starting with `#' after some white space?"
  19134. (save-excursion
  19135. (save-match-data
  19136. (goto-char (point-at-bol))
  19137. (looking-at "[ \t]*#"))))
  19138. (defun org-in-verbatim-emphasis ()
  19139. (save-match-data
  19140. (and (org-in-regexp org-emph-re 2)
  19141. (>= (point) (match-beginning 3))
  19142. (<= (point) (match-end 4))
  19143. (member (match-string 3) '("=" "~")))))
  19144. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  19145. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  19146. (if (and marker (marker-buffer marker)
  19147. (buffer-live-p (marker-buffer marker)))
  19148. (progn
  19149. (org-pop-to-buffer-same-window (marker-buffer marker))
  19150. (if (or (> marker (point-max)) (< marker (point-min)))
  19151. (widen))
  19152. (goto-char marker)
  19153. (org-show-context 'org-goto))
  19154. (if bookmark
  19155. (bookmark-jump bookmark)
  19156. (error "Cannot find location"))))
  19157. (defun org-quote-csv-field (s)
  19158. "Quote field for inclusion in CSV material."
  19159. (if (string-match "[\",]" s)
  19160. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  19161. s))
  19162. (defun org-force-self-insert (N)
  19163. "Needed to enforce self-insert under remapping."
  19164. (interactive "p")
  19165. (self-insert-command N))
  19166. (defun org-string-width (s)
  19167. "Compute width of string, ignoring invisible characters.
  19168. This ignores character with invisibility property `org-link', and also
  19169. characters with property `org-cwidth', because these will become invisible
  19170. upon the next fontification round."
  19171. (let (b l)
  19172. (when (or (eq t buffer-invisibility-spec)
  19173. (assq 'org-link buffer-invisibility-spec))
  19174. (while (setq b (text-property-any 0 (length s)
  19175. 'invisible 'org-link s))
  19176. (setq s (concat (substring s 0 b)
  19177. (substring s (or (next-single-property-change
  19178. b 'invisible s) (length s)))))))
  19179. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  19180. (setq s (concat (substring s 0 b)
  19181. (substring s (or (next-single-property-change
  19182. b 'org-cwidth s) (length s))))))
  19183. (setq l (string-width s) b -1)
  19184. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  19185. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  19186. l))
  19187. (defun org-shorten-string (s maxlength)
  19188. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  19189. If the string is shorter or has length MAXLENGTH, just return the
  19190. original string. If it is longer, the functions finds a space in the
  19191. string, breaks this string off at that locations and adds three dots
  19192. as ellipsis. Including the ellipsis, the string will not be longer
  19193. than MAXLENGTH. If finding a good breaking point in the string does
  19194. not work, the string is just chopped off in the middle of a word
  19195. if necessary."
  19196. (if (<= (length s) maxlength)
  19197. s
  19198. (let* ((n (max (- maxlength 4) 1))
  19199. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  19200. (if (string-match re s)
  19201. (concat (match-string 1 s) "...")
  19202. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  19203. (defun org-get-indentation (&optional line)
  19204. "Get the indentation of the current line, interpreting tabs.
  19205. When LINE is given, assume it represents a line and compute its indentation."
  19206. (if line
  19207. (if (string-match "^ *" (org-remove-tabs line))
  19208. (match-end 0))
  19209. (save-excursion
  19210. (beginning-of-line 1)
  19211. (skip-chars-forward " \t")
  19212. (current-column))))
  19213. (defun org-get-string-indentation (s)
  19214. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  19215. (let ((n -1) (i 0) (w tab-width) c)
  19216. (catch 'exit
  19217. (while (< (setq n (1+ n)) (length s))
  19218. (setq c (aref s n))
  19219. (cond ((= c ?\ ) (setq i (1+ i)))
  19220. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  19221. (t (throw 'exit t)))))
  19222. i))
  19223. (defun org-remove-tabs (s &optional width)
  19224. "Replace tabulators in S with spaces.
  19225. Assumes that s is a single line, starting in column 0."
  19226. (setq width (or width tab-width))
  19227. (while (string-match "\t" s)
  19228. (setq s (replace-match
  19229. (make-string
  19230. (- (* width (/ (+ (match-beginning 0) width) width))
  19231. (match-beginning 0)) ?\ )
  19232. t t s)))
  19233. s)
  19234. (defun org-fix-indentation (line ind)
  19235. "Fix indentation in LINE.
  19236. IND is a cons cell with target and minimum indentation.
  19237. If the current indentation in LINE is smaller than the minimum,
  19238. leave it alone. If it is larger than ind, set it to the target."
  19239. (let* ((l (org-remove-tabs line))
  19240. (i (org-get-indentation l))
  19241. (i1 (car ind)) (i2 (cdr ind)))
  19242. (if (>= i i2) (setq l (substring line i2)))
  19243. (if (> i1 0)
  19244. (concat (make-string i1 ?\ ) l)
  19245. l)))
  19246. (defun org-remove-indentation (code &optional n)
  19247. "Remove the maximum common indentation from the lines in CODE.
  19248. N may optionally be the number of spaces to remove."
  19249. (with-temp-buffer
  19250. (insert code)
  19251. (org-do-remove-indentation n)
  19252. (buffer-string)))
  19253. (defun org-do-remove-indentation (&optional n)
  19254. "Remove the maximum common indentation from the buffer."
  19255. (untabify (point-min) (point-max))
  19256. (let ((min 10000) re)
  19257. (if n
  19258. (setq min n)
  19259. (goto-char (point-min))
  19260. (while (re-search-forward "^ *[^ \n]" nil t)
  19261. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  19262. (unless (or (= min 0) (= min 10000))
  19263. (setq re (format "^ \\{%d\\}" min))
  19264. (goto-char (point-min))
  19265. (while (re-search-forward re nil t)
  19266. (replace-match "")
  19267. (end-of-line 1))
  19268. min)))
  19269. (defun org-fill-template (template alist)
  19270. "Find each %key of ALIST in TEMPLATE and replace it."
  19271. (let ((case-fold-search nil)
  19272. entry key value)
  19273. (setq alist (sort (copy-sequence alist)
  19274. (lambda (a b) (< (length (car a)) (length (car b))))))
  19275. (while (setq entry (pop alist))
  19276. (setq template
  19277. (replace-regexp-in-string
  19278. (concat "%" (regexp-quote (car entry)))
  19279. (or (cdr entry) "") template t t)))
  19280. template))
  19281. (defun org-base-buffer (buffer)
  19282. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  19283. (if (not buffer)
  19284. buffer
  19285. (or (buffer-base-buffer buffer)
  19286. buffer)))
  19287. (defun org-trim (s)
  19288. "Remove whitespace at beginning and end of string."
  19289. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  19290. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  19291. s)
  19292. (defun org-wrap (string &optional width lines)
  19293. "Wrap string to either a number of lines, or a width in characters.
  19294. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  19295. that costs. If there is a word longer than WIDTH, the text is actually
  19296. wrapped to the length of that word.
  19297. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  19298. many lines, whatever width that takes.
  19299. The return value is a list of lines, without newlines at the end."
  19300. (let* ((words (org-split-string string "[ \t\n]+"))
  19301. (maxword (apply 'max (mapcar 'org-string-width words)))
  19302. w ll)
  19303. (cond (width
  19304. (org-do-wrap words (max maxword width)))
  19305. (lines
  19306. (setq w maxword)
  19307. (setq ll (org-do-wrap words maxword))
  19308. (if (<= (length ll) lines)
  19309. ll
  19310. (setq ll words)
  19311. (while (> (length ll) lines)
  19312. (setq w (1+ w))
  19313. (setq ll (org-do-wrap words w)))
  19314. ll))
  19315. (t (error "Cannot wrap this")))))
  19316. (defun org-do-wrap (words width)
  19317. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  19318. (let (lines line)
  19319. (while words
  19320. (setq line (pop words))
  19321. (while (and words (< (+ (length line) (length (car words))) width))
  19322. (setq line (concat line " " (pop words))))
  19323. (setq lines (push line lines)))
  19324. (nreverse lines)))
  19325. (defun org-split-string (string &optional separators)
  19326. "Splits STRING into substrings at SEPARATORS.
  19327. No empty strings are returned if there are matches at the beginning
  19328. and end of string."
  19329. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  19330. (start 0)
  19331. notfirst
  19332. (list nil))
  19333. (while (and (string-match rexp string
  19334. (if (and notfirst
  19335. (= start (match-beginning 0))
  19336. (< start (length string)))
  19337. (1+ start) start))
  19338. (< (match-beginning 0) (length string)))
  19339. (setq notfirst t)
  19340. (or (eq (match-beginning 0) 0)
  19341. (and (eq (match-beginning 0) (match-end 0))
  19342. (eq (match-beginning 0) start))
  19343. (setq list
  19344. (cons (substring string start (match-beginning 0))
  19345. list)))
  19346. (setq start (match-end 0)))
  19347. (or (eq start (length string))
  19348. (setq list
  19349. (cons (substring string start)
  19350. list)))
  19351. (nreverse list)))
  19352. (defun org-quote-vert (s)
  19353. "Replace \"|\" with \"\\vert\"."
  19354. (while (string-match "|" s)
  19355. (setq s (replace-match "\\vert" t t s)))
  19356. s)
  19357. (defun org-uuidgen-p (s)
  19358. "Is S an ID created by UUIDGEN?"
  19359. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  19360. (defun org-in-src-block-p (&optional inside)
  19361. "Whether point is in a code source block.
  19362. When INSIDE is non-nil, don't consider we are within a src block
  19363. when point is at #+BEGIN_SRC or #+END_SRC."
  19364. (let ((case-fold-search t) ov)
  19365. (or (and (setq ov (overlays-at (point)))
  19366. (memq 'org-block-background
  19367. (overlay-properties (car ov))))
  19368. (and (not inside)
  19369. (save-match-data
  19370. (save-excursion
  19371. (beginning-of-line)
  19372. (looking-at ".*#\\+\\(begin\\|end\\)_src")))))))
  19373. (defun org-context ()
  19374. "Return a list of contexts of the current cursor position.
  19375. If several contexts apply, all are returned.
  19376. Each context entry is a list with a symbol naming the context, and
  19377. two positions indicating start and end of the context. Possible
  19378. contexts are:
  19379. :headline anywhere in a headline
  19380. :headline-stars on the leading stars in a headline
  19381. :todo-keyword on a TODO keyword (including DONE) in a headline
  19382. :tags on the TAGS in a headline
  19383. :priority on the priority cookie in a headline
  19384. :item on the first line of a plain list item
  19385. :item-bullet on the bullet/number of a plain list item
  19386. :checkbox on the checkbox in a plain list item
  19387. :table in an org-mode table
  19388. :table-special on a special filed in a table
  19389. :table-table in a table.el table
  19390. :clocktable in a clocktable
  19391. :src-block in a source block
  19392. :link on a hyperlink
  19393. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT, QUOTE.
  19394. :target on a <<target>>
  19395. :radio-target on a <<<radio-target>>>
  19396. :latex-fragment on a LaTeX fragment
  19397. :latex-preview on a LaTeX fragment with overlaid preview image
  19398. This function expects the position to be visible because it uses font-lock
  19399. faces as a help to recognize the following contexts: :table-special, :link,
  19400. and :keyword."
  19401. (let* ((f (get-text-property (point) 'face))
  19402. (faces (if (listp f) f (list f)))
  19403. (case-fold-search t)
  19404. (p (point)) clist o)
  19405. ;; First the large context
  19406. (cond
  19407. ((org-at-heading-p t)
  19408. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  19409. (when (progn
  19410. (beginning-of-line 1)
  19411. (looking-at org-todo-line-tags-regexp))
  19412. (push (org-point-in-group p 1 :headline-stars) clist)
  19413. (push (org-point-in-group p 2 :todo-keyword) clist)
  19414. (push (org-point-in-group p 4 :tags) clist))
  19415. (goto-char p)
  19416. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  19417. (if (looking-at "\\[#[A-Z0-9]\\]")
  19418. (push (org-point-in-group p 0 :priority) clist)))
  19419. ((org-at-item-p)
  19420. (push (org-point-in-group p 2 :item-bullet) clist)
  19421. (push (list :item (point-at-bol)
  19422. (save-excursion (org-end-of-item) (point)))
  19423. clist)
  19424. (and (org-at-item-checkbox-p)
  19425. (push (org-point-in-group p 0 :checkbox) clist)))
  19426. ((org-at-table-p)
  19427. (push (list :table (org-table-begin) (org-table-end)) clist)
  19428. (if (memq 'org-formula faces)
  19429. (push (list :table-special
  19430. (previous-single-property-change p 'face)
  19431. (next-single-property-change p 'face)) clist)))
  19432. ((org-at-table-p 'any)
  19433. (push (list :table-table) clist)))
  19434. (goto-char p)
  19435. (let ((case-fold-search t))
  19436. ;; New the "medium" contexts: clocktables, source blocks
  19437. (cond ((org-in-clocktable-p)
  19438. (push (list :clocktable
  19439. (and (or (looking-at "#\\+BEGIN: clocktable")
  19440. (search-backward "#+BEGIN: clocktable" nil t))
  19441. (match-beginning 0))
  19442. (and (re-search-forward "#\\+END:?" nil t)
  19443. (match-end 0))) clist))
  19444. ((org-in-src-block-p)
  19445. (push (list :src-block
  19446. (and (or (looking-at "#\\+BEGIN_SRC")
  19447. (search-backward "#+BEGIN_SRC" nil t))
  19448. (match-beginning 0))
  19449. (and (search-forward "#+END_SRC" nil t)
  19450. (match-beginning 0))) clist))))
  19451. (goto-char p)
  19452. ;; Now the small context
  19453. (cond
  19454. ((org-at-timestamp-p)
  19455. (push (org-point-in-group p 0 :timestamp) clist))
  19456. ((memq 'org-link faces)
  19457. (push (list :link
  19458. (previous-single-property-change p 'face)
  19459. (next-single-property-change p 'face)) clist))
  19460. ((memq 'org-special-keyword faces)
  19461. (push (list :keyword
  19462. (previous-single-property-change p 'face)
  19463. (next-single-property-change p 'face)) clist))
  19464. ((org-at-target-p)
  19465. (push (org-point-in-group p 0 :target) clist)
  19466. (goto-char (1- (match-beginning 0)))
  19467. (if (looking-at org-radio-target-regexp)
  19468. (push (org-point-in-group p 0 :radio-target) clist))
  19469. (goto-char p))
  19470. ((setq o (car (delq nil
  19471. (mapcar
  19472. (lambda (x)
  19473. (if (memq x org-latex-fragment-image-overlays) x))
  19474. (overlays-at (point))))))
  19475. (push (list :latex-fragment
  19476. (overlay-start o) (overlay-end o)) clist)
  19477. (push (list :latex-preview
  19478. (overlay-start o) (overlay-end o)) clist))
  19479. ((org-inside-LaTeX-fragment-p)
  19480. ;; FIXME: positions wrong.
  19481. (push (list :latex-fragment (point) (point)) clist)))
  19482. (setq clist (nreverse (delq nil clist)))
  19483. clist))
  19484. ;; FIXME: Compare with at-regexp-p Do we need both?
  19485. (defun org-in-regexp (re &optional nlines visually)
  19486. "Check if point is inside a match of regexp.
  19487. Normally only the current line is checked, but you can include NLINES extra
  19488. lines both before and after point into the search.
  19489. If VISUALLY is set, require that the cursor is not after the match but
  19490. really on, so that the block visually is on the match."
  19491. (catch 'exit
  19492. (let ((pos (point))
  19493. (eol (point-at-eol (+ 1 (or nlines 0))))
  19494. (inc (if visually 1 0)))
  19495. (save-excursion
  19496. (beginning-of-line (- 1 (or nlines 0)))
  19497. (while (re-search-forward re eol t)
  19498. (if (and (<= (match-beginning 0) pos)
  19499. (>= (+ inc (match-end 0)) pos))
  19500. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  19501. (defun org-at-regexp-p (regexp)
  19502. "Is point inside a match of REGEXP in the current line?"
  19503. (catch 'exit
  19504. (save-excursion
  19505. (let ((pos (point)) (end (point-at-eol)))
  19506. (beginning-of-line 1)
  19507. (while (re-search-forward regexp end t)
  19508. (if (and (<= (match-beginning 0) pos)
  19509. (>= (match-end 0) pos))
  19510. (throw 'exit t)))
  19511. nil))))
  19512. (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
  19513. "Non-nil when point is between matches of START-RE and END-RE.
  19514. Also return a non-nil value when point is on one of the matches.
  19515. Optional arguments LIM-UP and LIM-DOWN bound the search; they are
  19516. buffer positions. Default values are the positions of headlines
  19517. surrounding the point.
  19518. The functions returns a cons cell whose car (resp. cdr) is the
  19519. position before START-RE (resp. after END-RE)."
  19520. (save-match-data
  19521. (let ((pos (point))
  19522. (limit-up (or lim-up (save-excursion (outline-previous-heading))))
  19523. (limit-down (or lim-down (save-excursion (outline-next-heading))))
  19524. beg end)
  19525. (save-excursion
  19526. ;; Point is on a block when on START-RE or if START-RE can be
  19527. ;; found before it...
  19528. (and (or (org-at-regexp-p start-re)
  19529. (re-search-backward start-re limit-up t))
  19530. (setq beg (match-beginning 0))
  19531. ;; ... and END-RE after it...
  19532. (goto-char (match-end 0))
  19533. (re-search-forward end-re limit-down t)
  19534. (> (setq end (match-end 0)) pos)
  19535. ;; ... without another START-RE in-between.
  19536. (goto-char (match-beginning 0))
  19537. (not (re-search-backward start-re (1+ beg) t))
  19538. ;; Return value.
  19539. (cons beg end))))))
  19540. (defun org-in-block-p (names)
  19541. "Non-nil when point belongs to a block whose name belongs to NAMES.
  19542. NAMES is a list of strings containing names of blocks.
  19543. Return first block name matched, or nil. Beware that in case of
  19544. nested blocks, the returned name may not belong to the closest
  19545. block from point."
  19546. (save-match-data
  19547. (catch 'exit
  19548. (let ((case-fold-search t)
  19549. (lim-up (save-excursion (outline-previous-heading)))
  19550. (lim-down (save-excursion (outline-next-heading))))
  19551. (mapc (lambda (name)
  19552. (let ((n (regexp-quote name)))
  19553. (when (org-between-regexps-p
  19554. (concat "^[ \t]*#\\+begin_" n)
  19555. (concat "^[ \t]*#\\+end_" n)
  19556. lim-up lim-down)
  19557. (throw 'exit n))))
  19558. names))
  19559. nil)))
  19560. (defun org-occur-in-agenda-files (regexp &optional nlines)
  19561. "Call `multi-occur' with buffers for all agenda files."
  19562. (interactive "sOrg-files matching: \np")
  19563. (let* ((files (org-agenda-files))
  19564. (tnames (mapcar 'file-truename files))
  19565. (extra org-agenda-text-search-extra-files)
  19566. f)
  19567. (when (eq (car extra) 'agenda-archives)
  19568. (setq extra (cdr extra))
  19569. (setq files (org-add-archive-files files)))
  19570. (while (setq f (pop extra))
  19571. (unless (member (file-truename f) tnames)
  19572. (add-to-list 'files f 'append)
  19573. (add-to-list 'tnames (file-truename f) 'append)))
  19574. (multi-occur
  19575. (mapcar (lambda (x)
  19576. (with-current-buffer
  19577. (or (get-file-buffer x) (find-file-noselect x))
  19578. (widen)
  19579. (current-buffer)))
  19580. files)
  19581. regexp)))
  19582. (if (boundp 'occur-mode-find-occurrence-hook)
  19583. ;; Emacs 23
  19584. (add-hook 'occur-mode-find-occurrence-hook
  19585. (lambda ()
  19586. (when (derived-mode-p 'org-mode)
  19587. (org-reveal))))
  19588. ;; Emacs 22
  19589. (defadvice occur-mode-goto-occurrence
  19590. (after org-occur-reveal activate)
  19591. (and (derived-mode-p 'org-mode) (org-reveal)))
  19592. (defadvice occur-mode-goto-occurrence-other-window
  19593. (after org-occur-reveal activate)
  19594. (and (derived-mode-p 'org-mode) (org-reveal)))
  19595. (defadvice occur-mode-display-occurrence
  19596. (after org-occur-reveal activate)
  19597. (when (derived-mode-p 'org-mode)
  19598. (let ((pos (occur-mode-find-occurrence)))
  19599. (with-current-buffer (marker-buffer pos)
  19600. (save-excursion
  19601. (goto-char pos)
  19602. (org-reveal)))))))
  19603. (defun org-occur-link-in-agenda-files ()
  19604. "Create a link and search for it in the agendas.
  19605. The link is not stored in `org-stored-links', it is just created
  19606. for the search purpose."
  19607. (interactive)
  19608. (let ((link (condition-case nil
  19609. (org-store-link nil)
  19610. (error "Unable to create a link to here"))))
  19611. (org-occur-in-agenda-files (regexp-quote link))))
  19612. (defun org-reverse-string (string)
  19613. "Return the reverse of STRING."
  19614. (apply 'string (reverse (string-to-list string))))
  19615. (defun org-uniquify (list)
  19616. "Remove duplicate elements from LIST."
  19617. (let (res)
  19618. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  19619. res))
  19620. (defun org-delete-all (elts list)
  19621. "Remove all elements in ELTS from LIST."
  19622. (while elts
  19623. (setq list (delete (pop elts) list)))
  19624. list)
  19625. (defun org-count (cl-item cl-seq)
  19626. "Count the number of occurrences of ITEM in SEQ.
  19627. Taken from `count' in cl-seq.el with all keyword arguments removed."
  19628. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  19629. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  19630. (while (< cl-start cl-end)
  19631. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  19632. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  19633. (setq cl-start (1+ cl-start)))
  19634. cl-count))
  19635. (defun org-remove-if (predicate seq)
  19636. "Remove everything from SEQ that fulfills PREDICATE."
  19637. (let (res e)
  19638. (while seq
  19639. (setq e (pop seq))
  19640. (if (not (funcall predicate e)) (push e res)))
  19641. (nreverse res)))
  19642. (defun org-remove-if-not (predicate seq)
  19643. "Remove everything from SEQ that does not fulfill PREDICATE."
  19644. (let (res e)
  19645. (while seq
  19646. (setq e (pop seq))
  19647. (if (funcall predicate e) (push e res)))
  19648. (nreverse res)))
  19649. (defun org-reduce (cl-func cl-seq &rest cl-keys)
  19650. "Reduce two-argument FUNCTION across SEQ.
  19651. Taken from `reduce' in cl-seq.el with all keyword arguments but
  19652. \":initial-value\" removed."
  19653. (let ((cl-accum (cond ((memq :initial-value cl-keys)
  19654. (cadr (memq :initial-value cl-keys)))
  19655. (cl-seq (pop cl-seq))
  19656. (t (funcall cl-func)))))
  19657. (while cl-seq
  19658. (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
  19659. cl-accum))
  19660. (defun org-back-over-empty-lines ()
  19661. "Move backwards over whitespace, to the beginning of the first empty line.
  19662. Returns the number of empty lines passed."
  19663. (let ((pos (point)))
  19664. (if (cdr (assoc 'heading org-blank-before-new-entry))
  19665. (skip-chars-backward " \t\n\r")
  19666. (unless (eobp)
  19667. (forward-line -1)))
  19668. (beginning-of-line 2)
  19669. (goto-char (min (point) pos))
  19670. (count-lines (point) pos)))
  19671. (defun org-skip-whitespace ()
  19672. (skip-chars-forward " \t\n\r"))
  19673. (defun org-point-in-group (point group &optional context)
  19674. "Check if POINT is in match-group GROUP.
  19675. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  19676. match. If the match group does not exist or point is not inside it,
  19677. return nil."
  19678. (and (match-beginning group)
  19679. (>= point (match-beginning group))
  19680. (<= point (match-end group))
  19681. (if context
  19682. (list context (match-beginning group) (match-end group))
  19683. t)))
  19684. (defun org-switch-to-buffer-other-window (&rest args)
  19685. "Switch to buffer in a second window on the current frame.
  19686. In particular, do not allow pop-up frames.
  19687. Returns the newly created buffer."
  19688. (org-no-popups
  19689. (apply 'switch-to-buffer-other-window args)))
  19690. (defun org-combine-plists (&rest plists)
  19691. "Create a single property list from all plists in PLISTS.
  19692. The process starts by copying the first list, and then setting properties
  19693. from the other lists. Settings in the last list are the most significant
  19694. ones and overrule settings in the other lists."
  19695. (let ((rtn (copy-sequence (pop plists)))
  19696. p v ls)
  19697. (while plists
  19698. (setq ls (pop plists))
  19699. (while ls
  19700. (setq p (pop ls) v (pop ls))
  19701. (setq rtn (plist-put rtn p v))))
  19702. rtn))
  19703. (defun org-replace-escapes (string table)
  19704. "Replace %-escapes in STRING with values in TABLE.
  19705. TABLE is an association list with keys like \"%a\" and string values.
  19706. The sequences in STRING may contain normal field width and padding information,
  19707. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  19708. so values can contain further %-escapes if they are define later in TABLE."
  19709. (let ((tbl (copy-alist table))
  19710. (case-fold-search nil)
  19711. (pchg 0)
  19712. e re rpl)
  19713. (while (setq e (pop tbl))
  19714. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  19715. (when (and (cdr e) (string-match re (cdr e)))
  19716. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  19717. (safe "SREF"))
  19718. (add-text-properties 0 3 (list 'sref sref) safe)
  19719. (setcdr e (replace-match safe t t (cdr e)))))
  19720. (while (string-match re string)
  19721. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  19722. (cdr e)))
  19723. (setq string (replace-match rpl t t string))))
  19724. (while (setq pchg (next-property-change pchg string))
  19725. (let ((sref (get-text-property pchg 'sref string)))
  19726. (when (and sref (string-match "SREF" string pchg))
  19727. (setq string (replace-match sref t t string)))))
  19728. string))
  19729. (defun org-sublist (list start end)
  19730. "Return a section of LIST, from START to END.
  19731. Counting starts at 1."
  19732. (let (rtn (c start))
  19733. (setq list (nthcdr (1- start) list))
  19734. (while (and list (<= c end))
  19735. (push (pop list) rtn)
  19736. (setq c (1+ c)))
  19737. (nreverse rtn)))
  19738. (defun org-find-base-buffer-visiting (file)
  19739. "Like `find-buffer-visiting' but always return the base buffer and
  19740. not an indirect buffer."
  19741. (let ((buf (or (get-file-buffer file)
  19742. (find-buffer-visiting file))))
  19743. (if buf
  19744. (or (buffer-base-buffer buf) buf)
  19745. nil)))
  19746. (defun org-image-file-name-regexp (&optional extensions)
  19747. "Return regexp matching the file names of images.
  19748. If EXTENSIONS is given, only match these."
  19749. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  19750. (image-file-name-regexp)
  19751. (let ((image-file-name-extensions
  19752. (or extensions
  19753. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  19754. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  19755. (concat "\\."
  19756. (regexp-opt (nconc (mapcar 'upcase
  19757. image-file-name-extensions)
  19758. image-file-name-extensions)
  19759. t)
  19760. "\\'"))))
  19761. (defun org-file-image-p (file &optional extensions)
  19762. "Return non-nil if FILE is an image."
  19763. (save-match-data
  19764. (string-match (org-image-file-name-regexp extensions) file)))
  19765. (defun org-get-cursor-date (&optional with-time)
  19766. "Return the date at cursor in as a time.
  19767. This works in the calendar and in the agenda, anywhere else it just
  19768. returns the current time.
  19769. If WITH-TIME is non-nil, returns the time of the event at point (in
  19770. the agenda) or the current time of the day."
  19771. (let (date day defd tp tm hod mod)
  19772. (when with-time
  19773. (setq tp (get-text-property (point) 'time))
  19774. (when (and tp (string-match "\\([0-9][0-9]\\):\\([0-9][0-9]\\)" tp))
  19775. (setq hod (string-to-number (match-string 1 tp))
  19776. mod (string-to-number (match-string 2 tp))))
  19777. (or tp (setq hod (nth 2 (decode-time (current-time)))
  19778. mod (nth 1 (decode-time (current-time))))))
  19779. (cond
  19780. ((eq major-mode 'calendar-mode)
  19781. (setq date (calendar-cursor-to-date)
  19782. defd (encode-time 0 (or mod 0) (or hod 0)
  19783. (nth 1 date) (nth 0 date) (nth 2 date))))
  19784. ((eq major-mode 'org-agenda-mode)
  19785. (setq day (get-text-property (point) 'day))
  19786. (if day
  19787. (setq date (calendar-gregorian-from-absolute day)
  19788. defd (encode-time 0 (or mod 0) (or hod 0)
  19789. (nth 1 date) (nth 0 date) (nth 2 date))))))
  19790. (or defd (current-time))))
  19791. (defun org-mark-subtree (&optional up)
  19792. "Mark the current subtree.
  19793. This puts point at the start of the current subtree, and mark at
  19794. the end. If a numeric prefix UP is given, move up into the
  19795. hierarchy of headlines by UP levels before marking the subtree."
  19796. (interactive "P")
  19797. (org-with-limited-levels
  19798. (cond ((org-at-heading-p) (beginning-of-line))
  19799. ((org-before-first-heading-p) (error "Not in a subtree"))
  19800. (t (outline-previous-visible-heading 1))))
  19801. (when up (while (and (> up 0) (org-up-heading-safe)) (decf up)))
  19802. (if (org-called-interactively-p 'any)
  19803. (call-interactively 'org-mark-element)
  19804. (org-mark-element)))
  19805. ;;; Indentation
  19806. (defun org-indent-line ()
  19807. "Indent line depending on context."
  19808. (interactive)
  19809. (let* ((pos (point))
  19810. (itemp (org-at-item-p))
  19811. (case-fold-search t)
  19812. (org-drawer-regexp (or org-drawer-regexp "\000"))
  19813. (inline-task-p (and (featurep 'org-inlinetask)
  19814. (org-inlinetask-in-task-p)))
  19815. (inline-re (and inline-task-p
  19816. (org-inlinetask-outline-regexp)))
  19817. column)
  19818. (if (and orgstruct-is-++ (eq pos (point)))
  19819. (let ((indent-line-function (cadadr (assoc 'indent-line-function org-fb-vars))))
  19820. (indent-according-to-mode))
  19821. (beginning-of-line 1)
  19822. (cond
  19823. ;; Headings
  19824. ((looking-at org-outline-regexp) (setq column 0))
  19825. ;; Footnote definition
  19826. ((looking-at org-footnote-definition-re) (setq column 0))
  19827. ;; Literal examples
  19828. ((looking-at "[ \t]*:\\( \\|$\\)")
  19829. (setq column (org-get-indentation))) ; do nothing
  19830. ;; Lists
  19831. ((ignore-errors (goto-char (org-in-item-p)))
  19832. (setq column (if itemp
  19833. (org-get-indentation)
  19834. (org-list-item-body-column (point))))
  19835. (goto-char pos))
  19836. ;; Drawers
  19837. ((and (looking-at "[ \t]*:END:")
  19838. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  19839. (save-excursion
  19840. (goto-char (1- (match-beginning 1)))
  19841. (setq column (current-column))))
  19842. ;; Special blocks
  19843. ((and (looking-at "[ \t]*#\\+end_\\([a-z]+\\)")
  19844. (save-excursion
  19845. (re-search-backward
  19846. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  19847. (setq column (org-get-indentation (match-string 0))))
  19848. ((and (not (looking-at "[ \t]*#\\+begin_"))
  19849. (org-between-regexps-p "^[ \t]*#\\+begin_" "[ \t]*#\\+end_"))
  19850. (save-excursion
  19851. (re-search-backward "^[ \t]*#\\+begin_\\([a-z]+\\)" nil t))
  19852. (setq column
  19853. (cond ((equal (downcase (match-string 1)) "src")
  19854. ;; src blocks: let `org-edit-src-exit' handle them
  19855. (org-get-indentation))
  19856. ((equal (downcase (match-string 1)) "example")
  19857. (max (org-get-indentation)
  19858. (org-get-indentation (match-string 0))))
  19859. (t
  19860. (org-get-indentation (match-string 0))))))
  19861. ;; This line has nothing special, look at the previous relevant
  19862. ;; line to compute indentation
  19863. (t
  19864. (beginning-of-line 0)
  19865. (while (and (not (bobp))
  19866. (not (looking-at org-table-line-regexp))
  19867. (not (looking-at org-drawer-regexp))
  19868. ;; When point started in an inline task, do not move
  19869. ;; above task starting line.
  19870. (not (and inline-task-p (looking-at inline-re)))
  19871. ;; Skip drawers, blocks, empty lines, verbatim,
  19872. ;; comments, tables, footnotes definitions, lists,
  19873. ;; inline tasks.
  19874. (or (and (looking-at "[ \t]*:END:")
  19875. (re-search-backward org-drawer-regexp nil t))
  19876. (and (looking-at "[ \t]*#\\+end_")
  19877. (re-search-backward "[ \t]*#\\+begin_"nil t))
  19878. (looking-at "[ \t]*[\n:#|]")
  19879. (looking-at org-footnote-definition-re)
  19880. (and (ignore-errors (goto-char (org-in-item-p)))
  19881. (goto-char
  19882. (org-list-get-top-point (org-list-struct))))
  19883. (and (not inline-task-p)
  19884. (featurep 'org-inlinetask)
  19885. (org-inlinetask-in-task-p)
  19886. (or (org-inlinetask-goto-beginning) t))))
  19887. (beginning-of-line 0))
  19888. (cond
  19889. ;; There was an heading above.
  19890. ((looking-at "\\*+[ \t]+")
  19891. (if (not org-adapt-indentation)
  19892. (setq column 0)
  19893. (goto-char (match-end 0))
  19894. (setq column (current-column))))
  19895. ;; A drawer had started and is unfinished
  19896. ((looking-at org-drawer-regexp)
  19897. (goto-char (1- (match-beginning 1)))
  19898. (setq column (current-column)))
  19899. ;; Else, nothing noticeable found: get indentation and go on.
  19900. (t (setq column (org-get-indentation))))))
  19901. ;; Now apply indentation and move cursor accordingly
  19902. (goto-char pos)
  19903. (if (<= (current-column) (current-indentation))
  19904. (org-indent-line-to column)
  19905. (save-excursion (org-indent-line-to column)))
  19906. ;; Special polishing for properties, see `org-property-format'
  19907. (setq column (current-column))
  19908. (beginning-of-line 1)
  19909. (if (looking-at
  19910. "\\([ \t]*\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  19911. (replace-match (concat (match-string 1)
  19912. (format org-property-format
  19913. (match-string 2) (match-string 3)))
  19914. t t))
  19915. (org-move-to-column column))))
  19916. (defun org-indent-drawer ()
  19917. "Indent the drawer at point."
  19918. (interactive)
  19919. (let ((p (point))
  19920. (e (and (save-excursion (re-search-forward ":END:" nil t))
  19921. (match-end 0)))
  19922. (folded
  19923. (save-excursion
  19924. (end-of-line)
  19925. (when (overlays-at (point))
  19926. (member 'invisible (overlay-properties
  19927. (car (overlays-at (point)))))))))
  19928. (when folded (org-cycle))
  19929. (indent-for-tab-command)
  19930. (while (and (move-beginning-of-line 2) (< (point) e))
  19931. (indent-for-tab-command))
  19932. (goto-char p)
  19933. (when folded (org-cycle)))
  19934. (message "Drawer at point indented"))
  19935. (defun org-indent-block ()
  19936. "Indent the block at point."
  19937. (interactive)
  19938. (let ((p (point))
  19939. (case-fold-search t)
  19940. (e (and (save-excursion (re-search-forward "#\\+end_?\\(?:[a-z]+\\)?" nil t))
  19941. (match-end 0)))
  19942. (folded
  19943. (save-excursion
  19944. (end-of-line)
  19945. (when (overlays-at (point))
  19946. (member 'invisible (overlay-properties
  19947. (car (overlays-at (point)))))))))
  19948. (when folded (org-cycle))
  19949. (indent-for-tab-command)
  19950. (while (and (move-beginning-of-line 2) (< (point) e))
  19951. (indent-for-tab-command))
  19952. (goto-char p)
  19953. (when folded (org-cycle)))
  19954. (message "Block at point indented"))
  19955. (defun org-indent-region (start end)
  19956. "Indent region."
  19957. (interactive "r")
  19958. (save-excursion
  19959. (let ((line-end (org-current-line end)))
  19960. (goto-char start)
  19961. (while (< (org-current-line) line-end)
  19962. (cond ((org-in-src-block-p) (org-src-native-tab-command-maybe))
  19963. (t (call-interactively 'org-indent-line)))
  19964. (move-beginning-of-line 2)))))
  19965. ;;; Filling
  19966. ;; We use our own fill-paragraph and auto-fill functions.
  19967. ;; `org-fill-paragraph' relies on adaptive filling and context
  19968. ;; checking. Appropriate `fill-prefix' is computed with
  19969. ;; `org-adaptive-fill-function'.
  19970. ;; `org-auto-fill-function' takes care of auto-filling. It calls
  19971. ;; `do-auto-fill' only on valid areas with `fill-prefix' shadowed with
  19972. ;; `org-adaptive-fill-function' value. Internally,
  19973. ;; `org-comment-line-break-function' breaks the line.
  19974. ;; `org-setup-filling' installs filling and auto-filling related
  19975. ;; variables during `org-mode' initialization.
  19976. (defun org-setup-filling ()
  19977. (interactive)
  19978. ;; Prevent auto-fill from inserting unwanted new items.
  19979. (when (boundp 'fill-nobreak-predicate)
  19980. (org-set-local
  19981. 'fill-nobreak-predicate
  19982. (org-uniquify
  19983. (append fill-nobreak-predicate
  19984. '(org-fill-paragraph-separate-nobreak-p
  19985. org-fill-line-break-nobreak-p
  19986. org-fill-paragraph-with-timestamp-nobreak-p)))))
  19987. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  19988. (org-set-local 'auto-fill-inhibit-regexp nil)
  19989. (org-set-local 'adaptive-fill-function 'org-adaptive-fill-function)
  19990. (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
  19991. (org-set-local 'comment-line-break-function 'org-comment-line-break-function))
  19992. (defvar org-element-paragraph-separate) ; org-element.el
  19993. (defun org-fill-paragraph-separate-nobreak-p ()
  19994. "Non-nil when a line break at point would insert a new item."
  19995. (looking-at (substring org-element-paragraph-separate 1)))
  19996. (defun org-fill-line-break-nobreak-p ()
  19997. "Non-nil when a line break at point would create an Org line break."
  19998. (save-excursion
  19999. (skip-chars-backward "[ \t]")
  20000. (skip-chars-backward "\\\\")
  20001. (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
  20002. (defun org-fill-paragraph-with-timestamp-nobreak-p ()
  20003. "Non-nil when a line break at point would insert a new item."
  20004. (and (org-at-timestamp-p t)
  20005. (not (looking-at org-ts-regexp-both))))
  20006. (declare-function message-in-body-p "message" ())
  20007. (defvar orgtbl-line-start-regexp) ; From org-table.el
  20008. (defun org-adaptive-fill-function ()
  20009. "Compute a fill prefix for the current line.
  20010. Return fill prefix, as a string, or nil if current line isn't
  20011. meant to be filled."
  20012. (let (prefix)
  20013. (catch 'exit
  20014. (when (derived-mode-p 'message-mode)
  20015. (save-excursion
  20016. (beginning-of-line)
  20017. (cond ((or (not (message-in-body-p))
  20018. (looking-at orgtbl-line-start-regexp))
  20019. (throw 'exit nil))
  20020. ((looking-at message-cite-prefix-regexp)
  20021. (throw 'exit (match-string-no-properties 0)))
  20022. ((looking-at org-outline-regexp)
  20023. (throw 'exit (make-string (length (match-string 0)) ? ))))))
  20024. (org-with-wide-buffer
  20025. (let* ((p (line-beginning-position))
  20026. (element (save-excursion (beginning-of-line)
  20027. (org-element-at-point)))
  20028. (type (org-element-type element))
  20029. (post-affiliated (org-element-property :post-affiliated element)))
  20030. (unless (and post-affiliated (< p post-affiliated))
  20031. (case type
  20032. (comment (looking-at "[ \t]*# ?") (match-string 0))
  20033. (footnote-definition "")
  20034. ((item plain-list)
  20035. (make-string (org-list-item-body-column
  20036. (or post-affiliated
  20037. (org-element-property :begin element)))
  20038. ? ))
  20039. (paragraph
  20040. ;; Fill prefix is usually the same as the current line,
  20041. ;; except if the paragraph is at the beginning of an item.
  20042. (let ((parent (org-element-property :parent element)))
  20043. (cond ((eq (org-element-type parent) 'item)
  20044. (make-string (org-list-item-body-column
  20045. (org-element-property :begin parent))
  20046. ? ))
  20047. ((save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  20048. (match-string 0))
  20049. (t ""))))
  20050. (comment-block
  20051. ;; Only fill contents if P is within block boundaries.
  20052. (let* ((cbeg (save-excursion (goto-char post-affiliated)
  20053. (forward-line)
  20054. (point)))
  20055. (cend (save-excursion
  20056. (goto-char (org-element-property :end element))
  20057. (skip-chars-backward " \r\t\n")
  20058. (line-beginning-position))))
  20059. (when (and (>= p cbeg) (< p cend))
  20060. (if (save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  20061. (match-string 0)
  20062. "")))))))))))
  20063. (declare-function message-goto-body "message" ())
  20064. (defvar message-cite-prefix-regexp) ; From message.el
  20065. (defun org-fill-paragraph (&optional justify)
  20066. "Fill element at point, when applicable.
  20067. This function only applies to comment blocks, comments, example
  20068. blocks and paragraphs. Also, as a special case, re-align table
  20069. when point is at one.
  20070. If JUSTIFY is non-nil (interactively, with prefix argument),
  20071. justify as well. If `sentence-end-double-space' is non-nil, then
  20072. period followed by one space does not end a sentence, so don't
  20073. break a line there. The variable `fill-column' controls the
  20074. width for filling.
  20075. For convenience, when point is at a plain list, an item or
  20076. a footnote definition, try to fill the first paragraph within."
  20077. (interactive)
  20078. (if (and (derived-mode-p 'message-mode)
  20079. (or (not (message-in-body-p))
  20080. (save-excursion (move-beginning-of-line 1)
  20081. (looking-at message-cite-prefix-regexp))))
  20082. ;; First ensure filling is correct in message-mode.
  20083. (let ((fill-paragraph-function
  20084. (cadadr (assoc 'fill-paragraph-function org-fb-vars)))
  20085. (fill-prefix (cadadr (assoc 'fill-prefix org-fb-vars)))
  20086. (paragraph-start (cadadr (assoc 'paragraph-start org-fb-vars)))
  20087. (paragraph-separate
  20088. (cadadr (assoc 'paragraph-separate org-fb-vars))))
  20089. (fill-paragraph nil))
  20090. (with-syntax-table org-mode-transpose-word-syntax-table
  20091. ;; Move to end of line in order to get the first paragraph
  20092. ;; within a plain list or a footnote definition.
  20093. (let ((element (save-excursion (end-of-line) (org-element-at-point))))
  20094. ;; First check if point is in a blank line at the beginning of
  20095. ;; the buffer. In that case, ignore filling.
  20096. (case (org-element-type element)
  20097. ;; Use major mode filling function is src blocks.
  20098. (src-block (org-babel-do-key-sequence-in-edit-buffer (kbd "M-q")))
  20099. ;; Align Org tables, leave table.el tables as-is.
  20100. (table-row (org-table-align) t)
  20101. (table
  20102. (when (eq (org-element-property :type element) 'org)
  20103. (org-table-align))
  20104. t)
  20105. (paragraph
  20106. ;; Paragraphs may contain `line-break' type objects.
  20107. (let ((beg (max (point-min)
  20108. (org-element-property :contents-begin element)))
  20109. (end (min (point-max)
  20110. (org-element-property :contents-end element))))
  20111. ;; Do nothing if point is at an affiliated keyword.
  20112. (if (< (line-end-position) beg) t
  20113. (when (derived-mode-p 'message-mode)
  20114. ;; In `message-mode', do not fill following citation
  20115. ;; in current paragraph nor text before message body.
  20116. (let ((body-start (save-excursion (message-goto-body))))
  20117. (when body-start (setq beg (max body-start beg))))
  20118. (when (save-excursion
  20119. (re-search-forward
  20120. (concat "^" message-cite-prefix-regexp) end t))
  20121. (setq end (match-beginning 0))))
  20122. ;; Fill paragraph, taking line breaks into account.
  20123. ;; For that, slice the paragraph using line breaks as
  20124. ;; separators, and fill the parts in reverse order to
  20125. ;; avoid messing with markers.
  20126. (save-excursion
  20127. (goto-char end)
  20128. (mapc
  20129. (lambda (pos)
  20130. (fill-region-as-paragraph pos (point) justify)
  20131. (goto-char pos))
  20132. ;; Find the list of ending positions for line breaks
  20133. ;; in the current paragraph. Add paragraph
  20134. ;; beginning to include first slice.
  20135. (nreverse
  20136. (cons beg
  20137. (org-element-map
  20138. (org-element--parse-objects
  20139. beg end nil (org-element-restriction 'paragraph))
  20140. 'line-break
  20141. (lambda (lb) (org-element-property :end lb)))))))
  20142. t)))
  20143. ;; Contents of `comment-block' type elements should be
  20144. ;; filled as plain text, but only if point is within block
  20145. ;; markers.
  20146. (comment-block
  20147. (let* ((case-fold-search t)
  20148. (beg (save-excursion
  20149. (goto-char (org-element-property :begin element))
  20150. (re-search-forward "^[ \t]*#\\+begin_comment" nil t)
  20151. (forward-line)
  20152. (point)))
  20153. (end (save-excursion
  20154. (goto-char (org-element-property :end element))
  20155. (re-search-backward "^[ \t]*#\\+end_comment" nil t)
  20156. (line-beginning-position))))
  20157. (when (and (>= (point) beg) (< (point) end))
  20158. (fill-region-as-paragraph
  20159. (save-excursion
  20160. (end-of-line)
  20161. (re-search-backward "^[ \t]*$" beg 'move)
  20162. (line-beginning-position))
  20163. (save-excursion
  20164. (beginning-of-line)
  20165. (re-search-forward "^[ \t]*$" end 'move)
  20166. (line-beginning-position))
  20167. justify)))
  20168. t)
  20169. ;; Fill comments.
  20170. (comment (fill-comment-paragraph justify))
  20171. ;; Ignore every other element.
  20172. (otherwise t))))))
  20173. (defun org-auto-fill-function ()
  20174. "Auto-fill function."
  20175. ;; Check if auto-filling is meaningful.
  20176. (let ((fc (current-fill-column)))
  20177. (when (and fc (> (current-column) fc))
  20178. (let* ((fill-prefix (org-adaptive-fill-function))
  20179. ;; Enforce empty fill prefix, if required. Otherwise, it
  20180. ;; will be computed again.
  20181. (adaptive-fill-mode (not (equal fill-prefix ""))))
  20182. (when fill-prefix (do-auto-fill))))))
  20183. (defun org-comment-line-break-function (&optional soft)
  20184. "Break line at point and indent, continuing comment if within one.
  20185. The inserted newline is marked hard if variable
  20186. `use-hard-newlines' is true, unless optional argument SOFT is
  20187. non-nil."
  20188. (if soft (insert-and-inherit ?\n) (newline 1))
  20189. (save-excursion (forward-char -1) (delete-horizontal-space))
  20190. (delete-horizontal-space)
  20191. (indent-to-left-margin)
  20192. (insert-before-markers-and-inherit fill-prefix))
  20193. ;;; Comments
  20194. ;; Org comments syntax is quite complex. It requires the entire line
  20195. ;; to be just a comment. Also, even with the right syntax at the
  20196. ;; beginning of line, some some elements (i.e. verse-block or
  20197. ;; example-block) don't accept comments. Usual Emacs comment commands
  20198. ;; cannot cope with those requirements. Therefore, Org replaces them.
  20199. ;; Org still relies on `comment-dwim', but cannot trust
  20200. ;; `comment-only-p'. So, `comment-region-function' and
  20201. ;; `uncomment-region-function' both point
  20202. ;; to`org-comment-or-uncomment-region'. Eventually,
  20203. ;; `org-insert-comment' takes care of insertion of comments at the
  20204. ;; beginning of line.
  20205. ;; `org-setup-comments-handling' install comments related variables
  20206. ;; during `org-mode' initialization.
  20207. (defun org-setup-comments-handling ()
  20208. (interactive)
  20209. (org-set-local 'comment-use-syntax nil)
  20210. (org-set-local 'comment-start "# ")
  20211. (org-set-local 'comment-start-skip "^\\s-*#\\(?: \\|$\\)")
  20212. (org-set-local 'comment-insert-comment-function 'org-insert-comment)
  20213. (org-set-local 'comment-region-function 'org-comment-or-uncomment-region)
  20214. (org-set-local 'uncomment-region-function 'org-comment-or-uncomment-region))
  20215. (defun org-insert-comment ()
  20216. "Insert an empty comment above current line.
  20217. If the line is empty, insert comment at its beginning."
  20218. (beginning-of-line)
  20219. (if (looking-at "\\s-*$") (replace-match "") (open-line 1))
  20220. (org-indent-line)
  20221. (insert "# "))
  20222. (defvar comment-empty-lines) ; From newcomment.el.
  20223. (defun org-comment-or-uncomment-region (beg end &rest ignore)
  20224. "Comment or uncomment each non-blank line in the region.
  20225. Uncomment each non-blank line between BEG and END if it only
  20226. contains commented lines. Otherwise, comment them."
  20227. (save-restriction
  20228. ;; Restrict region
  20229. (narrow-to-region (save-excursion (goto-char beg)
  20230. (skip-chars-forward " \r\t\n" end)
  20231. (line-beginning-position))
  20232. (save-excursion (goto-char end)
  20233. (skip-chars-backward " \r\t\n" beg)
  20234. (line-end-position)))
  20235. (let ((uncommentp
  20236. ;; UNCOMMENTP is non-nil when every non blank line between
  20237. ;; BEG and END is a comment.
  20238. (save-excursion
  20239. (goto-char (point-min))
  20240. (while (and (not (eobp))
  20241. (let ((element (org-element-at-point)))
  20242. (and (eq (org-element-type element) 'comment)
  20243. (goto-char (min (point-max)
  20244. (org-element-property
  20245. :end element)))))))
  20246. (eobp))))
  20247. (if uncommentp
  20248. ;; Only blank lines and comments in region: uncomment it.
  20249. (save-excursion
  20250. (goto-char (point-min))
  20251. (while (not (eobp))
  20252. (when (looking-at "[ \t]*\\(#\\(?: \\|$\\)\\)")
  20253. (replace-match "" nil nil nil 1))
  20254. (forward-line)))
  20255. ;; Comment each line in region.
  20256. (let ((min-indent (point-max)))
  20257. ;; First find the minimum indentation across all lines.
  20258. (save-excursion
  20259. (goto-char (point-min))
  20260. (while (and (not (eobp)) (not (zerop min-indent)))
  20261. (unless (looking-at "[ \t]*$")
  20262. (setq min-indent (min min-indent (current-indentation))))
  20263. (forward-line)))
  20264. ;; Then loop over all lines.
  20265. (save-excursion
  20266. (goto-char (point-min))
  20267. (while (not (eobp))
  20268. (unless (and (not comment-empty-lines) (looking-at "[ \t]*$"))
  20269. (org-move-to-column min-indent t)
  20270. (insert comment-start))
  20271. (forward-line))))))))
  20272. ;;; Planning
  20273. ;; This section contains tools to operate on timestamp objects, as
  20274. ;; returned by, e.g. `org-element-context'.
  20275. (defun org-timestamp-has-time-p (timestamp)
  20276. "Non-nil when TIMESTAMP has a time specified."
  20277. (org-element-property :hour-start timestamp))
  20278. (defun org-timestamp-format (timestamp format &optional end utc)
  20279. "Format a TIMESTAMP element into a string.
  20280. FORMAT is a format specifier to be passed to
  20281. `format-time-string'.
  20282. When optional argument END is non-nil, use end of date-range or
  20283. time-range, if possible.
  20284. When optional argument UTC is non-nil, time will be expressed as
  20285. Universal Time."
  20286. (format-time-string
  20287. format
  20288. (apply 'encode-time
  20289. (cons 0
  20290. (mapcar
  20291. (lambda (prop) (or (org-element-property prop timestamp) 0))
  20292. (if end '(:minute-end :hour-end :day-end :month-end :year-end)
  20293. '(:minute-start :hour-start :day-start :month-start
  20294. :year-start)))))
  20295. utc))
  20296. (defun org-timestamp-split-range (timestamp &optional end)
  20297. "Extract a timestamp object from a date or time range.
  20298. TIMESTAMP is a timestamp object. END, when non-nil, means extract
  20299. the end of the range. Otherwise, extract its start.
  20300. Return a new timestamp object sharing the same parent as
  20301. TIMESTAMP."
  20302. (let ((type (org-element-property :type timestamp)))
  20303. (if (memq type '(active inactive diary)) timestamp
  20304. (let ((split-ts (list 'timestamp (copy-sequence (nth 1 timestamp)))))
  20305. ;; Set new type.
  20306. (org-element-put-property
  20307. split-ts :type (if (eq type 'active-range) 'active 'inactive))
  20308. ;; Copy start properties over end properties if END is
  20309. ;; non-nil. Otherwise, copy end properties over `start' ones.
  20310. (let ((p-alist '((:minute-start . :minute-end)
  20311. (:hour-start . :hour-end)
  20312. (:day-start . :day-end)
  20313. (:month-start . :month-end)
  20314. (:year-start . :year-end))))
  20315. (dolist (p-cell p-alist)
  20316. (org-element-put-property
  20317. split-ts
  20318. (funcall (if end 'car 'cdr) p-cell)
  20319. (org-element-property
  20320. (funcall (if end 'cdr 'car) p-cell) split-ts)))
  20321. ;; Eventually refresh `:raw-value'.
  20322. (org-element-put-property split-ts :raw-value nil)
  20323. (org-element-put-property
  20324. split-ts :raw-value (org-element-interpret-data split-ts)))))))
  20325. (defun org-timestamp-translate (timestamp &optional boundary)
  20326. "Apply `org-translate-time' on a TIMESTAMP object.
  20327. When optional argument BOUNDARY is non-nil, it is either the
  20328. symbol `start' or `end'. In this case, only translate the
  20329. starting or ending part of TIMESTAMP if it is a date or time
  20330. range. Otherwise, translate both parts."
  20331. (if (and (not boundary)
  20332. (memq (org-element-property :type timestamp)
  20333. '(active-range inactive-range)))
  20334. (concat
  20335. (org-translate-time
  20336. (org-element-property :raw-value
  20337. (org-timestamp-split-range timestamp)))
  20338. "--"
  20339. (org-translate-time
  20340. (org-element-property :raw-value
  20341. (org-timestamp-split-range timestamp t))))
  20342. (org-translate-time
  20343. (org-element-property
  20344. :raw-value
  20345. (if (not boundary) timestamp
  20346. (org-timestamp-split-range timestamp (eq boundary 'end)))))))
  20347. ;;; Other stuff.
  20348. (defun org-toggle-fixed-width-section (arg)
  20349. "Toggle the fixed-width export.
  20350. If there is no active region, the QUOTE keyword at the current headline is
  20351. inserted or removed. When present, it causes the text between this headline
  20352. and the next to be exported as fixed-width text, and unmodified.
  20353. If there is an active region, this command adds or removes a colon as the
  20354. first character of this line. If the first character of a line is a colon,
  20355. this line is also exported in fixed-width font."
  20356. (interactive "P")
  20357. (let* ((cc 0)
  20358. (regionp (org-region-active-p))
  20359. (beg (if regionp (region-beginning) (point)))
  20360. (end (if regionp (region-end)))
  20361. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  20362. (case-fold-search nil)
  20363. (re "[ \t]*\\(:\\(?: \\|$\\)\\)")
  20364. off)
  20365. (if regionp
  20366. (save-excursion
  20367. (goto-char beg)
  20368. (setq cc (current-column))
  20369. (beginning-of-line 1)
  20370. (setq off (looking-at re))
  20371. (while (> nlines 0)
  20372. (setq nlines (1- nlines))
  20373. (beginning-of-line 1)
  20374. (cond
  20375. (arg
  20376. (org-move-to-column cc t)
  20377. (insert ": \n")
  20378. (forward-line -1))
  20379. ((and off (looking-at re))
  20380. (replace-match "" t t nil 1))
  20381. ((not off) (org-move-to-column cc t) (insert ": ")))
  20382. (forward-line 1)))
  20383. (save-excursion
  20384. (org-back-to-heading)
  20385. (cond
  20386. ((looking-at (format org-heading-keyword-regexp-format
  20387. org-quote-string))
  20388. (goto-char (match-end 1))
  20389. (looking-at (concat " +" org-quote-string))
  20390. (replace-match "" t t)
  20391. (when (eolp) (insert " ")))
  20392. ((looking-at org-outline-regexp)
  20393. (goto-char (match-end 0))
  20394. (insert org-quote-string " ")))))))
  20395. (defun org-reftex-citation ()
  20396. "Use reftex-citation to insert a citation into the buffer.
  20397. This looks for a line like
  20398. #+BIBLIOGRAPHY: foo plain option:-d
  20399. and derives from it that foo.bib is the bibliography file relevant
  20400. for this document. It then installs the necessary environment for RefTeX
  20401. to work in this buffer and calls `reftex-citation' to insert a citation
  20402. into the buffer.
  20403. Export of such citations to both LaTeX and HTML is handled by the contributed
  20404. package org-exp-bibtex by Taru Karttunen."
  20405. (interactive)
  20406. (let ((reftex-docstruct-symbol 'rds)
  20407. (reftex-cite-format "\\cite{%l}")
  20408. rds bib)
  20409. (save-excursion
  20410. (save-restriction
  20411. (widen)
  20412. (let ((case-fold-search t)
  20413. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  20414. (if (not (save-excursion
  20415. (or (re-search-forward re nil t)
  20416. (re-search-backward re nil t))))
  20417. (error "No bibliography defined in file")
  20418. (setq bib (concat (match-string 1) ".bib")
  20419. rds (list (list 'bib bib)))))))
  20420. (call-interactively 'reftex-citation)))
  20421. ;;;; Functions extending outline functionality
  20422. (defun org-beginning-of-line (&optional arg)
  20423. "Go to the beginning of the current line. If that is invisible, continue
  20424. to a visible line beginning. This makes the function of C-a more intuitive.
  20425. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  20426. first attempt, and only move to after the tags when the cursor is already
  20427. beyond the end of the headline."
  20428. (interactive "P")
  20429. (let ((pos (point))
  20430. (special (if (consp org-special-ctrl-a/e)
  20431. (car org-special-ctrl-a/e)
  20432. org-special-ctrl-a/e))
  20433. refpos)
  20434. (if (org-bound-and-true-p visual-line-mode)
  20435. (beginning-of-visual-line 1)
  20436. (beginning-of-line 1))
  20437. (if (and arg (fboundp 'move-beginning-of-line))
  20438. (call-interactively 'move-beginning-of-line)
  20439. (if (bobp)
  20440. nil
  20441. (backward-char 1)
  20442. (if (org-truely-invisible-p)
  20443. (while (and (not (bobp)) (org-truely-invisible-p))
  20444. (backward-char 1)
  20445. (beginning-of-line 1))
  20446. (forward-char 1))))
  20447. (when special
  20448. (cond
  20449. ((and (looking-at org-complex-heading-regexp)
  20450. (= (char-after (match-end 1)) ?\ ))
  20451. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  20452. (point-at-eol)))
  20453. (goto-char
  20454. (if (eq special t)
  20455. (cond ((> pos refpos) refpos)
  20456. ((= pos (point)) refpos)
  20457. (t (point)))
  20458. (cond ((> pos (point)) (point))
  20459. ((not (eq last-command this-command)) (point))
  20460. (t refpos)))))
  20461. ((org-at-item-p)
  20462. ;; Being at an item and not looking at an the item means point
  20463. ;; was previously moved to beginning of a visual line, which
  20464. ;; doesn't contain the item. Therefore, do nothing special,
  20465. ;; just stay here.
  20466. (when (looking-at org-list-full-item-re)
  20467. ;; Set special position at first white space character after
  20468. ;; bullet, and check-box, if any.
  20469. (let ((after-bullet
  20470. (let ((box (match-end 3)))
  20471. (if (not box) (match-end 1)
  20472. (let ((after (char-after box)))
  20473. (if (and after (= after ? )) (1+ box) box))))))
  20474. ;; Special case: Move point to special position when
  20475. ;; currently after it or at beginning of line.
  20476. (if (eq special t)
  20477. (when (or (> pos after-bullet) (= (point) pos))
  20478. (goto-char after-bullet))
  20479. ;; Reversed case: Move point to special position when
  20480. ;; point was already at beginning of line and command is
  20481. ;; repeated.
  20482. (when (and (= (point) pos) (eq last-command this-command))
  20483. (goto-char after-bullet))))))))
  20484. (org-no-warnings
  20485. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  20486. (defun org-end-of-line (&optional arg)
  20487. "Go to the end of the line.
  20488. If this is a headline, and `org-special-ctrl-a/e' is set, ignore
  20489. tags on the first attempt, and only move to after the tags when
  20490. the cursor is already beyond the end of the headline."
  20491. (interactive "P")
  20492. (let ((special (if (consp org-special-ctrl-a/e) (cdr org-special-ctrl-a/e)
  20493. org-special-ctrl-a/e))
  20494. (move-fun (cond ((org-bound-and-true-p visual-line-mode)
  20495. 'end-of-visual-line)
  20496. ((fboundp 'move-end-of-line) 'move-end-of-line)
  20497. (t 'end-of-line))))
  20498. (if (or (not special) arg) (call-interactively move-fun)
  20499. (let* ((element (save-excursion (beginning-of-line)
  20500. (org-element-at-point)))
  20501. (type (org-element-type element)))
  20502. (cond
  20503. ((memq type '(headline inlinetask))
  20504. (let ((pos (point)))
  20505. (beginning-of-line 1)
  20506. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  20507. (if (eq special t)
  20508. (if (or (< pos (match-beginning 1)) (= pos (match-end 0)))
  20509. (goto-char (match-beginning 1))
  20510. (goto-char (match-end 0)))
  20511. (if (or (< pos (match-end 0))
  20512. (not (eq this-command last-command)))
  20513. (goto-char (match-end 0))
  20514. (goto-char (match-beginning 1))))
  20515. (call-interactively move-fun))))
  20516. ((org-element-property :hiddenp element)
  20517. ;; If element is hidden, `move-end-of-line' would put point
  20518. ;; after it. Use `end-of-line' to stay on current line.
  20519. (call-interactively 'end-of-line))
  20520. (t (call-interactively move-fun)))))
  20521. (org-no-warnings (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  20522. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  20523. (define-key org-mode-map "\C-e" 'org-end-of-line)
  20524. (defun org-backward-sentence (&optional arg)
  20525. "Go to beginning of sentence, or beginning of table field.
  20526. This will call `backward-sentence' or `org-table-beginning-of-field',
  20527. depending on context."
  20528. (interactive "P")
  20529. (cond
  20530. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  20531. (t (call-interactively 'backward-sentence))))
  20532. (defun org-forward-sentence (&optional arg)
  20533. "Go to end of sentence, or end of table field.
  20534. This will call `forward-sentence' or `org-table-end-of-field',
  20535. depending on context."
  20536. (interactive "P")
  20537. (cond
  20538. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  20539. (t (call-interactively 'forward-sentence))))
  20540. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  20541. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  20542. (defun org-kill-line (&optional arg)
  20543. "Kill line, to tags or end of line."
  20544. (interactive "P")
  20545. (cond
  20546. ((or (not org-special-ctrl-k)
  20547. (bolp)
  20548. (not (org-at-heading-p)))
  20549. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  20550. org-ctrl-k-protect-subtree)
  20551. (if (or (eq org-ctrl-k-protect-subtree 'error)
  20552. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  20553. (error "C-k aborted - would kill hidden subtree")))
  20554. (call-interactively
  20555. (if (org-bound-and-true-p visual-line-mode) 'kill-visual-line 'kill-line)))
  20556. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  20557. (kill-region (point) (match-beginning 1))
  20558. (org-set-tags nil t))
  20559. (t (kill-region (point) (point-at-eol)))))
  20560. (define-key org-mode-map "\C-k" 'org-kill-line)
  20561. (defun org-yank (&optional arg)
  20562. "Yank. If the kill is a subtree, treat it specially.
  20563. This command will look at the current kill and check if is a single
  20564. subtree, or a series of subtrees[1]. If it passes the test, and if the
  20565. cursor is at the beginning of a line or after the stars of a currently
  20566. empty headline, then the yank is handled specially. How exactly depends
  20567. on the value of the following variables, both set by default.
  20568. org-yank-folded-subtrees
  20569. When set, the subtree(s) will be folded after insertion, but only
  20570. if doing so would now swallow text after the yanked text.
  20571. org-yank-adjusted-subtrees
  20572. When set, the subtree will be promoted or demoted in order to
  20573. fit into the local outline tree structure, which means that the level
  20574. will be adjusted so that it becomes the smaller one of the two
  20575. *visible* surrounding headings.
  20576. Any prefix to this command will cause `yank' to be called directly with
  20577. no special treatment. In particular, a simple \\[universal-argument] prefix \
  20578. will just
  20579. plainly yank the text as it is.
  20580. \[1] The test checks if the first non-white line is a heading
  20581. and if there are no other headings with fewer stars."
  20582. (interactive "P")
  20583. (org-yank-generic 'yank arg))
  20584. (defun org-yank-generic (command arg)
  20585. "Perform some yank-like command.
  20586. This function implements the behavior described in the `org-yank'
  20587. documentation. However, it has been generalized to work for any
  20588. interactive command with similar behavior."
  20589. ;; pretend to be command COMMAND
  20590. (setq this-command command)
  20591. (if arg
  20592. (call-interactively command)
  20593. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  20594. (and (org-kill-is-subtree-p)
  20595. (or (bolp)
  20596. (and (looking-at "[ \t]*$")
  20597. (string-match
  20598. "\\`\\*+\\'"
  20599. (buffer-substring (point-at-bol) (point)))))))
  20600. swallowp)
  20601. (cond
  20602. ((and subtreep org-yank-folded-subtrees)
  20603. (let ((beg (point))
  20604. end)
  20605. (if (and subtreep org-yank-adjusted-subtrees)
  20606. (org-paste-subtree nil nil 'for-yank)
  20607. (call-interactively command))
  20608. (setq end (point))
  20609. (goto-char beg)
  20610. (when (and (bolp) subtreep
  20611. (not (setq swallowp
  20612. (org-yank-folding-would-swallow-text beg end))))
  20613. (org-with-limited-levels
  20614. (or (looking-at org-outline-regexp)
  20615. (re-search-forward org-outline-regexp-bol end t))
  20616. (while (and (< (point) end) (looking-at org-outline-regexp))
  20617. (hide-subtree)
  20618. (org-cycle-show-empty-lines 'folded)
  20619. (condition-case nil
  20620. (outline-forward-same-level 1)
  20621. (error (goto-char end))))))
  20622. (when swallowp
  20623. (message
  20624. "Inserted text not folded because that would swallow text"))
  20625. (goto-char end)
  20626. (skip-chars-forward " \t\n\r")
  20627. (beginning-of-line 1)
  20628. (push-mark beg 'nomsg)))
  20629. ((and subtreep org-yank-adjusted-subtrees)
  20630. (let ((beg (point-at-bol)))
  20631. (org-paste-subtree nil nil 'for-yank)
  20632. (push-mark beg 'nomsg)))
  20633. (t
  20634. (call-interactively command))))))
  20635. (defun org-yank-folding-would-swallow-text (beg end)
  20636. "Would hide-subtree at BEG swallow any text after END?"
  20637. (let (level)
  20638. (org-with-limited-levels
  20639. (save-excursion
  20640. (goto-char beg)
  20641. (when (or (looking-at org-outline-regexp)
  20642. (re-search-forward org-outline-regexp-bol end t))
  20643. (setq level (org-outline-level)))
  20644. (goto-char end)
  20645. (skip-chars-forward " \t\r\n\v\f")
  20646. (if (or (eobp)
  20647. (and (bolp) (looking-at org-outline-regexp)
  20648. (<= (org-outline-level) level)))
  20649. nil ; Nothing would be swallowed
  20650. t))))) ; something would swallow
  20651. (define-key org-mode-map "\C-y" 'org-yank)
  20652. (defun org-truely-invisible-p ()
  20653. "Check if point is at a character currently not visible.
  20654. This version does not only check the character property, but also
  20655. `visible-mode'."
  20656. ;; Early versions of noutline don't have `outline-invisible-p'.
  20657. (if (org-bound-and-true-p visible-mode)
  20658. nil
  20659. (outline-invisible-p)))
  20660. (defun org-invisible-p2 ()
  20661. "Check if point is at a character currently not visible."
  20662. (save-excursion
  20663. (if (and (eolp) (not (bobp))) (backward-char 1))
  20664. ;; Early versions of noutline don't have `outline-invisible-p'.
  20665. (outline-invisible-p)))
  20666. (defun org-back-to-heading (&optional invisible-ok)
  20667. "Call `outline-back-to-heading', but provide a better error message."
  20668. (condition-case nil
  20669. (outline-back-to-heading invisible-ok)
  20670. (error (error "Before first headline at position %d in buffer %s"
  20671. (point) (current-buffer)))))
  20672. (defun org-before-first-heading-p ()
  20673. "Before first heading?"
  20674. (save-excursion
  20675. (end-of-line)
  20676. (null (re-search-backward org-outline-regexp-bol nil t))))
  20677. (defun org-at-heading-p (&optional ignored)
  20678. (outline-on-heading-p t))
  20679. ;; Compatibility alias with Org versions < 7.8.03
  20680. (defalias 'org-on-heading-p 'org-at-heading-p)
  20681. (defun org-at-comment-p nil
  20682. "Is cursor in a line starting with a # character?"
  20683. (save-excursion
  20684. (beginning-of-line)
  20685. (looking-at "^#")))
  20686. (defun org-at-drawer-p nil
  20687. "Is cursor at a drawer keyword?"
  20688. (save-excursion
  20689. (move-beginning-of-line 1)
  20690. (looking-at org-drawer-regexp)))
  20691. (defun org-at-block-p nil
  20692. "Is cursor at a block keyword?"
  20693. (save-excursion
  20694. (move-beginning-of-line 1)
  20695. (looking-at org-block-regexp)))
  20696. (defun org-point-at-end-of-empty-headline ()
  20697. "If point is at the end of an empty headline, return t, else nil.
  20698. If the heading only contains a TODO keyword, it is still still considered
  20699. empty."
  20700. (and (looking-at "[ \t]*$")
  20701. (when org-todo-line-regexp
  20702. (save-excursion
  20703. (beginning-of-line 1)
  20704. (let ((case-fold-search nil))
  20705. (looking-at org-todo-line-regexp)
  20706. (string= (match-string 3) ""))))))
  20707. (defun org-at-heading-or-item-p ()
  20708. (or (org-at-heading-p) (org-at-item-p)))
  20709. (defun org-at-target-p ()
  20710. (or (org-in-regexp org-radio-target-regexp)
  20711. (org-in-regexp org-target-regexp)))
  20712. ;; Compatibility alias with Org versions < 7.8.03
  20713. (defalias 'org-on-target-p 'org-at-target-p)
  20714. (defun org-up-heading-all (arg)
  20715. "Move to the heading line of which the present line is a subheading.
  20716. This function considers both visible and invisible heading lines.
  20717. With argument, move up ARG levels."
  20718. (if (fboundp 'outline-up-heading-all)
  20719. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  20720. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  20721. (defun org-up-heading-safe ()
  20722. "Move to the heading line of which the present line is a subheading.
  20723. This version will not throw an error. It will return the level of the
  20724. headline found, or nil if no higher level is found.
  20725. Also, this function will be a lot faster than `outline-up-heading',
  20726. because it relies on stars being the outline starters. This can really
  20727. make a significant difference in outlines with very many siblings."
  20728. (let (start-level re)
  20729. (org-back-to-heading t)
  20730. (setq start-level (funcall outline-level))
  20731. (if (equal start-level 1)
  20732. nil
  20733. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  20734. (if (re-search-backward re nil t)
  20735. (funcall outline-level)))))
  20736. (defun org-first-sibling-p ()
  20737. "Is this heading the first child of its parents?"
  20738. (interactive)
  20739. (let ((re org-outline-regexp-bol)
  20740. level l)
  20741. (unless (org-at-heading-p t)
  20742. (error "Not at a heading"))
  20743. (setq level (funcall outline-level))
  20744. (save-excursion
  20745. (if (not (re-search-backward re nil t))
  20746. t
  20747. (setq l (funcall outline-level))
  20748. (< l level)))))
  20749. (defun org-goto-sibling (&optional previous)
  20750. "Goto the next sibling, even if it is invisible.
  20751. When PREVIOUS is set, go to the previous sibling instead. Returns t
  20752. when a sibling was found. When none is found, return nil and don't
  20753. move point."
  20754. (let ((fun (if previous 're-search-backward 're-search-forward))
  20755. (pos (point))
  20756. (re org-outline-regexp-bol)
  20757. level l)
  20758. (when (condition-case nil (org-back-to-heading t) (error nil))
  20759. (setq level (funcall outline-level))
  20760. (catch 'exit
  20761. (or previous (forward-char 1))
  20762. (while (funcall fun re nil t)
  20763. (setq l (funcall outline-level))
  20764. (when (< l level) (goto-char pos) (throw 'exit nil))
  20765. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  20766. (goto-char pos)
  20767. nil))))
  20768. (defun org-show-siblings ()
  20769. "Show all siblings of the current headline."
  20770. (save-excursion
  20771. (while (org-goto-sibling) (org-flag-heading nil)))
  20772. (save-excursion
  20773. (while (org-goto-sibling 'previous)
  20774. (org-flag-heading nil))))
  20775. (defun org-goto-first-child ()
  20776. "Goto the first child, even if it is invisible.
  20777. Return t when a child was found. Otherwise don't move point and
  20778. return nil."
  20779. (let (level (pos (point)) (re org-outline-regexp-bol))
  20780. (when (condition-case nil (org-back-to-heading t) (error nil))
  20781. (setq level (outline-level))
  20782. (forward-char 1)
  20783. (if (and (re-search-forward re nil t) (> (outline-level) level))
  20784. (progn (goto-char (match-beginning 0)) t)
  20785. (goto-char pos) nil))))
  20786. (defun org-show-hidden-entry ()
  20787. "Show an entry where even the heading is hidden."
  20788. (save-excursion
  20789. (org-show-entry)))
  20790. (defun org-flag-heading (flag &optional entry)
  20791. "Flag the current heading. FLAG non-nil means make invisible.
  20792. When ENTRY is non-nil, show the entire entry."
  20793. (save-excursion
  20794. (org-back-to-heading t)
  20795. ;; Check if we should show the entire entry
  20796. (if entry
  20797. (progn
  20798. (org-show-entry)
  20799. (save-excursion
  20800. (and (outline-next-heading)
  20801. (org-flag-heading nil))))
  20802. (outline-flag-region (max (point-min) (1- (point)))
  20803. (save-excursion (outline-end-of-heading) (point))
  20804. flag))))
  20805. (defun org-get-next-sibling ()
  20806. "Move to next heading of the same level, and return point.
  20807. If there is no such heading, return nil.
  20808. This is like outline-next-sibling, but invisible headings are ok."
  20809. (let ((level (funcall outline-level)))
  20810. (outline-next-heading)
  20811. (while (and (not (eobp)) (> (funcall outline-level) level))
  20812. (outline-next-heading))
  20813. (if (or (eobp) (< (funcall outline-level) level))
  20814. nil
  20815. (point))))
  20816. (defun org-get-last-sibling ()
  20817. "Move to previous heading of the same level, and return point.
  20818. If there is no such heading, return nil."
  20819. (let ((opoint (point))
  20820. (level (funcall outline-level)))
  20821. (outline-previous-heading)
  20822. (when (and (/= (point) opoint) (outline-on-heading-p t))
  20823. (while (and (> (funcall outline-level) level)
  20824. (not (bobp)))
  20825. (outline-previous-heading))
  20826. (if (< (funcall outline-level) level)
  20827. nil
  20828. (point)))))
  20829. (defun org-end-of-subtree (&optional invisible-ok to-heading)
  20830. "Goto to the end of a subtree."
  20831. ;; This contains an exact copy of the original function, but it uses
  20832. ;; `org-back-to-heading', to make it work also in invisible
  20833. ;; trees. And is uses an invisible-ok argument.
  20834. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  20835. ;; Furthermore, when used inside Org, finding the end of a large subtree
  20836. ;; with many children and grandchildren etc, this can be much faster
  20837. ;; than the outline version.
  20838. (org-back-to-heading invisible-ok)
  20839. (let ((first t)
  20840. (level (funcall outline-level)))
  20841. (if (and (derived-mode-p 'org-mode) (< level 1000))
  20842. ;; A true heading (not a plain list item), in Org-mode
  20843. ;; This means we can easily find the end by looking
  20844. ;; only for the right number of stars. Using a regexp to do
  20845. ;; this is so much faster than using a Lisp loop.
  20846. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  20847. (forward-char 1)
  20848. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  20849. ;; something else, do it the slow way
  20850. (while (and (not (eobp))
  20851. (or first (> (funcall outline-level) level)))
  20852. (setq first nil)
  20853. (outline-next-heading)))
  20854. (unless to-heading
  20855. (if (memq (preceding-char) '(?\n ?\^M))
  20856. (progn
  20857. ;; Go to end of line before heading
  20858. (forward-char -1)
  20859. (if (memq (preceding-char) '(?\n ?\^M))
  20860. ;; leave blank line before heading
  20861. (forward-char -1))))))
  20862. (point))
  20863. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  20864. "Use Org version in org-mode, for dramatic speed-up."
  20865. (if (derived-mode-p 'org-mode)
  20866. (progn
  20867. (org-end-of-subtree nil t)
  20868. (unless (eobp) (backward-char 1)))
  20869. ad-do-it))
  20870. (defun org-end-of-meta-data-and-drawers ()
  20871. "Jump to the first text after meta data and drawers in the current entry.
  20872. This will move over empty lines, lines with planning time stamps,
  20873. clocking lines, and drawers."
  20874. (org-back-to-heading t)
  20875. (let ((end (save-excursion (outline-next-heading) (point)))
  20876. (re (concat "\\(" org-drawer-regexp "\\)"
  20877. "\\|" "[ \t]*" org-keyword-time-regexp)))
  20878. (forward-line 1)
  20879. (while (re-search-forward re end t)
  20880. (if (not (match-end 1))
  20881. ;; empty or planning line
  20882. (forward-line 1)
  20883. ;; a drawer, find the end
  20884. (re-search-forward "^[ \t]*:END:" end 'move)
  20885. (forward-line 1)))
  20886. (and (re-search-forward "[^\n]" nil t) (backward-char 1))
  20887. (point)))
  20888. (defun org-forward-heading-same-level (arg &optional invisible-ok)
  20889. "Move forward to the ARG'th subheading at same level as this one.
  20890. Stop at the first and last subheadings of a superior heading.
  20891. Normally this only looks at visible headings, but when INVISIBLE-OK is
  20892. non-nil it will also look at invisible ones."
  20893. (interactive "p")
  20894. (if (not (ignore-errors (org-back-to-heading invisible-ok)))
  20895. (if (and arg (< arg 0))
  20896. (goto-char (point-min))
  20897. (outline-next-heading))
  20898. (org-at-heading-p)
  20899. (let ((level (- (match-end 0) (match-beginning 0) 1))
  20900. (f (if (and arg (< arg 0))
  20901. 're-search-backward
  20902. 're-search-forward))
  20903. (count (if arg (abs arg) 1))
  20904. (result (point)))
  20905. (forward-char (if (and arg (< arg 0)) -1 1))
  20906. (while (and (> count 0)
  20907. (funcall f org-outline-regexp-bol nil 'move))
  20908. (let ((l (- (match-end 0) (match-beginning 0) 1)))
  20909. (cond ((< l level) (setq count 0))
  20910. ((and (= l level)
  20911. (or invisible-ok
  20912. (progn
  20913. (goto-char (line-beginning-position))
  20914. (not (outline-invisible-p)))))
  20915. (setq count (1- count))
  20916. (when (eq l level)
  20917. (setq result (point)))))))
  20918. (goto-char result))
  20919. (beginning-of-line 1)))
  20920. (defun org-backward-heading-same-level (arg &optional invisible-ok)
  20921. "Move backward to the ARG'th subheading at same level as this one.
  20922. Stop at the first and last subheadings of a superior heading."
  20923. (interactive "p")
  20924. (org-forward-heading-same-level (if arg (- arg) -1) invisible-ok))
  20925. (defun org-next-block (arg &optional backward block-regexp)
  20926. "Jump to the next block.
  20927. With a prefix argument ARG, jump forward ARG many source blocks.
  20928. When BACKWARD is non-nil, jump to the previous block.
  20929. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  20930. (interactive "p")
  20931. (let ((re (or block-regexp org-block-regexp))
  20932. (re-search-fn (or (and backward 're-search-backward)
  20933. 're-search-forward)))
  20934. (if (looking-at re) (forward-char 1))
  20935. (condition-case nil
  20936. (funcall re-search-fn re nil nil arg)
  20937. (error (error "No %s code blocks" (if backward "previous" "further" ))))
  20938. (goto-char (match-beginning 0)) (org-show-context)))
  20939. (defun org-previous-block (arg &optional block-regexp)
  20940. "Jump to the previous block.
  20941. With a prefix argument ARG, jump backward ARG many source blocks.
  20942. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  20943. (interactive "p")
  20944. (org-next-block arg t block-regexp))
  20945. (defun org-forward-element ()
  20946. "Move forward by one element.
  20947. Move to the next element at the same level, when possible."
  20948. (interactive)
  20949. (cond ((eobp) (user-error "Cannot move further down"))
  20950. ((org-with-limited-levels (org-at-heading-p))
  20951. (let ((origin (point)))
  20952. (goto-char (org-end-of-subtree nil t))
  20953. (unless (org-with-limited-levels (org-at-heading-p))
  20954. (goto-char origin)
  20955. (user-error "Cannot move further down"))))
  20956. (t
  20957. (let* ((elem (org-element-at-point))
  20958. (end (org-element-property :end elem))
  20959. (parent (org-element-property :parent elem)))
  20960. (if (and parent (= (org-element-property :contents-end parent) end))
  20961. (goto-char (org-element-property :end parent))
  20962. (goto-char end))))))
  20963. (defun org-backward-element ()
  20964. "Move backward by one element.
  20965. Move to the previous element at the same level, when possible."
  20966. (interactive)
  20967. (cond ((bobp) (user-error "Cannot move further up"))
  20968. ((org-with-limited-levels (org-at-heading-p))
  20969. ;; At a headline, move to the previous one, if any, or stay
  20970. ;; here.
  20971. (let ((origin (point)))
  20972. (org-with-limited-levels (org-backward-heading-same-level 1))
  20973. ;; When current headline has no sibling above, move to its
  20974. ;; parent.
  20975. (when (= (point) origin)
  20976. (or (org-with-limited-levels (org-up-heading-safe))
  20977. (progn (goto-char origin)
  20978. (user-error "Cannot move further up"))))))
  20979. (t
  20980. (let* ((trail (org-element-at-point 'keep-trail))
  20981. (elem (car trail))
  20982. (prev-elem (nth 1 trail))
  20983. (beg (org-element-property :begin elem)))
  20984. (cond
  20985. ;; Move to beginning of current element if point isn't
  20986. ;; there already.
  20987. ((/= (point) beg) (goto-char beg))
  20988. (prev-elem (goto-char (org-element-property :begin prev-elem)))
  20989. ((org-before-first-heading-p) (goto-char (point-min)))
  20990. (t (org-back-to-heading)))))))
  20991. (defun org-up-element ()
  20992. "Move to upper element."
  20993. (interactive)
  20994. (if (org-with-limited-levels (org-at-heading-p))
  20995. (unless (org-up-heading-safe) (error "No surrounding element"))
  20996. (let* ((elem (org-element-at-point))
  20997. (parent (org-element-property :parent elem)))
  20998. (if parent (goto-char (org-element-property :begin parent))
  20999. (if (org-with-limited-levels (org-before-first-heading-p))
  21000. (error "No surrounding element")
  21001. (org-with-limited-levels (org-back-to-heading)))))))
  21002. (defvar org-element-greater-elements)
  21003. (defun org-down-element ()
  21004. "Move to inner element."
  21005. (interactive)
  21006. (let ((element (org-element-at-point)))
  21007. (cond
  21008. ((memq (org-element-type element) '(plain-list table))
  21009. (goto-char (org-element-property :contents-begin element))
  21010. (forward-char))
  21011. ((memq (org-element-type element) org-element-greater-elements)
  21012. ;; If contents are hidden, first disclose them.
  21013. (when (org-element-property :hiddenp element) (org-cycle))
  21014. (goto-char (or (org-element-property :contents-begin element)
  21015. (error "No content for this element"))))
  21016. (t (error "No inner element")))))
  21017. (defun org-drag-element-backward ()
  21018. "Move backward element at point."
  21019. (interactive)
  21020. (if (org-with-limited-levels (org-at-heading-p)) (org-move-subtree-up)
  21021. (let* ((trail (org-element-at-point 'keep-trail))
  21022. (elem (car trail))
  21023. (prev-elem (nth 1 trail)))
  21024. ;; Error out if no previous element or previous element is
  21025. ;; a parent of the current one.
  21026. (if (or (not prev-elem) (org-element-nested-p elem prev-elem))
  21027. (error "Cannot drag element backward")
  21028. (let ((pos (point)))
  21029. (org-element-swap-A-B prev-elem elem)
  21030. (goto-char (+ (org-element-property :begin prev-elem)
  21031. (- pos (org-element-property :begin elem)))))))))
  21032. (defun org-drag-element-forward ()
  21033. "Move forward element at point."
  21034. (interactive)
  21035. (let* ((pos (point))
  21036. (elem (org-element-at-point)))
  21037. (when (= (point-max) (org-element-property :end elem))
  21038. (error "Cannot drag element forward"))
  21039. (goto-char (org-element-property :end elem))
  21040. (let ((next-elem (org-element-at-point)))
  21041. (when (or (org-element-nested-p elem next-elem)
  21042. (and (eq (org-element-type next-elem) 'headline)
  21043. (not (eq (org-element-type elem) 'headline))))
  21044. (goto-char pos)
  21045. (error "Cannot drag element forward"))
  21046. ;; Compute new position of point: it's shifted by NEXT-ELEM
  21047. ;; body's length (without final blanks) and by the length of
  21048. ;; blanks between ELEM and NEXT-ELEM.
  21049. (let ((size-next (- (save-excursion
  21050. (goto-char (org-element-property :end next-elem))
  21051. (skip-chars-backward " \r\t\n")
  21052. (forward-line)
  21053. ;; Small correction if buffer doesn't end
  21054. ;; with a newline character.
  21055. (if (and (eolp) (not (bolp))) (1+ (point)) (point)))
  21056. (org-element-property :begin next-elem)))
  21057. (size-blank (- (org-element-property :end elem)
  21058. (save-excursion
  21059. (goto-char (org-element-property :end elem))
  21060. (skip-chars-backward " \r\t\n")
  21061. (forward-line)
  21062. (point)))))
  21063. (org-element-swap-A-B elem next-elem)
  21064. (goto-char (+ pos size-next size-blank))))))
  21065. (defun org-drag-line-forward (arg)
  21066. "Drag the line at point ARG lines forward."
  21067. (interactive "p")
  21068. (dotimes (n (abs arg))
  21069. (let ((c (current-column)))
  21070. (if (< 0 arg)
  21071. (progn
  21072. (beginning-of-line 2)
  21073. (transpose-lines 1)
  21074. (beginning-of-line 0))
  21075. (transpose-lines 1)
  21076. (beginning-of-line -1))
  21077. (org-move-to-column c))))
  21078. (defun org-drag-line-backward (arg)
  21079. "Drag the line at point ARG lines backward."
  21080. (interactive "p")
  21081. (org-drag-line-forward (- arg)))
  21082. (defun org-mark-element ()
  21083. "Put point at beginning of this element, mark at end.
  21084. Interactively, if this command is repeated or (in Transient Mark
  21085. mode) if the mark is active, it marks the next element after the
  21086. ones already marked."
  21087. (interactive)
  21088. (let (deactivate-mark)
  21089. (if (and (org-called-interactively-p 'any)
  21090. (or (and (eq last-command this-command) (mark t))
  21091. (and transient-mark-mode mark-active)))
  21092. (set-mark
  21093. (save-excursion
  21094. (goto-char (mark))
  21095. (goto-char (org-element-property :end (org-element-at-point)))))
  21096. (let ((element (org-element-at-point)))
  21097. (end-of-line)
  21098. (push-mark (org-element-property :end element) t t)
  21099. (goto-char (org-element-property :begin element))))))
  21100. (defun org-narrow-to-element ()
  21101. "Narrow buffer to current element."
  21102. (interactive)
  21103. (let ((elem (org-element-at-point)))
  21104. (cond
  21105. ((eq (car elem) 'headline)
  21106. (narrow-to-region
  21107. (org-element-property :begin elem)
  21108. (org-element-property :end elem)))
  21109. ((memq (car elem) org-element-greater-elements)
  21110. (narrow-to-region
  21111. (org-element-property :contents-begin elem)
  21112. (org-element-property :contents-end elem)))
  21113. (t
  21114. (narrow-to-region
  21115. (org-element-property :begin elem)
  21116. (org-element-property :end elem))))))
  21117. (defun org-transpose-element ()
  21118. "Transpose current and previous elements, keeping blank lines between.
  21119. Point is moved after both elements."
  21120. (interactive)
  21121. (org-skip-whitespace)
  21122. (let ((end (org-element-property :end (org-element-at-point))))
  21123. (org-drag-element-backward)
  21124. (goto-char end)))
  21125. (defun org-unindent-buffer ()
  21126. "Un-indent the visible part of the buffer.
  21127. Relative indentation (between items, inside blocks, etc.) isn't
  21128. modified."
  21129. (interactive)
  21130. (unless (eq major-mode 'org-mode)
  21131. (error "Cannot un-indent a buffer not in Org mode"))
  21132. (let* ((parse-tree (org-element-parse-buffer 'greater-element))
  21133. unindent-tree ; For byte-compiler.
  21134. (unindent-tree
  21135. (function
  21136. (lambda (contents)
  21137. (mapc
  21138. (lambda (element)
  21139. (if (memq (org-element-type element) '(headline section))
  21140. (funcall unindent-tree (org-element-contents element))
  21141. (save-excursion
  21142. (save-restriction
  21143. (narrow-to-region
  21144. (org-element-property :begin element)
  21145. (org-element-property :end element))
  21146. (org-do-remove-indentation)))))
  21147. (reverse contents))))))
  21148. (funcall unindent-tree (org-element-contents parse-tree))))
  21149. (defun org-show-subtree ()
  21150. "Show everything after this heading at deeper levels."
  21151. (interactive)
  21152. (outline-flag-region
  21153. (point)
  21154. (save-excursion
  21155. (org-end-of-subtree t t))
  21156. nil))
  21157. (defun org-show-entry ()
  21158. "Show the body directly following this heading.
  21159. Show the heading too, if it is currently invisible."
  21160. (interactive)
  21161. (save-excursion
  21162. (condition-case nil
  21163. (progn
  21164. (org-back-to-heading t)
  21165. (outline-flag-region
  21166. (max (point-min) (1- (point)))
  21167. (save-excursion
  21168. (if (re-search-forward
  21169. (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
  21170. (match-beginning 1)
  21171. (point-max)))
  21172. nil)
  21173. (org-cycle-hide-drawers 'children))
  21174. (error nil))))
  21175. (defun org-make-options-regexp (kwds &optional extra)
  21176. "Make a regular expression for keyword lines."
  21177. (concat
  21178. "^#\\+\\("
  21179. (mapconcat 'regexp-quote kwds "\\|")
  21180. (if extra (concat "\\|" extra))
  21181. "\\):[ \t]*\\(.*\\)"))
  21182. ;; Make isearch reveal the necessary context
  21183. (defun org-isearch-end ()
  21184. "Reveal context after isearch exits."
  21185. (when isearch-success ; only if search was successful
  21186. (if (featurep 'xemacs)
  21187. ;; Under XEmacs, the hook is run in the correct place,
  21188. ;; we directly show the context.
  21189. (org-show-context 'isearch)
  21190. ;; In Emacs the hook runs *before* restoring the overlays.
  21191. ;; So we have to use a one-time post-command-hook to do this.
  21192. ;; (Emacs 22 has a special variable, see function `org-mode')
  21193. (unless (and (boundp 'isearch-mode-end-hook-quit)
  21194. isearch-mode-end-hook-quit)
  21195. ;; Only when the isearch was not quitted.
  21196. (org-add-hook 'post-command-hook 'org-isearch-post-command
  21197. 'append 'local)))
  21198. (org-fix-ellipsis-at-bol)))
  21199. (defun org-isearch-post-command ()
  21200. "Remove self from hook, and show context."
  21201. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  21202. (org-show-context 'isearch))
  21203. ;;;; Integration with and fixes for other packages
  21204. ;;; Imenu support
  21205. (defvar org-imenu-markers nil
  21206. "All markers currently used by Imenu.")
  21207. (make-variable-buffer-local 'org-imenu-markers)
  21208. (defun org-imenu-new-marker (&optional pos)
  21209. "Return a new marker for use by Imenu, and remember the marker."
  21210. (let ((m (make-marker)))
  21211. (move-marker m (or pos (point)))
  21212. (push m org-imenu-markers)
  21213. m))
  21214. (defun org-imenu-get-tree ()
  21215. "Produce the index for Imenu."
  21216. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  21217. (setq org-imenu-markers nil)
  21218. (let* ((n org-imenu-depth)
  21219. (re (concat "^" (org-get-limited-outline-regexp)))
  21220. (subs (make-vector (1+ n) nil))
  21221. (last-level 0)
  21222. m level head0 head)
  21223. (save-excursion
  21224. (save-restriction
  21225. (widen)
  21226. (goto-char (point-max))
  21227. (while (re-search-backward re nil t)
  21228. (setq level (org-reduced-level (funcall outline-level)))
  21229. (when (and (<= level n)
  21230. (looking-at org-complex-heading-regexp)
  21231. (setq head0 (org-match-string-no-properties 4)))
  21232. (setq head (org-link-display-format head0)
  21233. m (org-imenu-new-marker))
  21234. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  21235. (if (>= level last-level)
  21236. (push (cons head m) (aref subs level))
  21237. (push (cons head (aref subs (1+ level))) (aref subs level))
  21238. (loop for i from (1+ level) to n do (aset subs i nil)))
  21239. (setq last-level level)))))
  21240. (aref subs 1)))
  21241. (eval-after-load "imenu"
  21242. '(progn
  21243. (add-hook 'imenu-after-jump-hook
  21244. (lambda ()
  21245. (if (derived-mode-p 'org-mode)
  21246. (org-show-context 'org-goto))))))
  21247. (defun org-link-display-format (link)
  21248. "Replace a link with either the description, or the link target
  21249. if no description is present"
  21250. (save-match-data
  21251. (if (string-match org-bracket-link-analytic-regexp link)
  21252. (replace-match (if (match-end 5)
  21253. (match-string 5 link)
  21254. (concat (match-string 1 link)
  21255. (match-string 3 link)))
  21256. nil t link)
  21257. link)))
  21258. (defun org-toggle-link-display ()
  21259. "Toggle the literal or descriptive display of links."
  21260. (interactive)
  21261. (if org-descriptive-links
  21262. (progn (org-remove-from-invisibility-spec '(org-link))
  21263. (org-restart-font-lock)
  21264. (setq org-descriptive-links nil))
  21265. (progn (add-to-invisibility-spec '(org-link))
  21266. (org-restart-font-lock)
  21267. (setq org-descriptive-links t))))
  21268. ;; Speedbar support
  21269. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  21270. "Overlay marking the agenda restriction line in speedbar.")
  21271. (overlay-put org-speedbar-restriction-lock-overlay
  21272. 'face 'org-agenda-restriction-lock)
  21273. (overlay-put org-speedbar-restriction-lock-overlay
  21274. 'help-echo "Agendas are currently limited to this item.")
  21275. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  21276. (defun org-speedbar-set-agenda-restriction ()
  21277. "Restrict future agenda commands to the location at point in speedbar.
  21278. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  21279. (interactive)
  21280. (require 'org-agenda)
  21281. (let (p m tp np dir txt)
  21282. (cond
  21283. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  21284. 'org-imenu t))
  21285. (setq m (get-text-property p 'org-imenu-marker))
  21286. (with-current-buffer (marker-buffer m)
  21287. (goto-char m)
  21288. (org-agenda-set-restriction-lock 'subtree)))
  21289. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  21290. 'speedbar-function 'speedbar-find-file))
  21291. (setq tp (previous-single-property-change
  21292. (1+ p) 'speedbar-function)
  21293. np (next-single-property-change
  21294. tp 'speedbar-function)
  21295. dir (speedbar-line-directory)
  21296. txt (buffer-substring-no-properties (or tp (point-min))
  21297. (or np (point-max))))
  21298. (with-current-buffer (find-file-noselect
  21299. (let ((default-directory dir))
  21300. (expand-file-name txt)))
  21301. (unless (derived-mode-p 'org-mode)
  21302. (error "Cannot restrict to non-Org-mode file"))
  21303. (org-agenda-set-restriction-lock 'file)))
  21304. (t (error "Don't know how to restrict Org-mode's agenda")))
  21305. (move-overlay org-speedbar-restriction-lock-overlay
  21306. (point-at-bol) (point-at-eol))
  21307. (setq current-prefix-arg nil)
  21308. (org-agenda-maybe-redo)))
  21309. (eval-after-load "speedbar"
  21310. '(progn
  21311. (speedbar-add-supported-extension ".org")
  21312. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  21313. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  21314. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  21315. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  21316. (add-hook 'speedbar-visiting-tag-hook
  21317. (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
  21318. ;;; Fixes and Hacks for problems with other packages
  21319. ;; Make flyspell not check words in links, to not mess up our keymap
  21320. (defvar org-element-affiliated-keywords) ; From org-element.el
  21321. (defvar org-element-block-name-alist) ; From org-element.el
  21322. (defun org-mode-flyspell-verify ()
  21323. "Don't let flyspell put overlays at active buttons, or on
  21324. {todo,all-time,additional-option-like}-keywords."
  21325. (let ((pos (max (1- (point)) (point-min)))
  21326. (word (thing-at-point 'word)))
  21327. (and (not (get-text-property pos 'keymap))
  21328. (not (get-text-property pos 'org-no-flyspell))
  21329. (not (member word org-todo-keywords-1))
  21330. (not (member word org-all-time-keywords))
  21331. (not (member word org-options-keywords))
  21332. (not (member word (mapcar 'car org-startup-options)))
  21333. (not (member-ignore-case word org-element-affiliated-keywords))
  21334. (not (member-ignore-case word (org-get-export-keywords)))
  21335. (not (member-ignore-case
  21336. word (mapcar 'car org-element-block-name-alist)))
  21337. (not (member-ignore-case word '("BEGIN" "END" "ATTR"))))))
  21338. (defun org-remove-flyspell-overlays-in (beg end)
  21339. "Remove flyspell overlays in region."
  21340. (and (org-bound-and-true-p flyspell-mode)
  21341. (fboundp 'flyspell-delete-region-overlays)
  21342. (flyspell-delete-region-overlays beg end))
  21343. (add-text-properties beg end '(org-no-flyspell t)))
  21344. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  21345. (eval-after-load "bookmark"
  21346. '(if (boundp 'bookmark-after-jump-hook)
  21347. ;; We can use the hook
  21348. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  21349. ;; Hook not available, use advice
  21350. (defadvice bookmark-jump (after org-make-visible activate)
  21351. "Make the position visible."
  21352. (org-bookmark-jump-unhide))))
  21353. ;; Make sure saveplace shows the location if it was hidden
  21354. (eval-after-load "saveplace"
  21355. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  21356. "Make the position visible."
  21357. (org-bookmark-jump-unhide)))
  21358. ;; Make sure ecb shows the location if it was hidden
  21359. (eval-after-load "ecb"
  21360. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  21361. "Make hierarchy visible when jumping into location from ECB tree buffer."
  21362. (if (derived-mode-p 'org-mode)
  21363. (org-show-context))))
  21364. (defun org-bookmark-jump-unhide ()
  21365. "Unhide the current position, to show the bookmark location."
  21366. (and (derived-mode-p 'org-mode)
  21367. (or (outline-invisible-p)
  21368. (save-excursion (goto-char (max (point-min) (1- (point))))
  21369. (outline-invisible-p)))
  21370. (org-show-context 'bookmark-jump)))
  21371. ;; Make session.el ignore our circular variable
  21372. (eval-after-load "session"
  21373. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  21374. ;;;; Experimental code
  21375. (defun org-closed-in-range ()
  21376. "Sparse tree of items closed in a certain time range.
  21377. Still experimental, may disappear in the future."
  21378. (interactive)
  21379. ;; Get the time interval from the user.
  21380. (let* ((time1 (org-float-time
  21381. (org-read-date nil 'to-time nil "Starting date: ")))
  21382. (time2 (org-float-time
  21383. (org-read-date nil 'to-time nil "End date:")))
  21384. ;; callback function
  21385. (callback (lambda ()
  21386. (let ((time
  21387. (org-float-time
  21388. (apply 'encode-time
  21389. (org-parse-time-string
  21390. (match-string 1))))))
  21391. ;; check if time in interval
  21392. (and (>= time time1) (<= time time2))))))
  21393. ;; make tree, check each match with the callback
  21394. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  21395. ;;;; Finish up
  21396. (provide 'org)
  21397. (run-hooks 'org-load-hook)
  21398. ;;; org.el ends here