org.el 883 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680216812168221683216842168521686216872168821689216902169121692216932169421695216962169721698216992170021701217022170321704217052170621707217082170921710217112171221713217142171521716217172171821719217202172121722217232172421725217262172721728217292173021731217322173321734217352173621737217382173921740217412174221743217442174521746217472174821749217502175121752217532175421755217562175721758217592176021761217622176321764217652176621767217682176921770217712177221773217742177521776217772177821779217802178121782217832178421785217862178721788217892179021791217922179321794217952179621797217982179921800218012180221803218042180521806218072180821809218102181121812218132181421815218162181721818218192182021821218222182321824218252182621827218282182921830218312183221833218342183521836218372183821839218402184121842218432184421845218462184721848218492185021851218522185321854218552185621857218582185921860218612186221863218642186521866218672186821869218702187121872218732187421875218762187721878218792188021881218822188321884218852188621887218882188921890218912189221893218942189521896218972189821899219002190121902219032190421905219062190721908219092191021911219122191321914219152191621917219182191921920219212192221923219242192521926219272192821929219302193121932219332193421935219362193721938219392194021941219422194321944219452194621947219482194921950219512195221953219542195521956219572195821959219602196121962219632196421965219662196721968219692197021971219722197321974219752197621977219782197921980219812198221983219842198521986219872198821989219902199121992219932199421995219962199721998219992200022001220022200322004220052200622007220082200922010220112201222013220142201522016220172201822019220202202122022220232202422025220262202722028220292203022031220322203322034220352203622037220382203922040220412204222043220442204522046220472204822049220502205122052220532205422055220562205722058220592206022061220622206322064220652206622067220682206922070220712207222073220742207522076220772207822079220802208122082220832208422085220862208722088220892209022091220922209322094220952209622097220982209922100221012210222103221042210522106221072210822109221102211122112221132211422115221162211722118221192212022121221222212322124221252212622127221282212922130221312213222133221342213522136221372213822139221402214122142221432214422145221462214722148221492215022151221522215322154221552215622157221582215922160221612216222163221642216522166221672216822169221702217122172221732217422175221762217722178221792218022181221822218322184221852218622187221882218922190221912219222193221942219522196221972219822199222002220122202222032220422205222062220722208222092221022211222122221322214222152221622217222182221922220222212222222223222242222522226222272222822229222302223122232222332223422235222362223722238222392224022241222422224322244222452224622247222482224922250222512225222253222542225522256222572225822259222602226122262222632226422265222662226722268222692227022271222722227322274222752227622277222782227922280222812228222283222842228522286222872228822289222902229122292222932229422295222962229722298222992230022301223022230322304223052230622307223082230922310223112231222313223142231522316223172231822319223202232122322223232232422325223262232722328223292233022331223322233322334223352233622337223382233922340223412234222343223442234522346223472234822349223502235122352223532235422355223562235722358223592236022361223622236322364223652236622367223682236922370223712237222373223742237522376223772237822379223802238122382223832238422385223862238722388223892239022391223922239322394223952239622397223982239922400224012240222403224042240522406224072240822409224102241122412224132241422415224162241722418224192242022421224222242322424224252242622427224282242922430224312243222433224342243522436224372243822439224402244122442224432244422445224462244722448224492245022451224522245322454224552245622457224582245922460224612246222463224642246522466224672246822469224702247122472224732247422475224762247722478224792248022481224822248322484224852248622487224882248922490224912249222493224942249522496224972249822499225002250122502225032250422505225062250722508225092251022511225122251322514225152251622517225182251922520225212252222523225242252522526225272252822529225302253122532225332253422535225362253722538225392254022541225422254322544225452254622547225482254922550225512255222553225542255522556225572255822559225602256122562225632256422565225662256722568225692257022571225722257322574225752257622577225782257922580225812258222583225842258522586225872258822589225902259122592225932259422595225962259722598225992260022601226022260322604226052260622607226082260922610226112261222613226142261522616226172261822619226202262122622226232262422625226262262722628226292263022631226322263322634226352263622637226382263922640226412264222643226442264522646226472264822649226502265122652226532265422655226562265722658226592266022661226622266322664226652266622667226682266922670226712267222673226742267522676226772267822679226802268122682226832268422685226862268722688226892269022691226922269322694226952269622697226982269922700227012270222703227042270522706227072270822709227102271122712227132271422715227162271722718227192272022721227222272322724227252272622727227282272922730227312273222733227342273522736227372273822739227402274122742227432274422745227462274722748227492275022751227522275322754227552275622757227582275922760227612276222763227642276522766227672276822769227702277122772227732277422775227762277722778227792278022781227822278322784227852278622787227882278922790227912279222793227942279522796227972279822799228002280122802228032280422805228062280722808228092281022811228122281322814228152281622817228182281922820228212282222823228242282522826228272282822829228302283122832228332283422835228362283722838228392284022841228422284322844228452284622847228482284922850228512285222853228542285522856228572285822859228602286122862228632286422865228662286722868228692287022871228722287322874228752287622877228782287922880228812288222883228842288522886228872288822889228902289122892228932289422895228962289722898228992290022901229022290322904229052290622907229082290922910229112291222913229142291522916229172291822919229202292122922229232292422925229262292722928229292293022931229322293322934229352293622937229382293922940229412294222943229442294522946229472294822949229502295122952229532295422955229562295722958229592296022961229622296322964229652296622967229682296922970229712297222973229742297522976229772297822979229802298122982229832298422985229862298722988229892299022991229922299322994229952299622997229982299923000230012300223003230042300523006230072300823009230102301123012230132301423015230162301723018230192302023021230222302323024230252302623027230282302923030230312303223033230342303523036230372303823039230402304123042230432304423045230462304723048230492305023051230522305323054230552305623057230582305923060230612306223063230642306523066230672306823069230702307123072230732307423075230762307723078230792308023081230822308323084230852308623087230882308923090230912309223093230942309523096230972309823099231002310123102231032310423105231062310723108231092311023111231122311323114231152311623117231182311923120231212312223123231242312523126231272312823129231302313123132231332313423135231362313723138231392314023141231422314323144231452314623147231482314923150231512315223153231542315523156231572315823159231602316123162231632316423165231662316723168231692317023171231722317323174231752317623177231782317923180231812318223183231842318523186231872318823189231902319123192231932319423195231962319723198231992320023201232022320323204232052320623207232082320923210232112321223213232142321523216232172321823219232202322123222232232322423225232262322723228232292323023231232322323323234232352323623237232382323923240232412324223243232442324523246232472324823249232502325123252232532325423255232562325723258232592326023261232622326323264232652326623267232682326923270232712327223273232742327523276232772327823279232802328123282232832328423285232862328723288232892329023291232922329323294232952329623297232982329923300233012330223303233042330523306233072330823309233102331123312233132331423315233162331723318233192332023321233222332323324233252332623327233282332923330233312333223333
  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 deck Export buffer to deck.js presentations" deck)
  453. (const :tag "C freemind Export buffer to Freemind mindmap format" freemind)
  454. (const :tag "C groff Export buffer to Groff format" groff)
  455. (const :tag "C koma-letter Export buffer to KOMA Scrlttrl2 format" koma-letter)
  456. (const :tag "C RSS 2.0 Export buffer to RSS 2.0 format" rss)
  457. (const :tag "C s5 Export buffer to s5 presentations" s5)
  458. (const :tag "C taskjuggler Export buffer to TaskJuggler format" taskjuggler)))
  459. (eval-after-load 'ox
  460. '(mapc
  461. (lambda (backend)
  462. (condition-case nil (require (intern (format "ox-%s" backend)))
  463. (error (message "Problems while trying to load export back-end `%s'"
  464. backend))))
  465. org-export-backends))
  466. (defcustom org-support-shift-select nil
  467. "Non-nil means make shift-cursor commands select text when possible.
  468. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
  469. start selecting a region, or enlarge regions started in this way.
  470. In Org-mode, in special contexts, these same keys are used for
  471. other purposes, important enough to compete with shift selection.
  472. Org tries to balance these needs by supporting `shift-select-mode'
  473. outside these special contexts, under control of this variable.
  474. The default of this variable is nil, to avoid confusing behavior. Shifted
  475. cursor keys will then execute Org commands in the following contexts:
  476. - on a headline, changing TODO state (left/right) and priority (up/down)
  477. - on a time stamp, changing the time
  478. - in a plain list item, changing the bullet type
  479. - in a property definition line, switching between allowed values
  480. - in the BEGIN line of a clock table (changing the time block).
  481. Outside these contexts, the commands will throw an error.
  482. When this variable is t and the cursor is not in a special
  483. context, Org-mode will support shift-selection for making and
  484. enlarging regions. To make this more effective, the bullet
  485. cycling will no longer happen anywhere in an item line, but only
  486. if the cursor is exactly on the bullet.
  487. If you set this variable to the symbol `always', then the keys
  488. will not be special in headlines, property lines, and item lines,
  489. to make shift selection work there as well. If this is what you
  490. want, you can use the following alternative commands: `C-c C-t'
  491. and `C-c ,' to change TODO state and priority, `C-u C-u C-c C-t'
  492. can be used to switch TODO sets, `C-c -' to cycle item bullet
  493. types, and properties can be edited by hand or in column view.
  494. However, when the cursor is on a timestamp, shift-cursor commands
  495. will still edit the time stamp - this is just too good to give up.
  496. XEmacs user should have this variable set to nil, because
  497. `shift-select-mode' is in Emacs 23 or later only."
  498. :group 'org
  499. :type '(choice
  500. (const :tag "Never" nil)
  501. (const :tag "When outside special context" t)
  502. (const :tag "Everywhere except timestamps" always)))
  503. (defcustom org-loop-over-headlines-in-active-region nil
  504. "Shall some commands act upon headlines in the active region?
  505. When set to `t', some commands will be performed in all headlines
  506. within the active region.
  507. When set to `start-level', some commands will be performed in all
  508. headlines within the active region, provided that these headlines
  509. are of the same level than the first one.
  510. When set to a string, those commands will be performed on the
  511. matching headlines within the active region. Such string must be
  512. a tags/property/todo match as it is used in the agenda tags view.
  513. The list of commands is: `org-schedule', `org-deadline',
  514. `org-todo', `org-archive-subtree', `org-archive-set-tag' and
  515. `org-archive-to-archive-sibling'. The archiving commands skip
  516. already archived entries."
  517. :type '(choice (const :tag "Don't loop" nil)
  518. (const :tag "All headlines in active region" t)
  519. (const :tag "In active region, headlines at the same level than the first one" 'start-level)
  520. (string :tag "Tags/Property/Todo matcher"))
  521. :version "24.1"
  522. :group 'org-todo
  523. :group 'org-archive)
  524. (defgroup org-startup nil
  525. "Options concerning startup of Org-mode."
  526. :tag "Org Startup"
  527. :group 'org)
  528. (defcustom org-startup-folded t
  529. "Non-nil means entering Org-mode will switch to OVERVIEW.
  530. This can also be configured on a per-file basis by adding one of
  531. the following lines anywhere in the buffer:
  532. #+STARTUP: fold (or `overview', this is equivalent)
  533. #+STARTUP: nofold (or `showall', this is equivalent)
  534. #+STARTUP: content
  535. #+STARTUP: showeverything
  536. By default, this option is ignored when Org opens agenda files
  537. for the first time. If you want the agenda to honor the startup
  538. option, set `org-agenda-inhibit-startup' to nil."
  539. :group 'org-startup
  540. :type '(choice
  541. (const :tag "nofold: show all" nil)
  542. (const :tag "fold: overview" t)
  543. (const :tag "content: all headlines" content)
  544. (const :tag "show everything, even drawers" showeverything)))
  545. (defcustom org-startup-truncated t
  546. "Non-nil means entering Org-mode will set `truncate-lines'.
  547. This is useful since some lines containing links can be very long and
  548. uninteresting. Also tables look terrible when wrapped."
  549. :group 'org-startup
  550. :type 'boolean)
  551. (defcustom org-startup-indented nil
  552. "Non-nil means turn on `org-indent-mode' on startup.
  553. This can also be configured on a per-file basis by adding one of
  554. the following lines anywhere in the buffer:
  555. #+STARTUP: indent
  556. #+STARTUP: noindent"
  557. :group 'org-structure
  558. :type '(choice
  559. (const :tag "Not" nil)
  560. (const :tag "Globally (slow on startup in large files)" t)))
  561. (defcustom org-use-sub-superscripts t
  562. "Non-nil means interpret \"_\" and \"^\" for display.
  563. When this option is turned on, you can use TeX-like syntax for sub- and
  564. superscripts. Several characters after \"_\" or \"^\" will be
  565. considered as a single item - so grouping with {} is normally not
  566. needed. For example, the following things will be parsed as single
  567. sub- or superscripts.
  568. 10^24 or 10^tau several digits will be considered 1 item.
  569. 10^-12 or 10^-tau a leading sign with digits or a word
  570. x^2-y^3 will be read as x^2 - y^3, because items are
  571. terminated by almost any nonword/nondigit char.
  572. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  573. Still, ambiguity is possible - so when in doubt use {} to enclose
  574. the sub/superscript. If you set this variable to the symbol
  575. `{}', the braces are *required* in order to trigger
  576. interpretations as sub/superscript. This can be helpful in
  577. documents that need \"_\" frequently in plain text."
  578. :group 'org-startup
  579. :version "24.1"
  580. :type '(choice
  581. (const :tag "Always interpret" t)
  582. (const :tag "Only with braces" {})
  583. (const :tag "Never interpret" nil)))
  584. (defcustom org-startup-with-beamer-mode nil
  585. "Non-nil means turn on `org-beamer-mode' on startup.
  586. This can also be configured on a per-file basis by adding one of
  587. the following lines anywhere in the buffer:
  588. #+STARTUP: beamer"
  589. :group 'org-startup
  590. :version "24.1"
  591. :type 'boolean)
  592. (defcustom org-startup-align-all-tables nil
  593. "Non-nil means align all tables when visiting a file.
  594. This is useful when the column width in tables is forced with <N> cookies
  595. in table fields. Such tables will look correct only after the first re-align.
  596. This can also be configured on a per-file basis by adding one of
  597. the following lines anywhere in the buffer:
  598. #+STARTUP: align
  599. #+STARTUP: noalign"
  600. :group 'org-startup
  601. :type 'boolean)
  602. (defcustom org-startup-with-inline-images nil
  603. "Non-nil means show inline images when loading a new Org file.
  604. This can also be configured on a per-file basis by adding one of
  605. the following lines anywhere in the buffer:
  606. #+STARTUP: inlineimages
  607. #+STARTUP: noinlineimages"
  608. :group 'org-startup
  609. :version "24.1"
  610. :type 'boolean)
  611. (defcustom org-startup-with-latex-preview nil
  612. "Non-nil means preview LaTeX fragments when loading a new Org file.
  613. This can also be configured on a per-file basis by adding one of
  614. the followinglines anywhere in the buffer:
  615. #+STARTUP: latexpreview
  616. #+STARTUP: nolatexpreview"
  617. :group 'org-startup
  618. :version "24.4"
  619. :package-version '(Org . "8.0")
  620. :type 'boolean)
  621. (defcustom org-insert-mode-line-in-empty-file nil
  622. "Non-nil means insert the first line setting Org-mode in empty files.
  623. When the function `org-mode' is called interactively in an empty file, this
  624. normally means that the file name does not automatically trigger Org-mode.
  625. To ensure that the file will always be in Org-mode in the future, a
  626. line enforcing Org-mode will be inserted into the buffer, if this option
  627. has been set."
  628. :group 'org-startup
  629. :type 'boolean)
  630. (defcustom org-replace-disputed-keys nil
  631. "Non-nil means use alternative key bindings for some keys.
  632. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  633. These keys are also used by other packages like shift-selection-mode'
  634. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  635. If you want to use Org-mode together with one of these other modes,
  636. or more generally if you would like to move some Org-mode commands to
  637. other keys, set this variable and configure the keys with the variable
  638. `org-disputed-keys'.
  639. This option is only relevant at load-time of Org-mode, and must be set
  640. *before* org.el is loaded. Changing it requires a restart of Emacs to
  641. become effective."
  642. :group 'org-startup
  643. :type 'boolean)
  644. (defcustom org-use-extra-keys nil
  645. "Non-nil means use extra key sequence definitions for certain commands.
  646. This happens automatically if you run XEmacs or if `window-system'
  647. is nil. This variable lets you do the same manually. You must
  648. set it before loading org.
  649. Example: on Carbon Emacs 22 running graphically, with an external
  650. keyboard on a Powerbook, the default way of setting M-left might
  651. not work for either Alt or ESC. Setting this variable will make
  652. it work for ESC."
  653. :group 'org-startup
  654. :type 'boolean)
  655. (org-defvaralias 'org-CUA-compatible 'org-replace-disputed-keys)
  656. (defcustom org-disputed-keys
  657. '(([(shift up)] . [(meta p)])
  658. ([(shift down)] . [(meta n)])
  659. ([(shift left)] . [(meta -)])
  660. ([(shift right)] . [(meta +)])
  661. ([(control shift right)] . [(meta shift +)])
  662. ([(control shift left)] . [(meta shift -)]))
  663. "Keys for which Org-mode and other modes compete.
  664. This is an alist, cars are the default keys, second element specifies
  665. the alternative to use when `org-replace-disputed-keys' is t.
  666. Keys can be specified in any syntax supported by `define-key'.
  667. The value of this option takes effect only at Org-mode's startup,
  668. therefore you'll have to restart Emacs to apply it after changing."
  669. :group 'org-startup
  670. :type 'alist)
  671. (defun org-key (key)
  672. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  673. Or return the original if not disputed.
  674. Also apply the translations defined in `org-xemacs-key-equivalents'."
  675. (when org-replace-disputed-keys
  676. (let* ((nkey (key-description key))
  677. (x (org-find-if (lambda (x)
  678. (equal (key-description (car x)) nkey))
  679. org-disputed-keys)))
  680. (setq key (if x (cdr x) key))))
  681. (when (featurep 'xemacs)
  682. (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
  683. key)
  684. (defun org-find-if (predicate seq)
  685. (catch 'exit
  686. (while seq
  687. (if (funcall predicate (car seq))
  688. (throw 'exit (car seq))
  689. (pop seq)))))
  690. (defun org-defkey (keymap key def)
  691. "Define a key, possibly translated, as returned by `org-key'."
  692. (define-key keymap (org-key key) def))
  693. (defcustom org-ellipsis nil
  694. "The ellipsis to use in the Org-mode outline.
  695. When nil, just use the standard three dots. When a string, use that instead,
  696. When a face, use the standard 3 dots, but with the specified face.
  697. The change affects only Org-mode (which will then use its own display table).
  698. Changing this requires executing `M-x org-mode' in a buffer to become
  699. effective."
  700. :group 'org-startup
  701. :type '(choice (const :tag "Default" nil)
  702. (face :tag "Face" :value org-warning)
  703. (string :tag "String" :value "...#")))
  704. (defvar org-display-table nil
  705. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  706. (defgroup org-keywords nil
  707. "Keywords in Org-mode."
  708. :tag "Org Keywords"
  709. :group 'org)
  710. (defcustom org-deadline-string "DEADLINE:"
  711. "String to mark deadline entries.
  712. A deadline is this string, followed by a time stamp. Should be a word,
  713. terminated by a colon. You can insert a schedule keyword and
  714. a timestamp with \\[org-deadline].
  715. Changes become only effective after restarting Emacs."
  716. :group 'org-keywords
  717. :type 'string)
  718. (defcustom org-scheduled-string "SCHEDULED:"
  719. "String to mark scheduled TODO entries.
  720. A schedule is this string, followed by a time stamp. Should be a word,
  721. terminated by a colon. You can insert a schedule keyword and
  722. a timestamp with \\[org-schedule].
  723. Changes become only effective after restarting Emacs."
  724. :group 'org-keywords
  725. :type 'string)
  726. (defcustom org-closed-string "CLOSED:"
  727. "String used as the prefix for timestamps logging closing a TODO entry."
  728. :group 'org-keywords
  729. :type 'string)
  730. (defcustom org-clock-string "CLOCK:"
  731. "String used as prefix for timestamps clocking work hours on an item."
  732. :group 'org-keywords
  733. :type 'string)
  734. (defconst org-planning-or-clock-line-re (concat "^[ \t]*\\("
  735. org-scheduled-string "\\|"
  736. org-deadline-string "\\|"
  737. org-closed-string "\\|"
  738. org-clock-string "\\)")
  739. "Matches a line with planning or clock info.")
  740. (defcustom org-comment-string "COMMENT"
  741. "Entries starting with this keyword will never be exported.
  742. An entry can be toggled between COMMENT and normal with
  743. \\[org-toggle-comment].
  744. Changes become only effective after restarting Emacs."
  745. :group 'org-keywords
  746. :type 'string)
  747. (defcustom org-quote-string "QUOTE"
  748. "Entries starting with this keyword will be exported in fixed-width font.
  749. Quoting applies only to the text in the entry following the headline, and does
  750. not extend beyond the next headline, even if that is lower level.
  751. An entry can be toggled between QUOTE and normal with
  752. \\[org-toggle-fixed-width-section]."
  753. :group 'org-keywords
  754. :type 'string)
  755. (defconst org-repeat-re
  756. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)"
  757. "Regular expression for specifying repeated events.
  758. After a match, group 1 contains the repeat expression.")
  759. (defgroup org-structure nil
  760. "Options concerning the general structure of Org-mode files."
  761. :tag "Org Structure"
  762. :group 'org)
  763. (defgroup org-reveal-location nil
  764. "Options about how to make context of a location visible."
  765. :tag "Org Reveal Location"
  766. :group 'org-structure)
  767. (defconst org-context-choice
  768. '(choice
  769. (const :tag "Always" t)
  770. (const :tag "Never" nil)
  771. (repeat :greedy t :tag "Individual contexts"
  772. (cons
  773. (choice :tag "Context"
  774. (const agenda)
  775. (const org-goto)
  776. (const occur-tree)
  777. (const tags-tree)
  778. (const link-search)
  779. (const mark-goto)
  780. (const bookmark-jump)
  781. (const isearch)
  782. (const default))
  783. (boolean))))
  784. "Contexts for the reveal options.")
  785. (defcustom org-show-hierarchy-above '((default . t))
  786. "Non-nil means show full hierarchy when revealing a location.
  787. Org-mode often shows locations in an org-mode file which might have
  788. been invisible before. When this is set, the hierarchy of headings
  789. above the exposed location is shown.
  790. Turning this off for example for sparse trees makes them very compact.
  791. Instead of t, this can also be an alist specifying this option for different
  792. contexts. Valid contexts are
  793. agenda when exposing an entry from the agenda
  794. org-goto when using the command `org-goto' on key C-c C-j
  795. occur-tree when using the command `org-occur' on key C-c /
  796. tags-tree when constructing a sparse tree based on tags matches
  797. link-search when exposing search matches associated with a link
  798. mark-goto when exposing the jump goal of a mark
  799. bookmark-jump when exposing a bookmark location
  800. isearch when exiting from an incremental search
  801. default default for all contexts not set explicitly"
  802. :group 'org-reveal-location
  803. :type org-context-choice)
  804. (defcustom org-show-following-heading '((default . nil))
  805. "Non-nil means show following heading when revealing a location.
  806. Org-mode often shows locations in an org-mode file which might have
  807. been invisible before. When this is set, the heading following the
  808. match is shown.
  809. Turning this off for example for sparse trees makes them very compact,
  810. but makes it harder to edit the location of the match. In such a case,
  811. use the command \\[org-reveal] to show more context.
  812. Instead of t, this can also be an alist specifying this option for different
  813. contexts. See `org-show-hierarchy-above' for valid contexts."
  814. :group 'org-reveal-location
  815. :type org-context-choice)
  816. (defcustom org-show-siblings '((default . nil) (isearch t))
  817. "Non-nil means show all sibling heading when revealing a location.
  818. Org-mode often shows locations in an org-mode file which might have
  819. been invisible before. When this is set, the sibling of the current entry
  820. heading are all made visible. If `org-show-hierarchy-above' is t,
  821. the same happens on each level of the hierarchy above the current entry.
  822. By default this is on for the isearch context, off for all other contexts.
  823. Turning this off for example for sparse trees makes them very compact,
  824. but makes it harder to edit the location of the match. In such a case,
  825. use the command \\[org-reveal] to show more context.
  826. Instead of t, this can also be an alist specifying this option for different
  827. contexts. See `org-show-hierarchy-above' for valid contexts."
  828. :group 'org-reveal-location
  829. :type org-context-choice)
  830. (defcustom org-show-entry-below '((default . nil))
  831. "Non-nil means show the entry below a headline when revealing a location.
  832. Org-mode often shows locations in an org-mode file which might have
  833. been invisible before. When this is set, the text below the headline that is
  834. exposed is also shown.
  835. By default this is off for all contexts.
  836. Instead of t, this can also be an alist specifying this option for different
  837. contexts. See `org-show-hierarchy-above' for valid contexts."
  838. :group 'org-reveal-location
  839. :type org-context-choice)
  840. (defcustom org-indirect-buffer-display 'other-window
  841. "How should indirect tree buffers be displayed?
  842. This applies to indirect buffers created with the commands
  843. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  844. Valid values are:
  845. current-window Display in the current window
  846. other-window Just display in another window.
  847. dedicated-frame Create one new frame, and re-use it each time.
  848. new-frame Make a new frame each time. Note that in this case
  849. previously-made indirect buffers are kept, and you need to
  850. kill these buffers yourself."
  851. :group 'org-structure
  852. :group 'org-agenda-windows
  853. :type '(choice
  854. (const :tag "In current window" current-window)
  855. (const :tag "In current frame, other window" other-window)
  856. (const :tag "Each time a new frame" new-frame)
  857. (const :tag "One dedicated frame" dedicated-frame)))
  858. (defcustom org-use-speed-commands nil
  859. "Non-nil means activate single letter commands at beginning of a headline.
  860. This may also be a function to test for appropriate locations where speed
  861. commands should be active."
  862. :group 'org-structure
  863. :type '(choice
  864. (const :tag "Never" nil)
  865. (const :tag "At beginning of headline stars" t)
  866. (function)))
  867. (defcustom org-speed-commands-user nil
  868. "Alist of additional speed commands.
  869. This list will be checked before `org-speed-commands-default'
  870. when the variable `org-use-speed-commands' is non-nil
  871. and when the cursor is at the beginning of a headline.
  872. The car if each entry is a string with a single letter, which must
  873. be assigned to `self-insert-command' in the global map.
  874. The cdr is either a command to be called interactively, a function
  875. to be called, or a form to be evaluated.
  876. An entry that is just a list with a single string will be interpreted
  877. as a descriptive headline that will be added when listing the speed
  878. commands in the Help buffer using the `?' speed command."
  879. :group 'org-structure
  880. :type '(repeat :value ("k" . ignore)
  881. (choice :value ("k" . ignore)
  882. (list :tag "Descriptive Headline" (string :tag "Headline"))
  883. (cons :tag "Letter and Command"
  884. (string :tag "Command letter")
  885. (choice
  886. (function)
  887. (sexp))))))
  888. (defgroup org-cycle nil
  889. "Options concerning visibility cycling in Org-mode."
  890. :tag "Org Cycle"
  891. :group 'org-structure)
  892. (defcustom org-cycle-skip-children-state-if-no-children t
  893. "Non-nil means skip CHILDREN state in entries that don't have any."
  894. :group 'org-cycle
  895. :type 'boolean)
  896. (defcustom org-cycle-max-level nil
  897. "Maximum level which should still be subject to visibility cycling.
  898. Levels higher than this will, for cycling, be treated as text, not a headline.
  899. When `org-odd-levels-only' is set, a value of N in this variable actually
  900. means 2N-1 stars as the limiting headline.
  901. When nil, cycle all levels.
  902. Note that the limiting level of cycling is also influenced by
  903. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  904. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  905. than its value."
  906. :group 'org-cycle
  907. :type '(choice
  908. (const :tag "No limit" nil)
  909. (integer :tag "Maximum level")))
  910. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK" "RESULTS")
  911. "Names of drawers. Drawers are not opened by cycling on the headline above.
  912. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  913. this:
  914. :DRAWERNAME:
  915. .....
  916. :END:
  917. The drawer \"PROPERTIES\" is special for capturing properties through
  918. the property API.
  919. Drawers can be defined on the per-file basis with a line like:
  920. #+DRAWERS: HIDDEN STATE PROPERTIES"
  921. :group 'org-structure
  922. :group 'org-cycle
  923. :type '(repeat (string :tag "Drawer Name")))
  924. (defcustom org-hide-block-startup nil
  925. "Non-nil means entering Org-mode will fold all blocks.
  926. This can also be set in on a per-file basis with
  927. #+STARTUP: hideblocks
  928. #+STARTUP: showblocks"
  929. :group 'org-startup
  930. :group 'org-cycle
  931. :type 'boolean)
  932. (defcustom org-cycle-global-at-bob nil
  933. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  934. This makes it possible to do global cycling without having to use S-TAB or
  935. \\[universal-argument] TAB. For this special case to work, the first line
  936. of the buffer must not be a headline -- it may be empty or some other text.
  937. When used in this way, `org-cycle-hook' is disabled temporarily to make
  938. sure the cursor stays at the beginning of the buffer. When this option is
  939. nil, don't do anything special at the beginning of the buffer."
  940. :group 'org-cycle
  941. :type 'boolean)
  942. (defcustom org-cycle-level-after-item/entry-creation t
  943. "Non-nil means cycle entry level or item indentation in new empty entries.
  944. When the cursor is at the end of an empty headline, i.e., with only stars
  945. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  946. and then all possible ancestor states, before returning to the original state.
  947. This makes data entry extremely fast: M-RET to create a new headline,
  948. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  949. When the cursor is at the end of an empty plain list item, one TAB will
  950. make it a subitem, two or more tabs will back up to make this an item
  951. higher up in the item hierarchy."
  952. :group 'org-cycle
  953. :type 'boolean)
  954. (defcustom org-cycle-emulate-tab t
  955. "Where should `org-cycle' emulate TAB.
  956. nil Never
  957. white Only in completely white lines
  958. whitestart Only at the beginning of lines, before the first non-white char
  959. t Everywhere except in headlines
  960. exc-hl-bol Everywhere except at the start of a headline
  961. If TAB is used in a place where it does not emulate TAB, the current subtree
  962. visibility is cycled."
  963. :group 'org-cycle
  964. :type '(choice (const :tag "Never" nil)
  965. (const :tag "Only in completely white lines" white)
  966. (const :tag "Before first char in a line" whitestart)
  967. (const :tag "Everywhere except in headlines" t)
  968. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  969. ))
  970. (defcustom org-cycle-separator-lines 2
  971. "Number of empty lines needed to keep an empty line between collapsed trees.
  972. If you leave an empty line between the end of a subtree and the following
  973. headline, this empty line is hidden when the subtree is folded.
  974. Org-mode will leave (exactly) one empty line visible if the number of
  975. empty lines is equal or larger to the number given in this variable.
  976. So the default 2 means at least 2 empty lines after the end of a subtree
  977. are needed to produce free space between a collapsed subtree and the
  978. following headline.
  979. If the number is negative, and the number of empty lines is at least -N,
  980. all empty lines are shown.
  981. Special case: when 0, never leave empty lines in collapsed view."
  982. :group 'org-cycle
  983. :type 'integer)
  984. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  985. (defcustom org-pre-cycle-hook nil
  986. "Hook that is run before visibility cycling is happening.
  987. The function(s) in this hook must accept a single argument which indicates
  988. the new state that will be set right after running this hook. The
  989. argument is a symbol. Before a global state change, it can have the values
  990. `overview', `content', or `all'. Before a local state change, it can have
  991. the values `folded', `children', or `subtree'."
  992. :group 'org-cycle
  993. :type 'hook)
  994. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  995. org-cycle-hide-drawers
  996. org-cycle-hide-inline-tasks
  997. org-cycle-show-empty-lines
  998. org-optimize-window-after-visibility-change)
  999. "Hook that is run after `org-cycle' has changed the buffer visibility.
  1000. The function(s) in this hook must accept a single argument which indicates
  1001. the new state that was set by the most recent `org-cycle' command. The
  1002. argument is a symbol. After a global state change, it can have the values
  1003. `overview', `contents', or `all'. After a local state change, it can have
  1004. the values `folded', `children', or `subtree'."
  1005. :group 'org-cycle
  1006. :type 'hook)
  1007. (defgroup org-edit-structure nil
  1008. "Options concerning structure editing in Org-mode."
  1009. :tag "Org Edit Structure"
  1010. :group 'org-structure)
  1011. (defcustom org-odd-levels-only nil
  1012. "Non-nil means skip even levels and only use odd levels for the outline.
  1013. This has the effect that two stars are being added/taken away in
  1014. promotion/demotion commands. It also influences how levels are
  1015. handled by the exporters.
  1016. Changing it requires restart of `font-lock-mode' to become effective
  1017. for fontification also in regions already fontified.
  1018. You may also set this on a per-file basis by adding one of the following
  1019. lines to the buffer:
  1020. #+STARTUP: odd
  1021. #+STARTUP: oddeven"
  1022. :group 'org-edit-structure
  1023. :group 'org-appearance
  1024. :type 'boolean)
  1025. (defcustom org-adapt-indentation t
  1026. "Non-nil means adapt indentation to outline node level.
  1027. When this variable is set, Org assumes that you write outlines by
  1028. indenting text in each node to align with the headline (after the stars).
  1029. The following issues are influenced by this variable:
  1030. - When this is set and the *entire* text in an entry is indented, the
  1031. indentation is increased by one space in a demotion command, and
  1032. decreased by one in a promotion command. If any line in the entry
  1033. body starts with text at column 0, indentation is not changed at all.
  1034. - Property drawers and planning information is inserted indented when
  1035. this variable s set. When nil, they will not be indented.
  1036. - TAB indents a line relative to context. The lines below a headline
  1037. will be indented when this variable is set.
  1038. Note that this is all about true indentation, by adding and removing
  1039. space characters. See also `org-indent.el' which does level-dependent
  1040. indentation in a virtual way, i.e. at display time in Emacs."
  1041. :group 'org-edit-structure
  1042. :type 'boolean)
  1043. (defcustom org-special-ctrl-a/e nil
  1044. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  1045. When t, `C-a' will bring back the cursor to the beginning of the
  1046. headline text, i.e. after the stars and after a possible TODO
  1047. keyword. In an item, this will be the position after bullet and
  1048. check-box, if any. When the cursor is already at that position,
  1049. another `C-a' will bring it to the beginning of the line.
  1050. `C-e' will jump to the end of the headline, ignoring the presence
  1051. of tags in the headline. A second `C-e' will then jump to the
  1052. true end of the line, after any tags. This also means that, when
  1053. this variable is non-nil, `C-e' also will never jump beyond the
  1054. end of the heading of a folded section, i.e. not after the
  1055. ellipses.
  1056. When set to the symbol `reversed', the first `C-a' or `C-e' works
  1057. normally, going to the true line boundary first. Only a directly
  1058. following, identical keypress will bring the cursor to the
  1059. special positions.
  1060. This may also be a cons cell where the behavior for `C-a' and
  1061. `C-e' is set separately."
  1062. :group 'org-edit-structure
  1063. :type '(choice
  1064. (const :tag "off" nil)
  1065. (const :tag "on: after stars/bullet and before tags first" t)
  1066. (const :tag "reversed: true line boundary first" reversed)
  1067. (cons :tag "Set C-a and C-e separately"
  1068. (choice :tag "Special C-a"
  1069. (const :tag "off" nil)
  1070. (const :tag "on: after stars/bullet first" t)
  1071. (const :tag "reversed: before stars/bullet first" reversed))
  1072. (choice :tag "Special C-e"
  1073. (const :tag "off" nil)
  1074. (const :tag "on: before tags first" t)
  1075. (const :tag "reversed: after tags first" reversed)))))
  1076. (org-defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e)
  1077. (defcustom org-special-ctrl-k nil
  1078. "Non-nil means `C-k' will behave specially in headlines.
  1079. When nil, `C-k' will call the default `kill-line' command.
  1080. When t, the following will happen while the cursor is in the headline:
  1081. - When the cursor is at the beginning of a headline, kill the entire
  1082. line and possible the folded subtree below the line.
  1083. - When in the middle of the headline text, kill the headline up to the tags.
  1084. - When after the headline text, kill the tags."
  1085. :group 'org-edit-structure
  1086. :type 'boolean)
  1087. (defcustom org-ctrl-k-protect-subtree nil
  1088. "Non-nil means, do not delete a hidden subtree with C-k.
  1089. When set to the symbol `error', simply throw an error when C-k is
  1090. used to kill (part-of) a headline that has hidden text behind it.
  1091. Any other non-nil value will result in a query to the user, if it is
  1092. OK to kill that hidden subtree. When nil, kill without remorse."
  1093. :group 'org-edit-structure
  1094. :version "24.1"
  1095. :type '(choice
  1096. (const :tag "Do not protect hidden subtrees" nil)
  1097. (const :tag "Protect hidden subtrees with a security query" t)
  1098. (const :tag "Never kill a hidden subtree with C-k" error)))
  1099. (defcustom org-catch-invisible-edits nil
  1100. "Check if in invisible region before inserting or deleting a character.
  1101. Valid values are:
  1102. nil Do not check, so just do invisible edits.
  1103. error Throw an error and do nothing.
  1104. show Make point visible, and do the requested edit.
  1105. show-and-error Make point visible, then throw an error and abort the edit.
  1106. smart Make point visible, and do insertion/deletion if it is
  1107. adjacent to visible text and the change feels predictable.
  1108. Never delete a previously invisible character or add in the
  1109. middle or right after an invisible region. Basically, this
  1110. allows insertion and backward-delete right before ellipses.
  1111. FIXME: maybe in this case we should not even show?"
  1112. :group 'org-edit-structure
  1113. :version "24.1"
  1114. :type '(choice
  1115. (const :tag "Do not check" nil)
  1116. (const :tag "Throw error when trying to edit" error)
  1117. (const :tag "Unhide, but do not do the edit" show-and-error)
  1118. (const :tag "Show invisible part and do the edit" show)
  1119. (const :tag "Be smart and do the right thing" smart)))
  1120. (defcustom org-yank-folded-subtrees t
  1121. "Non-nil means when yanking subtrees, fold them.
  1122. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  1123. it starts with a heading and all other headings in it are either children
  1124. or siblings, then fold all the subtrees. However, do this only if no
  1125. text after the yank would be swallowed into a folded tree by this action."
  1126. :group 'org-edit-structure
  1127. :type 'boolean)
  1128. (defcustom org-yank-adjusted-subtrees nil
  1129. "Non-nil means when yanking subtrees, adjust the level.
  1130. With this setting, `org-paste-subtree' is used to insert the subtree, see
  1131. this function for details."
  1132. :group 'org-edit-structure
  1133. :type 'boolean)
  1134. (defcustom org-M-RET-may-split-line '((default . t))
  1135. "Non-nil means M-RET will split the line at the cursor position.
  1136. When nil, it will go to the end of the line before making a
  1137. new line.
  1138. You may also set this option in a different way for different
  1139. contexts. Valid contexts are:
  1140. headline when creating a new headline
  1141. item when creating a new item
  1142. table in a table field
  1143. default the value to be used for all contexts not explicitly
  1144. customized"
  1145. :group 'org-structure
  1146. :group 'org-table
  1147. :type '(choice
  1148. (const :tag "Always" t)
  1149. (const :tag "Never" nil)
  1150. (repeat :greedy t :tag "Individual contexts"
  1151. (cons
  1152. (choice :tag "Context"
  1153. (const headline)
  1154. (const item)
  1155. (const table)
  1156. (const default))
  1157. (boolean)))))
  1158. (defcustom org-insert-heading-respect-content nil
  1159. "Non-nil means insert new headings after the current subtree.
  1160. When nil, the new heading is created directly after the current line.
  1161. The commands \\[org-insert-heading-respect-content] and
  1162. \\[org-insert-todo-heading-respect-content] turn this variable on
  1163. for the duration of the command."
  1164. :group 'org-structure
  1165. :type 'boolean)
  1166. (defcustom org-blank-before-new-entry '((heading . auto)
  1167. (plain-list-item . auto))
  1168. "Should `org-insert-heading' leave a blank line before new heading/item?
  1169. The value is an alist, with `heading' and `plain-list-item' as CAR,
  1170. and a boolean flag as CDR. The cdr may also be the symbol `auto', in
  1171. which case Org will look at the surrounding headings/items and try to
  1172. make an intelligent decision whether to insert a blank line or not.
  1173. For plain lists, if the variable `org-empty-line-terminates-plain-lists' is
  1174. set, the setting here is ignored and no empty line is inserted, to avoid
  1175. breaking the list structure."
  1176. :group 'org-edit-structure
  1177. :type '(list
  1178. (cons (const heading)
  1179. (choice (const :tag "Never" nil)
  1180. (const :tag "Always" t)
  1181. (const :tag "Auto" auto)))
  1182. (cons (const plain-list-item)
  1183. (choice (const :tag "Never" nil)
  1184. (const :tag "Always" t)
  1185. (const :tag "Auto" auto)))))
  1186. (defcustom org-insert-heading-hook nil
  1187. "Hook being run after inserting a new heading."
  1188. :group 'org-edit-structure
  1189. :type 'hook)
  1190. (defcustom org-enable-fixed-width-editor t
  1191. "Non-nil means lines starting with \":\" are treated as fixed-width.
  1192. This currently only means they are never auto-wrapped.
  1193. When nil, such lines will be treated like ordinary lines.
  1194. See also the QUOTE keyword."
  1195. :group 'org-edit-structure
  1196. :type 'boolean)
  1197. (defcustom org-goto-auto-isearch t
  1198. "Non-nil means typing characters in `org-goto' starts incremental search.
  1199. When nil, you can use these keybindings to navigate the buffer:
  1200. q Quit the org-goto interface
  1201. n Go to the next visible heading
  1202. p Go to the previous visible heading
  1203. f Go one heading forward on same level
  1204. b Go one heading backward on same level
  1205. u Go one heading up"
  1206. :group 'org-edit-structure
  1207. :type 'boolean)
  1208. (defgroup org-sparse-trees nil
  1209. "Options concerning sparse trees in Org-mode."
  1210. :tag "Org Sparse Trees"
  1211. :group 'org-structure)
  1212. (defcustom org-highlight-sparse-tree-matches t
  1213. "Non-nil means highlight all matches that define a sparse tree.
  1214. The highlights will automatically disappear the next time the buffer is
  1215. changed by an edit command."
  1216. :group 'org-sparse-trees
  1217. :type 'boolean)
  1218. (defcustom org-remove-highlights-with-change t
  1219. "Non-nil means any change to the buffer will remove temporary highlights.
  1220. Such highlights are created by `org-occur' and `org-clock-display'.
  1221. When nil, `C-c C-c needs to be used to get rid of the highlights.
  1222. The highlights created by `org-preview-latex-fragment' always need
  1223. `C-c C-c' to be removed."
  1224. :group 'org-sparse-trees
  1225. :group 'org-time
  1226. :type 'boolean)
  1227. (defcustom org-occur-hook '(org-first-headline-recenter)
  1228. "Hook that is run after `org-occur' has constructed a sparse tree.
  1229. This can be used to recenter the window to show as much of the structure
  1230. as possible."
  1231. :group 'org-sparse-trees
  1232. :type 'hook)
  1233. (defgroup org-imenu-and-speedbar nil
  1234. "Options concerning imenu and speedbar in Org-mode."
  1235. :tag "Org Imenu and Speedbar"
  1236. :group 'org-structure)
  1237. (defcustom org-imenu-depth 2
  1238. "The maximum level for Imenu access to Org-mode headlines.
  1239. This also applied for speedbar access."
  1240. :group 'org-imenu-and-speedbar
  1241. :type 'integer)
  1242. (defgroup org-table nil
  1243. "Options concerning tables in Org-mode."
  1244. :tag "Org Table"
  1245. :group 'org)
  1246. (defcustom org-enable-table-editor 'optimized
  1247. "Non-nil means lines starting with \"|\" are handled by the table editor.
  1248. When nil, such lines will be treated like ordinary lines.
  1249. When equal to the symbol `optimized', the table editor will be optimized to
  1250. do the following:
  1251. - Automatic overwrite mode in front of whitespace in table fields.
  1252. This makes the structure of the table stay in tact as long as the edited
  1253. field does not exceed the column width.
  1254. - Minimize the number of realigns. Normally, the table is aligned each time
  1255. TAB or RET are pressed to move to another field. With optimization this
  1256. happens only if changes to a field might have changed the column width.
  1257. Optimization requires replacing the functions `self-insert-command',
  1258. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  1259. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  1260. very good at guessing when a re-align will be necessary, but you can always
  1261. force one with \\[org-ctrl-c-ctrl-c].
  1262. If you would like to use the optimized version in Org-mode, but the
  1263. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  1264. This variable can be used to turn on and off the table editor during a session,
  1265. but in order to toggle optimization, a restart is required.
  1266. See also the variable `org-table-auto-blank-field'."
  1267. :group 'org-table
  1268. :type '(choice
  1269. (const :tag "off" nil)
  1270. (const :tag "on" t)
  1271. (const :tag "on, optimized" optimized)))
  1272. (defcustom org-self-insert-cluster-for-undo (or (featurep 'xemacs)
  1273. (version<= emacs-version "24.1"))
  1274. "Non-nil means cluster self-insert commands for undo when possible.
  1275. If this is set, then, like in the Emacs command loop, 20 consecutive
  1276. characters will be undone together.
  1277. This is configurable, because there is some impact on typing performance."
  1278. :group 'org-table
  1279. :type 'boolean)
  1280. (defcustom org-table-tab-recognizes-table.el t
  1281. "Non-nil means TAB will automatically notice a table.el table.
  1282. When it sees such a table, it moves point into it and - if necessary -
  1283. calls `table-recognize-table'."
  1284. :group 'org-table-editing
  1285. :type 'boolean)
  1286. (defgroup org-link nil
  1287. "Options concerning links in Org-mode."
  1288. :tag "Org Link"
  1289. :group 'org)
  1290. (defvar org-link-abbrev-alist-local nil
  1291. "Buffer-local version of `org-link-abbrev-alist', which see.
  1292. The value of this is taken from the #+LINK lines.")
  1293. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  1294. (defcustom org-link-abbrev-alist nil
  1295. "Alist of link abbreviations.
  1296. The car of each element is a string, to be replaced at the start of a link.
  1297. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1298. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  1299. [[linkkey:tag][description]]
  1300. The 'linkkey' must be a word word, starting with a letter, followed
  1301. by letters, numbers, '-' or '_'.
  1302. If REPLACE is a string, the tag will simply be appended to create the link.
  1303. If the string contains \"%s\", the tag will be inserted there. If the string
  1304. contains \"%h\", it will cause a url-encoded version of the tag to be inserted
  1305. at that point (see the function `url-hexify-string'). If the string contains
  1306. the specifier \"%(my-function)\", then the custom function `my-function' will
  1307. be invoked: this function takes the tag as its only argument and must return
  1308. a string.
  1309. REPLACE may also be a function that will be called with the tag as the
  1310. only argument to create the link, which should be returned as a string.
  1311. See the manual for examples."
  1312. :group 'org-link
  1313. :type '(repeat
  1314. (cons
  1315. (string :tag "Protocol")
  1316. (choice
  1317. (string :tag "Format")
  1318. (function)))))
  1319. (defcustom org-descriptive-links t
  1320. "Non-nil means Org will display descriptive links.
  1321. E.g. [[http://orgmode.org][Org website]] will be displayed as
  1322. \"Org Website\", hiding the link itself and just displaying its
  1323. description. When set to `nil', Org will display the full links
  1324. literally.
  1325. You can interactively set the value of this variable by calling
  1326. `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
  1327. :group 'org-link
  1328. :type 'boolean)
  1329. (defcustom org-link-file-path-type 'adaptive
  1330. "How the path name in file links should be stored.
  1331. Valid values are:
  1332. relative Relative to the current directory, i.e. the directory of the file
  1333. into which the link is being inserted.
  1334. absolute Absolute path, if possible with ~ for home directory.
  1335. noabbrev Absolute path, no abbreviation of home directory.
  1336. adaptive Use relative path for files in the current directory and sub-
  1337. directories of it. For other files, use an absolute path."
  1338. :group 'org-link
  1339. :type '(choice
  1340. (const relative)
  1341. (const absolute)
  1342. (const noabbrev)
  1343. (const adaptive)))
  1344. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  1345. "Types of links that should be activated in Org-mode files.
  1346. This is a list of symbols, each leading to the activation of a certain link
  1347. type. In principle, it does not hurt to turn on most link types - there may
  1348. be a small gain when turning off unused link types. The types are:
  1349. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1350. angle Links in angular brackets that may contain whitespace like
  1351. <bbdb:Carsten Dominik>.
  1352. plain Plain links in normal text, no whitespace, like http://google.com.
  1353. radio Text that is matched by a radio target, see manual for details.
  1354. tag Tag settings in a headline (link to tag search).
  1355. date Time stamps (link to calendar).
  1356. footnote Footnote labels.
  1357. Changing this variable requires a restart of Emacs to become effective."
  1358. :group 'org-link
  1359. :type '(set :greedy t
  1360. (const :tag "Double bracket links" bracket)
  1361. (const :tag "Angular bracket links" angle)
  1362. (const :tag "Plain text links" plain)
  1363. (const :tag "Radio target matches" radio)
  1364. (const :tag "Tags" tag)
  1365. (const :tag "Timestamps" date)
  1366. (const :tag "Footnotes" footnote)))
  1367. (defcustom org-make-link-description-function nil
  1368. "Function to use for generating link descriptions from links.
  1369. When nil, the link location will be used. This function must take
  1370. two parameters: the first one is the link, the second one is the
  1371. description generated by `org-insert-link'. The function should
  1372. return the description to use."
  1373. :group 'org-link
  1374. :type 'function)
  1375. (defgroup org-link-store nil
  1376. "Options concerning storing links in Org-mode."
  1377. :tag "Org Store Link"
  1378. :group 'org-link)
  1379. (defcustom org-url-hexify-p t
  1380. "When non-nil, hexify URL when creating a link."
  1381. :type 'boolean
  1382. :version "24.3"
  1383. :group 'org-link-store)
  1384. (defcustom org-email-link-description-format "Email %c: %.30s"
  1385. "Format of the description part of a link to an email or usenet message.
  1386. The following %-escapes will be replaced by corresponding information:
  1387. %F full \"From\" field
  1388. %f name, taken from \"From\" field, address if no name
  1389. %T full \"To\" field
  1390. %t first name in \"To\" field, address if no name
  1391. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1392. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1393. %s subject
  1394. %d date
  1395. %m message-id.
  1396. You may use normal field width specification between the % and the letter.
  1397. This is for example useful to limit the length of the subject.
  1398. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1399. :group 'org-link-store
  1400. :type 'string)
  1401. (defcustom org-from-is-user-regexp
  1402. (let (r1 r2)
  1403. (when (and user-mail-address (not (string= user-mail-address "")))
  1404. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1405. (when (and user-full-name (not (string= user-full-name "")))
  1406. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1407. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1408. "Regexp matched against the \"From:\" header of an email or usenet message.
  1409. It should match if the message is from the user him/herself."
  1410. :group 'org-link-store
  1411. :type 'regexp)
  1412. (defcustom org-context-in-file-links t
  1413. "Non-nil means file links from `org-store-link' contain context.
  1414. A search string will be added to the file name with :: as separator and
  1415. used to find the context when the link is activated by the command
  1416. `org-open-at-point'. When this option is t, the entire active region
  1417. will be placed in the search string of the file link. If set to a
  1418. positive integer, only the first n lines of context will be stored.
  1419. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1420. negates this setting for the duration of the command."
  1421. :group 'org-link-store
  1422. :type '(choice boolean integer))
  1423. (defcustom org-keep-stored-link-after-insertion nil
  1424. "Non-nil means keep link in list for entire session.
  1425. The command `org-store-link' adds a link pointing to the current
  1426. location to an internal list. These links accumulate during a session.
  1427. The command `org-insert-link' can be used to insert links into any
  1428. Org-mode file (offering completion for all stored links). When this
  1429. option is nil, every link which has been inserted once using \\[org-insert-link]
  1430. will be removed from the list, to make completing the unused links
  1431. more efficient."
  1432. :group 'org-link-store
  1433. :type 'boolean)
  1434. (defgroup org-link-follow nil
  1435. "Options concerning following links in Org-mode."
  1436. :tag "Org Follow Link"
  1437. :group 'org-link)
  1438. (defcustom org-link-translation-function nil
  1439. "Function to translate links with different syntax to Org syntax.
  1440. This can be used to translate links created for example by the Planner
  1441. or emacs-wiki packages to Org syntax.
  1442. The function must accept two parameters, a TYPE containing the link
  1443. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1444. which is everything after the link protocol. It should return a cons
  1445. with possibly modified values of type and path.
  1446. Org contains a function for this, so if you set this variable to
  1447. `org-translate-link-from-planner', you should be able follow many
  1448. links created by planner."
  1449. :group 'org-link-follow
  1450. :type 'function)
  1451. (defcustom org-follow-link-hook nil
  1452. "Hook that is run after a link has been followed."
  1453. :group 'org-link-follow
  1454. :type 'hook)
  1455. (defcustom org-tab-follows-link nil
  1456. "Non-nil means on links TAB will follow the link.
  1457. Needs to be set before org.el is loaded.
  1458. This really should not be used, it does not make sense, and the
  1459. implementation is bad."
  1460. :group 'org-link-follow
  1461. :type 'boolean)
  1462. (defcustom org-return-follows-link nil
  1463. "Non-nil means on links RET will follow the link.
  1464. In tables, the special behavior of RET has precedence."
  1465. :group 'org-link-follow
  1466. :type 'boolean)
  1467. (defcustom org-mouse-1-follows-link
  1468. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1469. "Non-nil means mouse-1 on a link will follow the link.
  1470. A longer mouse click will still set point. Does not work on XEmacs.
  1471. Needs to be set before org.el is loaded."
  1472. :group 'org-link-follow
  1473. :type 'boolean)
  1474. (defcustom org-mark-ring-length 4
  1475. "Number of different positions to be recorded in the ring.
  1476. Changing this requires a restart of Emacs to work correctly."
  1477. :group 'org-link-follow
  1478. :type 'integer)
  1479. (defcustom org-link-search-must-match-exact-headline 'query-to-create
  1480. "Non-nil means internal links in Org files must exactly match a headline.
  1481. When nil, the link search tries to match a phrase with all words
  1482. in the search text."
  1483. :group 'org-link-follow
  1484. :version "24.1"
  1485. :type '(choice
  1486. (const :tag "Use fuzzy text search" nil)
  1487. (const :tag "Match only exact headline" t)
  1488. (const :tag "Match exact headline or query to create it"
  1489. query-to-create)))
  1490. (defcustom org-link-frame-setup
  1491. '((vm . vm-visit-folder-other-frame)
  1492. (vm-imap . vm-visit-imap-folder-other-frame)
  1493. (gnus . org-gnus-no-new-news)
  1494. (file . find-file-other-window)
  1495. (wl . wl-other-frame))
  1496. "Setup the frame configuration for following links.
  1497. When following a link with Emacs, it may often be useful to display
  1498. this link in another window or frame. This variable can be used to
  1499. set this up for the different types of links.
  1500. For VM, use any of
  1501. `vm-visit-folder'
  1502. `vm-visit-folder-other-window'
  1503. `vm-visit-folder-other-frame'
  1504. For Gnus, use any of
  1505. `gnus'
  1506. `gnus-other-frame'
  1507. `org-gnus-no-new-news'
  1508. For FILE, use any of
  1509. `find-file'
  1510. `find-file-other-window'
  1511. `find-file-other-frame'
  1512. For Wanderlust use any of
  1513. `wl'
  1514. `wl-other-frame'
  1515. For the calendar, use the variable `calendar-setup'.
  1516. For BBDB, it is currently only possible to display the matches in
  1517. another window."
  1518. :group 'org-link-follow
  1519. :type '(list
  1520. (cons (const vm)
  1521. (choice
  1522. (const vm-visit-folder)
  1523. (const vm-visit-folder-other-window)
  1524. (const vm-visit-folder-other-frame)))
  1525. (cons (const gnus)
  1526. (choice
  1527. (const gnus)
  1528. (const gnus-other-frame)
  1529. (const org-gnus-no-new-news)))
  1530. (cons (const file)
  1531. (choice
  1532. (const find-file)
  1533. (const find-file-other-window)
  1534. (const find-file-other-frame)))
  1535. (cons (const wl)
  1536. (choice
  1537. (const wl)
  1538. (const wl-other-frame)))))
  1539. (defcustom org-display-internal-link-with-indirect-buffer nil
  1540. "Non-nil means use indirect buffer to display infile links.
  1541. Activating internal links (from one location in a file to another location
  1542. in the same file) normally just jumps to the location. When the link is
  1543. activated with a \\[universal-argument] prefix (or with mouse-3), the link \
  1544. is displayed in
  1545. another window. When this option is set, the other window actually displays
  1546. an indirect buffer clone of the current buffer, to avoid any visibility
  1547. changes to the current buffer."
  1548. :group 'org-link-follow
  1549. :type 'boolean)
  1550. (defcustom org-open-non-existing-files nil
  1551. "Non-nil means `org-open-file' will open non-existing files.
  1552. When nil, an error will be generated.
  1553. This variable applies only to external applications because they
  1554. might choke on non-existing files. If the link is to a file that
  1555. will be opened in Emacs, the variable is ignored."
  1556. :group 'org-link-follow
  1557. :type 'boolean)
  1558. (defcustom org-open-directory-means-index-dot-org nil
  1559. "Non-nil means a link to a directory really means to index.org.
  1560. When nil, following a directory link will run dired or open a finder/explorer
  1561. window on that directory."
  1562. :group 'org-link-follow
  1563. :type 'boolean)
  1564. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1565. "Function and arguments to call for following mailto links.
  1566. This is a list with the first element being a Lisp function, and the
  1567. remaining elements being arguments to the function. In string arguments,
  1568. %a will be replaced by the address, and %s will be replaced by the subject
  1569. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1570. :group 'org-link-follow
  1571. :type '(choice
  1572. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1573. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1574. (const :tag "message-mail" (message-mail "%a" "%s"))
  1575. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1576. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1577. "Non-nil means ask for confirmation before executing shell links.
  1578. Shell links can be dangerous: just think about a link
  1579. [[shell:rm -rf ~/*][Google Search]]
  1580. This link would show up in your Org-mode document as \"Google Search\",
  1581. but really it would remove your entire home directory.
  1582. Therefore we advise against setting this variable to nil.
  1583. Just change it to `y-or-n-p' if you want to confirm with a
  1584. single keystroke rather than having to type \"yes\"."
  1585. :group 'org-link-follow
  1586. :type '(choice
  1587. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1588. (const :tag "with y-or-n (faster)" y-or-n-p)
  1589. (const :tag "no confirmation (dangerous)" nil)))
  1590. (put 'org-confirm-shell-link-function
  1591. 'safe-local-variable
  1592. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1593. (defcustom org-confirm-shell-link-not-regexp ""
  1594. "A regexp to skip confirmation for shell links."
  1595. :group 'org-link-follow
  1596. :version "24.1"
  1597. :type 'regexp)
  1598. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1599. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1600. Elisp links can be dangerous: just think about a link
  1601. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1602. This link would show up in your Org-mode document as \"Google Search\",
  1603. but really it would remove your entire home directory.
  1604. Therefore we advise against setting this variable to nil.
  1605. Just change it to `y-or-n-p' if you want to confirm with a
  1606. single keystroke rather than having to type \"yes\"."
  1607. :group 'org-link-follow
  1608. :type '(choice
  1609. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1610. (const :tag "with y-or-n (faster)" y-or-n-p)
  1611. (const :tag "no confirmation (dangerous)" nil)))
  1612. (put 'org-confirm-shell-link-function
  1613. 'safe-local-variable
  1614. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1615. (defcustom org-confirm-elisp-link-not-regexp ""
  1616. "A regexp to skip confirmation for Elisp links."
  1617. :group 'org-link-follow
  1618. :version "24.1"
  1619. :type 'regexp)
  1620. (defconst org-file-apps-defaults-gnu
  1621. '((remote . emacs)
  1622. (system . mailcap)
  1623. (t . mailcap))
  1624. "Default file applications on a UNIX or GNU/Linux system.
  1625. See `org-file-apps'.")
  1626. (defconst org-file-apps-defaults-macosx
  1627. '((remote . emacs)
  1628. (t . "open %s")
  1629. (system . "open %s")
  1630. ("ps.gz" . "gv %s")
  1631. ("eps.gz" . "gv %s")
  1632. ("dvi" . "xdvi %s")
  1633. ("fig" . "xfig %s"))
  1634. "Default file applications on a MacOS X system.
  1635. The system \"open\" is known as a default, but we use X11 applications
  1636. for some files for which the OS does not have a good default.
  1637. See `org-file-apps'.")
  1638. (defconst org-file-apps-defaults-windowsnt
  1639. (list
  1640. '(remote . emacs)
  1641. (cons t
  1642. (list (if (featurep 'xemacs)
  1643. 'mswindows-shell-execute
  1644. 'w32-shell-execute)
  1645. "open" 'file))
  1646. (cons 'system
  1647. (list (if (featurep 'xemacs)
  1648. 'mswindows-shell-execute
  1649. 'w32-shell-execute)
  1650. "open" 'file)))
  1651. "Default file applications on a Windows NT system.
  1652. The system \"open\" is used for most files.
  1653. See `org-file-apps'.")
  1654. (defcustom org-file-apps
  1655. '(
  1656. (auto-mode . emacs)
  1657. ("\\.mm\\'" . default)
  1658. ("\\.x?html?\\'" . default)
  1659. ("\\.pdf\\'" . default)
  1660. )
  1661. "External applications for opening `file:path' items in a document.
  1662. Org-mode uses system defaults for different file types, but
  1663. you can use this variable to set the application for a given file
  1664. extension. The entries in this list are cons cells where the car identifies
  1665. files and the cdr the corresponding command. Possible values for the
  1666. file identifier are
  1667. \"string\" A string as a file identifier can be interpreted in different
  1668. ways, depending on its contents:
  1669. - Alphanumeric characters only:
  1670. Match links with this file extension.
  1671. Example: (\"pdf\" . \"evince %s\")
  1672. to open PDFs with evince.
  1673. - Regular expression: Match links where the
  1674. filename matches the regexp. If you want to
  1675. use groups here, use shy groups.
  1676. Example: (\"\\.x?html\\'\" . \"firefox %s\")
  1677. (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1678. to open *.html and *.xhtml with firefox.
  1679. - Regular expression which contains (non-shy) groups:
  1680. Match links where the whole link, including \"::\", and
  1681. anything after that, matches the regexp.
  1682. In a custom command string, %1, %2, etc. are replaced with
  1683. the parts of the link that were matched by the groups.
  1684. For backwards compatibility, if a command string is given
  1685. that does not use any of the group matches, this case is
  1686. handled identically to the second one (i.e. match against
  1687. file name only).
  1688. In a custom lisp form, you can access the group matches with
  1689. (match-string n link).
  1690. Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
  1691. to open [[file:document.pdf::5]] with evince at page 5.
  1692. `directory' Matches a directory
  1693. `remote' Matches a remote file, accessible through tramp or efs.
  1694. Remote files most likely should be visited through Emacs
  1695. because external applications cannot handle such paths.
  1696. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1697. so all files Emacs knows how to handle. Using this with
  1698. command `emacs' will open most files in Emacs. Beware that this
  1699. will also open html files inside Emacs, unless you add
  1700. (\"html\" . default) to the list as well.
  1701. t Default for files not matched by any of the other options.
  1702. `system' The system command to open files, like `open' on Windows
  1703. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1704. that will be selected if you call `C-c C-o' with a double
  1705. \\[universal-argument] \\[universal-argument] prefix.
  1706. Possible values for the command are:
  1707. `emacs' The file will be visited by the current Emacs process.
  1708. `default' Use the default application for this file type, which is the
  1709. association for t in the list, most likely in the system-specific
  1710. part.
  1711. This can be used to overrule an unwanted setting in the
  1712. system-specific variable.
  1713. `system' Use the system command for opening files, like \"open\".
  1714. This command is specified by the entry whose car is `system'.
  1715. Most likely, the system-specific version of this variable
  1716. does define this command, but you can overrule/replace it
  1717. here.
  1718. string A command to be executed by a shell; %s will be replaced
  1719. by the path to the file.
  1720. sexp A Lisp form which will be evaluated. The file path will
  1721. be available in the Lisp variable `file'.
  1722. For more examples, see the system specific constants
  1723. `org-file-apps-defaults-macosx'
  1724. `org-file-apps-defaults-windowsnt'
  1725. `org-file-apps-defaults-gnu'."
  1726. :group 'org-link-follow
  1727. :type '(repeat
  1728. (cons (choice :value ""
  1729. (string :tag "Extension")
  1730. (const :tag "System command to open files" system)
  1731. (const :tag "Default for unrecognized files" t)
  1732. (const :tag "Remote file" remote)
  1733. (const :tag "Links to a directory" directory)
  1734. (const :tag "Any files that have Emacs modes"
  1735. auto-mode))
  1736. (choice :value ""
  1737. (const :tag "Visit with Emacs" emacs)
  1738. (const :tag "Use default" default)
  1739. (const :tag "Use the system command" system)
  1740. (string :tag "Command")
  1741. (sexp :tag "Lisp form")))))
  1742. (defcustom org-doi-server-url "http://dx.doi.org/"
  1743. "The URL of the DOI server."
  1744. :type 'string
  1745. :version "24.3"
  1746. :group 'org-link-follow)
  1747. (defgroup org-refile nil
  1748. "Options concerning refiling entries in Org-mode."
  1749. :tag "Org Refile"
  1750. :group 'org)
  1751. (defcustom org-directory "~/org"
  1752. "Directory with org files.
  1753. This is just a default location to look for Org files. There is no need
  1754. at all to put your files into this directory. It is only used in the
  1755. following situations:
  1756. 1. When a capture template specifies a target file that is not an
  1757. absolute path. The path will then be interpreted relative to
  1758. `org-directory'
  1759. 2. When a capture note is filed away in an interactive way (when exiting the
  1760. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1761. with `org-directory' as the default path."
  1762. :group 'org-refile
  1763. :group 'org-remember
  1764. :group 'org-capture
  1765. :type 'directory)
  1766. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1767. "Default target for storing notes.
  1768. Used as a fall back file for org-remember.el and org-capture.el, for
  1769. templates that do not specify a target file."
  1770. :group 'org-refile
  1771. :group 'org-remember
  1772. :group 'org-capture
  1773. :type '(choice
  1774. (const :tag "Default from remember-data-file" nil)
  1775. file))
  1776. (defcustom org-goto-interface 'outline
  1777. "The default interface to be used for `org-goto'.
  1778. Allowed values are:
  1779. outline The interface shows an outline of the relevant file
  1780. and the correct heading is found by moving through
  1781. the outline or by searching with incremental search.
  1782. outline-path-completion Headlines in the current buffer are offered via
  1783. completion. This is the interface also used by
  1784. the refile command."
  1785. :group 'org-refile
  1786. :type '(choice
  1787. (const :tag "Outline" outline)
  1788. (const :tag "Outline-path-completion" outline-path-completion)))
  1789. (defcustom org-goto-max-level 5
  1790. "Maximum target level when running `org-goto' with refile interface."
  1791. :group 'org-refile
  1792. :type 'integer)
  1793. (defcustom org-reverse-note-order nil
  1794. "Non-nil means store new notes at the beginning of a file or entry.
  1795. When nil, new notes will be filed to the end of a file or entry.
  1796. This can also be a list with cons cells of regular expressions that
  1797. are matched against file names, and values."
  1798. :group 'org-remember
  1799. :group 'org-capture
  1800. :group 'org-refile
  1801. :type '(choice
  1802. (const :tag "Reverse always" t)
  1803. (const :tag "Reverse never" nil)
  1804. (repeat :tag "By file name regexp"
  1805. (cons regexp boolean))))
  1806. (defcustom org-log-refile nil
  1807. "Information to record when a task is refiled.
  1808. Possible values are:
  1809. nil Don't add anything
  1810. time Add a time stamp to the task
  1811. note Prompt for a note and add it with template `org-log-note-headings'
  1812. This option can also be set with on a per-file-basis with
  1813. #+STARTUP: nologrefile
  1814. #+STARTUP: logrefile
  1815. #+STARTUP: lognoterefile
  1816. You can have local logging settings for a subtree by setting the LOGGING
  1817. property to one or more of these keywords.
  1818. When bulk-refiling from the agenda, the value `note' is forbidden and
  1819. will temporarily be changed to `time'."
  1820. :group 'org-refile
  1821. :group 'org-progress
  1822. :version "24.1"
  1823. :type '(choice
  1824. (const :tag "No logging" nil)
  1825. (const :tag "Record timestamp" time)
  1826. (const :tag "Record timestamp with note." note)))
  1827. (defcustom org-refile-targets nil
  1828. "Targets for refiling entries with \\[org-refile].
  1829. This is a list of cons cells. Each cell contains:
  1830. - a specification of the files to be considered, either a list of files,
  1831. or a symbol whose function or variable value will be used to retrieve
  1832. a file name or a list of file names. If you use `org-agenda-files' for
  1833. that, all agenda files will be scanned for targets. Nil means consider
  1834. headings in the current buffer.
  1835. - A specification of how to find candidate refile targets. This may be
  1836. any of:
  1837. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1838. This tag has to be present in all target headlines, inheritance will
  1839. not be considered.
  1840. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1841. todo keyword.
  1842. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1843. headlines that are refiling targets.
  1844. - a cons cell (:level . N). Any headline of level N is considered a target.
  1845. Note that, when `org-odd-levels-only' is set, level corresponds to
  1846. order in hierarchy, not to the number of stars.
  1847. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1848. Note that, when `org-odd-levels-only' is set, level corresponds to
  1849. order in hierarchy, not to the number of stars.
  1850. Each element of this list generates a set of possible targets.
  1851. The union of these sets is presented (with completion) to
  1852. the user by `org-refile'.
  1853. You can set the variable `org-refile-target-verify-function' to a function
  1854. to verify each headline found by the simple criteria above.
  1855. When this variable is nil, all top-level headlines in the current buffer
  1856. are used, equivalent to the value `((nil . (:level . 1))'."
  1857. :group 'org-refile
  1858. :type '(repeat
  1859. (cons
  1860. (choice :value org-agenda-files
  1861. (const :tag "All agenda files" org-agenda-files)
  1862. (const :tag "Current buffer" nil)
  1863. (function) (variable) (file))
  1864. (choice :tag "Identify target headline by"
  1865. (cons :tag "Specific tag" (const :value :tag) (string))
  1866. (cons :tag "TODO keyword" (const :value :todo) (string))
  1867. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1868. (cons :tag "Level number" (const :value :level) (integer))
  1869. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1870. (defcustom org-refile-target-verify-function nil
  1871. "Function to verify if the headline at point should be a refile target.
  1872. The function will be called without arguments, with point at the
  1873. beginning of the headline. It should return t and leave point
  1874. where it is if the headline is a valid target for refiling.
  1875. If the target should not be selected, the function must return nil.
  1876. In addition to this, it may move point to a place from where the search
  1877. should be continued. For example, the function may decide that the entire
  1878. subtree of the current entry should be excluded and move point to the end
  1879. of the subtree."
  1880. :group 'org-refile
  1881. :type 'function)
  1882. (defcustom org-refile-use-cache nil
  1883. "Non-nil means cache refile targets to speed up the process.
  1884. The cache for a particular file will be updated automatically when
  1885. the buffer has been killed, or when any of the marker used for flagging
  1886. refile targets no longer points at a live buffer.
  1887. If you have added new entries to a buffer that might themselves be targets,
  1888. you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
  1889. find that easier, `C-u C-u C-u C-c C-w'."
  1890. :group 'org-refile
  1891. :version "24.1"
  1892. :type 'boolean)
  1893. (defcustom org-refile-use-outline-path nil
  1894. "Non-nil means provide refile targets as paths.
  1895. So a level 3 headline will be available as level1/level2/level3.
  1896. When the value is `file', also include the file name (without directory)
  1897. into the path. In this case, you can also stop the completion after
  1898. the file name, to get entries inserted as top level in the file.
  1899. When `full-file-path', include the full file path."
  1900. :group 'org-refile
  1901. :type '(choice
  1902. (const :tag "Not" nil)
  1903. (const :tag "Yes" t)
  1904. (const :tag "Start with file name" file)
  1905. (const :tag "Start with full file path" full-file-path)))
  1906. (defcustom org-outline-path-complete-in-steps t
  1907. "Non-nil means complete the outline path in hierarchical steps.
  1908. When Org-mode uses the refile interface to select an outline path
  1909. \(see variable `org-refile-use-outline-path'), the completion of
  1910. the path can be done is a single go, or if can be done in steps down
  1911. the headline hierarchy. Going in steps is probably the best if you
  1912. do not use a special completion package like `ido' or `icicles'.
  1913. However, when using these packages, going in one step can be very
  1914. fast, while still showing the whole path to the entry."
  1915. :group 'org-refile
  1916. :type 'boolean)
  1917. (defcustom org-refile-allow-creating-parent-nodes nil
  1918. "Non-nil means allow to create new nodes as refile targets.
  1919. New nodes are then created by adding \"/new node name\" to the completion
  1920. of an existing node. When the value of this variable is `confirm',
  1921. new node creation must be confirmed by the user (recommended)
  1922. When nil, the completion must match an existing entry.
  1923. Note that, if the new heading is not seen by the criteria
  1924. listed in `org-refile-targets', multiple instances of the same
  1925. heading would be created by trying again to file under the new
  1926. heading."
  1927. :group 'org-refile
  1928. :type '(choice
  1929. (const :tag "Never" nil)
  1930. (const :tag "Always" t)
  1931. (const :tag "Prompt for confirmation" confirm)))
  1932. (defcustom org-refile-active-region-within-subtree nil
  1933. "Non-nil means also refile active region within a subtree.
  1934. By default `org-refile' doesn't allow refiling regions if they
  1935. don't contain a set of subtrees, but it might be convenient to
  1936. do so sometimes: in that case, the first line of the region is
  1937. converted to a headline before refiling."
  1938. :group 'org-refile
  1939. :version "24.1"
  1940. :type 'boolean)
  1941. (defgroup org-todo nil
  1942. "Options concerning TODO items in Org-mode."
  1943. :tag "Org TODO"
  1944. :group 'org)
  1945. (defgroup org-progress nil
  1946. "Options concerning Progress logging in Org-mode."
  1947. :tag "Org Progress"
  1948. :group 'org-time)
  1949. (defvar org-todo-interpretation-widgets
  1950. '((:tag "Sequence (cycling hits every state)" sequence)
  1951. (:tag "Type (cycling directly to DONE)" type))
  1952. "The available interpretation symbols for customizing `org-todo-keywords'.
  1953. Interested libraries should add to this list.")
  1954. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1955. "List of TODO entry keyword sequences and their interpretation.
  1956. \\<org-mode-map>This is a list of sequences.
  1957. Each sequence starts with a symbol, either `sequence' or `type',
  1958. indicating if the keywords should be interpreted as a sequence of
  1959. action steps, or as different types of TODO items. The first
  1960. keywords are states requiring action - these states will select a headline
  1961. for inclusion into the global TODO list Org-mode produces. If one of
  1962. the \"keywords\" is the vertical bar, \"|\", the remaining keywords
  1963. signify that no further action is necessary. If \"|\" is not found,
  1964. the last keyword is treated as the only DONE state of the sequence.
  1965. The command \\[org-todo] cycles an entry through these states, and one
  1966. additional state where no keyword is present. For details about this
  1967. cycling, see the manual.
  1968. TODO keywords and interpretation can also be set on a per-file basis with
  1969. the special #+SEQ_TODO and #+TYP_TODO lines.
  1970. Each keyword can optionally specify a character for fast state selection
  1971. \(in combination with the variable `org-use-fast-todo-selection')
  1972. and specifiers for state change logging, using the same syntax that
  1973. is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says that
  1974. the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1975. indicates to record a time stamp each time this state is selected.
  1976. Each keyword may also specify if a timestamp or a note should be
  1977. recorded when entering or leaving the state, by adding additional
  1978. characters in the parenthesis after the keyword. This looks like this:
  1979. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1980. record only the time of the state change. With X and Y being either
  1981. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1982. Y when leaving the state if and only if the *target* state does not
  1983. define X. You may omit any of the fast-selection key or X or /Y,
  1984. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1985. For backward compatibility, this variable may also be just a list
  1986. of keywords. In this case the interpretation (sequence or type) will be
  1987. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1988. :group 'org-todo
  1989. :group 'org-keywords
  1990. :type '(choice
  1991. (repeat :tag "Old syntax, just keywords"
  1992. (string :tag "Keyword"))
  1993. (repeat :tag "New syntax"
  1994. (cons
  1995. (choice
  1996. :tag "Interpretation"
  1997. ;;Quick and dirty way to see
  1998. ;;`org-todo-interpretations'. This takes the
  1999. ;;place of item arguments
  2000. :convert-widget
  2001. (lambda (widget)
  2002. (widget-put widget
  2003. :args (mapcar
  2004. #'(lambda (x)
  2005. (widget-convert
  2006. (cons 'const x)))
  2007. org-todo-interpretation-widgets))
  2008. widget))
  2009. (repeat
  2010. (string :tag "Keyword"))))))
  2011. (defvar org-todo-keywords-1 nil
  2012. "All TODO and DONE keywords active in a buffer.")
  2013. (make-variable-buffer-local 'org-todo-keywords-1)
  2014. (defvar org-todo-keywords-for-agenda nil)
  2015. (defvar org-done-keywords-for-agenda nil)
  2016. (defvar org-drawers-for-agenda nil)
  2017. (defvar org-todo-keyword-alist-for-agenda nil)
  2018. (defvar org-tag-alist-for-agenda nil)
  2019. (defvar org-agenda-contributing-files nil)
  2020. (defvar org-not-done-keywords nil)
  2021. (make-variable-buffer-local 'org-not-done-keywords)
  2022. (defvar org-done-keywords nil)
  2023. (make-variable-buffer-local 'org-done-keywords)
  2024. (defvar org-todo-heads nil)
  2025. (make-variable-buffer-local 'org-todo-heads)
  2026. (defvar org-todo-sets nil)
  2027. (make-variable-buffer-local 'org-todo-sets)
  2028. (defvar org-todo-log-states nil)
  2029. (make-variable-buffer-local 'org-todo-log-states)
  2030. (defvar org-todo-kwd-alist nil)
  2031. (make-variable-buffer-local 'org-todo-kwd-alist)
  2032. (defvar org-todo-key-alist nil)
  2033. (make-variable-buffer-local 'org-todo-key-alist)
  2034. (defvar org-todo-key-trigger nil)
  2035. (make-variable-buffer-local 'org-todo-key-trigger)
  2036. (defcustom org-todo-interpretation 'sequence
  2037. "Controls how TODO keywords are interpreted.
  2038. This variable is in principle obsolete and is only used for
  2039. backward compatibility, if the interpretation of todo keywords is
  2040. not given already in `org-todo-keywords'. See that variable for
  2041. more information."
  2042. :group 'org-todo
  2043. :group 'org-keywords
  2044. :type '(choice (const sequence)
  2045. (const type)))
  2046. (defcustom org-use-fast-todo-selection t
  2047. "Non-nil means use the fast todo selection scheme with C-c C-t.
  2048. This variable describes if and under what circumstances the cycling
  2049. mechanism for TODO keywords will be replaced by a single-key, direct
  2050. selection scheme.
  2051. When nil, fast selection is never used.
  2052. When the symbol `prefix', it will be used when `org-todo' is called
  2053. with a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and
  2054. `C-u t' in an agenda buffer.
  2055. When t, fast selection is used by default. In this case, the prefix
  2056. argument forces cycling instead.
  2057. In all cases, the special interface is only used if access keys have
  2058. actually been assigned by the user, i.e. if keywords in the configuration
  2059. are followed by a letter in parenthesis, like TODO(t)."
  2060. :group 'org-todo
  2061. :type '(choice
  2062. (const :tag "Never" nil)
  2063. (const :tag "By default" t)
  2064. (const :tag "Only with C-u C-c C-t" prefix)))
  2065. (defcustom org-provide-todo-statistics t
  2066. "Non-nil means update todo statistics after insert and toggle.
  2067. ALL-HEADLINES means update todo statistics by including headlines
  2068. with no TODO keyword as well, counting them as not done.
  2069. A list of TODO keywords means the same, but skip keywords that are
  2070. not in this list.
  2071. When this is set, todo statistics is updated in the parent of the
  2072. current entry each time a todo state is changed."
  2073. :group 'org-todo
  2074. :type '(choice
  2075. (const :tag "Yes, only for TODO entries" t)
  2076. (const :tag "Yes, including all entries" 'all-headlines)
  2077. (repeat :tag "Yes, for TODOs in this list"
  2078. (string :tag "TODO keyword"))
  2079. (other :tag "No TODO statistics" nil)))
  2080. (defcustom org-hierarchical-todo-statistics t
  2081. "Non-nil means TODO statistics covers just direct children.
  2082. When nil, all entries in the subtree are considered.
  2083. This has only an effect if `org-provide-todo-statistics' is set.
  2084. To set this to nil for only a single subtree, use a COOKIE_DATA
  2085. property and include the word \"recursive\" into the value."
  2086. :group 'org-todo
  2087. :type 'boolean)
  2088. (defcustom org-after-todo-state-change-hook nil
  2089. "Hook which is run after the state of a TODO item was changed.
  2090. The new state (a string with a TODO keyword, or nil) is available in the
  2091. Lisp variable `org-state'."
  2092. :group 'org-todo
  2093. :type 'hook)
  2094. (defvar org-blocker-hook nil
  2095. "Hook for functions that are allowed to block a state change.
  2096. Functions in this hook should not modify the buffer.
  2097. Each function gets as its single argument a property list,
  2098. see `org-trigger-hook' for more information about this list.
  2099. If any of the functions in this hook returns nil, the state change
  2100. is blocked.")
  2101. (defvar org-trigger-hook nil
  2102. "Hook for functions that are triggered by a state change.
  2103. Each function gets as its single argument a property list with at
  2104. least the following elements:
  2105. (:type type-of-change :position pos-at-entry-start
  2106. :from old-state :to new-state)
  2107. Depending on the type, more properties may be present.
  2108. This mechanism is currently implemented for:
  2109. TODO state changes
  2110. ------------------
  2111. :type todo-state-change
  2112. :from previous state (keyword as a string), or nil, or a symbol
  2113. 'todo' or 'done', to indicate the general type of state.
  2114. :to new state, like in :from")
  2115. (defcustom org-enforce-todo-dependencies nil
  2116. "Non-nil means undone TODO entries will block switching the parent to DONE.
  2117. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  2118. be blocked if any prior sibling is not yet done.
  2119. Finally, if the parent is blocked because of ordered siblings of its own,
  2120. the child will also be blocked."
  2121. :set (lambda (var val)
  2122. (set var val)
  2123. (if val
  2124. (add-hook 'org-blocker-hook
  2125. 'org-block-todo-from-children-or-siblings-or-parent)
  2126. (remove-hook 'org-blocker-hook
  2127. 'org-block-todo-from-children-or-siblings-or-parent)))
  2128. :group 'org-todo
  2129. :type 'boolean)
  2130. (defcustom org-enforce-todo-checkbox-dependencies nil
  2131. "Non-nil means unchecked boxes will block switching the parent to DONE.
  2132. When this is nil, checkboxes have no influence on switching TODO states.
  2133. When non-nil, you first need to check off all check boxes before the TODO
  2134. entry can be switched to DONE.
  2135. This variable needs to be set before org.el is loaded, and you need to
  2136. restart Emacs after a change to make the change effective. The only way
  2137. to change is while Emacs is running is through the customize interface."
  2138. :set (lambda (var val)
  2139. (set var val)
  2140. (if val
  2141. (add-hook 'org-blocker-hook
  2142. 'org-block-todo-from-checkboxes)
  2143. (remove-hook 'org-blocker-hook
  2144. 'org-block-todo-from-checkboxes)))
  2145. :group 'org-todo
  2146. :type 'boolean)
  2147. (defcustom org-treat-insert-todo-heading-as-state-change nil
  2148. "Non-nil means inserting a TODO heading is treated as state change.
  2149. So when the command \\[org-insert-todo-heading] is used, state change
  2150. logging will apply if appropriate. When nil, the new TODO item will
  2151. be inserted directly, and no logging will take place."
  2152. :group 'org-todo
  2153. :type 'boolean)
  2154. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  2155. "Non-nil means switching TODO states with S-cursor counts as state change.
  2156. This is the default behavior. However, setting this to nil allows a
  2157. convenient way to select a TODO state and bypass any logging associated
  2158. with that."
  2159. :group 'org-todo
  2160. :type 'boolean)
  2161. (defcustom org-todo-state-tags-triggers nil
  2162. "Tag changes that should be triggered by TODO state changes.
  2163. This is a list. Each entry is
  2164. (state-change (tag . flag) .......)
  2165. State-change can be a string with a state, and empty string to indicate the
  2166. state that has no TODO keyword, or it can be one of the symbols `todo'
  2167. or `done', meaning any not-done or done state, respectively."
  2168. :group 'org-todo
  2169. :group 'org-tags
  2170. :type '(repeat
  2171. (cons (choice :tag "When changing to"
  2172. (const :tag "Not-done state" todo)
  2173. (const :tag "Done state" done)
  2174. (string :tag "State"))
  2175. (repeat
  2176. (cons :tag "Tag action"
  2177. (string :tag "Tag")
  2178. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  2179. (defcustom org-log-done nil
  2180. "Information to record when a task moves to the DONE state.
  2181. Possible values are:
  2182. nil Don't add anything, just change the keyword
  2183. time Add a time stamp to the task
  2184. note Prompt for a note and add it with template `org-log-note-headings'
  2185. This option can also be set with on a per-file-basis with
  2186. #+STARTUP: nologdone
  2187. #+STARTUP: logdone
  2188. #+STARTUP: lognotedone
  2189. You can have local logging settings for a subtree by setting the LOGGING
  2190. property to one or more of these keywords."
  2191. :group 'org-todo
  2192. :group 'org-progress
  2193. :type '(choice
  2194. (const :tag "No logging" nil)
  2195. (const :tag "Record CLOSED timestamp" time)
  2196. (const :tag "Record CLOSED timestamp with note." note)))
  2197. ;; Normalize old uses of org-log-done.
  2198. (cond
  2199. ((eq org-log-done t) (setq org-log-done 'time))
  2200. ((and (listp org-log-done) (memq 'done org-log-done))
  2201. (setq org-log-done 'note)))
  2202. (defcustom org-log-reschedule nil
  2203. "Information to record when the scheduling date of a tasks is modified.
  2204. Possible values are:
  2205. nil Don't add anything, just change the date
  2206. time Add a time stamp to the task
  2207. note Prompt for a note and add it with template `org-log-note-headings'
  2208. This option can also be set with on a per-file-basis with
  2209. #+STARTUP: nologreschedule
  2210. #+STARTUP: logreschedule
  2211. #+STARTUP: lognotereschedule"
  2212. :group 'org-todo
  2213. :group 'org-progress
  2214. :type '(choice
  2215. (const :tag "No logging" nil)
  2216. (const :tag "Record timestamp" time)
  2217. (const :tag "Record timestamp with note." note)))
  2218. (defcustom org-log-redeadline nil
  2219. "Information to record when the deadline date of a tasks is modified.
  2220. Possible values are:
  2221. nil Don't add anything, just change the date
  2222. time Add a time stamp to the task
  2223. note Prompt for a note and add it with template `org-log-note-headings'
  2224. This option can also be set with on a per-file-basis with
  2225. #+STARTUP: nologredeadline
  2226. #+STARTUP: logredeadline
  2227. #+STARTUP: lognoteredeadline
  2228. You can have local logging settings for a subtree by setting the LOGGING
  2229. property to one or more of these keywords."
  2230. :group 'org-todo
  2231. :group 'org-progress
  2232. :type '(choice
  2233. (const :tag "No logging" nil)
  2234. (const :tag "Record timestamp" time)
  2235. (const :tag "Record timestamp with note." note)))
  2236. (defcustom org-log-note-clock-out nil
  2237. "Non-nil means record a note when clocking out of an item.
  2238. This can also be configured on a per-file basis by adding one of
  2239. the following lines anywhere in the buffer:
  2240. #+STARTUP: lognoteclock-out
  2241. #+STARTUP: nolognoteclock-out"
  2242. :group 'org-todo
  2243. :group 'org-progress
  2244. :type 'boolean)
  2245. (defcustom org-log-done-with-time t
  2246. "Non-nil means the CLOSED time stamp will contain date and time.
  2247. When nil, only the date will be recorded."
  2248. :group 'org-progress
  2249. :type 'boolean)
  2250. (defcustom org-log-note-headings
  2251. '((done . "CLOSING NOTE %t")
  2252. (state . "State %-12s from %-12S %t")
  2253. (note . "Note taken on %t")
  2254. (reschedule . "Rescheduled from %S on %t")
  2255. (delschedule . "Not scheduled, was %S on %t")
  2256. (redeadline . "New deadline from %S on %t")
  2257. (deldeadline . "Removed deadline, was %S on %t")
  2258. (refile . "Refiled on %t")
  2259. (clock-out . ""))
  2260. "Headings for notes added to entries.
  2261. The value is an alist, with the car being a symbol indicating the note
  2262. context, and the cdr is the heading to be used. The heading may also be the
  2263. empty string.
  2264. %t in the heading will be replaced by a time stamp.
  2265. %T will be an active time stamp instead the default inactive one
  2266. %d will be replaced by a short-format time stamp.
  2267. %D will be replaced by an active short-format time stamp.
  2268. %s will be replaced by the new TODO state, in double quotes.
  2269. %S will be replaced by the old TODO state, in double quotes.
  2270. %u will be replaced by the user name.
  2271. %U will be replaced by the full user name.
  2272. In fact, it is not a good idea to change the `state' entry, because
  2273. agenda log mode depends on the format of these entries."
  2274. :group 'org-todo
  2275. :group 'org-progress
  2276. :type '(list :greedy t
  2277. (cons (const :tag "Heading when closing an item" done) string)
  2278. (cons (const :tag
  2279. "Heading when changing todo state (todo sequence only)"
  2280. state) string)
  2281. (cons (const :tag "Heading when just taking a note" note) string)
  2282. (cons (const :tag "Heading when clocking out" clock-out) string)
  2283. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2284. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2285. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2286. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2287. (cons (const :tag "Heading when refiling" refile) string)))
  2288. (unless (assq 'note org-log-note-headings)
  2289. (push '(note . "%t") org-log-note-headings))
  2290. (defcustom org-log-into-drawer nil
  2291. "Non-nil means insert state change notes and time stamps into a drawer.
  2292. When nil, state changes notes will be inserted after the headline and
  2293. any scheduling and clock lines, but not inside a drawer.
  2294. The value of this variable should be the name of the drawer to use.
  2295. LOGBOOK is proposed as the default drawer for this purpose, you can
  2296. also set this to a string to define the drawer of your choice.
  2297. A value of t is also allowed, representing \"LOGBOOK\".
  2298. A value of t or nil can also be set with on a per-file-basis with
  2299. #+STARTUP: logdrawer
  2300. #+STARTUP: nologdrawer
  2301. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2302. will be ignored.
  2303. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2304. a subtree."
  2305. :group 'org-todo
  2306. :group 'org-progress
  2307. :type '(choice
  2308. (const :tag "Not into a drawer" nil)
  2309. (const :tag "LOGBOOK" t)
  2310. (string :tag "Other")))
  2311. (org-defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer)
  2312. (defun org-log-into-drawer ()
  2313. "Return the value of `org-log-into-drawer', but let properties overrule.
  2314. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  2315. used instead of the default value."
  2316. (let ((p (org-entry-get nil "LOG_INTO_DRAWER" 'inherit t)))
  2317. (cond
  2318. ((not p) org-log-into-drawer)
  2319. ((equal p "nil") nil)
  2320. ((equal p "t") "LOGBOOK")
  2321. (t p))))
  2322. (defcustom org-log-state-notes-insert-after-drawers nil
  2323. "Non-nil means insert state change notes after any drawers in entry.
  2324. Only the drawers that *immediately* follow the headline and the
  2325. deadline/scheduled line are skipped.
  2326. When nil, insert notes right after the heading and perhaps the line
  2327. with deadline/scheduling if present.
  2328. This variable will have no effect if `org-log-into-drawer' is
  2329. set."
  2330. :group 'org-todo
  2331. :group 'org-progress
  2332. :type 'boolean)
  2333. (defcustom org-log-states-order-reversed t
  2334. "Non-nil means the latest state note will be directly after heading.
  2335. When nil, the state change notes will be ordered according to time.
  2336. This option can also be set with on a per-file-basis with
  2337. #+STARTUP: logstatesreversed
  2338. #+STARTUP: nologstatesreversed"
  2339. :group 'org-todo
  2340. :group 'org-progress
  2341. :type 'boolean)
  2342. (defcustom org-todo-repeat-to-state nil
  2343. "The TODO state to which a repeater should return the repeating task.
  2344. By default this is the first task in a TODO sequence, or the previous state
  2345. in a TODO_TYP set. But you can specify another task here.
  2346. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2347. :group 'org-todo
  2348. :version "24.1"
  2349. :type '(choice (const :tag "Head of sequence" nil)
  2350. (string :tag "Specific state")))
  2351. (defcustom org-log-repeat 'time
  2352. "Non-nil means record moving through the DONE state when triggering repeat.
  2353. An auto-repeating task is immediately switched back to TODO when
  2354. marked DONE. If you are not logging state changes (by adding \"@\"
  2355. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2356. record a closing note, there will be no record of the task moving
  2357. through DONE. This variable forces taking a note anyway.
  2358. nil Don't force a record
  2359. time Record a time stamp
  2360. note Prompt for a note and add it with template `org-log-note-headings'
  2361. This option can also be set with on a per-file-basis with
  2362. #+STARTUP: nologrepeat
  2363. #+STARTUP: logrepeat
  2364. #+STARTUP: lognoterepeat
  2365. You can have local logging settings for a subtree by setting the LOGGING
  2366. property to one or more of these keywords."
  2367. :group 'org-todo
  2368. :group 'org-progress
  2369. :type '(choice
  2370. (const :tag "Don't force a record" nil)
  2371. (const :tag "Force recording the DONE state" time)
  2372. (const :tag "Force recording a note with the DONE state" note)))
  2373. (defgroup org-priorities nil
  2374. "Priorities in Org-mode."
  2375. :tag "Org Priorities"
  2376. :group 'org-todo)
  2377. (defcustom org-enable-priority-commands t
  2378. "Non-nil means priority commands are active.
  2379. When nil, these commands will be disabled, so that you never accidentally
  2380. set a priority."
  2381. :group 'org-priorities
  2382. :type 'boolean)
  2383. (defcustom org-highest-priority ?A
  2384. "The highest priority of TODO items. A character like ?A, ?B etc.
  2385. Must have a smaller ASCII number than `org-lowest-priority'."
  2386. :group 'org-priorities
  2387. :type 'character)
  2388. (defcustom org-lowest-priority ?C
  2389. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2390. Must have a larger ASCII number than `org-highest-priority'."
  2391. :group 'org-priorities
  2392. :type 'character)
  2393. (defcustom org-default-priority ?B
  2394. "The default priority of TODO items.
  2395. This is the priority an item gets if no explicit priority is given.
  2396. When starting to cycle on an empty priority the first step in the cycle
  2397. depends on `org-priority-start-cycle-with-default'. The resulting first
  2398. step priority must not exceed the range from `org-highest-priority' to
  2399. `org-lowest-priority' which means that `org-default-priority' has to be
  2400. in this range exclusive or inclusive the range boundaries. Else the
  2401. first step refuses to set the default and the second will fall back
  2402. to (depending on the command used) the highest or lowest priority."
  2403. :group 'org-priorities
  2404. :type 'character)
  2405. (defcustom org-priority-start-cycle-with-default t
  2406. "Non-nil means start with default priority when starting to cycle.
  2407. When this is nil, the first step in the cycle will be (depending on the
  2408. command used) one higher or lower than the default priority.
  2409. See also `org-default-priority'."
  2410. :group 'org-priorities
  2411. :type 'boolean)
  2412. (defcustom org-get-priority-function nil
  2413. "Function to extract the priority from a string.
  2414. The string is normally the headline. If this is nil Org computes the
  2415. priority from the priority cookie like [#A] in the headline. It returns
  2416. an integer, increasing by 1000 for each priority level.
  2417. The user can set a different function here, which should take a string
  2418. as an argument and return the numeric priority."
  2419. :group 'org-priorities
  2420. :version "24.1"
  2421. :type 'function)
  2422. (defgroup org-time nil
  2423. "Options concerning time stamps and deadlines in Org-mode."
  2424. :tag "Org Time"
  2425. :group 'org)
  2426. (defcustom org-insert-labeled-timestamps-at-point nil
  2427. "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
  2428. When nil, these labeled time stamps are forces into the second line of an
  2429. entry, just after the headline. When scheduling from the global TODO list,
  2430. the time stamp will always be forced into the second line."
  2431. :group 'org-time
  2432. :type 'boolean)
  2433. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  2434. "Formats for `format-time-string' which are used for time stamps.
  2435. It is not recommended to change this constant.")
  2436. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2437. "Number of minutes to round time stamps to.
  2438. These are two values, the first applies when first creating a time stamp.
  2439. The second applies when changing it with the commands `S-up' and `S-down'.
  2440. When changing the time stamp, this means that it will change in steps
  2441. of N minutes, as given by the second value.
  2442. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2443. numbers should be factors of 60, so for example 5, 10, 15.
  2444. When this is larger than 1, you can still force an exact time stamp by using
  2445. a double prefix argument to a time stamp command like `C-c .' or `C-c !',
  2446. and by using a prefix arg to `S-up/down' to specify the exact number
  2447. of minutes to shift."
  2448. :group 'org-time
  2449. :get #'(lambda (var) ; Make sure both elements are there
  2450. (if (integerp (default-value var))
  2451. (list (default-value var) 5)
  2452. (default-value var)))
  2453. :type '(list
  2454. (integer :tag "when inserting times")
  2455. (integer :tag "when modifying times")))
  2456. ;; Normalize old customizations of this variable.
  2457. (when (integerp org-time-stamp-rounding-minutes)
  2458. (setq org-time-stamp-rounding-minutes
  2459. (list org-time-stamp-rounding-minutes
  2460. org-time-stamp-rounding-minutes)))
  2461. (defcustom org-display-custom-times nil
  2462. "Non-nil means overlay custom formats over all time stamps.
  2463. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2464. To turn this on on a per-file basis, insert anywhere in the file:
  2465. #+STARTUP: customtime"
  2466. :group 'org-time
  2467. :set 'set-default
  2468. :type 'sexp)
  2469. (make-variable-buffer-local 'org-display-custom-times)
  2470. (defcustom org-time-stamp-custom-formats
  2471. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2472. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2473. These are overlaid over the default ISO format if the variable
  2474. `org-display-custom-times' is set. Time like %H:%M should be at the
  2475. end of the second format. The custom formats are also honored by export
  2476. commands, if custom time display is turned on at the time of export."
  2477. :group 'org-time
  2478. :type 'sexp)
  2479. (defun org-time-stamp-format (&optional long inactive)
  2480. "Get the right format for a time string."
  2481. (let ((f (if long (cdr org-time-stamp-formats)
  2482. (car org-time-stamp-formats))))
  2483. (if inactive
  2484. (concat "[" (substring f 1 -1) "]")
  2485. f)))
  2486. (defcustom org-time-clocksum-format
  2487. '(:days "%dd " :hours "%d" :require-hours t :minutes ":%02d" :require-minutes t)
  2488. "The format string used when creating CLOCKSUM lines.
  2489. This is also used when Org mode generates a time duration.
  2490. The value can be a single format string containing two
  2491. %-sequences, which will be filled with the number of hours and
  2492. minutes in that order.
  2493. Alternatively, the value can be a plist associating any of the
  2494. keys :years, :months, :weeks, :days, :hours or :minutes with
  2495. format strings. The time duration is formatted using only the
  2496. time components that are needed and concatenating the results.
  2497. If a time unit in absent, it falls back to the next smallest
  2498. unit.
  2499. The keys :require-years, :require-months, :require-days,
  2500. :require-weeks, :require-hours, :require-minutes are also
  2501. meaningful. A non-nil value for these keys indicates that the
  2502. corresponding time component should always be included, even if
  2503. its value is 0.
  2504. For example,
  2505. \(:days \"%dd\" :hours \"%d\" :require-hours t :minutes \":%02d\"
  2506. :require-minutes t)
  2507. means durations longer than a day will be expressed in days,
  2508. hours and minutes, and durations less than a day will always be
  2509. expressed in hours and minutes (even for durations less than an
  2510. hour).
  2511. The value
  2512. \(:days \"%dd\" :minutes \"%dm\")
  2513. means durations longer than a day will be expressed in days and
  2514. minutes, and durations less than a day will be expressed entirely
  2515. in minutes (even for durations longer than an hour)."
  2516. :group 'org-time
  2517. :group 'org-clock
  2518. :version "24.4"
  2519. :package-version '(Org . "8.0")
  2520. :type '(choice (string :tag "Format string")
  2521. (set :tag "Plist"
  2522. (group :inline t (const :tag "Years" :years)
  2523. (string :tag "Format string"))
  2524. (group :inline t
  2525. (const :tag "Always show years" :require-years)
  2526. (const t))
  2527. (group :inline t (const :tag "Months" :months)
  2528. (string :tag "Format string"))
  2529. (group :inline t
  2530. (const :tag "Always show months" :require-months)
  2531. (const t))
  2532. (group :inline t (const :tag "Weeks" :weeks)
  2533. (string :tag "Format string"))
  2534. (group :inline t
  2535. (const :tag "Always show weeks" :require-weeks)
  2536. (const t))
  2537. (group :inline t (const :tag "Days" :days)
  2538. (string :tag "Format string"))
  2539. (group :inline t
  2540. (const :tag "Always show days" :require-days)
  2541. (const t))
  2542. (group :inline t (const :tag "Hours" :hours)
  2543. (string :tag "Format string"))
  2544. (group :inline t
  2545. (const :tag "Always show hours" :require-hours)
  2546. (const t))
  2547. (group :inline t (const :tag "Minutes" :minutes)
  2548. (string :tag "Format string"))
  2549. (group :inline t
  2550. (const :tag "Always show minutes" :require-minutes)
  2551. (const t)))))
  2552. (defcustom org-time-clocksum-use-fractional nil
  2553. "When non-nil, \\[org-clock-display] uses fractional times.
  2554. See `org-time-clocksum-format' for more on time clock formats."
  2555. :group 'org-time
  2556. :group 'org-clock
  2557. :version "24.3"
  2558. :type 'boolean)
  2559. (defcustom org-time-clocksum-use-effort-durations nil
  2560. "When non-nil, \\[org-clock-display] uses effort durations.
  2561. E.g. by default, one day is considered to be a 8 hours effort,
  2562. so a task that has been clocked for 16 hours will be displayed
  2563. as during 2 days in the clock display or in the clocktable.
  2564. See `org-effort-durations' on how to set effort durations
  2565. and `org-time-clocksum-format' for more on time clock formats."
  2566. :group 'org-time
  2567. :group 'org-clock
  2568. :version "24.4"
  2569. :package-version '(Org . "8.0")
  2570. :type 'boolean)
  2571. (defcustom org-time-clocksum-fractional-format "%.2f"
  2572. "The format string used when creating CLOCKSUM lines,
  2573. or when Org mode generates a time duration, if
  2574. `org-time-clocksum-use-fractional' is enabled.
  2575. The value can be a single format string containing one
  2576. %-sequence, which will be filled with the number of hours as
  2577. a float.
  2578. Alternatively, the value can be a plist associating any of the
  2579. keys :years, :months, :weeks, :days, :hours or :minutes with
  2580. a format string. The time duration is formatted using the
  2581. largest time unit which gives a non-zero integer part. If all
  2582. specified formats have zero integer part, the smallest time unit
  2583. is used."
  2584. :group 'org-time
  2585. :type '(choice (string :tag "Format string")
  2586. (set (group :inline t (const :tag "Years" :years)
  2587. (string :tag "Format string"))
  2588. (group :inline t (const :tag "Months" :months)
  2589. (string :tag "Format string"))
  2590. (group :inline t (const :tag "Weeks" :weeks)
  2591. (string :tag "Format string"))
  2592. (group :inline t (const :tag "Days" :days)
  2593. (string :tag "Format string"))
  2594. (group :inline t (const :tag "Hours" :hours)
  2595. (string :tag "Format string"))
  2596. (group :inline t (const :tag "Minutes" :minutes)
  2597. (string :tag "Format string")))))
  2598. (defcustom org-deadline-warning-days 14
  2599. "Number of days before expiration during which a deadline becomes active.
  2600. This variable governs the display in sparse trees and in the agenda.
  2601. When 0 or negative, it means use this number (the absolute value of it)
  2602. even if a deadline has a different individual lead time specified.
  2603. Custom commands can set this variable in the options section."
  2604. :group 'org-time
  2605. :group 'org-agenda-daily/weekly
  2606. :type 'integer)
  2607. (defcustom org-scheduled-delay-days 0
  2608. "Number of days before a scheduled item becomes active.
  2609. This variable governs the display in sparse trees and in the agenda.
  2610. The default value (i.e. 0) means: don't delay scheduled item.
  2611. When negative, it means use this number (the absolute value of it)
  2612. even if a scheduled item has a different individual delay time
  2613. specified.
  2614. Custom commands can set this variable in the options section."
  2615. :group 'org-time
  2616. :group 'org-agenda-daily/weekly
  2617. :version "24.4"
  2618. :package-version '(Org . "8.0")
  2619. :type 'integer)
  2620. (defcustom org-read-date-prefer-future t
  2621. "Non-nil means assume future for incomplete date input from user.
  2622. This affects the following situations:
  2623. 1. The user gives a month but not a year.
  2624. For example, if it is April and you enter \"feb 2\", this will be read
  2625. as Feb 2, *next* year. \"May 5\", however, will be this year.
  2626. 2. The user gives a day, but no month.
  2627. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2628. read this as the third of *next* month. However, if you enter \"17\",
  2629. it will be considered as *this* month.
  2630. If you set this variable to the symbol `time', then also the following
  2631. will work:
  2632. 3. If the user gives a time.
  2633. If the time is before now, it will be interpreted as tomorrow.
  2634. Currently none of this works for ISO week specifications.
  2635. When this option is nil, the current day, month and year will always be
  2636. used as defaults.
  2637. See also `org-agenda-jump-prefer-future'."
  2638. :group 'org-time
  2639. :type '(choice
  2640. (const :tag "Never" nil)
  2641. (const :tag "Check month and day" t)
  2642. (const :tag "Check month, day, and time" time)))
  2643. (defcustom org-agenda-jump-prefer-future 'org-read-date-prefer-future
  2644. "Should the agenda jump command prefer the future for incomplete dates?
  2645. The default is to do the same as configured in `org-read-date-prefer-future'.
  2646. But you can also set a deviating value here.
  2647. This may t or nil, or the symbol `org-read-date-prefer-future'."
  2648. :group 'org-agenda
  2649. :group 'org-time
  2650. :version "24.1"
  2651. :type '(choice
  2652. (const :tag "Use org-read-date-prefer-future"
  2653. org-read-date-prefer-future)
  2654. (const :tag "Never" nil)
  2655. (const :tag "Always" t)))
  2656. (defcustom org-read-date-force-compatible-dates t
  2657. "Should date/time prompt force dates that are guaranteed to work in Emacs?
  2658. Depending on the system Emacs is running on, certain dates cannot
  2659. be represented with the type used internally to represent time.
  2660. Dates between 1970-1-1 and 2038-1-1 can always be represented
  2661. correctly. Some systems allow for earlier dates, some for later,
  2662. some for both. One way to find out it to insert any date into an
  2663. Org buffer, putting the cursor on the year and hitting S-up and
  2664. S-down to test the range.
  2665. When this variable is set to t, the date/time prompt will not let
  2666. you specify dates outside the 1970-2037 range, so it is certain that
  2667. these dates will work in whatever version of Emacs you are
  2668. running, and also that you can move a file from one Emacs implementation
  2669. to another. WHenever Org is forcing the year for you, it will display
  2670. a message and beep.
  2671. When this variable is nil, Org will check if the date is
  2672. representable in the specific Emacs implementation you are using.
  2673. If not, it will force a year, usually the current year, and beep
  2674. to remind you. Currently this setting is not recommended because
  2675. the likelihood that you will open your Org files in an Emacs that
  2676. has limited date range is not negligible.
  2677. A workaround for this problem is to use diary sexp dates for time
  2678. stamps outside of this range."
  2679. :group 'org-time
  2680. :version "24.1"
  2681. :type 'boolean)
  2682. (defcustom org-read-date-display-live t
  2683. "Non-nil means display current interpretation of date prompt live.
  2684. This display will be in an overlay, in the minibuffer."
  2685. :group 'org-time
  2686. :type 'boolean)
  2687. (defcustom org-read-date-popup-calendar t
  2688. "Non-nil means pop up a calendar when prompting for a date.
  2689. In the calendar, the date can be selected with mouse-1. However, the
  2690. minibuffer will also be active, and you can simply enter the date as well.
  2691. When nil, only the minibuffer will be available."
  2692. :group 'org-time
  2693. :type 'boolean)
  2694. (org-defvaralias 'org-popup-calendar-for-date-prompt
  2695. 'org-read-date-popup-calendar)
  2696. (make-obsolete-variable
  2697. 'org-read-date-minibuffer-setup-hook
  2698. "Set `org-read-date-minibuffer-local-map' instead." "24.4")
  2699. (defcustom org-read-date-minibuffer-setup-hook nil
  2700. "Hook to be used to set up keys for the date/time interface.
  2701. Add key definitions to `minibuffer-local-map', which will be a
  2702. temporary copy.
  2703. WARNING: This option is obsolete, you should use
  2704. `org-read-date-minibuffer-local-map' to set up keys."
  2705. :group 'org-time
  2706. :type 'hook)
  2707. (defcustom org-extend-today-until 0
  2708. "The hour when your day really ends. Must be an integer.
  2709. This has influence for the following applications:
  2710. - When switching the agenda to \"today\". It it is still earlier than
  2711. the time given here, the day recognized as TODAY is actually yesterday.
  2712. - When a date is read from the user and it is still before the time given
  2713. here, the current date and time will be assumed to be yesterday, 23:59.
  2714. Also, timestamps inserted in capture templates follow this rule.
  2715. IMPORTANT: This is a feature whose implementation is and likely will
  2716. remain incomplete. Really, it is only here because past midnight seems to
  2717. be the favorite working time of John Wiegley :-)"
  2718. :group 'org-time
  2719. :type 'integer)
  2720. (defcustom org-use-effective-time nil
  2721. "If non-nil, consider `org-extend-today-until' when creating timestamps.
  2722. For example, if `org-extend-today-until' is 8, and it's 4am, then the
  2723. \"effective time\" of any timestamps between midnight and 8am will be
  2724. 23:59 of the previous day."
  2725. :group 'org-time
  2726. :version "24.1"
  2727. :type 'boolean)
  2728. (defcustom org-use-last-clock-out-time-as-effective-time nil
  2729. "When non-nil, use the last clock out time for `org-todo'.
  2730. Note that this option has precedence over the combined use of
  2731. `org-use-effective-time' and `org-extend-today-until'."
  2732. :group 'org-time
  2733. :version "24.4"
  2734. :package-version '(Org . "8.0")
  2735. :type 'boolean)
  2736. (defcustom org-edit-timestamp-down-means-later nil
  2737. "Non-nil means S-down will increase the time in a time stamp.
  2738. When nil, S-up will increase."
  2739. :group 'org-time
  2740. :type 'boolean)
  2741. (defcustom org-calendar-follow-timestamp-change t
  2742. "Non-nil means make the calendar window follow timestamp changes.
  2743. When a timestamp is modified and the calendar window is visible, it will be
  2744. moved to the new date."
  2745. :group 'org-time
  2746. :type 'boolean)
  2747. (defgroup org-tags nil
  2748. "Options concerning tags in Org-mode."
  2749. :tag "Org Tags"
  2750. :group 'org)
  2751. (defcustom org-tag-alist nil
  2752. "List of tags allowed in Org-mode files.
  2753. When this list is nil, Org-mode will base TAG input on what is already in the
  2754. buffer.
  2755. The value of this variable is an alist, the car of each entry must be a
  2756. keyword as a string, the cdr may be a character that is used to select
  2757. that tag through the fast-tag-selection interface.
  2758. See the manual for details."
  2759. :group 'org-tags
  2760. :type '(repeat
  2761. (choice
  2762. (cons (string :tag "Tag name")
  2763. (character :tag "Access char"))
  2764. (list :tag "Start radio group"
  2765. (const :startgroup)
  2766. (option (string :tag "Group description")))
  2767. (list :tag "End radio group"
  2768. (const :endgroup)
  2769. (option (string :tag "Group description")))
  2770. (const :tag "New line" (:newline)))))
  2771. (defcustom org-tag-persistent-alist nil
  2772. "List of tags that will always appear in all Org-mode files.
  2773. This is in addition to any in buffer settings or customizations
  2774. of `org-tag-alist'.
  2775. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  2776. The value of this variable is an alist, the car of each entry must be a
  2777. keyword as a string, the cdr may be a character that is used to select
  2778. that tag through the fast-tag-selection interface.
  2779. See the manual for details.
  2780. To disable these tags on a per-file basis, insert anywhere in the file:
  2781. #+STARTUP: noptag"
  2782. :group 'org-tags
  2783. :type '(repeat
  2784. (choice
  2785. (cons (string :tag "Tag name")
  2786. (character :tag "Access char"))
  2787. (const :tag "Start radio group" (:startgroup))
  2788. (const :tag "End radio group" (:endgroup))
  2789. (const :tag "New line" (:newline)))))
  2790. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  2791. "If non-nil, always offer completion for all tags of all agenda files.
  2792. Instead of customizing this variable directly, you might want to
  2793. set it locally for capture buffers, because there no list of
  2794. tags in that file can be created dynamically (there are none).
  2795. (add-hook 'org-capture-mode-hook
  2796. (lambda ()
  2797. (set (make-local-variable
  2798. 'org-complete-tags-always-offer-all-agenda-tags)
  2799. t)))"
  2800. :group 'org-tags
  2801. :version "24.1"
  2802. :type 'boolean)
  2803. (defvar org-file-tags nil
  2804. "List of tags that can be inherited by all entries in the file.
  2805. The tags will be inherited if the variable `org-use-tag-inheritance'
  2806. says they should be.
  2807. This variable is populated from #+FILETAGS lines.")
  2808. (defcustom org-use-fast-tag-selection 'auto
  2809. "Non-nil means use fast tag selection scheme.
  2810. This is a special interface to select and deselect tags with single keys.
  2811. When nil, fast selection is never used.
  2812. When the symbol `auto', fast selection is used if and only if selection
  2813. characters for tags have been configured, either through the variable
  2814. `org-tag-alist' or through a #+TAGS line in the buffer.
  2815. When t, fast selection is always used and selection keys are assigned
  2816. automatically if necessary."
  2817. :group 'org-tags
  2818. :type '(choice
  2819. (const :tag "Always" t)
  2820. (const :tag "Never" nil)
  2821. (const :tag "When selection characters are configured" 'auto)))
  2822. (defcustom org-fast-tag-selection-single-key nil
  2823. "Non-nil means fast tag selection exits after first change.
  2824. When nil, you have to press RET to exit it.
  2825. During fast tag selection, you can toggle this flag with `C-c'.
  2826. This variable can also have the value `expert'. In this case, the window
  2827. displaying the tags menu is not even shown, until you press C-c again."
  2828. :group 'org-tags
  2829. :type '(choice
  2830. (const :tag "No" nil)
  2831. (const :tag "Yes" t)
  2832. (const :tag "Expert" expert)))
  2833. (defvar org-fast-tag-selection-include-todo nil
  2834. "Non-nil means fast tags selection interface will also offer TODO states.
  2835. This is an undocumented feature, you should not rely on it.")
  2836. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2837. "The column to which tags should be indented in a headline.
  2838. If this number is positive, it specifies the column. If it is negative,
  2839. it means that the tags should be flushright to that column. For example,
  2840. -80 works well for a normal 80 character screen.
  2841. When 0, place tags directly after headline text, with only one space in
  2842. between."
  2843. :group 'org-tags
  2844. :type 'integer)
  2845. (defcustom org-auto-align-tags t
  2846. "Non-nil keeps tags aligned when modifying headlines.
  2847. Some operations (i.e. demoting) change the length of a headline and
  2848. therefore shift the tags around. With this option turned on, after
  2849. each such operation the tags are again aligned to `org-tags-column'."
  2850. :group 'org-tags
  2851. :type 'boolean)
  2852. (defcustom org-use-tag-inheritance t
  2853. "Non-nil means tags in levels apply also for sublevels.
  2854. When nil, only the tags directly given in a specific line apply there.
  2855. This may also be a list of tags that should be inherited, or a regexp that
  2856. matches tags that should be inherited. Additional control is possible
  2857. with the variable `org-tags-exclude-from-inheritance' which gives an
  2858. explicit list of tags to be excluded from inheritance, even if the value of
  2859. `org-use-tag-inheritance' would select it for inheritance.
  2860. If this option is t, a match early-on in a tree can lead to a large
  2861. number of matches in the subtree when constructing the agenda or creating
  2862. a sparse tree. If you only want to see the first match in a tree during
  2863. a search, check out the variable `org-tags-match-list-sublevels'."
  2864. :group 'org-tags
  2865. :type '(choice
  2866. (const :tag "Not" nil)
  2867. (const :tag "Always" t)
  2868. (repeat :tag "Specific tags" (string :tag "Tag"))
  2869. (regexp :tag "Tags matched by regexp")))
  2870. (defcustom org-tags-exclude-from-inheritance nil
  2871. "List of tags that should never be inherited.
  2872. This is a way to exclude a few tags from inheritance. For way to do
  2873. the opposite, to actively allow inheritance for selected tags,
  2874. see the variable `org-use-tag-inheritance'."
  2875. :group 'org-tags
  2876. :type '(repeat (string :tag "Tag")))
  2877. (defun org-tag-inherit-p (tag)
  2878. "Check if TAG is one that should be inherited."
  2879. (cond
  2880. ((member tag org-tags-exclude-from-inheritance) nil)
  2881. ((eq org-use-tag-inheritance t) t)
  2882. ((not org-use-tag-inheritance) nil)
  2883. ((stringp org-use-tag-inheritance)
  2884. (string-match org-use-tag-inheritance tag))
  2885. ((listp org-use-tag-inheritance)
  2886. (member tag org-use-tag-inheritance))
  2887. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2888. (defcustom org-tags-match-list-sublevels t
  2889. "Non-nil means list also sublevels of headlines matching a search.
  2890. This variable applies to tags/property searches, and also to stuck
  2891. projects because this search is based on a tags match as well.
  2892. When set to the symbol `indented', sublevels are indented with
  2893. leading dots.
  2894. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2895. the sublevels of a headline matching a tag search often also match
  2896. the same search. Listing all of them can create very long lists.
  2897. Setting this variable to nil causes subtrees of a match to be skipped.
  2898. This variable is semi-obsolete and probably should always be true. It
  2899. is better to limit inheritance to certain tags using the variables
  2900. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2901. :group 'org-tags
  2902. :type '(choice
  2903. (const :tag "No, don't list them" nil)
  2904. (const :tag "Yes, do list them" t)
  2905. (const :tag "List them, indented with leading dots" indented)))
  2906. (defcustom org-tags-sort-function nil
  2907. "When set, tags are sorted using this function as a comparator."
  2908. :group 'org-tags
  2909. :type '(choice
  2910. (const :tag "No sorting" nil)
  2911. (const :tag "Alphabetical" string<)
  2912. (const :tag "Reverse alphabetical" string>)
  2913. (function :tag "Custom function" nil)))
  2914. (defvar org-tags-history nil
  2915. "History of minibuffer reads for tags.")
  2916. (defvar org-last-tags-completion-table nil
  2917. "The last used completion table for tags.")
  2918. (defvar org-after-tags-change-hook nil
  2919. "Hook that is run after the tags in a line have changed.")
  2920. (defgroup org-properties nil
  2921. "Options concerning properties in Org-mode."
  2922. :tag "Org Properties"
  2923. :group 'org)
  2924. (defcustom org-property-format "%-10s %s"
  2925. "How property key/value pairs should be formatted by `indent-line'.
  2926. When `indent-line' hits a property definition, it will format the line
  2927. according to this format, mainly to make sure that the values are
  2928. lined-up with respect to each other."
  2929. :group 'org-properties
  2930. :type 'string)
  2931. (defcustom org-properties-postprocess-alist nil
  2932. "Alist of properties and functions to adjust inserted values.
  2933. Elements of this alist must be of the form
  2934. ([string] [function])
  2935. where [string] must be a property name and [function] must be a
  2936. lambda expression: this lambda expression must take one argument,
  2937. the value to adjust, and return the new value as a string.
  2938. For example, this element will allow the property \"Remaining\"
  2939. to be updated wrt the relation between the \"Effort\" property
  2940. and the clock summary:
  2941. ((\"Remaining\" (lambda(value)
  2942. (let ((clocksum (org-clock-sum-current-item))
  2943. (effort (org-duration-string-to-minutes
  2944. (org-entry-get (point) \"Effort\"))))
  2945. (org-minutes-to-clocksum-string (- effort clocksum))))))"
  2946. :group 'org-properties
  2947. :version "24.1"
  2948. :type '(alist :key-type (string :tag "Property")
  2949. :value-type (function :tag "Function")))
  2950. (defcustom org-use-property-inheritance nil
  2951. "Non-nil means properties apply also for sublevels.
  2952. This setting is chiefly used during property searches. Turning it on can
  2953. cause significant overhead when doing a search, which is why it is not
  2954. on by default.
  2955. When nil, only the properties directly given in the current entry count.
  2956. When t, every property is inherited. The value may also be a list of
  2957. properties that should have inheritance, or a regular expression matching
  2958. properties that should be inherited.
  2959. However, note that some special properties use inheritance under special
  2960. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2961. and the properties ending in \"_ALL\" when they are used as descriptor
  2962. for valid values of a property.
  2963. Note for programmers:
  2964. When querying an entry with `org-entry-get', you can control if inheritance
  2965. should be used. By default, `org-entry-get' looks only at the local
  2966. properties. You can request inheritance by setting the inherit argument
  2967. to t (to force inheritance) or to `selective' (to respect the setting
  2968. in this variable)."
  2969. :group 'org-properties
  2970. :type '(choice
  2971. (const :tag "Not" nil)
  2972. (const :tag "Always" t)
  2973. (repeat :tag "Specific properties" (string :tag "Property"))
  2974. (regexp :tag "Properties matched by regexp")))
  2975. (defun org-property-inherit-p (property)
  2976. "Check if PROPERTY is one that should be inherited."
  2977. (cond
  2978. ((eq org-use-property-inheritance t) t)
  2979. ((not org-use-property-inheritance) nil)
  2980. ((stringp org-use-property-inheritance)
  2981. (string-match org-use-property-inheritance property))
  2982. ((listp org-use-property-inheritance)
  2983. (member property org-use-property-inheritance))
  2984. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2985. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2986. "The default column format, if no other format has been defined.
  2987. This variable can be set on the per-file basis by inserting a line
  2988. #+COLUMNS: %25ITEM ....."
  2989. :group 'org-properties
  2990. :type 'string)
  2991. (defcustom org-columns-ellipses ".."
  2992. "The ellipses to be used when a field in column view is truncated.
  2993. When this is the empty string, as many characters as possible are shown,
  2994. but then there will be no visual indication that the field has been truncated.
  2995. When this is a string of length N, the last N characters of a truncated
  2996. field are replaced by this string. If the column is narrower than the
  2997. ellipses string, only part of the ellipses string will be shown."
  2998. :group 'org-properties
  2999. :type 'string)
  3000. (defcustom org-columns-modify-value-for-display-function nil
  3001. "Function that modifies values for display in column view.
  3002. For example, it can be used to cut out a certain part from a time stamp.
  3003. The function must take 2 arguments:
  3004. column-title The title of the column (*not* the property name)
  3005. value The value that should be modified.
  3006. The function should return the value that should be displayed,
  3007. or nil if the normal value should be used."
  3008. :group 'org-properties
  3009. :type 'function)
  3010. (defcustom org-effort-property "Effort"
  3011. "The property that is being used to keep track of effort estimates.
  3012. Effort estimates given in this property need to have the format H:MM."
  3013. :group 'org-properties
  3014. :group 'org-progress
  3015. :type '(string :tag "Property"))
  3016. (defconst org-global-properties-fixed
  3017. '(("VISIBILITY_ALL" . "folded children content all")
  3018. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  3019. "List of property/value pairs that can be inherited by any entry.
  3020. These are fixed values, for the preset properties. The user variable
  3021. that can be used to add to this list is `org-global-properties'.
  3022. The entries in this list are cons cells where the car is a property
  3023. name and cdr is a string with the value. If the value represents
  3024. multiple items like an \"_ALL\" property, separate the items by
  3025. spaces.")
  3026. (defcustom org-global-properties nil
  3027. "List of property/value pairs that can be inherited by any entry.
  3028. This list will be combined with the constant `org-global-properties-fixed'.
  3029. The entries in this list are cons cells where the car is a property
  3030. name and cdr is a string with the value.
  3031. You can set buffer-local values for the same purpose in the variable
  3032. `org-file-properties' this by adding lines like
  3033. #+PROPERTY: NAME VALUE"
  3034. :group 'org-properties
  3035. :type '(repeat
  3036. (cons (string :tag "Property")
  3037. (string :tag "Value"))))
  3038. (defvar org-file-properties nil
  3039. "List of property/value pairs that can be inherited by any entry.
  3040. Valid for the current buffer.
  3041. This variable is populated from #+PROPERTY lines.")
  3042. (make-variable-buffer-local 'org-file-properties)
  3043. (defgroup org-agenda nil
  3044. "Options concerning agenda views in Org-mode."
  3045. :tag "Org Agenda"
  3046. :group 'org)
  3047. (defvar org-category nil
  3048. "Variable used by org files to set a category for agenda display.
  3049. Such files should use a file variable to set it, for example
  3050. # -*- mode: org; org-category: \"ELisp\"
  3051. or contain a special line
  3052. #+CATEGORY: ELisp
  3053. If the file does not specify a category, then file's base name
  3054. is used instead.")
  3055. (make-variable-buffer-local 'org-category)
  3056. (put 'org-category 'safe-local-variable #'(lambda (x) (or (symbolp x) (stringp x))))
  3057. (defcustom org-agenda-files nil
  3058. "The files to be used for agenda display.
  3059. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  3060. \\[org-remove-file]. You can also use customize to edit the list.
  3061. If an entry is a directory, all files in that directory that are matched by
  3062. `org-agenda-file-regexp' will be part of the file list.
  3063. If the value of the variable is not a list but a single file name, then
  3064. the list of agenda files is actually stored and maintained in that file, one
  3065. agenda file per line. In this file paths can be given relative to
  3066. `org-directory'. Tilde expansion and environment variable substitution
  3067. are also made."
  3068. :group 'org-agenda
  3069. :type '(choice
  3070. (repeat :tag "List of files and directories" file)
  3071. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  3072. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  3073. "Regular expression to match files for `org-agenda-files'.
  3074. If any element in the list in that variable contains a directory instead
  3075. of a normal file, all files in that directory that are matched by this
  3076. regular expression will be included."
  3077. :group 'org-agenda
  3078. :type 'regexp)
  3079. (defcustom org-agenda-text-search-extra-files nil
  3080. "List of extra files to be searched by text search commands.
  3081. These files will be search in addition to the agenda files by the
  3082. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  3083. Note that these files will only be searched for text search commands,
  3084. not for the other agenda views like todo lists, tag searches or the weekly
  3085. agenda. This variable is intended to list notes and possibly archive files
  3086. that should also be searched by these two commands.
  3087. In fact, if the first element in the list is the symbol `agenda-archives',
  3088. than all archive files of all agenda files will be added to the search
  3089. scope."
  3090. :group 'org-agenda
  3091. :type '(set :greedy t
  3092. (const :tag "Agenda Archives" agenda-archives)
  3093. (repeat :inline t (file))))
  3094. (org-defvaralias 'org-agenda-multi-occur-extra-files
  3095. 'org-agenda-text-search-extra-files)
  3096. (defcustom org-agenda-skip-unavailable-files nil
  3097. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  3098. A nil value means to remove them, after a query, from the list."
  3099. :group 'org-agenda
  3100. :type 'boolean)
  3101. (defcustom org-calendar-to-agenda-key [?c]
  3102. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  3103. The command `org-calendar-goto-agenda' will be bound to this key. The
  3104. default is the character `c' because then `c' can be used to switch back and
  3105. forth between agenda and calendar."
  3106. :group 'org-agenda
  3107. :type 'sexp)
  3108. (defcustom org-calendar-insert-diary-entry-key [?i]
  3109. "The key to be installed in `calendar-mode-map' for adding diary entries.
  3110. This option is irrelevant until `org-agenda-diary-file' has been configured
  3111. to point to an Org-mode file. When that is the case, the command
  3112. `org-agenda-diary-entry' will be bound to the key given here, by default
  3113. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  3114. if you want to continue doing this, you need to change this to a different
  3115. key."
  3116. :group 'org-agenda
  3117. :type 'sexp)
  3118. (defcustom org-agenda-diary-file 'diary-file
  3119. "File to which to add new entries with the `i' key in agenda and calendar.
  3120. When this is the symbol `diary-file', the functionality in the Emacs
  3121. calendar will be used to add entries to the `diary-file'. But when this
  3122. points to a file, `org-agenda-diary-entry' will be used instead."
  3123. :group 'org-agenda
  3124. :type '(choice
  3125. (const :tag "The standard Emacs diary file" diary-file)
  3126. (file :tag "Special Org file diary entries")))
  3127. (eval-after-load "calendar"
  3128. '(progn
  3129. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  3130. 'org-calendar-goto-agenda)
  3131. (add-hook 'calendar-mode-hook
  3132. (lambda ()
  3133. (unless (eq org-agenda-diary-file 'diary-file)
  3134. (define-key calendar-mode-map
  3135. org-calendar-insert-diary-entry-key
  3136. 'org-agenda-diary-entry))))))
  3137. (defgroup org-latex nil
  3138. "Options for embedding LaTeX code into Org-mode."
  3139. :tag "Org LaTeX"
  3140. :group 'org)
  3141. (defcustom org-format-latex-options
  3142. '(:foreground default :background default :scale 1.0
  3143. :html-foreground "Black" :html-background "Transparent"
  3144. :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  3145. "Options for creating images from LaTeX fragments.
  3146. This is a property list with the following properties:
  3147. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  3148. `default' means use the foreground of the default face.
  3149. `auto' means use the foreground from the text face.
  3150. :background the background color, or \"Transparent\".
  3151. `default' means use the background of the default face.
  3152. `auto' means use the background from the text face.
  3153. :scale a scaling factor for the size of the images, to get more pixels
  3154. :html-foreground, :html-background, :html-scale
  3155. the same numbers for HTML export.
  3156. :matchers a list indicating which matchers should be used to
  3157. find LaTeX fragments. Valid members of this list are:
  3158. \"begin\" find environments
  3159. \"$1\" find single characters surrounded by $.$
  3160. \"$\" find math expressions surrounded by $...$
  3161. \"$$\" find math expressions surrounded by $$....$$
  3162. \"\\(\" find math expressions surrounded by \\(...\\)
  3163. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  3164. :group 'org-latex
  3165. :type 'plist)
  3166. (defcustom org-format-latex-signal-error t
  3167. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  3168. When nil, just push out a message."
  3169. :group 'org-latex
  3170. :version "24.1"
  3171. :type 'boolean)
  3172. (defcustom org-latex-to-mathml-jar-file nil
  3173. "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
  3174. Use this to specify additional executable file say a jar file.
  3175. When using MathToWeb as the converter, specify the full-path to
  3176. your mathtoweb.jar file."
  3177. :group 'org-latex
  3178. :version "24.1"
  3179. :type '(choice
  3180. (const :tag "None" nil)
  3181. (file :tag "JAR file" :must-match t)))
  3182. (defcustom org-latex-to-mathml-convert-command nil
  3183. "Command to convert LaTeX fragments to MathML.
  3184. Replace format-specifiers in the command as noted below and use
  3185. `shell-command' to convert LaTeX to MathML.
  3186. %j: Executable file in fully expanded form as specified by
  3187. `org-latex-to-mathml-jar-file'.
  3188. %I: Input LaTeX file in fully expanded form
  3189. %o: Output MathML file
  3190. This command is used by `org-create-math-formula'.
  3191. When using MathToWeb as the converter, set this to
  3192. \"java -jar %j -unicode -force -df %o %I\"."
  3193. :group 'org-latex
  3194. :version "24.1"
  3195. :type '(choice
  3196. (const :tag "None" nil)
  3197. (string :tag "\nShell command")))
  3198. (defcustom org-latex-create-formula-image-program 'dvipng
  3199. "Program to convert LaTeX fragments with.
  3200. dvipng Process the LaTeX fragments to dvi file, then convert
  3201. dvi files to png files using dvipng.
  3202. This will also include processing of non-math environments.
  3203. imagemagick Convert the LaTeX fragments to pdf files and use imagemagick
  3204. to convert pdf files to png files"
  3205. :group 'org-latex
  3206. :version "24.1"
  3207. :type '(choice
  3208. (const :tag "dvipng" dvipng)
  3209. (const :tag "imagemagick" imagemagick)))
  3210. (defcustom org-latex-preview-ltxpng-directory "ltxpng/"
  3211. "Path to store latex preview images.
  3212. A relative path here creates many directories relative to the
  3213. processed org files paths. An absolute path puts all preview
  3214. images at the same place."
  3215. :group 'org-latex
  3216. :version "24.3"
  3217. :type 'string)
  3218. (defun org-format-latex-mathml-available-p ()
  3219. "Return t if `org-latex-to-mathml-convert-command' is usable."
  3220. (save-match-data
  3221. (when (and (boundp 'org-latex-to-mathml-convert-command)
  3222. org-latex-to-mathml-convert-command)
  3223. (let ((executable (car (split-string
  3224. org-latex-to-mathml-convert-command))))
  3225. (when (executable-find executable)
  3226. (if (string-match
  3227. "%j" org-latex-to-mathml-convert-command)
  3228. (file-readable-p org-latex-to-mathml-jar-file)
  3229. t))))))
  3230. (defcustom org-format-latex-header "\\documentclass{article}
  3231. \\usepackage[usenames]{color}
  3232. \\usepackage{amsmath}
  3233. \\usepackage[mathscr]{eucal}
  3234. \\pagestyle{empty} % do not remove
  3235. \[PACKAGES]
  3236. \[DEFAULT-PACKAGES]
  3237. % The settings below are copied from fullpage.sty
  3238. \\setlength{\\textwidth}{\\paperwidth}
  3239. \\addtolength{\\textwidth}{-3cm}
  3240. \\setlength{\\oddsidemargin}{1.5cm}
  3241. \\addtolength{\\oddsidemargin}{-2.54cm}
  3242. \\setlength{\\evensidemargin}{\\oddsidemargin}
  3243. \\setlength{\\textheight}{\\paperheight}
  3244. \\addtolength{\\textheight}{-\\headheight}
  3245. \\addtolength{\\textheight}{-\\headsep}
  3246. \\addtolength{\\textheight}{-\\footskip}
  3247. \\addtolength{\\textheight}{-3cm}
  3248. \\setlength{\\topmargin}{1.5cm}
  3249. \\addtolength{\\topmargin}{-2.54cm}"
  3250. "The document header used for processing LaTeX fragments.
  3251. It is imperative that this header make sure that no page number
  3252. appears on the page. The package defined in the variables
  3253. `org-latex-default-packages-alist' and `org-latex-packages-alist'
  3254. will either replace the placeholder \"[PACKAGES]\" in this
  3255. header, or they will be appended."
  3256. :group 'org-latex
  3257. :type 'string)
  3258. (defun org-set-packages-alist (var val)
  3259. "Set the packages alist and make sure it has 3 elements per entry."
  3260. (set var (mapcar (lambda (x)
  3261. (if (and (consp x) (= (length x) 2))
  3262. (list (car x) (nth 1 x) t)
  3263. x))
  3264. val)))
  3265. (defun org-get-packages-alist (var)
  3266. "Get the packages alist and make sure it has 3 elements per entry."
  3267. (mapcar (lambda (x)
  3268. (if (and (consp x) (= (length x) 2))
  3269. (list (car x) (nth 1 x) t)
  3270. x))
  3271. (default-value var)))
  3272. (defcustom org-latex-default-packages-alist
  3273. '(("AUTO" "inputenc" t)
  3274. ("T1" "fontenc" t)
  3275. ("" "fixltx2e" nil)
  3276. ("" "graphicx" t)
  3277. ("" "longtable" nil)
  3278. ("" "float" nil)
  3279. ("" "wrapfig" nil)
  3280. ("" "soul" t)
  3281. ("" "textcomp" t)
  3282. ("" "marvosym" t)
  3283. ("" "wasysym" t)
  3284. ("" "latexsym" t)
  3285. ("" "amssymb" t)
  3286. ("" "hyperref" nil)
  3287. "\\tolerance=1000")
  3288. "Alist of default packages to be inserted in the header.
  3289. Change this only if one of the packages here causes an
  3290. incompatibility with another package you are using.
  3291. The packages in this list are needed by one part or another of
  3292. Org mode to function properly:
  3293. - inputenc, fontenc: for basic font and character selection
  3294. - textcomp, marvosymb, wasysym, latexsym, amssym: for various
  3295. symbols used for interpreting the entities in `org-entities'.
  3296. You can skip some of these packages if you don't use any of the
  3297. symbols in it.
  3298. - graphicx: for including images
  3299. - float, wrapfig: for figure placement
  3300. - longtable: for long tables
  3301. - hyperref: for cross references
  3302. Therefore you should not modify this variable unless you know
  3303. what you are doing. The one reason to change it anyway is that
  3304. you might be loading some other package that conflicts with one
  3305. of the default packages. Each cell is of the format
  3306. \( \"options\" \"package\" snippet-flag). If SNIPPET-FLAG is t,
  3307. the package also needs to be included when compiling LaTeX
  3308. snippets into images for inclusion into non-LaTeX output."
  3309. :group 'org-latex
  3310. :group 'org-export-latex
  3311. :set 'org-set-packages-alist
  3312. :get 'org-get-packages-alist
  3313. :version "24.1"
  3314. :type '(repeat
  3315. (choice
  3316. (list :tag "options/package pair"
  3317. (string :tag "options")
  3318. (string :tag "package")
  3319. (boolean :tag "Snippet"))
  3320. (string :tag "A line of LaTeX"))))
  3321. (defcustom org-latex-packages-alist nil
  3322. "Alist of packages to be inserted in every LaTeX header.
  3323. These will be inserted after `org-latex-default-packages-alist'.
  3324. Each cell is of the format:
  3325. \(\"options\" \"package\" snippet-flag)
  3326. SNIPPET-FLAG, when t, indicates that this package is also needed
  3327. when turning LaTeX snippets into images for inclusion into
  3328. non-LaTeX output.
  3329. Make sure that you only list packages here which:
  3330. - you want in every file
  3331. - do not conflict with the setup in `org-format-latex-header'.
  3332. - do not conflict with the default packages in
  3333. `org-latex-default-packages-alist'."
  3334. :group 'org-latex
  3335. :group 'org-export-latex
  3336. :set 'org-set-packages-alist
  3337. :get 'org-get-packages-alist
  3338. :type '(repeat
  3339. (choice
  3340. (list :tag "options/package pair"
  3341. (string :tag "options")
  3342. (string :tag "package")
  3343. (boolean :tag "Snippet"))
  3344. (string :tag "A line of LaTeX"))))
  3345. (defgroup org-appearance nil
  3346. "Settings for Org-mode appearance."
  3347. :tag "Org Appearance"
  3348. :group 'org)
  3349. (defcustom org-level-color-stars-only nil
  3350. "Non-nil means fontify only the stars in each headline.
  3351. When nil, the entire headline is fontified.
  3352. Changing it requires restart of `font-lock-mode' to become effective
  3353. also in regions already fontified."
  3354. :group 'org-appearance
  3355. :type 'boolean)
  3356. (defcustom org-hide-leading-stars nil
  3357. "Non-nil means hide the first N-1 stars in a headline.
  3358. This works by using the face `org-hide' for these stars. This
  3359. face is white for a light background, and black for a dark
  3360. background. You may have to customize the face `org-hide' to
  3361. make this work.
  3362. Changing it requires restart of `font-lock-mode' to become effective
  3363. also in regions already fontified.
  3364. You may also set this on a per-file basis by adding one of the following
  3365. lines to the buffer:
  3366. #+STARTUP: hidestars
  3367. #+STARTUP: showstars"
  3368. :group 'org-appearance
  3369. :type 'boolean)
  3370. (defcustom org-hidden-keywords nil
  3371. "List of symbols corresponding to keywords to be hidden the org buffer.
  3372. For example, a value '(title) for this list will make the document's title
  3373. appear in the buffer without the initial #+TITLE: keyword."
  3374. :group 'org-appearance
  3375. :version "24.1"
  3376. :type '(set (const :tag "#+AUTHOR" author)
  3377. (const :tag "#+DATE" date)
  3378. (const :tag "#+EMAIL" email)
  3379. (const :tag "#+TITLE" title)))
  3380. (defcustom org-custom-properties nil
  3381. "List of properties (as strings) with a special meaning.
  3382. The default use of these custom properties is to let the user
  3383. hide them with `org-toggle-custom-properties-visibility'."
  3384. :group 'org-properties
  3385. :group 'org-appearance
  3386. :version "24.3"
  3387. :type '(repeat (string :tag "Property Name")))
  3388. (defcustom org-fontify-done-headline nil
  3389. "Non-nil means change the face of a headline if it is marked DONE.
  3390. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3391. When this is non-nil, the headline after the keyword is set to the
  3392. `org-headline-done' as an additional indication."
  3393. :group 'org-appearance
  3394. :type 'boolean)
  3395. (defcustom org-fontify-emphasized-text t
  3396. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3397. Changing this variable requires a restart of Emacs to take effect."
  3398. :group 'org-appearance
  3399. :type 'boolean)
  3400. (defcustom org-fontify-whole-heading-line nil
  3401. "Non-nil means fontify the whole line for headings.
  3402. This is useful when setting a background color for the
  3403. org-level-* faces."
  3404. :group 'org-appearance
  3405. :type 'boolean)
  3406. (defcustom org-highlight-latex-and-related nil
  3407. "Non-nil means highlight LaTeX related syntax in the buffer.
  3408. When non nil, the value should be a list containing any of the
  3409. following symbols:
  3410. `latex' Highlight LaTeX snippets and environments.
  3411. `script' Highlight subscript and superscript.
  3412. `entities' Highlight entities."
  3413. :group 'org-appearance
  3414. :version "24.4"
  3415. :package-version '(Org . "8.0")
  3416. :type '(choice
  3417. (const :tag "No highlighting" nil)
  3418. (set :greedy t :tag "Highlight"
  3419. (const :tag "LaTeX snippets and environments" latex)
  3420. (const :tag "Subscript and superscript" script)
  3421. (const :tag "Entities" entities))))
  3422. (defcustom org-hide-emphasis-markers nil
  3423. "Non-nil mean font-lock should hide the emphasis marker characters."
  3424. :group 'org-appearance
  3425. :type 'boolean)
  3426. (defcustom org-pretty-entities nil
  3427. "Non-nil means show entities as UTF8 characters.
  3428. When nil, the \\name form remains in the buffer."
  3429. :group 'org-appearance
  3430. :version "24.1"
  3431. :type 'boolean)
  3432. (defcustom org-pretty-entities-include-sub-superscripts t
  3433. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  3434. :group 'org-appearance
  3435. :version "24.1"
  3436. :type 'boolean)
  3437. (defvar org-emph-re nil
  3438. "Regular expression for matching emphasis.
  3439. After a match, the match groups contain these elements:
  3440. 0 The match of the full regular expression, including the characters
  3441. before and after the proper match
  3442. 1 The character before the proper match, or empty at beginning of line
  3443. 2 The proper match, including the leading and trailing markers
  3444. 3 The leading marker like * or /, indicating the type of highlighting
  3445. 4 The text between the emphasis markers, not including the markers
  3446. 5 The character after the match, empty at the end of a line")
  3447. (defvar org-verbatim-re nil
  3448. "Regular expression for matching verbatim text.")
  3449. (defvar org-emphasis-regexp-components) ; defined just below
  3450. (defvar org-emphasis-alist) ; defined just below
  3451. (defun org-set-emph-re (var val)
  3452. "Set variable and compute the emphasis regular expression."
  3453. (set var val)
  3454. (when (and (boundp 'org-emphasis-alist)
  3455. (boundp 'org-emphasis-regexp-components)
  3456. org-emphasis-alist org-emphasis-regexp-components)
  3457. (let* ((e org-emphasis-regexp-components)
  3458. (pre (car e))
  3459. (post (nth 1 e))
  3460. (border (nth 2 e))
  3461. (body (nth 3 e))
  3462. (nl (nth 4 e))
  3463. (body1 (concat body "*?"))
  3464. (markers (mapconcat 'car org-emphasis-alist ""))
  3465. (vmarkers (mapconcat
  3466. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  3467. org-emphasis-alist "")))
  3468. ;; make sure special characters appear at the right position in the class
  3469. (if (string-match "\\^" markers)
  3470. (setq markers (concat (replace-match "" t t markers) "^")))
  3471. (if (string-match "-" markers)
  3472. (setq markers (concat (replace-match "" t t markers) "-")))
  3473. (if (string-match "\\^" vmarkers)
  3474. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3475. (if (string-match "-" vmarkers)
  3476. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3477. (if (> nl 0)
  3478. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3479. (int-to-string nl) "\\}")))
  3480. ;; Make the regexp
  3481. (setq org-emph-re
  3482. (concat "\\([" pre "]\\|^\\)"
  3483. "\\("
  3484. "\\([" markers "]\\)"
  3485. "\\("
  3486. "[^" border "]\\|"
  3487. "[^" border "]"
  3488. body1
  3489. "[^" border "]"
  3490. "\\)"
  3491. "\\3\\)"
  3492. "\\([" post "]\\|$\\)"))
  3493. (setq org-verbatim-re
  3494. (concat "\\([" pre "]\\|^\\)"
  3495. "\\("
  3496. "\\([" vmarkers "]\\)"
  3497. "\\("
  3498. "[^" border "]\\|"
  3499. "[^" border "]"
  3500. body1
  3501. "[^" border "]"
  3502. "\\)"
  3503. "\\3\\)"
  3504. "\\([" post "]\\|$\\)")))))
  3505. (defcustom org-emphasis-regexp-components
  3506. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  3507. "Components used to build the regular expression for emphasis.
  3508. This is a list with five entries. Terminology: In an emphasis string
  3509. like \" *strong word* \", we call the initial space PREMATCH, the final
  3510. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3511. and \"trong wor\" is the body. The different components in this variable
  3512. specify what is allowed/forbidden in each part:
  3513. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3514. post Chars allowed as postmatch. End of line will be allowed too.
  3515. border The chars *forbidden* as border characters.
  3516. body-regexp A regexp like \".\" to match a body character. Don't use
  3517. non-shy groups here, and don't allow newline here.
  3518. newline The maximum number of newlines allowed in an emphasis exp.
  3519. Use customize to modify this, or restart Emacs after changing it."
  3520. :group 'org-appearance
  3521. :set 'org-set-emph-re
  3522. :type '(list
  3523. (sexp :tag "Allowed chars in pre ")
  3524. (sexp :tag "Allowed chars in post ")
  3525. (sexp :tag "Forbidden chars in border ")
  3526. (sexp :tag "Regexp for body ")
  3527. (integer :tag "number of newlines allowed")
  3528. (option (boolean :tag "Please ignore this button"))))
  3529. (defcustom org-emphasis-alist
  3530. `(("*" bold "<b>" "</b>")
  3531. ("/" italic "<i>" "</i>")
  3532. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  3533. ("=" org-code "<code>" "</code>" verbatim)
  3534. ("~" org-verbatim "<code>" "</code>" verbatim)
  3535. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  3536. "<del>" "</del>")
  3537. )
  3538. "Special syntax for emphasized text.
  3539. Text starting and ending with a special character will be emphasized, for
  3540. example *bold*, _underlined_ and /italic/. This variable sets the marker
  3541. characters, the face to be used by font-lock for highlighting in Org-mode
  3542. Emacs buffers, and the HTML tags to be used for this.
  3543. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  3544. Use customize to modify this, or restart Emacs after changing it."
  3545. :group 'org-appearance
  3546. :set 'org-set-emph-re
  3547. :type '(repeat
  3548. (list
  3549. (string :tag "Marker character")
  3550. (choice
  3551. (face :tag "Font-lock-face")
  3552. (plist :tag "Face property list"))
  3553. (string :tag "HTML start tag")
  3554. (string :tag "HTML end tag")
  3555. (option (const verbatim)))))
  3556. (defvar org-protecting-blocks
  3557. '("src" "example" "latex" "ascii" "html" "ditaa" "dot" "r" "R")
  3558. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3559. This is needed for font-lock setup.")
  3560. ;;; Miscellaneous options
  3561. (defgroup org-completion nil
  3562. "Completion in Org-mode."
  3563. :tag "Org Completion"
  3564. :group 'org)
  3565. (defcustom org-completion-use-ido nil
  3566. "Non-nil means use ido completion wherever possible.
  3567. Note that `ido-mode' must be active for this variable to be relevant.
  3568. If you decide to turn this variable on, you might well want to turn off
  3569. `org-outline-path-complete-in-steps'.
  3570. See also `org-completion-use-iswitchb'."
  3571. :group 'org-completion
  3572. :type 'boolean)
  3573. (defcustom org-completion-use-iswitchb nil
  3574. "Non-nil means use iswitchb completion wherever possible.
  3575. Note that `iswitchb-mode' must be active for this variable to be relevant.
  3576. If you decide to turn this variable on, you might well want to turn off
  3577. `org-outline-path-complete-in-steps'.
  3578. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  3579. :group 'org-completion
  3580. :type 'boolean)
  3581. (defcustom org-completion-fallback-command 'hippie-expand
  3582. "The expansion command called by \\[pcomplete] in normal context.
  3583. Normal means, no org-mode-specific context."
  3584. :group 'org-completion
  3585. :type 'function)
  3586. ;;; Functions and variables from their packages
  3587. ;; Declared here to avoid compiler warnings
  3588. ;; XEmacs only
  3589. (defvar outline-mode-menu-heading)
  3590. (defvar outline-mode-menu-show)
  3591. (defvar outline-mode-menu-hide)
  3592. (defvar zmacs-regions) ; XEmacs regions
  3593. ;; Emacs only
  3594. (defvar mark-active)
  3595. ;; Various packages
  3596. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  3597. (declare-function calendar-forward-day "cal-move" (arg))
  3598. (declare-function calendar-goto-date "cal-move" (date))
  3599. (declare-function calendar-goto-today "cal-move" ())
  3600. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3601. (defvar calc-embedded-close-formula)
  3602. (defvar calc-embedded-open-formula)
  3603. (declare-function cdlatex-tab "ext:cdlatex" ())
  3604. (declare-function cdlatex-compute-tables "ext:cdlatex" ())
  3605. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3606. (defvar font-lock-unfontify-region-function)
  3607. (declare-function iswitchb-read-buffer "iswitchb"
  3608. (prompt &optional default require-match start matches-set))
  3609. (defvar iswitchb-temp-buflist)
  3610. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3611. (defvar org-agenda-tags-todo-honor-ignore-options)
  3612. (declare-function org-agenda-skip "org-agenda" ())
  3613. (declare-function
  3614. org-agenda-format-item "org-agenda"
  3615. (extra txt &optional level category tags dotime noprefix remove-re habitp))
  3616. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3617. (declare-function org-agenda-change-all-lines "org-agenda"
  3618. (newhead hdmarker &optional fixface just-this))
  3619. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3620. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3621. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  3622. (beg end))
  3623. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3624. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3625. "org-agenda" (&optional end))
  3626. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3627. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  3628. (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
  3629. (declare-function org-inlinetask-goto-end "org-inlinetask" ())
  3630. (declare-function org-indent-mode "org-indent" (&optional arg))
  3631. (declare-function parse-time-string "parse-time" (string))
  3632. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3633. (declare-function orgtbl-send-table "org-table" (&optional maybe))
  3634. (defvar remember-data-file)
  3635. (defvar texmathp-why)
  3636. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3637. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3638. (defvar org-latex-regexps)
  3639. ;;; Autoload and prepare some org modules
  3640. ;; Some table stuff that needs to be defined here, because it is used
  3641. ;; by the functions setting up org-mode or checking for table context.
  3642. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  3643. "Detect an org-type or table-type table.")
  3644. (defconst org-table-line-regexp "^[ \t]*|"
  3645. "Detect an org-type table line.")
  3646. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  3647. "Detect an org-type table line.")
  3648. (defconst org-table-hline-regexp "^[ \t]*|-"
  3649. "Detect an org-type table hline.")
  3650. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  3651. "Detect a table-type table hline.")
  3652. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  3653. "Detect the first line outside a table when searching from within it.
  3654. This works for both table types.")
  3655. ;; Autoload the functions in org-table.el that are needed by functions here.
  3656. (eval-and-compile
  3657. (org-autoload "org-table"
  3658. '(org-table-begin org-table-blank-field org-table-end)))
  3659. ;;;###autoload
  3660. (defun turn-on-orgtbl ()
  3661. "Unconditionally turn on `orgtbl-mode'."
  3662. (require 'org-table)
  3663. (orgtbl-mode 1))
  3664. (defun org-at-table-p (&optional table-type)
  3665. "Return t if the cursor is inside an org-type table.
  3666. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  3667. (if org-enable-table-editor
  3668. (save-excursion
  3669. (beginning-of-line 1)
  3670. (looking-at (if table-type org-table-any-line-regexp
  3671. org-table-line-regexp)))
  3672. nil))
  3673. (defsubst org-table-p () (org-at-table-p))
  3674. (defun org-at-table.el-p ()
  3675. "Return t if and only if we are at a table.el table."
  3676. (and (org-at-table-p 'any)
  3677. (save-excursion
  3678. (goto-char (org-table-begin 'any))
  3679. (looking-at org-table1-hline-regexp))))
  3680. (defun org-table-recognize-table.el ()
  3681. "If there is a table.el table nearby, recognize it and move into it."
  3682. (if org-table-tab-recognizes-table.el
  3683. (if (org-at-table.el-p)
  3684. (progn
  3685. (beginning-of-line 1)
  3686. (if (looking-at org-table-dataline-regexp)
  3687. nil
  3688. (if (looking-at org-table1-hline-regexp)
  3689. (progn
  3690. (beginning-of-line 2)
  3691. (if (looking-at org-table-any-border-regexp)
  3692. (beginning-of-line -1)))))
  3693. (if (re-search-forward "|" (org-table-end t) t)
  3694. (progn
  3695. (require 'table)
  3696. (if (table--at-cell-p (point))
  3697. t
  3698. (message "recognizing table.el table...")
  3699. (table-recognize-table)
  3700. (message "recognizing table.el table...done")))
  3701. (error "This should not happen"))
  3702. t)
  3703. nil)
  3704. nil))
  3705. (defun org-at-table-hline-p ()
  3706. "Return t if the cursor is inside a hline in a table."
  3707. (if org-enable-table-editor
  3708. (save-excursion
  3709. (beginning-of-line 1)
  3710. (looking-at org-table-hline-regexp))
  3711. nil))
  3712. (defvar org-table-clean-did-remove-column nil)
  3713. (defun org-table-map-tables (function &optional quietly)
  3714. "Apply FUNCTION to the start of all tables in the buffer."
  3715. (save-excursion
  3716. (save-restriction
  3717. (widen)
  3718. (goto-char (point-min))
  3719. (while (re-search-forward org-table-any-line-regexp nil t)
  3720. (unless quietly
  3721. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
  3722. (beginning-of-line 1)
  3723. (when (and (looking-at org-table-line-regexp)
  3724. ;; Exclude tables in src/example/verbatim/clocktable blocks
  3725. (not (org-in-block-p '("src" "example" "verbatim" "clocktable"))))
  3726. (save-excursion (funcall function))
  3727. (or (looking-at org-table-line-regexp)
  3728. (forward-char 1)))
  3729. (re-search-forward org-table-any-border-regexp nil 1))))
  3730. (unless quietly (message "Mapping tables: done")))
  3731. ;; Declare and autoload functions from ox.el and al.
  3732. (declare-function org-export-get-environment "ox"
  3733. (&optional backend subtreep ext-plist))
  3734. (declare-function org-latex-guess-inputenc "ox-latex" (header))
  3735. ;; Declare and autoload functions from org-agenda.el
  3736. (eval-and-compile
  3737. (org-autoload "org-agenda"
  3738. '(org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  3739. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock" (beg end))
  3740. (declare-function org-clock-update-mode-line "org-clock" ())
  3741. (declare-function org-resolve-clocks "org-clock"
  3742. (&optional also-non-dangling-p prompt last-valid))
  3743. (defvar org-clock-start-time)
  3744. (defvar org-clock-marker (make-marker)
  3745. "Marker recording the last clock-in.")
  3746. (defvar org-clock-hd-marker (make-marker)
  3747. "Marker recording the last clock-in, but the headline position.")
  3748. (defvar org-clock-heading ""
  3749. "The heading of the current clock entry.")
  3750. (defun org-clock-is-active ()
  3751. "Return non-nil if clock is currently running.
  3752. The return value is actually the clock marker."
  3753. (marker-buffer org-clock-marker))
  3754. (eval-and-compile
  3755. (org-autoload "org-clock" '(org-clock-remove-overlays
  3756. org-clock-update-time-maybe
  3757. org-clocktable-shift)))
  3758. (defun org-check-running-clock ()
  3759. "Check if the current buffer contains the running clock.
  3760. If yes, offer to stop it and to save the buffer with the changes."
  3761. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3762. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3763. (buffer-name))))
  3764. (org-clock-out)
  3765. (when (y-or-n-p "Save changed buffer?")
  3766. (save-buffer))))
  3767. (defun org-clocktable-try-shift (dir n)
  3768. "Check if this line starts a clock table, if yes, shift the time block."
  3769. (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
  3770. (org-clocktable-shift dir n)))
  3771. ;;;###autoload
  3772. (defun org-clock-persistence-insinuate ()
  3773. "Set up hooks for clock persistence."
  3774. (require 'org-clock)
  3775. (add-hook 'org-mode-hook 'org-clock-load)
  3776. (add-hook 'kill-emacs-hook 'org-clock-save))
  3777. ;; Define the variable already here, to make sure we have it.
  3778. (defvar org-indent-mode nil
  3779. "Non-nil if Org-Indent mode is enabled.
  3780. Use the command `org-indent-mode' to change this variable.")
  3781. ;; Autoload archiving code
  3782. ;; The stuff that is needed for cycling and tags has to be defined here.
  3783. (defgroup org-archive nil
  3784. "Options concerning archiving in Org-mode."
  3785. :tag "Org Archive"
  3786. :group 'org-structure)
  3787. (defcustom org-archive-location "%s_archive::"
  3788. "The location where subtrees should be archived.
  3789. The value of this variable is a string, consisting of two parts,
  3790. separated by a double-colon. The first part is a filename and
  3791. the second part is a headline.
  3792. When the filename is omitted, archiving happens in the same file.
  3793. %s in the filename will be replaced by the current file
  3794. name (without the directory part). Archiving to a different file
  3795. is useful to keep archived entries from contributing to the
  3796. Org-mode Agenda.
  3797. The archived entries will be filed as subtrees of the specified
  3798. headline. When the headline is omitted, the subtrees are simply
  3799. filed away at the end of the file, as top-level entries. Also in
  3800. the heading you can use %s to represent the file name, this can be
  3801. useful when using the same archive for a number of different files.
  3802. Here are a few examples:
  3803. \"%s_archive::\"
  3804. If the current file is Projects.org, archive in file
  3805. Projects.org_archive, as top-level trees. This is the default.
  3806. \"::* Archived Tasks\"
  3807. Archive in the current file, under the top-level headline
  3808. \"* Archived Tasks\".
  3809. \"~/org/archive.org::\"
  3810. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  3811. \"~/org/archive.org::* From %s\"
  3812. Archive in file ~/org/archive.org (absolute path), under headlines
  3813. \"From FILENAME\" where file name is the current file name.
  3814. \"~/org/datetree.org::datetree/* Finished Tasks\"
  3815. The \"datetree/\" string is special, signifying to archive
  3816. items to the datetree. Items are placed in either the CLOSED
  3817. date of the item, or the current date if there is no CLOSED date.
  3818. The heading will be a subentry to the current date. There doesn't
  3819. need to be a heading, but there always needs to be a slash after
  3820. datetree. For example, to store archived items directly in the
  3821. datetree, use \"~/org/datetree.org::datetree/\".
  3822. \"basement::** Finished Tasks\"
  3823. Archive in file ./basement (relative path), as level 3 trees
  3824. below the level 2 heading \"** Finished Tasks\".
  3825. You may set this option on a per-file basis by adding to the buffer a
  3826. line like
  3827. #+ARCHIVE: basement::** Finished Tasks
  3828. You may also define it locally for a subtree by setting an ARCHIVE property
  3829. in the entry. If such a property is found in an entry, or anywhere up
  3830. the hierarchy, it will be used."
  3831. :group 'org-archive
  3832. :type 'string)
  3833. (defcustom org-archive-tag "ARCHIVE"
  3834. "The tag that marks a subtree as archived.
  3835. An archived subtree does not open during visibility cycling, and does
  3836. not contribute to the agenda listings.
  3837. After changing this, font-lock must be restarted in the relevant buffers to
  3838. get the proper fontification."
  3839. :group 'org-archive
  3840. :group 'org-keywords
  3841. :type 'string)
  3842. (defcustom org-agenda-skip-archived-trees t
  3843. "Non-nil means the agenda will skip any items located in archived trees.
  3844. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  3845. variable is no longer recommended, you should leave it at the value t.
  3846. Instead, use the key `v' to cycle the archives-mode in the agenda."
  3847. :group 'org-archive
  3848. :group 'org-agenda-skip
  3849. :type 'boolean)
  3850. (defcustom org-columns-skip-archived-trees t
  3851. "Non-nil means ignore archived trees when creating column view."
  3852. :group 'org-archive
  3853. :group 'org-properties
  3854. :type 'boolean)
  3855. (defcustom org-cycle-open-archived-trees nil
  3856. "Non-nil means `org-cycle' will open archived trees.
  3857. An archived tree is a tree marked with the tag ARCHIVE.
  3858. When nil, archived trees will stay folded. You can still open them with
  3859. normal outline commands like `show-all', but not with the cycling commands."
  3860. :group 'org-archive
  3861. :group 'org-cycle
  3862. :type 'boolean)
  3863. (defcustom org-sparse-tree-open-archived-trees nil
  3864. "Non-nil means sparse tree construction shows matches in archived trees.
  3865. When nil, matches in these trees are highlighted, but the trees are kept in
  3866. collapsed state."
  3867. :group 'org-archive
  3868. :group 'org-sparse-trees
  3869. :type 'boolean)
  3870. (defcustom org-sparse-tree-default-date-type 'scheduled-or-deadline
  3871. "The default date type when building a sparse tree.
  3872. When this is nil, a date is a scheduled or a deadline timestamp.
  3873. Otherwise, these types are allowed:
  3874. all: all timestamps
  3875. active: only active timestamps (<...>)
  3876. inactive: only inactive timestamps (<...)
  3877. scheduled: only scheduled timestamps
  3878. deadline: only deadline timestamps"
  3879. :type '(choice (const :tag "Scheduled or deadline" 'scheduled-or-deadline)
  3880. (const :tag "All timestamps" all)
  3881. (const :tag "Only active timestamps" active)
  3882. (const :tag "Only inactive timestamps" inactive)
  3883. (const :tag "Only scheduled timestamps" scheduled)
  3884. (const :tag "Only deadline timestamps" deadline))
  3885. :version "24.3"
  3886. :group 'org-sparse-trees)
  3887. (defun org-cycle-hide-archived-subtrees (state)
  3888. "Re-hide all archived subtrees after a visibility state change."
  3889. (when (and (not org-cycle-open-archived-trees)
  3890. (not (memq state '(overview folded))))
  3891. (save-excursion
  3892. (let* ((globalp (memq state '(contents all)))
  3893. (beg (if globalp (point-min) (point)))
  3894. (end (if globalp (point-max) (org-end-of-subtree t))))
  3895. (org-hide-archived-subtrees beg end)
  3896. (goto-char beg)
  3897. (if (looking-at (concat ".*:" org-archive-tag ":"))
  3898. (message "%s" (substitute-command-keys
  3899. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  3900. (defun org-force-cycle-archived ()
  3901. "Cycle subtree even if it is archived."
  3902. (interactive)
  3903. (setq this-command 'org-cycle)
  3904. (let ((org-cycle-open-archived-trees t))
  3905. (call-interactively 'org-cycle)))
  3906. (defun org-hide-archived-subtrees (beg end)
  3907. "Re-hide all archived subtrees after a visibility state change."
  3908. (save-excursion
  3909. (let* ((re (concat ":" org-archive-tag ":")))
  3910. (goto-char beg)
  3911. (while (re-search-forward re end t)
  3912. (when (org-at-heading-p)
  3913. (org-flag-subtree t)
  3914. (org-end-of-subtree t))))))
  3915. (declare-function outline-end-of-heading "outline" ())
  3916. (declare-function outline-flag-region "outline" (from to flag))
  3917. (defun org-flag-subtree (flag)
  3918. (save-excursion
  3919. (org-back-to-heading t)
  3920. (outline-end-of-heading)
  3921. (outline-flag-region (point)
  3922. (progn (org-end-of-subtree t) (point))
  3923. flag)))
  3924. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  3925. (eval-and-compile
  3926. (org-autoload "org-archive"
  3927. '(org-add-archive-files)))
  3928. ;; Autoload Column View Code
  3929. (declare-function org-columns-number-to-string "org-colview" (n fmt &optional printf))
  3930. (declare-function org-columns-get-format-and-top-level "org-colview" ())
  3931. (declare-function org-columns-compute "org-colview" (property))
  3932. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  3933. '(org-columns-number-to-string
  3934. org-columns-get-format-and-top-level
  3935. org-columns-compute
  3936. org-columns-remove-overlays))
  3937. ;; Autoload ID code
  3938. (declare-function org-id-store-link "org-id")
  3939. (declare-function org-id-locations-load "org-id")
  3940. (declare-function org-id-locations-save "org-id")
  3941. (defvar org-id-track-globally)
  3942. (org-autoload "org-id"
  3943. '(org-id-new
  3944. org-id-copy
  3945. org-id-get-with-outline-path-completion
  3946. org-id-get-with-outline-drilling))
  3947. ;;; Variables for pre-computed regular expressions, all buffer local
  3948. (defvar org-drawer-regexp "^[ \t]*:PROPERTIES:[ \t]*$"
  3949. "Matches first line of a hidden block.")
  3950. (make-variable-buffer-local 'org-drawer-regexp)
  3951. (defvar org-todo-regexp nil
  3952. "Matches any of the TODO state keywords.")
  3953. (make-variable-buffer-local 'org-todo-regexp)
  3954. (defvar org-not-done-regexp nil
  3955. "Matches any of the TODO state keywords except the last one.")
  3956. (make-variable-buffer-local 'org-not-done-regexp)
  3957. (defvar org-not-done-heading-regexp nil
  3958. "Matches a TODO headline that is not done.")
  3959. (make-variable-buffer-local 'org-not-done-regexp)
  3960. (defvar org-todo-line-regexp nil
  3961. "Matches a headline and puts TODO state into group 2 if present.")
  3962. (make-variable-buffer-local 'org-todo-line-regexp)
  3963. (defvar org-complex-heading-regexp nil
  3964. "Matches a headline and puts everything into groups:
  3965. group 1: the stars
  3966. group 2: The todo keyword, maybe
  3967. group 3: Priority cookie
  3968. group 4: True headline
  3969. group 5: Tags")
  3970. (make-variable-buffer-local 'org-complex-heading-regexp)
  3971. (defvar org-complex-heading-regexp-format nil
  3972. "Printf format to make regexp to match an exact headline.
  3973. This regexp will match the headline of any node which has the
  3974. exact headline text that is put into the format, but may have any
  3975. TODO state, priority and tags.")
  3976. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  3977. (defvar org-todo-line-tags-regexp nil
  3978. "Matches a headline and puts TODO state into group 2 if present.
  3979. Also put tags into group 4 if tags are present.")
  3980. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3981. (defvar org-ds-keyword-length 12
  3982. "Maximum length of the DEADLINE and SCHEDULED keywords.")
  3983. (make-variable-buffer-local 'org-ds-keyword-length)
  3984. (defvar org-deadline-regexp nil
  3985. "Matches the DEADLINE keyword.")
  3986. (make-variable-buffer-local 'org-deadline-regexp)
  3987. (defvar org-deadline-time-regexp nil
  3988. "Matches the DEADLINE keyword together with a time stamp.")
  3989. (make-variable-buffer-local 'org-deadline-time-regexp)
  3990. (defvar org-deadline-time-hour-regexp nil
  3991. "Matches the DEADLINE keyword together with a time-and-hour stamp.")
  3992. (make-variable-buffer-local 'org-deadline-time-hour-regexp)
  3993. (defvar org-deadline-line-regexp nil
  3994. "Matches the DEADLINE keyword and the rest of the line.")
  3995. (make-variable-buffer-local 'org-deadline-line-regexp)
  3996. (defvar org-scheduled-regexp nil
  3997. "Matches the SCHEDULED keyword.")
  3998. (make-variable-buffer-local 'org-scheduled-regexp)
  3999. (defvar org-scheduled-time-regexp nil
  4000. "Matches the SCHEDULED keyword together with a time stamp.")
  4001. (make-variable-buffer-local 'org-scheduled-time-regexp)
  4002. (defvar org-scheduled-time-hour-regexp nil
  4003. "Matches the SCHEDULED keyword together with a time-and-hour stamp.")
  4004. (make-variable-buffer-local 'org-scheduled-time-hour-regexp)
  4005. (defvar org-closed-time-regexp nil
  4006. "Matches the CLOSED keyword together with a time stamp.")
  4007. (make-variable-buffer-local 'org-closed-time-regexp)
  4008. (defvar org-keyword-time-regexp nil
  4009. "Matches any of the 4 keywords, together with the time stamp.")
  4010. (make-variable-buffer-local 'org-keyword-time-regexp)
  4011. (defvar org-keyword-time-not-clock-regexp nil
  4012. "Matches any of the 3 keywords, together with the time stamp.")
  4013. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  4014. (defvar org-maybe-keyword-time-regexp nil
  4015. "Matches a timestamp, possibly preceded by a keyword.")
  4016. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  4017. (defvar org-all-time-keywords nil
  4018. "List of time keywords.")
  4019. (make-variable-buffer-local 'org-all-time-keywords)
  4020. (defconst org-plain-time-of-day-regexp
  4021. (concat
  4022. "\\(\\<[012]?[0-9]"
  4023. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4024. "\\(--?"
  4025. "\\(\\<[012]?[0-9]"
  4026. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4027. "\\)?")
  4028. "Regular expression to match a plain time or time range.
  4029. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4030. groups carry important information:
  4031. 0 the full match
  4032. 1 the first time, range or not
  4033. 8 the second time, if it is a range.")
  4034. (defconst org-plain-time-extension-regexp
  4035. (concat
  4036. "\\(\\<[012]?[0-9]"
  4037. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4038. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  4039. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  4040. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4041. groups carry important information:
  4042. 0 the full match
  4043. 7 hours of duration
  4044. 9 minutes of duration")
  4045. (defconst org-stamp-time-of-day-regexp
  4046. (concat
  4047. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  4048. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  4049. "\\(--?"
  4050. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  4051. "Regular expression to match a timestamp time or time range.
  4052. After a match, the following groups carry important information:
  4053. 0 the full match
  4054. 1 date plus weekday, for back referencing to make sure both times are on the same day
  4055. 2 the first time, range or not
  4056. 4 the second time, if it is a range.")
  4057. (defconst org-startup-options
  4058. '(("fold" org-startup-folded t)
  4059. ("overview" org-startup-folded t)
  4060. ("nofold" org-startup-folded nil)
  4061. ("showall" org-startup-folded nil)
  4062. ("showeverything" org-startup-folded showeverything)
  4063. ("content" org-startup-folded content)
  4064. ("indent" org-startup-indented t)
  4065. ("noindent" org-startup-indented nil)
  4066. ("hidestars" org-hide-leading-stars t)
  4067. ("showstars" org-hide-leading-stars nil)
  4068. ("odd" org-odd-levels-only t)
  4069. ("oddeven" org-odd-levels-only nil)
  4070. ("align" org-startup-align-all-tables t)
  4071. ("noalign" org-startup-align-all-tables nil)
  4072. ("inlineimages" org-startup-with-inline-images t)
  4073. ("noinlineimages" org-startup-with-inline-images nil)
  4074. ("latexpreview" org-startup-with-latex-preview t)
  4075. ("nolatexpreview" org-startup-with-latex-preview nil)
  4076. ("customtime" org-display-custom-times t)
  4077. ("logdone" org-log-done time)
  4078. ("lognotedone" org-log-done note)
  4079. ("nologdone" org-log-done nil)
  4080. ("lognoteclock-out" org-log-note-clock-out t)
  4081. ("nolognoteclock-out" org-log-note-clock-out nil)
  4082. ("logrepeat" org-log-repeat state)
  4083. ("lognoterepeat" org-log-repeat note)
  4084. ("logdrawer" org-log-into-drawer t)
  4085. ("nologdrawer" org-log-into-drawer nil)
  4086. ("logstatesreversed" org-log-states-order-reversed t)
  4087. ("nologstatesreversed" org-log-states-order-reversed nil)
  4088. ("nologrepeat" org-log-repeat nil)
  4089. ("logreschedule" org-log-reschedule time)
  4090. ("lognotereschedule" org-log-reschedule note)
  4091. ("nologreschedule" org-log-reschedule nil)
  4092. ("logredeadline" org-log-redeadline time)
  4093. ("lognoteredeadline" org-log-redeadline note)
  4094. ("nologredeadline" org-log-redeadline nil)
  4095. ("logrefile" org-log-refile time)
  4096. ("lognoterefile" org-log-refile note)
  4097. ("nologrefile" org-log-refile nil)
  4098. ("fninline" org-footnote-define-inline t)
  4099. ("nofninline" org-footnote-define-inline nil)
  4100. ("fnlocal" org-footnote-section nil)
  4101. ("fnauto" org-footnote-auto-label t)
  4102. ("fnprompt" org-footnote-auto-label nil)
  4103. ("fnconfirm" org-footnote-auto-label confirm)
  4104. ("fnplain" org-footnote-auto-label plain)
  4105. ("fnadjust" org-footnote-auto-adjust t)
  4106. ("nofnadjust" org-footnote-auto-adjust nil)
  4107. ("constcgs" constants-unit-system cgs)
  4108. ("constSI" constants-unit-system SI)
  4109. ("noptag" org-tag-persistent-alist nil)
  4110. ("hideblocks" org-hide-block-startup t)
  4111. ("nohideblocks" org-hide-block-startup nil)
  4112. ("beamer" org-startup-with-beamer-mode t)
  4113. ("entitiespretty" org-pretty-entities t)
  4114. ("entitiesplain" org-pretty-entities nil))
  4115. "Variable associated with STARTUP options for org-mode.
  4116. Each element is a list of three items: the startup options (as written
  4117. in the #+STARTUP line), the corresponding variable, and the value to set
  4118. this variable to if the option is found. An optional forth element PUSH
  4119. means to push this value onto the list in the variable.")
  4120. (defun org-update-property-plist (key val props)
  4121. "Update PROPS with KEY and VAL."
  4122. (let* ((appending (string= "+" (substring key (- (length key) 1))))
  4123. (key (if appending (substring key 0 (- (length key) 1)) key))
  4124. (remainder (org-remove-if (lambda (p) (string= (car p) key)) props))
  4125. (previous (cdr (assoc key props))))
  4126. (if appending
  4127. (cons (cons key (if previous (concat previous " " val) val)) remainder)
  4128. (cons (cons key val) remainder))))
  4129. (defconst org-block-regexp
  4130. "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
  4131. "Regular expression for hiding blocks.")
  4132. (defconst org-heading-keyword-regexp-format
  4133. "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  4134. "Printf format for a regexp matching a headline with some keyword.
  4135. This regexp will match the headline of any node which has the
  4136. exact keyword that is put into the format. The keyword isn't in
  4137. any group by default, but the stars and the body are.")
  4138. (defconst org-heading-keyword-maybe-regexp-format
  4139. "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
  4140. "Printf format for a regexp matching a headline, possibly with some keyword.
  4141. This regexp can match any headline with the specified keyword, or
  4142. without a keyword. The keyword isn't in any group by default,
  4143. but the stars and the body are.")
  4144. (defun org-set-regexps-and-options ()
  4145. "Precompute regular expressions for current buffer."
  4146. (when (derived-mode-p 'org-mode)
  4147. (org-set-local 'org-todo-kwd-alist nil)
  4148. (org-set-local 'org-todo-key-alist nil)
  4149. (org-set-local 'org-todo-key-trigger nil)
  4150. (org-set-local 'org-todo-keywords-1 nil)
  4151. (org-set-local 'org-done-keywords nil)
  4152. (org-set-local 'org-todo-heads nil)
  4153. (org-set-local 'org-todo-sets nil)
  4154. (org-set-local 'org-todo-log-states nil)
  4155. (org-set-local 'org-file-properties nil)
  4156. (org-set-local 'org-file-tags nil)
  4157. (let ((re (org-make-options-regexp
  4158. '("CATEGORY" "TODO" "COLUMNS" "STARTUP" "ARCHIVE" "FILETAGS"
  4159. "TAGS" "LINK" "PRIORITIES" "CONSTANTS" "PROPERTY" "DRAWERS"
  4160. "SETUPFILE" "OPTIONS")
  4161. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  4162. (splitre "[ \t]+")
  4163. (scripts org-use-sub-superscripts)
  4164. kwds kws0 kwsa key log value cat arch tags const links hw dws
  4165. tail sep kws1 prio props ftags drawers ext-setup-or-nil setup-contents
  4166. (start 0))
  4167. (save-excursion
  4168. (save-restriction
  4169. (widen)
  4170. (goto-char (point-min))
  4171. (while (or (and ext-setup-or-nil
  4172. (string-match re ext-setup-or-nil start)
  4173. (setq start (match-end 0)))
  4174. (and (setq ext-setup-or-nil nil start 0)
  4175. (re-search-forward re nil t)))
  4176. (setq key (upcase (match-string 1 ext-setup-or-nil))
  4177. value (org-match-string-no-properties 2 ext-setup-or-nil))
  4178. (if (stringp value) (setq value (org-trim value)))
  4179. (cond
  4180. ((equal key "CATEGORY")
  4181. (setq cat value))
  4182. ((member key '("SEQ_TODO" "TODO"))
  4183. (push (cons 'sequence (org-split-string value splitre)) kwds))
  4184. ((equal key "TYP_TODO")
  4185. (push (cons 'type (org-split-string value splitre)) kwds))
  4186. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  4187. ;; general TODO-like setup
  4188. (push (cons (intern (downcase (match-string 1 key)))
  4189. (org-split-string value splitre)) kwds))
  4190. ((equal key "TAGS")
  4191. (setq tags (append tags (if tags '("\\n") nil)
  4192. (org-split-string value splitre))))
  4193. ((equal key "COLUMNS")
  4194. (org-set-local 'org-columns-default-format value))
  4195. ((equal key "LINK")
  4196. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  4197. (push (cons (match-string 1 value)
  4198. (org-trim (match-string 2 value)))
  4199. links)))
  4200. ((equal key "PRIORITIES")
  4201. (setq prio (org-split-string value " +")))
  4202. ((equal key "PROPERTY")
  4203. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4204. (setq props (org-update-property-plist (match-string 1 value)
  4205. (match-string 2 value)
  4206. props))))
  4207. ((equal key "FILETAGS")
  4208. (when (string-match "\\S-" value)
  4209. (setq ftags
  4210. (append
  4211. ftags
  4212. (apply 'append
  4213. (mapcar (lambda (x) (org-split-string x ":"))
  4214. (org-split-string value)))))))
  4215. ((equal key "DRAWERS")
  4216. (setq drawers (delete-dups (append org-drawers (org-split-string value splitre)))))
  4217. ((equal key "CONSTANTS")
  4218. (setq const (append const (org-split-string value splitre))))
  4219. ((equal key "STARTUP")
  4220. (let ((opts (org-split-string value splitre))
  4221. l var val)
  4222. (while (setq l (pop opts))
  4223. (when (setq l (assoc l org-startup-options))
  4224. (setq var (nth 1 l) val (nth 2 l))
  4225. (if (not (nth 3 l))
  4226. (set (make-local-variable var) val)
  4227. (if (not (listp (symbol-value var)))
  4228. (set (make-local-variable var) nil))
  4229. (set (make-local-variable var) (symbol-value var))
  4230. (add-to-list var val))))))
  4231. ((equal key "ARCHIVE")
  4232. (setq arch value)
  4233. (remove-text-properties 0 (length arch)
  4234. '(face t fontified t) arch))
  4235. ((equal key "OPTIONS")
  4236. (if (string-match "\\([ \t]\\|\\`\\)\\^:\\(t\\|nil\\|{}\\)" value)
  4237. (setq scripts (read (match-string 2 value)))))
  4238. ((equal key "SETUPFILE")
  4239. (setq setup-contents (org-file-contents
  4240. (expand-file-name
  4241. (org-remove-double-quotes value))
  4242. 'noerror))
  4243. (if (not ext-setup-or-nil)
  4244. (setq ext-setup-or-nil setup-contents start 0)
  4245. (setq ext-setup-or-nil
  4246. (concat (substring ext-setup-or-nil 0 start)
  4247. "\n" setup-contents "\n"
  4248. (substring ext-setup-or-nil start)))))))
  4249. ;; search for property blocks
  4250. (goto-char (point-min))
  4251. (while (re-search-forward org-block-regexp nil t)
  4252. (when (equal "PROPERTY" (upcase (match-string 1)))
  4253. (setq value (replace-regexp-in-string
  4254. "[\n\r]" " " (match-string 4)))
  4255. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4256. (setq props (org-update-property-plist (match-string 1 value)
  4257. (match-string 2 value)
  4258. props)))))))
  4259. (org-set-local 'org-use-sub-superscripts scripts)
  4260. (when cat
  4261. (org-set-local 'org-category (intern cat))
  4262. (push (cons "CATEGORY" cat) props))
  4263. (when prio
  4264. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  4265. (setq prio (mapcar 'string-to-char prio))
  4266. (org-set-local 'org-highest-priority (nth 0 prio))
  4267. (org-set-local 'org-lowest-priority (nth 1 prio))
  4268. (org-set-local 'org-default-priority (nth 2 prio)))
  4269. (and props (org-set-local 'org-file-properties (nreverse props)))
  4270. (and ftags (org-set-local 'org-file-tags
  4271. (mapcar 'org-add-prop-inherited ftags)))
  4272. (and drawers (org-set-local 'org-drawers drawers))
  4273. (and arch (org-set-local 'org-archive-location arch))
  4274. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  4275. ;; Process the TODO keywords
  4276. (unless kwds
  4277. ;; Use the global values as if they had been given locally.
  4278. (setq kwds (default-value 'org-todo-keywords))
  4279. (if (stringp (car kwds))
  4280. (setq kwds (list (cons org-todo-interpretation
  4281. (default-value 'org-todo-keywords)))))
  4282. (setq kwds (reverse kwds)))
  4283. (setq kwds (nreverse kwds))
  4284. (let (inter kws kw)
  4285. (while (setq kws (pop kwds))
  4286. (let ((kws (or
  4287. (run-hook-with-args-until-success
  4288. 'org-todo-setup-filter-hook kws)
  4289. kws)))
  4290. (setq inter (pop kws) sep (member "|" kws)
  4291. kws0 (delete "|" (copy-sequence kws))
  4292. kwsa nil
  4293. kws1 (mapcar
  4294. (lambda (x)
  4295. ;; 1 2
  4296. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  4297. (progn
  4298. (setq kw (match-string 1 x)
  4299. key (and (match-end 2) (match-string 2 x))
  4300. log (org-extract-log-state-settings x))
  4301. (push (cons kw (and key (string-to-char key))) kwsa)
  4302. (and log (push log org-todo-log-states))
  4303. kw)
  4304. (error "Invalid TODO keyword %s" x)))
  4305. kws0)
  4306. kwsa (if kwsa (append '((:startgroup))
  4307. (nreverse kwsa)
  4308. '((:endgroup))))
  4309. hw (car kws1)
  4310. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  4311. tail (list inter hw (car dws) (org-last dws))))
  4312. (add-to-list 'org-todo-heads hw 'append)
  4313. (push kws1 org-todo-sets)
  4314. (setq org-done-keywords (append org-done-keywords dws nil))
  4315. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  4316. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  4317. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  4318. (setq org-todo-sets (nreverse org-todo-sets)
  4319. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  4320. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  4321. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  4322. ;; Process the constants
  4323. (when const
  4324. (let (e cst)
  4325. (while (setq e (pop const))
  4326. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  4327. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  4328. (setq org-table-formula-constants-local cst)))
  4329. ;; Process the tags.
  4330. (when tags
  4331. (let (e tgs)
  4332. (while (setq e (pop tags))
  4333. (cond
  4334. ((equal e "{") (push '(:startgroup) tgs))
  4335. ((equal e "}") (push '(:endgroup) tgs))
  4336. ((equal e "\\n") (push '(:newline) tgs))
  4337. ((string-match (org-re "^\\([[:alnum:]_@#%]+\\)(\\(.\\))$") e)
  4338. (push (cons (match-string 1 e)
  4339. (string-to-char (match-string 2 e)))
  4340. tgs))
  4341. (t (push (list e) tgs))))
  4342. (org-set-local 'org-tag-alist nil)
  4343. (while (setq e (pop tgs))
  4344. (or (and (stringp (car e))
  4345. (assoc (car e) org-tag-alist))
  4346. (push e org-tag-alist)))))
  4347. ;; Compute the regular expressions and other local variables.
  4348. ;; Using `org-outline-regexp-bol' would complicate them much,
  4349. ;; because of the fixed white space at the end of that string.
  4350. (if (not org-done-keywords)
  4351. (setq org-done-keywords (and org-todo-keywords-1
  4352. (list (org-last org-todo-keywords-1)))))
  4353. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  4354. (length org-scheduled-string)
  4355. (length org-clock-string)
  4356. (length org-closed-string)))
  4357. org-drawer-regexp
  4358. (concat "^[ \t]*:\\("
  4359. (mapconcat 'regexp-quote org-drawers "\\|")
  4360. "\\):[ \t]*$")
  4361. org-not-done-keywords
  4362. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  4363. org-todo-regexp
  4364. (concat "\\("
  4365. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4366. "\\)")
  4367. org-not-done-regexp
  4368. (concat "\\("
  4369. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  4370. "\\)")
  4371. org-not-done-heading-regexp
  4372. (format org-heading-keyword-regexp-format org-not-done-regexp)
  4373. org-todo-line-regexp
  4374. (format org-heading-keyword-maybe-regexp-format org-todo-regexp)
  4375. org-complex-heading-regexp
  4376. (concat "^\\(\\*+\\)"
  4377. "\\(?: +" org-todo-regexp "\\)?"
  4378. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4379. "\\(?: +\\(.*?\\)\\)??"
  4380. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?")
  4381. "[ \t]*$")
  4382. org-complex-heading-regexp-format
  4383. (concat "^\\(\\*+\\)"
  4384. "\\(?: +" org-todo-regexp "\\)?"
  4385. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4386. "\\(?: +"
  4387. ;; Stats cookies can be stuck to body.
  4388. "\\(?:\\[[0-9%%/]+\\] *\\)?"
  4389. "\\(%s\\)"
  4390. "\\(?: *\\[[0-9%%/]+\\]\\)?"
  4391. "\\)"
  4392. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?")
  4393. "[ \t]*$")
  4394. org-todo-line-tags-regexp
  4395. (concat "^\\(\\*+\\)"
  4396. "\\(?: +" org-todo-regexp "\\)?"
  4397. "\\(?: +\\(.*?\\)\\)??"
  4398. (org-re "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?")
  4399. "[ \t]*$")
  4400. org-deadline-regexp (concat "\\<" org-deadline-string)
  4401. org-deadline-time-regexp
  4402. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  4403. org-deadline-time-hour-regexp
  4404. (concat "\\<" org-deadline-string
  4405. " *<\\(.+[0-9]\\{1,2\\}:[0-9]\\{2\\}[^>]*\\)>")
  4406. org-deadline-line-regexp
  4407. (concat "\\<\\(" org-deadline-string "\\).*")
  4408. org-scheduled-regexp
  4409. (concat "\\<" org-scheduled-string)
  4410. org-scheduled-time-regexp
  4411. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  4412. org-scheduled-time-hour-regexp
  4413. (concat "\\<" org-scheduled-string
  4414. " *<\\(.+[0-9]\\{1,2\\}:[0-9]\\{2\\}[^>]*\\)>")
  4415. org-closed-time-regexp
  4416. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  4417. org-keyword-time-regexp
  4418. (concat "\\<\\(" org-scheduled-string
  4419. "\\|" org-deadline-string
  4420. "\\|" org-closed-string
  4421. "\\|" org-clock-string "\\)"
  4422. " *[[<]\\([^]>]+\\)[]>]")
  4423. org-keyword-time-not-clock-regexp
  4424. (concat "\\<\\(" org-scheduled-string
  4425. "\\|" org-deadline-string
  4426. "\\|" org-closed-string
  4427. "\\)"
  4428. " *[[<]\\([^]>]+\\)[]>]")
  4429. org-maybe-keyword-time-regexp
  4430. (concat "\\(\\<\\(" org-scheduled-string
  4431. "\\|" org-deadline-string
  4432. "\\|" org-closed-string
  4433. "\\|" org-clock-string "\\)\\)?"
  4434. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  4435. org-all-time-keywords
  4436. (mapcar (lambda (w) (substring w 0 -1))
  4437. (list org-scheduled-string org-deadline-string
  4438. org-clock-string org-closed-string)))
  4439. (org-compute-latex-and-related-regexp)
  4440. (org-set-font-lock-defaults))))
  4441. (defun org-file-contents (file &optional noerror)
  4442. "Return the contents of FILE, as a string."
  4443. (if (or (not file)
  4444. (not (file-readable-p file)))
  4445. (if noerror
  4446. (progn
  4447. (message "Cannot read file \"%s\"" file)
  4448. (ding) (sit-for 2)
  4449. "")
  4450. (error "Cannot read file \"%s\"" file))
  4451. (with-temp-buffer
  4452. (insert-file-contents file)
  4453. (buffer-string))))
  4454. (defun org-extract-log-state-settings (x)
  4455. "Extract the log state setting from a TODO keyword string.
  4456. This will extract info from a string like \"WAIT(w@/!)\"."
  4457. (let (kw key log1 log2)
  4458. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4459. (setq kw (match-string 1 x)
  4460. key (and (match-end 2) (match-string 2 x))
  4461. log1 (and (match-end 3) (match-string 3 x))
  4462. log2 (and (match-end 4) (match-string 4 x)))
  4463. (and (or log1 log2)
  4464. (list kw
  4465. (and log1 (if (equal log1 "!") 'time 'note))
  4466. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4467. (defun org-remove-keyword-keys (list)
  4468. "Remove a pair of parenthesis at the end of each string in LIST."
  4469. (mapcar (lambda (x)
  4470. (if (string-match "(.*)$" x)
  4471. (substring x 0 (match-beginning 0))
  4472. x))
  4473. list))
  4474. (defun org-assign-fast-keys (alist)
  4475. "Assign fast keys to a keyword-key alist.
  4476. Respect keys that are already there."
  4477. (let (new e (alt ?0))
  4478. (while (setq e (pop alist))
  4479. (if (or (memq (car e) '(:newline :endgroup :startgroup))
  4480. (cdr e)) ;; Key already assigned.
  4481. (push e new)
  4482. (let ((clist (string-to-list (downcase (car e))))
  4483. (used (append new alist)))
  4484. (when (= (car clist) ?@)
  4485. (pop clist))
  4486. (while (and clist (rassoc (car clist) used))
  4487. (pop clist))
  4488. (unless clist
  4489. (while (rassoc alt used)
  4490. (incf alt)))
  4491. (push (cons (car e) (or (car clist) alt)) new))))
  4492. (nreverse new)))
  4493. ;;; Some variables used in various places
  4494. (defvar org-window-configuration nil
  4495. "Used in various places to store a window configuration.")
  4496. (defvar org-selected-window nil
  4497. "Used in various places to store a window configuration.")
  4498. (defvar org-finish-function nil
  4499. "Function to be called when `C-c C-c' is used.
  4500. This is for getting out of special buffers like capture.")
  4501. ;; FIXME: Occasionally check by commenting these, to make sure
  4502. ;; no other functions uses these, forgetting to let-bind them.
  4503. (org-no-warnings (defvar entry)) ;; unprefixed, from calendar.el
  4504. (defvar org-last-state)
  4505. (org-no-warnings (defvar date)) ;; unprefixed, from calendar.el
  4506. ;; Defined somewhere in this file, but used before definition.
  4507. (defvar org-entities) ;; defined in org-entities.el
  4508. (defvar org-struct-menu)
  4509. (defvar org-org-menu)
  4510. (defvar org-tbl-menu)
  4511. ;;;; Define the Org-mode
  4512. ;; We use a before-change function to check if a table might need
  4513. ;; an update.
  4514. (defvar org-table-may-need-update t
  4515. "Indicates that a table might need an update.
  4516. This variable is set by `org-before-change-function'.
  4517. `org-table-align' sets it back to nil.")
  4518. (defun org-before-change-function (beg end)
  4519. "Every change indicates that a table might need an update."
  4520. (setq org-table-may-need-update t))
  4521. (defvar org-mode-map)
  4522. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4523. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4524. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4525. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4526. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4527. (defvar org-table-buffer-is-an nil)
  4528. (defvar bidi-paragraph-direction)
  4529. (defvar buffer-face-mode-face)
  4530. (require 'outline)
  4531. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4532. (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"))
  4533. (require 'noutline "noutline" 'noerror) ;; stock XEmacs does not have it
  4534. ;; Other stuff we need.
  4535. (require 'time-date)
  4536. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  4537. (require 'easymenu)
  4538. (require 'overlay)
  4539. ;; (require 'org-macs) moved higher up in the file before it is first used
  4540. (require 'org-entities)
  4541. ;; (require 'org-compat) moved higher up in the file before it is first used
  4542. (require 'org-faces)
  4543. (require 'org-list)
  4544. (require 'org-pcomplete)
  4545. (require 'org-src)
  4546. (require 'org-footnote)
  4547. (require 'org-macro)
  4548. ;; babel
  4549. (require 'ob)
  4550. ;;;###autoload
  4551. (define-derived-mode org-mode outline-mode "Org"
  4552. "Outline-based notes management and organizer, alias
  4553. \"Carsten's outline-mode for keeping track of everything.\"
  4554. Org-mode develops organizational tasks around a NOTES file which
  4555. contains information about projects as plain text. Org-mode is
  4556. implemented on top of outline-mode, which is ideal to keep the content
  4557. of large files well structured. It supports ToDo items, deadlines and
  4558. time stamps, which magically appear in the diary listing of the Emacs
  4559. calendar. Tables are easily created with a built-in table editor.
  4560. Plain text URL-like links connect to websites, emails (VM), Usenet
  4561. messages (Gnus), BBDB entries, and any files related to the project.
  4562. For printing and sharing of notes, an Org-mode file (or a part of it)
  4563. can be exported as a structured ASCII or HTML file.
  4564. The following commands are available:
  4565. \\{org-mode-map}"
  4566. ;; Get rid of Outline menus, they are not needed
  4567. ;; Need to do this here because define-derived-mode sets up
  4568. ;; the keymap so late. Still, it is a waste to call this each time
  4569. ;; we switch another buffer into org-mode.
  4570. (if (featurep 'xemacs)
  4571. (when (boundp 'outline-mode-menu-heading)
  4572. ;; Assume this is Greg's port, it uses easymenu
  4573. (easy-menu-remove outline-mode-menu-heading)
  4574. (easy-menu-remove outline-mode-menu-show)
  4575. (easy-menu-remove outline-mode-menu-hide))
  4576. (define-key org-mode-map [menu-bar headings] 'undefined)
  4577. (define-key org-mode-map [menu-bar hide] 'undefined)
  4578. (define-key org-mode-map [menu-bar show] 'undefined))
  4579. (org-load-modules-maybe)
  4580. (easy-menu-add org-org-menu)
  4581. (easy-menu-add org-tbl-menu)
  4582. (org-install-agenda-files-menu)
  4583. (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4584. (add-to-invisibility-spec '(org-cwidth))
  4585. (add-to-invisibility-spec '(org-hide-block . t))
  4586. (when (featurep 'xemacs)
  4587. (org-set-local 'line-move-ignore-invisible t))
  4588. (org-set-local 'outline-regexp org-outline-regexp)
  4589. (org-set-local 'outline-level 'org-outline-level)
  4590. (setq bidi-paragraph-direction 'left-to-right)
  4591. ;; FIXME Circumvent a bug in outline.el (Emacs <24.4)
  4592. (set (make-local-variable 'paragraph-start) " \\|[ \t]*$\\|\\*+ ")
  4593. (when (and org-ellipsis
  4594. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4595. (fboundp 'make-glyph-code))
  4596. (unless org-display-table
  4597. (setq org-display-table (make-display-table)))
  4598. (set-display-table-slot
  4599. org-display-table 4
  4600. (vconcat (mapcar
  4601. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4602. org-ellipsis)))
  4603. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4604. (setq buffer-display-table org-display-table))
  4605. (org-set-regexps-and-options)
  4606. (when (and org-tag-faces (not org-tags-special-faces-re))
  4607. ;; tag faces set outside customize.... force initialization.
  4608. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4609. ;; Calc embedded
  4610. (org-set-local 'calc-embedded-open-mode "# ")
  4611. ;; Modify a few syntax entries
  4612. (modify-syntax-entry ?@ "w")
  4613. (modify-syntax-entry ?\" "\"")
  4614. (if org-startup-truncated (setq truncate-lines t))
  4615. (when org-startup-indented (require 'org-indent) (org-indent-mode 1))
  4616. (org-set-local 'font-lock-unfontify-region-function
  4617. 'org-unfontify-region)
  4618. ;; Activate before-change-function
  4619. (org-set-local 'org-table-may-need-update t)
  4620. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4621. 'local)
  4622. ;; Check for running clock before killing a buffer
  4623. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4624. ;; Initialize macros templates.
  4625. (org-macro-initialize-templates)
  4626. ;; Initialize radio targets.
  4627. (org-update-radio-target-regexp)
  4628. ;; Indentation.
  4629. (org-set-local 'indent-line-function 'org-indent-line)
  4630. (org-set-local 'indent-region-function 'org-indent-region)
  4631. ;; Filling and auto-filling.
  4632. (org-setup-filling)
  4633. ;; Comments.
  4634. (org-setup-comments-handling)
  4635. ;; Beginning/end of defun
  4636. (org-set-local 'beginning-of-defun-function 'org-backward-element)
  4637. (org-set-local 'end-of-defun-function 'org-forward-element)
  4638. ;; Next error for sparse trees
  4639. (org-set-local 'next-error-function 'org-occur-next-match)
  4640. ;; Make sure dependence stuff works reliably, even for users who set it
  4641. ;; too late :-(
  4642. (if org-enforce-todo-dependencies
  4643. (add-hook 'org-blocker-hook
  4644. 'org-block-todo-from-children-or-siblings-or-parent)
  4645. (remove-hook 'org-blocker-hook
  4646. 'org-block-todo-from-children-or-siblings-or-parent))
  4647. (if org-enforce-todo-checkbox-dependencies
  4648. (add-hook 'org-blocker-hook
  4649. 'org-block-todo-from-checkboxes)
  4650. (remove-hook 'org-blocker-hook
  4651. 'org-block-todo-from-checkboxes))
  4652. ;; Align options lines
  4653. (org-set-local
  4654. 'align-mode-rules-list
  4655. '((org-in-buffer-settings
  4656. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4657. (modes . '(org-mode)))))
  4658. ;; Imenu
  4659. (org-set-local 'imenu-create-index-function
  4660. 'org-imenu-get-tree)
  4661. ;; Make isearch reveal context
  4662. (if (or (featurep 'xemacs)
  4663. (not (boundp 'outline-isearch-open-invisible-function)))
  4664. ;; Emacs 21 and XEmacs make use of the hook
  4665. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4666. ;; Emacs 22 deals with this through a special variable
  4667. (org-set-local 'outline-isearch-open-invisible-function
  4668. (lambda (&rest ignore) (org-show-context 'isearch)))
  4669. (org-add-hook 'isearch-mode-end-hook 'org-fix-ellipsis-at-bol 'append 'local))
  4670. ;; Setup the pcomplete hooks
  4671. (set (make-local-variable 'pcomplete-command-completion-function)
  4672. 'org-pcomplete-initial)
  4673. (set (make-local-variable 'pcomplete-command-name-function)
  4674. 'org-command-at-point)
  4675. (set (make-local-variable 'pcomplete-default-completion-function)
  4676. 'ignore)
  4677. (set (make-local-variable 'pcomplete-parse-arguments-function)
  4678. 'org-parse-arguments)
  4679. (set (make-local-variable 'pcomplete-termination-string) "")
  4680. (when (>= emacs-major-version 23)
  4681. (set (make-local-variable 'buffer-face-mode-face) 'org-default))
  4682. ;; If empty file that did not turn on org-mode automatically, make it to.
  4683. (if (and org-insert-mode-line-in-empty-file
  4684. (org-called-interactively-p 'any)
  4685. (= (point-min) (point-max)))
  4686. (insert "# -*- mode: org -*-\n\n"))
  4687. (unless org-inhibit-startup
  4688. (org-unmodified
  4689. (and org-startup-with-beamer-mode (org-beamer-mode))
  4690. (when org-startup-align-all-tables
  4691. (org-table-map-tables 'org-table-align 'quietly))
  4692. (when org-startup-with-inline-images
  4693. (org-display-inline-images))
  4694. (when org-startup-with-latex-preview
  4695. (org-preview-latex-fragment))
  4696. (unless org-inhibit-startup-visibility-stuff
  4697. (org-set-startup-visibility))))
  4698. ;; Try to set org-hide correctly
  4699. (set-face-foreground 'org-hide (org-find-invisible-foreground)))
  4700. ;; Update `customize-package-emacs-version-alist'
  4701. (add-to-list 'customize-package-emacs-version-alist
  4702. '(Org ("6.21b" . "23.1") ("6.33x" . "23.2")
  4703. ("7.8.11" . "24.1") ("7.9.4" . "24.3")
  4704. ("8.0" . "24.4")))
  4705. (defvar org-mode-transpose-word-syntax-table
  4706. (let ((st (make-syntax-table)))
  4707. (mapc (lambda(c) (modify-syntax-entry
  4708. (string-to-char (car c)) "w p" st))
  4709. org-emphasis-alist)
  4710. st))
  4711. (when (fboundp 'abbrev-table-put)
  4712. (abbrev-table-put org-mode-abbrev-table
  4713. :parents (list text-mode-abbrev-table)))
  4714. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4715. (defsubst org-fix-ellipsis-at-bol ()
  4716. (save-excursion (goto-char (window-start)) (recenter 0)))
  4717. (defun org-find-invisible-foreground ()
  4718. (let ((candidates (remove
  4719. "unspecified-bg"
  4720. (nconc
  4721. (list (face-background 'default)
  4722. (face-background 'org-default))
  4723. (mapcar
  4724. (lambda (alist)
  4725. (when (boundp alist)
  4726. (cdr (assoc 'background-color (symbol-value alist)))))
  4727. '(default-frame-alist initial-frame-alist window-system-default-frame-alist))
  4728. (list (face-foreground 'org-hide))))))
  4729. (car (remove nil candidates))))
  4730. (defun org-current-time (&optional rounding-minutes past)
  4731. "Current time, possibly rounded to ROUNDING-MINUTES.
  4732. When ROUNDING-MINUTES is not an integer, fall back on the car of
  4733. `org-time-stamp-rounding-minutes'. When PAST is non-nil, ensure
  4734. the rounding returns a past time."
  4735. (let ((r (or (and (integerp rounding-minutes) rounding-minutes)
  4736. (car org-time-stamp-rounding-minutes)))
  4737. (time (decode-time)) res)
  4738. (if (< r 1)
  4739. (current-time)
  4740. (setq res
  4741. (apply 'encode-time
  4742. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4743. (nthcdr 2 time))))
  4744. (if (and past (< (org-float-time (time-subtract (current-time) res)) 0))
  4745. (seconds-to-time (- (org-float-time res) (* r 60)))
  4746. res))))
  4747. (defun org-today ()
  4748. "Return today date, considering `org-extend-today-until'."
  4749. (time-to-days
  4750. (time-subtract (current-time)
  4751. (list 0 (* 3600 org-extend-today-until) 0))))
  4752. ;;;; Font-Lock stuff, including the activators
  4753. (defvar org-mouse-map (make-sparse-keymap))
  4754. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4755. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4756. (when org-mouse-1-follows-link
  4757. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4758. (when org-tab-follows-link
  4759. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4760. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4761. (require 'font-lock)
  4762. (defconst org-non-link-chars "]\t\n\r<>")
  4763. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  4764. "shell" "elisp" "doi" "message"))
  4765. (defvar org-link-types-re nil
  4766. "Matches a link that has a url-like prefix like \"http:\"")
  4767. (defvar org-link-re-with-space nil
  4768. "Matches a link with spaces, optional angular brackets around it.")
  4769. (defvar org-link-re-with-space2 nil
  4770. "Matches a link with spaces, optional angular brackets around it.")
  4771. (defvar org-link-re-with-space3 nil
  4772. "Matches a link with spaces, only for internal part in bracket links.")
  4773. (defvar org-angle-link-re nil
  4774. "Matches link with angular brackets, spaces are allowed.")
  4775. (defvar org-plain-link-re nil
  4776. "Matches plain link, without spaces.")
  4777. (defvar org-bracket-link-regexp nil
  4778. "Matches a link in double brackets.")
  4779. (defvar org-bracket-link-analytic-regexp nil
  4780. "Regular expression used to analyze links.
  4781. Here is what the match groups contain after a match:
  4782. 1: http:
  4783. 2: http
  4784. 3: path
  4785. 4: [desc]
  4786. 5: desc")
  4787. (defvar org-bracket-link-analytic-regexp++ nil
  4788. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4789. (defvar org-any-link-re nil
  4790. "Regular expression matching any link.")
  4791. (defconst org-match-sexp-depth 3
  4792. "Number of stacked braces for sub/superscript matching.")
  4793. (defun org-create-multibrace-regexp (left right n)
  4794. "Create a regular expression which will match a balanced sexp.
  4795. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4796. as single character strings.
  4797. The regexp returned will match the entire expression including the
  4798. delimiters. It will also define a single group which contains the
  4799. match except for the outermost delimiters. The maximum depth of
  4800. stacked delimiters is N. Escaping delimiters is not possible."
  4801. (let* ((nothing (concat "[^" left right "]*?"))
  4802. (or "\\|")
  4803. (re nothing)
  4804. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4805. (while (> n 1)
  4806. (setq n (1- n)
  4807. re (concat re or next)
  4808. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4809. (concat left "\\(" re "\\)" right)))
  4810. (defvar org-match-substring-regexp
  4811. (concat
  4812. "\\(\\S-\\)\\([_^]\\)\\("
  4813. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4814. "\\|"
  4815. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  4816. "\\|"
  4817. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  4818. "The regular expression matching a sub- or superscript.")
  4819. (defvar org-match-substring-with-braces-regexp
  4820. (concat
  4821. "\\(\\S-\\)\\([_^]\\)\\("
  4822. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4823. "\\)")
  4824. "The regular expression matching a sub- or superscript, forcing braces.")
  4825. (defun org-make-link-regexps ()
  4826. "Update the link regular expressions.
  4827. This should be called after the variable `org-link-types' has changed."
  4828. (setq org-link-types-re
  4829. (concat
  4830. "\\`\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):")
  4831. org-link-re-with-space
  4832. (concat
  4833. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4834. "\\([^" org-non-link-chars " ]"
  4835. "[^" org-non-link-chars "]*"
  4836. "[^" org-non-link-chars " ]\\)>?")
  4837. org-link-re-with-space2
  4838. (concat
  4839. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4840. "\\([^" org-non-link-chars " ]"
  4841. "[^\t\n\r]*"
  4842. "[^" org-non-link-chars " ]\\)>?")
  4843. org-link-re-with-space3
  4844. (concat
  4845. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4846. "\\([^" org-non-link-chars " ]"
  4847. "[^\t\n\r]*\\)")
  4848. org-angle-link-re
  4849. (concat
  4850. "<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4851. "\\([^" org-non-link-chars " ]"
  4852. "[^" org-non-link-chars "]*"
  4853. "\\)>")
  4854. org-plain-link-re
  4855. (concat
  4856. "\\<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4857. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  4858. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4859. org-bracket-link-regexp
  4860. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4861. org-bracket-link-analytic-regexp
  4862. (concat
  4863. "\\[\\["
  4864. "\\(\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):\\)?"
  4865. "\\([^]]+\\)"
  4866. "\\]"
  4867. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4868. "\\]")
  4869. org-bracket-link-analytic-regexp++
  4870. (concat
  4871. "\\[\\["
  4872. "\\(\\(" (mapconcat 'regexp-quote (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  4873. "\\([^]]+\\)"
  4874. "\\]"
  4875. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4876. "\\]")
  4877. org-any-link-re
  4878. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4879. org-angle-link-re "\\)\\|\\("
  4880. org-plain-link-re "\\)")))
  4881. (org-make-link-regexps)
  4882. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
  4883. "Regular expression for fast time stamp matching.")
  4884. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
  4885. "Regular expression for fast time stamp matching.")
  4886. (defconst org-ts-regexp0
  4887. "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\( +[^]+0-9>\r\n -]+\\)?\\( +\\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4888. "Regular expression matching time strings for analysis.
  4889. This one does not require the space after the date, so it can be used
  4890. on a string that terminates immediately after the date.")
  4891. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4892. "Regular expression matching time strings for analysis.")
  4893. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  4894. "Regular expression matching time stamps, with groups.")
  4895. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  4896. "Regular expression matching time stamps (also [..]), with groups.")
  4897. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  4898. "Regular expression matching a time stamp range.")
  4899. (defconst org-tr-regexp-both
  4900. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  4901. "Regular expression matching a time stamp range.")
  4902. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  4903. org-ts-regexp "\\)?")
  4904. "Regular expression matching a time stamp or time stamp range.")
  4905. (defconst org-tsr-regexp-both
  4906. (concat org-ts-regexp-both "\\(--?-?"
  4907. org-ts-regexp-both "\\)?")
  4908. "Regular expression matching a time stamp or time stamp range.
  4909. The time stamps may be either active or inactive.")
  4910. (defvar org-emph-face nil)
  4911. (defun org-do-emphasis-faces (limit)
  4912. "Run through the buffer and add overlays to emphasized strings."
  4913. (let (rtn a)
  4914. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  4915. (if (not (= (char-after (match-beginning 3))
  4916. (char-after (match-beginning 4))))
  4917. (progn
  4918. (setq rtn t)
  4919. (setq a (assoc (match-string 3) org-emphasis-alist))
  4920. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  4921. 'face
  4922. (nth 1 a))
  4923. (and (nth 4 a)
  4924. (org-remove-flyspell-overlays-in
  4925. (match-beginning 0) (match-end 0)))
  4926. (add-text-properties (match-beginning 2) (match-end 2)
  4927. '(font-lock-multiline t org-emphasis t))
  4928. (when org-hide-emphasis-markers
  4929. (add-text-properties (match-end 4) (match-beginning 5)
  4930. '(invisible org-link))
  4931. (add-text-properties (match-beginning 3) (match-end 3)
  4932. '(invisible org-link)))))
  4933. (backward-char 1))
  4934. rtn))
  4935. (defun org-emphasize (&optional char)
  4936. "Insert or change an emphasis, i.e. a font like bold or italic.
  4937. If there is an active region, change that region to a new emphasis.
  4938. If there is no region, just insert the marker characters and position
  4939. the cursor between them.
  4940. CHAR should be either the marker character, or the first character of the
  4941. HTML tag associated with that emphasis. If CHAR is a space, the means
  4942. to remove the emphasis of the selected region.
  4943. If char is not given (for example in an interactive call) it
  4944. will be prompted for."
  4945. (interactive)
  4946. (let ((eal org-emphasis-alist) e det
  4947. (erc org-emphasis-regexp-components)
  4948. (prompt "")
  4949. (string "") beg end move tag c s)
  4950. (if (org-region-active-p)
  4951. (setq beg (region-beginning) end (region-end)
  4952. string (buffer-substring beg end))
  4953. (setq move t))
  4954. (while (setq e (pop eal))
  4955. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  4956. c (aref tag 0))
  4957. (push (cons c (string-to-char (car e))) det)
  4958. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  4959. (substring tag 1)))))
  4960. (setq det (nreverse det))
  4961. (unless char
  4962. (message "%s" (concat "Emphasis marker or tag:" prompt))
  4963. (setq char (read-char-exclusive)))
  4964. (setq char (or (cdr (assoc char det)) char))
  4965. (if (equal char ?\ )
  4966. (setq s "" move nil)
  4967. (unless (assoc (char-to-string char) org-emphasis-alist)
  4968. (error "No such emphasis marker: \"%c\"" char))
  4969. (setq s (char-to-string char)))
  4970. (while (and (> (length string) 1)
  4971. (equal (substring string 0 1) (substring string -1))
  4972. (assoc (substring string 0 1) org-emphasis-alist))
  4973. (setq string (substring string 1 -1)))
  4974. (setq string (concat s string s))
  4975. (if beg (delete-region beg end))
  4976. (unless (or (bolp)
  4977. (string-match (concat "[" (nth 0 erc) "\n]")
  4978. (char-to-string (char-before (point)))))
  4979. (insert " "))
  4980. (unless (or (eobp)
  4981. (string-match (concat "[" (nth 1 erc) "\n]")
  4982. (char-to-string (char-after (point)))))
  4983. (insert " ") (backward-char 1))
  4984. (insert string)
  4985. (and move (backward-char 1))))
  4986. (defconst org-nonsticky-props
  4987. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text htmlize-link))
  4988. (defsubst org-rear-nonsticky-at (pos)
  4989. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  4990. (defun org-activate-plain-links (limit)
  4991. "Run through the buffer and add overlays to links."
  4992. (let (f hl)
  4993. (when (and (re-search-forward (concat org-plain-link-re) limit t)
  4994. (not (org-in-src-block-p)))
  4995. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4996. (setq f (get-text-property (match-beginning 0) 'face))
  4997. (setq hl (org-match-string-no-properties 0))
  4998. (if (or (eq f 'org-tag)
  4999. (and (listp f) (memq 'org-tag f)))
  5000. nil
  5001. (add-text-properties (match-beginning 0) (match-end 0)
  5002. (list 'mouse-face 'highlight
  5003. 'face 'org-link
  5004. 'htmlize-link `(:uri ,hl)
  5005. 'keymap org-mouse-map))
  5006. (org-rear-nonsticky-at (match-end 0)))
  5007. t)))
  5008. (defun org-activate-code (limit)
  5009. (if (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t)
  5010. (progn
  5011. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5012. (remove-text-properties (match-beginning 0) (match-end 0)
  5013. '(display t invisible t intangible t))
  5014. t)))
  5015. (defcustom org-src-fontify-natively nil
  5016. "When non-nil, fontify code in code blocks."
  5017. :type 'boolean
  5018. :version "24.1"
  5019. :group 'org-appearance
  5020. :group 'org-babel)
  5021. (defcustom org-allow-promoting-top-level-subtree nil
  5022. "When non-nil, allow promoting a top level subtree.
  5023. The leading star of the top level headline will be replaced
  5024. by a #."
  5025. :type 'boolean
  5026. :version "24.1"
  5027. :group 'org-appearance)
  5028. (defun org-fontify-meta-lines-and-blocks (limit)
  5029. (condition-case nil
  5030. (org-fontify-meta-lines-and-blocks-1 limit)
  5031. (error (message "org-mode fontification error"))))
  5032. (defun org-fontify-meta-lines-and-blocks-1 (limit)
  5033. "Fontify #+ lines and blocks, in the correct ways."
  5034. (let ((case-fold-search t))
  5035. (if (re-search-forward
  5036. "^\\([ \t]*#\\(\\(\\+[a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  5037. limit t)
  5038. (let ((beg (match-beginning 0))
  5039. (block-start (match-end 0))
  5040. (block-end nil)
  5041. (lang (match-string 7))
  5042. (beg1 (line-beginning-position 2))
  5043. (dc1 (downcase (match-string 2)))
  5044. (dc3 (downcase (match-string 3)))
  5045. end end1 quoting block-type ovl)
  5046. (cond
  5047. ((member dc1 '("+html:" "+ascii:" "+latex:"))
  5048. ;; a single line of backend-specific content
  5049. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5050. (remove-text-properties (match-beginning 0) (match-end 0)
  5051. '(display t invisible t intangible t))
  5052. (add-text-properties (match-beginning 1) (match-end 3)
  5053. '(font-lock-fontified t face org-meta-line))
  5054. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  5055. '(font-lock-fontified t face org-block))
  5056. ; for backend-specific code
  5057. t)
  5058. ((and (match-end 4) (equal dc3 "+begin"))
  5059. ;; Truly a block
  5060. (setq block-type (downcase (match-string 5))
  5061. quoting (member block-type org-protecting-blocks))
  5062. (when (re-search-forward
  5063. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  5064. nil t) ;; on purpose, we look further than LIMIT
  5065. (setq end (min (point-max) (match-end 0))
  5066. end1 (min (point-max) (1- (match-beginning 0))))
  5067. (setq block-end (match-beginning 0))
  5068. (when quoting
  5069. (remove-text-properties beg end
  5070. '(display t invisible t intangible t)))
  5071. (add-text-properties
  5072. beg end
  5073. '(font-lock-fontified t font-lock-multiline t))
  5074. (add-text-properties beg beg1 '(face org-meta-line))
  5075. (add-text-properties end1 (min (point-max) (1+ end))
  5076. '(face org-meta-line)) ; for end_src
  5077. (cond
  5078. ((and lang (not (string= lang "")) org-src-fontify-natively)
  5079. (org-src-font-lock-fontify-block lang block-start block-end)
  5080. ;; remove old background overlays
  5081. (mapc (lambda (ov)
  5082. (if (eq (overlay-get ov 'face) 'org-block-background)
  5083. (delete-overlay ov)))
  5084. (overlays-at (/ (+ beg1 block-end) 2)))
  5085. ;; add a background overlay
  5086. (setq ovl (make-overlay beg1 block-end))
  5087. (overlay-put ovl 'face 'org-block-background)
  5088. (overlay-put ovl 'evaporate t)) ;; make it go away when empty
  5089. (quoting
  5090. (add-text-properties beg1 (min (point-max) (1+ end1))
  5091. '(face org-block))) ; end of source block
  5092. ((not org-fontify-quote-and-verse-blocks))
  5093. ((string= block-type "quote")
  5094. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-quote)))
  5095. ((string= block-type "verse")
  5096. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-verse))))
  5097. (add-text-properties beg beg1 '(face org-block-begin-line))
  5098. (add-text-properties (min (point-max) (1+ end)) (min (point-max) (1+ end1))
  5099. '(face org-block-end-line))
  5100. t))
  5101. ((member dc1 '("+title:" "+author:" "+email:" "+date:"))
  5102. (add-text-properties
  5103. beg (match-end 3)
  5104. (if (member (intern (substring dc1 0 -1)) org-hidden-keywords)
  5105. '(font-lock-fontified t invisible t)
  5106. '(font-lock-fontified t face org-document-info-keyword)))
  5107. (add-text-properties
  5108. (match-beginning 6) (min (point-max) (1+ (match-end 6)))
  5109. (if (string-equal dc1 "+title:")
  5110. '(font-lock-fontified t face org-document-title)
  5111. '(font-lock-fontified t face org-document-info))))
  5112. ((or (equal dc1 "+results")
  5113. (member dc1 '("+begin:" "+end:" "+caption:" "+label:"
  5114. "+orgtbl:" "+tblfm:" "+tblname:" "+results:"
  5115. "+call:" "+header:" "+headers:" "+name:"))
  5116. (and (match-end 4) (equal dc3 "+attr")))
  5117. (add-text-properties
  5118. beg (match-end 0)
  5119. '(font-lock-fontified t face org-meta-line))
  5120. t)
  5121. ((member dc3 '(" " ""))
  5122. (add-text-properties
  5123. beg (match-end 0)
  5124. '(font-lock-fontified t face font-lock-comment-face)))
  5125. ((not (member (char-after beg) '(?\ ?\t)))
  5126. ;; just any other in-buffer setting, but not indented
  5127. (add-text-properties
  5128. beg (match-end 0)
  5129. '(font-lock-fontified t face org-meta-line))
  5130. t)
  5131. (t nil))))))
  5132. (defun org-activate-angle-links (limit)
  5133. "Run through the buffer and add overlays to links."
  5134. (if (and (re-search-forward org-angle-link-re limit t)
  5135. (not (org-in-src-block-p)))
  5136. (progn
  5137. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5138. (add-text-properties (match-beginning 0) (match-end 0)
  5139. (list 'mouse-face 'highlight
  5140. 'keymap org-mouse-map))
  5141. (org-rear-nonsticky-at (match-end 0))
  5142. t)))
  5143. (defun org-activate-footnote-links (limit)
  5144. "Run through the buffer and add overlays to footnotes."
  5145. (let ((fn (org-footnote-next-reference-or-definition limit)))
  5146. (when fn
  5147. (let ((beg (nth 1 fn)) (end (nth 2 fn)))
  5148. (org-remove-flyspell-overlays-in beg end)
  5149. (add-text-properties beg end
  5150. (list 'mouse-face 'highlight
  5151. 'keymap org-mouse-map
  5152. 'help-echo
  5153. (if (= (point-at-bol) beg)
  5154. "Footnote definition"
  5155. "Footnote reference")
  5156. 'font-lock-fontified t
  5157. 'font-lock-multiline t
  5158. 'face 'org-footnote))))))
  5159. (defun org-activate-bracket-links (limit)
  5160. "Run through the buffer and add overlays to bracketed links."
  5161. (if (and (re-search-forward org-bracket-link-regexp limit t)
  5162. (not (org-in-src-block-p)))
  5163. (let* ((hl (org-match-string-no-properties 1))
  5164. (help (concat "LINK: " hl))
  5165. ;; FIXME: Above we should remove the escapes. But that
  5166. ;; requires another match, protecting match data, a lot
  5167. ;; of overhead for font-lock.
  5168. (ip (org-maybe-intangible
  5169. (list 'invisible 'org-link
  5170. 'keymap org-mouse-map 'mouse-face 'highlight
  5171. 'font-lock-multiline t 'help-echo help
  5172. 'htmlize-link `(:uri ,hl))))
  5173. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  5174. 'font-lock-multiline t 'help-echo help
  5175. 'htmlize-link `(:uri ,hl))))
  5176. ;; We need to remove the invisible property here. Table narrowing
  5177. ;; may have made some of this invisible.
  5178. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5179. (remove-text-properties (match-beginning 0) (match-end 0)
  5180. '(invisible nil))
  5181. (if (match-end 3)
  5182. (progn
  5183. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  5184. (org-rear-nonsticky-at (match-beginning 3))
  5185. (add-text-properties (match-beginning 3) (match-end 3) vp)
  5186. (org-rear-nonsticky-at (match-end 3))
  5187. (add-text-properties (match-end 3) (match-end 0) ip)
  5188. (org-rear-nonsticky-at (match-end 0)))
  5189. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  5190. (org-rear-nonsticky-at (match-beginning 1))
  5191. (add-text-properties (match-beginning 1) (match-end 1) vp)
  5192. (org-rear-nonsticky-at (match-end 1))
  5193. (add-text-properties (match-end 1) (match-end 0) ip)
  5194. (org-rear-nonsticky-at (match-end 0)))
  5195. t)))
  5196. (defun org-activate-dates (limit)
  5197. "Run through the buffer and add overlays to dates."
  5198. (if (and (re-search-forward org-tsr-regexp-both limit t)
  5199. (not (equal (char-before (match-beginning 0)) 91)))
  5200. (progn
  5201. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5202. (add-text-properties (match-beginning 0) (match-end 0)
  5203. (list 'mouse-face 'highlight
  5204. 'keymap org-mouse-map))
  5205. (org-rear-nonsticky-at (match-end 0))
  5206. (when org-display-custom-times
  5207. (if (match-end 3)
  5208. (org-display-custom-time (match-beginning 3) (match-end 3)))
  5209. (org-display-custom-time (match-beginning 1) (match-end 1)))
  5210. t)))
  5211. (defvar org-target-link-regexp nil
  5212. "Regular expression matching radio targets in plain text.")
  5213. (make-variable-buffer-local 'org-target-link-regexp)
  5214. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  5215. "Regular expression matching a link target.")
  5216. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  5217. "Regular expression matching a radio target.")
  5218. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  5219. "Regular expression matching any target.")
  5220. (defun org-activate-target-links (limit)
  5221. "Run through the buffer and add overlays to target matches."
  5222. (when org-target-link-regexp
  5223. (let ((case-fold-search t))
  5224. (if (re-search-forward org-target-link-regexp limit t)
  5225. (progn
  5226. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5227. (add-text-properties (match-beginning 0) (match-end 0)
  5228. (list 'mouse-face 'highlight
  5229. 'keymap org-mouse-map
  5230. 'help-echo "Radio target link"
  5231. 'org-linked-text t))
  5232. (org-rear-nonsticky-at (match-end 0))
  5233. t)))))
  5234. (defun org-update-radio-target-regexp ()
  5235. "Find all radio targets in this file and update the regular expression."
  5236. (interactive)
  5237. (when (memq 'radio org-activate-links)
  5238. (setq org-target-link-regexp
  5239. (org-make-target-link-regexp (org-all-targets 'radio)))
  5240. (org-restart-font-lock)))
  5241. (defun org-hide-wide-columns (limit)
  5242. (let (s e)
  5243. (setq s (text-property-any (point) (or limit (point-max))
  5244. 'org-cwidth t))
  5245. (when s
  5246. (setq e (next-single-property-change s 'org-cwidth))
  5247. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  5248. (goto-char e)
  5249. t)))
  5250. (defvar org-latex-and-related-regexp nil
  5251. "Regular expression for highlighting LaTeX, entities and sub/superscript.")
  5252. (defvar org-match-substring-regexp)
  5253. (defvar org-match-substring-with-braces-regexp)
  5254. (defun org-compute-latex-and-related-regexp ()
  5255. "Compute regular expression for LaTeX, entities and sub/superscript.
  5256. Result depends on variable `org-highlight-latex-and-related'."
  5257. (org-set-local
  5258. 'org-latex-and-related-regexp
  5259. (let* ((re-sub
  5260. (cond ((not (memq 'script org-highlight-latex-and-related)) nil)
  5261. ((eq org-use-sub-superscripts '{})
  5262. (list org-match-substring-with-braces-regexp))
  5263. (org-use-sub-superscripts (list org-match-substring-regexp))))
  5264. (re-latex
  5265. (when (memq 'latex org-highlight-latex-and-related)
  5266. (let ((matchers (plist-get org-format-latex-options :matchers)))
  5267. (delq nil
  5268. (mapcar (lambda (x)
  5269. (and (member (car x) matchers) (nth 1 x)))
  5270. org-latex-regexps)))))
  5271. (re-entities
  5272. (when (memq 'entities org-highlight-latex-and-related)
  5273. (list "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))))
  5274. (mapconcat 'identity (append re-latex re-entities re-sub) "\\|"))))
  5275. (defun org-do-latex-and-related (limit)
  5276. "Highlight LaTeX snippets and environments, entities and sub/superscript.
  5277. LIMIT bounds the search for syntax to highlight. Stop at first
  5278. highlighted object, if any. Return t if some highlighting was
  5279. done, nil otherwise."
  5280. (when (org-string-nw-p org-latex-and-related-regexp)
  5281. (catch 'found
  5282. (while (re-search-forward org-latex-and-related-regexp limit t)
  5283. (unless (memq (car-safe (get-text-property (1+ (match-beginning 0))
  5284. 'face))
  5285. '(org-code org-verbatim underline))
  5286. (let ((offset (if (memq (char-after (1+ (match-beginning 0)))
  5287. '(?_ ?^))
  5288. 1
  5289. 0)))
  5290. (font-lock-prepend-text-property
  5291. (+ offset (match-beginning 0)) (match-end 0)
  5292. 'face 'org-latex-and-related)
  5293. (add-text-properties (+ offset (match-beginning 0)) (match-end 0)
  5294. '(font-lock-multiline t)))
  5295. (throw 'found t)))
  5296. nil)))
  5297. (defun org-restart-font-lock ()
  5298. "Restart `font-lock-mode', to force refontification."
  5299. (when (and (boundp 'font-lock-mode) font-lock-mode)
  5300. (font-lock-mode -1)
  5301. (font-lock-mode 1)))
  5302. (defun org-all-targets (&optional radio)
  5303. "Return a list of all targets in this file.
  5304. When optional argument RADIO is non-nil, only find radio
  5305. targets."
  5306. (let ((re (if radio org-radio-target-regexp org-target-regexp)) rtn)
  5307. (save-excursion
  5308. (goto-char (point-min))
  5309. (while (re-search-forward re nil t)
  5310. ;; Make sure point is really within the object.
  5311. (backward-char)
  5312. (let ((obj (org-element-context)))
  5313. (when (memq (org-element-type obj) '(radio-target target))
  5314. (add-to-list 'rtn (downcase (org-element-property :value obj))))))
  5315. rtn)))
  5316. (defun org-make-target-link-regexp (targets)
  5317. "Make regular expression matching all strings in TARGETS.
  5318. The regular expression finds the targets also if there is a line break
  5319. between words."
  5320. (and targets
  5321. (concat
  5322. "\\<\\("
  5323. (mapconcat
  5324. (lambda (x)
  5325. (setq x (regexp-quote x))
  5326. (while (string-match " +" x)
  5327. (setq x (replace-match "\\s-+" t t x)))
  5328. x)
  5329. targets
  5330. "\\|")
  5331. "\\)\\>")))
  5332. (defun org-activate-tags (limit)
  5333. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t)
  5334. (progn
  5335. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5336. (add-text-properties (match-beginning 1) (match-end 1)
  5337. (list 'mouse-face 'highlight
  5338. 'keymap org-mouse-map))
  5339. (org-rear-nonsticky-at (match-end 1))
  5340. t)))
  5341. (defun org-outline-level ()
  5342. "Compute the outline level of the heading at point.
  5343. If this is called at a normal headline, the level is the number of stars.
  5344. Use `org-reduced-level' to remove the effect of `org-odd-levels'."
  5345. (save-excursion
  5346. (if (not (condition-case nil
  5347. (org-back-to-heading t)
  5348. (error nil)))
  5349. 0
  5350. (looking-at org-outline-regexp)
  5351. (1- (- (match-end 0) (match-beginning 0))))))
  5352. (defvar org-font-lock-keywords nil)
  5353. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\+?\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  5354. "Regular expression matching a property line.")
  5355. (defvar org-font-lock-hook nil
  5356. "Functions to be called for special font lock stuff.")
  5357. (defvar org-font-lock-set-keywords-hook nil
  5358. "Functions that can manipulate `org-font-lock-extra-keywords'.
  5359. This is called after `org-font-lock-extra-keywords' is defined, but before
  5360. it is installed to be used by font lock. This can be useful if something
  5361. needs to be inserted at a specific position in the font-lock sequence.")
  5362. (defun org-font-lock-hook (limit)
  5363. "Run `org-font-lock-hook' within LIMIT."
  5364. (run-hook-with-args 'org-font-lock-hook limit))
  5365. (defun org-set-font-lock-defaults ()
  5366. "Set font lock defaults for the current buffer."
  5367. (let* ((em org-fontify-emphasized-text)
  5368. (lk org-activate-links)
  5369. (org-font-lock-extra-keywords
  5370. (list
  5371. ;; Call the hook
  5372. '(org-font-lock-hook)
  5373. ;; Headlines
  5374. `(,(if org-fontify-whole-heading-line
  5375. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  5376. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  5377. (1 (org-get-level-face 1))
  5378. (2 (org-get-level-face 2))
  5379. (3 (org-get-level-face 3)))
  5380. ;; Table lines
  5381. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  5382. (1 'org-table t))
  5383. ;; Table internals
  5384. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  5385. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  5386. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  5387. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  5388. ;; Drawers
  5389. (list org-drawer-regexp '(0 'org-special-keyword t))
  5390. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  5391. ;; Properties
  5392. (list org-property-re
  5393. '(1 'org-special-keyword t)
  5394. '(3 'org-property-value t))
  5395. ;; Links
  5396. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  5397. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  5398. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  5399. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  5400. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  5401. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  5402. (if (memq 'footnote lk) '(org-activate-footnote-links))
  5403. ;; Targets.
  5404. (list org-any-target-regexp '(0 'org-target t))
  5405. ;; Diary sexps.
  5406. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  5407. ;; Macro
  5408. '("{{{.+}}}" (0 'org-macro t))
  5409. '(org-hide-wide-columns (0 nil append))
  5410. ;; TODO keyword
  5411. (list (format org-heading-keyword-regexp-format
  5412. org-todo-regexp)
  5413. '(2 (org-get-todo-face 2) t))
  5414. ;; DONE
  5415. (if org-fontify-done-headline
  5416. (list (format org-heading-keyword-regexp-format
  5417. (concat
  5418. "\\(?:"
  5419. (mapconcat 'regexp-quote org-done-keywords "\\|")
  5420. "\\)"))
  5421. '(2 'org-headline-done t))
  5422. nil)
  5423. ;; Priorities
  5424. '(org-font-lock-add-priority-faces)
  5425. ;; Tags
  5426. '(org-font-lock-add-tag-faces)
  5427. ;; Special keywords
  5428. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  5429. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  5430. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  5431. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  5432. ;; Emphasis
  5433. (if em
  5434. (if (featurep 'xemacs)
  5435. '(org-do-emphasis-faces (0 nil append))
  5436. '(org-do-emphasis-faces)))
  5437. ;; Checkboxes
  5438. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  5439. 1 'org-checkbox prepend)
  5440. (if (cdr (assq 'checkbox org-list-automatic-rules))
  5441. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5442. (0 (org-get-checkbox-statistics-face) t)))
  5443. ;; Description list items
  5444. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  5445. 1 'org-list-dt prepend)
  5446. ;; ARCHIVEd headings
  5447. (list (concat
  5448. org-outline-regexp-bol
  5449. "\\(.*:" org-archive-tag ":.*\\)")
  5450. '(1 'org-archived prepend))
  5451. ;; Specials
  5452. '(org-do-latex-and-related)
  5453. '(org-fontify-entities)
  5454. '(org-raise-scripts)
  5455. ;; Code
  5456. '(org-activate-code (1 'org-code t))
  5457. ;; COMMENT
  5458. (list (format org-heading-keyword-regexp-format
  5459. (concat "\\("
  5460. org-comment-string "\\|" org-quote-string
  5461. "\\)"))
  5462. '(2 'org-special-keyword t))
  5463. ;; Blocks and meta lines
  5464. '(org-fontify-meta-lines-and-blocks)
  5465. )))
  5466. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5467. (run-hooks 'org-font-lock-set-keywords-hook)
  5468. ;; Now set the full font-lock-keywords
  5469. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  5470. (org-set-local 'font-lock-defaults
  5471. '(org-font-lock-keywords t nil nil backward-paragraph))
  5472. (kill-local-variable 'font-lock-keywords) nil))
  5473. (defun org-toggle-pretty-entities ()
  5474. "Toggle the composition display of entities as UTF8 characters."
  5475. (interactive)
  5476. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  5477. (org-restart-font-lock)
  5478. (if org-pretty-entities
  5479. (message "Entities are displayed as UTF8 characters")
  5480. (save-restriction
  5481. (widen)
  5482. (org-decompose-region (point-min) (point-max))
  5483. (message "Entities are displayed plain"))))
  5484. (defvar org-custom-properties-overlays nil
  5485. "List of overlays used for custom properties.")
  5486. (make-variable-buffer-local 'org-custom-properties-overlays)
  5487. (defun org-toggle-custom-properties-visibility ()
  5488. "Display or hide properties in `org-custom-properties'."
  5489. (interactive)
  5490. (if org-custom-properties-overlays
  5491. (progn (mapc 'delete-overlay org-custom-properties-overlays)
  5492. (setq org-custom-properties-overlays nil))
  5493. (unless (not org-custom-properties)
  5494. (save-excursion
  5495. (save-restriction
  5496. (widen)
  5497. (goto-char (point-min))
  5498. (while (re-search-forward org-property-re nil t)
  5499. (mapc (lambda(p)
  5500. (when (equal p (substring (match-string 1) 1 -1))
  5501. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5502. (overlay-put o 'invisible t)
  5503. (overlay-put o 'org-custom-property t)
  5504. (push o org-custom-properties-overlays))))
  5505. org-custom-properties)))))))
  5506. (defun org-fontify-entities (limit)
  5507. "Find an entity to fontify."
  5508. (let (ee)
  5509. (when org-pretty-entities
  5510. (catch 'match
  5511. (while (re-search-forward
  5512. "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]\n]\\)"
  5513. limit t)
  5514. (if (and (not (org-in-indented-comment-line))
  5515. (setq ee (org-entity-get (match-string 1)))
  5516. (= (length (nth 6 ee)) 1))
  5517. (let*
  5518. ((end (if (equal (match-string 2) "{}")
  5519. (match-end 2)
  5520. (match-end 1))))
  5521. (add-text-properties
  5522. (match-beginning 0) end
  5523. (list 'font-lock-fontified t))
  5524. (compose-region (match-beginning 0) end
  5525. (nth 6 ee) nil)
  5526. (backward-char 1)
  5527. (throw 'match t))))
  5528. nil))))
  5529. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5530. "Fontify string S like in Org-mode."
  5531. (with-temp-buffer
  5532. (insert s)
  5533. (let ((org-odd-levels-only odd-levels))
  5534. (org-mode)
  5535. (font-lock-fontify-buffer)
  5536. (buffer-string))))
  5537. (defvar org-m nil)
  5538. (defvar org-l nil)
  5539. (defvar org-f nil)
  5540. (defun org-get-level-face (n)
  5541. "Get the right face for match N in font-lock matching of headlines."
  5542. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5543. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5544. (if org-cycle-level-faces
  5545. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5546. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5547. (cond
  5548. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5549. ((eq n 2) org-f)
  5550. (t (if org-level-color-stars-only nil org-f))))
  5551. (defun org-get-todo-face (kwd)
  5552. "Get the right face for a TODO keyword KWD.
  5553. If KWD is a number, get the corresponding match group."
  5554. (if (numberp kwd) (setq kwd (match-string kwd)))
  5555. (or (org-face-from-face-or-color
  5556. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5557. (and (member kwd org-done-keywords) 'org-done)
  5558. 'org-todo))
  5559. (defun org-face-from-face-or-color (context inherit face-or-color)
  5560. "Create a face list that inherits INHERIT, but sets the foreground color.
  5561. When FACE-OR-COLOR is not a string, just return it."
  5562. (if (stringp face-or-color)
  5563. (list :inherit inherit
  5564. (cdr (assoc context org-faces-easy-properties))
  5565. face-or-color)
  5566. face-or-color))
  5567. (defun org-font-lock-add-tag-faces (limit)
  5568. "Add the special tag faces."
  5569. (when (and org-tag-faces org-tags-special-faces-re)
  5570. (while (re-search-forward org-tags-special-faces-re limit t)
  5571. (add-text-properties (match-beginning 1) (match-end 1)
  5572. (list 'face (org-get-tag-face 1)
  5573. 'font-lock-fontified t))
  5574. (backward-char 1))))
  5575. (defun org-font-lock-add-priority-faces (limit)
  5576. "Add the special priority faces."
  5577. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  5578. (when (save-match-data (org-at-heading-p))
  5579. (add-text-properties
  5580. (match-beginning 0) (match-end 0)
  5581. (list 'face (or (org-face-from-face-or-color
  5582. 'priority 'org-priority
  5583. (cdr (assoc (char-after (match-beginning 1))
  5584. org-priority-faces)))
  5585. 'org-priority)
  5586. 'font-lock-fontified t)))))
  5587. (defun org-get-tag-face (kwd)
  5588. "Get the right face for a TODO keyword KWD.
  5589. If KWD is a number, get the corresponding match group."
  5590. (if (numberp kwd) (setq kwd (match-string kwd)))
  5591. (or (org-face-from-face-or-color
  5592. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  5593. 'org-tag))
  5594. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5595. "Remove fontification and activation overlays from links."
  5596. (font-lock-default-unfontify-region beg end)
  5597. (let* ((buffer-undo-list t)
  5598. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5599. (inhibit-modification-hooks t)
  5600. deactivate-mark buffer-file-name buffer-file-truename)
  5601. (org-decompose-region beg end)
  5602. (remove-text-properties beg end
  5603. '(mouse-face t keymap t org-linked-text t
  5604. invisible t intangible t
  5605. org-no-flyspell t org-emphasis t))
  5606. (org-remove-font-lock-display-properties beg end)))
  5607. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5608. ((raise 0.3) (height 0.7))
  5609. ((raise -0.5))
  5610. ((raise 0.5)))
  5611. "Display properties for showing superscripts and subscripts.")
  5612. (defun org-remove-font-lock-display-properties (beg end)
  5613. "Remove specific display properties that have been added by font lock.
  5614. The will remove the raise properties that are used to show superscripts
  5615. and subscripts."
  5616. (let (next prop)
  5617. (while (< beg end)
  5618. (setq next (next-single-property-change beg 'display nil end)
  5619. prop (get-text-property beg 'display))
  5620. (if (member prop org-script-display)
  5621. (put-text-property beg next 'display nil))
  5622. (setq beg next))))
  5623. (defun org-raise-scripts (limit)
  5624. "Add raise properties to sub/superscripts."
  5625. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts)
  5626. (if (re-search-forward
  5627. (if (eq org-use-sub-superscripts t)
  5628. org-match-substring-regexp
  5629. org-match-substring-with-braces-regexp)
  5630. limit t)
  5631. (let* ((pos (point)) table-p comment-p
  5632. (mpos (match-beginning 3))
  5633. (emph-p (get-text-property mpos 'org-emphasis))
  5634. (link-p (get-text-property mpos 'mouse-face))
  5635. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5636. (goto-char (point-at-bol))
  5637. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5638. comment-p (org-looking-at-p "[ \t]*#"))
  5639. (goto-char pos)
  5640. ;; FIXME: Should we go back one character here, for a_b^c
  5641. ;; (goto-char (1- pos)) ;????????????????????
  5642. (if (or comment-p emph-p link-p keyw-p)
  5643. t
  5644. (put-text-property (match-beginning 3) (match-end 0)
  5645. 'display
  5646. (if (equal (char-after (match-beginning 2)) ?^)
  5647. (nth (if table-p 3 1) org-script-display)
  5648. (nth (if table-p 2 0) org-script-display)))
  5649. (add-text-properties (match-beginning 2) (match-end 2)
  5650. (list 'invisible t
  5651. 'org-dwidth t 'org-dwidth-n 1))
  5652. (if (and (eq (char-after (match-beginning 3)) ?{)
  5653. (eq (char-before (match-end 3)) ?}))
  5654. (progn
  5655. (add-text-properties
  5656. (match-beginning 3) (1+ (match-beginning 3))
  5657. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5658. (add-text-properties
  5659. (1- (match-end 3)) (match-end 3)
  5660. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))))
  5661. t)))))
  5662. ;;;; Visibility cycling, including org-goto and indirect buffer
  5663. ;;; Cycling
  5664. (defvar org-cycle-global-status nil)
  5665. (make-variable-buffer-local 'org-cycle-global-status)
  5666. (put 'org-cycle-global-status 'org-state t)
  5667. (defvar org-cycle-subtree-status nil)
  5668. (make-variable-buffer-local 'org-cycle-subtree-status)
  5669. (put 'org-cycle-subtree-status 'org-state t)
  5670. (defvar org-inlinetask-min-level)
  5671. ;;;###autoload
  5672. (defun org-cycle (&optional arg)
  5673. "TAB-action and visibility cycling for Org-mode.
  5674. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5675. is outline visibility cycling, but it also invokes other actions
  5676. in special contexts.
  5677. - When this function is called with a prefix argument, rotate the entire
  5678. buffer through 3 states (global cycling)
  5679. 1. OVERVIEW: Show only top-level headlines.
  5680. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5681. 3. SHOW ALL: Show everything.
  5682. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  5683. determined by the variable `org-startup-folded', and by any VISIBILITY
  5684. properties in the buffer.
  5685. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  5686. including any drawers.
  5687. - When inside a table, re-align the table and move to the next field.
  5688. - When point is at the beginning of a headline, rotate the subtree started
  5689. by this line through 3 different states (local cycling)
  5690. 1. FOLDED: Only the main headline is shown.
  5691. 2. CHILDREN: The main headline and the direct children are shown.
  5692. From this state, you can move to one of the children
  5693. and zoom in further.
  5694. 3. SUBTREE: Show the entire subtree, including body text.
  5695. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5696. - When point is at the beginning of an empty headline and the variable
  5697. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5698. of the headline by demoting and promoting it to likely levels. This
  5699. speeds up creation document structure by pressing TAB once or several
  5700. times right after creating a new headline.
  5701. - When there is a numeric prefix, go up to a heading with level ARG, do
  5702. a `show-subtree' and return to the previous cursor position. If ARG
  5703. is negative, go up that many levels.
  5704. - When point is not at the beginning of a headline, execute the global
  5705. binding for TAB, which is re-indenting the line. See the option
  5706. `org-cycle-emulate-tab' for details.
  5707. - Special case: if point is at the beginning of the buffer and there is
  5708. no headline in line 1, this function will act as if called with prefix arg
  5709. (C-u TAB, same as S-TAB) also when called without prefix arg.
  5710. But only if also the variable `org-cycle-global-at-bob' is t."
  5711. (interactive "P")
  5712. (org-load-modules-maybe)
  5713. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5714. (and org-cycle-level-after-item/entry-creation
  5715. (or (org-cycle-level)
  5716. (org-cycle-item-indentation))))
  5717. (let* (message-log-max ; Don't populate the *Messages* buffer
  5718. (limit-level
  5719. (or org-cycle-max-level
  5720. (and (boundp 'org-inlinetask-min-level)
  5721. org-inlinetask-min-level
  5722. (1- org-inlinetask-min-level))))
  5723. (nstars (and limit-level
  5724. (if org-odd-levels-only
  5725. (and limit-level (1- (* limit-level 2)))
  5726. limit-level)))
  5727. (org-outline-regexp
  5728. (if (not (derived-mode-p 'org-mode))
  5729. outline-regexp
  5730. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
  5731. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5732. (not (looking-at org-outline-regexp))))
  5733. (org-cycle-hook
  5734. (if bob-special
  5735. (delq 'org-optimize-window-after-visibility-change
  5736. (copy-sequence org-cycle-hook))
  5737. org-cycle-hook))
  5738. (pos (point)))
  5739. (if (or bob-special (equal arg '(4)))
  5740. ;; special case: use global cycling
  5741. (setq arg t))
  5742. (cond
  5743. ((equal arg '(16))
  5744. (setq last-command 'dummy)
  5745. (org-set-startup-visibility)
  5746. (message "Startup visibility, plus VISIBILITY properties"))
  5747. ((equal arg '(64))
  5748. (show-all)
  5749. (message "Entire buffer visible, including drawers"))
  5750. ;; Table: enter it or move to the next field.
  5751. ((org-at-table-p 'any)
  5752. (if (org-at-table.el-p)
  5753. (message "Use C-c ' to edit table.el tables")
  5754. (if arg (org-table-edit-field t)
  5755. (org-table-justify-field-maybe)
  5756. (call-interactively 'org-table-next-field))))
  5757. ((run-hook-with-args-until-success
  5758. 'org-tab-after-check-for-table-hook))
  5759. ;; Global cycling: delegate to `org-cycle-internal-global'.
  5760. ((eq arg t) (org-cycle-internal-global))
  5761. ;; Drawers: delegate to `org-flag-drawer'.
  5762. ((and org-drawers org-drawer-regexp
  5763. (save-excursion
  5764. (beginning-of-line 1)
  5765. (looking-at org-drawer-regexp)))
  5766. (org-flag-drawer ; toggle block visibility
  5767. (not (get-char-property (match-end 0) 'invisible))))
  5768. ;; Show-subtree, ARG levels up from here.
  5769. ((integerp arg)
  5770. (save-excursion
  5771. (org-back-to-heading)
  5772. (outline-up-heading (if (< arg 0) (- arg)
  5773. (- (funcall outline-level) arg)))
  5774. (org-show-subtree)))
  5775. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  5776. ((and (featurep 'org-inlinetask)
  5777. (org-inlinetask-at-task-p)
  5778. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5779. (org-inlinetask-toggle-visibility))
  5780. ((org-try-cdlatex-tab))
  5781. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  5782. ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
  5783. (save-excursion (beginning-of-line 1)
  5784. (looking-at org-outline-regexp)))
  5785. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5786. (org-cycle-internal-local))
  5787. ;; From there: TAB emulation and template completion.
  5788. (buffer-read-only (org-back-to-heading))
  5789. ((run-hook-with-args-until-success
  5790. 'org-tab-after-check-for-cycling-hook))
  5791. ((org-try-structure-completion))
  5792. ((run-hook-with-args-until-success
  5793. 'org-tab-before-tab-emulation-hook))
  5794. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5795. (or (not (bolp))
  5796. (not (looking-at org-outline-regexp))))
  5797. (call-interactively (global-key-binding "\t")))
  5798. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5799. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5800. (or (and (eq org-cycle-emulate-tab 'white)
  5801. (= (match-end 0) (point-at-eol)))
  5802. (and (eq org-cycle-emulate-tab 'whitestart)
  5803. (>= (match-end 0) pos))))
  5804. t
  5805. (eq org-cycle-emulate-tab t))
  5806. (call-interactively (global-key-binding "\t")))
  5807. (t (save-excursion
  5808. (org-back-to-heading)
  5809. (org-cycle)))))))
  5810. (defun org-cycle-internal-global ()
  5811. "Do the global cycling action."
  5812. ;; Hack to avoid display of messages for .org attachments in Gnus
  5813. (let (message-log-max ; Don't populate the *Messages* buffer
  5814. (ga (string-match "\\*fontification" (buffer-name))))
  5815. (cond
  5816. ((and (eq last-command this-command)
  5817. (eq org-cycle-global-status 'overview))
  5818. ;; We just created the overview - now do table of contents
  5819. ;; This can be slow in very large buffers, so indicate action
  5820. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  5821. (unless ga (message "CONTENTS..."))
  5822. (org-content)
  5823. (unless ga (message "CONTENTS...done"))
  5824. (setq org-cycle-global-status 'contents)
  5825. (run-hook-with-args 'org-cycle-hook 'contents))
  5826. ((and (eq last-command this-command)
  5827. (eq org-cycle-global-status 'contents))
  5828. ;; We just showed the table of contents - now show everything
  5829. (run-hook-with-args 'org-pre-cycle-hook 'all)
  5830. (show-all)
  5831. (unless ga (message "SHOW ALL"))
  5832. (setq org-cycle-global-status 'all)
  5833. (run-hook-with-args 'org-cycle-hook 'all))
  5834. (t
  5835. ;; Default action: go to overview
  5836. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  5837. (org-overview)
  5838. (unless ga (message "OVERVIEW"))
  5839. (setq org-cycle-global-status 'overview)
  5840. (run-hook-with-args 'org-cycle-hook 'overview)))))
  5841. (defun org-cycle-internal-local ()
  5842. "Do the local cycling action."
  5843. (let (message-log-max ; Don't populate the *Messages* buffer
  5844. (goal-column 0) eoh eol eos has-children children-skipped struct)
  5845. ;; First, determine end of headline (EOH), end of subtree or item
  5846. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  5847. (save-excursion
  5848. (if (org-at-item-p)
  5849. (progn
  5850. (beginning-of-line)
  5851. (setq struct (org-list-struct))
  5852. (setq eoh (point-at-eol))
  5853. (setq eos (org-list-get-item-end-before-blank (point) struct))
  5854. (setq has-children (org-list-has-child-p (point) struct)))
  5855. (org-back-to-heading)
  5856. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  5857. (setq eos (save-excursion (1- (org-end-of-subtree t t))))
  5858. (setq has-children
  5859. (or (save-excursion
  5860. (let ((level (funcall outline-level)))
  5861. (outline-next-heading)
  5862. (and (org-at-heading-p t)
  5863. (> (funcall outline-level) level))))
  5864. (save-excursion
  5865. (org-list-search-forward (org-item-beginning-re) eos t)))))
  5866. ;; Determine end invisible part of buffer (EOL)
  5867. (beginning-of-line 2)
  5868. ;; XEmacs doesn't have `next-single-char-property-change'
  5869. (if (featurep 'xemacs)
  5870. (while (and (not (eobp)) ;; this is like `next-line'
  5871. (get-char-property (1- (point)) 'invisible))
  5872. (beginning-of-line 2))
  5873. (while (and (not (eobp)) ;; this is like `next-line'
  5874. (get-char-property (1- (point)) 'invisible))
  5875. (goto-char (next-single-char-property-change (point) 'invisible))
  5876. (and (eolp) (beginning-of-line 2))))
  5877. (setq eol (point)))
  5878. ;; Find out what to do next and set `this-command'
  5879. (cond
  5880. ((= eos eoh)
  5881. ;; Nothing is hidden behind this heading
  5882. (unless (org-before-first-heading-p)
  5883. (run-hook-with-args 'org-pre-cycle-hook 'empty))
  5884. (message "EMPTY ENTRY")
  5885. (setq org-cycle-subtree-status nil)
  5886. (save-excursion
  5887. (goto-char eos)
  5888. (outline-next-heading)
  5889. (if (outline-invisible-p) (org-flag-heading nil))))
  5890. ((and (or (>= eol eos)
  5891. (not (string-match "\\S-" (buffer-substring eol eos))))
  5892. (or has-children
  5893. (not (setq children-skipped
  5894. org-cycle-skip-children-state-if-no-children))))
  5895. ;; Entire subtree is hidden in one line: children view
  5896. (unless (org-before-first-heading-p)
  5897. (run-hook-with-args 'org-pre-cycle-hook 'children))
  5898. (if (org-at-item-p)
  5899. (org-list-set-item-visibility (point-at-bol) struct 'children)
  5900. (org-show-entry)
  5901. (org-with-limited-levels (show-children))
  5902. ;; FIXME: This slows down the func way too much.
  5903. ;; How keep drawers hidden in subtree anyway?
  5904. ;; (when (memq 'org-cycle-hide-drawers org-cycle-hook)
  5905. ;; (org-cycle-hide-drawers 'subtree))
  5906. ;; Fold every list in subtree to top-level items.
  5907. (when (eq org-cycle-include-plain-lists 'integrate)
  5908. (save-excursion
  5909. (org-back-to-heading)
  5910. (while (org-list-search-forward (org-item-beginning-re) eos t)
  5911. (beginning-of-line 1)
  5912. (let* ((struct (org-list-struct))
  5913. (prevs (org-list-prevs-alist struct))
  5914. (end (org-list-get-bottom-point struct)))
  5915. (mapc (lambda (e) (org-list-set-item-visibility e struct 'folded))
  5916. (org-list-get-all-items (point) struct prevs))
  5917. (goto-char end))))))
  5918. (message "CHILDREN")
  5919. (save-excursion
  5920. (goto-char eos)
  5921. (outline-next-heading)
  5922. (if (outline-invisible-p) (org-flag-heading nil)))
  5923. (setq org-cycle-subtree-status 'children)
  5924. (unless (org-before-first-heading-p)
  5925. (run-hook-with-args 'org-cycle-hook 'children)))
  5926. ((or children-skipped
  5927. (and (eq last-command this-command)
  5928. (eq org-cycle-subtree-status 'children)))
  5929. ;; We just showed the children, or no children are there,
  5930. ;; now show everything.
  5931. (unless (org-before-first-heading-p)
  5932. (run-hook-with-args 'org-pre-cycle-hook 'subtree))
  5933. (outline-flag-region eoh eos nil)
  5934. (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  5935. (setq org-cycle-subtree-status 'subtree)
  5936. (unless (org-before-first-heading-p)
  5937. (run-hook-with-args 'org-cycle-hook 'subtree)))
  5938. (t
  5939. ;; Default action: hide the subtree.
  5940. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  5941. (outline-flag-region eoh eos t)
  5942. (message "FOLDED")
  5943. (setq org-cycle-subtree-status 'folded)
  5944. (unless (org-before-first-heading-p)
  5945. (run-hook-with-args 'org-cycle-hook 'folded))))))
  5946. ;;;###autoload
  5947. (defun org-global-cycle (&optional arg)
  5948. "Cycle the global visibility. For details see `org-cycle'.
  5949. With \\[universal-argument] prefix arg, switch to startup visibility.
  5950. With a numeric prefix, show all headlines up to that level."
  5951. (interactive "P")
  5952. (let ((org-cycle-include-plain-lists
  5953. (if (derived-mode-p 'org-mode) org-cycle-include-plain-lists nil)))
  5954. (cond
  5955. ((integerp arg)
  5956. (show-all)
  5957. (hide-sublevels arg)
  5958. (setq org-cycle-global-status 'contents))
  5959. ((equal arg '(4))
  5960. (org-set-startup-visibility)
  5961. (message "Startup visibility, plus VISIBILITY properties."))
  5962. (t
  5963. (org-cycle '(4))))))
  5964. (defun org-set-startup-visibility ()
  5965. "Set the visibility required by startup options and properties."
  5966. (cond
  5967. ((eq org-startup-folded t)
  5968. (org-cycle '(4)))
  5969. ((eq org-startup-folded 'content)
  5970. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  5971. (org-cycle '(4)) (org-cycle '(4)))))
  5972. (unless (eq org-startup-folded 'showeverything)
  5973. (if org-hide-block-startup (org-hide-block-all))
  5974. (org-set-visibility-according-to-property 'no-cleanup)
  5975. (org-cycle-hide-archived-subtrees 'all)
  5976. (org-cycle-hide-drawers 'all)
  5977. (org-cycle-show-empty-lines t)))
  5978. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  5979. "Switch subtree visibilities according to :VISIBILITY: property."
  5980. (interactive)
  5981. (let (org-show-entry-below state)
  5982. (save-excursion
  5983. (goto-char (point-min))
  5984. (while (re-search-forward
  5985. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  5986. nil t)
  5987. (setq state (match-string 1))
  5988. (save-excursion
  5989. (org-back-to-heading t)
  5990. (hide-subtree)
  5991. (org-reveal)
  5992. (cond
  5993. ((equal state '("fold" "folded"))
  5994. (hide-subtree))
  5995. ((equal state "children")
  5996. (org-show-hidden-entry)
  5997. (show-children))
  5998. ((equal state "content")
  5999. (save-excursion
  6000. (save-restriction
  6001. (org-narrow-to-subtree)
  6002. (org-content))))
  6003. ((member state '("all" "showall"))
  6004. (show-subtree)))))
  6005. (unless no-cleanup
  6006. (org-cycle-hide-archived-subtrees 'all)
  6007. (org-cycle-hide-drawers 'all)
  6008. (org-cycle-show-empty-lines 'all)))))
  6009. ;; This function uses outline-regexp instead of the more fundamental
  6010. ;; org-outline-regexp so that org-cycle-global works outside of Org
  6011. ;; buffers, where outline-regexp is needed.
  6012. (defun org-overview ()
  6013. "Switch to overview mode, showing only top-level headlines.
  6014. Really, this shows all headlines with level equal or greater than the level
  6015. of the first headline in the buffer. This is important, because if the
  6016. first headline is not level one, then (hide-sublevels 1) gives confusing
  6017. results."
  6018. (interactive)
  6019. (let ((l (org-current-line))
  6020. (level (save-excursion
  6021. (goto-char (point-min))
  6022. (if (re-search-forward (concat "^" outline-regexp) nil t)
  6023. (progn
  6024. (goto-char (match-beginning 0))
  6025. (funcall outline-level))))))
  6026. (and level (hide-sublevels level))
  6027. (recenter '(4))
  6028. (org-goto-line l)))
  6029. (defun org-content (&optional arg)
  6030. "Show all headlines in the buffer, like a table of contents.
  6031. With numerical argument N, show content up to level N."
  6032. (interactive "P")
  6033. (save-excursion
  6034. ;; Visit all headings and show their offspring
  6035. (and (integerp arg) (org-overview))
  6036. (goto-char (point-max))
  6037. (catch 'exit
  6038. (while (and (progn (condition-case nil
  6039. (outline-previous-visible-heading 1)
  6040. (error (goto-char (point-min))))
  6041. t)
  6042. (looking-at org-outline-regexp))
  6043. (if (integerp arg)
  6044. (show-children (1- arg))
  6045. (show-branches))
  6046. (if (bobp) (throw 'exit nil))))))
  6047. (defun org-optimize-window-after-visibility-change (state)
  6048. "Adjust the window after a change in outline visibility.
  6049. This function is the default value of the hook `org-cycle-hook'."
  6050. (when (get-buffer-window (current-buffer))
  6051. (cond
  6052. ((eq state 'content) nil)
  6053. ((eq state 'all) nil)
  6054. ((eq state 'folded) nil)
  6055. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  6056. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  6057. (defun org-remove-empty-overlays-at (pos)
  6058. "Remove outline overlays that do not contain non-white stuff."
  6059. (mapc
  6060. (lambda (o)
  6061. (and (eq 'outline (overlay-get o 'invisible))
  6062. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  6063. (overlay-end o))))
  6064. (delete-overlay o)))
  6065. (overlays-at pos)))
  6066. (defun org-clean-visibility-after-subtree-move ()
  6067. "Fix visibility issues after moving a subtree."
  6068. ;; First, find a reasonable region to look at:
  6069. ;; Start two siblings above, end three below
  6070. (let* ((beg (save-excursion
  6071. (and (org-get-last-sibling)
  6072. (org-get-last-sibling))
  6073. (point)))
  6074. (end (save-excursion
  6075. (and (org-get-next-sibling)
  6076. (org-get-next-sibling)
  6077. (org-get-next-sibling))
  6078. (if (org-at-heading-p)
  6079. (point-at-eol)
  6080. (point))))
  6081. (level (looking-at "\\*+"))
  6082. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  6083. (save-excursion
  6084. (save-restriction
  6085. (narrow-to-region beg end)
  6086. (when re
  6087. ;; Properly fold already folded siblings
  6088. (goto-char (point-min))
  6089. (while (re-search-forward re nil t)
  6090. (if (and (not (outline-invisible-p))
  6091. (save-excursion
  6092. (goto-char (point-at-eol)) (outline-invisible-p)))
  6093. (hide-entry))))
  6094. (org-cycle-show-empty-lines 'overview)
  6095. (org-cycle-hide-drawers 'overview)))))
  6096. (defun org-cycle-show-empty-lines (state)
  6097. "Show empty lines above all visible headlines.
  6098. The region to be covered depends on STATE when called through
  6099. `org-cycle-hook'. Lisp program can use t for STATE to get the
  6100. entire buffer covered. Note that an empty line is only shown if there
  6101. are at least `org-cycle-separator-lines' empty lines before the headline."
  6102. (when (not (= org-cycle-separator-lines 0))
  6103. (save-excursion
  6104. (let* ((n (abs org-cycle-separator-lines))
  6105. (re (cond
  6106. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  6107. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  6108. (t (let ((ns (number-to-string (- n 2))))
  6109. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  6110. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  6111. beg end b e)
  6112. (cond
  6113. ((memq state '(overview contents t))
  6114. (setq beg (point-min) end (point-max)))
  6115. ((memq state '(children folded))
  6116. (setq beg (point) end (progn (org-end-of-subtree t t)
  6117. (beginning-of-line 2)
  6118. (point)))))
  6119. (when beg
  6120. (goto-char beg)
  6121. (while (re-search-forward re end t)
  6122. (unless (get-char-property (match-end 1) 'invisible)
  6123. (setq e (match-end 1))
  6124. (if (< org-cycle-separator-lines 0)
  6125. (setq b (save-excursion
  6126. (goto-char (match-beginning 0))
  6127. (org-back-over-empty-lines)
  6128. (if (save-excursion
  6129. (goto-char (max (point-min) (1- (point))))
  6130. (org-at-heading-p))
  6131. (1- (point))
  6132. (point))))
  6133. (setq b (match-beginning 1)))
  6134. (outline-flag-region b e nil)))))))
  6135. ;; Never hide empty lines at the end of the file.
  6136. (save-excursion
  6137. (goto-char (point-max))
  6138. (outline-previous-heading)
  6139. (outline-end-of-heading)
  6140. (if (and (looking-at "[ \t\n]+")
  6141. (= (match-end 0) (point-max)))
  6142. (outline-flag-region (point) (match-end 0) nil))))
  6143. (defun org-show-empty-lines-in-parent ()
  6144. "Move to the parent and re-show empty lines before visible headlines."
  6145. (save-excursion
  6146. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  6147. (org-cycle-show-empty-lines context))))
  6148. (defun org-files-list ()
  6149. "Return `org-agenda-files' list, plus all open org-mode files.
  6150. This is useful for operations that need to scan all of a user's
  6151. open and agenda-wise Org files."
  6152. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  6153. (dolist (buf (buffer-list))
  6154. (with-current-buffer buf
  6155. (if (and (derived-mode-p 'org-mode) (buffer-file-name))
  6156. (let ((file (expand-file-name (buffer-file-name))))
  6157. (unless (member file files)
  6158. (push file files))))))
  6159. files))
  6160. (defsubst org-entry-beginning-position ()
  6161. "Return the beginning position of the current entry."
  6162. (save-excursion (outline-back-to-heading t) (point)))
  6163. (defsubst org-entry-end-position ()
  6164. "Return the end position of the current entry."
  6165. (save-excursion (outline-next-heading) (point)))
  6166. (defun org-cycle-hide-drawers (state)
  6167. "Re-hide all drawers after a visibility state change."
  6168. (when (and (derived-mode-p 'org-mode)
  6169. (not (memq state '(overview folded contents))))
  6170. (save-excursion
  6171. (let* ((globalp (memq state '(contents all)))
  6172. (beg (if globalp (point-min) (point)))
  6173. (end (if globalp (point-max)
  6174. (if (eq state 'children)
  6175. (save-excursion (outline-next-heading) (point))
  6176. (org-end-of-subtree t)))))
  6177. (goto-char beg)
  6178. (while (re-search-forward org-drawer-regexp end t)
  6179. (org-flag-drawer t))))))
  6180. (defun org-cycle-hide-inline-tasks (state)
  6181. "Re-hide inline task when switching to 'contents visibility state."
  6182. (when (and (eq state 'contents)
  6183. (boundp 'org-inlinetask-min-level)
  6184. org-inlinetask-min-level)
  6185. (hide-sublevels (1- org-inlinetask-min-level))))
  6186. (defun org-flag-drawer (flag)
  6187. "When FLAG is non-nil, hide the drawer we are within.
  6188. Otherwise make it visible."
  6189. (save-excursion
  6190. (beginning-of-line 1)
  6191. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  6192. (let ((b (match-end 0)))
  6193. (if (re-search-forward
  6194. "^[ \t]*:END:"
  6195. (save-excursion (outline-next-heading) (point)) t)
  6196. (outline-flag-region b (point-at-eol) flag)
  6197. (error ":END: line missing at position %s" b))))))
  6198. (defun org-subtree-end-visible-p ()
  6199. "Is the end of the current subtree visible?"
  6200. (pos-visible-in-window-p
  6201. (save-excursion (org-end-of-subtree t) (point))))
  6202. (defun org-first-headline-recenter (&optional N)
  6203. "Move cursor to the first headline and recenter the headline.
  6204. Optional argument N means put the headline into the Nth line of the window."
  6205. (goto-char (point-min))
  6206. (when (re-search-forward (concat "^\\(" org-outline-regexp "\\)") nil t)
  6207. (beginning-of-line)
  6208. (recenter (prefix-numeric-value N))))
  6209. ;;; Saving and restoring visibility
  6210. (defun org-outline-overlay-data (&optional use-markers)
  6211. "Return a list of the locations of all outline overlays.
  6212. These are overlays with the `invisible' property value `outline'.
  6213. The return value is a list of cons cells, with start and stop
  6214. positions for each overlay.
  6215. If USE-MARKERS is set, return the positions as markers."
  6216. (let (beg end)
  6217. (save-excursion
  6218. (save-restriction
  6219. (widen)
  6220. (delq nil
  6221. (mapcar (lambda (o)
  6222. (when (eq (overlay-get o 'invisible) 'outline)
  6223. (setq beg (overlay-start o)
  6224. end (overlay-end o))
  6225. (and beg end (> end beg)
  6226. (if use-markers
  6227. (cons (move-marker (make-marker) beg)
  6228. (move-marker (make-marker) end))
  6229. (cons beg end)))))
  6230. (overlays-in (point-min) (point-max))))))))
  6231. (defun org-set-outline-overlay-data (data)
  6232. "Create visibility overlays for all positions in DATA.
  6233. DATA should have been made by `org-outline-overlay-data'."
  6234. (let (o)
  6235. (save-excursion
  6236. (save-restriction
  6237. (widen)
  6238. (show-all)
  6239. (mapc (lambda (c)
  6240. (outline-flag-region (car c) (cdr c) t))
  6241. data)))))
  6242. ;;; Folding of blocks
  6243. (defvar org-hide-block-overlays nil
  6244. "Overlays hiding blocks.")
  6245. (make-variable-buffer-local 'org-hide-block-overlays)
  6246. (defun org-block-map (function &optional start end)
  6247. "Call FUNCTION at the head of all source blocks in the current buffer.
  6248. Optional arguments START and END can be used to limit the range."
  6249. (let ((start (or start (point-min)))
  6250. (end (or end (point-max))))
  6251. (save-excursion
  6252. (goto-char start)
  6253. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  6254. (save-excursion
  6255. (save-match-data
  6256. (goto-char (match-beginning 0))
  6257. (funcall function)))))))
  6258. (defun org-hide-block-toggle-all ()
  6259. "Toggle the visibility of all blocks in the current buffer."
  6260. (org-block-map #'org-hide-block-toggle))
  6261. (defun org-hide-block-all ()
  6262. "Fold all blocks in the current buffer."
  6263. (interactive)
  6264. (org-show-block-all)
  6265. (org-block-map #'org-hide-block-toggle-maybe))
  6266. (defun org-show-block-all ()
  6267. "Unfold all blocks in the current buffer."
  6268. (interactive)
  6269. (mapc 'delete-overlay org-hide-block-overlays)
  6270. (setq org-hide-block-overlays nil))
  6271. (defun org-hide-block-toggle-maybe ()
  6272. "Toggle visibility of block at point."
  6273. (interactive)
  6274. (let ((case-fold-search t))
  6275. (if (save-excursion
  6276. (beginning-of-line 1)
  6277. (looking-at org-block-regexp))
  6278. (progn (org-hide-block-toggle)
  6279. t) ;; to signal that we took action
  6280. nil))) ;; to signal that we did not
  6281. (defun org-hide-block-toggle (&optional force)
  6282. "Toggle the visibility of the current block."
  6283. (interactive)
  6284. (save-excursion
  6285. (beginning-of-line)
  6286. (if (re-search-forward org-block-regexp nil t)
  6287. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  6288. (end (match-end 0)) ;; end of entire body
  6289. ov)
  6290. (if (memq t (mapcar (lambda (overlay)
  6291. (eq (overlay-get overlay 'invisible)
  6292. 'org-hide-block))
  6293. (overlays-at start)))
  6294. (if (or (not force) (eq force 'off))
  6295. (mapc (lambda (ov)
  6296. (when (member ov org-hide-block-overlays)
  6297. (setq org-hide-block-overlays
  6298. (delq ov org-hide-block-overlays)))
  6299. (when (eq (overlay-get ov 'invisible)
  6300. 'org-hide-block)
  6301. (delete-overlay ov)))
  6302. (overlays-at start)))
  6303. (setq ov (make-overlay start end))
  6304. (overlay-put ov 'invisible 'org-hide-block)
  6305. ;; make the block accessible to isearch
  6306. (overlay-put
  6307. ov 'isearch-open-invisible
  6308. (lambda (ov)
  6309. (when (member ov org-hide-block-overlays)
  6310. (setq org-hide-block-overlays
  6311. (delq ov org-hide-block-overlays)))
  6312. (when (eq (overlay-get ov 'invisible)
  6313. 'org-hide-block)
  6314. (delete-overlay ov))))
  6315. (push ov org-hide-block-overlays)))
  6316. (error "Not looking at a source block"))))
  6317. ;; org-tab-after-check-for-cycling-hook
  6318. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  6319. ;; Remove overlays when changing major mode
  6320. (add-hook 'org-mode-hook
  6321. (lambda () (org-add-hook 'change-major-mode-hook
  6322. 'org-show-block-all 'append 'local)))
  6323. ;;; Org-goto
  6324. (defvar org-goto-window-configuration nil)
  6325. (defvar org-goto-marker nil)
  6326. (defvar org-goto-map)
  6327. (defun org-goto-map ()
  6328. "Set the keymap `org-goto'."
  6329. (setq org-goto-map
  6330. (let ((map (make-sparse-keymap)))
  6331. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command
  6332. mouse-drag-region universal-argument org-occur))
  6333. cmd)
  6334. (while (setq cmd (pop cmds))
  6335. (substitute-key-definition cmd cmd map global-map)))
  6336. (suppress-keymap map)
  6337. (org-defkey map "\C-m" 'org-goto-ret)
  6338. (org-defkey map [(return)] 'org-goto-ret)
  6339. (org-defkey map [(left)] 'org-goto-left)
  6340. (org-defkey map [(right)] 'org-goto-right)
  6341. (org-defkey map [(control ?g)] 'org-goto-quit)
  6342. (org-defkey map "\C-i" 'org-cycle)
  6343. (org-defkey map [(tab)] 'org-cycle)
  6344. (org-defkey map [(down)] 'outline-next-visible-heading)
  6345. (org-defkey map [(up)] 'outline-previous-visible-heading)
  6346. (if org-goto-auto-isearch
  6347. (if (fboundp 'define-key-after)
  6348. (define-key-after map [t] 'org-goto-local-auto-isearch)
  6349. nil)
  6350. (org-defkey map "q" 'org-goto-quit)
  6351. (org-defkey map "n" 'outline-next-visible-heading)
  6352. (org-defkey map "p" 'outline-previous-visible-heading)
  6353. (org-defkey map "f" 'outline-forward-same-level)
  6354. (org-defkey map "b" 'outline-backward-same-level)
  6355. (org-defkey map "u" 'outline-up-heading))
  6356. (org-defkey map "/" 'org-occur)
  6357. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  6358. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  6359. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  6360. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  6361. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  6362. map)))
  6363. (defconst org-goto-help
  6364. "Browse buffer copy, to find location or copy text.%s
  6365. RET=jump to location C-g=quit and return to previous location
  6366. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  6367. (defvar org-goto-start-pos) ; dynamically scoped parameter
  6368. (defun org-goto (&optional alternative-interface)
  6369. "Look up a different location in the current file, keeping current visibility.
  6370. When you want look-up or go to a different location in a
  6371. document, the fastest way is often to fold the entire buffer and
  6372. then dive into the tree. This method has the disadvantage, that
  6373. the previous location will be folded, which may not be what you
  6374. want.
  6375. This command works around this by showing a copy of the current
  6376. buffer in an indirect buffer, in overview mode. You can dive
  6377. into the tree in that copy, use org-occur and incremental search
  6378. to find a location. When pressing RET or `Q', the command
  6379. returns to the original buffer in which the visibility is still
  6380. unchanged. After RET it will also jump to the location selected
  6381. in the indirect buffer and expose the headline hierarchy above.
  6382. With a prefix argument, use the alternative interface: e.g. if
  6383. `org-goto-interface' is 'outline use 'outline-path-completion."
  6384. (interactive "P")
  6385. (org-goto-map)
  6386. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  6387. (org-refile-use-outline-path t)
  6388. (org-refile-target-verify-function nil)
  6389. (interface
  6390. (if (not alternative-interface)
  6391. org-goto-interface
  6392. (if (eq org-goto-interface 'outline)
  6393. 'outline-path-completion
  6394. 'outline)))
  6395. (org-goto-start-pos (point))
  6396. (selected-point
  6397. (if (eq interface 'outline)
  6398. (car (org-get-location (current-buffer) org-goto-help))
  6399. (let ((pa (org-refile-get-location "Goto" nil nil t)))
  6400. (org-refile-check-position pa)
  6401. (nth 3 pa)))))
  6402. (if selected-point
  6403. (progn
  6404. (org-mark-ring-push org-goto-start-pos)
  6405. (goto-char selected-point)
  6406. (if (or (outline-invisible-p) (org-invisible-p2))
  6407. (org-show-context 'org-goto)))
  6408. (message "Quit"))))
  6409. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  6410. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  6411. (defvar org-goto-local-auto-isearch-map) ; defined below
  6412. (defun org-get-location (buf help)
  6413. "Let the user select a location in the Org-mode buffer BUF.
  6414. This function uses a recursive edit. It returns the selected position
  6415. or nil."
  6416. (org-no-popups
  6417. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  6418. (isearch-hide-immediately nil)
  6419. (isearch-search-fun-function
  6420. (lambda () 'org-goto-local-search-headings))
  6421. (org-goto-selected-point org-goto-exit-command))
  6422. (save-excursion
  6423. (save-window-excursion
  6424. (delete-other-windows)
  6425. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  6426. (org-pop-to-buffer-same-window
  6427. (condition-case nil
  6428. (make-indirect-buffer (current-buffer) "*org-goto*")
  6429. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  6430. (with-output-to-temp-buffer "*Org Help*"
  6431. (princ (format help (if org-goto-auto-isearch
  6432. " Just type for auto-isearch."
  6433. " n/p/f/b/u to navigate, q to quit."))))
  6434. (org-fit-window-to-buffer (get-buffer-window "*Org Help*"))
  6435. (setq buffer-read-only nil)
  6436. (let ((org-startup-truncated t)
  6437. (org-startup-folded nil)
  6438. (org-startup-align-all-tables nil))
  6439. (org-mode)
  6440. (org-overview))
  6441. (setq buffer-read-only t)
  6442. (if (and (boundp 'org-goto-start-pos)
  6443. (integer-or-marker-p org-goto-start-pos))
  6444. (let ((org-show-hierarchy-above t)
  6445. (org-show-siblings t)
  6446. (org-show-following-heading t))
  6447. (goto-char org-goto-start-pos)
  6448. (and (outline-invisible-p) (org-show-context)))
  6449. (goto-char (point-min)))
  6450. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  6451. (message "Select location and press RET")
  6452. (use-local-map org-goto-map)
  6453. (recursive-edit)))
  6454. (kill-buffer "*org-goto*")
  6455. (cons org-goto-selected-point org-goto-exit-command))))
  6456. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  6457. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  6458. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  6459. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  6460. (defun org-goto-local-search-headings (string bound noerror)
  6461. "Search and make sure that any matches are in headlines."
  6462. (catch 'return
  6463. (while (if isearch-forward
  6464. (search-forward string bound noerror)
  6465. (search-backward string bound noerror))
  6466. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  6467. (and (member :headline context)
  6468. (not (member :tags context))))
  6469. (throw 'return (point))))))
  6470. (defun org-goto-local-auto-isearch ()
  6471. "Start isearch."
  6472. (interactive)
  6473. (goto-char (point-min))
  6474. (let ((keys (this-command-keys)))
  6475. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  6476. (isearch-mode t)
  6477. (isearch-process-search-char (string-to-char keys)))))
  6478. (defun org-goto-ret (&optional arg)
  6479. "Finish `org-goto' by going to the new location."
  6480. (interactive "P")
  6481. (setq org-goto-selected-point (point)
  6482. org-goto-exit-command 'return)
  6483. (throw 'exit nil))
  6484. (defun org-goto-left ()
  6485. "Finish `org-goto' by going to the new location."
  6486. (interactive)
  6487. (if (org-at-heading-p)
  6488. (progn
  6489. (beginning-of-line 1)
  6490. (setq org-goto-selected-point (point)
  6491. org-goto-exit-command 'left)
  6492. (throw 'exit nil))
  6493. (error "Not on a heading")))
  6494. (defun org-goto-right ()
  6495. "Finish `org-goto' by going to the new location."
  6496. (interactive)
  6497. (if (org-at-heading-p)
  6498. (progn
  6499. (setq org-goto-selected-point (point)
  6500. org-goto-exit-command 'right)
  6501. (throw 'exit nil))
  6502. (error "Not on a heading")))
  6503. (defun org-goto-quit ()
  6504. "Finish `org-goto' without cursor motion."
  6505. (interactive)
  6506. (setq org-goto-selected-point nil)
  6507. (setq org-goto-exit-command 'quit)
  6508. (throw 'exit nil))
  6509. ;;; Indirect buffer display of subtrees
  6510. (defvar org-indirect-dedicated-frame nil
  6511. "This is the frame being used for indirect tree display.")
  6512. (defvar org-last-indirect-buffer nil)
  6513. (defun org-tree-to-indirect-buffer (&optional arg)
  6514. "Create indirect buffer and narrow it to current subtree.
  6515. With a numerical prefix ARG, go up to this level and then take that tree.
  6516. If ARG is negative, go up that many levels.
  6517. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  6518. indirect buffer previously made with this command, to avoid proliferation of
  6519. indirect buffers. However, when you call the command with a \
  6520. \\[universal-argument] prefix, or
  6521. when `org-indirect-buffer-display' is `new-frame', the last buffer
  6522. is kept so that you can work with several indirect buffers at the same time.
  6523. If `org-indirect-buffer-display' is `dedicated-frame', the \
  6524. \\[universal-argument] prefix also
  6525. requests that a new frame be made for the new buffer, so that the dedicated
  6526. frame is not changed."
  6527. (interactive "P")
  6528. (let ((cbuf (current-buffer))
  6529. (cwin (selected-window))
  6530. (pos (point))
  6531. beg end level heading ibuf)
  6532. (save-excursion
  6533. (org-back-to-heading t)
  6534. (when (numberp arg)
  6535. (setq level (org-outline-level))
  6536. (if (< arg 0) (setq arg (+ level arg)))
  6537. (while (> (setq level (org-outline-level)) arg)
  6538. (org-up-heading-safe)))
  6539. (setq beg (point)
  6540. heading (org-get-heading))
  6541. (org-end-of-subtree t t)
  6542. (if (org-at-heading-p) (backward-char 1))
  6543. (setq end (point)))
  6544. (if (and (buffer-live-p org-last-indirect-buffer)
  6545. (not (eq org-indirect-buffer-display 'new-frame))
  6546. (not arg))
  6547. (kill-buffer org-last-indirect-buffer))
  6548. (setq ibuf (org-get-indirect-buffer cbuf)
  6549. org-last-indirect-buffer ibuf)
  6550. (cond
  6551. ((or (eq org-indirect-buffer-display 'new-frame)
  6552. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  6553. (select-frame (make-frame))
  6554. (delete-other-windows)
  6555. (org-pop-to-buffer-same-window ibuf)
  6556. (org-set-frame-title heading))
  6557. ((eq org-indirect-buffer-display 'dedicated-frame)
  6558. (raise-frame
  6559. (select-frame (or (and org-indirect-dedicated-frame
  6560. (frame-live-p org-indirect-dedicated-frame)
  6561. org-indirect-dedicated-frame)
  6562. (setq org-indirect-dedicated-frame (make-frame)))))
  6563. (delete-other-windows)
  6564. (org-pop-to-buffer-same-window ibuf)
  6565. (org-set-frame-title (concat "Indirect: " heading)))
  6566. ((eq org-indirect-buffer-display 'current-window)
  6567. (org-pop-to-buffer-same-window ibuf))
  6568. ((eq org-indirect-buffer-display 'other-window)
  6569. (pop-to-buffer ibuf))
  6570. (t (error "Invalid value")))
  6571. (if (featurep 'xemacs)
  6572. (save-excursion (org-mode) (turn-on-font-lock)))
  6573. (narrow-to-region beg end)
  6574. (show-all)
  6575. (goto-char pos)
  6576. (run-hook-with-args 'org-cycle-hook 'all)
  6577. (and (window-live-p cwin) (select-window cwin))))
  6578. (defun org-get-indirect-buffer (&optional buffer)
  6579. (setq buffer (or buffer (current-buffer)))
  6580. (let ((n 1) (base (buffer-name buffer)) bname)
  6581. (while (buffer-live-p
  6582. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  6583. (setq n (1+ n)))
  6584. (condition-case nil
  6585. (make-indirect-buffer buffer bname 'clone)
  6586. (error (make-indirect-buffer buffer bname)))))
  6587. (defun org-set-frame-title (title)
  6588. "Set the title of the current frame to the string TITLE."
  6589. ;; FIXME: how to name a single frame in XEmacs???
  6590. (unless (featurep 'xemacs)
  6591. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  6592. ;;;; Structure editing
  6593. ;;; Inserting headlines
  6594. (defun org-previous-line-empty-p (&optional next)
  6595. "Is the previous line a blank line?
  6596. When NEXT is non-nil, check the next line instead."
  6597. (save-excursion
  6598. (and (not (bobp))
  6599. (or (beginning-of-line (if next 2 0)) t)
  6600. (save-match-data
  6601. (looking-at "[ \t]*$")))))
  6602. (defun org-insert-heading (&optional arg invisible-ok)
  6603. "Insert a new heading or item with same depth at point.
  6604. If point is in a plain list and ARG is nil, create a new list item.
  6605. With one universal prefix argument, insert a heading even in lists.
  6606. With two universal prefix arguments, insert the heading at the end
  6607. of the parent subtree.
  6608. If point is at the beginning of a headline, insert a sibling before
  6609. the current headline. If point is not at the beginning, split the line
  6610. and create a new headline with the text in the current line after point
  6611. \(see `org-M-RET-may-split-line' on how to modify this behavior).
  6612. When INVISIBLE-OK is set, stop at invisible headlines when going back.
  6613. This is important for non-interactive uses of the command."
  6614. (interactive "P")
  6615. (cond
  6616. ((or (= (buffer-size) 0)
  6617. (and (not (save-excursion
  6618. (and (ignore-errors (org-back-to-heading invisible-ok))
  6619. (org-at-heading-p))))
  6620. (or arg (not (org-in-item-p)))))
  6621. (insert
  6622. (if (org-previous-line-empty-p) "" "\n")
  6623. (if (org-in-src-block-p) ",* " "* "))
  6624. (run-hooks 'org-insert-heading-hook))
  6625. ((or arg (not (org-insert-item
  6626. (save-excursion
  6627. (beginning-of-line)
  6628. (looking-at org-list-full-item-re)
  6629. (match-string 3)))))
  6630. (let (begn endn)
  6631. (when (org-buffer-narrowed-p)
  6632. (setq begn (point-min) endn (point-max))
  6633. (widen))
  6634. (let* ((empty-line-p nil)
  6635. (eops (equal arg '(16))) ; insert at end of parent subtree
  6636. (org-insert-heading-respect-content
  6637. (or (not (null arg)) org-insert-heading-respect-content))
  6638. (level nil)
  6639. (on-heading (org-at-heading-p))
  6640. (head (save-excursion
  6641. (condition-case nil
  6642. (progn
  6643. (org-back-to-heading invisible-ok)
  6644. (when (and (not on-heading)
  6645. (featurep 'org-inlinetask)
  6646. (integerp org-inlinetask-min-level)
  6647. (>= (length (match-string 0))
  6648. org-inlinetask-min-level))
  6649. ;; Find a heading level before the inline task
  6650. (while (and (setq level (org-up-heading-safe))
  6651. (>= level org-inlinetask-min-level)))
  6652. (if (org-at-heading-p)
  6653. (org-back-to-heading invisible-ok)
  6654. (error "This should not happen")))
  6655. (unless (and (save-excursion
  6656. (save-match-data
  6657. (org-backward-heading-same-level 1 invisible-ok))
  6658. (= (point) (match-beginning 0)))
  6659. (not (org-previous-line-empty-p t)))
  6660. (setq empty-line-p (org-previous-line-empty-p)))
  6661. (match-string 0))
  6662. (error "* "))))
  6663. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6664. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  6665. pos hide-previous previous-pos)
  6666. (if ;; At the beginning of a heading, open a new line for insertiong
  6667. (and (bolp) (org-at-heading-p)
  6668. (not eops)
  6669. (or (bobp)
  6670. (save-excursion (backward-char 1) (not (outline-invisible-p)))))
  6671. (open-line (if blank 2 1))
  6672. (save-excursion
  6673. (setq previous-pos (point-at-bol))
  6674. (end-of-line)
  6675. (setq hide-previous (outline-invisible-p)))
  6676. (and org-insert-heading-respect-content
  6677. (save-excursion
  6678. (while (outline-invisible-p)
  6679. (org-show-subtree)
  6680. (org-up-heading-safe))))
  6681. (let ((split
  6682. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  6683. (save-excursion
  6684. (let ((p (point)))
  6685. (goto-char (point-at-bol))
  6686. (and (looking-at org-complex-heading-regexp)
  6687. (match-beginning 4)
  6688. (> p (match-beginning 4)))))))
  6689. tags pos)
  6690. (cond
  6691. ;; Insert a new line, possibly at end of parent subtree
  6692. (org-insert-heading-respect-content
  6693. (if (not eops)
  6694. (progn
  6695. (org-end-of-subtree nil t)
  6696. (and (looking-at "^\\*") (backward-char 1))
  6697. (while (and (not (bobp))
  6698. ;; Don't delete spaces in empty headlines
  6699. (not (looking-back org-outline-regexp))
  6700. (member (char-before) '(?\ ?\t ?\n)))
  6701. (backward-delete-char 1)))
  6702. (let ((p (point)))
  6703. (org-up-heading-safe)
  6704. (if (= p (point))
  6705. (goto-char (point-max))
  6706. (org-end-of-subtree nil t))))
  6707. (when (featurep 'org-inlinetask)
  6708. (while (and (not (eobp))
  6709. (looking-at "\\(\\*+\\)[ \t]+")
  6710. (>= (length (match-string 1))
  6711. org-inlinetask-min-level))
  6712. (org-end-of-subtree nil t)))
  6713. (or (bolp) (newline))
  6714. (or (org-previous-line-empty-p)
  6715. (and blank (newline)))
  6716. (if (or empty-line-p eops) (open-line 1)))
  6717. ;; Insert a headling containing text after point
  6718. ((org-at-heading-p)
  6719. (when hide-previous
  6720. (show-children)
  6721. (org-show-entry))
  6722. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?[ \t]*$")
  6723. (setq tags (and (match-end 2) (match-string 2)))
  6724. (and (match-end 1)
  6725. (delete-region (match-beginning 1) (match-end 1)))
  6726. (setq pos (point-at-bol))
  6727. (or split (end-of-line 1))
  6728. (delete-horizontal-space)
  6729. (if (string-match "\\`\\*+\\'"
  6730. (buffer-substring (point-at-bol) (point)))
  6731. (insert " "))
  6732. (newline (if blank 2 1))
  6733. (when tags
  6734. (save-excursion
  6735. (goto-char pos)
  6736. (end-of-line 1)
  6737. (insert " " tags)
  6738. (org-set-tags nil 'align))))
  6739. (t
  6740. (or split (end-of-line 1))
  6741. (newline (if blank 2 1))))))
  6742. (insert head) (just-one-space)
  6743. (setq pos (point))
  6744. (end-of-line 1)
  6745. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  6746. (when (and org-insert-heading-respect-content hide-previous)
  6747. (save-excursion
  6748. (goto-char previous-pos)
  6749. (hide-subtree)))
  6750. (when (and begn endn)
  6751. (narrow-to-region (min (point) begn) (max (point) endn)))
  6752. (run-hooks 'org-insert-heading-hook))))))
  6753. (defun org-get-heading (&optional no-tags no-todo)
  6754. "Return the heading of the current entry, without the stars.
  6755. When NO-TAGS is non-nil, don't include tags.
  6756. When NO-TODO is non-nil, don't include TODO keywords."
  6757. (save-excursion
  6758. (org-back-to-heading t)
  6759. (cond
  6760. ((and no-tags no-todo)
  6761. (looking-at org-complex-heading-regexp)
  6762. (match-string 4))
  6763. (no-tags
  6764. (looking-at (concat org-outline-regexp
  6765. "\\(.*?\\)"
  6766. "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
  6767. (match-string 1))
  6768. (no-todo
  6769. (looking-at org-todo-line-regexp)
  6770. (match-string 3))
  6771. (t (looking-at org-heading-regexp)
  6772. (match-string 2)))))
  6773. (defvar orgstruct-mode) ; defined below
  6774. (defun org-heading-components ()
  6775. "Return the components of the current heading.
  6776. This is a list with the following elements:
  6777. - the level as an integer
  6778. - the reduced level, different if `org-odd-levels-only' is set.
  6779. - the TODO keyword, or nil
  6780. - the priority character, like ?A, or nil if no priority is given
  6781. - the headline text itself, or the tags string if no headline text
  6782. - the tags string, or nil."
  6783. (save-excursion
  6784. (org-back-to-heading t)
  6785. (if (let (case-fold-search)
  6786. (looking-at
  6787. (if orgstruct-mode
  6788. org-heading-regexp
  6789. org-complex-heading-regexp)))
  6790. (if orgstruct-mode
  6791. (list (length (match-string 1))
  6792. (org-reduced-level (length (match-string 1)))
  6793. nil
  6794. nil
  6795. (match-string 2)
  6796. nil)
  6797. (list (length (match-string 1))
  6798. (org-reduced-level (length (match-string 1)))
  6799. (org-match-string-no-properties 2)
  6800. (and (match-end 3) (aref (match-string 3) 2))
  6801. (org-match-string-no-properties 4)
  6802. (org-match-string-no-properties 5))))))
  6803. (defun org-get-entry ()
  6804. "Get the entry text, after heading, entire subtree."
  6805. (save-excursion
  6806. (org-back-to-heading t)
  6807. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  6808. (defun org-insert-heading-after-current ()
  6809. "Insert a new heading with same level as current, after current subtree."
  6810. (interactive)
  6811. (org-back-to-heading)
  6812. (org-insert-heading)
  6813. (org-move-subtree-down)
  6814. (end-of-line 1))
  6815. (defun org-insert-heading-respect-content (&optional arg invisible-ok)
  6816. "Insert heading with `org-insert-heading-respect-content' set to t."
  6817. (interactive "P")
  6818. (let ((org-insert-heading-respect-content t))
  6819. (org-insert-heading arg invisible-ok)))
  6820. (defun org-insert-todo-heading-respect-content (&optional force-state)
  6821. "Insert TODO heading with `org-insert-heading-respect-content' set to t."
  6822. (interactive "P")
  6823. (let ((org-insert-heading-respect-content t))
  6824. (org-insert-todo-heading force-state t)))
  6825. (defun org-insert-todo-heading (arg &optional force-heading)
  6826. "Insert a new heading with the same level and TODO state as current heading.
  6827. If the heading has no TODO state, or if the state is DONE, use the first
  6828. state (TODO by default). Also one prefix arg, force first state. With two
  6829. prefix args, force inserting at the end of the parent subtree."
  6830. (interactive "P")
  6831. (when (or force-heading (not (org-insert-item 'checkbox)))
  6832. (org-insert-heading (or (and (equal arg '(16)) '(16))
  6833. force-heading))
  6834. (save-excursion
  6835. (org-back-to-heading)
  6836. (outline-previous-heading)
  6837. (looking-at org-todo-line-regexp))
  6838. (let*
  6839. ((new-mark-x
  6840. (if (or arg
  6841. (not (match-beginning 2))
  6842. (member (match-string 2) org-done-keywords))
  6843. (car org-todo-keywords-1)
  6844. (match-string 2)))
  6845. (new-mark
  6846. (or
  6847. (run-hook-with-args-until-success
  6848. 'org-todo-get-default-hook new-mark-x nil)
  6849. new-mark-x)))
  6850. (beginning-of-line 1)
  6851. (and (looking-at org-outline-regexp) (goto-char (match-end 0))
  6852. (if org-treat-insert-todo-heading-as-state-change
  6853. (org-todo new-mark)
  6854. (insert new-mark " "))))
  6855. (when org-provide-todo-statistics
  6856. (org-update-parent-todo-statistics))))
  6857. (defun org-insert-subheading (arg)
  6858. "Insert a new subheading and demote it.
  6859. Works for outline headings and for plain lists alike."
  6860. (interactive "P")
  6861. (org-insert-heading arg)
  6862. (cond
  6863. ((org-at-heading-p) (org-do-demote))
  6864. ((org-at-item-p) (org-indent-item))))
  6865. (defun org-insert-todo-subheading (arg)
  6866. "Insert a new subheading with TODO keyword or checkbox and demote it.
  6867. Works for outline headings and for plain lists alike."
  6868. (interactive "P")
  6869. (org-insert-todo-heading arg)
  6870. (cond
  6871. ((org-at-heading-p) (org-do-demote))
  6872. ((org-at-item-p) (org-indent-item))))
  6873. ;;; Promotion and Demotion
  6874. (defvar org-after-demote-entry-hook nil
  6875. "Hook run after an entry has been demoted.
  6876. The cursor will be at the beginning of the entry.
  6877. When a subtree is being demoted, the hook will be called for each node.")
  6878. (defvar org-after-promote-entry-hook nil
  6879. "Hook run after an entry has been promoted.
  6880. The cursor will be at the beginning of the entry.
  6881. When a subtree is being promoted, the hook will be called for each node.")
  6882. (defun org-promote-subtree ()
  6883. "Promote the entire subtree.
  6884. See also `org-promote'."
  6885. (interactive)
  6886. (save-excursion
  6887. (org-with-limited-levels (org-map-tree 'org-promote)))
  6888. (org-fix-position-after-promote))
  6889. (defun org-demote-subtree ()
  6890. "Demote the entire subtree. See `org-demote'.
  6891. See also `org-promote'."
  6892. (interactive)
  6893. (save-excursion
  6894. (org-with-limited-levels (org-map-tree 'org-demote)))
  6895. (org-fix-position-after-promote))
  6896. (defun org-do-promote ()
  6897. "Promote the current heading higher up the tree.
  6898. If the region is active in `transient-mark-mode', promote all headings
  6899. in the region."
  6900. (interactive)
  6901. (save-excursion
  6902. (if (org-region-active-p)
  6903. (org-map-region 'org-promote (region-beginning) (region-end))
  6904. (org-promote)))
  6905. (org-fix-position-after-promote))
  6906. (defun org-do-demote ()
  6907. "Demote the current heading lower down the tree.
  6908. If the region is active in `transient-mark-mode', demote all headings
  6909. in the region."
  6910. (interactive)
  6911. (save-excursion
  6912. (if (org-region-active-p)
  6913. (org-map-region 'org-demote (region-beginning) (region-end))
  6914. (org-demote)))
  6915. (org-fix-position-after-promote))
  6916. (defun org-fix-position-after-promote ()
  6917. "Make sure that after pro/demotion cursor position is right."
  6918. (let ((pos (point)))
  6919. (when (save-excursion
  6920. (beginning-of-line 1)
  6921. (looking-at org-todo-line-regexp)
  6922. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  6923. (cond ((eobp) (insert " "))
  6924. ((eolp) (insert " "))
  6925. ((equal (char-after) ?\ ) (forward-char 1))))))
  6926. (defun org-current-level ()
  6927. "Return the level of the current entry, or nil if before the first headline.
  6928. The level is the number of stars at the beginning of the headline."
  6929. (save-excursion
  6930. (org-with-limited-levels
  6931. (if (ignore-errors (org-back-to-heading t))
  6932. (funcall outline-level)))))
  6933. (defun org-get-previous-line-level ()
  6934. "Return the outline depth of the last headline before the current line.
  6935. Returns 0 for the first headline in the buffer, and nil if before the
  6936. first headline."
  6937. (let ((current-level (org-current-level))
  6938. (prev-level (when (> (line-number-at-pos) 1)
  6939. (save-excursion
  6940. (beginning-of-line 0)
  6941. (org-current-level)))))
  6942. (cond ((null current-level) nil) ; Before first headline
  6943. ((null prev-level) 0) ; At first headline
  6944. (prev-level))))
  6945. (defun org-reduced-level (l)
  6946. "Compute the effective level of a heading.
  6947. This takes into account the setting of `org-odd-levels-only'."
  6948. (cond
  6949. ((zerop l) 0)
  6950. (org-odd-levels-only (1+ (floor (/ l 2))))
  6951. (t l)))
  6952. (defun org-level-increment ()
  6953. "Return the number of stars that will be added or removed at a
  6954. time to headlines when structure editing, based on the value of
  6955. `org-odd-levels-only'."
  6956. (if org-odd-levels-only 2 1))
  6957. (defun org-get-valid-level (level &optional change)
  6958. "Rectify a level change under the influence of `org-odd-levels-only'
  6959. LEVEL is a current level, CHANGE is by how much the level should be
  6960. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  6961. even level numbers will become the next higher odd number."
  6962. (if org-odd-levels-only
  6963. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  6964. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  6965. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  6966. (max 1 (+ level (or change 0)))))
  6967. (if (boundp 'define-obsolete-function-alias)
  6968. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  6969. (define-obsolete-function-alias 'org-get-legal-level
  6970. 'org-get-valid-level)
  6971. (define-obsolete-function-alias 'org-get-legal-level
  6972. 'org-get-valid-level "23.1")))
  6973. (defvar org-called-with-limited-levels nil) ;; Dynamically bound in
  6974. ;; ̀org-with-limited-levels'
  6975. (defun org-promote ()
  6976. "Promote the current heading higher up the tree.
  6977. If the region is active in `transient-mark-mode', promote all headings
  6978. in the region."
  6979. (org-back-to-heading t)
  6980. (let* ((level (save-match-data (funcall outline-level)))
  6981. (after-change-functions (remove 'flyspell-after-change-function
  6982. after-change-functions))
  6983. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  6984. (diff (abs (- level (length up-head) -1))))
  6985. (cond ((and (= level 1) org-called-with-limited-levels
  6986. org-allow-promoting-top-level-subtree)
  6987. (replace-match "# " nil t))
  6988. ((= level 1)
  6989. (error "Cannot promote to level 0. UNDO to recover if necessary"))
  6990. (t (replace-match up-head nil t)))
  6991. ;; Fixup tag positioning
  6992. (unless (= level 1)
  6993. (and org-auto-align-tags (org-set-tags nil t))
  6994. (if org-adapt-indentation (org-fixup-indentation (- diff))))
  6995. (run-hooks 'org-after-promote-entry-hook)))
  6996. (defun org-demote ()
  6997. "Demote the current heading lower down the tree.
  6998. If the region is active in `transient-mark-mode', demote all headings
  6999. in the region."
  7000. (org-back-to-heading t)
  7001. (let* ((level (save-match-data (funcall outline-level)))
  7002. (after-change-functions (remove 'flyspell-after-change-function
  7003. after-change-functions))
  7004. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  7005. (diff (abs (- level (length down-head) -1))))
  7006. (replace-match down-head nil t)
  7007. ;; Fixup tag positioning
  7008. (and org-auto-align-tags (org-set-tags nil t))
  7009. (if org-adapt-indentation (org-fixup-indentation diff))
  7010. (run-hooks 'org-after-demote-entry-hook)))
  7011. (defun org-cycle-level ()
  7012. "Cycle the level of an empty headline through possible states.
  7013. This goes first to child, then to parent, level, then up the hierarchy.
  7014. After top level, it switches back to sibling level."
  7015. (interactive)
  7016. (let ((org-adapt-indentation nil))
  7017. (when (org-point-at-end-of-empty-headline)
  7018. (setq this-command 'org-cycle-level) ; Only needed for caching
  7019. (let ((cur-level (org-current-level))
  7020. (prev-level (org-get-previous-line-level)))
  7021. (cond
  7022. ;; If first headline in file, promote to top-level.
  7023. ((= prev-level 0)
  7024. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7025. do (org-do-promote)))
  7026. ;; If same level as prev, demote one.
  7027. ((= prev-level cur-level)
  7028. (org-do-demote))
  7029. ;; If parent is top-level, promote to top level if not already.
  7030. ((= prev-level 1)
  7031. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7032. do (org-do-promote)))
  7033. ;; If top-level, return to prev-level.
  7034. ((= cur-level 1)
  7035. (loop repeat (/ (- prev-level 1) (org-level-increment))
  7036. do (org-do-demote)))
  7037. ;; If less than prev-level, promote one.
  7038. ((< cur-level prev-level)
  7039. (org-do-promote))
  7040. ;; If deeper than prev-level, promote until higher than
  7041. ;; prev-level.
  7042. ((> cur-level prev-level)
  7043. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  7044. do (org-do-promote))))
  7045. t))))
  7046. (defun org-map-tree (fun)
  7047. "Call FUN for every heading underneath the current one."
  7048. (org-back-to-heading)
  7049. (let ((level (funcall outline-level)))
  7050. (save-excursion
  7051. (funcall fun)
  7052. (while (and (progn
  7053. (outline-next-heading)
  7054. (> (funcall outline-level) level))
  7055. (not (eobp)))
  7056. (funcall fun)))))
  7057. (defun org-map-region (fun beg end)
  7058. "Call FUN for every heading between BEG and END."
  7059. (let ((org-ignore-region t))
  7060. (save-excursion
  7061. (setq end (copy-marker end))
  7062. (goto-char beg)
  7063. (if (and (re-search-forward org-outline-regexp-bol nil t)
  7064. (< (point) end))
  7065. (funcall fun))
  7066. (while (and (progn
  7067. (outline-next-heading)
  7068. (< (point) end))
  7069. (not (eobp)))
  7070. (funcall fun)))))
  7071. (defvar org-property-end-re) ; silence byte-compiler
  7072. (defun org-fixup-indentation (diff)
  7073. "Change the indentation in the current entry by DIFF.
  7074. However, if any line in the current entry has no indentation, or if it
  7075. would end up with no indentation after the change, nothing at all is done."
  7076. (save-excursion
  7077. (let ((end (save-excursion (outline-next-heading)
  7078. (point-marker)))
  7079. (prohibit (if (> diff 0)
  7080. "^\\S-"
  7081. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  7082. col)
  7083. (unless (save-excursion (end-of-line 1)
  7084. (re-search-forward prohibit end t))
  7085. (while (and (< (point) end)
  7086. (re-search-forward "^[ \t]+" end t))
  7087. (goto-char (match-end 0))
  7088. (setq col (current-column))
  7089. (if (< diff 0) (replace-match ""))
  7090. (org-indent-to-column (+ diff col))))
  7091. (move-marker end nil))))
  7092. (defun org-convert-to-odd-levels ()
  7093. "Convert an org-mode file with all levels allowed to one with odd levels.
  7094. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  7095. level 5 etc."
  7096. (interactive)
  7097. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  7098. (let ((outline-level 'org-outline-level)
  7099. (org-odd-levels-only nil) n)
  7100. (save-excursion
  7101. (goto-char (point-min))
  7102. (while (re-search-forward "^\\*\\*+ " nil t)
  7103. (setq n (- (length (match-string 0)) 2))
  7104. (while (>= (setq n (1- n)) 0)
  7105. (org-demote))
  7106. (end-of-line 1))))))
  7107. (defun org-convert-to-oddeven-levels ()
  7108. "Convert an org-mode file with only odd levels to one with odd/even levels.
  7109. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  7110. file contains a section with an even level, conversion would
  7111. destroy the structure of the file. An error is signaled in this
  7112. case."
  7113. (interactive)
  7114. (goto-char (point-min))
  7115. ;; First check if there are no even levels
  7116. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  7117. (org-show-context t)
  7118. (error "Not all levels are odd in this file. Conversion not possible"))
  7119. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  7120. (let ((outline-regexp org-outline-regexp)
  7121. (outline-level 'org-outline-level)
  7122. (org-odd-levels-only nil) n)
  7123. (save-excursion
  7124. (goto-char (point-min))
  7125. (while (re-search-forward "^\\*\\*+ " nil t)
  7126. (setq n (/ (1- (length (match-string 0))) 2))
  7127. (while (>= (setq n (1- n)) 0)
  7128. (org-promote))
  7129. (end-of-line 1))))))
  7130. (defun org-tr-level (n)
  7131. "Make N odd if required."
  7132. (if org-odd-levels-only (1+ (/ n 2)) n))
  7133. ;;; Vertical tree motion, cutting and pasting of subtrees
  7134. (defun org-move-subtree-up (&optional arg)
  7135. "Move the current subtree up past ARG headlines of the same level."
  7136. (interactive "p")
  7137. (org-move-subtree-down (- (prefix-numeric-value arg))))
  7138. (defun org-move-subtree-down (&optional arg)
  7139. "Move the current subtree down past ARG headlines of the same level."
  7140. (interactive "p")
  7141. (setq arg (prefix-numeric-value arg))
  7142. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  7143. 'org-get-last-sibling))
  7144. (ins-point (make-marker))
  7145. (cnt (abs arg))
  7146. (col (current-column))
  7147. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  7148. ;; Select the tree
  7149. (org-back-to-heading)
  7150. (setq beg0 (point))
  7151. (save-excursion
  7152. (setq ne-beg (org-back-over-empty-lines))
  7153. (setq beg (point)))
  7154. (save-match-data
  7155. (save-excursion (outline-end-of-heading)
  7156. (setq folded (outline-invisible-p)))
  7157. (outline-end-of-subtree))
  7158. (outline-next-heading)
  7159. (setq ne-end (org-back-over-empty-lines))
  7160. (setq end (point))
  7161. (goto-char beg0)
  7162. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  7163. ;; include less whitespace
  7164. (save-excursion
  7165. (goto-char beg)
  7166. (forward-line (- ne-beg ne-end))
  7167. (setq beg (point))))
  7168. ;; Find insertion point, with error handling
  7169. (while (> cnt 0)
  7170. (or (and (funcall movfunc) (looking-at org-outline-regexp))
  7171. (progn (goto-char beg0)
  7172. (user-error "Cannot move past superior level or buffer limit")))
  7173. (setq cnt (1- cnt)))
  7174. (if (> arg 0)
  7175. ;; Moving forward - still need to move over subtree
  7176. (progn (org-end-of-subtree t t)
  7177. (save-excursion
  7178. (org-back-over-empty-lines)
  7179. (or (bolp) (newline)))))
  7180. (setq ne-ins (org-back-over-empty-lines))
  7181. (move-marker ins-point (point))
  7182. (setq txt (buffer-substring beg end))
  7183. (org-save-markers-in-region beg end)
  7184. (delete-region beg end)
  7185. (org-remove-empty-overlays-at beg)
  7186. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  7187. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  7188. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  7189. (let ((bbb (point)))
  7190. (insert-before-markers txt)
  7191. (org-reinstall-markers-in-region bbb)
  7192. (move-marker ins-point bbb))
  7193. (or (bolp) (insert "\n"))
  7194. (setq ins-end (point))
  7195. (goto-char ins-point)
  7196. (org-skip-whitespace)
  7197. (when (and (< arg 0)
  7198. (org-first-sibling-p)
  7199. (> ne-ins ne-beg))
  7200. ;; Move whitespace back to beginning
  7201. (save-excursion
  7202. (goto-char ins-end)
  7203. (let ((kill-whole-line t))
  7204. (kill-line (- ne-ins ne-beg)) (point)))
  7205. (insert (make-string (- ne-ins ne-beg) ?\n)))
  7206. (move-marker ins-point nil)
  7207. (if folded
  7208. (hide-subtree)
  7209. (org-show-entry)
  7210. (show-children)
  7211. (org-cycle-hide-drawers 'children))
  7212. (org-clean-visibility-after-subtree-move)
  7213. ;; move back to the initial column we were at
  7214. (move-to-column col)))
  7215. (defvar org-subtree-clip ""
  7216. "Clipboard for cut and paste of subtrees.
  7217. This is actually only a copy of the kill, because we use the normal kill
  7218. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  7219. (defvar org-subtree-clip-folded nil
  7220. "Was the last copied subtree folded?
  7221. This is used to fold the tree back after pasting.")
  7222. (defun org-cut-subtree (&optional n)
  7223. "Cut the current subtree into the clipboard.
  7224. With prefix arg N, cut this many sequential subtrees.
  7225. This is a short-hand for marking the subtree and then cutting it."
  7226. (interactive "p")
  7227. (org-copy-subtree n 'cut))
  7228. (defun org-copy-subtree (&optional n cut force-store-markers nosubtrees)
  7229. "Cut the current subtree into the clipboard.
  7230. With prefix arg N, cut this many sequential subtrees.
  7231. This is a short-hand for marking the subtree and then copying it.
  7232. If CUT is non-nil, actually cut the subtree.
  7233. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  7234. of some markers in the region, even if CUT is non-nil. This is
  7235. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  7236. (interactive "p")
  7237. (let (beg end folded (beg0 (point)))
  7238. (if (org-called-interactively-p 'any)
  7239. (org-back-to-heading nil) ; take what looks like a subtree
  7240. (org-back-to-heading t)) ; take what is really there
  7241. (setq beg (point))
  7242. (skip-chars-forward " \t\r\n")
  7243. (save-match-data
  7244. (if nosubtrees
  7245. (outline-next-heading)
  7246. (save-excursion (outline-end-of-heading)
  7247. (setq folded (outline-invisible-p)))
  7248. (condition-case nil
  7249. (org-forward-heading-same-level (1- n) t)
  7250. (error nil))
  7251. (org-end-of-subtree t t)))
  7252. (setq end (point))
  7253. (goto-char beg0)
  7254. (when (> end beg)
  7255. (setq org-subtree-clip-folded folded)
  7256. (when (or cut force-store-markers)
  7257. (org-save-markers-in-region beg end))
  7258. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  7259. (setq org-subtree-clip (current-kill 0))
  7260. (message "%s: Subtree(s) with %d characters"
  7261. (if cut "Cut" "Copied")
  7262. (length org-subtree-clip)))))
  7263. (defun org-paste-subtree (&optional level tree for-yank)
  7264. "Paste the clipboard as a subtree, with modification of headline level.
  7265. The entire subtree is promoted or demoted in order to match a new headline
  7266. level.
  7267. If the cursor is at the beginning of a headline, the same level as
  7268. that headline is used to paste the tree.
  7269. If not, the new level is derived from the *visible* headings
  7270. before and after the insertion point, and taken to be the inferior headline
  7271. level of the two. So if the previous visible heading is level 3 and the
  7272. next is level 4 (or vice versa), level 4 will be used for insertion.
  7273. This makes sure that the subtree remains an independent subtree and does
  7274. not swallow low level entries.
  7275. You can also force a different level, either by using a numeric prefix
  7276. argument, or by inserting the heading marker by hand. For example, if the
  7277. cursor is after \"*****\", then the tree will be shifted to level 5.
  7278. If optional TREE is given, use this text instead of the kill ring.
  7279. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  7280. move back over whitespace before inserting, and move point to the end of
  7281. the inserted text when done."
  7282. (interactive "P")
  7283. (setq tree (or tree (and kill-ring (current-kill 0))))
  7284. (unless (org-kill-is-subtree-p tree)
  7285. (error "%s"
  7286. (substitute-command-keys
  7287. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  7288. (org-with-limited-levels
  7289. (let* ((visp (not (outline-invisible-p)))
  7290. (txt tree)
  7291. (^re_ "\\(\\*+\\)[ \t]*")
  7292. (old-level (if (string-match org-outline-regexp-bol txt)
  7293. (- (match-end 0) (match-beginning 0) 1)
  7294. -1))
  7295. (force-level (cond (level (prefix-numeric-value level))
  7296. ((and (looking-at "[ \t]*$")
  7297. (string-match
  7298. "^\\*+$" (buffer-substring
  7299. (point-at-bol) (point))))
  7300. (- (match-end 1) (match-beginning 1)))
  7301. ((and (bolp)
  7302. (looking-at org-outline-regexp))
  7303. (- (match-end 0) (point) 1))))
  7304. (previous-level (save-excursion
  7305. (condition-case nil
  7306. (progn
  7307. (outline-previous-visible-heading 1)
  7308. (if (looking-at ^re_)
  7309. (- (match-end 0) (match-beginning 0) 1)
  7310. 1))
  7311. (error 1))))
  7312. (next-level (save-excursion
  7313. (condition-case nil
  7314. (progn
  7315. (or (looking-at org-outline-regexp)
  7316. (outline-next-visible-heading 1))
  7317. (if (looking-at ^re_)
  7318. (- (match-end 0) (match-beginning 0) 1)
  7319. 1))
  7320. (error 1))))
  7321. (new-level (or force-level (max previous-level next-level)))
  7322. (shift (if (or (= old-level -1)
  7323. (= new-level -1)
  7324. (= old-level new-level))
  7325. 0
  7326. (- new-level old-level)))
  7327. (delta (if (> shift 0) -1 1))
  7328. (func (if (> shift 0) 'org-demote 'org-promote))
  7329. (org-odd-levels-only nil)
  7330. beg end newend)
  7331. ;; Remove the forced level indicator
  7332. (if force-level
  7333. (delete-region (point-at-bol) (point)))
  7334. ;; Paste
  7335. (beginning-of-line (if (bolp) 1 2))
  7336. (setq beg (point))
  7337. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  7338. (insert-before-markers txt)
  7339. (unless (string-match "\n\\'" txt) (insert "\n"))
  7340. (setq newend (point))
  7341. (org-reinstall-markers-in-region beg)
  7342. (setq end (point))
  7343. (goto-char beg)
  7344. (skip-chars-forward " \t\n\r")
  7345. (setq beg (point))
  7346. (if (and (outline-invisible-p) visp)
  7347. (save-excursion (outline-show-heading)))
  7348. ;; Shift if necessary
  7349. (unless (= shift 0)
  7350. (save-restriction
  7351. (narrow-to-region beg end)
  7352. (while (not (= shift 0))
  7353. (org-map-region func (point-min) (point-max))
  7354. (setq shift (+ delta shift)))
  7355. (goto-char (point-min))
  7356. (setq newend (point-max))))
  7357. (when (or (org-called-interactively-p 'interactive) for-yank)
  7358. (message "Clipboard pasted as level %d subtree" new-level))
  7359. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  7360. kill-ring
  7361. (eq org-subtree-clip (current-kill 0))
  7362. org-subtree-clip-folded)
  7363. ;; The tree was folded before it was killed/copied
  7364. (hide-subtree))
  7365. (and for-yank (goto-char newend)))))
  7366. (defun org-kill-is-subtree-p (&optional txt)
  7367. "Check if the current kill is an outline subtree, or a set of trees.
  7368. Returns nil if kill does not start with a headline, or if the first
  7369. headline level is not the largest headline level in the tree.
  7370. So this will actually accept several entries of equal levels as well,
  7371. which is OK for `org-paste-subtree'.
  7372. If optional TXT is given, check this string instead of the current kill."
  7373. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  7374. (re (org-get-limited-outline-regexp))
  7375. (^re (concat "^" re))
  7376. (start-level (and kill
  7377. (string-match
  7378. (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
  7379. kill)
  7380. (- (match-end 2) (match-beginning 2) 1)))
  7381. (start (1+ (or (match-beginning 2) -1))))
  7382. (if (not start-level)
  7383. (progn
  7384. nil) ;; does not even start with a heading
  7385. (catch 'exit
  7386. (while (setq start (string-match ^re kill (1+ start)))
  7387. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  7388. (throw 'exit nil)))
  7389. t))))
  7390. (defvar org-markers-to-move nil
  7391. "Markers that should be moved with a cut-and-paste operation.
  7392. Those markers are stored together with their positions relative to
  7393. the start of the region.")
  7394. (defun org-save-markers-in-region (beg end)
  7395. "Check markers in region.
  7396. If these markers are between BEG and END, record their position relative
  7397. to BEG, so that after moving the block of text, we can put the markers back
  7398. into place.
  7399. This function gets called just before an entry or tree gets cut from the
  7400. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  7401. called immediately, to move the markers with the entries."
  7402. (setq org-markers-to-move nil)
  7403. (when (featurep 'org-clock)
  7404. (org-clock-save-markers-for-cut-and-paste beg end))
  7405. (when (featurep 'org-agenda)
  7406. (org-agenda-save-markers-for-cut-and-paste beg end)))
  7407. (defun org-check-and-save-marker (marker beg end)
  7408. "Check if MARKER is between BEG and END.
  7409. If yes, remember the marker and the distance to BEG."
  7410. (when (and (marker-buffer marker)
  7411. (equal (marker-buffer marker) (current-buffer)))
  7412. (if (and (>= marker beg) (< marker end))
  7413. (push (cons marker (- marker beg)) org-markers-to-move))))
  7414. (defun org-reinstall-markers-in-region (beg)
  7415. "Move all remembered markers to their position relative to BEG."
  7416. (mapc (lambda (x)
  7417. (move-marker (car x) (+ beg (cdr x))))
  7418. org-markers-to-move)
  7419. (setq org-markers-to-move nil))
  7420. (defun org-narrow-to-subtree ()
  7421. "Narrow buffer to the current subtree."
  7422. (interactive)
  7423. (save-excursion
  7424. (save-match-data
  7425. (org-with-limited-levels
  7426. (narrow-to-region
  7427. (progn (org-back-to-heading t) (point))
  7428. (progn (org-end-of-subtree t t)
  7429. (if (and (org-at-heading-p) (not (eobp))) (backward-char 1))
  7430. (point)))))))
  7431. (defun org-narrow-to-block ()
  7432. "Narrow buffer to the current block."
  7433. (interactive)
  7434. (let* ((case-fold-search t)
  7435. (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
  7436. "^[ \t]*#\\+end_.*")))
  7437. (if blockp
  7438. (narrow-to-region (car blockp) (cdr blockp))
  7439. (error "Not in a block"))))
  7440. (eval-when-compile
  7441. (defvar org-property-drawer-re))
  7442. (defvar org-property-start-re) ;; defined below
  7443. (defun org-clone-subtree-with-time-shift (n &optional shift)
  7444. "Clone the task (subtree) at point N times.
  7445. The clones will be inserted as siblings.
  7446. In interactive use, the user will be prompted for the number of
  7447. clones to be produced. When called with a universal prefix argument
  7448. and if the entry has a timestamp, the user will also be prompted for
  7449. a time shift, which may be a repeater as used in time stamps, for
  7450. example `+3d'.
  7451. When a valid repeater is given and the entry contains any time
  7452. stamps, the clones will become a sequence in time, with time
  7453. stamps in the subtree shifted for each clone produced. If SHIFT
  7454. is nil or the empty string, time stamps will be left alone. The
  7455. ID property of the original subtree is removed.
  7456. If the original subtree did contain time stamps with a repeater,
  7457. the following will happen:
  7458. - the repeater will be removed in each clone
  7459. - an additional clone will be produced, with the current, unshifted
  7460. date(s) in the entry.
  7461. - the original entry will be placed *after* all the clones, with
  7462. repeater intact.
  7463. - the start days in the repeater in the original entry will be shifted
  7464. to past the last clone.
  7465. In this way you can spell out a number of instances of a repeating task,
  7466. and still retain the repeater to cover future instances of the task."
  7467. (interactive "nNumber of clones to produce: ")
  7468. (let ((shift
  7469. (or shift
  7470. (if (and (equal current-prefix-arg '(4))
  7471. (save-excursion
  7472. (re-search-forward org-ts-regexp-both
  7473. (save-excursion
  7474. (org-end-of-subtree t)
  7475. (point)) t)))
  7476. (read-from-minibuffer
  7477. "Date shift per clone (e.g. +1w, empty to copy unchanged): ")
  7478. ""))) ;; No time shift
  7479. (n-no-remove -1)
  7480. (drawer-re org-drawer-regexp)
  7481. beg end template task idprop
  7482. shift-n shift-what doshift nmin nmax)
  7483. (if (not (and (integerp n) (> n 0)))
  7484. (error "Invalid number of replications %s" n))
  7485. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  7486. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([hdwmy]\\)[ \t]*\\'"
  7487. shift)))
  7488. (error "Invalid shift specification %s" shift))
  7489. (when doshift
  7490. (setq shift-n (string-to-number (match-string 1 shift))
  7491. shift-what (cdr (assoc (match-string 2 shift)
  7492. '(("d" . day) ("w" . week)
  7493. ("m" . month) ("y" . year))))))
  7494. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  7495. (setq nmin 1 nmax n)
  7496. (org-back-to-heading t)
  7497. (setq beg (point))
  7498. (setq idprop (org-entry-get nil "ID"))
  7499. (org-end-of-subtree t t)
  7500. (or (bolp) (insert "\n"))
  7501. (setq end (point))
  7502. (setq template (buffer-substring beg end))
  7503. (when (and doshift
  7504. (string-match "<[^<>\n]+ [.+]?\\+[0-9]+[hdwmy][^<>\n]*>" template))
  7505. (delete-region beg end)
  7506. (setq end beg)
  7507. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  7508. (goto-char end)
  7509. (loop for n from nmin to nmax do
  7510. ;; prepare clone
  7511. (with-temp-buffer
  7512. (insert template)
  7513. (org-mode)
  7514. (goto-char (point-min))
  7515. (org-show-subtree)
  7516. (and idprop (if org-clone-delete-id
  7517. (org-entry-delete nil "ID")
  7518. (org-id-get-create t)))
  7519. (unless (= n 0)
  7520. (while (re-search-forward "^[ \t]*CLOCK:.*$" nil t)
  7521. (kill-whole-line))
  7522. (goto-char (point-min))
  7523. (while (re-search-forward drawer-re nil t)
  7524. (mapc (lambda (d)
  7525. (org-remove-empty-drawer-at d (point))) org-drawers)))
  7526. (goto-char (point-min))
  7527. (when doshift
  7528. (while (re-search-forward org-ts-regexp-both nil t)
  7529. (org-timestamp-change (* n shift-n) shift-what))
  7530. (unless (= n n-no-remove)
  7531. (goto-char (point-min))
  7532. (while (re-search-forward org-ts-regexp nil t)
  7533. (save-excursion
  7534. (goto-char (match-beginning 0))
  7535. (if (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[hdwmy]\\)")
  7536. (delete-region (match-beginning 1) (match-end 1)))))))
  7537. (setq task (buffer-string)))
  7538. (insert task))
  7539. (goto-char beg)))
  7540. ;;; Outline Sorting
  7541. (defun org-sort (with-case)
  7542. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  7543. Optional argument WITH-CASE means sort case-sensitively."
  7544. (interactive "P")
  7545. (cond
  7546. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  7547. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  7548. (t
  7549. (org-call-with-arg 'org-sort-entries with-case))))
  7550. (defun org-sort-remove-invisible (s)
  7551. "Remove invisible links from string S."
  7552. (remove-text-properties 0 (length s) org-rm-props s)
  7553. (while (string-match org-bracket-link-regexp s)
  7554. (setq s (replace-match (if (match-end 2)
  7555. (match-string 3 s)
  7556. (match-string 1 s)) t t s)))
  7557. (let ((st (format " %s " s)))
  7558. (while (string-match org-emph-re st)
  7559. (setq st (replace-match (format " %s " (match-string 4 st)) t t st)))
  7560. (setq s (substring st 1 -1)))
  7561. s)
  7562. (defvar org-priority-regexp) ; defined later in the file
  7563. (defvar org-after-sorting-entries-or-items-hook nil
  7564. "Hook that is run after a bunch of entries or items have been sorted.
  7565. When children are sorted, the cursor is in the parent line when this
  7566. hook gets called. When a region or a plain list is sorted, the cursor
  7567. will be in the first entry of the sorted region/list.")
  7568. (defun org-sort-entries
  7569. (&optional with-case sorting-type getkey-func compare-func property)
  7570. "Sort entries on a certain level of an outline tree.
  7571. If there is an active region, the entries in the region are sorted.
  7572. Else, if the cursor is before the first entry, sort the top-level items.
  7573. Else, the children of the entry at point are sorted.
  7574. Sorting can be alphabetically, numerically, by date/time as given by
  7575. a time stamp, by a property, by priority order, or by a custom function.
  7576. The command prompts for the sorting type unless it has been given to the
  7577. function through the SORTING-TYPE argument, which needs to be a character,
  7578. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?o ?O ?r ?R ?f ?F). Here is the
  7579. precise meaning of each character:
  7580. n Numerically, by converting the beginning of the entry/item to a number.
  7581. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  7582. o By order of TODO keywords.
  7583. t By date/time, either the first active time stamp in the entry, or, if
  7584. none exist, by the first inactive one.
  7585. s By the scheduled date/time.
  7586. d By deadline date/time.
  7587. c By creation time, which is assumed to be the first inactive time stamp
  7588. at the beginning of a line.
  7589. p By priority according to the cookie.
  7590. r By the value of a property.
  7591. Capital letters will reverse the sort order.
  7592. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  7593. called with point at the beginning of the record. It must return either
  7594. a string or a number that should serve as the sorting key for that record.
  7595. Comparing entries ignores case by default. However, with an optional argument
  7596. WITH-CASE, the sorting considers case as well.
  7597. Sorting is done against the visible part of the headlines, it ignores hidden
  7598. links."
  7599. (interactive "P")
  7600. (let ((case-func (if with-case 'identity 'downcase))
  7601. (cmstr
  7602. ;; The clock marker is lost when using `sort-subr', let's
  7603. ;; store the clocking string.
  7604. (when (equal (marker-buffer org-clock-marker) (current-buffer))
  7605. (save-excursion
  7606. (goto-char org-clock-marker)
  7607. (looking-back "^.*") (match-string-no-properties 0))))
  7608. start beg end stars re re2
  7609. txt what tmp)
  7610. ;; Find beginning and end of region to sort
  7611. (cond
  7612. ((org-region-active-p)
  7613. ;; we will sort the region
  7614. (setq end (region-end)
  7615. what "region")
  7616. (goto-char (region-beginning))
  7617. (if (not (org-at-heading-p)) (outline-next-heading))
  7618. (setq start (point)))
  7619. ((or (org-at-heading-p)
  7620. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  7621. ;; we will sort the children of the current headline
  7622. (org-back-to-heading)
  7623. (setq start (point)
  7624. end (progn (org-end-of-subtree t t)
  7625. (or (bolp) (insert "\n"))
  7626. (org-back-over-empty-lines)
  7627. (point))
  7628. what "children")
  7629. (goto-char start)
  7630. (show-subtree)
  7631. (outline-next-heading))
  7632. (t
  7633. ;; we will sort the top-level entries in this file
  7634. (goto-char (point-min))
  7635. (or (org-at-heading-p) (outline-next-heading))
  7636. (setq start (point))
  7637. (goto-char (point-max))
  7638. (beginning-of-line 1)
  7639. (when (looking-at ".*?\\S-")
  7640. ;; File ends in a non-white line
  7641. (end-of-line 1)
  7642. (insert "\n"))
  7643. (setq end (point-max))
  7644. (setq what "top-level")
  7645. (goto-char start)
  7646. (show-all)))
  7647. (setq beg (point))
  7648. (if (>= beg end) (error "Nothing to sort"))
  7649. (looking-at "\\(\\*+\\)")
  7650. (setq stars (match-string 1)
  7651. re (concat "^" (regexp-quote stars) " +")
  7652. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  7653. txt (buffer-substring beg end))
  7654. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  7655. (if (and (not (equal stars "*")) (string-match re2 txt))
  7656. (error "Region to sort contains a level above the first entry"))
  7657. (unless sorting-type
  7658. (message
  7659. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  7660. [t]ime [s]cheduled [d]eadline [c]reated
  7661. A/N/P/R/O/F/T/S/D/C means reversed:"
  7662. what)
  7663. (setq sorting-type (read-char-exclusive))
  7664. (unless getkey-func
  7665. (and (= (downcase sorting-type) ?f)
  7666. (setq getkey-func
  7667. (org-icompleting-read "Sort using function: "
  7668. obarray 'fboundp t nil nil))
  7669. (setq getkey-func (intern getkey-func))))
  7670. (and (= (downcase sorting-type) ?r)
  7671. (not property)
  7672. (setq property
  7673. (org-icompleting-read "Property: "
  7674. (mapcar 'list (org-buffer-property-keys t))
  7675. nil t))))
  7676. (message "Sorting entries...")
  7677. (save-restriction
  7678. (narrow-to-region start end)
  7679. (let ((dcst (downcase sorting-type))
  7680. (case-fold-search nil)
  7681. (now (current-time)))
  7682. (sort-subr
  7683. (/= dcst sorting-type)
  7684. ;; This function moves to the beginning character of the "record" to
  7685. ;; be sorted.
  7686. (lambda nil
  7687. (if (re-search-forward re nil t)
  7688. (goto-char (match-beginning 0))
  7689. (goto-char (point-max))))
  7690. ;; This function moves to the last character of the "record" being
  7691. ;; sorted.
  7692. (lambda nil
  7693. (save-match-data
  7694. (condition-case nil
  7695. (outline-forward-same-level 1)
  7696. (error
  7697. (goto-char (point-max))))))
  7698. ;; This function returns the value that gets sorted against.
  7699. (lambda nil
  7700. (cond
  7701. ((= dcst ?n)
  7702. (if (looking-at org-complex-heading-regexp)
  7703. (string-to-number (org-sort-remove-invisible (match-string 4)))
  7704. nil))
  7705. ((= dcst ?a)
  7706. (if (looking-at org-complex-heading-regexp)
  7707. (funcall case-func (org-sort-remove-invisible (match-string 4)))
  7708. nil))
  7709. ((= dcst ?t)
  7710. (let ((end (save-excursion (outline-next-heading) (point))))
  7711. (if (or (re-search-forward org-ts-regexp end t)
  7712. (re-search-forward org-ts-regexp-both end t))
  7713. (org-time-string-to-seconds (match-string 0))
  7714. (org-float-time now))))
  7715. ((= dcst ?c)
  7716. (let ((end (save-excursion (outline-next-heading) (point))))
  7717. (if (re-search-forward
  7718. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  7719. end t)
  7720. (org-time-string-to-seconds (match-string 0))
  7721. (org-float-time now))))
  7722. ((= dcst ?s)
  7723. (let ((end (save-excursion (outline-next-heading) (point))))
  7724. (if (re-search-forward org-scheduled-time-regexp end t)
  7725. (org-time-string-to-seconds (match-string 1))
  7726. (org-float-time now))))
  7727. ((= dcst ?d)
  7728. (let ((end (save-excursion (outline-next-heading) (point))))
  7729. (if (re-search-forward org-deadline-time-regexp end t)
  7730. (org-time-string-to-seconds (match-string 1))
  7731. (org-float-time now))))
  7732. ((= dcst ?p)
  7733. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  7734. (string-to-char (match-string 2))
  7735. org-default-priority))
  7736. ((= dcst ?r)
  7737. (or (org-entry-get nil property) ""))
  7738. ((= dcst ?o)
  7739. (if (looking-at org-complex-heading-regexp)
  7740. (- 9999 (length (member (match-string 2)
  7741. org-todo-keywords-1)))))
  7742. ((= dcst ?f)
  7743. (if getkey-func
  7744. (progn
  7745. (setq tmp (funcall getkey-func))
  7746. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  7747. tmp)
  7748. (error "Invalid key function `%s'" getkey-func)))
  7749. (t (error "Invalid sorting type `%c'" sorting-type))))
  7750. nil
  7751. (cond
  7752. ((= dcst ?a) 'string<)
  7753. ((= dcst ?f) compare-func)
  7754. ((member dcst '(?p ?t ?s ?d ?c)) '<)))))
  7755. (run-hooks 'org-after-sorting-entries-or-items-hook)
  7756. ;; Reset the clock marker if needed
  7757. (when cmstr
  7758. (save-excursion
  7759. (goto-char start)
  7760. (search-forward cmstr nil t)
  7761. (move-marker org-clock-marker (point))))
  7762. (message "Sorting entries...done")))
  7763. (defun org-do-sort (table what &optional with-case sorting-type)
  7764. "Sort TABLE of WHAT according to SORTING-TYPE.
  7765. The user will be prompted for the SORTING-TYPE if the call to this
  7766. function does not specify it. WHAT is only for the prompt, to indicate
  7767. what is being sorted. The sorting key will be extracted from
  7768. the car of the elements of the table.
  7769. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  7770. (unless sorting-type
  7771. (message
  7772. "Sort %s: [a]lphabetic, [n]umeric, [t]ime. A/N/T means reversed:"
  7773. what)
  7774. (setq sorting-type (read-char-exclusive)))
  7775. (let ((dcst (downcase sorting-type))
  7776. extractfun comparefun)
  7777. ;; Define the appropriate functions
  7778. (cond
  7779. ((= dcst ?n)
  7780. (setq extractfun 'string-to-number
  7781. comparefun (if (= dcst sorting-type) '< '>)))
  7782. ((= dcst ?a)
  7783. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  7784. (lambda(x) (downcase (org-sort-remove-invisible x))))
  7785. comparefun (if (= dcst sorting-type)
  7786. 'string<
  7787. (lambda (a b) (and (not (string< a b))
  7788. (not (string= a b)))))))
  7789. ((= dcst ?t)
  7790. (setq extractfun
  7791. (lambda (x)
  7792. (if (or (string-match org-ts-regexp x)
  7793. (string-match org-ts-regexp-both x))
  7794. (org-float-time
  7795. (org-time-string-to-time (match-string 0 x)))
  7796. 0))
  7797. comparefun (if (= dcst sorting-type) '< '>)))
  7798. (t (error "Invalid sorting type `%c'" sorting-type)))
  7799. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  7800. table)
  7801. (lambda (a b) (funcall comparefun (car a) (car b))))))
  7802. ;;; The orgstruct minor mode
  7803. ;; Define a minor mode which can be used in other modes in order to
  7804. ;; integrate the org-mode structure editing commands.
  7805. ;; This is really a hack, because the org-mode structure commands use
  7806. ;; keys which normally belong to the major mode. Here is how it
  7807. ;; works: The minor mode defines all the keys necessary to operate the
  7808. ;; structure commands, but wraps the commands into a function which
  7809. ;; tests if the cursor is currently at a headline or a plain list
  7810. ;; item. If that is the case, the structure command is used,
  7811. ;; temporarily setting many Org-mode variables like regular
  7812. ;; expressions for filling etc. However, when any of those keys is
  7813. ;; used at a different location, function uses `key-binding' to look
  7814. ;; up if the key has an associated command in another currently active
  7815. ;; keymap (minor modes, major mode, global), and executes that
  7816. ;; command. There might be problems if any of the keys is otherwise
  7817. ;; used as a prefix key.
  7818. (defcustom orgstruct-heading-prefix-regexp nil
  7819. "Regexp that matches the custom prefix of Org headlines in
  7820. orgstruct(++)-mode."
  7821. :group 'org
  7822. :version "24.4"
  7823. :package-version '(Org . "8.0")
  7824. :type 'string)
  7825. ;;;###autoload(put 'orgstruct-heading-prefix-regexp 'safe-local-variable 'stringp)
  7826. (defcustom orgstruct-setup-hook nil
  7827. "Hook run after orgstruct-mode-map is filled."
  7828. :group 'org
  7829. :version "24.4"
  7830. :package-version '(Org . "8.0")
  7831. :type 'hook)
  7832. (defvar orgstruct-initialized nil)
  7833. (defvar org-local-vars nil
  7834. "List of local variables, for use by `orgstruct-mode'.")
  7835. ;;;###autoload
  7836. (define-minor-mode orgstruct-mode
  7837. "Toggle the minor mode `orgstruct-mode'.
  7838. This mode is for using Org-mode structure commands in other
  7839. modes. The following keys behave as if Org-mode were active, if
  7840. the cursor is on a headline, or on a plain list item (both as
  7841. defined by Org-mode)."
  7842. nil " OrgStruct" (make-sparse-keymap)
  7843. (funcall (if orgstruct-mode
  7844. 'add-to-invisibility-spec
  7845. 'remove-from-invisibility-spec)
  7846. '(outline . t))
  7847. (when orgstruct-mode
  7848. (org-load-modules-maybe)
  7849. (unless orgstruct-initialized
  7850. (orgstruct-setup)
  7851. (setq orgstruct-initialized t))))
  7852. ;;;###autoload
  7853. (defun turn-on-orgstruct ()
  7854. "Unconditionally turn on `orgstruct-mode'."
  7855. (orgstruct-mode 1))
  7856. (defvar org-fb-vars nil)
  7857. (make-variable-buffer-local 'org-fb-vars)
  7858. (defun orgstruct++-mode (&optional arg)
  7859. "Toggle `orgstruct-mode', the enhanced version of it.
  7860. In addition to setting orgstruct-mode, this also exports all
  7861. indentation and autofilling variables from org-mode into the
  7862. buffer. It will also recognize item context in multiline items."
  7863. (interactive "P")
  7864. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  7865. (if (< arg 1)
  7866. (progn (orgstruct-mode -1)
  7867. (mapc (lambda(v)
  7868. (org-set-local (car v)
  7869. (if (eq (car-safe (cadr v)) 'quote) (cadadr v) (cadr v))))
  7870. org-fb-vars))
  7871. (orgstruct-mode 1)
  7872. (setq org-fb-vars nil)
  7873. (let (var val)
  7874. (mapc
  7875. (lambda (x)
  7876. (when (string-match
  7877. "^\\(paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|fill-prefix\\|indent-\\)"
  7878. (symbol-name (car x)))
  7879. (setq var (car x) val (nth 1 x))
  7880. (push (list var `(quote ,(eval var))) org-fb-vars)
  7881. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  7882. org-local-vars)
  7883. (org-set-local 'orgstruct-is-++ t))))
  7884. (defvar orgstruct-is-++ nil
  7885. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  7886. (make-variable-buffer-local 'orgstruct-is-++)
  7887. ;;;###autoload
  7888. (defun turn-on-orgstruct++ ()
  7889. "Unconditionally turn on `orgstruct++-mode'."
  7890. (orgstruct++-mode 1))
  7891. (defun orgstruct-error ()
  7892. "Error when there is no default binding for a structure key."
  7893. (interactive)
  7894. (funcall (if (fboundp 'user-error)
  7895. 'user-error
  7896. 'error)
  7897. "This key has no function outside structure elements"))
  7898. (defun orgstruct-setup ()
  7899. "Setup orgstruct keymap."
  7900. (dolist (cell '((org-demote . t)
  7901. (org-metaleft . t)
  7902. (org-metaright . t)
  7903. (org-promote . t)
  7904. (org-shiftmetaleft . t)
  7905. (org-shiftmetaright . t)
  7906. org-backward-element
  7907. org-backward-heading-same-level
  7908. org-ctrl-c-ret
  7909. org-ctrl-c-minus
  7910. org-ctrl-c-star
  7911. org-cycle
  7912. org-forward-heading-same-level
  7913. org-insert-heading
  7914. org-insert-heading-respect-content
  7915. org-kill-note-or-show-branches
  7916. org-mark-subtree
  7917. org-meta-return
  7918. org-metadown
  7919. org-metaup
  7920. org-narrow-to-subtree
  7921. org-promote-subtree
  7922. org-reveal
  7923. org-shiftdown
  7924. org-shiftleft
  7925. org-shiftmetadown
  7926. org-shiftmetaup
  7927. org-shiftright
  7928. org-shifttab
  7929. org-shifttab
  7930. org-shiftup
  7931. org-show-subtree
  7932. org-sort
  7933. org-up-element
  7934. outline-demote
  7935. outline-next-visible-heading
  7936. outline-previous-visible-heading
  7937. outline-promote
  7938. outline-up-heading
  7939. show-children))
  7940. (let ((f (or (car-safe cell) cell))
  7941. (disable-when-heading-prefix (cdr-safe cell)))
  7942. (when (fboundp f)
  7943. (dolist (binding (nconc (where-is-internal f org-mode-map)
  7944. (where-is-internal f outline-mode-map)))
  7945. ;; TODO use local-function-key-map
  7946. (dolist (rep '(("<tab>" . "TAB")
  7947. ("<return>" . "RET")
  7948. ("<escape>" . "ESC")
  7949. ("<delete>" . "DEL")))
  7950. (setq binding (read-kbd-macro
  7951. (let ((case-fold-search))
  7952. (replace-regexp-in-string
  7953. (regexp-quote (cdr rep))
  7954. (car rep)
  7955. (key-description binding))))))
  7956. (let ((key (lookup-key orgstruct-mode-map binding)))
  7957. (when (or (not key) (numberp key))
  7958. (condition-case nil
  7959. (org-defkey orgstruct-mode-map
  7960. binding
  7961. (orgstruct-make-binding f binding disable-when-heading-prefix))
  7962. (error nil))))))))
  7963. (run-hooks 'orgstruct-setup-hook))
  7964. (defun orgstruct-make-binding (fun key disable-when-heading-prefix)
  7965. "Create a function for binding in the structure minor mode.
  7966. FUN is the command to call inside a table. KEY is the key that
  7967. should be checked in for a command to execute outside of tables.
  7968. Non-nil DISABLE-WHEN-HEADING-PREFIX means to disable the command
  7969. if `orgstruct-heading-prefix-regexp' is non-nil."
  7970. (let ((name (concat "orgstruct-hijacker-" (symbol-name fun))))
  7971. (let ((nname name)
  7972. (i 0))
  7973. (while (fboundp (intern nname))
  7974. (setq nname (format "%s-%d" name (setq i (1+ i)))))
  7975. (setq name (intern nname)))
  7976. (eval
  7977. (let ((bindings '((org-heading-regexp
  7978. (concat "^"
  7979. orgstruct-heading-prefix-regexp
  7980. "\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ ]*$"))
  7981. (org-outline-regexp
  7982. (concat orgstruct-heading-prefix-regexp "\\*+ "))
  7983. (org-outline-regexp-bol
  7984. (concat "^" org-outline-regexp))
  7985. (outline-regexp org-outline-regexp)
  7986. (outline-heading-end-regexp "\n")
  7987. (outline-level 'org-outline-level)
  7988. (outline-heading-alist))))
  7989. `(defun ,name (arg)
  7990. ,(concat "In Structure, run `" (symbol-name fun) "'.\n"
  7991. "Outside of structure, run the binding of `"
  7992. (key-description key) "'."
  7993. (when disable-when-heading-prefix
  7994. (concat
  7995. "\nIf `orgstruct-heading-prefix-regexp' is non-nil, this command will always fall\n"
  7996. "back to the default binding due to limitations of Org's implementation of\n"
  7997. "`" (symbol-name fun) "'.")))
  7998. (interactive "p")
  7999. (let* ((disable
  8000. ,(when disable-when-heading-prefix
  8001. '(and orgstruct-heading-prefix-regexp
  8002. (not (string= orgstruct-heading-prefix-regexp "")))))
  8003. (fallback
  8004. (or disable
  8005. (not
  8006. (let* ,bindings
  8007. (org-context-p 'headline 'item
  8008. ,(when (memq fun '(org-insert-heading))
  8009. '(when orgstruct-is-++
  8010. 'item-body))))))))
  8011. (if fallback
  8012. (let* ((orgstruct-mode)
  8013. (binding
  8014. (loop with key = ,key
  8015. for rep in '(nil
  8016. ("<tab>" . "TAB")
  8017. ("<return>" . "RET")
  8018. ("<escape>" . "ESC")
  8019. ("<delete>" . "DEL"))
  8020. do
  8021. (when rep
  8022. (setq key (read-kbd-macro
  8023. (let ((case-fold-search))
  8024. (replace-regexp-in-string
  8025. (regexp-quote (car rep))
  8026. (cdr rep)
  8027. (key-description key))))))
  8028. thereis (key-binding key))))
  8029. (if (keymapp binding)
  8030. (set-temporary-overlay-map binding)
  8031. (let ((func (or binding
  8032. (unless disable
  8033. 'orgstruct-error))))
  8034. (when func
  8035. (call-interactively func)))))
  8036. (org-run-like-in-org-mode
  8037. (lambda ()
  8038. (interactive)
  8039. (let* ,bindings
  8040. (call-interactively ',fun)))))))))
  8041. name))
  8042. (defun org-contextualize-keys (alist contexts)
  8043. "Return valid elements in ALIST depending on CONTEXTS.
  8044. `org-agenda-custom-commands' or `org-capture-templates' are the
  8045. values used for ALIST, and `org-agenda-custom-commands-contexts'
  8046. or `org-capture-templates-contexts' are the associated contexts
  8047. definitions."
  8048. (let ((contexts
  8049. ;; normalize contexts
  8050. (mapcar
  8051. (lambda(c) (cond ((listp (cadr c))
  8052. (list (car c) (car c) (cadr c)))
  8053. ((string= "" (cadr c))
  8054. (list (car c) (car c) (caddr c)))
  8055. (t c))) contexts))
  8056. (a alist) c r s)
  8057. ;; loop over all commands or templates
  8058. (while (setq c (pop a))
  8059. (let (vrules repl)
  8060. (cond
  8061. ((not (assoc (car c) contexts))
  8062. (push c r))
  8063. ((and (assoc (car c) contexts)
  8064. (setq vrules (org-contextualize-validate-key
  8065. (car c) contexts)))
  8066. (mapc (lambda (vr)
  8067. (when (not (equal (car vr) (cadr vr)))
  8068. (setq repl vr))) vrules)
  8069. (if (not repl) (push c r)
  8070. (push (cadr repl) s)
  8071. (push
  8072. (cons (car c)
  8073. (cdr (or (assoc (cadr repl) alist)
  8074. (error "Undefined key `%s' as contextual replacement for `%s'"
  8075. (cadr repl) (car c)))))
  8076. r))))))
  8077. ;; Return limited ALIST, possibly with keys modified, and deduplicated
  8078. (delq
  8079. nil
  8080. (delete-dups
  8081. (mapcar (lambda (x)
  8082. (let ((tpl (car x)))
  8083. (when (not (delq
  8084. nil
  8085. (mapcar (lambda(y)
  8086. (equal y tpl)) s))) x)))
  8087. (reverse r))))))
  8088. (defun org-contextualize-validate-key (key contexts)
  8089. "Check CONTEXTS for agenda or capture KEY."
  8090. (let (r rr res)
  8091. (while (setq r (pop contexts))
  8092. (mapc
  8093. (lambda (rr)
  8094. (when
  8095. (and (equal key (car r))
  8096. (if (functionp rr) (funcall rr)
  8097. (or (and (eq (car rr) 'in-file)
  8098. (buffer-file-name)
  8099. (string-match (cdr rr) (buffer-file-name)))
  8100. (and (eq (car rr) 'in-mode)
  8101. (string-match (cdr rr) (symbol-name major-mode)))
  8102. (and (eq (car rr) 'in-buffer)
  8103. (string-match (cdr rr) (buffer-name)))
  8104. (when (and (eq (car rr) 'not-in-file)
  8105. (buffer-file-name))
  8106. (not (string-match (cdr rr) (buffer-file-name))))
  8107. (when (eq (car rr) 'not-in-mode)
  8108. (not (string-match (cdr rr) (symbol-name major-mode))))
  8109. (when (eq (car rr) 'not-in-buffer)
  8110. (not (string-match (cdr rr) (buffer-name)))))))
  8111. (push r res)))
  8112. (car (last r))))
  8113. (delete-dups (delq nil res))))
  8114. (defun org-context-p (&rest contexts)
  8115. "Check if local context is any of CONTEXTS.
  8116. Possible values in the list of contexts are `table', `headline', and `item'."
  8117. (let ((pos (point)))
  8118. (goto-char (point-at-bol))
  8119. (prog1 (or (and (memq 'table contexts)
  8120. (looking-at "[ \t]*|"))
  8121. (and (memq 'headline contexts)
  8122. (looking-at org-outline-regexp))
  8123. (and (memq 'item contexts)
  8124. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  8125. (and (memq 'item-body contexts)
  8126. (org-in-item-p)))
  8127. (goto-char pos))))
  8128. (defun org-get-local-variables ()
  8129. "Return a list of all local variables in an Org mode buffer."
  8130. (let (varlist)
  8131. (with-current-buffer (get-buffer-create "*Org tmp*")
  8132. (erase-buffer)
  8133. (org-mode)
  8134. (setq varlist (buffer-local-variables)))
  8135. (kill-buffer "*Org tmp*")
  8136. (delq nil
  8137. (mapcar
  8138. (lambda (x)
  8139. (setq x
  8140. (if (symbolp x)
  8141. (list x)
  8142. (list (car x) (cdr x))))
  8143. (if (and (not (get (car x) 'org-state))
  8144. (string-match
  8145. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|indent-\\)"
  8146. (symbol-name (car x))))
  8147. x nil))
  8148. varlist))))
  8149. (defun org-clone-local-variables (from-buffer &optional regexp)
  8150. "Clone local variables from FROM-BUFFER.
  8151. Optional argument REGEXP selects variables to clone."
  8152. (mapc
  8153. (lambda (pair)
  8154. (and (symbolp (car pair))
  8155. (or (null regexp)
  8156. (string-match regexp (symbol-name (car pair))))
  8157. (set (make-local-variable (car pair))
  8158. (cdr pair))))
  8159. (buffer-local-variables from-buffer)))
  8160. ;;;###autoload
  8161. (defun org-run-like-in-org-mode (cmd)
  8162. "Run a command, pretending that the current buffer is in Org-mode.
  8163. This will temporarily bind local variables that are typically bound in
  8164. Org-mode to the values they have in Org-mode, and then interactively
  8165. call CMD."
  8166. (org-load-modules-maybe)
  8167. (unless org-local-vars
  8168. (setq org-local-vars (org-get-local-variables)))
  8169. (let (binds)
  8170. (dolist (var org-local-vars)
  8171. (when (or (not (boundp (car var)))
  8172. (eq (symbol-value (car var))
  8173. (default-value (car var))))
  8174. (push (list (car var) `(quote ,(cadr var))) binds)))
  8175. (eval `(let ,binds
  8176. (call-interactively (quote ,cmd))))))
  8177. ;;;; Archiving
  8178. (defun org-get-category (&optional pos force-refresh)
  8179. "Get the category applying to position POS."
  8180. (save-match-data
  8181. (if force-refresh (org-refresh-category-properties))
  8182. (let ((pos (or pos (point))))
  8183. (or (get-text-property pos 'org-category)
  8184. (progn (org-refresh-category-properties)
  8185. (get-text-property pos 'org-category))))))
  8186. (defun org-refresh-category-properties ()
  8187. "Refresh category text properties in the buffer."
  8188. (let ((case-fold-search t)
  8189. (inhibit-read-only t)
  8190. (def-cat (cond
  8191. ((null org-category)
  8192. (if buffer-file-name
  8193. (file-name-sans-extension
  8194. (file-name-nondirectory buffer-file-name))
  8195. "???"))
  8196. ((symbolp org-category) (symbol-name org-category))
  8197. (t org-category)))
  8198. beg end cat pos optionp)
  8199. (org-with-silent-modifications
  8200. (save-excursion
  8201. (save-restriction
  8202. (widen)
  8203. (goto-char (point-min))
  8204. (put-text-property (point) (point-max) 'org-category def-cat)
  8205. (while (re-search-forward
  8206. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  8207. (setq pos (match-end 0)
  8208. optionp (equal (char-after (match-beginning 0)) ?#)
  8209. cat (org-trim (match-string 2)))
  8210. (if optionp
  8211. (setq beg (point-at-bol) end (point-max))
  8212. (org-back-to-heading t)
  8213. (setq beg (point) end (org-end-of-subtree t t)))
  8214. (put-text-property beg end 'org-category cat)
  8215. (put-text-property beg end 'org-category-position beg)
  8216. (goto-char pos)))))))
  8217. (defun org-refresh-properties (dprop tprop)
  8218. "Refresh buffer text properties.
  8219. DPROP is the drawer property and TPROP is the corresponding text
  8220. property to set."
  8221. (let ((case-fold-search t)
  8222. (inhibit-read-only t) p)
  8223. (org-with-silent-modifications
  8224. (save-excursion
  8225. (save-restriction
  8226. (widen)
  8227. (goto-char (point-min))
  8228. (while (re-search-forward (concat "^[ \t]*:" dprop ": +\\(.*\\)[ \t]*$") nil t)
  8229. (setq p (org-match-string-no-properties 1))
  8230. (save-excursion
  8231. (org-back-to-heading t)
  8232. (put-text-property
  8233. (point-at-bol) (point-at-eol) tprop p))))))))
  8234. ;;;; Link Stuff
  8235. ;;; Link abbreviations
  8236. (defun org-link-expand-abbrev (link)
  8237. "Apply replacements as defined in `org-link-abbrev-alist'."
  8238. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  8239. (let* ((key (match-string 1 link))
  8240. (as (or (assoc key org-link-abbrev-alist-local)
  8241. (assoc key org-link-abbrev-alist)))
  8242. (tag (and (match-end 2) (match-string 3 link)))
  8243. rpl)
  8244. (if (not as)
  8245. link
  8246. (setq rpl (cdr as))
  8247. (cond
  8248. ((symbolp rpl) (funcall rpl tag))
  8249. ((string-match "%(\\([^)]+\\))" rpl)
  8250. (replace-match (funcall (intern-soft (match-string 1 rpl)) tag) t t rpl))
  8251. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  8252. ((string-match "%h" rpl)
  8253. (replace-match (url-hexify-string (or tag "")) t t rpl))
  8254. (t (concat rpl tag)))))
  8255. link))
  8256. ;;; Storing and inserting links
  8257. (defvar org-insert-link-history nil
  8258. "Minibuffer history for links inserted with `org-insert-link'.")
  8259. (defvar org-stored-links nil
  8260. "Contains the links stored with `org-store-link'.")
  8261. (defvar org-store-link-plist nil
  8262. "Plist with info about the most recently link created with `org-store-link'.")
  8263. (defvar org-link-protocols nil
  8264. "Link protocols added to Org-mode using `org-add-link-type'.")
  8265. (defvar org-store-link-functions nil
  8266. "List of functions that are called to create and store a link.
  8267. Each function will be called in turn until one returns a non-nil
  8268. value. Each function should check if it is responsible for creating
  8269. this link (for example by looking at the major mode).
  8270. If not, it must exit and return nil.
  8271. If yes, it should return a non-nil value after a calling
  8272. `org-store-link-props' with a list of properties and values.
  8273. Special properties are:
  8274. :type The link prefix, like \"http\". This must be given.
  8275. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  8276. This is obligatory as well.
  8277. :description Optional default description for the second pair
  8278. of brackets in an Org-mode link. The user can still change
  8279. this when inserting this link into an Org-mode buffer.
  8280. In addition to these, any additional properties can be specified
  8281. and then used in capture templates.")
  8282. (defun org-add-link-type (type &optional follow export)
  8283. "Add TYPE to the list of `org-link-types'.
  8284. Re-compute all regular expressions depending on `org-link-types'
  8285. FOLLOW and EXPORT are two functions.
  8286. FOLLOW should take the link path as the single argument and do whatever
  8287. is necessary to follow the link, for example find a file or display
  8288. a mail message.
  8289. EXPORT should format the link path for export to one of the export formats.
  8290. It should be a function accepting three arguments:
  8291. path the path of the link, the text after the prefix (like \"http:\")
  8292. desc the description of the link, if any, or a description added by
  8293. org-export-normalize-links if there is none
  8294. format the export format, a symbol like `html' or `latex' or `ascii'..
  8295. The function may use the FORMAT information to return different values
  8296. depending on the format. The return value will be put literally into
  8297. the exported file. If the return value is nil, this means Org should
  8298. do what it normally does with links which do not have EXPORT defined.
  8299. Org-mode has a built-in default for exporting links. If you are happy with
  8300. this default, there is no need to define an export function for the link
  8301. type. For a simple example of an export function, see `org-bbdb.el'."
  8302. (add-to-list 'org-link-types type t)
  8303. (org-make-link-regexps)
  8304. (if (assoc type org-link-protocols)
  8305. (setcdr (assoc type org-link-protocols) (list follow export))
  8306. (push (list type follow export) org-link-protocols)))
  8307. (defvar org-agenda-buffer-name) ; Defined in org-agenda.el
  8308. (defvar org-id-link-to-org-use-id) ; Defined in org-id.el
  8309. ;;;###autoload
  8310. (defun org-store-link (arg &optional ignore-region)
  8311. "\\<org-mode-map>Store an org-link to the current location.
  8312. This link is added to `org-stored-links' and can later be inserted
  8313. into an org-buffer with \\[org-insert-link].
  8314. For some link types, a prefix arg is interpreted.
  8315. For links to Usenet articles, arg negates `org-gnus-prefer-web-links'.
  8316. For file links, arg negates `org-context-in-file-links'.
  8317. A double prefix arg force skipping storing functions that are not
  8318. part of Org's core.
  8319. When the region is active and IGNORE-REGION is nil, store each line
  8320. in the region as a separate link."
  8321. (interactive "P")
  8322. (org-load-modules-maybe)
  8323. (setq org-store-link-plist nil) ; reset
  8324. (if (and (org-region-active-p) (not ignore-region))
  8325. (save-excursion
  8326. (let ((beg (region-beginning)) (end (region-end)))
  8327. (goto-char beg)
  8328. (while (< (point-at-eol) end)
  8329. (funcall 'org-store-link arg t)
  8330. (move-beginning-of-line 2))))
  8331. (org-with-limited-levels
  8332. (let (link cpltxt desc description search txt custom-id agenda-link sfuns sfunsn)
  8333. (cond
  8334. ((and (not (equal arg '(16)))
  8335. (setq sfuns
  8336. (delq
  8337. nil (mapcar (lambda (f) (let (fs) (if (funcall f) (push f fs))))
  8338. org-store-link-functions))
  8339. sfunsn (mapcar (lambda (fu) (symbol-name (car fu))) sfuns))
  8340. (or (and (cdr sfuns)
  8341. (funcall (intern
  8342. (completing-read "Which function for creating the link? "
  8343. sfunsn t (car sfunsn)))))
  8344. (funcall (caar sfuns)))
  8345. (setq link (plist-get org-store-link-plist :link)
  8346. desc (or (plist-get org-store-link-plist :description) link))))
  8347. ((org-src-edit-buffer-p)
  8348. (let (label gc)
  8349. (while (or (not label)
  8350. (save-excursion
  8351. (save-restriction
  8352. (widen)
  8353. (goto-char (point-min))
  8354. (re-search-forward
  8355. (regexp-quote (format org-coderef-label-format label))
  8356. nil t))))
  8357. (when label (message "Label exists already") (sit-for 2))
  8358. (setq label (read-string "Code line label: " label)))
  8359. (end-of-line 1)
  8360. (setq link (format org-coderef-label-format label))
  8361. (setq gc (- 79 (length link)))
  8362. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  8363. (insert link)
  8364. (setq link (concat "(" label ")") desc nil)))
  8365. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  8366. ;; We are in the agenda, link to referenced location
  8367. (let ((m (or (get-text-property (point) 'org-hd-marker)
  8368. (get-text-property (point) 'org-marker))))
  8369. (when m
  8370. (org-with-point-at m
  8371. (setq agenda-link
  8372. (if (org-called-interactively-p 'any)
  8373. (call-interactively 'org-store-link)
  8374. (org-store-link nil)))))))
  8375. ((eq major-mode 'calendar-mode)
  8376. (let ((cd (calendar-cursor-to-date)))
  8377. (setq link
  8378. (format-time-string
  8379. (car org-time-stamp-formats)
  8380. (apply 'encode-time
  8381. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  8382. nil nil nil))))
  8383. (org-store-link-props :type "calendar" :date cd)))
  8384. ((eq major-mode 'help-mode)
  8385. (setq link (concat "help:" (save-excursion
  8386. (goto-char (point-min))
  8387. (looking-at "^[^ ]+")
  8388. (match-string 0))))
  8389. (org-store-link-props :type "help"))
  8390. ((eq major-mode 'w3-mode)
  8391. (setq cpltxt (if (and (buffer-name)
  8392. (not (string-match "Untitled" (buffer-name))))
  8393. (buffer-name)
  8394. (url-view-url t))
  8395. link (url-view-url t))
  8396. (org-store-link-props :type "w3" :url (url-view-url t)))
  8397. ((setq search (run-hook-with-args-until-success
  8398. 'org-create-file-search-functions))
  8399. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  8400. "::" search))
  8401. (setq cpltxt (or description link)))
  8402. ((eq major-mode 'image-mode)
  8403. (setq cpltxt (concat "file:"
  8404. (abbreviate-file-name buffer-file-name))
  8405. link cpltxt)
  8406. (org-store-link-props :type "image" :file buffer-file-name))
  8407. ((eq major-mode 'dired-mode)
  8408. ;; link to the file in the current line
  8409. (let ((file (dired-get-filename nil t)))
  8410. (setq file (if file
  8411. (abbreviate-file-name
  8412. (expand-file-name (dired-get-filename nil t)))
  8413. ;; otherwise, no file so use current directory.
  8414. default-directory))
  8415. (setq cpltxt (concat "file:" file)
  8416. link cpltxt)))
  8417. ((and (buffer-file-name (buffer-base-buffer)) (derived-mode-p 'org-mode))
  8418. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  8419. (cond
  8420. ((org-in-regexp "<<\\(.*?\\)>>")
  8421. (setq cpltxt
  8422. (concat "file:"
  8423. (abbreviate-file-name
  8424. (buffer-file-name (buffer-base-buffer)))
  8425. "::" (match-string 1))
  8426. link cpltxt))
  8427. ((and (featurep 'org-id)
  8428. (or (eq org-id-link-to-org-use-id t)
  8429. (and (org-called-interactively-p 'any)
  8430. (or (eq org-id-link-to-org-use-id 'create-if-interactive)
  8431. (and (eq org-id-link-to-org-use-id
  8432. 'create-if-interactive-and-no-custom-id)
  8433. (not custom-id))))
  8434. (and org-id-link-to-org-use-id (org-entry-get nil "ID"))))
  8435. ;; We can make a link using the ID.
  8436. (setq link (condition-case nil
  8437. (prog1 (org-id-store-link)
  8438. (setq desc (plist-get org-store-link-plist :description)))
  8439. (error
  8440. ;; probably before first headline, link to file only
  8441. (concat "file:"
  8442. (abbreviate-file-name
  8443. (buffer-file-name (buffer-base-buffer))))))))
  8444. (t
  8445. ;; Just link to current headline
  8446. (setq cpltxt (concat "file:"
  8447. (abbreviate-file-name
  8448. (buffer-file-name (buffer-base-buffer)))))
  8449. ;; Add a context search string
  8450. (when (org-xor org-context-in-file-links arg)
  8451. (let* ((ee (org-element-at-point))
  8452. (et (org-element-type ee))
  8453. (ev (plist-get (cadr ee) :value)))
  8454. (setq txt (cond
  8455. ((org-at-heading-p) nil)
  8456. ((eq et 'keyword) ev)
  8457. ((org-region-active-p)
  8458. (buffer-substring (region-beginning) (region-end)))))
  8459. (when (or (null txt) (string-match "\\S-" txt))
  8460. (setq cpltxt
  8461. (concat cpltxt "::"
  8462. (condition-case nil
  8463. (org-make-org-heading-search-string txt)
  8464. (error "")))
  8465. desc (or (and (eq et 'keyword) ev)
  8466. (nth 4 (ignore-errors (org-heading-components)))
  8467. "NONE")))))
  8468. (if (string-match "::\\'" cpltxt)
  8469. (setq cpltxt (substring cpltxt 0 -2)))
  8470. (setq link cpltxt))))
  8471. ((buffer-file-name (buffer-base-buffer))
  8472. ;; Just link to this file here.
  8473. (setq cpltxt (concat "file:"
  8474. (abbreviate-file-name
  8475. (buffer-file-name (buffer-base-buffer)))))
  8476. ;; Add a context string
  8477. (when (org-xor org-context-in-file-links arg)
  8478. (setq txt (if (org-region-active-p)
  8479. (buffer-substring (region-beginning) (region-end))
  8480. (buffer-substring (point-at-bol) (point-at-eol))))
  8481. ;; Only use search option if there is some text.
  8482. (when (string-match "\\S-" txt)
  8483. (setq cpltxt
  8484. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  8485. desc "NONE")))
  8486. (setq link cpltxt))
  8487. ((org-called-interactively-p 'interactive)
  8488. (user-error "No method for storing a link from this buffer"))
  8489. (t (setq link nil)))
  8490. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  8491. (setq link (or link cpltxt)
  8492. desc (or desc cpltxt))
  8493. (cond ((equal desc "NONE") (setq desc nil))
  8494. ((string-match org-bracket-link-regexp desc)
  8495. (setq desc (replace-regexp-in-string
  8496. org-bracket-link-regexp
  8497. (concat "\\3" (if (equal (length (match-string 0 desc))
  8498. (length desc)) "*" "")) desc))))
  8499. (if (and (or (org-called-interactively-p 'any)
  8500. executing-kbd-macro ignore-region) link)
  8501. (progn
  8502. (setq org-stored-links
  8503. (cons (list link desc) org-stored-links))
  8504. (message "Stored: %s" (or desc link))
  8505. (when custom-id
  8506. (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
  8507. "::#" custom-id))
  8508. (setq org-stored-links
  8509. (cons (list link desc) org-stored-links))))
  8510. (or agenda-link (and link (org-make-link-string link desc))))))))
  8511. (defun org-store-link-props (&rest plist)
  8512. "Store link properties, extract names and addresses."
  8513. (let (x adr)
  8514. (when (setq x (plist-get plist :from))
  8515. (setq adr (mail-extract-address-components x))
  8516. (setq plist (plist-put plist :fromname (car adr)))
  8517. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  8518. (when (setq x (plist-get plist :to))
  8519. (setq adr (mail-extract-address-components x))
  8520. (setq plist (plist-put plist :toname (car adr)))
  8521. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  8522. (let ((from (plist-get plist :from))
  8523. (to (plist-get plist :to)))
  8524. (when (and from to org-from-is-user-regexp)
  8525. (setq plist
  8526. (plist-put plist :fromto
  8527. (if (string-match org-from-is-user-regexp from)
  8528. (concat "to %t")
  8529. (concat "from %f"))))))
  8530. (setq org-store-link-plist plist))
  8531. (defun org-add-link-props (&rest plist)
  8532. "Add these properties to the link property list."
  8533. (let (key value)
  8534. (while plist
  8535. (setq key (pop plist) value (pop plist))
  8536. (setq org-store-link-plist
  8537. (plist-put org-store-link-plist key value)))))
  8538. (defun org-email-link-description (&optional fmt)
  8539. "Return the description part of an email link.
  8540. This takes information from `org-store-link-plist' and formats it
  8541. according to FMT (default from `org-email-link-description-format')."
  8542. (setq fmt (or fmt org-email-link-description-format))
  8543. (let* ((p org-store-link-plist)
  8544. (to (plist-get p :toaddress))
  8545. (from (plist-get p :fromaddress))
  8546. (table
  8547. (list
  8548. (cons "%c" (plist-get p :fromto))
  8549. (cons "%F" (plist-get p :from))
  8550. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  8551. (cons "%T" (plist-get p :to))
  8552. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  8553. (cons "%s" (plist-get p :subject))
  8554. (cons "%d" (plist-get p :date))
  8555. (cons "%m" (plist-get p :message-id)))))
  8556. (when (string-match "%c" fmt)
  8557. ;; Check if the user wrote this message
  8558. (if (and org-from-is-user-regexp from to
  8559. (save-match-data (string-match org-from-is-user-regexp from)))
  8560. (setq fmt (replace-match "to %t" t t fmt))
  8561. (setq fmt (replace-match "from %f" t t fmt))))
  8562. (org-replace-escapes fmt table)))
  8563. (defun org-make-org-heading-search-string (&optional string)
  8564. "Make search string for the current headline or STRING."
  8565. (let ((s (or string
  8566. (and (derived-mode-p 'org-mode)
  8567. (save-excursion
  8568. (org-back-to-heading t)
  8569. (org-element-property :raw-value (org-element-at-point))))))
  8570. (lines org-context-in-file-links))
  8571. (or string (setq s (concat "*" s))) ; Add * for headlines
  8572. (setq s (replace-regexp-in-string "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" "" s))
  8573. (when (and string (integerp lines) (> lines 0))
  8574. (let ((slines (org-split-string s "\n")))
  8575. (when (< lines (length slines))
  8576. (setq s (mapconcat
  8577. 'identity
  8578. (reverse (nthcdr (- (length slines) lines)
  8579. (reverse slines))) "\n")))))
  8580. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  8581. (defun org-make-link-string (link &optional description)
  8582. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  8583. (unless (string-match "\\S-" link)
  8584. (error "Empty link"))
  8585. (when (and description
  8586. (stringp description)
  8587. (not (string-match "\\S-" description)))
  8588. (setq description nil))
  8589. (when (stringp description)
  8590. ;; Remove brackets from the description, they are fatal.
  8591. (while (string-match "\\[" description)
  8592. (setq description (replace-match "{" t t description)))
  8593. (while (string-match "\\]" description)
  8594. (setq description (replace-match "}" t t description))))
  8595. (when (equal link description)
  8596. ;; No description needed, it is identical
  8597. (setq description nil))
  8598. (when (and (not description)
  8599. (not (string-match (org-image-file-name-regexp) link))
  8600. (not (equal link (org-link-escape link))))
  8601. (setq description (org-extract-attributes link)))
  8602. (setq link
  8603. (cond ((string-match (org-image-file-name-regexp) link) link)
  8604. ((string-match org-link-types-re link)
  8605. (concat (match-string 1 link)
  8606. (org-link-escape (substring link (match-end 1)))))
  8607. (t (org-link-escape link))))
  8608. (concat "[[" link "]"
  8609. (if description (concat "[" description "]") "")
  8610. "]"))
  8611. (defconst org-link-escape-chars
  8612. '(?\ ?\[ ?\] ?\; ?\= ?\+)
  8613. "List of characters that should be escaped in link.
  8614. This is the list that is used for internal purposes.")
  8615. (defconst org-link-escape-chars-browser
  8616. '(?\ )
  8617. "List of escapes for characters that are problematic in links.
  8618. This is the list that is used before handing over to the browser.")
  8619. (defun org-link-escape (text &optional table merge)
  8620. "Return percent escaped representation of TEXT.
  8621. TEXT is a string with the text to escape.
  8622. Optional argument TABLE is a list with characters that should be
  8623. escaped. When nil, `org-link-escape-chars' is used.
  8624. If optional argument MERGE is set, merge TABLE into
  8625. `org-link-escape-chars'."
  8626. (cond
  8627. ((and table merge)
  8628. (mapc (lambda (defchr)
  8629. (unless (member defchr table)
  8630. (setq table (cons defchr table)))) org-link-escape-chars))
  8631. ((null table)
  8632. (setq table org-link-escape-chars)))
  8633. (mapconcat
  8634. (lambda (char)
  8635. (if (or (member char table)
  8636. (and (or (< char 32) (= char 37) (> char 126))
  8637. org-url-hexify-p))
  8638. (mapconcat (lambda (sequence-element)
  8639. (format "%%%.2X" sequence-element))
  8640. (or (encode-coding-char char 'utf-8)
  8641. (error "Unable to percent escape character: %s"
  8642. (char-to-string char))) "")
  8643. (char-to-string char))) text ""))
  8644. (defun org-link-unescape (str)
  8645. "Unhex hexified Unicode strings as returned from the JavaScript function
  8646. encodeURIComponent. E.g. `%C3%B6' is the german o-Umlaut."
  8647. (unless (and (null str) (string= "" str))
  8648. (let ((pos 0) (case-fold-search t) unhexed)
  8649. (while (setq pos (string-match "\\(%[0-9a-f][0-9a-f]\\)+" str pos))
  8650. (setq unhexed (org-link-unescape-compound (match-string 0 str)))
  8651. (setq str (replace-match unhexed t t str))
  8652. (setq pos (+ pos (length unhexed))))))
  8653. str)
  8654. (defun org-link-unescape-compound (hex)
  8655. "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German o-Umlaut.
  8656. Note: this function also decodes single byte encodings like
  8657. `%E1' (a-acute) if not followed by another `%[A-F0-9]{2}' group."
  8658. (save-match-data
  8659. (let* ((bytes (cdr (split-string hex "%")))
  8660. (ret "")
  8661. (eat 0)
  8662. (sum 0))
  8663. (while bytes
  8664. (let* ((val (string-to-number (pop bytes) 16))
  8665. (shift-xor
  8666. (if (= 0 eat)
  8667. (cond
  8668. ((>= val 252) (cons 6 252))
  8669. ((>= val 248) (cons 5 248))
  8670. ((>= val 240) (cons 4 240))
  8671. ((>= val 224) (cons 3 224))
  8672. ((>= val 192) (cons 2 192))
  8673. (t (cons 0 0)))
  8674. (cons 6 128))))
  8675. (if (>= val 192) (setq eat (car shift-xor)))
  8676. (setq val (logxor val (cdr shift-xor)))
  8677. (setq sum (+ (lsh sum (car shift-xor)) val))
  8678. (if (> eat 0) (setq eat (- eat 1)))
  8679. (cond
  8680. ((= 0 eat) ;multi byte
  8681. (setq ret (concat ret (org-char-to-string sum)))
  8682. (setq sum 0))
  8683. ((not bytes) ; single byte(s)
  8684. (setq ret (org-link-unescape-single-byte-sequence hex))))
  8685. )) ;; end (while bytes
  8686. ret )))
  8687. (defun org-link-unescape-single-byte-sequence (hex)
  8688. "Unhexify hex-encoded single byte character sequences."
  8689. (mapconcat (lambda (byte)
  8690. (char-to-string (string-to-number byte 16)))
  8691. (cdr (split-string hex "%")) ""))
  8692. (defun org-xor (a b)
  8693. "Exclusive or."
  8694. (if a (not b) b))
  8695. (defun org-fixup-message-id-for-http (s)
  8696. "Replace special characters in a message id, so it can be used in an http query."
  8697. (when (string-match "%" s)
  8698. (setq s (mapconcat (lambda (c)
  8699. (if (eq c ?%)
  8700. "%25"
  8701. (char-to-string c)))
  8702. s "")))
  8703. (while (string-match "<" s)
  8704. (setq s (replace-match "%3C" t t s)))
  8705. (while (string-match ">" s)
  8706. (setq s (replace-match "%3E" t t s)))
  8707. (while (string-match "@" s)
  8708. (setq s (replace-match "%40" t t s)))
  8709. s)
  8710. (defun org-link-prettify (link)
  8711. "Return a human-readable representation of LINK.
  8712. The car of LINK must be a raw link the cdr of LINK must be either
  8713. a link description or nil."
  8714. (let ((desc (or (cadr link) "<no description>")))
  8715. (concat (format "%-45s" (substring desc 0 (min (length desc) 40)))
  8716. "<" (car link) ">")))
  8717. ;;;###autoload
  8718. (defun org-insert-link-global ()
  8719. "Insert a link like Org-mode does.
  8720. This command can be called in any mode to insert a link in Org-mode syntax."
  8721. (interactive)
  8722. (org-load-modules-maybe)
  8723. (org-run-like-in-org-mode 'org-insert-link))
  8724. (defun org-insert-all-links (&optional keep)
  8725. "Insert all links in `org-stored-links'."
  8726. (interactive "P")
  8727. (let ((links (copy-sequence org-stored-links)) l)
  8728. (while (setq l (if keep (pop links) (pop org-stored-links)))
  8729. (insert "- ")
  8730. (org-insert-link nil (car l) (or (cadr l) "<no description>"))
  8731. (insert "\n"))))
  8732. (defun org-link-fontify-links-to-this-file ()
  8733. "Fontify links to the current file in `org-stored-links'."
  8734. (let ((f (buffer-file-name)) a b)
  8735. (setq a (mapcar (lambda(l)
  8736. (let ((ll (car l)))
  8737. (when (and (string-match "^file:\\(.+\\)::" ll)
  8738. (equal f (expand-file-name (match-string 1 ll))))
  8739. ll)))
  8740. org-stored-links))
  8741. (when (featurep 'org-id)
  8742. (setq b (mapcar (lambda(l)
  8743. (let ((ll (car l)))
  8744. (when (and (string-match "^id:\\(.+\\)$" ll)
  8745. (equal f (expand-file-name
  8746. (or (org-id-find-id-file
  8747. (match-string 1 ll)) ""))))
  8748. ll)))
  8749. org-stored-links)))
  8750. (mapcar (lambda(l)
  8751. (put-text-property 0 (length l) 'face 'font-lock-comment-face l))
  8752. (delq nil (append a b)))))
  8753. (defvar org-link-links-in-this-file nil)
  8754. (defun org-insert-link (&optional complete-file link-location default-description)
  8755. "Insert a link. At the prompt, enter the link.
  8756. Completion can be used to insert any of the link protocol prefixes like
  8757. http or ftp in use.
  8758. The history can be used to select a link previously stored with
  8759. `org-store-link'. When the empty string is entered (i.e. if you just
  8760. press RET at the prompt), the link defaults to the most recently
  8761. stored link. As SPC triggers completion in the minibuffer, you need to
  8762. use M-SPC or C-q SPC to force the insertion of a space character.
  8763. You will also be prompted for a description, and if one is given, it will
  8764. be displayed in the buffer instead of the link.
  8765. If there is already a link at point, this command will allow you to edit link
  8766. and description parts.
  8767. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  8768. be selected using completion. The path to the file will be relative to the
  8769. current directory if the file is in the current directory or a subdirectory.
  8770. Otherwise, the link will be the absolute path as completed in the minibuffer
  8771. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  8772. option `org-link-file-path-type'.
  8773. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  8774. the current directory or below.
  8775. With three \\[universal-argument] prefixes, negate the meaning of
  8776. `org-keep-stored-link-after-insertion'.
  8777. If `org-make-link-description-function' is non-nil, this function will be
  8778. called with the link target, and the result will be the default
  8779. link description.
  8780. If the LINK-LOCATION parameter is non-nil, this value will be
  8781. used as the link location instead of reading one interactively.
  8782. If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
  8783. be used as the default description."
  8784. (interactive "P")
  8785. (let* ((wcf (current-window-configuration))
  8786. (origbuf (current-buffer))
  8787. (region (if (org-region-active-p)
  8788. (buffer-substring (region-beginning) (region-end))))
  8789. (remove (and region (list (region-beginning) (region-end))))
  8790. (desc region)
  8791. tmphist ; byte-compile incorrectly complains about this
  8792. (link link-location)
  8793. (abbrevs org-link-abbrev-alist-local)
  8794. entry file all-prefixes auto-desc)
  8795. (cond
  8796. (link-location) ; specified by arg, just use it.
  8797. ((org-in-regexp org-bracket-link-regexp 1)
  8798. ;; We do have a link at point, and we are going to edit it.
  8799. (setq remove (list (match-beginning 0) (match-end 0)))
  8800. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  8801. (setq link (read-string "Link: "
  8802. (org-link-unescape
  8803. (org-match-string-no-properties 1)))))
  8804. ((or (org-in-regexp org-angle-link-re)
  8805. (org-in-regexp org-plain-link-re))
  8806. ;; Convert to bracket link
  8807. (setq remove (list (match-beginning 0) (match-end 0))
  8808. link (read-string "Link: "
  8809. (org-remove-angle-brackets (match-string 0)))))
  8810. ((member complete-file '((4) (16)))
  8811. ;; Completing read for file names.
  8812. (setq link (org-file-complete-link complete-file)))
  8813. (t
  8814. ;; Read link, with completion for stored links.
  8815. (org-link-fontify-links-to-this-file)
  8816. (org-switch-to-buffer-other-window "*Org Links*")
  8817. (with-current-buffer "*Org Links*"
  8818. (erase-buffer)
  8819. (insert "Insert a link.
  8820. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  8821. (when org-stored-links
  8822. (insert "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  8823. (insert (mapconcat 'org-link-prettify
  8824. (reverse org-stored-links) "\n")))
  8825. (goto-char (point-min)))
  8826. (let ((cw (selected-window)))
  8827. (select-window (get-buffer-window "*Org Links*" 'visible))
  8828. (with-current-buffer "*Org Links*" (setq truncate-lines t))
  8829. (unless (pos-visible-in-window-p (point-max))
  8830. (org-fit-window-to-buffer))
  8831. (and (window-live-p cw) (select-window cw)))
  8832. ;; Fake a link history, containing the stored links.
  8833. (setq tmphist (append (mapcar 'car org-stored-links)
  8834. org-insert-link-history))
  8835. (setq all-prefixes (append (mapcar 'car abbrevs)
  8836. (mapcar 'car org-link-abbrev-alist)
  8837. org-link-types))
  8838. (unwind-protect
  8839. (progn
  8840. (setq link
  8841. (org-completing-read
  8842. "Link: "
  8843. (append
  8844. (mapcar (lambda (x) (concat x ":"))
  8845. all-prefixes)
  8846. (mapcar 'car org-stored-links))
  8847. nil nil nil
  8848. 'tmphist
  8849. (caar org-stored-links)))
  8850. (if (not (string-match "\\S-" link))
  8851. (error "No link selected"))
  8852. (mapc (lambda(l)
  8853. (when (equal link (cadr l)) (setq link (car l) auto-desc t)))
  8854. org-stored-links)
  8855. (if (or (member link all-prefixes)
  8856. (and (equal ":" (substring link -1))
  8857. (member (substring link 0 -1) all-prefixes)
  8858. (setq link (substring link 0 -1))))
  8859. (setq link (with-current-buffer origbuf
  8860. (org-link-try-special-completion link)))))
  8861. (set-window-configuration wcf)
  8862. (kill-buffer "*Org Links*"))
  8863. (setq entry (assoc link org-stored-links))
  8864. (or entry (push link org-insert-link-history))
  8865. (setq desc (or desc (nth 1 entry)))))
  8866. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  8867. (not org-keep-stored-link-after-insertion))
  8868. (setq org-stored-links (delq (assoc link org-stored-links)
  8869. org-stored-links)))
  8870. (if (string-match org-plain-link-re link)
  8871. ;; URL-like link, normalize the use of angular brackets.
  8872. (setq link (org-remove-angle-brackets link)))
  8873. ;; Check if we are linking to the current file with a search
  8874. ;; option If yes, simplify the link by using only the search
  8875. ;; option.
  8876. (when (and buffer-file-name
  8877. (string-match "^file:\\(.+?\\)::\\(.+\\)" link))
  8878. (let* ((path (match-string 1 link))
  8879. (case-fold-search nil)
  8880. (search (match-string 2 link)))
  8881. (save-match-data
  8882. (if (equal (file-truename buffer-file-name) (file-truename path))
  8883. ;; We are linking to this same file, with a search option
  8884. (setq link search)))))
  8885. ;; Check if we can/should use a relative path. If yes, simplify the link
  8886. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  8887. (let* ((type (match-string 1 link))
  8888. (path (match-string 2 link))
  8889. (origpath path)
  8890. (case-fold-search nil))
  8891. (cond
  8892. ((or (eq org-link-file-path-type 'absolute)
  8893. (equal complete-file '(16)))
  8894. (setq path (abbreviate-file-name (expand-file-name path))))
  8895. ((eq org-link-file-path-type 'noabbrev)
  8896. (setq path (expand-file-name path)))
  8897. ((eq org-link-file-path-type 'relative)
  8898. (setq path (file-relative-name path)))
  8899. (t
  8900. (save-match-data
  8901. (if (string-match (concat "^" (regexp-quote
  8902. (expand-file-name
  8903. (file-name-as-directory
  8904. default-directory))))
  8905. (expand-file-name path))
  8906. ;; We are linking a file with relative path name.
  8907. (setq path (substring (expand-file-name path)
  8908. (match-end 0)))
  8909. (setq path (abbreviate-file-name (expand-file-name path)))))))
  8910. (setq link (concat type path))
  8911. (if (equal desc origpath)
  8912. (setq desc path))))
  8913. (if org-make-link-description-function
  8914. (setq desc
  8915. (or (condition-case nil
  8916. (funcall org-make-link-description-function link desc)
  8917. (error (progn (message "Can't get link description from `%s'"
  8918. (symbol-name org-make-link-description-function))
  8919. (sit-for 2) nil)))
  8920. (read-string "Description: " default-description)))
  8921. (if default-description (setq desc default-description)
  8922. (setq desc (or (and auto-desc desc)
  8923. (read-string "Description: " desc)))))
  8924. (unless (string-match "\\S-" desc) (setq desc nil))
  8925. (if remove (apply 'delete-region remove))
  8926. (insert (org-make-link-string link desc))))
  8927. (defun org-link-try-special-completion (type)
  8928. "If there is completion support for link type TYPE, offer it."
  8929. (let ((fun (intern (concat "org-" type "-complete-link"))))
  8930. (if (functionp fun)
  8931. (funcall fun)
  8932. (read-string "Link (no completion support): " (concat type ":")))))
  8933. (defun org-file-complete-link (&optional arg)
  8934. "Create a file link using completion."
  8935. (let (file link)
  8936. (setq file (org-iread-file-name "File: "))
  8937. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  8938. (pwd1 (file-name-as-directory (abbreviate-file-name
  8939. (expand-file-name ".")))))
  8940. (cond
  8941. ((equal arg '(16))
  8942. (setq link (concat
  8943. "file:"
  8944. (abbreviate-file-name (expand-file-name file)))))
  8945. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  8946. (setq link (concat "file:" (match-string 1 file))))
  8947. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  8948. (expand-file-name file))
  8949. (setq link (concat
  8950. "file:" (match-string 1 (expand-file-name file)))))
  8951. (t (setq link (concat "file:" file)))))
  8952. link))
  8953. (defun org-iread-file-name (&rest args)
  8954. "Read-file-name using `ido-mode' speedup if available.
  8955. ARGS are arguments that may be passed to `ido-read-file-name' or `read-file-name'.
  8956. See `read-file-name' for a description of parameters."
  8957. (org-without-partial-completion
  8958. (if (and org-completion-use-ido
  8959. (fboundp 'ido-read-file-name)
  8960. (boundp 'ido-mode) ido-mode
  8961. (listp (second args)))
  8962. (let ((ido-enter-matching-directory nil))
  8963. (apply 'ido-read-file-name args))
  8964. (apply 'read-file-name args))))
  8965. (defun org-completing-read (&rest args)
  8966. "Completing-read with SPACE being a normal character."
  8967. (let ((enable-recursive-minibuffers t)
  8968. (minibuffer-local-completion-map
  8969. (copy-keymap minibuffer-local-completion-map)))
  8970. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  8971. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  8972. (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
  8973. (apply 'org-icompleting-read args)))
  8974. (defun org-completing-read-no-i (&rest args)
  8975. (let (org-completion-use-ido org-completion-use-iswitchb)
  8976. (apply 'org-completing-read args)))
  8977. (defun org-iswitchb-completing-read (prompt choices &rest args)
  8978. "Use iswitch as a completing-read replacement to choose from choices.
  8979. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  8980. from."
  8981. (let* ((iswitchb-use-virtual-buffers nil)
  8982. (iswitchb-make-buflist-hook
  8983. (lambda ()
  8984. (setq iswitchb-temp-buflist choices))))
  8985. (iswitchb-read-buffer prompt)))
  8986. (defun org-icompleting-read (&rest args)
  8987. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  8988. (org-without-partial-completion
  8989. (if (and org-completion-use-ido
  8990. (fboundp 'ido-completing-read)
  8991. (boundp 'ido-mode) ido-mode
  8992. (listp (second args)))
  8993. (let ((ido-enter-matching-directory nil))
  8994. (apply 'ido-completing-read (concat (car args))
  8995. (if (consp (car (nth 1 args)))
  8996. (mapcar 'car (nth 1 args))
  8997. (nth 1 args))
  8998. (cddr args)))
  8999. (if (and org-completion-use-iswitchb
  9000. (boundp 'iswitchb-mode) iswitchb-mode
  9001. (listp (second args)))
  9002. (apply 'org-iswitchb-completing-read (concat (car args))
  9003. (if (consp (car (nth 1 args)))
  9004. (mapcar 'car (nth 1 args))
  9005. (nth 1 args))
  9006. (cddr args))
  9007. (apply 'completing-read args)))))
  9008. (defun org-extract-attributes (s)
  9009. "Extract the attributes cookie from a string and set as text property."
  9010. (let (a attr (start 0) key value)
  9011. (save-match-data
  9012. (when (string-match "{{\\([^}]+\\)}}$" s)
  9013. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  9014. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  9015. (setq key (match-string 1 a) value (match-string 2 a)
  9016. start (match-end 0)
  9017. attr (plist-put attr (intern key) value))))
  9018. (org-add-props s nil 'org-attr attr))
  9019. s))
  9020. (defun org-extract-attributes-from-string (tag)
  9021. (let (key value attr)
  9022. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  9023. (setq key (match-string 1 tag) value (match-string 2 tag)
  9024. tag (replace-match "" t t tag)
  9025. attr (plist-put attr (intern key) value)))
  9026. (cons tag attr)))
  9027. (defun org-attributes-to-string (plist)
  9028. "Format a property list into an HTML attribute list."
  9029. (let ((s "") key value)
  9030. (while plist
  9031. (setq key (pop plist) value (pop plist))
  9032. (and value
  9033. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  9034. s))
  9035. ;;; Opening/following a link
  9036. (defvar org-link-search-failed nil)
  9037. (defvar org-open-link-functions nil
  9038. "Hook for functions finding a plain text link.
  9039. These functions must take a single argument, the link content.
  9040. They will be called for links that look like [[link text][description]]
  9041. when LINK TEXT does not have a protocol like \"http:\" and does not look
  9042. like a filename (e.g. \"./blue.png\").
  9043. These functions will be called *before* Org attempts to resolve the
  9044. link by doing text searches in the current buffer - so if you want a
  9045. link \"[[target]]\" to still find \"<<target>>\", your function should
  9046. handle this as a special case.
  9047. When the function does handle the link, it must return a non-nil value.
  9048. If it decides that it is not responsible for this link, it must return
  9049. nil to indicate that that Org-mode can continue with other options
  9050. like exact and fuzzy text search.")
  9051. (defun org-next-link (&optional search-backward)
  9052. "Move forward to the next link.
  9053. If the link is in hidden text, expose it."
  9054. (interactive "P")
  9055. (when (and org-link-search-failed (eq this-command last-command))
  9056. (goto-char (point-min))
  9057. (message "Link search wrapped back to beginning of buffer"))
  9058. (setq org-link-search-failed nil)
  9059. (let* ((pos (point))
  9060. (ct (org-context))
  9061. (a (assoc :link ct))
  9062. (srch-fun (if search-backward 're-search-backward 're-search-forward)))
  9063. (cond (a (goto-char (nth (if search-backward 1 2) a)))
  9064. ((looking-at org-any-link-re)
  9065. ;; Don't stay stuck at link without an org-link face
  9066. (forward-char (if search-backward -1 1))))
  9067. (if (funcall srch-fun org-any-link-re nil t)
  9068. (progn
  9069. (goto-char (match-beginning 0))
  9070. (if (outline-invisible-p) (org-show-context)))
  9071. (goto-char pos)
  9072. (setq org-link-search-failed t)
  9073. (message "No further link found"))))
  9074. (defun org-previous-link ()
  9075. "Move backward to the previous link.
  9076. If the link is in hidden text, expose it."
  9077. (interactive)
  9078. (funcall 'org-next-link t))
  9079. (defun org-translate-link (s)
  9080. "Translate a link string if a translation function has been defined."
  9081. (if (and org-link-translation-function
  9082. (fboundp org-link-translation-function)
  9083. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  9084. (progn
  9085. (setq s (funcall org-link-translation-function
  9086. (match-string 1 s) (match-string 2 s)))
  9087. (concat (car s) ":" (cdr s)))
  9088. s))
  9089. (defun org-translate-link-from-planner (type path)
  9090. "Translate a link from Emacs Planner syntax so that Org can follow it.
  9091. This is still an experimental function, your mileage may vary."
  9092. (cond
  9093. ((member type '("http" "https" "news" "ftp"))
  9094. ;; standard Internet links are the same.
  9095. nil)
  9096. ((and (equal type "irc") (string-match "^//" path))
  9097. ;; Planner has two / at the beginning of an irc link, we have 1.
  9098. ;; We should have zero, actually....
  9099. (setq path (substring path 1)))
  9100. ((and (equal type "lisp") (string-match "^/" path))
  9101. ;; Planner has a slash, we do not.
  9102. (setq type "elisp" path (substring path 1)))
  9103. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  9104. ;; A typical message link. Planner has the id after the final slash,
  9105. ;; we separate it with a hash mark
  9106. (setq path (concat (match-string 1 path) "#"
  9107. (org-remove-angle-brackets (match-string 2 path)))))
  9108. )
  9109. (cons type path))
  9110. (defun org-find-file-at-mouse (ev)
  9111. "Open file link or URL at mouse."
  9112. (interactive "e")
  9113. (mouse-set-point ev)
  9114. (org-open-at-point 'in-emacs))
  9115. (defun org-open-at-mouse (ev)
  9116. "Open file link or URL at mouse.
  9117. See the docstring of `org-open-file' for details."
  9118. (interactive "e")
  9119. (mouse-set-point ev)
  9120. (if (eq major-mode 'org-agenda-mode)
  9121. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  9122. (org-open-at-point))
  9123. (defvar org-window-config-before-follow-link nil
  9124. "The window configuration before following a link.
  9125. This is saved in case the need arises to restore it.")
  9126. (defvar org-open-link-marker (make-marker)
  9127. "Marker pointing to the location where `org-open-at-point; was called.")
  9128. ;;;###autoload
  9129. (defun org-open-at-point-global ()
  9130. "Follow a link like Org-mode does.
  9131. This command can be called in any mode to follow a link that has
  9132. Org-mode syntax."
  9133. (interactive)
  9134. (org-run-like-in-org-mode 'org-open-at-point))
  9135. ;;;###autoload
  9136. (defun org-open-link-from-string (s &optional arg reference-buffer)
  9137. "Open a link in the string S, as if it was in Org-mode."
  9138. (interactive "sLink: \nP")
  9139. (let ((reference-buffer (or reference-buffer (current-buffer))))
  9140. (with-temp-buffer
  9141. (let ((org-inhibit-startup (not reference-buffer)))
  9142. (org-mode)
  9143. (insert s)
  9144. (goto-char (point-min))
  9145. (when reference-buffer
  9146. (setq org-link-abbrev-alist-local
  9147. (with-current-buffer reference-buffer
  9148. org-link-abbrev-alist-local)))
  9149. (org-open-at-point arg reference-buffer)))))
  9150. (defvar org-open-at-point-functions nil
  9151. "Hook that is run when following a link at point.
  9152. Functions in this hook must return t if they identify and follow
  9153. a link at point. If they don't find anything interesting at point,
  9154. they must return nil.")
  9155. (defvar clean-buffer-list-kill-buffer-names) ; Defined in midnight.el
  9156. (defun org-open-at-point (&optional arg reference-buffer)
  9157. "Open link at or after point.
  9158. If there is no link at point, this function will search forward up to
  9159. the end of the current line.
  9160. Normally, files will be opened by an appropriate application. If the
  9161. optional prefix argument ARG is non-nil, Emacs will visit the file.
  9162. With a double prefix argument, try to open outside of Emacs, in the
  9163. application the system uses for this file type."
  9164. (interactive "P")
  9165. ;; if in a code block, then open the block's results
  9166. (unless (call-interactively #'org-babel-open-src-block-result)
  9167. (org-load-modules-maybe)
  9168. (move-marker org-open-link-marker (point))
  9169. (setq org-window-config-before-follow-link (current-window-configuration))
  9170. (org-remove-occur-highlights nil nil t)
  9171. (cond
  9172. ((and (org-at-heading-p)
  9173. (not (org-at-timestamp-p t))
  9174. (not (org-in-regexp
  9175. (concat org-plain-link-re "\\|"
  9176. org-bracket-link-regexp "\\|"
  9177. org-angle-link-re "\\|"
  9178. "[ \t]:[^ \t\n]+:[ \t]*$")))
  9179. (not (get-text-property (point) 'org-linked-text)))
  9180. (or (let* ((lkall (org-offer-links-in-entry (current-buffer) (point) arg))
  9181. (lk0 (car lkall))
  9182. (lk (if (stringp lk0) (list lk0) lk0))
  9183. (lkend (cdr lkall)))
  9184. (mapcar (lambda(l)
  9185. (search-forward l nil lkend)
  9186. (goto-char (match-beginning 0))
  9187. (org-open-at-point))
  9188. lk))
  9189. (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
  9190. ((run-hook-with-args-until-success 'org-open-at-point-functions))
  9191. ((and (org-at-timestamp-p t)
  9192. (not (org-in-regexp org-bracket-link-regexp)))
  9193. (org-follow-timestamp-link))
  9194. ((and (or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  9195. (not (org-in-regexp org-any-link-re)))
  9196. (org-footnote-action))
  9197. (t
  9198. (let (type path link line search (pos (point)))
  9199. (catch 'match
  9200. (save-excursion
  9201. (skip-chars-forward "^]\n\r")
  9202. (when (org-in-regexp org-bracket-link-regexp 1)
  9203. (setq link (org-extract-attributes
  9204. (org-link-unescape (org-match-string-no-properties 1))))
  9205. (while (string-match " *\n *" link)
  9206. (setq link (replace-match " " t t link)))
  9207. (setq link (org-link-expand-abbrev link))
  9208. (cond
  9209. ((or (file-name-absolute-p link)
  9210. (string-match "^\\.\\.?/" link))
  9211. (setq type "file" path link))
  9212. ((string-match org-link-re-with-space3 link)
  9213. (setq type (match-string 1 link) path (match-string 2 link)))
  9214. ((string-match "^help:+\\(.+\\)" link)
  9215. (setq type "help" path (match-string 1 link)))
  9216. (t (setq type "thisfile" path link)))
  9217. (throw 'match t)))
  9218. (when (get-text-property (point) 'org-linked-text)
  9219. (setq type "thisfile"
  9220. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  9221. (1+ (point)) (point))
  9222. path (buffer-substring
  9223. (or (previous-single-property-change pos 'org-linked-text)
  9224. (point-min))
  9225. (or (next-single-property-change pos 'org-linked-text)
  9226. (point-max))))
  9227. (throw 'match t))
  9228. (save-excursion
  9229. (when (or (org-in-regexp org-angle-link-re)
  9230. (let ((match (org-in-regexp org-plain-link-re)))
  9231. ;; Check a plain link is not within a bracket link
  9232. (and match
  9233. (save-excursion
  9234. (progn
  9235. (goto-char (car match))
  9236. (not (org-in-regexp org-bracket-link-regexp))))))
  9237. (let ((line_ending (save-excursion (end-of-line) (point))))
  9238. ;; We are in a line before a plain or bracket link
  9239. (or (re-search-forward org-plain-link-re line_ending t)
  9240. (re-search-forward org-bracket-link-regexp line_ending t))))
  9241. (setq type (match-string 1)
  9242. path (org-link-unescape (match-string 2)))
  9243. (throw 'match t)))
  9244. (save-excursion
  9245. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@#%:]+\\):[ \t]*$"))
  9246. (setq type "tags"
  9247. path (match-string 1))
  9248. (while (string-match ":" path)
  9249. (setq path (replace-match "+" t t path)))
  9250. (throw 'match t)))
  9251. (when (org-in-regexp "<\\([^><\n]+\\)>")
  9252. (setq type "tree-match"
  9253. path (match-string 1))
  9254. (throw 'match t)))
  9255. (unless path
  9256. (user-error "No link found"))
  9257. ;; switch back to reference buffer
  9258. ;; needed when if called in a temporary buffer through
  9259. ;; org-open-link-from-string
  9260. (with-current-buffer (or reference-buffer (current-buffer))
  9261. ;; Remove any trailing spaces in path
  9262. (if (string-match " +\\'" path)
  9263. (setq path (replace-match "" t t path)))
  9264. (if (and org-link-translation-function
  9265. (fboundp org-link-translation-function))
  9266. ;; Check if we need to translate the link
  9267. (let ((tmp (funcall org-link-translation-function type path)))
  9268. (setq type (car tmp) path (cdr tmp))))
  9269. (cond
  9270. ((assoc type org-link-protocols)
  9271. (funcall (nth 1 (assoc type org-link-protocols)) path))
  9272. ((equal type "help")
  9273. (let ((f-or-v (intern path)))
  9274. (cond ((fboundp f-or-v)
  9275. (describe-function f-or-v))
  9276. ((boundp f-or-v)
  9277. (describe-variable f-or-v))
  9278. (t (error "Not a known function or variable")))))
  9279. ((equal type "mailto")
  9280. (let ((cmd (car org-link-mailto-program))
  9281. (args (cdr org-link-mailto-program)) args1
  9282. (address path) (subject "") a)
  9283. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  9284. (setq address (match-string 1 path)
  9285. subject (org-link-escape (match-string 2 path))))
  9286. (while args
  9287. (cond
  9288. ((not (stringp (car args))) (push (pop args) args1))
  9289. (t (setq a (pop args))
  9290. (if (string-match "%a" a)
  9291. (setq a (replace-match address t t a)))
  9292. (if (string-match "%s" a)
  9293. (setq a (replace-match subject t t a)))
  9294. (push a args1))))
  9295. (apply cmd (nreverse args1))))
  9296. ((member type '("http" "https" "ftp" "news"))
  9297. (browse-url (concat type ":" (if (org-string-match-p "[[:nonascii:] ]" path)
  9298. (org-link-escape
  9299. path org-link-escape-chars-browser)
  9300. path))))
  9301. ((string= type "doi")
  9302. (browse-url (concat org-doi-server-url (if (org-string-match-p "[[:nonascii:] ]" path)
  9303. (org-link-escape
  9304. path org-link-escape-chars-browser)
  9305. path))))
  9306. ((member type '("message"))
  9307. (browse-url (concat type ":" path)))
  9308. ((string= type "tags")
  9309. (org-tags-view arg path))
  9310. ((string= type "tree-match")
  9311. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  9312. ((string= type "file")
  9313. (if (string-match "::\\([0-9]+\\)\\'" path)
  9314. (setq line (string-to-number (match-string 1 path))
  9315. path (substring path 0 (match-beginning 0)))
  9316. (if (string-match "::\\(.+\\)\\'" path)
  9317. (setq search (match-string 1 path)
  9318. path (substring path 0 (match-beginning 0)))))
  9319. (if (string-match "[*?{]" (file-name-nondirectory path))
  9320. (dired path)
  9321. (org-open-file path arg line search)))
  9322. ((string= type "shell")
  9323. (let ((buf (generate-new-buffer "*Org Shell Output"))
  9324. (cmd path))
  9325. (if (or (and (not (string= org-confirm-shell-link-not-regexp ""))
  9326. (string-match org-confirm-shell-link-not-regexp cmd))
  9327. (not org-confirm-shell-link-function)
  9328. (funcall org-confirm-shell-link-function
  9329. (format "Execute \"%s\" in shell? "
  9330. (org-add-props cmd nil
  9331. 'face 'org-warning))))
  9332. (progn
  9333. (message "Executing %s" cmd)
  9334. (shell-command cmd buf)
  9335. (if (featurep 'midnight)
  9336. (setq clean-buffer-list-kill-buffer-names
  9337. (cons buf clean-buffer-list-kill-buffer-names))))
  9338. (error "Abort"))))
  9339. ((string= type "elisp")
  9340. (let ((cmd path))
  9341. (if (or (and (not (string= org-confirm-elisp-link-not-regexp ""))
  9342. (string-match org-confirm-elisp-link-not-regexp cmd))
  9343. (not org-confirm-elisp-link-function)
  9344. (funcall org-confirm-elisp-link-function
  9345. (format "Execute \"%s\" as elisp? "
  9346. (org-add-props cmd nil
  9347. 'face 'org-warning))))
  9348. (message "%s => %s" cmd
  9349. (if (equal (string-to-char cmd) ?\()
  9350. (eval (read cmd))
  9351. (call-interactively (read cmd))))
  9352. (error "Abort"))))
  9353. ((and (string= type "thisfile")
  9354. (run-hook-with-args-until-success
  9355. 'org-open-link-functions path)))
  9356. ((string= type "thisfile")
  9357. (if arg
  9358. (switch-to-buffer-other-window
  9359. (org-get-buffer-for-internal-link (current-buffer)))
  9360. (org-mark-ring-push))
  9361. (let ((cmd `(org-link-search
  9362. ,path
  9363. ,(cond ((equal arg '(4)) ''occur)
  9364. ((equal arg '(16)) ''org-occur))
  9365. ,pos)))
  9366. (condition-case nil (let ((org-link-search-inhibit-query t))
  9367. (eval cmd))
  9368. (error (progn (widen) (eval cmd))))))
  9369. (t (browse-url-at-point)))))))
  9370. (move-marker org-open-link-marker nil)
  9371. (run-hook-with-args 'org-follow-link-hook)))
  9372. (defun org-offer-links-in-entry (buffer marker &optional nth zero)
  9373. "Offer links in the current entry and return the selected link.
  9374. If there is only one link, return it.
  9375. If NTH is an integer, return the NTH link found.
  9376. If ZERO is a string, check also this string for a link, and if
  9377. there is one, return it."
  9378. (with-current-buffer buffer
  9379. (save-excursion
  9380. (save-restriction
  9381. (widen)
  9382. (goto-char marker)
  9383. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  9384. "\\(" org-angle-link-re "\\)\\|"
  9385. "\\(" org-plain-link-re "\\)"))
  9386. (cnt ?0)
  9387. (in-emacs (if (integerp nth) nil nth))
  9388. have-zero end links link c)
  9389. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  9390. (push (match-string 0 zero) links)
  9391. (setq cnt (1- cnt) have-zero t))
  9392. (save-excursion
  9393. (org-back-to-heading t)
  9394. (setq end (save-excursion (outline-next-heading) (point)))
  9395. (while (re-search-forward re end t)
  9396. (push (match-string 0) links))
  9397. (setq links (org-uniquify (reverse links))))
  9398. (cond
  9399. ((null links)
  9400. (message "No links"))
  9401. ((equal (length links) 1)
  9402. (setq link (car links)))
  9403. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  9404. (setq link (nth (if have-zero nth (1- nth)) links)))
  9405. (t ; we have to select a link
  9406. (save-excursion
  9407. (save-window-excursion
  9408. (delete-other-windows)
  9409. (with-output-to-temp-buffer "*Select Link*"
  9410. (mapc (lambda (l)
  9411. (if (not (string-match org-bracket-link-regexp l))
  9412. (princ (format "[%c] %s\n" (incf cnt)
  9413. (org-remove-angle-brackets l)))
  9414. (if (match-end 3)
  9415. (princ (format "[%c] %s (%s)\n" (incf cnt)
  9416. (match-string 3 l) (match-string 1 l)))
  9417. (princ (format "[%c] %s\n" (incf cnt)
  9418. (match-string 1 l))))))
  9419. links))
  9420. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  9421. (message "Select link to open, RET to open all:")
  9422. (setq c (read-char-exclusive))
  9423. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  9424. (when (equal c ?q) (error "Abort"))
  9425. (if (equal c ?\C-m)
  9426. (setq link links)
  9427. (setq nth (- c ?0))
  9428. (if have-zero (setq nth (1+ nth)))
  9429. (unless (and (integerp nth) (>= (length links) nth))
  9430. (error "Invalid link selection"))
  9431. (setq link (nth (1- nth) links)))))
  9432. (cons link end))))))
  9433. ;; Add special file links that specify the way of opening
  9434. (org-add-link-type "file+sys" 'org-open-file-with-system)
  9435. (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
  9436. (defun org-open-file-with-system (path)
  9437. "Open file at PATH using the system way of opening it."
  9438. (org-open-file path 'system))
  9439. (defun org-open-file-with-emacs (path)
  9440. "Open file at PATH in Emacs."
  9441. (org-open-file path 'emacs))
  9442. ;;; File search
  9443. (defvar org-create-file-search-functions nil
  9444. "List of functions to construct the right search string for a file link.
  9445. These functions are called in turn with point at the location to
  9446. which the link should point.
  9447. A function in the hook should first test if it would like to
  9448. handle this file type, for example by checking the `major-mode'
  9449. or the file extension. If it decides not to handle this file, it
  9450. should just return nil to give other functions a chance. If it
  9451. does handle the file, it must return the search string to be used
  9452. when following the link. The search string will be part of the
  9453. file link, given after a double colon, and `org-open-at-point'
  9454. will automatically search for it. If special measures must be
  9455. taken to make the search successful, another function should be
  9456. added to the companion hook `org-execute-file-search-functions',
  9457. which see.
  9458. A function in this hook may also use `setq' to set the variable
  9459. `description' to provide a suggestion for the descriptive text to
  9460. be used for this link when it gets inserted into an Org-mode
  9461. buffer with \\[org-insert-link].")
  9462. (defvar org-execute-file-search-functions nil
  9463. "List of functions to execute a file search triggered by a link.
  9464. Functions added to this hook must accept a single argument, the
  9465. search string that was part of the file link, the part after the
  9466. double colon. The function must first check if it would like to
  9467. handle this search, for example by checking the `major-mode' or
  9468. the file extension. If it decides not to handle this search, it
  9469. should just return nil to give other functions a chance. If it
  9470. does handle the search, it must return a non-nil value to keep
  9471. other functions from trying.
  9472. Each function can access the current prefix argument through the
  9473. variable `current-prefix-arg'. Note that a single prefix is used
  9474. to force opening a link in Emacs, so it may be good to only use a
  9475. numeric or double prefix to guide the search function.
  9476. In case this is needed, a function in this hook can also restore
  9477. the window configuration before `org-open-at-point' was called using:
  9478. (set-window-configuration org-window-config-before-follow-link)")
  9479. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  9480. (defun org-link-search (s &optional type avoid-pos stealth)
  9481. "Search for a link search option.
  9482. If S is surrounded by forward slashes, it is interpreted as a
  9483. regular expression. In org-mode files, this will create an `org-occur'
  9484. sparse tree. In ordinary files, `occur' will be used to list matches.
  9485. If the current buffer is in `dired-mode', grep will be used to search
  9486. in all files. If AVOID-POS is given, ignore matches near that position.
  9487. When optional argument STEALTH is non-nil, do not modify
  9488. visibility around point, thus ignoring
  9489. `org-show-hierarchy-above', `org-show-following-heading' and
  9490. `org-show-siblings' variables."
  9491. (let ((case-fold-search t)
  9492. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  9493. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  9494. (append '(("") (" ") ("\t") ("\n"))
  9495. org-emphasis-alist)
  9496. "\\|") "\\)"))
  9497. (pos (point))
  9498. (pre nil) (post nil)
  9499. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  9500. (cond
  9501. ;; First check if there are any special search functions
  9502. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  9503. ;; Now try the builtin stuff
  9504. ((and (equal (string-to-char s0) ?#)
  9505. (> (length s0) 1)
  9506. (save-excursion
  9507. (goto-char (point-min))
  9508. (and
  9509. (re-search-forward
  9510. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  9511. (setq type 'dedicated
  9512. pos (match-beginning 0))))
  9513. ;; There is an exact target for this
  9514. (goto-char pos)
  9515. (org-back-to-heading t)))
  9516. ((save-excursion
  9517. (goto-char (point-min))
  9518. (and
  9519. (re-search-forward
  9520. (concat "<<" (regexp-quote s0) ">>") nil t)
  9521. (setq type 'dedicated
  9522. pos (match-beginning 0))))
  9523. ;; There is an exact target for this
  9524. (goto-char pos))
  9525. ((save-excursion
  9526. (goto-char (point-min))
  9527. (and
  9528. (re-search-forward
  9529. (format "^[ \t]*#\\+TARGET: %s" (regexp-quote s0)) nil t)
  9530. (setq type 'dedicated pos (match-beginning 0))))
  9531. ;; Found an invisible target.
  9532. (goto-char pos))
  9533. ((save-excursion
  9534. (goto-char (point-min))
  9535. (and
  9536. (re-search-forward
  9537. (format "^[ \t]*#\\+NAME: %s" (regexp-quote s0)) nil t)
  9538. (setq type 'dedicated pos (match-beginning 0))))
  9539. ;; Found an element with a matching #+name affiliated keyword.
  9540. (goto-char pos))
  9541. ((and (string-match "^(\\(.*\\))$" s0)
  9542. (save-excursion
  9543. (goto-char (point-min))
  9544. (and
  9545. (re-search-forward
  9546. (concat "[^[]" (regexp-quote
  9547. (format org-coderef-label-format
  9548. (match-string 1 s0))))
  9549. nil t)
  9550. (setq type 'dedicated
  9551. pos (1+ (match-beginning 0))))))
  9552. ;; There is a coderef target for this
  9553. (goto-char pos))
  9554. ((string-match "^/\\(.*\\)/$" s)
  9555. ;; A regular expression
  9556. (cond
  9557. ((derived-mode-p 'org-mode)
  9558. (org-occur (match-string 1 s)))
  9559. (t (org-do-occur (match-string 1 s)))))
  9560. ((and (derived-mode-p 'org-mode) org-link-search-must-match-exact-headline)
  9561. (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
  9562. (goto-char (point-min))
  9563. (cond
  9564. ((let (case-fold-search)
  9565. (re-search-forward (format org-complex-heading-regexp-format
  9566. (regexp-quote s))
  9567. nil t))
  9568. ;; OK, found a match
  9569. (setq type 'dedicated)
  9570. (goto-char (match-beginning 0)))
  9571. ((and (not org-link-search-inhibit-query)
  9572. (eq org-link-search-must-match-exact-headline 'query-to-create)
  9573. (y-or-n-p "No match - create this as a new heading? "))
  9574. (goto-char (point-max))
  9575. (or (bolp) (newline))
  9576. (insert "* " s "\n")
  9577. (beginning-of-line 0))
  9578. (t
  9579. (goto-char pos)
  9580. (error "No match"))))
  9581. (t
  9582. ;; A normal search string
  9583. (when (equal (string-to-char s) ?*)
  9584. ;; Anchor on headlines, post may include tags.
  9585. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  9586. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
  9587. s (substring s 1)))
  9588. (remove-text-properties
  9589. 0 (length s)
  9590. '(face nil mouse-face nil keymap nil fontified nil) s)
  9591. ;; Make a series of regular expressions to find a match
  9592. (setq words (org-split-string s "[ \n\r\t]+")
  9593. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  9594. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  9595. "\\)" markers)
  9596. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  9597. re2a (concat "[ \t\r\n]" re2a_)
  9598. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  9599. re4 (concat "[^a-zA-Z_]" re4_)
  9600. re1 (concat pre re2 post)
  9601. re3 (concat pre (if pre re4_ re4) post)
  9602. re5 (concat pre ".*" re4)
  9603. re2 (concat pre re2)
  9604. re2a (concat pre (if pre re2a_ re2a))
  9605. re4 (concat pre (if pre re4_ re4))
  9606. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  9607. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  9608. re5 "\\)"
  9609. ))
  9610. (cond
  9611. ((eq type 'org-occur) (org-occur reall))
  9612. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  9613. (t (goto-char (point-min))
  9614. (setq type 'fuzzy)
  9615. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  9616. (org-search-not-self 1 re1 nil t)
  9617. (org-search-not-self 1 re2 nil t)
  9618. (org-search-not-self 1 re2a nil t)
  9619. (org-search-not-self 1 re3 nil t)
  9620. (org-search-not-self 1 re4 nil t)
  9621. (org-search-not-self 1 re5 nil t)
  9622. )
  9623. (goto-char (match-beginning 1))
  9624. (goto-char pos)
  9625. (error "No match"))))))
  9626. (and (derived-mode-p 'org-mode)
  9627. (not stealth)
  9628. (org-show-context 'link-search))
  9629. type))
  9630. (defun org-search-not-self (group &rest args)
  9631. "Execute `re-search-forward', but only accept matches that do not
  9632. enclose the position of `org-open-link-marker'."
  9633. (let ((m org-open-link-marker))
  9634. (catch 'exit
  9635. (while (apply 're-search-forward args)
  9636. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  9637. (goto-char (match-end group))
  9638. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  9639. (> (match-beginning 0) (marker-position m))
  9640. (< (match-end 0) (marker-position m)))
  9641. (save-match-data
  9642. (or (not (org-in-regexp
  9643. org-bracket-link-analytic-regexp 1))
  9644. (not (match-end 4)) ; no description
  9645. (and (<= (match-beginning 4) (point))
  9646. (>= (match-end 4) (point))))))
  9647. (throw 'exit (point))))))))
  9648. (defun org-get-buffer-for-internal-link (buffer)
  9649. "Return a buffer to be used for displaying the link target of internal links."
  9650. (cond
  9651. ((not org-display-internal-link-with-indirect-buffer)
  9652. buffer)
  9653. ((string-match "(Clone)$" (buffer-name buffer))
  9654. (message "Buffer is already a clone, not making another one")
  9655. ;; we also do not modify visibility in this case
  9656. buffer)
  9657. (t ; make a new indirect buffer for displaying the link
  9658. (let* ((bn (buffer-name buffer))
  9659. (ibn (concat bn "(Clone)"))
  9660. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  9661. (with-current-buffer ib (org-overview))
  9662. ib))))
  9663. (defun org-do-occur (regexp &optional cleanup)
  9664. "Call the Emacs command `occur'.
  9665. If CLEANUP is non-nil, remove the printout of the regular expression
  9666. in the *Occur* buffer. This is useful if the regex is long and not useful
  9667. to read."
  9668. (occur regexp)
  9669. (when cleanup
  9670. (let ((cwin (selected-window)) win beg end)
  9671. (when (setq win (get-buffer-window "*Occur*"))
  9672. (select-window win))
  9673. (goto-char (point-min))
  9674. (when (re-search-forward "match[a-z]+" nil t)
  9675. (setq beg (match-end 0))
  9676. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  9677. (setq end (1- (match-beginning 0)))))
  9678. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  9679. (goto-char (point-min))
  9680. (select-window cwin))))
  9681. ;;; The mark ring for links jumps
  9682. (defvar org-mark-ring nil
  9683. "Mark ring for positions before jumps in Org-mode.")
  9684. (defvar org-mark-ring-last-goto nil
  9685. "Last position in the mark ring used to go back.")
  9686. ;; Fill and close the ring
  9687. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  9688. (loop for i from 1 to org-mark-ring-length do
  9689. (push (make-marker) org-mark-ring))
  9690. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  9691. org-mark-ring)
  9692. (defun org-mark-ring-push (&optional pos buffer)
  9693. "Put the current position or POS into the mark ring and rotate it."
  9694. (interactive)
  9695. (setq pos (or pos (point)))
  9696. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  9697. (move-marker (car org-mark-ring)
  9698. (or pos (point))
  9699. (or buffer (current-buffer)))
  9700. (message "%s"
  9701. (substitute-command-keys
  9702. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  9703. (defun org-mark-ring-goto (&optional n)
  9704. "Jump to the previous position in the mark ring.
  9705. With prefix arg N, jump back that many stored positions. When
  9706. called several times in succession, walk through the entire ring.
  9707. Org-mode commands jumping to a different position in the current file,
  9708. or to another Org-mode file, automatically push the old position
  9709. onto the ring."
  9710. (interactive "p")
  9711. (let (p m)
  9712. (if (eq last-command this-command)
  9713. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  9714. (setq p org-mark-ring))
  9715. (setq org-mark-ring-last-goto p)
  9716. (setq m (car p))
  9717. (org-pop-to-buffer-same-window (marker-buffer m))
  9718. (goto-char m)
  9719. (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  9720. (defun org-remove-angle-brackets (s)
  9721. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  9722. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  9723. s)
  9724. (defun org-add-angle-brackets (s)
  9725. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  9726. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  9727. s)
  9728. (defun org-remove-double-quotes (s)
  9729. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  9730. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  9731. s)
  9732. ;;; Following specific links
  9733. (defun org-follow-timestamp-link ()
  9734. "Open an agenda view for the time-stamp date/range at point."
  9735. (cond
  9736. ((org-at-date-range-p t)
  9737. (let ((org-agenda-start-on-weekday)
  9738. (t1 (match-string 1))
  9739. (t2 (match-string 2)) tt1 tt2)
  9740. (setq tt1 (time-to-days (org-time-string-to-time t1))
  9741. tt2 (time-to-days (org-time-string-to-time t2)))
  9742. (let ((org-agenda-buffer-tmp-name
  9743. (format "*Org Agenda(a:%s)"
  9744. (concat (substring t1 0 10) "--" (substring t2 0 10)))))
  9745. (org-agenda-list nil tt1 (1+ (- tt2 tt1))))))
  9746. ((org-at-timestamp-p t)
  9747. (let ((org-agenda-buffer-tmp-name
  9748. (format "*Org Agenda(a:%s)" (substring (match-string 1) 0 10))))
  9749. (org-agenda-list nil (time-to-days (org-time-string-to-time
  9750. (substring (match-string 1) 0 10)))
  9751. 1)))
  9752. (t (error "This should not happen"))))
  9753. ;;; Following file links
  9754. (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
  9755. (declare-function mailcap-extension-to-mime "mailcap" (extn))
  9756. (declare-function mailcap-mime-info
  9757. "mailcap" (string &optional request no-decode))
  9758. (defvar org-wait nil)
  9759. (defun org-open-file (path &optional in-emacs line search)
  9760. "Open the file at PATH.
  9761. First, this expands any special file name abbreviations. Then the
  9762. configuration variable `org-file-apps' is checked if it contains an
  9763. entry for this file type, and if yes, the corresponding command is launched.
  9764. If no application is found, Emacs simply visits the file.
  9765. With optional prefix argument IN-EMACS, Emacs will visit the file.
  9766. With a double \\[universal-argument] \\[universal-argument] \
  9767. prefix arg, Org tries to avoid opening in Emacs
  9768. and to use an external application to visit the file.
  9769. Optional LINE specifies a line to go to, optional SEARCH a string
  9770. to search for. If LINE or SEARCH is given, the file will be
  9771. opened in Emacs, unless an entry from org-file-apps that makes
  9772. use of groups in a regexp matches.
  9773. If you want to change the way frames are used when following a
  9774. link, please customize `org-link-frame-setup'.
  9775. If the file does not exist, an error is thrown."
  9776. (let* ((file (if (equal path "")
  9777. buffer-file-name
  9778. (substitute-in-file-name (expand-file-name path))))
  9779. (file-apps (append org-file-apps (org-default-apps)))
  9780. (apps (org-remove-if
  9781. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9782. (apps-dlink (org-remove-if-not
  9783. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9784. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  9785. (dirp (if remp nil (file-directory-p file)))
  9786. (file (if (and dirp org-open-directory-means-index-dot-org)
  9787. (concat (file-name-as-directory file) "index.org")
  9788. file))
  9789. (a-m-a-p (assq 'auto-mode apps))
  9790. (dfile (downcase file))
  9791. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  9792. (link (cond ((and (eq line nil)
  9793. (eq search nil))
  9794. file)
  9795. (line
  9796. (concat file "::" (number-to-string line)))
  9797. (search
  9798. (concat file "::" search))))
  9799. (dlink (downcase link))
  9800. (old-buffer (current-buffer))
  9801. (old-pos (point))
  9802. (old-mode major-mode)
  9803. ext cmd link-match-data)
  9804. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  9805. (setq ext (match-string 1 dfile))
  9806. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  9807. (setq ext (match-string 1 dfile))))
  9808. (cond
  9809. ((member in-emacs '((16) system))
  9810. (setq cmd (cdr (assoc 'system apps))))
  9811. (in-emacs (setq cmd 'emacs))
  9812. (t
  9813. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  9814. (and dirp (cdr (assoc 'directory apps)))
  9815. ; first, try matching against apps-dlink
  9816. ; if we get a match here, store the match data for later
  9817. (let ((match (assoc-default dlink apps-dlink
  9818. 'string-match)))
  9819. (if match
  9820. (progn (setq link-match-data (match-data))
  9821. match)
  9822. (progn (setq in-emacs (or in-emacs line search))
  9823. nil))) ; if we have no match in apps-dlink,
  9824. ; always open the file in emacs if line or search
  9825. ; is given (for backwards compatibility)
  9826. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  9827. 'string-match)
  9828. (cdr (assoc ext apps))
  9829. (cdr (assoc t apps))))))
  9830. (when (eq cmd 'system)
  9831. (setq cmd (cdr (assoc 'system apps))))
  9832. (when (eq cmd 'default)
  9833. (setq cmd (cdr (assoc t apps))))
  9834. (when (eq cmd 'mailcap)
  9835. (require 'mailcap)
  9836. (mailcap-parse-mailcaps)
  9837. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  9838. (command (mailcap-mime-info mime-type)))
  9839. (if (stringp command)
  9840. (setq cmd command)
  9841. (setq cmd 'emacs))))
  9842. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  9843. (not (file-exists-p file))
  9844. (not org-open-non-existing-files))
  9845. (error "No such file: %s" file))
  9846. (cond
  9847. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  9848. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  9849. (while (string-match "['\"]%s['\"]" cmd)
  9850. (setq cmd (replace-match "%s" t t cmd)))
  9851. (while (string-match "%s" cmd)
  9852. (setq cmd (replace-match
  9853. (save-match-data
  9854. (shell-quote-argument
  9855. (convert-standard-filename file)))
  9856. t t cmd)))
  9857. ;; Replace "%1", "%2" etc. in command with group matches from regex
  9858. (save-match-data
  9859. (let ((match-index 1)
  9860. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  9861. (set-match-data link-match-data)
  9862. (while (<= match-index number-of-groups)
  9863. (let ((regex (concat "%" (number-to-string match-index)))
  9864. (replace-with (match-string match-index dlink)))
  9865. (while (string-match regex cmd)
  9866. (setq cmd (replace-match replace-with t t cmd))))
  9867. (setq match-index (+ match-index 1)))))
  9868. (save-window-excursion
  9869. (message "Running %s...done" cmd)
  9870. (start-process-shell-command cmd nil cmd)
  9871. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))))
  9872. ((or (stringp cmd)
  9873. (eq cmd 'emacs))
  9874. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  9875. (widen)
  9876. (if line (org-goto-line line)
  9877. (if search (org-link-search search))))
  9878. ((consp cmd)
  9879. (let ((file (convert-standard-filename file)))
  9880. (save-match-data
  9881. (set-match-data link-match-data)
  9882. (eval cmd))))
  9883. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  9884. (and (derived-mode-p 'org-mode) (eq old-mode 'org-mode)
  9885. (or (not (equal old-buffer (current-buffer)))
  9886. (not (equal old-pos (point))))
  9887. (org-mark-ring-push old-pos old-buffer))))
  9888. (defun org-file-apps-entry-match-against-dlink-p (entry)
  9889. "This function returns non-nil if `entry' uses a regular
  9890. expression which should be matched against the whole link by
  9891. org-open-file.
  9892. It assumes that is the case when the entry uses a regular
  9893. expression which has at least one grouping construct and the
  9894. action is either a lisp form or a command string containing
  9895. '%1', i.e. using at least one subexpression match as a
  9896. parameter."
  9897. (let ((selector (car entry))
  9898. (action (cdr entry)))
  9899. (if (stringp selector)
  9900. (and (> (regexp-opt-depth selector) 0)
  9901. (or (and (stringp action)
  9902. (string-match "%[0-9]" action))
  9903. (consp action)))
  9904. nil)))
  9905. (defun org-default-apps ()
  9906. "Return the default applications for this operating system."
  9907. (cond
  9908. ((eq system-type 'darwin)
  9909. org-file-apps-defaults-macosx)
  9910. ((eq system-type 'windows-nt)
  9911. org-file-apps-defaults-windowsnt)
  9912. (t org-file-apps-defaults-gnu)))
  9913. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  9914. "Convert extensions to regular expressions in the cars of LIST.
  9915. Also, weed out any non-string entries, because the return value is used
  9916. only for regexp matching.
  9917. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  9918. point to the symbol `emacs', indicating that the file should
  9919. be opened in Emacs."
  9920. (append
  9921. (delq nil
  9922. (mapcar (lambda (x)
  9923. (if (not (stringp (car x)))
  9924. nil
  9925. (if (string-match "\\W" (car x))
  9926. x
  9927. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  9928. list))
  9929. (if add-auto-mode
  9930. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  9931. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  9932. (defun org-file-remote-p (file)
  9933. "Test whether FILE specifies a location on a remote system.
  9934. Return non-nil if the location is indeed remote.
  9935. For example, the filename \"/user@host:/foo\" specifies a location
  9936. on the system \"/user@host:\"."
  9937. (cond ((fboundp 'file-remote-p)
  9938. (file-remote-p file))
  9939. ((fboundp 'tramp-handle-file-remote-p)
  9940. (tramp-handle-file-remote-p file))
  9941. ((and (boundp 'ange-ftp-name-format)
  9942. (string-match (car ange-ftp-name-format) file))
  9943. t)))
  9944. ;;;; Refiling
  9945. (defun org-get-org-file ()
  9946. "Read a filename, with default directory `org-directory'."
  9947. (let ((default (or org-default-notes-file remember-data-file)))
  9948. (read-file-name (format "File name [%s]: " default)
  9949. (file-name-as-directory org-directory)
  9950. default)))
  9951. (defun org-notes-order-reversed-p ()
  9952. "Check if the current file should receive notes in reversed order."
  9953. (cond
  9954. ((not org-reverse-note-order) nil)
  9955. ((eq t org-reverse-note-order) t)
  9956. ((not (listp org-reverse-note-order)) nil)
  9957. (t (catch 'exit
  9958. (let ((all org-reverse-note-order)
  9959. entry)
  9960. (while (setq entry (pop all))
  9961. (if (string-match (car entry) buffer-file-name)
  9962. (throw 'exit (cdr entry))))
  9963. nil)))))
  9964. (defvar org-refile-target-table nil
  9965. "The list of refile targets, created by `org-refile'.")
  9966. (defvar org-agenda-new-buffers nil
  9967. "Buffers created to visit agenda files.")
  9968. (defvar org-refile-cache nil
  9969. "Cache for refile targets.")
  9970. (defvar org-refile-markers nil
  9971. "All the markers used for caching refile locations.")
  9972. (defun org-refile-marker (pos)
  9973. "Get a new refile marker, but only if caching is in use."
  9974. (if (not org-refile-use-cache)
  9975. pos
  9976. (let ((m (make-marker)))
  9977. (move-marker m pos)
  9978. (push m org-refile-markers)
  9979. m)))
  9980. (defun org-refile-cache-clear ()
  9981. "Clear the refile cache and disable all the markers."
  9982. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  9983. (setq org-refile-markers nil)
  9984. (setq org-refile-cache nil)
  9985. (message "Refile cache has been cleared"))
  9986. (defun org-refile-cache-check-set (set)
  9987. "Check if all the markers in the cache still have live buffers."
  9988. (let (marker)
  9989. (catch 'exit
  9990. (while (and set (setq marker (nth 3 (pop set))))
  9991. ;; if org-refile-use-outline-path is 'file, marker may be nil
  9992. (when (and marker (null (marker-buffer marker)))
  9993. (message "not found") (sit-for 3)
  9994. (throw 'exit nil)))
  9995. t)))
  9996. (defun org-refile-cache-put (set &rest identifiers)
  9997. "Push the refile targets SET into the cache, under IDENTIFIERS."
  9998. (let* ((key (sha1 (prin1-to-string identifiers)))
  9999. (entry (assoc key org-refile-cache)))
  10000. (if entry
  10001. (setcdr entry set)
  10002. (push (cons key set) org-refile-cache))))
  10003. (defun org-refile-cache-get (&rest identifiers)
  10004. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  10005. (cond
  10006. ((not org-refile-cache) nil)
  10007. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  10008. (t
  10009. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  10010. org-refile-cache))))
  10011. (and set (org-refile-cache-check-set set) set)))))
  10012. (defun org-refile-get-targets (&optional default-buffer excluded-entries)
  10013. "Produce a table with refile targets."
  10014. (let ((case-fold-search nil)
  10015. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  10016. (entries (or org-refile-targets '((nil . (:level . 1)))))
  10017. targets tgs txt re files f desc descre fast-path-p level pos0)
  10018. (message "Getting targets...")
  10019. (with-current-buffer (or default-buffer (current-buffer))
  10020. (while (setq entry (pop entries))
  10021. (setq files (car entry) desc (cdr entry))
  10022. (setq fast-path-p nil)
  10023. (cond
  10024. ((null files) (setq files (list (current-buffer))))
  10025. ((eq files 'org-agenda-files)
  10026. (setq files (org-agenda-files 'unrestricted)))
  10027. ((and (symbolp files) (fboundp files))
  10028. (setq files (funcall files)))
  10029. ((and (symbolp files) (boundp files))
  10030. (setq files (symbol-value files))))
  10031. (if (stringp files) (setq files (list files)))
  10032. (cond
  10033. ((eq (car desc) :tag)
  10034. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  10035. ((eq (car desc) :todo)
  10036. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  10037. ((eq (car desc) :regexp)
  10038. (setq descre (cdr desc)))
  10039. ((eq (car desc) :level)
  10040. (setq descre (concat "^\\*\\{" (number-to-string
  10041. (if org-odd-levels-only
  10042. (1- (* 2 (cdr desc)))
  10043. (cdr desc)))
  10044. "\\}[ \t]")))
  10045. ((eq (car desc) :maxlevel)
  10046. (setq fast-path-p t)
  10047. (setq descre (concat "^\\*\\{1," (number-to-string
  10048. (if org-odd-levels-only
  10049. (1- (* 2 (cdr desc)))
  10050. (cdr desc)))
  10051. "\\}[ \t]")))
  10052. (t (error "Bad refiling target description %s" desc)))
  10053. (while (setq f (pop files))
  10054. (with-current-buffer
  10055. (if (bufferp f) f (org-get-agenda-file-buffer f))
  10056. (or
  10057. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  10058. (progn
  10059. (if (bufferp f) (setq f (buffer-file-name
  10060. (buffer-base-buffer f))))
  10061. (setq f (and f (expand-file-name f)))
  10062. (if (eq org-refile-use-outline-path 'file)
  10063. (push (list (file-name-nondirectory f) f nil nil) tgs))
  10064. (save-excursion
  10065. (save-restriction
  10066. (widen)
  10067. (goto-char (point-min))
  10068. (while (re-search-forward descre nil t)
  10069. (goto-char (setq pos0 (point-at-bol)))
  10070. (catch 'next
  10071. (when org-refile-target-verify-function
  10072. (save-match-data
  10073. (or (funcall org-refile-target-verify-function)
  10074. (throw 'next t))))
  10075. (when (and (looking-at org-complex-heading-regexp)
  10076. (not (member (match-string 4) excluded-entries))
  10077. (match-string 4))
  10078. (setq level (org-reduced-level
  10079. (- (match-end 1) (match-beginning 1)))
  10080. txt (org-link-display-format (match-string 4))
  10081. txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
  10082. re (format org-complex-heading-regexp-format
  10083. (regexp-quote (match-string 4))))
  10084. (when org-refile-use-outline-path
  10085. (setq txt (mapconcat
  10086. 'org-protect-slash
  10087. (append
  10088. (if (eq org-refile-use-outline-path
  10089. 'file)
  10090. (list (file-name-nondirectory
  10091. (buffer-file-name
  10092. (buffer-base-buffer))))
  10093. (if (eq org-refile-use-outline-path
  10094. 'full-file-path)
  10095. (list (buffer-file-name
  10096. (buffer-base-buffer)))))
  10097. (org-get-outline-path fast-path-p
  10098. level txt)
  10099. (list txt))
  10100. "/")))
  10101. (push (list txt f re (org-refile-marker (point)))
  10102. tgs)))
  10103. (when (= (point) pos0)
  10104. ;; verification function has not moved point
  10105. (goto-char (point-at-eol))))))))
  10106. (when org-refile-use-cache
  10107. (org-refile-cache-put tgs (buffer-file-name) descre))
  10108. (setq targets (append tgs targets))
  10109. ))))
  10110. (message "Getting targets...done")
  10111. (nreverse targets)))
  10112. (defun org-protect-slash (s)
  10113. (while (string-match "/" s)
  10114. (setq s (replace-match "\\" t t s)))
  10115. s)
  10116. (defvar org-olpa (make-vector 20 nil))
  10117. (defun org-get-outline-path (&optional fastp level heading)
  10118. "Return the outline path to the current entry, as a list.
  10119. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  10120. routine which makes outline path derivations for an entire file,
  10121. avoiding backtracing. Refile target collection makes use of that."
  10122. (if fastp
  10123. (progn
  10124. (if (> level 19)
  10125. (error "Outline path failure, more than 19 levels"))
  10126. (loop for i from level upto 19 do
  10127. (aset org-olpa i nil))
  10128. (prog1
  10129. (delq nil (append org-olpa nil))
  10130. (aset org-olpa level heading)))
  10131. (let (rtn case-fold-search)
  10132. (save-excursion
  10133. (save-restriction
  10134. (widen)
  10135. (while (org-up-heading-safe)
  10136. (when (looking-at org-complex-heading-regexp)
  10137. (push (org-match-string-no-properties 4) rtn)))
  10138. rtn)))))
  10139. (defun org-format-outline-path (path &optional width prefix separator)
  10140. "Format the outline path PATH for display.
  10141. WIDTH is the maximum number of characters that is available.
  10142. PREFIX is a prefix to be included in the returned string,
  10143. such as the file name.
  10144. SEPARATOR is inserted between the different parts of the path,
  10145. the default is \"/\"."
  10146. (setq width (or width 79))
  10147. (if prefix (setq width (- width (length prefix))))
  10148. (if (not path)
  10149. (or prefix "")
  10150. (let* ((nsteps (length path))
  10151. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  10152. (maxwidth (if (<= total-width width)
  10153. 10000 ;; everything fits
  10154. ;; we need to shorten the level headings
  10155. (/ (- width nsteps) nsteps)))
  10156. (org-odd-levels-only nil)
  10157. (n 0)
  10158. (total (1+ (length prefix))))
  10159. (setq maxwidth (max maxwidth 10))
  10160. (concat prefix
  10161. (if prefix (or separator "/"))
  10162. (mapconcat
  10163. (lambda (h)
  10164. (setq n (1+ n))
  10165. (if (and (= n nsteps) (< maxwidth 10000))
  10166. (setq maxwidth (- total-width total)))
  10167. (if (< (length h) maxwidth)
  10168. (progn (setq total (+ total (length h) 1)) h)
  10169. (setq h (substring h 0 (- maxwidth 2))
  10170. total (+ total maxwidth 1))
  10171. (if (string-match "[ \t]+\\'" h)
  10172. (setq h (substring h 0 (match-beginning 0))))
  10173. (setq h (concat h "..")))
  10174. (org-add-props h nil 'face
  10175. (nth (% (1- n) org-n-level-faces)
  10176. org-level-faces))
  10177. h)
  10178. path (or separator "/"))))))
  10179. (defun org-display-outline-path (&optional file current separator just-return-string)
  10180. "Display the current outline path in the echo area.
  10181. If FILE is non-nil, prepend the output with the file name.
  10182. If CURRENT is non-nil, append the current heading to the output.
  10183. SEPARATOR is passed through to `org-format-outline-path'. It separates
  10184. the different parts of the path and defaults to \"/\".
  10185. If JUST-RETURN-STRING is non-nil, return a string, don't display a message."
  10186. (interactive "P")
  10187. (let* (case-fold-search
  10188. message-log-max ; Don't populate the *Messages* buffer
  10189. (bfn (buffer-file-name (buffer-base-buffer)))
  10190. (path (and (derived-mode-p 'org-mode) (org-get-outline-path)))
  10191. res)
  10192. (if current (setq path (append path
  10193. (save-excursion
  10194. (org-back-to-heading t)
  10195. (if (looking-at org-complex-heading-regexp)
  10196. (list (match-string 4)))))))
  10197. (setq res
  10198. (org-format-outline-path
  10199. path
  10200. (1- (frame-width))
  10201. (and file bfn (concat (file-name-nondirectory bfn) separator))
  10202. separator))
  10203. (if just-return-string
  10204. (org-no-properties res)
  10205. (message "%s" res))))
  10206. (defvar org-refile-history nil
  10207. "History for refiling operations.")
  10208. (defvar org-after-refile-insert-hook nil
  10209. "Hook run after `org-refile' has inserted its stuff at the new location.
  10210. Note that this is still *before* the stuff will be removed from
  10211. the *old* location.")
  10212. (defvar org-capture-last-stored-marker)
  10213. (defvar org-refile-keep nil
  10214. "Non-nil means `org-refile' will copy instead of refile.")
  10215. (defun org-copy ()
  10216. "Like `org-refile', but copy."
  10217. (interactive)
  10218. (let ((org-refile-keep t))
  10219. (funcall 'org-refile nil nil nil "Copy")))
  10220. (defun org-refile (&optional goto default-buffer rfloc msg)
  10221. "Move the entry or entries at point to another heading.
  10222. The list of target headings is compiled using the information in
  10223. `org-refile-targets', which see.
  10224. At the target location, the entry is filed as a subitem of the target
  10225. heading. Depending on `org-reverse-note-order', the new subitem will
  10226. either be the first or the last subitem.
  10227. If there is an active region, all entries in that region will be moved.
  10228. However, the region must fulfill the requirement that the first heading
  10229. is the first one sets the top-level of the moved text - at most siblings
  10230. below it are allowed.
  10231. With prefix arg GOTO, the command will only visit the target location
  10232. and not actually move anything.
  10233. With a double prefix arg \\[universal-argument] \\[universal-argument], \
  10234. go to the location where the last refiling operation has put the subtree.
  10235. With a prefix argument of `2', refile to the running clock.
  10236. RFLOC can be a refile location obtained in a different way.
  10237. MSG is a string to replace \"Refile\" in the default prompt with
  10238. another verb. E.g. `org-copy' sets this parameter to \"Copy\".
  10239. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  10240. If you are using target caching (see `org-refile-use-cache'),
  10241. you have to clear the target cache in order to find new targets.
  10242. This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
  10243. prefix argument (`C-u C-u C-u C-c C-w')."
  10244. (interactive "P")
  10245. (if (member goto '(0 (64)))
  10246. (org-refile-cache-clear)
  10247. (let* ((actionmsg (or msg "Refile"))
  10248. (cbuf (current-buffer))
  10249. (regionp (org-region-active-p))
  10250. (region-start (and regionp (region-beginning)))
  10251. (region-end (and regionp (region-end)))
  10252. (region-length (and regionp (- region-end region-start)))
  10253. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  10254. pos it nbuf file re level reversed)
  10255. (setq last-command nil)
  10256. (when regionp
  10257. (goto-char region-start)
  10258. (or (bolp) (goto-char (point-at-bol)))
  10259. (setq region-start (point))
  10260. (unless (or (org-kill-is-subtree-p
  10261. (buffer-substring region-start region-end))
  10262. (prog1 org-refile-active-region-within-subtree
  10263. (org-toggle-heading)))
  10264. (error "The region is not a (sequence of) subtree(s)")))
  10265. (if (equal goto '(16))
  10266. (org-refile-goto-last-stored)
  10267. (when (or
  10268. (and (equal goto 2)
  10269. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  10270. (prog1
  10271. (setq it (list (or org-clock-heading "running clock")
  10272. (buffer-file-name
  10273. (marker-buffer org-clock-hd-marker))
  10274. ""
  10275. (marker-position org-clock-hd-marker)))
  10276. (setq goto nil)))
  10277. (setq it (or rfloc
  10278. (let (heading-text)
  10279. (save-excursion
  10280. (unless goto
  10281. (org-back-to-heading t)
  10282. (setq heading-text
  10283. (nth 4 (org-heading-components))))
  10284. (org-refile-get-location
  10285. (cond (goto "Goto")
  10286. (regionp (concat actionmsg " region to"))
  10287. (t (concat actionmsg " subtree \""
  10288. heading-text "\" to")))
  10289. default-buffer
  10290. (and (not (equal '(4) goto))
  10291. org-refile-allow-creating-parent-nodes)
  10292. goto))))))
  10293. (setq file (nth 1 it)
  10294. re (nth 2 it)
  10295. pos (nth 3 it))
  10296. (if (and (not goto)
  10297. pos
  10298. (equal (buffer-file-name) file)
  10299. (if regionp
  10300. (and (>= pos region-start)
  10301. (<= pos region-end))
  10302. (and (>= pos (point))
  10303. (< pos (save-excursion
  10304. (org-end-of-subtree t t))))))
  10305. (error "Cannot refile to position inside the tree or region"))
  10306. (setq nbuf (or (find-buffer-visiting file)
  10307. (find-file-noselect file)))
  10308. (if goto
  10309. (progn
  10310. (org-pop-to-buffer-same-window nbuf)
  10311. (goto-char pos)
  10312. (org-show-context 'org-goto))
  10313. (if regionp
  10314. (progn
  10315. (org-kill-new (buffer-substring region-start region-end))
  10316. (org-save-markers-in-region region-start region-end))
  10317. (org-copy-subtree 1 nil t))
  10318. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  10319. (find-file-noselect file)))
  10320. (setq reversed (org-notes-order-reversed-p))
  10321. (save-excursion
  10322. (save-restriction
  10323. (widen)
  10324. (if pos
  10325. (progn
  10326. (goto-char pos)
  10327. (looking-at org-outline-regexp)
  10328. (setq level (org-get-valid-level (funcall outline-level) 1))
  10329. (goto-char
  10330. (if reversed
  10331. (or (outline-next-heading) (point-max))
  10332. (or (save-excursion (org-get-next-sibling))
  10333. (org-end-of-subtree t t)
  10334. (point-max)))))
  10335. (setq level 1)
  10336. (if (not reversed)
  10337. (goto-char (point-max))
  10338. (goto-char (point-min))
  10339. (or (outline-next-heading) (goto-char (point-max)))))
  10340. (if (not (bolp)) (newline))
  10341. (org-paste-subtree level)
  10342. (when org-log-refile
  10343. (org-add-log-setup 'refile nil nil 'findpos
  10344. org-log-refile)
  10345. (unless (eq org-log-refile 'note)
  10346. (save-excursion (org-add-log-note))))
  10347. (and org-auto-align-tags
  10348. (let ((org-loop-over-headlines-in-active-region nil))
  10349. (org-set-tags nil t)))
  10350. (with-demoted-errors
  10351. (bookmark-set "org-refile-last-stored"))
  10352. ;; If we are refiling for capture, make sure that the
  10353. ;; last-capture pointers point here
  10354. (when (org-bound-and-true-p org-refile-for-capture)
  10355. (with-demoted-errors
  10356. (bookmark-set "org-capture-last-stored-marker"))
  10357. (move-marker org-capture-last-stored-marker (point)))
  10358. (if (fboundp 'deactivate-mark) (deactivate-mark))
  10359. (run-hooks 'org-after-refile-insert-hook))))
  10360. (unless org-refile-keep
  10361. (if regionp
  10362. (delete-region (point) (+ (point) region-length))
  10363. (org-cut-subtree)))
  10364. (when (featurep 'org-inlinetask)
  10365. (org-inlinetask-remove-END-maybe))
  10366. (setq org-markers-to-move nil)
  10367. (message (concat actionmsg " to \"%s\" in file %s: done") (car it) file)))))))
  10368. (defun org-refile-goto-last-stored ()
  10369. "Go to the location where the last refile was stored."
  10370. (interactive)
  10371. (bookmark-jump "org-refile-last-stored")
  10372. (message "This is the location of the last refile"))
  10373. (defun org-refile-get-location (&optional prompt default-buffer new-nodes
  10374. no-exclude)
  10375. "Prompt the user for a refile location, using PROMPT.
  10376. PROMPT should not be suffixed with a colon and a space, because
  10377. this function appends the default value from
  10378. `org-refile-history' automatically, if that is not empty.
  10379. When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
  10380. this is used for the GOTO interface."
  10381. (let ((org-refile-targets org-refile-targets)
  10382. (org-refile-use-outline-path org-refile-use-outline-path)
  10383. excluded-entries)
  10384. (when (and (derived-mode-p 'org-mode)
  10385. (not org-refile-use-cache)
  10386. (not no-exclude))
  10387. (org-map-tree
  10388. (lambda()
  10389. (setq excluded-entries
  10390. (append excluded-entries (list (org-get-heading t t)))))))
  10391. (setq org-refile-target-table
  10392. (org-refile-get-targets default-buffer excluded-entries)))
  10393. (unless org-refile-target-table
  10394. (error "No refile targets"))
  10395. (let* ((cbuf (current-buffer))
  10396. (partial-completion-mode nil)
  10397. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  10398. (cfunc (if (and org-refile-use-outline-path
  10399. org-outline-path-complete-in-steps)
  10400. 'org-olpath-completing-read
  10401. 'org-icompleting-read))
  10402. (extra (if org-refile-use-outline-path "/" ""))
  10403. (cbnex (concat (buffer-name) extra))
  10404. (filename (and cfn (expand-file-name cfn)))
  10405. (tbl (mapcar
  10406. (lambda (x)
  10407. (if (and (not (member org-refile-use-outline-path
  10408. '(file full-file-path)))
  10409. (not (equal filename (nth 1 x))))
  10410. (cons (concat (car x) extra " ("
  10411. (file-name-nondirectory (nth 1 x)) ")")
  10412. (cdr x))
  10413. (cons (concat (car x) extra) (cdr x))))
  10414. org-refile-target-table))
  10415. (completion-ignore-case t)
  10416. cdef
  10417. (prompt (concat prompt
  10418. (or (and (car org-refile-history)
  10419. (concat " (default " (car org-refile-history) ")"))
  10420. (and (assoc cbnex tbl) (setq cdef cbnex)
  10421. (concat " (default " cbnex ")"))) ": "))
  10422. pa answ parent-target child parent old-hist)
  10423. (setq old-hist org-refile-history)
  10424. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  10425. nil 'org-refile-history (or cdef (car org-refile-history))))
  10426. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  10427. (org-refile-check-position pa)
  10428. (if pa
  10429. (progn
  10430. (when (or (not org-refile-history)
  10431. (not (eq old-hist org-refile-history))
  10432. (not (equal (car pa) (car org-refile-history))))
  10433. (setq org-refile-history
  10434. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  10435. org-refile-history
  10436. (cdr org-refile-history))))
  10437. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  10438. (pop org-refile-history)))
  10439. pa)
  10440. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  10441. (progn
  10442. (setq parent (match-string 1 answ)
  10443. child (match-string 2 answ))
  10444. (setq parent-target (or (assoc parent tbl)
  10445. (assoc (concat parent "/") tbl)))
  10446. (when (and parent-target
  10447. (or (eq new-nodes t)
  10448. (and (eq new-nodes 'confirm)
  10449. (y-or-n-p (format "Create new node \"%s\"? "
  10450. child)))))
  10451. (org-refile-new-child parent-target child)))
  10452. (error "Invalid target location")))))
  10453. (declare-function org-string-nw-p "org-macs" (s))
  10454. (defun org-refile-check-position (refile-pointer)
  10455. "Check if the refile pointer matches the headline to which it points."
  10456. (let* ((file (nth 1 refile-pointer))
  10457. (re (nth 2 refile-pointer))
  10458. (pos (nth 3 refile-pointer))
  10459. buffer)
  10460. (if (and (not (markerp pos)) (not file))
  10461. (error "Please save the buffer to a file before refiling")
  10462. (when (org-string-nw-p re)
  10463. (setq buffer (if (markerp pos)
  10464. (marker-buffer pos)
  10465. (or (find-buffer-visiting file)
  10466. (find-file-noselect file))))
  10467. (with-current-buffer buffer
  10468. (save-excursion
  10469. (save-restriction
  10470. (widen)
  10471. (goto-char pos)
  10472. (beginning-of-line 1)
  10473. (unless (org-looking-at-p re)
  10474. (error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling")))))))))
  10475. (defun org-refile-new-child (parent-target child)
  10476. "Use refile target PARENT-TARGET to add new CHILD below it."
  10477. (unless parent-target
  10478. (error "Cannot find parent for new node"))
  10479. (let ((file (nth 1 parent-target))
  10480. (pos (nth 3 parent-target))
  10481. level)
  10482. (with-current-buffer (or (find-buffer-visiting file)
  10483. (find-file-noselect file))
  10484. (save-excursion
  10485. (save-restriction
  10486. (widen)
  10487. (if pos
  10488. (goto-char pos)
  10489. (goto-char (point-max))
  10490. (if (not (bolp)) (newline)))
  10491. (when (looking-at org-outline-regexp)
  10492. (setq level (funcall outline-level))
  10493. (org-end-of-subtree t t))
  10494. (org-back-over-empty-lines)
  10495. (insert "\n" (make-string
  10496. (if pos (org-get-valid-level level 1) 1) ?*)
  10497. " " child "\n")
  10498. (beginning-of-line 0)
  10499. (list (concat (car parent-target) "/" child) file "" (point)))))))
  10500. (defun org-olpath-completing-read (prompt collection &rest args)
  10501. "Read an outline path like a file name."
  10502. (let ((thetable collection)
  10503. (org-completion-use-ido nil) ; does not work with ido.
  10504. (org-completion-use-iswitchb nil)) ; or iswitchb
  10505. (apply
  10506. 'org-icompleting-read prompt
  10507. (lambda (string predicate &optional flag)
  10508. (let (rtn r f (l (length string)))
  10509. (cond
  10510. ((eq flag nil)
  10511. ;; try completion
  10512. (try-completion string thetable))
  10513. ((eq flag t)
  10514. ;; all-completions
  10515. (setq rtn (all-completions string thetable predicate))
  10516. (mapcar
  10517. (lambda (x)
  10518. (setq r (substring x l))
  10519. (if (string-match " ([^)]*)$" x)
  10520. (setq f (match-string 0 x))
  10521. (setq f ""))
  10522. (if (string-match "/" r)
  10523. (concat string (substring r 0 (match-end 0)) f)
  10524. x))
  10525. rtn))
  10526. ((eq flag 'lambda)
  10527. ;; exact match?
  10528. (assoc string thetable)))))
  10529. args)))
  10530. ;;;; Dynamic blocks
  10531. (defun org-find-dblock (name)
  10532. "Find the first dynamic block with name NAME in the buffer.
  10533. If not found, stay at current position and return nil."
  10534. (let ((case-fold-search t) pos)
  10535. (save-excursion
  10536. (goto-char (point-min))
  10537. (setq pos (and (re-search-forward
  10538. (concat "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+" name "\\>") nil t)
  10539. (match-beginning 0))))
  10540. (if pos (goto-char pos))
  10541. pos))
  10542. (defconst org-dblock-start-re
  10543. "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  10544. "Matches the start line of a dynamic block, with parameters.")
  10545. (defconst org-dblock-end-re "^[ \t]*#\\+\\(?:END\\|end\\)\\([: \t\r\n]\\|$\\)"
  10546. "Matches the end of a dynamic block.")
  10547. (defun org-create-dblock (plist)
  10548. "Create a dynamic block section, with parameters taken from PLIST.
  10549. PLIST must contain a :name entry which is used as name of the block."
  10550. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  10551. (end-of-line 1)
  10552. (newline))
  10553. (let ((col (current-column))
  10554. (name (plist-get plist :name)))
  10555. (insert "#+BEGIN: " name)
  10556. (while plist
  10557. (if (eq (car plist) :name)
  10558. (setq plist (cddr plist))
  10559. (insert " " (prin1-to-string (pop plist)))))
  10560. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  10561. (beginning-of-line -2)))
  10562. (defun org-prepare-dblock ()
  10563. "Prepare dynamic block for refresh.
  10564. This empties the block, puts the cursor at the insert position and returns
  10565. the property list including an extra property :name with the block name."
  10566. (unless (looking-at org-dblock-start-re)
  10567. (error "Not at a dynamic block"))
  10568. (let* ((begdel (1+ (match-end 0)))
  10569. (name (org-no-properties (match-string 1)))
  10570. (params (append (list :name name)
  10571. (read (concat "(" (match-string 3) ")")))))
  10572. (save-excursion
  10573. (beginning-of-line 1)
  10574. (skip-chars-forward " \t")
  10575. (setq params (plist-put params :indentation-column (current-column))))
  10576. (unless (re-search-forward org-dblock-end-re nil t)
  10577. (error "Dynamic block not terminated"))
  10578. (setq params
  10579. (append params
  10580. (list :content (buffer-substring
  10581. begdel (match-beginning 0)))))
  10582. (delete-region begdel (match-beginning 0))
  10583. (goto-char begdel)
  10584. (open-line 1)
  10585. params))
  10586. (defun org-map-dblocks (&optional command)
  10587. "Apply COMMAND to all dynamic blocks in the current buffer.
  10588. If COMMAND is not given, use `org-update-dblock'."
  10589. (let ((cmd (or command 'org-update-dblock)))
  10590. (save-excursion
  10591. (goto-char (point-min))
  10592. (while (re-search-forward org-dblock-start-re nil t)
  10593. (goto-char (match-beginning 0))
  10594. (save-excursion
  10595. (condition-case nil
  10596. (funcall cmd)
  10597. (error (message "Error during update of dynamic block"))))
  10598. (unless (re-search-forward org-dblock-end-re nil t)
  10599. (error "Dynamic block not terminated"))))))
  10600. (defun org-dblock-update (&optional arg)
  10601. "User command for updating dynamic blocks.
  10602. Update the dynamic block at point. With prefix ARG, update all dynamic
  10603. blocks in the buffer."
  10604. (interactive "P")
  10605. (if arg
  10606. (org-update-all-dblocks)
  10607. (or (looking-at org-dblock-start-re)
  10608. (org-beginning-of-dblock))
  10609. (org-update-dblock)))
  10610. (defun org-update-dblock ()
  10611. "Update the dynamic block at point.
  10612. This means to empty the block, parse for parameters and then call
  10613. the correct writing function."
  10614. (interactive)
  10615. (save-window-excursion
  10616. (let* ((pos (point))
  10617. (line (org-current-line))
  10618. (params (org-prepare-dblock))
  10619. (name (plist-get params :name))
  10620. (indent (plist-get params :indentation-column))
  10621. (cmd (intern (concat "org-dblock-write:" name))))
  10622. (message "Updating dynamic block `%s' at line %d..." name line)
  10623. (funcall cmd params)
  10624. (message "Updating dynamic block `%s' at line %d...done" name line)
  10625. (goto-char pos)
  10626. (when (and indent (> indent 0))
  10627. (setq indent (make-string indent ?\ ))
  10628. (save-excursion
  10629. (org-beginning-of-dblock)
  10630. (forward-line 1)
  10631. (while (not (looking-at org-dblock-end-re))
  10632. (insert indent)
  10633. (beginning-of-line 2))
  10634. (when (looking-at org-dblock-end-re)
  10635. (and (looking-at "[ \t]+")
  10636. (replace-match ""))
  10637. (insert indent)))))))
  10638. (defun org-beginning-of-dblock ()
  10639. "Find the beginning of the dynamic block at point.
  10640. Error if there is no such block at point."
  10641. (let ((pos (point))
  10642. beg)
  10643. (end-of-line 1)
  10644. (if (and (re-search-backward org-dblock-start-re nil t)
  10645. (setq beg (match-beginning 0))
  10646. (re-search-forward org-dblock-end-re nil t)
  10647. (> (match-end 0) pos))
  10648. (goto-char beg)
  10649. (goto-char pos)
  10650. (error "Not in a dynamic block"))))
  10651. (defun org-update-all-dblocks ()
  10652. "Update all dynamic blocks in the buffer.
  10653. This function can be used in a hook."
  10654. (interactive)
  10655. (when (derived-mode-p 'org-mode)
  10656. (org-map-dblocks 'org-update-dblock)))
  10657. ;;;; Completion
  10658. (defun org-get-export-keywords ()
  10659. "Return a list of all currently understood export keywords.
  10660. Export keywords include options, block names, attributes and
  10661. keywords relative to each registered export back-end."
  10662. (delq nil
  10663. (let (keywords)
  10664. (mapc
  10665. (lambda (back-end)
  10666. (let ((props (cdr back-end)))
  10667. ;; Back-end name (for keywords, like #+LATEX:)
  10668. (push (upcase (symbol-name (car back-end))) keywords)
  10669. ;; Back-end options.
  10670. (mapc (lambda (option) (push (cadr option) keywords))
  10671. (plist-get (cdr back-end) :options-alist))))
  10672. (org-bound-and-true-p org-export-registered-backends))
  10673. keywords)))
  10674. (defconst org-options-keywords
  10675. '("ARCHIVE:" "AUTHOR:" "BIND:" "CATEGORY:" "COLUMNS:" "CREATOR:" "DATE:"
  10676. "DESCRIPTION:" "DRAWERS:" "EMAIL:" "EXCLUDE_TAGS:" "FILETAGS:" "INCLUDE:"
  10677. "INDEX:" "KEYWORDS:" "LANGUAGE:" "MACRO:" "OPTIONS:" "PROPERTY:"
  10678. "PRIORITIES:" "SELECT_TAGS:" "SEQ_TODO:" "SETUPFILE:" "STARTUP:" "TAGS:"
  10679. "TITLE:" "TODO:" "TYP_TODO:" "SELECT_TAGS:" "EXCLUDE_TAGS:"))
  10680. (defcustom org-structure-template-alist
  10681. '(("s" "#+BEGIN_SRC ?\n\n#+END_SRC"
  10682. "<src lang=\"?\">\n\n</src>")
  10683. ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE"
  10684. "<example>\n?\n</example>")
  10685. ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE"
  10686. "<quote>\n?\n</quote>")
  10687. ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE"
  10688. "<verse>\n?\n</verse>")
  10689. ("V" "#+BEGIN_VERBATIM\n?\n#+END_VERBATIM"
  10690. "<verbatim>\n?\n</verbatim>")
  10691. ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER"
  10692. "<center>\n?\n</center>")
  10693. ("l" "#+BEGIN_LaTeX\n?\n#+END_LaTeX"
  10694. "<literal style=\"latex\">\n?\n</literal>")
  10695. ("L" "#+LaTeX: "
  10696. "<literal style=\"latex\">?</literal>")
  10697. ("h" "#+BEGIN_HTML\n?\n#+END_HTML"
  10698. "<literal style=\"html\">\n?\n</literal>")
  10699. ("H" "#+HTML: "
  10700. "<literal style=\"html\">?</literal>")
  10701. ("a" "#+BEGIN_ASCII\n?\n#+END_ASCII")
  10702. ("A" "#+ASCII: ")
  10703. ("i" "#+INDEX: ?"
  10704. "#+INDEX: ?")
  10705. ("I" "#+INCLUDE: %file ?"
  10706. "<include file=%file markup=\"?\">"))
  10707. "Structure completion elements.
  10708. This is a list of abbreviation keys and values. The value gets inserted
  10709. if you type `<' followed by the key and then press the completion key,
  10710. usually `M-TAB'. %file will be replaced by a file name after prompting
  10711. for the file using completion. The cursor will be placed at the position
  10712. of the `?` in the template.
  10713. There are two templates for each key, the first uses the original Org syntax,
  10714. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  10715. the default when the /org-mtags.el/ module has been loaded. See also the
  10716. variable `org-mtags-prefer-muse-templates'."
  10717. :group 'org-completion
  10718. :type '(repeat
  10719. (string :tag "Key")
  10720. (string :tag "Template")
  10721. (string :tag "Muse Template")))
  10722. (defun org-try-structure-completion ()
  10723. "Try to complete a structure template before point.
  10724. This looks for strings like \"<e\" on an otherwise empty line and
  10725. expands them."
  10726. (let ((l (buffer-substring (point-at-bol) (point)))
  10727. a)
  10728. (when (and (looking-at "[ \t]*$")
  10729. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  10730. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  10731. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  10732. (match-beginning 1)) a)
  10733. t)))
  10734. (defun org-complete-expand-structure-template (start cell)
  10735. "Expand a structure template."
  10736. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  10737. (rpl (nth (if musep 2 1) cell))
  10738. (ind ""))
  10739. (delete-region start (point))
  10740. (when (string-match "\\`#\\+" rpl)
  10741. (cond
  10742. ((bolp))
  10743. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  10744. (setq ind (buffer-substring (point-at-bol) (point))))
  10745. (t (newline))))
  10746. (setq start (point))
  10747. (if (string-match "%file" rpl)
  10748. (setq rpl (replace-match
  10749. (concat
  10750. "\""
  10751. (save-match-data
  10752. (abbreviate-file-name (read-file-name "Include file: ")))
  10753. "\"")
  10754. t t rpl)))
  10755. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  10756. (concat "\n" ind)))
  10757. (insert rpl)
  10758. (if (re-search-backward "\\?" start t) (delete-char 1))))
  10759. ;;;; TODO, DEADLINE, Comments
  10760. (defun org-toggle-comment ()
  10761. "Change the COMMENT state of an entry."
  10762. (interactive)
  10763. (save-excursion
  10764. (org-back-to-heading)
  10765. (let (case-fold-search)
  10766. (cond
  10767. ((looking-at (format org-heading-keyword-regexp-format
  10768. org-comment-string))
  10769. (goto-char (match-end 1))
  10770. (looking-at (concat " +" org-comment-string))
  10771. (replace-match "" t t)
  10772. (when (eolp) (insert " ")))
  10773. ((looking-at org-outline-regexp)
  10774. (goto-char (match-end 0))
  10775. (insert org-comment-string " "))))))
  10776. (defvar org-last-todo-state-is-todo nil
  10777. "This is non-nil when the last TODO state change led to a TODO state.
  10778. If the last change removed the TODO tag or switched to DONE, then
  10779. this is nil.")
  10780. (defvar org-setting-tags nil) ; dynamically skipped
  10781. (defvar org-todo-setup-filter-hook nil
  10782. "Hook for functions that pre-filter todo specs.
  10783. Each function takes a todo spec and returns either nil or the spec
  10784. transformed into canonical form." )
  10785. (defvar org-todo-get-default-hook nil
  10786. "Hook for functions that get a default item for todo.
  10787. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  10788. nil or a string to be used for the todo mark." )
  10789. (defvar org-agenda-headline-snapshot-before-repeat)
  10790. (defun org-current-effective-time ()
  10791. "Return current time adjusted for `org-extend-today-until' variable."
  10792. (let* ((ct (org-current-time))
  10793. (dct (decode-time ct))
  10794. (ct1
  10795. (cond
  10796. (org-use-last-clock-out-time-as-effective-time
  10797. (or (org-clock-get-last-clock-out-time) ct))
  10798. ((and org-use-effective-time (< (nth 2 dct) org-extend-today-until))
  10799. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct)))
  10800. (t ct))))
  10801. ct1))
  10802. (defun org-todo-yesterday (&optional arg)
  10803. "Like `org-todo' but the time of change will be 23:59 of yesterday."
  10804. (interactive "P")
  10805. (if (eq major-mode 'org-agenda-mode)
  10806. (apply 'org-agenda-todo-yesterday arg)
  10807. (let* ((hour (third (decode-time
  10808. (org-current-time))))
  10809. (org-extend-today-until (1+ hour)))
  10810. (org-todo arg))))
  10811. (defvar org-block-entry-blocking ""
  10812. "First entry preventing the TODO state change.")
  10813. (defun org-todo (&optional arg)
  10814. "Change the TODO state of an item.
  10815. The state of an item is given by a keyword at the start of the heading,
  10816. like
  10817. *** TODO Write paper
  10818. *** DONE Call mom
  10819. The different keywords are specified in the variable `org-todo-keywords'.
  10820. By default the available states are \"TODO\" and \"DONE\".
  10821. So for this example: when the item starts with TODO, it is changed to DONE.
  10822. When it starts with DONE, the DONE is removed. And when neither TODO nor
  10823. DONE are present, add TODO at the beginning of the heading.
  10824. With \\[universal-argument] prefix arg, use completion to determine the new \
  10825. state.
  10826. With numeric prefix arg, switch to that state.
  10827. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  10828. keywords (nextset).
  10829. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  10830. With a numeric prefix arg of 0, inhibit note taking for the change.
  10831. For calling through lisp, arg is also interpreted in the following way:
  10832. 'none -> empty state
  10833. \"\"(empty string) -> switch to empty state
  10834. 'done -> switch to DONE
  10835. 'nextset -> switch to the next set of keywords
  10836. 'previousset -> switch to the previous set of keywords
  10837. \"WAITING\" -> switch to the specified keyword, but only if it
  10838. really is a member of `org-todo-keywords'."
  10839. (interactive "P")
  10840. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  10841. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  10842. 'region-start-level 'region))
  10843. org-loop-over-headlines-in-active-region)
  10844. (org-map-entries
  10845. `(org-todo ,arg)
  10846. org-loop-over-headlines-in-active-region
  10847. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  10848. (if (equal arg '(16)) (setq arg 'nextset))
  10849. (let ((org-blocker-hook org-blocker-hook)
  10850. commentp
  10851. case-fold-search)
  10852. (when (equal arg '(64))
  10853. (setq arg nil org-blocker-hook nil))
  10854. (when (and org-blocker-hook
  10855. (or org-inhibit-blocking
  10856. (org-entry-get nil "NOBLOCKING")))
  10857. (setq org-blocker-hook nil))
  10858. (save-excursion
  10859. (catch 'exit
  10860. (org-back-to-heading t)
  10861. (when (looking-at (concat "^\\*+ " org-comment-string))
  10862. (org-toggle-comment)
  10863. (setq commentp t))
  10864. (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
  10865. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
  10866. (looking-at "\\(?: *\\|[ \t]*$\\)"))
  10867. (let* ((match-data (match-data))
  10868. (startpos (point-at-bol))
  10869. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  10870. (org-log-done org-log-done)
  10871. (org-log-repeat org-log-repeat)
  10872. (org-todo-log-states org-todo-log-states)
  10873. (org-inhibit-logging
  10874. (if (equal arg 0)
  10875. (progn (setq arg nil) 'note) org-inhibit-logging))
  10876. (this (match-string 1))
  10877. (hl-pos (match-beginning 0))
  10878. (head (org-get-todo-sequence-head this))
  10879. (ass (assoc head org-todo-kwd-alist))
  10880. (interpret (nth 1 ass))
  10881. (done-word (nth 3 ass))
  10882. (final-done-word (nth 4 ass))
  10883. (org-last-state (or this ""))
  10884. (completion-ignore-case t)
  10885. (member (member this org-todo-keywords-1))
  10886. (tail (cdr member))
  10887. (org-state (cond
  10888. ((and org-todo-key-trigger
  10889. (or (and (equal arg '(4))
  10890. (eq org-use-fast-todo-selection 'prefix))
  10891. (and (not arg) org-use-fast-todo-selection
  10892. (not (eq org-use-fast-todo-selection
  10893. 'prefix)))))
  10894. ;; Use fast selection
  10895. (org-fast-todo-selection))
  10896. ((and (equal arg '(4))
  10897. (or (not org-use-fast-todo-selection)
  10898. (not org-todo-key-trigger)))
  10899. ;; Read a state with completion
  10900. (org-icompleting-read
  10901. "State: " (mapcar (lambda(x) (list x))
  10902. org-todo-keywords-1)
  10903. nil t))
  10904. ((eq arg 'right)
  10905. (if this
  10906. (if tail (car tail) nil)
  10907. (car org-todo-keywords-1)))
  10908. ((eq arg 'left)
  10909. (if (equal member org-todo-keywords-1)
  10910. nil
  10911. (if this
  10912. (nth (- (length org-todo-keywords-1)
  10913. (length tail) 2)
  10914. org-todo-keywords-1)
  10915. (org-last org-todo-keywords-1))))
  10916. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  10917. (setq arg nil))) ; hack to fall back to cycling
  10918. (arg
  10919. ;; user or caller requests a specific state
  10920. (cond
  10921. ((equal arg "") nil)
  10922. ((eq arg 'none) nil)
  10923. ((eq arg 'done) (or done-word (car org-done-keywords)))
  10924. ((eq arg 'nextset)
  10925. (or (car (cdr (member head org-todo-heads)))
  10926. (car org-todo-heads)))
  10927. ((eq arg 'previousset)
  10928. (let ((org-todo-heads (reverse org-todo-heads)))
  10929. (or (car (cdr (member head org-todo-heads)))
  10930. (car org-todo-heads))))
  10931. ((car (member arg org-todo-keywords-1)))
  10932. ((stringp arg)
  10933. (error "State `%s' not valid in this file" arg))
  10934. ((nth (1- (prefix-numeric-value arg))
  10935. org-todo-keywords-1))))
  10936. ((null member) (or head (car org-todo-keywords-1)))
  10937. ((equal this final-done-word) nil) ;; -> make empty
  10938. ((null tail) nil) ;; -> first entry
  10939. ((memq interpret '(type priority))
  10940. (if (eq this-command last-command)
  10941. (car tail)
  10942. (if (> (length tail) 0)
  10943. (or done-word (car org-done-keywords))
  10944. nil)))
  10945. (t
  10946. (car tail))))
  10947. (org-state (or
  10948. (run-hook-with-args-until-success
  10949. 'org-todo-get-default-hook org-state org-last-state)
  10950. org-state))
  10951. (next (if org-state (concat " " org-state " ") " "))
  10952. (change-plist (list :type 'todo-state-change :from this :to org-state
  10953. :position startpos))
  10954. dolog now-done-p)
  10955. (when org-blocker-hook
  10956. (setq org-last-todo-state-is-todo
  10957. (not (member this org-done-keywords)))
  10958. (unless (save-excursion
  10959. (save-match-data
  10960. (org-with-wide-buffer
  10961. (run-hook-with-args-until-failure
  10962. 'org-blocker-hook change-plist))))
  10963. (if (org-called-interactively-p 'interactive)
  10964. (user-error "TODO state change from %s to %s blocked (by \"%s\")"
  10965. this org-state org-block-entry-blocking)
  10966. ;; fail silently
  10967. (message "TODO state change from %s to %s blocked (by \"%s\")"
  10968. this org-state org-block-entry-blocking)
  10969. (throw 'exit nil))))
  10970. (store-match-data match-data)
  10971. (replace-match next t t)
  10972. (unless (pos-visible-in-window-p hl-pos)
  10973. (message "TODO state changed to %s" (org-trim next)))
  10974. (unless head
  10975. (setq head (org-get-todo-sequence-head org-state)
  10976. ass (assoc head org-todo-kwd-alist)
  10977. interpret (nth 1 ass)
  10978. done-word (nth 3 ass)
  10979. final-done-word (nth 4 ass)))
  10980. (when (memq arg '(nextset previousset))
  10981. (message "Keyword-Set %d/%d: %s"
  10982. (- (length org-todo-sets) -1
  10983. (length (memq (assoc org-state org-todo-sets) org-todo-sets)))
  10984. (length org-todo-sets)
  10985. (mapconcat 'identity (assoc org-state org-todo-sets) " ")))
  10986. (setq org-last-todo-state-is-todo
  10987. (not (member org-state org-done-keywords)))
  10988. (setq now-done-p (and (member org-state org-done-keywords)
  10989. (not (member this org-done-keywords))))
  10990. (and logging (org-local-logging logging))
  10991. (when (and (or org-todo-log-states org-log-done)
  10992. (not (eq org-inhibit-logging t))
  10993. (not (memq arg '(nextset previousset))))
  10994. ;; we need to look at recording a time and note
  10995. (setq dolog (or (nth 1 (assoc org-state org-todo-log-states))
  10996. (nth 2 (assoc this org-todo-log-states))))
  10997. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  10998. (setq dolog 'time))
  10999. (when (and org-state
  11000. (member org-state org-not-done-keywords)
  11001. (not (member this org-not-done-keywords)))
  11002. ;; This is now a todo state and was not one before
  11003. ;; If there was a CLOSED time stamp, get rid of it.
  11004. (org-add-planning-info nil nil 'closed))
  11005. (when (and now-done-p org-log-done)
  11006. ;; It is now done, and it was not done before
  11007. (org-add-planning-info 'closed (org-current-effective-time))
  11008. (if (and (not dolog) (eq 'note org-log-done))
  11009. (org-add-log-setup 'done org-state this 'findpos 'note)))
  11010. (when (and org-state dolog)
  11011. ;; This is a non-nil state, and we need to log it
  11012. (org-add-log-setup 'state org-state this 'findpos dolog)))
  11013. ;; Fixup tag positioning
  11014. (org-todo-trigger-tag-changes org-state)
  11015. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  11016. (when org-provide-todo-statistics
  11017. (org-update-parent-todo-statistics))
  11018. (run-hooks 'org-after-todo-state-change-hook)
  11019. (if (and arg (not (member org-state org-done-keywords)))
  11020. (setq head (org-get-todo-sequence-head org-state)))
  11021. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  11022. ;; Do we need to trigger a repeat?
  11023. (when now-done-p
  11024. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  11025. ;; This is for the agenda, take a snapshot of the headline.
  11026. (save-match-data
  11027. (setq org-agenda-headline-snapshot-before-repeat
  11028. (org-get-heading))))
  11029. (org-auto-repeat-maybe org-state))
  11030. ;; Fixup cursor location if close to the keyword
  11031. (if (and (outline-on-heading-p)
  11032. (not (bolp))
  11033. (save-excursion (beginning-of-line 1)
  11034. (looking-at org-todo-line-regexp))
  11035. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  11036. (progn
  11037. (goto-char (or (match-end 2) (match-end 1)))
  11038. (and (looking-at " ") (just-one-space))))
  11039. (when org-trigger-hook
  11040. (save-excursion
  11041. (run-hook-with-args 'org-trigger-hook change-plist)))
  11042. (when commentp (org-toggle-comment))))))))
  11043. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  11044. "Block turning an entry into a TODO, using the hierarchy.
  11045. This checks whether the current task should be blocked from state
  11046. changes. Such blocking occurs when:
  11047. 1. The task has children which are not all in a completed state.
  11048. 2. A task has a parent with the property :ORDERED:, and there
  11049. are siblings prior to the current task with incomplete
  11050. status.
  11051. 3. The parent of the task is blocked because it has siblings that should
  11052. be done first, or is child of a block grandparent TODO entry."
  11053. (if (not org-enforce-todo-dependencies)
  11054. t ; if locally turned off don't block
  11055. (catch 'dont-block
  11056. ;; If this is not a todo state change, or if this entry is already DONE,
  11057. ;; do not block
  11058. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11059. (member (plist-get change-plist :from)
  11060. (cons 'done org-done-keywords))
  11061. (member (plist-get change-plist :to)
  11062. (cons 'todo org-not-done-keywords))
  11063. (not (plist-get change-plist :to)))
  11064. (throw 'dont-block t))
  11065. ;; If this task has children, and any are undone, it's blocked
  11066. (save-excursion
  11067. (org-back-to-heading t)
  11068. (let ((this-level (funcall outline-level)))
  11069. (outline-next-heading)
  11070. (let ((child-level (funcall outline-level)))
  11071. (while (and (not (eobp))
  11072. (> child-level this-level))
  11073. ;; this todo has children, check whether they are all
  11074. ;; completed
  11075. (if (and (not (org-entry-is-done-p))
  11076. (org-entry-is-todo-p))
  11077. (progn (setq org-block-entry-blocking (org-get-heading))
  11078. (throw 'dont-block nil)))
  11079. (outline-next-heading)
  11080. (setq child-level (funcall outline-level))))))
  11081. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  11082. ;; any previous siblings are undone, it's blocked
  11083. (save-excursion
  11084. (org-back-to-heading t)
  11085. (let* ((pos (point))
  11086. (parent-pos (and (org-up-heading-safe) (point))))
  11087. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  11088. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11089. (forward-line 1)
  11090. (re-search-forward org-not-done-heading-regexp pos t))
  11091. (setq org-block-entry-blocking (match-string 0))
  11092. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  11093. ;; Search further up the hierarchy, to see if an ancestor is blocked
  11094. (while t
  11095. (goto-char parent-pos)
  11096. (if (not (looking-at org-not-done-heading-regexp))
  11097. (throw 'dont-block t)) ; do not block, parent is not a TODO
  11098. (setq pos (point))
  11099. (setq parent-pos (and (org-up-heading-safe) (point)))
  11100. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  11101. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11102. (forward-line 1)
  11103. (re-search-forward org-not-done-heading-regexp pos t)
  11104. (setq org-block-entry-blocking (org-get-heading)))
  11105. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  11106. (defcustom org-track-ordered-property-with-tag nil
  11107. "Should the ORDERED property also be shown as a tag?
  11108. The ORDERED property decides if an entry should require subtasks to be
  11109. completed in sequence. Since a property is not very visible, setting
  11110. this option means that toggling the ORDERED property with the command
  11111. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  11112. not relevant for the behavior, but it makes things more visible.
  11113. Note that toggling the tag with tags commands will not change the property
  11114. and therefore not influence behavior!
  11115. This can be t, meaning the tag ORDERED should be used, It can also be a
  11116. string to select a different tag for this task."
  11117. :group 'org-todo
  11118. :type '(choice
  11119. (const :tag "No tracking" nil)
  11120. (const :tag "Track with ORDERED tag" t)
  11121. (string :tag "Use other tag")))
  11122. (defun org-toggle-ordered-property ()
  11123. "Toggle the ORDERED property of the current entry.
  11124. For better visibility, you can track the value of this property with a tag.
  11125. See variable `org-track-ordered-property-with-tag'."
  11126. (interactive)
  11127. (let* ((t1 org-track-ordered-property-with-tag)
  11128. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  11129. (save-excursion
  11130. (org-back-to-heading)
  11131. (if (org-entry-get nil "ORDERED")
  11132. (progn
  11133. (org-delete-property "ORDERED" "PROPERTIES")
  11134. (and tag (org-toggle-tag tag 'off))
  11135. (message "Subtasks can be completed in arbitrary order"))
  11136. (org-entry-put nil "ORDERED" "t")
  11137. (and tag (org-toggle-tag tag 'on))
  11138. (message "Subtasks must be completed in sequence")))))
  11139. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  11140. (defun org-block-todo-from-checkboxes (change-plist)
  11141. "Block turning an entry into a TODO, using checkboxes.
  11142. This checks whether the current task should be blocked from state
  11143. changes because there are unchecked boxes in this entry."
  11144. (if (not org-enforce-todo-checkbox-dependencies)
  11145. t ; if locally turned off don't block
  11146. (catch 'dont-block
  11147. ;; If this is not a todo state change, or if this entry is already DONE,
  11148. ;; do not block
  11149. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11150. (member (plist-get change-plist :from)
  11151. (cons 'done org-done-keywords))
  11152. (member (plist-get change-plist :to)
  11153. (cons 'todo org-not-done-keywords))
  11154. (not (plist-get change-plist :to)))
  11155. (throw 'dont-block t))
  11156. ;; If this task has checkboxes that are not checked, it's blocked
  11157. (save-excursion
  11158. (org-back-to-heading t)
  11159. (let ((beg (point)) end)
  11160. (outline-next-heading)
  11161. (setq end (point))
  11162. (goto-char beg)
  11163. (if (org-list-search-forward
  11164. (concat (org-item-beginning-re)
  11165. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
  11166. "\\[[- ]\\]")
  11167. end t)
  11168. (progn
  11169. (if (boundp 'org-blocked-by-checkboxes)
  11170. (setq org-blocked-by-checkboxes t))
  11171. (throw 'dont-block nil)))))
  11172. t))) ; do not block
  11173. (defun org-entry-blocked-p ()
  11174. "Is the current entry blocked?"
  11175. (org-with-silent-modifications
  11176. (if (org-entry-get nil "NOBLOCKING")
  11177. nil ;; Never block this entry
  11178. (not (run-hook-with-args-until-failure
  11179. 'org-blocker-hook
  11180. (list :type 'todo-state-change
  11181. :position (point)
  11182. :from 'todo
  11183. :to 'done))))))
  11184. (defun org-update-statistics-cookies (all)
  11185. "Update the statistics cookie, either from TODO or from checkboxes.
  11186. This should be called with the cursor in a line with a statistics cookie."
  11187. (interactive "P")
  11188. (if all
  11189. (progn
  11190. (org-update-checkbox-count 'all)
  11191. (org-map-entries 'org-update-parent-todo-statistics))
  11192. (if (not (org-at-heading-p))
  11193. (org-update-checkbox-count)
  11194. (let ((pos (point-marker))
  11195. end l1 l2)
  11196. (ignore-errors (org-back-to-heading t))
  11197. (if (not (org-at-heading-p))
  11198. (org-update-checkbox-count)
  11199. (setq l1 (org-outline-level))
  11200. (setq end (save-excursion
  11201. (outline-next-heading)
  11202. (if (org-at-heading-p) (setq l2 (org-outline-level)))
  11203. (point)))
  11204. (if (and (save-excursion
  11205. (re-search-forward
  11206. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  11207. (not (save-excursion (re-search-forward
  11208. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  11209. (org-update-checkbox-count)
  11210. (if (and l2 (> l2 l1))
  11211. (progn
  11212. (goto-char end)
  11213. (org-update-parent-todo-statistics))
  11214. (goto-char pos)
  11215. (beginning-of-line 1)
  11216. (while (re-search-forward
  11217. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  11218. (point-at-eol) t)
  11219. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  11220. (goto-char pos)
  11221. (move-marker pos nil)))))
  11222. (defvar org-entry-property-inherited-from) ;; defined below
  11223. (defun org-update-parent-todo-statistics ()
  11224. "Update any statistics cookie in the parent of the current headline.
  11225. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  11226. the entire subtree and this will travel up the hierarchy and update
  11227. statistics everywhere."
  11228. (let* ((prop (save-excursion (org-up-heading-safe)
  11229. (org-entry-get nil "COOKIE_DATA" 'inherit)))
  11230. (recursive (or (not org-hierarchical-todo-statistics)
  11231. (and prop (string-match "\\<recursive\\>" prop))))
  11232. (lim (or (and prop (marker-position org-entry-property-inherited-from))
  11233. 0))
  11234. (first t)
  11235. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  11236. level ltoggle l1 new ndel
  11237. (cnt-all 0) (cnt-done 0) is-percent kwd
  11238. checkbox-beg ov ovs ove cookie-present)
  11239. (catch 'exit
  11240. (save-excursion
  11241. (beginning-of-line 1)
  11242. (setq ltoggle (funcall outline-level))
  11243. ;; Three situations are to consider:
  11244. ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
  11245. ;; to the top-level ancestor on the headline;
  11246. ;; 2. If parent has "recursive" property, repeat up to the
  11247. ;; headline setting that property, taking inheritance into
  11248. ;; account;
  11249. ;; 3. Else, move up to direct parent and proceed only once.
  11250. (while (and (setq level (org-up-heading-safe))
  11251. (or recursive first)
  11252. (>= (point) lim))
  11253. (setq first nil cookie-present nil)
  11254. (unless (and level
  11255. (not (string-match
  11256. "\\<checkbox\\>"
  11257. (downcase (or (org-entry-get nil "COOKIE_DATA")
  11258. "")))))
  11259. (throw 'exit nil))
  11260. (while (re-search-forward box-re (point-at-eol) t)
  11261. (setq cnt-all 0 cnt-done 0 cookie-present t)
  11262. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  11263. (save-match-data
  11264. (unless (outline-next-heading) (throw 'exit nil))
  11265. (while (and (looking-at org-complex-heading-regexp)
  11266. (> (setq l1 (length (match-string 1))) level))
  11267. (setq kwd (and (or recursive (= l1 ltoggle))
  11268. (match-string 2)))
  11269. (if (or (eq org-provide-todo-statistics 'all-headlines)
  11270. (and (listp org-provide-todo-statistics)
  11271. (or (member kwd org-provide-todo-statistics)
  11272. (member kwd org-done-keywords))))
  11273. (setq cnt-all (1+ cnt-all))
  11274. (if (eq org-provide-todo-statistics t)
  11275. (and kwd (setq cnt-all (1+ cnt-all)))))
  11276. (and (member kwd org-done-keywords)
  11277. (setq cnt-done (1+ cnt-done)))
  11278. (outline-next-heading)))
  11279. (setq new
  11280. (if is-percent
  11281. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  11282. (format "[%d/%d]" cnt-done cnt-all))
  11283. ndel (- (match-end 0) checkbox-beg))
  11284. ;; handle overlays when updating cookie from column view
  11285. (when (setq ov (car (overlays-at checkbox-beg)))
  11286. (setq ovs (overlay-start ov) ove (overlay-end ov))
  11287. (delete-overlay ov))
  11288. (goto-char checkbox-beg)
  11289. (insert new)
  11290. (delete-region (point) (+ (point) ndel))
  11291. (when org-auto-align-tags (org-fix-tags-on-the-fly))
  11292. (when ov (move-overlay ov ovs ove)))
  11293. (when cookie-present
  11294. (run-hook-with-args 'org-after-todo-statistics-hook
  11295. cnt-done (- cnt-all cnt-done))))))
  11296. (run-hooks 'org-todo-statistics-hook)))
  11297. (defvar org-after-todo-statistics-hook nil
  11298. "Hook that is called after a TODO statistics cookie has been updated.
  11299. Each function is called with two arguments: the number of not-done entries
  11300. and the number of done entries.
  11301. For example, the following function, when added to this hook, will switch
  11302. an entry to DONE when all children are done, and back to TODO when new
  11303. entries are set to a TODO status. Note that this hook is only called
  11304. when there is a statistics cookie in the headline!
  11305. (defun org-summary-todo (n-done n-not-done)
  11306. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  11307. (let (org-log-done org-log-states) ; turn off logging
  11308. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  11309. ")
  11310. (defvar org-todo-statistics-hook nil
  11311. "Hook that is run whenever Org thinks TODO statistics should be updated.
  11312. This hook runs even if there is no statistics cookie present, in which case
  11313. `org-after-todo-statistics-hook' would not run.")
  11314. (defun org-todo-trigger-tag-changes (state)
  11315. "Apply the changes defined in `org-todo-state-tags-triggers'."
  11316. (let ((l org-todo-state-tags-triggers)
  11317. changes)
  11318. (when (or (not state) (equal state ""))
  11319. (setq changes (append changes (cdr (assoc "" l)))))
  11320. (when (and (stringp state) (> (length state) 0))
  11321. (setq changes (append changes (cdr (assoc state l)))))
  11322. (when (member state org-not-done-keywords)
  11323. (setq changes (append changes (cdr (assoc 'todo l)))))
  11324. (when (member state org-done-keywords)
  11325. (setq changes (append changes (cdr (assoc 'done l)))))
  11326. (dolist (c changes)
  11327. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  11328. (defun org-local-logging (value)
  11329. "Get logging settings from a property VALUE."
  11330. (let* (words w a)
  11331. ;; directly set the variables, they are already local.
  11332. (setq org-log-done nil
  11333. org-log-repeat nil
  11334. org-todo-log-states nil)
  11335. (setq words (org-split-string value))
  11336. (while (setq w (pop words))
  11337. (cond
  11338. ((setq a (assoc w org-startup-options))
  11339. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  11340. (set (nth 1 a) (nth 2 a))))
  11341. ((setq a (org-extract-log-state-settings w))
  11342. (and (member (car a) org-todo-keywords-1)
  11343. (push a org-todo-log-states)))))))
  11344. (defun org-get-todo-sequence-head (kwd)
  11345. "Return the head of the TODO sequence to which KWD belongs.
  11346. If KWD is not set, check if there is a text property remembering the
  11347. right sequence."
  11348. (let (p)
  11349. (cond
  11350. ((not kwd)
  11351. (or (get-text-property (point-at-bol) 'org-todo-head)
  11352. (progn
  11353. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  11354. nil (point-at-eol)))
  11355. (get-text-property p 'org-todo-head))))
  11356. ((not (member kwd org-todo-keywords-1))
  11357. (car org-todo-keywords-1))
  11358. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  11359. (defun org-fast-todo-selection ()
  11360. "Fast TODO keyword selection with single keys.
  11361. Returns the new TODO keyword, or nil if no state change should occur."
  11362. (let* ((fulltable org-todo-key-alist)
  11363. (done-keywords org-done-keywords) ;; needed for the faces.
  11364. (maxlen (apply 'max (mapcar
  11365. (lambda (x)
  11366. (if (stringp (car x)) (string-width (car x)) 0))
  11367. fulltable)))
  11368. (expert nil)
  11369. (fwidth (+ maxlen 3 1 3))
  11370. (ncol (/ (- (window-width) 4) fwidth))
  11371. tg cnt e c tbl
  11372. groups ingroup)
  11373. (save-excursion
  11374. (save-window-excursion
  11375. (if expert
  11376. (set-buffer (get-buffer-create " *Org todo*"))
  11377. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  11378. (erase-buffer)
  11379. (org-set-local 'org-done-keywords done-keywords)
  11380. (setq tbl fulltable cnt 0)
  11381. (while (setq e (pop tbl))
  11382. (cond
  11383. ((equal e '(:startgroup))
  11384. (push '() groups) (setq ingroup t)
  11385. (when (not (= cnt 0))
  11386. (setq cnt 0)
  11387. (insert "\n"))
  11388. (insert "{ "))
  11389. ((equal e '(:endgroup))
  11390. (setq ingroup nil cnt 0)
  11391. (insert "}\n"))
  11392. ((equal e '(:newline))
  11393. (when (not (= cnt 0))
  11394. (setq cnt 0)
  11395. (insert "\n")
  11396. (setq e (car tbl))
  11397. (while (equal (car tbl) '(:newline))
  11398. (insert "\n")
  11399. (setq tbl (cdr tbl)))))
  11400. (t
  11401. (setq tg (car e) c (cdr e))
  11402. (if ingroup (push tg (car groups)))
  11403. (setq tg (org-add-props tg nil 'face
  11404. (org-get-todo-face tg)))
  11405. (if (and (= cnt 0) (not ingroup)) (insert " "))
  11406. (insert "[" c "] " tg (make-string
  11407. (- fwidth 4 (length tg)) ?\ ))
  11408. (when (= (setq cnt (1+ cnt)) ncol)
  11409. (insert "\n")
  11410. (if ingroup (insert " "))
  11411. (setq cnt 0)))))
  11412. (insert "\n")
  11413. (goto-char (point-min))
  11414. (if (not expert) (org-fit-window-to-buffer))
  11415. (message "[a-z..]:Set [SPC]:clear")
  11416. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  11417. (cond
  11418. ((or (= c ?\C-g)
  11419. (and (= c ?q) (not (rassoc c fulltable))))
  11420. (setq quit-flag t))
  11421. ((= c ?\ ) nil)
  11422. ((setq e (rassoc c fulltable) tg (car e))
  11423. tg)
  11424. (t (setq quit-flag t)))))))
  11425. (defun org-entry-is-todo-p ()
  11426. (member (org-get-todo-state) org-not-done-keywords))
  11427. (defun org-entry-is-done-p ()
  11428. (member (org-get-todo-state) org-done-keywords))
  11429. (defun org-get-todo-state ()
  11430. (save-excursion
  11431. (org-back-to-heading t)
  11432. (and (looking-at org-todo-line-regexp)
  11433. (match-end 2)
  11434. (match-string 2))))
  11435. (defun org-at-date-range-p (&optional inactive-ok)
  11436. "Is the cursor inside a date range?"
  11437. (interactive)
  11438. (save-excursion
  11439. (catch 'exit
  11440. (let ((pos (point)))
  11441. (skip-chars-backward "^[<\r\n")
  11442. (skip-chars-backward "<[")
  11443. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11444. (>= (match-end 0) pos)
  11445. (throw 'exit t))
  11446. (skip-chars-backward "^<[\r\n")
  11447. (skip-chars-backward "<[")
  11448. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11449. (>= (match-end 0) pos)
  11450. (throw 'exit t)))
  11451. nil)))
  11452. (defun org-get-repeat (&optional tagline)
  11453. "Check if there is a deadline/schedule with repeater in this entry."
  11454. (save-match-data
  11455. (save-excursion
  11456. (org-back-to-heading t)
  11457. (and (re-search-forward (if tagline
  11458. (concat tagline "\\s-*" org-repeat-re)
  11459. org-repeat-re)
  11460. (org-entry-end-position) t)
  11461. (match-string-no-properties 1)))))
  11462. (defvar org-last-changed-timestamp)
  11463. (defvar org-last-inserted-timestamp)
  11464. (defvar org-log-post-message)
  11465. (defvar org-log-note-purpose)
  11466. (defvar org-log-note-how)
  11467. (defvar org-log-note-extra)
  11468. (defun org-auto-repeat-maybe (done-word)
  11469. "Check if the current headline contains a repeated deadline/schedule.
  11470. If yes, set TODO state back to what it was and change the base date
  11471. of repeating deadline/scheduled time stamps to new date.
  11472. This function is run automatically after each state change to a DONE state."
  11473. ;; last-state is dynamically scoped into this function
  11474. (let* ((repeat (org-get-repeat))
  11475. (aa (assoc org-last-state org-todo-kwd-alist))
  11476. (interpret (nth 1 aa))
  11477. (head (nth 2 aa))
  11478. (whata '(("h" . hour) ("d" . day) ("m" . month) ("y" . year)))
  11479. (msg "Entry repeats: ")
  11480. (org-log-done nil)
  11481. (org-todo-log-states nil)
  11482. re type n what ts time to-state)
  11483. (when repeat
  11484. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  11485. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  11486. org-todo-repeat-to-state))
  11487. (unless (and to-state (member to-state org-todo-keywords-1))
  11488. (setq to-state (if (eq interpret 'type) org-last-state head)))
  11489. (org-todo to-state)
  11490. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  11491. (org-entry-put nil "LAST_REPEAT" (format-time-string
  11492. (org-time-stamp-format t t))))
  11493. (when org-log-repeat
  11494. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  11495. (memq 'org-add-log-note post-command-hook))
  11496. ;; OK, we are already setup for some record
  11497. (if (eq org-log-repeat 'note)
  11498. ;; make sure we take a note, not only a time stamp
  11499. (setq org-log-note-how 'note))
  11500. ;; Set up for taking a record
  11501. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  11502. org-last-state
  11503. 'findpos org-log-repeat)))
  11504. (org-back-to-heading t)
  11505. (org-add-planning-info nil nil 'closed)
  11506. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11507. org-deadline-time-regexp "\\)\\|\\("
  11508. org-ts-regexp "\\)"))
  11509. (while (re-search-forward
  11510. re (save-excursion (outline-next-heading) (point)) t)
  11511. (setq type (if (match-end 1) org-scheduled-string
  11512. (if (match-end 3) org-deadline-string "Plain:"))
  11513. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  11514. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts)
  11515. (setq n (string-to-number (match-string 2 ts))
  11516. what (match-string 3 ts))
  11517. (if (equal what "w") (setq n (* n 7) what "d"))
  11518. (if (and (equal what "h") (not (string-match "[0-9]\\{1,2\\}:[0-9]\\{2\\}" ts)))
  11519. (error "Cannot repeat in Repeat in %d hour(s) because no hour has been set" n))
  11520. ;; Preparation, see if we need to modify the start date for the change
  11521. (when (match-end 1)
  11522. (setq time (save-match-data (org-time-string-to-time ts)))
  11523. (cond
  11524. ((equal (match-string 1 ts) ".")
  11525. ;; Shift starting date to today
  11526. (org-timestamp-change
  11527. (- (org-today) (time-to-days time))
  11528. 'day))
  11529. ((equal (match-string 1 ts) "+")
  11530. (let ((nshiftmax 10) (nshift 0))
  11531. (while (or (= nshift 0)
  11532. (<= (time-to-days time)
  11533. (time-to-days (current-time))))
  11534. (when (= (incf nshift) nshiftmax)
  11535. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  11536. (error "Abort")))
  11537. (org-timestamp-change n (cdr (assoc what whata)))
  11538. (org-at-timestamp-p t)
  11539. (setq ts (match-string 1))
  11540. (setq time (save-match-data (org-time-string-to-time ts)))))
  11541. (org-timestamp-change (- n) (cdr (assoc what whata)))
  11542. ;; rematch, so that we have everything in place for the real shift
  11543. (org-at-timestamp-p t)
  11544. (setq ts (match-string 1))
  11545. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts))))
  11546. (save-excursion (org-timestamp-change n (cdr (assoc what whata)) nil t))
  11547. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  11548. (setq org-log-post-message msg)
  11549. (message "%s" msg))))
  11550. (defun org-show-todo-tree (arg)
  11551. "Make a compact tree which shows all headlines marked with TODO.
  11552. The tree will show the lines where the regexp matches, and all higher
  11553. headlines above the match.
  11554. With a \\[universal-argument] prefix, prompt for a regexp to match.
  11555. With a numeric prefix N, construct a sparse tree for the Nth element
  11556. of `org-todo-keywords-1'."
  11557. (interactive "P")
  11558. (let ((case-fold-search nil)
  11559. (kwd-re
  11560. (cond ((null arg) org-not-done-regexp)
  11561. ((equal arg '(4))
  11562. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  11563. (mapcar 'list org-todo-keywords-1))))
  11564. (concat "\\("
  11565. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  11566. "\\)\\>")))
  11567. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  11568. (regexp-quote (nth (1- (prefix-numeric-value arg))
  11569. org-todo-keywords-1)))
  11570. (t (error "Invalid prefix argument: %s" arg)))))
  11571. (message "%d TODO entries found"
  11572. (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
  11573. (defun org-deadline (arg &optional time)
  11574. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  11575. With one universal prefix argument, remove any deadline from the item.
  11576. With two universal prefix arguments, prompt for a warning delay.
  11577. With argument TIME, set the deadline at the corresponding date. TIME
  11578. can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11579. (interactive "P")
  11580. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11581. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11582. 'region-start-level 'region))
  11583. org-loop-over-headlines-in-active-region)
  11584. (org-map-entries
  11585. `(org-deadline ',arg ,time)
  11586. org-loop-over-headlines-in-active-region
  11587. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11588. (let* ((old-date (org-entry-get nil "DEADLINE"))
  11589. (old-date-time (if old-date (org-time-string-to-time old-date)))
  11590. (repeater (and old-date
  11591. (string-match
  11592. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11593. old-date)
  11594. (match-string 1 old-date))))
  11595. (cond
  11596. ((equal arg '(4))
  11597. (when (and old-date org-log-redeadline)
  11598. (org-add-log-setup 'deldeadline nil old-date 'findpos
  11599. org-log-redeadline))
  11600. (org-remove-timestamp-with-keyword org-deadline-string)
  11601. (message "Item no longer has a deadline."))
  11602. ((equal arg '(16))
  11603. (save-excursion
  11604. (if (re-search-forward
  11605. org-deadline-time-regexp
  11606. (save-excursion (outline-next-heading) (point)) t)
  11607. (let* ((rpl0 (match-string 1))
  11608. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  11609. (replace-match
  11610. (concat org-deadline-string
  11611. " <" rpl
  11612. (format " -%dd"
  11613. (abs
  11614. (- (time-to-days
  11615. (save-match-data
  11616. (org-read-date nil t nil "Warn starting from" old-date-time)))
  11617. (time-to-days old-date-time))))
  11618. ">") t t))
  11619. (user-error "No deadline information to update"))))
  11620. (t
  11621. (org-add-planning-info 'deadline time 'closed)
  11622. (when (and old-date org-log-redeadline
  11623. (not (equal old-date
  11624. (substring org-last-inserted-timestamp 1 -1))))
  11625. (org-add-log-setup 'redeadline nil old-date 'findpos
  11626. org-log-redeadline))
  11627. (when repeater
  11628. (save-excursion
  11629. (org-back-to-heading t)
  11630. (when (re-search-forward (concat org-deadline-string " "
  11631. org-last-inserted-timestamp)
  11632. (save-excursion
  11633. (outline-next-heading) (point)) t)
  11634. (goto-char (1- (match-end 0)))
  11635. (insert " " repeater)
  11636. (setq org-last-inserted-timestamp
  11637. (concat (substring org-last-inserted-timestamp 0 -1)
  11638. " " repeater
  11639. (substring org-last-inserted-timestamp -1))))))
  11640. (message "Deadline on %s" org-last-inserted-timestamp))))))
  11641. (defun org-schedule (arg &optional time)
  11642. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  11643. With one universal prefix argument, remove any scheduling date from the item.
  11644. With two universal prefix arguments, prompt for a delay cookie.
  11645. With argument TIME, scheduled at the corresponding date. TIME can
  11646. either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11647. (interactive "P")
  11648. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11649. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11650. 'region-start-level 'region))
  11651. org-loop-over-headlines-in-active-region)
  11652. (org-map-entries
  11653. `(org-schedule ',arg ,time)
  11654. org-loop-over-headlines-in-active-region
  11655. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11656. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  11657. (old-date-time (if old-date (org-time-string-to-time old-date)))
  11658. (repeater (and old-date
  11659. (string-match
  11660. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11661. old-date)
  11662. (match-string 1 old-date))))
  11663. (cond
  11664. ((equal arg '(4))
  11665. (progn
  11666. (when (and old-date org-log-reschedule)
  11667. (org-add-log-setup 'delschedule nil old-date 'findpos
  11668. org-log-reschedule))
  11669. (org-remove-timestamp-with-keyword org-scheduled-string)
  11670. (message "Item is no longer scheduled.")))
  11671. ((equal arg '(16))
  11672. (save-excursion
  11673. (if (re-search-forward
  11674. org-scheduled-time-regexp
  11675. (save-excursion (outline-next-heading) (point)) t)
  11676. (let* ((rpl0 (match-string 1))
  11677. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  11678. (replace-match
  11679. (concat org-scheduled-string
  11680. " <" rpl
  11681. (format " -%dd"
  11682. (abs
  11683. (- (time-to-days
  11684. (save-match-data
  11685. (org-read-date nil t nil "Delay until" old-date-time)))
  11686. (time-to-days old-date-time))))
  11687. ">") t t))
  11688. (user-error "No scheduled information to update"))))
  11689. (t
  11690. (org-add-planning-info 'scheduled time 'closed)
  11691. (when (and old-date org-log-reschedule
  11692. (not (equal old-date
  11693. (substring org-last-inserted-timestamp 1 -1))))
  11694. (org-add-log-setup 'reschedule nil old-date 'findpos
  11695. org-log-reschedule))
  11696. (when repeater
  11697. (save-excursion
  11698. (org-back-to-heading t)
  11699. (when (re-search-forward (concat org-scheduled-string " "
  11700. org-last-inserted-timestamp)
  11701. (save-excursion
  11702. (outline-next-heading) (point)) t)
  11703. (goto-char (1- (match-end 0)))
  11704. (insert " " repeater)
  11705. (setq org-last-inserted-timestamp
  11706. (concat (substring org-last-inserted-timestamp 0 -1)
  11707. " " repeater
  11708. (substring org-last-inserted-timestamp -1))))))
  11709. (message "Scheduled to %s" org-last-inserted-timestamp))))))
  11710. (defun org-get-scheduled-time (pom &optional inherit)
  11711. "Get the scheduled time as a time tuple, of a format suitable
  11712. for calling org-schedule with, or if there is no scheduling,
  11713. returns nil."
  11714. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  11715. (when time
  11716. (apply 'encode-time (org-parse-time-string time)))))
  11717. (defun org-get-deadline-time (pom &optional inherit)
  11718. "Get the deadline as a time tuple, of a format suitable for
  11719. calling org-deadline with, or if there is no scheduling, returns
  11720. nil."
  11721. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  11722. (when time
  11723. (apply 'encode-time (org-parse-time-string time)))))
  11724. (defun org-remove-timestamp-with-keyword (keyword)
  11725. "Remove all time stamps with KEYWORD in the current entry."
  11726. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  11727. beg)
  11728. (save-excursion
  11729. (org-back-to-heading t)
  11730. (setq beg (point))
  11731. (outline-next-heading)
  11732. (while (re-search-backward re beg t)
  11733. (replace-match "")
  11734. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  11735. (equal (char-before) ?\ ))
  11736. (backward-delete-char 1)
  11737. (if (string-match "^[ \t]*$" (buffer-substring
  11738. (point-at-bol) (point-at-eol)))
  11739. (delete-region (point-at-bol)
  11740. (min (point-max) (1+ (point-at-eol))))))))))
  11741. (defun org-add-planning-info (what &optional time &rest remove)
  11742. "Insert new timestamp with keyword in the line directly after the headline.
  11743. WHAT indicates what kind of time stamp to add. TIME indicates the time to use.
  11744. If non is given, the user is prompted for a date.
  11745. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  11746. be removed."
  11747. (interactive)
  11748. (let (org-time-was-given org-end-time-was-given ts
  11749. end default-time default-input)
  11750. (catch 'exit
  11751. (when (and (memq what '(scheduled deadline))
  11752. (or (not time)
  11753. (and (stringp time)
  11754. (string-match "^[-+]+[0-9]" time))))
  11755. ;; Try to get a default date/time from existing timestamp
  11756. (save-excursion
  11757. (org-back-to-heading t)
  11758. (setq end (save-excursion (outline-next-heading) (point)))
  11759. (when (re-search-forward (if (eq what 'scheduled)
  11760. org-scheduled-time-regexp
  11761. org-deadline-time-regexp)
  11762. end t)
  11763. (setq ts (match-string 1)
  11764. default-time
  11765. (apply 'encode-time (org-parse-time-string ts))
  11766. default-input (and ts (org-get-compact-tod ts))))))
  11767. (when what
  11768. (setq time
  11769. (if (stringp time)
  11770. ;; This is a string (relative or absolute), set proper date
  11771. (apply 'encode-time
  11772. (org-read-date-analyze
  11773. time default-time (decode-time default-time)))
  11774. ;; If necessary, get the time from the user
  11775. (or time (org-read-date nil 'to-time nil nil
  11776. default-time default-input)))))
  11777. (when (and org-insert-labeled-timestamps-at-point
  11778. (member what '(scheduled deadline)))
  11779. (insert
  11780. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  11781. (org-insert-time-stamp time org-time-was-given
  11782. nil nil nil (list org-end-time-was-given))
  11783. (setq what nil))
  11784. (save-excursion
  11785. (save-restriction
  11786. (let (col list elt ts buffer-invisibility-spec)
  11787. (org-back-to-heading t)
  11788. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"))
  11789. (goto-char (match-end 1))
  11790. (setq col (current-column))
  11791. (goto-char (match-end 0))
  11792. (if (eobp) (insert "\n") (forward-char 1))
  11793. (when (and (not what)
  11794. (not (looking-at
  11795. (concat "[ \t]*"
  11796. org-keyword-time-not-clock-regexp))))
  11797. ;; Nothing to add, nothing to remove...... :-)
  11798. (throw 'exit nil))
  11799. (if (and (not (looking-at org-outline-regexp))
  11800. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  11801. "[^\r\n]*"))
  11802. (not (equal (match-string 1) org-clock-string)))
  11803. (narrow-to-region (match-beginning 0) (match-end 0))
  11804. (insert-before-markers "\n")
  11805. (backward-char 1)
  11806. (narrow-to-region (point) (point))
  11807. (and org-adapt-indentation (org-indent-to-column col)))
  11808. ;; Check if we have to remove something.
  11809. (setq list (cons what remove))
  11810. (while list
  11811. (setq elt (pop list))
  11812. (when (or (and (eq elt 'scheduled)
  11813. (re-search-forward org-scheduled-time-regexp nil t))
  11814. (and (eq elt 'deadline)
  11815. (re-search-forward org-deadline-time-regexp nil t))
  11816. (and (eq elt 'closed)
  11817. (re-search-forward org-closed-time-regexp nil t)))
  11818. (replace-match "")
  11819. (if (looking-at "--+<[^>]+>") (replace-match ""))))
  11820. (and (looking-at "[ \t]+") (replace-match ""))
  11821. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  11822. (when what
  11823. (insert
  11824. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  11825. (cond ((eq what 'scheduled) org-scheduled-string)
  11826. ((eq what 'deadline) org-deadline-string)
  11827. ((eq what 'closed) org-closed-string))
  11828. " ")
  11829. (setq ts (org-insert-time-stamp
  11830. time
  11831. (or org-time-was-given
  11832. (and (eq what 'closed) org-log-done-with-time))
  11833. (eq what 'closed)
  11834. nil nil (list org-end-time-was-given)))
  11835. (insert
  11836. (if (not (or (bolp) (eq (char-before) ?\ )
  11837. (memq (char-after) '(32 10))
  11838. (eobp))) " " ""))
  11839. (end-of-line 1))
  11840. (goto-char (point-min))
  11841. (widen)
  11842. (if (and (looking-at "[ \t]*\n")
  11843. (equal (char-before) ?\n))
  11844. (delete-region (1- (point)) (point-at-eol)))
  11845. ts))))))
  11846. (defvar org-log-note-marker (make-marker))
  11847. (defvar org-log-note-purpose nil)
  11848. (defvar org-log-note-state nil)
  11849. (defvar org-log-note-previous-state nil)
  11850. (defvar org-log-note-how nil)
  11851. (defvar org-log-note-extra nil)
  11852. (defvar org-log-note-window-configuration nil)
  11853. (defvar org-log-note-return-to (make-marker))
  11854. (defvar org-log-note-effective-time nil
  11855. "Remembered current time so that dynamically scoped
  11856. `org-extend-today-until' affects tha timestamps in state change
  11857. log")
  11858. (defvar org-log-post-message nil
  11859. "Message to be displayed after a log note has been stored.
  11860. The auto-repeater uses this.")
  11861. (defun org-add-note ()
  11862. "Add a note to the current entry.
  11863. This is done in the same way as adding a state change note."
  11864. (interactive)
  11865. (org-add-log-setup 'note nil nil 'findpos nil))
  11866. (defvar org-property-end-re)
  11867. (defun org-add-log-setup (&optional purpose state prev-state
  11868. findpos how extra)
  11869. "Set up the post command hook to take a note.
  11870. If this is about to TODO state change, the new state is expected in STATE.
  11871. When FINDPOS is non-nil, find the correct position for the note in
  11872. the current entry. If not, assume that it can be inserted at point.
  11873. HOW is an indicator what kind of note should be created.
  11874. EXTRA is additional text that will be inserted into the notes buffer."
  11875. (let* ((org-log-into-drawer (org-log-into-drawer))
  11876. (drawer (cond ((stringp org-log-into-drawer)
  11877. org-log-into-drawer)
  11878. (org-log-into-drawer "LOGBOOK"))))
  11879. (save-restriction
  11880. (save-excursion
  11881. (when findpos
  11882. (org-back-to-heading t)
  11883. (narrow-to-region (point) (save-excursion
  11884. (outline-next-heading) (point)))
  11885. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"
  11886. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  11887. "[^\r\n]*\\)?"))
  11888. (goto-char (match-end 0))
  11889. (cond
  11890. (drawer
  11891. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  11892. nil t)
  11893. (progn
  11894. (goto-char (match-end 0))
  11895. (or org-log-states-order-reversed
  11896. (and (re-search-forward org-property-end-re nil t)
  11897. (goto-char (1- (match-beginning 0))))))
  11898. (insert "\n:" drawer ":\n:END:")
  11899. (beginning-of-line 0)
  11900. (org-indent-line)
  11901. (beginning-of-line 2)
  11902. (org-indent-line)
  11903. (end-of-line 0)))
  11904. ((and org-log-state-notes-insert-after-drawers
  11905. (save-excursion
  11906. (forward-line) (looking-at org-drawer-regexp)))
  11907. (forward-line)
  11908. (while (looking-at org-drawer-regexp)
  11909. (goto-char (match-end 0))
  11910. (re-search-forward org-property-end-re (point-max) t)
  11911. (forward-line))
  11912. (forward-line -1)))
  11913. (unless org-log-states-order-reversed
  11914. (and (= (char-after) ?\n) (forward-char 1))
  11915. (org-skip-over-state-notes)
  11916. (skip-chars-backward " \t\n\r")))
  11917. (move-marker org-log-note-marker (point))
  11918. (setq org-log-note-purpose purpose
  11919. org-log-note-state state
  11920. org-log-note-previous-state prev-state
  11921. org-log-note-how how
  11922. org-log-note-extra extra
  11923. org-log-note-effective-time (org-current-effective-time))
  11924. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  11925. (defun org-skip-over-state-notes ()
  11926. "Skip past the list of State notes in an entry."
  11927. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  11928. (when (ignore-errors (goto-char (org-in-item-p)))
  11929. (let* ((struct (org-list-struct))
  11930. (prevs (org-list-prevs-alist struct)))
  11931. (while (looking-at "[ \t]*- State")
  11932. (goto-char (or (org-list-get-next-item (point) struct prevs)
  11933. (org-list-get-item-end (point) struct)))))))
  11934. (defun org-add-log-note (&optional purpose)
  11935. "Pop up a window for taking a note, and add this note later at point."
  11936. (remove-hook 'post-command-hook 'org-add-log-note)
  11937. (setq org-log-note-window-configuration (current-window-configuration))
  11938. (delete-other-windows)
  11939. (move-marker org-log-note-return-to (point))
  11940. (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
  11941. (goto-char org-log-note-marker)
  11942. (org-switch-to-buffer-other-window "*Org Note*")
  11943. (erase-buffer)
  11944. (if (memq org-log-note-how '(time state))
  11945. (let (current-prefix-arg) (org-store-log-note))
  11946. (let ((org-inhibit-startup t)) (org-mode))
  11947. (insert (format "# Insert note for %s.
  11948. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  11949. (cond
  11950. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  11951. ((eq org-log-note-purpose 'done) "closed todo item")
  11952. ((eq org-log-note-purpose 'state)
  11953. (format "state change from \"%s\" to \"%s\""
  11954. (or org-log-note-previous-state "")
  11955. (or org-log-note-state "")))
  11956. ((eq org-log-note-purpose 'reschedule)
  11957. "rescheduling")
  11958. ((eq org-log-note-purpose 'delschedule)
  11959. "no longer scheduled")
  11960. ((eq org-log-note-purpose 'redeadline)
  11961. "changing deadline")
  11962. ((eq org-log-note-purpose 'deldeadline)
  11963. "removing deadline")
  11964. ((eq org-log-note-purpose 'refile)
  11965. "refiling")
  11966. ((eq org-log-note-purpose 'note)
  11967. "this entry")
  11968. (t (error "This should not happen")))))
  11969. (if org-log-note-extra (insert org-log-note-extra))
  11970. (org-set-local 'org-finish-function 'org-store-log-note)
  11971. (run-hooks 'org-log-buffer-setup-hook)))
  11972. (defvar org-note-abort nil) ; dynamically scoped
  11973. (defun org-store-log-note ()
  11974. "Finish taking a log note, and insert it to where it belongs."
  11975. (let ((txt (buffer-string)))
  11976. (kill-buffer (current-buffer))
  11977. (let ((note (cdr (assq org-log-note-purpose org-log-note-headings)))
  11978. lines ind bul)
  11979. (while (string-match "\\`# .*\n[ \t\n]*" txt)
  11980. (setq txt (replace-match "" t t txt)))
  11981. (if (string-match "\\s-+\\'" txt)
  11982. (setq txt (replace-match "" t t txt)))
  11983. (setq lines (org-split-string txt "\n"))
  11984. (when (and note (string-match "\\S-" note))
  11985. (setq note
  11986. (org-replace-escapes
  11987. note
  11988. (list (cons "%u" (user-login-name))
  11989. (cons "%U" user-full-name)
  11990. (cons "%t" (format-time-string
  11991. (org-time-stamp-format 'long 'inactive)
  11992. org-log-note-effective-time))
  11993. (cons "%T" (format-time-string
  11994. (org-time-stamp-format 'long nil)
  11995. org-log-note-effective-time))
  11996. (cons "%d" (format-time-string
  11997. (org-time-stamp-format nil 'inactive)
  11998. org-log-note-effective-time))
  11999. (cons "%D" (format-time-string
  12000. (org-time-stamp-format nil nil)
  12001. org-log-note-effective-time))
  12002. (cons "%s" (if org-log-note-state
  12003. (concat "\"" org-log-note-state "\"")
  12004. ""))
  12005. (cons "%S" (if org-log-note-previous-state
  12006. (concat "\"" org-log-note-previous-state "\"")
  12007. "\"\"")))))
  12008. (if lines (setq note (concat note " \\\\")))
  12009. (push note lines))
  12010. (when (or current-prefix-arg org-note-abort)
  12011. (when org-log-into-drawer
  12012. (org-remove-empty-drawer-at
  12013. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  12014. org-log-note-marker))
  12015. (setq lines nil))
  12016. (when lines
  12017. (with-current-buffer (marker-buffer org-log-note-marker)
  12018. (save-excursion
  12019. (goto-char org-log-note-marker)
  12020. (move-marker org-log-note-marker nil)
  12021. (end-of-line 1)
  12022. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  12023. (setq ind (save-excursion
  12024. (if (ignore-errors (goto-char (org-in-item-p)))
  12025. (let ((struct (org-list-struct)))
  12026. (org-list-get-ind
  12027. (org-list-get-top-point struct) struct))
  12028. (skip-chars-backward " \r\t\n")
  12029. (cond
  12030. ((and (org-at-heading-p)
  12031. org-adapt-indentation)
  12032. (1+ (org-current-level)))
  12033. ((org-at-heading-p) 0)
  12034. (t (org-get-indentation))))))
  12035. (setq bul (org-list-bullet-string "-"))
  12036. (org-indent-line-to ind)
  12037. (insert bul (pop lines))
  12038. (let ((ind-body (+ (length bul) ind)))
  12039. (while lines
  12040. (insert "\n")
  12041. (org-indent-line-to ind-body)
  12042. (insert (pop lines))))
  12043. (message "Note stored")
  12044. (org-back-to-heading t)
  12045. (org-cycle-hide-drawers 'children))))))
  12046. (set-window-configuration org-log-note-window-configuration)
  12047. (with-current-buffer (marker-buffer org-log-note-return-to)
  12048. (goto-char org-log-note-return-to))
  12049. (move-marker org-log-note-return-to nil)
  12050. (and org-log-post-message (message "%s" org-log-post-message)))
  12051. (defun org-remove-empty-drawer-at (drawer pos)
  12052. "Remove an empty drawer DRAWER at position POS.
  12053. POS may also be a marker."
  12054. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  12055. (save-excursion
  12056. (save-restriction
  12057. (widen)
  12058. (goto-char pos)
  12059. (if (org-in-regexp
  12060. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  12061. (replace-match ""))))))
  12062. (defvar org-ts-type nil)
  12063. (defun org-sparse-tree (&optional arg type)
  12064. "Create a sparse tree, prompt for the details.
  12065. This command can create sparse trees. You first need to select the type
  12066. of match used to create the tree:
  12067. t Show all TODO entries.
  12068. T Show entries with a specific TODO keyword.
  12069. m Show entries selected by a tags/property match.
  12070. p Enter a property name and its value (both with completion on existing
  12071. names/values) and show entries with that property.
  12072. r Show entries matching a regular expression (`/' can be used as well).
  12073. b Show deadlines and scheduled items before a date.
  12074. a Show deadlines and scheduled items after a date.
  12075. d Show deadlines due within `org-deadline-warning-days'.
  12076. D Show deadlines and scheduled items between a date range."
  12077. (interactive "P")
  12078. (let (ans kwd value ts-type)
  12079. (setq type (or type org-sparse-tree-default-date-type))
  12080. (setq org-ts-type type)
  12081. (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"
  12082. (cond ((eq type 'all) "all timestamps")
  12083. ((eq type 'scheduled) "only scheduled")
  12084. ((eq type 'deadline) "only deadline")
  12085. ((eq type 'active) "only active timestamps")
  12086. ((eq type 'inactive) "only inactive timestamps")
  12087. ((eq type 'scheduled-or-deadline) "scheduled/deadline")
  12088. (t "scheduled/deadline")))
  12089. (setq ans (read-char-exclusive))
  12090. (cond
  12091. ((equal ans ?c)
  12092. (org-sparse-tree arg (cadr (member type '(scheduled-or-deadline all scheduled deadline active inactive)))))
  12093. ((equal ans ?d)
  12094. (call-interactively 'org-check-deadlines))
  12095. ((equal ans ?b)
  12096. (call-interactively 'org-check-before-date))
  12097. ((equal ans ?a)
  12098. (call-interactively 'org-check-after-date))
  12099. ((equal ans ?D)
  12100. (call-interactively 'org-check-dates-range))
  12101. ((equal ans ?t)
  12102. (call-interactively 'org-show-todo-tree))
  12103. ((equal ans ?T)
  12104. (org-show-todo-tree '(4)))
  12105. ((member ans '(?T ?m))
  12106. (call-interactively 'org-match-sparse-tree))
  12107. ((member ans '(?p ?P))
  12108. (setq kwd (org-icompleting-read "Property: "
  12109. (mapcar 'list (org-buffer-property-keys))))
  12110. (setq value (org-icompleting-read "Value: "
  12111. (mapcar 'list (org-property-values kwd))))
  12112. (unless (string-match "\\`{.*}\\'" value)
  12113. (setq value (concat "\"" value "\"")))
  12114. (org-match-sparse-tree arg (concat kwd "=" value)))
  12115. ((member ans '(?r ?R ?/))
  12116. (call-interactively 'org-occur))
  12117. (t (error "No such sparse tree command \"%c\"" ans)))))
  12118. (defvar org-occur-highlights nil
  12119. "List of overlays used for occur matches.")
  12120. (make-variable-buffer-local 'org-occur-highlights)
  12121. (defvar org-occur-parameters nil
  12122. "Parameters of the active org-occur calls.
  12123. This is a list, each call to org-occur pushes as cons cell,
  12124. containing the regular expression and the callback, onto the list.
  12125. The list can contain several entries if `org-occur' has been called
  12126. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  12127. will only contain one set of parameters. When the highlights are
  12128. removed (for example with `C-c C-c', or with the next edit (depending
  12129. on `org-remove-highlights-with-change'), this variable is emptied
  12130. as well.")
  12131. (make-variable-buffer-local 'org-occur-parameters)
  12132. (defun org-occur (regexp &optional keep-previous callback)
  12133. "Make a compact tree which shows all matches of REGEXP.
  12134. The tree will show the lines where the regexp matches, and all higher
  12135. headlines above the match. It will also show the heading after the match,
  12136. to make sure editing the matching entry is easy.
  12137. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  12138. call to `org-occur' will be kept, to allow stacking of calls to this
  12139. command.
  12140. If CALLBACK is non-nil, it is a function which is called to confirm
  12141. that the match should indeed be shown."
  12142. (interactive "sRegexp: \nP")
  12143. (when (equal regexp "")
  12144. (error "Regexp cannot be empty"))
  12145. (unless keep-previous
  12146. (org-remove-occur-highlights nil nil t))
  12147. (push (cons regexp callback) org-occur-parameters)
  12148. (let ((cnt 0))
  12149. (save-excursion
  12150. (goto-char (point-min))
  12151. (if (or (not keep-previous) ; do not want to keep
  12152. (not org-occur-highlights)) ; no previous matches
  12153. ;; hide everything
  12154. (org-overview))
  12155. (while (re-search-forward regexp nil t)
  12156. (when (or (not callback)
  12157. (save-match-data (funcall callback)))
  12158. (setq cnt (1+ cnt))
  12159. (when org-highlight-sparse-tree-matches
  12160. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  12161. (org-show-context 'occur-tree))))
  12162. (when org-remove-highlights-with-change
  12163. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  12164. nil 'local))
  12165. (unless org-sparse-tree-open-archived-trees
  12166. (org-hide-archived-subtrees (point-min) (point-max)))
  12167. (run-hooks 'org-occur-hook)
  12168. (if (org-called-interactively-p 'interactive)
  12169. (message "%d match(es) for regexp %s" cnt regexp))
  12170. cnt))
  12171. (defun org-occur-next-match (&optional n reset)
  12172. "Function for `next-error-function' to find sparse tree matches.
  12173. N is the number of matches to move, when negative move backwards.
  12174. RESET is entirely ignored - this function always goes back to the
  12175. starting point when no match is found."
  12176. (let* ((limit (if (< n 0) (point-min) (point-max)))
  12177. (search-func (if (< n 0)
  12178. 'previous-single-char-property-change
  12179. 'next-single-char-property-change))
  12180. (n (abs n))
  12181. (pos (point))
  12182. p1)
  12183. (catch 'exit
  12184. (while (setq p1 (funcall search-func (point) 'org-type))
  12185. (when (equal p1 limit)
  12186. (goto-char pos)
  12187. (error "No more matches"))
  12188. (when (equal (get-char-property p1 'org-type) 'org-occur)
  12189. (setq n (1- n))
  12190. (when (= n 0)
  12191. (goto-char p1)
  12192. (throw 'exit (point))))
  12193. (goto-char p1))
  12194. (goto-char p1)
  12195. (error "No more matches"))))
  12196. (defun org-show-context (&optional key)
  12197. "Make sure point and context are visible.
  12198. How much context is shown depends upon the variables
  12199. `org-show-hierarchy-above', `org-show-following-heading',
  12200. `org-show-entry-below' and `org-show-siblings'."
  12201. (let ((heading-p (org-at-heading-p t))
  12202. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  12203. (following-p (org-get-alist-option org-show-following-heading key))
  12204. (entry-p (org-get-alist-option org-show-entry-below key))
  12205. (siblings-p (org-get-alist-option org-show-siblings key)))
  12206. ;; Show heading or entry text
  12207. (if (and heading-p (not entry-p))
  12208. (org-flag-heading nil) ; only show the heading
  12209. (and (or entry-p (outline-invisible-p) (org-invisible-p2))
  12210. (org-show-hidden-entry))) ; show entire entry
  12211. (when following-p
  12212. ;; Show next sibling, or heading below text
  12213. (save-excursion
  12214. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  12215. (org-flag-heading nil))))
  12216. (when siblings-p (org-show-siblings))
  12217. (when hierarchy-p
  12218. ;; show all higher headings, possibly with siblings
  12219. (save-excursion
  12220. (while (and (condition-case nil
  12221. (progn (org-up-heading-all 1) t)
  12222. (error nil))
  12223. (not (bobp)))
  12224. (org-flag-heading nil)
  12225. (when siblings-p (org-show-siblings)))))
  12226. (unless (eq key 'agenda) (org-fix-ellipsis-at-bol))))
  12227. (defvar org-reveal-start-hook nil
  12228. "Hook run before revealing a location.")
  12229. (defun org-reveal (&optional siblings)
  12230. "Show current entry, hierarchy above it, and the following headline.
  12231. This can be used to show a consistent set of context around locations
  12232. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  12233. not t for the search context.
  12234. With optional argument SIBLINGS, on each level of the hierarchy all
  12235. siblings are shown. This repairs the tree structure to what it would
  12236. look like when opened with hierarchical calls to `org-cycle'.
  12237. With double optional argument \\[universal-argument] \\[universal-argument], \
  12238. go to the parent and show the
  12239. entire tree."
  12240. (interactive "P")
  12241. (run-hooks 'org-reveal-start-hook)
  12242. (let ((org-show-hierarchy-above t)
  12243. (org-show-following-heading t)
  12244. (org-show-siblings (if siblings t org-show-siblings)))
  12245. (org-show-context nil))
  12246. (when (equal siblings '(16))
  12247. (save-excursion
  12248. (when (org-up-heading-safe)
  12249. (org-show-subtree)
  12250. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  12251. (defun org-highlight-new-match (beg end)
  12252. "Highlight from BEG to END and mark the highlight is an occur headline."
  12253. (let ((ov (make-overlay beg end)))
  12254. (overlay-put ov 'face 'secondary-selection)
  12255. (overlay-put ov 'org-type 'org-occur)
  12256. (push ov org-occur-highlights)))
  12257. (defun org-remove-occur-highlights (&optional beg end noremove)
  12258. "Remove the occur highlights from the buffer.
  12259. BEG and END are ignored. If NOREMOVE is nil, remove this function
  12260. from the `before-change-functions' in the current buffer."
  12261. (interactive)
  12262. (unless org-inhibit-highlight-removal
  12263. (mapc 'delete-overlay org-occur-highlights)
  12264. (setq org-occur-highlights nil)
  12265. (setq org-occur-parameters nil)
  12266. (unless noremove
  12267. (remove-hook 'before-change-functions
  12268. 'org-remove-occur-highlights 'local))))
  12269. ;;;; Priorities
  12270. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  12271. "Regular expression matching the priority indicator.")
  12272. (defvar org-remove-priority-next-time nil)
  12273. (defun org-priority-up ()
  12274. "Increase the priority of the current item."
  12275. (interactive)
  12276. (org-priority 'up))
  12277. (defun org-priority-down ()
  12278. "Decrease the priority of the current item."
  12279. (interactive)
  12280. (org-priority 'down))
  12281. (defun org-priority (&optional action show)
  12282. "Change the priority of an item.
  12283. ACTION can be `set', `up', `down', or a character."
  12284. (interactive "P")
  12285. (if (equal action '(4))
  12286. (org-show-priority)
  12287. (unless org-enable-priority-commands
  12288. (error "Priority commands are disabled"))
  12289. (setq action (or action 'set))
  12290. (let (current new news have remove)
  12291. (save-excursion
  12292. (org-back-to-heading t)
  12293. (if (looking-at org-priority-regexp)
  12294. (setq current (string-to-char (match-string 2))
  12295. have t))
  12296. (cond
  12297. ((eq action 'remove)
  12298. (setq remove t new ?\ ))
  12299. ((or (eq action 'set)
  12300. (if (featurep 'xemacs) (characterp action) (integerp action)))
  12301. (if (not (eq action 'set))
  12302. (setq new action)
  12303. (message "Priority %c-%c, SPC to remove: "
  12304. org-highest-priority org-lowest-priority)
  12305. (save-match-data
  12306. (setq new (read-char-exclusive))))
  12307. (if (and (= (upcase org-highest-priority) org-highest-priority)
  12308. (= (upcase org-lowest-priority) org-lowest-priority))
  12309. (setq new (upcase new)))
  12310. (cond ((equal new ?\ ) (setq remove t))
  12311. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  12312. (error "Priority must be between `%c' and `%c'"
  12313. org-highest-priority org-lowest-priority))))
  12314. ((eq action 'up)
  12315. (setq new (if have
  12316. (1- current) ; normal cycling
  12317. ;; last priority was empty
  12318. (if (eq last-command this-command)
  12319. org-lowest-priority ; wrap around empty to lowest
  12320. ;; default
  12321. (if org-priority-start-cycle-with-default
  12322. org-default-priority
  12323. (1- org-default-priority))))))
  12324. ((eq action 'down)
  12325. (setq new (if have
  12326. (1+ current) ; normal cycling
  12327. ;; last priority was empty
  12328. (if (eq last-command this-command)
  12329. org-highest-priority ; wrap around empty to highest
  12330. ;; default
  12331. (if org-priority-start-cycle-with-default
  12332. org-default-priority
  12333. (1+ org-default-priority))))))
  12334. (t (error "Invalid action")))
  12335. (if (or (< (upcase new) org-highest-priority)
  12336. (> (upcase new) org-lowest-priority))
  12337. (if (and (memq action '(up down))
  12338. (not have) (not (eq last-command this-command)))
  12339. ;; `new' is from default priority
  12340. (error
  12341. "The default can not be set, see `org-default-priority' why")
  12342. ;; normal cycling: `new' is beyond highest/lowest priority
  12343. ;; and is wrapped around to the empty priority
  12344. (setq remove t)))
  12345. (setq news (format "%c" new))
  12346. (if have
  12347. (if remove
  12348. (replace-match "" t t nil 1)
  12349. (replace-match news t t nil 2))
  12350. (if remove
  12351. (error "No priority cookie found in line")
  12352. (let ((case-fold-search nil))
  12353. (looking-at org-todo-line-regexp))
  12354. (if (match-end 2)
  12355. (progn
  12356. (goto-char (match-end 2))
  12357. (insert " [#" news "]"))
  12358. (goto-char (match-beginning 3))
  12359. (insert "[#" news "] "))))
  12360. (org-preserve-lc (org-set-tags nil 'align)))
  12361. (if remove
  12362. (message "Priority removed")
  12363. (message "Priority of current item set to %s" news)))))
  12364. (defun org-show-priority ()
  12365. "Show the priority of the current item.
  12366. This priority is composed of the main priority given with the [#A] cookies,
  12367. and by additional input from the age of a schedules or deadline entry."
  12368. (interactive)
  12369. (let ((pri (if (eq major-mode 'org-agenda-mode)
  12370. (org-get-at-bol 'priority)
  12371. (save-excursion
  12372. (save-match-data
  12373. (beginning-of-line)
  12374. (and (looking-at org-heading-regexp)
  12375. (org-get-priority (match-string 0))))))))
  12376. (message "Priority is %d" (if pri pri -1000))))
  12377. (defun org-get-priority (s)
  12378. "Find priority cookie and return priority."
  12379. (save-match-data
  12380. (if (functionp org-get-priority-function)
  12381. (funcall org-get-priority-function)
  12382. (if (not (string-match org-priority-regexp s))
  12383. (* 1000 (- org-lowest-priority org-default-priority))
  12384. (* 1000 (- org-lowest-priority
  12385. (string-to-char (match-string 2 s))))))))
  12386. ;;;; Tags
  12387. (defvar org-agenda-archives-mode)
  12388. (defvar org-map-continue-from nil
  12389. "Position from where mapping should continue.
  12390. Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
  12391. (defvar org-scanner-tags nil
  12392. "The current tag list while the tags scanner is running.")
  12393. (defvar org-trust-scanner-tags nil
  12394. "Should `org-get-tags-at' use the tags for the scanner.
  12395. This is for internal dynamical scoping only.
  12396. When this is non-nil, the function `org-get-tags-at' will return the value
  12397. of `org-scanner-tags' instead of building the list by itself. This
  12398. can lead to large speed-ups when the tags scanner is used in a file with
  12399. many entries, and when the list of tags is retrieved, for example to
  12400. obtain a list of properties. Building the tags list for each entry in such
  12401. a file becomes an N^2 operation - but with this variable set, it scales
  12402. as N.")
  12403. (defun org-scan-tags (action matcher todo-only &optional start-level)
  12404. "Sca headline tags with inheritance and produce output ACTION.
  12405. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  12406. or `agenda' to produce an entry list for an agenda view. It can also be
  12407. a Lisp form or a function that should be called at each matched headline, in
  12408. this case the return value is a list of all return values from these calls.
  12409. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  12410. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  12411. only lines with a not-done TODO keyword are included in the output.
  12412. This should be the same variable that was scoped into
  12413. and set by `org-make-tags-matcher' when it constructed MATCHER.
  12414. START-LEVEL can be a string with asterisks, reducing the scope to
  12415. headlines matching this string."
  12416. (require 'org-agenda)
  12417. (let* ((re (concat "^"
  12418. (if start-level
  12419. ;; Get the correct level to match
  12420. (concat "\\*\\{" (number-to-string start-level) "\\} ")
  12421. org-outline-regexp)
  12422. " *\\(\\<\\("
  12423. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  12424. (org-re "\\)\\>\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  12425. (props (list 'face 'default
  12426. 'done-face 'org-agenda-done
  12427. 'undone-face 'default
  12428. 'mouse-face 'highlight
  12429. 'org-not-done-regexp org-not-done-regexp
  12430. 'org-todo-regexp org-todo-regexp
  12431. 'org-complex-heading-regexp org-complex-heading-regexp
  12432. 'help-echo
  12433. (format "mouse-2 or RET jump to org file %s"
  12434. (abbreviate-file-name
  12435. (or (buffer-file-name (buffer-base-buffer))
  12436. (buffer-name (buffer-base-buffer)))))))
  12437. (case-fold-search nil)
  12438. (org-map-continue-from nil)
  12439. lspos tags tags-list
  12440. (tags-alist (list (cons 0 org-file-tags)))
  12441. (llast 0) rtn rtn1 level category i txt
  12442. todo marker entry priority)
  12443. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  12444. (setq action (list 'lambda nil action)))
  12445. (save-excursion
  12446. (goto-char (point-min))
  12447. (when (eq action 'sparse-tree)
  12448. (org-overview)
  12449. (org-remove-occur-highlights))
  12450. (while (re-search-forward re nil t)
  12451. (setq org-map-continue-from nil)
  12452. (catch :skip
  12453. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  12454. tags (if (match-end 4) (org-match-string-no-properties 4)))
  12455. (goto-char (setq lspos (match-beginning 0)))
  12456. (setq level (org-reduced-level (org-outline-level))
  12457. category (org-get-category))
  12458. (setq i llast llast level)
  12459. ;; remove tag lists from same and sublevels
  12460. (while (>= i level)
  12461. (when (setq entry (assoc i tags-alist))
  12462. (setq tags-alist (delete entry tags-alist)))
  12463. (setq i (1- i)))
  12464. ;; add the next tags
  12465. (when tags
  12466. (setq tags (org-split-string tags ":")
  12467. tags-alist
  12468. (cons (cons level tags) tags-alist)))
  12469. ;; compile tags for current headline
  12470. (setq tags-list
  12471. (if org-use-tag-inheritance
  12472. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  12473. tags)
  12474. org-scanner-tags tags-list)
  12475. (when org-use-tag-inheritance
  12476. (setcdr (car tags-alist)
  12477. (mapcar (lambda (x)
  12478. (setq x (copy-sequence x))
  12479. (org-add-prop-inherited x))
  12480. (cdar tags-alist))))
  12481. (when (and tags org-use-tag-inheritance
  12482. (or (not (eq t org-use-tag-inheritance))
  12483. org-tags-exclude-from-inheritance))
  12484. ;; selective inheritance, remove uninherited ones
  12485. (setcdr (car tags-alist)
  12486. (org-remove-uninherited-tags (cdar tags-alist))))
  12487. (when (and
  12488. ;; eval matcher only when the todo condition is OK
  12489. (and (or (not todo-only) (member todo org-not-done-keywords))
  12490. (let ((case-fold-search t) (org-trust-scanner-tags t))
  12491. (eval matcher)))
  12492. ;; Call the skipper, but return t if it does not skip,
  12493. ;; so that the `and' form continues evaluating
  12494. (progn
  12495. (unless (eq action 'sparse-tree) (org-agenda-skip))
  12496. t)
  12497. ;; Check if timestamps are deselecting this entry
  12498. (or (not todo-only)
  12499. (and (member todo org-not-done-keywords)
  12500. (or (not org-agenda-tags-todo-honor-ignore-options)
  12501. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item))))))
  12502. ;; select this headline
  12503. (cond
  12504. ((eq action 'sparse-tree)
  12505. (and org-highlight-sparse-tree-matches
  12506. (org-get-heading) (match-end 0)
  12507. (org-highlight-new-match
  12508. (match-beginning 1) (match-end 1)))
  12509. (org-show-context 'tags-tree))
  12510. ((eq action 'agenda)
  12511. (setq txt (org-agenda-format-item
  12512. ""
  12513. (concat
  12514. (if (eq org-tags-match-list-sublevels 'indented)
  12515. (make-string (1- level) ?.) "")
  12516. (org-get-heading))
  12517. level category
  12518. tags-list)
  12519. priority (org-get-priority txt))
  12520. (goto-char lspos)
  12521. (setq marker (org-agenda-new-marker))
  12522. (org-add-props txt props
  12523. 'org-marker marker 'org-hd-marker marker 'org-category category
  12524. 'todo-state todo
  12525. 'priority priority 'type "tagsmatch")
  12526. (push txt rtn))
  12527. ((functionp action)
  12528. (setq org-map-continue-from nil)
  12529. (save-excursion
  12530. (setq rtn1 (funcall action))
  12531. (push rtn1 rtn)))
  12532. (t (error "Invalid action")))
  12533. ;; if we are to skip sublevels, jump to end of subtree
  12534. (unless org-tags-match-list-sublevels
  12535. (org-end-of-subtree t)
  12536. (backward-char 1))))
  12537. ;; Get the correct position from where to continue
  12538. (if org-map-continue-from
  12539. (goto-char org-map-continue-from)
  12540. (and (= (point) lspos) (end-of-line 1)))))
  12541. (when (and (eq action 'sparse-tree)
  12542. (not org-sparse-tree-open-archived-trees))
  12543. (org-hide-archived-subtrees (point-min) (point-max)))
  12544. (nreverse rtn)))
  12545. (defun org-remove-uninherited-tags (tags)
  12546. "Remove all tags that are not inherited from the list TAGS."
  12547. (cond
  12548. ((eq org-use-tag-inheritance t)
  12549. (if org-tags-exclude-from-inheritance
  12550. (org-delete-all org-tags-exclude-from-inheritance tags)
  12551. tags))
  12552. ((not org-use-tag-inheritance) nil)
  12553. ((stringp org-use-tag-inheritance)
  12554. (delq nil (mapcar
  12555. (lambda (x)
  12556. (if (and (string-match org-use-tag-inheritance x)
  12557. (not (member x org-tags-exclude-from-inheritance)))
  12558. x nil))
  12559. tags)))
  12560. ((listp org-use-tag-inheritance)
  12561. (delq nil (mapcar
  12562. (lambda (x)
  12563. (if (member x org-use-tag-inheritance) x nil))
  12564. tags)))))
  12565. (defun org-match-sparse-tree (&optional todo-only match)
  12566. "Create a sparse tree according to tags string MATCH.
  12567. MATCH can contain positive and negative selection of tags, like
  12568. \"+WORK+URGENT-WITHBOSS\".
  12569. If optional argument TODO-ONLY is non-nil, only select lines that are
  12570. also TODO lines."
  12571. (interactive "P")
  12572. (org-agenda-prepare-buffers (list (current-buffer)))
  12573. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  12574. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  12575. (defvar org-cached-props nil)
  12576. (defun org-cached-entry-get (pom property)
  12577. (if (or (eq t org-use-property-inheritance)
  12578. (and (stringp org-use-property-inheritance)
  12579. (string-match org-use-property-inheritance property))
  12580. (and (listp org-use-property-inheritance)
  12581. (member property org-use-property-inheritance)))
  12582. ;; Caching is not possible, check it directly
  12583. (org-entry-get pom property 'inherit)
  12584. ;; Get all properties, so that we can do complicated checks easily
  12585. (cdr (assoc property (or org-cached-props
  12586. (setq org-cached-props
  12587. (org-entry-properties pom)))))))
  12588. (defun org-global-tags-completion-table (&optional files)
  12589. "Return the list of all tags in all agenda buffer/files.
  12590. Optional FILES argument is a list of files which can be used
  12591. instead of the agenda files."
  12592. (save-excursion
  12593. (org-uniquify
  12594. (delq nil
  12595. (apply 'append
  12596. (mapcar
  12597. (lambda (file)
  12598. (set-buffer (find-file-noselect file))
  12599. (append (org-get-buffer-tags)
  12600. (mapcar (lambda (x) (if (stringp (car-safe x))
  12601. (list (car-safe x)) nil))
  12602. org-tag-alist)))
  12603. (if (and files (car files))
  12604. files
  12605. (org-agenda-files))))))))
  12606. (defun org-make-tags-matcher (match)
  12607. "Create the TAGS/TODO matcher form for the selection string MATCH.
  12608. The variable `todo-only' is scoped dynamically into this function.
  12609. It will be set to t if the matcher restricts matching to TODO entries,
  12610. otherwise will not be touched.
  12611. Returns a cons of the selection string MATCH and the constructed
  12612. lisp form implementing the matcher. The matcher is to be evaluated
  12613. at an Org entry, with point on the headline, and returns t if the
  12614. entry matches the selection string MATCH. The returned lisp form
  12615. references two variables with information about the entry, which
  12616. must be bound around the form's evaluation: todo, the TODO keyword
  12617. at the entry (or nil of none); and tags-list, the list of all tags
  12618. at the entry including inherited ones. Additionally, the category
  12619. of the entry (if any) must be specified as the text property
  12620. 'org-category on the headline.
  12621. See also `org-scan-tags'.
  12622. "
  12623. (declare (special todo-only))
  12624. (unless (boundp 'todo-only)
  12625. (error "org-make-tags-matcher expects todo-only to be scoped in"))
  12626. (unless match
  12627. ;; Get a new match request, with completion
  12628. (let ((org-last-tags-completion-table
  12629. (org-global-tags-completion-table)))
  12630. (setq match (org-completing-read-no-i
  12631. "Match: " 'org-tags-completion-function nil nil nil
  12632. 'org-tags-history))))
  12633. ;; Parse the string and create a lisp form
  12634. (let ((match0 match)
  12635. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  12636. minus tag mm
  12637. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  12638. orterms term orlist re-p str-p level-p level-op time-p
  12639. prop-p pn pv po gv rest)
  12640. (if (string-match "/+" match)
  12641. ;; match contains also a todo-matching request
  12642. (progn
  12643. (setq tagsmatch (substring match 0 (match-beginning 0))
  12644. todomatch (substring match (match-end 0)))
  12645. (if (string-match "^!" todomatch)
  12646. (setq todo-only t todomatch (substring todomatch 1)))
  12647. (if (string-match "^\\s-*$" todomatch)
  12648. (setq todomatch nil)))
  12649. ;; only matching tags
  12650. (setq tagsmatch match todomatch nil))
  12651. ;; Make the tags matcher
  12652. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  12653. (setq tagsmatcher t)
  12654. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  12655. (while (setq term (pop orterms))
  12656. (while (and (equal (substring term -1) "\\") orterms)
  12657. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  12658. (while (string-match re term)
  12659. (setq rest (substring term (match-end 0))
  12660. minus (and (match-end 1)
  12661. (equal (match-string 1 term) "-"))
  12662. tag (save-match-data (replace-regexp-in-string
  12663. "\\\\-" "-"
  12664. (match-string 2 term)))
  12665. re-p (equal (string-to-char tag) ?{)
  12666. level-p (match-end 4)
  12667. prop-p (match-end 5)
  12668. mm (cond
  12669. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  12670. (level-p
  12671. (setq level-op (org-op-to-function (match-string 3 term)))
  12672. `(,level-op level ,(string-to-number
  12673. (match-string 4 term))))
  12674. (prop-p
  12675. (setq pn (match-string 5 term)
  12676. po (match-string 6 term)
  12677. pv (match-string 7 term)
  12678. re-p (equal (string-to-char pv) ?{)
  12679. str-p (equal (string-to-char pv) ?\")
  12680. time-p (save-match-data
  12681. (string-match "^\"[[<].*[]>]\"$" pv))
  12682. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  12683. (if time-p (setq pv (org-matcher-time pv)))
  12684. (setq po (org-op-to-function po (if time-p 'time str-p)))
  12685. (cond
  12686. ((equal pn "CATEGORY")
  12687. (setq gv '(get-text-property (point) 'org-category)))
  12688. ((equal pn "TODO")
  12689. (setq gv 'todo))
  12690. (t
  12691. (setq gv `(org-cached-entry-get nil ,pn))))
  12692. (if re-p
  12693. (if (eq po 'org<>)
  12694. `(not (string-match ,pv (or ,gv "")))
  12695. `(string-match ,pv (or ,gv "")))
  12696. (if str-p
  12697. `(,po (or ,gv "") ,pv)
  12698. `(,po (string-to-number (or ,gv ""))
  12699. ,(string-to-number pv) ))))
  12700. (t `(member ,tag tags-list)))
  12701. mm (if minus (list 'not mm) mm)
  12702. term rest)
  12703. (push mm tagsmatcher))
  12704. (push (if (> (length tagsmatcher) 1)
  12705. (cons 'and tagsmatcher)
  12706. (car tagsmatcher))
  12707. orlist)
  12708. (setq tagsmatcher nil))
  12709. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  12710. (setq tagsmatcher
  12711. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  12712. ;; Make the todo matcher
  12713. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  12714. (setq todomatcher t)
  12715. (setq orterms (org-split-string todomatch "|") orlist nil)
  12716. (while (setq term (pop orterms))
  12717. (while (string-match re term)
  12718. (setq minus (and (match-end 1)
  12719. (equal (match-string 1 term) "-"))
  12720. kwd (match-string 2 term)
  12721. re-p (equal (string-to-char kwd) ?{)
  12722. term (substring term (match-end 0))
  12723. mm (if re-p
  12724. `(string-match ,(substring kwd 1 -1) todo)
  12725. (list 'equal 'todo kwd))
  12726. mm (if minus (list 'not mm) mm))
  12727. (push mm todomatcher))
  12728. (push (if (> (length todomatcher) 1)
  12729. (cons 'and todomatcher)
  12730. (car todomatcher))
  12731. orlist)
  12732. (setq todomatcher nil))
  12733. (setq todomatcher (if (> (length orlist) 1)
  12734. (cons 'or orlist) (car orlist))))
  12735. ;; Return the string and lisp forms of the matcher
  12736. (setq matcher (if todomatcher
  12737. (list 'and tagsmatcher todomatcher)
  12738. tagsmatcher))
  12739. (when todo-only
  12740. (setq matcher (list 'and '(member todo org-not-done-keywords)
  12741. matcher)))
  12742. (cons match0 matcher)))
  12743. (defun org-op-to-function (op &optional stringp)
  12744. "Turn an operator into the appropriate function."
  12745. (setq op
  12746. (cond
  12747. ((equal op "<" ) '(< string< org-time<))
  12748. ((equal op ">" ) '(> org-string> org-time>))
  12749. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  12750. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  12751. ((member op '("=" "==")) '(= string= org-time=))
  12752. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  12753. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  12754. (defun org<> (a b) (not (= a b)))
  12755. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  12756. (defun org-string>= (a b) (not (string< a b)))
  12757. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  12758. (defun org-string<> (a b) (not (string= a b)))
  12759. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  12760. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  12761. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  12762. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  12763. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  12764. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  12765. (defun org-2ft (s)
  12766. "Convert S to a floating point time.
  12767. If S is already a number, just return it. If it is a string, parse
  12768. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  12769. (cond
  12770. ((numberp s) s)
  12771. ((stringp s)
  12772. (condition-case nil
  12773. (float-time (apply 'encode-time (org-parse-time-string s)))
  12774. (error 0.)))
  12775. (t 0.)))
  12776. (defun org-time-today ()
  12777. "Time in seconds today at 0:00.
  12778. Returns the float number of seconds since the beginning of the
  12779. epoch to the beginning of today (00:00)."
  12780. (float-time (apply 'encode-time
  12781. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  12782. (defun org-matcher-time (s)
  12783. "Interpret a time comparison value."
  12784. (save-match-data
  12785. (cond
  12786. ((string= s "<now>") (float-time))
  12787. ((string= s "<today>") (org-time-today))
  12788. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  12789. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  12790. ((string-match "^<\\([-+][0-9]+\\)\\([hdwmy]\\)>$" s)
  12791. (+ (org-time-today)
  12792. (* (string-to-number (match-string 1 s))
  12793. (cdr (assoc (match-string 2 s)
  12794. '(("d" . 86400.0) ("w" . 604800.0)
  12795. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  12796. (t (org-2ft s)))))
  12797. (defun org-match-any-p (re list)
  12798. "Does re match any element of list?"
  12799. (setq list (mapcar (lambda (x) (string-match re x)) list))
  12800. (delq nil list))
  12801. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  12802. (defvar org-tags-overlay (make-overlay 1 1))
  12803. (org-detach-overlay org-tags-overlay)
  12804. (defun org-get-local-tags-at (&optional pos)
  12805. "Get a list of tags defined in the current headline."
  12806. (org-get-tags-at pos 'local))
  12807. (defun org-get-local-tags ()
  12808. "Get a list of tags defined in the current headline."
  12809. (org-get-tags-at nil 'local))
  12810. (defun org-get-tags-at (&optional pos local)
  12811. "Get a list of all headline tags applicable at POS.
  12812. POS defaults to point. If tags are inherited, the list contains
  12813. the targets in the same sequence as the headlines appear, i.e.
  12814. the tags of the current headline come last.
  12815. When LOCAL is non-nil, only return tags from the current headline,
  12816. ignore inherited ones."
  12817. (interactive)
  12818. (if (and org-trust-scanner-tags
  12819. (or (not pos) (equal pos (point)))
  12820. (not local))
  12821. org-scanner-tags
  12822. (let (tags ltags lastpos parent)
  12823. (save-excursion
  12824. (save-restriction
  12825. (widen)
  12826. (goto-char (or pos (point)))
  12827. (save-match-data
  12828. (catch 'done
  12829. (condition-case nil
  12830. (progn
  12831. (org-back-to-heading t)
  12832. (while (not (equal lastpos (point)))
  12833. (setq lastpos (point))
  12834. (when (looking-at
  12835. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  12836. (setq ltags (org-split-string
  12837. (org-match-string-no-properties 1) ":"))
  12838. (when parent
  12839. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  12840. (setq tags (append
  12841. (if parent
  12842. (org-remove-uninherited-tags ltags)
  12843. ltags)
  12844. tags)))
  12845. (or org-use-tag-inheritance (throw 'done t))
  12846. (if local (throw 'done t))
  12847. (or (org-up-heading-safe) (error nil))
  12848. (setq parent t)))
  12849. (error nil)))))
  12850. (if local
  12851. tags
  12852. (reverse (delete-dups
  12853. (reverse (append
  12854. (org-remove-uninherited-tags
  12855. org-file-tags) tags)))))))))
  12856. (defun org-add-prop-inherited (s)
  12857. (add-text-properties 0 (length s) '(inherited t) s)
  12858. s)
  12859. (defun org-toggle-tag (tag &optional onoff)
  12860. "Toggle the tag TAG for the current line.
  12861. If ONOFF is `on' or `off', don't toggle but set to this state."
  12862. (let (res current)
  12863. (save-excursion
  12864. (org-back-to-heading t)
  12865. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  12866. (point-at-eol) t)
  12867. (progn
  12868. (setq current (match-string 1))
  12869. (replace-match ""))
  12870. (setq current ""))
  12871. (setq current (nreverse (org-split-string current ":")))
  12872. (cond
  12873. ((eq onoff 'on)
  12874. (setq res t)
  12875. (or (member tag current) (push tag current)))
  12876. ((eq onoff 'off)
  12877. (or (not (member tag current)) (setq current (delete tag current))))
  12878. (t (if (member tag current)
  12879. (setq current (delete tag current))
  12880. (setq res t)
  12881. (push tag current))))
  12882. (end-of-line 1)
  12883. (if current
  12884. (progn
  12885. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  12886. (org-set-tags nil t))
  12887. (delete-horizontal-space))
  12888. (run-hooks 'org-after-tags-change-hook))
  12889. res))
  12890. (defun org-align-tags-here (to-col)
  12891. ;; Assumes that this is a headline
  12892. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  12893. (beginning-of-line 1)
  12894. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12895. (< pos (match-beginning 2)))
  12896. (progn
  12897. (setq tags-l (- (match-end 2) (match-beginning 2)))
  12898. (goto-char (match-beginning 1))
  12899. (insert " ")
  12900. (delete-region (point) (1+ (match-beginning 2)))
  12901. (setq ncol (max (current-column)
  12902. (1+ col)
  12903. (if (> to-col 0)
  12904. to-col
  12905. (- (abs to-col) tags-l))))
  12906. (setq p (point))
  12907. (insert (make-string (- ncol (current-column)) ?\ ))
  12908. (setq ncol (current-column))
  12909. (when indent-tabs-mode (tabify p (point-at-eol)))
  12910. (org-move-to-column (min ncol col) t))
  12911. (goto-char pos))))
  12912. (defun org-set-tags-command (&optional arg just-align)
  12913. "Call the set-tags command for the current entry."
  12914. (interactive "P")
  12915. (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
  12916. (org-set-tags arg just-align)
  12917. (save-excursion
  12918. (unless (and (org-region-active-p)
  12919. org-loop-over-headlines-in-active-region)
  12920. (org-back-to-heading t))
  12921. (org-set-tags arg just-align))))
  12922. (defun org-set-tags-to (data)
  12923. "Set the tags of the current entry to DATA, replacing the current tags.
  12924. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  12925. If DATA is nil or the empty string, any tags will be removed."
  12926. (interactive "sTags: ")
  12927. (setq data
  12928. (cond
  12929. ((eq data nil) "")
  12930. ((equal data "") "")
  12931. ((stringp data)
  12932. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  12933. ":"))
  12934. ((listp data)
  12935. (concat ":" (mapconcat 'identity data ":") ":"))))
  12936. (when data
  12937. (save-excursion
  12938. (org-back-to-heading t)
  12939. (when (looking-at org-complex-heading-regexp)
  12940. (if (match-end 5)
  12941. (progn
  12942. (goto-char (match-beginning 5))
  12943. (insert data)
  12944. (delete-region (point) (point-at-eol))
  12945. (org-set-tags nil 'align))
  12946. (goto-char (point-at-eol))
  12947. (insert " " data)
  12948. (org-set-tags nil 'align)))
  12949. (beginning-of-line 1)
  12950. (if (looking-at ".*?\\([ \t]+\\)$")
  12951. (delete-region (match-beginning 1) (match-end 1))))))
  12952. (defun org-align-all-tags ()
  12953. "Align the tags i all headings."
  12954. (interactive)
  12955. (save-excursion
  12956. (or (ignore-errors (org-back-to-heading t))
  12957. (outline-next-heading))
  12958. (if (org-at-heading-p)
  12959. (org-set-tags t)
  12960. (message "No headings"))))
  12961. (defvar org-indent-indentation-per-level)
  12962. (defun org-set-tags (&optional arg just-align)
  12963. "Set the tags for the current headline.
  12964. With prefix ARG, realign all tags in headings in the current buffer."
  12965. (interactive "P")
  12966. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  12967. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  12968. 'region-start-level 'region))
  12969. org-loop-over-headlines-in-active-region)
  12970. (org-map-entries
  12971. ;; We don't use ARG and JUST-ALIGN here these args are not
  12972. ;; useful when looping over headlines
  12973. `(org-set-tags)
  12974. org-loop-over-headlines-in-active-region
  12975. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  12976. (let* ((re org-outline-regexp-bol)
  12977. (current (unless arg (org-get-tags-string)))
  12978. (col (current-column))
  12979. (org-setting-tags t)
  12980. table current-tags inherited-tags ; computed below when needed
  12981. tags p0 c0 c1 rpl di tc level)
  12982. (if arg
  12983. (save-excursion
  12984. (goto-char (point-min))
  12985. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  12986. (while (re-search-forward re nil t)
  12987. (org-set-tags nil t)
  12988. (end-of-line 1)))
  12989. (message "All tags realigned to column %d" org-tags-column))
  12990. (if just-align
  12991. (setq tags current)
  12992. ;; Get a new set of tags from the user
  12993. (save-excursion
  12994. (setq table (append org-tag-persistent-alist
  12995. (or org-tag-alist (org-get-buffer-tags))
  12996. (and
  12997. org-complete-tags-always-offer-all-agenda-tags
  12998. (org-global-tags-completion-table
  12999. (org-agenda-files))))
  13000. org-last-tags-completion-table table
  13001. current-tags (org-split-string current ":")
  13002. inherited-tags (nreverse
  13003. (nthcdr (length current-tags)
  13004. (nreverse (org-get-tags-at))))
  13005. tags
  13006. (if (or (eq t org-use-fast-tag-selection)
  13007. (and org-use-fast-tag-selection
  13008. (delq nil (mapcar 'cdr table))))
  13009. (org-fast-tag-selection
  13010. current-tags inherited-tags table
  13011. (if org-fast-tag-selection-include-todo
  13012. org-todo-key-alist))
  13013. (let ((org-add-colon-after-tag-completion (< 1 (length table))))
  13014. (org-trim
  13015. (org-icompleting-read "Tags: "
  13016. 'org-tags-completion-function
  13017. nil nil current 'org-tags-history))))))
  13018. (while (string-match "[-+&]+" tags)
  13019. ;; No boolean logic, just a list
  13020. (setq tags (replace-match ":" t t tags))))
  13021. (setq tags (replace-regexp-in-string "[,]" ":" tags))
  13022. (if org-tags-sort-function
  13023. (setq tags (mapconcat 'identity
  13024. (sort (org-split-string
  13025. tags (org-re "[^[:alnum:]_@#%]+"))
  13026. org-tags-sort-function) ":")))
  13027. (if (string-match "\\`[\t ]*\\'" tags)
  13028. (setq tags "")
  13029. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  13030. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  13031. ;; Insert new tags at the correct column
  13032. (beginning-of-line 1)
  13033. (setq level (or (and (looking-at org-outline-regexp)
  13034. (- (match-end 0) (point) 1))
  13035. 1))
  13036. (cond
  13037. ((and (equal current "") (equal tags "")))
  13038. ((re-search-forward
  13039. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  13040. (point-at-eol) t)
  13041. (if (equal tags "")
  13042. (setq rpl "")
  13043. (goto-char (match-beginning 0))
  13044. (setq c0 (current-column)
  13045. ;; compute offset for the case of org-indent-mode active
  13046. di (if org-indent-mode
  13047. (* (1- org-indent-indentation-per-level) (1- level))
  13048. 0)
  13049. p0 (if (equal (char-before) ?*) (1+ (point)) (point))
  13050. tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
  13051. c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (length tags))))
  13052. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  13053. (replace-match rpl t t)
  13054. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  13055. tags)
  13056. (t (error "Tags alignment failed")))
  13057. (org-move-to-column col)
  13058. (unless just-align
  13059. (run-hooks 'org-after-tags-change-hook))))))
  13060. (defun org-change-tag-in-region (beg end tag off)
  13061. "Add or remove TAG for each entry in the region.
  13062. This works in the agenda, and also in an org-mode buffer."
  13063. (interactive
  13064. (list (region-beginning) (region-end)
  13065. (let ((org-last-tags-completion-table
  13066. (if (derived-mode-p 'org-mode)
  13067. (org-get-buffer-tags)
  13068. (org-global-tags-completion-table))))
  13069. (org-icompleting-read
  13070. "Tag: " 'org-tags-completion-function nil nil nil
  13071. 'org-tags-history))
  13072. (progn
  13073. (message "[s]et or [r]emove? ")
  13074. (equal (read-char-exclusive) ?r))))
  13075. (if (fboundp 'deactivate-mark) (deactivate-mark))
  13076. (let ((agendap (equal major-mode 'org-agenda-mode))
  13077. l1 l2 m buf pos newhead (cnt 0))
  13078. (goto-char end)
  13079. (setq l2 (1- (org-current-line)))
  13080. (goto-char beg)
  13081. (setq l1 (org-current-line))
  13082. (loop for l from l1 to l2 do
  13083. (org-goto-line l)
  13084. (setq m (get-text-property (point) 'org-hd-marker))
  13085. (when (or (and (derived-mode-p 'org-mode) (org-at-heading-p))
  13086. (and agendap m))
  13087. (setq buf (if agendap (marker-buffer m) (current-buffer))
  13088. pos (if agendap m (point)))
  13089. (with-current-buffer buf
  13090. (save-excursion
  13091. (save-restriction
  13092. (goto-char pos)
  13093. (setq cnt (1+ cnt))
  13094. (org-toggle-tag tag (if off 'off 'on))
  13095. (setq newhead (org-get-heading)))))
  13096. (and agendap (org-agenda-change-all-lines newhead m))))
  13097. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  13098. (defun org-tags-completion-function (string predicate &optional flag)
  13099. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  13100. (confirm (lambda (x) (stringp (car x)))))
  13101. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  13102. (setq s1 (match-string 1 string)
  13103. s2 (match-string 2 string))
  13104. (setq s1 "" s2 string))
  13105. (cond
  13106. ((eq flag nil)
  13107. ;; try completion
  13108. (setq rtn (try-completion s2 ctable confirm))
  13109. (if (stringp rtn)
  13110. (setq rtn
  13111. (concat s1 s2 (substring rtn (length s2))
  13112. (if (and org-add-colon-after-tag-completion
  13113. (assoc rtn ctable))
  13114. ":" ""))))
  13115. rtn)
  13116. ((eq flag t)
  13117. ;; all-completions
  13118. (all-completions s2 ctable confirm)
  13119. )
  13120. ((eq flag 'lambda)
  13121. ;; exact match?
  13122. (assoc s2 ctable)))
  13123. ))
  13124. (defun org-fast-tag-insert (kwd tags face &optional end)
  13125. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  13126. (insert (format "%-12s" (concat kwd ":"))
  13127. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  13128. (or end "")))
  13129. (defun org-fast-tag-show-exit (flag)
  13130. (save-excursion
  13131. (org-goto-line 3)
  13132. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  13133. (replace-match ""))
  13134. (when flag
  13135. (end-of-line 1)
  13136. (org-move-to-column (- (window-width) 19) t)
  13137. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  13138. (defun org-set-current-tags-overlay (current prefix)
  13139. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  13140. (if (featurep 'xemacs)
  13141. (org-overlay-display org-tags-overlay (concat prefix s)
  13142. 'secondary-selection)
  13143. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  13144. (org-overlay-display org-tags-overlay (concat prefix s)))))
  13145. (defvar org-last-tag-selection-key nil)
  13146. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  13147. "Fast tag selection with single keys.
  13148. CURRENT is the current list of tags in the headline, INHERITED is the
  13149. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  13150. possibly with grouping information. TODO-TABLE is a similar table with
  13151. TODO keywords, should these have keys assigned to them.
  13152. If the keys are nil, a-z are automatically assigned.
  13153. Returns the new tags string, or nil to not change the current settings."
  13154. (let* ((fulltable (append table todo-table))
  13155. (maxlen (apply 'max (mapcar
  13156. (lambda (x)
  13157. (if (stringp (car x)) (string-width (car x)) 0))
  13158. fulltable)))
  13159. (buf (current-buffer))
  13160. (expert (eq org-fast-tag-selection-single-key 'expert))
  13161. (buffer-tags nil)
  13162. (fwidth (+ maxlen 3 1 3))
  13163. (ncol (/ (- (window-width) 4) fwidth))
  13164. (i-face 'org-done)
  13165. (c-face 'org-todo)
  13166. tg cnt e c char c1 c2 ntable tbl rtn
  13167. ov-start ov-end ov-prefix
  13168. (exit-after-next org-fast-tag-selection-single-key)
  13169. (done-keywords org-done-keywords)
  13170. groups ingroup)
  13171. (save-excursion
  13172. (beginning-of-line 1)
  13173. (if (looking-at
  13174. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13175. (setq ov-start (match-beginning 1)
  13176. ov-end (match-end 1)
  13177. ov-prefix "")
  13178. (setq ov-start (1- (point-at-eol))
  13179. ov-end (1+ ov-start))
  13180. (skip-chars-forward "^\n\r")
  13181. (setq ov-prefix
  13182. (concat
  13183. (buffer-substring (1- (point)) (point))
  13184. (if (> (current-column) org-tags-column)
  13185. " "
  13186. (make-string (- org-tags-column (current-column)) ?\ ))))))
  13187. (move-overlay org-tags-overlay ov-start ov-end)
  13188. (save-window-excursion
  13189. (if expert
  13190. (set-buffer (get-buffer-create " *Org tags*"))
  13191. (delete-other-windows)
  13192. (split-window-vertically)
  13193. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  13194. (erase-buffer)
  13195. (org-set-local 'org-done-keywords done-keywords)
  13196. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  13197. (org-fast-tag-insert "Current" current c-face "\n\n")
  13198. (org-fast-tag-show-exit exit-after-next)
  13199. (org-set-current-tags-overlay current ov-prefix)
  13200. (setq tbl fulltable char ?a cnt 0)
  13201. (while (setq e (pop tbl))
  13202. (cond
  13203. ((equal (car e) :startgroup)
  13204. (push '() groups) (setq ingroup t)
  13205. (when (not (= cnt 0))
  13206. (setq cnt 0)
  13207. (insert "\n"))
  13208. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  13209. ((equal (car e) :endgroup)
  13210. (setq ingroup nil cnt 0)
  13211. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  13212. ((equal e '(:newline))
  13213. (when (not (= cnt 0))
  13214. (setq cnt 0)
  13215. (insert "\n")
  13216. (setq e (car tbl))
  13217. (while (equal (car tbl) '(:newline))
  13218. (insert "\n")
  13219. (setq tbl (cdr tbl)))))
  13220. (t
  13221. (setq tg (copy-sequence (car e)) c2 nil)
  13222. (if (cdr e)
  13223. (setq c (cdr e))
  13224. ;; automatically assign a character.
  13225. (setq c1 (string-to-char
  13226. (downcase (substring
  13227. tg (if (= (string-to-char tg) ?@) 1 0)))))
  13228. (if (or (rassoc c1 ntable) (rassoc c1 table))
  13229. (while (or (rassoc char ntable) (rassoc char table))
  13230. (setq char (1+ char)))
  13231. (setq c2 c1))
  13232. (setq c (or c2 char)))
  13233. (if ingroup (push tg (car groups)))
  13234. (setq tg (org-add-props tg nil 'face
  13235. (cond
  13236. ((not (assoc tg table))
  13237. (org-get-todo-face tg))
  13238. ((member tg current) c-face)
  13239. ((member tg inherited) i-face))))
  13240. (if (and (= cnt 0) (not ingroup)) (insert " "))
  13241. (insert "[" c "] " tg (make-string
  13242. (- fwidth 4 (length tg)) ?\ ))
  13243. (push (cons tg c) ntable)
  13244. (when (= (setq cnt (1+ cnt)) ncol)
  13245. (insert "\n")
  13246. (if ingroup (insert " "))
  13247. (setq cnt 0)))))
  13248. (setq ntable (nreverse ntable))
  13249. (insert "\n")
  13250. (goto-char (point-min))
  13251. (if (not expert) (org-fit-window-to-buffer))
  13252. (setq rtn
  13253. (catch 'exit
  13254. (while t
  13255. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  13256. (if (not groups) "no " "")
  13257. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  13258. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  13259. (setq org-last-tag-selection-key c)
  13260. (cond
  13261. ((= c ?\r) (throw 'exit t))
  13262. ((= c ?!)
  13263. (setq groups (not groups))
  13264. (goto-char (point-min))
  13265. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  13266. ((= c ?\C-c)
  13267. (if (not expert)
  13268. (org-fast-tag-show-exit
  13269. (setq exit-after-next (not exit-after-next)))
  13270. (setq expert nil)
  13271. (delete-other-windows)
  13272. (set-window-buffer (split-window-vertically) " *Org tags*")
  13273. (org-switch-to-buffer-other-window " *Org tags*")
  13274. (org-fit-window-to-buffer)))
  13275. ((or (= c ?\C-g)
  13276. (and (= c ?q) (not (rassoc c ntable))))
  13277. (org-detach-overlay org-tags-overlay)
  13278. (setq quit-flag t))
  13279. ((= c ?\ )
  13280. (setq current nil)
  13281. (if exit-after-next (setq exit-after-next 'now)))
  13282. ((= c ?\t)
  13283. (condition-case nil
  13284. (setq tg (org-icompleting-read
  13285. "Tag: "
  13286. (or buffer-tags
  13287. (with-current-buffer buf
  13288. (org-get-buffer-tags)))))
  13289. (quit (setq tg "")))
  13290. (when (string-match "\\S-" tg)
  13291. (add-to-list 'buffer-tags (list tg))
  13292. (if (member tg current)
  13293. (setq current (delete tg current))
  13294. (push tg current)))
  13295. (if exit-after-next (setq exit-after-next 'now)))
  13296. ((setq e (rassoc c todo-table) tg (car e))
  13297. (with-current-buffer buf
  13298. (save-excursion (org-todo tg)))
  13299. (if exit-after-next (setq exit-after-next 'now)))
  13300. ((setq e (rassoc c ntable) tg (car e))
  13301. (if (member tg current)
  13302. (setq current (delete tg current))
  13303. (loop for g in groups do
  13304. (if (member tg g)
  13305. (mapc (lambda (x)
  13306. (setq current (delete x current)))
  13307. g)))
  13308. (push tg current))
  13309. (if exit-after-next (setq exit-after-next 'now))))
  13310. ;; Create a sorted list
  13311. (setq current
  13312. (sort current
  13313. (lambda (a b)
  13314. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  13315. (if (eq exit-after-next 'now) (throw 'exit t))
  13316. (goto-char (point-min))
  13317. (beginning-of-line 2)
  13318. (delete-region (point) (point-at-eol))
  13319. (org-fast-tag-insert "Current" current c-face)
  13320. (org-set-current-tags-overlay current ov-prefix)
  13321. (while (re-search-forward
  13322. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  13323. (setq tg (match-string 1))
  13324. (add-text-properties
  13325. (match-beginning 1) (match-end 1)
  13326. (list 'face
  13327. (cond
  13328. ((member tg current) c-face)
  13329. ((member tg inherited) i-face)
  13330. (t (get-text-property (match-beginning 1) 'face))))))
  13331. (goto-char (point-min)))))
  13332. (org-detach-overlay org-tags-overlay)
  13333. (if rtn
  13334. (mapconcat 'identity current ":")
  13335. nil))))
  13336. (defun org-get-tags-string ()
  13337. "Get the TAGS string in the current headline."
  13338. (unless (org-at-heading-p t)
  13339. (error "Not on a heading"))
  13340. (save-excursion
  13341. (beginning-of-line 1)
  13342. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13343. (org-match-string-no-properties 1)
  13344. "")))
  13345. (defun org-get-tags ()
  13346. "Get the list of tags specified in the current headline."
  13347. (org-split-string (org-get-tags-string) ":"))
  13348. (defun org-get-buffer-tags ()
  13349. "Get a table of all tags used in the buffer, for completion."
  13350. (let (tags)
  13351. (save-excursion
  13352. (goto-char (point-min))
  13353. (while (re-search-forward
  13354. (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
  13355. (when (equal (char-after (point-at-bol 0)) ?*)
  13356. (mapc (lambda (x) (add-to-list 'tags x))
  13357. (org-split-string (org-match-string-no-properties 1) ":")))))
  13358. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  13359. (mapcar 'list tags)))
  13360. ;;;; The mapping API
  13361. (defun org-map-entries (func &optional match scope &rest skip)
  13362. "Call FUNC at each headline selected by MATCH in SCOPE.
  13363. FUNC is a function or a lisp form. The function will be called without
  13364. arguments, with the cursor positioned at the beginning of the headline.
  13365. The return values of all calls to the function will be collected and
  13366. returned as a list.
  13367. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  13368. does not need to preserve point. After evaluation, the cursor will be
  13369. moved to the end of the line (presumably of the headline of the
  13370. processed entry) and search continues from there. Under some
  13371. circumstances, this may not produce the wanted results. For example,
  13372. if you have removed (e.g. archived) the current (sub)tree it could
  13373. mean that the next entry will be skipped entirely. In such cases, you
  13374. can specify the position from where search should continue by making
  13375. FUNC set the variable `org-map-continue-from' to the desired buffer
  13376. position.
  13377. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  13378. Only headlines that are matched by this query will be considered during
  13379. the iteration. When MATCH is nil or t, all headlines will be
  13380. visited by the iteration.
  13381. SCOPE determines the scope of this command. It can be any of:
  13382. nil The current buffer, respecting the restriction if any
  13383. tree The subtree started with the entry at point
  13384. region The entries within the active region, if any
  13385. region-start-level
  13386. The entries within the active region, but only those at
  13387. the same level than the first one.
  13388. file The current buffer, without restriction
  13389. file-with-archives
  13390. The current buffer, and any archives associated with it
  13391. agenda All agenda files
  13392. agenda-with-archives
  13393. All agenda files with any archive files associated with them
  13394. \(file1 file2 ...)
  13395. If this is a list, all files in the list will be scanned
  13396. The remaining args are treated as settings for the skipping facilities of
  13397. the scanner. The following items can be given here:
  13398. archive skip trees with the archive tag
  13399. comment skip trees with the COMMENT keyword
  13400. function or Emacs Lisp form:
  13401. will be used as value for `org-agenda-skip-function', so
  13402. whenever the function returns a position, FUNC will not be
  13403. called for that entry and search will continue from the
  13404. position returned
  13405. If your function needs to retrieve the tags including inherited tags
  13406. at the *current* entry, you can use the value of the variable
  13407. `org-scanner-tags' which will be much faster than getting the value
  13408. with `org-get-tags-at'. If your function gets properties with
  13409. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  13410. to t around the call to `org-entry-properties' to get the same speedup.
  13411. Note that if your function moves around to retrieve tags and properties at
  13412. a *different* entry, you cannot use these techniques."
  13413. (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
  13414. (not (org-region-active-p)))
  13415. (let* ((org-agenda-archives-mode nil) ; just to make sure
  13416. (org-agenda-skip-archived-trees (memq 'archive skip))
  13417. (org-agenda-skip-comment-trees (memq 'comment skip))
  13418. (org-agenda-skip-function
  13419. (car (org-delete-all '(comment archive) skip)))
  13420. (org-tags-match-list-sublevels t)
  13421. (start-level (eq scope 'region-start-level))
  13422. matcher file res
  13423. org-todo-keywords-for-agenda
  13424. org-done-keywords-for-agenda
  13425. org-todo-keyword-alist-for-agenda
  13426. org-drawers-for-agenda
  13427. org-tag-alist-for-agenda
  13428. todo-only)
  13429. (cond
  13430. ((eq match t) (setq matcher t))
  13431. ((eq match nil) (setq matcher t))
  13432. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  13433. (save-window-excursion
  13434. (save-restriction
  13435. (cond ((eq scope 'tree)
  13436. (org-back-to-heading t)
  13437. (org-narrow-to-subtree)
  13438. (setq scope nil))
  13439. ((and (or (eq scope 'region) (eq scope 'region-start-level))
  13440. (org-region-active-p))
  13441. ;; If needed, set start-level to a string like "2"
  13442. (when start-level
  13443. (save-excursion
  13444. (goto-char (region-beginning))
  13445. (unless (org-at-heading-p) (outline-next-heading))
  13446. (setq start-level (org-current-level))))
  13447. (narrow-to-region (region-beginning)
  13448. (save-excursion
  13449. (goto-char (region-end))
  13450. (unless (and (bolp) (org-at-heading-p))
  13451. (outline-next-heading))
  13452. (point)))
  13453. (setq scope nil)))
  13454. (if (not scope)
  13455. (progn
  13456. (org-agenda-prepare-buffers
  13457. (list (buffer-file-name (current-buffer))))
  13458. (setq res (org-scan-tags func matcher todo-only start-level)))
  13459. ;; Get the right scope
  13460. (cond
  13461. ((and scope (listp scope) (symbolp (car scope)))
  13462. (setq scope (eval scope)))
  13463. ((eq scope 'agenda)
  13464. (setq scope (org-agenda-files t)))
  13465. ((eq scope 'agenda-with-archives)
  13466. (setq scope (org-agenda-files t))
  13467. (setq scope (org-add-archive-files scope)))
  13468. ((eq scope 'file)
  13469. (setq scope (list (buffer-file-name))))
  13470. ((eq scope 'file-with-archives)
  13471. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  13472. (org-agenda-prepare-buffers scope)
  13473. (while (setq file (pop scope))
  13474. (with-current-buffer (org-find-base-buffer-visiting file)
  13475. (save-excursion
  13476. (save-restriction
  13477. (widen)
  13478. (goto-char (point-min))
  13479. (setq res (append res (org-scan-tags func matcher todo-only))))))))))
  13480. res)))
  13481. ;;;; Properties
  13482. ;;; Setting and retrieving properties
  13483. (defconst org-special-properties
  13484. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  13485. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED" "FILE" "CLOCKSUM" "CLOCKSUM_T")
  13486. "The special properties valid in Org-mode.
  13487. These are properties that are not defined in the property drawer,
  13488. but in some other way.")
  13489. (defconst org-default-properties
  13490. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  13491. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  13492. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  13493. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  13494. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  13495. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  13496. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  13497. "Some properties that are used by Org-mode for various purposes.
  13498. Being in this list makes sure that they are offered for completion.")
  13499. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  13500. "Regular expression matching the first line of a property drawer.")
  13501. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  13502. "Regular expression matching the last line of a property drawer.")
  13503. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  13504. "Regular expression matching the first line of a property drawer.")
  13505. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  13506. "Regular expression matching the first line of a property drawer.")
  13507. (defconst org-property-drawer-re
  13508. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  13509. org-property-end-re "\\)\n?")
  13510. "Matches an entire property drawer.")
  13511. (defconst org-clock-drawer-re
  13512. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  13513. org-property-end-re "\\)\n?")
  13514. "Matches an entire clock drawer.")
  13515. (defsubst org-re-property (property)
  13516. "Return a regexp matching a PROPERTY line.
  13517. Match group 1 will be set to the value."
  13518. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)"))
  13519. (defsubst org-re-property-keyword (property)
  13520. "Return a regexp matching a PROPERTY line, possibly with no
  13521. value for the property."
  13522. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)?"))
  13523. (defun org-property-action ()
  13524. "Do an action on properties."
  13525. (interactive)
  13526. (let (c)
  13527. (org-at-property-p)
  13528. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  13529. (setq c (read-char-exclusive))
  13530. (cond
  13531. ((equal c ?s)
  13532. (call-interactively 'org-set-property))
  13533. ((equal c ?d)
  13534. (call-interactively 'org-delete-property))
  13535. ((equal c ?D)
  13536. (call-interactively 'org-delete-property-globally))
  13537. ((equal c ?c)
  13538. (call-interactively 'org-compute-property-at-point))
  13539. (t (error "No such property action %c" c)))))
  13540. (defun org-inc-effort ()
  13541. "Increment the value of the effort property in the current entry."
  13542. (interactive)
  13543. (org-set-effort nil t))
  13544. (defvar org-clock-effort) ;; Defined in org-clock.el
  13545. (defvar org-clock-current-task) ;; Defined in org-clock.el
  13546. (defun org-set-effort (&optional value increment)
  13547. "Set the effort property of the current entry.
  13548. With numerical prefix arg, use the nth allowed value, 0 stands for the
  13549. 10th allowed value.
  13550. When INCREMENT is non-nil, set the property to the next allowed value."
  13551. (interactive "P")
  13552. (if (equal value 0) (setq value 10))
  13553. (let* ((completion-ignore-case t)
  13554. (prop org-effort-property)
  13555. (cur (org-entry-get nil prop))
  13556. (allowed (org-property-get-allowed-values nil prop 'table))
  13557. (existing (mapcar 'list (org-property-values prop)))
  13558. (heading (nth 4 (org-heading-components)))
  13559. rpl
  13560. (val (cond
  13561. ((stringp value) value)
  13562. ((and allowed (integerp value))
  13563. (or (car (nth (1- value) allowed))
  13564. (car (org-last allowed))))
  13565. ((and allowed increment)
  13566. (or (caadr (member (list cur) allowed))
  13567. (error "Allowed effort values are not set")))
  13568. (allowed
  13569. (message "Select 1-9,0, [RET%s]: %s"
  13570. (if cur (concat "=" cur) "")
  13571. (mapconcat 'car allowed " "))
  13572. (setq rpl (read-char-exclusive))
  13573. (if (equal rpl ?\r)
  13574. cur
  13575. (setq rpl (- rpl ?0))
  13576. (if (equal rpl 0) (setq rpl 10))
  13577. (if (and (> rpl 0) (<= rpl (length allowed)))
  13578. (car (nth (1- rpl) allowed))
  13579. (org-completing-read "Effort: " allowed nil))))
  13580. (t
  13581. (let (org-completion-use-ido org-completion-use-iswitchb)
  13582. (org-completing-read
  13583. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  13584. (concat "[" cur "]") "")
  13585. ": ")
  13586. existing nil nil "" nil cur))))))
  13587. (unless (equal (org-entry-get nil prop) val)
  13588. (org-entry-put nil prop val))
  13589. (save-excursion
  13590. (org-back-to-heading t)
  13591. (put-text-property (point-at-bol) (point-at-eol) 'org-effort val))
  13592. (when (string= heading org-clock-current-task)
  13593. (setq org-clock-effort (get-text-property (point-at-bol) 'org-effort))
  13594. (org-clock-update-mode-line))
  13595. (message "%s is now %s" prop val)))
  13596. (defun org-at-property-p ()
  13597. "Is cursor inside a property drawer?"
  13598. (save-excursion
  13599. (beginning-of-line 1)
  13600. (when (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))
  13601. (save-match-data ;; Used by calling procedures
  13602. (let ((p (point))
  13603. (range (unless (org-before-first-heading-p)
  13604. (org-get-property-block))))
  13605. (and range (<= (car range) p) (< p (cdr range))))))))
  13606. (defun org-get-property-block (&optional beg end force)
  13607. "Return the (beg . end) range of the body of the property drawer.
  13608. BEG and END are the beginning and end of the current subtree, or of
  13609. the part before the first headline. If they are not given, they will
  13610. be found. If the drawer does not exist and FORCE is non-nil, create
  13611. the drawer."
  13612. (catch 'exit
  13613. (save-excursion
  13614. (let* ((beg (or beg (and (org-before-first-heading-p) (point-min))
  13615. (progn (org-back-to-heading t) (point))))
  13616. (end (or end (and (not (outline-next-heading)) (point-max))
  13617. (point))))
  13618. (goto-char beg)
  13619. (if (re-search-forward org-property-start-re end t)
  13620. (setq beg (1+ (match-end 0)))
  13621. (if force
  13622. (save-excursion
  13623. (org-insert-property-drawer)
  13624. (setq end (progn (outline-next-heading) (point))))
  13625. (throw 'exit nil))
  13626. (goto-char beg)
  13627. (if (re-search-forward org-property-start-re end t)
  13628. (setq beg (1+ (match-end 0)))))
  13629. (if (re-search-forward org-property-end-re end t)
  13630. (setq end (match-beginning 0))
  13631. (or force (throw 'exit nil))
  13632. (goto-char beg)
  13633. (setq end beg)
  13634. (org-indent-line)
  13635. (insert ":END:\n"))
  13636. (cons beg end)))))
  13637. (defun org-entry-properties (&optional pom which specific)
  13638. "Get all properties of the entry at point-or-marker POM.
  13639. This includes the TODO keyword, the tags, time strings for deadline,
  13640. scheduled, and clocking, and any additional properties defined in the
  13641. entry. The return value is an alist, keys may occur multiple times
  13642. if the property key was used several times.
  13643. POM may also be nil, in which case the current entry is used.
  13644. If WHICH is nil or `all', get all properties. If WHICH is
  13645. `special' or `standard', only get that subclass. If WHICH
  13646. is a string only get exactly this property. SPECIFIC can be a string, the
  13647. specific property we are interested in. Specifying it can speed
  13648. things up because then unnecessary parsing is avoided."
  13649. (setq which (or which 'all))
  13650. (org-with-point-at pom
  13651. (let ((clockstr (substring org-clock-string 0 -1))
  13652. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
  13653. (case-fold-search nil)
  13654. beg end range props sum-props key key1 value string clocksum clocksumt)
  13655. (save-excursion
  13656. (when (condition-case nil
  13657. (and (derived-mode-p 'org-mode) (org-back-to-heading t))
  13658. (error nil))
  13659. (setq beg (point))
  13660. (setq sum-props (get-text-property (point) 'org-summaries))
  13661. (setq clocksum (get-text-property (point) :org-clock-minutes)
  13662. clocksumt (get-text-property (point) :org-clock-minutes-today))
  13663. (outline-next-heading)
  13664. (setq end (point))
  13665. (when (memq which '(all special))
  13666. ;; Get the special properties, like TODO and tags
  13667. (goto-char beg)
  13668. (when (and (or (not specific) (string= specific "TODO"))
  13669. (looking-at org-todo-line-regexp) (match-end 2))
  13670. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  13671. (when (and (or (not specific) (string= specific "PRIORITY"))
  13672. (looking-at org-priority-regexp))
  13673. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  13674. (when (or (not specific) (string= specific "FILE"))
  13675. (push (cons "FILE" buffer-file-name) props))
  13676. (when (and (or (not specific) (string= specific "TAGS"))
  13677. (setq value (org-get-tags-string))
  13678. (string-match "\\S-" value))
  13679. (push (cons "TAGS" value) props))
  13680. (when (and (or (not specific) (string= specific "ALLTAGS"))
  13681. (setq value (org-get-tags-at)))
  13682. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
  13683. ":"))
  13684. props))
  13685. (when (or (not specific) (string= specific "BLOCKED"))
  13686. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
  13687. (when (or (not specific)
  13688. (member specific
  13689. '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
  13690. "TIMESTAMP" "TIMESTAMP_IA")))
  13691. (catch 'match
  13692. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  13693. (setq key (if (match-end 1)
  13694. (substring (org-match-string-no-properties 1)
  13695. 0 -1))
  13696. string (if (equal key clockstr)
  13697. (org-trim
  13698. (buffer-substring-no-properties
  13699. (match-beginning 3) (goto-char
  13700. (point-at-eol))))
  13701. (substring (org-match-string-no-properties 3)
  13702. 1 -1)))
  13703. ;; Get the correct property name from the key. This is
  13704. ;; necessary if the user has configured time keywords.
  13705. (setq key1 (concat key ":"))
  13706. (cond
  13707. ((not key)
  13708. (setq key
  13709. (if (= (char-after (match-beginning 3)) ?\[)
  13710. "TIMESTAMP_IA" "TIMESTAMP")))
  13711. ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
  13712. ((equal key1 org-deadline-string) (setq key "DEADLINE"))
  13713. ((equal key1 org-closed-string) (setq key "CLOSED"))
  13714. ((equal key1 org-clock-string) (setq key "CLOCK")))
  13715. (if (and specific (equal key specific) (not (equal key "CLOCK")))
  13716. (progn
  13717. (push (cons key string) props)
  13718. ;; no need to search further if match is found
  13719. (throw 'match t))
  13720. (when (or (equal key "CLOCK") (not (assoc key props)))
  13721. (push (cons key string) props)))))))
  13722. (when (memq which '(all standard))
  13723. ;; Get the standard properties, like :PROP: ...
  13724. (setq range (org-get-property-block beg end))
  13725. (when range
  13726. (goto-char (car range))
  13727. (while (re-search-forward
  13728. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  13729. (cdr range) t)
  13730. (setq key (org-match-string-no-properties 1)
  13731. value (org-trim (or (org-match-string-no-properties 2) "")))
  13732. (unless (member key excluded)
  13733. (push (cons key (or value "")) props)))))
  13734. (if clocksum
  13735. (push (cons "CLOCKSUM"
  13736. (org-columns-number-to-string (/ (float clocksum) 60.)
  13737. 'add_times))
  13738. props))
  13739. (if clocksumt
  13740. (push (cons "CLOCKSUM_T"
  13741. (org-columns-number-to-string (/ (float clocksumt) 60.)
  13742. 'add_times))
  13743. props))
  13744. (unless (assoc "CATEGORY" props)
  13745. (push (cons "CATEGORY" (org-get-category)) props))
  13746. (append sum-props (nreverse props)))))))
  13747. (defun org-entry-get (pom property &optional inherit literal-nil)
  13748. "Get value of PROPERTY for entry or content at point-or-marker POM.
  13749. If INHERIT is non-nil and the entry does not have the property,
  13750. then also check higher levels of the hierarchy.
  13751. If INHERIT is the symbol `selective', use inheritance only if the setting
  13752. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  13753. If the property is present but empty, the return value is the empty string.
  13754. If the property is not present at all, nil is returned.
  13755. If LITERAL-NIL is set, return the string value \"nil\" as a string,
  13756. do not interpret it as the list atom nil. This is used for inheritance
  13757. when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
  13758. (org-with-point-at pom
  13759. (if (and inherit (if (eq inherit 'selective)
  13760. (org-property-inherit-p property)
  13761. t))
  13762. (org-entry-get-with-inheritance property literal-nil)
  13763. (if (member property org-special-properties)
  13764. ;; We need a special property. Use `org-entry-properties' to
  13765. ;; retrieve it, but specify the wanted property
  13766. (cdr (assoc property (org-entry-properties nil 'special property)))
  13767. (let ((range (org-get-property-block)))
  13768. (when (and range (not (eq (car range) (cdr range))))
  13769. (let* ((props (list (or (assoc property org-file-properties)
  13770. (assoc property org-global-properties)
  13771. (assoc property org-global-properties-fixed))))
  13772. (ap (lambda (key)
  13773. (when (re-search-forward
  13774. (org-re-property key) (cdr range) t)
  13775. (setq props
  13776. (org-update-property-plist
  13777. key
  13778. (if (match-end 1)
  13779. (org-match-string-no-properties 1) "")
  13780. props)))))
  13781. val)
  13782. (goto-char (car range))
  13783. (funcall ap property)
  13784. (goto-char (car range))
  13785. (while (funcall ap (concat property "+")))
  13786. (setq val (cdr (assoc property props)))
  13787. (when val (if literal-nil val (org-not-nil val))))))))))
  13788. (defun org-property-or-variable-value (var &optional inherit)
  13789. "Check if there is a property fixing the value of VAR.
  13790. If yes, return this value. If not, return the current value of the variable."
  13791. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  13792. (if (and prop (stringp prop) (string-match "\\S-" prop))
  13793. (read prop)
  13794. (symbol-value var))))
  13795. (defun org-entry-delete (pom property &optional delete-empty-drawer)
  13796. "Delete the property PROPERTY from entry at point-or-marker POM.
  13797. When optional argument DELETE-EMPTY-DRAWER is a string, it defines
  13798. an empty drawer to delete."
  13799. (org-with-point-at pom
  13800. (if (member property org-special-properties)
  13801. nil ; cannot delete these properties.
  13802. (let ((range (org-get-property-block)))
  13803. (if (and range
  13804. (goto-char (car range))
  13805. (re-search-forward
  13806. (org-re-property property)
  13807. (cdr range) t))
  13808. (progn
  13809. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  13810. (and delete-empty-drawer
  13811. (org-remove-empty-drawer-at
  13812. delete-empty-drawer (car range)))
  13813. t)
  13814. nil)))))
  13815. ;; Multi-values properties are properties that contain multiple values
  13816. ;; These values are assumed to be single words, separated by whitespace.
  13817. (defun org-entry-add-to-multivalued-property (pom property value)
  13818. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  13819. (let* ((old (org-entry-get pom property))
  13820. (values (and old (org-split-string old "[ \t]"))))
  13821. (setq value (org-entry-protect-space value))
  13822. (unless (member value values)
  13823. (setq values (cons value values))
  13824. (org-entry-put pom property
  13825. (mapconcat 'identity values " ")))))
  13826. (defun org-entry-remove-from-multivalued-property (pom property value)
  13827. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  13828. (let* ((old (org-entry-get pom property))
  13829. (values (and old (org-split-string old "[ \t]"))))
  13830. (setq value (org-entry-protect-space value))
  13831. (when (member value values)
  13832. (setq values (delete value values))
  13833. (org-entry-put pom property
  13834. (mapconcat 'identity values " ")))))
  13835. (defun org-entry-member-in-multivalued-property (pom property value)
  13836. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  13837. (let* ((old (org-entry-get pom property))
  13838. (values (and old (org-split-string old "[ \t]"))))
  13839. (setq value (org-entry-protect-space value))
  13840. (member value values)))
  13841. (defun org-entry-get-multivalued-property (pom property)
  13842. "Return a list of values in a multivalued property."
  13843. (let* ((value (org-entry-get pom property))
  13844. (values (and value (org-split-string value "[ \t]"))))
  13845. (mapcar 'org-entry-restore-space values)))
  13846. (defun org-entry-put-multivalued-property (pom property &rest values)
  13847. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  13848. VALUES should be a list of strings. Spaces will be protected."
  13849. (org-entry-put pom property
  13850. (mapconcat 'org-entry-protect-space values " "))
  13851. (let* ((value (org-entry-get pom property))
  13852. (values (and value (org-split-string value "[ \t]"))))
  13853. (mapcar 'org-entry-restore-space values)))
  13854. (defun org-entry-protect-space (s)
  13855. "Protect spaces and newline in string S."
  13856. (while (string-match " " s)
  13857. (setq s (replace-match "%20" t t s)))
  13858. (while (string-match "\n" s)
  13859. (setq s (replace-match "%0A" t t s)))
  13860. s)
  13861. (defun org-entry-restore-space (s)
  13862. "Restore spaces and newline in string S."
  13863. (while (string-match "%20" s)
  13864. (setq s (replace-match " " t t s)))
  13865. (while (string-match "%0A" s)
  13866. (setq s (replace-match "\n" t t s)))
  13867. s)
  13868. (defvar org-entry-property-inherited-from (make-marker)
  13869. "Marker pointing to the entry from where a property was inherited.
  13870. Each call to `org-entry-get-with-inheritance' will set this marker to the
  13871. location of the entry where the inheritance search matched. If there was
  13872. no match, the marker will point nowhere.
  13873. Note that also `org-entry-get' calls this function, if the INHERIT flag
  13874. is set.")
  13875. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  13876. "Get PROPERTY of entry or content at point, search higher levels if needed.
  13877. The search will stop at the first ancestor which has the property defined.
  13878. If the value found is \"nil\", return nil to show that the property
  13879. should be considered as undefined (this is the meaning of nil here).
  13880. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  13881. (move-marker org-entry-property-inherited-from nil)
  13882. (let (tmp)
  13883. (save-excursion
  13884. (save-restriction
  13885. (widen)
  13886. (catch 'ex
  13887. (while t
  13888. (when (setq tmp (org-entry-get nil property nil 'literal-nil))
  13889. (or (ignore-errors (org-back-to-heading t))
  13890. (goto-char (point-min)))
  13891. (move-marker org-entry-property-inherited-from (point))
  13892. (throw 'ex tmp))
  13893. (or (ignore-errors (org-up-heading-safe))
  13894. (throw 'ex nil))))))
  13895. (setq tmp (or tmp
  13896. (cdr (assoc property org-file-properties))
  13897. (cdr (assoc property org-global-properties))
  13898. (cdr (assoc property org-global-properties-fixed))))
  13899. (if literal-nil tmp (org-not-nil tmp))))
  13900. (defvar org-property-changed-functions nil
  13901. "Hook called when the value of a property has changed.
  13902. Each hook function should accept two arguments, the name of the property
  13903. and the new value.")
  13904. (defun org-entry-put (pom property value)
  13905. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  13906. (org-with-point-at pom
  13907. (org-back-to-heading t)
  13908. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  13909. range)
  13910. (cond
  13911. ((equal property "TODO")
  13912. (when (and (stringp value) (string-match "\\S-" value)
  13913. (not (member value org-todo-keywords-1)))
  13914. (error "\"%s\" is not a valid TODO state" value))
  13915. (if (or (not value)
  13916. (not (string-match "\\S-" value)))
  13917. (setq value 'none))
  13918. (org-todo value)
  13919. (org-set-tags nil 'align))
  13920. ((equal property "PRIORITY")
  13921. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  13922. (string-to-char value) ?\ ))
  13923. (org-set-tags nil 'align))
  13924. ((equal property "CLOCKSUM")
  13925. (if (not (re-search-forward
  13926. (concat org-clock-string ".*\\]--\\(\\[[^]]+\\]\\)") nil t))
  13927. (error "Cannot find a clock log")
  13928. (goto-char (- (match-end 1) 2))
  13929. (cond
  13930. ((eq value 'earlier) (org-timestamp-down))
  13931. ((eq value 'later) (org-timestamp-up)))
  13932. (org-clock-sum-current-item)))
  13933. ((equal property "SCHEDULED")
  13934. (if (re-search-forward org-scheduled-time-regexp end t)
  13935. (cond
  13936. ((eq value 'earlier) (org-timestamp-change -1 'day))
  13937. ((eq value 'later) (org-timestamp-change 1 'day))
  13938. (t (call-interactively 'org-schedule)))
  13939. (call-interactively 'org-schedule)))
  13940. ((equal property "DEADLINE")
  13941. (if (re-search-forward org-deadline-time-regexp end t)
  13942. (cond
  13943. ((eq value 'earlier) (org-timestamp-change -1 'day))
  13944. ((eq value 'later) (org-timestamp-change 1 'day))
  13945. (t (call-interactively 'org-deadline)))
  13946. (call-interactively 'org-deadline)))
  13947. ((member property org-special-properties)
  13948. (error "The %s property can not yet be set with `org-entry-put'"
  13949. property))
  13950. (t ; a non-special property
  13951. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  13952. (setq range (org-get-property-block beg end 'force))
  13953. (goto-char (car range))
  13954. (if (re-search-forward
  13955. (org-re-property-keyword property) (cdr range) t)
  13956. (progn
  13957. (delete-region (match-beginning 0) (match-end 0))
  13958. (goto-char (match-beginning 0)))
  13959. (goto-char (cdr range))
  13960. (insert "\n")
  13961. (backward-char 1)
  13962. (org-indent-line))
  13963. (insert ":" property ":")
  13964. (and value (insert " " value))
  13965. (org-indent-line)))))
  13966. (run-hook-with-args 'org-property-changed-functions property value)))
  13967. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  13968. "Get all property keys in the current buffer.
  13969. With INCLUDE-SPECIALS, also list the special properties that reflect things
  13970. like tags and TODO state.
  13971. With INCLUDE-DEFAULTS, also include properties that has special meaning
  13972. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING
  13973. and others.
  13974. With INCLUDE-COLUMNS, also include property names given in COLUMN
  13975. formats in the current buffer."
  13976. (let (rtn range cfmt s p)
  13977. (save-excursion
  13978. (save-restriction
  13979. (widen)
  13980. (goto-char (point-min))
  13981. (while (re-search-forward org-property-start-re nil t)
  13982. (setq range (org-get-property-block))
  13983. (goto-char (car range))
  13984. (while (re-search-forward
  13985. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  13986. (cdr range) t)
  13987. (add-to-list 'rtn (org-match-string-no-properties 1)))
  13988. (outline-next-heading))))
  13989. (when include-specials
  13990. (setq rtn (append org-special-properties rtn)))
  13991. (when include-defaults
  13992. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  13993. (add-to-list 'rtn org-effort-property))
  13994. (when include-columns
  13995. (save-excursion
  13996. (save-restriction
  13997. (widen)
  13998. (goto-char (point-min))
  13999. (while (re-search-forward
  14000. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  14001. nil t)
  14002. (setq cfmt (match-string 2) s 0)
  14003. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  14004. cfmt s)
  14005. (setq s (match-end 0)
  14006. p (match-string 1 cfmt))
  14007. (unless (or (equal p "ITEM")
  14008. (member p org-special-properties))
  14009. (add-to-list 'rtn (match-string 1 cfmt))))))))
  14010. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  14011. (defun org-property-values (key)
  14012. "Return a list of all values of property KEY in the current buffer."
  14013. (save-excursion
  14014. (save-restriction
  14015. (widen)
  14016. (goto-char (point-min))
  14017. (let ((re (org-re-property key))
  14018. values)
  14019. (while (re-search-forward re nil t)
  14020. (add-to-list 'values (org-trim (match-string 1))))
  14021. (delete "" values)))))
  14022. (defun org-insert-property-drawer ()
  14023. "Insert a property drawer into the current entry."
  14024. (org-back-to-heading t)
  14025. (looking-at org-outline-regexp)
  14026. (let ((indent (if org-adapt-indentation
  14027. (- (match-end 0) (match-beginning 0))
  14028. 0))
  14029. (beg (point))
  14030. (re (concat "^[ \t]*" org-keyword-time-regexp))
  14031. end hiddenp)
  14032. (outline-next-heading)
  14033. (setq end (point))
  14034. (goto-char beg)
  14035. (while (re-search-forward re end t))
  14036. (setq hiddenp (outline-invisible-p))
  14037. (end-of-line 1)
  14038. (and (equal (char-after) ?\n) (forward-char 1))
  14039. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  14040. (if (member (match-string 1) '("CLOCK:" ":END:"))
  14041. ;; just skip this line
  14042. (beginning-of-line 2)
  14043. ;; Drawer start, find the end
  14044. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  14045. (beginning-of-line 1)))
  14046. (org-skip-over-state-notes)
  14047. (skip-chars-backward " \t\n\r")
  14048. (if (eq (char-before) ?*) (forward-char 1))
  14049. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  14050. (beginning-of-line 0)
  14051. (org-indent-to-column indent)
  14052. (beginning-of-line 2)
  14053. (org-indent-to-column indent)
  14054. (beginning-of-line 0)
  14055. (if hiddenp
  14056. (save-excursion
  14057. (org-back-to-heading t)
  14058. (hide-entry))
  14059. (org-flag-drawer t))))
  14060. (defun org-insert-drawer (&optional arg drawer)
  14061. "Insert a drawer at point.
  14062. Optional argument DRAWER, when non-nil, is a string representing
  14063. drawer's name. Otherwise, the user is prompted for a name.
  14064. If a region is active, insert the drawer around that region
  14065. instead.
  14066. Point is left between drawer's boundaries."
  14067. (interactive "P")
  14068. (let* ((logbook (if (stringp org-log-into-drawer) org-log-into-drawer
  14069. "LOGBOOK"))
  14070. ;; SYSTEM-DRAWERS is a list of drawer names that are used
  14071. ;; internally by Org. They are meant to be inserted
  14072. ;; automatically.
  14073. (system-drawers `("CLOCK" ,logbook "PROPERTIES"))
  14074. ;; Remove system drawers from list. Note: For some reason,
  14075. ;; `org-completing-read' ignores the predicate while
  14076. ;; `completing-read' handles it fine.
  14077. (drawer (if arg "PROPERTIES"
  14078. (or drawer
  14079. (completing-read
  14080. "Drawer: " org-drawers
  14081. (lambda (d) (not (member d system-drawers))))))))
  14082. (cond
  14083. ;; With C-u, fall back on `org-insert-property-drawer'
  14084. (arg (org-insert-property-drawer))
  14085. ;; With an active region, insert a drawer at point.
  14086. ((not (org-region-active-p))
  14087. (progn
  14088. (unless (bolp) (insert "\n"))
  14089. (insert (format ":%s:\n\n:END:\n" drawer))
  14090. (forward-line -2)))
  14091. ;; Otherwise, insert the drawer at point
  14092. (t
  14093. (let ((rbeg (region-beginning))
  14094. (rend (copy-marker (region-end))))
  14095. (unwind-protect
  14096. (progn
  14097. (goto-char rbeg)
  14098. (beginning-of-line)
  14099. (when (save-excursion
  14100. (re-search-forward org-outline-regexp-bol rend t))
  14101. (error "Drawers cannot contain headlines"))
  14102. ;; Position point at the beginning of the first
  14103. ;; non-blank line in region. Insert drawer's opening
  14104. ;; there, then indent it.
  14105. (org-skip-whitespace)
  14106. (beginning-of-line)
  14107. (insert ":" drawer ":\n")
  14108. (forward-line -1)
  14109. (indent-for-tab-command)
  14110. ;; Move point to the beginning of the first blank line
  14111. ;; after the last non-blank line in region. Insert
  14112. ;; drawer's closing, then indent it.
  14113. (goto-char rend)
  14114. (skip-chars-backward " \r\t\n")
  14115. (insert "\n:END:")
  14116. (deactivate-mark t)
  14117. (indent-for-tab-command)
  14118. (unless (eolp) (insert "\n")))
  14119. ;; Clear marker, whatever the outcome of insertion is.
  14120. (set-marker rend nil)))))))
  14121. (defvar org-property-set-functions-alist nil
  14122. "Property set function alist.
  14123. Each entry should have the following format:
  14124. (PROPERTY . READ-FUNCTION)
  14125. The read function will be called with the same argument as
  14126. `org-completing-read'.")
  14127. (defun org-set-property-function (property)
  14128. "Get the function that should be used to set PROPERTY.
  14129. This is computed according to `org-property-set-functions-alist'."
  14130. (or (cdr (assoc property org-property-set-functions-alist))
  14131. 'org-completing-read))
  14132. (defun org-read-property-value (property)
  14133. "Read PROPERTY value from user."
  14134. (let* ((completion-ignore-case t)
  14135. (allowed (org-property-get-allowed-values nil property 'table))
  14136. (cur (org-entry-get nil property))
  14137. (prompt (concat property " value"
  14138. (if (and cur (string-match "\\S-" cur))
  14139. (concat " [" cur "]") "") ": "))
  14140. (set-function (org-set-property-function property))
  14141. (val (if allowed
  14142. (funcall set-function prompt allowed nil
  14143. (not (get-text-property 0 'org-unrestricted
  14144. (caar allowed))))
  14145. (let (org-completion-use-ido org-completion-use-iswitchb)
  14146. (funcall set-function prompt
  14147. (mapcar 'list (org-property-values property))
  14148. nil nil "" nil cur)))))
  14149. (if (equal val "")
  14150. cur
  14151. val)))
  14152. (defvar org-last-set-property nil)
  14153. (defvar org-last-set-property-value nil)
  14154. (defun org-read-property-name ()
  14155. "Read a property name."
  14156. (let* ((completion-ignore-case t)
  14157. (keys (org-buffer-property-keys nil t t))
  14158. (default-prop (or (save-excursion
  14159. (save-match-data
  14160. (beginning-of-line)
  14161. (and (looking-at "^\\s-*:\\([^:\n]+\\):")
  14162. (null (string= (match-string 1) "END"))
  14163. (match-string 1))))
  14164. org-last-set-property))
  14165. (property (org-icompleting-read
  14166. (concat "Property"
  14167. (if default-prop (concat " [" default-prop "]") "")
  14168. ": ")
  14169. (mapcar 'list keys)
  14170. nil nil nil nil
  14171. default-prop)))
  14172. (if (member property keys)
  14173. property
  14174. (or (cdr (assoc (downcase property)
  14175. (mapcar (lambda (x) (cons (downcase x) x))
  14176. keys)))
  14177. property))))
  14178. (defun org-set-property-and-value (use-last)
  14179. "Allow to set [PROPERTY]: [value] direction from prompt.
  14180. When use-default, don't even ask, just use the last
  14181. \"[PROPERTY]: [value]\" string from the history."
  14182. (interactive "P")
  14183. (let* ((completion-ignore-case t)
  14184. (pv (or (and use-last org-last-set-property-value)
  14185. (org-completing-read
  14186. "Enter a \"[Property]: [value]\" pair: "
  14187. nil nil nil nil nil
  14188. org-last-set-property-value)))
  14189. prop val)
  14190. (when (string-match "^[ \t]*\\([^:]+\\):[ \t]*\\(.*\\)[ \t]*$" pv)
  14191. (setq prop (match-string 1 pv)
  14192. val (match-string 2 pv))
  14193. (org-set-property prop val))))
  14194. (defun org-set-property (property value)
  14195. "In the current entry, set PROPERTY to VALUE.
  14196. When called interactively, this will prompt for a property name, offering
  14197. completion on existing and default properties. And then it will prompt
  14198. for a value, offering completion either on allowed values (via an inherited
  14199. xxx_ALL property) or on existing values in other instances of this property
  14200. in the current file."
  14201. (interactive (list nil nil))
  14202. (let* ((property (or property (org-read-property-name)))
  14203. (value (or value (org-read-property-value property)))
  14204. (fn (cdr (assoc property org-properties-postprocess-alist))))
  14205. (setq org-last-set-property property)
  14206. (setq org-last-set-property-value (concat property ": " value))
  14207. ;; Possibly postprocess the inserted value:
  14208. (when fn (setq value (funcall fn value)))
  14209. (unless (equal (org-entry-get nil property) value)
  14210. (org-entry-put nil property value))))
  14211. (defun org-delete-property (property &optional delete-empty-drawer)
  14212. "In the current entry, delete PROPERTY.
  14213. When optional argument DELETE-EMPTY-DRAWER is a string, it defines
  14214. an empty drawer to delete."
  14215. (interactive
  14216. (let* ((completion-ignore-case t)
  14217. (prop (org-icompleting-read "Property: "
  14218. (org-entry-properties nil 'standard))))
  14219. (list prop)))
  14220. (message "Property %s %s" property
  14221. (if (org-entry-delete nil property delete-empty-drawer)
  14222. "deleted"
  14223. "was not present in the entry")))
  14224. (defun org-delete-property-globally (property)
  14225. "Remove PROPERTY globally, from all entries."
  14226. (interactive
  14227. (let* ((completion-ignore-case t)
  14228. (prop (org-icompleting-read
  14229. "Globally remove property: "
  14230. (mapcar 'list (org-buffer-property-keys)))))
  14231. (list prop)))
  14232. (save-excursion
  14233. (save-restriction
  14234. (widen)
  14235. (goto-char (point-min))
  14236. (let ((cnt 0))
  14237. (while (re-search-forward
  14238. (org-re-property property)
  14239. nil t)
  14240. (setq cnt (1+ cnt))
  14241. (delete-region (match-beginning 0) (1+ (point-at-eol))))
  14242. (message "Property \"%s\" removed from %d entries" property cnt)))))
  14243. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  14244. (defun org-compute-property-at-point ()
  14245. "Compute the property at point.
  14246. This looks for an enclosing column format, extracts the operator and
  14247. then applies it to the property in the column format's scope."
  14248. (interactive)
  14249. (unless (org-at-property-p)
  14250. (error "Not at a property"))
  14251. (let ((prop (org-match-string-no-properties 2)))
  14252. (org-columns-get-format-and-top-level)
  14253. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  14254. (error "No operator defined for property %s" prop))
  14255. (org-columns-compute prop)))
  14256. (defvar org-property-allowed-value-functions nil
  14257. "Hook for functions supplying allowed values for a specific property.
  14258. The functions must take a single argument, the name of the property, and
  14259. return a flat list of allowed values. If \":ETC\" is one of
  14260. the values, this means that these values are intended as defaults for
  14261. completion, but that other values should be allowed too.
  14262. The functions must return nil if they are not responsible for this
  14263. property.")
  14264. (defun org-property-get-allowed-values (pom property &optional table)
  14265. "Get allowed values for the property PROPERTY.
  14266. When TABLE is non-nil, return an alist that can directly be used for
  14267. completion."
  14268. (let (vals)
  14269. (cond
  14270. ((equal property "TODO")
  14271. (setq vals (org-with-point-at pom
  14272. (append org-todo-keywords-1 '("")))))
  14273. ((equal property "PRIORITY")
  14274. (let ((n org-lowest-priority))
  14275. (while (>= n org-highest-priority)
  14276. (push (char-to-string n) vals)
  14277. (setq n (1- n)))))
  14278. ((member property org-special-properties))
  14279. ((setq vals (run-hook-with-args-until-success
  14280. 'org-property-allowed-value-functions property)))
  14281. (t
  14282. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  14283. (when (and vals (string-match "\\S-" vals))
  14284. (setq vals (car (read-from-string (concat "(" vals ")"))))
  14285. (setq vals (mapcar (lambda (x)
  14286. (cond ((stringp x) x)
  14287. ((numberp x) (number-to-string x))
  14288. ((symbolp x) (symbol-name x))
  14289. (t "???")))
  14290. vals)))))
  14291. (when (member ":ETC" vals)
  14292. (setq vals (remove ":ETC" vals))
  14293. (org-add-props (car vals) '(org-unrestricted t)))
  14294. (if table (mapcar 'list vals) vals)))
  14295. (defun org-property-previous-allowed-value (&optional previous)
  14296. "Switch to the next allowed value for this property."
  14297. (interactive)
  14298. (org-property-next-allowed-value t))
  14299. (defun org-property-next-allowed-value (&optional previous)
  14300. "Switch to the next allowed value for this property."
  14301. (interactive)
  14302. (unless (org-at-property-p)
  14303. (error "Not at a property"))
  14304. (let* ((prop (car (save-match-data (org-split-string (match-string 1) ":"))))
  14305. (key (match-string 2))
  14306. (value (match-string 3))
  14307. (allowed (or (org-property-get-allowed-values (point) key)
  14308. (and (member value '("[ ]" "[-]" "[X]"))
  14309. '("[ ]" "[X]"))))
  14310. (heading (save-match-data (nth 4 (org-heading-components))))
  14311. nval)
  14312. (unless allowed
  14313. (error "Allowed values for this property have not been defined"))
  14314. (if previous (setq allowed (reverse allowed)))
  14315. (if (member value allowed)
  14316. (setq nval (car (cdr (member value allowed)))))
  14317. (setq nval (or nval (car allowed)))
  14318. (if (equal nval value)
  14319. (error "Only one allowed value for this property"))
  14320. (org-at-property-p)
  14321. (replace-match (concat " :" key ": " nval) t t)
  14322. (org-indent-line)
  14323. (beginning-of-line 1)
  14324. (skip-chars-forward " \t")
  14325. (when (equal prop org-effort-property)
  14326. (save-excursion
  14327. (org-back-to-heading t)
  14328. (put-text-property (point-at-bol) (point-at-eol) 'org-effort nval))
  14329. (when (string= org-clock-current-task heading)
  14330. (setq org-clock-effort nval)
  14331. (org-clock-update-mode-line)))
  14332. (run-hook-with-args 'org-property-changed-functions key nval)))
  14333. (defun org-find-olp (path &optional this-buffer)
  14334. "Return a marker pointing to the entry at outline path OLP.
  14335. If anything goes wrong, throw an error.
  14336. You can wrap this call to catch the error like this:
  14337. (condition-case msg
  14338. (org-mobile-locate-entry (match-string 4))
  14339. (error (nth 1 msg)))
  14340. The return value will then be either a string with the error message,
  14341. or a marker if everything is OK.
  14342. If THIS-BUFFER is set, the outline path does not contain a file,
  14343. only headings."
  14344. (let* ((file (if this-buffer buffer-file-name (pop path)))
  14345. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  14346. (level 1)
  14347. (lmin 1)
  14348. (lmax 1)
  14349. limit re end found pos heading cnt flevel)
  14350. (unless buffer (error "File not found :%s" file))
  14351. (with-current-buffer buffer
  14352. (save-excursion
  14353. (save-restriction
  14354. (widen)
  14355. (setq limit (point-max))
  14356. (goto-char (point-min))
  14357. (while (setq heading (pop path))
  14358. (setq re (format org-complex-heading-regexp-format
  14359. (regexp-quote heading)))
  14360. (setq cnt 0 pos (point))
  14361. (while (re-search-forward re end t)
  14362. (setq level (- (match-end 1) (match-beginning 1)))
  14363. (if (and (>= level lmin) (<= level lmax))
  14364. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  14365. (when (= cnt 0) (error "Heading not found on level %d: %s"
  14366. lmax heading))
  14367. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  14368. lmax heading))
  14369. (goto-char found)
  14370. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  14371. (setq end (save-excursion (org-end-of-subtree t t))))
  14372. (when (org-at-heading-p)
  14373. (point-marker)))))))
  14374. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  14375. "Find node HEADING in BUFFER.
  14376. Return a marker to the heading if it was found, or nil if not.
  14377. If POS-ONLY is set, return just the position instead of a marker.
  14378. The heading text must match exact, but it may have a TODO keyword,
  14379. a priority cookie and tags in the standard locations."
  14380. (with-current-buffer (or buffer (current-buffer))
  14381. (save-excursion
  14382. (save-restriction
  14383. (widen)
  14384. (goto-char (point-min))
  14385. (let (case-fold-search)
  14386. (if (re-search-forward
  14387. (format org-complex-heading-regexp-format
  14388. (regexp-quote heading)) nil t)
  14389. (if pos-only
  14390. (match-beginning 0)
  14391. (move-marker (make-marker) (match-beginning 0)))))))))
  14392. (defun org-find-exact-heading-in-directory (heading &optional dir)
  14393. "Find Org node headline HEADING in all .org files in directory DIR.
  14394. When the target headline is found, return a marker to this location."
  14395. (let ((files (directory-files (or dir default-directory)
  14396. nil "\\`[^.#].*\\.org\\'"))
  14397. file visiting m buffer)
  14398. (catch 'found
  14399. (while (setq file (pop files))
  14400. (message "trying %s" file)
  14401. (setq visiting (org-find-base-buffer-visiting file))
  14402. (setq buffer (or visiting (find-file-noselect file)))
  14403. (setq m (org-find-exact-headline-in-buffer
  14404. heading buffer))
  14405. (when (and (not m) (not visiting)) (kill-buffer buffer))
  14406. (and m (throw 'found m))))))
  14407. (defun org-find-entry-with-id (ident)
  14408. "Locate the entry that contains the ID property with exact value IDENT.
  14409. IDENT can be a string, a symbol or a number, this function will search for
  14410. the string representation of it.
  14411. Return the position where this entry starts, or nil if there is no such entry."
  14412. (interactive "sID: ")
  14413. (let ((id (cond
  14414. ((stringp ident) ident)
  14415. ((symbol-name ident) (symbol-name ident))
  14416. ((numberp ident) (number-to-string ident))
  14417. (t (error "IDENT %s must be a string, symbol or number" ident))))
  14418. (case-fold-search nil))
  14419. (save-excursion
  14420. (save-restriction
  14421. (widen)
  14422. (goto-char (point-min))
  14423. (when (re-search-forward
  14424. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  14425. nil t)
  14426. (org-back-to-heading t)
  14427. (point))))))
  14428. ;;;; Timestamps
  14429. (defvar org-last-changed-timestamp nil)
  14430. (defvar org-last-inserted-timestamp nil
  14431. "The last time stamp inserted with `org-insert-time-stamp'.")
  14432. (defvar org-time-was-given) ; dynamically scoped parameter
  14433. (defvar org-end-time-was-given) ; dynamically scoped parameter
  14434. (defvar org-ts-what) ; dynamically scoped parameter
  14435. (defun org-time-stamp (arg &optional inactive)
  14436. "Prompt for a date/time and insert a time stamp.
  14437. If the user specifies a time like HH:MM or if this command is
  14438. called with at least one prefix argument, the time stamp contains
  14439. the date and the time. Otherwise, only the date is be included.
  14440. All parts of a date not specified by the user is filled in from
  14441. the current date/time. So if you just press return without
  14442. typing anything, the time stamp will represent the current
  14443. date/time.
  14444. If there is already a timestamp at the cursor, it will be
  14445. modified.
  14446. With two universal prefix arguments, insert an active timestamp
  14447. with the current time without prompting the user."
  14448. (interactive "P")
  14449. (let* ((ts nil)
  14450. (default-time
  14451. ;; Default time is either today, or, when entering a range,
  14452. ;; the range start.
  14453. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  14454. (save-excursion
  14455. (re-search-backward
  14456. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  14457. (- (point) 20) t)))
  14458. (apply 'encode-time (org-parse-time-string (match-string 1)))
  14459. (current-time)))
  14460. (default-input (and ts (org-get-compact-tod ts)))
  14461. (repeater (save-excursion
  14462. (save-match-data
  14463. (beginning-of-line)
  14464. (when (re-search-forward
  14465. "\\([.+-]+[0-9]+[hdwmy] ?\\)+" ;;\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  14466. (save-excursion (progn (end-of-line) (point))) t)
  14467. (match-string 0)))))
  14468. org-time-was-given org-end-time-was-given time)
  14469. (cond
  14470. ((and (org-at-timestamp-p t)
  14471. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  14472. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  14473. (insert "--")
  14474. (setq time (let ((this-command this-command))
  14475. (org-read-date arg 'totime nil nil
  14476. default-time default-input inactive)))
  14477. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  14478. ((org-at-timestamp-p t)
  14479. (setq time (let ((this-command this-command))
  14480. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  14481. (when (org-at-timestamp-p t) ; just to get the match data
  14482. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  14483. (replace-match "")
  14484. (setq org-last-changed-timestamp
  14485. (org-insert-time-stamp
  14486. time (or org-time-was-given arg)
  14487. inactive nil nil (list org-end-time-was-given)))
  14488. (when repeater (goto-char (1- (point))) (insert " " repeater)
  14489. (setq org-last-changed-timestamp
  14490. (concat (substring org-last-inserted-timestamp 0 -1)
  14491. " " repeater ">"))))
  14492. (message "Timestamp updated"))
  14493. ((equal arg '(16))
  14494. (org-insert-time-stamp (current-time) t))
  14495. (t
  14496. (setq time (let ((this-command this-command))
  14497. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  14498. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  14499. nil nil (list org-end-time-was-given))))))
  14500. ;; FIXME: can we use this for something else, like computing time differences?
  14501. (defun org-get-compact-tod (s)
  14502. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  14503. (let* ((t1 (match-string 1 s))
  14504. (h1 (string-to-number (match-string 2 s)))
  14505. (m1 (string-to-number (match-string 3 s)))
  14506. (t2 (and (match-end 4) (match-string 5 s)))
  14507. (h2 (and t2 (string-to-number (match-string 6 s))))
  14508. (m2 (and t2 (string-to-number (match-string 7 s))))
  14509. dh dm)
  14510. (if (not t2)
  14511. t1
  14512. (setq dh (- h2 h1) dm (- m2 m1))
  14513. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  14514. (concat t1 "+" (number-to-string dh)
  14515. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  14516. (defun org-time-stamp-inactive (&optional arg)
  14517. "Insert an inactive time stamp.
  14518. An inactive time stamp is enclosed in square brackets instead of angle
  14519. brackets. It is inactive in the sense that it does not trigger agenda entries,
  14520. does not link to the calendar and cannot be changed with the S-cursor keys.
  14521. So these are more for recording a certain time/date."
  14522. (interactive "P")
  14523. (org-time-stamp arg 'inactive))
  14524. (defvar org-date-ovl (make-overlay 1 1))
  14525. (overlay-put org-date-ovl 'face 'org-date-selected)
  14526. (org-detach-overlay org-date-ovl)
  14527. (defvar org-ans1) ; dynamically scoped parameter
  14528. (defvar org-ans2) ; dynamically scoped parameter
  14529. (defvar org-plain-time-of-day-regexp) ; defined below
  14530. (defvar org-overriding-default-time nil) ; dynamically scoped
  14531. (defvar org-read-date-overlay nil)
  14532. (defvar org-dcst nil) ; dynamically scoped
  14533. (defvar org-read-date-history nil)
  14534. (defvar org-read-date-final-answer nil)
  14535. (defvar org-read-date-analyze-futurep nil)
  14536. (defvar org-read-date-analyze-forced-year nil)
  14537. (defvar org-read-date-inactive)
  14538. (defvar org-read-date-minibuffer-local-map
  14539. (let ((map (make-sparse-keymap)))
  14540. (set-keymap-parent map minibuffer-local-map)
  14541. (org-defkey map (kbd ".")
  14542. (lambda () (interactive)
  14543. (org-eval-in-calendar '(calendar-goto-today))))
  14544. (org-defkey map [(meta shift left)]
  14545. (lambda () (interactive)
  14546. (org-eval-in-calendar '(calendar-backward-month 1))))
  14547. (org-defkey map [(meta shift right)]
  14548. (lambda () (interactive)
  14549. (org-eval-in-calendar '(calendar-forward-month 1))))
  14550. (org-defkey map [(meta shift up)]
  14551. (lambda () (interactive)
  14552. (org-eval-in-calendar '(calendar-backward-year 1))))
  14553. (org-defkey map [(meta shift down)]
  14554. (lambda () (interactive)
  14555. (org-eval-in-calendar '(calendar-forward-year 1))))
  14556. (org-defkey map [?\e (shift left)]
  14557. (lambda () (interactive)
  14558. (org-eval-in-calendar '(calendar-backward-month 1))))
  14559. (org-defkey map [?\e (shift right)]
  14560. (lambda () (interactive)
  14561. (org-eval-in-calendar '(calendar-forward-month 1))))
  14562. (org-defkey map [?\e (shift up)]
  14563. (lambda () (interactive)
  14564. (org-eval-in-calendar '(calendar-backward-year 1))))
  14565. (org-defkey map [?\e (shift down)]
  14566. (lambda () (interactive)
  14567. (org-eval-in-calendar '(calendar-forward-year 1))))
  14568. (org-defkey map [(shift up)]
  14569. (lambda () (interactive)
  14570. (org-eval-in-calendar '(calendar-backward-week 1))))
  14571. (org-defkey map [(shift down)]
  14572. (lambda () (interactive)
  14573. (org-eval-in-calendar '(calendar-forward-week 1))))
  14574. (org-defkey map [(shift left)]
  14575. (lambda () (interactive)
  14576. (org-eval-in-calendar '(calendar-backward-day 1))))
  14577. (org-defkey map [(shift right)]
  14578. (lambda () (interactive)
  14579. (org-eval-in-calendar '(calendar-forward-day 1))))
  14580. (org-defkey map "!"
  14581. (lambda () (interactive)
  14582. (org-eval-in-calendar '(diary-view-entries))
  14583. (message "")))
  14584. (org-defkey map ">"
  14585. (lambda () (interactive)
  14586. (org-eval-in-calendar '(scroll-calendar-left 1))))
  14587. (org-defkey map "<"
  14588. (lambda () (interactive)
  14589. (org-eval-in-calendar '(scroll-calendar-right 1))))
  14590. (org-defkey map "\C-v"
  14591. (lambda () (interactive)
  14592. (org-eval-in-calendar
  14593. '(calendar-scroll-left-three-months 1))))
  14594. (org-defkey map "\M-v"
  14595. (lambda () (interactive)
  14596. (org-eval-in-calendar
  14597. '(calendar-scroll-right-three-months 1))))
  14598. map)
  14599. "Keymap for minibuffer commands when using `org-read-date'.")
  14600. (defun org-read-date (&optional org-with-time to-time from-string prompt
  14601. default-time default-input inactive)
  14602. "Read a date, possibly a time, and make things smooth for the user.
  14603. The prompt will suggest to enter an ISO date, but you can also enter anything
  14604. which will at least partially be understood by `parse-time-string'.
  14605. Unrecognized parts of the date will default to the current day, month, year,
  14606. hour and minute. If this command is called to replace a timestamp at point,
  14607. or to enter the second timestamp of a range, the default time is taken
  14608. from the existing stamp. Furthermore, the command prefers the future,
  14609. so if you are giving a date where the year is not given, and the day-month
  14610. combination is already past in the current year, it will assume you
  14611. mean next year. For details, see the manual. A few examples:
  14612. 3-2-5 --> 2003-02-05
  14613. feb 15 --> currentyear-02-15
  14614. 2/15 --> currentyear-02-15
  14615. sep 12 9 --> 2009-09-12
  14616. 12:45 --> today 12:45
  14617. 22 sept 0:34 --> currentyear-09-22 0:34
  14618. 12 --> currentyear-currentmonth-12
  14619. Fri --> nearest Friday (today or later)
  14620. etc.
  14621. Furthermore you can specify a relative date by giving, as the *first* thing
  14622. in the input: a plus/minus sign, a number and a letter [hdwmy] to indicate
  14623. change in days weeks, months, years.
  14624. With a single plus or minus, the date is relative to today. With a double
  14625. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  14626. +4d --> four days from today
  14627. +4 --> same as above
  14628. +2w --> two weeks from today
  14629. ++5 --> five days from default date
  14630. The function understands only English month and weekday abbreviations.
  14631. While prompting, a calendar is popped up - you can also select the
  14632. date with the mouse (button 1). The calendar shows a period of three
  14633. months. To scroll it to other months, use the keys `>' and `<'.
  14634. If you don't like the calendar, turn it off with
  14635. \(setq org-read-date-popup-calendar nil)
  14636. With optional argument TO-TIME, the date will immediately be converted
  14637. to an internal time.
  14638. With an optional argument ORG-WITH-TIME, the prompt will suggest to
  14639. also insert a time. Note that when ORG-WITH-TIME is not set, you can
  14640. still enter a time, and this function will inform the calling routine
  14641. about this change. The calling routine may then choose to change the
  14642. format used to insert the time stamp into the buffer to include the time.
  14643. With optional argument FROM-STRING, read from this string instead from
  14644. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  14645. the time/date that is used for everything that is not specified by the
  14646. user."
  14647. (require 'parse-time)
  14648. (let* ((org-time-stamp-rounding-minutes
  14649. (if (equal org-with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  14650. (org-dcst org-display-custom-times)
  14651. (ct (org-current-time))
  14652. (org-def (or org-overriding-default-time default-time ct))
  14653. (org-defdecode (decode-time org-def))
  14654. (dummy (progn
  14655. (when (< (nth 2 org-defdecode) org-extend-today-until)
  14656. (setcar (nthcdr 2 org-defdecode) -1)
  14657. (setcar (nthcdr 1 org-defdecode) 59)
  14658. (setq org-def (apply 'encode-time org-defdecode)
  14659. org-defdecode (decode-time org-def)))))
  14660. (mouse-autoselect-window nil) ; Don't let the mouse jump
  14661. (calendar-frame-setup nil)
  14662. (calendar-setup nil)
  14663. (calendar-move-hook nil)
  14664. (calendar-view-diary-initially-flag nil)
  14665. (calendar-view-holidays-initially-flag nil)
  14666. (timestr (format-time-string
  14667. (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") org-def))
  14668. (prompt (concat (if prompt (concat prompt " ") "")
  14669. (format "Date+time [%s]: " timestr)))
  14670. ans (org-ans0 "") org-ans1 org-ans2 final)
  14671. (cond
  14672. (from-string (setq ans from-string))
  14673. (org-read-date-popup-calendar
  14674. (save-excursion
  14675. (save-window-excursion
  14676. (calendar)
  14677. (org-eval-in-calendar '(setq cursor-type nil) t)
  14678. (unwind-protect
  14679. (progn
  14680. (calendar-forward-day (- (time-to-days org-def)
  14681. (calendar-absolute-from-gregorian
  14682. (calendar-current-date))))
  14683. (org-eval-in-calendar nil t)
  14684. (let* ((old-map (current-local-map))
  14685. (map (copy-keymap calendar-mode-map))
  14686. (minibuffer-local-map
  14687. (copy-keymap org-read-date-minibuffer-local-map)))
  14688. (org-defkey map (kbd "RET") 'org-calendar-select)
  14689. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  14690. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  14691. (unwind-protect
  14692. (progn
  14693. (use-local-map map)
  14694. (setq org-read-date-inactive inactive)
  14695. (add-hook 'post-command-hook 'org-read-date-display)
  14696. (setq org-ans0 (read-string prompt default-input
  14697. 'org-read-date-history nil))
  14698. ;; org-ans0: from prompt
  14699. ;; org-ans1: from mouse click
  14700. ;; org-ans2: from calendar motion
  14701. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  14702. (remove-hook 'post-command-hook 'org-read-date-display)
  14703. (use-local-map old-map)
  14704. (when org-read-date-overlay
  14705. (delete-overlay org-read-date-overlay)
  14706. (setq org-read-date-overlay nil)))))
  14707. (bury-buffer "*Calendar*")))))
  14708. (t ; Naked prompt only
  14709. (unwind-protect
  14710. (setq ans (read-string prompt default-input
  14711. 'org-read-date-history timestr))
  14712. (when org-read-date-overlay
  14713. (delete-overlay org-read-date-overlay)
  14714. (setq org-read-date-overlay nil)))))
  14715. (setq final (org-read-date-analyze ans org-def org-defdecode))
  14716. (when org-read-date-analyze-forced-year
  14717. (message "Year was forced into %s"
  14718. (if org-read-date-force-compatible-dates
  14719. "compatible range (1970-2037)"
  14720. "range representable on this machine"))
  14721. (ding))
  14722. ;; One round trip to get rid of 34th of August and stuff like that....
  14723. (setq final (decode-time (apply 'encode-time final)))
  14724. (setq org-read-date-final-answer ans)
  14725. (if to-time
  14726. (apply 'encode-time final)
  14727. (if (and (boundp 'org-time-was-given) org-time-was-given)
  14728. (format "%04d-%02d-%02d %02d:%02d"
  14729. (nth 5 final) (nth 4 final) (nth 3 final)
  14730. (nth 2 final) (nth 1 final))
  14731. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  14732. (defvar org-def)
  14733. (defvar org-defdecode)
  14734. (defvar org-with-time)
  14735. (defun org-read-date-display ()
  14736. "Display the current date prompt interpretation in the minibuffer."
  14737. (when org-read-date-display-live
  14738. (when org-read-date-overlay
  14739. (delete-overlay org-read-date-overlay))
  14740. (when (minibufferp (current-buffer))
  14741. (save-excursion
  14742. (end-of-line 1)
  14743. (while (not (equal (buffer-substring
  14744. (max (point-min) (- (point) 4)) (point))
  14745. " "))
  14746. (insert " ")))
  14747. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  14748. " " (or org-ans1 org-ans2)))
  14749. (org-end-time-was-given nil)
  14750. (f (org-read-date-analyze ans org-def org-defdecode))
  14751. (fmts (if org-dcst
  14752. org-time-stamp-custom-formats
  14753. org-time-stamp-formats))
  14754. (fmt (if (or org-with-time
  14755. (and (boundp 'org-time-was-given) org-time-was-given))
  14756. (cdr fmts)
  14757. (car fmts)))
  14758. (txt (format-time-string fmt (apply 'encode-time f)))
  14759. (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
  14760. (txt (concat "=> " txt)))
  14761. (when (and org-end-time-was-given
  14762. (string-match org-plain-time-of-day-regexp txt))
  14763. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  14764. org-end-time-was-given
  14765. (substring txt (match-end 0)))))
  14766. (when org-read-date-analyze-futurep
  14767. (setq txt (concat txt " (=>F)")))
  14768. (setq org-read-date-overlay
  14769. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  14770. (org-overlay-display org-read-date-overlay txt 'secondary-selection)))))
  14771. (defun org-read-date-analyze (ans org-def org-defdecode)
  14772. "Analyze the combined answer of the date prompt."
  14773. ;; FIXME: cleanup and comment
  14774. (let ((nowdecode (decode-time (current-time)))
  14775. delta deltan deltaw deltadef year month day
  14776. hour minute second wday pm h2 m2 tl wday1
  14777. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  14778. (setq org-read-date-analyze-futurep nil
  14779. org-read-date-analyze-forced-year nil)
  14780. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  14781. (setq ans "+0"))
  14782. (when (setq delta (org-read-date-get-relative ans (current-time) org-def))
  14783. (setq ans (replace-match "" t t ans)
  14784. deltan (car delta)
  14785. deltaw (nth 1 delta)
  14786. deltadef (nth 2 delta)))
  14787. ;; Check if there is an iso week date in there. If yes, store the
  14788. ;; info and postpone interpreting it until the rest of the parsing
  14789. ;; is done.
  14790. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  14791. (setq iso-year (if (match-end 1)
  14792. (org-small-year-to-year
  14793. (string-to-number (match-string 1 ans))))
  14794. iso-weekday (if (match-end 3)
  14795. (string-to-number (match-string 3 ans)))
  14796. iso-week (string-to-number (match-string 2 ans)))
  14797. (setq ans (replace-match "" t t ans)))
  14798. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  14799. (when (string-match
  14800. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  14801. (setq year (if (match-end 2)
  14802. (string-to-number (match-string 2 ans))
  14803. (progn (setq kill-year t)
  14804. (string-to-number (format-time-string "%Y"))))
  14805. month (string-to-number (match-string 3 ans))
  14806. day (string-to-number (match-string 4 ans)))
  14807. (if (< year 100) (setq year (+ 2000 year)))
  14808. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  14809. t nil ans)))
  14810. ;; Help matching dotted european dates
  14811. (when (string-match
  14812. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\.\\( ?[1-9][0-9]\\{3\\}\\)?" ans)
  14813. (setq year (if (match-end 3) (string-to-number (match-string 3 ans))
  14814. (setq kill-year t)
  14815. (string-to-number (format-time-string "%Y")))
  14816. day (string-to-number (match-string 1 ans))
  14817. month (string-to-number (match-string 2 ans))
  14818. ans (replace-match (format "%04d-%02d-%02d" year month day)
  14819. t nil ans)))
  14820. ;; Help matching american dates, like 5/30 or 5/30/7
  14821. (when (string-match
  14822. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  14823. (setq year (if (match-end 4)
  14824. (string-to-number (match-string 4 ans))
  14825. (progn (setq kill-year t)
  14826. (string-to-number (format-time-string "%Y"))))
  14827. month (string-to-number (match-string 1 ans))
  14828. day (string-to-number (match-string 2 ans)))
  14829. (if (< year 100) (setq year (+ 2000 year)))
  14830. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  14831. t nil ans)))
  14832. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  14833. ;; If there is a time with am/pm, and *no* time without it, we convert
  14834. ;; so that matching will be successful.
  14835. (loop for i from 1 to 2 do ; twice, for end time as well
  14836. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  14837. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  14838. (setq hour (string-to-number (match-string 1 ans))
  14839. minute (if (match-end 3)
  14840. (string-to-number (match-string 3 ans))
  14841. 0)
  14842. pm (equal ?p
  14843. (string-to-char (downcase (match-string 4 ans)))))
  14844. (if (and (= hour 12) (not pm))
  14845. (setq hour 0)
  14846. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  14847. (setq ans (replace-match (format "%02d:%02d" hour minute)
  14848. t t ans))))
  14849. ;; Check if a time range is given as a duration
  14850. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  14851. (setq hour (string-to-number (match-string 1 ans))
  14852. h2 (+ hour (string-to-number (match-string 3 ans)))
  14853. minute (string-to-number (match-string 2 ans))
  14854. m2 (+ minute (if (match-end 5) (string-to-number
  14855. (match-string 5 ans))0)))
  14856. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  14857. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  14858. t t ans)))
  14859. ;; Check if there is a time range
  14860. (when (boundp 'org-end-time-was-given)
  14861. (setq org-time-was-given nil)
  14862. (when (and (string-match org-plain-time-of-day-regexp ans)
  14863. (match-end 8))
  14864. (setq org-end-time-was-given (match-string 8 ans))
  14865. (setq ans (concat (substring ans 0 (match-beginning 7))
  14866. (substring ans (match-end 7))))))
  14867. (setq tl (parse-time-string ans)
  14868. day (or (nth 3 tl) (nth 3 org-defdecode))
  14869. month (or (nth 4 tl)
  14870. (if (and org-read-date-prefer-future
  14871. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  14872. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  14873. (nth 4 org-defdecode)))
  14874. year (or (and (not kill-year) (nth 5 tl))
  14875. (if (and org-read-date-prefer-future
  14876. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  14877. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  14878. (nth 5 org-defdecode)))
  14879. hour (or (nth 2 tl) (nth 2 org-defdecode))
  14880. minute (or (nth 1 tl) (nth 1 org-defdecode))
  14881. second (or (nth 0 tl) 0)
  14882. wday (nth 6 tl))
  14883. (when (and (eq org-read-date-prefer-future 'time)
  14884. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  14885. (equal day (nth 3 nowdecode))
  14886. (equal month (nth 4 nowdecode))
  14887. (equal year (nth 5 nowdecode))
  14888. (nth 2 tl)
  14889. (or (< (nth 2 tl) (nth 2 nowdecode))
  14890. (and (= (nth 2 tl) (nth 2 nowdecode))
  14891. (nth 1 tl)
  14892. (< (nth 1 tl) (nth 1 nowdecode)))))
  14893. (setq day (1+ day)
  14894. futurep t))
  14895. ;; Special date definitions below
  14896. (cond
  14897. (iso-week
  14898. ;; There was an iso week
  14899. (require 'cal-iso)
  14900. (setq futurep nil)
  14901. (setq year (or iso-year year)
  14902. day (or iso-weekday wday 1)
  14903. wday nil ; to make sure that the trigger below does not match
  14904. iso-date (calendar-gregorian-from-absolute
  14905. (calendar-absolute-from-iso
  14906. (list iso-week day year))))
  14907. ; FIXME: Should we also push ISO weeks into the future?
  14908. ; (when (and org-read-date-prefer-future
  14909. ; (not iso-year)
  14910. ; (< (calendar-absolute-from-gregorian iso-date)
  14911. ; (time-to-days (current-time))))
  14912. ; (setq year (1+ year)
  14913. ; iso-date (calendar-gregorian-from-absolute
  14914. ; (calendar-absolute-from-iso
  14915. ; (list iso-week day year)))))
  14916. (setq month (car iso-date)
  14917. year (nth 2 iso-date)
  14918. day (nth 1 iso-date)))
  14919. (deltan
  14920. (setq futurep nil)
  14921. (unless deltadef
  14922. (let ((now (decode-time (current-time))))
  14923. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  14924. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  14925. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  14926. ((equal deltaw "m") (setq month (+ month deltan)))
  14927. ((equal deltaw "y") (setq year (+ year deltan)))))
  14928. ((and wday (not (nth 3 tl)))
  14929. ;; Weekday was given, but no day, so pick that day in the week
  14930. ;; on or after the derived date.
  14931. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  14932. (unless (equal wday wday1)
  14933. (setq day (+ day (% (- wday wday1 -7) 7))))))
  14934. (if (and (boundp 'org-time-was-given)
  14935. (nth 2 tl))
  14936. (setq org-time-was-given t))
  14937. (if (< year 100) (setq year (+ 2000 year)))
  14938. ;; Check of the date is representable
  14939. (if org-read-date-force-compatible-dates
  14940. (progn
  14941. (if (< year 1970)
  14942. (setq year 1970 org-read-date-analyze-forced-year t))
  14943. (if (> year 2037)
  14944. (setq year 2037 org-read-date-analyze-forced-year t)))
  14945. (condition-case nil
  14946. (ignore (encode-time second minute hour day month year))
  14947. (error
  14948. (setq year (nth 5 org-defdecode))
  14949. (setq org-read-date-analyze-forced-year t))))
  14950. (setq org-read-date-analyze-futurep futurep)
  14951. (list second minute hour day month year)))
  14952. (defvar parse-time-weekdays)
  14953. (defun org-read-date-get-relative (s today default)
  14954. "Check string S for special relative date string.
  14955. TODAY and DEFAULT are internal times, for today and for a default.
  14956. Return shift list (N what def-flag)
  14957. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  14958. N is the number of WHATs to shift.
  14959. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  14960. the DEFAULT date rather than TODAY."
  14961. (require 'parse-time)
  14962. (when (and
  14963. (string-match
  14964. (concat
  14965. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  14966. "\\([0-9]+\\)?"
  14967. "\\([hdwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  14968. "\\([ \t]\\|$\\)") s)
  14969. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  14970. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  14971. (string-to-char (substring (match-string 1 s) -1))
  14972. ?+))
  14973. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  14974. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  14975. (what (if (match-end 3) (match-string 3 s) "d"))
  14976. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  14977. (date (if rel default today))
  14978. (wday (nth 6 (decode-time date)))
  14979. delta)
  14980. (if wday1
  14981. (progn
  14982. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  14983. (if (= dir ?-) (setq delta (- delta 7)))
  14984. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  14985. (list delta "d" rel))
  14986. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  14987. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  14988. "Turn a user-specified date into the internal representation.
  14989. The internal representation needed by the calendar is (month day year).
  14990. This is a wrapper to handle the brain-dead convention in calendar that
  14991. user function argument order change dependent on argument order."
  14992. (if (boundp 'calendar-date-style)
  14993. (cond
  14994. ((eq calendar-date-style 'american)
  14995. (list arg1 arg2 arg3))
  14996. ((eq calendar-date-style 'european)
  14997. (list arg2 arg1 arg3))
  14998. ((eq calendar-date-style 'iso)
  14999. (list arg2 arg3 arg1)))
  15000. (org-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  15001. (if (org-bound-and-true-p european-calendar-style)
  15002. (list arg2 arg1 arg3)
  15003. (list arg1 arg2 arg3)))))
  15004. (defun org-eval-in-calendar (form &optional keepdate)
  15005. "Eval FORM in the calendar window and return to current window.
  15006. When KEEPDATE is non-nil, update `org-ans2' from the cursor date,
  15007. otherwise stick to the current value of `org-ans2'."
  15008. (let ((sf (selected-frame))
  15009. (sw (selected-window)))
  15010. (select-window (get-buffer-window "*Calendar*" t))
  15011. (eval form)
  15012. (when (and (not keepdate) (calendar-cursor-to-date))
  15013. (let* ((date (calendar-cursor-to-date))
  15014. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15015. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  15016. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  15017. (select-window sw)
  15018. (org-select-frame-set-input-focus sf)))
  15019. (defun org-calendar-select ()
  15020. "Return to `org-read-date' with the date currently selected.
  15021. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15022. (interactive)
  15023. (when (calendar-cursor-to-date)
  15024. (let* ((date (calendar-cursor-to-date))
  15025. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15026. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15027. (if (active-minibuffer-window) (exit-minibuffer))))
  15028. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  15029. "Insert a date stamp for the date given by the internal TIME.
  15030. WITH-HM means use the stamp format that includes the time of the day.
  15031. INACTIVE means use square brackets instead of angular ones, so that the
  15032. stamp will not contribute to the agenda.
  15033. PRE and POST are optional strings to be inserted before and after the
  15034. stamp.
  15035. The command returns the inserted time stamp."
  15036. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  15037. stamp)
  15038. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  15039. (insert-before-markers (or pre ""))
  15040. (when (listp extra)
  15041. (setq extra (car extra))
  15042. (if (and (stringp extra)
  15043. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  15044. (setq extra (format "-%02d:%02d"
  15045. (string-to-number (match-string 1 extra))
  15046. (string-to-number (match-string 2 extra))))
  15047. (setq extra nil)))
  15048. (when extra
  15049. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  15050. (insert-before-markers (setq stamp (format-time-string fmt time)))
  15051. (insert-before-markers (or post ""))
  15052. (setq org-last-inserted-timestamp stamp)))
  15053. (defun org-toggle-time-stamp-overlays ()
  15054. "Toggle the use of custom time stamp formats."
  15055. (interactive)
  15056. (setq org-display-custom-times (not org-display-custom-times))
  15057. (unless org-display-custom-times
  15058. (let ((p (point-min)) (bmp (buffer-modified-p)))
  15059. (while (setq p (next-single-property-change p 'display))
  15060. (if (and (get-text-property p 'display)
  15061. (eq (get-text-property p 'face) 'org-date))
  15062. (remove-text-properties
  15063. p (setq p (next-single-property-change p 'display))
  15064. '(display t))))
  15065. (set-buffer-modified-p bmp)))
  15066. (if (featurep 'xemacs)
  15067. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  15068. (org-restart-font-lock)
  15069. (setq org-table-may-need-update t)
  15070. (if org-display-custom-times
  15071. (message "Time stamps are overlaid with custom format")
  15072. (message "Time stamp overlays removed")))
  15073. (defun org-display-custom-time (beg end)
  15074. "Overlay modified time stamp format over timestamp between BEG and END."
  15075. (let* ((ts (buffer-substring beg end))
  15076. t1 w1 with-hm tf time str w2 (off 0))
  15077. (save-match-data
  15078. (setq t1 (org-parse-time-string ts t))
  15079. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)?\\'" ts)
  15080. (setq off (- (match-end 0) (match-beginning 0)))))
  15081. (setq end (- end off))
  15082. (setq w1 (- end beg)
  15083. with-hm (and (nth 1 t1) (nth 2 t1))
  15084. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  15085. time (org-fix-decoded-time t1)
  15086. str (org-add-props
  15087. (format-time-string
  15088. (substring tf 1 -1) (apply 'encode-time time))
  15089. nil 'mouse-face 'highlight)
  15090. w2 (length str))
  15091. (if (not (= w2 w1))
  15092. (add-text-properties (1+ beg) (+ 2 beg)
  15093. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  15094. (if (featurep 'xemacs)
  15095. (progn
  15096. (put-text-property beg end 'invisible t)
  15097. (put-text-property beg end 'end-glyph (make-glyph str)))
  15098. (put-text-property beg end 'display str))))
  15099. (defun org-translate-time (string)
  15100. "Translate all timestamps in STRING to custom format.
  15101. But do this only if the variable `org-display-custom-times' is set."
  15102. (when org-display-custom-times
  15103. (save-match-data
  15104. (let* ((start 0)
  15105. (re org-ts-regexp-both)
  15106. t1 with-hm inactive tf time str beg end)
  15107. (while (setq start (string-match re string start))
  15108. (setq beg (match-beginning 0)
  15109. end (match-end 0)
  15110. t1 (save-match-data
  15111. (org-parse-time-string (substring string beg end) t))
  15112. with-hm (and (nth 1 t1) (nth 2 t1))
  15113. inactive (equal (substring string beg (1+ beg)) "[")
  15114. tf (funcall (if with-hm 'cdr 'car)
  15115. org-time-stamp-custom-formats)
  15116. time (org-fix-decoded-time t1)
  15117. str (format-time-string
  15118. (concat
  15119. (if inactive "[" "<") (substring tf 1 -1)
  15120. (if inactive "]" ">"))
  15121. (apply 'encode-time time))
  15122. string (replace-match str t t string)
  15123. start (+ start (length str)))))))
  15124. string)
  15125. (defun org-fix-decoded-time (time)
  15126. "Set 0 instead of nil for the first 6 elements of time.
  15127. Don't touch the rest."
  15128. (let ((n 0))
  15129. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  15130. (define-obsolete-function-alias 'org-days-to-time 'org-time-stamp-to-now "24.4")
  15131. (defun org-time-stamp-to-now (timestamp-string &optional seconds)
  15132. "Difference between TIMESTAMP-STRING and now in days.
  15133. If SECONDS is non-nil, return the difference in seconds."
  15134. (let ((fdiff (if seconds 'org-float-time 'time-to-days)))
  15135. (- (funcall fdiff (org-time-string-to-time timestamp-string))
  15136. (funcall fdiff (current-time)))))
  15137. (defun org-deadline-close (timestamp-string &optional ndays)
  15138. "Is the time in TIMESTAMP-STRING close to the current date?"
  15139. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  15140. (and (< (org-time-stamp-to-now timestamp-string) ndays)
  15141. (not (org-entry-is-done-p))))
  15142. (defun org-get-wdays (ts &optional delay zero-delay)
  15143. "Get the deadline lead time appropriate for timestring TS.
  15144. When DELAY is non-nil, get the delay time for scheduled items
  15145. instead of the deadline lead time. When ZERO-DELAY is non-nil
  15146. and `org-scheduled-delay-days' is 0, enforce 0 as the delay,
  15147. don't try to find the delay cookie in the scheduled timestamp."
  15148. (let ((tv (if delay org-scheduled-delay-days
  15149. org-deadline-warning-days)))
  15150. (cond
  15151. ((or (and delay (< tv 0))
  15152. (and delay zero-delay (<= tv 0))
  15153. (and (not delay) (<= tv 0)))
  15154. ;; Enforce this value no matter what
  15155. (- tv))
  15156. ((string-match "-\\([0-9]+\\)\\([hdwmy]\\)\\(\\'\\|>\\| \\)" ts)
  15157. ;; lead time is specified.
  15158. (floor (* (string-to-number (match-string 1 ts))
  15159. (cdr (assoc (match-string 2 ts)
  15160. '(("d" . 1) ("w" . 7)
  15161. ("m" . 30.4) ("y" . 365.25)
  15162. ("h" . 0.041667)))))))
  15163. ;; go for the default.
  15164. (t tv))))
  15165. (defun org-calendar-select-mouse (ev)
  15166. "Return to `org-read-date' with the date currently selected.
  15167. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15168. (interactive "e")
  15169. (mouse-set-point ev)
  15170. (when (calendar-cursor-to-date)
  15171. (let* ((date (calendar-cursor-to-date))
  15172. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15173. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15174. (if (active-minibuffer-window) (exit-minibuffer))))
  15175. (defun org-check-deadlines (ndays)
  15176. "Check if there are any deadlines due or past due.
  15177. A deadline is considered due if it happens within `org-deadline-warning-days'
  15178. days from today's date. If the deadline appears in an entry marked DONE,
  15179. it is not shown. The prefix arg NDAYS can be used to test that many
  15180. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  15181. (interactive "P")
  15182. (let* ((org-warn-days
  15183. (cond
  15184. ((equal ndays '(4)) 100000)
  15185. (ndays (prefix-numeric-value ndays))
  15186. (t (abs org-deadline-warning-days))))
  15187. (case-fold-search nil)
  15188. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15189. (callback
  15190. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  15191. (message "%d deadlines past-due or due within %d days"
  15192. (org-occur regexp nil callback)
  15193. org-warn-days)))
  15194. (defsubst org-re-timestamp (type)
  15195. "Return a regexp for timestamp TYPE.
  15196. Allowed values for TYPE are:
  15197. all: all timestamps
  15198. active: only active timestamps (<...>)
  15199. inactive: only inactive timestamps ([...])
  15200. scheduled: only scheduled timestamps
  15201. deadline: only deadline timestamps
  15202. When TYPE is nil, fall back on returning a regexp that matches
  15203. both scheduled and deadline timestamps."
  15204. (cond ((eq type 'all) "\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}\\(?: +[^]+0-9> \n -]+\\)?\\(?: +[0-9]\\{1,2\\}:[0-9]\\{2\\}\\)?\\)")
  15205. ((eq type 'active) org-ts-regexp)
  15206. ((eq type 'inactive) "\\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^ \n>]*?\\)\\]")
  15207. ((eq type 'scheduled) (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>"))
  15208. ((eq type 'deadline) (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15209. ((eq type 'scheduled-or-deadline)
  15210. (concat "\\<\\(?:" org-deadline-string "\\|" org-scheduled-string "\\) *<\\([^>]+\\)>"))))
  15211. (defun org-check-before-date (date)
  15212. "Check if there are deadlines or scheduled entries before DATE."
  15213. (interactive (list (org-read-date)))
  15214. (let ((case-fold-search nil)
  15215. (regexp (org-re-timestamp org-ts-type))
  15216. (callback
  15217. (lambda () (time-less-p
  15218. (org-time-string-to-time (match-string 1))
  15219. (org-time-string-to-time date)))))
  15220. (message "%d entries before %s"
  15221. (org-occur regexp nil callback) date)))
  15222. (defun org-check-after-date (date)
  15223. "Check if there are deadlines or scheduled entries after DATE."
  15224. (interactive (list (org-read-date)))
  15225. (let ((case-fold-search nil)
  15226. (regexp (org-re-timestamp org-ts-type))
  15227. (callback
  15228. (lambda () (not
  15229. (time-less-p
  15230. (org-time-string-to-time (match-string 1))
  15231. (org-time-string-to-time date))))))
  15232. (message "%d entries after %s"
  15233. (org-occur regexp nil callback) date)))
  15234. (defun org-check-dates-range (start-date end-date)
  15235. "Check for deadlines/scheduled entries between START-DATE and END-DATE."
  15236. (interactive (list (org-read-date nil nil nil "Range starts")
  15237. (org-read-date nil nil nil "Range end")))
  15238. (let ((case-fold-search nil)
  15239. (regexp (org-re-timestamp org-ts-type))
  15240. (callback
  15241. (lambda ()
  15242. (let ((match (match-string 1)))
  15243. (and
  15244. (not (time-less-p
  15245. (org-time-string-to-time match)
  15246. (org-time-string-to-time start-date)))
  15247. (time-less-p
  15248. (org-time-string-to-time match)
  15249. (org-time-string-to-time end-date)))))))
  15250. (message "%d entries between %s and %s"
  15251. (org-occur regexp nil callback) start-date end-date)))
  15252. (defun org-evaluate-time-range (&optional to-buffer)
  15253. "Evaluate a time range by computing the difference between start and end.
  15254. Normally the result is just printed in the echo area, but with prefix arg
  15255. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  15256. If the time range is actually in a table, the result is inserted into the
  15257. next column.
  15258. For time difference computation, a year is assumed to be exactly 365
  15259. days in order to avoid rounding problems."
  15260. (interactive "P")
  15261. (or
  15262. (org-clock-update-time-maybe)
  15263. (save-excursion
  15264. (unless (org-at-date-range-p t)
  15265. (goto-char (point-at-bol))
  15266. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  15267. (if (not (org-at-date-range-p t))
  15268. (error "Not at a time-stamp range, and none found in current line")))
  15269. (let* ((ts1 (match-string 1))
  15270. (ts2 (match-string 2))
  15271. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  15272. (match-end (match-end 0))
  15273. (time1 (org-time-string-to-time ts1))
  15274. (time2 (org-time-string-to-time ts2))
  15275. (t1 (org-float-time time1))
  15276. (t2 (org-float-time time2))
  15277. (diff (abs (- t2 t1)))
  15278. (negative (< (- t2 t1) 0))
  15279. ;; (ys (floor (* 365 24 60 60)))
  15280. (ds (* 24 60 60))
  15281. (hs (* 60 60))
  15282. (fy "%dy %dd %02d:%02d")
  15283. (fy1 "%dy %dd")
  15284. (fd "%dd %02d:%02d")
  15285. (fd1 "%dd")
  15286. (fh "%02d:%02d")
  15287. y d h m align)
  15288. (if havetime
  15289. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  15290. y 0
  15291. d (floor (/ diff ds)) diff (mod diff ds)
  15292. h (floor (/ diff hs)) diff (mod diff hs)
  15293. m (floor (/ diff 60)))
  15294. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  15295. y 0
  15296. d (floor (+ (/ diff ds) 0.5))
  15297. h 0 m 0))
  15298. (if (not to-buffer)
  15299. (message "%s" (org-make-tdiff-string y d h m))
  15300. (if (org-at-table-p)
  15301. (progn
  15302. (goto-char match-end)
  15303. (setq align t)
  15304. (and (looking-at " *|") (goto-char (match-end 0))))
  15305. (goto-char match-end))
  15306. (if (looking-at
  15307. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  15308. (replace-match ""))
  15309. (if negative (insert " -"))
  15310. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  15311. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  15312. (insert " " (format fh h m))))
  15313. (if align (org-table-align))
  15314. (message "Time difference inserted")))))
  15315. (defun org-make-tdiff-string (y d h m)
  15316. (let ((fmt "")
  15317. (l nil))
  15318. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  15319. l (push y l)))
  15320. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  15321. l (push d l)))
  15322. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  15323. l (push h l)))
  15324. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  15325. l (push m l)))
  15326. (apply 'format fmt (nreverse l))))
  15327. (defun org-time-string-to-time (s &optional buffer pos)
  15328. "Convert a timestamp string into internal time."
  15329. (condition-case errdata
  15330. (apply 'encode-time (org-parse-time-string s))
  15331. (error (error "Bad timestamp `%s'%s\nError was: %s"
  15332. s (if (not (and buffer pos))
  15333. ""
  15334. (format " at %d in buffer `%s'" pos buffer))
  15335. (cdr errdata)))))
  15336. (defun org-time-string-to-seconds (s)
  15337. "Convert a timestamp string to a number of seconds."
  15338. (org-float-time (org-time-string-to-time s)))
  15339. (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
  15340. "Convert a time stamp to an absolute day number.
  15341. If there is a specifier for a cyclic time stamp, get the closest
  15342. date to DAYNR.
  15343. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  15344. The variable `date' is bound by the calendar when this is called."
  15345. (cond
  15346. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  15347. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  15348. daynr
  15349. (+ daynr 1000)))
  15350. ((and daynr (string-match "\\+[0-9]+[hdwmy]" s))
  15351. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  15352. (time-to-days (current-time))) (match-string 0 s)
  15353. prefer show-all))
  15354. (t (time-to-days
  15355. (condition-case errdata
  15356. (apply 'encode-time (org-parse-time-string s))
  15357. (error (error "Bad timestamp `%s'%s\nError was: %s"
  15358. s (if (not (and buffer pos))
  15359. ""
  15360. (format " at %d in buffer `%s'" pos buffer))
  15361. (cdr errdata))))))))
  15362. (defun org-days-to-iso-week (days)
  15363. "Return the iso week number."
  15364. (require 'cal-iso)
  15365. (car (calendar-iso-from-absolute days)))
  15366. (defun org-small-year-to-year (year)
  15367. "Convert 2-digit years into 4-digit years.
  15368. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  15369. The year 2000 cannot be abbreviated. Any year larger than 99
  15370. is returned unchanged."
  15371. (if (< year 38)
  15372. (setq year (+ 2000 year))
  15373. (if (< year 100)
  15374. (setq year (+ 1900 year))))
  15375. year)
  15376. (defun org-time-from-absolute (d)
  15377. "Return the time corresponding to date D.
  15378. D may be an absolute day number, or a calendar-type list (month day year)."
  15379. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  15380. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  15381. (defun org-calendar-holiday ()
  15382. "List of holidays, for Diary display in Org-mode."
  15383. (require 'holidays)
  15384. (let ((hl (funcall
  15385. (if (fboundp 'calendar-check-holidays)
  15386. 'calendar-check-holidays 'check-calendar-holidays) date)))
  15387. (if hl (mapconcat 'identity hl "; "))))
  15388. (defun org-diary-sexp-entry (sexp entry date)
  15389. "Process a SEXP diary ENTRY for DATE."
  15390. (require 'diary-lib)
  15391. (let ((result (if calendar-debug-sexp
  15392. (let ((stack-trace-on-error t))
  15393. (eval (car (read-from-string sexp))))
  15394. (condition-case nil
  15395. (eval (car (read-from-string sexp)))
  15396. (error
  15397. (beep)
  15398. (message "Bad sexp at line %d in %s: %s"
  15399. (org-current-line)
  15400. (buffer-file-name) sexp)
  15401. (sleep-for 2))))))
  15402. (cond ((stringp result) (split-string result "; "))
  15403. ((and (consp result)
  15404. (not (consp (cdr result)))
  15405. (stringp (cdr result))) (cdr result))
  15406. ((and (consp result)
  15407. (stringp (car result))) result)
  15408. (result entry))))
  15409. (defun org-diary-to-ical-string (frombuf)
  15410. "Get iCalendar entries from diary entries in buffer FROMBUF.
  15411. This uses the icalendar.el library."
  15412. (let* ((tmpdir (if (featurep 'xemacs)
  15413. (temp-directory)
  15414. temporary-file-directory))
  15415. (tmpfile (make-temp-name
  15416. (expand-file-name "orgics" tmpdir)))
  15417. buf rtn b e)
  15418. (with-current-buffer frombuf
  15419. (icalendar-export-region (point-min) (point-max) tmpfile)
  15420. (setq buf (find-buffer-visiting tmpfile))
  15421. (set-buffer buf)
  15422. (goto-char (point-min))
  15423. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  15424. (setq b (match-beginning 0)))
  15425. (goto-char (point-max))
  15426. (if (re-search-backward "^END:VEVENT" nil t)
  15427. (setq e (match-end 0)))
  15428. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  15429. (kill-buffer buf)
  15430. (delete-file tmpfile)
  15431. rtn))
  15432. (defun org-closest-date (start current change prefer show-all)
  15433. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  15434. When PREFER is `past', return a date that is either CURRENT or past.
  15435. When PREFER is `future', return a date that is either CURRENT or future.
  15436. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  15437. ;; Make the proper lists from the dates
  15438. (catch 'exit
  15439. (let ((a1 '(("h" . hour)
  15440. ("d" . day)
  15441. ("w" . week)
  15442. ("m" . month)
  15443. ("y" . year)))
  15444. (shour (nth 2 (org-parse-time-string start)))
  15445. dn dw sday cday n1 n2 n0
  15446. d m y y1 y2 date1 date2 nmonths nm ny m2)
  15447. (setq start (org-date-to-gregorian start)
  15448. current (org-date-to-gregorian
  15449. (if show-all
  15450. current
  15451. (time-to-days (current-time))))
  15452. sday (calendar-absolute-from-gregorian start)
  15453. cday (calendar-absolute-from-gregorian current))
  15454. (if (<= cday sday) (throw 'exit sday))
  15455. (if (string-match "\\(\\+[0-9]+\\)\\([hdwmy]\\)" change)
  15456. (setq dn (string-to-number (match-string 1 change))
  15457. dw (cdr (assoc (match-string 2 change) a1)))
  15458. (error "Invalid change specifier: %s" change))
  15459. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  15460. (cond
  15461. ((eq dw 'hour)
  15462. (let ((missing-hours
  15463. (mod (+ (- (* 24 (- cday sday)) shour) org-extend-today-until)
  15464. dn)))
  15465. (setq n1 (if (zerop missing-hours) cday
  15466. (- cday (1+ (floor (/ missing-hours 24)))))
  15467. n2 (+ cday (floor (/ (- dn missing-hours) 24))))))
  15468. ((eq dw 'day)
  15469. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  15470. n2 (+ n1 dn)))
  15471. ((eq dw 'year)
  15472. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  15473. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  15474. (setq date1 (list m d y1)
  15475. n1 (calendar-absolute-from-gregorian date1)
  15476. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  15477. n2 (calendar-absolute-from-gregorian date2)))
  15478. ((eq dw 'month)
  15479. ;; approx number of month between the two dates
  15480. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  15481. ;; How often does dn fit in there?
  15482. (setq d (nth 1 start) m (car start) y (nth 2 start)
  15483. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  15484. m (+ m nm)
  15485. ny (floor (/ m 12))
  15486. y (+ y ny)
  15487. m (- m (* ny 12)))
  15488. (while (> m 12) (setq m (- m 12) y (1+ y)))
  15489. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  15490. (setq m2 (+ m dn) y2 y)
  15491. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  15492. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  15493. (while (<= n2 cday)
  15494. (setq n1 n2 m m2 y y2)
  15495. (setq m2 (+ m dn) y2 y)
  15496. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  15497. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  15498. ;; Make sure n1 is the earlier date
  15499. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  15500. (if show-all
  15501. (cond
  15502. ((eq prefer 'past) (if (= cday n2) n2 n1))
  15503. ((eq prefer 'future) (if (= cday n1) n1 n2))
  15504. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  15505. (cond
  15506. ((eq prefer 'past) (if (= cday n2) n2 n1))
  15507. ((eq prefer 'future) (if (= cday n1) n1 n2))
  15508. (t (if (= cday n1) n1 n2)))))))
  15509. (defun org-date-to-gregorian (date)
  15510. "Turn any specification of DATE into a Gregorian date for the calendar."
  15511. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  15512. ((and (listp date) (= (length date) 3)) date)
  15513. ((stringp date)
  15514. (setq date (org-parse-time-string date))
  15515. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  15516. ((listp date)
  15517. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  15518. (defun org-parse-time-string (s &optional nodefault)
  15519. "Parse the standard Org-mode time string.
  15520. This should be a lot faster than the normal `parse-time-string'.
  15521. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  15522. hour and minute fields will be nil if not given."
  15523. (cond ((string-match org-ts-regexp0 s)
  15524. (list 0
  15525. (if (or (match-beginning 8) (not nodefault))
  15526. (string-to-number (or (match-string 8 s) "0")))
  15527. (if (or (match-beginning 7) (not nodefault))
  15528. (string-to-number (or (match-string 7 s) "0")))
  15529. (string-to-number (match-string 4 s))
  15530. (string-to-number (match-string 3 s))
  15531. (string-to-number (match-string 2 s))
  15532. nil nil nil))
  15533. ((string-match "^<[^>]+>$" s)
  15534. (decode-time (seconds-to-time (org-matcher-time s))))
  15535. (t (error "Not a standard Org-mode time string: %s" s))))
  15536. (defun org-timestamp-up (&optional arg)
  15537. "Increase the date item at the cursor by one.
  15538. If the cursor is on the year, change the year. If it is on the month,
  15539. the day or the time, change that.
  15540. With prefix ARG, change by that many units."
  15541. (interactive "p")
  15542. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  15543. (defun org-timestamp-down (&optional arg)
  15544. "Decrease the date item at the cursor by one.
  15545. If the cursor is on the year, change the year. If it is on the month,
  15546. the day or the time, change that.
  15547. With prefix ARG, change by that many units."
  15548. (interactive "p")
  15549. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  15550. (defun org-timestamp-up-day (&optional arg)
  15551. "Increase the date in the time stamp by one day.
  15552. With prefix ARG, change that many days."
  15553. (interactive "p")
  15554. (if (and (not (org-at-timestamp-p t))
  15555. (org-at-heading-p))
  15556. (org-todo 'up)
  15557. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  15558. (defun org-timestamp-down-day (&optional arg)
  15559. "Decrease the date in the time stamp by one day.
  15560. With prefix ARG, change that many days."
  15561. (interactive "p")
  15562. (if (and (not (org-at-timestamp-p t))
  15563. (org-at-heading-p))
  15564. (org-todo 'down)
  15565. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  15566. (defun org-at-timestamp-p (&optional inactive-ok)
  15567. "Determine if the cursor is in or at a timestamp."
  15568. (interactive)
  15569. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  15570. (pos (point))
  15571. (ans (or (looking-at tsr)
  15572. (save-excursion
  15573. (skip-chars-backward "^[<\n\r\t")
  15574. (if (> (point) (point-min)) (backward-char 1))
  15575. (and (looking-at tsr)
  15576. (> (- (match-end 0) pos) -1))))))
  15577. (and ans
  15578. (boundp 'org-ts-what)
  15579. (setq org-ts-what
  15580. (cond
  15581. ((= pos (match-beginning 0)) 'bracket)
  15582. ;; Point is considered to be "on the bracket" whether
  15583. ;; it's really on it or right after it.
  15584. ((= pos (1- (match-end 0))) 'bracket)
  15585. ((= pos (match-end 0)) 'after)
  15586. ((org-pos-in-match-range pos 2) 'year)
  15587. ((org-pos-in-match-range pos 3) 'month)
  15588. ((org-pos-in-match-range pos 7) 'hour)
  15589. ((org-pos-in-match-range pos 8) 'minute)
  15590. ((or (org-pos-in-match-range pos 4)
  15591. (org-pos-in-match-range pos 5)) 'day)
  15592. ((and (> pos (or (match-end 8) (match-end 5)))
  15593. (< pos (match-end 0)))
  15594. (- pos (or (match-end 8) (match-end 5))))
  15595. (t 'day))))
  15596. ans))
  15597. (defun org-toggle-timestamp-type ()
  15598. "Toggle the type (<active> or [inactive]) of a time stamp."
  15599. (interactive)
  15600. (when (org-at-timestamp-p t)
  15601. (let ((beg (match-beginning 0)) (end (match-end 0))
  15602. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  15603. (save-excursion
  15604. (goto-char beg)
  15605. (while (re-search-forward "[][<>]" end t)
  15606. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  15607. t t)))
  15608. (message "Timestamp is now %sactive"
  15609. (if (equal (char-after beg) ?<) "" "in")))))
  15610. (defun org-at-clock-log-p nil
  15611. "Is the cursor on the clock log line?"
  15612. (save-excursion
  15613. (move-beginning-of-line 1)
  15614. (looking-at "^[ \t]*CLOCK:")))
  15615. (defvar org-clock-history) ; defined in org-clock.el
  15616. (defvar org-clock-adjust-closest nil) ; defined in org-clock.el
  15617. (defun org-timestamp-change (n &optional what updown suppress-tmp-delay)
  15618. "Change the date in the time stamp at point.
  15619. The date will be changed by N times WHAT. WHAT can be `day', `month',
  15620. `year', `minute', `second'. If WHAT is not given, the cursor position
  15621. in the timestamp determines what will be changed.
  15622. When SUPPRESS-TMP-DELAY is non-nil, suppress delays like \"--2d\"."
  15623. (let ((origin (point)) origin-cat
  15624. with-hm inactive
  15625. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  15626. org-ts-what
  15627. extra rem
  15628. ts time time0 fixnext clrgx)
  15629. (if (not (org-at-timestamp-p t))
  15630. (error "Not at a timestamp"))
  15631. (if (and (not what) (eq org-ts-what 'bracket))
  15632. (org-toggle-timestamp-type)
  15633. ;; Point isn't on brackets. Remember the part of the time-stamp
  15634. ;; the point was in. Indeed, size of time-stamps may change,
  15635. ;; but point must be kept in the same category nonetheless.
  15636. (setq origin-cat org-ts-what)
  15637. (if (and (not what) (not (eq org-ts-what 'day))
  15638. org-display-custom-times
  15639. (get-text-property (point) 'display)
  15640. (not (get-text-property (1- (point)) 'display)))
  15641. (setq org-ts-what 'day))
  15642. (setq org-ts-what (or what org-ts-what)
  15643. inactive (= (char-after (match-beginning 0)) ?\[)
  15644. ts (match-string 0))
  15645. (replace-match "")
  15646. (when (string-match
  15647. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?-?[-+][0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)*\\)[]>]"
  15648. ts)
  15649. (setq extra (match-string 1 ts))
  15650. (if suppress-tmp-delay
  15651. (setq extra (replace-regexp-in-string " --[0-9]+[hdwmy]" "" extra))))
  15652. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  15653. (setq with-hm t))
  15654. (setq time0 (org-parse-time-string ts))
  15655. (when (and updown
  15656. (eq org-ts-what 'minute)
  15657. (not current-prefix-arg))
  15658. ;; This looks like s-up and s-down. Change by one rounding step.
  15659. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  15660. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  15661. (setcar (cdr time0) (+ (nth 1 time0)
  15662. (if (> n 0) (- rem) (- dm rem))))))
  15663. (setq time
  15664. (encode-time (or (car time0) 0)
  15665. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  15666. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  15667. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  15668. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  15669. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  15670. (nthcdr 6 time0)))
  15671. (when (and (member org-ts-what '(hour minute))
  15672. extra
  15673. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  15674. (setq extra (org-modify-ts-extra
  15675. extra
  15676. (if (eq org-ts-what 'hour) 2 5)
  15677. n dm)))
  15678. (when (integerp org-ts-what)
  15679. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  15680. (if (eq what 'calendar)
  15681. (let ((cal-date (org-get-date-from-calendar)))
  15682. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  15683. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  15684. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  15685. (setcar time0 (or (car time0) 0))
  15686. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  15687. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  15688. (setq time (apply 'encode-time time0))))
  15689. ;; Insert the new time-stamp, and ensure point stays in the same
  15690. ;; category as before (i.e. not after the last position in that
  15691. ;; category).
  15692. (let ((pos (point)))
  15693. ;; Stay before inserted string. `save-excursion' is of no use.
  15694. (setq org-last-changed-timestamp
  15695. (org-insert-time-stamp time with-hm inactive nil nil extra))
  15696. (goto-char pos))
  15697. (save-match-data
  15698. (looking-at org-ts-regexp3)
  15699. (goto-char (cond
  15700. ;; `day' category ends before `hour' if any, or at
  15701. ;; the end of the day name.
  15702. ((eq origin-cat 'day)
  15703. (min (or (match-beginning 7) (1- (match-end 5))) origin))
  15704. ((eq origin-cat 'hour) (min (match-end 7) origin))
  15705. ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
  15706. ((integerp origin-cat) (min (1- (match-end 0)) origin))
  15707. ;; `year' and `month' have both fixed size: point
  15708. ;; couldn't have moved into another part.
  15709. (t origin))))
  15710. ;; Update clock if on a CLOCK line.
  15711. (org-clock-update-time-maybe)
  15712. ;; Maybe adjust the closest clock in `org-clock-history'
  15713. (when org-clock-adjust-closest
  15714. (if (not (and (org-at-clock-log-p)
  15715. (< 1 (length (delq nil (mapcar (lambda(m) (marker-position m))
  15716. org-clock-history))))))
  15717. (message "No clock to adjust")
  15718. (cond ((save-excursion ; fix previous clock?
  15719. (re-search-backward org-ts-regexp0 nil t)
  15720. (org-looking-back (concat org-clock-string " \\[")))
  15721. (setq fixnext 1 clrgx (concat org-ts-regexp0 "\\] =>.*$")))
  15722. ((save-excursion ; fix next clock?
  15723. (re-search-backward org-ts-regexp0 nil t)
  15724. (looking-at (concat org-ts-regexp0 "\\] =>")))
  15725. (setq fixnext -1 clrgx (concat org-clock-string " \\[" org-ts-regexp0))))
  15726. (save-window-excursion
  15727. ;; Find closest clock to point, adjust the previous/next one in history
  15728. (let* ((p (save-excursion (org-back-to-heading t)))
  15729. (cl (mapcar (lambda(c) (abs (- (marker-position c) p))) org-clock-history))
  15730. (clfixnth
  15731. (+ fixnext (- (length cl) (or (length (member (apply #'min cl) cl)) 100))))
  15732. (clfixpos (if (> 0 clfixnth) nil (nth clfixnth org-clock-history))))
  15733. (if (not clfixpos)
  15734. (message "No clock to adjust")
  15735. (save-excursion
  15736. (org-goto-marker-or-bmk clfixpos)
  15737. (org-show-subtree)
  15738. (when (re-search-forward clrgx nil t)
  15739. (goto-char (match-beginning 1))
  15740. (let (org-clock-adjust-closest)
  15741. (org-timestamp-change n org-ts-what updown))
  15742. (message "Clock adjusted in %s for heading: %s"
  15743. (file-name-nondirectory (buffer-file-name))
  15744. (org-get-heading t t)))))))))
  15745. ;; Try to recenter the calendar window, if any.
  15746. (if (and org-calendar-follow-timestamp-change
  15747. (get-buffer-window "*Calendar*" t)
  15748. (memq org-ts-what '(day month year)))
  15749. (org-recenter-calendar (time-to-days time))))))
  15750. (defun org-modify-ts-extra (s pos n dm)
  15751. "Change the different parts of the lead-time and repeat fields in timestamp."
  15752. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  15753. ng h m new rem)
  15754. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  15755. (cond
  15756. ((or (org-pos-in-match-range pos 2)
  15757. (org-pos-in-match-range pos 3))
  15758. (setq m (string-to-number (match-string 3 s))
  15759. h (string-to-number (match-string 2 s)))
  15760. (if (org-pos-in-match-range pos 2)
  15761. (setq h (+ h n))
  15762. (setq n (* dm (org-no-warnings (signum n))))
  15763. (when (not (= 0 (setq rem (% m dm))))
  15764. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  15765. (setq m (+ m n)))
  15766. (if (< m 0) (setq m (+ m 60) h (1- h)))
  15767. (if (> m 59) (setq m (- m 60) h (1+ h)))
  15768. (setq h (min 24 (max 0 h)))
  15769. (setq ng 1 new (format "-%02d:%02d" h m)))
  15770. ((org-pos-in-match-range pos 6)
  15771. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  15772. ((org-pos-in-match-range pos 5)
  15773. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  15774. ((org-pos-in-match-range pos 9)
  15775. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  15776. ((org-pos-in-match-range pos 8)
  15777. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  15778. (when ng
  15779. (setq s (concat
  15780. (substring s 0 (match-beginning ng))
  15781. new
  15782. (substring s (match-end ng))))))
  15783. s))
  15784. (defun org-recenter-calendar (date)
  15785. "If the calendar is visible, recenter it to DATE."
  15786. (let ((cwin (get-buffer-window "*Calendar*" t)))
  15787. (when cwin
  15788. (let ((calendar-move-hook nil))
  15789. (with-selected-window cwin
  15790. (calendar-goto-date (if (listp date) date
  15791. (calendar-gregorian-from-absolute date))))))))
  15792. (defun org-goto-calendar (&optional arg)
  15793. "Go to the Emacs calendar at the current date.
  15794. If there is a time stamp in the current line, go to that date.
  15795. A prefix ARG can be used to force the current date."
  15796. (interactive "P")
  15797. (let ((tsr org-ts-regexp) diff
  15798. (calendar-move-hook nil)
  15799. (calendar-view-holidays-initially-flag nil)
  15800. (calendar-view-diary-initially-flag nil))
  15801. (if (or (org-at-timestamp-p)
  15802. (save-excursion
  15803. (beginning-of-line 1)
  15804. (looking-at (concat ".*" tsr))))
  15805. (let ((d1 (time-to-days (current-time)))
  15806. (d2 (time-to-days
  15807. (org-time-string-to-time (match-string 1)))))
  15808. (setq diff (- d2 d1))))
  15809. (calendar)
  15810. (calendar-goto-today)
  15811. (if (and diff (not arg)) (calendar-forward-day diff))))
  15812. (defun org-get-date-from-calendar ()
  15813. "Return a list (month day year) of date at point in calendar."
  15814. (with-current-buffer "*Calendar*"
  15815. (save-match-data
  15816. (calendar-cursor-to-date))))
  15817. (defun org-date-from-calendar ()
  15818. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  15819. If there is already a time stamp at the cursor position, update it."
  15820. (interactive)
  15821. (if (org-at-timestamp-p t)
  15822. (org-timestamp-change 0 'calendar)
  15823. (let ((cal-date (org-get-date-from-calendar)))
  15824. (org-insert-time-stamp
  15825. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  15826. (defcustom org-effort-durations
  15827. `(("h" . 60)
  15828. ("d" . ,(* 60 8))
  15829. ("w" . ,(* 60 8 5))
  15830. ("m" . ,(* 60 8 5 4))
  15831. ("y" . ,(* 60 8 5 40)))
  15832. "Conversion factor to minutes for an effort modifier.
  15833. Each entry has the form (MODIFIER . MINUTES).
  15834. In an effort string, a number followed by MODIFIER is multiplied
  15835. by the specified number of MINUTES to obtain an effort in
  15836. minutes.
  15837. For example, if the value of this variable is ((\"hours\" . 60)), then an
  15838. effort string \"2hours\" is equivalent to 120 minutes."
  15839. :group 'org-agenda
  15840. :version "24.1"
  15841. :type '(alist :key-type (string :tag "Modifier")
  15842. :value-type (number :tag "Minutes")))
  15843. (defun org-minutes-to-clocksum-string (m)
  15844. "Format number of minutes as a clocksum string.
  15845. The format is determined by `org-time-clocksum-format',
  15846. `org-time-clocksum-use-fractional' and
  15847. `org-time-clocksum-fractional-format' and
  15848. `org-time-clocksum-use-effort-durations'."
  15849. (let ((clocksum "") h d w mo y fmt n)
  15850. (setq h (if org-time-clocksum-use-effort-durations
  15851. (cdr (assoc "h" org-effort-durations)) 60)
  15852. d (if org-time-clocksum-use-effort-durations
  15853. (/ (cdr (assoc "d" org-effort-durations)) h) 24)
  15854. w (if org-time-clocksum-use-effort-durations
  15855. (/ (cdr (assoc "w" org-effort-durations)) (* d h)) 7)
  15856. mo (if org-time-clocksum-use-effort-durations
  15857. (/ (cdr (assoc "m" org-effort-durations)) (* d h)) 30)
  15858. y (if org-time-clocksum-use-effort-durations
  15859. (/ (cdr (assoc "y" org-effort-durations)) (* d h)) 365))
  15860. ;; fractional format
  15861. (if org-time-clocksum-use-fractional
  15862. (cond
  15863. ;; single format string
  15864. ((stringp org-time-clocksum-fractional-format)
  15865. (format org-time-clocksum-fractional-format (/ m (float h))))
  15866. ;; choice of fractional formats for different time units
  15867. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :years))
  15868. (> (/ (truncate m) (* y d h)) 0))
  15869. (format fmt (/ m (* y d (float h)))))
  15870. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :months))
  15871. (> (/ (truncate m) (* mo d h)) 0))
  15872. (format fmt (/ m (* mo d (float h)))))
  15873. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  15874. (> (/ (truncate m) (* w d h)) 0))
  15875. (format fmt (/ m (* w d (float h)))))
  15876. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :days))
  15877. (> (/ (truncate m) (* d h)) 0))
  15878. (format fmt (/ m (* d (float h)))))
  15879. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  15880. (> (/ (truncate m) h) 0))
  15881. (format fmt (/ m (float h))))
  15882. ((setq fmt (plist-get org-time-clocksum-fractional-format :minutes))
  15883. (format fmt m))
  15884. ;; fall back to smallest time unit with a format
  15885. ((setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  15886. (format fmt (/ m (float h))))
  15887. ((setq fmt (plist-get org-time-clocksum-fractional-format :days))
  15888. (format fmt (/ m (* d (float h)))))
  15889. ((setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  15890. (format fmt (/ m (* w d (float h)))))
  15891. ((setq fmt (plist-get org-time-clocksum-fractional-format :months))
  15892. (format fmt (/ m (* mo d (float h)))))
  15893. ((setq fmt (plist-get org-time-clocksum-fractional-format :years))
  15894. (format fmt (/ m (* y d (float h))))))
  15895. ;; standard (non-fractional) format, with single format string
  15896. (if (stringp org-time-clocksum-format)
  15897. (format org-time-clocksum-format (setq n (/ m h)) (- m (* h n)))
  15898. ;; separate formats components
  15899. (and (setq fmt (plist-get org-time-clocksum-format :years))
  15900. (or (> (setq n (/ (truncate m) (* y d h))) 0)
  15901. (plist-get org-time-clocksum-format :require-years))
  15902. (setq clocksum (concat clocksum (format fmt n))
  15903. m (- m (* n y d h))))
  15904. (and (setq fmt (plist-get org-time-clocksum-format :months))
  15905. (or (> (setq n (/ (truncate m) (* mo d h))) 0)
  15906. (plist-get org-time-clocksum-format :require-months))
  15907. (setq clocksum (concat clocksum (format fmt n))
  15908. m (- m (* n mo d h))))
  15909. (and (setq fmt (plist-get org-time-clocksum-format :weeks))
  15910. (or (> (setq n (/ (truncate m) (* w d h))) 0)
  15911. (plist-get org-time-clocksum-format :require-weeks))
  15912. (setq clocksum (concat clocksum (format fmt n))
  15913. m (- m (* n w d h))))
  15914. (and (setq fmt (plist-get org-time-clocksum-format :days))
  15915. (or (> (setq n (/ (truncate m) (* d h))) 0)
  15916. (plist-get org-time-clocksum-format :require-days))
  15917. (setq clocksum (concat clocksum (format fmt n))
  15918. m (- m (* n d h))))
  15919. (and (setq fmt (plist-get org-time-clocksum-format :hours))
  15920. (or (> (setq n (/ (truncate m) h)) 0)
  15921. (plist-get org-time-clocksum-format :require-hours))
  15922. (setq clocksum (concat clocksum (format fmt n))
  15923. m (- m (* n h))))
  15924. (and (setq fmt (plist-get org-time-clocksum-format :minutes))
  15925. (or (> m 0) (plist-get org-time-clocksum-format :require-minutes))
  15926. (setq clocksum (concat clocksum (format fmt m))))
  15927. ;; return formatted time duration
  15928. clocksum))))
  15929. (defalias 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string)
  15930. (make-obsolete 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string
  15931. "Org mode version 8.0")
  15932. (defun org-hours-to-clocksum-string (n)
  15933. (org-minutes-to-clocksum-string (* n 60)))
  15934. (defun org-hh:mm-string-to-minutes (s)
  15935. "Convert a string H:MM to a number of minutes.
  15936. If the string is just a number, interpret it as minutes.
  15937. In fact, the first hh:mm or number in the string will be taken,
  15938. there can be extra stuff in the string.
  15939. If no number is found, the return value is 0."
  15940. (cond
  15941. ((integerp s) s)
  15942. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  15943. (+ (* (string-to-number (match-string 1 s)) 60)
  15944. (string-to-number (match-string 2 s))))
  15945. ((string-match "\\([0-9]+\\)" s)
  15946. (string-to-number (match-string 1 s)))
  15947. (t 0)))
  15948. (defcustom org-image-actual-width t
  15949. "Should we use the actual width of images when inlining them?
  15950. When set to `t', always use the image width.
  15951. When set to a number, use imagemagick (when available) to set
  15952. the image's width to this value.
  15953. When set to a number in a list, try to get the width from the
  15954. #+ATTR.* keyword if it matches a width specification like
  15955. width=\"[0-9]+\", and fall back on that number if none is found.
  15956. When set to nil, try to get the width from an #+ATTR.* keyword
  15957. and fall back on the original width if none is found.
  15958. This requires Emacs >= 24.1, build with imagemagick support."
  15959. :group 'org-appearance
  15960. :version "24.4"
  15961. :package-version '(Org . "8.0")
  15962. :type '(choice
  15963. (const :tag "Use the image width" t)
  15964. (integer :tag "Use a number of pixels")
  15965. (list :tag "Use #+ATTR* or a number of pixels" (integer))
  15966. (const :tag "Use #+ATTR* or don't resize" nil)))
  15967. (defcustom org-agenda-inhibit-startup nil
  15968. "Inhibit startup when preparing agenda buffers.
  15969. When this variable is `t' (the default), the initialization of
  15970. the Org agenda buffers is inhibited: e.g. the visibility state
  15971. is not set, the tables are not re-aligned, etc."
  15972. :type 'boolean
  15973. :version "24.3"
  15974. :group 'org-agenda)
  15975. (defun org-duration-string-to-minutes (s &optional output-to-string)
  15976. "Convert a duration string S to minutes.
  15977. A bare number is interpreted as minutes, modifiers can be set by
  15978. customizing `org-effort-durations' (which see).
  15979. Entries containing a colon are interpreted as H:MM by
  15980. `org-hh:mm-string-to-minutes'."
  15981. (let ((result 0)
  15982. (re (concat "\\([0-9.]+\\) *\\("
  15983. (regexp-opt (mapcar 'car org-effort-durations))
  15984. "\\)")))
  15985. (while (string-match re s)
  15986. (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  15987. (string-to-number (match-string 1 s))))
  15988. (setq s (replace-match "" nil t s)))
  15989. (setq result (floor result))
  15990. (incf result (org-hh:mm-string-to-minutes s))
  15991. (if output-to-string (number-to-string result) result)))
  15992. ;;;; Files
  15993. (defun org-save-all-org-buffers ()
  15994. "Save all Org-mode buffers without user confirmation."
  15995. (interactive)
  15996. (message "Saving all Org-mode buffers...")
  15997. (save-some-buffers t (lambda () (derived-mode-p 'org-mode)))
  15998. (when (featurep 'org-id) (org-id-locations-save))
  15999. (message "Saving all Org-mode buffers... done"))
  16000. (defun org-revert-all-org-buffers ()
  16001. "Revert all Org-mode buffers.
  16002. Prompt for confirmation when there are unsaved changes.
  16003. Be sure you know what you are doing before letting this function
  16004. overwrite your changes.
  16005. This function is useful in a setup where one tracks org files
  16006. with a version control system, to revert on one machine after pulling
  16007. changes from another. I believe the procedure must be like this:
  16008. 1. M-x org-save-all-org-buffers
  16009. 2. Pull changes from the other machine, resolve conflicts
  16010. 3. M-x org-revert-all-org-buffers"
  16011. (interactive)
  16012. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  16013. (error "Abort"))
  16014. (save-excursion
  16015. (save-window-excursion
  16016. (mapc
  16017. (lambda (b)
  16018. (when (and (with-current-buffer b (derived-mode-p 'org-mode))
  16019. (with-current-buffer b buffer-file-name))
  16020. (org-pop-to-buffer-same-window b)
  16021. (revert-buffer t 'no-confirm)))
  16022. (buffer-list))
  16023. (when (and (featurep 'org-id) org-id-track-globally)
  16024. (org-id-locations-load)))))
  16025. ;;;; Agenda files
  16026. ;;;###autoload
  16027. (defun org-switchb (&optional arg)
  16028. "Switch between Org buffers.
  16029. With one prefix argument, restrict available buffers to files.
  16030. With two prefix arguments, restrict available buffers to agenda files.
  16031. Defaults to `iswitchb' for buffer name completion.
  16032. Set `org-completion-use-ido' to make it use ido instead."
  16033. (interactive "P")
  16034. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  16035. ((equal arg '(16)) (org-buffer-list 'agenda))
  16036. (t (org-buffer-list))))
  16037. (org-completion-use-iswitchb org-completion-use-iswitchb)
  16038. (org-completion-use-ido org-completion-use-ido))
  16039. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  16040. (setq org-completion-use-iswitchb t))
  16041. (org-pop-to-buffer-same-window
  16042. (org-icompleting-read "Org buffer: "
  16043. (mapcar 'list (mapcar 'buffer-name blist))
  16044. nil t))))
  16045. ;;; Define some older names previously used for this functionality
  16046. ;;;###autoload
  16047. (defalias 'org-ido-switchb 'org-switchb)
  16048. ;;;###autoload
  16049. (defalias 'org-iswitchb 'org-switchb)
  16050. (defun org-buffer-list (&optional predicate exclude-tmp)
  16051. "Return a list of Org buffers.
  16052. PREDICATE can be `export', `files' or `agenda'.
  16053. export restrict the list to Export buffers.
  16054. files restrict the list to buffers visiting Org files.
  16055. agenda restrict the list to buffers visiting agenda files.
  16056. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  16057. (let* ((bfn nil)
  16058. (agenda-files (and (eq predicate 'agenda)
  16059. (mapcar 'file-truename (org-agenda-files t))))
  16060. (filter
  16061. (cond
  16062. ((eq predicate 'files)
  16063. (lambda (b) (with-current-buffer b (derived-mode-p 'org-mode))))
  16064. ((eq predicate 'export)
  16065. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  16066. ((eq predicate 'agenda)
  16067. (lambda (b)
  16068. (with-current-buffer b
  16069. (and (derived-mode-p 'org-mode)
  16070. (setq bfn (buffer-file-name b))
  16071. (member (file-truename bfn) agenda-files)))))
  16072. (t (lambda (b) (with-current-buffer b
  16073. (or (derived-mode-p 'org-mode)
  16074. (string-match "\*Org .*Export"
  16075. (buffer-name b)))))))))
  16076. (delq nil
  16077. (mapcar
  16078. (lambda(b)
  16079. (if (and (funcall filter b)
  16080. (or (not exclude-tmp)
  16081. (not (string-match "tmp" (buffer-name b)))))
  16082. b
  16083. nil))
  16084. (buffer-list)))))
  16085. (defun org-agenda-files (&optional unrestricted archives)
  16086. "Get the list of agenda files.
  16087. Optional UNRESTRICTED means return the full list even if a restriction
  16088. is currently in place.
  16089. When ARCHIVES is t, include all archive files that are really being
  16090. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  16091. `org-agenda-archives-mode' is t."
  16092. (let ((files
  16093. (cond
  16094. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  16095. ((stringp org-agenda-files) (org-read-agenda-file-list))
  16096. ((listp org-agenda-files) org-agenda-files)
  16097. (t (error "Invalid value of `org-agenda-files'")))))
  16098. (setq files (apply 'append
  16099. (mapcar (lambda (f)
  16100. (if (file-directory-p f)
  16101. (directory-files
  16102. f t org-agenda-file-regexp)
  16103. (list f)))
  16104. files)))
  16105. (when org-agenda-skip-unavailable-files
  16106. (setq files (delq nil
  16107. (mapcar (function
  16108. (lambda (file)
  16109. (and (file-readable-p file) file)))
  16110. files))))
  16111. (when (or (eq archives t)
  16112. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  16113. (setq files (org-add-archive-files files)))
  16114. files))
  16115. (defun org-agenda-file-p (&optional file)
  16116. "Return non-nil, if FILE is an agenda file.
  16117. If FILE is omitted, use the file associated with the current
  16118. buffer."
  16119. (member (or file (buffer-file-name))
  16120. (org-agenda-files t)))
  16121. (defun org-edit-agenda-file-list ()
  16122. "Edit the list of agenda files.
  16123. Depending on setup, this either uses customize to edit the variable
  16124. `org-agenda-files', or it visits the file that is holding the list. In the
  16125. latter case, the buffer is set up in a way that saving it automatically kills
  16126. the buffer and restores the previous window configuration."
  16127. (interactive)
  16128. (if (stringp org-agenda-files)
  16129. (let ((cw (current-window-configuration)))
  16130. (find-file org-agenda-files)
  16131. (org-set-local 'org-window-configuration cw)
  16132. (org-add-hook 'after-save-hook
  16133. (lambda ()
  16134. (set-window-configuration
  16135. (prog1 org-window-configuration
  16136. (kill-buffer (current-buffer))))
  16137. (org-install-agenda-files-menu)
  16138. (message "New agenda file list installed"))
  16139. nil 'local)
  16140. (message "%s" (substitute-command-keys
  16141. "Edit list and finish with \\[save-buffer]")))
  16142. (customize-variable 'org-agenda-files)))
  16143. (defun org-store-new-agenda-file-list (list)
  16144. "Set new value for the agenda file list and save it correctly."
  16145. (if (stringp org-agenda-files)
  16146. (let ((fe (org-read-agenda-file-list t)) b u)
  16147. (while (setq b (find-buffer-visiting org-agenda-files))
  16148. (kill-buffer b))
  16149. (with-temp-file org-agenda-files
  16150. (insert
  16151. (mapconcat
  16152. (lambda (f) ;; Keep un-expanded entries.
  16153. (if (setq u (assoc f fe))
  16154. (cdr u)
  16155. f))
  16156. list "\n")
  16157. "\n")))
  16158. (let ((org-mode-hook nil) (org-inhibit-startup t)
  16159. (org-insert-mode-line-in-empty-file nil))
  16160. (setq org-agenda-files list)
  16161. (customize-save-variable 'org-agenda-files org-agenda-files))))
  16162. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  16163. "Read the list of agenda files from a file.
  16164. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  16165. filenames, used by `org-store-new-agenda-file-list' to write back
  16166. un-expanded file names."
  16167. (when (file-directory-p org-agenda-files)
  16168. (error "`org-agenda-files' cannot be a single directory"))
  16169. (when (stringp org-agenda-files)
  16170. (with-temp-buffer
  16171. (insert-file-contents org-agenda-files)
  16172. (mapcar
  16173. (lambda (f)
  16174. (let ((e (expand-file-name (substitute-in-file-name f)
  16175. org-directory)))
  16176. (if pair-with-expansion
  16177. (cons e f)
  16178. e)))
  16179. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  16180. ;;;###autoload
  16181. (defun org-cycle-agenda-files ()
  16182. "Cycle through the files in `org-agenda-files'.
  16183. If the current buffer visits an agenda file, find the next one in the list.
  16184. If the current buffer does not, find the first agenda file."
  16185. (interactive)
  16186. (let* ((fs (org-agenda-files t))
  16187. (files (append fs (list (car fs))))
  16188. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  16189. file)
  16190. (unless files (error "No agenda files"))
  16191. (catch 'exit
  16192. (while (setq file (pop files))
  16193. (if (equal (file-truename file) tcf)
  16194. (when (car files)
  16195. (find-file (car files))
  16196. (throw 'exit t))))
  16197. (find-file (car fs)))
  16198. (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
  16199. (defun org-agenda-file-to-front (&optional to-end)
  16200. "Move/add the current file to the top of the agenda file list.
  16201. If the file is not present in the list, it is added to the front. If it is
  16202. present, it is moved there. With optional argument TO-END, add/move to the
  16203. end of the list."
  16204. (interactive "P")
  16205. (let ((org-agenda-skip-unavailable-files nil)
  16206. (file-alist (mapcar (lambda (x)
  16207. (cons (file-truename x) x))
  16208. (org-agenda-files t)))
  16209. (ctf (file-truename
  16210. (or buffer-file-name
  16211. (error "Please save the current buffer to a file"))))
  16212. x had)
  16213. (setq x (assoc ctf file-alist) had x)
  16214. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  16215. (if to-end
  16216. (setq file-alist (append (delq x file-alist) (list x)))
  16217. (setq file-alist (cons x (delq x file-alist))))
  16218. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  16219. (org-install-agenda-files-menu)
  16220. (message "File %s to %s of agenda file list"
  16221. (if had "moved" "added") (if to-end "end" "front"))))
  16222. (defun org-remove-file (&optional file)
  16223. "Remove current file from the list of files in variable `org-agenda-files'.
  16224. These are the files which are being checked for agenda entries.
  16225. Optional argument FILE means use this file instead of the current."
  16226. (interactive)
  16227. (let* ((org-agenda-skip-unavailable-files nil)
  16228. (file (or file buffer-file-name
  16229. (error "Current buffer does not visit a file")))
  16230. (true-file (file-truename file))
  16231. (afile (abbreviate-file-name file))
  16232. (files (delq nil (mapcar
  16233. (lambda (x)
  16234. (if (equal true-file
  16235. (file-truename x))
  16236. nil x))
  16237. (org-agenda-files t)))))
  16238. (if (not (= (length files) (length (org-agenda-files t))))
  16239. (progn
  16240. (org-store-new-agenda-file-list files)
  16241. (org-install-agenda-files-menu)
  16242. (message "Removed file: %s" afile))
  16243. (message "File was not in list: %s (not removed)" afile))))
  16244. (defun org-file-menu-entry (file)
  16245. (vector file (list 'find-file file) t))
  16246. (defun org-check-agenda-file (file)
  16247. "Make sure FILE exists. If not, ask user what to do."
  16248. (when (not (file-exists-p file))
  16249. (message "Non-existent agenda file %s. [R]emove from list or [A]bort?"
  16250. (abbreviate-file-name file))
  16251. (let ((r (downcase (read-char-exclusive))))
  16252. (cond
  16253. ((equal r ?r)
  16254. (org-remove-file file)
  16255. (throw 'nextfile t))
  16256. (t (error "Abort"))))))
  16257. (defun org-get-agenda-file-buffer (file)
  16258. "Get a buffer visiting FILE. If the buffer needs to be created, add
  16259. it to the list of buffers which might be released later."
  16260. (let ((buf (org-find-base-buffer-visiting file)))
  16261. (if buf
  16262. buf ; just return it
  16263. ;; Make a new buffer and remember it
  16264. (setq buf (find-file-noselect file))
  16265. (if buf (push buf org-agenda-new-buffers))
  16266. buf)))
  16267. (defun org-release-buffers (blist)
  16268. "Release all buffers in list, asking the user for confirmation when needed.
  16269. When a buffer is unmodified, it is just killed. When modified, it is saved
  16270. \(if the user agrees) and then killed."
  16271. (let (buf file)
  16272. (while (setq buf (pop blist))
  16273. (setq file (buffer-file-name buf))
  16274. (when (and (buffer-modified-p buf)
  16275. file
  16276. (y-or-n-p (format "Save file %s? " file)))
  16277. (with-current-buffer buf (save-buffer)))
  16278. (kill-buffer buf))))
  16279. (defun org-agenda-prepare-buffers (files)
  16280. "Create buffers for all agenda files, protect archived trees and comments."
  16281. (interactive)
  16282. (let ((pa '(:org-archived t))
  16283. (pc '(:org-comment t))
  16284. (pall '(:org-archived t :org-comment t))
  16285. (inhibit-read-only t)
  16286. (org-inhibit-startup org-agenda-inhibit-startup)
  16287. (rea (concat ":" org-archive-tag ":"))
  16288. file re)
  16289. (save-excursion
  16290. (save-restriction
  16291. (while (setq file (pop files))
  16292. (catch 'nextfile
  16293. (if (bufferp file)
  16294. (set-buffer file)
  16295. (org-check-agenda-file file)
  16296. (set-buffer (org-get-agenda-file-buffer file)))
  16297. (widen)
  16298. (org-refresh-category-properties)
  16299. (org-refresh-properties org-effort-property 'org-effort)
  16300. (org-refresh-properties "APPT_WARNTIME" 'org-appt-warntime)
  16301. (setq org-todo-keywords-for-agenda
  16302. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  16303. (setq org-done-keywords-for-agenda
  16304. (append org-done-keywords-for-agenda org-done-keywords))
  16305. (setq org-todo-keyword-alist-for-agenda
  16306. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  16307. (setq org-drawers-for-agenda
  16308. (append org-drawers-for-agenda org-drawers))
  16309. (setq org-tag-alist-for-agenda
  16310. (append org-tag-alist-for-agenda org-tag-alist))
  16311. (org-with-silent-modifications
  16312. (save-excursion
  16313. (remove-text-properties (point-min) (point-max) pall)
  16314. (when org-agenda-skip-archived-trees
  16315. (goto-char (point-min))
  16316. (while (re-search-forward rea nil t)
  16317. (if (org-at-heading-p t)
  16318. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  16319. (goto-char (point-min))
  16320. (setq re (format org-heading-keyword-regexp-format
  16321. org-comment-string))
  16322. (while (re-search-forward re nil t)
  16323. (add-text-properties
  16324. (match-beginning 0) (org-end-of-subtree t) pc))))))))
  16325. (setq org-todo-keywords-for-agenda
  16326. (org-uniquify org-todo-keywords-for-agenda))
  16327. (setq org-todo-keyword-alist-for-agenda
  16328. (org-uniquify org-todo-keyword-alist-for-agenda)
  16329. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  16330. ;;;; CDLaTeX minor mode
  16331. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  16332. "Keymap for the minor `org-cdlatex-mode'.")
  16333. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  16334. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  16335. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  16336. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  16337. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  16338. (defvar org-cdlatex-texmathp-advice-is-done nil
  16339. "Flag remembering if we have applied the advice to texmathp already.")
  16340. (define-minor-mode org-cdlatex-mode
  16341. "Toggle the minor `org-cdlatex-mode'.
  16342. This mode supports entering LaTeX environment and math in LaTeX fragments
  16343. in Org-mode.
  16344. \\{org-cdlatex-mode-map}"
  16345. nil " OCDL" nil
  16346. (when org-cdlatex-mode
  16347. (require 'cdlatex)
  16348. (run-hooks 'cdlatex-mode-hook)
  16349. (cdlatex-compute-tables))
  16350. (unless org-cdlatex-texmathp-advice-is-done
  16351. (setq org-cdlatex-texmathp-advice-is-done t)
  16352. (defadvice texmathp (around org-math-always-on activate)
  16353. "Always return t in org-mode buffers.
  16354. This is because we want to insert math symbols without dollars even outside
  16355. the LaTeX math segments. If Orgmode thinks that point is actually inside
  16356. an embedded LaTeX fragment, let texmathp do its job.
  16357. \\[org-cdlatex-mode-map]"
  16358. (interactive)
  16359. (let (p)
  16360. (cond
  16361. ((not (derived-mode-p 'org-mode)) ad-do-it)
  16362. ((eq this-command 'cdlatex-math-symbol)
  16363. (setq ad-return-value t
  16364. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  16365. (t
  16366. (let ((p (org-inside-LaTeX-fragment-p)))
  16367. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  16368. (setq ad-return-value t
  16369. texmathp-why '("Org-mode embedded math" . 0))
  16370. (if p ad-do-it)))))))))
  16371. (defun turn-on-org-cdlatex ()
  16372. "Unconditionally turn on `org-cdlatex-mode'."
  16373. (org-cdlatex-mode 1))
  16374. (defun org-try-cdlatex-tab ()
  16375. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  16376. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  16377. - inside a LaTeX fragment, or
  16378. - after the first word in a line, where an abbreviation expansion could
  16379. insert a LaTeX environment."
  16380. (when org-cdlatex-mode
  16381. (cond
  16382. ;; Before any word on the line: No expansion possible.
  16383. ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
  16384. ;; Just after first word on the line: Expand it. Make sure it
  16385. ;; cannot happen on headlines, though.
  16386. ((save-excursion
  16387. (skip-chars-backward "a-zA-Z0-9*")
  16388. (skip-chars-backward " \t")
  16389. (and (bolp) (not (org-at-heading-p))))
  16390. (cdlatex-tab) t)
  16391. ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
  16392. (defun org-cdlatex-underscore-caret (&optional arg)
  16393. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  16394. Revert to the normal definition outside of these fragments."
  16395. (interactive "P")
  16396. (if (org-inside-LaTeX-fragment-p)
  16397. (call-interactively 'cdlatex-sub-superscript)
  16398. (let (org-cdlatex-mode)
  16399. (call-interactively (key-binding (vector last-input-event))))))
  16400. (defun org-cdlatex-math-modify (&optional arg)
  16401. "Execute `cdlatex-math-modify' in LaTeX fragments.
  16402. Revert to the normal definition outside of these fragments."
  16403. (interactive "P")
  16404. (if (org-inside-LaTeX-fragment-p)
  16405. (call-interactively 'cdlatex-math-modify)
  16406. (let (org-cdlatex-mode)
  16407. (call-interactively (key-binding (vector last-input-event))))))
  16408. ;;;; LaTeX fragments
  16409. (defvar org-latex-regexps
  16410. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  16411. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  16412. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  16413. ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  16414. ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  16415. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  16416. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  16417. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  16418. "Regular expressions for matching embedded LaTeX.")
  16419. (defun org-inside-LaTeX-fragment-p ()
  16420. "Test if point is inside a LaTeX fragment.
  16421. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  16422. sequence appearing also before point.
  16423. Even though the matchers for math are configurable, this function assumes
  16424. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  16425. delimiters are skipped when they have been removed by customization.
  16426. The return value is nil, or a cons cell with the delimiter and the
  16427. position of this delimiter.
  16428. This function does a reasonably good job, but can locally be fooled by
  16429. for example currency specifications. For example it will assume being in
  16430. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  16431. fragments that are properly closed, but during editing, we have to live
  16432. with the uncertainty caused by missing closing delimiters. This function
  16433. looks only before point, not after."
  16434. (catch 'exit
  16435. (let ((pos (point))
  16436. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  16437. (lim (progn
  16438. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  16439. (point)))
  16440. dd-on str (start 0) m re)
  16441. (goto-char pos)
  16442. (when dodollar
  16443. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  16444. re (nth 1 (assoc "$" org-latex-regexps)))
  16445. (while (string-match re str start)
  16446. (cond
  16447. ((= (match-end 0) (length str))
  16448. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  16449. ((= (match-end 0) (- (length str) 5))
  16450. (throw 'exit nil))
  16451. (t (setq start (match-end 0))))))
  16452. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  16453. (goto-char pos)
  16454. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  16455. (and (match-beginning 2) (throw 'exit nil))
  16456. ;; count $$
  16457. (while (re-search-backward "\\$\\$" lim t)
  16458. (setq dd-on (not dd-on)))
  16459. (goto-char pos)
  16460. (if dd-on (cons "$$" m))))))
  16461. (defun org-inside-latex-macro-p ()
  16462. "Is point inside a LaTeX macro or its arguments?"
  16463. (save-match-data
  16464. (org-in-regexp
  16465. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  16466. (defvar org-latex-fragment-image-overlays nil
  16467. "List of overlays carrying the images of latex fragments.")
  16468. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  16469. (defun org-remove-latex-fragment-image-overlays ()
  16470. "Remove all overlays with LaTeX fragment images in current buffer."
  16471. (mapc 'delete-overlay org-latex-fragment-image-overlays)
  16472. (setq org-latex-fragment-image-overlays nil))
  16473. (defun org-preview-latex-fragment (&optional subtree)
  16474. "Preview the LaTeX fragment at point, or all locally or globally.
  16475. If the cursor is in a LaTeX fragment, create the image and overlay
  16476. it over the source code. If there is no fragment at point, display
  16477. all fragments in the current text, from one headline to the next. With
  16478. prefix SUBTREE, display all fragments in the current subtree. With a
  16479. double prefix arg \\[universal-argument] \\[universal-argument], or when \
  16480. the cursor is before the first headline,
  16481. display all fragments in the buffer.
  16482. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  16483. (interactive "P")
  16484. (unless buffer-file-name
  16485. (error "Can't preview LaTeX fragment in a non-file buffer"))
  16486. (org-remove-latex-fragment-image-overlays)
  16487. (save-excursion
  16488. (save-restriction
  16489. (let (beg end at msg)
  16490. (cond
  16491. ((or (equal subtree '(16))
  16492. (not (save-excursion
  16493. (re-search-backward org-outline-regexp-bol nil t))))
  16494. (setq beg (point-min) end (point-max)
  16495. msg "Creating images for buffer...%s"))
  16496. ((equal subtree '(4))
  16497. (org-back-to-heading)
  16498. (setq beg (point) end (org-end-of-subtree t)
  16499. msg "Creating images for subtree...%s"))
  16500. (t
  16501. (if (setq at (org-inside-LaTeX-fragment-p))
  16502. (goto-char (max (point-min) (- (cdr at) 2)))
  16503. (org-back-to-heading))
  16504. (setq beg (point) end (progn (outline-next-heading) (point))
  16505. msg (if at "Creating image...%s"
  16506. "Creating images for entry...%s"))))
  16507. (message msg "")
  16508. (narrow-to-region beg end)
  16509. (goto-char beg)
  16510. (org-format-latex
  16511. (concat org-latex-preview-ltxpng-directory (file-name-sans-extension
  16512. (file-name-nondirectory
  16513. buffer-file-name)))
  16514. default-directory 'overlays msg at 'forbuffer
  16515. org-latex-create-formula-image-program)
  16516. (message msg "done. Use `C-c C-c' to remove images.")))))
  16517. (defun org-format-latex (prefix &optional dir overlays msg at
  16518. forbuffer processing-type)
  16519. "Replace LaTeX fragments with links to an image, and produce images.
  16520. Some of the options can be changed using the variable
  16521. `org-format-latex-options'."
  16522. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  16523. (let* ((prefixnodir (file-name-nondirectory prefix))
  16524. (absprefix (expand-file-name prefix dir))
  16525. (todir (file-name-directory absprefix))
  16526. (opt org-format-latex-options)
  16527. (optnew org-format-latex-options)
  16528. (matchers (plist-get opt :matchers))
  16529. (re-list org-latex-regexps)
  16530. (cnt 0) txt hash link beg end re e checkdir
  16531. string
  16532. m n block-type block linkfile movefile ov)
  16533. ;; Check the different regular expressions
  16534. (while (setq e (pop re-list))
  16535. (setq m (car e) re (nth 1 e) n (nth 2 e) block-type (nth 3 e)
  16536. block (if block-type "\n\n" ""))
  16537. (when (member m matchers)
  16538. (goto-char (point-min))
  16539. (while (re-search-forward re nil t)
  16540. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  16541. (or (not overlays)
  16542. (not (eq (get-char-property (match-beginning n)
  16543. 'org-overlay-type)
  16544. 'org-latex-overlay))))
  16545. (cond
  16546. ((eq processing-type 'verbatim))
  16547. ((eq processing-type 'mathjax)
  16548. ;; Prepare for MathJax processing.
  16549. (setq string (match-string n))
  16550. (when (member m '("$" "$1"))
  16551. (save-excursion
  16552. (delete-region (match-beginning n) (match-end n))
  16553. (goto-char (match-beginning n))
  16554. (insert (concat "\\(" (substring string 1 -1) "\\)")))))
  16555. ((or (eq processing-type 'dvipng)
  16556. (eq processing-type 'imagemagick))
  16557. ;; Process to an image.
  16558. (setq txt (match-string n)
  16559. beg (match-beginning n) end (match-end n)
  16560. cnt (1+ cnt))
  16561. (let ((face (face-at-point))
  16562. (fg (plist-get opt :foreground))
  16563. (bg (plist-get opt :background))
  16564. ;; Ensure full list is printed.
  16565. print-length print-level)
  16566. (when forbuffer
  16567. ;; Get the colors from the face at point.
  16568. (goto-char beg)
  16569. (when (eq fg 'auto)
  16570. (setq fg (face-attribute face :foreground nil 'default)))
  16571. (when (eq bg 'auto)
  16572. (setq bg (face-attribute face :background nil 'default)))
  16573. (setq optnew (copy-sequence opt))
  16574. (plist-put optnew :foreground fg)
  16575. (plist-put optnew :background bg))
  16576. (setq hash (sha1 (prin1-to-string
  16577. (list org-format-latex-header
  16578. org-latex-default-packages-alist
  16579. org-latex-packages-alist
  16580. org-format-latex-options
  16581. forbuffer txt fg bg)))
  16582. linkfile (format "%s_%s.png" prefix hash)
  16583. movefile (format "%s_%s.png" absprefix hash)))
  16584. (setq link (concat block "[[file:" linkfile "]]" block))
  16585. (if msg (message msg cnt))
  16586. (goto-char beg)
  16587. (unless checkdir ; Ensure the directory exists.
  16588. (setq checkdir t)
  16589. (or (file-directory-p todir) (make-directory todir t)))
  16590. (unless (file-exists-p movefile)
  16591. (org-create-formula-image
  16592. txt movefile optnew forbuffer processing-type))
  16593. (if overlays
  16594. (progn
  16595. (mapc (lambda (o)
  16596. (if (eq (overlay-get o 'org-overlay-type)
  16597. 'org-latex-overlay)
  16598. (delete-overlay o)))
  16599. (overlays-in beg end))
  16600. (setq ov (make-overlay beg end))
  16601. (overlay-put ov 'org-overlay-type 'org-latex-overlay)
  16602. (if (featurep 'xemacs)
  16603. (progn
  16604. (overlay-put ov 'invisible t)
  16605. (overlay-put
  16606. ov 'end-glyph
  16607. (make-glyph (vector 'png :file movefile))))
  16608. (overlay-put
  16609. ov 'display
  16610. (list 'image :type 'png :file movefile :ascent 'center)))
  16611. (push ov org-latex-fragment-image-overlays)
  16612. (goto-char end))
  16613. (delete-region beg end)
  16614. (insert (org-add-props link
  16615. (list 'org-latex-src
  16616. (replace-regexp-in-string
  16617. "\"" "" txt)
  16618. 'org-latex-src-embed-type
  16619. (if block-type 'paragraph 'character))))))
  16620. ((eq processing-type 'mathml)
  16621. ;; Process to MathML
  16622. (unless (save-match-data (org-format-latex-mathml-available-p))
  16623. (error "LaTeX to MathML converter not configured"))
  16624. (setq txt (match-string n)
  16625. beg (match-beginning n) end (match-end n)
  16626. cnt (1+ cnt))
  16627. (if msg (message msg cnt))
  16628. (goto-char beg)
  16629. (delete-region beg end)
  16630. (insert (org-format-latex-as-mathml
  16631. txt block-type prefix dir)))
  16632. (t
  16633. (error "Unknown conversion type %s for latex fragments"
  16634. processing-type)))))))))
  16635. (defun org-create-math-formula (latex-frag &optional mathml-file)
  16636. "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
  16637. Use `org-latex-to-mathml-convert-command'. If the conversion is
  16638. sucessful, return the portion between \"<math...> </math>\"
  16639. elements otherwise return nil. When MATHML-FILE is specified,
  16640. write the results in to that file. When invoked as an
  16641. interactive command, prompt for LATEX-FRAG, with initial value
  16642. set to the current active region and echo the results for user
  16643. inspection."
  16644. (interactive (list (let ((frag (when (org-region-active-p)
  16645. (buffer-substring-no-properties
  16646. (region-beginning) (region-end)))))
  16647. (read-string "LaTeX Fragment: " frag nil frag))))
  16648. (unless latex-frag (error "Invalid latex-frag"))
  16649. (let* ((tmp-in-file (file-relative-name
  16650. (make-temp-name (expand-file-name "ltxmathml-in"))))
  16651. (ignore (write-region latex-frag nil tmp-in-file))
  16652. (tmp-out-file (file-relative-name
  16653. (make-temp-name (expand-file-name "ltxmathml-out"))))
  16654. (cmd (format-spec
  16655. org-latex-to-mathml-convert-command
  16656. `((?j . ,(shell-quote-argument
  16657. (expand-file-name org-latex-to-mathml-jar-file)))
  16658. (?I . ,(shell-quote-argument tmp-in-file))
  16659. (?o . ,(shell-quote-argument tmp-out-file)))))
  16660. mathml shell-command-output)
  16661. (when (org-called-interactively-p 'any)
  16662. (unless (org-format-latex-mathml-available-p)
  16663. (error "LaTeX to MathML converter not configured")))
  16664. (message "Running %s" cmd)
  16665. (setq shell-command-output (shell-command-to-string cmd))
  16666. (setq mathml
  16667. (when (file-readable-p tmp-out-file)
  16668. (with-current-buffer (find-file-noselect tmp-out-file t)
  16669. (goto-char (point-min))
  16670. (when (re-search-forward
  16671. (concat
  16672. (regexp-quote
  16673. "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">")
  16674. "\\(.\\|\n\\)*"
  16675. (regexp-quote "</math>")) nil t)
  16676. (prog1 (match-string 0) (kill-buffer))))))
  16677. (cond
  16678. (mathml
  16679. (setq mathml
  16680. (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
  16681. (when mathml-file
  16682. (write-region mathml nil mathml-file))
  16683. (when (org-called-interactively-p 'any)
  16684. (message mathml)))
  16685. ((message "LaTeX to MathML conversion failed")
  16686. (message shell-command-output)))
  16687. (delete-file tmp-in-file)
  16688. (when (file-exists-p tmp-out-file)
  16689. (delete-file tmp-out-file))
  16690. mathml))
  16691. (defun org-format-latex-as-mathml (latex-frag latex-frag-type
  16692. prefix &optional dir)
  16693. "Use `org-create-math-formula' but check local cache first."
  16694. (let* ((absprefix (expand-file-name prefix dir))
  16695. (print-length nil) (print-level nil)
  16696. (formula-id (concat
  16697. "formula-"
  16698. (sha1
  16699. (prin1-to-string
  16700. (list latex-frag
  16701. org-latex-to-mathml-convert-command)))))
  16702. (formula-cache (format "%s-%s.mathml" absprefix formula-id))
  16703. (formula-cache-dir (file-name-directory formula-cache)))
  16704. (unless (file-directory-p formula-cache-dir)
  16705. (make-directory formula-cache-dir t))
  16706. (unless (file-exists-p formula-cache)
  16707. (org-create-math-formula latex-frag formula-cache))
  16708. (if (file-exists-p formula-cache)
  16709. ;; Successful conversion. Return the link to MathML file.
  16710. (org-add-props
  16711. (format "[[file:%s]]" (file-relative-name formula-cache dir))
  16712. (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
  16713. 'org-latex-src-embed-type (if latex-frag-type
  16714. 'paragraph 'character)))
  16715. ;; Failed conversion. Return the LaTeX fragment verbatim
  16716. latex-frag)))
  16717. (defun org-create-formula-image (string tofile options buffer &optional type)
  16718. "Create an image from LaTeX source using dvipng or convert.
  16719. This function calls either `org-create-formula-image-with-dvipng'
  16720. or `org-create-formula-image-with-imagemagick' depending on the
  16721. value of `org-latex-create-formula-image-program' or on the value
  16722. of the optional TYPE variable.
  16723. Note: ultimately these two function should be combined as they
  16724. share a good deal of logic."
  16725. (org-check-external-command
  16726. "latex" "needed to convert LaTeX fragments to images")
  16727. (funcall
  16728. (case (or type org-latex-create-formula-image-program)
  16729. ('dvipng
  16730. (org-check-external-command
  16731. "dvipng" "needed to convert LaTeX fragments to images")
  16732. #'org-create-formula-image-with-dvipng)
  16733. ('imagemagick
  16734. (org-check-external-command
  16735. "convert" "you need to install imagemagick")
  16736. #'org-create-formula-image-with-imagemagick)
  16737. (t (error
  16738. "invalid value of `org-latex-create-formula-image-program'")))
  16739. string tofile options buffer))
  16740. (declare-function org-export--get-global-options "ox" (&optional backend))
  16741. (declare-function org-export--get-inbuffer-options "ox" (&optional backend))
  16742. (defun org-create-formula--latex-header ()
  16743. "Return LaTeX header appropriate for previewing a LaTeX snippet."
  16744. (org-latex-guess-inputenc
  16745. (org-splice-latex-header
  16746. org-format-latex-header
  16747. org-latex-default-packages-alist
  16748. org-latex-packages-alist t
  16749. (plist-get
  16750. (org-combine-plists
  16751. (org-export--get-global-options 'latex)
  16752. (org-export--get-inbuffer-options 'latex))
  16753. :latex-header))))
  16754. ;; This function borrows from Ganesh Swami's latex2png.el
  16755. (defun org-create-formula-image-with-dvipng (string tofile options buffer)
  16756. "This calls dvipng."
  16757. (require 'ox-latex)
  16758. (let* ((tmpdir (if (featurep 'xemacs)
  16759. (temp-directory)
  16760. temporary-file-directory))
  16761. (texfilebase (make-temp-name
  16762. (expand-file-name "orgtex" tmpdir)))
  16763. (texfile (concat texfilebase ".tex"))
  16764. (dvifile (concat texfilebase ".dvi"))
  16765. (pngfile (concat texfilebase ".png"))
  16766. (fnh (if (featurep 'xemacs)
  16767. (font-height (face-font 'default))
  16768. (face-attribute 'default :height nil)))
  16769. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  16770. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  16771. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  16772. "Black"))
  16773. (bg (or (plist-get options (if buffer :background :html-background))
  16774. "Transparent")))
  16775. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground))
  16776. (unless (string= fg "Transparent") (setq fg (org-dvipng-color-format fg))))
  16777. (if (eq bg 'default) (setq bg (org-dvipng-color :background))
  16778. (unless (string= bg "Transparent") (setq bg (org-dvipng-color-format bg))))
  16779. (let ((latex-header (org-create-formula--latex-header)))
  16780. (with-temp-file texfile
  16781. (insert latex-header)
  16782. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")))
  16783. (let ((dir default-directory))
  16784. (condition-case nil
  16785. (progn
  16786. (cd tmpdir)
  16787. (call-process "latex" nil nil nil texfile))
  16788. (error nil))
  16789. (cd dir))
  16790. (if (not (file-exists-p dvifile))
  16791. (progn (message "Failed to create dvi file from %s" texfile) nil)
  16792. (condition-case nil
  16793. (if (featurep 'xemacs)
  16794. (call-process "dvipng" nil nil nil
  16795. "-fg" fg "-bg" bg
  16796. "-T" "tight"
  16797. "-o" pngfile
  16798. dvifile)
  16799. (call-process "dvipng" nil nil nil
  16800. "-fg" fg "-bg" bg
  16801. "-D" dpi
  16802. ;;"-x" scale "-y" scale
  16803. "-T" "tight"
  16804. "-o" pngfile
  16805. dvifile))
  16806. (error nil))
  16807. (if (not (file-exists-p pngfile))
  16808. (if org-format-latex-signal-error
  16809. (error "Failed to create png file from %s" texfile)
  16810. (message "Failed to create png file from %s" texfile)
  16811. nil)
  16812. ;; Use the requested file name and clean up
  16813. (copy-file pngfile tofile 'replace)
  16814. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png" ".out") do
  16815. (if (file-exists-p (concat texfilebase e))
  16816. (delete-file (concat texfilebase e))))
  16817. pngfile))))
  16818. (declare-function org-latex-compile "ox-latex" (texfile &optional snippet))
  16819. (defun org-create-formula-image-with-imagemagick (string tofile options buffer)
  16820. "This calls convert, which is included into imagemagick."
  16821. (require 'ox-latex)
  16822. (let* ((tmpdir (if (featurep 'xemacs)
  16823. (temp-directory)
  16824. temporary-file-directory))
  16825. (texfilebase (make-temp-name
  16826. (expand-file-name "orgtex" tmpdir)))
  16827. (texfile (concat texfilebase ".tex"))
  16828. (pdffile (concat texfilebase ".pdf"))
  16829. (pngfile (concat texfilebase ".png"))
  16830. (fnh (if (featurep 'xemacs)
  16831. (font-height (face-font 'default))
  16832. (face-attribute 'default :height nil)))
  16833. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  16834. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  16835. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  16836. "black"))
  16837. (bg (or (plist-get options (if buffer :background :html-background))
  16838. "white")))
  16839. (if (eq fg 'default) (setq fg (org-latex-color :foreground))
  16840. (setq fg (org-latex-color-format fg)))
  16841. (if (eq bg 'default) (setq bg (org-latex-color :background))
  16842. (setq bg (org-latex-color-format
  16843. (if (string= bg "Transparent") "white" bg))))
  16844. (let ((latex-header (org-create-formula--latex-header)))
  16845. (with-temp-file texfile
  16846. (insert latex-header)
  16847. (insert "\n\\begin{document}\n"
  16848. "\\definecolor{fg}{rgb}{" fg "}\n"
  16849. "\\definecolor{bg}{rgb}{" bg "}\n"
  16850. "\n\\pagecolor{bg}\n"
  16851. "\n{\\color{fg}\n"
  16852. string
  16853. "\n}\n"
  16854. "\n\\end{document}\n")))
  16855. (org-latex-compile texfile t)
  16856. (if (not (file-exists-p pdffile))
  16857. (progn (message "Failed to create pdf file from %s" texfile) nil)
  16858. (condition-case nil
  16859. (if (featurep 'xemacs)
  16860. (call-process "convert" nil nil nil
  16861. "-density" "96"
  16862. "-trim"
  16863. "-antialias"
  16864. pdffile
  16865. "-quality" "100"
  16866. ;; "-sharpen" "0x1.0"
  16867. pngfile)
  16868. (call-process "convert" nil nil nil
  16869. "-density" dpi
  16870. "-trim"
  16871. "-antialias"
  16872. pdffile
  16873. "-quality" "100"
  16874. ;; "-sharpen" "0x1.0"
  16875. pngfile))
  16876. (error nil))
  16877. (if (not (file-exists-p pngfile))
  16878. (if org-format-latex-signal-error
  16879. (error "Failed to create png file from %s" texfile)
  16880. (message "Failed to create png file from %s" texfile)
  16881. nil)
  16882. ;; Use the requested file name and clean up
  16883. (copy-file pngfile tofile 'replace)
  16884. (loop for e in '(".pdf" ".tex" ".aux" ".log" ".png") do
  16885. (if (file-exists-p (concat texfilebase e))
  16886. (delete-file (concat texfilebase e))))
  16887. pngfile))))
  16888. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  16889. "Fill a LaTeX header template TPL.
  16890. In the template, the following place holders will be recognized:
  16891. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  16892. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  16893. [PACKAGES] \\usepackage statements for PKG
  16894. [NO-PACKAGES] do not include PKG
  16895. [EXTRA] the string EXTRA
  16896. [NO-EXTRA] do not include EXTRA
  16897. For backward compatibility, if both the positive and the negative place
  16898. holder is missing, the positive one (without the \"NO-\") will be
  16899. assumed to be present at the end of the template.
  16900. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  16901. EXTRA is a string.
  16902. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  16903. (let (rpl (end ""))
  16904. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  16905. (setq rpl (if (or (match-end 1) (not def-pkg))
  16906. "" (org-latex-packages-to-string def-pkg snippets-p t))
  16907. tpl (replace-match rpl t t tpl))
  16908. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  16909. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  16910. (setq rpl (if (or (match-end 1) (not pkg))
  16911. "" (org-latex-packages-to-string pkg snippets-p t))
  16912. tpl (replace-match rpl t t tpl))
  16913. (if pkg (setq end
  16914. (concat end "\n"
  16915. (org-latex-packages-to-string pkg snippets-p)))))
  16916. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  16917. (setq rpl (if (or (match-end 1) (not extra))
  16918. "" (concat extra "\n"))
  16919. tpl (replace-match rpl t t tpl))
  16920. (if (and extra (string-match "\\S-" extra))
  16921. (setq end (concat end "\n" extra))))
  16922. (if (string-match "\\S-" end)
  16923. (concat tpl "\n" end)
  16924. tpl)))
  16925. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  16926. "Turn an alist of packages into a string with the \\usepackage macros."
  16927. (setq pkg (mapconcat (lambda(p)
  16928. (cond
  16929. ((stringp p) p)
  16930. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  16931. (format "%% Package %s omitted" (cadr p)))
  16932. ((equal "" (car p))
  16933. (format "\\usepackage{%s}" (cadr p)))
  16934. (t
  16935. (format "\\usepackage[%s]{%s}"
  16936. (car p) (cadr p)))))
  16937. pkg
  16938. "\n"))
  16939. (if newline (concat pkg "\n") pkg))
  16940. (defun org-dvipng-color (attr)
  16941. "Return a RGB color specification for dvipng."
  16942. (apply 'format "rgb %s %s %s"
  16943. (mapcar 'org-normalize-color
  16944. (if (featurep 'xemacs)
  16945. (color-rgb-components
  16946. (face-property 'default
  16947. (cond ((eq attr :foreground) 'foreground)
  16948. ((eq attr :background) 'background))))
  16949. (color-values (face-attribute 'default attr nil))))))
  16950. (defun org-dvipng-color-format (color-name)
  16951. "Convert COLOR-NAME to a RGB color value for dvipng."
  16952. (apply 'format "rgb %s %s %s"
  16953. (mapcar 'org-normalize-color
  16954. (color-values color-name))))
  16955. (defun org-latex-color (attr)
  16956. "Return a RGB color for the LaTeX color package."
  16957. (apply 'format "%s,%s,%s"
  16958. (mapcar 'org-normalize-color
  16959. (if (featurep 'xemacs)
  16960. (color-rgb-components
  16961. (face-property 'default
  16962. (cond ((eq attr :foreground) 'foreground)
  16963. ((eq attr :background) 'background))))
  16964. (color-values (face-attribute 'default attr nil))))))
  16965. (defun org-latex-color-format (color-name)
  16966. "Convert COLOR-NAME to a RGB color value."
  16967. (apply 'format "%s,%s,%s"
  16968. (mapcar 'org-normalize-color
  16969. (color-values color-name))))
  16970. (defun org-normalize-color (value)
  16971. "Return string to be used as color value for an RGB component."
  16972. (format "%g" (/ value 65535.0)))
  16973. ;; Image display
  16974. (defvar org-inline-image-overlays nil)
  16975. (make-variable-buffer-local 'org-inline-image-overlays)
  16976. (defun org-toggle-inline-images (&optional include-linked)
  16977. "Toggle the display of inline images.
  16978. INCLUDE-LINKED is passed to `org-display-inline-images'."
  16979. (interactive "P")
  16980. (if org-inline-image-overlays
  16981. (progn
  16982. (org-remove-inline-images)
  16983. (message "Inline image display turned off"))
  16984. (org-display-inline-images include-linked)
  16985. (if (and (org-called-interactively-p)
  16986. org-inline-image-overlays)
  16987. (message "%d images displayed inline"
  16988. (length org-inline-image-overlays))
  16989. (message "No images to display inline"))))
  16990. (defun org-redisplay-inline-images ()
  16991. "Refresh the display of inline images."
  16992. (interactive)
  16993. (if (not org-inline-image-overlays)
  16994. (org-toggle-inline-images)
  16995. (org-toggle-inline-images)
  16996. (org-toggle-inline-images)))
  16997. (defun org-display-inline-images (&optional include-linked refresh beg end)
  16998. "Display inline images.
  16999. Normally only links without a description part are inlined, because this
  17000. is how it will work for export. When INCLUDE-LINKED is set, also links
  17001. with a description part will be inlined. This can be nice for a quick
  17002. look at those images, but it does not reflect what exported files will look
  17003. like.
  17004. When REFRESH is set, refresh existing images between BEG and END.
  17005. This will create new image displays only if necessary.
  17006. BEG and END default to the buffer boundaries."
  17007. (interactive "P")
  17008. (unless refresh
  17009. (org-remove-inline-images)
  17010. (if (fboundp 'clear-image-cache) (clear-image-cache)))
  17011. (save-excursion
  17012. (save-restriction
  17013. (widen)
  17014. (setq beg (or beg (point-min)) end (or end (point-max)))
  17015. (goto-char beg)
  17016. (let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([^]\n]+?"
  17017. (substring (org-image-file-name-regexp) 0 -2)
  17018. "\\)\\]" (if include-linked "" "\\]")))
  17019. old file ov img type attrwidth width)
  17020. (while (re-search-forward re end t)
  17021. (setq old (get-char-property-and-overlay (match-beginning 1)
  17022. 'org-image-overlay)
  17023. file (expand-file-name
  17024. (concat (or (match-string 3) "") (match-string 4))))
  17025. (when (image-type-available-p 'imagemagick)
  17026. (setq attrwidth (if (or (listp org-image-actual-width)
  17027. (null org-image-actual-width))
  17028. (save-excursion
  17029. (save-match-data
  17030. (when (re-search-backward
  17031. "#\\+ATTR.*width=\"\\([^\"]+\\)\""
  17032. (save-excursion
  17033. (re-search-backward "^[ \t]*$\\|\\`" nil t)) t)
  17034. (string-to-number (match-string 1))))))
  17035. width (cond ((eq org-image-actual-width t) nil)
  17036. ((null org-image-actual-width) attrwidth)
  17037. ((numberp org-image-actual-width)
  17038. org-image-actual-width)
  17039. ((listp org-image-actual-width)
  17040. (or attrwidth (car org-image-actual-width))))
  17041. type (if width 'imagemagick)))
  17042. (when (file-exists-p file)
  17043. (if (and (car-safe old) refresh)
  17044. (image-refresh (overlay-get (cdr old) 'display))
  17045. (setq img (save-match-data (create-image file type nil :width width)))
  17046. (when img
  17047. (setq ov (make-overlay (match-beginning 0) (match-end 0)))
  17048. (overlay-put ov 'display img)
  17049. (overlay-put ov 'face 'default)
  17050. (overlay-put ov 'org-image-overlay t)
  17051. (overlay-put ov 'modification-hooks
  17052. (list 'org-display-inline-remove-overlay))
  17053. (push ov org-inline-image-overlays)))))))))
  17054. (define-obsolete-function-alias
  17055. 'org-display-inline-modification-hook 'org-display-inline-remove-overlay "24.3")
  17056. (defun org-display-inline-remove-overlay (ov after beg end &optional len)
  17057. "Remove inline-display overlay if a corresponding region is modified."
  17058. (let ((inhibit-modification-hooks t))
  17059. (when (and ov after)
  17060. (delete ov org-inline-image-overlays)
  17061. (delete-overlay ov))))
  17062. (defun org-remove-inline-images ()
  17063. "Remove inline display of images."
  17064. (interactive)
  17065. (mapc 'delete-overlay org-inline-image-overlays)
  17066. (setq org-inline-image-overlays nil))
  17067. ;;;; Key bindings
  17068. ;; Outline functions from `outline-mode-prefix-map'
  17069. ;; that can be remapped in Org:
  17070. (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
  17071. (define-key org-mode-map [remap show-subtree] 'org-show-subtree)
  17072. (define-key org-mode-map [remap outline-forward-same-level]
  17073. 'org-forward-heading-same-level)
  17074. (define-key org-mode-map [remap outline-backward-same-level]
  17075. 'org-backward-heading-same-level)
  17076. (define-key org-mode-map [remap show-branches]
  17077. 'org-kill-note-or-show-branches)
  17078. (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
  17079. (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
  17080. (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
  17081. ;; Outline functions from `outline-mode-prefix-map' that can not
  17082. ;; be remapped in Org:
  17083. ;;
  17084. ;; - the column "key binding" shows whether the Outline function is still
  17085. ;; available in Org mode on the same key that it has been bound to in
  17086. ;; Outline mode:
  17087. ;; - "overridden": key used for a different functionality in Org mode
  17088. ;; - else: key still bound to the same Outline function in Org mode
  17089. ;;
  17090. ;; | Outline function | key binding | Org replacement |
  17091. ;; |------------------------------------+-------------+-----------------------|
  17092. ;; | `outline-next-visible-heading' | `C-c C-n' | still same function |
  17093. ;; | `outline-previous-visible-heading' | `C-c C-p' | still same function |
  17094. ;; | `outline-up-heading' | `C-c C-u' | still same function |
  17095. ;; | `outline-move-subtree-up' | overridden | better: org-shiftup |
  17096. ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
  17097. ;; | `show-entry' | overridden | no replacement |
  17098. ;; | `show-children' | `C-c C-i' | visibility cycling |
  17099. ;; | `show-branches' | `C-c C-k' | still same function |
  17100. ;; | `show-subtree' | overridden | visibility cycling |
  17101. ;; | `show-all' | overridden | no replacement |
  17102. ;; | `hide-subtree' | overridden | visibility cycling |
  17103. ;; | `hide-body' | overridden | no replacement |
  17104. ;; | `hide-entry' | overridden | visibility cycling |
  17105. ;; | `hide-leaves' | overridden | no replacement |
  17106. ;; | `hide-sublevels' | overridden | no replacement |
  17107. ;; | `hide-other' | overridden | no replacement |
  17108. ;; Make `C-c C-x' a prefix key
  17109. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  17110. ;; TAB key with modifiers
  17111. (org-defkey org-mode-map "\C-i" 'org-cycle)
  17112. (org-defkey org-mode-map [(tab)] 'org-cycle)
  17113. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  17114. (org-defkey org-mode-map "\M-\t" 'pcomplete)
  17115. ;; The following line is necessary under Suse GNU/Linux
  17116. (unless (featurep 'xemacs)
  17117. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  17118. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  17119. (define-key org-mode-map [backtab] 'org-shifttab)
  17120. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  17121. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  17122. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  17123. ;; Cursor keys with modifiers
  17124. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  17125. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  17126. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  17127. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  17128. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  17129. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  17130. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  17131. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  17132. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  17133. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  17134. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  17135. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  17136. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  17137. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  17138. (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
  17139. (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
  17140. ;; Babel keys
  17141. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  17142. (mapc (lambda (pair)
  17143. (define-key org-babel-map (car pair) (cdr pair)))
  17144. org-babel-key-bindings)
  17145. ;;; Extra keys for tty access.
  17146. ;; We only set them when really needed because otherwise the
  17147. ;; menus don't show the simple keys
  17148. (when (or org-use-extra-keys
  17149. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  17150. (not window-system))
  17151. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  17152. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  17153. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  17154. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  17155. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  17156. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  17157. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  17158. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  17159. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  17160. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  17161. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  17162. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  17163. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  17164. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  17165. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  17166. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  17167. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  17168. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  17169. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  17170. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  17171. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  17172. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  17173. (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
  17174. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  17175. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  17176. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  17177. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  17178. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  17179. ;; All the other keys
  17180. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  17181. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  17182. (if (boundp 'narrow-map)
  17183. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  17184. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  17185. (if (boundp 'narrow-map)
  17186. (org-defkey narrow-map "b" 'org-narrow-to-block)
  17187. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  17188. (if (boundp 'narrow-map)
  17189. (org-defkey narrow-map "e" 'org-narrow-to-element)
  17190. (org-defkey org-mode-map "\C-xne" 'org-narrow-to-element))
  17191. (org-defkey org-mode-map "\C-\M-t" 'org-transpose-element)
  17192. (org-defkey org-mode-map "\M-}" 'org-forward-element)
  17193. (org-defkey org-mode-map "\M-{" 'org-backward-element)
  17194. (org-defkey org-mode-map "\C-c\C-^" 'org-up-element)
  17195. (org-defkey org-mode-map "\C-c\C-_" 'org-down-element)
  17196. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-heading-same-level)
  17197. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-heading-same-level)
  17198. (org-defkey org-mode-map "\C-c\M-f" 'org-next-block)
  17199. (org-defkey org-mode-map "\C-c\M-b" 'org-previous-block)
  17200. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  17201. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  17202. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  17203. (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
  17204. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  17205. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  17206. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  17207. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  17208. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  17209. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  17210. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  17211. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  17212. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  17213. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  17214. (org-defkey org-mode-map "\C-c\M-w" 'org-copy)
  17215. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  17216. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  17217. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  17218. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  17219. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  17220. (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
  17221. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  17222. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  17223. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  17224. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  17225. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  17226. (org-defkey org-mode-map "\C-c\C-\M-l" 'org-insert-all-links)
  17227. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  17228. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  17229. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  17230. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  17231. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  17232. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  17233. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  17234. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  17235. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  17236. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  17237. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  17238. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  17239. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  17240. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  17241. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  17242. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  17243. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  17244. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  17245. (org-defkey org-mode-map "\C-c^" 'org-sort)
  17246. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  17247. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  17248. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  17249. (org-defkey org-mode-map "\C-m" 'org-return)
  17250. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  17251. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  17252. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  17253. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  17254. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  17255. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  17256. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  17257. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  17258. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  17259. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  17260. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  17261. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  17262. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  17263. (org-defkey org-mode-map "\C-c\C-e" 'org-export-dispatch)
  17264. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  17265. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  17266. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  17267. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  17268. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  17269. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  17270. (org-defkey org-mode-map "\M-h" 'org-mark-element)
  17271. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  17272. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  17273. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  17274. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  17275. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  17276. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  17277. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  17278. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-in-last)
  17279. (org-defkey org-mode-map "\C-c\C-x\C-z" 'org-resolve-clocks)
  17280. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  17281. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  17282. (org-defkey org-mode-map "\C-c\C-x\C-q" 'org-clock-cancel)
  17283. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  17284. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  17285. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  17286. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  17287. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  17288. (org-defkey org-mode-map "\C-c\C-x\C-\M-v" 'org-redisplay-inline-images)
  17289. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  17290. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  17291. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  17292. (org-defkey org-mode-map "\C-c\C-xP" 'org-set-property-and-value)
  17293. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  17294. (org-defkey org-mode-map "\C-c\C-xE" 'org-inc-effort)
  17295. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  17296. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  17297. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  17298. (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
  17299. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  17300. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  17301. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  17302. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  17303. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  17304. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  17305. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  17306. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  17307. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  17308. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  17309. (when (featurep 'xemacs)
  17310. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  17311. (defconst org-speed-commands-default
  17312. '(
  17313. ("Outline Navigation")
  17314. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  17315. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  17316. ("f" . (org-speed-move-safe 'org-forward-heading-same-level))
  17317. ("b" . (org-speed-move-safe 'org-backward-heading-same-level))
  17318. ("F" . org-next-block)
  17319. ("B" . org-previous-block)
  17320. ("u" . (org-speed-move-safe 'outline-up-heading))
  17321. ("j" . org-goto)
  17322. ("g" . (org-refile t))
  17323. ("Outline Visibility")
  17324. ("c" . org-cycle)
  17325. ("C" . org-shifttab)
  17326. (" " . org-display-outline-path)
  17327. ("=" . org-columns)
  17328. ("Outline Structure Editing")
  17329. ("U" . org-shiftmetaup)
  17330. ("D" . org-shiftmetadown)
  17331. ("r" . org-metaright)
  17332. ("l" . org-metaleft)
  17333. ("R" . org-shiftmetaright)
  17334. ("L" . org-shiftmetaleft)
  17335. ("i" . (progn (forward-char 1) (call-interactively
  17336. 'org-insert-heading-respect-content)))
  17337. ("^" . org-sort)
  17338. ("w" . org-refile)
  17339. ("a" . org-archive-subtree-default-with-confirmation)
  17340. ("@" . org-mark-subtree)
  17341. ("#" . org-toggle-comment)
  17342. ("Clock Commands")
  17343. ("I" . org-clock-in)
  17344. ("O" . org-clock-out)
  17345. ("Meta Data Editing")
  17346. ("t" . org-todo)
  17347. ("," . (org-priority))
  17348. ("0" . (org-priority ?\ ))
  17349. ("1" . (org-priority ?A))
  17350. ("2" . (org-priority ?B))
  17351. ("3" . (org-priority ?C))
  17352. (":" . org-set-tags-command)
  17353. ("e" . org-set-effort)
  17354. ("E" . org-inc-effort)
  17355. ("W" . (lambda(m) (interactive "sMinutes before warning: ")
  17356. (org-entry-put (point) "APPT_WARNTIME" m)))
  17357. ("Agenda Views etc")
  17358. ("v" . org-agenda)
  17359. ("/" . org-sparse-tree)
  17360. ("Misc")
  17361. ("o" . org-open-at-point)
  17362. ("?" . org-speed-command-help)
  17363. ("<" . (org-agenda-set-restriction-lock 'subtree))
  17364. (">" . (org-agenda-remove-restriction-lock))
  17365. )
  17366. "The default speed commands.")
  17367. (defun org-print-speed-command (e)
  17368. (if (> (length (car e)) 1)
  17369. (progn
  17370. (princ "\n")
  17371. (princ (car e))
  17372. (princ "\n")
  17373. (princ (make-string (length (car e)) ?-))
  17374. (princ "\n"))
  17375. (princ (car e))
  17376. (princ " ")
  17377. (if (symbolp (cdr e))
  17378. (princ (symbol-name (cdr e)))
  17379. (prin1 (cdr e)))
  17380. (princ "\n")))
  17381. (defun org-speed-command-help ()
  17382. "Show the available speed commands."
  17383. (interactive)
  17384. (if (not org-use-speed-commands)
  17385. (error "Speed commands are not activated, customize `org-use-speed-commands'")
  17386. (with-output-to-temp-buffer "*Help*"
  17387. (princ "User-defined Speed commands\n===========================\n")
  17388. (mapc 'org-print-speed-command org-speed-commands-user)
  17389. (princ "\n")
  17390. (princ "Built-in Speed commands\n=======================\n")
  17391. (mapc 'org-print-speed-command org-speed-commands-default))
  17392. (with-current-buffer "*Help*"
  17393. (setq truncate-lines t))))
  17394. (defun org-speed-move-safe (cmd)
  17395. "Execute CMD, but make sure that the cursor always ends up in a headline.
  17396. If not, return to the original position and throw an error."
  17397. (interactive)
  17398. (let ((pos (point)))
  17399. (call-interactively cmd)
  17400. (unless (and (bolp) (org-at-heading-p))
  17401. (goto-char pos)
  17402. (error "Boundary reached while executing %s" cmd))))
  17403. (defvar org-self-insert-command-undo-counter 0)
  17404. (defvar org-table-auto-blank-field) ; defined in org-table.el
  17405. (defvar org-speed-command nil)
  17406. (define-obsolete-function-alias
  17407. 'org-speed-command-default-hook 'org-speed-command-activate "24.3")
  17408. (defun org-speed-command-activate (keys)
  17409. "Hook for activating single-letter speed commands.
  17410. `org-speed-commands-default' specifies a minimal command set.
  17411. Use `org-speed-commands-user' for further customization."
  17412. (when (or (and (bolp) (looking-at org-outline-regexp))
  17413. (and (functionp org-use-speed-commands)
  17414. (funcall org-use-speed-commands)))
  17415. (cdr (assoc keys (append org-speed-commands-user
  17416. org-speed-commands-default)))))
  17417. (define-obsolete-function-alias
  17418. 'org-babel-speed-command-hook 'org-babel-speed-command-activate "24.3")
  17419. (defun org-babel-speed-command-activate (keys)
  17420. "Hook for activating single-letter code block commands."
  17421. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  17422. (cdr (assoc keys org-babel-key-bindings))))
  17423. (defcustom org-speed-command-hook
  17424. '(org-speed-command-default-hook org-babel-speed-command-hook)
  17425. "Hook for activating speed commands at strategic locations.
  17426. Hook functions are called in sequence until a valid handler is
  17427. found.
  17428. Each hook takes a single argument, a user-pressed command key
  17429. which is also a `self-insert-command' from the global map.
  17430. Within the hook, examine the cursor position and the command key
  17431. and return nil or a valid handler as appropriate. Handler could
  17432. be one of an interactive command, a function, or a form.
  17433. Set `org-use-speed-commands' to non-nil value to enable this
  17434. hook. The default setting is `org-speed-command-activate'."
  17435. :group 'org-structure
  17436. :version "24.1"
  17437. :type 'hook)
  17438. (defun org-self-insert-command (N)
  17439. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  17440. If the cursor is in a table looking at whitespace, the whitespace is
  17441. overwritten, and the table is not marked as requiring realignment."
  17442. (interactive "p")
  17443. (org-check-before-invisible-edit 'insert)
  17444. (cond
  17445. ((and org-use-speed-commands
  17446. (setq org-speed-command
  17447. (run-hook-with-args-until-success
  17448. 'org-speed-command-hook (this-command-keys))))
  17449. (cond
  17450. ((commandp org-speed-command)
  17451. (setq this-command org-speed-command)
  17452. (call-interactively org-speed-command))
  17453. ((functionp org-speed-command)
  17454. (funcall org-speed-command))
  17455. ((and org-speed-command (listp org-speed-command))
  17456. (eval org-speed-command))
  17457. (t (let (org-use-speed-commands)
  17458. (call-interactively 'org-self-insert-command)))))
  17459. ((and
  17460. (org-table-p)
  17461. (progn
  17462. ;; check if we blank the field, and if that triggers align
  17463. (and (featurep 'org-table) org-table-auto-blank-field
  17464. (member last-command
  17465. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  17466. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  17467. ;; got extra space, this field does not determine column width
  17468. (let (org-table-may-need-update) (org-table-blank-field))
  17469. ;; no extra space, this field may determine column width
  17470. (org-table-blank-field)))
  17471. t)
  17472. (eq N 1)
  17473. (looking-at "[^|\n]* |"))
  17474. (let (org-table-may-need-update)
  17475. (goto-char (1- (match-end 0)))
  17476. (backward-delete-char 1)
  17477. (goto-char (match-beginning 0))
  17478. (self-insert-command N)))
  17479. (t
  17480. (setq org-table-may-need-update t)
  17481. (self-insert-command N)
  17482. (org-fix-tags-on-the-fly)
  17483. (if org-self-insert-cluster-for-undo
  17484. (if (not (eq last-command 'org-self-insert-command))
  17485. (setq org-self-insert-command-undo-counter 1)
  17486. (if (>= org-self-insert-command-undo-counter 20)
  17487. (setq org-self-insert-command-undo-counter 1)
  17488. (and (> org-self-insert-command-undo-counter 0)
  17489. buffer-undo-list (listp buffer-undo-list)
  17490. (not (cadr buffer-undo-list)) ; remove nil entry
  17491. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  17492. (setq org-self-insert-command-undo-counter
  17493. (1+ org-self-insert-command-undo-counter))))))))
  17494. (defun org-check-before-invisible-edit (kind)
  17495. "Check is editing if kind KIND would be dangerous with invisible text around.
  17496. The detailed reaction depends on the user option `org-catch-invisible-edits'."
  17497. ;; First, try to get out of here as quickly as possible, to reduce overhead
  17498. (if (and org-catch-invisible-edits
  17499. (or (not (boundp 'visible-mode)) (not visible-mode))
  17500. (or (get-char-property (point) 'invisible)
  17501. (get-char-property (max (point-min) (1- (point))) 'invisible)))
  17502. ;; OK, we need to take a closer look
  17503. (let* ((invisible-at-point (get-char-property (point) 'invisible))
  17504. (invisible-before-point (if (bobp) nil (get-char-property
  17505. (1- (point)) 'invisible)))
  17506. (border-and-ok-direction
  17507. (or
  17508. ;; Check if we are acting predictably before invisible text
  17509. (and invisible-at-point (not invisible-before-point)
  17510. (memq kind '(insert delete-backward)))
  17511. ;; Check if we are acting predictably after invisible text
  17512. ;; This works not well, and I have turned it off. It seems
  17513. ;; better to always show and stop after invisible text.
  17514. ;; (and (not invisible-at-point) invisible-before-point
  17515. ;; (memq kind '(insert delete)))
  17516. )))
  17517. (when (or (memq invisible-at-point '(outline org-hide-block t))
  17518. (memq invisible-before-point '(outline org-hide-block t)))
  17519. (if (eq org-catch-invisible-edits 'error)
  17520. (error "Editing in invisible areas is prohibited - make visible first"))
  17521. (if (and org-custom-properties-overlays
  17522. (y-or-n-p "Display invisible properties in this buffer? "))
  17523. (org-toggle-custom-properties-visibility)
  17524. ;; Make the area visible
  17525. (save-excursion
  17526. (if invisible-before-point
  17527. (goto-char (previous-single-char-property-change
  17528. (point) 'invisible)))
  17529. (org-cycle))
  17530. (cond
  17531. ((eq org-catch-invisible-edits 'show)
  17532. ;; That's it, we do the edit after showing
  17533. (message
  17534. "Unfolding invisible region around point before editing")
  17535. (sit-for 1))
  17536. ((and (eq org-catch-invisible-edits 'smart)
  17537. border-and-ok-direction)
  17538. (message "Unfolding invisible region around point before editing"))
  17539. (t
  17540. ;; Don't do the edit, make the user repeat it in full visibility
  17541. (error "Edit in invisible region aborted, repeat to confirm with text visible"))))))))
  17542. (defun org-fix-tags-on-the-fly ()
  17543. (when (and (equal (char-after (point-at-bol)) ?*)
  17544. (org-at-heading-p))
  17545. (org-align-tags-here org-tags-column)))
  17546. (defun org-delete-backward-char (N)
  17547. "Like `delete-backward-char', insert whitespace at field end in tables.
  17548. When deleting backwards, in tables this function will insert whitespace in
  17549. front of the next \"|\" separator, to keep the table aligned. The table will
  17550. still be marked for re-alignment if the field did fill the entire column,
  17551. because, in this case the deletion might narrow the column."
  17552. (interactive "p")
  17553. (save-match-data
  17554. (org-check-before-invisible-edit 'delete-backward)
  17555. (if (and (org-table-p)
  17556. (eq N 1)
  17557. (string-match "|" (buffer-substring (point-at-bol) (point)))
  17558. (looking-at ".*?|"))
  17559. (let ((pos (point))
  17560. (noalign (looking-at "[^|\n\r]* |"))
  17561. (c org-table-may-need-update))
  17562. (backward-delete-char N)
  17563. (if (not overwrite-mode)
  17564. (progn
  17565. (skip-chars-forward "^|")
  17566. (insert " ")
  17567. (goto-char (1- pos))))
  17568. ;; noalign: if there were two spaces at the end, this field
  17569. ;; does not determine the width of the column.
  17570. (if noalign (setq org-table-may-need-update c)))
  17571. (backward-delete-char N)
  17572. (org-fix-tags-on-the-fly))))
  17573. (defun org-delete-char (N)
  17574. "Like `delete-char', but insert whitespace at field end in tables.
  17575. When deleting characters, in tables this function will insert whitespace in
  17576. front of the next \"|\" separator, to keep the table aligned. The table will
  17577. still be marked for re-alignment if the field did fill the entire column,
  17578. because, in this case the deletion might narrow the column."
  17579. (interactive "p")
  17580. (save-match-data
  17581. (org-check-before-invisible-edit 'delete)
  17582. (if (and (org-table-p)
  17583. (not (bolp))
  17584. (not (= (char-after) ?|))
  17585. (eq N 1))
  17586. (if (looking-at ".*?|")
  17587. (let ((pos (point))
  17588. (noalign (looking-at "[^|\n\r]* |"))
  17589. (c org-table-may-need-update))
  17590. (replace-match
  17591. (concat (substring (match-string 0) 1 -1) " |") nil t)
  17592. (goto-char pos)
  17593. ;; noalign: if there were two spaces at the end, this field
  17594. ;; does not determine the width of the column.
  17595. (if noalign (setq org-table-may-need-update c)))
  17596. (delete-char N))
  17597. (delete-char N)
  17598. (org-fix-tags-on-the-fly))))
  17599. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  17600. (put 'org-self-insert-command 'delete-selection t)
  17601. (put 'orgtbl-self-insert-command 'delete-selection t)
  17602. (put 'org-delete-char 'delete-selection 'supersede)
  17603. (put 'org-delete-backward-char 'delete-selection 'supersede)
  17604. (put 'org-yank 'delete-selection 'yank)
  17605. ;; Make `flyspell-mode' delay after some commands
  17606. (put 'org-self-insert-command 'flyspell-delayed t)
  17607. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  17608. (put 'org-delete-char 'flyspell-delayed t)
  17609. (put 'org-delete-backward-char 'flyspell-delayed t)
  17610. ;; Make pabbrev-mode expand after org-mode commands
  17611. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  17612. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  17613. ;; How to do this: Measure non-white length of current string
  17614. ;; If equal to column width, we should realign.
  17615. (defun org-remap (map &rest commands)
  17616. "In MAP, remap the functions given in COMMANDS.
  17617. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  17618. (let (new old)
  17619. (while commands
  17620. (setq old (pop commands) new (pop commands))
  17621. (if (fboundp 'command-remapping)
  17622. (org-defkey map (vector 'remap old) new)
  17623. (substitute-key-definition old new map global-map)))))
  17624. (defun org-transpose-words ()
  17625. "Transpose words for Org.
  17626. This uses the `org-mode-transpose-word-syntax-table' syntax
  17627. table, which interprets characters in `org-emphasis-alist' as
  17628. word constituants."
  17629. (interactive)
  17630. (with-syntax-table org-mode-transpose-word-syntax-table
  17631. (call-interactively 'transpose-words)))
  17632. (org-remap org-mode-map 'transpose-words 'org-transpose-words)
  17633. (when (eq org-enable-table-editor 'optimized)
  17634. ;; If the user wants maximum table support, we need to hijack
  17635. ;; some standard editing functions
  17636. (org-remap org-mode-map
  17637. 'self-insert-command 'org-self-insert-command
  17638. 'delete-char 'org-delete-char
  17639. 'delete-backward-char 'org-delete-backward-char)
  17640. (org-defkey org-mode-map "|" 'org-force-self-insert))
  17641. (defvar org-ctrl-c-ctrl-c-hook nil
  17642. "Hook for functions attaching themselves to `C-c C-c'.
  17643. This can be used to add additional functionality to the C-c C-c
  17644. key which executes context-dependent commands. This hook is run
  17645. before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
  17646. run after the last test.
  17647. Each function will be called with no arguments. The function
  17648. must check if the context is appropriate for it to act. If yes,
  17649. it should do its thing and then return a non-nil value. If the
  17650. context is wrong, just do nothing and return nil.")
  17651. (defvar org-ctrl-c-ctrl-c-final-hook nil
  17652. "Hook for functions attaching themselves to `C-c C-c'.
  17653. This can be used to add additional functionality to the C-c C-c
  17654. key which executes context-dependent commands. This hook is run
  17655. after any other test, while `org-ctrl-c-ctrl-c-hook' is run
  17656. before the first test.
  17657. Each function will be called with no arguments. The function
  17658. must check if the context is appropriate for it to act. If yes,
  17659. it should do its thing and then return a non-nil value. If the
  17660. context is wrong, just do nothing and return nil.")
  17661. (defvar org-tab-first-hook nil
  17662. "Hook for functions to attach themselves to TAB.
  17663. See `org-ctrl-c-ctrl-c-hook' for more information.
  17664. This hook runs as the first action when TAB is pressed, even before
  17665. `org-cycle' messes around with the `outline-regexp' to cater for
  17666. inline tasks and plain list item folding.
  17667. If any function in this hook returns t, any other actions that
  17668. would have been caused by TAB (such as table field motion or visibility
  17669. cycling) will not occur.")
  17670. (defvar org-tab-after-check-for-table-hook nil
  17671. "Hook for functions to attach themselves to TAB.
  17672. See `org-ctrl-c-ctrl-c-hook' for more information.
  17673. This hook runs after it has been established that the cursor is not in a
  17674. table, but before checking if the cursor is in a headline or if global cycling
  17675. should be done.
  17676. If any function in this hook returns t, not other actions like visibility
  17677. cycling will be done.")
  17678. (defvar org-tab-after-check-for-cycling-hook nil
  17679. "Hook for functions to attach themselves to TAB.
  17680. See `org-ctrl-c-ctrl-c-hook' for more information.
  17681. This hook runs after it has been established that not table field motion and
  17682. not visibility should be done because of current context. This is probably
  17683. the place where a package like yasnippets can hook in.")
  17684. (defvar org-tab-before-tab-emulation-hook nil
  17685. "Hook for functions to attach themselves to TAB.
  17686. See `org-ctrl-c-ctrl-c-hook' for more information.
  17687. This hook runs after every other options for TAB have been exhausted, but
  17688. before indentation and \t insertion takes place.")
  17689. (defvar org-metaleft-hook nil
  17690. "Hook for functions attaching themselves to `M-left'.
  17691. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17692. (defvar org-metaright-hook nil
  17693. "Hook for functions attaching themselves to `M-right'.
  17694. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17695. (defvar org-metaup-hook nil
  17696. "Hook for functions attaching themselves to `M-up'.
  17697. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17698. (defvar org-metadown-hook nil
  17699. "Hook for functions attaching themselves to `M-down'.
  17700. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17701. (defvar org-shiftmetaleft-hook nil
  17702. "Hook for functions attaching themselves to `M-S-left'.
  17703. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17704. (defvar org-shiftmetaright-hook nil
  17705. "Hook for functions attaching themselves to `M-S-right'.
  17706. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17707. (defvar org-shiftmetaup-hook nil
  17708. "Hook for functions attaching themselves to `M-S-up'.
  17709. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17710. (defvar org-shiftmetadown-hook nil
  17711. "Hook for functions attaching themselves to `M-S-down'.
  17712. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17713. (defvar org-metareturn-hook nil
  17714. "Hook for functions attaching themselves to `M-RET'.
  17715. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17716. (defvar org-shiftup-hook nil
  17717. "Hook for functions attaching themselves to `S-up'.
  17718. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17719. (defvar org-shiftup-final-hook nil
  17720. "Hook for functions attaching themselves to `S-up'.
  17721. This one runs after all other options except shift-select have been excluded.
  17722. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17723. (defvar org-shiftdown-hook nil
  17724. "Hook for functions attaching themselves to `S-down'.
  17725. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17726. (defvar org-shiftdown-final-hook nil
  17727. "Hook for functions attaching themselves to `S-down'.
  17728. This one runs after all other options except shift-select have been excluded.
  17729. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17730. (defvar org-shiftleft-hook nil
  17731. "Hook for functions attaching themselves to `S-left'.
  17732. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17733. (defvar org-shiftleft-final-hook nil
  17734. "Hook for functions attaching themselves to `S-left'.
  17735. This one runs after all other options except shift-select have been excluded.
  17736. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17737. (defvar org-shiftright-hook nil
  17738. "Hook for functions attaching themselves to `S-right'.
  17739. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17740. (defvar org-shiftright-final-hook nil
  17741. "Hook for functions attaching themselves to `S-right'.
  17742. This one runs after all other options except shift-select have been excluded.
  17743. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17744. (defun org-modifier-cursor-error ()
  17745. "Throw an error, a modified cursor command was applied in wrong context."
  17746. (error "This command is active in special context like tables, headlines or items"))
  17747. (defun org-shiftselect-error ()
  17748. "Throw an error because Shift-Cursor command was applied in wrong context."
  17749. (if (and (boundp 'shift-select-mode) shift-select-mode)
  17750. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  17751. (error "This command works only in special context like headlines or timestamps")))
  17752. (defun org-call-for-shift-select (cmd)
  17753. (let ((this-command-keys-shift-translated t))
  17754. (call-interactively cmd)))
  17755. (defun org-shifttab (&optional arg)
  17756. "Global visibility cycling or move to previous table field.
  17757. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  17758. on context.
  17759. See the individual commands for more information."
  17760. (interactive "P")
  17761. (cond
  17762. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  17763. ((integerp arg)
  17764. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  17765. (message "Content view to level: %d" arg)
  17766. (org-content (prefix-numeric-value arg2))
  17767. (setq org-cycle-global-status 'overview)))
  17768. (t (call-interactively 'org-global-cycle))))
  17769. (defun org-shiftmetaleft ()
  17770. "Promote subtree or delete table column.
  17771. Calls `org-promote-subtree', `org-outdent-item-tree', or
  17772. `org-table-delete-column', depending on context. See the
  17773. individual commands for more information."
  17774. (interactive)
  17775. (cond
  17776. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  17777. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  17778. ((org-at-heading-p) (call-interactively 'org-promote-subtree))
  17779. ((if (not (org-region-active-p)) (org-at-item-p)
  17780. (save-excursion (goto-char (region-beginning))
  17781. (org-at-item-p)))
  17782. (call-interactively 'org-outdent-item-tree))
  17783. (t (org-modifier-cursor-error))))
  17784. (defun org-shiftmetaright ()
  17785. "Demote subtree or insert table column.
  17786. Calls `org-demote-subtree', `org-indent-item-tree', or
  17787. `org-table-insert-column', depending on context. See the
  17788. individual commands for more information."
  17789. (interactive)
  17790. (cond
  17791. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  17792. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  17793. ((org-at-heading-p) (call-interactively 'org-demote-subtree))
  17794. ((if (not (org-region-active-p)) (org-at-item-p)
  17795. (save-excursion (goto-char (region-beginning))
  17796. (org-at-item-p)))
  17797. (call-interactively 'org-indent-item-tree))
  17798. (t (org-modifier-cursor-error))))
  17799. (defun org-shiftmetaup (&optional arg)
  17800. "Move subtree up or kill table row.
  17801. Calls `org-move-subtree-up' or `org-table-kill-row' or
  17802. `org-move-item-up' or `org-timestamp-up', depending on context.
  17803. See the individual commands for more information."
  17804. (interactive "P")
  17805. (cond
  17806. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  17807. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  17808. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  17809. ((org-at-item-p) (call-interactively 'org-move-item-up))
  17810. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  17811. (call-interactively 'org-timestamp-up)))
  17812. (t (call-interactively 'org-drag-line-backward))))
  17813. (defun org-shiftmetadown (&optional arg)
  17814. "Move subtree down or insert table row.
  17815. Calls `org-move-subtree-down' or `org-table-insert-row' or
  17816. `org-move-item-down' or `org-timestamp-up', depending on context.
  17817. See the individual commands for more information."
  17818. (interactive "P")
  17819. (cond
  17820. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  17821. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  17822. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  17823. ((org-at-item-p) (call-interactively 'org-move-item-down))
  17824. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  17825. (call-interactively 'org-timestamp-down)))
  17826. (t (call-interactively 'org-drag-line-forward))))
  17827. (defsubst org-hidden-tree-error ()
  17828. (error
  17829. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  17830. (defun org-metaleft (&optional arg)
  17831. "Promote heading or move table column to left.
  17832. Calls `org-do-promote' or `org-table-move-column', depending on context.
  17833. With no specific context, calls the Emacs default `backward-word'.
  17834. See the individual commands for more information."
  17835. (interactive "P")
  17836. (cond
  17837. ((run-hook-with-args-until-success 'org-metaleft-hook))
  17838. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  17839. ((org-with-limited-levels
  17840. (or (org-at-heading-p)
  17841. (and (org-region-active-p)
  17842. (save-excursion
  17843. (goto-char (region-beginning))
  17844. (org-at-heading-p)))))
  17845. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  17846. (call-interactively 'org-do-promote))
  17847. ;; At an inline task.
  17848. ((org-at-heading-p)
  17849. (call-interactively 'org-inlinetask-promote))
  17850. ((or (org-at-item-p)
  17851. (and (org-region-active-p)
  17852. (save-excursion
  17853. (goto-char (region-beginning))
  17854. (org-at-item-p))))
  17855. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  17856. (call-interactively 'org-outdent-item))
  17857. (t (call-interactively 'backward-word))))
  17858. (defun org-metaright (&optional arg)
  17859. "Demote a subtree, a list item or move table column to right.
  17860. In front of a drawer or a block keyword, indent it correctly.
  17861. With no specific context, calls the Emacs default `forward-word'.
  17862. See the individual commands for more information."
  17863. (interactive "P")
  17864. (cond
  17865. ((run-hook-with-args-until-success 'org-metaright-hook))
  17866. ((org-at-table-p) (call-interactively 'org-table-move-column))
  17867. ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
  17868. ((org-at-block-p) (call-interactively 'org-indent-block))
  17869. ((org-with-limited-levels
  17870. (or (org-at-heading-p)
  17871. (and (org-region-active-p)
  17872. (save-excursion
  17873. (goto-char (region-beginning))
  17874. (org-at-heading-p)))))
  17875. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  17876. (call-interactively 'org-do-demote))
  17877. ;; At an inline task.
  17878. ((org-at-heading-p)
  17879. (call-interactively 'org-inlinetask-demote))
  17880. ((or (org-at-item-p)
  17881. (and (org-region-active-p)
  17882. (save-excursion
  17883. (goto-char (region-beginning))
  17884. (org-at-item-p))))
  17885. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  17886. (call-interactively 'org-indent-item))
  17887. (t (call-interactively 'forward-word))))
  17888. (defun org-check-for-hidden (what)
  17889. "Check if there are hidden headlines/items in the current visual line.
  17890. WHAT can be either `headlines' or `items'. If the current line is
  17891. an outline or item heading and it has a folded subtree below it,
  17892. this function returns t, nil otherwise."
  17893. (let ((re (cond
  17894. ((eq what 'headlines) org-outline-regexp-bol)
  17895. ((eq what 'items) (org-item-beginning-re))
  17896. (t (error "This should not happen"))))
  17897. beg end)
  17898. (save-excursion
  17899. (catch 'exit
  17900. (unless (org-region-active-p)
  17901. (setq beg (point-at-bol))
  17902. (beginning-of-line 2)
  17903. (while (and (not (eobp)) ;; this is like `next-line'
  17904. (get-char-property (1- (point)) 'invisible))
  17905. (beginning-of-line 2))
  17906. (setq end (point))
  17907. (goto-char beg)
  17908. (goto-char (point-at-eol))
  17909. (setq end (max end (point)))
  17910. (while (re-search-forward re end t)
  17911. (if (get-char-property (match-beginning 0) 'invisible)
  17912. (throw 'exit t))))
  17913. nil))))
  17914. (defun org-metaup (&optional arg)
  17915. "Move subtree up or move table row up.
  17916. Calls `org-move-subtree-up' or `org-table-move-row' or
  17917. `org-move-item-up', depending on context. See the individual commands
  17918. for more information."
  17919. (interactive "P")
  17920. (cond
  17921. ((run-hook-with-args-until-success 'org-metaup-hook))
  17922. ((org-region-active-p)
  17923. (let* ((a (min (region-beginning) (region-end)))
  17924. (b (1- (max (region-beginning) (region-end))))
  17925. (c (save-excursion (goto-char a)
  17926. (move-beginning-of-line 0)))
  17927. (d (save-excursion (goto-char a)
  17928. (move-end-of-line 0) (point))))
  17929. (transpose-regions a b c d)
  17930. (goto-char c)))
  17931. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  17932. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  17933. ((org-at-item-p) (call-interactively 'org-move-item-up))
  17934. (t (org-drag-element-backward))))
  17935. (defun org-metadown (&optional arg)
  17936. "Move subtree down or move table row down.
  17937. Calls `org-move-subtree-down' or `org-table-move-row' or
  17938. `org-move-item-down', depending on context. See the individual
  17939. commands for more information."
  17940. (interactive "P")
  17941. (cond
  17942. ((run-hook-with-args-until-success 'org-metadown-hook))
  17943. ((org-region-active-p)
  17944. (let* ((a (min (region-beginning) (region-end)))
  17945. (b (max (region-beginning) (region-end)))
  17946. (c (save-excursion (goto-char b)
  17947. (move-beginning-of-line 1)))
  17948. (d (save-excursion (goto-char b)
  17949. (move-end-of-line 1) (1+ (point)))))
  17950. (transpose-regions a b c d)
  17951. (goto-char d)))
  17952. ((org-at-table-p) (call-interactively 'org-table-move-row))
  17953. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  17954. ((org-at-item-p) (call-interactively 'org-move-item-down))
  17955. (t (org-drag-element-forward))))
  17956. (defun org-shiftup (&optional arg)
  17957. "Increase item in timestamp or increase priority of current headline.
  17958. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  17959. depending on context. See the individual commands for more information."
  17960. (interactive "P")
  17961. (cond
  17962. ((run-hook-with-args-until-success 'org-shiftup-hook))
  17963. ((and org-support-shift-select (org-region-active-p))
  17964. (org-call-for-shift-select 'previous-line))
  17965. ((org-at-timestamp-p t)
  17966. (call-interactively (if org-edit-timestamp-down-means-later
  17967. 'org-timestamp-down 'org-timestamp-up)))
  17968. ((and (not (eq org-support-shift-select 'always))
  17969. org-enable-priority-commands
  17970. (org-at-heading-p))
  17971. (call-interactively 'org-priority-up))
  17972. ((and (not org-support-shift-select) (org-at-item-p))
  17973. (call-interactively 'org-previous-item))
  17974. ((org-clocktable-try-shift 'up arg))
  17975. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  17976. (org-support-shift-select
  17977. (org-call-for-shift-select 'previous-line))
  17978. (t (org-shiftselect-error))))
  17979. (defun org-shiftdown (&optional arg)
  17980. "Decrease item in timestamp or decrease priority of current headline.
  17981. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  17982. depending on context. See the individual commands for more information."
  17983. (interactive "P")
  17984. (cond
  17985. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  17986. ((and org-support-shift-select (org-region-active-p))
  17987. (org-call-for-shift-select 'next-line))
  17988. ((org-at-timestamp-p t)
  17989. (call-interactively (if org-edit-timestamp-down-means-later
  17990. 'org-timestamp-up 'org-timestamp-down)))
  17991. ((and (not (eq org-support-shift-select 'always))
  17992. org-enable-priority-commands
  17993. (org-at-heading-p))
  17994. (call-interactively 'org-priority-down))
  17995. ((and (not org-support-shift-select) (org-at-item-p))
  17996. (call-interactively 'org-next-item))
  17997. ((org-clocktable-try-shift 'down arg))
  17998. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  17999. (org-support-shift-select
  18000. (org-call-for-shift-select 'next-line))
  18001. (t (org-shiftselect-error))))
  18002. (defun org-shiftright (&optional arg)
  18003. "Cycle the thing at point or in the current line, depending on context.
  18004. Depending on context, this does one of the following:
  18005. - switch a timestamp at point one day into the future
  18006. - on a headline, switch to the next TODO keyword.
  18007. - on an item, switch entire list to the next bullet type
  18008. - on a property line, switch to the next allowed value
  18009. - on a clocktable definition line, move time block into the future"
  18010. (interactive "P")
  18011. (cond
  18012. ((run-hook-with-args-until-success 'org-shiftright-hook))
  18013. ((and org-support-shift-select (org-region-active-p))
  18014. (org-call-for-shift-select 'forward-char))
  18015. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  18016. ((and (not (eq org-support-shift-select 'always))
  18017. (org-at-heading-p))
  18018. (let ((org-inhibit-logging
  18019. (not org-treat-S-cursor-todo-selection-as-state-change))
  18020. (org-inhibit-blocking
  18021. (not org-treat-S-cursor-todo-selection-as-state-change)))
  18022. (org-call-with-arg 'org-todo 'right)))
  18023. ((or (and org-support-shift-select
  18024. (not (eq org-support-shift-select 'always))
  18025. (org-at-item-bullet-p))
  18026. (and (not org-support-shift-select) (org-at-item-p)))
  18027. (org-call-with-arg 'org-cycle-list-bullet nil))
  18028. ((and (not (eq org-support-shift-select 'always))
  18029. (org-at-property-p))
  18030. (call-interactively 'org-property-next-allowed-value))
  18031. ((org-clocktable-try-shift 'right arg))
  18032. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  18033. (org-support-shift-select
  18034. (org-call-for-shift-select 'forward-char))
  18035. (t (org-shiftselect-error))))
  18036. (defun org-shiftleft (&optional arg)
  18037. "Cycle the thing at point or in the current line, depending on context.
  18038. Depending on context, this does one of the following:
  18039. - switch a timestamp at point one day into the past
  18040. - on a headline, switch to the previous TODO keyword.
  18041. - on an item, switch entire list to the previous bullet type
  18042. - on a property line, switch to the previous allowed value
  18043. - on a clocktable definition line, move time block into the past"
  18044. (interactive "P")
  18045. (cond
  18046. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  18047. ((and org-support-shift-select (org-region-active-p))
  18048. (org-call-for-shift-select 'backward-char))
  18049. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  18050. ((and (not (eq org-support-shift-select 'always))
  18051. (org-at-heading-p))
  18052. (let ((org-inhibit-logging
  18053. (not org-treat-S-cursor-todo-selection-as-state-change))
  18054. (org-inhibit-blocking
  18055. (not org-treat-S-cursor-todo-selection-as-state-change)))
  18056. (org-call-with-arg 'org-todo 'left)))
  18057. ((or (and org-support-shift-select
  18058. (not (eq org-support-shift-select 'always))
  18059. (org-at-item-bullet-p))
  18060. (and (not org-support-shift-select) (org-at-item-p)))
  18061. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  18062. ((and (not (eq org-support-shift-select 'always))
  18063. (org-at-property-p))
  18064. (call-interactively 'org-property-previous-allowed-value))
  18065. ((org-clocktable-try-shift 'left arg))
  18066. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  18067. (org-support-shift-select
  18068. (org-call-for-shift-select 'backward-char))
  18069. (t (org-shiftselect-error))))
  18070. (defun org-shiftcontrolright ()
  18071. "Switch to next TODO set."
  18072. (interactive)
  18073. (cond
  18074. ((and org-support-shift-select (org-region-active-p))
  18075. (org-call-for-shift-select 'forward-word))
  18076. ((and (not (eq org-support-shift-select 'always))
  18077. (org-at-heading-p))
  18078. (org-call-with-arg 'org-todo 'nextset))
  18079. (org-support-shift-select
  18080. (org-call-for-shift-select 'forward-word))
  18081. (t (org-shiftselect-error))))
  18082. (defun org-shiftcontrolleft ()
  18083. "Switch to previous TODO set."
  18084. (interactive)
  18085. (cond
  18086. ((and org-support-shift-select (org-region-active-p))
  18087. (org-call-for-shift-select 'backward-word))
  18088. ((and (not (eq org-support-shift-select 'always))
  18089. (org-at-heading-p))
  18090. (org-call-with-arg 'org-todo 'previousset))
  18091. (org-support-shift-select
  18092. (org-call-for-shift-select 'backward-word))
  18093. (t (org-shiftselect-error))))
  18094. (defun org-shiftcontrolup (&optional n)
  18095. "Change timestamps synchronously up in CLOCK log lines.
  18096. Optional argument N tells to change by that many units."
  18097. (interactive "P")
  18098. (cond ((and (not org-support-shift-select)
  18099. (org-at-clock-log-p)
  18100. (org-at-timestamp-p t))
  18101. (org-clock-timestamps-up n))
  18102. (t (org-shiftselect-error))))
  18103. (defun org-shiftcontroldown (&optional n)
  18104. "Change timestamps synchronously down in CLOCK log lines.
  18105. Optional argument N tells to change by that many units."
  18106. (interactive "P")
  18107. (cond ((and (not org-support-shift-select)
  18108. (org-at-clock-log-p)
  18109. (org-at-timestamp-p t))
  18110. (org-clock-timestamps-down n))
  18111. (t (org-shiftselect-error))))
  18112. (defun org-ctrl-c-ret ()
  18113. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  18114. (interactive)
  18115. (cond
  18116. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  18117. (t (call-interactively 'org-insert-heading))))
  18118. (defun org-find-visible ()
  18119. (let ((s (point)))
  18120. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  18121. (get-char-property s 'invisible)))
  18122. s))
  18123. (defun org-find-invisible ()
  18124. (let ((s (point)))
  18125. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  18126. (not (get-char-property s 'invisible))))
  18127. s))
  18128. (defun org-copy-visible (beg end)
  18129. "Copy the visible parts of the region."
  18130. (interactive "r")
  18131. (let (snippets s)
  18132. (save-excursion
  18133. (save-restriction
  18134. (narrow-to-region beg end)
  18135. (setq s (goto-char (point-min)))
  18136. (while (not (= (point) (point-max)))
  18137. (goto-char (org-find-invisible))
  18138. (push (buffer-substring s (point)) snippets)
  18139. (setq s (goto-char (org-find-visible))))))
  18140. (kill-new (apply 'concat (nreverse snippets)))))
  18141. (defun org-copy-special ()
  18142. "Copy region in table or copy current subtree.
  18143. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  18144. See the individual commands for more information."
  18145. (interactive)
  18146. (call-interactively
  18147. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  18148. (defun org-cut-special ()
  18149. "Cut region in table or cut current subtree.
  18150. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  18151. See the individual commands for more information."
  18152. (interactive)
  18153. (call-interactively
  18154. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  18155. (defun org-paste-special (arg)
  18156. "Paste rectangular region into table, or past subtree relative to level.
  18157. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  18158. See the individual commands for more information."
  18159. (interactive "P")
  18160. (if (org-at-table-p)
  18161. (org-table-paste-rectangle)
  18162. (org-paste-subtree arg)))
  18163. (defsubst org-in-fixed-width-region-p ()
  18164. "Is point in a fixed-width region?"
  18165. (save-match-data
  18166. (eq 'fixed-width (org-element-type (org-element-at-point)))))
  18167. (defun org-edit-special (&optional arg)
  18168. "Call a special editor for the element at point.
  18169. When at a table, call the formula editor with `org-table-edit-formulas'.
  18170. When in a source code block, call `org-edit-src-code'.
  18171. When in a fixed-width region, call `org-edit-fixed-width-region'.
  18172. When at an #+INCLUDE keyword, visit the included file.
  18173. On a link, call `ffap' to visit the link at point.
  18174. Otherwise, return a user error."
  18175. (interactive)
  18176. (let ((element (org-element-at-point)))
  18177. (assert (not buffer-read-only) nil
  18178. "Buffer is read-only: %s" (buffer-name))
  18179. (case (org-element-type element)
  18180. (src-block
  18181. (if (not arg) (org-edit-src-code)
  18182. (let* ((info (org-babel-get-src-block-info))
  18183. (lang (nth 0 info))
  18184. (params (nth 2 info))
  18185. (session (cdr (assq :session params))))
  18186. (if (not session) (org-edit-src-code)
  18187. ;; At a src-block with a session and function called with
  18188. ;; an ARG: switch to the buffer related to the inferior
  18189. ;; process.
  18190. (funcall (intern (concat "org-babel-prep-session:" lang))
  18191. session params)))))
  18192. (keyword
  18193. (if (member (org-element-property :key element) '("INCLUDE" "SETUPFILE"))
  18194. (find-file
  18195. (org-remove-double-quotes
  18196. (car (org-split-string (org-element-property :value element)))))
  18197. (user-error "No special environment to edit here")))
  18198. (table
  18199. (if (eq (org-element-property :type element) 'table.el)
  18200. (org-edit-src-code)
  18201. (call-interactively 'org-table-edit-formulas)))
  18202. ;; Only Org tables contain `table-row' type elements.
  18203. (table-row (call-interactively 'org-table-edit-formulas))
  18204. ((example-block export-block) (org-edit-src-code))
  18205. (fixed-width (org-edit-fixed-width-region))
  18206. (otherwise
  18207. ;; No notable element at point. Though, we may be at a link,
  18208. ;; which is an object. Thus, scan deeper.
  18209. (if (eq (org-element-type (org-element-context element)) 'link)
  18210. (call-interactively 'ffap)
  18211. (user-error "No special environment to edit here"))))))
  18212. (defvar org-table-coordinate-overlays) ; defined in org-table.el
  18213. (defun org-ctrl-c-ctrl-c (&optional arg)
  18214. "Set tags in headline, or update according to changed information at point.
  18215. This command does many different things, depending on context:
  18216. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  18217. this is what we do.
  18218. - If the cursor is on a statistics cookie, update it.
  18219. - If the cursor is in a headline, prompt for tags and insert them
  18220. into the current line, aligned to `org-tags-column'. When called
  18221. with prefix arg, realign all tags in the current buffer.
  18222. - If the cursor is in one of the special #+KEYWORD lines, this
  18223. triggers scanning the buffer for these lines and updating the
  18224. information.
  18225. - If the cursor is inside a table, realign the table. This command
  18226. works even if the automatic table editor has been turned off.
  18227. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  18228. the entire table.
  18229. - If the cursor is at a footnote reference or definition, jump to
  18230. the corresponding definition or references, respectively.
  18231. - If the cursor is a the beginning of a dynamic block, update it.
  18232. - If the current buffer is a capture buffer, close note and file it.
  18233. - If the cursor is on a <<<target>>>, update radio targets and
  18234. corresponding links in this buffer.
  18235. - If the cursor is on a numbered item in a plain list, renumber the
  18236. ordered list.
  18237. - If the cursor is on a checkbox, toggle it.
  18238. - If the cursor is on a code block, evaluate it. The variable
  18239. `org-confirm-babel-evaluate' can be used to control prompting
  18240. before code block evaluation, by default every code block
  18241. evaluation requires confirmation. Code block evaluation can be
  18242. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  18243. (interactive "P")
  18244. (cond
  18245. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  18246. org-occur-highlights
  18247. org-latex-fragment-image-overlays)
  18248. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  18249. (org-remove-occur-highlights)
  18250. (org-remove-latex-fragment-image-overlays)
  18251. (message "Temporary highlights/overlays removed from current buffer"))
  18252. ((and (local-variable-p 'org-finish-function (current-buffer))
  18253. (fboundp org-finish-function))
  18254. (funcall org-finish-function))
  18255. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  18256. (t
  18257. (let* ((context (org-element-context)) (type (org-element-type context)))
  18258. ;; Test if point is within a blank line.
  18259. (if (save-excursion (beginning-of-line) (looking-at "[ \t]*$"))
  18260. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  18261. (user-error "C-c C-c can do nothing useful at this location"))
  18262. ;; For convenience: at the first line of a paragraph on the
  18263. ;; same line as an item, apply function on that item instead.
  18264. (when (eq type 'paragraph)
  18265. (let ((parent (org-element-property :parent context)))
  18266. (when (and (eq (org-element-type parent) 'item)
  18267. (= (point-at-bol) (org-element-property :begin parent)))
  18268. (setq context parent type 'item))))
  18269. ;; Act according to type of element or object at point.
  18270. (case type
  18271. (clock (org-clock-update-time-maybe))
  18272. (dynamic-block
  18273. (save-excursion
  18274. (goto-char (org-element-property :post-affiliated context))
  18275. (org-update-dblock)))
  18276. (footnote-definition
  18277. (goto-char (org-element-property :post-affiliated context))
  18278. (call-interactively 'org-footnote-action))
  18279. (footnote-reference (call-interactively 'org-footnote-action))
  18280. ((headline inlinetask)
  18281. (save-excursion (goto-char (org-element-property :begin context))
  18282. (call-interactively 'org-set-tags)))
  18283. (item
  18284. ;; At an item: a double C-u set checkbox to "[-]"
  18285. ;; unconditionally, whereas a single one will toggle its
  18286. ;; presence. Without an universal argument, if the item
  18287. ;; has a checkbox, toggle it. Otherwise repair the list.
  18288. (let* ((box (org-element-property :checkbox context))
  18289. (struct (org-element-property :structure context))
  18290. (old-struct (copy-tree struct))
  18291. (parents (org-list-parents-alist struct))
  18292. (prevs (org-list-prevs-alist struct))
  18293. (orderedp (org-not-nil (org-entry-get nil "ORDERED"))))
  18294. (org-list-set-checkbox
  18295. (org-element-property :begin context) struct
  18296. (cond ((equal arg '(16)) "[-]")
  18297. ((and (not box) (equal arg '(4))) "[ ]")
  18298. ((or (not box) (equal arg '(4))) nil)
  18299. ((eq box 'on) "[ ]")
  18300. (t "[X]")))
  18301. ;; Mimic `org-list-write-struct' but with grabbing
  18302. ;; a return value from `org-list-struct-fix-box'.
  18303. (org-list-struct-fix-ind struct parents 2)
  18304. (org-list-struct-fix-item-end struct)
  18305. (org-list-struct-fix-bul struct prevs)
  18306. (org-list-struct-fix-ind struct parents)
  18307. (let ((block-item
  18308. (org-list-struct-fix-box struct parents prevs orderedp)))
  18309. (if (and box (equal struct old-struct))
  18310. (if (equal arg '(16))
  18311. (message "Checkboxes already reset")
  18312. (user-error "Cannot toggle this checkbox: %s"
  18313. (if (eq box 'on)
  18314. "all subitems checked"
  18315. "unchecked subitems")))
  18316. (org-list-struct-apply-struct struct old-struct)
  18317. (org-update-checkbox-count-maybe))
  18318. (when block-item
  18319. (message "Checkboxes were removed due to empty box at line %d"
  18320. (org-current-line block-item))))))
  18321. (keyword
  18322. (let ((org-inhibit-startup-visibility-stuff t)
  18323. (org-startup-align-all-tables nil))
  18324. (when (boundp 'org-table-coordinate-overlays)
  18325. (mapc 'delete-overlay org-table-coordinate-overlays)
  18326. (setq org-table-coordinate-overlays nil))
  18327. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  18328. (message "Local setup has been refreshed"))
  18329. (plain-list
  18330. ;; At a plain list, with a double C-u argument, set
  18331. ;; checkboxes of each item to "[-]", whereas a single one
  18332. ;; will toggle their presence according to the state of the
  18333. ;; first item in the list. Without an argument, repair the
  18334. ;; list.
  18335. (let* ((begin (org-element-property :contents-begin context))
  18336. (struct (org-element-property :structure context))
  18337. (old-struct (copy-tree struct))
  18338. (first-box (save-excursion
  18339. (goto-char begin)
  18340. (looking-at org-list-full-item-re)
  18341. (match-string-no-properties 3)))
  18342. (new-box (cond ((equal arg '(16)) "[-]")
  18343. ((equal arg '(4)) (unless first-box "[ ]"))
  18344. ((equal first-box "[X]") "[ ]")
  18345. (t "[X]"))))
  18346. (cond
  18347. (arg
  18348. (mapc (lambda (pos) (org-list-set-checkbox pos struct new-box))
  18349. (org-list-get-all-items
  18350. begin struct (org-list-prevs-alist struct))))
  18351. ((and first-box (eq (point) begin))
  18352. ;; For convenience, when point is at bol on the first
  18353. ;; item of the list and no argument is provided, simply
  18354. ;; toggle checkbox of that item, if any.
  18355. (org-list-set-checkbox begin struct new-box)))
  18356. (org-list-write-struct
  18357. struct (org-list-parents-alist struct) old-struct)
  18358. (org-update-checkbox-count-maybe)
  18359. (save-excursion (goto-char begin) (org-list-send-list 'maybe))))
  18360. ((property-drawer node-property)
  18361. (call-interactively 'org-property-action))
  18362. ((radio-target target)
  18363. (call-interactively 'org-update-radio-target-regexp))
  18364. (statistics-cookie
  18365. (call-interactively 'org-update-statistics-cookies))
  18366. ((table table-cell table-row)
  18367. ;; At a table, recalculate every field and align it. Also
  18368. ;; send the table if necessary. If the table has
  18369. ;; a `table.el' type, just give up. At a table row or
  18370. ;; cell, maybe recalculate line but always align table.
  18371. (if (eq (org-element-property :type context) 'table.el)
  18372. (message "Use C-c ' to edit table.el tables")
  18373. (let ((org-enable-table-editor t))
  18374. (if (or (eq type 'table)
  18375. ;; Check if point is at a TBLFM line.
  18376. (and (eq type 'table-row)
  18377. (= (point) (org-element-property :end context))))
  18378. (save-excursion
  18379. (goto-char (org-element-property :contents-begin context))
  18380. (org-call-with-arg 'org-table-recalculate (or arg t))
  18381. (orgtbl-send-table 'maybe))
  18382. (org-table-maybe-eval-formula)
  18383. (cond (arg (call-interactively 'org-table-recalculate))
  18384. ((org-table-maybe-recalculate-line))
  18385. (t (org-table-align)))))))
  18386. (timestamp (org-timestamp-change 0 'day))
  18387. (otherwise
  18388. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  18389. (user-error
  18390. "C-c C-c can do nothing useful at this location")))))))))
  18391. (defun org-mode-restart ()
  18392. "Restart Org-mode, to scan again for special lines.
  18393. Also updates the keyword regular expressions."
  18394. (interactive)
  18395. (org-mode)
  18396. (message "Org-mode restarted"))
  18397. (defun org-kill-note-or-show-branches ()
  18398. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  18399. (interactive)
  18400. (if (not org-finish-function)
  18401. (progn
  18402. (hide-subtree)
  18403. (call-interactively 'show-branches))
  18404. (let ((org-note-abort t))
  18405. (funcall org-finish-function))))
  18406. (defun org-return (&optional indent)
  18407. "Goto next table row or insert a newline.
  18408. Calls `org-table-next-row' or `newline', depending on context.
  18409. See the individual commands for more information."
  18410. (interactive)
  18411. (let (org-ts-what)
  18412. (cond
  18413. ((or (bobp) (org-in-src-block-p))
  18414. (if indent (newline-and-indent) (newline)))
  18415. ((org-at-table-p)
  18416. (org-table-justify-field-maybe)
  18417. (call-interactively 'org-table-next-row))
  18418. ;; when `newline-and-indent' is called within a list, make sure
  18419. ;; text moved stays inside the item.
  18420. ((and (org-in-item-p) indent)
  18421. (if (and (org-at-item-p) (>= (point) (match-end 0)))
  18422. (progn
  18423. (save-match-data (newline))
  18424. (org-indent-line-to (length (match-string 0))))
  18425. (let ((ind (org-get-indentation)))
  18426. (newline)
  18427. (if (org-looking-back org-list-end-re)
  18428. (org-indent-line)
  18429. (org-indent-line-to ind)))))
  18430. ((and org-return-follows-link
  18431. (org-at-timestamp-p t)
  18432. (not (eq org-ts-what 'after)))
  18433. (org-follow-timestamp-link))
  18434. ((and org-return-follows-link
  18435. (let ((tprop (get-text-property (point) 'face)))
  18436. (or (eq tprop 'org-link)
  18437. (and (listp tprop) (memq 'org-link tprop)))))
  18438. (call-interactively 'org-open-at-point))
  18439. ((and (org-at-heading-p)
  18440. (looking-at
  18441. (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
  18442. (org-show-entry)
  18443. (end-of-line 1)
  18444. (newline))
  18445. (t (if indent (newline-and-indent) (newline))))))
  18446. (defun org-return-indent ()
  18447. "Goto next table row or insert a newline and indent.
  18448. Calls `org-table-next-row' or `newline-and-indent', depending on
  18449. context. See the individual commands for more information."
  18450. (interactive)
  18451. (org-return t))
  18452. (defun org-ctrl-c-star ()
  18453. "Compute table, or change heading status of lines.
  18454. Calls `org-table-recalculate' or `org-toggle-heading',
  18455. depending on context."
  18456. (interactive)
  18457. (cond
  18458. ((org-at-table-p)
  18459. (call-interactively 'org-table-recalculate))
  18460. (t
  18461. ;; Convert all lines in region to list items
  18462. (call-interactively 'org-toggle-heading))))
  18463. (defun org-ctrl-c-minus ()
  18464. "Insert separator line in table or modify bullet status of line.
  18465. Also turns a plain line or a region of lines into list items.
  18466. Calls `org-table-insert-hline', `org-toggle-item', or
  18467. `org-cycle-list-bullet', depending on context."
  18468. (interactive)
  18469. (cond
  18470. ((org-at-table-p)
  18471. (call-interactively 'org-table-insert-hline))
  18472. ((org-region-active-p)
  18473. (call-interactively 'org-toggle-item))
  18474. ((org-in-item-p)
  18475. (call-interactively 'org-cycle-list-bullet))
  18476. (t
  18477. (call-interactively 'org-toggle-item))))
  18478. (defun org-toggle-item (arg)
  18479. "Convert headings or normal lines to items, items to normal lines.
  18480. If there is no active region, only the current line is considered.
  18481. If the first non blank line in the region is a headline, convert
  18482. all headlines to items, shifting text accordingly.
  18483. If it is an item, convert all items to normal lines.
  18484. If it is normal text, change region into a list of items.
  18485. With a prefix argument ARG, change the region in a single item."
  18486. (interactive "P")
  18487. (let ((shift-text
  18488. (function
  18489. ;; Shift text in current section to IND, from point to END.
  18490. ;; The function leaves point to END line.
  18491. (lambda (ind end)
  18492. (let ((min-i 1000) (end (copy-marker end)))
  18493. ;; First determine the minimum indentation (MIN-I) of
  18494. ;; the text.
  18495. (save-excursion
  18496. (catch 'exit
  18497. (while (< (point) end)
  18498. (let ((i (org-get-indentation)))
  18499. (cond
  18500. ;; Skip blank lines and inline tasks.
  18501. ((looking-at "^[ \t]*$"))
  18502. ((looking-at org-outline-regexp-bol))
  18503. ;; We can't find less than 0 indentation.
  18504. ((zerop i) (throw 'exit (setq min-i 0)))
  18505. ((< i min-i) (setq min-i i))))
  18506. (forward-line))))
  18507. ;; Then indent each line so that a line indented to
  18508. ;; MIN-I becomes indented to IND. Ignore blank lines
  18509. ;; and inline tasks in the process.
  18510. (let ((delta (- ind min-i)))
  18511. (while (< (point) end)
  18512. (unless (or (looking-at "^[ \t]*$")
  18513. (looking-at org-outline-regexp-bol))
  18514. (org-indent-line-to (+ (org-get-indentation) delta)))
  18515. (forward-line)))))))
  18516. (skip-blanks
  18517. (function
  18518. ;; Return beginning of first non-blank line, starting from
  18519. ;; line at POS.
  18520. (lambda (pos)
  18521. (save-excursion
  18522. (goto-char pos)
  18523. (skip-chars-forward " \r\t\n")
  18524. (point-at-bol)))))
  18525. beg end)
  18526. ;; Determine boundaries of changes.
  18527. (if (org-region-active-p)
  18528. (setq beg (funcall skip-blanks (region-beginning))
  18529. end (copy-marker (region-end)))
  18530. (setq beg (funcall skip-blanks (point-at-bol))
  18531. end (copy-marker (point-at-eol))))
  18532. ;; Depending on the starting line, choose an action on the text
  18533. ;; between BEG and END.
  18534. (org-with-limited-levels
  18535. (save-excursion
  18536. (goto-char beg)
  18537. (cond
  18538. ;; Case 1. Start at an item: de-itemize. Note that it only
  18539. ;; happens when a region is active: `org-ctrl-c-minus'
  18540. ;; would call `org-cycle-list-bullet' otherwise.
  18541. ((org-at-item-p)
  18542. (while (< (point) end)
  18543. (when (org-at-item-p)
  18544. (skip-chars-forward " \t")
  18545. (delete-region (point) (match-end 0)))
  18546. (forward-line)))
  18547. ;; Case 2. Start at an heading: convert to items.
  18548. ((org-at-heading-p)
  18549. (let* ((bul (org-list-bullet-string "-"))
  18550. (bul-len (length bul))
  18551. ;; Indentation of the first heading. It should be
  18552. ;; relative to the indentation of its parent, if any.
  18553. (start-ind (save-excursion
  18554. (cond
  18555. ((not org-adapt-indentation) 0)
  18556. ((not (outline-previous-heading)) 0)
  18557. (t (length (match-string 0))))))
  18558. ;; Level of first heading. Further headings will be
  18559. ;; compared to it to determine hierarchy in the list.
  18560. (ref-level (org-reduced-level (org-outline-level))))
  18561. (while (< (point) end)
  18562. (let* ((level (org-reduced-level (org-outline-level)))
  18563. (delta (max 0 (- level ref-level))))
  18564. ;; If current headline is less indented than the first
  18565. ;; one, set it as reference, in order to preserve
  18566. ;; subtrees.
  18567. (when (< level ref-level) (setq ref-level level))
  18568. (replace-match bul t t)
  18569. (org-indent-line-to (+ start-ind (* delta bul-len)))
  18570. ;; Ensure all text down to END (or SECTION-END) belongs
  18571. ;; to the newly created item.
  18572. (let ((section-end (save-excursion
  18573. (or (outline-next-heading) (point)))))
  18574. (forward-line)
  18575. (funcall shift-text
  18576. (+ start-ind (* (1+ delta) bul-len))
  18577. (min end section-end)))))))
  18578. ;; Case 3. Normal line with ARG: make the first line of region
  18579. ;; an item, and shift indentation of others lines to
  18580. ;; set them as item's body.
  18581. (arg (let* ((bul (org-list-bullet-string "-"))
  18582. (bul-len (length bul))
  18583. (ref-ind (org-get-indentation)))
  18584. (skip-chars-forward " \t")
  18585. (insert bul)
  18586. (forward-line)
  18587. (while (< (point) end)
  18588. ;; Ensure that lines less indented than first one
  18589. ;; still get included in item body.
  18590. (funcall shift-text
  18591. (+ ref-ind bul-len)
  18592. (min end (save-excursion (or (outline-next-heading)
  18593. (point)))))
  18594. (forward-line))))
  18595. ;; Case 4. Normal line without ARG: turn each non-item line
  18596. ;; into an item.
  18597. (t
  18598. (while (< (point) end)
  18599. (unless (or (org-at-heading-p) (org-at-item-p))
  18600. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  18601. (replace-match
  18602. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  18603. (forward-line))))))))
  18604. (defun org-toggle-heading (&optional nstars)
  18605. "Convert headings to normal text, or items or text to headings.
  18606. If there is no active region, only convert the current line.
  18607. With a \\[universal-argument] prefix, convert the whole list at
  18608. point into heading.
  18609. In a region:
  18610. - If the first non blank line is a headline, remove the stars
  18611. from all headlines in the region.
  18612. - If it is a normal line, turn each and every normal line (i.e.,
  18613. not an heading or an item) in the region into headings. If you
  18614. want to convert only the first line of this region, use one
  18615. universal prefix argument.
  18616. - If it is a plain list item, turn all plain list items into headings.
  18617. When converting a line into a heading, the number of stars is chosen
  18618. such that the lines become children of the current entry. However,
  18619. when a numeric prefix argument is given, its value determines the
  18620. number of stars to add."
  18621. (interactive "P")
  18622. (let ((skip-blanks
  18623. (function
  18624. ;; Return beginning of first non-blank line, starting from
  18625. ;; line at POS.
  18626. (lambda (pos)
  18627. (save-excursion
  18628. (goto-char pos)
  18629. (while (org-at-comment-p) (forward-line))
  18630. (skip-chars-forward " \r\t\n")
  18631. (point-at-bol)))))
  18632. beg end toggled)
  18633. ;; Determine boundaries of changes. If a universal prefix has
  18634. ;; been given, put the list in a region. If region ends at a bol,
  18635. ;; do not consider the last line to be in the region.
  18636. (when (and current-prefix-arg (org-at-item-p))
  18637. (if (listp current-prefix-arg) (setq current-prefix-arg 1))
  18638. (org-mark-element))
  18639. (if (org-region-active-p)
  18640. (setq beg (funcall skip-blanks (region-beginning))
  18641. end (copy-marker (save-excursion
  18642. (goto-char (region-end))
  18643. (if (bolp) (point) (point-at-eol)))))
  18644. (setq beg (funcall skip-blanks (point-at-bol))
  18645. end (copy-marker (point-at-eol))))
  18646. ;; Ensure inline tasks don't count as headings.
  18647. (org-with-limited-levels
  18648. (save-excursion
  18649. (goto-char beg)
  18650. (cond
  18651. ;; Case 1. Started at an heading: de-star headings.
  18652. ((org-at-heading-p)
  18653. (while (< (point) end)
  18654. (when (org-at-heading-p t)
  18655. (looking-at org-outline-regexp) (replace-match "")
  18656. (setq toggled t))
  18657. (forward-line)))
  18658. ;; Case 2. Started at an item: change items into headlines.
  18659. ;; One star will be added by `org-list-to-subtree'.
  18660. ((org-at-item-p)
  18661. (let* ((stars (make-string
  18662. ;; subtract the star that will be added again by
  18663. ;; `org-list-to-subtree'
  18664. (if (numberp nstars) (1- nstars)
  18665. (or (org-current-level) 0))
  18666. ?*))
  18667. (add-stars
  18668. (cond (nstars "") ; stars from prefix only
  18669. ((equal stars "") "") ; before first heading
  18670. (org-odd-levels-only "*") ; inside heading, odd
  18671. (t "")))) ; inside heading, oddeven
  18672. (while (< (point) end)
  18673. (when (org-at-item-p)
  18674. ;; Pay attention to cases when region ends before list.
  18675. (let* ((struct (org-list-struct))
  18676. (list-end (min (org-list-get-bottom-point struct) (1+ end))))
  18677. (save-restriction
  18678. (narrow-to-region (point) list-end)
  18679. (insert
  18680. (org-list-to-subtree
  18681. (org-list-parse-list t)
  18682. '(:istart (concat stars add-stars (funcall get-stars depth))
  18683. :icount (concat stars add-stars (funcall get-stars depth)))))))
  18684. (setq toggled t))
  18685. (forward-line))))
  18686. ;; Case 3. Started at normal text: make every line an heading,
  18687. ;; skipping headlines and items.
  18688. (t (let* ((stars
  18689. (make-string
  18690. (if (numberp nstars) nstars (or (org-current-level) 0)) ?*))
  18691. (add-stars
  18692. (cond (nstars "") ; stars from prefix only
  18693. ((equal stars "") "*") ; before first heading
  18694. (org-odd-levels-only "**") ; inside heading, odd
  18695. (t "*"))) ; inside heading, oddeven
  18696. (rpl (concat stars add-stars " "))
  18697. (lend (if (listp nstars) (save-excursion (end-of-line) (point)))))
  18698. (while (< (point) (if (equal nstars '(4)) lend end))
  18699. (when (and (not (or (org-at-heading-p) (org-at-item-p) (org-at-comment-p)))
  18700. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  18701. (replace-match (concat rpl (match-string 2))) (setq toggled t))
  18702. (forward-line)))))))
  18703. (unless toggled (message "Cannot toggle heading from here"))))
  18704. (defun org-meta-return (&optional arg)
  18705. "Insert a new heading or wrap a region in a table.
  18706. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  18707. See the individual commands for more information."
  18708. (interactive "P")
  18709. (cond
  18710. ((run-hook-with-args-until-success 'org-metareturn-hook))
  18711. ((or (org-at-drawer-p) (org-at-property-p))
  18712. (newline-and-indent))
  18713. ((org-at-table-p)
  18714. (call-interactively 'org-table-wrap-region))
  18715. (t (call-interactively 'org-insert-heading))))
  18716. ;;; Menu entries
  18717. (defsubst org-in-subtree-not-table-p ()
  18718. "Are we in a subtree and not in a table?"
  18719. (and (not (org-before-first-heading-p))
  18720. (not (org-at-table-p))))
  18721. ;; Define the Org-mode menus
  18722. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  18723. '("Tbl"
  18724. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  18725. ["Next Field" org-cycle (org-at-table-p)]
  18726. ["Previous Field" org-shifttab (org-at-table-p)]
  18727. ["Next Row" org-return (org-at-table-p)]
  18728. "--"
  18729. ["Blank Field" org-table-blank-field (org-at-table-p)]
  18730. ["Edit Field" org-table-edit-field (org-at-table-p)]
  18731. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  18732. "--"
  18733. ("Column"
  18734. ["Move Column Left" org-metaleft (org-at-table-p)]
  18735. ["Move Column Right" org-metaright (org-at-table-p)]
  18736. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  18737. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  18738. ("Row"
  18739. ["Move Row Up" org-metaup (org-at-table-p)]
  18740. ["Move Row Down" org-metadown (org-at-table-p)]
  18741. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  18742. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  18743. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  18744. "--"
  18745. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  18746. ("Rectangle"
  18747. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  18748. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  18749. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  18750. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  18751. "--"
  18752. ("Calculate"
  18753. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  18754. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  18755. ["Edit Formulas" org-edit-special (org-at-table-p)]
  18756. "--"
  18757. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  18758. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  18759. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  18760. "--"
  18761. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  18762. "--"
  18763. ["Sum Column/Rectangle" org-table-sum
  18764. (or (org-at-table-p) (org-region-active-p))]
  18765. ["Which Column?" org-table-current-column (org-at-table-p)])
  18766. ["Debug Formulas"
  18767. org-table-toggle-formula-debugger
  18768. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  18769. ["Show Col/Row Numbers"
  18770. org-table-toggle-coordinate-overlays
  18771. :style toggle
  18772. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  18773. "--"
  18774. ["Create" org-table-create (and (not (org-at-table-p))
  18775. org-enable-table-editor)]
  18776. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  18777. ["Import from File" org-table-import (not (org-at-table-p))]
  18778. ["Export to File" org-table-export (org-at-table-p)]
  18779. "--"
  18780. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  18781. (easy-menu-define org-org-menu org-mode-map "Org menu"
  18782. '("Org"
  18783. ("Show/Hide"
  18784. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  18785. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  18786. ["Sparse Tree..." org-sparse-tree t]
  18787. ["Reveal Context" org-reveal t]
  18788. ["Show All" show-all t]
  18789. "--"
  18790. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  18791. "--"
  18792. ["New Heading" org-insert-heading t]
  18793. ("Navigate Headings"
  18794. ["Up" outline-up-heading t]
  18795. ["Next" outline-next-visible-heading t]
  18796. ["Previous" outline-previous-visible-heading t]
  18797. ["Next Same Level" outline-forward-same-level t]
  18798. ["Previous Same Level" outline-backward-same-level t]
  18799. "--"
  18800. ["Jump" org-goto t])
  18801. ("Edit Structure"
  18802. ["Refile Subtree" org-refile (org-in-subtree-not-table-p)]
  18803. "--"
  18804. ["Move Subtree Up" org-shiftmetaup (org-in-subtree-not-table-p)]
  18805. ["Move Subtree Down" org-shiftmetadown (org-in-subtree-not-table-p)]
  18806. "--"
  18807. ["Copy Subtree" org-copy-special (org-in-subtree-not-table-p)]
  18808. ["Cut Subtree" org-cut-special (org-in-subtree-not-table-p)]
  18809. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  18810. "--"
  18811. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  18812. "--"
  18813. ["Copy visible text" org-copy-visible t]
  18814. "--"
  18815. ["Promote Heading" org-metaleft (org-in-subtree-not-table-p)]
  18816. ["Promote Subtree" org-shiftmetaleft (org-in-subtree-not-table-p)]
  18817. ["Demote Heading" org-metaright (org-in-subtree-not-table-p)]
  18818. ["Demote Subtree" org-shiftmetaright (org-in-subtree-not-table-p)]
  18819. "--"
  18820. ["Sort Region/Children" org-sort t]
  18821. "--"
  18822. ["Convert to odd levels" org-convert-to-odd-levels t]
  18823. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  18824. ("Editing"
  18825. ["Emphasis..." org-emphasize t]
  18826. ["Edit Source Example" org-edit-special t]
  18827. "--"
  18828. ["Footnote new/jump" org-footnote-action t]
  18829. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  18830. ("Archive"
  18831. ["Archive (default method)" org-archive-subtree-default (org-in-subtree-not-table-p)]
  18832. "--"
  18833. ["Move Subtree to Archive file" org-advertized-archive-subtree (org-in-subtree-not-table-p)]
  18834. ["Toggle ARCHIVE tag" org-toggle-archive-tag (org-in-subtree-not-table-p)]
  18835. ["Move subtree to Archive sibling" org-archive-to-archive-sibling (org-in-subtree-not-table-p)]
  18836. )
  18837. "--"
  18838. ("Hyperlinks"
  18839. ["Store Link (Global)" org-store-link t]
  18840. ["Find existing link to here" org-occur-link-in-agenda-files t]
  18841. ["Insert Link" org-insert-link t]
  18842. ["Follow Link" org-open-at-point t]
  18843. "--"
  18844. ["Next link" org-next-link t]
  18845. ["Previous link" org-previous-link t]
  18846. "--"
  18847. ["Descriptive Links"
  18848. org-toggle-link-display
  18849. :style radio
  18850. :selected org-descriptive-links
  18851. ]
  18852. ["Literal Links"
  18853. org-toggle-link-display
  18854. :style radio
  18855. :selected (not org-descriptive-links)])
  18856. "--"
  18857. ("TODO Lists"
  18858. ["TODO/DONE/-" org-todo t]
  18859. ("Select keyword"
  18860. ["Next keyword" org-shiftright (org-at-heading-p)]
  18861. ["Previous keyword" org-shiftleft (org-at-heading-p)]
  18862. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  18863. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
  18864. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
  18865. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  18866. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  18867. "--"
  18868. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  18869. :selected org-enforce-todo-dependencies :style toggle :active t]
  18870. "Settings for tree at point"
  18871. ["Do Children sequentially" org-toggle-ordered-property :style radio
  18872. :selected (org-entry-get nil "ORDERED")
  18873. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  18874. ["Do Children parallel" org-toggle-ordered-property :style radio
  18875. :selected (not (org-entry-get nil "ORDERED"))
  18876. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  18877. "--"
  18878. ["Set Priority" org-priority t]
  18879. ["Priority Up" org-shiftup t]
  18880. ["Priority Down" org-shiftdown t]
  18881. "--"
  18882. ["Get news from all feeds" org-feed-update-all t]
  18883. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  18884. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  18885. ("TAGS and Properties"
  18886. ["Set Tags" org-set-tags-command (not (org-before-first-heading-p))]
  18887. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  18888. "--"
  18889. ["Set property" org-set-property (not (org-before-first-heading-p))]
  18890. ["Column view of properties" org-columns t]
  18891. ["Insert Column View DBlock" org-insert-columns-dblock t])
  18892. ("Dates and Scheduling"
  18893. ["Timestamp" org-time-stamp (not (org-before-first-heading-p))]
  18894. ["Timestamp (inactive)" org-time-stamp-inactive (not (org-before-first-heading-p))]
  18895. ("Change Date"
  18896. ["1 Day Later" org-shiftright (org-at-timestamp-p)]
  18897. ["1 Day Earlier" org-shiftleft (org-at-timestamp-p)]
  18898. ["1 ... Later" org-shiftup (org-at-timestamp-p)]
  18899. ["1 ... Earlier" org-shiftdown (org-at-timestamp-p)])
  18900. ["Compute Time Range" org-evaluate-time-range t]
  18901. ["Schedule Item" org-schedule (not (org-before-first-heading-p))]
  18902. ["Deadline" org-deadline (not (org-before-first-heading-p))]
  18903. "--"
  18904. ["Custom time format" org-toggle-time-stamp-overlays
  18905. :style radio :selected org-display-custom-times]
  18906. "--"
  18907. ["Goto Calendar" org-goto-calendar t]
  18908. ["Date from Calendar" org-date-from-calendar t]
  18909. "--"
  18910. ["Start/Restart Timer" org-timer-start t]
  18911. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  18912. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  18913. ["Insert Timer String" org-timer t]
  18914. ["Insert Timer Item" org-timer-item t])
  18915. ("Logging work"
  18916. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  18917. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  18918. ["Clock out" org-clock-out t]
  18919. ["Clock cancel" org-clock-cancel t]
  18920. "--"
  18921. ["Mark as default task" org-clock-mark-default-task t]
  18922. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  18923. ["Goto running clock" org-clock-goto t]
  18924. "--"
  18925. ["Display times" org-clock-display t]
  18926. ["Create clock table" org-clock-report t]
  18927. "--"
  18928. ["Record DONE time"
  18929. (progn (setq org-log-done (not org-log-done))
  18930. (message "Switching to %s will %s record a timestamp"
  18931. (car org-done-keywords)
  18932. (if org-log-done "automatically" "not")))
  18933. :style toggle :selected org-log-done])
  18934. "--"
  18935. ["Agenda Command..." org-agenda t]
  18936. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  18937. ("File List for Agenda")
  18938. ("Special views current file"
  18939. ["TODO Tree" org-show-todo-tree t]
  18940. ["Check Deadlines" org-check-deadlines t]
  18941. ["Timeline" org-timeline t]
  18942. ["Tags/Property tree" org-match-sparse-tree t])
  18943. "--"
  18944. ["Export/Publish..." org-export-dispatch t]
  18945. ("LaTeX"
  18946. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  18947. :selected org-cdlatex-mode]
  18948. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  18949. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  18950. ["Modify math symbol" org-cdlatex-math-modify
  18951. (org-inside-LaTeX-fragment-p)]
  18952. ["Insert citation" org-reftex-citation t]
  18953. "--"
  18954. ["Template for BEAMER" (org-beamer-insert-options-template) t])
  18955. "--"
  18956. ("MobileOrg"
  18957. ["Push Files and Views" org-mobile-push t]
  18958. ["Get Captured and Flagged" org-mobile-pull t]
  18959. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  18960. "--"
  18961. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  18962. "--"
  18963. ("Documentation"
  18964. ["Show Version" org-version t]
  18965. ["Info Documentation" org-info t])
  18966. ("Customize"
  18967. ["Browse Org Group" org-customize t]
  18968. "--"
  18969. ["Expand This Menu" org-create-customize-menu
  18970. (fboundp 'customize-menu-create)])
  18971. ["Send bug report" org-submit-bug-report t]
  18972. "--"
  18973. ("Refresh/Reload"
  18974. ["Refresh setup current buffer" org-mode-restart t]
  18975. ["Reload Org (after update)" org-reload t]
  18976. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x !"])
  18977. ))
  18978. (defun org-info (&optional node)
  18979. "Read documentation for Org-mode in the info system.
  18980. With optional NODE, go directly to that node."
  18981. (interactive)
  18982. (info (format "(org)%s" (or node ""))))
  18983. ;;;###autoload
  18984. (defun org-submit-bug-report ()
  18985. "Submit a bug report on Org-mode via mail.
  18986. Don't hesitate to report any problems or inaccurate documentation.
  18987. If you don't have setup sending mail from (X)Emacs, please copy the
  18988. output buffer into your mail program, as it gives us important
  18989. information about your Org-mode version and configuration."
  18990. (interactive)
  18991. (require 'reporter)
  18992. (org-load-modules-maybe)
  18993. (org-require-autoloaded-modules)
  18994. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  18995. (reporter-submit-bug-report
  18996. "emacs-orgmode@gnu.org"
  18997. (org-version nil 'full)
  18998. (let (list)
  18999. (save-window-excursion
  19000. (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
  19001. (delete-other-windows)
  19002. (erase-buffer)
  19003. (insert "You are about to submit a bug report to the Org-mode mailing list.
  19004. We would like to add your full Org-mode and Outline configuration to the
  19005. bug report. This greatly simplifies the work of the maintainer and
  19006. other experts on the mailing list.
  19007. HOWEVER, some variables you have customized may contain private
  19008. information. The names of customers, colleagues, or friends, might
  19009. appear in the form of file names, tags, todo states, or search strings.
  19010. If you answer yes to the prompt, you might want to check and remove
  19011. such private information before sending the email.")
  19012. (add-text-properties (point-min) (point-max) '(face org-warning))
  19013. (when (yes-or-no-p "Include your Org-mode configuration ")
  19014. (mapatoms
  19015. (lambda (v)
  19016. (and (boundp v)
  19017. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  19018. (or (and (symbol-value v)
  19019. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  19020. (and
  19021. (get v 'custom-type) (get v 'standard-value)
  19022. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  19023. (push v list)))))
  19024. (kill-buffer (get-buffer "*Warn about privacy*"))
  19025. list))
  19026. nil nil
  19027. "Remember to cover the basics, that is, what you expected to happen and
  19028. what in fact did happen. You don't know how to make a good report? See
  19029. http://orgmode.org/manual/Feedback.html#Feedback
  19030. Your bug report will be posted to the Org-mode mailing list.
  19031. ------------------------------------------------------------------------")
  19032. (save-excursion
  19033. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  19034. (replace-match "\\1Bug: \\3 [\\2]")))))
  19035. (defun org-install-agenda-files-menu ()
  19036. (let ((bl (buffer-list)))
  19037. (save-excursion
  19038. (while bl
  19039. (set-buffer (pop bl))
  19040. (if (derived-mode-p 'org-mode) (setq bl nil)))
  19041. (when (derived-mode-p 'org-mode)
  19042. (easy-menu-change
  19043. '("Org") "File List for Agenda"
  19044. (append
  19045. (list
  19046. ["Edit File List" (org-edit-agenda-file-list) t]
  19047. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  19048. ["Remove Current File from List" org-remove-file t]
  19049. ["Cycle through agenda files" org-cycle-agenda-files t]
  19050. ["Occur in all agenda files" org-occur-in-agenda-files t]
  19051. "--")
  19052. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  19053. ;;;; Documentation
  19054. (defun org-require-autoloaded-modules ()
  19055. (interactive)
  19056. (mapc 'require
  19057. '(org-agenda org-archive org-attach org-clock org-colview org-id
  19058. org-remember org-table org-timer)))
  19059. ;;;###autoload
  19060. (defun org-reload (&optional uncompiled)
  19061. "Reload all org lisp files.
  19062. With prefix arg UNCOMPILED, load the uncompiled versions."
  19063. (interactive "P")
  19064. (require 'loadhist)
  19065. (let* ((org-dir (org-find-library-dir "org"))
  19066. (contrib-dir (or (org-find-library-dir "org-contribdir") org-dir))
  19067. (feature-re "^\\(org\\|ob\\|ox\\)\\(-.*\\)?")
  19068. (remove-re (mapconcat 'identity
  19069. (mapcar (lambda (f) (concat "^" f "$"))
  19070. (list (if (featurep 'xemacs)
  19071. "org-colview"
  19072. "org-colview-xemacs")
  19073. "org" "org-loaddefs" "org-version"))
  19074. "\\|"))
  19075. (feats (delete-dups
  19076. (mapcar 'file-name-sans-extension
  19077. (mapcar 'file-name-nondirectory
  19078. (delq nil
  19079. (mapcar 'feature-file
  19080. features))))))
  19081. (lfeat (append
  19082. (sort
  19083. (setq feats
  19084. (delq nil (mapcar
  19085. (lambda (f)
  19086. (if (and (string-match feature-re f)
  19087. (not (string-match remove-re f)))
  19088. f nil))
  19089. feats)))
  19090. 'string-lessp)
  19091. (list "org-version" "org")))
  19092. (load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  19093. (load-suffixes (if uncompiled (reverse load-suffixes) load-suffixes))
  19094. load-uncore load-misses)
  19095. (setq load-misses
  19096. (delq 't
  19097. (mapcar (lambda (f)
  19098. (or (org-load-noerror-mustsuffix (concat org-dir f))
  19099. (and (string= org-dir contrib-dir)
  19100. (org-load-noerror-mustsuffix (concat contrib-dir f)))
  19101. (and (org-load-noerror-mustsuffix (concat (org-find-library-dir f) f))
  19102. (add-to-list 'load-uncore f 'append)
  19103. 't)
  19104. f))
  19105. lfeat)))
  19106. (if load-uncore
  19107. (message "The following feature%s found in load-path, please check if that's correct:\n%s"
  19108. (if (> (length load-uncore) 1) "s were" " was") load-uncore))
  19109. (if load-misses
  19110. (message "Some error occured while reloading Org feature%s\n%s\nPlease check *Messages*!\n%s"
  19111. (if (> (length load-misses) 1) "s" "") load-misses (org-version nil 'full))
  19112. (message "Successfully reloaded Org\n%s" (org-version nil 'full)))))
  19113. ;;;###autoload
  19114. (defun org-customize ()
  19115. "Call the customize function with org as argument."
  19116. (interactive)
  19117. (org-load-modules-maybe)
  19118. (org-require-autoloaded-modules)
  19119. (customize-browse 'org))
  19120. (defun org-create-customize-menu ()
  19121. "Create a full customization menu for Org-mode, insert it into the menu."
  19122. (interactive)
  19123. (org-load-modules-maybe)
  19124. (org-require-autoloaded-modules)
  19125. (if (fboundp 'customize-menu-create)
  19126. (progn
  19127. (easy-menu-change
  19128. '("Org") "Customize"
  19129. `(["Browse Org group" org-customize t]
  19130. "--"
  19131. ,(customize-menu-create 'org)
  19132. ["Set" Custom-set t]
  19133. ["Save" Custom-save t]
  19134. ["Reset to Current" Custom-reset-current t]
  19135. ["Reset to Saved" Custom-reset-saved t]
  19136. ["Reset to Standard Settings" Custom-reset-standard t]))
  19137. (message "\"Org\"-menu now contains full customization menu"))
  19138. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  19139. ;;;; Miscellaneous stuff
  19140. ;;; Generally useful functions
  19141. (defun org-get-at-bol (property)
  19142. "Get text property PROPERTY at beginning of line."
  19143. (get-text-property (point-at-bol) property))
  19144. (defun org-find-text-property-in-string (prop s)
  19145. "Return the first non-nil value of property PROP in string S."
  19146. (or (get-text-property 0 prop s)
  19147. (get-text-property (or (next-single-property-change 0 prop s) 0)
  19148. prop s)))
  19149. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  19150. "Display the given MESSAGE as a warning."
  19151. (if (fboundp 'display-warning)
  19152. (display-warning 'org message
  19153. (if (featurep 'xemacs) 'warning :warning))
  19154. (let ((buf (get-buffer-create "*Org warnings*")))
  19155. (with-current-buffer buf
  19156. (goto-char (point-max))
  19157. (insert "Warning (Org): " message)
  19158. (unless (bolp)
  19159. (newline)))
  19160. (display-buffer buf)
  19161. (sit-for 0))))
  19162. (defun org-eval (form)
  19163. "Eval FORM and return result."
  19164. (condition-case error
  19165. (eval form)
  19166. (error (format "%%![Error: %s]" error))))
  19167. (defun org-in-clocktable-p ()
  19168. "Check if the cursor is in a clocktable."
  19169. (let ((pos (point)) start)
  19170. (save-excursion
  19171. (end-of-line 1)
  19172. (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
  19173. (setq start (match-beginning 0))
  19174. (re-search-forward "^[ \t]*#\\+END:.*" nil t)
  19175. (>= (match-end 0) pos)
  19176. start))))
  19177. (defun org-in-commented-line ()
  19178. "Is point in a line starting with `#'?"
  19179. (equal (char-after (point-at-bol)) ?#))
  19180. (defun org-in-indented-comment-line ()
  19181. "Is point in a line starting with `#' after some white space?"
  19182. (save-excursion
  19183. (save-match-data
  19184. (goto-char (point-at-bol))
  19185. (looking-at "[ \t]*#"))))
  19186. (defun org-in-verbatim-emphasis ()
  19187. (save-match-data
  19188. (and (org-in-regexp org-emph-re 2)
  19189. (>= (point) (match-beginning 3))
  19190. (<= (point) (match-end 4))
  19191. (member (match-string 3) '("=" "~")))))
  19192. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  19193. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  19194. (if (and marker (marker-buffer marker)
  19195. (buffer-live-p (marker-buffer marker)))
  19196. (progn
  19197. (org-pop-to-buffer-same-window (marker-buffer marker))
  19198. (if (or (> marker (point-max)) (< marker (point-min)))
  19199. (widen))
  19200. (goto-char marker)
  19201. (org-show-context 'org-goto))
  19202. (if bookmark
  19203. (bookmark-jump bookmark)
  19204. (error "Cannot find location"))))
  19205. (defun org-quote-csv-field (s)
  19206. "Quote field for inclusion in CSV material."
  19207. (if (string-match "[\",]" s)
  19208. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  19209. s))
  19210. (defun org-force-self-insert (N)
  19211. "Needed to enforce self-insert under remapping."
  19212. (interactive "p")
  19213. (self-insert-command N))
  19214. (defun org-string-width (s)
  19215. "Compute width of string, ignoring invisible characters.
  19216. This ignores character with invisibility property `org-link', and also
  19217. characters with property `org-cwidth', because these will become invisible
  19218. upon the next fontification round."
  19219. (let (b l)
  19220. (when (or (eq t buffer-invisibility-spec)
  19221. (assq 'org-link buffer-invisibility-spec))
  19222. (while (setq b (text-property-any 0 (length s)
  19223. 'invisible 'org-link s))
  19224. (setq s (concat (substring s 0 b)
  19225. (substring s (or (next-single-property-change
  19226. b 'invisible s) (length s)))))))
  19227. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  19228. (setq s (concat (substring s 0 b)
  19229. (substring s (or (next-single-property-change
  19230. b 'org-cwidth s) (length s))))))
  19231. (setq l (string-width s) b -1)
  19232. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  19233. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  19234. l))
  19235. (defun org-shorten-string (s maxlength)
  19236. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  19237. If the string is shorter or has length MAXLENGTH, just return the
  19238. original string. If it is longer, the functions finds a space in the
  19239. string, breaks this string off at that locations and adds three dots
  19240. as ellipsis. Including the ellipsis, the string will not be longer
  19241. than MAXLENGTH. If finding a good breaking point in the string does
  19242. not work, the string is just chopped off in the middle of a word
  19243. if necessary."
  19244. (if (<= (length s) maxlength)
  19245. s
  19246. (let* ((n (max (- maxlength 4) 1))
  19247. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  19248. (if (string-match re s)
  19249. (concat (match-string 1 s) "...")
  19250. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  19251. (defun org-get-indentation (&optional line)
  19252. "Get the indentation of the current line, interpreting tabs.
  19253. When LINE is given, assume it represents a line and compute its indentation."
  19254. (if line
  19255. (if (string-match "^ *" (org-remove-tabs line))
  19256. (match-end 0))
  19257. (save-excursion
  19258. (beginning-of-line 1)
  19259. (skip-chars-forward " \t")
  19260. (current-column))))
  19261. (defun org-get-string-indentation (s)
  19262. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  19263. (let ((n -1) (i 0) (w tab-width) c)
  19264. (catch 'exit
  19265. (while (< (setq n (1+ n)) (length s))
  19266. (setq c (aref s n))
  19267. (cond ((= c ?\ ) (setq i (1+ i)))
  19268. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  19269. (t (throw 'exit t)))))
  19270. i))
  19271. (defun org-remove-tabs (s &optional width)
  19272. "Replace tabulators in S with spaces.
  19273. Assumes that s is a single line, starting in column 0."
  19274. (setq width (or width tab-width))
  19275. (while (string-match "\t" s)
  19276. (setq s (replace-match
  19277. (make-string
  19278. (- (* width (/ (+ (match-beginning 0) width) width))
  19279. (match-beginning 0)) ?\ )
  19280. t t s)))
  19281. s)
  19282. (defun org-fix-indentation (line ind)
  19283. "Fix indentation in LINE.
  19284. IND is a cons cell with target and minimum indentation.
  19285. If the current indentation in LINE is smaller than the minimum,
  19286. leave it alone. If it is larger than ind, set it to the target."
  19287. (let* ((l (org-remove-tabs line))
  19288. (i (org-get-indentation l))
  19289. (i1 (car ind)) (i2 (cdr ind)))
  19290. (if (>= i i2) (setq l (substring line i2)))
  19291. (if (> i1 0)
  19292. (concat (make-string i1 ?\ ) l)
  19293. l)))
  19294. (defun org-remove-indentation (code &optional n)
  19295. "Remove the maximum common indentation from the lines in CODE.
  19296. N may optionally be the number of spaces to remove."
  19297. (with-temp-buffer
  19298. (insert code)
  19299. (org-do-remove-indentation n)
  19300. (buffer-string)))
  19301. (defun org-do-remove-indentation (&optional n)
  19302. "Remove the maximum common indentation from the buffer."
  19303. (untabify (point-min) (point-max))
  19304. (let ((min 10000) re)
  19305. (if n
  19306. (setq min n)
  19307. (goto-char (point-min))
  19308. (while (re-search-forward "^ *[^ \n]" nil t)
  19309. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  19310. (unless (or (= min 0) (= min 10000))
  19311. (setq re (format "^ \\{%d\\}" min))
  19312. (goto-char (point-min))
  19313. (while (re-search-forward re nil t)
  19314. (replace-match "")
  19315. (end-of-line 1))
  19316. min)))
  19317. (defun org-fill-template (template alist)
  19318. "Find each %key of ALIST in TEMPLATE and replace it."
  19319. (let ((case-fold-search nil)
  19320. entry key value)
  19321. (setq alist (sort (copy-sequence alist)
  19322. (lambda (a b) (< (length (car a)) (length (car b))))))
  19323. (while (setq entry (pop alist))
  19324. (setq template
  19325. (replace-regexp-in-string
  19326. (concat "%" (regexp-quote (car entry)))
  19327. (or (cdr entry) "") template t t)))
  19328. template))
  19329. (defun org-base-buffer (buffer)
  19330. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  19331. (if (not buffer)
  19332. buffer
  19333. (or (buffer-base-buffer buffer)
  19334. buffer)))
  19335. (defun org-trim (s)
  19336. "Remove whitespace at beginning and end of string."
  19337. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  19338. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  19339. s)
  19340. (defun org-wrap (string &optional width lines)
  19341. "Wrap string to either a number of lines, or a width in characters.
  19342. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  19343. that costs. If there is a word longer than WIDTH, the text is actually
  19344. wrapped to the length of that word.
  19345. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  19346. many lines, whatever width that takes.
  19347. The return value is a list of lines, without newlines at the end."
  19348. (let* ((words (org-split-string string "[ \t\n]+"))
  19349. (maxword (apply 'max (mapcar 'org-string-width words)))
  19350. w ll)
  19351. (cond (width
  19352. (org-do-wrap words (max maxword width)))
  19353. (lines
  19354. (setq w maxword)
  19355. (setq ll (org-do-wrap words maxword))
  19356. (if (<= (length ll) lines)
  19357. ll
  19358. (setq ll words)
  19359. (while (> (length ll) lines)
  19360. (setq w (1+ w))
  19361. (setq ll (org-do-wrap words w)))
  19362. ll))
  19363. (t (error "Cannot wrap this")))))
  19364. (defun org-do-wrap (words width)
  19365. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  19366. (let (lines line)
  19367. (while words
  19368. (setq line (pop words))
  19369. (while (and words (< (+ (length line) (length (car words))) width))
  19370. (setq line (concat line " " (pop words))))
  19371. (setq lines (push line lines)))
  19372. (nreverse lines)))
  19373. (defun org-split-string (string &optional separators)
  19374. "Splits STRING into substrings at SEPARATORS.
  19375. No empty strings are returned if there are matches at the beginning
  19376. and end of string."
  19377. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  19378. (start 0)
  19379. notfirst
  19380. (list nil))
  19381. (while (and (string-match rexp string
  19382. (if (and notfirst
  19383. (= start (match-beginning 0))
  19384. (< start (length string)))
  19385. (1+ start) start))
  19386. (< (match-beginning 0) (length string)))
  19387. (setq notfirst t)
  19388. (or (eq (match-beginning 0) 0)
  19389. (and (eq (match-beginning 0) (match-end 0))
  19390. (eq (match-beginning 0) start))
  19391. (setq list
  19392. (cons (substring string start (match-beginning 0))
  19393. list)))
  19394. (setq start (match-end 0)))
  19395. (or (eq start (length string))
  19396. (setq list
  19397. (cons (substring string start)
  19398. list)))
  19399. (nreverse list)))
  19400. (defun org-quote-vert (s)
  19401. "Replace \"|\" with \"\\vert\"."
  19402. (while (string-match "|" s)
  19403. (setq s (replace-match "\\vert" t t s)))
  19404. s)
  19405. (defun org-uuidgen-p (s)
  19406. "Is S an ID created by UUIDGEN?"
  19407. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  19408. (defun org-in-src-block-p (&optional inside)
  19409. "Whether point is in a code source block.
  19410. When INSIDE is non-nil, don't consider we are within a src block
  19411. when point is at #+BEGIN_SRC or #+END_SRC."
  19412. (let ((case-fold-search t) ov)
  19413. (or (and (setq ov (overlays-at (point)))
  19414. (memq 'org-block-background
  19415. (overlay-properties (car ov))))
  19416. (and (not inside)
  19417. (save-match-data
  19418. (save-excursion
  19419. (beginning-of-line)
  19420. (looking-at ".*#\\+\\(begin\\|end\\)_src")))))))
  19421. (defun org-context ()
  19422. "Return a list of contexts of the current cursor position.
  19423. If several contexts apply, all are returned.
  19424. Each context entry is a list with a symbol naming the context, and
  19425. two positions indicating start and end of the context. Possible
  19426. contexts are:
  19427. :headline anywhere in a headline
  19428. :headline-stars on the leading stars in a headline
  19429. :todo-keyword on a TODO keyword (including DONE) in a headline
  19430. :tags on the TAGS in a headline
  19431. :priority on the priority cookie in a headline
  19432. :item on the first line of a plain list item
  19433. :item-bullet on the bullet/number of a plain list item
  19434. :checkbox on the checkbox in a plain list item
  19435. :table in an org-mode table
  19436. :table-special on a special filed in a table
  19437. :table-table in a table.el table
  19438. :clocktable in a clocktable
  19439. :src-block in a source block
  19440. :link on a hyperlink
  19441. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT, QUOTE.
  19442. :target on a <<target>>
  19443. :radio-target on a <<<radio-target>>>
  19444. :latex-fragment on a LaTeX fragment
  19445. :latex-preview on a LaTeX fragment with overlaid preview image
  19446. This function expects the position to be visible because it uses font-lock
  19447. faces as a help to recognize the following contexts: :table-special, :link,
  19448. and :keyword."
  19449. (let* ((f (get-text-property (point) 'face))
  19450. (faces (if (listp f) f (list f)))
  19451. (case-fold-search t)
  19452. (p (point)) clist o)
  19453. ;; First the large context
  19454. (cond
  19455. ((org-at-heading-p t)
  19456. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  19457. (when (progn
  19458. (beginning-of-line 1)
  19459. (looking-at org-todo-line-tags-regexp))
  19460. (push (org-point-in-group p 1 :headline-stars) clist)
  19461. (push (org-point-in-group p 2 :todo-keyword) clist)
  19462. (push (org-point-in-group p 4 :tags) clist))
  19463. (goto-char p)
  19464. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  19465. (if (looking-at "\\[#[A-Z0-9]\\]")
  19466. (push (org-point-in-group p 0 :priority) clist)))
  19467. ((org-at-item-p)
  19468. (push (org-point-in-group p 2 :item-bullet) clist)
  19469. (push (list :item (point-at-bol)
  19470. (save-excursion (org-end-of-item) (point)))
  19471. clist)
  19472. (and (org-at-item-checkbox-p)
  19473. (push (org-point-in-group p 0 :checkbox) clist)))
  19474. ((org-at-table-p)
  19475. (push (list :table (org-table-begin) (org-table-end)) clist)
  19476. (if (memq 'org-formula faces)
  19477. (push (list :table-special
  19478. (previous-single-property-change p 'face)
  19479. (next-single-property-change p 'face)) clist)))
  19480. ((org-at-table-p 'any)
  19481. (push (list :table-table) clist)))
  19482. (goto-char p)
  19483. (let ((case-fold-search t))
  19484. ;; New the "medium" contexts: clocktables, source blocks
  19485. (cond ((org-in-clocktable-p)
  19486. (push (list :clocktable
  19487. (and (or (looking-at "#\\+BEGIN: clocktable")
  19488. (search-backward "#+BEGIN: clocktable" nil t))
  19489. (match-beginning 0))
  19490. (and (re-search-forward "#\\+END:?" nil t)
  19491. (match-end 0))) clist))
  19492. ((org-in-src-block-p)
  19493. (push (list :src-block
  19494. (and (or (looking-at "#\\+BEGIN_SRC")
  19495. (search-backward "#+BEGIN_SRC" nil t))
  19496. (match-beginning 0))
  19497. (and (search-forward "#+END_SRC" nil t)
  19498. (match-beginning 0))) clist))))
  19499. (goto-char p)
  19500. ;; Now the small context
  19501. (cond
  19502. ((org-at-timestamp-p)
  19503. (push (org-point-in-group p 0 :timestamp) clist))
  19504. ((memq 'org-link faces)
  19505. (push (list :link
  19506. (previous-single-property-change p 'face)
  19507. (next-single-property-change p 'face)) clist))
  19508. ((memq 'org-special-keyword faces)
  19509. (push (list :keyword
  19510. (previous-single-property-change p 'face)
  19511. (next-single-property-change p 'face)) clist))
  19512. ((org-at-target-p)
  19513. (push (org-point-in-group p 0 :target) clist)
  19514. (goto-char (1- (match-beginning 0)))
  19515. (if (looking-at org-radio-target-regexp)
  19516. (push (org-point-in-group p 0 :radio-target) clist))
  19517. (goto-char p))
  19518. ((setq o (car (delq nil
  19519. (mapcar
  19520. (lambda (x)
  19521. (if (memq x org-latex-fragment-image-overlays) x))
  19522. (overlays-at (point))))))
  19523. (push (list :latex-fragment
  19524. (overlay-start o) (overlay-end o)) clist)
  19525. (push (list :latex-preview
  19526. (overlay-start o) (overlay-end o)) clist))
  19527. ((org-inside-LaTeX-fragment-p)
  19528. ;; FIXME: positions wrong.
  19529. (push (list :latex-fragment (point) (point)) clist)))
  19530. (setq clist (nreverse (delq nil clist)))
  19531. clist))
  19532. ;; FIXME: Compare with at-regexp-p Do we need both?
  19533. (defun org-in-regexp (re &optional nlines visually)
  19534. "Check if point is inside a match of regexp.
  19535. Normally only the current line is checked, but you can include NLINES extra
  19536. lines both before and after point into the search.
  19537. If VISUALLY is set, require that the cursor is not after the match but
  19538. really on, so that the block visually is on the match."
  19539. (catch 'exit
  19540. (let ((pos (point))
  19541. (eol (point-at-eol (+ 1 (or nlines 0))))
  19542. (inc (if visually 1 0)))
  19543. (save-excursion
  19544. (beginning-of-line (- 1 (or nlines 0)))
  19545. (while (re-search-forward re eol t)
  19546. (if (and (<= (match-beginning 0) pos)
  19547. (>= (+ inc (match-end 0)) pos))
  19548. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  19549. (defun org-at-regexp-p (regexp)
  19550. "Is point inside a match of REGEXP in the current line?"
  19551. (catch 'exit
  19552. (save-excursion
  19553. (let ((pos (point)) (end (point-at-eol)))
  19554. (beginning-of-line 1)
  19555. (while (re-search-forward regexp end t)
  19556. (if (and (<= (match-beginning 0) pos)
  19557. (>= (match-end 0) pos))
  19558. (throw 'exit t)))
  19559. nil))))
  19560. (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
  19561. "Non-nil when point is between matches of START-RE and END-RE.
  19562. Also return a non-nil value when point is on one of the matches.
  19563. Optional arguments LIM-UP and LIM-DOWN bound the search; they are
  19564. buffer positions. Default values are the positions of headlines
  19565. surrounding the point.
  19566. The functions returns a cons cell whose car (resp. cdr) is the
  19567. position before START-RE (resp. after END-RE)."
  19568. (save-match-data
  19569. (let ((pos (point))
  19570. (limit-up (or lim-up (save-excursion (outline-previous-heading))))
  19571. (limit-down (or lim-down (save-excursion (outline-next-heading))))
  19572. beg end)
  19573. (save-excursion
  19574. ;; Point is on a block when on START-RE or if START-RE can be
  19575. ;; found before it...
  19576. (and (or (org-at-regexp-p start-re)
  19577. (re-search-backward start-re limit-up t))
  19578. (setq beg (match-beginning 0))
  19579. ;; ... and END-RE after it...
  19580. (goto-char (match-end 0))
  19581. (re-search-forward end-re limit-down t)
  19582. (> (setq end (match-end 0)) pos)
  19583. ;; ... without another START-RE in-between.
  19584. (goto-char (match-beginning 0))
  19585. (not (re-search-backward start-re (1+ beg) t))
  19586. ;; Return value.
  19587. (cons beg end))))))
  19588. (defun org-in-block-p (names)
  19589. "Non-nil when point belongs to a block whose name belongs to NAMES.
  19590. NAMES is a list of strings containing names of blocks.
  19591. Return first block name matched, or nil. Beware that in case of
  19592. nested blocks, the returned name may not belong to the closest
  19593. block from point."
  19594. (save-match-data
  19595. (catch 'exit
  19596. (let ((case-fold-search t)
  19597. (lim-up (save-excursion (outline-previous-heading)))
  19598. (lim-down (save-excursion (outline-next-heading))))
  19599. (mapc (lambda (name)
  19600. (let ((n (regexp-quote name)))
  19601. (when (org-between-regexps-p
  19602. (concat "^[ \t]*#\\+begin_" n)
  19603. (concat "^[ \t]*#\\+end_" n)
  19604. lim-up lim-down)
  19605. (throw 'exit n))))
  19606. names))
  19607. nil)))
  19608. (defun org-occur-in-agenda-files (regexp &optional nlines)
  19609. "Call `multi-occur' with buffers for all agenda files."
  19610. (interactive "sOrg-files matching: \np")
  19611. (let* ((files (org-agenda-files))
  19612. (tnames (mapcar 'file-truename files))
  19613. (extra org-agenda-text-search-extra-files)
  19614. f)
  19615. (when (eq (car extra) 'agenda-archives)
  19616. (setq extra (cdr extra))
  19617. (setq files (org-add-archive-files files)))
  19618. (while (setq f (pop extra))
  19619. (unless (member (file-truename f) tnames)
  19620. (add-to-list 'files f 'append)
  19621. (add-to-list 'tnames (file-truename f) 'append)))
  19622. (multi-occur
  19623. (mapcar (lambda (x)
  19624. (with-current-buffer
  19625. (or (get-file-buffer x) (find-file-noselect x))
  19626. (widen)
  19627. (current-buffer)))
  19628. files)
  19629. regexp)))
  19630. (if (boundp 'occur-mode-find-occurrence-hook)
  19631. ;; Emacs 23
  19632. (add-hook 'occur-mode-find-occurrence-hook
  19633. (lambda ()
  19634. (when (derived-mode-p 'org-mode)
  19635. (org-reveal))))
  19636. ;; Emacs 22
  19637. (defadvice occur-mode-goto-occurrence
  19638. (after org-occur-reveal activate)
  19639. (and (derived-mode-p 'org-mode) (org-reveal)))
  19640. (defadvice occur-mode-goto-occurrence-other-window
  19641. (after org-occur-reveal activate)
  19642. (and (derived-mode-p 'org-mode) (org-reveal)))
  19643. (defadvice occur-mode-display-occurrence
  19644. (after org-occur-reveal activate)
  19645. (when (derived-mode-p 'org-mode)
  19646. (let ((pos (occur-mode-find-occurrence)))
  19647. (with-current-buffer (marker-buffer pos)
  19648. (save-excursion
  19649. (goto-char pos)
  19650. (org-reveal)))))))
  19651. (defun org-occur-link-in-agenda-files ()
  19652. "Create a link and search for it in the agendas.
  19653. The link is not stored in `org-stored-links', it is just created
  19654. for the search purpose."
  19655. (interactive)
  19656. (let ((link (condition-case nil
  19657. (org-store-link nil)
  19658. (error "Unable to create a link to here"))))
  19659. (org-occur-in-agenda-files (regexp-quote link))))
  19660. (defun org-reverse-string (string)
  19661. "Return the reverse of STRING."
  19662. (apply 'string (reverse (string-to-list string))))
  19663. (defun org-uniquify (list)
  19664. "Remove duplicate elements from LIST."
  19665. (let (res)
  19666. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  19667. res))
  19668. (defun org-delete-all (elts list)
  19669. "Remove all elements in ELTS from LIST."
  19670. (while elts
  19671. (setq list (delete (pop elts) list)))
  19672. list)
  19673. (defun org-count (cl-item cl-seq)
  19674. "Count the number of occurrences of ITEM in SEQ.
  19675. Taken from `count' in cl-seq.el with all keyword arguments removed."
  19676. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  19677. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  19678. (while (< cl-start cl-end)
  19679. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  19680. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  19681. (setq cl-start (1+ cl-start)))
  19682. cl-count))
  19683. (defun org-remove-if (predicate seq)
  19684. "Remove everything from SEQ that fulfills PREDICATE."
  19685. (let (res e)
  19686. (while seq
  19687. (setq e (pop seq))
  19688. (if (not (funcall predicate e)) (push e res)))
  19689. (nreverse res)))
  19690. (defun org-remove-if-not (predicate seq)
  19691. "Remove everything from SEQ that does not fulfill PREDICATE."
  19692. (let (res e)
  19693. (while seq
  19694. (setq e (pop seq))
  19695. (if (funcall predicate e) (push e res)))
  19696. (nreverse res)))
  19697. (defun org-reduce (cl-func cl-seq &rest cl-keys)
  19698. "Reduce two-argument FUNCTION across SEQ.
  19699. Taken from `reduce' in cl-seq.el with all keyword arguments but
  19700. \":initial-value\" removed."
  19701. (let ((cl-accum (cond ((memq :initial-value cl-keys)
  19702. (cadr (memq :initial-value cl-keys)))
  19703. (cl-seq (pop cl-seq))
  19704. (t (funcall cl-func)))))
  19705. (while cl-seq
  19706. (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
  19707. cl-accum))
  19708. (defun org-back-over-empty-lines ()
  19709. "Move backwards over whitespace, to the beginning of the first empty line.
  19710. Returns the number of empty lines passed."
  19711. (let ((pos (point)))
  19712. (if (cdr (assoc 'heading org-blank-before-new-entry))
  19713. (skip-chars-backward " \t\n\r")
  19714. (unless (eobp)
  19715. (forward-line -1)))
  19716. (beginning-of-line 2)
  19717. (goto-char (min (point) pos))
  19718. (count-lines (point) pos)))
  19719. (defun org-skip-whitespace ()
  19720. (skip-chars-forward " \t\n\r"))
  19721. (defun org-point-in-group (point group &optional context)
  19722. "Check if POINT is in match-group GROUP.
  19723. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  19724. match. If the match group does not exist or point is not inside it,
  19725. return nil."
  19726. (and (match-beginning group)
  19727. (>= point (match-beginning group))
  19728. (<= point (match-end group))
  19729. (if context
  19730. (list context (match-beginning group) (match-end group))
  19731. t)))
  19732. (defun org-switch-to-buffer-other-window (&rest args)
  19733. "Switch to buffer in a second window on the current frame.
  19734. In particular, do not allow pop-up frames.
  19735. Returns the newly created buffer."
  19736. (org-no-popups
  19737. (apply 'switch-to-buffer-other-window args)))
  19738. (defun org-combine-plists (&rest plists)
  19739. "Create a single property list from all plists in PLISTS.
  19740. The process starts by copying the first list, and then setting properties
  19741. from the other lists. Settings in the last list are the most significant
  19742. ones and overrule settings in the other lists."
  19743. (let ((rtn (copy-sequence (pop plists)))
  19744. p v ls)
  19745. (while plists
  19746. (setq ls (pop plists))
  19747. (while ls
  19748. (setq p (pop ls) v (pop ls))
  19749. (setq rtn (plist-put rtn p v))))
  19750. rtn))
  19751. (defun org-replace-escapes (string table)
  19752. "Replace %-escapes in STRING with values in TABLE.
  19753. TABLE is an association list with keys like \"%a\" and string values.
  19754. The sequences in STRING may contain normal field width and padding information,
  19755. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  19756. so values can contain further %-escapes if they are define later in TABLE."
  19757. (let ((tbl (copy-alist table))
  19758. (case-fold-search nil)
  19759. (pchg 0)
  19760. e re rpl)
  19761. (while (setq e (pop tbl))
  19762. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  19763. (when (and (cdr e) (string-match re (cdr e)))
  19764. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  19765. (safe "SREF"))
  19766. (add-text-properties 0 3 (list 'sref sref) safe)
  19767. (setcdr e (replace-match safe t t (cdr e)))))
  19768. (while (string-match re string)
  19769. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  19770. (cdr e)))
  19771. (setq string (replace-match rpl t t string))))
  19772. (while (setq pchg (next-property-change pchg string))
  19773. (let ((sref (get-text-property pchg 'sref string)))
  19774. (when (and sref (string-match "SREF" string pchg))
  19775. (setq string (replace-match sref t t string)))))
  19776. string))
  19777. (defun org-sublist (list start end)
  19778. "Return a section of LIST, from START to END.
  19779. Counting starts at 1."
  19780. (let (rtn (c start))
  19781. (setq list (nthcdr (1- start) list))
  19782. (while (and list (<= c end))
  19783. (push (pop list) rtn)
  19784. (setq c (1+ c)))
  19785. (nreverse rtn)))
  19786. (defun org-find-base-buffer-visiting (file)
  19787. "Like `find-buffer-visiting' but always return the base buffer and
  19788. not an indirect buffer."
  19789. (let ((buf (or (get-file-buffer file)
  19790. (find-buffer-visiting file))))
  19791. (if buf
  19792. (or (buffer-base-buffer buf) buf)
  19793. nil)))
  19794. (defun org-image-file-name-regexp (&optional extensions)
  19795. "Return regexp matching the file names of images.
  19796. If EXTENSIONS is given, only match these."
  19797. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  19798. (image-file-name-regexp)
  19799. (let ((image-file-name-extensions
  19800. (or extensions
  19801. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  19802. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  19803. (concat "\\."
  19804. (regexp-opt (nconc (mapcar 'upcase
  19805. image-file-name-extensions)
  19806. image-file-name-extensions)
  19807. t)
  19808. "\\'"))))
  19809. (defun org-file-image-p (file &optional extensions)
  19810. "Return non-nil if FILE is an image."
  19811. (save-match-data
  19812. (string-match (org-image-file-name-regexp extensions) file)))
  19813. (defun org-get-cursor-date (&optional with-time)
  19814. "Return the date at cursor in as a time.
  19815. This works in the calendar and in the agenda, anywhere else it just
  19816. returns the current time.
  19817. If WITH-TIME is non-nil, returns the time of the event at point (in
  19818. the agenda) or the current time of the day."
  19819. (let (date day defd tp tm hod mod)
  19820. (when with-time
  19821. (setq tp (get-text-property (point) 'time))
  19822. (when (and tp (string-match "\\([0-9][0-9]\\):\\([0-9][0-9]\\)" tp))
  19823. (setq hod (string-to-number (match-string 1 tp))
  19824. mod (string-to-number (match-string 2 tp))))
  19825. (or tp (setq hod (nth 2 (decode-time (current-time)))
  19826. mod (nth 1 (decode-time (current-time))))))
  19827. (cond
  19828. ((eq major-mode 'calendar-mode)
  19829. (setq date (calendar-cursor-to-date)
  19830. defd (encode-time 0 (or mod 0) (or hod 0)
  19831. (nth 1 date) (nth 0 date) (nth 2 date))))
  19832. ((eq major-mode 'org-agenda-mode)
  19833. (setq day (get-text-property (point) 'day))
  19834. (if day
  19835. (setq date (calendar-gregorian-from-absolute day)
  19836. defd (encode-time 0 (or mod 0) (or hod 0)
  19837. (nth 1 date) (nth 0 date) (nth 2 date))))))
  19838. (or defd (current-time))))
  19839. (defun org-mark-subtree (&optional up)
  19840. "Mark the current subtree.
  19841. This puts point at the start of the current subtree, and mark at
  19842. the end. If a numeric prefix UP is given, move up into the
  19843. hierarchy of headlines by UP levels before marking the subtree."
  19844. (interactive "P")
  19845. (org-with-limited-levels
  19846. (cond ((org-at-heading-p) (beginning-of-line))
  19847. ((org-before-first-heading-p) (error "Not in a subtree"))
  19848. (t (outline-previous-visible-heading 1))))
  19849. (when up (while (and (> up 0) (org-up-heading-safe)) (decf up)))
  19850. (if (org-called-interactively-p 'any)
  19851. (call-interactively 'org-mark-element)
  19852. (org-mark-element)))
  19853. ;;; Indentation
  19854. (defun org-indent-line ()
  19855. "Indent line depending on context."
  19856. (interactive)
  19857. (let* ((pos (point))
  19858. (itemp (org-at-item-p))
  19859. (case-fold-search t)
  19860. (org-drawer-regexp (or org-drawer-regexp "\000"))
  19861. (inline-task-p (and (featurep 'org-inlinetask)
  19862. (org-inlinetask-in-task-p)))
  19863. (inline-re (and inline-task-p
  19864. (org-inlinetask-outline-regexp)))
  19865. column)
  19866. (if (and orgstruct-is-++ (eq pos (point)))
  19867. (let ((indent-line-function (cadadr (assoc 'indent-line-function org-fb-vars))))
  19868. (indent-according-to-mode))
  19869. (beginning-of-line 1)
  19870. (cond
  19871. ;; Headings
  19872. ((looking-at org-outline-regexp) (setq column 0))
  19873. ;; Footnote definition
  19874. ((looking-at org-footnote-definition-re) (setq column 0))
  19875. ;; Literal examples
  19876. ((looking-at "[ \t]*:\\( \\|$\\)")
  19877. (setq column (org-get-indentation))) ; do nothing
  19878. ;; Lists
  19879. ((ignore-errors (goto-char (org-in-item-p)))
  19880. (setq column (if itemp
  19881. (org-get-indentation)
  19882. (org-list-item-body-column (point))))
  19883. (goto-char pos))
  19884. ;; Drawers
  19885. ((and (looking-at "[ \t]*:END:")
  19886. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  19887. (save-excursion
  19888. (goto-char (1- (match-beginning 1)))
  19889. (setq column (current-column))))
  19890. ;; Special blocks
  19891. ((and (looking-at "[ \t]*#\\+end_\\([a-z]+\\)")
  19892. (save-excursion
  19893. (re-search-backward
  19894. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  19895. (setq column (org-get-indentation (match-string 0))))
  19896. ((and (not (looking-at "[ \t]*#\\+begin_"))
  19897. (org-between-regexps-p "^[ \t]*#\\+begin_" "[ \t]*#\\+end_"))
  19898. (save-excursion
  19899. (re-search-backward "^[ \t]*#\\+begin_\\([a-z]+\\)" nil t))
  19900. (setq column
  19901. (cond ((equal (downcase (match-string 1)) "src")
  19902. ;; src blocks: let `org-edit-src-exit' handle them
  19903. (org-get-indentation))
  19904. ((equal (downcase (match-string 1)) "example")
  19905. (max (org-get-indentation)
  19906. (org-get-indentation (match-string 0))))
  19907. (t
  19908. (org-get-indentation (match-string 0))))))
  19909. ;; This line has nothing special, look at the previous relevant
  19910. ;; line to compute indentation
  19911. (t
  19912. (beginning-of-line 0)
  19913. (while (and (not (bobp))
  19914. (not (looking-at org-table-line-regexp))
  19915. (not (looking-at org-drawer-regexp))
  19916. ;; When point started in an inline task, do not move
  19917. ;; above task starting line.
  19918. (not (and inline-task-p (looking-at inline-re)))
  19919. ;; Skip drawers, blocks, empty lines, verbatim,
  19920. ;; comments, tables, footnotes definitions, lists,
  19921. ;; inline tasks.
  19922. (or (and (looking-at "[ \t]*:END:")
  19923. (re-search-backward org-drawer-regexp nil t))
  19924. (and (looking-at "[ \t]*#\\+end_")
  19925. (re-search-backward "[ \t]*#\\+begin_"nil t))
  19926. (looking-at "[ \t]*[\n:#|]")
  19927. (looking-at org-footnote-definition-re)
  19928. (and (ignore-errors (goto-char (org-in-item-p)))
  19929. (goto-char
  19930. (org-list-get-top-point (org-list-struct))))
  19931. (and (not inline-task-p)
  19932. (featurep 'org-inlinetask)
  19933. (org-inlinetask-in-task-p)
  19934. (or (org-inlinetask-goto-beginning) t))))
  19935. (beginning-of-line 0))
  19936. (cond
  19937. ;; There was an heading above.
  19938. ((looking-at "\\*+[ \t]+")
  19939. (if (not org-adapt-indentation)
  19940. (setq column 0)
  19941. (goto-char (match-end 0))
  19942. (setq column (current-column))))
  19943. ;; A drawer had started and is unfinished
  19944. ((looking-at org-drawer-regexp)
  19945. (goto-char (1- (match-beginning 1)))
  19946. (setq column (current-column)))
  19947. ;; Else, nothing noticeable found: get indentation and go on.
  19948. (t (setq column (org-get-indentation))))))
  19949. ;; Now apply indentation and move cursor accordingly
  19950. (goto-char pos)
  19951. (if (<= (current-column) (current-indentation))
  19952. (org-indent-line-to column)
  19953. (save-excursion (org-indent-line-to column)))
  19954. ;; Special polishing for properties, see `org-property-format'
  19955. (setq column (current-column))
  19956. (beginning-of-line 1)
  19957. (if (looking-at
  19958. "\\([ \t]*\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  19959. (replace-match (concat (match-string 1)
  19960. (format org-property-format
  19961. (match-string 2) (match-string 3)))
  19962. t t))
  19963. (org-move-to-column column))))
  19964. (defun org-indent-drawer ()
  19965. "Indent the drawer at point."
  19966. (interactive)
  19967. (let ((p (point))
  19968. (e (and (save-excursion (re-search-forward ":END:" nil t))
  19969. (match-end 0)))
  19970. (folded
  19971. (save-excursion
  19972. (end-of-line)
  19973. (when (overlays-at (point))
  19974. (member 'invisible (overlay-properties
  19975. (car (overlays-at (point)))))))))
  19976. (when folded (org-cycle))
  19977. (indent-for-tab-command)
  19978. (while (and (move-beginning-of-line 2) (< (point) e))
  19979. (indent-for-tab-command))
  19980. (goto-char p)
  19981. (when folded (org-cycle)))
  19982. (message "Drawer at point indented"))
  19983. (defun org-indent-block ()
  19984. "Indent the block at point."
  19985. (interactive)
  19986. (let ((p (point))
  19987. (case-fold-search t)
  19988. (e (and (save-excursion (re-search-forward "#\\+end_?\\(?:[a-z]+\\)?" nil t))
  19989. (match-end 0)))
  19990. (folded
  19991. (save-excursion
  19992. (end-of-line)
  19993. (when (overlays-at (point))
  19994. (member 'invisible (overlay-properties
  19995. (car (overlays-at (point)))))))))
  19996. (when folded (org-cycle))
  19997. (indent-for-tab-command)
  19998. (while (and (move-beginning-of-line 2) (< (point) e))
  19999. (indent-for-tab-command))
  20000. (goto-char p)
  20001. (when folded (org-cycle)))
  20002. (message "Block at point indented"))
  20003. (defun org-indent-region (start end)
  20004. "Indent region."
  20005. (interactive "r")
  20006. (save-excursion
  20007. (let ((line-end (org-current-line end)))
  20008. (goto-char start)
  20009. (while (< (org-current-line) line-end)
  20010. (cond ((org-in-src-block-p) (org-src-native-tab-command-maybe))
  20011. (t (call-interactively 'org-indent-line)))
  20012. (move-beginning-of-line 2)))))
  20013. ;;; Filling
  20014. ;; We use our own fill-paragraph and auto-fill functions.
  20015. ;; `org-fill-paragraph' relies on adaptive filling and context
  20016. ;; checking. Appropriate `fill-prefix' is computed with
  20017. ;; `org-adaptive-fill-function'.
  20018. ;; `org-auto-fill-function' takes care of auto-filling. It calls
  20019. ;; `do-auto-fill' only on valid areas with `fill-prefix' shadowed with
  20020. ;; `org-adaptive-fill-function' value. Internally,
  20021. ;; `org-comment-line-break-function' breaks the line.
  20022. ;; `org-setup-filling' installs filling and auto-filling related
  20023. ;; variables during `org-mode' initialization.
  20024. (defun org-setup-filling ()
  20025. (interactive)
  20026. ;; Prevent auto-fill from inserting unwanted new items.
  20027. (when (boundp 'fill-nobreak-predicate)
  20028. (org-set-local
  20029. 'fill-nobreak-predicate
  20030. (org-uniquify
  20031. (append fill-nobreak-predicate
  20032. '(org-fill-paragraph-separate-nobreak-p
  20033. org-fill-line-break-nobreak-p
  20034. org-fill-paragraph-with-timestamp-nobreak-p)))))
  20035. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  20036. (org-set-local 'auto-fill-inhibit-regexp nil)
  20037. (org-set-local 'adaptive-fill-function 'org-adaptive-fill-function)
  20038. (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
  20039. (org-set-local 'comment-line-break-function 'org-comment-line-break-function))
  20040. (defvar org-element-paragraph-separate) ; org-element.el
  20041. (defun org-fill-paragraph-separate-nobreak-p ()
  20042. "Non-nil when a line break at point would insert a new item."
  20043. (looking-at (substring org-element-paragraph-separate 1)))
  20044. (defun org-fill-line-break-nobreak-p ()
  20045. "Non-nil when a line break at point would create an Org line break."
  20046. (save-excursion
  20047. (skip-chars-backward "[ \t]")
  20048. (skip-chars-backward "\\\\")
  20049. (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
  20050. (defun org-fill-paragraph-with-timestamp-nobreak-p ()
  20051. "Non-nil when a line break at point would insert a new item."
  20052. (and (org-at-timestamp-p t)
  20053. (not (looking-at org-ts-regexp-both))))
  20054. (declare-function message-in-body-p "message" ())
  20055. (defvar orgtbl-line-start-regexp) ; From org-table.el
  20056. (defun org-adaptive-fill-function ()
  20057. "Compute a fill prefix for the current line.
  20058. Return fill prefix, as a string, or nil if current line isn't
  20059. meant to be filled."
  20060. (let (prefix)
  20061. (catch 'exit
  20062. (when (derived-mode-p 'message-mode)
  20063. (save-excursion
  20064. (beginning-of-line)
  20065. (cond ((or (not (message-in-body-p))
  20066. (looking-at orgtbl-line-start-regexp))
  20067. (throw 'exit nil))
  20068. ((looking-at message-cite-prefix-regexp)
  20069. (throw 'exit (match-string-no-properties 0)))
  20070. ((looking-at org-outline-regexp)
  20071. (throw 'exit (make-string (length (match-string 0)) ? ))))))
  20072. (org-with-wide-buffer
  20073. (let* ((p (line-beginning-position))
  20074. (element (save-excursion (beginning-of-line)
  20075. (org-element-at-point)))
  20076. (type (org-element-type element))
  20077. (post-affiliated (org-element-property :post-affiliated element)))
  20078. (unless (and post-affiliated (< p post-affiliated))
  20079. (case type
  20080. (comment (looking-at "[ \t]*# ?") (match-string 0))
  20081. (footnote-definition "")
  20082. ((item plain-list)
  20083. (make-string (org-list-item-body-column
  20084. (or post-affiliated
  20085. (org-element-property :begin element)))
  20086. ? ))
  20087. (paragraph
  20088. ;; Fill prefix is usually the same as the current line,
  20089. ;; except if the paragraph is at the beginning of an item.
  20090. (let ((parent (org-element-property :parent element)))
  20091. (cond ((eq (org-element-type parent) 'item)
  20092. (make-string (org-list-item-body-column
  20093. (org-element-property :begin parent))
  20094. ? ))
  20095. ((save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  20096. (match-string 0))
  20097. (t ""))))
  20098. (comment-block
  20099. ;; Only fill contents if P is within block boundaries.
  20100. (let* ((cbeg (save-excursion (goto-char post-affiliated)
  20101. (forward-line)
  20102. (point)))
  20103. (cend (save-excursion
  20104. (goto-char (org-element-property :end element))
  20105. (skip-chars-backward " \r\t\n")
  20106. (line-beginning-position))))
  20107. (when (and (>= p cbeg) (< p cend))
  20108. (if (save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  20109. (match-string 0)
  20110. "")))))))))))
  20111. (declare-function message-goto-body "message" ())
  20112. (defvar message-cite-prefix-regexp) ; From message.el
  20113. (defun org-fill-paragraph (&optional justify)
  20114. "Fill element at point, when applicable.
  20115. This function only applies to comment blocks, comments, example
  20116. blocks and paragraphs. Also, as a special case, re-align table
  20117. when point is at one.
  20118. If JUSTIFY is non-nil (interactively, with prefix argument),
  20119. justify as well. If `sentence-end-double-space' is non-nil, then
  20120. period followed by one space does not end a sentence, so don't
  20121. break a line there. The variable `fill-column' controls the
  20122. width for filling.
  20123. For convenience, when point is at a plain list, an item or
  20124. a footnote definition, try to fill the first paragraph within."
  20125. (interactive)
  20126. (if (and (derived-mode-p 'message-mode)
  20127. (or (not (message-in-body-p))
  20128. (save-excursion (move-beginning-of-line 1)
  20129. (looking-at message-cite-prefix-regexp))))
  20130. ;; First ensure filling is correct in message-mode.
  20131. (let ((fill-paragraph-function
  20132. (cadadr (assoc 'fill-paragraph-function org-fb-vars)))
  20133. (fill-prefix (cadadr (assoc 'fill-prefix org-fb-vars)))
  20134. (paragraph-start (cadadr (assoc 'paragraph-start org-fb-vars)))
  20135. (paragraph-separate
  20136. (cadadr (assoc 'paragraph-separate org-fb-vars))))
  20137. (fill-paragraph nil))
  20138. (with-syntax-table org-mode-transpose-word-syntax-table
  20139. ;; Move to end of line in order to get the first paragraph
  20140. ;; within a plain list or a footnote definition.
  20141. (let ((element (save-excursion (end-of-line) (org-element-at-point))))
  20142. ;; First check if point is in a blank line at the beginning of
  20143. ;; the buffer. In that case, ignore filling.
  20144. (case (org-element-type element)
  20145. ;; Use major mode filling function is src blocks.
  20146. (src-block (org-babel-do-key-sequence-in-edit-buffer (kbd "M-q")))
  20147. ;; Align Org tables, leave table.el tables as-is.
  20148. (table-row (org-table-align) t)
  20149. (table
  20150. (when (eq (org-element-property :type element) 'org)
  20151. (org-table-align))
  20152. t)
  20153. (paragraph
  20154. ;; Paragraphs may contain `line-break' type objects.
  20155. (let ((beg (max (point-min)
  20156. (org-element-property :contents-begin element)))
  20157. (end (min (point-max)
  20158. (org-element-property :contents-end element))))
  20159. ;; Do nothing if point is at an affiliated keyword.
  20160. (if (< (line-end-position) beg) t
  20161. (when (derived-mode-p 'message-mode)
  20162. ;; In `message-mode', do not fill following citation
  20163. ;; in current paragraph nor text before message body.
  20164. (let ((body-start (save-excursion (message-goto-body))))
  20165. (when body-start (setq beg (max body-start beg))))
  20166. (when (save-excursion
  20167. (re-search-forward
  20168. (concat "^" message-cite-prefix-regexp) end t))
  20169. (setq end (match-beginning 0))))
  20170. ;; Fill paragraph, taking line breaks into account.
  20171. ;; For that, slice the paragraph using line breaks as
  20172. ;; separators, and fill the parts in reverse order to
  20173. ;; avoid messing with markers.
  20174. (save-excursion
  20175. (goto-char end)
  20176. (mapc
  20177. (lambda (pos)
  20178. (fill-region-as-paragraph pos (point) justify)
  20179. (goto-char pos))
  20180. ;; Find the list of ending positions for line breaks
  20181. ;; in the current paragraph. Add paragraph
  20182. ;; beginning to include first slice.
  20183. (nreverse
  20184. (cons beg
  20185. (org-element-map
  20186. (org-element--parse-objects
  20187. beg end nil (org-element-restriction 'paragraph))
  20188. 'line-break
  20189. (lambda (lb) (org-element-property :end lb)))))))
  20190. t)))
  20191. ;; Contents of `comment-block' type elements should be
  20192. ;; filled as plain text, but only if point is within block
  20193. ;; markers.
  20194. (comment-block
  20195. (let* ((case-fold-search t)
  20196. (beg (save-excursion
  20197. (goto-char (org-element-property :begin element))
  20198. (re-search-forward "^[ \t]*#\\+begin_comment" nil t)
  20199. (forward-line)
  20200. (point)))
  20201. (end (save-excursion
  20202. (goto-char (org-element-property :end element))
  20203. (re-search-backward "^[ \t]*#\\+end_comment" nil t)
  20204. (line-beginning-position))))
  20205. (when (and (>= (point) beg) (< (point) end))
  20206. (fill-region-as-paragraph
  20207. (save-excursion
  20208. (end-of-line)
  20209. (re-search-backward "^[ \t]*$" beg 'move)
  20210. (line-beginning-position))
  20211. (save-excursion
  20212. (beginning-of-line)
  20213. (re-search-forward "^[ \t]*$" end 'move)
  20214. (line-beginning-position))
  20215. justify)))
  20216. t)
  20217. ;; Fill comments.
  20218. (comment (fill-comment-paragraph justify))
  20219. ;; Ignore every other element.
  20220. (otherwise t))))))
  20221. (defun org-auto-fill-function ()
  20222. "Auto-fill function."
  20223. ;; Check if auto-filling is meaningful.
  20224. (let ((fc (current-fill-column)))
  20225. (when (and fc (> (current-column) fc))
  20226. (let* ((fill-prefix (org-adaptive-fill-function))
  20227. ;; Enforce empty fill prefix, if required. Otherwise, it
  20228. ;; will be computed again.
  20229. (adaptive-fill-mode (not (equal fill-prefix ""))))
  20230. (when fill-prefix (do-auto-fill))))))
  20231. (defun org-comment-line-break-function (&optional soft)
  20232. "Break line at point and indent, continuing comment if within one.
  20233. The inserted newline is marked hard if variable
  20234. `use-hard-newlines' is true, unless optional argument SOFT is
  20235. non-nil."
  20236. (if soft (insert-and-inherit ?\n) (newline 1))
  20237. (save-excursion (forward-char -1) (delete-horizontal-space))
  20238. (delete-horizontal-space)
  20239. (indent-to-left-margin)
  20240. (insert-before-markers-and-inherit fill-prefix))
  20241. ;;; Comments
  20242. ;; Org comments syntax is quite complex. It requires the entire line
  20243. ;; to be just a comment. Also, even with the right syntax at the
  20244. ;; beginning of line, some some elements (i.e. verse-block or
  20245. ;; example-block) don't accept comments. Usual Emacs comment commands
  20246. ;; cannot cope with those requirements. Therefore, Org replaces them.
  20247. ;; Org still relies on `comment-dwim', but cannot trust
  20248. ;; `comment-only-p'. So, `comment-region-function' and
  20249. ;; `uncomment-region-function' both point
  20250. ;; to`org-comment-or-uncomment-region'. Eventually,
  20251. ;; `org-insert-comment' takes care of insertion of comments at the
  20252. ;; beginning of line.
  20253. ;; `org-setup-comments-handling' install comments related variables
  20254. ;; during `org-mode' initialization.
  20255. (defun org-setup-comments-handling ()
  20256. (interactive)
  20257. (org-set-local 'comment-use-syntax nil)
  20258. (org-set-local 'comment-start "# ")
  20259. (org-set-local 'comment-start-skip "^\\s-*#\\(?: \\|$\\)")
  20260. (org-set-local 'comment-insert-comment-function 'org-insert-comment)
  20261. (org-set-local 'comment-region-function 'org-comment-or-uncomment-region)
  20262. (org-set-local 'uncomment-region-function 'org-comment-or-uncomment-region))
  20263. (defun org-insert-comment ()
  20264. "Insert an empty comment above current line.
  20265. If the line is empty, insert comment at its beginning."
  20266. (beginning-of-line)
  20267. (if (looking-at "\\s-*$") (replace-match "") (open-line 1))
  20268. (org-indent-line)
  20269. (insert "# "))
  20270. (defvar comment-empty-lines) ; From newcomment.el.
  20271. (defun org-comment-or-uncomment-region (beg end &rest ignore)
  20272. "Comment or uncomment each non-blank line in the region.
  20273. Uncomment each non-blank line between BEG and END if it only
  20274. contains commented lines. Otherwise, comment them."
  20275. (save-restriction
  20276. ;; Restrict region
  20277. (narrow-to-region (save-excursion (goto-char beg)
  20278. (skip-chars-forward " \r\t\n" end)
  20279. (line-beginning-position))
  20280. (save-excursion (goto-char end)
  20281. (skip-chars-backward " \r\t\n" beg)
  20282. (line-end-position)))
  20283. (let ((uncommentp
  20284. ;; UNCOMMENTP is non-nil when every non blank line between
  20285. ;; BEG and END is a comment.
  20286. (save-excursion
  20287. (goto-char (point-min))
  20288. (while (and (not (eobp))
  20289. (let ((element (org-element-at-point)))
  20290. (and (eq (org-element-type element) 'comment)
  20291. (goto-char (min (point-max)
  20292. (org-element-property
  20293. :end element)))))))
  20294. (eobp))))
  20295. (if uncommentp
  20296. ;; Only blank lines and comments in region: uncomment it.
  20297. (save-excursion
  20298. (goto-char (point-min))
  20299. (while (not (eobp))
  20300. (when (looking-at "[ \t]*\\(#\\(?: \\|$\\)\\)")
  20301. (replace-match "" nil nil nil 1))
  20302. (forward-line)))
  20303. ;; Comment each line in region.
  20304. (let ((min-indent (point-max)))
  20305. ;; First find the minimum indentation across all lines.
  20306. (save-excursion
  20307. (goto-char (point-min))
  20308. (while (and (not (eobp)) (not (zerop min-indent)))
  20309. (unless (looking-at "[ \t]*$")
  20310. (setq min-indent (min min-indent (current-indentation))))
  20311. (forward-line)))
  20312. ;; Then loop over all lines.
  20313. (save-excursion
  20314. (goto-char (point-min))
  20315. (while (not (eobp))
  20316. (unless (and (not comment-empty-lines) (looking-at "[ \t]*$"))
  20317. ;; Don't get fooled by invisible text (e.g. link path)
  20318. ;; when moving to column MIN-INDENT.
  20319. (let ((buffer-invisibility-spec nil))
  20320. (org-move-to-column min-indent t))
  20321. (insert comment-start))
  20322. (forward-line))))))))
  20323. ;;; Planning
  20324. ;; This section contains tools to operate on timestamp objects, as
  20325. ;; returned by, e.g. `org-element-context'.
  20326. (defun org-timestamp-has-time-p (timestamp)
  20327. "Non-nil when TIMESTAMP has a time specified."
  20328. (org-element-property :hour-start timestamp))
  20329. (defun org-timestamp-format (timestamp format &optional end utc)
  20330. "Format a TIMESTAMP element into a string.
  20331. FORMAT is a format specifier to be passed to
  20332. `format-time-string'.
  20333. When optional argument END is non-nil, use end of date-range or
  20334. time-range, if possible.
  20335. When optional argument UTC is non-nil, time will be expressed as
  20336. Universal Time."
  20337. (format-time-string
  20338. format
  20339. (apply 'encode-time
  20340. (cons 0
  20341. (mapcar
  20342. (lambda (prop) (or (org-element-property prop timestamp) 0))
  20343. (if end '(:minute-end :hour-end :day-end :month-end :year-end)
  20344. '(:minute-start :hour-start :day-start :month-start
  20345. :year-start)))))
  20346. utc))
  20347. (defun org-timestamp-split-range (timestamp &optional end)
  20348. "Extract a timestamp object from a date or time range.
  20349. TIMESTAMP is a timestamp object. END, when non-nil, means extract
  20350. the end of the range. Otherwise, extract its start.
  20351. Return a new timestamp object sharing the same parent as
  20352. TIMESTAMP."
  20353. (let ((type (org-element-property :type timestamp)))
  20354. (if (memq type '(active inactive diary)) timestamp
  20355. (let ((split-ts (list 'timestamp (copy-sequence (nth 1 timestamp)))))
  20356. ;; Set new type.
  20357. (org-element-put-property
  20358. split-ts :type (if (eq type 'active-range) 'active 'inactive))
  20359. ;; Copy start properties over end properties if END is
  20360. ;; non-nil. Otherwise, copy end properties over `start' ones.
  20361. (let ((p-alist '((:minute-start . :minute-end)
  20362. (:hour-start . :hour-end)
  20363. (:day-start . :day-end)
  20364. (:month-start . :month-end)
  20365. (:year-start . :year-end))))
  20366. (dolist (p-cell p-alist)
  20367. (org-element-put-property
  20368. split-ts
  20369. (funcall (if end 'car 'cdr) p-cell)
  20370. (org-element-property
  20371. (funcall (if end 'cdr 'car) p-cell) split-ts)))
  20372. ;; Eventually refresh `:raw-value'.
  20373. (org-element-put-property split-ts :raw-value nil)
  20374. (org-element-put-property
  20375. split-ts :raw-value (org-element-interpret-data split-ts)))))))
  20376. (defun org-timestamp-translate (timestamp &optional boundary)
  20377. "Apply `org-translate-time' on a TIMESTAMP object.
  20378. When optional argument BOUNDARY is non-nil, it is either the
  20379. symbol `start' or `end'. In this case, only translate the
  20380. starting or ending part of TIMESTAMP if it is a date or time
  20381. range. Otherwise, translate both parts."
  20382. (if (and (not boundary)
  20383. (memq (org-element-property :type timestamp)
  20384. '(active-range inactive-range)))
  20385. (concat
  20386. (org-translate-time
  20387. (org-element-property :raw-value
  20388. (org-timestamp-split-range timestamp)))
  20389. "--"
  20390. (org-translate-time
  20391. (org-element-property :raw-value
  20392. (org-timestamp-split-range timestamp t))))
  20393. (org-translate-time
  20394. (org-element-property
  20395. :raw-value
  20396. (if (not boundary) timestamp
  20397. (org-timestamp-split-range timestamp (eq boundary 'end)))))))
  20398. ;;; Other stuff.
  20399. (defun org-toggle-fixed-width-section (arg)
  20400. "Toggle the fixed-width export.
  20401. If there is no active region, the QUOTE keyword at the current headline is
  20402. inserted or removed. When present, it causes the text between this headline
  20403. and the next to be exported as fixed-width text, and unmodified.
  20404. If there is an active region, this command adds or removes a colon as the
  20405. first character of this line. If the first character of a line is a colon,
  20406. this line is also exported in fixed-width font."
  20407. (interactive "P")
  20408. (let* ((cc 0)
  20409. (regionp (org-region-active-p))
  20410. (beg (if regionp (region-beginning) (point)))
  20411. (end (if regionp (region-end)))
  20412. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  20413. (case-fold-search nil)
  20414. (re "[ \t]*\\(:\\(?: \\|$\\)\\)")
  20415. off)
  20416. (if regionp
  20417. (save-excursion
  20418. (goto-char beg)
  20419. (setq cc (current-column))
  20420. (beginning-of-line 1)
  20421. (setq off (looking-at re))
  20422. (while (> nlines 0)
  20423. (setq nlines (1- nlines))
  20424. (beginning-of-line 1)
  20425. (cond
  20426. (arg
  20427. (org-move-to-column cc t)
  20428. (insert ": \n")
  20429. (forward-line -1))
  20430. ((and off (looking-at re))
  20431. (replace-match "" t t nil 1))
  20432. ((not off) (org-move-to-column cc t) (insert ": ")))
  20433. (forward-line 1)))
  20434. (save-excursion
  20435. (org-back-to-heading)
  20436. (cond
  20437. ((looking-at (format org-heading-keyword-regexp-format
  20438. org-quote-string))
  20439. (goto-char (match-end 1))
  20440. (looking-at (concat " +" org-quote-string))
  20441. (replace-match "" t t)
  20442. (when (eolp) (insert " ")))
  20443. ((looking-at org-outline-regexp)
  20444. (goto-char (match-end 0))
  20445. (insert org-quote-string " ")))))))
  20446. (defun org-reftex-citation ()
  20447. "Use reftex-citation to insert a citation into the buffer.
  20448. This looks for a line like
  20449. #+BIBLIOGRAPHY: foo plain option:-d
  20450. and derives from it that foo.bib is the bibliography file relevant
  20451. for this document. It then installs the necessary environment for RefTeX
  20452. to work in this buffer and calls `reftex-citation' to insert a citation
  20453. into the buffer.
  20454. Export of such citations to both LaTeX and HTML is handled by the contributed
  20455. package org-exp-bibtex by Taru Karttunen."
  20456. (interactive)
  20457. (let ((reftex-docstruct-symbol 'rds)
  20458. (reftex-cite-format "\\cite{%l}")
  20459. rds bib)
  20460. (save-excursion
  20461. (save-restriction
  20462. (widen)
  20463. (let ((case-fold-search t)
  20464. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  20465. (if (not (save-excursion
  20466. (or (re-search-forward re nil t)
  20467. (re-search-backward re nil t))))
  20468. (error "No bibliography defined in file")
  20469. (setq bib (concat (match-string 1) ".bib")
  20470. rds (list (list 'bib bib)))))))
  20471. (call-interactively 'reftex-citation)))
  20472. ;;;; Functions extending outline functionality
  20473. (defun org-beginning-of-line (&optional arg)
  20474. "Go to the beginning of the current line. If that is invisible, continue
  20475. to a visible line beginning. This makes the function of C-a more intuitive.
  20476. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  20477. first attempt, and only move to after the tags when the cursor is already
  20478. beyond the end of the headline."
  20479. (interactive "P")
  20480. (let ((pos (point))
  20481. (special (if (consp org-special-ctrl-a/e)
  20482. (car org-special-ctrl-a/e)
  20483. org-special-ctrl-a/e))
  20484. refpos)
  20485. (if (org-bound-and-true-p visual-line-mode)
  20486. (beginning-of-visual-line 1)
  20487. (beginning-of-line 1))
  20488. (if (and arg (fboundp 'move-beginning-of-line))
  20489. (call-interactively 'move-beginning-of-line)
  20490. (if (bobp)
  20491. nil
  20492. (backward-char 1)
  20493. (if (org-truely-invisible-p)
  20494. (while (and (not (bobp)) (org-truely-invisible-p))
  20495. (backward-char 1)
  20496. (beginning-of-line 1))
  20497. (forward-char 1))))
  20498. (when special
  20499. (cond
  20500. ((and (looking-at org-complex-heading-regexp)
  20501. (= (char-after (match-end 1)) ?\ ))
  20502. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  20503. (point-at-eol)))
  20504. (goto-char
  20505. (if (eq special t)
  20506. (cond ((> pos refpos) refpos)
  20507. ((= pos (point)) refpos)
  20508. (t (point)))
  20509. (cond ((> pos (point)) (point))
  20510. ((not (eq last-command this-command)) (point))
  20511. (t refpos)))))
  20512. ((org-at-item-p)
  20513. ;; Being at an item and not looking at an the item means point
  20514. ;; was previously moved to beginning of a visual line, which
  20515. ;; doesn't contain the item. Therefore, do nothing special,
  20516. ;; just stay here.
  20517. (when (looking-at org-list-full-item-re)
  20518. ;; Set special position at first white space character after
  20519. ;; bullet, and check-box, if any.
  20520. (let ((after-bullet
  20521. (let ((box (match-end 3)))
  20522. (if (not box) (match-end 1)
  20523. (let ((after (char-after box)))
  20524. (if (and after (= after ? )) (1+ box) box))))))
  20525. ;; Special case: Move point to special position when
  20526. ;; currently after it or at beginning of line.
  20527. (if (eq special t)
  20528. (when (or (> pos after-bullet) (= (point) pos))
  20529. (goto-char after-bullet))
  20530. ;; Reversed case: Move point to special position when
  20531. ;; point was already at beginning of line and command is
  20532. ;; repeated.
  20533. (when (and (= (point) pos) (eq last-command this-command))
  20534. (goto-char after-bullet))))))))
  20535. (org-no-warnings
  20536. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  20537. (defun org-end-of-line (&optional arg)
  20538. "Go to the end of the line.
  20539. If this is a headline, and `org-special-ctrl-a/e' is set, ignore
  20540. tags on the first attempt, and only move to after the tags when
  20541. the cursor is already beyond the end of the headline."
  20542. (interactive "P")
  20543. (let ((special (if (consp org-special-ctrl-a/e) (cdr org-special-ctrl-a/e)
  20544. org-special-ctrl-a/e))
  20545. (move-fun (cond ((org-bound-and-true-p visual-line-mode)
  20546. 'end-of-visual-line)
  20547. ((fboundp 'move-end-of-line) 'move-end-of-line)
  20548. (t 'end-of-line))))
  20549. (if (or (not special) arg) (call-interactively move-fun)
  20550. (let* ((element (save-excursion (beginning-of-line)
  20551. (org-element-at-point)))
  20552. (type (org-element-type element)))
  20553. (cond
  20554. ((memq type '(headline inlinetask))
  20555. (let ((pos (point)))
  20556. (beginning-of-line 1)
  20557. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  20558. (if (eq special t)
  20559. (if (or (< pos (match-beginning 1)) (= pos (match-end 0)))
  20560. (goto-char (match-beginning 1))
  20561. (goto-char (match-end 0)))
  20562. (if (or (< pos (match-end 0))
  20563. (not (eq this-command last-command)))
  20564. (goto-char (match-end 0))
  20565. (goto-char (match-beginning 1))))
  20566. (call-interactively move-fun))))
  20567. ((org-element-property :hiddenp element)
  20568. ;; If element is hidden, `move-end-of-line' would put point
  20569. ;; after it. Use `end-of-line' to stay on current line.
  20570. (call-interactively 'end-of-line))
  20571. (t (call-interactively move-fun)))))
  20572. (org-no-warnings (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  20573. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  20574. (define-key org-mode-map "\C-e" 'org-end-of-line)
  20575. (defun org-backward-sentence (&optional arg)
  20576. "Go to beginning of sentence, or beginning of table field.
  20577. This will call `backward-sentence' or `org-table-beginning-of-field',
  20578. depending on context."
  20579. (interactive "P")
  20580. (cond
  20581. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  20582. (t (call-interactively 'backward-sentence))))
  20583. (defun org-forward-sentence (&optional arg)
  20584. "Go to end of sentence, or end of table field.
  20585. This will call `forward-sentence' or `org-table-end-of-field',
  20586. depending on context."
  20587. (interactive "P")
  20588. (cond
  20589. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  20590. (t (call-interactively 'forward-sentence))))
  20591. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  20592. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  20593. (defun org-kill-line (&optional arg)
  20594. "Kill line, to tags or end of line."
  20595. (interactive "P")
  20596. (cond
  20597. ((or (not org-special-ctrl-k)
  20598. (bolp)
  20599. (not (org-at-heading-p)))
  20600. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  20601. org-ctrl-k-protect-subtree)
  20602. (if (or (eq org-ctrl-k-protect-subtree 'error)
  20603. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  20604. (error "C-k aborted - would kill hidden subtree")))
  20605. (call-interactively
  20606. (if (org-bound-and-true-p visual-line-mode) 'kill-visual-line 'kill-line)))
  20607. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  20608. (kill-region (point) (match-beginning 1))
  20609. (org-set-tags nil t))
  20610. (t (kill-region (point) (point-at-eol)))))
  20611. (define-key org-mode-map "\C-k" 'org-kill-line)
  20612. (defun org-yank (&optional arg)
  20613. "Yank. If the kill is a subtree, treat it specially.
  20614. This command will look at the current kill and check if is a single
  20615. subtree, or a series of subtrees[1]. If it passes the test, and if the
  20616. cursor is at the beginning of a line or after the stars of a currently
  20617. empty headline, then the yank is handled specially. How exactly depends
  20618. on the value of the following variables, both set by default.
  20619. org-yank-folded-subtrees
  20620. When set, the subtree(s) will be folded after insertion, but only
  20621. if doing so would now swallow text after the yanked text.
  20622. org-yank-adjusted-subtrees
  20623. When set, the subtree will be promoted or demoted in order to
  20624. fit into the local outline tree structure, which means that the level
  20625. will be adjusted so that it becomes the smaller one of the two
  20626. *visible* surrounding headings.
  20627. Any prefix to this command will cause `yank' to be called directly with
  20628. no special treatment. In particular, a simple \\[universal-argument] prefix \
  20629. will just
  20630. plainly yank the text as it is.
  20631. \[1] The test checks if the first non-white line is a heading
  20632. and if there are no other headings with fewer stars."
  20633. (interactive "P")
  20634. (org-yank-generic 'yank arg))
  20635. (defun org-yank-generic (command arg)
  20636. "Perform some yank-like command.
  20637. This function implements the behavior described in the `org-yank'
  20638. documentation. However, it has been generalized to work for any
  20639. interactive command with similar behavior."
  20640. ;; pretend to be command COMMAND
  20641. (setq this-command command)
  20642. (if arg
  20643. (call-interactively command)
  20644. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  20645. (and (org-kill-is-subtree-p)
  20646. (or (bolp)
  20647. (and (looking-at "[ \t]*$")
  20648. (string-match
  20649. "\\`\\*+\\'"
  20650. (buffer-substring (point-at-bol) (point)))))))
  20651. swallowp)
  20652. (cond
  20653. ((and subtreep org-yank-folded-subtrees)
  20654. (let ((beg (point))
  20655. end)
  20656. (if (and subtreep org-yank-adjusted-subtrees)
  20657. (org-paste-subtree nil nil 'for-yank)
  20658. (call-interactively command))
  20659. (setq end (point))
  20660. (goto-char beg)
  20661. (when (and (bolp) subtreep
  20662. (not (setq swallowp
  20663. (org-yank-folding-would-swallow-text beg end))))
  20664. (org-with-limited-levels
  20665. (or (looking-at org-outline-regexp)
  20666. (re-search-forward org-outline-regexp-bol end t))
  20667. (while (and (< (point) end) (looking-at org-outline-regexp))
  20668. (hide-subtree)
  20669. (org-cycle-show-empty-lines 'folded)
  20670. (condition-case nil
  20671. (outline-forward-same-level 1)
  20672. (error (goto-char end))))))
  20673. (when swallowp
  20674. (message
  20675. "Inserted text not folded because that would swallow text"))
  20676. (goto-char end)
  20677. (skip-chars-forward " \t\n\r")
  20678. (beginning-of-line 1)
  20679. (push-mark beg 'nomsg)))
  20680. ((and subtreep org-yank-adjusted-subtrees)
  20681. (let ((beg (point-at-bol)))
  20682. (org-paste-subtree nil nil 'for-yank)
  20683. (push-mark beg 'nomsg)))
  20684. (t
  20685. (call-interactively command))))))
  20686. (defun org-yank-folding-would-swallow-text (beg end)
  20687. "Would hide-subtree at BEG swallow any text after END?"
  20688. (let (level)
  20689. (org-with-limited-levels
  20690. (save-excursion
  20691. (goto-char beg)
  20692. (when (or (looking-at org-outline-regexp)
  20693. (re-search-forward org-outline-regexp-bol end t))
  20694. (setq level (org-outline-level)))
  20695. (goto-char end)
  20696. (skip-chars-forward " \t\r\n\v\f")
  20697. (if (or (eobp)
  20698. (and (bolp) (looking-at org-outline-regexp)
  20699. (<= (org-outline-level) level)))
  20700. nil ; Nothing would be swallowed
  20701. t))))) ; something would swallow
  20702. (define-key org-mode-map "\C-y" 'org-yank)
  20703. (defun org-truely-invisible-p ()
  20704. "Check if point is at a character currently not visible.
  20705. This version does not only check the character property, but also
  20706. `visible-mode'."
  20707. ;; Early versions of noutline don't have `outline-invisible-p'.
  20708. (if (org-bound-and-true-p visible-mode)
  20709. nil
  20710. (outline-invisible-p)))
  20711. (defun org-invisible-p2 ()
  20712. "Check if point is at a character currently not visible."
  20713. (save-excursion
  20714. (if (and (eolp) (not (bobp))) (backward-char 1))
  20715. ;; Early versions of noutline don't have `outline-invisible-p'.
  20716. (outline-invisible-p)))
  20717. (defun org-back-to-heading (&optional invisible-ok)
  20718. "Call `outline-back-to-heading', but provide a better error message."
  20719. (condition-case nil
  20720. (outline-back-to-heading invisible-ok)
  20721. (error (error "Before first headline at position %d in buffer %s"
  20722. (point) (current-buffer)))))
  20723. (defun org-before-first-heading-p ()
  20724. "Before first heading?"
  20725. (save-excursion
  20726. (end-of-line)
  20727. (null (re-search-backward org-outline-regexp-bol nil t))))
  20728. (defun org-at-heading-p (&optional ignored)
  20729. (outline-on-heading-p t))
  20730. ;; Compatibility alias with Org versions < 7.8.03
  20731. (defalias 'org-on-heading-p 'org-at-heading-p)
  20732. (defun org-at-comment-p nil
  20733. "Is cursor in a line starting with a # character?"
  20734. (save-excursion
  20735. (beginning-of-line)
  20736. (looking-at "^#")))
  20737. (defun org-at-drawer-p nil
  20738. "Is cursor at a drawer keyword?"
  20739. (save-excursion
  20740. (move-beginning-of-line 1)
  20741. (looking-at org-drawer-regexp)))
  20742. (defun org-at-block-p nil
  20743. "Is cursor at a block keyword?"
  20744. (save-excursion
  20745. (move-beginning-of-line 1)
  20746. (looking-at org-block-regexp)))
  20747. (defun org-point-at-end-of-empty-headline ()
  20748. "If point is at the end of an empty headline, return t, else nil.
  20749. If the heading only contains a TODO keyword, it is still still considered
  20750. empty."
  20751. (and (looking-at "[ \t]*$")
  20752. (when org-todo-line-regexp
  20753. (save-excursion
  20754. (beginning-of-line 1)
  20755. (let ((case-fold-search nil))
  20756. (looking-at org-todo-line-regexp)
  20757. (string= (match-string 3) ""))))))
  20758. (defun org-at-heading-or-item-p ()
  20759. (or (org-at-heading-p) (org-at-item-p)))
  20760. (defun org-at-target-p ()
  20761. (or (org-in-regexp org-radio-target-regexp)
  20762. (org-in-regexp org-target-regexp)))
  20763. ;; Compatibility alias with Org versions < 7.8.03
  20764. (defalias 'org-on-target-p 'org-at-target-p)
  20765. (defun org-up-heading-all (arg)
  20766. "Move to the heading line of which the present line is a subheading.
  20767. This function considers both visible and invisible heading lines.
  20768. With argument, move up ARG levels."
  20769. (if (fboundp 'outline-up-heading-all)
  20770. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  20771. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  20772. (defun org-up-heading-safe ()
  20773. "Move to the heading line of which the present line is a subheading.
  20774. This version will not throw an error. It will return the level of the
  20775. headline found, or nil if no higher level is found.
  20776. Also, this function will be a lot faster than `outline-up-heading',
  20777. because it relies on stars being the outline starters. This can really
  20778. make a significant difference in outlines with very many siblings."
  20779. (let (start-level re)
  20780. (org-back-to-heading t)
  20781. (setq start-level (funcall outline-level))
  20782. (if (equal start-level 1)
  20783. nil
  20784. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  20785. (if (re-search-backward re nil t)
  20786. (funcall outline-level)))))
  20787. (defun org-first-sibling-p ()
  20788. "Is this heading the first child of its parents?"
  20789. (interactive)
  20790. (let ((re org-outline-regexp-bol)
  20791. level l)
  20792. (unless (org-at-heading-p t)
  20793. (error "Not at a heading"))
  20794. (setq level (funcall outline-level))
  20795. (save-excursion
  20796. (if (not (re-search-backward re nil t))
  20797. t
  20798. (setq l (funcall outline-level))
  20799. (< l level)))))
  20800. (defun org-goto-sibling (&optional previous)
  20801. "Goto the next sibling, even if it is invisible.
  20802. When PREVIOUS is set, go to the previous sibling instead. Returns t
  20803. when a sibling was found. When none is found, return nil and don't
  20804. move point."
  20805. (let ((fun (if previous 're-search-backward 're-search-forward))
  20806. (pos (point))
  20807. (re org-outline-regexp-bol)
  20808. level l)
  20809. (when (condition-case nil (org-back-to-heading t) (error nil))
  20810. (setq level (funcall outline-level))
  20811. (catch 'exit
  20812. (or previous (forward-char 1))
  20813. (while (funcall fun re nil t)
  20814. (setq l (funcall outline-level))
  20815. (when (< l level) (goto-char pos) (throw 'exit nil))
  20816. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  20817. (goto-char pos)
  20818. nil))))
  20819. (defun org-show-siblings ()
  20820. "Show all siblings of the current headline."
  20821. (save-excursion
  20822. (while (org-goto-sibling) (org-flag-heading nil)))
  20823. (save-excursion
  20824. (while (org-goto-sibling 'previous)
  20825. (org-flag-heading nil))))
  20826. (defun org-goto-first-child ()
  20827. "Goto the first child, even if it is invisible.
  20828. Return t when a child was found. Otherwise don't move point and
  20829. return nil."
  20830. (let (level (pos (point)) (re org-outline-regexp-bol))
  20831. (when (condition-case nil (org-back-to-heading t) (error nil))
  20832. (setq level (outline-level))
  20833. (forward-char 1)
  20834. (if (and (re-search-forward re nil t) (> (outline-level) level))
  20835. (progn (goto-char (match-beginning 0)) t)
  20836. (goto-char pos) nil))))
  20837. (defun org-show-hidden-entry ()
  20838. "Show an entry where even the heading is hidden."
  20839. (save-excursion
  20840. (org-show-entry)))
  20841. (defun org-flag-heading (flag &optional entry)
  20842. "Flag the current heading. FLAG non-nil means make invisible.
  20843. When ENTRY is non-nil, show the entire entry."
  20844. (save-excursion
  20845. (org-back-to-heading t)
  20846. ;; Check if we should show the entire entry
  20847. (if entry
  20848. (progn
  20849. (org-show-entry)
  20850. (save-excursion
  20851. (and (outline-next-heading)
  20852. (org-flag-heading nil))))
  20853. (outline-flag-region (max (point-min) (1- (point)))
  20854. (save-excursion (outline-end-of-heading) (point))
  20855. flag))))
  20856. (defun org-get-next-sibling ()
  20857. "Move to next heading of the same level, and return point.
  20858. If there is no such heading, return nil.
  20859. This is like outline-next-sibling, but invisible headings are ok."
  20860. (let ((level (funcall outline-level)))
  20861. (outline-next-heading)
  20862. (while (and (not (eobp)) (> (funcall outline-level) level))
  20863. (outline-next-heading))
  20864. (if (or (eobp) (< (funcall outline-level) level))
  20865. nil
  20866. (point))))
  20867. (defun org-get-last-sibling ()
  20868. "Move to previous heading of the same level, and return point.
  20869. If there is no such heading, return nil."
  20870. (let ((opoint (point))
  20871. (level (funcall outline-level)))
  20872. (outline-previous-heading)
  20873. (when (and (/= (point) opoint) (outline-on-heading-p t))
  20874. (while (and (> (funcall outline-level) level)
  20875. (not (bobp)))
  20876. (outline-previous-heading))
  20877. (if (< (funcall outline-level) level)
  20878. nil
  20879. (point)))))
  20880. (defun org-end-of-subtree (&optional invisible-ok to-heading)
  20881. "Goto to the end of a subtree."
  20882. ;; This contains an exact copy of the original function, but it uses
  20883. ;; `org-back-to-heading', to make it work also in invisible
  20884. ;; trees. And is uses an invisible-ok argument.
  20885. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  20886. ;; Furthermore, when used inside Org, finding the end of a large subtree
  20887. ;; with many children and grandchildren etc, this can be much faster
  20888. ;; than the outline version.
  20889. (org-back-to-heading invisible-ok)
  20890. (let ((first t)
  20891. (level (funcall outline-level)))
  20892. (if (and (derived-mode-p 'org-mode) (< level 1000))
  20893. ;; A true heading (not a plain list item), in Org-mode
  20894. ;; This means we can easily find the end by looking
  20895. ;; only for the right number of stars. Using a regexp to do
  20896. ;; this is so much faster than using a Lisp loop.
  20897. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  20898. (forward-char 1)
  20899. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  20900. ;; something else, do it the slow way
  20901. (while (and (not (eobp))
  20902. (or first (> (funcall outline-level) level)))
  20903. (setq first nil)
  20904. (outline-next-heading)))
  20905. (unless to-heading
  20906. (if (memq (preceding-char) '(?\n ?\^M))
  20907. (progn
  20908. ;; Go to end of line before heading
  20909. (forward-char -1)
  20910. (if (memq (preceding-char) '(?\n ?\^M))
  20911. ;; leave blank line before heading
  20912. (forward-char -1))))))
  20913. (point))
  20914. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  20915. "Use Org version in org-mode, for dramatic speed-up."
  20916. (if (derived-mode-p 'org-mode)
  20917. (progn
  20918. (org-end-of-subtree nil t)
  20919. (unless (eobp) (backward-char 1)))
  20920. ad-do-it))
  20921. (defun org-end-of-meta-data-and-drawers ()
  20922. "Jump to the first text after meta data and drawers in the current entry.
  20923. This will move over empty lines, lines with planning time stamps,
  20924. clocking lines, and drawers."
  20925. (org-back-to-heading t)
  20926. (let ((end (save-excursion (outline-next-heading) (point)))
  20927. (re (concat "\\(" org-drawer-regexp "\\)"
  20928. "\\|" "[ \t]*" org-keyword-time-regexp)))
  20929. (forward-line 1)
  20930. (while (re-search-forward re end t)
  20931. (if (not (match-end 1))
  20932. ;; empty or planning line
  20933. (forward-line 1)
  20934. ;; a drawer, find the end
  20935. (re-search-forward "^[ \t]*:END:" end 'move)
  20936. (forward-line 1)))
  20937. (and (re-search-forward "[^\n]" nil t) (backward-char 1))
  20938. (point)))
  20939. (defun org-forward-heading-same-level (arg &optional invisible-ok)
  20940. "Move forward to the ARG'th subheading at same level as this one.
  20941. Stop at the first and last subheadings of a superior heading.
  20942. Normally this only looks at visible headings, but when INVISIBLE-OK is
  20943. non-nil it will also look at invisible ones."
  20944. (interactive "p")
  20945. (if (not (ignore-errors (org-back-to-heading invisible-ok)))
  20946. (if (and arg (< arg 0))
  20947. (goto-char (point-min))
  20948. (outline-next-heading))
  20949. (org-at-heading-p)
  20950. (let ((level (- (match-end 0) (match-beginning 0) 1))
  20951. (f (if (and arg (< arg 0))
  20952. 're-search-backward
  20953. 're-search-forward))
  20954. (count (if arg (abs arg) 1))
  20955. (result (point)))
  20956. (forward-char (if (and arg (< arg 0)) -1 1))
  20957. (while (and (> count 0)
  20958. (funcall f org-outline-regexp-bol nil 'move))
  20959. (let ((l (- (match-end 0) (match-beginning 0) 1)))
  20960. (cond ((< l level) (setq count 0))
  20961. ((and (= l level)
  20962. (or invisible-ok
  20963. (progn
  20964. (goto-char (line-beginning-position))
  20965. (not (outline-invisible-p)))))
  20966. (setq count (1- count))
  20967. (when (eq l level)
  20968. (setq result (point)))))))
  20969. (goto-char result))
  20970. (beginning-of-line 1)))
  20971. (defun org-backward-heading-same-level (arg &optional invisible-ok)
  20972. "Move backward to the ARG'th subheading at same level as this one.
  20973. Stop at the first and last subheadings of a superior heading."
  20974. (interactive "p")
  20975. (org-forward-heading-same-level (if arg (- arg) -1) invisible-ok))
  20976. (defun org-next-block (arg &optional backward block-regexp)
  20977. "Jump to the next block.
  20978. With a prefix argument ARG, jump forward ARG many source blocks.
  20979. When BACKWARD is non-nil, jump to the previous block.
  20980. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  20981. (interactive "p")
  20982. (let ((re (or block-regexp org-block-regexp))
  20983. (re-search-fn (or (and backward 're-search-backward)
  20984. 're-search-forward)))
  20985. (if (looking-at re) (forward-char 1))
  20986. (condition-case nil
  20987. (funcall re-search-fn re nil nil arg)
  20988. (error (error "No %s code blocks" (if backward "previous" "further" ))))
  20989. (goto-char (match-beginning 0)) (org-show-context)))
  20990. (defun org-previous-block (arg &optional block-regexp)
  20991. "Jump to the previous block.
  20992. With a prefix argument ARG, jump backward ARG many source blocks.
  20993. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  20994. (interactive "p")
  20995. (org-next-block arg t block-regexp))
  20996. (defun org-forward-element ()
  20997. "Move forward by one element.
  20998. Move to the next element at the same level, when possible."
  20999. (interactive)
  21000. (cond ((eobp) (user-error "Cannot move further down"))
  21001. ((org-with-limited-levels (org-at-heading-p))
  21002. (let ((origin (point)))
  21003. (goto-char (org-end-of-subtree nil t))
  21004. (unless (org-with-limited-levels (org-at-heading-p))
  21005. (goto-char origin)
  21006. (user-error "Cannot move further down"))))
  21007. (t
  21008. (let* ((elem (org-element-at-point))
  21009. (end (org-element-property :end elem))
  21010. (parent (org-element-property :parent elem)))
  21011. (if (and parent (= (org-element-property :contents-end parent) end))
  21012. (goto-char (org-element-property :end parent))
  21013. (goto-char end))))))
  21014. (defun org-backward-element ()
  21015. "Move backward by one element.
  21016. Move to the previous element at the same level, when possible."
  21017. (interactive)
  21018. (cond ((bobp) (user-error "Cannot move further up"))
  21019. ((org-with-limited-levels (org-at-heading-p))
  21020. ;; At a headline, move to the previous one, if any, or stay
  21021. ;; here.
  21022. (let ((origin (point)))
  21023. (org-with-limited-levels (org-backward-heading-same-level 1))
  21024. ;; When current headline has no sibling above, move to its
  21025. ;; parent.
  21026. (when (= (point) origin)
  21027. (or (org-with-limited-levels (org-up-heading-safe))
  21028. (progn (goto-char origin)
  21029. (user-error "Cannot move further up"))))))
  21030. (t
  21031. (let* ((trail (org-element-at-point 'keep-trail))
  21032. (elem (car trail))
  21033. (prev-elem (nth 1 trail))
  21034. (beg (org-element-property :begin elem)))
  21035. (cond
  21036. ;; Move to beginning of current element if point isn't
  21037. ;; there already.
  21038. ((/= (point) beg) (goto-char beg))
  21039. (prev-elem (goto-char (org-element-property :begin prev-elem)))
  21040. ((org-before-first-heading-p) (goto-char (point-min)))
  21041. (t (org-back-to-heading)))))))
  21042. (defun org-up-element ()
  21043. "Move to upper element."
  21044. (interactive)
  21045. (if (org-with-limited-levels (org-at-heading-p))
  21046. (unless (org-up-heading-safe) (error "No surrounding element"))
  21047. (let* ((elem (org-element-at-point))
  21048. (parent (org-element-property :parent elem)))
  21049. (if parent (goto-char (org-element-property :begin parent))
  21050. (if (org-with-limited-levels (org-before-first-heading-p))
  21051. (error "No surrounding element")
  21052. (org-with-limited-levels (org-back-to-heading)))))))
  21053. (defvar org-element-greater-elements)
  21054. (defun org-down-element ()
  21055. "Move to inner element."
  21056. (interactive)
  21057. (let ((element (org-element-at-point)))
  21058. (cond
  21059. ((memq (org-element-type element) '(plain-list table))
  21060. (goto-char (org-element-property :contents-begin element))
  21061. (forward-char))
  21062. ((memq (org-element-type element) org-element-greater-elements)
  21063. ;; If contents are hidden, first disclose them.
  21064. (when (org-element-property :hiddenp element) (org-cycle))
  21065. (goto-char (or (org-element-property :contents-begin element)
  21066. (error "No content for this element"))))
  21067. (t (error "No inner element")))))
  21068. (defun org-drag-element-backward ()
  21069. "Move backward element at point."
  21070. (interactive)
  21071. (if (org-with-limited-levels (org-at-heading-p)) (org-move-subtree-up)
  21072. (let* ((trail (org-element-at-point 'keep-trail))
  21073. (elem (car trail))
  21074. (prev-elem (nth 1 trail)))
  21075. ;; Error out if no previous element or previous element is
  21076. ;; a parent of the current one.
  21077. (if (or (not prev-elem) (org-element-nested-p elem prev-elem))
  21078. (error "Cannot drag element backward")
  21079. (let ((pos (point)))
  21080. (org-element-swap-A-B prev-elem elem)
  21081. (goto-char (+ (org-element-property :begin prev-elem)
  21082. (- pos (org-element-property :begin elem)))))))))
  21083. (defun org-drag-element-forward ()
  21084. "Move forward element at point."
  21085. (interactive)
  21086. (let* ((pos (point))
  21087. (elem (org-element-at-point)))
  21088. (when (= (point-max) (org-element-property :end elem))
  21089. (error "Cannot drag element forward"))
  21090. (goto-char (org-element-property :end elem))
  21091. (let ((next-elem (org-element-at-point)))
  21092. (when (or (org-element-nested-p elem next-elem)
  21093. (and (eq (org-element-type next-elem) 'headline)
  21094. (not (eq (org-element-type elem) 'headline))))
  21095. (goto-char pos)
  21096. (error "Cannot drag element forward"))
  21097. ;; Compute new position of point: it's shifted by NEXT-ELEM
  21098. ;; body's length (without final blanks) and by the length of
  21099. ;; blanks between ELEM and NEXT-ELEM.
  21100. (let ((size-next (- (save-excursion
  21101. (goto-char (org-element-property :end next-elem))
  21102. (skip-chars-backward " \r\t\n")
  21103. (forward-line)
  21104. ;; Small correction if buffer doesn't end
  21105. ;; with a newline character.
  21106. (if (and (eolp) (not (bolp))) (1+ (point)) (point)))
  21107. (org-element-property :begin next-elem)))
  21108. (size-blank (- (org-element-property :end elem)
  21109. (save-excursion
  21110. (goto-char (org-element-property :end elem))
  21111. (skip-chars-backward " \r\t\n")
  21112. (forward-line)
  21113. (point)))))
  21114. (org-element-swap-A-B elem next-elem)
  21115. (goto-char (+ pos size-next size-blank))))))
  21116. (defun org-drag-line-forward (arg)
  21117. "Drag the line at point ARG lines forward."
  21118. (interactive "p")
  21119. (dotimes (n (abs arg))
  21120. (let ((c (current-column)))
  21121. (if (< 0 arg)
  21122. (progn
  21123. (beginning-of-line 2)
  21124. (transpose-lines 1)
  21125. (beginning-of-line 0))
  21126. (transpose-lines 1)
  21127. (beginning-of-line -1))
  21128. (org-move-to-column c))))
  21129. (defun org-drag-line-backward (arg)
  21130. "Drag the line at point ARG lines backward."
  21131. (interactive "p")
  21132. (org-drag-line-forward (- arg)))
  21133. (defun org-mark-element ()
  21134. "Put point at beginning of this element, mark at end.
  21135. Interactively, if this command is repeated or (in Transient Mark
  21136. mode) if the mark is active, it marks the next element after the
  21137. ones already marked."
  21138. (interactive)
  21139. (let (deactivate-mark)
  21140. (if (and (org-called-interactively-p 'any)
  21141. (or (and (eq last-command this-command) (mark t))
  21142. (and transient-mark-mode mark-active)))
  21143. (set-mark
  21144. (save-excursion
  21145. (goto-char (mark))
  21146. (goto-char (org-element-property :end (org-element-at-point)))))
  21147. (let ((element (org-element-at-point)))
  21148. (end-of-line)
  21149. (push-mark (org-element-property :end element) t t)
  21150. (goto-char (org-element-property :begin element))))))
  21151. (defun org-narrow-to-element ()
  21152. "Narrow buffer to current element."
  21153. (interactive)
  21154. (let ((elem (org-element-at-point)))
  21155. (cond
  21156. ((eq (car elem) 'headline)
  21157. (narrow-to-region
  21158. (org-element-property :begin elem)
  21159. (org-element-property :end elem)))
  21160. ((memq (car elem) org-element-greater-elements)
  21161. (narrow-to-region
  21162. (org-element-property :contents-begin elem)
  21163. (org-element-property :contents-end elem)))
  21164. (t
  21165. (narrow-to-region
  21166. (org-element-property :begin elem)
  21167. (org-element-property :end elem))))))
  21168. (defun org-transpose-element ()
  21169. "Transpose current and previous elements, keeping blank lines between.
  21170. Point is moved after both elements."
  21171. (interactive)
  21172. (org-skip-whitespace)
  21173. (let ((end (org-element-property :end (org-element-at-point))))
  21174. (org-drag-element-backward)
  21175. (goto-char end)))
  21176. (defun org-unindent-buffer ()
  21177. "Un-indent the visible part of the buffer.
  21178. Relative indentation (between items, inside blocks, etc.) isn't
  21179. modified."
  21180. (interactive)
  21181. (unless (eq major-mode 'org-mode)
  21182. (error "Cannot un-indent a buffer not in Org mode"))
  21183. (let* ((parse-tree (org-element-parse-buffer 'greater-element))
  21184. unindent-tree ; For byte-compiler.
  21185. (unindent-tree
  21186. (function
  21187. (lambda (contents)
  21188. (mapc
  21189. (lambda (element)
  21190. (if (memq (org-element-type element) '(headline section))
  21191. (funcall unindent-tree (org-element-contents element))
  21192. (save-excursion
  21193. (save-restriction
  21194. (narrow-to-region
  21195. (org-element-property :begin element)
  21196. (org-element-property :end element))
  21197. (org-do-remove-indentation)))))
  21198. (reverse contents))))))
  21199. (funcall unindent-tree (org-element-contents parse-tree))))
  21200. (defun org-show-subtree ()
  21201. "Show everything after this heading at deeper levels."
  21202. (interactive)
  21203. (outline-flag-region
  21204. (point)
  21205. (save-excursion
  21206. (org-end-of-subtree t t))
  21207. nil))
  21208. (defun org-show-entry ()
  21209. "Show the body directly following this heading.
  21210. Show the heading too, if it is currently invisible."
  21211. (interactive)
  21212. (save-excursion
  21213. (condition-case nil
  21214. (progn
  21215. (org-back-to-heading t)
  21216. (outline-flag-region
  21217. (max (point-min) (1- (point)))
  21218. (save-excursion
  21219. (if (re-search-forward
  21220. (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
  21221. (match-beginning 1)
  21222. (point-max)))
  21223. nil)
  21224. (org-cycle-hide-drawers 'children))
  21225. (error nil))))
  21226. (defun org-make-options-regexp (kwds &optional extra)
  21227. "Make a regular expression for keyword lines."
  21228. (concat
  21229. "^#\\+\\("
  21230. (mapconcat 'regexp-quote kwds "\\|")
  21231. (if extra (concat "\\|" extra))
  21232. "\\):[ \t]*\\(.*\\)"))
  21233. ;; Make isearch reveal the necessary context
  21234. (defun org-isearch-end ()
  21235. "Reveal context after isearch exits."
  21236. (when isearch-success ; only if search was successful
  21237. (if (featurep 'xemacs)
  21238. ;; Under XEmacs, the hook is run in the correct place,
  21239. ;; we directly show the context.
  21240. (org-show-context 'isearch)
  21241. ;; In Emacs the hook runs *before* restoring the overlays.
  21242. ;; So we have to use a one-time post-command-hook to do this.
  21243. ;; (Emacs 22 has a special variable, see function `org-mode')
  21244. (unless (and (boundp 'isearch-mode-end-hook-quit)
  21245. isearch-mode-end-hook-quit)
  21246. ;; Only when the isearch was not quitted.
  21247. (org-add-hook 'post-command-hook 'org-isearch-post-command
  21248. 'append 'local)))
  21249. (org-fix-ellipsis-at-bol)))
  21250. (defun org-isearch-post-command ()
  21251. "Remove self from hook, and show context."
  21252. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  21253. (org-show-context 'isearch))
  21254. ;;;; Integration with and fixes for other packages
  21255. ;;; Imenu support
  21256. (defvar org-imenu-markers nil
  21257. "All markers currently used by Imenu.")
  21258. (make-variable-buffer-local 'org-imenu-markers)
  21259. (defun org-imenu-new-marker (&optional pos)
  21260. "Return a new marker for use by Imenu, and remember the marker."
  21261. (let ((m (make-marker)))
  21262. (move-marker m (or pos (point)))
  21263. (push m org-imenu-markers)
  21264. m))
  21265. (defun org-imenu-get-tree ()
  21266. "Produce the index for Imenu."
  21267. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  21268. (setq org-imenu-markers nil)
  21269. (let* ((n org-imenu-depth)
  21270. (re (concat "^" (org-get-limited-outline-regexp)))
  21271. (subs (make-vector (1+ n) nil))
  21272. (last-level 0)
  21273. m level head0 head)
  21274. (save-excursion
  21275. (save-restriction
  21276. (widen)
  21277. (goto-char (point-max))
  21278. (while (re-search-backward re nil t)
  21279. (setq level (org-reduced-level (funcall outline-level)))
  21280. (when (and (<= level n)
  21281. (looking-at org-complex-heading-regexp)
  21282. (setq head0 (org-match-string-no-properties 4)))
  21283. (setq head (org-link-display-format head0)
  21284. m (org-imenu-new-marker))
  21285. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  21286. (if (>= level last-level)
  21287. (push (cons head m) (aref subs level))
  21288. (push (cons head (aref subs (1+ level))) (aref subs level))
  21289. (loop for i from (1+ level) to n do (aset subs i nil)))
  21290. (setq last-level level)))))
  21291. (aref subs 1)))
  21292. (eval-after-load "imenu"
  21293. '(progn
  21294. (add-hook 'imenu-after-jump-hook
  21295. (lambda ()
  21296. (if (derived-mode-p 'org-mode)
  21297. (org-show-context 'org-goto))))))
  21298. (defun org-link-display-format (link)
  21299. "Replace a link with its the description.
  21300. If there is no description, use the link target."
  21301. (save-match-data
  21302. (if (string-match org-bracket-link-analytic-regexp link)
  21303. (replace-match (if (match-end 5)
  21304. (match-string 5 link)
  21305. (concat (match-string 1 link)
  21306. (match-string 3 link)))
  21307. nil t link)
  21308. link)))
  21309. (defun org-toggle-link-display ()
  21310. "Toggle the literal or descriptive display of links."
  21311. (interactive)
  21312. (if org-descriptive-links
  21313. (progn (org-remove-from-invisibility-spec '(org-link))
  21314. (org-restart-font-lock)
  21315. (setq org-descriptive-links nil))
  21316. (progn (add-to-invisibility-spec '(org-link))
  21317. (org-restart-font-lock)
  21318. (setq org-descriptive-links t))))
  21319. ;; Speedbar support
  21320. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  21321. "Overlay marking the agenda restriction line in speedbar.")
  21322. (overlay-put org-speedbar-restriction-lock-overlay
  21323. 'face 'org-agenda-restriction-lock)
  21324. (overlay-put org-speedbar-restriction-lock-overlay
  21325. 'help-echo "Agendas are currently limited to this item.")
  21326. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  21327. (defun org-speedbar-set-agenda-restriction ()
  21328. "Restrict future agenda commands to the location at point in speedbar.
  21329. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  21330. (interactive)
  21331. (require 'org-agenda)
  21332. (let (p m tp np dir txt)
  21333. (cond
  21334. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  21335. 'org-imenu t))
  21336. (setq m (get-text-property p 'org-imenu-marker))
  21337. (with-current-buffer (marker-buffer m)
  21338. (goto-char m)
  21339. (org-agenda-set-restriction-lock 'subtree)))
  21340. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  21341. 'speedbar-function 'speedbar-find-file))
  21342. (setq tp (previous-single-property-change
  21343. (1+ p) 'speedbar-function)
  21344. np (next-single-property-change
  21345. tp 'speedbar-function)
  21346. dir (speedbar-line-directory)
  21347. txt (buffer-substring-no-properties (or tp (point-min))
  21348. (or np (point-max))))
  21349. (with-current-buffer (find-file-noselect
  21350. (let ((default-directory dir))
  21351. (expand-file-name txt)))
  21352. (unless (derived-mode-p 'org-mode)
  21353. (error "Cannot restrict to non-Org-mode file"))
  21354. (org-agenda-set-restriction-lock 'file)))
  21355. (t (error "Don't know how to restrict Org-mode's agenda")))
  21356. (move-overlay org-speedbar-restriction-lock-overlay
  21357. (point-at-bol) (point-at-eol))
  21358. (setq current-prefix-arg nil)
  21359. (org-agenda-maybe-redo)))
  21360. (eval-after-load "speedbar"
  21361. '(progn
  21362. (speedbar-add-supported-extension ".org")
  21363. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  21364. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  21365. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  21366. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  21367. (add-hook 'speedbar-visiting-tag-hook
  21368. (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
  21369. ;;; Fixes and Hacks for problems with other packages
  21370. ;; Make flyspell not check words in links, to not mess up our keymap
  21371. (defvar org-element-affiliated-keywords) ; From org-element.el
  21372. (defvar org-element-block-name-alist) ; From org-element.el
  21373. (defun org-mode-flyspell-verify ()
  21374. "Don't let flyspell put overlays at active buttons, or on
  21375. {todo,all-time,additional-option-like}-keywords."
  21376. (let ((pos (max (1- (point)) (point-min)))
  21377. (word (thing-at-point 'word)))
  21378. (and (not (get-text-property pos 'keymap))
  21379. (not (get-text-property pos 'org-no-flyspell))
  21380. (not (member word org-todo-keywords-1))
  21381. (not (member word org-all-time-keywords))
  21382. (not (member word org-options-keywords))
  21383. (not (member word (mapcar 'car org-startup-options)))
  21384. (not (member-ignore-case word org-element-affiliated-keywords))
  21385. (not (member-ignore-case word (org-get-export-keywords)))
  21386. (not (member-ignore-case
  21387. word (mapcar 'car org-element-block-name-alist)))
  21388. (not (member-ignore-case word '("BEGIN" "END" "ATTR"))))))
  21389. (defun org-remove-flyspell-overlays-in (beg end)
  21390. "Remove flyspell overlays in region."
  21391. (and (org-bound-and-true-p flyspell-mode)
  21392. (fboundp 'flyspell-delete-region-overlays)
  21393. (flyspell-delete-region-overlays beg end))
  21394. (add-text-properties beg end '(org-no-flyspell t)))
  21395. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  21396. (eval-after-load "bookmark"
  21397. '(if (boundp 'bookmark-after-jump-hook)
  21398. ;; We can use the hook
  21399. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  21400. ;; Hook not available, use advice
  21401. (defadvice bookmark-jump (after org-make-visible activate)
  21402. "Make the position visible."
  21403. (org-bookmark-jump-unhide))))
  21404. ;; Make sure saveplace shows the location if it was hidden
  21405. (eval-after-load "saveplace"
  21406. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  21407. "Make the position visible."
  21408. (org-bookmark-jump-unhide)))
  21409. ;; Make sure ecb shows the location if it was hidden
  21410. (eval-after-load "ecb"
  21411. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  21412. "Make hierarchy visible when jumping into location from ECB tree buffer."
  21413. (if (derived-mode-p 'org-mode)
  21414. (org-show-context))))
  21415. (defun org-bookmark-jump-unhide ()
  21416. "Unhide the current position, to show the bookmark location."
  21417. (and (derived-mode-p 'org-mode)
  21418. (or (outline-invisible-p)
  21419. (save-excursion (goto-char (max (point-min) (1- (point))))
  21420. (outline-invisible-p)))
  21421. (org-show-context 'bookmark-jump)))
  21422. ;; Make session.el ignore our circular variable
  21423. (eval-after-load "session"
  21424. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  21425. ;;;; Experimental code
  21426. (defun org-closed-in-range ()
  21427. "Sparse tree of items closed in a certain time range.
  21428. Still experimental, may disappear in the future."
  21429. (interactive)
  21430. ;; Get the time interval from the user.
  21431. (let* ((time1 (org-float-time
  21432. (org-read-date nil 'to-time nil "Starting date: ")))
  21433. (time2 (org-float-time
  21434. (org-read-date nil 'to-time nil "End date:")))
  21435. ;; callback function
  21436. (callback (lambda ()
  21437. (let ((time
  21438. (org-float-time
  21439. (apply 'encode-time
  21440. (org-parse-time-string
  21441. (match-string 1))))))
  21442. ;; check if time in interval
  21443. (and (>= time time1) (<= time time2))))))
  21444. ;; make tree, check each match with the callback
  21445. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  21446. ;;;; Finish up
  21447. (provide 'org)
  21448. (run-hooks 'org-load-hook)
  21449. ;;; org.el ends here