org.el 890 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680216812168221683216842168521686216872168821689216902169121692216932169421695216962169721698216992170021701217022170321704217052170621707217082170921710217112171221713217142171521716217172171821719217202172121722217232172421725217262172721728217292173021731217322173321734217352173621737217382173921740217412174221743217442174521746217472174821749217502175121752217532175421755217562175721758217592176021761217622176321764217652176621767217682176921770217712177221773217742177521776217772177821779217802178121782217832178421785217862178721788217892179021791217922179321794217952179621797217982179921800218012180221803218042180521806218072180821809218102181121812218132181421815218162181721818218192182021821218222182321824218252182621827218282182921830218312183221833218342183521836218372183821839218402184121842218432184421845218462184721848218492185021851218522185321854218552185621857218582185921860218612186221863218642186521866218672186821869218702187121872218732187421875218762187721878218792188021881218822188321884218852188621887218882188921890218912189221893218942189521896218972189821899219002190121902219032190421905219062190721908219092191021911219122191321914219152191621917219182191921920219212192221923219242192521926219272192821929219302193121932219332193421935219362193721938219392194021941219422194321944219452194621947219482194921950219512195221953219542195521956219572195821959219602196121962219632196421965219662196721968219692197021971219722197321974219752197621977219782197921980219812198221983219842198521986219872198821989219902199121992219932199421995219962199721998219992200022001220022200322004220052200622007220082200922010220112201222013220142201522016220172201822019220202202122022220232202422025220262202722028220292203022031220322203322034220352203622037220382203922040220412204222043220442204522046220472204822049220502205122052220532205422055220562205722058220592206022061220622206322064220652206622067220682206922070220712207222073220742207522076220772207822079220802208122082220832208422085220862208722088220892209022091220922209322094220952209622097220982209922100221012210222103221042210522106221072210822109221102211122112221132211422115221162211722118221192212022121221222212322124221252212622127221282212922130221312213222133221342213522136221372213822139221402214122142221432214422145221462214722148221492215022151221522215322154221552215622157221582215922160221612216222163221642216522166221672216822169221702217122172221732217422175221762217722178221792218022181221822218322184221852218622187221882218922190221912219222193221942219522196221972219822199222002220122202222032220422205222062220722208222092221022211222122221322214222152221622217222182221922220222212222222223222242222522226222272222822229222302223122232222332223422235222362223722238222392224022241222422224322244222452224622247222482224922250222512225222253222542225522256222572225822259222602226122262222632226422265222662226722268222692227022271222722227322274222752227622277222782227922280222812228222283222842228522286222872228822289222902229122292222932229422295222962229722298222992230022301223022230322304223052230622307223082230922310223112231222313223142231522316223172231822319223202232122322223232232422325223262232722328223292233022331223322233322334223352233622337223382233922340223412234222343223442234522346223472234822349223502235122352223532235422355223562235722358223592236022361223622236322364223652236622367223682236922370223712237222373223742237522376223772237822379223802238122382223832238422385223862238722388223892239022391223922239322394223952239622397223982239922400224012240222403224042240522406224072240822409224102241122412224132241422415224162241722418224192242022421224222242322424224252242622427224282242922430224312243222433224342243522436224372243822439224402244122442224432244422445224462244722448224492245022451224522245322454224552245622457224582245922460224612246222463224642246522466224672246822469224702247122472224732247422475224762247722478224792248022481224822248322484224852248622487224882248922490224912249222493224942249522496224972249822499225002250122502225032250422505225062250722508225092251022511225122251322514225152251622517225182251922520225212252222523225242252522526225272252822529225302253122532225332253422535225362253722538225392254022541225422254322544225452254622547225482254922550225512255222553225542255522556225572255822559225602256122562225632256422565225662256722568225692257022571225722257322574225752257622577225782257922580225812258222583225842258522586225872258822589225902259122592225932259422595225962259722598225992260022601226022260322604226052260622607226082260922610226112261222613226142261522616226172261822619226202262122622226232262422625226262262722628226292263022631226322263322634226352263622637226382263922640226412264222643226442264522646226472264822649226502265122652226532265422655226562265722658226592266022661226622266322664226652266622667226682266922670226712267222673226742267522676226772267822679226802268122682226832268422685226862268722688226892269022691226922269322694226952269622697226982269922700227012270222703227042270522706227072270822709227102271122712227132271422715227162271722718227192272022721227222272322724227252272622727227282272922730227312273222733227342273522736227372273822739227402274122742227432274422745227462274722748227492275022751227522275322754227552275622757227582275922760227612276222763227642276522766227672276822769227702277122772227732277422775227762277722778227792278022781227822278322784227852278622787227882278922790227912279222793227942279522796227972279822799228002280122802228032280422805228062280722808228092281022811228122281322814228152281622817228182281922820228212282222823228242282522826228272282822829228302283122832228332283422835228362283722838228392284022841228422284322844228452284622847228482284922850228512285222853228542285522856228572285822859228602286122862228632286422865228662286722868228692287022871228722287322874228752287622877228782287922880228812288222883228842288522886228872288822889228902289122892228932289422895228962289722898228992290022901229022290322904229052290622907229082290922910229112291222913229142291522916229172291822919229202292122922229232292422925229262292722928229292293022931229322293322934229352293622937229382293922940229412294222943229442294522946229472294822949229502295122952229532295422955229562295722958229592296022961229622296322964229652296622967229682296922970229712297222973229742297522976229772297822979229802298122982229832298422985229862298722988229892299022991229922299322994229952299622997229982299923000230012300223003230042300523006230072300823009230102301123012230132301423015230162301723018230192302023021230222302323024230252302623027230282302923030230312303223033230342303523036230372303823039230402304123042230432304423045230462304723048230492305023051230522305323054230552305623057230582305923060230612306223063230642306523066230672306823069230702307123072230732307423075230762307723078230792308023081230822308323084230852308623087230882308923090230912309223093230942309523096230972309823099231002310123102231032310423105231062310723108231092311023111231122311323114231152311623117231182311923120231212312223123231242312523126231272312823129231302313123132231332313423135231362313723138231392314023141231422314323144231452314623147231482314923150231512315223153231542315523156231572315823159231602316123162231632316423165231662316723168231692317023171231722317323174231752317623177231782317923180231812318223183231842318523186231872318823189231902319123192231932319423195231962319723198231992320023201232022320323204232052320623207232082320923210232112321223213232142321523216232172321823219232202322123222232232322423225232262322723228232292323023231232322323323234232352323623237232382323923240232412324223243232442324523246232472324823249232502325123252232532325423255232562325723258232592326023261232622326323264232652326623267232682326923270232712327223273232742327523276232772327823279232802328123282232832328423285232862328723288232892329023291232922329323294232952329623297232982329923300233012330223303233042330523306233072330823309233102331123312233132331423315233162331723318233192332023321233222332323324233252332623327233282332923330233312333223333233342333523336233372333823339233402334123342233432334423345233462334723348233492335023351233522335323354233552335623357233582335923360233612336223363233642336523366233672336823369233702337123372233732337423375233762337723378233792338023381233822338323384233852338623387233882338923390233912339223393233942339523396233972339823399234002340123402234032340423405234062340723408234092341023411234122341323414234152341623417234182341923420234212342223423234242342523426234272342823429234302343123432234332343423435234362343723438234392344023441234422344323444234452344623447234482344923450234512345223453234542345523456234572345823459234602346123462234632346423465234662346723468234692347023471234722347323474234752347623477234782347923480234812348223483234842348523486234872348823489234902349123492234932349423495234962349723498234992350023501235022350323504235052350623507235082350923510235112351223513235142351523516235172351823519235202352123522235232352423525235262352723528235292353023531235322353323534235352353623537235382353923540235412354223543235442354523546235472354823549235502355123552
  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-table-set-constants "org-table" ())
  115. (declare-function org-id-get-create "org-id" (&optional force))
  116. (declare-function org-id-find-id-file "org-id" (id))
  117. (declare-function org-tags-view "org-agenda" (&optional todo-only match))
  118. (declare-function org-agenda-list "org-agenda" (&optional arg start-day span))
  119. (declare-function org-agenda-redo "org-agenda" (&optional all))
  120. (declare-function org-table-align "org-table" ())
  121. (declare-function org-table-paste-rectangle "org-table" ())
  122. (declare-function org-table-maybe-eval-formula "org-table" ())
  123. (declare-function org-table-maybe-recalculate-line "org-table" ())
  124. (declare-function org-element--parse-objects "org-element"
  125. (beg end acc restriction))
  126. (declare-function org-element-at-point "org-element" (&optional keep-trail))
  127. (declare-function org-element-contents "org-element" (element))
  128. (declare-function org-element-context "org-element" (&optional element))
  129. (declare-function org-element-interpret-data "org-element"
  130. (data &optional parent))
  131. (declare-function org-element-map "org-element"
  132. (data types fun &optional info first-match no-recursion))
  133. (declare-function org-element-nested-p "org-element" (elem-a elem-b))
  134. (declare-function org-element-parse-buffer "org-element"
  135. (&optional granularity visible-only))
  136. (declare-function org-element-property "org-element" (property element))
  137. (declare-function org-element-put-property "org-element"
  138. (element property value))
  139. (declare-function org-element-swap-A-B "org-element" (elem-a elem-b))
  140. (declare-function org-element--parse-objects "org-element"
  141. (beg end acc restriction))
  142. (declare-function org-element-parse-buffer "org-element"
  143. (&optional granularity visible-only))
  144. (declare-function org-element-restriction "org-element" (element))
  145. (declare-function org-element-type "org-element" (element))
  146. ;; load languages based on value of `org-babel-load-languages'
  147. (defvar org-babel-load-languages)
  148. ;;;###autoload
  149. (defun org-babel-do-load-languages (sym value)
  150. "Load the languages defined in `org-babel-load-languages'."
  151. (set-default sym value)
  152. (mapc (lambda (pair)
  153. (let ((active (cdr pair)) (lang (symbol-name (car pair))))
  154. (if active
  155. (progn
  156. (require (intern (concat "ob-" lang))))
  157. (progn
  158. (funcall 'fmakunbound
  159. (intern (concat "org-babel-execute:" lang)))
  160. (funcall 'fmakunbound
  161. (intern (concat "org-babel-expand-body:" lang)))))))
  162. org-babel-load-languages))
  163. ;;;###autoload
  164. (defun org-babel-load-file (file &optional compile)
  165. "Load Emacs Lisp source code blocks in the Org-mode FILE.
  166. This function exports the source code using `org-babel-tangle'
  167. and then loads the resulting file using `load-file'. With prefix
  168. arg (noninteractively: 2nd arg) COMPILE the tangled Emacs Lisp
  169. file to byte-code before it is loaded."
  170. (interactive "fFile to load: \nP")
  171. (require 'ob-core)
  172. (let* ((age (lambda (file)
  173. (float-time
  174. (time-subtract (current-time)
  175. (nth 5 (or (file-attributes (file-truename file))
  176. (file-attributes file)))))))
  177. (base-name (file-name-sans-extension file))
  178. (exported-file (concat base-name ".el")))
  179. ;; tangle if the org-mode file is newer than the elisp file
  180. (unless (and (file-exists-p exported-file)
  181. (> (funcall age file) (funcall age exported-file)))
  182. (org-babel-tangle-file file exported-file "emacs-lisp"))
  183. (message "%s %s"
  184. (if compile
  185. (progn (byte-compile-file exported-file 'load)
  186. "Compiled and loaded")
  187. (progn (load-file exported-file) "Loaded"))
  188. exported-file)))
  189. (defcustom org-babel-load-languages '((emacs-lisp . t))
  190. "Languages which can be evaluated in Org-mode buffers.
  191. This list can be used to load support for any of the languages
  192. below, note that each language will depend on a different set of
  193. system executables and/or Emacs modes. When a language is
  194. \"loaded\", then code blocks in that language can be evaluated
  195. with `org-babel-execute-src-block' bound by default to C-c
  196. C-c (note the `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can
  197. be set to remove code block evaluation from the C-c C-c
  198. keybinding. By default only Emacs Lisp (which has no
  199. requirements) is loaded."
  200. :group 'org-babel
  201. :set 'org-babel-do-load-languages
  202. :version "24.1"
  203. :type '(alist :tag "Babel Languages"
  204. :key-type
  205. (choice
  206. (const :tag "Awk" awk)
  207. (const :tag "C" C)
  208. (const :tag "R" R)
  209. (const :tag "Asymptote" asymptote)
  210. (const :tag "Calc" calc)
  211. (const :tag "Clojure" clojure)
  212. (const :tag "CSS" css)
  213. (const :tag "Ditaa" ditaa)
  214. (const :tag "Dot" dot)
  215. (const :tag "Emacs Lisp" emacs-lisp)
  216. (const :tag "Fortran" fortran)
  217. (const :tag "Gnuplot" gnuplot)
  218. (const :tag "Haskell" haskell)
  219. (const :tag "IO" io)
  220. (const :tag "Java" java)
  221. (const :tag "Javascript" js)
  222. (const :tag "LaTeX" latex)
  223. (const :tag "Ledger" ledger)
  224. (const :tag "Lilypond" lilypond)
  225. (const :tag "Lisp" lisp)
  226. (const :tag "Makefile" makefile)
  227. (const :tag "Maxima" maxima)
  228. (const :tag "Matlab" matlab)
  229. (const :tag "Mscgen" mscgen)
  230. (const :tag "Ocaml" ocaml)
  231. (const :tag "Octave" octave)
  232. (const :tag "Org" org)
  233. (const :tag "Perl" perl)
  234. (const :tag "Pico Lisp" picolisp)
  235. (const :tag "PlantUML" plantuml)
  236. (const :tag "Python" python)
  237. (const :tag "Ruby" ruby)
  238. (const :tag "Sass" sass)
  239. (const :tag "Scala" scala)
  240. (const :tag "Scheme" scheme)
  241. (const :tag "Screen" screen)
  242. (const :tag "Shell Script" sh)
  243. (const :tag "Shen" shen)
  244. (const :tag "Sql" sql)
  245. (const :tag "Sqlite" sqlite))
  246. :value-type (boolean :tag "Activate" :value t)))
  247. ;;;; Customization variables
  248. (defcustom org-clone-delete-id nil
  249. "Remove ID property of clones of a subtree.
  250. When non-nil, clones of a subtree don't inherit the ID property.
  251. Otherwise they inherit the ID property with a new unique
  252. identifier."
  253. :type 'boolean
  254. :version "24.1"
  255. :group 'org-id)
  256. ;;; Version
  257. (org-check-version)
  258. ;;;###autoload
  259. (defun org-version (&optional here full message)
  260. "Show the org-mode version in the echo area.
  261. With prefix argument HERE, insert it at point.
  262. When FULL is non-nil, use a verbose version string.
  263. When MESSAGE is non-nil, display a message with the version."
  264. (interactive "P")
  265. (let* ((org-dir (ignore-errors (org-find-library-dir "org")))
  266. (save-load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  267. (load-suffixes (list ".el"))
  268. (org-install-dir (ignore-errors (org-find-library-dir "org-loaddefs")))
  269. (org-trash (or
  270. (and (fboundp 'org-release) (fboundp 'org-git-version))
  271. (org-load-noerror-mustsuffix (concat org-dir "org-version"))))
  272. (load-suffixes save-load-suffixes)
  273. (org-version (org-release))
  274. (git-version (org-git-version))
  275. (version (format "Org-mode version %s (%s @ %s)"
  276. org-version
  277. git-version
  278. (if org-install-dir
  279. (if (string= org-dir org-install-dir)
  280. org-install-dir
  281. (concat "mixed installation! " org-install-dir " and " org-dir))
  282. "org-loaddefs.el can not be found!")))
  283. (_version (if full version org-version)))
  284. (if (org-called-interactively-p 'interactive)
  285. (if here
  286. (insert version)
  287. (message version))
  288. (if message (message _version))
  289. _version)))
  290. (defconst org-version (org-version))
  291. ;;; Compatibility constants
  292. ;;; The custom variables
  293. (defgroup org nil
  294. "Outline-based notes management and organizer."
  295. :tag "Org"
  296. :group 'outlines
  297. :group 'calendar)
  298. (defcustom org-mode-hook nil
  299. "Mode hook for Org-mode, run after the mode was turned on."
  300. :group 'org
  301. :type 'hook)
  302. (defcustom org-load-hook nil
  303. "Hook that is run after org.el has been loaded."
  304. :group 'org
  305. :type 'hook)
  306. (defcustom org-log-buffer-setup-hook nil
  307. "Hook that is run after an Org log buffer is created."
  308. :group 'org
  309. :version "24.1"
  310. :type 'hook)
  311. (defvar org-modules) ; defined below
  312. (defvar org-modules-loaded nil
  313. "Have the modules been loaded already?")
  314. (defun org-load-modules-maybe (&optional force)
  315. "Load all extensions listed in `org-modules'."
  316. (when (or force (not org-modules-loaded))
  317. (mapc (lambda (ext)
  318. (condition-case nil (require ext)
  319. (error (message "Problems while trying to load feature `%s'" ext))))
  320. org-modules)
  321. (setq org-modules-loaded t)))
  322. (defun org-set-modules (var value)
  323. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  324. (set var value)
  325. (when (featurep 'org)
  326. (org-load-modules-maybe 'force)))
  327. (defcustom org-modules '(org-w3m org-bbdb org-bibtex org-docview org-gnus org-info org-irc org-mhe org-rmail)
  328. "Modules that should always be loaded together with org.el.
  329. If a description starts with <C>, the file is not part of Emacs
  330. and loading it will require that you have downloaded and properly
  331. installed the Org mode distribution.
  332. You can also use this system to load external packages (i.e. neither Org
  333. core modules, nor modules from the CONTRIB directory). Just add symbols
  334. to the end of the list. If the package is called org-xyz.el, then you need
  335. to add the symbol `xyz', and the package must have a call to:
  336. \(provide 'org-xyz)
  337. For export specific modules, see also `org-export-backends'."
  338. :group 'org
  339. :set 'org-set-modules
  340. :version "24.4"
  341. :package-version '(Org . "8.0")
  342. :type
  343. '(set :greedy t
  344. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  345. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  346. (const :tag " crypt: Encryption of subtrees" org-crypt)
  347. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  348. (const :tag " docview: Links to doc-view buffers" org-docview)
  349. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  350. (const :tag " habit: Track your consistency with habits" org-habit)
  351. (const :tag " id: Global IDs for identifying entries" org-id)
  352. (const :tag " info: Links to Info nodes" org-info)
  353. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  354. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  355. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  356. (const :tag " mouse: Additional mouse support" org-mouse)
  357. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  358. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  359. (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  360. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  361. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  362. (const :tag "C bullets: Add overlays to headlines stars" org-bullets)
  363. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  364. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  365. (const :tag "C collector: Collect properties into tables" org-collector)
  366. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  367. (const :tag "C drill: Flashcards and spaced repetition for Org-mode" org-drill)
  368. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  369. (const :tag "C eshell Support for links to working directories in eshell" org-eshell)
  370. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  371. (const :tag "C eval: Include command output as text" org-eval)
  372. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  373. (const :tag "C favtable: Lookup table of favorite references and links" org-favtable)
  374. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  375. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  376. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  377. (const :tag "C jira: Add a jira:ticket protocol to Org-mode" org-jira)
  378. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  379. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  380. (const :tag "C mac-link-grabber Grab links and URLs from various Mac applications" org-mac-link-grabber)
  381. (const :tag "C mac-message: Links to messages in Apple Mail" org-mac-message)
  382. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  383. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  384. (const :tag "C mew: Links to Mew folders/messages" org-mew)
  385. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  386. (const :tag "C notmuch: Provide org links to notmuch searches or messages" org-notmuch)
  387. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  388. (const :tag "C registry: A registry for Org-mode links" org-registry)
  389. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  390. (const :tag "C secretary: Team management with org-mode" org-secretary)
  391. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  392. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  393. (const :tag "C track: Keep up with Org-mode development" org-track)
  394. (const :tag "C velocity Something like Notational Velocity for Org" org-velocity)
  395. (const :tag "C vm: Links to VM folders/messages" org-vm)
  396. (const :tag "C wikinodes: CamelCase wiki-like links" org-wikinodes)
  397. (const :tag "C wl: Links to Wanderlust folders/messages" org-wl)
  398. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  399. (defvar org-export-registered-backends) ; From ox.el
  400. (declare-function org-export-derived-backend-p "ox" (backend &rest backends))
  401. (defcustom org-export-backends '(ascii html icalendar latex)
  402. "List of export back-ends that should be always available.
  403. If a description starts with <C>, the file is not part of Emacs
  404. and loading it will require that you have downloaded and properly
  405. installed the Org mode distribution.
  406. Unlike to `org-modules', libraries in this list will not be
  407. loaded along with Org, but only once the export framework is
  408. needed.
  409. This variable needs to be set before org.el is loaded. If you
  410. need to make a change while Emacs is running, use the customize
  411. interface or run the following code, where VALUE stands for the
  412. new value of the variable, after updating it:
  413. \(progn
  414. \(setq org-export-registered-backends
  415. \(org-remove-if-not
  416. \(lambda (backend)
  417. \(or (memq backend val)
  418. \(catch 'parentp
  419. \(mapc
  420. \(lambda (b)
  421. \(and (org-export-derived-backend-p b (car backend))
  422. \(throw 'parentp t)))
  423. val)
  424. nil)))
  425. org-export-registered-backends))
  426. \(let ((new-list (mapcar 'car org-export-registered-backends)))
  427. \(dolist (backend val)
  428. \(cond
  429. \((not (load (format \"ox-%s\" backend) t t))
  430. \(message \"Problems while trying to load export back-end `%s'\"
  431. backend))
  432. \((not (memq backend new-list)) (push backend new-list))))
  433. \(set-default var new-list)))
  434. Adding a back-end to this list will also pull the back-end it
  435. depends on, if any."
  436. :group 'org
  437. :group 'org-export
  438. :version "24.4"
  439. :package-version '(Org . "8.0")
  440. :initialize 'custom-initialize-set
  441. :set (lambda (var val)
  442. (if (not (featurep 'ox)) (set-default var val)
  443. ;; Any back-end not required anymore (not present in VAL and not
  444. ;; a parent of any back-end in the new value) is removed from the
  445. ;; list of registered back-ends.
  446. (setq org-export-registered-backends
  447. (org-remove-if-not
  448. (lambda (backend)
  449. (or (memq backend val)
  450. (catch 'parentp
  451. (mapc
  452. (lambda (b)
  453. (and (org-export-derived-backend-p b (car backend))
  454. (throw 'parentp t)))
  455. val)
  456. nil)))
  457. org-export-registered-backends))
  458. ;; Now build NEW-LIST of both new back-ends and required
  459. ;; parents.
  460. (let ((new-list (mapcar 'car org-export-registered-backends)))
  461. (dolist (backend val)
  462. (cond
  463. ((not (load (format "ox-%s" backend) t t))
  464. (message "Problems while trying to load export back-end `%s'"
  465. backend))
  466. ((not (memq backend new-list)) (push backend new-list))))
  467. ;; Set VAR to that list with fixed dependencies.
  468. (set-default var new-list))))
  469. :type '(set :greedy t
  470. (const :tag " ascii Export buffer to ASCII format" ascii)
  471. (const :tag " beamer Export buffer to Beamer presentation" beamer)
  472. (const :tag " html Export buffer to HTML format" html)
  473. (const :tag " icalendar Export buffer to iCalendar format" icalendar)
  474. (const :tag " latex Export buffer to LaTeX format" latex)
  475. (const :tag " man Export buffer to MAN format" man)
  476. (const :tag " md Export buffer to Markdown format" md)
  477. (const :tag " odt Export buffer to ODT format" odt)
  478. (const :tag " org Export buffer to Org format" org)
  479. (const :tag " texinfo Export buffer to Texinfo format" texinfo)
  480. (const :tag "C confluence Export buffer to Confluence Wiki format" confluence)
  481. (const :tag "C deck Export buffer to deck.js presentations" deck)
  482. (const :tag "C freemind Export buffer to Freemind mindmap format" freemind)
  483. (const :tag "C groff Export buffer to Groff format" groff)
  484. (const :tag "C koma-letter Export buffer to KOMA Scrlttrl2 format" koma-letter)
  485. (const :tag "C RSS 2.0 Export buffer to RSS 2.0 format" rss)
  486. (const :tag "C s5 Export buffer to s5 presentations" s5)
  487. (const :tag "C taskjuggler Export buffer to TaskJuggler format" taskjuggler)))
  488. (eval-after-load 'ox
  489. '(mapc
  490. (lambda (backend)
  491. (condition-case nil (require (intern (format "ox-%s" backend)))
  492. (error (message "Problems while trying to load export back-end `%s'"
  493. backend))))
  494. org-export-backends))
  495. (defcustom org-support-shift-select nil
  496. "Non-nil means make shift-cursor commands select text when possible.
  497. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
  498. start selecting a region, or enlarge regions started in this way.
  499. In Org-mode, in special contexts, these same keys are used for
  500. other purposes, important enough to compete with shift selection.
  501. Org tries to balance these needs by supporting `shift-select-mode'
  502. outside these special contexts, under control of this variable.
  503. The default of this variable is nil, to avoid confusing behavior. Shifted
  504. cursor keys will then execute Org commands in the following contexts:
  505. - on a headline, changing TODO state (left/right) and priority (up/down)
  506. - on a time stamp, changing the time
  507. - in a plain list item, changing the bullet type
  508. - in a property definition line, switching between allowed values
  509. - in the BEGIN line of a clock table (changing the time block).
  510. Outside these contexts, the commands will throw an error.
  511. When this variable is t and the cursor is not in a special
  512. context, Org-mode will support shift-selection for making and
  513. enlarging regions. To make this more effective, the bullet
  514. cycling will no longer happen anywhere in an item line, but only
  515. if the cursor is exactly on the bullet.
  516. If you set this variable to the symbol `always', then the keys
  517. will not be special in headlines, property lines, and item lines,
  518. to make shift selection work there as well. If this is what you
  519. want, you can use the following alternative commands: `C-c C-t'
  520. and `C-c ,' to change TODO state and priority, `C-u C-u C-c C-t'
  521. can be used to switch TODO sets, `C-c -' to cycle item bullet
  522. types, and properties can be edited by hand or in column view.
  523. However, when the cursor is on a timestamp, shift-cursor commands
  524. will still edit the time stamp - this is just too good to give up.
  525. XEmacs user should have this variable set to nil, because
  526. `shift-select-mode' is in Emacs 23 or later only."
  527. :group 'org
  528. :type '(choice
  529. (const :tag "Never" nil)
  530. (const :tag "When outside special context" t)
  531. (const :tag "Everywhere except timestamps" always)))
  532. (defcustom org-loop-over-headlines-in-active-region nil
  533. "Shall some commands act upon headlines in the active region?
  534. When set to `t', some commands will be performed in all headlines
  535. within the active region.
  536. When set to `start-level', some commands will be performed in all
  537. headlines within the active region, provided that these headlines
  538. are of the same level than the first one.
  539. When set to a string, those commands will be performed on the
  540. matching headlines within the active region. Such string must be
  541. a tags/property/todo match as it is used in the agenda tags view.
  542. The list of commands is: `org-schedule', `org-deadline',
  543. `org-todo', `org-archive-subtree', `org-archive-set-tag' and
  544. `org-archive-to-archive-sibling'. The archiving commands skip
  545. already archived entries."
  546. :type '(choice (const :tag "Don't loop" nil)
  547. (const :tag "All headlines in active region" t)
  548. (const :tag "In active region, headlines at the same level than the first one" 'start-level)
  549. (string :tag "Tags/Property/Todo matcher"))
  550. :version "24.1"
  551. :group 'org-todo
  552. :group 'org-archive)
  553. (defgroup org-startup nil
  554. "Options concerning startup of Org-mode."
  555. :tag "Org Startup"
  556. :group 'org)
  557. (defcustom org-startup-folded t
  558. "Non-nil means entering Org-mode will switch to OVERVIEW.
  559. This can also be configured on a per-file basis by adding one of
  560. the following lines anywhere in the buffer:
  561. #+STARTUP: fold (or `overview', this is equivalent)
  562. #+STARTUP: nofold (or `showall', this is equivalent)
  563. #+STARTUP: content
  564. #+STARTUP: showeverything
  565. By default, this option is ignored when Org opens agenda files
  566. for the first time. If you want the agenda to honor the startup
  567. option, set `org-agenda-inhibit-startup' to nil."
  568. :group 'org-startup
  569. :type '(choice
  570. (const :tag "nofold: show all" nil)
  571. (const :tag "fold: overview" t)
  572. (const :tag "content: all headlines" content)
  573. (const :tag "show everything, even drawers" showeverything)))
  574. (defcustom org-startup-truncated t
  575. "Non-nil means entering Org-mode will set `truncate-lines'.
  576. This is useful since some lines containing links can be very long and
  577. uninteresting. Also tables look terrible when wrapped."
  578. :group 'org-startup
  579. :type 'boolean)
  580. (defcustom org-startup-indented nil
  581. "Non-nil means turn on `org-indent-mode' on startup.
  582. This can also be configured on a per-file basis by adding one of
  583. the following lines anywhere in the buffer:
  584. #+STARTUP: indent
  585. #+STARTUP: noindent"
  586. :group 'org-structure
  587. :type '(choice
  588. (const :tag "Not" nil)
  589. (const :tag "Globally (slow on startup in large files)" t)))
  590. (defcustom org-use-sub-superscripts t
  591. "Non-nil means interpret \"_\" and \"^\" for display.
  592. When this option is turned on, you can use TeX-like syntax for sub- and
  593. superscripts. Several characters after \"_\" or \"^\" will be
  594. considered as a single item - so grouping with {} is normally not
  595. needed. For example, the following things will be parsed as single
  596. sub- or superscripts.
  597. 10^24 or 10^tau several digits will be considered 1 item.
  598. 10^-12 or 10^-tau a leading sign with digits or a word
  599. x^2-y^3 will be read as x^2 - y^3, because items are
  600. terminated by almost any nonword/nondigit char.
  601. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  602. Still, ambiguity is possible - so when in doubt use {} to enclose
  603. the sub/superscript. If you set this variable to the symbol
  604. `{}', the braces are *required* in order to trigger
  605. interpretations as sub/superscript. This can be helpful in
  606. documents that need \"_\" frequently in plain text."
  607. :group 'org-startup
  608. :version "24.1"
  609. :type '(choice
  610. (const :tag "Always interpret" t)
  611. (const :tag "Only with braces" {})
  612. (const :tag "Never interpret" nil)))
  613. (defcustom org-startup-with-beamer-mode nil
  614. "Non-nil means turn on `org-beamer-mode' on startup.
  615. This can also be configured on a per-file basis by adding one of
  616. the following lines anywhere in the buffer:
  617. #+STARTUP: beamer"
  618. :group 'org-startup
  619. :version "24.1"
  620. :type 'boolean)
  621. (defcustom org-startup-align-all-tables nil
  622. "Non-nil means align all tables when visiting a file.
  623. This is useful when the column width in tables is forced with <N> cookies
  624. in table fields. Such tables will look correct only after the first re-align.
  625. This can also be configured on a per-file basis by adding one of
  626. the following lines anywhere in the buffer:
  627. #+STARTUP: align
  628. #+STARTUP: noalign"
  629. :group 'org-startup
  630. :type 'boolean)
  631. (defcustom org-startup-with-inline-images nil
  632. "Non-nil means show inline images when loading a new Org file.
  633. This can also be configured on a per-file basis by adding one of
  634. the following lines anywhere in the buffer:
  635. #+STARTUP: inlineimages
  636. #+STARTUP: noinlineimages"
  637. :group 'org-startup
  638. :version "24.1"
  639. :type 'boolean)
  640. (defcustom org-startup-with-latex-preview nil
  641. "Non-nil means preview LaTeX fragments when loading a new Org file.
  642. This can also be configured on a per-file basis by adding one of
  643. the followinglines anywhere in the buffer:
  644. #+STARTUP: latexpreview
  645. #+STARTUP: nolatexpreview"
  646. :group 'org-startup
  647. :version "24.4"
  648. :package-version '(Org . "8.0")
  649. :type 'boolean)
  650. (defcustom org-insert-mode-line-in-empty-file nil
  651. "Non-nil means insert the first line setting Org-mode in empty files.
  652. When the function `org-mode' is called interactively in an empty file, this
  653. normally means that the file name does not automatically trigger Org-mode.
  654. To ensure that the file will always be in Org-mode in the future, a
  655. line enforcing Org-mode will be inserted into the buffer, if this option
  656. has been set."
  657. :group 'org-startup
  658. :type 'boolean)
  659. (defcustom org-replace-disputed-keys nil
  660. "Non-nil means use alternative key bindings for some keys.
  661. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  662. These keys are also used by other packages like shift-selection-mode'
  663. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  664. If you want to use Org-mode together with one of these other modes,
  665. or more generally if you would like to move some Org-mode commands to
  666. other keys, set this variable and configure the keys with the variable
  667. `org-disputed-keys'.
  668. This option is only relevant at load-time of Org-mode, and must be set
  669. *before* org.el is loaded. Changing it requires a restart of Emacs to
  670. become effective."
  671. :group 'org-startup
  672. :type 'boolean)
  673. (defcustom org-use-extra-keys nil
  674. "Non-nil means use extra key sequence definitions for certain commands.
  675. This happens automatically if you run XEmacs or if `window-system'
  676. is nil. This variable lets you do the same manually. You must
  677. set it before loading org.
  678. Example: on Carbon Emacs 22 running graphically, with an external
  679. keyboard on a Powerbook, the default way of setting M-left might
  680. not work for either Alt or ESC. Setting this variable will make
  681. it work for ESC."
  682. :group 'org-startup
  683. :type 'boolean)
  684. (org-defvaralias 'org-CUA-compatible 'org-replace-disputed-keys)
  685. (defcustom org-disputed-keys
  686. '(([(shift up)] . [(meta p)])
  687. ([(shift down)] . [(meta n)])
  688. ([(shift left)] . [(meta -)])
  689. ([(shift right)] . [(meta +)])
  690. ([(control shift right)] . [(meta shift +)])
  691. ([(control shift left)] . [(meta shift -)]))
  692. "Keys for which Org-mode and other modes compete.
  693. This is an alist, cars are the default keys, second element specifies
  694. the alternative to use when `org-replace-disputed-keys' is t.
  695. Keys can be specified in any syntax supported by `define-key'.
  696. The value of this option takes effect only at Org-mode's startup,
  697. therefore you'll have to restart Emacs to apply it after changing."
  698. :group 'org-startup
  699. :type 'alist)
  700. (defun org-key (key)
  701. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  702. Or return the original if not disputed.
  703. Also apply the translations defined in `org-xemacs-key-equivalents'."
  704. (when org-replace-disputed-keys
  705. (let* ((nkey (key-description key))
  706. (x (org-find-if (lambda (x)
  707. (equal (key-description (car x)) nkey))
  708. org-disputed-keys)))
  709. (setq key (if x (cdr x) key))))
  710. (when (featurep 'xemacs)
  711. (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
  712. key)
  713. (defun org-find-if (predicate seq)
  714. (catch 'exit
  715. (while seq
  716. (if (funcall predicate (car seq))
  717. (throw 'exit (car seq))
  718. (pop seq)))))
  719. (defun org-defkey (keymap key def)
  720. "Define a key, possibly translated, as returned by `org-key'."
  721. (define-key keymap (org-key key) def))
  722. (defcustom org-ellipsis nil
  723. "The ellipsis to use in the Org-mode outline.
  724. When nil, just use the standard three dots. When a string, use that instead,
  725. When a face, use the standard 3 dots, but with the specified face.
  726. The change affects only Org-mode (which will then use its own display table).
  727. Changing this requires executing `M-x org-mode' in a buffer to become
  728. effective."
  729. :group 'org-startup
  730. :type '(choice (const :tag "Default" nil)
  731. (face :tag "Face" :value org-warning)
  732. (string :tag "String" :value "...#")))
  733. (defvar org-display-table nil
  734. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  735. (defgroup org-keywords nil
  736. "Keywords in Org-mode."
  737. :tag "Org Keywords"
  738. :group 'org)
  739. (defcustom org-deadline-string "DEADLINE:"
  740. "String to mark deadline entries.
  741. A deadline is this string, followed by a time stamp. Should be a word,
  742. terminated by a colon. You can insert a schedule keyword and
  743. a timestamp with \\[org-deadline].
  744. Changes become only effective after restarting Emacs."
  745. :group 'org-keywords
  746. :type 'string)
  747. (defcustom org-scheduled-string "SCHEDULED:"
  748. "String to mark scheduled TODO entries.
  749. A schedule is this string, followed by a time stamp. Should be a word,
  750. terminated by a colon. You can insert a schedule keyword and
  751. a timestamp with \\[org-schedule].
  752. Changes become only effective after restarting Emacs."
  753. :group 'org-keywords
  754. :type 'string)
  755. (defcustom org-closed-string "CLOSED:"
  756. "String used as the prefix for timestamps logging closing a TODO entry."
  757. :group 'org-keywords
  758. :type 'string)
  759. (defcustom org-clock-string "CLOCK:"
  760. "String used as prefix for timestamps clocking work hours on an item."
  761. :group 'org-keywords
  762. :type 'string)
  763. (defconst org-planning-or-clock-line-re (concat "^[ \t]*\\("
  764. org-scheduled-string "\\|"
  765. org-deadline-string "\\|"
  766. org-closed-string "\\|"
  767. org-clock-string "\\)")
  768. "Matches a line with planning or clock info.")
  769. (defcustom org-comment-string "COMMENT"
  770. "Entries starting with this keyword will never be exported.
  771. An entry can be toggled between COMMENT and normal with
  772. \\[org-toggle-comment].
  773. Changes become only effective after restarting Emacs."
  774. :group 'org-keywords
  775. :type 'string)
  776. (defcustom org-quote-string "QUOTE"
  777. "Entries starting with this keyword will be exported in fixed-width font.
  778. Quoting applies only to the text in the entry following the headline, and does
  779. not extend beyond the next headline, even if that is lower level.
  780. An entry can be toggled between QUOTE and normal with
  781. \\[org-toggle-fixed-width-section]."
  782. :group 'org-keywords
  783. :type 'string)
  784. (defconst org-repeat-re
  785. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)"
  786. "Regular expression for specifying repeated events.
  787. After a match, group 1 contains the repeat expression.")
  788. (defgroup org-structure nil
  789. "Options concerning the general structure of Org-mode files."
  790. :tag "Org Structure"
  791. :group 'org)
  792. (defgroup org-reveal-location nil
  793. "Options about how to make context of a location visible."
  794. :tag "Org Reveal Location"
  795. :group 'org-structure)
  796. (defconst org-context-choice
  797. '(choice
  798. (const :tag "Always" t)
  799. (const :tag "Never" nil)
  800. (repeat :greedy t :tag "Individual contexts"
  801. (cons
  802. (choice :tag "Context"
  803. (const agenda)
  804. (const org-goto)
  805. (const occur-tree)
  806. (const tags-tree)
  807. (const link-search)
  808. (const mark-goto)
  809. (const bookmark-jump)
  810. (const isearch)
  811. (const default))
  812. (boolean))))
  813. "Contexts for the reveal options.")
  814. (defcustom org-show-hierarchy-above '((default . t))
  815. "Non-nil means show full hierarchy when revealing a location.
  816. Org-mode often shows locations in an org-mode file which might have
  817. been invisible before. When this is set, the hierarchy of headings
  818. above the exposed location is shown.
  819. Turning this off for example for sparse trees makes them very compact.
  820. Instead of t, this can also be an alist specifying this option for different
  821. contexts. Valid contexts are
  822. agenda when exposing an entry from the agenda
  823. org-goto when using the command `org-goto' on key C-c C-j
  824. occur-tree when using the command `org-occur' on key C-c /
  825. tags-tree when constructing a sparse tree based on tags matches
  826. link-search when exposing search matches associated with a link
  827. mark-goto when exposing the jump goal of a mark
  828. bookmark-jump when exposing a bookmark location
  829. isearch when exiting from an incremental search
  830. default default for all contexts not set explicitly"
  831. :group 'org-reveal-location
  832. :type org-context-choice)
  833. (defcustom org-show-following-heading '((default . nil))
  834. "Non-nil means show following heading when revealing a location.
  835. Org-mode often shows locations in an org-mode file which might have
  836. been invisible before. When this is set, the heading following the
  837. match is shown.
  838. Turning this off for example for sparse trees makes them very compact,
  839. but makes it harder to edit the location of the match. In such a case,
  840. use the command \\[org-reveal] to show more context.
  841. Instead of t, this can also be an alist specifying this option for different
  842. contexts. See `org-show-hierarchy-above' for valid contexts."
  843. :group 'org-reveal-location
  844. :type org-context-choice)
  845. (defcustom org-show-siblings '((default . nil) (isearch t) (bookmark-jump t))
  846. "Non-nil means show all sibling heading when revealing a location.
  847. Org-mode often shows locations in an org-mode file which might have
  848. been invisible before. When this is set, the sibling of the current entry
  849. heading are all made visible. If `org-show-hierarchy-above' is t,
  850. the same happens on each level of the hierarchy above the current entry.
  851. By default this is on for the isearch context, off for all other contexts.
  852. Turning this off for example for sparse trees makes them very compact,
  853. but makes it harder to edit the location of the match. In such a case,
  854. use the command \\[org-reveal] to show more context.
  855. Instead of t, this can also be an alist specifying this option for different
  856. contexts. See `org-show-hierarchy-above' for valid contexts."
  857. :group 'org-reveal-location
  858. :type org-context-choice
  859. :version "24.4"
  860. :package-version '(Org . "8.0"))
  861. (defcustom org-show-entry-below '((default . nil))
  862. "Non-nil means show the entry below a headline when revealing a location.
  863. Org-mode often shows locations in an org-mode file which might have
  864. been invisible before. When this is set, the text below the headline that is
  865. exposed is also shown.
  866. By default this is off for all contexts.
  867. Instead of t, this can also be an alist specifying this option for different
  868. contexts. See `org-show-hierarchy-above' for valid contexts."
  869. :group 'org-reveal-location
  870. :type org-context-choice)
  871. (defcustom org-indirect-buffer-display 'other-window
  872. "How should indirect tree buffers be displayed?
  873. This applies to indirect buffers created with the commands
  874. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  875. Valid values are:
  876. current-window Display in the current window
  877. other-window Just display in another window.
  878. dedicated-frame Create one new frame, and re-use it each time.
  879. new-frame Make a new frame each time. Note that in this case
  880. previously-made indirect buffers are kept, and you need to
  881. kill these buffers yourself."
  882. :group 'org-structure
  883. :group 'org-agenda-windows
  884. :type '(choice
  885. (const :tag "In current window" current-window)
  886. (const :tag "In current frame, other window" other-window)
  887. (const :tag "Each time a new frame" new-frame)
  888. (const :tag "One dedicated frame" dedicated-frame)))
  889. (defcustom org-use-speed-commands nil
  890. "Non-nil means activate single letter commands at beginning of a headline.
  891. This may also be a function to test for appropriate locations where speed
  892. commands should be active."
  893. :group 'org-structure
  894. :type '(choice
  895. (const :tag "Never" nil)
  896. (const :tag "At beginning of headline stars" t)
  897. (function)))
  898. (defcustom org-speed-commands-user nil
  899. "Alist of additional speed commands.
  900. This list will be checked before `org-speed-commands-default'
  901. when the variable `org-use-speed-commands' is non-nil
  902. and when the cursor is at the beginning of a headline.
  903. The car if each entry is a string with a single letter, which must
  904. be assigned to `self-insert-command' in the global map.
  905. The cdr is either a command to be called interactively, a function
  906. to be called, or a form to be evaluated.
  907. An entry that is just a list with a single string will be interpreted
  908. as a descriptive headline that will be added when listing the speed
  909. commands in the Help buffer using the `?' speed command."
  910. :group 'org-structure
  911. :type '(repeat :value ("k" . ignore)
  912. (choice :value ("k" . ignore)
  913. (list :tag "Descriptive Headline" (string :tag "Headline"))
  914. (cons :tag "Letter and Command"
  915. (string :tag "Command letter")
  916. (choice
  917. (function)
  918. (sexp))))))
  919. (defgroup org-cycle nil
  920. "Options concerning visibility cycling in Org-mode."
  921. :tag "Org Cycle"
  922. :group 'org-structure)
  923. (defcustom org-cycle-skip-children-state-if-no-children t
  924. "Non-nil means skip CHILDREN state in entries that don't have any."
  925. :group 'org-cycle
  926. :type 'boolean)
  927. (defcustom org-cycle-max-level nil
  928. "Maximum level which should still be subject to visibility cycling.
  929. Levels higher than this will, for cycling, be treated as text, not a headline.
  930. When `org-odd-levels-only' is set, a value of N in this variable actually
  931. means 2N-1 stars as the limiting headline.
  932. When nil, cycle all levels.
  933. Note that the limiting level of cycling is also influenced by
  934. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  935. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  936. than its value."
  937. :group 'org-cycle
  938. :type '(choice
  939. (const :tag "No limit" nil)
  940. (integer :tag "Maximum level")))
  941. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK" "RESULTS")
  942. "Names of drawers. Drawers are not opened by cycling on the headline above.
  943. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  944. this:
  945. :DRAWERNAME:
  946. .....
  947. :END:
  948. The drawer \"PROPERTIES\" is special for capturing properties through
  949. the property API.
  950. Drawers can be defined on the per-file basis with a line like:
  951. #+DRAWERS: HIDDEN STATE PROPERTIES"
  952. :group 'org-structure
  953. :group 'org-cycle
  954. :type '(repeat (string :tag "Drawer Name")))
  955. (defcustom org-hide-block-startup nil
  956. "Non-nil means entering Org-mode will fold all blocks.
  957. This can also be set in on a per-file basis with
  958. #+STARTUP: hideblocks
  959. #+STARTUP: showblocks"
  960. :group 'org-startup
  961. :group 'org-cycle
  962. :type 'boolean)
  963. (defcustom org-cycle-global-at-bob nil
  964. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  965. This makes it possible to do global cycling without having to use S-TAB or
  966. \\[universal-argument] TAB. For this special case to work, the first line
  967. of the buffer must not be a headline -- it may be empty or some other text.
  968. When used in this way, `org-cycle-hook' is disabled temporarily to make
  969. sure the cursor stays at the beginning of the buffer. When this option is
  970. nil, don't do anything special at the beginning of the buffer."
  971. :group 'org-cycle
  972. :type 'boolean)
  973. (defcustom org-cycle-level-after-item/entry-creation t
  974. "Non-nil means cycle entry level or item indentation in new empty entries.
  975. When the cursor is at the end of an empty headline, i.e., with only stars
  976. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  977. and then all possible ancestor states, before returning to the original state.
  978. This makes data entry extremely fast: M-RET to create a new headline,
  979. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  980. When the cursor is at the end of an empty plain list item, one TAB will
  981. make it a subitem, two or more tabs will back up to make this an item
  982. higher up in the item hierarchy."
  983. :group 'org-cycle
  984. :type 'boolean)
  985. (defcustom org-cycle-emulate-tab t
  986. "Where should `org-cycle' emulate TAB.
  987. nil Never
  988. white Only in completely white lines
  989. whitestart Only at the beginning of lines, before the first non-white char
  990. t Everywhere except in headlines
  991. exc-hl-bol Everywhere except at the start of a headline
  992. If TAB is used in a place where it does not emulate TAB, the current subtree
  993. visibility is cycled."
  994. :group 'org-cycle
  995. :type '(choice (const :tag "Never" nil)
  996. (const :tag "Only in completely white lines" white)
  997. (const :tag "Before first char in a line" whitestart)
  998. (const :tag "Everywhere except in headlines" t)
  999. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  1000. ))
  1001. (defcustom org-cycle-separator-lines 2
  1002. "Number of empty lines needed to keep an empty line between collapsed trees.
  1003. If you leave an empty line between the end of a subtree and the following
  1004. headline, this empty line is hidden when the subtree is folded.
  1005. Org-mode will leave (exactly) one empty line visible if the number of
  1006. empty lines is equal or larger to the number given in this variable.
  1007. So the default 2 means at least 2 empty lines after the end of a subtree
  1008. are needed to produce free space between a collapsed subtree and the
  1009. following headline.
  1010. If the number is negative, and the number of empty lines is at least -N,
  1011. all empty lines are shown.
  1012. Special case: when 0, never leave empty lines in collapsed view."
  1013. :group 'org-cycle
  1014. :type 'integer)
  1015. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  1016. (defcustom org-pre-cycle-hook nil
  1017. "Hook that is run before visibility cycling is happening.
  1018. The function(s) in this hook must accept a single argument which indicates
  1019. the new state that will be set right after running this hook. The
  1020. argument is a symbol. Before a global state change, it can have the values
  1021. `overview', `content', or `all'. Before a local state change, it can have
  1022. the values `folded', `children', or `subtree'."
  1023. :group 'org-cycle
  1024. :type 'hook)
  1025. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  1026. org-cycle-hide-drawers
  1027. org-cycle-hide-inline-tasks
  1028. org-cycle-show-empty-lines
  1029. org-optimize-window-after-visibility-change)
  1030. "Hook that is run after `org-cycle' has changed the buffer visibility.
  1031. The function(s) in this hook must accept a single argument which indicates
  1032. the new state that was set by the most recent `org-cycle' command. The
  1033. argument is a symbol. After a global state change, it can have the values
  1034. `overview', `contents', or `all'. After a local state change, it can have
  1035. the values `folded', `children', or `subtree'."
  1036. :group 'org-cycle
  1037. :type 'hook)
  1038. (defgroup org-edit-structure nil
  1039. "Options concerning structure editing in Org-mode."
  1040. :tag "Org Edit Structure"
  1041. :group 'org-structure)
  1042. (defcustom org-odd-levels-only nil
  1043. "Non-nil means skip even levels and only use odd levels for the outline.
  1044. This has the effect that two stars are being added/taken away in
  1045. promotion/demotion commands. It also influences how levels are
  1046. handled by the exporters.
  1047. Changing it requires restart of `font-lock-mode' to become effective
  1048. for fontification also in regions already fontified.
  1049. You may also set this on a per-file basis by adding one of the following
  1050. lines to the buffer:
  1051. #+STARTUP: odd
  1052. #+STARTUP: oddeven"
  1053. :group 'org-edit-structure
  1054. :group 'org-appearance
  1055. :type 'boolean)
  1056. (defcustom org-adapt-indentation t
  1057. "Non-nil means adapt indentation to outline node level.
  1058. When this variable is set, Org assumes that you write outlines by
  1059. indenting text in each node to align with the headline (after the stars).
  1060. The following issues are influenced by this variable:
  1061. - When this is set and the *entire* text in an entry is indented, the
  1062. indentation is increased by one space in a demotion command, and
  1063. decreased by one in a promotion command. If any line in the entry
  1064. body starts with text at column 0, indentation is not changed at all.
  1065. - Property drawers and planning information is inserted indented when
  1066. this variable s set. When nil, they will not be indented.
  1067. - TAB indents a line relative to context. The lines below a headline
  1068. will be indented when this variable is set.
  1069. Note that this is all about true indentation, by adding and removing
  1070. space characters. See also `org-indent.el' which does level-dependent
  1071. indentation in a virtual way, i.e. at display time in Emacs."
  1072. :group 'org-edit-structure
  1073. :type 'boolean)
  1074. (defcustom org-special-ctrl-a/e nil
  1075. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  1076. When t, `C-a' will bring back the cursor to the beginning of the
  1077. headline text, i.e. after the stars and after a possible TODO
  1078. keyword. In an item, this will be the position after bullet and
  1079. check-box, if any. When the cursor is already at that position,
  1080. another `C-a' will bring it to the beginning of the line.
  1081. `C-e' will jump to the end of the headline, ignoring the presence
  1082. of tags in the headline. A second `C-e' will then jump to the
  1083. true end of the line, after any tags. This also means that, when
  1084. this variable is non-nil, `C-e' also will never jump beyond the
  1085. end of the heading of a folded section, i.e. not after the
  1086. ellipses.
  1087. When set to the symbol `reversed', the first `C-a' or `C-e' works
  1088. normally, going to the true line boundary first. Only a directly
  1089. following, identical keypress will bring the cursor to the
  1090. special positions.
  1091. This may also be a cons cell where the behavior for `C-a' and
  1092. `C-e' is set separately."
  1093. :group 'org-edit-structure
  1094. :type '(choice
  1095. (const :tag "off" nil)
  1096. (const :tag "on: after stars/bullet and before tags first" t)
  1097. (const :tag "reversed: true line boundary first" reversed)
  1098. (cons :tag "Set C-a and C-e separately"
  1099. (choice :tag "Special C-a"
  1100. (const :tag "off" nil)
  1101. (const :tag "on: after stars/bullet first" t)
  1102. (const :tag "reversed: before stars/bullet first" reversed))
  1103. (choice :tag "Special C-e"
  1104. (const :tag "off" nil)
  1105. (const :tag "on: before tags first" t)
  1106. (const :tag "reversed: after tags first" reversed)))))
  1107. (org-defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e)
  1108. (defcustom org-special-ctrl-k nil
  1109. "Non-nil means `C-k' will behave specially in headlines.
  1110. When nil, `C-k' will call the default `kill-line' command.
  1111. When t, the following will happen while the cursor is in the headline:
  1112. - When the cursor is at the beginning of a headline, kill the entire
  1113. line and possible the folded subtree below the line.
  1114. - When in the middle of the headline text, kill the headline up to the tags.
  1115. - When after the headline text, kill the tags."
  1116. :group 'org-edit-structure
  1117. :type 'boolean)
  1118. (defcustom org-ctrl-k-protect-subtree nil
  1119. "Non-nil means, do not delete a hidden subtree with C-k.
  1120. When set to the symbol `error', simply throw an error when C-k is
  1121. used to kill (part-of) a headline that has hidden text behind it.
  1122. Any other non-nil value will result in a query to the user, if it is
  1123. OK to kill that hidden subtree. When nil, kill without remorse."
  1124. :group 'org-edit-structure
  1125. :version "24.1"
  1126. :type '(choice
  1127. (const :tag "Do not protect hidden subtrees" nil)
  1128. (const :tag "Protect hidden subtrees with a security query" t)
  1129. (const :tag "Never kill a hidden subtree with C-k" error)))
  1130. (defcustom org-catch-invisible-edits nil
  1131. "Check if in invisible region before inserting or deleting a character.
  1132. Valid values are:
  1133. nil Do not check, so just do invisible edits.
  1134. error Throw an error and do nothing.
  1135. show Make point visible, and do the requested edit.
  1136. show-and-error Make point visible, then throw an error and abort the edit.
  1137. smart Make point visible, and do insertion/deletion if it is
  1138. adjacent to visible text and the change feels predictable.
  1139. Never delete a previously invisible character or add in the
  1140. middle or right after an invisible region. Basically, this
  1141. allows insertion and backward-delete right before ellipses.
  1142. FIXME: maybe in this case we should not even show?"
  1143. :group 'org-edit-structure
  1144. :version "24.1"
  1145. :type '(choice
  1146. (const :tag "Do not check" nil)
  1147. (const :tag "Throw error when trying to edit" error)
  1148. (const :tag "Unhide, but do not do the edit" show-and-error)
  1149. (const :tag "Show invisible part and do the edit" show)
  1150. (const :tag "Be smart and do the right thing" smart)))
  1151. (defcustom org-yank-folded-subtrees t
  1152. "Non-nil means when yanking subtrees, fold them.
  1153. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  1154. it starts with a heading and all other headings in it are either children
  1155. or siblings, then fold all the subtrees. However, do this only if no
  1156. text after the yank would be swallowed into a folded tree by this action."
  1157. :group 'org-edit-structure
  1158. :type 'boolean)
  1159. (defcustom org-yank-adjusted-subtrees nil
  1160. "Non-nil means when yanking subtrees, adjust the level.
  1161. With this setting, `org-paste-subtree' is used to insert the subtree, see
  1162. this function for details."
  1163. :group 'org-edit-structure
  1164. :type 'boolean)
  1165. (defcustom org-M-RET-may-split-line '((default . t))
  1166. "Non-nil means M-RET will split the line at the cursor position.
  1167. When nil, it will go to the end of the line before making a
  1168. new line.
  1169. You may also set this option in a different way for different
  1170. contexts. Valid contexts are:
  1171. headline when creating a new headline
  1172. item when creating a new item
  1173. table in a table field
  1174. default the value to be used for all contexts not explicitly
  1175. customized"
  1176. :group 'org-structure
  1177. :group 'org-table
  1178. :type '(choice
  1179. (const :tag "Always" t)
  1180. (const :tag "Never" nil)
  1181. (repeat :greedy t :tag "Individual contexts"
  1182. (cons
  1183. (choice :tag "Context"
  1184. (const headline)
  1185. (const item)
  1186. (const table)
  1187. (const default))
  1188. (boolean)))))
  1189. (defcustom org-insert-heading-respect-content nil
  1190. "Non-nil means insert new headings after the current subtree.
  1191. When nil, the new heading is created directly after the current line.
  1192. The commands \\[org-insert-heading-respect-content] and \\[org-insert-todo-heading-respect-content] turn
  1193. this variable on for the duration of the command."
  1194. :group 'org-structure
  1195. :type 'boolean)
  1196. (defcustom org-blank-before-new-entry '((heading . auto)
  1197. (plain-list-item . auto))
  1198. "Should `org-insert-heading' leave a blank line before new heading/item?
  1199. The value is an alist, with `heading' and `plain-list-item' as CAR,
  1200. and a boolean flag as CDR. The cdr may also be the symbol `auto', in
  1201. which case Org will look at the surrounding headings/items and try to
  1202. make an intelligent decision whether to insert a blank line or not.
  1203. For plain lists, if `org-list-empty-line-terminates-plain-lists' is set,
  1204. the setting here is ignored and no empty line is inserted to avoid breaking
  1205. the list structure."
  1206. :group 'org-edit-structure
  1207. :type '(list
  1208. (cons (const heading)
  1209. (choice (const :tag "Never" nil)
  1210. (const :tag "Always" t)
  1211. (const :tag "Auto" auto)))
  1212. (cons (const plain-list-item)
  1213. (choice (const :tag "Never" nil)
  1214. (const :tag "Always" t)
  1215. (const :tag "Auto" auto)))))
  1216. (defcustom org-insert-heading-hook nil
  1217. "Hook being run after inserting a new heading."
  1218. :group 'org-edit-structure
  1219. :type 'hook)
  1220. (defcustom org-enable-fixed-width-editor t
  1221. "Non-nil means lines starting with \":\" are treated as fixed-width.
  1222. This currently only means they are never auto-wrapped.
  1223. When nil, such lines will be treated like ordinary lines.
  1224. See also the QUOTE keyword."
  1225. :group 'org-edit-structure
  1226. :type 'boolean)
  1227. (defcustom org-goto-auto-isearch t
  1228. "Non-nil means typing characters in `org-goto' starts incremental search.
  1229. When nil, you can use these keybindings to navigate the buffer:
  1230. q Quit the org-goto interface
  1231. n Go to the next visible heading
  1232. p Go to the previous visible heading
  1233. f Go one heading forward on same level
  1234. b Go one heading backward on same level
  1235. u Go one heading up"
  1236. :group 'org-edit-structure
  1237. :type 'boolean)
  1238. (defgroup org-sparse-trees nil
  1239. "Options concerning sparse trees in Org-mode."
  1240. :tag "Org Sparse Trees"
  1241. :group 'org-structure)
  1242. (defcustom org-highlight-sparse-tree-matches t
  1243. "Non-nil means highlight all matches that define a sparse tree.
  1244. The highlights will automatically disappear the next time the buffer is
  1245. changed by an edit command."
  1246. :group 'org-sparse-trees
  1247. :type 'boolean)
  1248. (defcustom org-remove-highlights-with-change t
  1249. "Non-nil means any change to the buffer will remove temporary highlights.
  1250. Such highlights are created by `org-occur' and `org-clock-display'.
  1251. When nil, `C-c C-c needs to be used to get rid of the highlights.
  1252. The highlights created by `org-preview-latex-fragment' always need
  1253. `C-c C-c' to be removed."
  1254. :group 'org-sparse-trees
  1255. :group 'org-time
  1256. :type 'boolean)
  1257. (defcustom org-occur-hook '(org-first-headline-recenter)
  1258. "Hook that is run after `org-occur' has constructed a sparse tree.
  1259. This can be used to recenter the window to show as much of the structure
  1260. as possible."
  1261. :group 'org-sparse-trees
  1262. :type 'hook)
  1263. (defgroup org-imenu-and-speedbar nil
  1264. "Options concerning imenu and speedbar in Org-mode."
  1265. :tag "Org Imenu and Speedbar"
  1266. :group 'org-structure)
  1267. (defcustom org-imenu-depth 2
  1268. "The maximum level for Imenu access to Org-mode headlines.
  1269. This also applied for speedbar access."
  1270. :group 'org-imenu-and-speedbar
  1271. :type 'integer)
  1272. (defgroup org-table nil
  1273. "Options concerning tables in Org-mode."
  1274. :tag "Org Table"
  1275. :group 'org)
  1276. (defcustom org-enable-table-editor 'optimized
  1277. "Non-nil means lines starting with \"|\" are handled by the table editor.
  1278. When nil, such lines will be treated like ordinary lines.
  1279. When equal to the symbol `optimized', the table editor will be optimized to
  1280. do the following:
  1281. - Automatic overwrite mode in front of whitespace in table fields.
  1282. This makes the structure of the table stay in tact as long as the edited
  1283. field does not exceed the column width.
  1284. - Minimize the number of realigns. Normally, the table is aligned each time
  1285. TAB or RET are pressed to move to another field. With optimization this
  1286. happens only if changes to a field might have changed the column width.
  1287. Optimization requires replacing the functions `self-insert-command',
  1288. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  1289. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  1290. very good at guessing when a re-align will be necessary, but you can always
  1291. force one with \\[org-ctrl-c-ctrl-c].
  1292. If you would like to use the optimized version in Org-mode, but the
  1293. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  1294. This variable can be used to turn on and off the table editor during a session,
  1295. but in order to toggle optimization, a restart is required.
  1296. See also the variable `org-table-auto-blank-field'."
  1297. :group 'org-table
  1298. :type '(choice
  1299. (const :tag "off" nil)
  1300. (const :tag "on" t)
  1301. (const :tag "on, optimized" optimized)))
  1302. (defcustom org-self-insert-cluster-for-undo (or (featurep 'xemacs)
  1303. (version<= emacs-version "24.1"))
  1304. "Non-nil means cluster self-insert commands for undo when possible.
  1305. If this is set, then, like in the Emacs command loop, 20 consecutive
  1306. characters will be undone together.
  1307. This is configurable, because there is some impact on typing performance."
  1308. :group 'org-table
  1309. :type 'boolean)
  1310. (defcustom org-table-tab-recognizes-table.el t
  1311. "Non-nil means TAB will automatically notice a table.el table.
  1312. When it sees such a table, it moves point into it and - if necessary -
  1313. calls `table-recognize-table'."
  1314. :group 'org-table-editing
  1315. :type 'boolean)
  1316. (defgroup org-link nil
  1317. "Options concerning links in Org-mode."
  1318. :tag "Org Link"
  1319. :group 'org)
  1320. (defvar org-link-abbrev-alist-local nil
  1321. "Buffer-local version of `org-link-abbrev-alist', which see.
  1322. The value of this is taken from the #+LINK lines.")
  1323. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  1324. (defcustom org-link-abbrev-alist nil
  1325. "Alist of link abbreviations.
  1326. The car of each element is a string, to be replaced at the start of a link.
  1327. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1328. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  1329. [[linkkey:tag][description]]
  1330. The 'linkkey' must be a word word, starting with a letter, followed
  1331. by letters, numbers, '-' or '_'.
  1332. If REPLACE is a string, the tag will simply be appended to create the link.
  1333. If the string contains \"%s\", the tag will be inserted there. If the string
  1334. contains \"%h\", it will cause a url-encoded version of the tag to be inserted
  1335. at that point (see the function `url-hexify-string'). If the string contains
  1336. the specifier \"%(my-function)\", then the custom function `my-function' will
  1337. be invoked: this function takes the tag as its only argument and must return
  1338. a string.
  1339. REPLACE may also be a function that will be called with the tag as the
  1340. only argument to create the link, which should be returned as a string.
  1341. See the manual for examples."
  1342. :group 'org-link
  1343. :type '(repeat
  1344. (cons
  1345. (string :tag "Protocol")
  1346. (choice
  1347. (string :tag "Format")
  1348. (function)))))
  1349. (defcustom org-descriptive-links t
  1350. "Non-nil means Org will display descriptive links.
  1351. E.g. [[http://orgmode.org][Org website]] will be displayed as
  1352. \"Org Website\", hiding the link itself and just displaying its
  1353. description. When set to `nil', Org will display the full links
  1354. literally.
  1355. You can interactively set the value of this variable by calling
  1356. `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
  1357. :group 'org-link
  1358. :type 'boolean)
  1359. (defcustom org-link-file-path-type 'adaptive
  1360. "How the path name in file links should be stored.
  1361. Valid values are:
  1362. relative Relative to the current directory, i.e. the directory of the file
  1363. into which the link is being inserted.
  1364. absolute Absolute path, if possible with ~ for home directory.
  1365. noabbrev Absolute path, no abbreviation of home directory.
  1366. adaptive Use relative path for files in the current directory and sub-
  1367. directories of it. For other files, use an absolute path."
  1368. :group 'org-link
  1369. :type '(choice
  1370. (const relative)
  1371. (const absolute)
  1372. (const noabbrev)
  1373. (const adaptive)))
  1374. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  1375. "Types of links that should be activated in Org-mode files.
  1376. This is a list of symbols, each leading to the activation of a certain link
  1377. type. In principle, it does not hurt to turn on most link types - there may
  1378. be a small gain when turning off unused link types. The types are:
  1379. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1380. angle Links in angular brackets that may contain whitespace like
  1381. <bbdb:Carsten Dominik>.
  1382. plain Plain links in normal text, no whitespace, like http://google.com.
  1383. radio Text that is matched by a radio target, see manual for details.
  1384. tag Tag settings in a headline (link to tag search).
  1385. date Time stamps (link to calendar).
  1386. footnote Footnote labels.
  1387. Changing this variable requires a restart of Emacs to become effective."
  1388. :group 'org-link
  1389. :type '(set :greedy t
  1390. (const :tag "Double bracket links" bracket)
  1391. (const :tag "Angular bracket links" angle)
  1392. (const :tag "Plain text links" plain)
  1393. (const :tag "Radio target matches" radio)
  1394. (const :tag "Tags" tag)
  1395. (const :tag "Timestamps" date)
  1396. (const :tag "Footnotes" footnote)))
  1397. (defcustom org-make-link-description-function nil
  1398. "Function to use for generating link descriptions from links.
  1399. When nil, the link location will be used. This function must take
  1400. two parameters: the first one is the link, the second one is the
  1401. description generated by `org-insert-link'. The function should
  1402. return the description to use."
  1403. :group 'org-link
  1404. :type 'function)
  1405. (defgroup org-link-store nil
  1406. "Options concerning storing links in Org-mode."
  1407. :tag "Org Store Link"
  1408. :group 'org-link)
  1409. (defcustom org-url-hexify-p t
  1410. "When non-nil, hexify URL when creating a link."
  1411. :type 'boolean
  1412. :version "24.3"
  1413. :group 'org-link-store)
  1414. (defcustom org-email-link-description-format "Email %c: %.30s"
  1415. "Format of the description part of a link to an email or usenet message.
  1416. The following %-escapes will be replaced by corresponding information:
  1417. %F full \"From\" field
  1418. %f name, taken from \"From\" field, address if no name
  1419. %T full \"To\" field
  1420. %t first name in \"To\" field, address if no name
  1421. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1422. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1423. %s subject
  1424. %d date
  1425. %m message-id.
  1426. You may use normal field width specification between the % and the letter.
  1427. This is for example useful to limit the length of the subject.
  1428. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1429. :group 'org-link-store
  1430. :type 'string)
  1431. (defcustom org-from-is-user-regexp
  1432. (let (r1 r2)
  1433. (when (and user-mail-address (not (string= user-mail-address "")))
  1434. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1435. (when (and user-full-name (not (string= user-full-name "")))
  1436. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1437. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1438. "Regexp matched against the \"From:\" header of an email or usenet message.
  1439. It should match if the message is from the user him/herself."
  1440. :group 'org-link-store
  1441. :type 'regexp)
  1442. (defcustom org-context-in-file-links t
  1443. "Non-nil means file links from `org-store-link' contain context.
  1444. A search string will be added to the file name with :: as separator and
  1445. used to find the context when the link is activated by the command
  1446. `org-open-at-point'. When this option is t, the entire active region
  1447. will be placed in the search string of the file link. If set to a
  1448. positive integer, only the first n lines of context will be stored.
  1449. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1450. negates this setting for the duration of the command."
  1451. :group 'org-link-store
  1452. :type '(choice boolean integer))
  1453. (defcustom org-keep-stored-link-after-insertion nil
  1454. "Non-nil means keep link in list for entire session.
  1455. The command `org-store-link' adds a link pointing to the current
  1456. location to an internal list. These links accumulate during a session.
  1457. The command `org-insert-link' can be used to insert links into any
  1458. Org-mode file (offering completion for all stored links). When this
  1459. option is nil, every link which has been inserted once using \\[org-insert-link]
  1460. will be removed from the list, to make completing the unused links
  1461. more efficient."
  1462. :group 'org-link-store
  1463. :type 'boolean)
  1464. (defgroup org-link-follow nil
  1465. "Options concerning following links in Org-mode."
  1466. :tag "Org Follow Link"
  1467. :group 'org-link)
  1468. (defcustom org-link-translation-function nil
  1469. "Function to translate links with different syntax to Org syntax.
  1470. This can be used to translate links created for example by the Planner
  1471. or emacs-wiki packages to Org syntax.
  1472. The function must accept two parameters, a TYPE containing the link
  1473. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1474. which is everything after the link protocol. It should return a cons
  1475. with possibly modified values of type and path.
  1476. Org contains a function for this, so if you set this variable to
  1477. `org-translate-link-from-planner', you should be able follow many
  1478. links created by planner."
  1479. :group 'org-link-follow
  1480. :type 'function)
  1481. (defcustom org-follow-link-hook nil
  1482. "Hook that is run after a link has been followed."
  1483. :group 'org-link-follow
  1484. :type 'hook)
  1485. (defcustom org-tab-follows-link nil
  1486. "Non-nil means on links TAB will follow the link.
  1487. Needs to be set before org.el is loaded.
  1488. This really should not be used, it does not make sense, and the
  1489. implementation is bad."
  1490. :group 'org-link-follow
  1491. :type 'boolean)
  1492. (defcustom org-return-follows-link nil
  1493. "Non-nil means on links RET will follow the link.
  1494. In tables, the special behavior of RET has precedence."
  1495. :group 'org-link-follow
  1496. :type 'boolean)
  1497. (defcustom org-mouse-1-follows-link
  1498. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1499. "Non-nil means mouse-1 on a link will follow the link.
  1500. A longer mouse click will still set point. Does not work on XEmacs.
  1501. Needs to be set before org.el is loaded."
  1502. :group 'org-link-follow
  1503. :type 'boolean)
  1504. (defcustom org-mark-ring-length 4
  1505. "Number of different positions to be recorded in the ring.
  1506. Changing this requires a restart of Emacs to work correctly."
  1507. :group 'org-link-follow
  1508. :type 'integer)
  1509. (defcustom org-link-search-must-match-exact-headline 'query-to-create
  1510. "Non-nil means internal links in Org files must exactly match a headline.
  1511. When nil, the link search tries to match a phrase with all words
  1512. in the search text."
  1513. :group 'org-link-follow
  1514. :version "24.1"
  1515. :type '(choice
  1516. (const :tag "Use fuzzy text search" nil)
  1517. (const :tag "Match only exact headline" t)
  1518. (const :tag "Match exact headline or query to create it"
  1519. query-to-create)))
  1520. (defcustom org-link-frame-setup
  1521. '((vm . vm-visit-folder-other-frame)
  1522. (vm-imap . vm-visit-imap-folder-other-frame)
  1523. (gnus . org-gnus-no-new-news)
  1524. (file . find-file-other-window)
  1525. (wl . wl-other-frame))
  1526. "Setup the frame configuration for following links.
  1527. When following a link with Emacs, it may often be useful to display
  1528. this link in another window or frame. This variable can be used to
  1529. set this up for the different types of links.
  1530. For VM, use any of
  1531. `vm-visit-folder'
  1532. `vm-visit-folder-other-window'
  1533. `vm-visit-folder-other-frame'
  1534. For Gnus, use any of
  1535. `gnus'
  1536. `gnus-other-frame'
  1537. `org-gnus-no-new-news'
  1538. For FILE, use any of
  1539. `find-file'
  1540. `find-file-other-window'
  1541. `find-file-other-frame'
  1542. For Wanderlust use any of
  1543. `wl'
  1544. `wl-other-frame'
  1545. For the calendar, use the variable `calendar-setup'.
  1546. For BBDB, it is currently only possible to display the matches in
  1547. another window."
  1548. :group 'org-link-follow
  1549. :type '(list
  1550. (cons (const vm)
  1551. (choice
  1552. (const vm-visit-folder)
  1553. (const vm-visit-folder-other-window)
  1554. (const vm-visit-folder-other-frame)))
  1555. (cons (const gnus)
  1556. (choice
  1557. (const gnus)
  1558. (const gnus-other-frame)
  1559. (const org-gnus-no-new-news)))
  1560. (cons (const file)
  1561. (choice
  1562. (const find-file)
  1563. (const find-file-other-window)
  1564. (const find-file-other-frame)))
  1565. (cons (const wl)
  1566. (choice
  1567. (const wl)
  1568. (const wl-other-frame)))))
  1569. (defcustom org-display-internal-link-with-indirect-buffer nil
  1570. "Non-nil means use indirect buffer to display infile links.
  1571. Activating internal links (from one location in a file to another location
  1572. in the same file) normally just jumps to the location. When the link is
  1573. activated with a \\[universal-argument] prefix (or with mouse-3), the link \
  1574. is displayed in
  1575. another window. When this option is set, the other window actually displays
  1576. an indirect buffer clone of the current buffer, to avoid any visibility
  1577. changes to the current buffer."
  1578. :group 'org-link-follow
  1579. :type 'boolean)
  1580. (defcustom org-open-non-existing-files nil
  1581. "Non-nil means `org-open-file' will open non-existing files.
  1582. When nil, an error will be generated.
  1583. This variable applies only to external applications because they
  1584. might choke on non-existing files. If the link is to a file that
  1585. will be opened in Emacs, the variable is ignored."
  1586. :group 'org-link-follow
  1587. :type 'boolean)
  1588. (defcustom org-open-directory-means-index-dot-org nil
  1589. "Non-nil means a link to a directory really means to index.org.
  1590. When nil, following a directory link will run dired or open a finder/explorer
  1591. window on that directory."
  1592. :group 'org-link-follow
  1593. :type 'boolean)
  1594. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1595. "Function and arguments to call for following mailto links.
  1596. This is a list with the first element being a Lisp function, and the
  1597. remaining elements being arguments to the function. In string arguments,
  1598. %a will be replaced by the address, and %s will be replaced by the subject
  1599. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1600. :group 'org-link-follow
  1601. :type '(choice
  1602. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1603. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1604. (const :tag "message-mail" (message-mail "%a" "%s"))
  1605. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1606. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1607. "Non-nil means ask for confirmation before executing shell links.
  1608. Shell links can be dangerous: just think about a link
  1609. [[shell:rm -rf ~/*][Google Search]]
  1610. This link would show up in your Org-mode document as \"Google Search\",
  1611. but really it would remove your entire home directory.
  1612. Therefore we advise against setting this variable to nil.
  1613. Just change it to `y-or-n-p' if you want to confirm with a
  1614. single keystroke rather than having to type \"yes\"."
  1615. :group 'org-link-follow
  1616. :type '(choice
  1617. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1618. (const :tag "with y-or-n (faster)" y-or-n-p)
  1619. (const :tag "no confirmation (dangerous)" nil)))
  1620. (put 'org-confirm-shell-link-function
  1621. 'safe-local-variable
  1622. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1623. (defcustom org-confirm-shell-link-not-regexp ""
  1624. "A regexp to skip confirmation for shell links."
  1625. :group 'org-link-follow
  1626. :version "24.1"
  1627. :type 'regexp)
  1628. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1629. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1630. Elisp links can be dangerous: just think about a link
  1631. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1632. This link would show up in your Org-mode document as \"Google Search\",
  1633. but really it would remove your entire home directory.
  1634. Therefore we advise against setting this variable to nil.
  1635. Just change it to `y-or-n-p' if you want to confirm with a
  1636. single keystroke rather than having to type \"yes\"."
  1637. :group 'org-link-follow
  1638. :type '(choice
  1639. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1640. (const :tag "with y-or-n (faster)" y-or-n-p)
  1641. (const :tag "no confirmation (dangerous)" nil)))
  1642. (put 'org-confirm-shell-link-function
  1643. 'safe-local-variable
  1644. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1645. (defcustom org-confirm-elisp-link-not-regexp ""
  1646. "A regexp to skip confirmation for Elisp links."
  1647. :group 'org-link-follow
  1648. :version "24.1"
  1649. :type 'regexp)
  1650. (defconst org-file-apps-defaults-gnu
  1651. '((remote . emacs)
  1652. (system . mailcap)
  1653. (t . mailcap))
  1654. "Default file applications on a UNIX or GNU/Linux system.
  1655. See `org-file-apps'.")
  1656. (defconst org-file-apps-defaults-macosx
  1657. '((remote . emacs)
  1658. (t . "open %s")
  1659. (system . "open %s")
  1660. ("ps.gz" . "gv %s")
  1661. ("eps.gz" . "gv %s")
  1662. ("dvi" . "xdvi %s")
  1663. ("fig" . "xfig %s"))
  1664. "Default file applications on a MacOS X system.
  1665. The system \"open\" is known as a default, but we use X11 applications
  1666. for some files for which the OS does not have a good default.
  1667. See `org-file-apps'.")
  1668. (defconst org-file-apps-defaults-windowsnt
  1669. (list
  1670. '(remote . emacs)
  1671. (cons t
  1672. (list (if (featurep 'xemacs)
  1673. 'mswindows-shell-execute
  1674. 'w32-shell-execute)
  1675. "open" 'file))
  1676. (cons 'system
  1677. (list (if (featurep 'xemacs)
  1678. 'mswindows-shell-execute
  1679. 'w32-shell-execute)
  1680. "open" 'file)))
  1681. "Default file applications on a Windows NT system.
  1682. The system \"open\" is used for most files.
  1683. See `org-file-apps'.")
  1684. (defcustom org-file-apps
  1685. '(
  1686. (auto-mode . emacs)
  1687. ("\\.mm\\'" . default)
  1688. ("\\.x?html?\\'" . default)
  1689. ("\\.pdf\\'" . default)
  1690. )
  1691. "External applications for opening `file:path' items in a document.
  1692. Org-mode uses system defaults for different file types, but
  1693. you can use this variable to set the application for a given file
  1694. extension. The entries in this list are cons cells where the car identifies
  1695. files and the cdr the corresponding command. Possible values for the
  1696. file identifier are
  1697. \"string\" A string as a file identifier can be interpreted in different
  1698. ways, depending on its contents:
  1699. - Alphanumeric characters only:
  1700. Match links with this file extension.
  1701. Example: (\"pdf\" . \"evince %s\")
  1702. to open PDFs with evince.
  1703. - Regular expression: Match links where the
  1704. filename matches the regexp. If you want to
  1705. use groups here, use shy groups.
  1706. Example: (\"\\.x?html\\'\" . \"firefox %s\")
  1707. (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1708. to open *.html and *.xhtml with firefox.
  1709. - Regular expression which contains (non-shy) groups:
  1710. Match links where the whole link, including \"::\", and
  1711. anything after that, matches the regexp.
  1712. In a custom command string, %1, %2, etc. are replaced with
  1713. the parts of the link that were matched by the groups.
  1714. For backwards compatibility, if a command string is given
  1715. that does not use any of the group matches, this case is
  1716. handled identically to the second one (i.e. match against
  1717. file name only).
  1718. In a custom lisp form, you can access the group matches with
  1719. (match-string n link).
  1720. Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
  1721. to open [[file:document.pdf::5]] with evince at page 5.
  1722. `directory' Matches a directory
  1723. `remote' Matches a remote file, accessible through tramp or efs.
  1724. Remote files most likely should be visited through Emacs
  1725. because external applications cannot handle such paths.
  1726. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1727. so all files Emacs knows how to handle. Using this with
  1728. command `emacs' will open most files in Emacs. Beware that this
  1729. will also open html files inside Emacs, unless you add
  1730. (\"html\" . default) to the list as well.
  1731. t Default for files not matched by any of the other options.
  1732. `system' The system command to open files, like `open' on Windows
  1733. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1734. that will be selected if you call `C-c C-o' with a double
  1735. \\[universal-argument] \\[universal-argument] prefix.
  1736. Possible values for the command are:
  1737. `emacs' The file will be visited by the current Emacs process.
  1738. `default' Use the default application for this file type, which is the
  1739. association for t in the list, most likely in the system-specific
  1740. part.
  1741. This can be used to overrule an unwanted setting in the
  1742. system-specific variable.
  1743. `system' Use the system command for opening files, like \"open\".
  1744. This command is specified by the entry whose car is `system'.
  1745. Most likely, the system-specific version of this variable
  1746. does define this command, but you can overrule/replace it
  1747. here.
  1748. string A command to be executed by a shell; %s will be replaced
  1749. by the path to the file.
  1750. sexp A Lisp form which will be evaluated. The file path will
  1751. be available in the Lisp variable `file'.
  1752. For more examples, see the system specific constants
  1753. `org-file-apps-defaults-macosx'
  1754. `org-file-apps-defaults-windowsnt'
  1755. `org-file-apps-defaults-gnu'."
  1756. :group 'org-link-follow
  1757. :type '(repeat
  1758. (cons (choice :value ""
  1759. (string :tag "Extension")
  1760. (const :tag "System command to open files" system)
  1761. (const :tag "Default for unrecognized files" t)
  1762. (const :tag "Remote file" remote)
  1763. (const :tag "Links to a directory" directory)
  1764. (const :tag "Any files that have Emacs modes"
  1765. auto-mode))
  1766. (choice :value ""
  1767. (const :tag "Visit with Emacs" emacs)
  1768. (const :tag "Use default" default)
  1769. (const :tag "Use the system command" system)
  1770. (string :tag "Command")
  1771. (sexp :tag "Lisp form")))))
  1772. (defcustom org-doi-server-url "http://dx.doi.org/"
  1773. "The URL of the DOI server."
  1774. :type 'string
  1775. :version "24.3"
  1776. :group 'org-link-follow)
  1777. (defgroup org-refile nil
  1778. "Options concerning refiling entries in Org-mode."
  1779. :tag "Org Refile"
  1780. :group 'org)
  1781. (defcustom org-directory "~/org"
  1782. "Directory with org files.
  1783. This is just a default location to look for Org files. There is no need
  1784. at all to put your files into this directory. It is only used in the
  1785. following situations:
  1786. 1. When a capture template specifies a target file that is not an
  1787. absolute path. The path will then be interpreted relative to
  1788. `org-directory'
  1789. 2. When a capture note is filed away in an interactive way (when exiting the
  1790. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1791. with `org-directory' as the default path."
  1792. :group 'org-refile
  1793. :group 'org-remember
  1794. :group 'org-capture
  1795. :type 'directory)
  1796. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1797. "Default target for storing notes.
  1798. Used as a fall back file for org-remember.el and org-capture.el, for
  1799. templates that do not specify a target file."
  1800. :group 'org-refile
  1801. :group 'org-remember
  1802. :group 'org-capture
  1803. :type '(choice
  1804. (const :tag "Default from remember-data-file" nil)
  1805. file))
  1806. (defcustom org-goto-interface 'outline
  1807. "The default interface to be used for `org-goto'.
  1808. Allowed values are:
  1809. outline The interface shows an outline of the relevant file
  1810. and the correct heading is found by moving through
  1811. the outline or by searching with incremental search.
  1812. outline-path-completion Headlines in the current buffer are offered via
  1813. completion. This is the interface also used by
  1814. the refile command."
  1815. :group 'org-refile
  1816. :type '(choice
  1817. (const :tag "Outline" outline)
  1818. (const :tag "Outline-path-completion" outline-path-completion)))
  1819. (defcustom org-goto-max-level 5
  1820. "Maximum target level when running `org-goto' with refile interface."
  1821. :group 'org-refile
  1822. :type 'integer)
  1823. (defcustom org-reverse-note-order nil
  1824. "Non-nil means store new notes at the beginning of a file or entry.
  1825. When nil, new notes will be filed to the end of a file or entry.
  1826. This can also be a list with cons cells of regular expressions that
  1827. are matched against file names, and values."
  1828. :group 'org-remember
  1829. :group 'org-capture
  1830. :group 'org-refile
  1831. :type '(choice
  1832. (const :tag "Reverse always" t)
  1833. (const :tag "Reverse never" nil)
  1834. (repeat :tag "By file name regexp"
  1835. (cons regexp boolean))))
  1836. (defcustom org-log-refile nil
  1837. "Information to record when a task is refiled.
  1838. Possible values are:
  1839. nil Don't add anything
  1840. time Add a time stamp to the task
  1841. note Prompt for a note and add it with template `org-log-note-headings'
  1842. This option can also be set with on a per-file-basis with
  1843. #+STARTUP: nologrefile
  1844. #+STARTUP: logrefile
  1845. #+STARTUP: lognoterefile
  1846. You can have local logging settings for a subtree by setting the LOGGING
  1847. property to one or more of these keywords.
  1848. When bulk-refiling from the agenda, the value `note' is forbidden and
  1849. will temporarily be changed to `time'."
  1850. :group 'org-refile
  1851. :group 'org-progress
  1852. :version "24.1"
  1853. :type '(choice
  1854. (const :tag "No logging" nil)
  1855. (const :tag "Record timestamp" time)
  1856. (const :tag "Record timestamp with note." note)))
  1857. (defcustom org-refile-targets nil
  1858. "Targets for refiling entries with \\[org-refile].
  1859. This is a list of cons cells. Each cell contains:
  1860. - a specification of the files to be considered, either a list of files,
  1861. or a symbol whose function or variable value will be used to retrieve
  1862. a file name or a list of file names. If you use `org-agenda-files' for
  1863. that, all agenda files will be scanned for targets. Nil means consider
  1864. headings in the current buffer.
  1865. - A specification of how to find candidate refile targets. This may be
  1866. any of:
  1867. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1868. This tag has to be present in all target headlines, inheritance will
  1869. not be considered.
  1870. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1871. todo keyword.
  1872. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1873. headlines that are refiling targets.
  1874. - a cons cell (:level . N). Any headline of level N is considered a target.
  1875. Note that, when `org-odd-levels-only' is set, level corresponds to
  1876. order in hierarchy, not to the number of stars.
  1877. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1878. Note that, when `org-odd-levels-only' is set, level corresponds to
  1879. order in hierarchy, not to the number of stars.
  1880. Each element of this list generates a set of possible targets.
  1881. The union of these sets is presented (with completion) to
  1882. the user by `org-refile'.
  1883. You can set the variable `org-refile-target-verify-function' to a function
  1884. to verify each headline found by the simple criteria above.
  1885. When this variable is nil, all top-level headlines in the current buffer
  1886. are used, equivalent to the value `((nil . (:level . 1))'."
  1887. :group 'org-refile
  1888. :type '(repeat
  1889. (cons
  1890. (choice :value org-agenda-files
  1891. (const :tag "All agenda files" org-agenda-files)
  1892. (const :tag "Current buffer" nil)
  1893. (function) (variable) (file))
  1894. (choice :tag "Identify target headline by"
  1895. (cons :tag "Specific tag" (const :value :tag) (string))
  1896. (cons :tag "TODO keyword" (const :value :todo) (string))
  1897. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1898. (cons :tag "Level number" (const :value :level) (integer))
  1899. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1900. (defcustom org-refile-target-verify-function nil
  1901. "Function to verify if the headline at point should be a refile target.
  1902. The function will be called without arguments, with point at the
  1903. beginning of the headline. It should return t and leave point
  1904. where it is if the headline is a valid target for refiling.
  1905. If the target should not be selected, the function must return nil.
  1906. In addition to this, it may move point to a place from where the search
  1907. should be continued. For example, the function may decide that the entire
  1908. subtree of the current entry should be excluded and move point to the end
  1909. of the subtree."
  1910. :group 'org-refile
  1911. :type 'function)
  1912. (defcustom org-refile-use-cache nil
  1913. "Non-nil means cache refile targets to speed up the process.
  1914. The cache for a particular file will be updated automatically when
  1915. the buffer has been killed, or when any of the marker used for flagging
  1916. refile targets no longer points at a live buffer.
  1917. If you have added new entries to a buffer that might themselves be targets,
  1918. you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
  1919. find that easier, `C-u C-u C-u C-c C-w'."
  1920. :group 'org-refile
  1921. :version "24.1"
  1922. :type 'boolean)
  1923. (defcustom org-refile-use-outline-path nil
  1924. "Non-nil means provide refile targets as paths.
  1925. So a level 3 headline will be available as level1/level2/level3.
  1926. When the value is `file', also include the file name (without directory)
  1927. into the path. In this case, you can also stop the completion after
  1928. the file name, to get entries inserted as top level in the file.
  1929. When `full-file-path', include the full file path."
  1930. :group 'org-refile
  1931. :type '(choice
  1932. (const :tag "Not" nil)
  1933. (const :tag "Yes" t)
  1934. (const :tag "Start with file name" file)
  1935. (const :tag "Start with full file path" full-file-path)))
  1936. (defcustom org-outline-path-complete-in-steps t
  1937. "Non-nil means complete the outline path in hierarchical steps.
  1938. When Org-mode uses the refile interface to select an outline path
  1939. \(see variable `org-refile-use-outline-path'), the completion of
  1940. the path can be done is a single go, or if can be done in steps down
  1941. the headline hierarchy. Going in steps is probably the best if you
  1942. do not use a special completion package like `ido' or `icicles'.
  1943. However, when using these packages, going in one step can be very
  1944. fast, while still showing the whole path to the entry."
  1945. :group 'org-refile
  1946. :type 'boolean)
  1947. (defcustom org-refile-allow-creating-parent-nodes nil
  1948. "Non-nil means allow to create new nodes as refile targets.
  1949. New nodes are then created by adding \"/new node name\" to the completion
  1950. of an existing node. When the value of this variable is `confirm',
  1951. new node creation must be confirmed by the user (recommended)
  1952. When nil, the completion must match an existing entry.
  1953. Note that, if the new heading is not seen by the criteria
  1954. listed in `org-refile-targets', multiple instances of the same
  1955. heading would be created by trying again to file under the new
  1956. heading."
  1957. :group 'org-refile
  1958. :type '(choice
  1959. (const :tag "Never" nil)
  1960. (const :tag "Always" t)
  1961. (const :tag "Prompt for confirmation" confirm)))
  1962. (defcustom org-refile-active-region-within-subtree nil
  1963. "Non-nil means also refile active region within a subtree.
  1964. By default `org-refile' doesn't allow refiling regions if they
  1965. don't contain a set of subtrees, but it might be convenient to
  1966. do so sometimes: in that case, the first line of the region is
  1967. converted to a headline before refiling."
  1968. :group 'org-refile
  1969. :version "24.1"
  1970. :type 'boolean)
  1971. (defgroup org-todo nil
  1972. "Options concerning TODO items in Org-mode."
  1973. :tag "Org TODO"
  1974. :group 'org)
  1975. (defgroup org-progress nil
  1976. "Options concerning Progress logging in Org-mode."
  1977. :tag "Org Progress"
  1978. :group 'org-time)
  1979. (defvar org-todo-interpretation-widgets
  1980. '((:tag "Sequence (cycling hits every state)" sequence)
  1981. (:tag "Type (cycling directly to DONE)" type))
  1982. "The available interpretation symbols for customizing `org-todo-keywords'.
  1983. Interested libraries should add to this list.")
  1984. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1985. "List of TODO entry keyword sequences and their interpretation.
  1986. \\<org-mode-map>This is a list of sequences.
  1987. Each sequence starts with a symbol, either `sequence' or `type',
  1988. indicating if the keywords should be interpreted as a sequence of
  1989. action steps, or as different types of TODO items. The first
  1990. keywords are states requiring action - these states will select a headline
  1991. for inclusion into the global TODO list Org-mode produces. If one of
  1992. the \"keywords\" is the vertical bar, \"|\", the remaining keywords
  1993. signify that no further action is necessary. If \"|\" is not found,
  1994. the last keyword is treated as the only DONE state of the sequence.
  1995. The command \\[org-todo] cycles an entry through these states, and one
  1996. additional state where no keyword is present. For details about this
  1997. cycling, see the manual.
  1998. TODO keywords and interpretation can also be set on a per-file basis with
  1999. the special #+SEQ_TODO and #+TYP_TODO lines.
  2000. Each keyword can optionally specify a character for fast state selection
  2001. \(in combination with the variable `org-use-fast-todo-selection')
  2002. and specifiers for state change logging, using the same syntax that
  2003. is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says that
  2004. the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  2005. indicates to record a time stamp each time this state is selected.
  2006. Each keyword may also specify if a timestamp or a note should be
  2007. recorded when entering or leaving the state, by adding additional
  2008. characters in the parenthesis after the keyword. This looks like this:
  2009. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  2010. record only the time of the state change. With X and Y being either
  2011. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  2012. Y when leaving the state if and only if the *target* state does not
  2013. define X. You may omit any of the fast-selection key or X or /Y,
  2014. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  2015. For backward compatibility, this variable may also be just a list
  2016. of keywords. In this case the interpretation (sequence or type) will be
  2017. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  2018. :group 'org-todo
  2019. :group 'org-keywords
  2020. :type '(choice
  2021. (repeat :tag "Old syntax, just keywords"
  2022. (string :tag "Keyword"))
  2023. (repeat :tag "New syntax"
  2024. (cons
  2025. (choice
  2026. :tag "Interpretation"
  2027. ;;Quick and dirty way to see
  2028. ;;`org-todo-interpretations'. This takes the
  2029. ;;place of item arguments
  2030. :convert-widget
  2031. (lambda (widget)
  2032. (widget-put widget
  2033. :args (mapcar
  2034. #'(lambda (x)
  2035. (widget-convert
  2036. (cons 'const x)))
  2037. org-todo-interpretation-widgets))
  2038. widget))
  2039. (repeat
  2040. (string :tag "Keyword"))))))
  2041. (defvar org-todo-keywords-1 nil
  2042. "All TODO and DONE keywords active in a buffer.")
  2043. (make-variable-buffer-local 'org-todo-keywords-1)
  2044. (defvar org-todo-keywords-for-agenda nil)
  2045. (defvar org-done-keywords-for-agenda nil)
  2046. (defvar org-drawers-for-agenda nil)
  2047. (defvar org-todo-keyword-alist-for-agenda nil)
  2048. (defvar org-tag-alist-for-agenda nil
  2049. "Alist of all tags from all agenda files.")
  2050. (defvar org-tag-groups-alist-for-agenda nil
  2051. "Alist of all groups tags from all current agenda files.")
  2052. (defvar org-tag-groups-alist nil)
  2053. (make-variable-buffer-local 'org-tag-groups-alist)
  2054. (defvar org-agenda-contributing-files nil)
  2055. (defvar org-not-done-keywords nil)
  2056. (make-variable-buffer-local 'org-not-done-keywords)
  2057. (defvar org-done-keywords nil)
  2058. (make-variable-buffer-local 'org-done-keywords)
  2059. (defvar org-todo-heads nil)
  2060. (make-variable-buffer-local 'org-todo-heads)
  2061. (defvar org-todo-sets nil)
  2062. (make-variable-buffer-local 'org-todo-sets)
  2063. (defvar org-todo-log-states nil)
  2064. (make-variable-buffer-local 'org-todo-log-states)
  2065. (defvar org-todo-kwd-alist nil)
  2066. (make-variable-buffer-local 'org-todo-kwd-alist)
  2067. (defvar org-todo-key-alist nil)
  2068. (make-variable-buffer-local 'org-todo-key-alist)
  2069. (defvar org-todo-key-trigger nil)
  2070. (make-variable-buffer-local 'org-todo-key-trigger)
  2071. (defcustom org-todo-interpretation 'sequence
  2072. "Controls how TODO keywords are interpreted.
  2073. This variable is in principle obsolete and is only used for
  2074. backward compatibility, if the interpretation of todo keywords is
  2075. not given already in `org-todo-keywords'. See that variable for
  2076. more information."
  2077. :group 'org-todo
  2078. :group 'org-keywords
  2079. :type '(choice (const sequence)
  2080. (const type)))
  2081. (defcustom org-use-fast-todo-selection t
  2082. "Non-nil means use the fast todo selection scheme with C-c C-t.
  2083. This variable describes if and under what circumstances the cycling
  2084. mechanism for TODO keywords will be replaced by a single-key, direct
  2085. selection scheme.
  2086. When nil, fast selection is never used.
  2087. When the symbol `prefix', it will be used when `org-todo' is called
  2088. with a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and
  2089. `C-u t' in an agenda buffer.
  2090. When t, fast selection is used by default. In this case, the prefix
  2091. argument forces cycling instead.
  2092. In all cases, the special interface is only used if access keys have
  2093. actually been assigned by the user, i.e. if keywords in the configuration
  2094. are followed by a letter in parenthesis, like TODO(t)."
  2095. :group 'org-todo
  2096. :type '(choice
  2097. (const :tag "Never" nil)
  2098. (const :tag "By default" t)
  2099. (const :tag "Only with C-u C-c C-t" prefix)))
  2100. (defcustom org-provide-todo-statistics t
  2101. "Non-nil means update todo statistics after insert and toggle.
  2102. ALL-HEADLINES means update todo statistics by including headlines
  2103. with no TODO keyword as well, counting them as not done.
  2104. A list of TODO keywords means the same, but skip keywords that are
  2105. not in this list.
  2106. When this is set, todo statistics is updated in the parent of the
  2107. current entry each time a todo state is changed."
  2108. :group 'org-todo
  2109. :type '(choice
  2110. (const :tag "Yes, only for TODO entries" t)
  2111. (const :tag "Yes, including all entries" 'all-headlines)
  2112. (repeat :tag "Yes, for TODOs in this list"
  2113. (string :tag "TODO keyword"))
  2114. (other :tag "No TODO statistics" nil)))
  2115. (defcustom org-hierarchical-todo-statistics t
  2116. "Non-nil means TODO statistics covers just direct children.
  2117. When nil, all entries in the subtree are considered.
  2118. This has only an effect if `org-provide-todo-statistics' is set.
  2119. To set this to nil for only a single subtree, use a COOKIE_DATA
  2120. property and include the word \"recursive\" into the value."
  2121. :group 'org-todo
  2122. :type 'boolean)
  2123. (defcustom org-after-todo-state-change-hook nil
  2124. "Hook which is run after the state of a TODO item was changed.
  2125. The new state (a string with a TODO keyword, or nil) is available in the
  2126. Lisp variable `org-state'."
  2127. :group 'org-todo
  2128. :type 'hook)
  2129. (defvar org-blocker-hook nil
  2130. "Hook for functions that are allowed to block a state change.
  2131. Functions in this hook should not modify the buffer.
  2132. Each function gets as its single argument a property list,
  2133. see `org-trigger-hook' for more information about this list.
  2134. If any of the functions in this hook returns nil, the state change
  2135. is blocked.")
  2136. (defvar org-trigger-hook nil
  2137. "Hook for functions that are triggered by a state change.
  2138. Each function gets as its single argument a property list with at
  2139. least the following elements:
  2140. (:type type-of-change :position pos-at-entry-start
  2141. :from old-state :to new-state)
  2142. Depending on the type, more properties may be present.
  2143. This mechanism is currently implemented for:
  2144. TODO state changes
  2145. ------------------
  2146. :type todo-state-change
  2147. :from previous state (keyword as a string), or nil, or a symbol
  2148. 'todo' or 'done', to indicate the general type of state.
  2149. :to new state, like in :from")
  2150. (defcustom org-enforce-todo-dependencies nil
  2151. "Non-nil means undone TODO entries will block switching the parent to DONE.
  2152. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  2153. be blocked if any prior sibling is not yet done.
  2154. Finally, if the parent is blocked because of ordered siblings of its own,
  2155. the child will also be blocked."
  2156. :set (lambda (var val)
  2157. (set var val)
  2158. (if val
  2159. (add-hook 'org-blocker-hook
  2160. 'org-block-todo-from-children-or-siblings-or-parent)
  2161. (remove-hook 'org-blocker-hook
  2162. 'org-block-todo-from-children-or-siblings-or-parent)))
  2163. :group 'org-todo
  2164. :type 'boolean)
  2165. (defcustom org-enforce-todo-checkbox-dependencies nil
  2166. "Non-nil means unchecked boxes will block switching the parent to DONE.
  2167. When this is nil, checkboxes have no influence on switching TODO states.
  2168. When non-nil, you first need to check off all check boxes before the TODO
  2169. entry can be switched to DONE.
  2170. This variable needs to be set before org.el is loaded, and you need to
  2171. restart Emacs after a change to make the change effective. The only way
  2172. to change is while Emacs is running is through the customize interface."
  2173. :set (lambda (var val)
  2174. (set var val)
  2175. (if val
  2176. (add-hook 'org-blocker-hook
  2177. 'org-block-todo-from-checkboxes)
  2178. (remove-hook 'org-blocker-hook
  2179. 'org-block-todo-from-checkboxes)))
  2180. :group 'org-todo
  2181. :type 'boolean)
  2182. (defcustom org-treat-insert-todo-heading-as-state-change nil
  2183. "Non-nil means inserting a TODO heading is treated as state change.
  2184. So when the command \\[org-insert-todo-heading] is used, state change
  2185. logging will apply if appropriate. When nil, the new TODO item will
  2186. be inserted directly, and no logging will take place."
  2187. :group 'org-todo
  2188. :type 'boolean)
  2189. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  2190. "Non-nil means switching TODO states with S-cursor counts as state change.
  2191. This is the default behavior. However, setting this to nil allows a
  2192. convenient way to select a TODO state and bypass any logging associated
  2193. with that."
  2194. :group 'org-todo
  2195. :type 'boolean)
  2196. (defcustom org-todo-state-tags-triggers nil
  2197. "Tag changes that should be triggered by TODO state changes.
  2198. This is a list. Each entry is
  2199. (state-change (tag . flag) .......)
  2200. State-change can be a string with a state, and empty string to indicate the
  2201. state that has no TODO keyword, or it can be one of the symbols `todo'
  2202. or `done', meaning any not-done or done state, respectively."
  2203. :group 'org-todo
  2204. :group 'org-tags
  2205. :type '(repeat
  2206. (cons (choice :tag "When changing to"
  2207. (const :tag "Not-done state" todo)
  2208. (const :tag "Done state" done)
  2209. (string :tag "State"))
  2210. (repeat
  2211. (cons :tag "Tag action"
  2212. (string :tag "Tag")
  2213. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  2214. (defcustom org-log-done nil
  2215. "Information to record when a task moves to the DONE state.
  2216. Possible values are:
  2217. nil Don't add anything, just change the keyword
  2218. time Add a time stamp to the task
  2219. note Prompt for a note and add it with template `org-log-note-headings'
  2220. This option can also be set with on a per-file-basis with
  2221. #+STARTUP: nologdone
  2222. #+STARTUP: logdone
  2223. #+STARTUP: lognotedone
  2224. You can have local logging settings for a subtree by setting the LOGGING
  2225. property to one or more of these keywords."
  2226. :group 'org-todo
  2227. :group 'org-progress
  2228. :type '(choice
  2229. (const :tag "No logging" nil)
  2230. (const :tag "Record CLOSED timestamp" time)
  2231. (const :tag "Record CLOSED timestamp with note." note)))
  2232. ;; Normalize old uses of org-log-done.
  2233. (cond
  2234. ((eq org-log-done t) (setq org-log-done 'time))
  2235. ((and (listp org-log-done) (memq 'done org-log-done))
  2236. (setq org-log-done 'note)))
  2237. (defcustom org-log-reschedule nil
  2238. "Information to record when the scheduling date of a tasks is modified.
  2239. Possible values are:
  2240. nil Don't add anything, just change the date
  2241. time Add a time stamp to the task
  2242. note Prompt for a note and add it with template `org-log-note-headings'
  2243. This option can also be set with on a per-file-basis with
  2244. #+STARTUP: nologreschedule
  2245. #+STARTUP: logreschedule
  2246. #+STARTUP: lognotereschedule"
  2247. :group 'org-todo
  2248. :group 'org-progress
  2249. :type '(choice
  2250. (const :tag "No logging" nil)
  2251. (const :tag "Record timestamp" time)
  2252. (const :tag "Record timestamp with note." note)))
  2253. (defcustom org-log-redeadline nil
  2254. "Information to record when the deadline date of a tasks is modified.
  2255. Possible values are:
  2256. nil Don't add anything, just change the date
  2257. time Add a time stamp to the task
  2258. note Prompt for a note and add it with template `org-log-note-headings'
  2259. This option can also be set with on a per-file-basis with
  2260. #+STARTUP: nologredeadline
  2261. #+STARTUP: logredeadline
  2262. #+STARTUP: lognoteredeadline
  2263. You can have local logging settings for a subtree by setting the LOGGING
  2264. property to one or more of these keywords."
  2265. :group 'org-todo
  2266. :group 'org-progress
  2267. :type '(choice
  2268. (const :tag "No logging" nil)
  2269. (const :tag "Record timestamp" time)
  2270. (const :tag "Record timestamp with note." note)))
  2271. (defcustom org-log-note-clock-out nil
  2272. "Non-nil means record a note when clocking out of an item.
  2273. This can also be configured on a per-file basis by adding one of
  2274. the following lines anywhere in the buffer:
  2275. #+STARTUP: lognoteclock-out
  2276. #+STARTUP: nolognoteclock-out"
  2277. :group 'org-todo
  2278. :group 'org-progress
  2279. :type 'boolean)
  2280. (defcustom org-log-done-with-time t
  2281. "Non-nil means the CLOSED time stamp will contain date and time.
  2282. When nil, only the date will be recorded."
  2283. :group 'org-progress
  2284. :type 'boolean)
  2285. (defcustom org-log-note-headings
  2286. '((done . "CLOSING NOTE %t")
  2287. (state . "State %-12s from %-12S %t")
  2288. (note . "Note taken on %t")
  2289. (reschedule . "Rescheduled from %S on %t")
  2290. (delschedule . "Not scheduled, was %S on %t")
  2291. (redeadline . "New deadline from %S on %t")
  2292. (deldeadline . "Removed deadline, was %S on %t")
  2293. (refile . "Refiled on %t")
  2294. (clock-out . ""))
  2295. "Headings for notes added to entries.
  2296. The value is an alist, with the car being a symbol indicating the note
  2297. context, and the cdr is the heading to be used. The heading may also be the
  2298. empty string.
  2299. %t in the heading will be replaced by a time stamp.
  2300. %T will be an active time stamp instead the default inactive one
  2301. %d will be replaced by a short-format time stamp.
  2302. %D will be replaced by an active short-format time stamp.
  2303. %s will be replaced by the new TODO state, in double quotes.
  2304. %S will be replaced by the old TODO state, in double quotes.
  2305. %u will be replaced by the user name.
  2306. %U will be replaced by the full user name.
  2307. In fact, it is not a good idea to change the `state' entry, because
  2308. agenda log mode depends on the format of these entries."
  2309. :group 'org-todo
  2310. :group 'org-progress
  2311. :type '(list :greedy t
  2312. (cons (const :tag "Heading when closing an item" done) string)
  2313. (cons (const :tag
  2314. "Heading when changing todo state (todo sequence only)"
  2315. state) string)
  2316. (cons (const :tag "Heading when just taking a note" note) string)
  2317. (cons (const :tag "Heading when clocking out" clock-out) string)
  2318. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2319. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2320. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2321. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2322. (cons (const :tag "Heading when refiling" refile) string)))
  2323. (unless (assq 'note org-log-note-headings)
  2324. (push '(note . "%t") org-log-note-headings))
  2325. (defcustom org-log-into-drawer nil
  2326. "Non-nil means insert state change notes and time stamps into a drawer.
  2327. When nil, state changes notes will be inserted after the headline and
  2328. any scheduling and clock lines, but not inside a drawer.
  2329. The value of this variable should be the name of the drawer to use.
  2330. LOGBOOK is proposed as the default drawer for this purpose, you can
  2331. also set this to a string to define the drawer of your choice.
  2332. A value of t is also allowed, representing \"LOGBOOK\".
  2333. A value of t or nil can also be set with on a per-file-basis with
  2334. #+STARTUP: logdrawer
  2335. #+STARTUP: nologdrawer
  2336. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2337. will be ignored.
  2338. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2339. a subtree."
  2340. :group 'org-todo
  2341. :group 'org-progress
  2342. :type '(choice
  2343. (const :tag "Not into a drawer" nil)
  2344. (const :tag "LOGBOOK" t)
  2345. (string :tag "Other")))
  2346. (org-defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer)
  2347. (defun org-log-into-drawer ()
  2348. "Return the value of `org-log-into-drawer', but let properties overrule.
  2349. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  2350. used instead of the default value."
  2351. (let ((p (org-entry-get nil "LOG_INTO_DRAWER" 'inherit t)))
  2352. (cond
  2353. ((not p) org-log-into-drawer)
  2354. ((equal p "nil") nil)
  2355. ((equal p "t") "LOGBOOK")
  2356. (t p))))
  2357. (defcustom org-log-state-notes-insert-after-drawers nil
  2358. "Non-nil means insert state change notes after any drawers in entry.
  2359. Only the drawers that *immediately* follow the headline and the
  2360. deadline/scheduled line are skipped.
  2361. When nil, insert notes right after the heading and perhaps the line
  2362. with deadline/scheduling if present.
  2363. This variable will have no effect if `org-log-into-drawer' is
  2364. set."
  2365. :group 'org-todo
  2366. :group 'org-progress
  2367. :type 'boolean)
  2368. (defcustom org-log-states-order-reversed t
  2369. "Non-nil means the latest state note will be directly after heading.
  2370. When nil, the state change notes will be ordered according to time.
  2371. This option can also be set with on a per-file-basis with
  2372. #+STARTUP: logstatesreversed
  2373. #+STARTUP: nologstatesreversed"
  2374. :group 'org-todo
  2375. :group 'org-progress
  2376. :type 'boolean)
  2377. (defcustom org-todo-repeat-to-state nil
  2378. "The TODO state to which a repeater should return the repeating task.
  2379. By default this is the first task in a TODO sequence, or the previous state
  2380. in a TODO_TYP set. But you can specify another task here.
  2381. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2382. :group 'org-todo
  2383. :version "24.1"
  2384. :type '(choice (const :tag "Head of sequence" nil)
  2385. (string :tag "Specific state")))
  2386. (defcustom org-log-repeat 'time
  2387. "Non-nil means record moving through the DONE state when triggering repeat.
  2388. An auto-repeating task is immediately switched back to TODO when
  2389. marked DONE. If you are not logging state changes (by adding \"@\"
  2390. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2391. record a closing note, there will be no record of the task moving
  2392. through DONE. This variable forces taking a note anyway.
  2393. nil Don't force a record
  2394. time Record a time stamp
  2395. note Prompt for a note and add it with template `org-log-note-headings'
  2396. This option can also be set with on a per-file-basis with
  2397. #+STARTUP: nologrepeat
  2398. #+STARTUP: logrepeat
  2399. #+STARTUP: lognoterepeat
  2400. You can have local logging settings for a subtree by setting the LOGGING
  2401. property to one or more of these keywords."
  2402. :group 'org-todo
  2403. :group 'org-progress
  2404. :type '(choice
  2405. (const :tag "Don't force a record" nil)
  2406. (const :tag "Force recording the DONE state" time)
  2407. (const :tag "Force recording a note with the DONE state" note)))
  2408. (defgroup org-priorities nil
  2409. "Priorities in Org-mode."
  2410. :tag "Org Priorities"
  2411. :group 'org-todo)
  2412. (defcustom org-enable-priority-commands t
  2413. "Non-nil means priority commands are active.
  2414. When nil, these commands will be disabled, so that you never accidentally
  2415. set a priority."
  2416. :group 'org-priorities
  2417. :type 'boolean)
  2418. (defcustom org-highest-priority ?A
  2419. "The highest priority of TODO items. A character like ?A, ?B etc.
  2420. Must have a smaller ASCII number than `org-lowest-priority'."
  2421. :group 'org-priorities
  2422. :type 'character)
  2423. (defcustom org-lowest-priority ?C
  2424. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2425. Must have a larger ASCII number than `org-highest-priority'."
  2426. :group 'org-priorities
  2427. :type 'character)
  2428. (defcustom org-default-priority ?B
  2429. "The default priority of TODO items.
  2430. This is the priority an item gets if no explicit priority is given.
  2431. When starting to cycle on an empty priority the first step in the cycle
  2432. depends on `org-priority-start-cycle-with-default'. The resulting first
  2433. step priority must not exceed the range from `org-highest-priority' to
  2434. `org-lowest-priority' which means that `org-default-priority' has to be
  2435. in this range exclusive or inclusive the range boundaries. Else the
  2436. first step refuses to set the default and the second will fall back
  2437. to (depending on the command used) the highest or lowest priority."
  2438. :group 'org-priorities
  2439. :type 'character)
  2440. (defcustom org-priority-start-cycle-with-default t
  2441. "Non-nil means start with default priority when starting to cycle.
  2442. When this is nil, the first step in the cycle will be (depending on the
  2443. command used) one higher or lower than the default priority.
  2444. See also `org-default-priority'."
  2445. :group 'org-priorities
  2446. :type 'boolean)
  2447. (defcustom org-get-priority-function nil
  2448. "Function to extract the priority from a string.
  2449. The string is normally the headline. If this is nil Org computes the
  2450. priority from the priority cookie like [#A] in the headline. It returns
  2451. an integer, increasing by 1000 for each priority level.
  2452. The user can set a different function here, which should take a string
  2453. as an argument and return the numeric priority."
  2454. :group 'org-priorities
  2455. :version "24.1"
  2456. :type 'function)
  2457. (defgroup org-time nil
  2458. "Options concerning time stamps and deadlines in Org-mode."
  2459. :tag "Org Time"
  2460. :group 'org)
  2461. (defcustom org-insert-labeled-timestamps-at-point nil
  2462. "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
  2463. When nil, these labeled time stamps are forces into the second line of an
  2464. entry, just after the headline. When scheduling from the global TODO list,
  2465. the time stamp will always be forced into the second line."
  2466. :group 'org-time
  2467. :type 'boolean)
  2468. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  2469. "Formats for `format-time-string' which are used for time stamps.
  2470. It is not recommended to change this constant.")
  2471. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2472. "Number of minutes to round time stamps to.
  2473. These are two values, the first applies when first creating a time stamp.
  2474. The second applies when changing it with the commands `S-up' and `S-down'.
  2475. When changing the time stamp, this means that it will change in steps
  2476. of N minutes, as given by the second value.
  2477. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2478. numbers should be factors of 60, so for example 5, 10, 15.
  2479. When this is larger than 1, you can still force an exact time stamp by using
  2480. a double prefix argument to a time stamp command like `C-c .' or `C-c !',
  2481. and by using a prefix arg to `S-up/down' to specify the exact number
  2482. of minutes to shift."
  2483. :group 'org-time
  2484. :get #'(lambda (var) ; Make sure both elements are there
  2485. (if (integerp (default-value var))
  2486. (list (default-value var) 5)
  2487. (default-value var)))
  2488. :type '(list
  2489. (integer :tag "when inserting times")
  2490. (integer :tag "when modifying times")))
  2491. ;; Normalize old customizations of this variable.
  2492. (when (integerp org-time-stamp-rounding-minutes)
  2493. (setq org-time-stamp-rounding-minutes
  2494. (list org-time-stamp-rounding-minutes
  2495. org-time-stamp-rounding-minutes)))
  2496. (defcustom org-display-custom-times nil
  2497. "Non-nil means overlay custom formats over all time stamps.
  2498. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2499. To turn this on on a per-file basis, insert anywhere in the file:
  2500. #+STARTUP: customtime"
  2501. :group 'org-time
  2502. :set 'set-default
  2503. :type 'sexp)
  2504. (make-variable-buffer-local 'org-display-custom-times)
  2505. (defcustom org-time-stamp-custom-formats
  2506. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2507. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2508. These are overlaid over the default ISO format if the variable
  2509. `org-display-custom-times' is set. Time like %H:%M should be at the
  2510. end of the second format. The custom formats are also honored by export
  2511. commands, if custom time display is turned on at the time of export."
  2512. :group 'org-time
  2513. :type 'sexp)
  2514. (defun org-time-stamp-format (&optional long inactive)
  2515. "Get the right format for a time string."
  2516. (let ((f (if long (cdr org-time-stamp-formats)
  2517. (car org-time-stamp-formats))))
  2518. (if inactive
  2519. (concat "[" (substring f 1 -1) "]")
  2520. f)))
  2521. (defcustom org-time-clocksum-format
  2522. '(:days "%dd " :hours "%d" :require-hours t :minutes ":%02d" :require-minutes t)
  2523. "The format string used when creating CLOCKSUM lines.
  2524. This is also used when Org mode generates a time duration.
  2525. The value can be a single format string containing two
  2526. %-sequences, which will be filled with the number of hours and
  2527. minutes in that order.
  2528. Alternatively, the value can be a plist associating any of the
  2529. keys :years, :months, :weeks, :days, :hours or :minutes with
  2530. format strings. The time duration is formatted using only the
  2531. time components that are needed and concatenating the results.
  2532. If a time unit in absent, it falls back to the next smallest
  2533. unit.
  2534. The keys :require-years, :require-months, :require-days,
  2535. :require-weeks, :require-hours, :require-minutes are also
  2536. meaningful. A non-nil value for these keys indicates that the
  2537. corresponding time component should always be included, even if
  2538. its value is 0.
  2539. For example,
  2540. \(:days \"%dd\" :hours \"%d\" :require-hours t :minutes \":%02d\"
  2541. :require-minutes t)
  2542. means durations longer than a day will be expressed in days,
  2543. hours and minutes, and durations less than a day will always be
  2544. expressed in hours and minutes (even for durations less than an
  2545. hour).
  2546. The value
  2547. \(:days \"%dd\" :minutes \"%dm\")
  2548. means durations longer than a day will be expressed in days and
  2549. minutes, and durations less than a day will be expressed entirely
  2550. in minutes (even for durations longer than an hour)."
  2551. :group 'org-time
  2552. :group 'org-clock
  2553. :version "24.4"
  2554. :package-version '(Org . "8.0")
  2555. :type '(choice (string :tag "Format string")
  2556. (set :tag "Plist"
  2557. (group :inline t (const :tag "Years" :years)
  2558. (string :tag "Format string"))
  2559. (group :inline t
  2560. (const :tag "Always show years" :require-years)
  2561. (const t))
  2562. (group :inline t (const :tag "Months" :months)
  2563. (string :tag "Format string"))
  2564. (group :inline t
  2565. (const :tag "Always show months" :require-months)
  2566. (const t))
  2567. (group :inline t (const :tag "Weeks" :weeks)
  2568. (string :tag "Format string"))
  2569. (group :inline t
  2570. (const :tag "Always show weeks" :require-weeks)
  2571. (const t))
  2572. (group :inline t (const :tag "Days" :days)
  2573. (string :tag "Format string"))
  2574. (group :inline t
  2575. (const :tag "Always show days" :require-days)
  2576. (const t))
  2577. (group :inline t (const :tag "Hours" :hours)
  2578. (string :tag "Format string"))
  2579. (group :inline t
  2580. (const :tag "Always show hours" :require-hours)
  2581. (const t))
  2582. (group :inline t (const :tag "Minutes" :minutes)
  2583. (string :tag "Format string"))
  2584. (group :inline t
  2585. (const :tag "Always show minutes" :require-minutes)
  2586. (const t)))))
  2587. (defcustom org-time-clocksum-use-fractional nil
  2588. "When non-nil, \\[org-clock-display] uses fractional times.
  2589. See `org-time-clocksum-format' for more on time clock formats."
  2590. :group 'org-time
  2591. :group 'org-clock
  2592. :version "24.3"
  2593. :type 'boolean)
  2594. (defcustom org-time-clocksum-use-effort-durations nil
  2595. "When non-nil, \\[org-clock-display] uses effort durations.
  2596. E.g. by default, one day is considered to be a 8 hours effort,
  2597. so a task that has been clocked for 16 hours will be displayed
  2598. as during 2 days in the clock display or in the clocktable.
  2599. See `org-effort-durations' on how to set effort durations
  2600. and `org-time-clocksum-format' for more on time clock formats."
  2601. :group 'org-time
  2602. :group 'org-clock
  2603. :version "24.4"
  2604. :package-version '(Org . "8.0")
  2605. :type 'boolean)
  2606. (defcustom org-time-clocksum-fractional-format "%.2f"
  2607. "The format string used when creating CLOCKSUM lines,
  2608. or when Org mode generates a time duration, if
  2609. `org-time-clocksum-use-fractional' is enabled.
  2610. The value can be a single format string containing one
  2611. %-sequence, which will be filled with the number of hours as
  2612. a float.
  2613. Alternatively, the value can be a plist associating any of the
  2614. keys :years, :months, :weeks, :days, :hours or :minutes with
  2615. a format string. The time duration is formatted using the
  2616. largest time unit which gives a non-zero integer part. If all
  2617. specified formats have zero integer part, the smallest time unit
  2618. is used."
  2619. :group 'org-time
  2620. :type '(choice (string :tag "Format string")
  2621. (set (group :inline t (const :tag "Years" :years)
  2622. (string :tag "Format string"))
  2623. (group :inline t (const :tag "Months" :months)
  2624. (string :tag "Format string"))
  2625. (group :inline t (const :tag "Weeks" :weeks)
  2626. (string :tag "Format string"))
  2627. (group :inline t (const :tag "Days" :days)
  2628. (string :tag "Format string"))
  2629. (group :inline t (const :tag "Hours" :hours)
  2630. (string :tag "Format string"))
  2631. (group :inline t (const :tag "Minutes" :minutes)
  2632. (string :tag "Format string")))))
  2633. (defcustom org-deadline-warning-days 14
  2634. "Number of days before expiration during which a deadline becomes active.
  2635. This variable governs the display in sparse trees and in the agenda.
  2636. When 0 or negative, it means use this number (the absolute value of it)
  2637. even if a deadline has a different individual lead time specified.
  2638. Custom commands can set this variable in the options section."
  2639. :group 'org-time
  2640. :group 'org-agenda-daily/weekly
  2641. :type 'integer)
  2642. (defcustom org-scheduled-delay-days 0
  2643. "Number of days before a scheduled item becomes active.
  2644. This variable governs the display in sparse trees and in the agenda.
  2645. The default value (i.e. 0) means: don't delay scheduled item.
  2646. When negative, it means use this number (the absolute value of it)
  2647. even if a scheduled item has a different individual delay time
  2648. specified.
  2649. Custom commands can set this variable in the options section."
  2650. :group 'org-time
  2651. :group 'org-agenda-daily/weekly
  2652. :version "24.4"
  2653. :package-version '(Org . "8.0")
  2654. :type 'integer)
  2655. (defcustom org-read-date-prefer-future t
  2656. "Non-nil means assume future for incomplete date input from user.
  2657. This affects the following situations:
  2658. 1. The user gives a month but not a year.
  2659. For example, if it is April and you enter \"feb 2\", this will be read
  2660. as Feb 2, *next* year. \"May 5\", however, will be this year.
  2661. 2. The user gives a day, but no month.
  2662. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2663. read this as the third of *next* month. However, if you enter \"17\",
  2664. it will be considered as *this* month.
  2665. If you set this variable to the symbol `time', then also the following
  2666. will work:
  2667. 3. If the user gives a time.
  2668. If the time is before now, it will be interpreted as tomorrow.
  2669. Currently none of this works for ISO week specifications.
  2670. When this option is nil, the current day, month and year will always be
  2671. used as defaults.
  2672. See also `org-agenda-jump-prefer-future'."
  2673. :group 'org-time
  2674. :type '(choice
  2675. (const :tag "Never" nil)
  2676. (const :tag "Check month and day" t)
  2677. (const :tag "Check month, day, and time" time)))
  2678. (defcustom org-agenda-jump-prefer-future 'org-read-date-prefer-future
  2679. "Should the agenda jump command prefer the future for incomplete dates?
  2680. The default is to do the same as configured in `org-read-date-prefer-future'.
  2681. But you can also set a deviating value here.
  2682. This may t or nil, or the symbol `org-read-date-prefer-future'."
  2683. :group 'org-agenda
  2684. :group 'org-time
  2685. :version "24.1"
  2686. :type '(choice
  2687. (const :tag "Use org-read-date-prefer-future"
  2688. org-read-date-prefer-future)
  2689. (const :tag "Never" nil)
  2690. (const :tag "Always" t)))
  2691. (defcustom org-read-date-force-compatible-dates t
  2692. "Should date/time prompt force dates that are guaranteed to work in Emacs?
  2693. Depending on the system Emacs is running on, certain dates cannot
  2694. be represented with the type used internally to represent time.
  2695. Dates between 1970-1-1 and 2038-1-1 can always be represented
  2696. correctly. Some systems allow for earlier dates, some for later,
  2697. some for both. One way to find out it to insert any date into an
  2698. Org buffer, putting the cursor on the year and hitting S-up and
  2699. S-down to test the range.
  2700. When this variable is set to t, the date/time prompt will not let
  2701. you specify dates outside the 1970-2037 range, so it is certain that
  2702. these dates will work in whatever version of Emacs you are
  2703. running, and also that you can move a file from one Emacs implementation
  2704. to another. WHenever Org is forcing the year for you, it will display
  2705. a message and beep.
  2706. When this variable is nil, Org will check if the date is
  2707. representable in the specific Emacs implementation you are using.
  2708. If not, it will force a year, usually the current year, and beep
  2709. to remind you. Currently this setting is not recommended because
  2710. the likelihood that you will open your Org files in an Emacs that
  2711. has limited date range is not negligible.
  2712. A workaround for this problem is to use diary sexp dates for time
  2713. stamps outside of this range."
  2714. :group 'org-time
  2715. :version "24.1"
  2716. :type 'boolean)
  2717. (defcustom org-read-date-display-live t
  2718. "Non-nil means display current interpretation of date prompt live.
  2719. This display will be in an overlay, in the minibuffer."
  2720. :group 'org-time
  2721. :type 'boolean)
  2722. (defcustom org-read-date-popup-calendar t
  2723. "Non-nil means pop up a calendar when prompting for a date.
  2724. In the calendar, the date can be selected with mouse-1. However, the
  2725. minibuffer will also be active, and you can simply enter the date as well.
  2726. When nil, only the minibuffer will be available."
  2727. :group 'org-time
  2728. :type 'boolean)
  2729. (org-defvaralias 'org-popup-calendar-for-date-prompt
  2730. 'org-read-date-popup-calendar)
  2731. (make-obsolete-variable
  2732. 'org-read-date-minibuffer-setup-hook
  2733. "Set `org-read-date-minibuffer-local-map' instead." "24.4")
  2734. (defcustom org-read-date-minibuffer-setup-hook nil
  2735. "Hook to be used to set up keys for the date/time interface.
  2736. Add key definitions to `minibuffer-local-map', which will be a
  2737. temporary copy.
  2738. WARNING: This option is obsolete, you should use
  2739. `org-read-date-minibuffer-local-map' to set up keys."
  2740. :group 'org-time
  2741. :type 'hook)
  2742. (defcustom org-extend-today-until 0
  2743. "The hour when your day really ends. Must be an integer.
  2744. This has influence for the following applications:
  2745. - When switching the agenda to \"today\". It it is still earlier than
  2746. the time given here, the day recognized as TODAY is actually yesterday.
  2747. - When a date is read from the user and it is still before the time given
  2748. here, the current date and time will be assumed to be yesterday, 23:59.
  2749. Also, timestamps inserted in capture templates follow this rule.
  2750. IMPORTANT: This is a feature whose implementation is and likely will
  2751. remain incomplete. Really, it is only here because past midnight seems to
  2752. be the favorite working time of John Wiegley :-)"
  2753. :group 'org-time
  2754. :type 'integer)
  2755. (defcustom org-use-effective-time nil
  2756. "If non-nil, consider `org-extend-today-until' when creating timestamps.
  2757. For example, if `org-extend-today-until' is 8, and it's 4am, then the
  2758. \"effective time\" of any timestamps between midnight and 8am will be
  2759. 23:59 of the previous day."
  2760. :group 'org-time
  2761. :version "24.1"
  2762. :type 'boolean)
  2763. (defcustom org-use-last-clock-out-time-as-effective-time nil
  2764. "When non-nil, use the last clock out time for `org-todo'.
  2765. Note that this option has precedence over the combined use of
  2766. `org-use-effective-time' and `org-extend-today-until'."
  2767. :group 'org-time
  2768. :version "24.4"
  2769. :package-version '(Org . "8.0")
  2770. :type 'boolean)
  2771. (defcustom org-edit-timestamp-down-means-later nil
  2772. "Non-nil means S-down will increase the time in a time stamp.
  2773. When nil, S-up will increase."
  2774. :group 'org-time
  2775. :type 'boolean)
  2776. (defcustom org-calendar-follow-timestamp-change t
  2777. "Non-nil means make the calendar window follow timestamp changes.
  2778. When a timestamp is modified and the calendar window is visible, it will be
  2779. moved to the new date."
  2780. :group 'org-time
  2781. :type 'boolean)
  2782. (defgroup org-tags nil
  2783. "Options concerning tags in Org-mode."
  2784. :tag "Org Tags"
  2785. :group 'org)
  2786. (defcustom org-tag-alist nil
  2787. "List of tags allowed in Org-mode files.
  2788. When this list is nil, Org-mode will base TAG input on what is already in the
  2789. buffer.
  2790. The value of this variable is an alist, the car of each entry must be a
  2791. keyword as a string, the cdr may be a character that is used to select
  2792. that tag through the fast-tag-selection interface.
  2793. See the manual for details."
  2794. :group 'org-tags
  2795. :type '(repeat
  2796. (choice
  2797. (cons (string :tag "Tag name")
  2798. (character :tag "Access char"))
  2799. (list :tag "Start radio group"
  2800. (const :startgroup)
  2801. (option (string :tag "Group description")))
  2802. (list :tag "Group tags delimiter"
  2803. (const :grouptags))
  2804. (list :tag "End radio group"
  2805. (const :endgroup)
  2806. (option (string :tag "Group description")))
  2807. (const :tag "New line" (:newline)))))
  2808. (defcustom org-tag-persistent-alist nil
  2809. "List of tags that will always appear in all Org-mode files.
  2810. This is in addition to any in buffer settings or customizations
  2811. of `org-tag-alist'.
  2812. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  2813. The value of this variable is an alist, the car of each entry must be a
  2814. keyword as a string, the cdr may be a character that is used to select
  2815. that tag through the fast-tag-selection interface.
  2816. See the manual for details.
  2817. To disable these tags on a per-file basis, insert anywhere in the file:
  2818. #+STARTUP: noptag"
  2819. :group 'org-tags
  2820. :type '(repeat
  2821. (choice
  2822. (cons (string :tag "Tag name")
  2823. (character :tag "Access char"))
  2824. (const :tag "Start radio group" (:startgroup))
  2825. (const :tag "Group tags delimiter" (:grouptags))
  2826. (const :tag "End radio group" (:endgroup))
  2827. (const :tag "New line" (:newline)))))
  2828. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  2829. "If non-nil, always offer completion for all tags of all agenda files.
  2830. Instead of customizing this variable directly, you might want to
  2831. set it locally for capture buffers, because there no list of
  2832. tags in that file can be created dynamically (there are none).
  2833. (add-hook 'org-capture-mode-hook
  2834. (lambda ()
  2835. (set (make-local-variable
  2836. 'org-complete-tags-always-offer-all-agenda-tags)
  2837. t)))"
  2838. :group 'org-tags
  2839. :version "24.1"
  2840. :type 'boolean)
  2841. (defvar org-file-tags nil
  2842. "List of tags that can be inherited by all entries in the file.
  2843. The tags will be inherited if the variable `org-use-tag-inheritance'
  2844. says they should be.
  2845. This variable is populated from #+FILETAGS lines.")
  2846. (defcustom org-use-fast-tag-selection 'auto
  2847. "Non-nil means use fast tag selection scheme.
  2848. This is a special interface to select and deselect tags with single keys.
  2849. When nil, fast selection is never used.
  2850. When the symbol `auto', fast selection is used if and only if selection
  2851. characters for tags have been configured, either through the variable
  2852. `org-tag-alist' or through a #+TAGS line in the buffer.
  2853. When t, fast selection is always used and selection keys are assigned
  2854. automatically if necessary."
  2855. :group 'org-tags
  2856. :type '(choice
  2857. (const :tag "Always" t)
  2858. (const :tag "Never" nil)
  2859. (const :tag "When selection characters are configured" 'auto)))
  2860. (defcustom org-fast-tag-selection-single-key nil
  2861. "Non-nil means fast tag selection exits after first change.
  2862. When nil, you have to press RET to exit it.
  2863. During fast tag selection, you can toggle this flag with `C-c'.
  2864. This variable can also have the value `expert'. In this case, the window
  2865. displaying the tags menu is not even shown, until you press C-c again."
  2866. :group 'org-tags
  2867. :type '(choice
  2868. (const :tag "No" nil)
  2869. (const :tag "Yes" t)
  2870. (const :tag "Expert" expert)))
  2871. (defvar org-fast-tag-selection-include-todo nil
  2872. "Non-nil means fast tags selection interface will also offer TODO states.
  2873. This is an undocumented feature, you should not rely on it.")
  2874. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2875. "The column to which tags should be indented in a headline.
  2876. If this number is positive, it specifies the column. If it is negative,
  2877. it means that the tags should be flushright to that column. For example,
  2878. -80 works well for a normal 80 character screen.
  2879. When 0, place tags directly after headline text, with only one space in
  2880. between."
  2881. :group 'org-tags
  2882. :type 'integer)
  2883. (defcustom org-auto-align-tags t
  2884. "Non-nil keeps tags aligned when modifying headlines.
  2885. Some operations (i.e. demoting) change the length of a headline and
  2886. therefore shift the tags around. With this option turned on, after
  2887. each such operation the tags are again aligned to `org-tags-column'."
  2888. :group 'org-tags
  2889. :type 'boolean)
  2890. (defcustom org-use-tag-inheritance t
  2891. "Non-nil means tags in levels apply also for sublevels.
  2892. When nil, only the tags directly given in a specific line apply there.
  2893. This may also be a list of tags that should be inherited, or a regexp that
  2894. matches tags that should be inherited. Additional control is possible
  2895. with the variable `org-tags-exclude-from-inheritance' which gives an
  2896. explicit list of tags to be excluded from inheritance, even if the value of
  2897. `org-use-tag-inheritance' would select it for inheritance.
  2898. If this option is t, a match early-on in a tree can lead to a large
  2899. number of matches in the subtree when constructing the agenda or creating
  2900. a sparse tree. If you only want to see the first match in a tree during
  2901. a search, check out the variable `org-tags-match-list-sublevels'."
  2902. :group 'org-tags
  2903. :type '(choice
  2904. (const :tag "Not" nil)
  2905. (const :tag "Always" t)
  2906. (repeat :tag "Specific tags" (string :tag "Tag"))
  2907. (regexp :tag "Tags matched by regexp")))
  2908. (defcustom org-tags-exclude-from-inheritance nil
  2909. "List of tags that should never be inherited.
  2910. This is a way to exclude a few tags from inheritance. For way to do
  2911. the opposite, to actively allow inheritance for selected tags,
  2912. see the variable `org-use-tag-inheritance'."
  2913. :group 'org-tags
  2914. :type '(repeat (string :tag "Tag")))
  2915. (defun org-tag-inherit-p (tag)
  2916. "Check if TAG is one that should be inherited."
  2917. (cond
  2918. ((member tag org-tags-exclude-from-inheritance) nil)
  2919. ((eq org-use-tag-inheritance t) t)
  2920. ((not org-use-tag-inheritance) nil)
  2921. ((stringp org-use-tag-inheritance)
  2922. (string-match org-use-tag-inheritance tag))
  2923. ((listp org-use-tag-inheritance)
  2924. (member tag org-use-tag-inheritance))
  2925. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2926. (defcustom org-tags-match-list-sublevels t
  2927. "Non-nil means list also sublevels of headlines matching a search.
  2928. This variable applies to tags/property searches, and also to stuck
  2929. projects because this search is based on a tags match as well.
  2930. When set to the symbol `indented', sublevels are indented with
  2931. leading dots.
  2932. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2933. the sublevels of a headline matching a tag search often also match
  2934. the same search. Listing all of them can create very long lists.
  2935. Setting this variable to nil causes subtrees of a match to be skipped.
  2936. This variable is semi-obsolete and probably should always be true. It
  2937. is better to limit inheritance to certain tags using the variables
  2938. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2939. :group 'org-tags
  2940. :type '(choice
  2941. (const :tag "No, don't list them" nil)
  2942. (const :tag "Yes, do list them" t)
  2943. (const :tag "List them, indented with leading dots" indented)))
  2944. (defcustom org-tags-sort-function nil
  2945. "When set, tags are sorted using this function as a comparator."
  2946. :group 'org-tags
  2947. :type '(choice
  2948. (const :tag "No sorting" nil)
  2949. (const :tag "Alphabetical" string<)
  2950. (const :tag "Reverse alphabetical" string>)
  2951. (function :tag "Custom function" nil)))
  2952. (defvar org-tags-history nil
  2953. "History of minibuffer reads for tags.")
  2954. (defvar org-last-tags-completion-table nil
  2955. "The last used completion table for tags.")
  2956. (defvar org-after-tags-change-hook nil
  2957. "Hook that is run after the tags in a line have changed.")
  2958. (defgroup org-properties nil
  2959. "Options concerning properties in Org-mode."
  2960. :tag "Org Properties"
  2961. :group 'org)
  2962. (defcustom org-property-format "%-10s %s"
  2963. "How property key/value pairs should be formatted by `indent-line'.
  2964. When `indent-line' hits a property definition, it will format the line
  2965. according to this format, mainly to make sure that the values are
  2966. lined-up with respect to each other."
  2967. :group 'org-properties
  2968. :type 'string)
  2969. (defcustom org-properties-postprocess-alist nil
  2970. "Alist of properties and functions to adjust inserted values.
  2971. Elements of this alist must be of the form
  2972. ([string] [function])
  2973. where [string] must be a property name and [function] must be a
  2974. lambda expression: this lambda expression must take one argument,
  2975. the value to adjust, and return the new value as a string.
  2976. For example, this element will allow the property \"Remaining\"
  2977. to be updated wrt the relation between the \"Effort\" property
  2978. and the clock summary:
  2979. ((\"Remaining\" (lambda(value)
  2980. (let ((clocksum (org-clock-sum-current-item))
  2981. (effort (org-duration-string-to-minutes
  2982. (org-entry-get (point) \"Effort\"))))
  2983. (org-minutes-to-clocksum-string (- effort clocksum))))))"
  2984. :group 'org-properties
  2985. :version "24.1"
  2986. :type '(alist :key-type (string :tag "Property")
  2987. :value-type (function :tag "Function")))
  2988. (defcustom org-use-property-inheritance nil
  2989. "Non-nil means properties apply also for sublevels.
  2990. This setting is chiefly used during property searches. Turning it on can
  2991. cause significant overhead when doing a search, which is why it is not
  2992. on by default.
  2993. When nil, only the properties directly given in the current entry count.
  2994. When t, every property is inherited. The value may also be a list of
  2995. properties that should have inheritance, or a regular expression matching
  2996. properties that should be inherited.
  2997. However, note that some special properties use inheritance under special
  2998. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2999. and the properties ending in \"_ALL\" when they are used as descriptor
  3000. for valid values of a property.
  3001. Note for programmers:
  3002. When querying an entry with `org-entry-get', you can control if inheritance
  3003. should be used. By default, `org-entry-get' looks only at the local
  3004. properties. You can request inheritance by setting the inherit argument
  3005. to t (to force inheritance) or to `selective' (to respect the setting
  3006. in this variable)."
  3007. :group 'org-properties
  3008. :type '(choice
  3009. (const :tag "Not" nil)
  3010. (const :tag "Always" t)
  3011. (repeat :tag "Specific properties" (string :tag "Property"))
  3012. (regexp :tag "Properties matched by regexp")))
  3013. (defun org-property-inherit-p (property)
  3014. "Check if PROPERTY is one that should be inherited."
  3015. (cond
  3016. ((eq org-use-property-inheritance t) t)
  3017. ((not org-use-property-inheritance) nil)
  3018. ((stringp org-use-property-inheritance)
  3019. (string-match org-use-property-inheritance property))
  3020. ((listp org-use-property-inheritance)
  3021. (member property org-use-property-inheritance))
  3022. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  3023. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  3024. "The default column format, if no other format has been defined.
  3025. This variable can be set on the per-file basis by inserting a line
  3026. #+COLUMNS: %25ITEM ....."
  3027. :group 'org-properties
  3028. :type 'string)
  3029. (defcustom org-columns-ellipses ".."
  3030. "The ellipses to be used when a field in column view is truncated.
  3031. When this is the empty string, as many characters as possible are shown,
  3032. but then there will be no visual indication that the field has been truncated.
  3033. When this is a string of length N, the last N characters of a truncated
  3034. field are replaced by this string. If the column is narrower than the
  3035. ellipses string, only part of the ellipses string will be shown."
  3036. :group 'org-properties
  3037. :type 'string)
  3038. (defcustom org-columns-modify-value-for-display-function nil
  3039. "Function that modifies values for display in column view.
  3040. For example, it can be used to cut out a certain part from a time stamp.
  3041. The function must take 2 arguments:
  3042. column-title The title of the column (*not* the property name)
  3043. value The value that should be modified.
  3044. The function should return the value that should be displayed,
  3045. or nil if the normal value should be used."
  3046. :group 'org-properties
  3047. :type 'function)
  3048. (defcustom org-effort-property "Effort"
  3049. "The property that is being used to keep track of effort estimates.
  3050. Effort estimates given in this property need to have the format H:MM."
  3051. :group 'org-properties
  3052. :group 'org-progress
  3053. :type '(string :tag "Property"))
  3054. (defconst org-global-properties-fixed
  3055. '(("VISIBILITY_ALL" . "folded children content all")
  3056. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  3057. "List of property/value pairs that can be inherited by any entry.
  3058. These are fixed values, for the preset properties. The user variable
  3059. that can be used to add to this list is `org-global-properties'.
  3060. The entries in this list are cons cells where the car is a property
  3061. name and cdr is a string with the value. If the value represents
  3062. multiple items like an \"_ALL\" property, separate the items by
  3063. spaces.")
  3064. (defcustom org-global-properties nil
  3065. "List of property/value pairs that can be inherited by any entry.
  3066. This list will be combined with the constant `org-global-properties-fixed'.
  3067. The entries in this list are cons cells where the car is a property
  3068. name and cdr is a string with the value.
  3069. You can set buffer-local values for the same purpose in the variable
  3070. `org-file-properties' this by adding lines like
  3071. #+PROPERTY: NAME VALUE"
  3072. :group 'org-properties
  3073. :type '(repeat
  3074. (cons (string :tag "Property")
  3075. (string :tag "Value"))))
  3076. (defvar org-file-properties nil
  3077. "List of property/value pairs that can be inherited by any entry.
  3078. Valid for the current buffer.
  3079. This variable is populated from #+PROPERTY lines.")
  3080. (make-variable-buffer-local 'org-file-properties)
  3081. (defgroup org-agenda nil
  3082. "Options concerning agenda views in Org-mode."
  3083. :tag "Org Agenda"
  3084. :group 'org)
  3085. (defvar org-category nil
  3086. "Variable used by org files to set a category for agenda display.
  3087. Such files should use a file variable to set it, for example
  3088. # -*- mode: org; org-category: \"ELisp\"
  3089. or contain a special line
  3090. #+CATEGORY: ELisp
  3091. If the file does not specify a category, then file's base name
  3092. is used instead.")
  3093. (make-variable-buffer-local 'org-category)
  3094. (put 'org-category 'safe-local-variable #'(lambda (x) (or (symbolp x) (stringp x))))
  3095. (defcustom org-agenda-files nil
  3096. "The files to be used for agenda display.
  3097. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  3098. \\[org-remove-file]. You can also use customize to edit the list.
  3099. If an entry is a directory, all files in that directory that are matched by
  3100. `org-agenda-file-regexp' will be part of the file list.
  3101. If the value of the variable is not a list but a single file name, then
  3102. the list of agenda files is actually stored and maintained in that file, one
  3103. agenda file per line. In this file paths can be given relative to
  3104. `org-directory'. Tilde expansion and environment variable substitution
  3105. are also made."
  3106. :group 'org-agenda
  3107. :type '(choice
  3108. (repeat :tag "List of files and directories" file)
  3109. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  3110. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  3111. "Regular expression to match files for `org-agenda-files'.
  3112. If any element in the list in that variable contains a directory instead
  3113. of a normal file, all files in that directory that are matched by this
  3114. regular expression will be included."
  3115. :group 'org-agenda
  3116. :type 'regexp)
  3117. (defcustom org-agenda-text-search-extra-files nil
  3118. "List of extra files to be searched by text search commands.
  3119. These files will be search in addition to the agenda files by the
  3120. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  3121. Note that these files will only be searched for text search commands,
  3122. not for the other agenda views like todo lists, tag searches or the weekly
  3123. agenda. This variable is intended to list notes and possibly archive files
  3124. that should also be searched by these two commands.
  3125. In fact, if the first element in the list is the symbol `agenda-archives',
  3126. than all archive files of all agenda files will be added to the search
  3127. scope."
  3128. :group 'org-agenda
  3129. :type '(set :greedy t
  3130. (const :tag "Agenda Archives" agenda-archives)
  3131. (repeat :inline t (file))))
  3132. (org-defvaralias 'org-agenda-multi-occur-extra-files
  3133. 'org-agenda-text-search-extra-files)
  3134. (defcustom org-agenda-skip-unavailable-files nil
  3135. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  3136. A nil value means to remove them, after a query, from the list."
  3137. :group 'org-agenda
  3138. :type 'boolean)
  3139. (defcustom org-calendar-to-agenda-key [?c]
  3140. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  3141. The command `org-calendar-goto-agenda' will be bound to this key. The
  3142. default is the character `c' because then `c' can be used to switch back and
  3143. forth between agenda and calendar."
  3144. :group 'org-agenda
  3145. :type 'sexp)
  3146. (defcustom org-calendar-insert-diary-entry-key [?i]
  3147. "The key to be installed in `calendar-mode-map' for adding diary entries.
  3148. This option is irrelevant until `org-agenda-diary-file' has been configured
  3149. to point to an Org-mode file. When that is the case, the command
  3150. `org-agenda-diary-entry' will be bound to the key given here, by default
  3151. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  3152. if you want to continue doing this, you need to change this to a different
  3153. key."
  3154. :group 'org-agenda
  3155. :type 'sexp)
  3156. (defcustom org-agenda-diary-file 'diary-file
  3157. "File to which to add new entries with the `i' key in agenda and calendar.
  3158. When this is the symbol `diary-file', the functionality in the Emacs
  3159. calendar will be used to add entries to the `diary-file'. But when this
  3160. points to a file, `org-agenda-diary-entry' will be used instead."
  3161. :group 'org-agenda
  3162. :type '(choice
  3163. (const :tag "The standard Emacs diary file" diary-file)
  3164. (file :tag "Special Org file diary entries")))
  3165. (eval-after-load "calendar"
  3166. '(progn
  3167. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  3168. 'org-calendar-goto-agenda)
  3169. (add-hook 'calendar-mode-hook
  3170. (lambda ()
  3171. (unless (eq org-agenda-diary-file 'diary-file)
  3172. (define-key calendar-mode-map
  3173. org-calendar-insert-diary-entry-key
  3174. 'org-agenda-diary-entry))))))
  3175. (defgroup org-latex nil
  3176. "Options for embedding LaTeX code into Org-mode."
  3177. :tag "Org LaTeX"
  3178. :group 'org)
  3179. (defcustom org-format-latex-options
  3180. '(:foreground default :background default :scale 1.0
  3181. :html-foreground "Black" :html-background "Transparent"
  3182. :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  3183. "Options for creating images from LaTeX fragments.
  3184. This is a property list with the following properties:
  3185. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  3186. `default' means use the foreground of the default face.
  3187. `auto' means use the foreground from the text face.
  3188. :background the background color, or \"Transparent\".
  3189. `default' means use the background of the default face.
  3190. `auto' means use the background from the text face.
  3191. :scale a scaling factor for the size of the images, to get more pixels
  3192. :html-foreground, :html-background, :html-scale
  3193. the same numbers for HTML export.
  3194. :matchers a list indicating which matchers should be used to
  3195. find LaTeX fragments. Valid members of this list are:
  3196. \"begin\" find environments
  3197. \"$1\" find single characters surrounded by $.$
  3198. \"$\" find math expressions surrounded by $...$
  3199. \"$$\" find math expressions surrounded by $$....$$
  3200. \"\\(\" find math expressions surrounded by \\(...\\)
  3201. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  3202. :group 'org-latex
  3203. :type 'plist)
  3204. (defcustom org-format-latex-signal-error t
  3205. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  3206. When nil, just push out a message."
  3207. :group 'org-latex
  3208. :version "24.1"
  3209. :type 'boolean)
  3210. (defcustom org-latex-to-mathml-jar-file nil
  3211. "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
  3212. Use this to specify additional executable file say a jar file.
  3213. When using MathToWeb as the converter, specify the full-path to
  3214. your mathtoweb.jar file."
  3215. :group 'org-latex
  3216. :version "24.1"
  3217. :type '(choice
  3218. (const :tag "None" nil)
  3219. (file :tag "JAR file" :must-match t)))
  3220. (defcustom org-latex-to-mathml-convert-command nil
  3221. "Command to convert LaTeX fragments to MathML.
  3222. Replace format-specifiers in the command as noted below and use
  3223. `shell-command' to convert LaTeX to MathML.
  3224. %j: Executable file in fully expanded form as specified by
  3225. `org-latex-to-mathml-jar-file'.
  3226. %I: Input LaTeX file in fully expanded form
  3227. %o: Output MathML file
  3228. This command is used by `org-create-math-formula'.
  3229. When using MathToWeb as the converter, set this to
  3230. \"java -jar %j -unicode -force -df %o %I\"."
  3231. :group 'org-latex
  3232. :version "24.1"
  3233. :type '(choice
  3234. (const :tag "None" nil)
  3235. (string :tag "\nShell command")))
  3236. (defcustom org-latex-create-formula-image-program 'dvipng
  3237. "Program to convert LaTeX fragments with.
  3238. dvipng Process the LaTeX fragments to dvi file, then convert
  3239. dvi files to png files using dvipng.
  3240. This will also include processing of non-math environments.
  3241. imagemagick Convert the LaTeX fragments to pdf files and use imagemagick
  3242. to convert pdf files to png files"
  3243. :group 'org-latex
  3244. :version "24.1"
  3245. :type '(choice
  3246. (const :tag "dvipng" dvipng)
  3247. (const :tag "imagemagick" imagemagick)))
  3248. (defcustom org-latex-preview-ltxpng-directory "ltxpng/"
  3249. "Path to store latex preview images.
  3250. A relative path here creates many directories relative to the
  3251. processed org files paths. An absolute path puts all preview
  3252. images at the same place."
  3253. :group 'org-latex
  3254. :version "24.3"
  3255. :type 'string)
  3256. (defun org-format-latex-mathml-available-p ()
  3257. "Return t if `org-latex-to-mathml-convert-command' is usable."
  3258. (save-match-data
  3259. (when (and (boundp 'org-latex-to-mathml-convert-command)
  3260. org-latex-to-mathml-convert-command)
  3261. (let ((executable (car (split-string
  3262. org-latex-to-mathml-convert-command))))
  3263. (when (executable-find executable)
  3264. (if (string-match
  3265. "%j" org-latex-to-mathml-convert-command)
  3266. (file-readable-p org-latex-to-mathml-jar-file)
  3267. t))))))
  3268. (defcustom org-format-latex-header "\\documentclass{article}
  3269. \\usepackage[usenames]{color}
  3270. \\usepackage{amsmath}
  3271. \\usepackage[mathscr]{eucal}
  3272. \\pagestyle{empty} % do not remove
  3273. \[PACKAGES]
  3274. \[DEFAULT-PACKAGES]
  3275. % The settings below are copied from fullpage.sty
  3276. \\setlength{\\textwidth}{\\paperwidth}
  3277. \\addtolength{\\textwidth}{-3cm}
  3278. \\setlength{\\oddsidemargin}{1.5cm}
  3279. \\addtolength{\\oddsidemargin}{-2.54cm}
  3280. \\setlength{\\evensidemargin}{\\oddsidemargin}
  3281. \\setlength{\\textheight}{\\paperheight}
  3282. \\addtolength{\\textheight}{-\\headheight}
  3283. \\addtolength{\\textheight}{-\\headsep}
  3284. \\addtolength{\\textheight}{-\\footskip}
  3285. \\addtolength{\\textheight}{-3cm}
  3286. \\setlength{\\topmargin}{1.5cm}
  3287. \\addtolength{\\topmargin}{-2.54cm}"
  3288. "The document header used for processing LaTeX fragments.
  3289. It is imperative that this header make sure that no page number
  3290. appears on the page. The package defined in the variables
  3291. `org-latex-default-packages-alist' and `org-latex-packages-alist'
  3292. will either replace the placeholder \"[PACKAGES]\" in this
  3293. header, or they will be appended."
  3294. :group 'org-latex
  3295. :type 'string)
  3296. (defun org-set-packages-alist (var val)
  3297. "Set the packages alist and make sure it has 3 elements per entry."
  3298. (set var (mapcar (lambda (x)
  3299. (if (and (consp x) (= (length x) 2))
  3300. (list (car x) (nth 1 x) t)
  3301. x))
  3302. val)))
  3303. (defun org-get-packages-alist (var)
  3304. "Get the packages alist and make sure it has 3 elements per entry."
  3305. (mapcar (lambda (x)
  3306. (if (and (consp x) (= (length x) 2))
  3307. (list (car x) (nth 1 x) t)
  3308. x))
  3309. (default-value var)))
  3310. (defcustom org-latex-default-packages-alist
  3311. '(("AUTO" "inputenc" t)
  3312. ("T1" "fontenc" t)
  3313. ("" "fixltx2e" nil)
  3314. ("" "graphicx" t)
  3315. ("" "longtable" nil)
  3316. ("" "float" nil)
  3317. ("" "wrapfig" nil)
  3318. ("" "soul" t)
  3319. ("" "textcomp" t)
  3320. ("" "marvosym" t)
  3321. ("" "wasysym" t)
  3322. ("" "latexsym" t)
  3323. ("" "amssymb" t)
  3324. ("" "hyperref" nil)
  3325. "\\tolerance=1000")
  3326. "Alist of default packages to be inserted in the header.
  3327. Change this only if one of the packages here causes an
  3328. incompatibility with another package you are using.
  3329. The packages in this list are needed by one part or another of
  3330. Org mode to function properly:
  3331. - inputenc, fontenc: for basic font and character selection
  3332. - textcomp, marvosymb, wasysym, latexsym, amssym: for various
  3333. symbols used for interpreting the entities in `org-entities'.
  3334. You can skip some of these packages if you don't use any of the
  3335. symbols in it.
  3336. - graphicx: for including images
  3337. - float, wrapfig: for figure placement
  3338. - longtable: for long tables
  3339. - hyperref: for cross references
  3340. Therefore you should not modify this variable unless you know
  3341. what you are doing. The one reason to change it anyway is that
  3342. you might be loading some other package that conflicts with one
  3343. of the default packages. Each cell is of the format
  3344. \( \"options\" \"package\" snippet-flag). If SNIPPET-FLAG is t,
  3345. the package also needs to be included when compiling LaTeX
  3346. snippets into images for inclusion into non-LaTeX output."
  3347. :group 'org-latex
  3348. :group 'org-export-latex
  3349. :set 'org-set-packages-alist
  3350. :get 'org-get-packages-alist
  3351. :version "24.1"
  3352. :type '(repeat
  3353. (choice
  3354. (list :tag "options/package pair"
  3355. (string :tag "options")
  3356. (string :tag "package")
  3357. (boolean :tag "Snippet"))
  3358. (string :tag "A line of LaTeX"))))
  3359. (defcustom org-latex-packages-alist nil
  3360. "Alist of packages to be inserted in every LaTeX header.
  3361. These will be inserted after `org-latex-default-packages-alist'.
  3362. Each cell is of the format:
  3363. \(\"options\" \"package\" snippet-flag)
  3364. SNIPPET-FLAG, when t, indicates that this package is also needed
  3365. when turning LaTeX snippets into images for inclusion into
  3366. non-LaTeX output.
  3367. Make sure that you only list packages here which:
  3368. - you want in every file
  3369. - do not conflict with the setup in `org-format-latex-header'.
  3370. - do not conflict with the default packages in
  3371. `org-latex-default-packages-alist'."
  3372. :group 'org-latex
  3373. :group 'org-export-latex
  3374. :set 'org-set-packages-alist
  3375. :get 'org-get-packages-alist
  3376. :type '(repeat
  3377. (choice
  3378. (list :tag "options/package pair"
  3379. (string :tag "options")
  3380. (string :tag "package")
  3381. (boolean :tag "Snippet"))
  3382. (string :tag "A line of LaTeX"))))
  3383. (defgroup org-appearance nil
  3384. "Settings for Org-mode appearance."
  3385. :tag "Org Appearance"
  3386. :group 'org)
  3387. (defcustom org-level-color-stars-only nil
  3388. "Non-nil means fontify only the stars in each headline.
  3389. When nil, the entire headline is fontified.
  3390. Changing it requires restart of `font-lock-mode' to become effective
  3391. also in regions already fontified."
  3392. :group 'org-appearance
  3393. :type 'boolean)
  3394. (defcustom org-hide-leading-stars nil
  3395. "Non-nil means hide the first N-1 stars in a headline.
  3396. This works by using the face `org-hide' for these stars. This
  3397. face is white for a light background, and black for a dark
  3398. background. You may have to customize the face `org-hide' to
  3399. make this work.
  3400. Changing it requires restart of `font-lock-mode' to become effective
  3401. also in regions already fontified.
  3402. You may also set this on a per-file basis by adding one of the following
  3403. lines to the buffer:
  3404. #+STARTUP: hidestars
  3405. #+STARTUP: showstars"
  3406. :group 'org-appearance
  3407. :type 'boolean)
  3408. (defcustom org-hidden-keywords nil
  3409. "List of symbols corresponding to keywords to be hidden the org buffer.
  3410. For example, a value '(title) for this list will make the document's title
  3411. appear in the buffer without the initial #+TITLE: keyword."
  3412. :group 'org-appearance
  3413. :version "24.1"
  3414. :type '(set (const :tag "#+AUTHOR" author)
  3415. (const :tag "#+DATE" date)
  3416. (const :tag "#+EMAIL" email)
  3417. (const :tag "#+TITLE" title)))
  3418. (defcustom org-custom-properties nil
  3419. "List of properties (as strings) with a special meaning.
  3420. The default use of these custom properties is to let the user
  3421. hide them with `org-toggle-custom-properties-visibility'."
  3422. :group 'org-properties
  3423. :group 'org-appearance
  3424. :version "24.3"
  3425. :type '(repeat (string :tag "Property Name")))
  3426. (defcustom org-fontify-done-headline nil
  3427. "Non-nil means change the face of a headline if it is marked DONE.
  3428. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3429. When this is non-nil, the headline after the keyword is set to the
  3430. `org-headline-done' as an additional indication."
  3431. :group 'org-appearance
  3432. :type 'boolean)
  3433. (defcustom org-fontify-emphasized-text t
  3434. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3435. Changing this variable requires a restart of Emacs to take effect."
  3436. :group 'org-appearance
  3437. :type 'boolean)
  3438. (defcustom org-fontify-whole-heading-line nil
  3439. "Non-nil means fontify the whole line for headings.
  3440. This is useful when setting a background color for the
  3441. org-level-* faces."
  3442. :group 'org-appearance
  3443. :type 'boolean)
  3444. (defcustom org-highlight-latex-and-related nil
  3445. "Non-nil means highlight LaTeX related syntax in the buffer.
  3446. When non nil, the value should be a list containing any of the
  3447. following symbols:
  3448. `latex' Highlight LaTeX snippets and environments.
  3449. `script' Highlight subscript and superscript.
  3450. `entities' Highlight entities."
  3451. :group 'org-appearance
  3452. :version "24.4"
  3453. :package-version '(Org . "8.0")
  3454. :type '(choice
  3455. (const :tag "No highlighting" nil)
  3456. (set :greedy t :tag "Highlight"
  3457. (const :tag "LaTeX snippets and environments" latex)
  3458. (const :tag "Subscript and superscript" script)
  3459. (const :tag "Entities" entities))))
  3460. (defcustom org-hide-emphasis-markers nil
  3461. "Non-nil mean font-lock should hide the emphasis marker characters."
  3462. :group 'org-appearance
  3463. :type 'boolean)
  3464. (defcustom org-pretty-entities nil
  3465. "Non-nil means show entities as UTF8 characters.
  3466. When nil, the \\name form remains in the buffer."
  3467. :group 'org-appearance
  3468. :version "24.1"
  3469. :type 'boolean)
  3470. (defcustom org-pretty-entities-include-sub-superscripts t
  3471. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  3472. :group 'org-appearance
  3473. :version "24.1"
  3474. :type 'boolean)
  3475. (defvar org-emph-re nil
  3476. "Regular expression for matching emphasis.
  3477. After a match, the match groups contain these elements:
  3478. 0 The match of the full regular expression, including the characters
  3479. before and after the proper match
  3480. 1 The character before the proper match, or empty at beginning of line
  3481. 2 The proper match, including the leading and trailing markers
  3482. 3 The leading marker like * or /, indicating the type of highlighting
  3483. 4 The text between the emphasis markers, not including the markers
  3484. 5 The character after the match, empty at the end of a line")
  3485. (defvar org-verbatim-re nil
  3486. "Regular expression for matching verbatim text.")
  3487. (defvar org-emphasis-regexp-components) ; defined just below
  3488. (defvar org-emphasis-alist) ; defined just below
  3489. (defun org-set-emph-re (var val)
  3490. "Set variable and compute the emphasis regular expression."
  3491. (set var val)
  3492. (when (and (boundp 'org-emphasis-alist)
  3493. (boundp 'org-emphasis-regexp-components)
  3494. org-emphasis-alist org-emphasis-regexp-components)
  3495. (let* ((e org-emphasis-regexp-components)
  3496. (pre (car e))
  3497. (post (nth 1 e))
  3498. (border (nth 2 e))
  3499. (body (nth 3 e))
  3500. (nl (nth 4 e))
  3501. (body1 (concat body "*?"))
  3502. (markers (mapconcat 'car org-emphasis-alist ""))
  3503. (vmarkers (mapconcat
  3504. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  3505. org-emphasis-alist "")))
  3506. ;; make sure special characters appear at the right position in the class
  3507. (if (string-match "\\^" markers)
  3508. (setq markers (concat (replace-match "" t t markers) "^")))
  3509. (if (string-match "-" markers)
  3510. (setq markers (concat (replace-match "" t t markers) "-")))
  3511. (if (string-match "\\^" vmarkers)
  3512. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3513. (if (string-match "-" vmarkers)
  3514. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3515. (if (> nl 0)
  3516. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3517. (int-to-string nl) "\\}")))
  3518. ;; Make the regexp
  3519. (setq org-emph-re
  3520. (concat "\\([" pre "]\\|^\\)"
  3521. "\\("
  3522. "\\([" markers "]\\)"
  3523. "\\("
  3524. "[^" border "]\\|"
  3525. "[^" border "]"
  3526. body1
  3527. "[^" border "]"
  3528. "\\)"
  3529. "\\3\\)"
  3530. "\\([" post "]\\|$\\)"))
  3531. (setq org-verbatim-re
  3532. (concat "\\([" pre "]\\|^\\)"
  3533. "\\("
  3534. "\\([" vmarkers "]\\)"
  3535. "\\("
  3536. "[^" border "]\\|"
  3537. "[^" border "]"
  3538. body1
  3539. "[^" border "]"
  3540. "\\)"
  3541. "\\3\\)"
  3542. "\\([" post "]\\|$\\)")))))
  3543. (defcustom org-emphasis-regexp-components
  3544. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  3545. "Components used to build the regular expression for emphasis.
  3546. This is a list with five entries. Terminology: In an emphasis string
  3547. like \" *strong word* \", we call the initial space PREMATCH, the final
  3548. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3549. and \"trong wor\" is the body. The different components in this variable
  3550. specify what is allowed/forbidden in each part:
  3551. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3552. post Chars allowed as postmatch. End of line will be allowed too.
  3553. border The chars *forbidden* as border characters.
  3554. body-regexp A regexp like \".\" to match a body character. Don't use
  3555. non-shy groups here, and don't allow newline here.
  3556. newline The maximum number of newlines allowed in an emphasis exp.
  3557. Use customize to modify this, or restart Emacs after changing it."
  3558. :group 'org-appearance
  3559. :set 'org-set-emph-re
  3560. :type '(list
  3561. (sexp :tag "Allowed chars in pre ")
  3562. (sexp :tag "Allowed chars in post ")
  3563. (sexp :tag "Forbidden chars in border ")
  3564. (sexp :tag "Regexp for body ")
  3565. (integer :tag "number of newlines allowed")
  3566. (option (boolean :tag "Please ignore this button"))))
  3567. (defcustom org-emphasis-alist
  3568. `(("*" bold "<b>" "</b>")
  3569. ("/" italic "<i>" "</i>")
  3570. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  3571. ("=" org-code "<code>" "</code>" verbatim)
  3572. ("~" org-verbatim "<code>" "</code>" verbatim)
  3573. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  3574. "<del>" "</del>")
  3575. )
  3576. "Special syntax for emphasized text.
  3577. Text starting and ending with a special character will be emphasized, for
  3578. example *bold*, _underlined_ and /italic/. This variable sets the marker
  3579. characters, the face to be used by font-lock for highlighting in Org-mode
  3580. Emacs buffers, and the HTML tags to be used for this.
  3581. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  3582. Use customize to modify this, or restart Emacs after changing it."
  3583. :group 'org-appearance
  3584. :set 'org-set-emph-re
  3585. :type '(repeat
  3586. (list
  3587. (string :tag "Marker character")
  3588. (choice
  3589. (face :tag "Font-lock-face")
  3590. (plist :tag "Face property list"))
  3591. (string :tag "HTML start tag")
  3592. (string :tag "HTML end tag")
  3593. (option (const verbatim)))))
  3594. (defvar org-protecting-blocks
  3595. '("src" "example" "latex" "ascii" "html" "ditaa" "dot" "r" "R")
  3596. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3597. This is needed for font-lock setup.")
  3598. ;;; Miscellaneous options
  3599. (defgroup org-completion nil
  3600. "Completion in Org-mode."
  3601. :tag "Org Completion"
  3602. :group 'org)
  3603. (defcustom org-completion-use-ido nil
  3604. "Non-nil means use ido completion wherever possible.
  3605. Note that `ido-mode' must be active for this variable to be relevant.
  3606. If you decide to turn this variable on, you might well want to turn off
  3607. `org-outline-path-complete-in-steps'.
  3608. See also `org-completion-use-iswitchb'."
  3609. :group 'org-completion
  3610. :type 'boolean)
  3611. (defcustom org-completion-use-iswitchb nil
  3612. "Non-nil means use iswitchb completion wherever possible.
  3613. Note that `iswitchb-mode' must be active for this variable to be relevant.
  3614. If you decide to turn this variable on, you might well want to turn off
  3615. `org-outline-path-complete-in-steps'.
  3616. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  3617. :group 'org-completion
  3618. :type 'boolean)
  3619. (defcustom org-completion-fallback-command 'hippie-expand
  3620. "The expansion command called by \\[pcomplete] in normal context.
  3621. Normal means, no org-mode-specific context."
  3622. :group 'org-completion
  3623. :type 'function)
  3624. ;;; Functions and variables from their packages
  3625. ;; Declared here to avoid compiler warnings
  3626. ;; XEmacs only
  3627. (defvar outline-mode-menu-heading)
  3628. (defvar outline-mode-menu-show)
  3629. (defvar outline-mode-menu-hide)
  3630. (defvar zmacs-regions) ; XEmacs regions
  3631. ;; Emacs only
  3632. (defvar mark-active)
  3633. ;; Various packages
  3634. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  3635. (declare-function calendar-forward-day "cal-move" (arg))
  3636. (declare-function calendar-goto-date "cal-move" (date))
  3637. (declare-function calendar-goto-today "cal-move" ())
  3638. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3639. (defvar calc-embedded-close-formula)
  3640. (defvar calc-embedded-open-formula)
  3641. (declare-function cdlatex-tab "ext:cdlatex" ())
  3642. (declare-function cdlatex-compute-tables "ext:cdlatex" ())
  3643. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3644. (defvar font-lock-unfontify-region-function)
  3645. (declare-function iswitchb-read-buffer "iswitchb"
  3646. (prompt &optional default require-match start matches-set))
  3647. (defvar iswitchb-temp-buflist)
  3648. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3649. (defvar org-agenda-tags-todo-honor-ignore-options)
  3650. (declare-function org-agenda-skip "org-agenda" ())
  3651. (declare-function
  3652. org-agenda-format-item "org-agenda"
  3653. (extra txt &optional level category tags dotime noprefix remove-re habitp))
  3654. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3655. (declare-function org-agenda-change-all-lines "org-agenda"
  3656. (newhead hdmarker &optional fixface just-this))
  3657. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3658. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3659. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  3660. (beg end))
  3661. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3662. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3663. "org-agenda" (&optional end))
  3664. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3665. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  3666. (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
  3667. (declare-function org-inlinetask-goto-end "org-inlinetask" ())
  3668. (declare-function org-indent-mode "org-indent" (&optional arg))
  3669. (declare-function parse-time-string "parse-time" (string))
  3670. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3671. (declare-function orgtbl-send-table "org-table" (&optional maybe))
  3672. (defvar remember-data-file)
  3673. (defvar texmathp-why)
  3674. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3675. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3676. (defvar org-latex-regexps)
  3677. ;;; Autoload and prepare some org modules
  3678. ;; Some table stuff that needs to be defined here, because it is used
  3679. ;; by the functions setting up org-mode or checking for table context.
  3680. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  3681. "Detect an org-type or table-type table.")
  3682. (defconst org-table-line-regexp "^[ \t]*|"
  3683. "Detect an org-type table line.")
  3684. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  3685. "Detect an org-type table line.")
  3686. (defconst org-table-hline-regexp "^[ \t]*|-"
  3687. "Detect an org-type table hline.")
  3688. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  3689. "Detect a table-type table hline.")
  3690. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  3691. "Detect the first line outside a table when searching from within it.
  3692. This works for both table types.")
  3693. ;; Autoload the functions in org-table.el that are needed by functions here.
  3694. (eval-and-compile
  3695. (org-autoload "org-table"
  3696. '(org-table-begin org-table-blank-field org-table-end)))
  3697. (defconst org-TBLFM-regexp "^[ \t]*#\\+TBLFM: "
  3698. "Detect a #+TBLFM line.")
  3699. ;;;###autoload
  3700. (defun turn-on-orgtbl ()
  3701. "Unconditionally turn on `orgtbl-mode'."
  3702. (require 'org-table)
  3703. (orgtbl-mode 1))
  3704. (defun org-at-table-p (&optional table-type)
  3705. "Return t if the cursor is inside an org-type table.
  3706. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  3707. (if org-enable-table-editor
  3708. (save-excursion
  3709. (beginning-of-line 1)
  3710. (looking-at (if table-type org-table-any-line-regexp
  3711. org-table-line-regexp)))
  3712. nil))
  3713. (defsubst org-table-p () (org-at-table-p))
  3714. (defun org-at-table.el-p ()
  3715. "Return t if and only if we are at a table.el table."
  3716. (and (org-at-table-p 'any)
  3717. (save-excursion
  3718. (goto-char (org-table-begin 'any))
  3719. (looking-at org-table1-hline-regexp))))
  3720. (defun org-table-recognize-table.el ()
  3721. "If there is a table.el table nearby, recognize it and move into it."
  3722. (if org-table-tab-recognizes-table.el
  3723. (if (org-at-table.el-p)
  3724. (progn
  3725. (beginning-of-line 1)
  3726. (if (looking-at org-table-dataline-regexp)
  3727. nil
  3728. (if (looking-at org-table1-hline-regexp)
  3729. (progn
  3730. (beginning-of-line 2)
  3731. (if (looking-at org-table-any-border-regexp)
  3732. (beginning-of-line -1)))))
  3733. (if (re-search-forward "|" (org-table-end t) t)
  3734. (progn
  3735. (require 'table)
  3736. (if (table--at-cell-p (point))
  3737. t
  3738. (message "recognizing table.el table...")
  3739. (table-recognize-table)
  3740. (message "recognizing table.el table...done")))
  3741. (error "This should not happen"))
  3742. t)
  3743. nil)
  3744. nil))
  3745. (defun org-at-table-hline-p ()
  3746. "Return t if the cursor is inside a hline in a table."
  3747. (if org-enable-table-editor
  3748. (save-excursion
  3749. (beginning-of-line 1)
  3750. (looking-at org-table-hline-regexp))
  3751. nil))
  3752. (defvar org-table-clean-did-remove-column nil)
  3753. (defun org-table-map-tables (function &optional quietly)
  3754. "Apply FUNCTION to the start of all tables in the buffer."
  3755. (save-excursion
  3756. (save-restriction
  3757. (widen)
  3758. (goto-char (point-min))
  3759. (while (re-search-forward org-table-any-line-regexp nil t)
  3760. (unless quietly
  3761. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
  3762. (beginning-of-line 1)
  3763. (when (and (looking-at org-table-line-regexp)
  3764. ;; Exclude tables in src/example/verbatim/clocktable blocks
  3765. (not (org-in-block-p '("src" "example" "verbatim" "clocktable"))))
  3766. (save-excursion (funcall function))
  3767. (or (looking-at org-table-line-regexp)
  3768. (forward-char 1)))
  3769. (re-search-forward org-table-any-border-regexp nil 1))))
  3770. (unless quietly (message "Mapping tables: done")))
  3771. ;; Declare and autoload functions from ox.el and al.
  3772. (declare-function org-export-get-environment "ox"
  3773. (&optional backend subtreep ext-plist))
  3774. (declare-function org-latex-guess-inputenc "ox-latex" (header))
  3775. ;; Declare and autoload functions from org-agenda.el
  3776. (eval-and-compile
  3777. (org-autoload "org-agenda"
  3778. '(org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  3779. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock" (beg end))
  3780. (declare-function org-clock-update-mode-line "org-clock" ())
  3781. (declare-function org-resolve-clocks "org-clock"
  3782. (&optional also-non-dangling-p prompt last-valid))
  3783. (defun org-at-TBLFM-p (&optional pos)
  3784. "Return t when point (or POS) is in #+TBLFM line."
  3785. (save-excursion
  3786. (let ((pos pos)))
  3787. (goto-char (or pos (point)))
  3788. (beginning-of-line 1)
  3789. (looking-at org-TBLFM-regexp)))
  3790. (defvar org-clock-start-time)
  3791. (defvar org-clock-marker (make-marker)
  3792. "Marker recording the last clock-in.")
  3793. (defvar org-clock-hd-marker (make-marker)
  3794. "Marker recording the last clock-in, but the headline position.")
  3795. (defvar org-clock-heading ""
  3796. "The heading of the current clock entry.")
  3797. (defun org-clock-is-active ()
  3798. "Return non-nil if clock is currently running.
  3799. The return value is actually the clock marker."
  3800. (marker-buffer org-clock-marker))
  3801. (eval-and-compile
  3802. (org-autoload "org-clock" '(org-clock-remove-overlays
  3803. org-clock-update-time-maybe
  3804. org-clocktable-shift)))
  3805. (defun org-check-running-clock ()
  3806. "Check if the current buffer contains the running clock.
  3807. If yes, offer to stop it and to save the buffer with the changes."
  3808. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3809. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3810. (buffer-name))))
  3811. (org-clock-out)
  3812. (when (y-or-n-p "Save changed buffer?")
  3813. (save-buffer))))
  3814. (defun org-clocktable-try-shift (dir n)
  3815. "Check if this line starts a clock table, if yes, shift the time block."
  3816. (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
  3817. (org-clocktable-shift dir n)))
  3818. ;;;###autoload
  3819. (defun org-clock-persistence-insinuate ()
  3820. "Set up hooks for clock persistence."
  3821. (require 'org-clock)
  3822. (add-hook 'org-mode-hook 'org-clock-load)
  3823. (add-hook 'kill-emacs-hook 'org-clock-save))
  3824. ;; Define the variable already here, to make sure we have it.
  3825. (defvar org-indent-mode nil
  3826. "Non-nil if Org-Indent mode is enabled.
  3827. Use the command `org-indent-mode' to change this variable.")
  3828. ;; Autoload archiving code
  3829. ;; The stuff that is needed for cycling and tags has to be defined here.
  3830. (defgroup org-archive nil
  3831. "Options concerning archiving in Org-mode."
  3832. :tag "Org Archive"
  3833. :group 'org-structure)
  3834. (defcustom org-archive-location "%s_archive::"
  3835. "The location where subtrees should be archived.
  3836. The value of this variable is a string, consisting of two parts,
  3837. separated by a double-colon. The first part is a filename and
  3838. the second part is a headline.
  3839. When the filename is omitted, archiving happens in the same file.
  3840. %s in the filename will be replaced by the current file
  3841. name (without the directory part). Archiving to a different file
  3842. is useful to keep archived entries from contributing to the
  3843. Org-mode Agenda.
  3844. The archived entries will be filed as subtrees of the specified
  3845. headline. When the headline is omitted, the subtrees are simply
  3846. filed away at the end of the file, as top-level entries. Also in
  3847. the heading you can use %s to represent the file name, this can be
  3848. useful when using the same archive for a number of different files.
  3849. Here are a few examples:
  3850. \"%s_archive::\"
  3851. If the current file is Projects.org, archive in file
  3852. Projects.org_archive, as top-level trees. This is the default.
  3853. \"::* Archived Tasks\"
  3854. Archive in the current file, under the top-level headline
  3855. \"* Archived Tasks\".
  3856. \"~/org/archive.org::\"
  3857. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  3858. \"~/org/archive.org::* From %s\"
  3859. Archive in file ~/org/archive.org (absolute path), under headlines
  3860. \"From FILENAME\" where file name is the current file name.
  3861. \"~/org/datetree.org::datetree/* Finished Tasks\"
  3862. The \"datetree/\" string is special, signifying to archive
  3863. items to the datetree. Items are placed in either the CLOSED
  3864. date of the item, or the current date if there is no CLOSED date.
  3865. The heading will be a subentry to the current date. There doesn't
  3866. need to be a heading, but there always needs to be a slash after
  3867. datetree. For example, to store archived items directly in the
  3868. datetree, use \"~/org/datetree.org::datetree/\".
  3869. \"basement::** Finished Tasks\"
  3870. Archive in file ./basement (relative path), as level 3 trees
  3871. below the level 2 heading \"** Finished Tasks\".
  3872. You may set this option on a per-file basis by adding to the buffer a
  3873. line like
  3874. #+ARCHIVE: basement::** Finished Tasks
  3875. You may also define it locally for a subtree by setting an ARCHIVE property
  3876. in the entry. If such a property is found in an entry, or anywhere up
  3877. the hierarchy, it will be used."
  3878. :group 'org-archive
  3879. :type 'string)
  3880. (defcustom org-archive-tag "ARCHIVE"
  3881. "The tag that marks a subtree as archived.
  3882. An archived subtree does not open during visibility cycling, and does
  3883. not contribute to the agenda listings.
  3884. After changing this, font-lock must be restarted in the relevant buffers to
  3885. get the proper fontification."
  3886. :group 'org-archive
  3887. :group 'org-keywords
  3888. :type 'string)
  3889. (defcustom org-agenda-skip-archived-trees t
  3890. "Non-nil means the agenda will skip any items located in archived trees.
  3891. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  3892. variable is no longer recommended, you should leave it at the value t.
  3893. Instead, use the key `v' to cycle the archives-mode in the agenda."
  3894. :group 'org-archive
  3895. :group 'org-agenda-skip
  3896. :type 'boolean)
  3897. (defcustom org-columns-skip-archived-trees t
  3898. "Non-nil means ignore archived trees when creating column view."
  3899. :group 'org-archive
  3900. :group 'org-properties
  3901. :type 'boolean)
  3902. (defcustom org-cycle-open-archived-trees nil
  3903. "Non-nil means `org-cycle' will open archived trees.
  3904. An archived tree is a tree marked with the tag ARCHIVE.
  3905. When nil, archived trees will stay folded. You can still open them with
  3906. normal outline commands like `show-all', but not with the cycling commands."
  3907. :group 'org-archive
  3908. :group 'org-cycle
  3909. :type 'boolean)
  3910. (defcustom org-sparse-tree-open-archived-trees nil
  3911. "Non-nil means sparse tree construction shows matches in archived trees.
  3912. When nil, matches in these trees are highlighted, but the trees are kept in
  3913. collapsed state."
  3914. :group 'org-archive
  3915. :group 'org-sparse-trees
  3916. :type 'boolean)
  3917. (defcustom org-sparse-tree-default-date-type 'scheduled-or-deadline
  3918. "The default date type when building a sparse tree.
  3919. When this is nil, a date is a scheduled or a deadline timestamp.
  3920. Otherwise, these types are allowed:
  3921. all: all timestamps
  3922. active: only active timestamps (<...>)
  3923. inactive: only inactive timestamps (<...)
  3924. scheduled: only scheduled timestamps
  3925. deadline: only deadline timestamps"
  3926. :type '(choice (const :tag "Scheduled or deadline" 'scheduled-or-deadline)
  3927. (const :tag "All timestamps" all)
  3928. (const :tag "Only active timestamps" active)
  3929. (const :tag "Only inactive timestamps" inactive)
  3930. (const :tag "Only scheduled timestamps" scheduled)
  3931. (const :tag "Only deadline timestamps" deadline)
  3932. (const :tag "Only closed timestamps" closed))
  3933. :version "24.3"
  3934. :group 'org-sparse-trees)
  3935. (defun org-cycle-hide-archived-subtrees (state)
  3936. "Re-hide all archived subtrees after a visibility state change."
  3937. (when (and (not org-cycle-open-archived-trees)
  3938. (not (memq state '(overview folded))))
  3939. (save-excursion
  3940. (let* ((globalp (memq state '(contents all)))
  3941. (beg (if globalp (point-min) (point)))
  3942. (end (if globalp (point-max) (org-end-of-subtree t))))
  3943. (org-hide-archived-subtrees beg end)
  3944. (goto-char beg)
  3945. (if (looking-at (concat ".*:" org-archive-tag ":"))
  3946. (message "%s" (substitute-command-keys
  3947. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  3948. (defun org-force-cycle-archived ()
  3949. "Cycle subtree even if it is archived."
  3950. (interactive)
  3951. (setq this-command 'org-cycle)
  3952. (let ((org-cycle-open-archived-trees t))
  3953. (call-interactively 'org-cycle)))
  3954. (defun org-hide-archived-subtrees (beg end)
  3955. "Re-hide all archived subtrees after a visibility state change."
  3956. (save-excursion
  3957. (let* ((re (concat ":" org-archive-tag ":")))
  3958. (goto-char beg)
  3959. (while (re-search-forward re end t)
  3960. (when (org-at-heading-p)
  3961. (org-flag-subtree t)
  3962. (org-end-of-subtree t))))))
  3963. (declare-function outline-end-of-heading "outline" ())
  3964. (declare-function outline-flag-region "outline" (from to flag))
  3965. (defun org-flag-subtree (flag)
  3966. (save-excursion
  3967. (org-back-to-heading t)
  3968. (outline-end-of-heading)
  3969. (outline-flag-region (point)
  3970. (progn (org-end-of-subtree t) (point))
  3971. flag)))
  3972. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  3973. (eval-and-compile
  3974. (org-autoload "org-archive"
  3975. '(org-add-archive-files)))
  3976. ;; Autoload Column View Code
  3977. (declare-function org-columns-number-to-string "org-colview" (n fmt &optional printf))
  3978. (declare-function org-columns-get-format-and-top-level "org-colview" ())
  3979. (declare-function org-columns-compute "org-colview" (property))
  3980. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  3981. '(org-columns-number-to-string
  3982. org-columns-get-format-and-top-level
  3983. org-columns-compute
  3984. org-columns-remove-overlays))
  3985. ;; Autoload ID code
  3986. (declare-function org-id-store-link "org-id")
  3987. (declare-function org-id-locations-load "org-id")
  3988. (declare-function org-id-locations-save "org-id")
  3989. (defvar org-id-track-globally)
  3990. (org-autoload "org-id"
  3991. '(org-id-new
  3992. org-id-copy
  3993. org-id-get-with-outline-path-completion
  3994. org-id-get-with-outline-drilling))
  3995. ;;; Variables for pre-computed regular expressions, all buffer local
  3996. (defvar org-drawer-regexp "^[ \t]*:PROPERTIES:[ \t]*$"
  3997. "Matches first line of a hidden block.")
  3998. (make-variable-buffer-local 'org-drawer-regexp)
  3999. (defvar org-todo-regexp nil
  4000. "Matches any of the TODO state keywords.")
  4001. (make-variable-buffer-local 'org-todo-regexp)
  4002. (defvar org-not-done-regexp nil
  4003. "Matches any of the TODO state keywords except the last one.")
  4004. (make-variable-buffer-local 'org-not-done-regexp)
  4005. (defvar org-not-done-heading-regexp nil
  4006. "Matches a TODO headline that is not done.")
  4007. (make-variable-buffer-local 'org-not-done-regexp)
  4008. (defvar org-todo-line-regexp nil
  4009. "Matches a headline and puts TODO state into group 2 if present.")
  4010. (make-variable-buffer-local 'org-todo-line-regexp)
  4011. (defvar org-complex-heading-regexp nil
  4012. "Matches a headline and puts everything into groups:
  4013. group 1: the stars
  4014. group 2: The todo keyword, maybe
  4015. group 3: Priority cookie
  4016. group 4: True headline
  4017. group 5: Tags")
  4018. (make-variable-buffer-local 'org-complex-heading-regexp)
  4019. (defvar org-complex-heading-regexp-format nil
  4020. "Printf format to make regexp to match an exact headline.
  4021. This regexp will match the headline of any node which has the
  4022. exact headline text that is put into the format, but may have any
  4023. TODO state, priority and tags.")
  4024. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  4025. (defvar org-todo-line-tags-regexp nil
  4026. "Matches a headline and puts TODO state into group 2 if present.
  4027. Also put tags into group 4 if tags are present.")
  4028. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  4029. (defvar org-ds-keyword-length 12
  4030. "Maximum length of the DEADLINE and SCHEDULED keywords.")
  4031. (make-variable-buffer-local 'org-ds-keyword-length)
  4032. (defvar org-deadline-regexp nil
  4033. "Matches the DEADLINE keyword.")
  4034. (make-variable-buffer-local 'org-deadline-regexp)
  4035. (defvar org-deadline-time-regexp nil
  4036. "Matches the DEADLINE keyword together with a time stamp.")
  4037. (make-variable-buffer-local 'org-deadline-time-regexp)
  4038. (defvar org-deadline-time-hour-regexp nil
  4039. "Matches the DEADLINE keyword together with a time-and-hour stamp.")
  4040. (make-variable-buffer-local 'org-deadline-time-hour-regexp)
  4041. (defvar org-deadline-line-regexp nil
  4042. "Matches the DEADLINE keyword and the rest of the line.")
  4043. (make-variable-buffer-local 'org-deadline-line-regexp)
  4044. (defvar org-scheduled-regexp nil
  4045. "Matches the SCHEDULED keyword.")
  4046. (make-variable-buffer-local 'org-scheduled-regexp)
  4047. (defvar org-scheduled-time-regexp nil
  4048. "Matches the SCHEDULED keyword together with a time stamp.")
  4049. (make-variable-buffer-local 'org-scheduled-time-regexp)
  4050. (defvar org-scheduled-time-hour-regexp nil
  4051. "Matches the SCHEDULED keyword together with a time-and-hour stamp.")
  4052. (make-variable-buffer-local 'org-scheduled-time-hour-regexp)
  4053. (defvar org-closed-time-regexp nil
  4054. "Matches the CLOSED keyword together with a time stamp.")
  4055. (make-variable-buffer-local 'org-closed-time-regexp)
  4056. (defvar org-keyword-time-regexp nil
  4057. "Matches any of the 4 keywords, together with the time stamp.")
  4058. (make-variable-buffer-local 'org-keyword-time-regexp)
  4059. (defvar org-keyword-time-not-clock-regexp nil
  4060. "Matches any of the 3 keywords, together with the time stamp.")
  4061. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  4062. (defvar org-maybe-keyword-time-regexp nil
  4063. "Matches a timestamp, possibly preceded by a keyword.")
  4064. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  4065. (defvar org-all-time-keywords nil
  4066. "List of time keywords.")
  4067. (make-variable-buffer-local 'org-all-time-keywords)
  4068. (defconst org-plain-time-of-day-regexp
  4069. (concat
  4070. "\\(\\<[012]?[0-9]"
  4071. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4072. "\\(--?"
  4073. "\\(\\<[012]?[0-9]"
  4074. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4075. "\\)?")
  4076. "Regular expression to match a plain time or time range.
  4077. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4078. groups carry important information:
  4079. 0 the full match
  4080. 1 the first time, range or not
  4081. 8 the second time, if it is a range.")
  4082. (defconst org-plain-time-extension-regexp
  4083. (concat
  4084. "\\(\\<[012]?[0-9]"
  4085. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4086. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  4087. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  4088. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4089. groups carry important information:
  4090. 0 the full match
  4091. 7 hours of duration
  4092. 9 minutes of duration")
  4093. (defconst org-stamp-time-of-day-regexp
  4094. (concat
  4095. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  4096. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  4097. "\\(--?"
  4098. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  4099. "Regular expression to match a timestamp time or time range.
  4100. After a match, the following groups carry important information:
  4101. 0 the full match
  4102. 1 date plus weekday, for back referencing to make sure both times are on the same day
  4103. 2 the first time, range or not
  4104. 4 the second time, if it is a range.")
  4105. (defconst org-startup-options
  4106. '(("fold" org-startup-folded t)
  4107. ("overview" org-startup-folded t)
  4108. ("nofold" org-startup-folded nil)
  4109. ("showall" org-startup-folded nil)
  4110. ("showeverything" org-startup-folded showeverything)
  4111. ("content" org-startup-folded content)
  4112. ("indent" org-startup-indented t)
  4113. ("noindent" org-startup-indented nil)
  4114. ("hidestars" org-hide-leading-stars t)
  4115. ("showstars" org-hide-leading-stars nil)
  4116. ("odd" org-odd-levels-only t)
  4117. ("oddeven" org-odd-levels-only nil)
  4118. ("align" org-startup-align-all-tables t)
  4119. ("noalign" org-startup-align-all-tables nil)
  4120. ("inlineimages" org-startup-with-inline-images t)
  4121. ("noinlineimages" org-startup-with-inline-images nil)
  4122. ("latexpreview" org-startup-with-latex-preview t)
  4123. ("nolatexpreview" org-startup-with-latex-preview nil)
  4124. ("customtime" org-display-custom-times t)
  4125. ("logdone" org-log-done time)
  4126. ("lognotedone" org-log-done note)
  4127. ("nologdone" org-log-done nil)
  4128. ("lognoteclock-out" org-log-note-clock-out t)
  4129. ("nolognoteclock-out" org-log-note-clock-out nil)
  4130. ("logrepeat" org-log-repeat state)
  4131. ("lognoterepeat" org-log-repeat note)
  4132. ("logdrawer" org-log-into-drawer t)
  4133. ("nologdrawer" org-log-into-drawer nil)
  4134. ("logstatesreversed" org-log-states-order-reversed t)
  4135. ("nologstatesreversed" org-log-states-order-reversed nil)
  4136. ("nologrepeat" org-log-repeat nil)
  4137. ("logreschedule" org-log-reschedule time)
  4138. ("lognotereschedule" org-log-reschedule note)
  4139. ("nologreschedule" org-log-reschedule nil)
  4140. ("logredeadline" org-log-redeadline time)
  4141. ("lognoteredeadline" org-log-redeadline note)
  4142. ("nologredeadline" org-log-redeadline nil)
  4143. ("logrefile" org-log-refile time)
  4144. ("lognoterefile" org-log-refile note)
  4145. ("nologrefile" org-log-refile nil)
  4146. ("fninline" org-footnote-define-inline t)
  4147. ("nofninline" org-footnote-define-inline nil)
  4148. ("fnlocal" org-footnote-section nil)
  4149. ("fnauto" org-footnote-auto-label t)
  4150. ("fnprompt" org-footnote-auto-label nil)
  4151. ("fnconfirm" org-footnote-auto-label confirm)
  4152. ("fnplain" org-footnote-auto-label plain)
  4153. ("fnadjust" org-footnote-auto-adjust t)
  4154. ("nofnadjust" org-footnote-auto-adjust nil)
  4155. ("constcgs" constants-unit-system cgs)
  4156. ("constSI" constants-unit-system SI)
  4157. ("noptag" org-tag-persistent-alist nil)
  4158. ("hideblocks" org-hide-block-startup t)
  4159. ("nohideblocks" org-hide-block-startup nil)
  4160. ("beamer" org-startup-with-beamer-mode t)
  4161. ("entitiespretty" org-pretty-entities t)
  4162. ("entitiesplain" org-pretty-entities nil))
  4163. "Variable associated with STARTUP options for org-mode.
  4164. Each element is a list of three items: the startup options (as written
  4165. in the #+STARTUP line), the corresponding variable, and the value to set
  4166. this variable to if the option is found. An optional forth element PUSH
  4167. means to push this value onto the list in the variable.")
  4168. (defun org-update-property-plist (key val props)
  4169. "Update PROPS with KEY and VAL."
  4170. (let* ((appending (string= "+" (substring key (- (length key) 1))))
  4171. (key (if appending (substring key 0 (- (length key) 1)) key))
  4172. (remainder (org-remove-if (lambda (p) (string= (car p) key)) props))
  4173. (previous (cdr (assoc key props))))
  4174. (if appending
  4175. (cons (cons key (if previous (concat previous " " val) val)) remainder)
  4176. (cons (cons key val) remainder))))
  4177. (defconst org-block-regexp
  4178. "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
  4179. "Regular expression for hiding blocks.")
  4180. (defconst org-heading-keyword-regexp-format
  4181. "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  4182. "Printf format for a regexp matching a headline with some keyword.
  4183. This regexp will match the headline of any node which has the
  4184. exact keyword that is put into the format. The keyword isn't in
  4185. any group by default, but the stars and the body are.")
  4186. (defconst org-heading-keyword-maybe-regexp-format
  4187. "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
  4188. "Printf format for a regexp matching a headline, possibly with some keyword.
  4189. This regexp can match any headline with the specified keyword, or
  4190. without a keyword. The keyword isn't in any group by default,
  4191. but the stars and the body are.")
  4192. (defcustom org-group-tags t
  4193. "When non-nil (the default), use group tags.
  4194. This can be turned on/off through `org-toggle-tags-groups'."
  4195. :group 'org-tags
  4196. :group 'org-startup
  4197. :type 'boolean)
  4198. (defun org-toggle-tags-groups ()
  4199. "Toggle support for group tags.
  4200. Support for group tags is controlled by the option
  4201. `org-group-tags', which is non-nil by default."
  4202. (interactive)
  4203. (setq org-group-tags (not org-group-tags))
  4204. (cond ((and (derived-mode-p 'org-agenda-mode)
  4205. org-group-tags)
  4206. (org-agenda-redo))
  4207. ((derived-mode-p 'org-mode)
  4208. (let ((org-inhibit-startup t)) (org-mode))))
  4209. (message "Groups tags support has been turned %s"
  4210. (if org-group-tags "on" "off")))
  4211. (defun org-set-regexps-and-options-for-tags ()
  4212. "Precompute regular expressions used for tags in the current buffer."
  4213. (when (derived-mode-p 'org-mode)
  4214. (org-set-local 'org-file-tags nil)
  4215. (let ((re (org-make-options-regexp '("FILETAGS" "TAGS")))
  4216. (splitre "[ \t]+")
  4217. (start 0)
  4218. tags ftags key value)
  4219. (save-excursion
  4220. (save-restriction
  4221. (widen)
  4222. (goto-char (point-min))
  4223. (while (re-search-forward re nil t)
  4224. (setq key (upcase (org-match-string-no-properties 1))
  4225. value (org-match-string-no-properties 2))
  4226. (if (stringp value) (setq value (org-trim value)))
  4227. (cond
  4228. ((equal key "TAGS")
  4229. (setq tags (append tags (if tags '("\\n") nil)
  4230. (org-split-string value splitre))))
  4231. ((equal key "FILETAGS")
  4232. (when (string-match "\\S-" value)
  4233. (setq ftags
  4234. (append
  4235. ftags
  4236. (apply 'append
  4237. (mapcar (lambda (x) (org-split-string x ":"))
  4238. (org-split-string value)))))))))))
  4239. ;; Process the file tags.
  4240. (and ftags (org-set-local 'org-file-tags
  4241. (mapcar 'org-add-prop-inherited ftags)))
  4242. (org-set-local 'org-tag-groups-alist nil)
  4243. ;; Process the tags.
  4244. ;; FIXME
  4245. (when tags
  4246. (let (e tgs g)
  4247. (while (setq e (pop tags))
  4248. (cond
  4249. ((equal e "{")
  4250. (progn (push '(:startgroup) tgs)
  4251. (when (equal (nth 1 tags) ":")
  4252. (push (list (replace-regexp-in-string
  4253. "(.+)$" "" (nth 0 tags)))
  4254. org-tag-groups-alist)
  4255. (setq g 0))))
  4256. ((equal e ":") (push '(:grouptags) tgs))
  4257. ((equal e "}") (push '(:endgroup) tgs) (if g (setq g nil)))
  4258. ((equal e "\\n") (push '(:newline) tgs))
  4259. ((string-match (org-re "^\\([[:alnum:]_@#%]+\\)(\\(.\\))$") e)
  4260. (push (cons (match-string 1 e)
  4261. (string-to-char (match-string 2 e))) tgs)
  4262. (if (and g (> g 0))
  4263. (setcar org-tag-groups-alist
  4264. (append (car org-tag-groups-alist)
  4265. (list (match-string 1 e)))))
  4266. (if g (setq g (1+ g))))
  4267. (t (push (list e) tgs)
  4268. (if (and g (> g 0))
  4269. (setcar org-tag-groups-alist
  4270. (append (car org-tag-groups-alist) (list e))))
  4271. (if g (setq g (1+ g))))))
  4272. (org-set-local 'org-tag-alist nil)
  4273. (while (setq e (pop tgs))
  4274. (or (and (stringp (car e))
  4275. (assoc (car e) org-tag-alist))
  4276. (push e org-tag-alist))))))))
  4277. (defun org-set-regexps-and-options ()
  4278. "Precompute regular expressions used in the current buffer."
  4279. (when (derived-mode-p 'org-mode)
  4280. (org-set-local 'org-todo-kwd-alist nil)
  4281. (org-set-local 'org-todo-key-alist nil)
  4282. (org-set-local 'org-todo-key-trigger nil)
  4283. (org-set-local 'org-todo-keywords-1 nil)
  4284. (org-set-local 'org-done-keywords nil)
  4285. (org-set-local 'org-todo-heads nil)
  4286. (org-set-local 'org-todo-sets nil)
  4287. (org-set-local 'org-todo-log-states nil)
  4288. (org-set-local 'org-file-properties nil)
  4289. (let ((re (org-make-options-regexp
  4290. '("CATEGORY" "TODO" "COLUMNS" "STARTUP" "ARCHIVE"
  4291. "LINK" "PRIORITIES" "CONSTANTS" "PROPERTY" "DRAWERS"
  4292. "SETUPFILE" "OPTIONS")
  4293. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  4294. (splitre "[ \t]+")
  4295. (scripts org-use-sub-superscripts)
  4296. kwds kws0 kwsa key log value cat arch const links hw dws
  4297. tail sep kws1 prio props drawers ext-setup-or-nil setup-contents
  4298. (start 0))
  4299. (save-excursion
  4300. (save-restriction
  4301. (widen)
  4302. (goto-char (point-min))
  4303. (while (or (and ext-setup-or-nil
  4304. (let (ret)
  4305. (with-temp-buffer
  4306. (insert ext-setup-or-nil)
  4307. (let ((major-mode 'org-mode))
  4308. (org-set-regexps-and-options-for-tags)
  4309. (setq ret (list org-file-tags org-tag-alist
  4310. org-tag-groups-alist))))
  4311. (setq org-file-tags (nth 0 ret)
  4312. org-tag-alist (nth 1 ret)
  4313. org-tag-groups-alist (nth 2 ret))))
  4314. (and ext-setup-or-nil
  4315. (string-match re ext-setup-or-nil start)
  4316. (setq start (match-end 0)))
  4317. (and (setq ext-setup-or-nil nil start 0)
  4318. (re-search-forward re nil t)))
  4319. (setq key (upcase (match-string 1 ext-setup-or-nil))
  4320. value (org-match-string-no-properties 2 ext-setup-or-nil))
  4321. (if (stringp value) (setq value (org-trim value)))
  4322. (cond
  4323. ((equal key "CATEGORY")
  4324. (setq cat value))
  4325. ((member key '("SEQ_TODO" "TODO"))
  4326. (push (cons 'sequence (org-split-string value splitre)) kwds))
  4327. ((equal key "TYP_TODO")
  4328. (push (cons 'type (org-split-string value splitre)) kwds))
  4329. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  4330. ;; general TODO-like setup
  4331. (push (cons (intern (downcase (match-string 1 key)))
  4332. (org-split-string value splitre)) kwds))
  4333. ((equal key "COLUMNS")
  4334. (org-set-local 'org-columns-default-format value))
  4335. ((equal key "LINK")
  4336. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  4337. (push (cons (match-string 1 value)
  4338. (org-trim (match-string 2 value)))
  4339. links)))
  4340. ((equal key "PRIORITIES")
  4341. (setq prio (org-split-string value " +")))
  4342. ((equal key "PROPERTY")
  4343. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4344. (setq props (org-update-property-plist (match-string 1 value)
  4345. (match-string 2 value)
  4346. props))))
  4347. ((equal key "DRAWERS")
  4348. (setq drawers (delete-dups (append org-drawers (org-split-string value splitre)))))
  4349. ((equal key "CONSTANTS")
  4350. (org-table-set-constants))
  4351. ((equal key "STARTUP")
  4352. (let ((opts (org-split-string value splitre))
  4353. l var val)
  4354. (while (setq l (pop opts))
  4355. (when (setq l (assoc l org-startup-options))
  4356. (setq var (nth 1 l) val (nth 2 l))
  4357. (if (not (nth 3 l))
  4358. (set (make-local-variable var) val)
  4359. (if (not (listp (symbol-value var)))
  4360. (set (make-local-variable var) nil))
  4361. (set (make-local-variable var) (symbol-value var))
  4362. (add-to-list var val))))))
  4363. ((equal key "ARCHIVE")
  4364. (setq arch value)
  4365. (remove-text-properties 0 (length arch)
  4366. '(face t fontified t) arch))
  4367. ((equal key "OPTIONS")
  4368. (if (string-match "\\([ \t]\\|\\`\\)\\^:\\(t\\|nil\\|{}\\)" value)
  4369. (setq scripts (read (match-string 2 value)))))
  4370. ((and (equal key "SETUPFILE")
  4371. ;; Prevent checking in Gnus messages
  4372. (not buffer-read-only))
  4373. (setq setup-contents (org-file-contents
  4374. (expand-file-name
  4375. (org-remove-double-quotes value))
  4376. 'noerror))
  4377. (if (not ext-setup-or-nil)
  4378. (setq ext-setup-or-nil setup-contents start 0)
  4379. (setq ext-setup-or-nil
  4380. (concat (substring ext-setup-or-nil 0 start)
  4381. "\n" setup-contents "\n"
  4382. (substring ext-setup-or-nil start)))))))
  4383. ;; search for property blocks
  4384. (goto-char (point-min))
  4385. (while (re-search-forward org-block-regexp nil t)
  4386. (when (equal "PROPERTY" (upcase (match-string 1)))
  4387. (setq value (replace-regexp-in-string
  4388. "[\n\r]" " " (match-string 4)))
  4389. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4390. (setq props (org-update-property-plist (match-string 1 value)
  4391. (match-string 2 value)
  4392. props)))))))
  4393. (org-set-local 'org-use-sub-superscripts scripts)
  4394. (when cat
  4395. (org-set-local 'org-category (intern cat))
  4396. (push (cons "CATEGORY" cat) props))
  4397. (when prio
  4398. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  4399. (setq prio (mapcar 'string-to-char prio))
  4400. (org-set-local 'org-highest-priority (nth 0 prio))
  4401. (org-set-local 'org-lowest-priority (nth 1 prio))
  4402. (org-set-local 'org-default-priority (nth 2 prio)))
  4403. (and props (org-set-local 'org-file-properties (nreverse props)))
  4404. (and drawers (org-set-local 'org-drawers drawers))
  4405. (and arch (org-set-local 'org-archive-location arch))
  4406. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  4407. ;; Process the TODO keywords
  4408. (unless kwds
  4409. ;; Use the global values as if they had been given locally.
  4410. (setq kwds (default-value 'org-todo-keywords))
  4411. (if (stringp (car kwds))
  4412. (setq kwds (list (cons org-todo-interpretation
  4413. (default-value 'org-todo-keywords)))))
  4414. (setq kwds (reverse kwds)))
  4415. (setq kwds (nreverse kwds))
  4416. (let (inter kws kw)
  4417. (while (setq kws (pop kwds))
  4418. (let ((kws (or
  4419. (run-hook-with-args-until-success
  4420. 'org-todo-setup-filter-hook kws)
  4421. kws)))
  4422. (setq inter (pop kws) sep (member "|" kws)
  4423. kws0 (delete "|" (copy-sequence kws))
  4424. kwsa nil
  4425. kws1 (mapcar
  4426. (lambda (x)
  4427. ;; 1 2
  4428. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  4429. (progn
  4430. (setq kw (match-string 1 x)
  4431. key (and (match-end 2) (match-string 2 x))
  4432. log (org-extract-log-state-settings x))
  4433. (push (cons kw (and key (string-to-char key))) kwsa)
  4434. (and log (push log org-todo-log-states))
  4435. kw)
  4436. (error "Invalid TODO keyword %s" x)))
  4437. kws0)
  4438. kwsa (if kwsa (append '((:startgroup))
  4439. (nreverse kwsa)
  4440. '((:endgroup))))
  4441. hw (car kws1)
  4442. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  4443. tail (list inter hw (car dws) (org-last dws))))
  4444. (add-to-list 'org-todo-heads hw 'append)
  4445. (push kws1 org-todo-sets)
  4446. (setq org-done-keywords (append org-done-keywords dws nil))
  4447. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  4448. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  4449. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  4450. (setq org-todo-sets (nreverse org-todo-sets)
  4451. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  4452. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  4453. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  4454. ;; Compute the regular expressions and other local variables.
  4455. ;; Using `org-outline-regexp-bol' would complicate them much,
  4456. ;; because of the fixed white space at the end of that string.
  4457. (if (not org-done-keywords)
  4458. (setq org-done-keywords (and org-todo-keywords-1
  4459. (list (org-last org-todo-keywords-1)))))
  4460. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  4461. (length org-scheduled-string)
  4462. (length org-clock-string)
  4463. (length org-closed-string)))
  4464. org-drawer-regexp
  4465. (concat "^[ \t]*:\\("
  4466. (mapconcat 'regexp-quote org-drawers "\\|")
  4467. "\\):[ \t]*$")
  4468. org-not-done-keywords
  4469. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  4470. org-todo-regexp
  4471. (concat "\\("
  4472. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4473. "\\)")
  4474. org-not-done-regexp
  4475. (concat "\\("
  4476. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  4477. "\\)")
  4478. org-not-done-heading-regexp
  4479. (format org-heading-keyword-regexp-format org-not-done-regexp)
  4480. org-todo-line-regexp
  4481. (format org-heading-keyword-maybe-regexp-format org-todo-regexp)
  4482. org-complex-heading-regexp
  4483. (concat "^\\(\\*+\\)"
  4484. "\\(?: +" org-todo-regexp "\\)?"
  4485. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4486. "\\(?: +\\(.*?\\)\\)??"
  4487. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?")
  4488. "[ \t]*$")
  4489. org-complex-heading-regexp-format
  4490. (concat "^\\(\\*+\\)"
  4491. "\\(?: +" org-todo-regexp "\\)?"
  4492. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4493. "\\(?: +"
  4494. ;; Stats cookies can be stuck to body.
  4495. "\\(?:\\[[0-9%%/]+\\] *\\)?"
  4496. "\\(%s\\)"
  4497. "\\(?: *\\[[0-9%%/]+\\]\\)?"
  4498. "\\)"
  4499. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?")
  4500. "[ \t]*$")
  4501. org-todo-line-tags-regexp
  4502. (concat "^\\(\\*+\\)"
  4503. "\\(?: +" org-todo-regexp "\\)?"
  4504. "\\(?: +\\(.*?\\)\\)??"
  4505. (org-re "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?")
  4506. "[ \t]*$")
  4507. org-deadline-regexp (concat "\\<" org-deadline-string)
  4508. org-deadline-time-regexp
  4509. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  4510. org-deadline-time-hour-regexp
  4511. (concat "\\<" org-deadline-string
  4512. " *<\\(.+[0-9]\\{1,2\\}:[0-9]\\{2\\}[^>]*\\)>")
  4513. org-deadline-line-regexp
  4514. (concat "\\<\\(" org-deadline-string "\\).*")
  4515. org-scheduled-regexp
  4516. (concat "\\<" org-scheduled-string)
  4517. org-scheduled-time-regexp
  4518. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  4519. org-scheduled-time-hour-regexp
  4520. (concat "\\<" org-scheduled-string
  4521. " *<\\(.+[0-9]\\{1,2\\}:[0-9]\\{2\\}[^>]*\\)>")
  4522. org-closed-time-regexp
  4523. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  4524. org-keyword-time-regexp
  4525. (concat "\\<\\(" org-scheduled-string
  4526. "\\|" org-deadline-string
  4527. "\\|" org-closed-string
  4528. "\\|" org-clock-string "\\)"
  4529. " *[[<]\\([^]>]+\\)[]>]")
  4530. org-keyword-time-not-clock-regexp
  4531. (concat "\\<\\(" org-scheduled-string
  4532. "\\|" org-deadline-string
  4533. "\\|" org-closed-string
  4534. "\\)"
  4535. " *[[<]\\([^]>]+\\)[]>]")
  4536. org-maybe-keyword-time-regexp
  4537. (concat "\\(\\<\\(" org-scheduled-string
  4538. "\\|" org-deadline-string
  4539. "\\|" org-closed-string
  4540. "\\|" org-clock-string "\\)\\)?"
  4541. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  4542. org-all-time-keywords
  4543. (mapcar (lambda (w) (substring w 0 -1))
  4544. (list org-scheduled-string org-deadline-string
  4545. org-clock-string org-closed-string)))
  4546. (org-compute-latex-and-related-regexp)
  4547. (org-set-font-lock-defaults))))
  4548. (defun org-file-contents (file &optional noerror)
  4549. "Return the contents of FILE, as a string."
  4550. (if (or (not file)
  4551. (not (file-readable-p file)))
  4552. (if noerror
  4553. (message "Cannot read file \"%s\"" file)
  4554. (error "Cannot read file \"%s\"" file))
  4555. (with-temp-buffer
  4556. (insert-file-contents file)
  4557. (buffer-string))))
  4558. (defun org-extract-log-state-settings (x)
  4559. "Extract the log state setting from a TODO keyword string.
  4560. This will extract info from a string like \"WAIT(w@/!)\"."
  4561. (let (kw key log1 log2)
  4562. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4563. (setq kw (match-string 1 x)
  4564. key (and (match-end 2) (match-string 2 x))
  4565. log1 (and (match-end 3) (match-string 3 x))
  4566. log2 (and (match-end 4) (match-string 4 x)))
  4567. (and (or log1 log2)
  4568. (list kw
  4569. (and log1 (if (equal log1 "!") 'time 'note))
  4570. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4571. (defun org-remove-keyword-keys (list)
  4572. "Remove a pair of parenthesis at the end of each string in LIST."
  4573. (mapcar (lambda (x)
  4574. (if (string-match "(.*)$" x)
  4575. (substring x 0 (match-beginning 0))
  4576. x))
  4577. list))
  4578. (defun org-assign-fast-keys (alist)
  4579. "Assign fast keys to a keyword-key alist.
  4580. Respect keys that are already there."
  4581. (let (new e (alt ?0))
  4582. (while (setq e (pop alist))
  4583. (if (or (memq (car e) '(:newline :grouptags :endgroup :startgroup))
  4584. (cdr e)) ;; Key already assigned.
  4585. (push e new)
  4586. (let ((clist (string-to-list (downcase (car e))))
  4587. (used (append new alist)))
  4588. (when (= (car clist) ?@)
  4589. (pop clist))
  4590. (while (and clist (rassoc (car clist) used))
  4591. (pop clist))
  4592. (unless clist
  4593. (while (rassoc alt used)
  4594. (incf alt)))
  4595. (push (cons (car e) (or (car clist) alt)) new))))
  4596. (nreverse new)))
  4597. ;;; Some variables used in various places
  4598. (defvar org-window-configuration nil
  4599. "Used in various places to store a window configuration.")
  4600. (defvar org-selected-window nil
  4601. "Used in various places to store a window configuration.")
  4602. (defvar org-finish-function nil
  4603. "Function to be called when `C-c C-c' is used.
  4604. This is for getting out of special buffers like capture.")
  4605. ;; FIXME: Occasionally check by commenting these, to make sure
  4606. ;; no other functions uses these, forgetting to let-bind them.
  4607. (org-no-warnings (defvar entry)) ;; unprefixed, from calendar.el
  4608. (defvar org-last-state)
  4609. (org-no-warnings (defvar date)) ;; unprefixed, from calendar.el
  4610. ;; Defined somewhere in this file, but used before definition.
  4611. (defvar org-entities) ;; defined in org-entities.el
  4612. (defvar org-struct-menu)
  4613. (defvar org-org-menu)
  4614. (defvar org-tbl-menu)
  4615. ;;;; Define the Org-mode
  4616. ;; We use a before-change function to check if a table might need
  4617. ;; an update.
  4618. (defvar org-table-may-need-update t
  4619. "Indicates that a table might need an update.
  4620. This variable is set by `org-before-change-function'.
  4621. `org-table-align' sets it back to nil.")
  4622. (defun org-before-change-function (beg end)
  4623. "Every change indicates that a table might need an update."
  4624. (setq org-table-may-need-update t))
  4625. (defvar org-mode-map)
  4626. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4627. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4628. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4629. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4630. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4631. (defvar org-table-buffer-is-an nil)
  4632. (defvar bidi-paragraph-direction)
  4633. (defvar buffer-face-mode-face)
  4634. (require 'outline)
  4635. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4636. (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"))
  4637. (require 'noutline "noutline" 'noerror) ;; stock XEmacs does not have it
  4638. ;; Other stuff we need.
  4639. (require 'time-date)
  4640. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  4641. (require 'easymenu)
  4642. (require 'overlay)
  4643. ;; (require 'org-macs) moved higher up in the file before it is first used
  4644. (require 'org-entities)
  4645. ;; (require 'org-compat) moved higher up in the file before it is first used
  4646. (require 'org-faces)
  4647. (require 'org-list)
  4648. (require 'org-pcomplete)
  4649. (require 'org-src)
  4650. (require 'org-footnote)
  4651. (require 'org-macro)
  4652. ;; babel
  4653. (require 'ob)
  4654. ;;;###autoload
  4655. (define-derived-mode org-mode outline-mode "Org"
  4656. "Outline-based notes management and organizer, alias
  4657. \"Carsten's outline-mode for keeping track of everything.\"
  4658. Org-mode develops organizational tasks around a NOTES file which
  4659. contains information about projects as plain text. Org-mode is
  4660. implemented on top of outline-mode, which is ideal to keep the content
  4661. of large files well structured. It supports ToDo items, deadlines and
  4662. time stamps, which magically appear in the diary listing of the Emacs
  4663. calendar. Tables are easily created with a built-in table editor.
  4664. Plain text URL-like links connect to websites, emails (VM), Usenet
  4665. messages (Gnus), BBDB entries, and any files related to the project.
  4666. For printing and sharing of notes, an Org-mode file (or a part of it)
  4667. can be exported as a structured ASCII or HTML file.
  4668. The following commands are available:
  4669. \\{org-mode-map}"
  4670. ;; Get rid of Outline menus, they are not needed
  4671. ;; Need to do this here because define-derived-mode sets up
  4672. ;; the keymap so late. Still, it is a waste to call this each time
  4673. ;; we switch another buffer into org-mode.
  4674. (if (featurep 'xemacs)
  4675. (when (boundp 'outline-mode-menu-heading)
  4676. ;; Assume this is Greg's port, it uses easymenu
  4677. (easy-menu-remove outline-mode-menu-heading)
  4678. (easy-menu-remove outline-mode-menu-show)
  4679. (easy-menu-remove outline-mode-menu-hide))
  4680. (define-key org-mode-map [menu-bar headings] 'undefined)
  4681. (define-key org-mode-map [menu-bar hide] 'undefined)
  4682. (define-key org-mode-map [menu-bar show] 'undefined))
  4683. (org-load-modules-maybe)
  4684. (easy-menu-add org-org-menu)
  4685. (easy-menu-add org-tbl-menu)
  4686. (org-install-agenda-files-menu)
  4687. (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4688. (add-to-invisibility-spec '(org-cwidth))
  4689. (add-to-invisibility-spec '(org-hide-block . t))
  4690. (when (featurep 'xemacs)
  4691. (org-set-local 'line-move-ignore-invisible t))
  4692. (org-set-local 'outline-regexp org-outline-regexp)
  4693. (org-set-local 'outline-level 'org-outline-level)
  4694. (setq bidi-paragraph-direction 'left-to-right)
  4695. ;; FIXME Circumvent a bug in outline.el (Emacs <24.4)
  4696. (set (make-local-variable 'paragraph-start) " \\|[ \t]*$\\|\\*+ ")
  4697. (when (and org-ellipsis
  4698. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4699. (fboundp 'make-glyph-code))
  4700. (unless org-display-table
  4701. (setq org-display-table (make-display-table)))
  4702. (set-display-table-slot
  4703. org-display-table 4
  4704. (vconcat (mapcar
  4705. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4706. org-ellipsis)))
  4707. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4708. (setq buffer-display-table org-display-table))
  4709. (org-set-regexps-and-options-for-tags)
  4710. (org-set-regexps-and-options)
  4711. (when (and org-tag-faces (not org-tags-special-faces-re))
  4712. ;; tag faces set outside customize.... force initialization.
  4713. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4714. ;; Calc embedded
  4715. (org-set-local 'calc-embedded-open-mode "# ")
  4716. ;; Modify a few syntax entries
  4717. (modify-syntax-entry ?@ "w")
  4718. (modify-syntax-entry ?\" "\"")
  4719. (if org-startup-truncated (setq truncate-lines t))
  4720. (when org-startup-indented (require 'org-indent) (org-indent-mode 1))
  4721. (org-set-local 'font-lock-unfontify-region-function
  4722. 'org-unfontify-region)
  4723. ;; Activate before-change-function
  4724. (org-set-local 'org-table-may-need-update t)
  4725. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4726. 'local)
  4727. ;; Check for running clock before killing a buffer
  4728. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4729. ;; Initialize macros templates.
  4730. (org-macro-initialize-templates)
  4731. ;; Initialize radio targets.
  4732. (org-update-radio-target-regexp)
  4733. ;; Indentation.
  4734. (org-set-local 'indent-line-function 'org-indent-line)
  4735. (org-set-local 'indent-region-function 'org-indent-region)
  4736. ;; Filling and auto-filling.
  4737. (org-setup-filling)
  4738. ;; Comments.
  4739. (org-setup-comments-handling)
  4740. ;; Beginning/end of defun
  4741. (org-set-local 'beginning-of-defun-function 'org-backward-element)
  4742. (org-set-local 'end-of-defun-function 'org-forward-element)
  4743. ;; Next error for sparse trees
  4744. (org-set-local 'next-error-function 'org-occur-next-match)
  4745. ;; Make sure dependence stuff works reliably, even for users who set it
  4746. ;; too late :-(
  4747. (if org-enforce-todo-dependencies
  4748. (add-hook 'org-blocker-hook
  4749. 'org-block-todo-from-children-or-siblings-or-parent)
  4750. (remove-hook 'org-blocker-hook
  4751. 'org-block-todo-from-children-or-siblings-or-parent))
  4752. (if org-enforce-todo-checkbox-dependencies
  4753. (add-hook 'org-blocker-hook
  4754. 'org-block-todo-from-checkboxes)
  4755. (remove-hook 'org-blocker-hook
  4756. 'org-block-todo-from-checkboxes))
  4757. ;; Align options lines
  4758. (org-set-local
  4759. 'align-mode-rules-list
  4760. '((org-in-buffer-settings
  4761. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4762. (modes . '(org-mode)))))
  4763. ;; Imenu
  4764. (org-set-local 'imenu-create-index-function
  4765. 'org-imenu-get-tree)
  4766. ;; Make isearch reveal context
  4767. (if (or (featurep 'xemacs)
  4768. (not (boundp 'outline-isearch-open-invisible-function)))
  4769. ;; Emacs 21 and XEmacs make use of the hook
  4770. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4771. ;; Emacs 22 deals with this through a special variable
  4772. (org-set-local 'outline-isearch-open-invisible-function
  4773. (lambda (&rest ignore) (org-show-context 'isearch)))
  4774. (org-add-hook 'isearch-mode-end-hook 'org-fix-ellipsis-at-bol 'append 'local))
  4775. ;; Setup the pcomplete hooks
  4776. (set (make-local-variable 'pcomplete-command-completion-function)
  4777. 'org-pcomplete-initial)
  4778. (set (make-local-variable 'pcomplete-command-name-function)
  4779. 'org-command-at-point)
  4780. (set (make-local-variable 'pcomplete-default-completion-function)
  4781. 'ignore)
  4782. (set (make-local-variable 'pcomplete-parse-arguments-function)
  4783. 'org-parse-arguments)
  4784. (set (make-local-variable 'pcomplete-termination-string) "")
  4785. (when (>= emacs-major-version 23)
  4786. (set (make-local-variable 'buffer-face-mode-face) 'org-default))
  4787. ;; If empty file that did not turn on org-mode automatically, make it to.
  4788. (if (and org-insert-mode-line-in-empty-file
  4789. (org-called-interactively-p 'any)
  4790. (= (point-min) (point-max)))
  4791. (insert "# -*- mode: org -*-\n\n"))
  4792. (unless org-inhibit-startup
  4793. (org-unmodified
  4794. (and org-startup-with-beamer-mode (org-beamer-mode))
  4795. (when org-startup-align-all-tables
  4796. (org-table-map-tables 'org-table-align 'quietly))
  4797. (when org-startup-with-inline-images
  4798. (org-display-inline-images))
  4799. (when org-startup-with-latex-preview
  4800. (org-preview-latex-fragment))
  4801. (unless org-inhibit-startup-visibility-stuff
  4802. (org-set-startup-visibility))))
  4803. ;; Try to set org-hide correctly
  4804. (set-face-foreground 'org-hide (org-find-invisible-foreground)))
  4805. ;; Update `customize-package-emacs-version-alist'
  4806. (add-to-list 'customize-package-emacs-version-alist
  4807. '(Org ("6.21b" . "23.1") ("6.33x" . "23.2")
  4808. ("7.8.11" . "24.1") ("7.9.4" . "24.3")
  4809. ("8.0" . "24.4")))
  4810. (defvar org-mode-transpose-word-syntax-table
  4811. (let ((st (make-syntax-table)))
  4812. (mapc (lambda(c) (modify-syntax-entry
  4813. (string-to-char (car c)) "w p" st))
  4814. org-emphasis-alist)
  4815. st))
  4816. (when (fboundp 'abbrev-table-put)
  4817. (abbrev-table-put org-mode-abbrev-table
  4818. :parents (list text-mode-abbrev-table)))
  4819. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4820. (defsubst org-fix-ellipsis-at-bol ()
  4821. (save-excursion (goto-char (window-start)) (recenter 0)))
  4822. (defun org-find-invisible-foreground ()
  4823. (let ((candidates (remove
  4824. "unspecified-bg"
  4825. (nconc
  4826. (list (face-background 'default)
  4827. (face-background 'org-default))
  4828. (mapcar
  4829. (lambda (alist)
  4830. (when (boundp alist)
  4831. (cdr (assoc 'background-color (symbol-value alist)))))
  4832. '(default-frame-alist initial-frame-alist window-system-default-frame-alist))
  4833. (list (face-foreground 'org-hide))))))
  4834. (car (remove nil candidates))))
  4835. (defun org-current-time (&optional rounding-minutes past)
  4836. "Current time, possibly rounded to ROUNDING-MINUTES.
  4837. When ROUNDING-MINUTES is not an integer, fall back on the car of
  4838. `org-time-stamp-rounding-minutes'. When PAST is non-nil, ensure
  4839. the rounding returns a past time."
  4840. (let ((r (or (and (integerp rounding-minutes) rounding-minutes)
  4841. (car org-time-stamp-rounding-minutes)))
  4842. (time (decode-time)) res)
  4843. (if (< r 1)
  4844. (current-time)
  4845. (setq res
  4846. (apply 'encode-time
  4847. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4848. (nthcdr 2 time))))
  4849. (if (and past (< (org-float-time (time-subtract (current-time) res)) 0))
  4850. (seconds-to-time (- (org-float-time res) (* r 60)))
  4851. res))))
  4852. (defun org-today ()
  4853. "Return today date, considering `org-extend-today-until'."
  4854. (time-to-days
  4855. (time-subtract (current-time)
  4856. (list 0 (* 3600 org-extend-today-until) 0))))
  4857. ;;;; Font-Lock stuff, including the activators
  4858. (defvar org-mouse-map (make-sparse-keymap))
  4859. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4860. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4861. (when org-mouse-1-follows-link
  4862. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4863. (when org-tab-follows-link
  4864. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4865. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4866. (require 'font-lock)
  4867. (defconst org-non-link-chars "]\t\n\r<>")
  4868. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  4869. "shell" "elisp" "doi" "message"))
  4870. (defvar org-link-types-re nil
  4871. "Matches a link that has a url-like prefix like \"http:\"")
  4872. (defvar org-link-re-with-space nil
  4873. "Matches a link with spaces, optional angular brackets around it.")
  4874. (defvar org-link-re-with-space2 nil
  4875. "Matches a link with spaces, optional angular brackets around it.")
  4876. (defvar org-link-re-with-space3 nil
  4877. "Matches a link with spaces, only for internal part in bracket links.")
  4878. (defvar org-angle-link-re nil
  4879. "Matches link with angular brackets, spaces are allowed.")
  4880. (defvar org-plain-link-re nil
  4881. "Matches plain link, without spaces.")
  4882. (defvar org-bracket-link-regexp nil
  4883. "Matches a link in double brackets.")
  4884. (defvar org-bracket-link-analytic-regexp nil
  4885. "Regular expression used to analyze links.
  4886. Here is what the match groups contain after a match:
  4887. 1: http:
  4888. 2: http
  4889. 3: path
  4890. 4: [desc]
  4891. 5: desc")
  4892. (defvar org-bracket-link-analytic-regexp++ nil
  4893. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4894. (defvar org-any-link-re nil
  4895. "Regular expression matching any link.")
  4896. (defconst org-match-sexp-depth 3
  4897. "Number of stacked braces for sub/superscript matching.")
  4898. (defun org-create-multibrace-regexp (left right n)
  4899. "Create a regular expression which will match a balanced sexp.
  4900. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4901. as single character strings.
  4902. The regexp returned will match the entire expression including the
  4903. delimiters. It will also define a single group which contains the
  4904. match except for the outermost delimiters. The maximum depth of
  4905. stacked delimiters is N. Escaping delimiters is not possible."
  4906. (let* ((nothing (concat "[^" left right "]*?"))
  4907. (or "\\|")
  4908. (re nothing)
  4909. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4910. (while (> n 1)
  4911. (setq n (1- n)
  4912. re (concat re or next)
  4913. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4914. (concat left "\\(" re "\\)" right)))
  4915. (defvar org-match-substring-regexp
  4916. (concat
  4917. "\\(\\S-\\)\\([_^]\\)\\("
  4918. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4919. "\\|"
  4920. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  4921. "\\|"
  4922. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  4923. "The regular expression matching a sub- or superscript.")
  4924. (defvar org-match-substring-with-braces-regexp
  4925. (concat
  4926. "\\(\\S-\\)\\([_^]\\)\\("
  4927. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4928. "\\)")
  4929. "The regular expression matching a sub- or superscript, forcing braces.")
  4930. (defun org-make-link-regexps ()
  4931. "Update the link regular expressions.
  4932. This should be called after the variable `org-link-types' has changed."
  4933. (setq org-link-types-re
  4934. (concat
  4935. "\\`\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):")
  4936. org-link-re-with-space
  4937. (concat
  4938. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4939. "\\([^" org-non-link-chars " ]"
  4940. "[^" org-non-link-chars "]*"
  4941. "[^" org-non-link-chars " ]\\)>?")
  4942. org-link-re-with-space2
  4943. (concat
  4944. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4945. "\\([^" org-non-link-chars " ]"
  4946. "[^\t\n\r]*"
  4947. "[^" org-non-link-chars " ]\\)>?")
  4948. org-link-re-with-space3
  4949. (concat
  4950. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4951. "\\([^" org-non-link-chars " ]"
  4952. "[^\t\n\r]*\\)")
  4953. org-angle-link-re
  4954. (concat
  4955. "<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4956. "\\([^" org-non-link-chars " ]"
  4957. "[^" org-non-link-chars "]*"
  4958. "\\)>")
  4959. org-plain-link-re
  4960. (concat
  4961. "\\<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4962. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  4963. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4964. org-bracket-link-regexp
  4965. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4966. org-bracket-link-analytic-regexp
  4967. (concat
  4968. "\\[\\["
  4969. "\\(\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):\\)?"
  4970. "\\([^]]+\\)"
  4971. "\\]"
  4972. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4973. "\\]")
  4974. org-bracket-link-analytic-regexp++
  4975. (concat
  4976. "\\[\\["
  4977. "\\(\\(" (mapconcat 'regexp-quote (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  4978. "\\([^]]+\\)"
  4979. "\\]"
  4980. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4981. "\\]")
  4982. org-any-link-re
  4983. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4984. org-angle-link-re "\\)\\|\\("
  4985. org-plain-link-re "\\)")))
  4986. (org-make-link-regexps)
  4987. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
  4988. "Regular expression for fast time stamp matching.")
  4989. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
  4990. "Regular expression for fast time stamp matching.")
  4991. (defconst org-ts-regexp0
  4992. "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\( +[^]+0-9>\r\n -]+\\)?\\( +\\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4993. "Regular expression matching time strings for analysis.
  4994. This one does not require the space after the date, so it can be used
  4995. on a string that terminates immediately after the date.")
  4996. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4997. "Regular expression matching time strings for analysis.")
  4998. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  4999. "Regular expression matching time stamps, with groups.")
  5000. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  5001. "Regular expression matching time stamps (also [..]), with groups.")
  5002. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  5003. "Regular expression matching a time stamp range.")
  5004. (defconst org-tr-regexp-both
  5005. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  5006. "Regular expression matching a time stamp range.")
  5007. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  5008. org-ts-regexp "\\)?")
  5009. "Regular expression matching a time stamp or time stamp range.")
  5010. (defconst org-tsr-regexp-both
  5011. (concat org-ts-regexp-both "\\(--?-?"
  5012. org-ts-regexp-both "\\)?")
  5013. "Regular expression matching a time stamp or time stamp range.
  5014. The time stamps may be either active or inactive.")
  5015. (defvar org-emph-face nil)
  5016. (defun org-do-emphasis-faces (limit)
  5017. "Run through the buffer and add overlays to emphasized strings."
  5018. (let (rtn a)
  5019. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  5020. (if (not (= (char-after (match-beginning 3))
  5021. (char-after (match-beginning 4))))
  5022. (progn
  5023. (setq rtn t)
  5024. (setq a (assoc (match-string 3) org-emphasis-alist))
  5025. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  5026. 'face
  5027. (nth 1 a))
  5028. (and (nth 4 a)
  5029. (org-remove-flyspell-overlays-in
  5030. (match-beginning 0) (match-end 0)))
  5031. (add-text-properties (match-beginning 2) (match-end 2)
  5032. '(font-lock-multiline t org-emphasis t))
  5033. (when org-hide-emphasis-markers
  5034. (add-text-properties (match-end 4) (match-beginning 5)
  5035. '(invisible org-link))
  5036. (add-text-properties (match-beginning 3) (match-end 3)
  5037. '(invisible org-link)))))
  5038. (backward-char 1))
  5039. rtn))
  5040. (defun org-emphasize (&optional char)
  5041. "Insert or change an emphasis, i.e. a font like bold or italic.
  5042. If there is an active region, change that region to a new emphasis.
  5043. If there is no region, just insert the marker characters and position
  5044. the cursor between them.
  5045. CHAR should be either the marker character, or the first character of the
  5046. HTML tag associated with that emphasis. If CHAR is a space, the means
  5047. to remove the emphasis of the selected region.
  5048. If char is not given (for example in an interactive call) it
  5049. will be prompted for."
  5050. (interactive)
  5051. (let ((eal org-emphasis-alist) e det
  5052. (erc org-emphasis-regexp-components)
  5053. (prompt "")
  5054. (string "") beg end move tag c s)
  5055. (if (org-region-active-p)
  5056. (setq beg (region-beginning) end (region-end)
  5057. string (buffer-substring beg end))
  5058. (setq move t))
  5059. (while (setq e (pop eal))
  5060. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  5061. c (aref tag 0))
  5062. (push (cons c (string-to-char (car e))) det)
  5063. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  5064. (substring tag 1)))))
  5065. (setq det (nreverse det))
  5066. (unless char
  5067. (message "%s" (concat "Emphasis marker or tag:" prompt))
  5068. (setq char (read-char-exclusive)))
  5069. (setq char (or (cdr (assoc char det)) char))
  5070. (if (equal char ?\ )
  5071. (setq s "" move nil)
  5072. (unless (assoc (char-to-string char) org-emphasis-alist)
  5073. (error "No such emphasis marker: \"%c\"" char))
  5074. (setq s (char-to-string char)))
  5075. (while (and (> (length string) 1)
  5076. (equal (substring string 0 1) (substring string -1))
  5077. (assoc (substring string 0 1) org-emphasis-alist))
  5078. (setq string (substring string 1 -1)))
  5079. (setq string (concat s string s))
  5080. (if beg (delete-region beg end))
  5081. (unless (or (bolp)
  5082. (string-match (concat "[" (nth 0 erc) "\n]")
  5083. (char-to-string (char-before (point)))))
  5084. (insert " "))
  5085. (unless (or (eobp)
  5086. (string-match (concat "[" (nth 1 erc) "\n]")
  5087. (char-to-string (char-after (point)))))
  5088. (insert " ") (backward-char 1))
  5089. (insert string)
  5090. (and move (backward-char 1))))
  5091. (defconst org-nonsticky-props
  5092. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text htmlize-link))
  5093. (defsubst org-rear-nonsticky-at (pos)
  5094. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  5095. (defun org-activate-plain-links (limit)
  5096. "Run through the buffer and add overlays to links."
  5097. (let (f hl)
  5098. (when (and (re-search-forward (concat org-plain-link-re) limit t)
  5099. (not (org-in-src-block-p)))
  5100. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5101. (setq f (get-text-property (match-beginning 0) 'face))
  5102. (setq hl (org-match-string-no-properties 0))
  5103. (if (or (eq f 'org-tag)
  5104. (and (listp f) (memq 'org-tag f)))
  5105. nil
  5106. (add-text-properties (match-beginning 0) (match-end 0)
  5107. (list 'mouse-face 'highlight
  5108. 'face 'org-link
  5109. 'htmlize-link `(:uri ,hl)
  5110. 'keymap org-mouse-map))
  5111. (org-rear-nonsticky-at (match-end 0)))
  5112. t)))
  5113. (defun org-activate-code (limit)
  5114. (if (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t)
  5115. (progn
  5116. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5117. (remove-text-properties (match-beginning 0) (match-end 0)
  5118. '(display t invisible t intangible t))
  5119. t)))
  5120. (defcustom org-src-fontify-natively nil
  5121. "When non-nil, fontify code in code blocks."
  5122. :type 'boolean
  5123. :version "24.1"
  5124. :group 'org-appearance
  5125. :group 'org-babel)
  5126. (defcustom org-allow-promoting-top-level-subtree nil
  5127. "When non-nil, allow promoting a top level subtree.
  5128. The leading star of the top level headline will be replaced
  5129. by a #."
  5130. :type 'boolean
  5131. :version "24.1"
  5132. :group 'org-appearance)
  5133. (defun org-fontify-meta-lines-and-blocks (limit)
  5134. (condition-case nil
  5135. (org-fontify-meta-lines-and-blocks-1 limit)
  5136. (error (message "org-mode fontification error"))))
  5137. (defun org-fontify-meta-lines-and-blocks-1 (limit)
  5138. "Fontify #+ lines and blocks."
  5139. (let ((case-fold-search t))
  5140. (if (re-search-forward
  5141. "^\\([ \t]*#\\(\\(\\+[a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  5142. limit t)
  5143. (let ((beg (match-beginning 0))
  5144. (block-start (match-end 0))
  5145. (block-end nil)
  5146. (lang (match-string 7))
  5147. (beg1 (line-beginning-position 2))
  5148. (dc1 (downcase (match-string 2)))
  5149. (dc3 (downcase (match-string 3)))
  5150. end end1 quoting block-type ovl)
  5151. (cond
  5152. ((member dc1 '("+html:" "+ascii:" "+latex:"))
  5153. ;; a single line of backend-specific content
  5154. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5155. (remove-text-properties (match-beginning 0) (match-end 0)
  5156. '(display t invisible t intangible t))
  5157. (add-text-properties (match-beginning 1) (match-end 3)
  5158. '(font-lock-fontified t face org-meta-line))
  5159. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  5160. '(font-lock-fontified t face org-block))
  5161. ; for backend-specific code
  5162. t)
  5163. ((and (match-end 4) (equal dc3 "+begin"))
  5164. ;; Truly a block
  5165. (setq block-type (downcase (match-string 5))
  5166. quoting (member block-type org-protecting-blocks))
  5167. (when (re-search-forward
  5168. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  5169. nil t) ;; on purpose, we look further than LIMIT
  5170. (setq end (min (point-max) (match-end 0))
  5171. end1 (min (point-max) (1- (match-beginning 0))))
  5172. (setq block-end (match-beginning 0))
  5173. (when quoting
  5174. (remove-text-properties beg end
  5175. '(display t invisible t intangible t)))
  5176. (add-text-properties
  5177. beg end
  5178. '(font-lock-fontified t font-lock-multiline t))
  5179. (add-text-properties beg beg1 '(face org-meta-line))
  5180. (add-text-properties end1 (min (point-max) (1+ end))
  5181. '(face org-meta-line)) ; for end_src
  5182. (cond
  5183. ((and lang (not (string= lang "")) org-src-fontify-natively)
  5184. (org-src-font-lock-fontify-block lang block-start block-end)
  5185. ;; remove old background overlays
  5186. (mapc (lambda (ov)
  5187. (if (eq (overlay-get ov 'face) 'org-block-background)
  5188. (delete-overlay ov)))
  5189. (overlays-at (/ (+ beg1 block-end) 2)))
  5190. ;; add a background overlay
  5191. (setq ovl (make-overlay beg1 block-end))
  5192. (overlay-put ovl 'face 'org-block-background)
  5193. (overlay-put ovl 'evaporate t)) ;; make it go away when empty
  5194. (quoting
  5195. (add-text-properties beg1 (min (point-max) (1+ end1))
  5196. '(face org-block))) ; end of source block
  5197. ((not org-fontify-quote-and-verse-blocks))
  5198. ((string= block-type "quote")
  5199. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-quote)))
  5200. ((string= block-type "verse")
  5201. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-verse))))
  5202. (add-text-properties beg beg1 '(face org-block-begin-line))
  5203. (add-text-properties (min (point-max) (1+ end)) (min (point-max) (1+ end1))
  5204. '(face org-block-end-line))
  5205. t))
  5206. ((member dc1 '("+title:" "+author:" "+email:" "+date:"))
  5207. (add-text-properties
  5208. beg (match-end 3)
  5209. (if (member (intern (substring dc1 0 -1)) org-hidden-keywords)
  5210. '(font-lock-fontified t invisible t)
  5211. '(font-lock-fontified t face org-document-info-keyword)))
  5212. (add-text-properties
  5213. (match-beginning 6) (min (point-max) (1+ (match-end 6)))
  5214. (if (string-equal dc1 "+title:")
  5215. '(font-lock-fontified t face org-document-title)
  5216. '(font-lock-fontified t face org-document-info))))
  5217. ((or (equal dc1 "+results")
  5218. (member dc1 '("+begin:" "+end:" "+caption:" "+label:"
  5219. "+orgtbl:" "+tblfm:" "+tblname:" "+results:"
  5220. "+call:" "+header:" "+headers:" "+name:"))
  5221. (and (match-end 4) (equal dc3 "+attr")))
  5222. (add-text-properties
  5223. beg (match-end 0)
  5224. '(font-lock-fontified t face org-meta-line))
  5225. t)
  5226. ((member dc3 '(" " ""))
  5227. (add-text-properties
  5228. beg (match-end 0)
  5229. '(font-lock-fontified t face font-lock-comment-face)))
  5230. ((not (member (char-after beg) '(?\ ?\t)))
  5231. ;; just any other in-buffer setting, but not indented
  5232. (add-text-properties
  5233. beg (match-end 0)
  5234. '(font-lock-fontified t face org-meta-line))
  5235. t)
  5236. (t nil))))))
  5237. (defun org-activate-angle-links (limit)
  5238. "Run through the buffer and add overlays to links."
  5239. (if (and (re-search-forward org-angle-link-re limit t)
  5240. (not (org-in-src-block-p)))
  5241. (progn
  5242. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5243. (add-text-properties (match-beginning 0) (match-end 0)
  5244. (list 'mouse-face 'highlight
  5245. 'keymap org-mouse-map))
  5246. (org-rear-nonsticky-at (match-end 0))
  5247. t)))
  5248. (defun org-activate-footnote-links (limit)
  5249. "Run through the buffer and add overlays to footnotes."
  5250. (let ((fn (org-footnote-next-reference-or-definition limit)))
  5251. (when fn
  5252. (let ((beg (nth 1 fn)) (end (nth 2 fn)))
  5253. (org-remove-flyspell-overlays-in beg end)
  5254. (add-text-properties beg end
  5255. (list 'mouse-face 'highlight
  5256. 'keymap org-mouse-map
  5257. 'help-echo
  5258. (if (= (point-at-bol) beg)
  5259. "Footnote definition"
  5260. "Footnote reference")
  5261. 'font-lock-fontified t
  5262. 'font-lock-multiline t
  5263. 'face 'org-footnote))))))
  5264. (defun org-activate-bracket-links (limit)
  5265. "Run through the buffer and add overlays to bracketed links."
  5266. (if (and (re-search-forward org-bracket-link-regexp limit t)
  5267. (not (org-in-src-block-p)))
  5268. (let* ((hl (org-match-string-no-properties 1))
  5269. (help (concat "LINK: " hl))
  5270. ;; FIXME: Above we should remove the escapes. But that
  5271. ;; requires another match, protecting match data, a lot
  5272. ;; of overhead for font-lock.
  5273. (ip (org-maybe-intangible
  5274. (list 'invisible 'org-link
  5275. 'keymap org-mouse-map 'mouse-face 'highlight
  5276. 'font-lock-multiline t 'help-echo help
  5277. 'htmlize-link `(:uri ,hl))))
  5278. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  5279. 'font-lock-multiline t 'help-echo help
  5280. 'htmlize-link `(:uri ,hl))))
  5281. ;; We need to remove the invisible property here. Table narrowing
  5282. ;; may have made some of this invisible.
  5283. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5284. (remove-text-properties (match-beginning 0) (match-end 0)
  5285. '(invisible nil))
  5286. (if (match-end 3)
  5287. (progn
  5288. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  5289. (org-rear-nonsticky-at (match-beginning 3))
  5290. (add-text-properties (match-beginning 3) (match-end 3) vp)
  5291. (org-rear-nonsticky-at (match-end 3))
  5292. (add-text-properties (match-end 3) (match-end 0) ip)
  5293. (org-rear-nonsticky-at (match-end 0)))
  5294. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  5295. (org-rear-nonsticky-at (match-beginning 1))
  5296. (add-text-properties (match-beginning 1) (match-end 1) vp)
  5297. (org-rear-nonsticky-at (match-end 1))
  5298. (add-text-properties (match-end 1) (match-end 0) ip)
  5299. (org-rear-nonsticky-at (match-end 0)))
  5300. t)))
  5301. (defun org-activate-dates (limit)
  5302. "Run through the buffer and add overlays to dates."
  5303. (if (and (re-search-forward org-tsr-regexp-both limit t)
  5304. (not (equal (char-before (match-beginning 0)) 91)))
  5305. (progn
  5306. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5307. (add-text-properties (match-beginning 0) (match-end 0)
  5308. (list 'mouse-face 'highlight
  5309. 'keymap org-mouse-map))
  5310. (org-rear-nonsticky-at (match-end 0))
  5311. (when org-display-custom-times
  5312. (if (match-end 3)
  5313. (org-display-custom-time (match-beginning 3) (match-end 3)))
  5314. (org-display-custom-time (match-beginning 1) (match-end 1)))
  5315. t)))
  5316. (defvar org-target-link-regexp nil
  5317. "Regular expression matching radio targets in plain text.")
  5318. (make-variable-buffer-local 'org-target-link-regexp)
  5319. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  5320. "Regular expression matching a link target.")
  5321. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  5322. "Regular expression matching a radio target.")
  5323. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  5324. "Regular expression matching any target.")
  5325. (defun org-activate-target-links (limit)
  5326. "Run through the buffer and add overlays to target matches."
  5327. (when org-target-link-regexp
  5328. (let ((case-fold-search t))
  5329. (if (re-search-forward org-target-link-regexp limit t)
  5330. (progn
  5331. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5332. (add-text-properties (match-beginning 0) (match-end 0)
  5333. (list 'mouse-face 'highlight
  5334. 'keymap org-mouse-map
  5335. 'help-echo "Radio target link"
  5336. 'org-linked-text t))
  5337. (org-rear-nonsticky-at (match-end 0))
  5338. t)))))
  5339. (defun org-update-radio-target-regexp ()
  5340. "Find all radio targets in this file and update the regular expression."
  5341. (interactive)
  5342. (when (memq 'radio org-activate-links)
  5343. (setq org-target-link-regexp
  5344. (org-make-target-link-regexp (org-all-targets 'radio)))
  5345. (org-restart-font-lock)))
  5346. (defun org-hide-wide-columns (limit)
  5347. (let (s e)
  5348. (setq s (text-property-any (point) (or limit (point-max))
  5349. 'org-cwidth t))
  5350. (when s
  5351. (setq e (next-single-property-change s 'org-cwidth))
  5352. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  5353. (goto-char e)
  5354. t)))
  5355. (defvar org-latex-and-related-regexp nil
  5356. "Regular expression for highlighting LaTeX, entities and sub/superscript.")
  5357. (defvar org-match-substring-regexp)
  5358. (defvar org-match-substring-with-braces-regexp)
  5359. (defun org-compute-latex-and-related-regexp ()
  5360. "Compute regular expression for LaTeX, entities and sub/superscript.
  5361. Result depends on variable `org-highlight-latex-and-related'."
  5362. (org-set-local
  5363. 'org-latex-and-related-regexp
  5364. (let* ((re-sub
  5365. (cond ((not (memq 'script org-highlight-latex-and-related)) nil)
  5366. ((eq org-use-sub-superscripts '{})
  5367. (list org-match-substring-with-braces-regexp))
  5368. (org-use-sub-superscripts (list org-match-substring-regexp))))
  5369. (re-latex
  5370. (when (memq 'latex org-highlight-latex-and-related)
  5371. (let ((matchers (plist-get org-format-latex-options :matchers)))
  5372. (delq nil
  5373. (mapcar (lambda (x)
  5374. (and (member (car x) matchers) (nth 1 x)))
  5375. org-latex-regexps)))))
  5376. (re-entities
  5377. (when (memq 'entities org-highlight-latex-and-related)
  5378. (list "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))))
  5379. (mapconcat 'identity (append re-latex re-entities re-sub) "\\|"))))
  5380. (defun org-do-latex-and-related (limit)
  5381. "Highlight LaTeX snippets and environments, entities and sub/superscript.
  5382. LIMIT bounds the search for syntax to highlight. Stop at first
  5383. highlighted object, if any. Return t if some highlighting was
  5384. done, nil otherwise."
  5385. (when (org-string-nw-p org-latex-and-related-regexp)
  5386. (catch 'found
  5387. (while (re-search-forward org-latex-and-related-regexp limit t)
  5388. (unless (memq (car-safe (get-text-property (1+ (match-beginning 0))
  5389. 'face))
  5390. '(org-code org-verbatim underline))
  5391. (let ((offset (if (memq (char-after (1+ (match-beginning 0)))
  5392. '(?_ ?^))
  5393. 1
  5394. 0)))
  5395. (font-lock-prepend-text-property
  5396. (+ offset (match-beginning 0)) (match-end 0)
  5397. 'face 'org-latex-and-related)
  5398. (add-text-properties (+ offset (match-beginning 0)) (match-end 0)
  5399. '(font-lock-multiline t)))
  5400. (throw 'found t)))
  5401. nil)))
  5402. (defun org-restart-font-lock ()
  5403. "Restart `font-lock-mode', to force refontification."
  5404. (when (and (boundp 'font-lock-mode) font-lock-mode)
  5405. (font-lock-mode -1)
  5406. (font-lock-mode 1)))
  5407. (defun org-all-targets (&optional radio)
  5408. "Return a list of all targets in this file.
  5409. When optional argument RADIO is non-nil, only find radio
  5410. targets."
  5411. (let ((re (if radio org-radio-target-regexp org-target-regexp)) rtn)
  5412. (save-excursion
  5413. (goto-char (point-min))
  5414. (while (re-search-forward re nil t)
  5415. ;; Make sure point is really within the object.
  5416. (backward-char)
  5417. (let ((obj (org-element-context)))
  5418. (when (memq (org-element-type obj) '(radio-target target))
  5419. (add-to-list 'rtn (downcase (org-element-property :value obj))))))
  5420. rtn)))
  5421. (defun org-make-target-link-regexp (targets)
  5422. "Make regular expression matching all strings in TARGETS.
  5423. The regular expression finds the targets also if there is a line break
  5424. between words."
  5425. (and targets
  5426. (concat
  5427. "\\<\\("
  5428. (mapconcat
  5429. (lambda (x)
  5430. (setq x (regexp-quote x))
  5431. (while (string-match " +" x)
  5432. (setq x (replace-match "\\s-+" t t x)))
  5433. x)
  5434. targets
  5435. "\\|")
  5436. "\\)\\>")))
  5437. (defun org-activate-tags (limit)
  5438. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t)
  5439. (progn
  5440. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5441. (add-text-properties (match-beginning 1) (match-end 1)
  5442. (list 'mouse-face 'highlight
  5443. 'keymap org-mouse-map))
  5444. (org-rear-nonsticky-at (match-end 1))
  5445. t)))
  5446. (defun org-outline-level ()
  5447. "Compute the outline level of the heading at point.
  5448. If this is called at a normal headline, the level is the number of stars.
  5449. Use `org-reduced-level' to remove the effect of `org-odd-levels'."
  5450. (save-excursion
  5451. (if (not (condition-case nil
  5452. (org-back-to-heading t)
  5453. (error nil)))
  5454. 0
  5455. (looking-at org-outline-regexp)
  5456. (1- (- (match-end 0) (match-beginning 0))))))
  5457. (defvar org-font-lock-keywords nil)
  5458. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\+?\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  5459. "Regular expression matching a property line.")
  5460. (defvar org-font-lock-hook nil
  5461. "Functions to be called for special font lock stuff.")
  5462. (defvar org-font-lock-set-keywords-hook nil
  5463. "Functions that can manipulate `org-font-lock-extra-keywords'.
  5464. This is called after `org-font-lock-extra-keywords' is defined, but before
  5465. it is installed to be used by font lock. This can be useful if something
  5466. needs to be inserted at a specific position in the font-lock sequence.")
  5467. (defun org-font-lock-hook (limit)
  5468. "Run `org-font-lock-hook' within LIMIT."
  5469. (run-hook-with-args 'org-font-lock-hook limit))
  5470. (defun org-set-font-lock-defaults ()
  5471. "Set font lock defaults for the current buffer."
  5472. (let* ((em org-fontify-emphasized-text)
  5473. (lk org-activate-links)
  5474. (org-font-lock-extra-keywords
  5475. (list
  5476. ;; Call the hook
  5477. '(org-font-lock-hook)
  5478. ;; Headlines
  5479. `(,(if org-fontify-whole-heading-line
  5480. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  5481. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  5482. (1 (org-get-level-face 1))
  5483. (2 (org-get-level-face 2))
  5484. (3 (org-get-level-face 3)))
  5485. ;; Table lines
  5486. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  5487. (1 'org-table t))
  5488. ;; Table internals
  5489. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  5490. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  5491. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  5492. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  5493. ;; Drawers
  5494. (list org-drawer-regexp '(0 'org-special-keyword t))
  5495. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  5496. ;; Properties
  5497. (list org-property-re
  5498. '(1 'org-special-keyword t)
  5499. '(3 'org-property-value t))
  5500. ;; Links
  5501. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  5502. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  5503. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  5504. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  5505. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  5506. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  5507. (if (memq 'footnote lk) '(org-activate-footnote-links))
  5508. ;; Targets.
  5509. (list org-any-target-regexp '(0 'org-target t))
  5510. ;; Diary sexps.
  5511. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  5512. ;; Macro
  5513. '("{{{.+}}}" (0 'org-macro t))
  5514. '(org-hide-wide-columns (0 nil append))
  5515. ;; TODO keyword
  5516. (list (format org-heading-keyword-regexp-format
  5517. org-todo-regexp)
  5518. '(2 (org-get-todo-face 2) t))
  5519. ;; DONE
  5520. (if org-fontify-done-headline
  5521. (list (format org-heading-keyword-regexp-format
  5522. (concat
  5523. "\\(?:"
  5524. (mapconcat 'regexp-quote org-done-keywords "\\|")
  5525. "\\)"))
  5526. '(2 'org-headline-done t))
  5527. nil)
  5528. ;; Priorities
  5529. '(org-font-lock-add-priority-faces)
  5530. ;; Tags
  5531. '(org-font-lock-add-tag-faces)
  5532. ;; Tags groups
  5533. (if (and org-group-tags org-tag-groups-alist)
  5534. (list (concat org-outline-regexp-bol ".+\\(:"
  5535. (regexp-opt (mapcar 'car org-tag-groups-alist))
  5536. ":\\).*$")
  5537. '(1 'org-tag-group prepend)))
  5538. ;; Special keywords
  5539. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  5540. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  5541. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  5542. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  5543. ;; Emphasis
  5544. (if em
  5545. (if (featurep 'xemacs)
  5546. '(org-do-emphasis-faces (0 nil append))
  5547. '(org-do-emphasis-faces)))
  5548. ;; Checkboxes
  5549. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  5550. 1 'org-checkbox prepend)
  5551. (if (cdr (assq 'checkbox org-list-automatic-rules))
  5552. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5553. (0 (org-get-checkbox-statistics-face) t)))
  5554. ;; Description list items
  5555. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  5556. 1 'org-list-dt prepend)
  5557. ;; ARCHIVEd headings
  5558. (list (concat
  5559. org-outline-regexp-bol
  5560. "\\(.*:" org-archive-tag ":.*\\)")
  5561. '(1 'org-archived prepend))
  5562. ;; Specials
  5563. '(org-do-latex-and-related)
  5564. '(org-fontify-entities)
  5565. '(org-raise-scripts)
  5566. ;; Code
  5567. '(org-activate-code (1 'org-code t))
  5568. ;; COMMENT
  5569. (list (format org-heading-keyword-regexp-format
  5570. (concat "\\("
  5571. org-comment-string "\\|" org-quote-string
  5572. "\\)"))
  5573. '(2 'org-special-keyword t))
  5574. ;; Blocks and meta lines
  5575. '(org-fontify-meta-lines-and-blocks)
  5576. )))
  5577. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5578. (run-hooks 'org-font-lock-set-keywords-hook)
  5579. ;; Now set the full font-lock-keywords
  5580. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  5581. (org-set-local 'font-lock-defaults
  5582. '(org-font-lock-keywords t nil nil backward-paragraph))
  5583. (kill-local-variable 'font-lock-keywords) nil))
  5584. (defun org-toggle-pretty-entities ()
  5585. "Toggle the composition display of entities as UTF8 characters."
  5586. (interactive)
  5587. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  5588. (org-restart-font-lock)
  5589. (if org-pretty-entities
  5590. (message "Entities are now displayed as UTF8 characters")
  5591. (save-restriction
  5592. (widen)
  5593. (org-decompose-region (point-min) (point-max))
  5594. (message "Entities are now displayed as plain text"))))
  5595. (defvar org-custom-properties-overlays nil
  5596. "List of overlays used for custom properties.")
  5597. (make-variable-buffer-local 'org-custom-properties-overlays)
  5598. (defun org-toggle-custom-properties-visibility ()
  5599. "Display or hide properties in `org-custom-properties'."
  5600. (interactive)
  5601. (if org-custom-properties-overlays
  5602. (progn (mapc 'delete-overlay org-custom-properties-overlays)
  5603. (setq org-custom-properties-overlays nil))
  5604. (unless (not org-custom-properties)
  5605. (save-excursion
  5606. (save-restriction
  5607. (widen)
  5608. (goto-char (point-min))
  5609. (while (re-search-forward org-property-re nil t)
  5610. (mapc (lambda(p)
  5611. (when (equal p (substring (match-string 1) 1 -1))
  5612. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5613. (overlay-put o 'invisible t)
  5614. (overlay-put o 'org-custom-property t)
  5615. (push o org-custom-properties-overlays))))
  5616. org-custom-properties)))))))
  5617. (defun org-fontify-entities (limit)
  5618. "Find an entity to fontify."
  5619. (let (ee)
  5620. (when org-pretty-entities
  5621. (catch 'match
  5622. (while (re-search-forward
  5623. "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]\n]\\)"
  5624. limit t)
  5625. (if (and (not (org-in-indented-comment-line))
  5626. (setq ee (org-entity-get (match-string 1)))
  5627. (= (length (nth 6 ee)) 1))
  5628. (let*
  5629. ((end (if (equal (match-string 2) "{}")
  5630. (match-end 2)
  5631. (match-end 1))))
  5632. (add-text-properties
  5633. (match-beginning 0) end
  5634. (list 'font-lock-fontified t))
  5635. (compose-region (match-beginning 0) end
  5636. (nth 6 ee) nil)
  5637. (backward-char 1)
  5638. (throw 'match t))))
  5639. nil))))
  5640. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5641. "Fontify string S like in Org-mode."
  5642. (with-temp-buffer
  5643. (insert s)
  5644. (let ((org-odd-levels-only odd-levels))
  5645. (org-mode)
  5646. (font-lock-fontify-buffer)
  5647. (buffer-string))))
  5648. (defvar org-m nil)
  5649. (defvar org-l nil)
  5650. (defvar org-f nil)
  5651. (defun org-get-level-face (n)
  5652. "Get the right face for match N in font-lock matching of headlines."
  5653. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5654. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5655. (if org-cycle-level-faces
  5656. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5657. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5658. (cond
  5659. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5660. ((eq n 2) org-f)
  5661. (t (if org-level-color-stars-only nil org-f))))
  5662. (defun org-get-todo-face (kwd)
  5663. "Get the right face for a TODO keyword KWD.
  5664. If KWD is a number, get the corresponding match group."
  5665. (if (numberp kwd) (setq kwd (match-string kwd)))
  5666. (or (org-face-from-face-or-color
  5667. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5668. (and (member kwd org-done-keywords) 'org-done)
  5669. 'org-todo))
  5670. (defun org-face-from-face-or-color (context inherit face-or-color)
  5671. "Create a face list that inherits INHERIT, but sets the foreground color.
  5672. When FACE-OR-COLOR is not a string, just return it."
  5673. (if (stringp face-or-color)
  5674. (list :inherit inherit
  5675. (cdr (assoc context org-faces-easy-properties))
  5676. face-or-color)
  5677. face-or-color))
  5678. (defun org-font-lock-add-tag-faces (limit)
  5679. "Add the special tag faces."
  5680. (when (and org-tag-faces org-tags-special-faces-re)
  5681. (while (re-search-forward org-tags-special-faces-re limit t)
  5682. (add-text-properties (match-beginning 1) (match-end 1)
  5683. (list 'face (org-get-tag-face 1)
  5684. 'font-lock-fontified t))
  5685. (backward-char 1))))
  5686. (defun org-font-lock-add-priority-faces (limit)
  5687. "Add the special priority faces."
  5688. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  5689. (when (save-match-data (org-at-heading-p))
  5690. (add-text-properties
  5691. (match-beginning 0) (match-end 0)
  5692. (list 'face (or (org-face-from-face-or-color
  5693. 'priority 'org-priority
  5694. (cdr (assoc (char-after (match-beginning 1))
  5695. org-priority-faces)))
  5696. 'org-priority)
  5697. 'font-lock-fontified t)))))
  5698. (defun org-get-tag-face (kwd)
  5699. "Get the right face for a TODO keyword KWD.
  5700. If KWD is a number, get the corresponding match group."
  5701. (if (numberp kwd) (setq kwd (match-string kwd)))
  5702. (or (org-face-from-face-or-color
  5703. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  5704. 'org-tag))
  5705. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5706. "Remove fontification and activation overlays from links."
  5707. (font-lock-default-unfontify-region beg end)
  5708. (let* ((buffer-undo-list t)
  5709. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5710. (inhibit-modification-hooks t)
  5711. deactivate-mark buffer-file-name buffer-file-truename)
  5712. (org-decompose-region beg end)
  5713. (remove-text-properties beg end
  5714. '(mouse-face t keymap t org-linked-text t
  5715. invisible t intangible t
  5716. org-no-flyspell t org-emphasis t))
  5717. (org-remove-font-lock-display-properties beg end)))
  5718. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5719. ((raise 0.3) (height 0.7))
  5720. ((raise -0.5))
  5721. ((raise 0.5)))
  5722. "Display properties for showing superscripts and subscripts.")
  5723. (defun org-remove-font-lock-display-properties (beg end)
  5724. "Remove specific display properties that have been added by font lock.
  5725. The will remove the raise properties that are used to show superscripts
  5726. and subscripts."
  5727. (let (next prop)
  5728. (while (< beg end)
  5729. (setq next (next-single-property-change beg 'display nil end)
  5730. prop (get-text-property beg 'display))
  5731. (if (member prop org-script-display)
  5732. (put-text-property beg next 'display nil))
  5733. (setq beg next))))
  5734. (defun org-raise-scripts (limit)
  5735. "Add raise properties to sub/superscripts."
  5736. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts)
  5737. (if (re-search-forward
  5738. (if (eq org-use-sub-superscripts t)
  5739. org-match-substring-regexp
  5740. org-match-substring-with-braces-regexp)
  5741. limit t)
  5742. (let* ((pos (point)) table-p comment-p
  5743. (mpos (match-beginning 3))
  5744. (emph-p (get-text-property mpos 'org-emphasis))
  5745. (link-p (get-text-property mpos 'mouse-face))
  5746. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5747. (goto-char (point-at-bol))
  5748. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5749. comment-p (org-looking-at-p "^[ \t]*#[ +]"))
  5750. (goto-char pos)
  5751. ;; Handle a_b^c
  5752. (if (member (char-after) '(?_ ?^)) (goto-char (1- pos)))
  5753. (if (or comment-p emph-p link-p keyw-p)
  5754. t
  5755. (put-text-property (match-beginning 3) (match-end 0)
  5756. 'display
  5757. (if (equal (char-after (match-beginning 2)) ?^)
  5758. (nth (if table-p 3 1) org-script-display)
  5759. (nth (if table-p 2 0) org-script-display)))
  5760. (add-text-properties (match-beginning 2) (match-end 2)
  5761. (list 'invisible t
  5762. 'org-dwidth t 'org-dwidth-n 1))
  5763. (if (and (eq (char-after (match-beginning 3)) ?{)
  5764. (eq (char-before (match-end 3)) ?}))
  5765. (progn
  5766. (add-text-properties
  5767. (match-beginning 3) (1+ (match-beginning 3))
  5768. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5769. (add-text-properties
  5770. (1- (match-end 3)) (match-end 3)
  5771. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))))
  5772. t)))))
  5773. ;;;; Visibility cycling, including org-goto and indirect buffer
  5774. ;;; Cycling
  5775. (defvar org-cycle-global-status nil)
  5776. (make-variable-buffer-local 'org-cycle-global-status)
  5777. (put 'org-cycle-global-status 'org-state t)
  5778. (defvar org-cycle-subtree-status nil)
  5779. (make-variable-buffer-local 'org-cycle-subtree-status)
  5780. (put 'org-cycle-subtree-status 'org-state t)
  5781. (defvar org-inlinetask-min-level)
  5782. ;;;###autoload
  5783. (defun org-cycle (&optional arg)
  5784. "TAB-action and visibility cycling for Org-mode.
  5785. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5786. is outline visibility cycling, but it also invokes other actions
  5787. in special contexts.
  5788. - When this function is called with a prefix argument, rotate the entire
  5789. buffer through 3 states (global cycling)
  5790. 1. OVERVIEW: Show only top-level headlines.
  5791. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5792. 3. SHOW ALL: Show everything.
  5793. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  5794. determined by the variable `org-startup-folded', and by any VISIBILITY
  5795. properties in the buffer.
  5796. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  5797. including any drawers.
  5798. - When inside a table, re-align the table and move to the next field.
  5799. - When point is at the beginning of a headline, rotate the subtree started
  5800. by this line through 3 different states (local cycling)
  5801. 1. FOLDED: Only the main headline is shown.
  5802. 2. CHILDREN: The main headline and the direct children are shown.
  5803. From this state, you can move to one of the children
  5804. and zoom in further.
  5805. 3. SUBTREE: Show the entire subtree, including body text.
  5806. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5807. - When point is at the beginning of an empty headline and the variable
  5808. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5809. of the headline by demoting and promoting it to likely levels. This
  5810. speeds up creation document structure by pressing TAB once or several
  5811. times right after creating a new headline.
  5812. - When there is a numeric prefix, go up to a heading with level ARG, do
  5813. a `show-subtree' and return to the previous cursor position. If ARG
  5814. is negative, go up that many levels.
  5815. - When point is not at the beginning of a headline, execute the global
  5816. binding for TAB, which is re-indenting the line. See the option
  5817. `org-cycle-emulate-tab' for details.
  5818. - Special case: if point is at the beginning of the buffer and there is
  5819. no headline in line 1, this function will act as if called with prefix arg
  5820. (C-u TAB, same as S-TAB) also when called without prefix arg.
  5821. But only if also the variable `org-cycle-global-at-bob' is t."
  5822. (interactive "P")
  5823. (org-load-modules-maybe)
  5824. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5825. (and org-cycle-level-after-item/entry-creation
  5826. (or (org-cycle-level)
  5827. (org-cycle-item-indentation))))
  5828. (let* (message-log-max ; Don't populate the *Messages* buffer
  5829. (limit-level
  5830. (or org-cycle-max-level
  5831. (and (boundp 'org-inlinetask-min-level)
  5832. org-inlinetask-min-level
  5833. (1- org-inlinetask-min-level))))
  5834. (nstars (and limit-level
  5835. (if org-odd-levels-only
  5836. (and limit-level (1- (* limit-level 2)))
  5837. limit-level)))
  5838. (org-outline-regexp
  5839. (if (not (derived-mode-p 'org-mode))
  5840. outline-regexp
  5841. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
  5842. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5843. (not (looking-at org-outline-regexp))))
  5844. (org-cycle-hook
  5845. (if bob-special
  5846. (delq 'org-optimize-window-after-visibility-change
  5847. (copy-sequence org-cycle-hook))
  5848. org-cycle-hook))
  5849. (pos (point)))
  5850. (if (or bob-special (equal arg '(4)))
  5851. ;; special case: use global cycling
  5852. (setq arg t))
  5853. (cond
  5854. ((equal arg '(16))
  5855. (setq last-command 'dummy)
  5856. (org-set-startup-visibility)
  5857. (message "Startup visibility, plus VISIBILITY properties"))
  5858. ((equal arg '(64))
  5859. (show-all)
  5860. (message "Entire buffer visible, including drawers"))
  5861. ;; Table: enter it or move to the next field.
  5862. ((org-at-table-p 'any)
  5863. (if (org-at-table.el-p)
  5864. (message "Use C-c ' to edit table.el tables")
  5865. (if arg (org-table-edit-field t)
  5866. (org-table-justify-field-maybe)
  5867. (call-interactively 'org-table-next-field))))
  5868. ((run-hook-with-args-until-success
  5869. 'org-tab-after-check-for-table-hook))
  5870. ;; Global cycling: delegate to `org-cycle-internal-global'.
  5871. ((eq arg t) (org-cycle-internal-global))
  5872. ;; Drawers: delegate to `org-flag-drawer'.
  5873. ((and org-drawers org-drawer-regexp
  5874. (save-excursion
  5875. (beginning-of-line 1)
  5876. (looking-at org-drawer-regexp)))
  5877. (org-flag-drawer ; toggle block visibility
  5878. (not (get-char-property (match-end 0) 'invisible))))
  5879. ;; Show-subtree, ARG levels up from here.
  5880. ((integerp arg)
  5881. (save-excursion
  5882. (org-back-to-heading)
  5883. (outline-up-heading (if (< arg 0) (- arg)
  5884. (- (funcall outline-level) arg)))
  5885. (org-show-subtree)))
  5886. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  5887. ((and (featurep 'org-inlinetask)
  5888. (org-inlinetask-at-task-p)
  5889. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5890. (org-inlinetask-toggle-visibility))
  5891. ((org-try-cdlatex-tab))
  5892. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  5893. ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
  5894. (save-excursion (beginning-of-line 1)
  5895. (looking-at org-outline-regexp)))
  5896. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5897. (org-cycle-internal-local))
  5898. ;; From there: TAB emulation and template completion.
  5899. (buffer-read-only (org-back-to-heading))
  5900. ((run-hook-with-args-until-success
  5901. 'org-tab-after-check-for-cycling-hook))
  5902. ((org-try-structure-completion))
  5903. ((run-hook-with-args-until-success
  5904. 'org-tab-before-tab-emulation-hook))
  5905. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5906. (or (not (bolp))
  5907. (not (looking-at org-outline-regexp))))
  5908. (call-interactively (global-key-binding "\t")))
  5909. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5910. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5911. (or (and (eq org-cycle-emulate-tab 'white)
  5912. (= (match-end 0) (point-at-eol)))
  5913. (and (eq org-cycle-emulate-tab 'whitestart)
  5914. (>= (match-end 0) pos))))
  5915. t
  5916. (eq org-cycle-emulate-tab t))
  5917. (call-interactively (global-key-binding "\t")))
  5918. (t (save-excursion
  5919. (org-back-to-heading)
  5920. (org-cycle)))))))
  5921. (defun org-cycle-internal-global ()
  5922. "Do the global cycling action."
  5923. ;; Hack to avoid display of messages for .org attachments in Gnus
  5924. (let (message-log-max ; Don't populate the *Messages* buffer
  5925. (ga (string-match "\\*fontification" (buffer-name))))
  5926. (cond
  5927. ((and (eq last-command this-command)
  5928. (eq org-cycle-global-status 'overview))
  5929. ;; We just created the overview - now do table of contents
  5930. ;; This can be slow in very large buffers, so indicate action
  5931. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  5932. (unless ga (message "CONTENTS..."))
  5933. (org-content)
  5934. (unless ga (message "CONTENTS...done"))
  5935. (setq org-cycle-global-status 'contents)
  5936. (run-hook-with-args 'org-cycle-hook 'contents))
  5937. ((and (eq last-command this-command)
  5938. (eq org-cycle-global-status 'contents))
  5939. ;; We just showed the table of contents - now show everything
  5940. (run-hook-with-args 'org-pre-cycle-hook 'all)
  5941. (show-all)
  5942. (unless ga (message "SHOW ALL"))
  5943. (setq org-cycle-global-status 'all)
  5944. (run-hook-with-args 'org-cycle-hook 'all))
  5945. (t
  5946. ;; Default action: go to overview
  5947. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  5948. (org-overview)
  5949. (unless ga (message "OVERVIEW"))
  5950. (setq org-cycle-global-status 'overview)
  5951. (run-hook-with-args 'org-cycle-hook 'overview)))))
  5952. (defun org-cycle-internal-local ()
  5953. "Do the local cycling action."
  5954. (let (message-log-max ; Don't populate the *Messages* buffer
  5955. (goal-column 0) eoh eol eos has-children children-skipped struct)
  5956. ;; First, determine end of headline (EOH), end of subtree or item
  5957. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  5958. (save-excursion
  5959. (if (org-at-item-p)
  5960. (progn
  5961. (beginning-of-line)
  5962. (setq struct (org-list-struct))
  5963. (setq eoh (point-at-eol))
  5964. (setq eos (org-list-get-item-end-before-blank (point) struct))
  5965. (setq has-children (org-list-has-child-p (point) struct)))
  5966. (org-back-to-heading)
  5967. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  5968. (setq eos (save-excursion (1- (org-end-of-subtree t t))))
  5969. (setq has-children
  5970. (or (save-excursion
  5971. (let ((level (funcall outline-level)))
  5972. (outline-next-heading)
  5973. (and (org-at-heading-p t)
  5974. (> (funcall outline-level) level))))
  5975. (save-excursion
  5976. (org-list-search-forward (org-item-beginning-re) eos t)))))
  5977. ;; Determine end invisible part of buffer (EOL)
  5978. (beginning-of-line 2)
  5979. ;; XEmacs doesn't have `next-single-char-property-change'
  5980. (if (featurep 'xemacs)
  5981. (while (and (not (eobp)) ;; this is like `next-line'
  5982. (get-char-property (1- (point)) 'invisible))
  5983. (beginning-of-line 2))
  5984. (while (and (not (eobp)) ;; this is like `next-line'
  5985. (get-char-property (1- (point)) 'invisible))
  5986. (goto-char (next-single-char-property-change (point) 'invisible))
  5987. (and (eolp) (beginning-of-line 2))))
  5988. (setq eol (point)))
  5989. ;; Find out what to do next and set `this-command'
  5990. (cond
  5991. ((= eos eoh)
  5992. ;; Nothing is hidden behind this heading
  5993. (unless (org-before-first-heading-p)
  5994. (run-hook-with-args 'org-pre-cycle-hook 'empty))
  5995. (message "EMPTY ENTRY")
  5996. (setq org-cycle-subtree-status nil)
  5997. (save-excursion
  5998. (goto-char eos)
  5999. (outline-next-heading)
  6000. (if (outline-invisible-p) (org-flag-heading nil))))
  6001. ((and (or (>= eol eos)
  6002. (not (string-match "\\S-" (buffer-substring eol eos))))
  6003. (or has-children
  6004. (not (setq children-skipped
  6005. org-cycle-skip-children-state-if-no-children))))
  6006. ;; Entire subtree is hidden in one line: children view
  6007. (unless (org-before-first-heading-p)
  6008. (run-hook-with-args 'org-pre-cycle-hook 'children))
  6009. (if (org-at-item-p)
  6010. (org-list-set-item-visibility (point-at-bol) struct 'children)
  6011. (org-show-entry)
  6012. (org-with-limited-levels (show-children))
  6013. ;; FIXME: This slows down the func way too much.
  6014. ;; How keep drawers hidden in subtree anyway?
  6015. ;; (when (memq 'org-cycle-hide-drawers org-cycle-hook)
  6016. ;; (org-cycle-hide-drawers 'subtree))
  6017. ;; Fold every list in subtree to top-level items.
  6018. (when (eq org-cycle-include-plain-lists 'integrate)
  6019. (save-excursion
  6020. (org-back-to-heading)
  6021. (while (org-list-search-forward (org-item-beginning-re) eos t)
  6022. (beginning-of-line 1)
  6023. (let* ((struct (org-list-struct))
  6024. (prevs (org-list-prevs-alist struct))
  6025. (end (org-list-get-bottom-point struct)))
  6026. (mapc (lambda (e) (org-list-set-item-visibility e struct 'folded))
  6027. (org-list-get-all-items (point) struct prevs))
  6028. (goto-char (if (< end eos) end eos)))))))
  6029. (message "CHILDREN")
  6030. (save-excursion
  6031. (goto-char eos)
  6032. (outline-next-heading)
  6033. (if (outline-invisible-p) (org-flag-heading nil)))
  6034. (setq org-cycle-subtree-status 'children)
  6035. (unless (org-before-first-heading-p)
  6036. (run-hook-with-args 'org-cycle-hook 'children)))
  6037. ((or children-skipped
  6038. (and (eq last-command this-command)
  6039. (eq org-cycle-subtree-status 'children)))
  6040. ;; We just showed the children, or no children are there,
  6041. ;; now show everything.
  6042. (unless (org-before-first-heading-p)
  6043. (run-hook-with-args 'org-pre-cycle-hook 'subtree))
  6044. (outline-flag-region eoh eos nil)
  6045. (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  6046. (setq org-cycle-subtree-status 'subtree)
  6047. (unless (org-before-first-heading-p)
  6048. (run-hook-with-args 'org-cycle-hook 'subtree)))
  6049. (t
  6050. ;; Default action: hide the subtree.
  6051. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  6052. (outline-flag-region eoh eos t)
  6053. (message "FOLDED")
  6054. (setq org-cycle-subtree-status 'folded)
  6055. (unless (org-before-first-heading-p)
  6056. (run-hook-with-args 'org-cycle-hook 'folded))))))
  6057. ;;;###autoload
  6058. (defun org-global-cycle (&optional arg)
  6059. "Cycle the global visibility. For details see `org-cycle'.
  6060. With \\[universal-argument] prefix arg, switch to startup visibility.
  6061. With a numeric prefix, show all headlines up to that level."
  6062. (interactive "P")
  6063. (let ((org-cycle-include-plain-lists
  6064. (if (derived-mode-p 'org-mode) org-cycle-include-plain-lists nil)))
  6065. (cond
  6066. ((integerp arg)
  6067. (show-all)
  6068. (hide-sublevels arg)
  6069. (setq org-cycle-global-status 'contents))
  6070. ((equal arg '(4))
  6071. (org-set-startup-visibility)
  6072. (message "Startup visibility, plus VISIBILITY properties."))
  6073. (t
  6074. (org-cycle '(4))))))
  6075. (defun org-set-startup-visibility ()
  6076. "Set the visibility required by startup options and properties."
  6077. (cond
  6078. ((eq org-startup-folded t)
  6079. (org-cycle '(4)))
  6080. ((eq org-startup-folded 'content)
  6081. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  6082. (org-cycle '(4)) (org-cycle '(4)))))
  6083. (unless (eq org-startup-folded 'showeverything)
  6084. (if org-hide-block-startup (org-hide-block-all))
  6085. (org-set-visibility-according-to-property 'no-cleanup)
  6086. (org-cycle-hide-archived-subtrees 'all)
  6087. (org-cycle-hide-drawers 'all)
  6088. (org-cycle-show-empty-lines t)))
  6089. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  6090. "Switch subtree visibilities according to :VISIBILITY: property."
  6091. (interactive)
  6092. (let (org-show-entry-below state)
  6093. (save-excursion
  6094. (goto-char (point-min))
  6095. (while (re-search-forward
  6096. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  6097. nil t)
  6098. (setq state (match-string 1))
  6099. (save-excursion
  6100. (org-back-to-heading t)
  6101. (hide-subtree)
  6102. (org-reveal)
  6103. (cond
  6104. ((equal state '("fold" "folded"))
  6105. (hide-subtree))
  6106. ((equal state "children")
  6107. (org-show-hidden-entry)
  6108. (show-children))
  6109. ((equal state "content")
  6110. (save-excursion
  6111. (save-restriction
  6112. (org-narrow-to-subtree)
  6113. (org-content))))
  6114. ((member state '("all" "showall"))
  6115. (show-subtree)))))
  6116. (unless no-cleanup
  6117. (org-cycle-hide-archived-subtrees 'all)
  6118. (org-cycle-hide-drawers 'all)
  6119. (org-cycle-show-empty-lines 'all)))))
  6120. ;; This function uses outline-regexp instead of the more fundamental
  6121. ;; org-outline-regexp so that org-cycle-global works outside of Org
  6122. ;; buffers, where outline-regexp is needed.
  6123. (defun org-overview ()
  6124. "Switch to overview mode, showing only top-level headlines.
  6125. Really, this shows all headlines with level equal or greater than the level
  6126. of the first headline in the buffer. This is important, because if the
  6127. first headline is not level one, then (hide-sublevels 1) gives confusing
  6128. results."
  6129. (interactive)
  6130. (let ((l (org-current-line))
  6131. (level (save-excursion
  6132. (goto-char (point-min))
  6133. (if (re-search-forward (concat "^" outline-regexp) nil t)
  6134. (progn
  6135. (goto-char (match-beginning 0))
  6136. (funcall outline-level))))))
  6137. (and level (hide-sublevels level))
  6138. (recenter '(4))
  6139. (org-goto-line l)))
  6140. (defun org-content (&optional arg)
  6141. "Show all headlines in the buffer, like a table of contents.
  6142. With numerical argument N, show content up to level N."
  6143. (interactive "P")
  6144. (save-excursion
  6145. ;; Visit all headings and show their offspring
  6146. (and (integerp arg) (org-overview))
  6147. (goto-char (point-max))
  6148. (catch 'exit
  6149. (while (and (progn (condition-case nil
  6150. (outline-previous-visible-heading 1)
  6151. (error (goto-char (point-min))))
  6152. t)
  6153. (looking-at org-outline-regexp))
  6154. (if (integerp arg)
  6155. (show-children (1- arg))
  6156. (show-branches))
  6157. (if (bobp) (throw 'exit nil))))))
  6158. (defun org-optimize-window-after-visibility-change (state)
  6159. "Adjust the window after a change in outline visibility.
  6160. This function is the default value of the hook `org-cycle-hook'."
  6161. (when (get-buffer-window (current-buffer))
  6162. (cond
  6163. ((eq state 'content) nil)
  6164. ((eq state 'all) nil)
  6165. ((eq state 'folded) nil)
  6166. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  6167. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  6168. (defun org-remove-empty-overlays-at (pos)
  6169. "Remove outline overlays that do not contain non-white stuff."
  6170. (mapc
  6171. (lambda (o)
  6172. (and (eq 'outline (overlay-get o 'invisible))
  6173. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  6174. (overlay-end o))))
  6175. (delete-overlay o)))
  6176. (overlays-at pos)))
  6177. (defun org-clean-visibility-after-subtree-move ()
  6178. "Fix visibility issues after moving a subtree."
  6179. ;; First, find a reasonable region to look at:
  6180. ;; Start two siblings above, end three below
  6181. (let* ((beg (save-excursion
  6182. (and (org-get-last-sibling)
  6183. (org-get-last-sibling))
  6184. (point)))
  6185. (end (save-excursion
  6186. (and (org-get-next-sibling)
  6187. (org-get-next-sibling)
  6188. (org-get-next-sibling))
  6189. (if (org-at-heading-p)
  6190. (point-at-eol)
  6191. (point))))
  6192. (level (looking-at "\\*+"))
  6193. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  6194. (save-excursion
  6195. (save-restriction
  6196. (narrow-to-region beg end)
  6197. (when re
  6198. ;; Properly fold already folded siblings
  6199. (goto-char (point-min))
  6200. (while (re-search-forward re nil t)
  6201. (if (and (not (outline-invisible-p))
  6202. (save-excursion
  6203. (goto-char (point-at-eol)) (outline-invisible-p)))
  6204. (hide-entry))))
  6205. (org-cycle-show-empty-lines 'overview)
  6206. (org-cycle-hide-drawers 'overview)))))
  6207. (defun org-cycle-show-empty-lines (state)
  6208. "Show empty lines above all visible headlines.
  6209. The region to be covered depends on STATE when called through
  6210. `org-cycle-hook'. Lisp program can use t for STATE to get the
  6211. entire buffer covered. Note that an empty line is only shown if there
  6212. are at least `org-cycle-separator-lines' empty lines before the headline."
  6213. (when (not (= org-cycle-separator-lines 0))
  6214. (save-excursion
  6215. (let* ((n (abs org-cycle-separator-lines))
  6216. (re (cond
  6217. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  6218. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  6219. (t (let ((ns (number-to-string (- n 2))))
  6220. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  6221. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  6222. beg end b e)
  6223. (cond
  6224. ((memq state '(overview contents t))
  6225. (setq beg (point-min) end (point-max)))
  6226. ((memq state '(children folded))
  6227. (setq beg (point) end (progn (org-end-of-subtree t t)
  6228. (beginning-of-line 2)
  6229. (point)))))
  6230. (when beg
  6231. (goto-char beg)
  6232. (while (re-search-forward re end t)
  6233. (unless (get-char-property (match-end 1) 'invisible)
  6234. (setq e (match-end 1))
  6235. (if (< org-cycle-separator-lines 0)
  6236. (setq b (save-excursion
  6237. (goto-char (match-beginning 0))
  6238. (org-back-over-empty-lines)
  6239. (if (save-excursion
  6240. (goto-char (max (point-min) (1- (point))))
  6241. (org-at-heading-p))
  6242. (1- (point))
  6243. (point))))
  6244. (setq b (match-beginning 1)))
  6245. (outline-flag-region b e nil)))))))
  6246. ;; Never hide empty lines at the end of the file.
  6247. (save-excursion
  6248. (goto-char (point-max))
  6249. (outline-previous-heading)
  6250. (outline-end-of-heading)
  6251. (if (and (looking-at "[ \t\n]+")
  6252. (= (match-end 0) (point-max)))
  6253. (outline-flag-region (point) (match-end 0) nil))))
  6254. (defun org-show-empty-lines-in-parent ()
  6255. "Move to the parent and re-show empty lines before visible headlines."
  6256. (save-excursion
  6257. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  6258. (org-cycle-show-empty-lines context))))
  6259. (defun org-files-list ()
  6260. "Return `org-agenda-files' list, plus all open org-mode files.
  6261. This is useful for operations that need to scan all of a user's
  6262. open and agenda-wise Org files."
  6263. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  6264. (dolist (buf (buffer-list))
  6265. (with-current-buffer buf
  6266. (if (and (derived-mode-p 'org-mode) (buffer-file-name))
  6267. (let ((file (expand-file-name (buffer-file-name))))
  6268. (unless (member file files)
  6269. (push file files))))))
  6270. files))
  6271. (defsubst org-entry-beginning-position ()
  6272. "Return the beginning position of the current entry."
  6273. (save-excursion (outline-back-to-heading t) (point)))
  6274. (defsubst org-entry-end-position ()
  6275. "Return the end position of the current entry."
  6276. (save-excursion (outline-next-heading) (point)))
  6277. (defun org-cycle-hide-drawers (state)
  6278. "Re-hide all drawers after a visibility state change."
  6279. (when (and (derived-mode-p 'org-mode)
  6280. (not (memq state '(overview folded contents))))
  6281. (save-excursion
  6282. (let* ((globalp (memq state '(contents all)))
  6283. (beg (if globalp (point-min) (point)))
  6284. (end (if globalp (point-max)
  6285. (if (eq state 'children)
  6286. (save-excursion (outline-next-heading) (point))
  6287. (org-end-of-subtree t)))))
  6288. (goto-char beg)
  6289. (while (re-search-forward org-drawer-regexp end t)
  6290. (org-flag-drawer t))))))
  6291. (defun org-cycle-hide-inline-tasks (state)
  6292. "Re-hide inline task when switching to 'contents visibility state."
  6293. (when (and (eq state 'contents)
  6294. (boundp 'org-inlinetask-min-level)
  6295. org-inlinetask-min-level)
  6296. (hide-sublevels (1- org-inlinetask-min-level))))
  6297. (defun org-flag-drawer (flag)
  6298. "When FLAG is non-nil, hide the drawer we are within.
  6299. Otherwise make it visible."
  6300. (save-excursion
  6301. (beginning-of-line 1)
  6302. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  6303. (let ((b (match-end 0)))
  6304. (if (re-search-forward
  6305. "^[ \t]*:END:"
  6306. (save-excursion (outline-next-heading) (point)) t)
  6307. (outline-flag-region b (point-at-eol) flag)
  6308. (error ":END: line missing at position %s" b))))))
  6309. (defun org-subtree-end-visible-p ()
  6310. "Is the end of the current subtree visible?"
  6311. (pos-visible-in-window-p
  6312. (save-excursion (org-end-of-subtree t) (point))))
  6313. (defun org-first-headline-recenter (&optional N)
  6314. "Move cursor to the first headline and recenter the headline.
  6315. Optional argument N means put the headline into the Nth line of the window."
  6316. (goto-char (point-min))
  6317. (when (re-search-forward (concat "^\\(" org-outline-regexp "\\)") nil t)
  6318. (beginning-of-line)
  6319. (recenter (prefix-numeric-value N))))
  6320. ;;; Saving and restoring visibility
  6321. (defun org-outline-overlay-data (&optional use-markers)
  6322. "Return a list of the locations of all outline overlays.
  6323. These are overlays with the `invisible' property value `outline'.
  6324. The return value is a list of cons cells, with start and stop
  6325. positions for each overlay.
  6326. If USE-MARKERS is set, return the positions as markers."
  6327. (let (beg end)
  6328. (save-excursion
  6329. (save-restriction
  6330. (widen)
  6331. (delq nil
  6332. (mapcar (lambda (o)
  6333. (when (eq (overlay-get o 'invisible) 'outline)
  6334. (setq beg (overlay-start o)
  6335. end (overlay-end o))
  6336. (and beg end (> end beg)
  6337. (if use-markers
  6338. (cons (move-marker (make-marker) beg)
  6339. (move-marker (make-marker) end))
  6340. (cons beg end)))))
  6341. (overlays-in (point-min) (point-max))))))))
  6342. (defun org-set-outline-overlay-data (data)
  6343. "Create visibility overlays for all positions in DATA.
  6344. DATA should have been made by `org-outline-overlay-data'."
  6345. (let (o)
  6346. (save-excursion
  6347. (save-restriction
  6348. (widen)
  6349. (show-all)
  6350. (mapc (lambda (c)
  6351. (outline-flag-region (car c) (cdr c) t))
  6352. data)))))
  6353. ;;; Folding of blocks
  6354. (defvar org-hide-block-overlays nil
  6355. "Overlays hiding blocks.")
  6356. (make-variable-buffer-local 'org-hide-block-overlays)
  6357. (defun org-block-map (function &optional start end)
  6358. "Call FUNCTION at the head of all source blocks in the current buffer.
  6359. Optional arguments START and END can be used to limit the range."
  6360. (let ((start (or start (point-min)))
  6361. (end (or end (point-max))))
  6362. (save-excursion
  6363. (goto-char start)
  6364. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  6365. (save-excursion
  6366. (save-match-data
  6367. (goto-char (match-beginning 0))
  6368. (funcall function)))))))
  6369. (defun org-hide-block-toggle-all ()
  6370. "Toggle the visibility of all blocks in the current buffer."
  6371. (org-block-map #'org-hide-block-toggle))
  6372. (defun org-hide-block-all ()
  6373. "Fold all blocks in the current buffer."
  6374. (interactive)
  6375. (org-show-block-all)
  6376. (org-block-map #'org-hide-block-toggle-maybe))
  6377. (defun org-show-block-all ()
  6378. "Unfold all blocks in the current buffer."
  6379. (interactive)
  6380. (mapc 'delete-overlay org-hide-block-overlays)
  6381. (setq org-hide-block-overlays nil))
  6382. (defun org-hide-block-toggle-maybe ()
  6383. "Toggle visibility of block at point."
  6384. (interactive)
  6385. (let ((case-fold-search t))
  6386. (if (save-excursion
  6387. (beginning-of-line 1)
  6388. (looking-at org-block-regexp))
  6389. (progn (org-hide-block-toggle)
  6390. t) ;; to signal that we took action
  6391. nil))) ;; to signal that we did not
  6392. (defun org-hide-block-toggle (&optional force)
  6393. "Toggle the visibility of the current block."
  6394. (interactive)
  6395. (save-excursion
  6396. (beginning-of-line)
  6397. (if (re-search-forward org-block-regexp nil t)
  6398. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  6399. (end (match-end 0)) ;; end of entire body
  6400. ov)
  6401. (if (memq t (mapcar (lambda (overlay)
  6402. (eq (overlay-get overlay 'invisible)
  6403. 'org-hide-block))
  6404. (overlays-at start)))
  6405. (if (or (not force) (eq force 'off))
  6406. (mapc (lambda (ov)
  6407. (when (member ov org-hide-block-overlays)
  6408. (setq org-hide-block-overlays
  6409. (delq ov org-hide-block-overlays)))
  6410. (when (eq (overlay-get ov 'invisible)
  6411. 'org-hide-block)
  6412. (delete-overlay ov)))
  6413. (overlays-at start)))
  6414. (setq ov (make-overlay start end))
  6415. (overlay-put ov 'invisible 'org-hide-block)
  6416. ;; make the block accessible to isearch
  6417. (overlay-put
  6418. ov 'isearch-open-invisible
  6419. (lambda (ov)
  6420. (when (member ov org-hide-block-overlays)
  6421. (setq org-hide-block-overlays
  6422. (delq ov org-hide-block-overlays)))
  6423. (when (eq (overlay-get ov 'invisible)
  6424. 'org-hide-block)
  6425. (delete-overlay ov))))
  6426. (push ov org-hide-block-overlays)))
  6427. (error "Not looking at a source block"))))
  6428. ;; org-tab-after-check-for-cycling-hook
  6429. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  6430. ;; Remove overlays when changing major mode
  6431. (add-hook 'org-mode-hook
  6432. (lambda () (org-add-hook 'change-major-mode-hook
  6433. 'org-show-block-all 'append 'local)))
  6434. ;;; Org-goto
  6435. (defvar org-goto-window-configuration nil)
  6436. (defvar org-goto-marker nil)
  6437. (defvar org-goto-map)
  6438. (defun org-goto-map ()
  6439. "Set the keymap `org-goto'."
  6440. (setq org-goto-map
  6441. (let ((map (make-sparse-keymap)))
  6442. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command
  6443. mouse-drag-region universal-argument org-occur))
  6444. cmd)
  6445. (while (setq cmd (pop cmds))
  6446. (substitute-key-definition cmd cmd map global-map)))
  6447. (suppress-keymap map)
  6448. (org-defkey map "\C-m" 'org-goto-ret)
  6449. (org-defkey map [(return)] 'org-goto-ret)
  6450. (org-defkey map [(left)] 'org-goto-left)
  6451. (org-defkey map [(right)] 'org-goto-right)
  6452. (org-defkey map [(control ?g)] 'org-goto-quit)
  6453. (org-defkey map "\C-i" 'org-cycle)
  6454. (org-defkey map [(tab)] 'org-cycle)
  6455. (org-defkey map [(down)] 'outline-next-visible-heading)
  6456. (org-defkey map [(up)] 'outline-previous-visible-heading)
  6457. (if org-goto-auto-isearch
  6458. (if (fboundp 'define-key-after)
  6459. (define-key-after map [t] 'org-goto-local-auto-isearch)
  6460. nil)
  6461. (org-defkey map "q" 'org-goto-quit)
  6462. (org-defkey map "n" 'outline-next-visible-heading)
  6463. (org-defkey map "p" 'outline-previous-visible-heading)
  6464. (org-defkey map "f" 'outline-forward-same-level)
  6465. (org-defkey map "b" 'outline-backward-same-level)
  6466. (org-defkey map "u" 'outline-up-heading))
  6467. (org-defkey map "/" 'org-occur)
  6468. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  6469. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  6470. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  6471. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  6472. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  6473. map)))
  6474. (defconst org-goto-help
  6475. "Browse buffer copy, to find location or copy text.%s
  6476. RET=jump to location C-g=quit and return to previous location
  6477. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  6478. (defvar org-goto-start-pos) ; dynamically scoped parameter
  6479. (defun org-goto (&optional alternative-interface)
  6480. "Look up a different location in the current file, keeping current visibility.
  6481. When you want look-up or go to a different location in a
  6482. document, the fastest way is often to fold the entire buffer and
  6483. then dive into the tree. This method has the disadvantage, that
  6484. the previous location will be folded, which may not be what you
  6485. want.
  6486. This command works around this by showing a copy of the current
  6487. buffer in an indirect buffer, in overview mode. You can dive
  6488. into the tree in that copy, use org-occur and incremental search
  6489. to find a location. When pressing RET or `Q', the command
  6490. returns to the original buffer in which the visibility is still
  6491. unchanged. After RET it will also jump to the location selected
  6492. in the indirect buffer and expose the headline hierarchy above.
  6493. With a prefix argument, use the alternative interface: e.g. if
  6494. `org-goto-interface' is 'outline use 'outline-path-completion."
  6495. (interactive "P")
  6496. (org-goto-map)
  6497. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  6498. (org-refile-use-outline-path t)
  6499. (org-refile-target-verify-function nil)
  6500. (interface
  6501. (if (not alternative-interface)
  6502. org-goto-interface
  6503. (if (eq org-goto-interface 'outline)
  6504. 'outline-path-completion
  6505. 'outline)))
  6506. (org-goto-start-pos (point))
  6507. (selected-point
  6508. (if (eq interface 'outline)
  6509. (car (org-get-location (current-buffer) org-goto-help))
  6510. (let ((pa (org-refile-get-location "Goto" nil nil t)))
  6511. (org-refile-check-position pa)
  6512. (nth 3 pa)))))
  6513. (if selected-point
  6514. (progn
  6515. (org-mark-ring-push org-goto-start-pos)
  6516. (goto-char selected-point)
  6517. (if (or (outline-invisible-p) (org-invisible-p2))
  6518. (org-show-context 'org-goto)))
  6519. (message "Quit"))))
  6520. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  6521. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  6522. (defvar org-goto-local-auto-isearch-map) ; defined below
  6523. (defun org-get-location (buf help)
  6524. "Let the user select a location in the Org-mode buffer BUF.
  6525. This function uses a recursive edit. It returns the selected position
  6526. or nil."
  6527. (org-no-popups
  6528. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  6529. (isearch-hide-immediately nil)
  6530. (isearch-search-fun-function
  6531. (lambda () 'org-goto-local-search-headings))
  6532. (org-goto-selected-point org-goto-exit-command))
  6533. (save-excursion
  6534. (save-window-excursion
  6535. (delete-other-windows)
  6536. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  6537. (org-pop-to-buffer-same-window
  6538. (condition-case nil
  6539. (make-indirect-buffer (current-buffer) "*org-goto*")
  6540. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  6541. (with-output-to-temp-buffer "*Org Help*"
  6542. (princ (format help (if org-goto-auto-isearch
  6543. " Just type for auto-isearch."
  6544. " n/p/f/b/u to navigate, q to quit."))))
  6545. (org-fit-window-to-buffer (get-buffer-window "*Org Help*"))
  6546. (setq buffer-read-only nil)
  6547. (let ((org-startup-truncated t)
  6548. (org-startup-folded nil)
  6549. (org-startup-align-all-tables nil))
  6550. (org-mode)
  6551. (org-overview))
  6552. (setq buffer-read-only t)
  6553. (if (and (boundp 'org-goto-start-pos)
  6554. (integer-or-marker-p org-goto-start-pos))
  6555. (let ((org-show-hierarchy-above t)
  6556. (org-show-siblings t)
  6557. (org-show-following-heading t))
  6558. (goto-char org-goto-start-pos)
  6559. (and (outline-invisible-p) (org-show-context)))
  6560. (goto-char (point-min)))
  6561. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  6562. (message "Select location and press RET")
  6563. (use-local-map org-goto-map)
  6564. (recursive-edit)))
  6565. (kill-buffer "*org-goto*")
  6566. (cons org-goto-selected-point org-goto-exit-command))))
  6567. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  6568. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  6569. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  6570. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  6571. (defun org-goto-local-search-headings (string bound noerror)
  6572. "Search and make sure that any matches are in headlines."
  6573. (catch 'return
  6574. (while (if isearch-forward
  6575. (search-forward string bound noerror)
  6576. (search-backward string bound noerror))
  6577. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  6578. (and (member :headline context)
  6579. (not (member :tags context))))
  6580. (throw 'return (point))))))
  6581. (defun org-goto-local-auto-isearch ()
  6582. "Start isearch."
  6583. (interactive)
  6584. (goto-char (point-min))
  6585. (let ((keys (this-command-keys)))
  6586. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  6587. (isearch-mode t)
  6588. (isearch-process-search-char (string-to-char keys)))))
  6589. (defun org-goto-ret (&optional arg)
  6590. "Finish `org-goto' by going to the new location."
  6591. (interactive "P")
  6592. (setq org-goto-selected-point (point)
  6593. org-goto-exit-command 'return)
  6594. (throw 'exit nil))
  6595. (defun org-goto-left ()
  6596. "Finish `org-goto' by going to the new location."
  6597. (interactive)
  6598. (if (org-at-heading-p)
  6599. (progn
  6600. (beginning-of-line 1)
  6601. (setq org-goto-selected-point (point)
  6602. org-goto-exit-command 'left)
  6603. (throw 'exit nil))
  6604. (error "Not on a heading")))
  6605. (defun org-goto-right ()
  6606. "Finish `org-goto' by going to the new location."
  6607. (interactive)
  6608. (if (org-at-heading-p)
  6609. (progn
  6610. (setq org-goto-selected-point (point)
  6611. org-goto-exit-command 'right)
  6612. (throw 'exit nil))
  6613. (error "Not on a heading")))
  6614. (defun org-goto-quit ()
  6615. "Finish `org-goto' without cursor motion."
  6616. (interactive)
  6617. (setq org-goto-selected-point nil)
  6618. (setq org-goto-exit-command 'quit)
  6619. (throw 'exit nil))
  6620. ;;; Indirect buffer display of subtrees
  6621. (defvar org-indirect-dedicated-frame nil
  6622. "This is the frame being used for indirect tree display.")
  6623. (defvar org-last-indirect-buffer nil)
  6624. (defun org-tree-to-indirect-buffer (&optional arg)
  6625. "Create indirect buffer and narrow it to current subtree.
  6626. With a numerical prefix ARG, go up to this level and then take that tree.
  6627. If ARG is negative, go up that many levels.
  6628. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  6629. indirect buffer previously made with this command, to avoid proliferation of
  6630. indirect buffers. However, when you call the command with a \
  6631. \\[universal-argument] prefix, or
  6632. when `org-indirect-buffer-display' is `new-frame', the last buffer
  6633. is kept so that you can work with several indirect buffers at the same time.
  6634. If `org-indirect-buffer-display' is `dedicated-frame', the \
  6635. \\[universal-argument] prefix also
  6636. requests that a new frame be made for the new buffer, so that the dedicated
  6637. frame is not changed."
  6638. (interactive "P")
  6639. (let ((cbuf (current-buffer))
  6640. (cwin (selected-window))
  6641. (pos (point))
  6642. beg end level heading ibuf)
  6643. (save-excursion
  6644. (org-back-to-heading t)
  6645. (when (numberp arg)
  6646. (setq level (org-outline-level))
  6647. (if (< arg 0) (setq arg (+ level arg)))
  6648. (while (> (setq level (org-outline-level)) arg)
  6649. (org-up-heading-safe)))
  6650. (setq beg (point)
  6651. heading (org-get-heading))
  6652. (org-end-of-subtree t t)
  6653. (if (org-at-heading-p) (backward-char 1))
  6654. (setq end (point)))
  6655. (if (and (buffer-live-p org-last-indirect-buffer)
  6656. (not (eq org-indirect-buffer-display 'new-frame))
  6657. (not arg))
  6658. (kill-buffer org-last-indirect-buffer))
  6659. (setq ibuf (org-get-indirect-buffer cbuf)
  6660. org-last-indirect-buffer ibuf)
  6661. (cond
  6662. ((or (eq org-indirect-buffer-display 'new-frame)
  6663. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  6664. (select-frame (make-frame))
  6665. (delete-other-windows)
  6666. (org-pop-to-buffer-same-window ibuf)
  6667. (org-set-frame-title heading))
  6668. ((eq org-indirect-buffer-display 'dedicated-frame)
  6669. (raise-frame
  6670. (select-frame (or (and org-indirect-dedicated-frame
  6671. (frame-live-p org-indirect-dedicated-frame)
  6672. org-indirect-dedicated-frame)
  6673. (setq org-indirect-dedicated-frame (make-frame)))))
  6674. (delete-other-windows)
  6675. (org-pop-to-buffer-same-window ibuf)
  6676. (org-set-frame-title (concat "Indirect: " heading)))
  6677. ((eq org-indirect-buffer-display 'current-window)
  6678. (org-pop-to-buffer-same-window ibuf))
  6679. ((eq org-indirect-buffer-display 'other-window)
  6680. (pop-to-buffer ibuf))
  6681. (t (error "Invalid value")))
  6682. (if (featurep 'xemacs)
  6683. (save-excursion (org-mode) (turn-on-font-lock)))
  6684. (narrow-to-region beg end)
  6685. (show-all)
  6686. (goto-char pos)
  6687. (run-hook-with-args 'org-cycle-hook 'all)
  6688. (and (window-live-p cwin) (select-window cwin))))
  6689. (defun org-get-indirect-buffer (&optional buffer)
  6690. (setq buffer (or buffer (current-buffer)))
  6691. (let ((n 1) (base (buffer-name buffer)) bname)
  6692. (while (buffer-live-p
  6693. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  6694. (setq n (1+ n)))
  6695. (condition-case nil
  6696. (make-indirect-buffer buffer bname 'clone)
  6697. (error (make-indirect-buffer buffer bname)))))
  6698. (defun org-set-frame-title (title)
  6699. "Set the title of the current frame to the string TITLE."
  6700. ;; FIXME: how to name a single frame in XEmacs???
  6701. (unless (featurep 'xemacs)
  6702. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  6703. ;;;; Structure editing
  6704. ;;; Inserting headlines
  6705. (defun org-previous-line-empty-p (&optional next)
  6706. "Is the previous line a blank line?
  6707. When NEXT is non-nil, check the next line instead."
  6708. (save-excursion
  6709. (and (not (bobp))
  6710. (or (beginning-of-line (if next 2 0)) t)
  6711. (save-match-data
  6712. (looking-at "[ \t]*$")))))
  6713. (defun org-insert-heading (&optional arg invisible-ok)
  6714. "Insert a new heading or item with same depth at point.
  6715. If point is in a plain list and ARG is nil, create a new list item.
  6716. With one universal prefix argument, insert a heading even in lists.
  6717. With two universal prefix arguments, insert the heading at the end
  6718. of the parent subtree.
  6719. If point is at the beginning of a headline, insert a sibling before
  6720. the current headline. If point is not at the beginning, split the line
  6721. and create a new headline with the text in the current line after point
  6722. \(see `org-M-RET-may-split-line' on how to modify this behavior).
  6723. When INVISIBLE-OK is set, stop at invisible headlines when going back.
  6724. This is important for non-interactive uses of the command."
  6725. (interactive "P")
  6726. (cond
  6727. ((or (= (buffer-size) 0)
  6728. (and (not (save-excursion
  6729. (and (ignore-errors (org-back-to-heading invisible-ok))
  6730. (org-at-heading-p))))
  6731. (or arg (not (org-in-item-p)))))
  6732. (insert
  6733. (if (org-previous-line-empty-p) "" "\n")
  6734. (if (org-in-src-block-p) ",* " "* "))
  6735. (run-hooks 'org-insert-heading-hook))
  6736. ((or arg
  6737. org-insert-heading-respect-content
  6738. (not (org-insert-item
  6739. (save-excursion
  6740. (beginning-of-line)
  6741. (looking-at org-list-full-item-re)
  6742. (match-string 3)))))
  6743. (let (begn endn)
  6744. (when (org-buffer-narrowed-p)
  6745. (setq begn (point-min) endn (point-max))
  6746. (widen))
  6747. (let* ((empty-line-p nil)
  6748. (eops (equal arg '(16))) ; insert at end of parent subtree
  6749. (org-insert-heading-respect-content
  6750. (or (not (null arg)) org-insert-heading-respect-content))
  6751. (level nil)
  6752. (on-heading (org-at-heading-p))
  6753. (on-empty-line
  6754. (save-excursion (beginning-of-line 1) (looking-at "^\\s-*$")))
  6755. (head (save-excursion
  6756. (condition-case nil
  6757. (progn
  6758. (org-back-to-heading invisible-ok)
  6759. (when (and (not on-heading)
  6760. (featurep 'org-inlinetask)
  6761. (integerp org-inlinetask-min-level)
  6762. (>= (length (match-string 0))
  6763. org-inlinetask-min-level))
  6764. ;; Find a heading level before the inline task
  6765. (while (and (setq level (org-up-heading-safe))
  6766. (>= level org-inlinetask-min-level)))
  6767. (if (org-at-heading-p)
  6768. (org-back-to-heading invisible-ok)
  6769. (error "This should not happen")))
  6770. (unless (and (save-excursion
  6771. (save-match-data
  6772. (org-backward-heading-same-level 1 invisible-ok))
  6773. (= (point) (match-beginning 0)))
  6774. (not (org-previous-line-empty-p t)))
  6775. (setq empty-line-p (org-previous-line-empty-p)))
  6776. (match-string 0))
  6777. (error "* "))))
  6778. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6779. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  6780. pos hide-previous previous-pos)
  6781. (if ;; At the beginning of a heading, open a new line for insertiong
  6782. (and (bolp) (org-at-heading-p)
  6783. (not eops)
  6784. (or (bobp)
  6785. (save-excursion (backward-char 1) (not (outline-invisible-p)))))
  6786. (open-line (if blank 2 1))
  6787. (save-excursion
  6788. (setq previous-pos (point-at-bol))
  6789. (end-of-line)
  6790. (setq hide-previous (outline-invisible-p)))
  6791. (and org-insert-heading-respect-content
  6792. (save-excursion
  6793. (while (outline-invisible-p)
  6794. (org-show-subtree)
  6795. (org-up-heading-safe))))
  6796. (let ((split
  6797. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  6798. (save-excursion
  6799. (let ((p (point)))
  6800. (goto-char (point-at-bol))
  6801. (and (looking-at org-complex-heading-regexp)
  6802. (match-beginning 4)
  6803. (> p (match-beginning 4)))))))
  6804. tags pos)
  6805. (cond
  6806. ;; Insert a new line, possibly at end of parent subtree
  6807. ((and (not arg) (not on-heading) (not on-empty-line)
  6808. (not (save-excursion
  6809. (beginning-of-line 1)
  6810. (looking-at org-list-full-item-re))))
  6811. (beginning-of-line 1))
  6812. (org-insert-heading-respect-content
  6813. (if (not eops)
  6814. (progn
  6815. (org-end-of-subtree nil t)
  6816. (and (looking-at "^\\*") (backward-char 1))
  6817. (while (and (not (bobp))
  6818. ;; Don't delete spaces in empty headlines
  6819. (not (looking-back org-outline-regexp))
  6820. (member (char-before) '(?\ ?\t ?\n)))
  6821. (backward-delete-char 1)))
  6822. (let ((p (point)))
  6823. (org-up-heading-safe)
  6824. (if (= p (point))
  6825. (goto-char (point-max))
  6826. (org-end-of-subtree nil t))))
  6827. (when (featurep 'org-inlinetask)
  6828. (while (and (not (eobp))
  6829. (looking-at "\\(\\*+\\)[ \t]+")
  6830. (>= (length (match-string 1))
  6831. org-inlinetask-min-level))
  6832. (org-end-of-subtree nil t)))
  6833. (or (bolp) (newline))
  6834. (or (org-previous-line-empty-p)
  6835. (and blank (newline)))
  6836. (if (or empty-line-p eops) (open-line 1)))
  6837. ;; Insert a headling containing text after point
  6838. ((org-at-heading-p)
  6839. (when hide-previous
  6840. (show-children)
  6841. (org-show-entry))
  6842. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?[ \t]*$")
  6843. (setq tags (and (match-end 2) (match-string 2)))
  6844. (and (match-end 1)
  6845. (delete-region (match-beginning 1) (match-end 1)))
  6846. (setq pos (point-at-bol))
  6847. (or split (end-of-line 1))
  6848. (delete-horizontal-space)
  6849. (if (string-match "\\`\\*+\\'"
  6850. (buffer-substring (point-at-bol) (point)))
  6851. (insert " "))
  6852. (newline (if blank 2 1))
  6853. (when tags
  6854. (save-excursion
  6855. (goto-char pos)
  6856. (end-of-line 1)
  6857. (insert " " tags)
  6858. (org-set-tags nil 'align))))
  6859. (t
  6860. (or split (end-of-line 1))
  6861. (newline (cond ((and blank (not on-empty-line)) 2)
  6862. (blank 1)
  6863. (on-empty-line 0) (t 1)))))))
  6864. (insert head) (just-one-space)
  6865. (setq pos (point))
  6866. (end-of-line 1)
  6867. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  6868. (when (and org-insert-heading-respect-content hide-previous)
  6869. (save-excursion
  6870. (goto-char previous-pos)
  6871. (hide-subtree)))
  6872. (when (and begn endn)
  6873. (narrow-to-region (min (point) begn) (max (point) endn)))
  6874. (run-hooks 'org-insert-heading-hook))))))
  6875. (defun org-get-heading (&optional no-tags no-todo)
  6876. "Return the heading of the current entry, without the stars.
  6877. When NO-TAGS is non-nil, don't include tags.
  6878. When NO-TODO is non-nil, don't include TODO keywords."
  6879. (save-excursion
  6880. (org-back-to-heading t)
  6881. (cond
  6882. ((and no-tags no-todo)
  6883. (looking-at org-complex-heading-regexp)
  6884. (match-string 4))
  6885. (no-tags
  6886. (looking-at (concat org-outline-regexp
  6887. "\\(.*?\\)"
  6888. "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
  6889. (match-string 1))
  6890. (no-todo
  6891. (looking-at org-todo-line-regexp)
  6892. (match-string 3))
  6893. (t (looking-at org-heading-regexp)
  6894. (match-string 2)))))
  6895. (defvar orgstruct-mode) ; defined below
  6896. (defun org-heading-components ()
  6897. "Return the components of the current heading.
  6898. This is a list with the following elements:
  6899. - the level as an integer
  6900. - the reduced level, different if `org-odd-levels-only' is set.
  6901. - the TODO keyword, or nil
  6902. - the priority character, like ?A, or nil if no priority is given
  6903. - the headline text itself, or the tags string if no headline text
  6904. - the tags string, or nil."
  6905. (save-excursion
  6906. (org-back-to-heading t)
  6907. (if (let (case-fold-search)
  6908. (looking-at
  6909. (if orgstruct-mode
  6910. org-heading-regexp
  6911. org-complex-heading-regexp)))
  6912. (if orgstruct-mode
  6913. (list (length (match-string 1))
  6914. (org-reduced-level (length (match-string 1)))
  6915. nil
  6916. nil
  6917. (match-string 2)
  6918. nil)
  6919. (list (length (match-string 1))
  6920. (org-reduced-level (length (match-string 1)))
  6921. (org-match-string-no-properties 2)
  6922. (and (match-end 3) (aref (match-string 3) 2))
  6923. (org-match-string-no-properties 4)
  6924. (org-match-string-no-properties 5))))))
  6925. (defun org-get-entry ()
  6926. "Get the entry text, after heading, entire subtree."
  6927. (save-excursion
  6928. (org-back-to-heading t)
  6929. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  6930. (defun org-insert-heading-after-current ()
  6931. "Insert a new heading with same level as current, after current subtree."
  6932. (interactive)
  6933. (org-back-to-heading)
  6934. (org-insert-heading)
  6935. (org-move-subtree-down)
  6936. (end-of-line 1))
  6937. (defun org-insert-heading-respect-content (&optional arg invisible-ok)
  6938. "Insert heading with `org-insert-heading-respect-content' set to t."
  6939. (interactive "P")
  6940. (let ((org-insert-heading-respect-content t))
  6941. (org-insert-heading arg invisible-ok)))
  6942. (defun org-insert-todo-heading-respect-content (&optional force-state)
  6943. "Insert TODO heading with `org-insert-heading-respect-content' set to t."
  6944. (interactive "P")
  6945. (let ((org-insert-heading-respect-content t))
  6946. (org-insert-todo-heading force-state t)))
  6947. (defun org-insert-todo-heading (arg &optional force-heading)
  6948. "Insert a new heading with the same level and TODO state as current heading.
  6949. If the heading has no TODO state, or if the state is DONE, use the first
  6950. state (TODO by default). Also one prefix arg, force first state. With two
  6951. prefix args, force inserting at the end of the parent subtree."
  6952. (interactive "P")
  6953. (when (or force-heading (not (org-insert-item 'checkbox)))
  6954. (org-insert-heading (or (and (equal arg '(16)) '(16))
  6955. force-heading))
  6956. (save-excursion
  6957. (org-back-to-heading)
  6958. (outline-previous-heading)
  6959. (looking-at org-todo-line-regexp))
  6960. (let*
  6961. ((new-mark-x
  6962. (if (or arg
  6963. (not (match-beginning 2))
  6964. (member (match-string 2) org-done-keywords))
  6965. (car org-todo-keywords-1)
  6966. (match-string 2)))
  6967. (new-mark
  6968. (or
  6969. (run-hook-with-args-until-success
  6970. 'org-todo-get-default-hook new-mark-x nil)
  6971. new-mark-x)))
  6972. (beginning-of-line 1)
  6973. (and (looking-at org-outline-regexp) (goto-char (match-end 0))
  6974. (if org-treat-insert-todo-heading-as-state-change
  6975. (org-todo new-mark)
  6976. (insert new-mark " "))))
  6977. (when org-provide-todo-statistics
  6978. (org-update-parent-todo-statistics))))
  6979. (defun org-insert-subheading (arg)
  6980. "Insert a new subheading and demote it.
  6981. Works for outline headings and for plain lists alike."
  6982. (interactive "P")
  6983. (org-insert-heading arg)
  6984. (cond
  6985. ((org-at-heading-p) (org-do-demote))
  6986. ((org-at-item-p) (org-indent-item))))
  6987. (defun org-insert-todo-subheading (arg)
  6988. "Insert a new subheading with TODO keyword or checkbox and demote it.
  6989. Works for outline headings and for plain lists alike."
  6990. (interactive "P")
  6991. (org-insert-todo-heading arg)
  6992. (cond
  6993. ((org-at-heading-p) (org-do-demote))
  6994. ((org-at-item-p) (org-indent-item))))
  6995. ;;; Promotion and Demotion
  6996. (defvar org-after-demote-entry-hook nil
  6997. "Hook run after an entry has been demoted.
  6998. The cursor will be at the beginning of the entry.
  6999. When a subtree is being demoted, the hook will be called for each node.")
  7000. (defvar org-after-promote-entry-hook nil
  7001. "Hook run after an entry has been promoted.
  7002. The cursor will be at the beginning of the entry.
  7003. When a subtree is being promoted, the hook will be called for each node.")
  7004. (defun org-promote-subtree ()
  7005. "Promote the entire subtree.
  7006. See also `org-promote'."
  7007. (interactive)
  7008. (save-excursion
  7009. (org-with-limited-levels (org-map-tree 'org-promote)))
  7010. (org-fix-position-after-promote))
  7011. (defun org-demote-subtree ()
  7012. "Demote the entire subtree. See `org-demote'.
  7013. See also `org-promote'."
  7014. (interactive)
  7015. (save-excursion
  7016. (org-with-limited-levels (org-map-tree 'org-demote)))
  7017. (org-fix-position-after-promote))
  7018. (defun org-do-promote ()
  7019. "Promote the current heading higher up the tree.
  7020. If the region is active in `transient-mark-mode', promote all headings
  7021. in the region."
  7022. (interactive)
  7023. (save-excursion
  7024. (if (org-region-active-p)
  7025. (org-map-region 'org-promote (region-beginning) (region-end))
  7026. (org-promote)))
  7027. (org-fix-position-after-promote))
  7028. (defun org-do-demote ()
  7029. "Demote the current heading lower down the tree.
  7030. If the region is active in `transient-mark-mode', demote all headings
  7031. in the region."
  7032. (interactive)
  7033. (save-excursion
  7034. (if (org-region-active-p)
  7035. (org-map-region 'org-demote (region-beginning) (region-end))
  7036. (org-demote)))
  7037. (org-fix-position-after-promote))
  7038. (defun org-fix-position-after-promote ()
  7039. "Make sure that after pro/demotion cursor position is right."
  7040. (let ((pos (point)))
  7041. (when (save-excursion
  7042. (beginning-of-line 1)
  7043. (looking-at org-todo-line-regexp)
  7044. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  7045. (cond ((eobp) (insert " "))
  7046. ((eolp) (insert " "))
  7047. ((equal (char-after) ?\ ) (forward-char 1))))))
  7048. (defun org-current-level ()
  7049. "Return the level of the current entry, or nil if before the first headline.
  7050. The level is the number of stars at the beginning of the headline."
  7051. (save-excursion
  7052. (org-with-limited-levels
  7053. (if (ignore-errors (org-back-to-heading t))
  7054. (funcall outline-level)))))
  7055. (defun org-get-previous-line-level ()
  7056. "Return the outline depth of the last headline before the current line.
  7057. Returns 0 for the first headline in the buffer, and nil if before the
  7058. first headline."
  7059. (let ((current-level (org-current-level))
  7060. (prev-level (when (> (line-number-at-pos) 1)
  7061. (save-excursion
  7062. (beginning-of-line 0)
  7063. (org-current-level)))))
  7064. (cond ((null current-level) nil) ; Before first headline
  7065. ((null prev-level) 0) ; At first headline
  7066. (prev-level))))
  7067. (defun org-reduced-level (l)
  7068. "Compute the effective level of a heading.
  7069. This takes into account the setting of `org-odd-levels-only'."
  7070. (cond
  7071. ((zerop l) 0)
  7072. (org-odd-levels-only (1+ (floor (/ l 2))))
  7073. (t l)))
  7074. (defun org-level-increment ()
  7075. "Return the number of stars that will be added or removed at a
  7076. time to headlines when structure editing, based on the value of
  7077. `org-odd-levels-only'."
  7078. (if org-odd-levels-only 2 1))
  7079. (defun org-get-valid-level (level &optional change)
  7080. "Rectify a level change under the influence of `org-odd-levels-only'
  7081. LEVEL is a current level, CHANGE is by how much the level should be
  7082. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  7083. even level numbers will become the next higher odd number."
  7084. (if org-odd-levels-only
  7085. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  7086. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  7087. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  7088. (max 1 (+ level (or change 0)))))
  7089. (if (boundp 'define-obsolete-function-alias)
  7090. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  7091. (define-obsolete-function-alias 'org-get-legal-level
  7092. 'org-get-valid-level)
  7093. (define-obsolete-function-alias 'org-get-legal-level
  7094. 'org-get-valid-level "23.1")))
  7095. (defvar org-called-with-limited-levels nil) ;; Dynamically bound in
  7096. ;; ̀org-with-limited-levels'
  7097. (defun org-promote ()
  7098. "Promote the current heading higher up the tree.
  7099. If the region is active in `transient-mark-mode', promote all headings
  7100. in the region."
  7101. (org-back-to-heading t)
  7102. (let* ((level (save-match-data (funcall outline-level)))
  7103. (after-change-functions (remove 'flyspell-after-change-function
  7104. after-change-functions))
  7105. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  7106. (diff (abs (- level (length up-head) -1))))
  7107. (cond ((and (= level 1) org-called-with-limited-levels
  7108. org-allow-promoting-top-level-subtree)
  7109. (replace-match "# " nil t))
  7110. ((= level 1)
  7111. (error "Cannot promote to level 0. UNDO to recover if necessary"))
  7112. (t (replace-match up-head nil t)))
  7113. ;; Fixup tag positioning
  7114. (unless (= level 1)
  7115. (and org-auto-align-tags (org-set-tags nil t))
  7116. (if org-adapt-indentation (org-fixup-indentation (- diff))))
  7117. (run-hooks 'org-after-promote-entry-hook)))
  7118. (defun org-demote ()
  7119. "Demote the current heading lower down the tree.
  7120. If the region is active in `transient-mark-mode', demote all headings
  7121. in the region."
  7122. (org-back-to-heading t)
  7123. (let* ((level (save-match-data (funcall outline-level)))
  7124. (after-change-functions (remove 'flyspell-after-change-function
  7125. after-change-functions))
  7126. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  7127. (diff (abs (- level (length down-head) -1))))
  7128. (replace-match down-head nil t)
  7129. ;; Fixup tag positioning
  7130. (and org-auto-align-tags (org-set-tags nil t))
  7131. (if org-adapt-indentation (org-fixup-indentation diff))
  7132. (run-hooks 'org-after-demote-entry-hook)))
  7133. (defun org-cycle-level ()
  7134. "Cycle the level of an empty headline through possible states.
  7135. This goes first to child, then to parent, level, then up the hierarchy.
  7136. After top level, it switches back to sibling level."
  7137. (interactive)
  7138. (let ((org-adapt-indentation nil))
  7139. (when (org-point-at-end-of-empty-headline)
  7140. (setq this-command 'org-cycle-level) ; Only needed for caching
  7141. (let ((cur-level (org-current-level))
  7142. (prev-level (org-get-previous-line-level)))
  7143. (cond
  7144. ;; If first headline in file, promote to top-level.
  7145. ((= prev-level 0)
  7146. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7147. do (org-do-promote)))
  7148. ;; If same level as prev, demote one.
  7149. ((= prev-level cur-level)
  7150. (org-do-demote))
  7151. ;; If parent is top-level, promote to top level if not already.
  7152. ((= prev-level 1)
  7153. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7154. do (org-do-promote)))
  7155. ;; If top-level, return to prev-level.
  7156. ((= cur-level 1)
  7157. (loop repeat (/ (- prev-level 1) (org-level-increment))
  7158. do (org-do-demote)))
  7159. ;; If less than prev-level, promote one.
  7160. ((< cur-level prev-level)
  7161. (org-do-promote))
  7162. ;; If deeper than prev-level, promote until higher than
  7163. ;; prev-level.
  7164. ((> cur-level prev-level)
  7165. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  7166. do (org-do-promote))))
  7167. t))))
  7168. (defun org-map-tree (fun)
  7169. "Call FUN for every heading underneath the current one."
  7170. (org-back-to-heading)
  7171. (let ((level (funcall outline-level)))
  7172. (save-excursion
  7173. (funcall fun)
  7174. (while (and (progn
  7175. (outline-next-heading)
  7176. (> (funcall outline-level) level))
  7177. (not (eobp)))
  7178. (funcall fun)))))
  7179. (defun org-map-region (fun beg end)
  7180. "Call FUN for every heading between BEG and END."
  7181. (let ((org-ignore-region t))
  7182. (save-excursion
  7183. (setq end (copy-marker end))
  7184. (goto-char beg)
  7185. (if (and (re-search-forward org-outline-regexp-bol nil t)
  7186. (< (point) end))
  7187. (funcall fun))
  7188. (while (and (progn
  7189. (outline-next-heading)
  7190. (< (point) end))
  7191. (not (eobp)))
  7192. (funcall fun)))))
  7193. (defvar org-property-end-re) ; silence byte-compiler
  7194. (defun org-fixup-indentation (diff)
  7195. "Change the indentation in the current entry by DIFF.
  7196. However, if any line in the current entry has no indentation, or if it
  7197. would end up with no indentation after the change, nothing at all is done."
  7198. (save-excursion
  7199. (let ((end (save-excursion (outline-next-heading)
  7200. (point-marker)))
  7201. (prohibit (if (> diff 0)
  7202. "^\\S-"
  7203. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  7204. col)
  7205. (unless (save-excursion (end-of-line 1)
  7206. (re-search-forward prohibit end t))
  7207. (while (and (< (point) end)
  7208. (re-search-forward "^[ \t]+" end t))
  7209. (goto-char (match-end 0))
  7210. (setq col (current-column))
  7211. (if (< diff 0) (replace-match ""))
  7212. (org-indent-to-column (+ diff col))))
  7213. (move-marker end nil))))
  7214. (defun org-convert-to-odd-levels ()
  7215. "Convert an org-mode file with all levels allowed to one with odd levels.
  7216. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  7217. level 5 etc."
  7218. (interactive)
  7219. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  7220. (let ((outline-level 'org-outline-level)
  7221. (org-odd-levels-only nil) n)
  7222. (save-excursion
  7223. (goto-char (point-min))
  7224. (while (re-search-forward "^\\*\\*+ " nil t)
  7225. (setq n (- (length (match-string 0)) 2))
  7226. (while (>= (setq n (1- n)) 0)
  7227. (org-demote))
  7228. (end-of-line 1))))))
  7229. (defun org-convert-to-oddeven-levels ()
  7230. "Convert an org-mode file with only odd levels to one with odd/even levels.
  7231. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  7232. file contains a section with an even level, conversion would
  7233. destroy the structure of the file. An error is signaled in this
  7234. case."
  7235. (interactive)
  7236. (goto-char (point-min))
  7237. ;; First check if there are no even levels
  7238. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  7239. (org-show-context t)
  7240. (error "Not all levels are odd in this file. Conversion not possible"))
  7241. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  7242. (let ((outline-regexp org-outline-regexp)
  7243. (outline-level 'org-outline-level)
  7244. (org-odd-levels-only nil) n)
  7245. (save-excursion
  7246. (goto-char (point-min))
  7247. (while (re-search-forward "^\\*\\*+ " nil t)
  7248. (setq n (/ (1- (length (match-string 0))) 2))
  7249. (while (>= (setq n (1- n)) 0)
  7250. (org-promote))
  7251. (end-of-line 1))))))
  7252. (defun org-tr-level (n)
  7253. "Make N odd if required."
  7254. (if org-odd-levels-only (1+ (/ n 2)) n))
  7255. ;;; Vertical tree motion, cutting and pasting of subtrees
  7256. (defun org-move-subtree-up (&optional arg)
  7257. "Move the current subtree up past ARG headlines of the same level."
  7258. (interactive "p")
  7259. (org-move-subtree-down (- (prefix-numeric-value arg))))
  7260. (defun org-move-subtree-down (&optional arg)
  7261. "Move the current subtree down past ARG headlines of the same level."
  7262. (interactive "p")
  7263. (setq arg (prefix-numeric-value arg))
  7264. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  7265. 'org-get-last-sibling))
  7266. (ins-point (make-marker))
  7267. (cnt (abs arg))
  7268. (col (current-column))
  7269. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  7270. ;; Select the tree
  7271. (org-back-to-heading)
  7272. (setq beg0 (point))
  7273. (save-excursion
  7274. (setq ne-beg (org-back-over-empty-lines))
  7275. (setq beg (point)))
  7276. (save-match-data
  7277. (save-excursion (outline-end-of-heading)
  7278. (setq folded (outline-invisible-p)))
  7279. (outline-end-of-subtree))
  7280. (outline-next-heading)
  7281. (setq ne-end (org-back-over-empty-lines))
  7282. (setq end (point))
  7283. (goto-char beg0)
  7284. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  7285. ;; include less whitespace
  7286. (save-excursion
  7287. (goto-char beg)
  7288. (forward-line (- ne-beg ne-end))
  7289. (setq beg (point))))
  7290. ;; Find insertion point, with error handling
  7291. (while (> cnt 0)
  7292. (or (and (funcall movfunc) (looking-at org-outline-regexp))
  7293. (progn (goto-char beg0)
  7294. (user-error "Cannot move past superior level or buffer limit")))
  7295. (setq cnt (1- cnt)))
  7296. (if (> arg 0)
  7297. ;; Moving forward - still need to move over subtree
  7298. (progn (org-end-of-subtree t t)
  7299. (save-excursion
  7300. (org-back-over-empty-lines)
  7301. (or (bolp) (newline)))))
  7302. (setq ne-ins (org-back-over-empty-lines))
  7303. (move-marker ins-point (point))
  7304. (setq txt (buffer-substring beg end))
  7305. (org-save-markers-in-region beg end)
  7306. (delete-region beg end)
  7307. (org-remove-empty-overlays-at beg)
  7308. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  7309. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  7310. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  7311. (let ((bbb (point)))
  7312. (insert-before-markers txt)
  7313. (org-reinstall-markers-in-region bbb)
  7314. (move-marker ins-point bbb))
  7315. (or (bolp) (insert "\n"))
  7316. (setq ins-end (point))
  7317. (goto-char ins-point)
  7318. (org-skip-whitespace)
  7319. (when (and (< arg 0)
  7320. (org-first-sibling-p)
  7321. (> ne-ins ne-beg))
  7322. ;; Move whitespace back to beginning
  7323. (save-excursion
  7324. (goto-char ins-end)
  7325. (let ((kill-whole-line t))
  7326. (kill-line (- ne-ins ne-beg)) (point)))
  7327. (insert (make-string (- ne-ins ne-beg) ?\n)))
  7328. (move-marker ins-point nil)
  7329. (if folded
  7330. (hide-subtree)
  7331. (org-show-entry)
  7332. (show-children)
  7333. (org-cycle-hide-drawers 'children))
  7334. (org-clean-visibility-after-subtree-move)
  7335. ;; move back to the initial column we were at
  7336. (move-to-column col)))
  7337. (defvar org-subtree-clip ""
  7338. "Clipboard for cut and paste of subtrees.
  7339. This is actually only a copy of the kill, because we use the normal kill
  7340. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  7341. (defvar org-subtree-clip-folded nil
  7342. "Was the last copied subtree folded?
  7343. This is used to fold the tree back after pasting.")
  7344. (defun org-cut-subtree (&optional n)
  7345. "Cut the current subtree into the clipboard.
  7346. With prefix arg N, cut this many sequential subtrees.
  7347. This is a short-hand for marking the subtree and then cutting it."
  7348. (interactive "p")
  7349. (org-copy-subtree n 'cut))
  7350. (defun org-copy-subtree (&optional n cut force-store-markers nosubtrees)
  7351. "Cut the current subtree into the clipboard.
  7352. With prefix arg N, cut this many sequential subtrees.
  7353. This is a short-hand for marking the subtree and then copying it.
  7354. If CUT is non-nil, actually cut the subtree.
  7355. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  7356. of some markers in the region, even if CUT is non-nil. This is
  7357. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  7358. (interactive "p")
  7359. (let (beg end folded (beg0 (point)))
  7360. (if (org-called-interactively-p 'any)
  7361. (org-back-to-heading nil) ; take what looks like a subtree
  7362. (org-back-to-heading t)) ; take what is really there
  7363. (setq beg (point))
  7364. (skip-chars-forward " \t\r\n")
  7365. (save-match-data
  7366. (if nosubtrees
  7367. (outline-next-heading)
  7368. (save-excursion (outline-end-of-heading)
  7369. (setq folded (outline-invisible-p)))
  7370. (condition-case nil
  7371. (org-forward-heading-same-level (1- n) t)
  7372. (error nil))
  7373. (org-end-of-subtree t t)))
  7374. (setq end (point))
  7375. (goto-char beg0)
  7376. (when (> end beg)
  7377. (setq org-subtree-clip-folded folded)
  7378. (when (or cut force-store-markers)
  7379. (org-save-markers-in-region beg end))
  7380. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  7381. (setq org-subtree-clip (current-kill 0))
  7382. (message "%s: Subtree(s) with %d characters"
  7383. (if cut "Cut" "Copied")
  7384. (length org-subtree-clip)))))
  7385. (defun org-paste-subtree (&optional level tree for-yank)
  7386. "Paste the clipboard as a subtree, with modification of headline level.
  7387. The entire subtree is promoted or demoted in order to match a new headline
  7388. level.
  7389. If the cursor is at the beginning of a headline, the same level as
  7390. that headline is used to paste the tree.
  7391. If not, the new level is derived from the *visible* headings
  7392. before and after the insertion point, and taken to be the inferior headline
  7393. level of the two. So if the previous visible heading is level 3 and the
  7394. next is level 4 (or vice versa), level 4 will be used for insertion.
  7395. This makes sure that the subtree remains an independent subtree and does
  7396. not swallow low level entries.
  7397. You can also force a different level, either by using a numeric prefix
  7398. argument, or by inserting the heading marker by hand. For example, if the
  7399. cursor is after \"*****\", then the tree will be shifted to level 5.
  7400. If optional TREE is given, use this text instead of the kill ring.
  7401. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  7402. move back over whitespace before inserting, and move point to the end of
  7403. the inserted text when done."
  7404. (interactive "P")
  7405. (setq tree (or tree (and kill-ring (current-kill 0))))
  7406. (unless (org-kill-is-subtree-p tree)
  7407. (error "%s"
  7408. (substitute-command-keys
  7409. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  7410. (org-with-limited-levels
  7411. (let* ((visp (not (outline-invisible-p)))
  7412. (txt tree)
  7413. (^re_ "\\(\\*+\\)[ \t]*")
  7414. (old-level (if (string-match org-outline-regexp-bol txt)
  7415. (- (match-end 0) (match-beginning 0) 1)
  7416. -1))
  7417. (force-level (cond (level (prefix-numeric-value level))
  7418. ((and (looking-at "[ \t]*$")
  7419. (string-match
  7420. "^\\*+$" (buffer-substring
  7421. (point-at-bol) (point))))
  7422. (- (match-end 1) (match-beginning 1)))
  7423. ((and (bolp)
  7424. (looking-at org-outline-regexp))
  7425. (- (match-end 0) (point) 1))))
  7426. (previous-level (save-excursion
  7427. (condition-case nil
  7428. (progn
  7429. (outline-previous-visible-heading 1)
  7430. (if (looking-at ^re_)
  7431. (- (match-end 0) (match-beginning 0) 1)
  7432. 1))
  7433. (error 1))))
  7434. (next-level (save-excursion
  7435. (condition-case nil
  7436. (progn
  7437. (or (looking-at org-outline-regexp)
  7438. (outline-next-visible-heading 1))
  7439. (if (looking-at ^re_)
  7440. (- (match-end 0) (match-beginning 0) 1)
  7441. 1))
  7442. (error 1))))
  7443. (new-level (or force-level (max previous-level next-level)))
  7444. (shift (if (or (= old-level -1)
  7445. (= new-level -1)
  7446. (= old-level new-level))
  7447. 0
  7448. (- new-level old-level)))
  7449. (delta (if (> shift 0) -1 1))
  7450. (func (if (> shift 0) 'org-demote 'org-promote))
  7451. (org-odd-levels-only nil)
  7452. beg end newend)
  7453. ;; Remove the forced level indicator
  7454. (if force-level
  7455. (delete-region (point-at-bol) (point)))
  7456. ;; Paste
  7457. (beginning-of-line (if (bolp) 1 2))
  7458. (setq beg (point))
  7459. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  7460. (insert-before-markers txt)
  7461. (unless (string-match "\n\\'" txt) (insert "\n"))
  7462. (setq newend (point))
  7463. (org-reinstall-markers-in-region beg)
  7464. (setq end (point))
  7465. (goto-char beg)
  7466. (skip-chars-forward " \t\n\r")
  7467. (setq beg (point))
  7468. (if (and (outline-invisible-p) visp)
  7469. (save-excursion (outline-show-heading)))
  7470. ;; Shift if necessary
  7471. (unless (= shift 0)
  7472. (save-restriction
  7473. (narrow-to-region beg end)
  7474. (while (not (= shift 0))
  7475. (org-map-region func (point-min) (point-max))
  7476. (setq shift (+ delta shift)))
  7477. (goto-char (point-min))
  7478. (setq newend (point-max))))
  7479. (when (or (org-called-interactively-p 'interactive) for-yank)
  7480. (message "Clipboard pasted as level %d subtree" new-level))
  7481. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  7482. kill-ring
  7483. (eq org-subtree-clip (current-kill 0))
  7484. org-subtree-clip-folded)
  7485. ;; The tree was folded before it was killed/copied
  7486. (hide-subtree))
  7487. (and for-yank (goto-char newend)))))
  7488. (defun org-kill-is-subtree-p (&optional txt)
  7489. "Check if the current kill is an outline subtree, or a set of trees.
  7490. Returns nil if kill does not start with a headline, or if the first
  7491. headline level is not the largest headline level in the tree.
  7492. So this will actually accept several entries of equal levels as well,
  7493. which is OK for `org-paste-subtree'.
  7494. If optional TXT is given, check this string instead of the current kill."
  7495. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  7496. (re (org-get-limited-outline-regexp))
  7497. (^re (concat "^" re))
  7498. (start-level (and kill
  7499. (string-match
  7500. (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
  7501. kill)
  7502. (- (match-end 2) (match-beginning 2) 1)))
  7503. (start (1+ (or (match-beginning 2) -1))))
  7504. (if (not start-level)
  7505. (progn
  7506. nil) ;; does not even start with a heading
  7507. (catch 'exit
  7508. (while (setq start (string-match ^re kill (1+ start)))
  7509. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  7510. (throw 'exit nil)))
  7511. t))))
  7512. (defvar org-markers-to-move nil
  7513. "Markers that should be moved with a cut-and-paste operation.
  7514. Those markers are stored together with their positions relative to
  7515. the start of the region.")
  7516. (defun org-save-markers-in-region (beg end)
  7517. "Check markers in region.
  7518. If these markers are between BEG and END, record their position relative
  7519. to BEG, so that after moving the block of text, we can put the markers back
  7520. into place.
  7521. This function gets called just before an entry or tree gets cut from the
  7522. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  7523. called immediately, to move the markers with the entries."
  7524. (setq org-markers-to-move nil)
  7525. (when (featurep 'org-clock)
  7526. (org-clock-save-markers-for-cut-and-paste beg end))
  7527. (when (featurep 'org-agenda)
  7528. (org-agenda-save-markers-for-cut-and-paste beg end)))
  7529. (defun org-check-and-save-marker (marker beg end)
  7530. "Check if MARKER is between BEG and END.
  7531. If yes, remember the marker and the distance to BEG."
  7532. (when (and (marker-buffer marker)
  7533. (equal (marker-buffer marker) (current-buffer)))
  7534. (if (and (>= marker beg) (< marker end))
  7535. (push (cons marker (- marker beg)) org-markers-to-move))))
  7536. (defun org-reinstall-markers-in-region (beg)
  7537. "Move all remembered markers to their position relative to BEG."
  7538. (mapc (lambda (x)
  7539. (move-marker (car x) (+ beg (cdr x))))
  7540. org-markers-to-move)
  7541. (setq org-markers-to-move nil))
  7542. (defun org-narrow-to-subtree ()
  7543. "Narrow buffer to the current subtree."
  7544. (interactive)
  7545. (save-excursion
  7546. (save-match-data
  7547. (org-with-limited-levels
  7548. (narrow-to-region
  7549. (progn (org-back-to-heading t) (point))
  7550. (progn (org-end-of-subtree t t)
  7551. (if (and (org-at-heading-p) (not (eobp))) (backward-char 1))
  7552. (point)))))))
  7553. (defun org-narrow-to-block ()
  7554. "Narrow buffer to the current block."
  7555. (interactive)
  7556. (let* ((case-fold-search t)
  7557. (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
  7558. "^[ \t]*#\\+end_.*")))
  7559. (if blockp
  7560. (narrow-to-region (car blockp) (cdr blockp))
  7561. (error "Not in a block"))))
  7562. (eval-when-compile
  7563. (defvar org-property-drawer-re))
  7564. (defvar org-property-start-re) ;; defined below
  7565. (defun org-clone-subtree-with-time-shift (n &optional shift)
  7566. "Clone the task (subtree) at point N times.
  7567. The clones will be inserted as siblings.
  7568. In interactive use, the user will be prompted for the number of
  7569. clones to be produced. When called with a universal prefix argument
  7570. and if the entry has a timestamp, the user will also be prompted for
  7571. a time shift, which may be a repeater as used in time stamps, for
  7572. example `+3d'.
  7573. When a valid repeater is given and the entry contains any time
  7574. stamps, the clones will become a sequence in time, with time
  7575. stamps in the subtree shifted for each clone produced. If SHIFT
  7576. is nil or the empty string, time stamps will be left alone. The
  7577. ID property of the original subtree is removed.
  7578. If the original subtree did contain time stamps with a repeater,
  7579. the following will happen:
  7580. - the repeater will be removed in each clone
  7581. - an additional clone will be produced, with the current, unshifted
  7582. date(s) in the entry.
  7583. - the original entry will be placed *after* all the clones, with
  7584. repeater intact.
  7585. - the start days in the repeater in the original entry will be shifted
  7586. to past the last clone.
  7587. In this way you can spell out a number of instances of a repeating task,
  7588. and still retain the repeater to cover future instances of the task."
  7589. (interactive "nNumber of clones to produce: ")
  7590. (let ((shift
  7591. (or shift
  7592. (if (and (equal current-prefix-arg '(4))
  7593. (save-excursion
  7594. (re-search-forward org-ts-regexp-both
  7595. (save-excursion
  7596. (org-end-of-subtree t)
  7597. (point)) t)))
  7598. (read-from-minibuffer
  7599. "Date shift per clone (e.g. +1w, empty to copy unchanged): ")
  7600. ""))) ;; No time shift
  7601. (n-no-remove -1)
  7602. (drawer-re org-drawer-regexp)
  7603. beg end template task idprop
  7604. shift-n shift-what doshift nmin nmax)
  7605. (if (not (and (integerp n) (> n 0)))
  7606. (error "Invalid number of replications %s" n))
  7607. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  7608. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([hdwmy]\\)[ \t]*\\'"
  7609. shift)))
  7610. (error "Invalid shift specification %s" shift))
  7611. (when doshift
  7612. (setq shift-n (string-to-number (match-string 1 shift))
  7613. shift-what (cdr (assoc (match-string 2 shift)
  7614. '(("d" . day) ("w" . week)
  7615. ("m" . month) ("y" . year))))))
  7616. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  7617. (setq nmin 1 nmax n)
  7618. (org-back-to-heading t)
  7619. (setq beg (point))
  7620. (setq idprop (org-entry-get nil "ID"))
  7621. (org-end-of-subtree t t)
  7622. (or (bolp) (insert "\n"))
  7623. (setq end (point))
  7624. (setq template (buffer-substring beg end))
  7625. (when (and doshift
  7626. (string-match "<[^<>\n]+ [.+]?\\+[0-9]+[hdwmy][^<>\n]*>" template))
  7627. (delete-region beg end)
  7628. (setq end beg)
  7629. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  7630. (goto-char end)
  7631. (loop for n from nmin to nmax do
  7632. ;; prepare clone
  7633. (with-temp-buffer
  7634. (insert template)
  7635. (org-mode)
  7636. (goto-char (point-min))
  7637. (org-show-subtree)
  7638. (and idprop (if org-clone-delete-id
  7639. (org-entry-delete nil "ID")
  7640. (org-id-get-create t)))
  7641. (unless (= n 0)
  7642. (while (re-search-forward "^[ \t]*CLOCK:.*$" nil t)
  7643. (kill-whole-line))
  7644. (goto-char (point-min))
  7645. (while (re-search-forward drawer-re nil t)
  7646. (mapc (lambda (d)
  7647. (org-remove-empty-drawer-at d (point))) org-drawers)))
  7648. (goto-char (point-min))
  7649. (when doshift
  7650. (while (re-search-forward org-ts-regexp-both nil t)
  7651. (org-timestamp-change (* n shift-n) shift-what))
  7652. (unless (= n n-no-remove)
  7653. (goto-char (point-min))
  7654. (while (re-search-forward org-ts-regexp nil t)
  7655. (save-excursion
  7656. (goto-char (match-beginning 0))
  7657. (if (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[hdwmy]\\)")
  7658. (delete-region (match-beginning 1) (match-end 1)))))))
  7659. (setq task (buffer-string)))
  7660. (insert task))
  7661. (goto-char beg)))
  7662. ;;; Outline Sorting
  7663. (defun org-sort (with-case)
  7664. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  7665. Optional argument WITH-CASE means sort case-sensitively."
  7666. (interactive "P")
  7667. (cond
  7668. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  7669. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  7670. (t
  7671. (org-call-with-arg 'org-sort-entries with-case))))
  7672. (defun org-sort-remove-invisible (s)
  7673. "Remove invisible links from string S."
  7674. (remove-text-properties 0 (length s) org-rm-props s)
  7675. (while (string-match org-bracket-link-regexp s)
  7676. (setq s (replace-match (if (match-end 2)
  7677. (match-string 3 s)
  7678. (match-string 1 s)) t t s)))
  7679. (let ((st (format " %s " s)))
  7680. (while (string-match org-emph-re st)
  7681. (setq st (replace-match (format " %s " (match-string 4 st)) t t st)))
  7682. (setq s (substring st 1 -1)))
  7683. s)
  7684. (defvar org-priority-regexp) ; defined later in the file
  7685. (defvar org-after-sorting-entries-or-items-hook nil
  7686. "Hook that is run after a bunch of entries or items have been sorted.
  7687. When children are sorted, the cursor is in the parent line when this
  7688. hook gets called. When a region or a plain list is sorted, the cursor
  7689. will be in the first entry of the sorted region/list.")
  7690. (defun org-sort-entries
  7691. (&optional with-case sorting-type getkey-func compare-func property)
  7692. "Sort entries on a certain level of an outline tree.
  7693. If there is an active region, the entries in the region are sorted.
  7694. Else, if the cursor is before the first entry, sort the top-level items.
  7695. Else, the children of the entry at point are sorted.
  7696. Sorting can be alphabetically, numerically, by date/time as given by
  7697. a time stamp, by a property, by priority order, or by a custom function.
  7698. The command prompts for the sorting type unless it has been given to the
  7699. function through the SORTING-TYPE argument, which needs to be a character,
  7700. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?o ?O ?r ?R ?f ?F). Here is the
  7701. precise meaning of each character:
  7702. n Numerically, by converting the beginning of the entry/item to a number.
  7703. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  7704. o By order of TODO keywords.
  7705. t By date/time, either the first active time stamp in the entry, or, if
  7706. none exist, by the first inactive one.
  7707. s By the scheduled date/time.
  7708. d By deadline date/time.
  7709. c By creation time, which is assumed to be the first inactive time stamp
  7710. at the beginning of a line.
  7711. p By priority according to the cookie.
  7712. r By the value of a property.
  7713. Capital letters will reverse the sort order.
  7714. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  7715. called with point at the beginning of the record. It must return either
  7716. a string or a number that should serve as the sorting key for that record.
  7717. Comparing entries ignores case by default. However, with an optional argument
  7718. WITH-CASE, the sorting considers case as well.
  7719. Sorting is done against the visible part of the headlines, it ignores hidden
  7720. links."
  7721. (interactive "P")
  7722. (let ((case-func (if with-case 'identity 'downcase))
  7723. (cmstr
  7724. ;; The clock marker is lost when using `sort-subr', let's
  7725. ;; store the clocking string.
  7726. (when (equal (marker-buffer org-clock-marker) (current-buffer))
  7727. (save-excursion
  7728. (goto-char org-clock-marker)
  7729. (looking-back "^.*") (match-string-no-properties 0))))
  7730. start beg end stars re re2
  7731. txt what tmp)
  7732. ;; Find beginning and end of region to sort
  7733. (cond
  7734. ((org-region-active-p)
  7735. ;; we will sort the region
  7736. (setq end (region-end)
  7737. what "region")
  7738. (goto-char (region-beginning))
  7739. (if (not (org-at-heading-p)) (outline-next-heading))
  7740. (setq start (point)))
  7741. ((or (org-at-heading-p)
  7742. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  7743. ;; we will sort the children of the current headline
  7744. (org-back-to-heading)
  7745. (setq start (point)
  7746. end (progn (org-end-of-subtree t t)
  7747. (or (bolp) (insert "\n"))
  7748. (org-back-over-empty-lines)
  7749. (point))
  7750. what "children")
  7751. (goto-char start)
  7752. (show-subtree)
  7753. (outline-next-heading))
  7754. (t
  7755. ;; we will sort the top-level entries in this file
  7756. (goto-char (point-min))
  7757. (or (org-at-heading-p) (outline-next-heading))
  7758. (setq start (point))
  7759. (goto-char (point-max))
  7760. (beginning-of-line 1)
  7761. (when (looking-at ".*?\\S-")
  7762. ;; File ends in a non-white line
  7763. (end-of-line 1)
  7764. (insert "\n"))
  7765. (setq end (point-max))
  7766. (setq what "top-level")
  7767. (goto-char start)
  7768. (show-all)))
  7769. (setq beg (point))
  7770. (if (>= beg end) (error "Nothing to sort"))
  7771. (looking-at "\\(\\*+\\)")
  7772. (setq stars (match-string 1)
  7773. re (concat "^" (regexp-quote stars) " +")
  7774. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  7775. txt (buffer-substring beg end))
  7776. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  7777. (if (and (not (equal stars "*")) (string-match re2 txt))
  7778. (error "Region to sort contains a level above the first entry"))
  7779. (unless sorting-type
  7780. (message
  7781. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  7782. [t]ime [s]cheduled [d]eadline [c]reated
  7783. A/N/P/R/O/F/T/S/D/C means reversed:"
  7784. what)
  7785. (setq sorting-type (read-char-exclusive))
  7786. (unless getkey-func
  7787. (and (= (downcase sorting-type) ?f)
  7788. (setq getkey-func
  7789. (org-icompleting-read "Sort using function: "
  7790. obarray 'fboundp t nil nil))
  7791. (setq getkey-func (intern getkey-func))))
  7792. (and (= (downcase sorting-type) ?r)
  7793. (not property)
  7794. (setq property
  7795. (org-icompleting-read "Property: "
  7796. (mapcar 'list (org-buffer-property-keys t))
  7797. nil t))))
  7798. (message "Sorting entries...")
  7799. (save-restriction
  7800. (narrow-to-region start end)
  7801. (let ((dcst (downcase sorting-type))
  7802. (case-fold-search nil)
  7803. (now (current-time)))
  7804. (sort-subr
  7805. (/= dcst sorting-type)
  7806. ;; This function moves to the beginning character of the "record" to
  7807. ;; be sorted.
  7808. (lambda nil
  7809. (if (re-search-forward re nil t)
  7810. (goto-char (match-beginning 0))
  7811. (goto-char (point-max))))
  7812. ;; This function moves to the last character of the "record" being
  7813. ;; sorted.
  7814. (lambda nil
  7815. (save-match-data
  7816. (condition-case nil
  7817. (outline-forward-same-level 1)
  7818. (error
  7819. (goto-char (point-max))))))
  7820. ;; This function returns the value that gets sorted against.
  7821. (lambda nil
  7822. (cond
  7823. ((= dcst ?n)
  7824. (if (looking-at org-complex-heading-regexp)
  7825. (string-to-number (org-sort-remove-invisible (match-string 4)))
  7826. nil))
  7827. ((= dcst ?a)
  7828. (if (looking-at org-complex-heading-regexp)
  7829. (funcall case-func (org-sort-remove-invisible (match-string 4)))
  7830. nil))
  7831. ((= dcst ?t)
  7832. (let ((end (save-excursion (outline-next-heading) (point))))
  7833. (if (or (re-search-forward org-ts-regexp end t)
  7834. (re-search-forward org-ts-regexp-both end t))
  7835. (org-time-string-to-seconds (match-string 0))
  7836. (org-float-time now))))
  7837. ((= dcst ?c)
  7838. (let ((end (save-excursion (outline-next-heading) (point))))
  7839. (if (re-search-forward
  7840. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  7841. end t)
  7842. (org-time-string-to-seconds (match-string 0))
  7843. (org-float-time now))))
  7844. ((= dcst ?s)
  7845. (let ((end (save-excursion (outline-next-heading) (point))))
  7846. (if (re-search-forward org-scheduled-time-regexp end t)
  7847. (org-time-string-to-seconds (match-string 1))
  7848. (org-float-time now))))
  7849. ((= dcst ?d)
  7850. (let ((end (save-excursion (outline-next-heading) (point))))
  7851. (if (re-search-forward org-deadline-time-regexp end t)
  7852. (org-time-string-to-seconds (match-string 1))
  7853. (org-float-time now))))
  7854. ((= dcst ?p)
  7855. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  7856. (string-to-char (match-string 2))
  7857. org-default-priority))
  7858. ((= dcst ?r)
  7859. (or (org-entry-get nil property) ""))
  7860. ((= dcst ?o)
  7861. (if (looking-at org-complex-heading-regexp)
  7862. (- 9999 (length (member (match-string 2)
  7863. org-todo-keywords-1)))))
  7864. ((= dcst ?f)
  7865. (if getkey-func
  7866. (progn
  7867. (setq tmp (funcall getkey-func))
  7868. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  7869. tmp)
  7870. (error "Invalid key function `%s'" getkey-func)))
  7871. (t (error "Invalid sorting type `%c'" sorting-type))))
  7872. nil
  7873. (cond
  7874. ((= dcst ?a) 'string<)
  7875. ((= dcst ?f) compare-func)
  7876. ((member dcst '(?p ?t ?s ?d ?c)) '<)))))
  7877. (run-hooks 'org-after-sorting-entries-or-items-hook)
  7878. ;; Reset the clock marker if needed
  7879. (when cmstr
  7880. (save-excursion
  7881. (goto-char start)
  7882. (search-forward cmstr nil t)
  7883. (move-marker org-clock-marker (point))))
  7884. (message "Sorting entries...done")))
  7885. (defun org-do-sort (table what &optional with-case sorting-type)
  7886. "Sort TABLE of WHAT according to SORTING-TYPE.
  7887. The user will be prompted for the SORTING-TYPE if the call to this
  7888. function does not specify it. WHAT is only for the prompt, to indicate
  7889. what is being sorted. The sorting key will be extracted from
  7890. the car of the elements of the table.
  7891. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  7892. (unless sorting-type
  7893. (message
  7894. "Sort %s: [a]lphabetic, [n]umeric, [t]ime. A/N/T means reversed:"
  7895. what)
  7896. (setq sorting-type (read-char-exclusive)))
  7897. (let ((dcst (downcase sorting-type))
  7898. extractfun comparefun)
  7899. ;; Define the appropriate functions
  7900. (cond
  7901. ((= dcst ?n)
  7902. (setq extractfun 'string-to-number
  7903. comparefun (if (= dcst sorting-type) '< '>)))
  7904. ((= dcst ?a)
  7905. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  7906. (lambda(x) (downcase (org-sort-remove-invisible x))))
  7907. comparefun (if (= dcst sorting-type)
  7908. 'string<
  7909. (lambda (a b) (and (not (string< a b))
  7910. (not (string= a b)))))))
  7911. ((= dcst ?t)
  7912. (setq extractfun
  7913. (lambda (x)
  7914. (if (or (string-match org-ts-regexp x)
  7915. (string-match org-ts-regexp-both x))
  7916. (org-float-time
  7917. (org-time-string-to-time (match-string 0 x)))
  7918. 0))
  7919. comparefun (if (= dcst sorting-type) '< '>)))
  7920. (t (error "Invalid sorting type `%c'" sorting-type)))
  7921. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  7922. table)
  7923. (lambda (a b) (funcall comparefun (car a) (car b))))))
  7924. ;;; The orgstruct minor mode
  7925. ;; Define a minor mode which can be used in other modes in order to
  7926. ;; integrate the org-mode structure editing commands.
  7927. ;; This is really a hack, because the org-mode structure commands use
  7928. ;; keys which normally belong to the major mode. Here is how it
  7929. ;; works: The minor mode defines all the keys necessary to operate the
  7930. ;; structure commands, but wraps the commands into a function which
  7931. ;; tests if the cursor is currently at a headline or a plain list
  7932. ;; item. If that is the case, the structure command is used,
  7933. ;; temporarily setting many Org-mode variables like regular
  7934. ;; expressions for filling etc. However, when any of those keys is
  7935. ;; used at a different location, function uses `key-binding' to look
  7936. ;; up if the key has an associated command in another currently active
  7937. ;; keymap (minor modes, major mode, global), and executes that
  7938. ;; command. There might be problems if any of the keys is otherwise
  7939. ;; used as a prefix key.
  7940. (defcustom orgstruct-heading-prefix-regexp nil
  7941. "Regexp that matches the custom prefix of Org headlines in
  7942. orgstruct(++)-mode."
  7943. :group 'org
  7944. :version "24.4"
  7945. :package-version '(Org . "8.0")
  7946. :type 'string)
  7947. ;;;###autoload(put 'orgstruct-heading-prefix-regexp 'safe-local-variable 'stringp)
  7948. (defcustom orgstruct-setup-hook nil
  7949. "Hook run after orgstruct-mode-map is filled."
  7950. :group 'org
  7951. :version "24.4"
  7952. :package-version '(Org . "8.0")
  7953. :type 'hook)
  7954. (defvar orgstruct-initialized nil)
  7955. (defvar org-local-vars nil
  7956. "List of local variables, for use by `orgstruct-mode'.")
  7957. ;;;###autoload
  7958. (define-minor-mode orgstruct-mode
  7959. "Toggle the minor mode `orgstruct-mode'.
  7960. This mode is for using Org-mode structure commands in other
  7961. modes. The following keys behave as if Org-mode were active, if
  7962. the cursor is on a headline, or on a plain list item (both as
  7963. defined by Org-mode)."
  7964. nil " OrgStruct" (make-sparse-keymap)
  7965. (funcall (if orgstruct-mode
  7966. 'add-to-invisibility-spec
  7967. 'remove-from-invisibility-spec)
  7968. '(outline . t))
  7969. (when orgstruct-mode
  7970. (org-load-modules-maybe)
  7971. (unless orgstruct-initialized
  7972. (orgstruct-setup)
  7973. (setq orgstruct-initialized t))))
  7974. ;;;###autoload
  7975. (defun turn-on-orgstruct ()
  7976. "Unconditionally turn on `orgstruct-mode'."
  7977. (orgstruct-mode 1))
  7978. (defvar org-fb-vars nil)
  7979. (make-variable-buffer-local 'org-fb-vars)
  7980. (defun orgstruct++-mode (&optional arg)
  7981. "Toggle `orgstruct-mode', the enhanced version of it.
  7982. In addition to setting orgstruct-mode, this also exports all
  7983. indentation and autofilling variables from org-mode into the
  7984. buffer. It will also recognize item context in multiline items."
  7985. (interactive "P")
  7986. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  7987. (if (< arg 1)
  7988. (progn (orgstruct-mode -1)
  7989. (mapc (lambda(v)
  7990. (org-set-local (car v)
  7991. (if (eq (car-safe (cadr v)) 'quote) (cadadr v) (cadr v))))
  7992. org-fb-vars))
  7993. (orgstruct-mode 1)
  7994. (setq org-fb-vars nil)
  7995. (let (var val)
  7996. (mapc
  7997. (lambda (x)
  7998. (when (string-match
  7999. "^\\(paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|fill-prefix\\|indent-\\)"
  8000. (symbol-name (car x)))
  8001. (setq var (car x) val (nth 1 x))
  8002. (push (list var `(quote ,(eval var))) org-fb-vars)
  8003. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  8004. org-local-vars)
  8005. (org-set-local 'orgstruct-is-++ t))))
  8006. (defvar orgstruct-is-++ nil
  8007. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  8008. (make-variable-buffer-local 'orgstruct-is-++)
  8009. ;;;###autoload
  8010. (defun turn-on-orgstruct++ ()
  8011. "Unconditionally turn on `orgstruct++-mode'."
  8012. (orgstruct++-mode 1))
  8013. (defun orgstruct-error ()
  8014. "Error when there is no default binding for a structure key."
  8015. (interactive)
  8016. (funcall (if (fboundp 'user-error)
  8017. 'user-error
  8018. 'error)
  8019. "This key has no function outside structure elements"))
  8020. (defun orgstruct-setup ()
  8021. "Setup orgstruct keymap."
  8022. (dolist (cell '((org-demote . t)
  8023. (org-metaleft . t)
  8024. (org-metaright . t)
  8025. (org-promote . t)
  8026. (org-shiftmetaleft . t)
  8027. (org-shiftmetaright . t)
  8028. org-backward-element
  8029. org-backward-heading-same-level
  8030. org-ctrl-c-ret
  8031. org-ctrl-c-minus
  8032. org-ctrl-c-star
  8033. org-cycle
  8034. org-forward-heading-same-level
  8035. org-insert-heading
  8036. org-insert-heading-respect-content
  8037. org-kill-note-or-show-branches
  8038. org-mark-subtree
  8039. org-meta-return
  8040. org-metadown
  8041. org-metaup
  8042. org-narrow-to-subtree
  8043. org-promote-subtree
  8044. org-reveal
  8045. org-shiftdown
  8046. org-shiftleft
  8047. org-shiftmetadown
  8048. org-shiftmetaup
  8049. org-shiftright
  8050. org-shifttab
  8051. org-shifttab
  8052. org-shiftup
  8053. org-show-subtree
  8054. org-sort
  8055. org-up-element
  8056. outline-demote
  8057. outline-next-visible-heading
  8058. outline-previous-visible-heading
  8059. outline-promote
  8060. outline-up-heading
  8061. show-children))
  8062. (let ((f (or (car-safe cell) cell))
  8063. (disable-when-heading-prefix (cdr-safe cell)))
  8064. (when (fboundp f)
  8065. (dolist (binding (nconc (where-is-internal f org-mode-map)
  8066. (where-is-internal f outline-mode-map)))
  8067. ;; TODO use local-function-key-map
  8068. (dolist (rep '(("<tab>" . "TAB")
  8069. ("<return>" . "RET")
  8070. ("<escape>" . "ESC")
  8071. ("<delete>" . "DEL")))
  8072. (setq binding (read-kbd-macro
  8073. (let ((case-fold-search))
  8074. (replace-regexp-in-string
  8075. (regexp-quote (cdr rep))
  8076. (car rep)
  8077. (key-description binding))))))
  8078. (let ((key (lookup-key orgstruct-mode-map binding)))
  8079. (when (or (not key) (numberp key))
  8080. (condition-case nil
  8081. (org-defkey orgstruct-mode-map
  8082. binding
  8083. (orgstruct-make-binding f binding disable-when-heading-prefix))
  8084. (error nil))))))))
  8085. (run-hooks 'orgstruct-setup-hook))
  8086. (defun orgstruct-make-binding (fun key disable-when-heading-prefix)
  8087. "Create a function for binding in the structure minor mode.
  8088. FUN is the command to call inside a table. KEY is the key that
  8089. should be checked in for a command to execute outside of tables.
  8090. Non-nil DISABLE-WHEN-HEADING-PREFIX means to disable the command
  8091. if `orgstruct-heading-prefix-regexp' is non-nil."
  8092. (let ((name (concat "orgstruct-hijacker-" (symbol-name fun))))
  8093. (let ((nname name)
  8094. (i 0))
  8095. (while (fboundp (intern nname))
  8096. (setq nname (format "%s-%d" name (setq i (1+ i)))))
  8097. (setq name (intern nname)))
  8098. (eval
  8099. (let ((bindings '((org-heading-regexp
  8100. (concat "^"
  8101. orgstruct-heading-prefix-regexp
  8102. "\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ ]*$"))
  8103. (org-outline-regexp
  8104. (concat orgstruct-heading-prefix-regexp "\\*+ "))
  8105. (org-outline-regexp-bol
  8106. (concat "^" org-outline-regexp))
  8107. (outline-regexp org-outline-regexp)
  8108. (outline-heading-end-regexp "\n")
  8109. (outline-level 'org-outline-level)
  8110. (outline-heading-alist))))
  8111. `(defun ,name (arg)
  8112. ,(concat "In Structure, run `" (symbol-name fun) "'.\n"
  8113. "Outside of structure, run the binding of `"
  8114. (key-description key) "'."
  8115. (when disable-when-heading-prefix
  8116. (concat
  8117. "\nIf `orgstruct-heading-prefix-regexp' is non-nil, this command will always fall\n"
  8118. "back to the default binding due to limitations of Org's implementation of\n"
  8119. "`" (symbol-name fun) "'.")))
  8120. (interactive "p")
  8121. (let* ((disable
  8122. ,(when disable-when-heading-prefix
  8123. '(and orgstruct-heading-prefix-regexp
  8124. (not (string= orgstruct-heading-prefix-regexp "")))))
  8125. (fallback
  8126. (or disable
  8127. (not
  8128. (let* ,bindings
  8129. (org-context-p 'headline 'item
  8130. ,(when (memq fun '(org-insert-heading))
  8131. '(when orgstruct-is-++
  8132. 'item-body))))))))
  8133. (if fallback
  8134. (let* ((orgstruct-mode)
  8135. (binding
  8136. (loop with key = ,key
  8137. for rep in
  8138. '(nil
  8139. ("<\\([^>]*\\)tab>" . "\\1TAB")
  8140. ("<\\([^>]*\\)return>" . "\\1RET")
  8141. ("<\\([^>]*\\)escape>" . "\\1ESC")
  8142. ("<\\([^>]*\\)delete>" . "\\1DEL"))
  8143. do
  8144. (when rep
  8145. (setq key (read-kbd-macro
  8146. (let ((case-fold-search))
  8147. (replace-regexp-in-string
  8148. (car rep)
  8149. (cdr rep)
  8150. (key-description key))))))
  8151. thereis (key-binding key))))
  8152. (if (keymapp binding)
  8153. (set-temporary-overlay-map binding)
  8154. (let ((func (or binding
  8155. (unless disable
  8156. 'orgstruct-error))))
  8157. (when func
  8158. (call-interactively func)))))
  8159. (org-run-like-in-org-mode
  8160. (lambda ()
  8161. (interactive)
  8162. (let* ,bindings
  8163. (call-interactively ',fun)))))))))
  8164. name))
  8165. (defun org-contextualize-keys (alist contexts)
  8166. "Return valid elements in ALIST depending on CONTEXTS.
  8167. `org-agenda-custom-commands' or `org-capture-templates' are the
  8168. values used for ALIST, and `org-agenda-custom-commands-contexts'
  8169. or `org-capture-templates-contexts' are the associated contexts
  8170. definitions."
  8171. (let ((contexts
  8172. ;; normalize contexts
  8173. (mapcar
  8174. (lambda(c) (cond ((listp (cadr c))
  8175. (list (car c) (car c) (cadr c)))
  8176. ((string= "" (cadr c))
  8177. (list (car c) (car c) (caddr c)))
  8178. (t c))) contexts))
  8179. (a alist) c r s)
  8180. ;; loop over all commands or templates
  8181. (while (setq c (pop a))
  8182. (let (vrules repl)
  8183. (cond
  8184. ((not (assoc (car c) contexts))
  8185. (push c r))
  8186. ((and (assoc (car c) contexts)
  8187. (setq vrules (org-contextualize-validate-key
  8188. (car c) contexts)))
  8189. (mapc (lambda (vr)
  8190. (when (not (equal (car vr) (cadr vr)))
  8191. (setq repl vr))) vrules)
  8192. (if (not repl) (push c r)
  8193. (push (cadr repl) s)
  8194. (push
  8195. (cons (car c)
  8196. (cdr (or (assoc (cadr repl) alist)
  8197. (error "Undefined key `%s' as contextual replacement for `%s'"
  8198. (cadr repl) (car c)))))
  8199. r))))))
  8200. ;; Return limited ALIST, possibly with keys modified, and deduplicated
  8201. (delq
  8202. nil
  8203. (delete-dups
  8204. (mapcar (lambda (x)
  8205. (let ((tpl (car x)))
  8206. (when (not (delq
  8207. nil
  8208. (mapcar (lambda(y)
  8209. (equal y tpl)) s))) x)))
  8210. (reverse r))))))
  8211. (defun org-contextualize-validate-key (key contexts)
  8212. "Check CONTEXTS for agenda or capture KEY."
  8213. (let (r rr res)
  8214. (while (setq r (pop contexts))
  8215. (mapc
  8216. (lambda (rr)
  8217. (when
  8218. (and (equal key (car r))
  8219. (if (functionp rr) (funcall rr)
  8220. (or (and (eq (car rr) 'in-file)
  8221. (buffer-file-name)
  8222. (string-match (cdr rr) (buffer-file-name)))
  8223. (and (eq (car rr) 'in-mode)
  8224. (string-match (cdr rr) (symbol-name major-mode)))
  8225. (and (eq (car rr) 'in-buffer)
  8226. (string-match (cdr rr) (buffer-name)))
  8227. (when (and (eq (car rr) 'not-in-file)
  8228. (buffer-file-name))
  8229. (not (string-match (cdr rr) (buffer-file-name))))
  8230. (when (eq (car rr) 'not-in-mode)
  8231. (not (string-match (cdr rr) (symbol-name major-mode))))
  8232. (when (eq (car rr) 'not-in-buffer)
  8233. (not (string-match (cdr rr) (buffer-name)))))))
  8234. (push r res)))
  8235. (car (last r))))
  8236. (delete-dups (delq nil res))))
  8237. (defun org-context-p (&rest contexts)
  8238. "Check if local context is any of CONTEXTS.
  8239. Possible values in the list of contexts are `table', `headline', and `item'."
  8240. (let ((pos (point)))
  8241. (goto-char (point-at-bol))
  8242. (prog1 (or (and (memq 'table contexts)
  8243. (looking-at "[ \t]*|"))
  8244. (and (memq 'headline contexts)
  8245. (looking-at org-outline-regexp))
  8246. (and (memq 'item contexts)
  8247. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  8248. (and (memq 'item-body contexts)
  8249. (org-in-item-p)))
  8250. (goto-char pos))))
  8251. (defun org-get-local-variables ()
  8252. "Return a list of all local variables in an Org mode buffer."
  8253. (let (varlist)
  8254. (with-current-buffer (get-buffer-create "*Org tmp*")
  8255. (erase-buffer)
  8256. (org-mode)
  8257. (setq varlist (buffer-local-variables)))
  8258. (kill-buffer "*Org tmp*")
  8259. (delq nil
  8260. (mapcar
  8261. (lambda (x)
  8262. (setq x
  8263. (if (symbolp x)
  8264. (list x)
  8265. (list (car x) (cdr x))))
  8266. (if (and (not (get (car x) 'org-state))
  8267. (string-match
  8268. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|indent-\\)"
  8269. (symbol-name (car x))))
  8270. x nil))
  8271. varlist))))
  8272. (defun org-clone-local-variables (from-buffer &optional regexp)
  8273. "Clone local variables from FROM-BUFFER.
  8274. Optional argument REGEXP selects variables to clone."
  8275. (mapc
  8276. (lambda (pair)
  8277. (and (symbolp (car pair))
  8278. (or (null regexp)
  8279. (string-match regexp (symbol-name (car pair))))
  8280. (set (make-local-variable (car pair))
  8281. (cdr pair))))
  8282. (buffer-local-variables from-buffer)))
  8283. ;;;###autoload
  8284. (defun org-run-like-in-org-mode (cmd)
  8285. "Run a command, pretending that the current buffer is in Org-mode.
  8286. This will temporarily bind local variables that are typically bound in
  8287. Org-mode to the values they have in Org-mode, and then interactively
  8288. call CMD."
  8289. (org-load-modules-maybe)
  8290. (unless org-local-vars
  8291. (setq org-local-vars (org-get-local-variables)))
  8292. (let (binds)
  8293. (dolist (var org-local-vars)
  8294. (when (or (not (boundp (car var)))
  8295. (eq (symbol-value (car var))
  8296. (default-value (car var))))
  8297. (push (list (car var) `(quote ,(cadr var))) binds)))
  8298. (eval `(let ,binds
  8299. (call-interactively (quote ,cmd))))))
  8300. ;;;; Archiving
  8301. (defun org-get-category (&optional pos force-refresh)
  8302. "Get the category applying to position POS."
  8303. (save-match-data
  8304. (if force-refresh (org-refresh-category-properties))
  8305. (let ((pos (or pos (point))))
  8306. (or (get-text-property pos 'org-category)
  8307. (progn (org-refresh-category-properties)
  8308. (get-text-property pos 'org-category))))))
  8309. (defun org-refresh-category-properties ()
  8310. "Refresh category text properties in the buffer."
  8311. (let ((case-fold-search t)
  8312. (inhibit-read-only t)
  8313. (def-cat (cond
  8314. ((null org-category)
  8315. (if buffer-file-name
  8316. (file-name-sans-extension
  8317. (file-name-nondirectory buffer-file-name))
  8318. "???"))
  8319. ((symbolp org-category) (symbol-name org-category))
  8320. (t org-category)))
  8321. beg end cat pos optionp)
  8322. (org-with-silent-modifications
  8323. (save-excursion
  8324. (save-restriction
  8325. (widen)
  8326. (goto-char (point-min))
  8327. (put-text-property (point) (point-max) 'org-category def-cat)
  8328. (while (re-search-forward
  8329. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  8330. (setq pos (match-end 0)
  8331. optionp (equal (char-after (match-beginning 0)) ?#)
  8332. cat (org-trim (match-string 2)))
  8333. (if optionp
  8334. (setq beg (point-at-bol) end (point-max))
  8335. (org-back-to-heading t)
  8336. (setq beg (point) end (org-end-of-subtree t t)))
  8337. (put-text-property beg end 'org-category cat)
  8338. (put-text-property beg end 'org-category-position beg)
  8339. (goto-char pos)))))))
  8340. (defun org-refresh-properties (dprop tprop)
  8341. "Refresh buffer text properties.
  8342. DPROP is the drawer property and TPROP is the corresponding text
  8343. property to set."
  8344. (let ((case-fold-search t)
  8345. (inhibit-read-only t) p)
  8346. (org-with-silent-modifications
  8347. (save-excursion
  8348. (save-restriction
  8349. (widen)
  8350. (goto-char (point-min))
  8351. (while (re-search-forward (concat "^[ \t]*:" dprop ": +\\(.*\\)[ \t]*$") nil t)
  8352. (setq p (org-match-string-no-properties 1))
  8353. (save-excursion
  8354. (org-back-to-heading t)
  8355. (put-text-property
  8356. (point-at-bol) (point-at-eol) tprop p))))))))
  8357. ;;;; Link Stuff
  8358. ;;; Link abbreviations
  8359. (defun org-link-expand-abbrev (link)
  8360. "Apply replacements as defined in `org-link-abbrev-alist'."
  8361. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  8362. (let* ((key (match-string 1 link))
  8363. (as (or (assoc key org-link-abbrev-alist-local)
  8364. (assoc key org-link-abbrev-alist)))
  8365. (tag (and (match-end 2) (match-string 3 link)))
  8366. rpl)
  8367. (if (not as)
  8368. link
  8369. (setq rpl (cdr as))
  8370. (cond
  8371. ((symbolp rpl) (funcall rpl tag))
  8372. ((string-match "%(\\([^)]+\\))" rpl)
  8373. (replace-match (funcall (intern-soft (match-string 1 rpl)) tag) t t rpl))
  8374. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  8375. ((string-match "%h" rpl)
  8376. (replace-match (url-hexify-string (or tag "")) t t rpl))
  8377. (t (concat rpl tag)))))
  8378. link))
  8379. ;;; Storing and inserting links
  8380. (defvar org-insert-link-history nil
  8381. "Minibuffer history for links inserted with `org-insert-link'.")
  8382. (defvar org-stored-links nil
  8383. "Contains the links stored with `org-store-link'.")
  8384. (defvar org-store-link-plist nil
  8385. "Plist with info about the most recently link created with `org-store-link'.")
  8386. (defvar org-link-protocols nil
  8387. "Link protocols added to Org-mode using `org-add-link-type'.")
  8388. (defvar org-store-link-functions nil
  8389. "List of functions that are called to create and store a link.
  8390. Each function will be called in turn until one returns a non-nil
  8391. value. Each function should check if it is responsible for creating
  8392. this link (for example by looking at the major mode).
  8393. If not, it must exit and return nil.
  8394. If yes, it should return a non-nil value after a calling
  8395. `org-store-link-props' with a list of properties and values.
  8396. Special properties are:
  8397. :type The link prefix, like \"http\". This must be given.
  8398. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  8399. This is obligatory as well.
  8400. :description Optional default description for the second pair
  8401. of brackets in an Org-mode link. The user can still change
  8402. this when inserting this link into an Org-mode buffer.
  8403. In addition to these, any additional properties can be specified
  8404. and then used in capture templates.")
  8405. (defun org-add-link-type (type &optional follow export)
  8406. "Add TYPE to the list of `org-link-types'.
  8407. Re-compute all regular expressions depending on `org-link-types'
  8408. FOLLOW and EXPORT are two functions.
  8409. FOLLOW should take the link path as the single argument and do whatever
  8410. is necessary to follow the link, for example find a file or display
  8411. a mail message.
  8412. EXPORT should format the link path for export to one of the export formats.
  8413. It should be a function accepting three arguments:
  8414. path the path of the link, the text after the prefix (like \"http:\")
  8415. desc the description of the link, if any, or a description added by
  8416. org-export-normalize-links if there is none
  8417. format the export format, a symbol like `html' or `latex' or `ascii'..
  8418. The function may use the FORMAT information to return different values
  8419. depending on the format. The return value will be put literally into
  8420. the exported file. If the return value is nil, this means Org should
  8421. do what it normally does with links which do not have EXPORT defined.
  8422. Org-mode has a built-in default for exporting links. If you are happy with
  8423. this default, there is no need to define an export function for the link
  8424. type. For a simple example of an export function, see `org-bbdb.el'."
  8425. (add-to-list 'org-link-types type t)
  8426. (org-make-link-regexps)
  8427. (if (assoc type org-link-protocols)
  8428. (setcdr (assoc type org-link-protocols) (list follow export))
  8429. (push (list type follow export) org-link-protocols)))
  8430. (defvar org-agenda-buffer-name) ; Defined in org-agenda.el
  8431. (defvar org-id-link-to-org-use-id) ; Defined in org-id.el
  8432. ;;;###autoload
  8433. (defun org-store-link (arg)
  8434. "\\<org-mode-map>Store an org-link to the current location.
  8435. This link is added to `org-stored-links' and can later be inserted
  8436. into an org-buffer with \\[org-insert-link].
  8437. For some link types, a prefix arg is interpreted.
  8438. For links to Usenet articles, arg negates `org-gnus-prefer-web-links'.
  8439. For file links, arg negates `org-context-in-file-links'.
  8440. A double prefix arg force skipping storing functions that are not
  8441. part of Org's core.
  8442. A triple prefix arg force storing a link for each line in the
  8443. active region."
  8444. (interactive "P")
  8445. (org-load-modules-maybe)
  8446. (if (and (equal arg '(64)) (org-region-active-p))
  8447. (save-excursion
  8448. (let ((end (region-end)))
  8449. (goto-char (region-beginning))
  8450. (set-mark (point))
  8451. (while (< (point-at-eol) end)
  8452. (move-end-of-line 1) (activate-mark)
  8453. (let (current-prefix-arg)
  8454. (call-interactively 'org-store-link))
  8455. (move-beginning-of-line 2)
  8456. (set-mark (point)))))
  8457. (org-with-limited-levels
  8458. (setq org-store-link-plist nil)
  8459. (let (link cpltxt desc description search
  8460. txt custom-id agenda-link sfuns sfunsn)
  8461. (cond
  8462. ;; Store a link using an external link type
  8463. ((and (not (equal arg '(16)))
  8464. (setq sfuns
  8465. (delq
  8466. nil (mapcar (lambda (f)
  8467. (let (fs) (if (funcall f) (push f fs))))
  8468. org-store-link-functions))
  8469. sfunsn (mapcar (lambda (fu) (symbol-name (car fu))) sfuns))
  8470. (or (and (cdr sfuns)
  8471. (funcall (intern
  8472. (completing-read
  8473. "Which function for creating the link? "
  8474. sfunsn t (car sfunsn)))))
  8475. (funcall (caar sfuns)))
  8476. (setq link (plist-get org-store-link-plist :link)
  8477. desc (or (plist-get org-store-link-plist
  8478. :description) link))))
  8479. ;; Store a link from a source code buffer
  8480. ((org-src-edit-buffer-p)
  8481. (let (label gc)
  8482. (while (or (not label)
  8483. (save-excursion
  8484. (save-restriction
  8485. (widen)
  8486. (goto-char (point-min))
  8487. (re-search-forward
  8488. (regexp-quote (format org-coderef-label-format label))
  8489. nil t))))
  8490. (when label (message "Label exists already") (sit-for 2))
  8491. (setq label (read-string "Code line label: " label)))
  8492. (end-of-line 1)
  8493. (setq link (format org-coderef-label-format label))
  8494. (setq gc (- 79 (length link)))
  8495. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  8496. (insert link)
  8497. (setq link (concat "(" label ")") desc nil)))
  8498. ;; We are in the agenda, link to referenced location
  8499. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  8500. (let ((m (or (get-text-property (point) 'org-hd-marker)
  8501. (get-text-property (point) 'org-marker))))
  8502. (when m
  8503. (org-with-point-at m
  8504. (setq agenda-link
  8505. (if (org-called-interactively-p 'any)
  8506. (call-interactively 'org-store-link)
  8507. (org-store-link nil)))))))
  8508. ((eq major-mode 'calendar-mode)
  8509. (let ((cd (calendar-cursor-to-date)))
  8510. (setq link
  8511. (format-time-string
  8512. (car org-time-stamp-formats)
  8513. (apply 'encode-time
  8514. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  8515. nil nil nil))))
  8516. (org-store-link-props :type "calendar" :date cd)))
  8517. ((eq major-mode 'help-mode)
  8518. (setq link (concat "help:" (save-excursion
  8519. (goto-char (point-min))
  8520. (looking-at "^[^ ]+")
  8521. (match-string 0))))
  8522. (org-store-link-props :type "help"))
  8523. ((eq major-mode 'w3-mode)
  8524. (setq cpltxt (if (and (buffer-name)
  8525. (not (string-match "Untitled" (buffer-name))))
  8526. (buffer-name)
  8527. (url-view-url t))
  8528. link (url-view-url t))
  8529. (org-store-link-props :type "w3" :url (url-view-url t)))
  8530. ((eq major-mode 'image-mode)
  8531. (setq cpltxt (concat "file:"
  8532. (abbreviate-file-name buffer-file-name))
  8533. link cpltxt)
  8534. (org-store-link-props :type "image" :file buffer-file-name))
  8535. ;; In dired, store a link to the file of the current line
  8536. ((eq major-mode 'dired-mode)
  8537. (let ((file (dired-get-filename nil t)))
  8538. (setq file (if file
  8539. (abbreviate-file-name
  8540. (expand-file-name (dired-get-filename nil t)))
  8541. ;; otherwise, no file so use current directory.
  8542. default-directory))
  8543. (setq cpltxt (concat "file:" file)
  8544. link cpltxt)))
  8545. ((setq search (run-hook-with-args-until-success
  8546. 'org-create-file-search-functions))
  8547. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  8548. "::" search))
  8549. (setq cpltxt (or description link)))
  8550. ((and (buffer-file-name (buffer-base-buffer)) (derived-mode-p 'org-mode))
  8551. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  8552. (cond
  8553. ;; Store a link using the radio target at point
  8554. ((org-in-regexp "<<\\(.*?\\)>>")
  8555. (setq cpltxt
  8556. (concat "file:"
  8557. (abbreviate-file-name
  8558. (buffer-file-name (buffer-base-buffer)))
  8559. "::" (match-string 1))
  8560. link cpltxt))
  8561. ((and (featurep 'org-id)
  8562. (or (eq org-id-link-to-org-use-id t)
  8563. (and (org-called-interactively-p 'any)
  8564. (or (eq org-id-link-to-org-use-id 'create-if-interactive)
  8565. (and (eq org-id-link-to-org-use-id
  8566. 'create-if-interactive-and-no-custom-id)
  8567. (not custom-id))))
  8568. (and org-id-link-to-org-use-id (org-entry-get nil "ID"))))
  8569. ;; Store a link using the ID at point
  8570. (setq link (condition-case nil
  8571. (prog1 (org-id-store-link)
  8572. (setq desc (plist-get org-store-link-plist
  8573. :description)))
  8574. (error
  8575. ;; Probably before first headline, link only to file
  8576. (concat "file:"
  8577. (abbreviate-file-name
  8578. (buffer-file-name (buffer-base-buffer))))))))
  8579. (t
  8580. ;; Just link to current headline
  8581. (setq cpltxt (concat "file:"
  8582. (abbreviate-file-name
  8583. (buffer-file-name (buffer-base-buffer)))))
  8584. ;; Add a context search string
  8585. (when (org-xor org-context-in-file-links arg)
  8586. (let* ((ee (org-element-at-point))
  8587. (et (org-element-type ee))
  8588. (ev (plist-get (cadr ee) :value))
  8589. (ek (plist-get (cadr ee) :key))
  8590. (eok (and (stringp ek) (string-match "name" ek))))
  8591. (setq txt (cond
  8592. ((org-at-heading-p) nil)
  8593. ((and (eq et 'keyword) eok) ev)
  8594. ((org-region-active-p)
  8595. (buffer-substring (region-beginning) (region-end)))))
  8596. (when (or (null txt) (string-match "\\S-" txt))
  8597. (setq cpltxt
  8598. (concat cpltxt "::"
  8599. (condition-case nil
  8600. (org-make-org-heading-search-string txt)
  8601. (error "")))
  8602. desc (or (and (eq et 'keyword) eok ev)
  8603. (nth 4 (ignore-errors (org-heading-components)))
  8604. "NONE")))))
  8605. (if (string-match "::\\'" cpltxt)
  8606. (setq cpltxt (substring cpltxt 0 -2)))
  8607. (setq link cpltxt))))
  8608. ((buffer-file-name (buffer-base-buffer))
  8609. ;; Just link to this file here.
  8610. (setq cpltxt (concat "file:"
  8611. (abbreviate-file-name
  8612. (buffer-file-name (buffer-base-buffer)))))
  8613. ;; Add a context string.
  8614. (when (org-xor org-context-in-file-links arg)
  8615. (setq txt (if (org-region-active-p)
  8616. (buffer-substring (region-beginning) (region-end))
  8617. (buffer-substring (point-at-bol) (point-at-eol))))
  8618. ;; Only use search option if there is some text.
  8619. (when (string-match "\\S-" txt)
  8620. (setq cpltxt
  8621. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  8622. desc "NONE")))
  8623. (setq link cpltxt))
  8624. ((org-called-interactively-p 'interactive)
  8625. (user-error "No method for storing a link from this buffer"))
  8626. (t (setq link nil)))
  8627. ;; We're done setting link and desc, clean up
  8628. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  8629. (setq link (or link cpltxt)
  8630. desc (or desc cpltxt))
  8631. (cond ((equal desc "NONE") (setq desc nil))
  8632. ((string-match org-bracket-link-regexp desc)
  8633. (setq desc
  8634. (replace-regexp-in-string
  8635. org-bracket-link-regexp
  8636. (concat "\\3" (if (equal (length (match-string 0 desc))
  8637. (length desc)) "*" "")) desc))))
  8638. ;; Return the link
  8639. (if (not (and (or (org-called-interactively-p 'any)
  8640. executing-kbd-macro) link))
  8641. (or agenda-link (and link (org-make-link-string link desc)))
  8642. (push (list link desc) org-stored-links)
  8643. (message "Stored: %s" (or desc link))
  8644. (when custom-id
  8645. (setq link (concat "file:" (abbreviate-file-name
  8646. (buffer-file-name)) "::#" custom-id))
  8647. (push (list link desc) org-stored-links)))))))
  8648. (defun org-store-link-props (&rest plist)
  8649. "Store link properties, extract names and addresses."
  8650. (let (x adr)
  8651. (when (setq x (plist-get plist :from))
  8652. (setq adr (mail-extract-address-components x))
  8653. (setq plist (plist-put plist :fromname (car adr)))
  8654. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  8655. (when (setq x (plist-get plist :to))
  8656. (setq adr (mail-extract-address-components x))
  8657. (setq plist (plist-put plist :toname (car adr)))
  8658. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  8659. (let ((from (plist-get plist :from))
  8660. (to (plist-get plist :to)))
  8661. (when (and from to org-from-is-user-regexp)
  8662. (setq plist
  8663. (plist-put plist :fromto
  8664. (if (string-match org-from-is-user-regexp from)
  8665. (concat "to %t")
  8666. (concat "from %f"))))))
  8667. (setq org-store-link-plist plist))
  8668. (defun org-add-link-props (&rest plist)
  8669. "Add these properties to the link property list."
  8670. (let (key value)
  8671. (while plist
  8672. (setq key (pop plist) value (pop plist))
  8673. (setq org-store-link-plist
  8674. (plist-put org-store-link-plist key value)))))
  8675. (defun org-email-link-description (&optional fmt)
  8676. "Return the description part of an email link.
  8677. This takes information from `org-store-link-plist' and formats it
  8678. according to FMT (default from `org-email-link-description-format')."
  8679. (setq fmt (or fmt org-email-link-description-format))
  8680. (let* ((p org-store-link-plist)
  8681. (to (plist-get p :toaddress))
  8682. (from (plist-get p :fromaddress))
  8683. (table
  8684. (list
  8685. (cons "%c" (plist-get p :fromto))
  8686. (cons "%F" (plist-get p :from))
  8687. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  8688. (cons "%T" (plist-get p :to))
  8689. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  8690. (cons "%s" (plist-get p :subject))
  8691. (cons "%d" (plist-get p :date))
  8692. (cons "%m" (plist-get p :message-id)))))
  8693. (when (string-match "%c" fmt)
  8694. ;; Check if the user wrote this message
  8695. (if (and org-from-is-user-regexp from to
  8696. (save-match-data (string-match org-from-is-user-regexp from)))
  8697. (setq fmt (replace-match "to %t" t t fmt))
  8698. (setq fmt (replace-match "from %f" t t fmt))))
  8699. (org-replace-escapes fmt table)))
  8700. (defun org-make-org-heading-search-string (&optional string)
  8701. "Make search string for the current headline or STRING."
  8702. (let ((s (or string
  8703. (and (derived-mode-p 'org-mode)
  8704. (save-excursion
  8705. (org-back-to-heading t)
  8706. (org-element-property :raw-value (org-element-at-point))))))
  8707. (lines org-context-in-file-links))
  8708. (or string (setq s (concat "*" s))) ; Add * for headlines
  8709. (setq s (replace-regexp-in-string "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" "" s))
  8710. (when (and string (integerp lines) (> lines 0))
  8711. (let ((slines (org-split-string s "\n")))
  8712. (when (< lines (length slines))
  8713. (setq s (mapconcat
  8714. 'identity
  8715. (reverse (nthcdr (- (length slines) lines)
  8716. (reverse slines))) "\n")))))
  8717. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  8718. (defun org-make-link-string (link &optional description)
  8719. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  8720. (unless (string-match "\\S-" link)
  8721. (error "Empty link"))
  8722. (when (and description
  8723. (stringp description)
  8724. (not (string-match "\\S-" description)))
  8725. (setq description nil))
  8726. (when (stringp description)
  8727. ;; Remove brackets from the description, they are fatal.
  8728. (while (string-match "\\[" description)
  8729. (setq description (replace-match "{" t t description)))
  8730. (while (string-match "\\]" description)
  8731. (setq description (replace-match "}" t t description))))
  8732. (when (equal link description)
  8733. ;; No description needed, it is identical
  8734. (setq description nil))
  8735. (when (and (not description)
  8736. (not (string-match (org-image-file-name-regexp) link))
  8737. (not (equal link (org-link-escape link))))
  8738. (setq description (org-extract-attributes link)))
  8739. (setq link
  8740. (cond ((string-match (org-image-file-name-regexp) link) link)
  8741. ((string-match org-link-types-re link)
  8742. (concat (match-string 1 link)
  8743. (org-link-escape (substring link (match-end 1)))))
  8744. (t (org-link-escape link))))
  8745. (concat "[[" link "]"
  8746. (if description (concat "[" description "]") "")
  8747. "]"))
  8748. (defconst org-link-escape-chars
  8749. '(?\ ?\[ ?\] ?\; ?\= ?\+)
  8750. "List of characters that should be escaped in link.
  8751. This is the list that is used for internal purposes.")
  8752. (defconst org-link-escape-chars-browser
  8753. '(?\ )
  8754. "List of escapes for characters that are problematic in links.
  8755. This is the list that is used before handing over to the browser.")
  8756. (defun org-link-escape (text &optional table merge)
  8757. "Return percent escaped representation of TEXT.
  8758. TEXT is a string with the text to escape.
  8759. Optional argument TABLE is a list with characters that should be
  8760. escaped. When nil, `org-link-escape-chars' is used.
  8761. If optional argument MERGE is set, merge TABLE into
  8762. `org-link-escape-chars'."
  8763. (cond
  8764. ((and table merge)
  8765. (mapc (lambda (defchr)
  8766. (unless (member defchr table)
  8767. (setq table (cons defchr table)))) org-link-escape-chars))
  8768. ((null table)
  8769. (setq table org-link-escape-chars)))
  8770. (mapconcat
  8771. (lambda (char)
  8772. (if (or (member char table)
  8773. (and (or (< char 32) (= char 37) (> char 126))
  8774. org-url-hexify-p))
  8775. (mapconcat (lambda (sequence-element)
  8776. (format "%%%.2X" sequence-element))
  8777. (or (encode-coding-char char 'utf-8)
  8778. (error "Unable to percent escape character: %s"
  8779. (char-to-string char))) "")
  8780. (char-to-string char))) text ""))
  8781. (defun org-link-unescape (str)
  8782. "Unhex hexified Unicode strings as returned from the JavaScript function
  8783. encodeURIComponent. E.g. `%C3%B6' is the german o-Umlaut."
  8784. (unless (and (null str) (string= "" str))
  8785. (let ((pos 0) (case-fold-search t) unhexed)
  8786. (while (setq pos (string-match "\\(%[0-9a-f][0-9a-f]\\)+" str pos))
  8787. (setq unhexed (org-link-unescape-compound (match-string 0 str)))
  8788. (setq str (replace-match unhexed t t str))
  8789. (setq pos (+ pos (length unhexed))))))
  8790. str)
  8791. (defun org-link-unescape-compound (hex)
  8792. "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German o-Umlaut.
  8793. Note: this function also decodes single byte encodings like
  8794. `%E1' (a-acute) if not followed by another `%[A-F0-9]{2}' group."
  8795. (save-match-data
  8796. (let* ((bytes (cdr (split-string hex "%")))
  8797. (ret "")
  8798. (eat 0)
  8799. (sum 0))
  8800. (while bytes
  8801. (let* ((val (string-to-number (pop bytes) 16))
  8802. (shift-xor
  8803. (if (= 0 eat)
  8804. (cond
  8805. ((>= val 252) (cons 6 252))
  8806. ((>= val 248) (cons 5 248))
  8807. ((>= val 240) (cons 4 240))
  8808. ((>= val 224) (cons 3 224))
  8809. ((>= val 192) (cons 2 192))
  8810. (t (cons 0 0)))
  8811. (cons 6 128))))
  8812. (if (>= val 192) (setq eat (car shift-xor)))
  8813. (setq val (logxor val (cdr shift-xor)))
  8814. (setq sum (+ (lsh sum (car shift-xor)) val))
  8815. (if (> eat 0) (setq eat (- eat 1)))
  8816. (cond
  8817. ((= 0 eat) ;multi byte
  8818. (setq ret (concat ret (org-char-to-string sum)))
  8819. (setq sum 0))
  8820. ((not bytes) ; single byte(s)
  8821. (setq ret (org-link-unescape-single-byte-sequence hex))))
  8822. )) ;; end (while bytes
  8823. ret )))
  8824. (defun org-link-unescape-single-byte-sequence (hex)
  8825. "Unhexify hex-encoded single byte character sequences."
  8826. (mapconcat (lambda (byte)
  8827. (char-to-string (string-to-number byte 16)))
  8828. (cdr (split-string hex "%")) ""))
  8829. (defun org-xor (a b)
  8830. "Exclusive or."
  8831. (if a (not b) b))
  8832. (defun org-fixup-message-id-for-http (s)
  8833. "Replace special characters in a message id, so it can be used in an http query."
  8834. (when (string-match "%" s)
  8835. (setq s (mapconcat (lambda (c)
  8836. (if (eq c ?%)
  8837. "%25"
  8838. (char-to-string c)))
  8839. s "")))
  8840. (while (string-match "<" s)
  8841. (setq s (replace-match "%3C" t t s)))
  8842. (while (string-match ">" s)
  8843. (setq s (replace-match "%3E" t t s)))
  8844. (while (string-match "@" s)
  8845. (setq s (replace-match "%40" t t s)))
  8846. s)
  8847. (defun org-link-prettify (link)
  8848. "Return a human-readable representation of LINK.
  8849. The car of LINK must be a raw link the cdr of LINK must be either
  8850. a link description or nil."
  8851. (let ((desc (or (cadr link) "<no description>")))
  8852. (concat (format "%-45s" (substring desc 0 (min (length desc) 40)))
  8853. "<" (car link) ">")))
  8854. ;;;###autoload
  8855. (defun org-insert-link-global ()
  8856. "Insert a link like Org-mode does.
  8857. This command can be called in any mode to insert a link in Org-mode syntax."
  8858. (interactive)
  8859. (org-load-modules-maybe)
  8860. (org-run-like-in-org-mode 'org-insert-link))
  8861. (defun org-insert-all-links (&optional keep)
  8862. "Insert all links in `org-stored-links'."
  8863. (interactive "P")
  8864. (let ((links (copy-sequence org-stored-links)) l)
  8865. (while (setq l (if keep (pop links) (pop org-stored-links)))
  8866. (insert "- ")
  8867. (org-insert-link nil (car l) (or (cadr l) "<no description>"))
  8868. (insert "\n"))))
  8869. (defun org-link-fontify-links-to-this-file ()
  8870. "Fontify links to the current file in `org-stored-links'."
  8871. (let ((f (buffer-file-name)) a b)
  8872. (setq a (mapcar (lambda(l)
  8873. (let ((ll (car l)))
  8874. (when (and (string-match "^file:\\(.+\\)::" ll)
  8875. (equal f (expand-file-name (match-string 1 ll))))
  8876. ll)))
  8877. org-stored-links))
  8878. (when (featurep 'org-id)
  8879. (setq b (mapcar (lambda(l)
  8880. (let ((ll (car l)))
  8881. (when (and (string-match "^id:\\(.+\\)$" ll)
  8882. (equal f (expand-file-name
  8883. (or (org-id-find-id-file
  8884. (match-string 1 ll)) ""))))
  8885. ll)))
  8886. org-stored-links)))
  8887. (mapcar (lambda(l)
  8888. (put-text-property 0 (length l) 'face 'font-lock-comment-face l))
  8889. (delq nil (append a b)))))
  8890. (defvar org-link-links-in-this-file nil)
  8891. (defun org-insert-link (&optional complete-file link-location default-description)
  8892. "Insert a link. At the prompt, enter the link.
  8893. Completion can be used to insert any of the link protocol prefixes like
  8894. http or ftp in use.
  8895. The history can be used to select a link previously stored with
  8896. `org-store-link'. When the empty string is entered (i.e. if you just
  8897. press RET at the prompt), the link defaults to the most recently
  8898. stored link. As SPC triggers completion in the minibuffer, you need to
  8899. use M-SPC or C-q SPC to force the insertion of a space character.
  8900. You will also be prompted for a description, and if one is given, it will
  8901. be displayed in the buffer instead of the link.
  8902. If there is already a link at point, this command will allow you to edit link
  8903. and description parts.
  8904. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  8905. be selected using completion. The path to the file will be relative to the
  8906. current directory if the file is in the current directory or a subdirectory.
  8907. Otherwise, the link will be the absolute path as completed in the minibuffer
  8908. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  8909. option `org-link-file-path-type'.
  8910. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  8911. the current directory or below.
  8912. With three \\[universal-argument] prefixes, negate the meaning of
  8913. `org-keep-stored-link-after-insertion'.
  8914. If `org-make-link-description-function' is non-nil, this function will be
  8915. called with the link target, and the result will be the default
  8916. link description.
  8917. If the LINK-LOCATION parameter is non-nil, this value will be
  8918. used as the link location instead of reading one interactively.
  8919. If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
  8920. be used as the default description."
  8921. (interactive "P")
  8922. (let* ((wcf (current-window-configuration))
  8923. (origbuf (current-buffer))
  8924. (region (if (org-region-active-p)
  8925. (buffer-substring (region-beginning) (region-end))))
  8926. (remove (and region (list (region-beginning) (region-end))))
  8927. (desc region)
  8928. tmphist ; byte-compile incorrectly complains about this
  8929. (link link-location)
  8930. (abbrevs org-link-abbrev-alist-local)
  8931. entry file all-prefixes auto-desc)
  8932. (cond
  8933. (link-location) ; specified by arg, just use it.
  8934. ((org-in-regexp org-bracket-link-regexp 1)
  8935. ;; We do have a link at point, and we are going to edit it.
  8936. (setq remove (list (match-beginning 0) (match-end 0)))
  8937. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  8938. (setq link (read-string "Link: "
  8939. (org-link-unescape
  8940. (org-match-string-no-properties 1)))))
  8941. ((or (org-in-regexp org-angle-link-re)
  8942. (org-in-regexp org-plain-link-re))
  8943. ;; Convert to bracket link
  8944. (setq remove (list (match-beginning 0) (match-end 0))
  8945. link (read-string "Link: "
  8946. (org-remove-angle-brackets (match-string 0)))))
  8947. ((member complete-file '((4) (16)))
  8948. ;; Completing read for file names.
  8949. (setq link (org-file-complete-link complete-file)))
  8950. (t
  8951. ;; Read link, with completion for stored links.
  8952. (org-link-fontify-links-to-this-file)
  8953. (org-switch-to-buffer-other-window "*Org Links*")
  8954. (with-current-buffer "*Org Links*"
  8955. (erase-buffer)
  8956. (insert "Insert a link.
  8957. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  8958. (when org-stored-links
  8959. (insert "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  8960. (insert (mapconcat 'org-link-prettify
  8961. (reverse org-stored-links) "\n")))
  8962. (goto-char (point-min)))
  8963. (let ((cw (selected-window)))
  8964. (select-window (get-buffer-window "*Org Links*" 'visible))
  8965. (with-current-buffer "*Org Links*" (setq truncate-lines t))
  8966. (unless (pos-visible-in-window-p (point-max))
  8967. (org-fit-window-to-buffer))
  8968. (and (window-live-p cw) (select-window cw)))
  8969. ;; Fake a link history, containing the stored links.
  8970. (setq tmphist (append (mapcar 'car org-stored-links)
  8971. org-insert-link-history))
  8972. (setq all-prefixes (append (mapcar 'car abbrevs)
  8973. (mapcar 'car org-link-abbrev-alist)
  8974. org-link-types))
  8975. (unwind-protect
  8976. (progn
  8977. (setq link
  8978. (org-completing-read
  8979. "Link: "
  8980. (append
  8981. (mapcar (lambda (x) (concat x ":"))
  8982. all-prefixes)
  8983. (mapcar 'car org-stored-links))
  8984. nil nil nil
  8985. 'tmphist
  8986. (caar org-stored-links)))
  8987. (if (not (string-match "\\S-" link))
  8988. (error "No link selected"))
  8989. (mapc (lambda(l)
  8990. (when (equal link (cadr l)) (setq link (car l) auto-desc t)))
  8991. org-stored-links)
  8992. (if (or (member link all-prefixes)
  8993. (and (equal ":" (substring link -1))
  8994. (member (substring link 0 -1) all-prefixes)
  8995. (setq link (substring link 0 -1))))
  8996. (setq link (with-current-buffer origbuf
  8997. (org-link-try-special-completion link)))))
  8998. (set-window-configuration wcf)
  8999. (kill-buffer "*Org Links*"))
  9000. (setq entry (assoc link org-stored-links))
  9001. (or entry (push link org-insert-link-history))
  9002. (setq desc (or desc (nth 1 entry)))))
  9003. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  9004. (not org-keep-stored-link-after-insertion))
  9005. (setq org-stored-links (delq (assoc link org-stored-links)
  9006. org-stored-links)))
  9007. (if (string-match org-plain-link-re link)
  9008. ;; URL-like link, normalize the use of angular brackets.
  9009. (setq link (org-remove-angle-brackets link)))
  9010. ;; Check if we are linking to the current file with a search
  9011. ;; option If yes, simplify the link by using only the search
  9012. ;; option.
  9013. (when (and buffer-file-name
  9014. (string-match "^file:\\(.+?\\)::\\(.+\\)" link))
  9015. (let* ((path (match-string 1 link))
  9016. (case-fold-search nil)
  9017. (search (match-string 2 link)))
  9018. (save-match-data
  9019. (if (equal (file-truename buffer-file-name) (file-truename path))
  9020. ;; We are linking to this same file, with a search option
  9021. (setq link search)))))
  9022. ;; Check if we can/should use a relative path. If yes, simplify the link
  9023. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  9024. (let* ((type (match-string 1 link))
  9025. (path (match-string 2 link))
  9026. (origpath path)
  9027. (case-fold-search nil))
  9028. (cond
  9029. ((or (eq org-link-file-path-type 'absolute)
  9030. (equal complete-file '(16)))
  9031. (setq path (abbreviate-file-name (expand-file-name path))))
  9032. ((eq org-link-file-path-type 'noabbrev)
  9033. (setq path (expand-file-name path)))
  9034. ((eq org-link-file-path-type 'relative)
  9035. (setq path (file-relative-name path)))
  9036. (t
  9037. (save-match-data
  9038. (if (string-match (concat "^" (regexp-quote
  9039. (expand-file-name
  9040. (file-name-as-directory
  9041. default-directory))))
  9042. (expand-file-name path))
  9043. ;; We are linking a file with relative path name.
  9044. (setq path (substring (expand-file-name path)
  9045. (match-end 0)))
  9046. (setq path (abbreviate-file-name (expand-file-name path)))))))
  9047. (setq link (concat type path))
  9048. (if (equal desc origpath)
  9049. (setq desc path))))
  9050. (if org-make-link-description-function
  9051. (setq desc
  9052. (or (condition-case nil
  9053. (funcall org-make-link-description-function link desc)
  9054. (error (progn (message "Can't get link description from `%s'"
  9055. (symbol-name org-make-link-description-function))
  9056. (sit-for 2) nil)))
  9057. (read-string "Description: " default-description)))
  9058. (if default-description (setq desc default-description)
  9059. (setq desc (or (and auto-desc desc)
  9060. (read-string "Description: " desc)))))
  9061. (unless (string-match "\\S-" desc) (setq desc nil))
  9062. (if remove (apply 'delete-region remove))
  9063. (insert (org-make-link-string link desc))))
  9064. (defun org-link-try-special-completion (type)
  9065. "If there is completion support for link type TYPE, offer it."
  9066. (let ((fun (intern (concat "org-" type "-complete-link"))))
  9067. (if (functionp fun)
  9068. (funcall fun)
  9069. (read-string "Link (no completion support): " (concat type ":")))))
  9070. (defun org-file-complete-link (&optional arg)
  9071. "Create a file link using completion."
  9072. (let (file link)
  9073. (setq file (org-iread-file-name "File: "))
  9074. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  9075. (pwd1 (file-name-as-directory (abbreviate-file-name
  9076. (expand-file-name ".")))))
  9077. (cond
  9078. ((equal arg '(16))
  9079. (setq link (concat
  9080. "file:"
  9081. (abbreviate-file-name (expand-file-name file)))))
  9082. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  9083. (setq link (concat "file:" (match-string 1 file))))
  9084. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  9085. (expand-file-name file))
  9086. (setq link (concat
  9087. "file:" (match-string 1 (expand-file-name file)))))
  9088. (t (setq link (concat "file:" file)))))
  9089. link))
  9090. (defun org-iread-file-name (&rest args)
  9091. "Read-file-name using `ido-mode' speedup if available.
  9092. ARGS are arguments that may be passed to `ido-read-file-name' or `read-file-name'.
  9093. See `read-file-name' for a description of parameters."
  9094. (org-without-partial-completion
  9095. (if (and org-completion-use-ido
  9096. (fboundp 'ido-read-file-name)
  9097. (boundp 'ido-mode) ido-mode
  9098. (listp (second args)))
  9099. (let ((ido-enter-matching-directory nil))
  9100. (apply 'ido-read-file-name args))
  9101. (apply 'read-file-name args))))
  9102. (defun org-completing-read (&rest args)
  9103. "Completing-read with SPACE being a normal character."
  9104. (let ((enable-recursive-minibuffers t)
  9105. (minibuffer-local-completion-map
  9106. (copy-keymap minibuffer-local-completion-map)))
  9107. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  9108. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  9109. (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
  9110. (apply 'org-icompleting-read args)))
  9111. (defun org-completing-read-no-i (&rest args)
  9112. (let (org-completion-use-ido org-completion-use-iswitchb)
  9113. (apply 'org-completing-read args)))
  9114. (defun org-iswitchb-completing-read (prompt choices &rest args)
  9115. "Use iswitch as a completing-read replacement to choose from choices.
  9116. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  9117. from."
  9118. (let* ((iswitchb-use-virtual-buffers nil)
  9119. (iswitchb-make-buflist-hook
  9120. (lambda ()
  9121. (setq iswitchb-temp-buflist choices))))
  9122. (iswitchb-read-buffer prompt)))
  9123. (defun org-icompleting-read (&rest args)
  9124. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  9125. (org-without-partial-completion
  9126. (if (and org-completion-use-ido
  9127. (fboundp 'ido-completing-read)
  9128. (boundp 'ido-mode) ido-mode
  9129. (listp (second args)))
  9130. (let ((ido-enter-matching-directory nil))
  9131. (apply 'ido-completing-read (concat (car args))
  9132. (if (consp (car (nth 1 args)))
  9133. (mapcar 'car (nth 1 args))
  9134. (nth 1 args))
  9135. (cddr args)))
  9136. (if (and org-completion-use-iswitchb
  9137. (boundp 'iswitchb-mode) iswitchb-mode
  9138. (listp (second args)))
  9139. (apply 'org-iswitchb-completing-read (concat (car args))
  9140. (if (consp (car (nth 1 args)))
  9141. (mapcar 'car (nth 1 args))
  9142. (nth 1 args))
  9143. (cddr args))
  9144. (apply 'completing-read args)))))
  9145. (defun org-extract-attributes (s)
  9146. "Extract the attributes cookie from a string and set as text property."
  9147. (let (a attr (start 0) key value)
  9148. (save-match-data
  9149. (when (string-match "{{\\([^}]+\\)}}$" s)
  9150. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  9151. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  9152. (setq key (match-string 1 a) value (match-string 2 a)
  9153. start (match-end 0)
  9154. attr (plist-put attr (intern key) value))))
  9155. (org-add-props s nil 'org-attr attr))
  9156. s))
  9157. ;;; Opening/following a link
  9158. (defvar org-link-search-failed nil)
  9159. (defvar org-open-link-functions nil
  9160. "Hook for functions finding a plain text link.
  9161. These functions must take a single argument, the link content.
  9162. They will be called for links that look like [[link text][description]]
  9163. when LINK TEXT does not have a protocol like \"http:\" and does not look
  9164. like a filename (e.g. \"./blue.png\").
  9165. These functions will be called *before* Org attempts to resolve the
  9166. link by doing text searches in the current buffer - so if you want a
  9167. link \"[[target]]\" to still find \"<<target>>\", your function should
  9168. handle this as a special case.
  9169. When the function does handle the link, it must return a non-nil value.
  9170. If it decides that it is not responsible for this link, it must return
  9171. nil to indicate that that Org-mode can continue with other options
  9172. like exact and fuzzy text search.")
  9173. (defun org-next-link (&optional search-backward)
  9174. "Move forward to the next link.
  9175. If the link is in hidden text, expose it."
  9176. (interactive "P")
  9177. (when (and org-link-search-failed (eq this-command last-command))
  9178. (goto-char (point-min))
  9179. (message "Link search wrapped back to beginning of buffer"))
  9180. (setq org-link-search-failed nil)
  9181. (let* ((pos (point))
  9182. (ct (org-context))
  9183. (a (assoc :link ct))
  9184. (srch-fun (if search-backward 're-search-backward 're-search-forward)))
  9185. (cond (a (goto-char (nth (if search-backward 1 2) a)))
  9186. ((looking-at org-any-link-re)
  9187. ;; Don't stay stuck at link without an org-link face
  9188. (forward-char (if search-backward -1 1))))
  9189. (if (funcall srch-fun org-any-link-re nil t)
  9190. (progn
  9191. (goto-char (match-beginning 0))
  9192. (if (outline-invisible-p) (org-show-context)))
  9193. (goto-char pos)
  9194. (setq org-link-search-failed t)
  9195. (message "No further link found"))))
  9196. (defun org-previous-link ()
  9197. "Move backward to the previous link.
  9198. If the link is in hidden text, expose it."
  9199. (interactive)
  9200. (funcall 'org-next-link t))
  9201. (defun org-translate-link (s)
  9202. "Translate a link string if a translation function has been defined."
  9203. (if (and org-link-translation-function
  9204. (fboundp org-link-translation-function)
  9205. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  9206. (progn
  9207. (setq s (funcall org-link-translation-function
  9208. (match-string 1 s) (match-string 2 s)))
  9209. (concat (car s) ":" (cdr s)))
  9210. s))
  9211. (defun org-translate-link-from-planner (type path)
  9212. "Translate a link from Emacs Planner syntax so that Org can follow it.
  9213. This is still an experimental function, your mileage may vary."
  9214. (cond
  9215. ((member type '("http" "https" "news" "ftp"))
  9216. ;; standard Internet links are the same.
  9217. nil)
  9218. ((and (equal type "irc") (string-match "^//" path))
  9219. ;; Planner has two / at the beginning of an irc link, we have 1.
  9220. ;; We should have zero, actually....
  9221. (setq path (substring path 1)))
  9222. ((and (equal type "lisp") (string-match "^/" path))
  9223. ;; Planner has a slash, we do not.
  9224. (setq type "elisp" path (substring path 1)))
  9225. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  9226. ;; A typical message link. Planner has the id after the final slash,
  9227. ;; we separate it with a hash mark
  9228. (setq path (concat (match-string 1 path) "#"
  9229. (org-remove-angle-brackets (match-string 2 path)))))
  9230. )
  9231. (cons type path))
  9232. (defun org-find-file-at-mouse (ev)
  9233. "Open file link or URL at mouse."
  9234. (interactive "e")
  9235. (mouse-set-point ev)
  9236. (org-open-at-point 'in-emacs))
  9237. (defun org-open-at-mouse (ev)
  9238. "Open file link or URL at mouse.
  9239. See the docstring of `org-open-file' for details."
  9240. (interactive "e")
  9241. (mouse-set-point ev)
  9242. (if (eq major-mode 'org-agenda-mode)
  9243. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  9244. (org-open-at-point))
  9245. (defvar org-window-config-before-follow-link nil
  9246. "The window configuration before following a link.
  9247. This is saved in case the need arises to restore it.")
  9248. (defvar org-open-link-marker (make-marker)
  9249. "Marker pointing to the location where `org-open-at-point; was called.")
  9250. ;;;###autoload
  9251. (defun org-open-at-point-global ()
  9252. "Follow a link like Org-mode does.
  9253. This command can be called in any mode to follow a link that has
  9254. Org-mode syntax."
  9255. (interactive)
  9256. (org-run-like-in-org-mode 'org-open-at-point))
  9257. ;;;###autoload
  9258. (defun org-open-link-from-string (s &optional arg reference-buffer)
  9259. "Open a link in the string S, as if it was in Org-mode."
  9260. (interactive "sLink: \nP")
  9261. (let ((reference-buffer (or reference-buffer (current-buffer))))
  9262. (with-temp-buffer
  9263. (let ((org-inhibit-startup (not reference-buffer)))
  9264. (org-mode)
  9265. (insert s)
  9266. (goto-char (point-min))
  9267. (when reference-buffer
  9268. (setq org-link-abbrev-alist-local
  9269. (with-current-buffer reference-buffer
  9270. org-link-abbrev-alist-local)))
  9271. (org-open-at-point arg reference-buffer)))))
  9272. (defvar org-open-at-point-functions nil
  9273. "Hook that is run when following a link at point.
  9274. Functions in this hook must return t if they identify and follow
  9275. a link at point. If they don't find anything interesting at point,
  9276. they must return nil.")
  9277. (defvar clean-buffer-list-kill-buffer-names) ; Defined in midnight.el
  9278. (defun org-open-at-point (&optional arg reference-buffer)
  9279. "Open link at or after point.
  9280. If there is no link at point, this function will search forward up to
  9281. the end of the current line.
  9282. Normally, files will be opened by an appropriate application. If the
  9283. optional prefix argument ARG is non-nil, Emacs will visit the file.
  9284. With a double prefix argument, try to open outside of Emacs, in the
  9285. application the system uses for this file type."
  9286. (interactive "P")
  9287. ;; if in a code block, then open the block's results
  9288. (unless (call-interactively #'org-babel-open-src-block-result)
  9289. (org-load-modules-maybe)
  9290. (move-marker org-open-link-marker (point))
  9291. (setq org-window-config-before-follow-link (current-window-configuration))
  9292. (org-remove-occur-highlights nil nil t)
  9293. (cond
  9294. ((and (org-at-heading-p)
  9295. (not (org-at-timestamp-p t))
  9296. (not (org-in-regexp
  9297. (concat org-plain-link-re "\\|"
  9298. org-bracket-link-regexp "\\|"
  9299. org-angle-link-re "\\|"
  9300. "[ \t]:[^ \t\n]+:[ \t]*$")))
  9301. (not (get-text-property (point) 'org-linked-text)))
  9302. (or (let* ((lkall (org-offer-links-in-entry (current-buffer) (point) arg))
  9303. (lk0 (car lkall))
  9304. (lk (if (stringp lk0) (list lk0) lk0))
  9305. (lkend (cdr lkall)))
  9306. (mapcar (lambda(l)
  9307. (search-forward l nil lkend)
  9308. (goto-char (match-beginning 0))
  9309. (org-open-at-point))
  9310. lk))
  9311. (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
  9312. ((run-hook-with-args-until-success 'org-open-at-point-functions))
  9313. ((and (org-at-timestamp-p t)
  9314. (not (org-in-regexp org-bracket-link-regexp)))
  9315. (org-follow-timestamp-link))
  9316. ((and (or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  9317. (not (org-in-regexp org-any-link-re)))
  9318. (org-footnote-action))
  9319. (t
  9320. (let (type path link line search (pos (point)))
  9321. (catch 'match
  9322. (save-excursion
  9323. (skip-chars-forward "^]\n\r")
  9324. (when (org-in-regexp org-bracket-link-regexp 1)
  9325. (setq link (org-extract-attributes
  9326. (org-link-unescape (org-match-string-no-properties 1))))
  9327. (while (string-match " *\n *" link)
  9328. (setq link (replace-match " " t t link)))
  9329. (setq link (org-link-expand-abbrev link))
  9330. (cond
  9331. ((or (file-name-absolute-p link)
  9332. (string-match "^\\.\\.?/" link))
  9333. (setq type "file" path link))
  9334. ((string-match org-link-re-with-space3 link)
  9335. (setq type (match-string 1 link) path (match-string 2 link)))
  9336. ((string-match "^help:+\\(.+\\)" link)
  9337. (setq type "help" path (match-string 1 link)))
  9338. (t (setq type "thisfile" path link)))
  9339. (throw 'match t)))
  9340. (when (get-text-property (point) 'org-linked-text)
  9341. (setq type "thisfile"
  9342. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  9343. (1+ (point)) (point))
  9344. path (buffer-substring
  9345. (or (previous-single-property-change pos 'org-linked-text)
  9346. (point-min))
  9347. (or (next-single-property-change pos 'org-linked-text)
  9348. (point-max))))
  9349. (throw 'match t))
  9350. (save-excursion
  9351. (when (or (org-in-regexp org-angle-link-re)
  9352. (let ((match (org-in-regexp org-plain-link-re)))
  9353. ;; Check a plain link is not within a bracket link
  9354. (and match
  9355. (save-excursion
  9356. (progn
  9357. (goto-char (car match))
  9358. (not (org-in-regexp org-bracket-link-regexp))))))
  9359. (let ((line_ending (save-excursion (end-of-line) (point))))
  9360. ;; We are in a line before a plain or bracket link
  9361. (or (re-search-forward org-plain-link-re line_ending t)
  9362. (re-search-forward org-bracket-link-regexp line_ending t))))
  9363. (setq type (match-string 1)
  9364. path (org-link-unescape (match-string 2)))
  9365. (throw 'match t)))
  9366. (save-excursion
  9367. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@#%:]+\\):[ \t]*$"))
  9368. (setq type "tags"
  9369. path (match-string 1))
  9370. (while (string-match ":" path)
  9371. (setq path (replace-match "+" t t path)))
  9372. (throw 'match t)))
  9373. (when (org-in-regexp "<\\([^><\n]+\\)>")
  9374. (setq type "tree-match"
  9375. path (match-string 1))
  9376. (throw 'match t)))
  9377. (unless path
  9378. (user-error "No link found"))
  9379. ;; switch back to reference buffer
  9380. ;; needed when if called in a temporary buffer through
  9381. ;; org-open-link-from-string
  9382. (with-current-buffer (or reference-buffer (current-buffer))
  9383. ;; Remove any trailing spaces in path
  9384. (if (string-match " +\\'" path)
  9385. (setq path (replace-match "" t t path)))
  9386. (if (and org-link-translation-function
  9387. (fboundp org-link-translation-function))
  9388. ;; Check if we need to translate the link
  9389. (let ((tmp (funcall org-link-translation-function type path)))
  9390. (setq type (car tmp) path (cdr tmp))))
  9391. (cond
  9392. ((assoc type org-link-protocols)
  9393. (funcall (nth 1 (assoc type org-link-protocols)) path))
  9394. ((equal type "help")
  9395. (let ((f-or-v (intern path)))
  9396. (cond ((fboundp f-or-v)
  9397. (describe-function f-or-v))
  9398. ((boundp f-or-v)
  9399. (describe-variable f-or-v))
  9400. (t (error "Not a known function or variable")))))
  9401. ((equal type "mailto")
  9402. (let ((cmd (car org-link-mailto-program))
  9403. (args (cdr org-link-mailto-program)) args1
  9404. (address path) (subject "") a)
  9405. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  9406. (setq address (match-string 1 path)
  9407. subject (org-link-escape (match-string 2 path))))
  9408. (while args
  9409. (cond
  9410. ((not (stringp (car args))) (push (pop args) args1))
  9411. (t (setq a (pop args))
  9412. (if (string-match "%a" a)
  9413. (setq a (replace-match address t t a)))
  9414. (if (string-match "%s" a)
  9415. (setq a (replace-match subject t t a)))
  9416. (push a args1))))
  9417. (apply cmd (nreverse args1))))
  9418. ((member type '("http" "https" "ftp" "news"))
  9419. (browse-url (concat type ":" (if (org-string-match-p "[[:nonascii:] ]" path)
  9420. (org-link-escape
  9421. path org-link-escape-chars-browser)
  9422. path))))
  9423. ((string= type "doi")
  9424. (browse-url (concat org-doi-server-url (if (org-string-match-p "[[:nonascii:] ]" path)
  9425. (org-link-escape
  9426. path org-link-escape-chars-browser)
  9427. path))))
  9428. ((member type '("message"))
  9429. (browse-url (concat type ":" path)))
  9430. ((string= type "tags")
  9431. (org-tags-view arg path))
  9432. ((string= type "tree-match")
  9433. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  9434. ((string= type "file")
  9435. (if (string-match "::\\([0-9]+\\)\\'" path)
  9436. (setq line (string-to-number (match-string 1 path))
  9437. path (substring path 0 (match-beginning 0)))
  9438. (if (string-match "::\\(.+\\)\\'" path)
  9439. (setq search (match-string 1 path)
  9440. path (substring path 0 (match-beginning 0)))))
  9441. (if (string-match "[*?{]" (file-name-nondirectory path))
  9442. (dired path)
  9443. (org-open-file path arg line search)))
  9444. ((string= type "shell")
  9445. (let ((buf (generate-new-buffer "*Org Shell Output"))
  9446. (cmd path))
  9447. (if (or (and (not (string= org-confirm-shell-link-not-regexp ""))
  9448. (string-match org-confirm-shell-link-not-regexp cmd))
  9449. (not org-confirm-shell-link-function)
  9450. (funcall org-confirm-shell-link-function
  9451. (format "Execute \"%s\" in shell? "
  9452. (org-add-props cmd nil
  9453. 'face 'org-warning))))
  9454. (progn
  9455. (message "Executing %s" cmd)
  9456. (shell-command cmd buf)
  9457. (if (featurep 'midnight)
  9458. (setq clean-buffer-list-kill-buffer-names
  9459. (cons buf clean-buffer-list-kill-buffer-names))))
  9460. (error "Abort"))))
  9461. ((string= type "elisp")
  9462. (let ((cmd path))
  9463. (if (or (and (not (string= org-confirm-elisp-link-not-regexp ""))
  9464. (string-match org-confirm-elisp-link-not-regexp cmd))
  9465. (not org-confirm-elisp-link-function)
  9466. (funcall org-confirm-elisp-link-function
  9467. (format "Execute \"%s\" as elisp? "
  9468. (org-add-props cmd nil
  9469. 'face 'org-warning))))
  9470. (message "%s => %s" cmd
  9471. (if (equal (string-to-char cmd) ?\()
  9472. (eval (read cmd))
  9473. (call-interactively (read cmd))))
  9474. (error "Abort"))))
  9475. ((and (string= type "thisfile")
  9476. (run-hook-with-args-until-success
  9477. 'org-open-link-functions path)))
  9478. ((string= type "thisfile")
  9479. (if arg
  9480. (switch-to-buffer-other-window
  9481. (org-get-buffer-for-internal-link (current-buffer)))
  9482. (org-mark-ring-push))
  9483. (let ((cmd `(org-link-search
  9484. ,path
  9485. ,(cond ((equal arg '(4)) ''occur)
  9486. ((equal arg '(16)) ''org-occur))
  9487. ,pos)))
  9488. (condition-case nil (let ((org-link-search-inhibit-query t))
  9489. (eval cmd))
  9490. (error (progn (widen) (eval cmd))))))
  9491. (t (browse-url-at-point)))))))
  9492. (move-marker org-open-link-marker nil)
  9493. (run-hook-with-args 'org-follow-link-hook)))
  9494. (defun org-offer-links-in-entry (buffer marker &optional nth zero)
  9495. "Offer links in the current entry and return the selected link.
  9496. If there is only one link, return it.
  9497. If NTH is an integer, return the NTH link found.
  9498. If ZERO is a string, check also this string for a link, and if
  9499. there is one, return it."
  9500. (with-current-buffer buffer
  9501. (save-excursion
  9502. (save-restriction
  9503. (widen)
  9504. (goto-char marker)
  9505. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  9506. "\\(" org-angle-link-re "\\)\\|"
  9507. "\\(" org-plain-link-re "\\)"))
  9508. (cnt ?0)
  9509. (in-emacs (if (integerp nth) nil nth))
  9510. have-zero end links link c)
  9511. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  9512. (push (match-string 0 zero) links)
  9513. (setq cnt (1- cnt) have-zero t))
  9514. (save-excursion
  9515. (org-back-to-heading t)
  9516. (setq end (save-excursion (outline-next-heading) (point)))
  9517. (while (re-search-forward re end t)
  9518. (push (match-string 0) links))
  9519. (setq links (org-uniquify (reverse links))))
  9520. (cond
  9521. ((null links)
  9522. (message "No links"))
  9523. ((equal (length links) 1)
  9524. (setq link (car links)))
  9525. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  9526. (setq link (nth (if have-zero nth (1- nth)) links)))
  9527. (t ; we have to select a link
  9528. (save-excursion
  9529. (save-window-excursion
  9530. (delete-other-windows)
  9531. (with-output-to-temp-buffer "*Select Link*"
  9532. (mapc (lambda (l)
  9533. (if (not (string-match org-bracket-link-regexp l))
  9534. (princ (format "[%c] %s\n" (incf cnt)
  9535. (org-remove-angle-brackets l)))
  9536. (if (match-end 3)
  9537. (princ (format "[%c] %s (%s)\n" (incf cnt)
  9538. (match-string 3 l) (match-string 1 l)))
  9539. (princ (format "[%c] %s\n" (incf cnt)
  9540. (match-string 1 l))))))
  9541. links))
  9542. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  9543. (message "Select link to open, RET to open all:")
  9544. (setq c (read-char-exclusive))
  9545. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  9546. (when (equal c ?q) (error "Abort"))
  9547. (if (equal c ?\C-m)
  9548. (setq link links)
  9549. (setq nth (- c ?0))
  9550. (if have-zero (setq nth (1+ nth)))
  9551. (unless (and (integerp nth) (>= (length links) nth))
  9552. (error "Invalid link selection"))
  9553. (setq link (nth (1- nth) links)))))
  9554. (cons link end))))))
  9555. ;; Add special file links that specify the way of opening
  9556. (org-add-link-type "file+sys" 'org-open-file-with-system)
  9557. (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
  9558. (defun org-open-file-with-system (path)
  9559. "Open file at PATH using the system way of opening it."
  9560. (org-open-file path 'system))
  9561. (defun org-open-file-with-emacs (path)
  9562. "Open file at PATH in Emacs."
  9563. (org-open-file path 'emacs))
  9564. ;;; File search
  9565. (defvar org-create-file-search-functions nil
  9566. "List of functions to construct the right search string for a file link.
  9567. These functions are called in turn with point at the location to
  9568. which the link should point.
  9569. A function in the hook should first test if it would like to
  9570. handle this file type, for example by checking the `major-mode'
  9571. or the file extension. If it decides not to handle this file, it
  9572. should just return nil to give other functions a chance. If it
  9573. does handle the file, it must return the search string to be used
  9574. when following the link. The search string will be part of the
  9575. file link, given after a double colon, and `org-open-at-point'
  9576. will automatically search for it. If special measures must be
  9577. taken to make the search successful, another function should be
  9578. added to the companion hook `org-execute-file-search-functions',
  9579. which see.
  9580. A function in this hook may also use `setq' to set the variable
  9581. `description' to provide a suggestion for the descriptive text to
  9582. be used for this link when it gets inserted into an Org-mode
  9583. buffer with \\[org-insert-link].")
  9584. (defvar org-execute-file-search-functions nil
  9585. "List of functions to execute a file search triggered by a link.
  9586. Functions added to this hook must accept a single argument, the
  9587. search string that was part of the file link, the part after the
  9588. double colon. The function must first check if it would like to
  9589. handle this search, for example by checking the `major-mode' or
  9590. the file extension. If it decides not to handle this search, it
  9591. should just return nil to give other functions a chance. If it
  9592. does handle the search, it must return a non-nil value to keep
  9593. other functions from trying.
  9594. Each function can access the current prefix argument through the
  9595. variable `current-prefix-arg'. Note that a single prefix is used
  9596. to force opening a link in Emacs, so it may be good to only use a
  9597. numeric or double prefix to guide the search function.
  9598. In case this is needed, a function in this hook can also restore
  9599. the window configuration before `org-open-at-point' was called using:
  9600. (set-window-configuration org-window-config-before-follow-link)")
  9601. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  9602. (defun org-link-search (s &optional type avoid-pos stealth)
  9603. "Search for a link search option.
  9604. If S is surrounded by forward slashes, it is interpreted as a
  9605. regular expression. In org-mode files, this will create an `org-occur'
  9606. sparse tree. In ordinary files, `occur' will be used to list matches.
  9607. If the current buffer is in `dired-mode', grep will be used to search
  9608. in all files. If AVOID-POS is given, ignore matches near that position.
  9609. When optional argument STEALTH is non-nil, do not modify
  9610. visibility around point, thus ignoring
  9611. `org-show-hierarchy-above', `org-show-following-heading' and
  9612. `org-show-siblings' variables."
  9613. (let ((case-fold-search t)
  9614. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  9615. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  9616. (append '(("") (" ") ("\t") ("\n"))
  9617. org-emphasis-alist)
  9618. "\\|") "\\)"))
  9619. (pos (point))
  9620. (pre nil) (post nil)
  9621. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  9622. (cond
  9623. ;; First check if there are any special search functions
  9624. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  9625. ;; Now try the builtin stuff
  9626. ((and (equal (string-to-char s0) ?#)
  9627. (> (length s0) 1)
  9628. (save-excursion
  9629. (goto-char (point-min))
  9630. (and
  9631. (re-search-forward
  9632. (concat "^[ \t]*:CUSTOM_ID:[ \t]+"
  9633. (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  9634. (setq type 'dedicated
  9635. pos (match-beginning 0))))
  9636. ;; There is an exact target for this
  9637. (goto-char pos)
  9638. (org-back-to-heading t)))
  9639. ((save-excursion
  9640. (goto-char (point-min))
  9641. (and
  9642. (re-search-forward
  9643. (concat "<<" (regexp-quote s0) ">>") nil t)
  9644. (setq type 'dedicated
  9645. pos (match-beginning 0))))
  9646. ;; There is an exact target for this
  9647. (goto-char pos))
  9648. ((save-excursion
  9649. (goto-char (point-min))
  9650. (and
  9651. (re-search-forward
  9652. (format "^[ \t]*#\\+NAME: %s" (regexp-quote s0)) nil t)
  9653. (setq type 'dedicated pos (match-beginning 0))))
  9654. ;; Found an element with a matching #+name affiliated keyword.
  9655. (goto-char pos))
  9656. ((and (string-match "^(\\(.*\\))$" s0)
  9657. (save-excursion
  9658. (goto-char (point-min))
  9659. (and
  9660. (re-search-forward
  9661. (concat "[^[]" (regexp-quote
  9662. (format org-coderef-label-format
  9663. (match-string 1 s0))))
  9664. nil t)
  9665. (setq type 'dedicated
  9666. pos (1+ (match-beginning 0))))))
  9667. ;; There is a coderef target for this
  9668. (goto-char pos))
  9669. ((string-match "^/\\(.*\\)/$" s)
  9670. ;; A regular expression
  9671. (cond
  9672. ((derived-mode-p 'org-mode)
  9673. (org-occur (match-string 1 s)))
  9674. (t (org-do-occur (match-string 1 s)))))
  9675. ((and (derived-mode-p 'org-mode) org-link-search-must-match-exact-headline)
  9676. (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
  9677. (goto-char (point-min))
  9678. (cond
  9679. ((let (case-fold-search)
  9680. (re-search-forward (format org-complex-heading-regexp-format
  9681. (regexp-quote s))
  9682. nil t))
  9683. ;; OK, found a match
  9684. (setq type 'dedicated)
  9685. (goto-char (match-beginning 0)))
  9686. ((and (not org-link-search-inhibit-query)
  9687. (eq org-link-search-must-match-exact-headline 'query-to-create)
  9688. (y-or-n-p "No match - create this as a new heading? "))
  9689. (goto-char (point-max))
  9690. (or (bolp) (newline))
  9691. (insert "* " s "\n")
  9692. (beginning-of-line 0))
  9693. (t
  9694. (goto-char pos)
  9695. (error "No match"))))
  9696. (t
  9697. ;; A normal search string
  9698. (when (equal (string-to-char s) ?*)
  9699. ;; Anchor on headlines, post may include tags.
  9700. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  9701. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
  9702. s (substring s 1)))
  9703. (remove-text-properties
  9704. 0 (length s)
  9705. '(face nil mouse-face nil keymap nil fontified nil) s)
  9706. ;; Make a series of regular expressions to find a match
  9707. (setq words (org-split-string s "[ \n\r\t]+")
  9708. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  9709. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  9710. "\\)" markers)
  9711. re2a_ (concat "\\(" (mapconcat 'downcase words
  9712. "[ \t\r\n]+") "\\)[ \t\r\n]")
  9713. re2a (concat "[ \t\r\n]" re2a_)
  9714. re4_ (concat "\\(" (mapconcat 'downcase words
  9715. "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  9716. re4 (concat "[^a-zA-Z_]" re4_)
  9717. re1 (concat pre re2 post)
  9718. re3 (concat pre (if pre re4_ re4) post)
  9719. re5 (concat pre ".*" re4)
  9720. re2 (concat pre re2)
  9721. re2a (concat pre (if pre re2a_ re2a))
  9722. re4 (concat pre (if pre re4_ re4))
  9723. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  9724. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  9725. re5 "\\)"
  9726. ))
  9727. (cond
  9728. ((eq type 'org-occur) (org-occur reall))
  9729. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  9730. (t (goto-char (point-min))
  9731. (setq type 'fuzzy)
  9732. (if (or (and (org-search-not-self 1 re0 nil t)
  9733. (setq type 'dedicated))
  9734. (org-search-not-self 1 re1 nil t)
  9735. (org-search-not-self 1 re2 nil t)
  9736. (org-search-not-self 1 re2a nil t)
  9737. (org-search-not-self 1 re3 nil t)
  9738. (org-search-not-self 1 re4 nil t)
  9739. (org-search-not-self 1 re5 nil t))
  9740. (goto-char (match-beginning 1))
  9741. (goto-char pos)
  9742. (error "No match"))))))
  9743. (and (derived-mode-p 'org-mode)
  9744. (not stealth)
  9745. (org-show-context 'link-search))
  9746. type))
  9747. (defun org-search-not-self (group &rest args)
  9748. "Execute `re-search-forward', but only accept matches that do not
  9749. enclose the position of `org-open-link-marker'."
  9750. (let ((m org-open-link-marker))
  9751. (catch 'exit
  9752. (while (apply 're-search-forward args)
  9753. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  9754. (goto-char (match-end group))
  9755. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  9756. (> (match-beginning 0) (marker-position m))
  9757. (< (match-end 0) (marker-position m)))
  9758. (save-match-data
  9759. (or (not (org-in-regexp
  9760. org-bracket-link-analytic-regexp 1))
  9761. (not (match-end 4)) ; no description
  9762. (and (<= (match-beginning 4) (point))
  9763. (>= (match-end 4) (point))))))
  9764. (throw 'exit (point))))))))
  9765. (defun org-get-buffer-for-internal-link (buffer)
  9766. "Return a buffer to be used for displaying the link target of internal links."
  9767. (cond
  9768. ((not org-display-internal-link-with-indirect-buffer)
  9769. buffer)
  9770. ((string-match "(Clone)$" (buffer-name buffer))
  9771. (message "Buffer is already a clone, not making another one")
  9772. ;; we also do not modify visibility in this case
  9773. buffer)
  9774. (t ; make a new indirect buffer for displaying the link
  9775. (let* ((bn (buffer-name buffer))
  9776. (ibn (concat bn "(Clone)"))
  9777. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  9778. (with-current-buffer ib (org-overview))
  9779. ib))))
  9780. (defun org-do-occur (regexp &optional cleanup)
  9781. "Call the Emacs command `occur'.
  9782. If CLEANUP is non-nil, remove the printout of the regular expression
  9783. in the *Occur* buffer. This is useful if the regex is long and not useful
  9784. to read."
  9785. (occur regexp)
  9786. (when cleanup
  9787. (let ((cwin (selected-window)) win beg end)
  9788. (when (setq win (get-buffer-window "*Occur*"))
  9789. (select-window win))
  9790. (goto-char (point-min))
  9791. (when (re-search-forward "match[a-z]+" nil t)
  9792. (setq beg (match-end 0))
  9793. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  9794. (setq end (1- (match-beginning 0)))))
  9795. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  9796. (goto-char (point-min))
  9797. (select-window cwin))))
  9798. ;;; The mark ring for links jumps
  9799. (defvar org-mark-ring nil
  9800. "Mark ring for positions before jumps in Org-mode.")
  9801. (defvar org-mark-ring-last-goto nil
  9802. "Last position in the mark ring used to go back.")
  9803. ;; Fill and close the ring
  9804. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  9805. (loop for i from 1 to org-mark-ring-length do
  9806. (push (make-marker) org-mark-ring))
  9807. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  9808. org-mark-ring)
  9809. (defun org-mark-ring-push (&optional pos buffer)
  9810. "Put the current position or POS into the mark ring and rotate it."
  9811. (interactive)
  9812. (setq pos (or pos (point)))
  9813. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  9814. (move-marker (car org-mark-ring)
  9815. (or pos (point))
  9816. (or buffer (current-buffer)))
  9817. (message "%s"
  9818. (substitute-command-keys
  9819. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  9820. (defun org-mark-ring-goto (&optional n)
  9821. "Jump to the previous position in the mark ring.
  9822. With prefix arg N, jump back that many stored positions. When
  9823. called several times in succession, walk through the entire ring.
  9824. Org-mode commands jumping to a different position in the current file,
  9825. or to another Org-mode file, automatically push the old position
  9826. onto the ring."
  9827. (interactive "p")
  9828. (let (p m)
  9829. (if (eq last-command this-command)
  9830. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  9831. (setq p org-mark-ring))
  9832. (setq org-mark-ring-last-goto p)
  9833. (setq m (car p))
  9834. (org-pop-to-buffer-same-window (marker-buffer m))
  9835. (goto-char m)
  9836. (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  9837. (defun org-remove-angle-brackets (s)
  9838. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  9839. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  9840. s)
  9841. (defun org-add-angle-brackets (s)
  9842. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  9843. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  9844. s)
  9845. (defun org-remove-double-quotes (s)
  9846. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  9847. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  9848. s)
  9849. ;;; Following specific links
  9850. (defun org-follow-timestamp-link ()
  9851. "Open an agenda view for the time-stamp date/range at point."
  9852. (cond
  9853. ((org-at-date-range-p t)
  9854. (let ((org-agenda-start-on-weekday)
  9855. (t1 (match-string 1))
  9856. (t2 (match-string 2)) tt1 tt2)
  9857. (setq tt1 (time-to-days (org-time-string-to-time t1))
  9858. tt2 (time-to-days (org-time-string-to-time t2)))
  9859. (let ((org-agenda-buffer-tmp-name
  9860. (format "*Org Agenda(a:%s)"
  9861. (concat (substring t1 0 10) "--" (substring t2 0 10)))))
  9862. (org-agenda-list nil tt1 (1+ (- tt2 tt1))))))
  9863. ((org-at-timestamp-p t)
  9864. (let ((org-agenda-buffer-tmp-name
  9865. (format "*Org Agenda(a:%s)" (substring (match-string 1) 0 10))))
  9866. (org-agenda-list nil (time-to-days (org-time-string-to-time
  9867. (substring (match-string 1) 0 10)))
  9868. 1)))
  9869. (t (error "This should not happen"))))
  9870. ;;; Following file links
  9871. (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
  9872. (declare-function mailcap-extension-to-mime "mailcap" (extn))
  9873. (declare-function mailcap-mime-info
  9874. "mailcap" (string &optional request no-decode))
  9875. (defvar org-wait nil)
  9876. (defun org-open-file (path &optional in-emacs line search)
  9877. "Open the file at PATH.
  9878. First, this expands any special file name abbreviations. Then the
  9879. configuration variable `org-file-apps' is checked if it contains an
  9880. entry for this file type, and if yes, the corresponding command is launched.
  9881. If no application is found, Emacs simply visits the file.
  9882. With optional prefix argument IN-EMACS, Emacs will visit the file.
  9883. With a double \\[universal-argument] \\[universal-argument] \
  9884. prefix arg, Org tries to avoid opening in Emacs
  9885. and to use an external application to visit the file.
  9886. Optional LINE specifies a line to go to, optional SEARCH a string
  9887. to search for. If LINE or SEARCH is given, the file will be
  9888. opened in Emacs, unless an entry from org-file-apps that makes
  9889. use of groups in a regexp matches.
  9890. If you want to change the way frames are used when following a
  9891. link, please customize `org-link-frame-setup'.
  9892. If the file does not exist, an error is thrown."
  9893. (let* ((file (if (equal path "")
  9894. buffer-file-name
  9895. (substitute-in-file-name (expand-file-name path))))
  9896. (file-apps (append org-file-apps (org-default-apps)))
  9897. (apps (org-remove-if
  9898. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9899. (apps-dlink (org-remove-if-not
  9900. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9901. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  9902. (dirp (if remp nil (file-directory-p file)))
  9903. (file (if (and dirp org-open-directory-means-index-dot-org)
  9904. (concat (file-name-as-directory file) "index.org")
  9905. file))
  9906. (a-m-a-p (assq 'auto-mode apps))
  9907. (dfile (downcase file))
  9908. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  9909. (link (cond ((and (eq line nil)
  9910. (eq search nil))
  9911. file)
  9912. (line
  9913. (concat file "::" (number-to-string line)))
  9914. (search
  9915. (concat file "::" search))))
  9916. (dlink (downcase link))
  9917. (old-buffer (current-buffer))
  9918. (old-pos (point))
  9919. (old-mode major-mode)
  9920. ext cmd link-match-data)
  9921. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  9922. (setq ext (match-string 1 dfile))
  9923. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  9924. (setq ext (match-string 1 dfile))))
  9925. (cond
  9926. ((member in-emacs '((16) system))
  9927. (setq cmd (cdr (assoc 'system apps))))
  9928. (in-emacs (setq cmd 'emacs))
  9929. (t
  9930. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  9931. (and dirp (cdr (assoc 'directory apps)))
  9932. ; first, try matching against apps-dlink
  9933. ; if we get a match here, store the match data for later
  9934. (let ((match (assoc-default dlink apps-dlink
  9935. 'string-match)))
  9936. (if match
  9937. (progn (setq link-match-data (match-data))
  9938. match)
  9939. (progn (setq in-emacs (or in-emacs line search))
  9940. nil))) ; if we have no match in apps-dlink,
  9941. ; always open the file in emacs if line or search
  9942. ; is given (for backwards compatibility)
  9943. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  9944. 'string-match)
  9945. (cdr (assoc ext apps))
  9946. (cdr (assoc t apps))))))
  9947. (when (eq cmd 'system)
  9948. (setq cmd (cdr (assoc 'system apps))))
  9949. (when (eq cmd 'default)
  9950. (setq cmd (cdr (assoc t apps))))
  9951. (when (eq cmd 'mailcap)
  9952. (require 'mailcap)
  9953. (mailcap-parse-mailcaps)
  9954. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  9955. (command (mailcap-mime-info mime-type)))
  9956. (if (stringp command)
  9957. (setq cmd command)
  9958. (setq cmd 'emacs))))
  9959. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  9960. (not (file-exists-p file))
  9961. (not org-open-non-existing-files))
  9962. (error "No such file: %s" file))
  9963. (cond
  9964. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  9965. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  9966. (while (string-match "['\"]%s['\"]" cmd)
  9967. (setq cmd (replace-match "%s" t t cmd)))
  9968. (while (string-match "%s" cmd)
  9969. (setq cmd (replace-match
  9970. (save-match-data
  9971. (shell-quote-argument
  9972. (convert-standard-filename file)))
  9973. t t cmd)))
  9974. ;; Replace "%1", "%2" etc. in command with group matches from regex
  9975. (save-match-data
  9976. (let ((match-index 1)
  9977. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  9978. (set-match-data link-match-data)
  9979. (while (<= match-index number-of-groups)
  9980. (let ((regex (concat "%" (number-to-string match-index)))
  9981. (replace-with (match-string match-index dlink)))
  9982. (while (string-match regex cmd)
  9983. (setq cmd (replace-match replace-with t t cmd))))
  9984. (setq match-index (+ match-index 1)))))
  9985. (save-window-excursion
  9986. (message "Running %s...done" cmd)
  9987. (start-process-shell-command cmd nil cmd)
  9988. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))))
  9989. ((or (stringp cmd)
  9990. (eq cmd 'emacs))
  9991. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  9992. (widen)
  9993. (if line (org-goto-line line)
  9994. (if search (org-link-search search))))
  9995. ((consp cmd)
  9996. (let ((file (convert-standard-filename file)))
  9997. (save-match-data
  9998. (set-match-data link-match-data)
  9999. (eval cmd))))
  10000. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  10001. (and (derived-mode-p 'org-mode) (eq old-mode 'org-mode)
  10002. (or (not (equal old-buffer (current-buffer)))
  10003. (not (equal old-pos (point))))
  10004. (org-mark-ring-push old-pos old-buffer))))
  10005. (defun org-file-apps-entry-match-against-dlink-p (entry)
  10006. "This function returns non-nil if `entry' uses a regular
  10007. expression which should be matched against the whole link by
  10008. org-open-file.
  10009. It assumes that is the case when the entry uses a regular
  10010. expression which has at least one grouping construct and the
  10011. action is either a lisp form or a command string containing
  10012. '%1', i.e. using at least one subexpression match as a
  10013. parameter."
  10014. (let ((selector (car entry))
  10015. (action (cdr entry)))
  10016. (if (stringp selector)
  10017. (and (> (regexp-opt-depth selector) 0)
  10018. (or (and (stringp action)
  10019. (string-match "%[0-9]" action))
  10020. (consp action)))
  10021. nil)))
  10022. (defun org-default-apps ()
  10023. "Return the default applications for this operating system."
  10024. (cond
  10025. ((eq system-type 'darwin)
  10026. org-file-apps-defaults-macosx)
  10027. ((eq system-type 'windows-nt)
  10028. org-file-apps-defaults-windowsnt)
  10029. (t org-file-apps-defaults-gnu)))
  10030. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  10031. "Convert extensions to regular expressions in the cars of LIST.
  10032. Also, weed out any non-string entries, because the return value is used
  10033. only for regexp matching.
  10034. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  10035. point to the symbol `emacs', indicating that the file should
  10036. be opened in Emacs."
  10037. (append
  10038. (delq nil
  10039. (mapcar (lambda (x)
  10040. (if (not (stringp (car x)))
  10041. nil
  10042. (if (string-match "\\W" (car x))
  10043. x
  10044. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  10045. list))
  10046. (if add-auto-mode
  10047. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  10048. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  10049. (defun org-file-remote-p (file)
  10050. "Test whether FILE specifies a location on a remote system.
  10051. Return non-nil if the location is indeed remote.
  10052. For example, the filename \"/user@host:/foo\" specifies a location
  10053. on the system \"/user@host:\"."
  10054. (cond ((fboundp 'file-remote-p)
  10055. (file-remote-p file))
  10056. ((fboundp 'tramp-handle-file-remote-p)
  10057. (tramp-handle-file-remote-p file))
  10058. ((and (boundp 'ange-ftp-name-format)
  10059. (string-match (car ange-ftp-name-format) file))
  10060. t)))
  10061. ;;;; Refiling
  10062. (defun org-get-org-file ()
  10063. "Read a filename, with default directory `org-directory'."
  10064. (let ((default (or org-default-notes-file remember-data-file)))
  10065. (read-file-name (format "File name [%s]: " default)
  10066. (file-name-as-directory org-directory)
  10067. default)))
  10068. (defun org-notes-order-reversed-p ()
  10069. "Check if the current file should receive notes in reversed order."
  10070. (cond
  10071. ((not org-reverse-note-order) nil)
  10072. ((eq t org-reverse-note-order) t)
  10073. ((not (listp org-reverse-note-order)) nil)
  10074. (t (catch 'exit
  10075. (let ((all org-reverse-note-order)
  10076. entry)
  10077. (while (setq entry (pop all))
  10078. (if (string-match (car entry) buffer-file-name)
  10079. (throw 'exit (cdr entry))))
  10080. nil)))))
  10081. (defvar org-refile-target-table nil
  10082. "The list of refile targets, created by `org-refile'.")
  10083. (defvar org-agenda-new-buffers nil
  10084. "Buffers created to visit agenda files.")
  10085. (defvar org-refile-cache nil
  10086. "Cache for refile targets.")
  10087. (defvar org-refile-markers nil
  10088. "All the markers used for caching refile locations.")
  10089. (defun org-refile-marker (pos)
  10090. "Get a new refile marker, but only if caching is in use."
  10091. (if (not org-refile-use-cache)
  10092. pos
  10093. (let ((m (make-marker)))
  10094. (move-marker m pos)
  10095. (push m org-refile-markers)
  10096. m)))
  10097. (defun org-refile-cache-clear ()
  10098. "Clear the refile cache and disable all the markers."
  10099. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  10100. (setq org-refile-markers nil)
  10101. (setq org-refile-cache nil)
  10102. (message "Refile cache has been cleared"))
  10103. (defun org-refile-cache-check-set (set)
  10104. "Check if all the markers in the cache still have live buffers."
  10105. (let (marker)
  10106. (catch 'exit
  10107. (while (and set (setq marker (nth 3 (pop set))))
  10108. ;; If `org-refile-use-outline-path' is 'file, marker may be nil
  10109. (when (and marker (null (marker-buffer marker)))
  10110. (message "Please regenerate the refile cache with `C-0 C-c C-w'")
  10111. (sit-for 3)
  10112. (throw 'exit nil)))
  10113. t)))
  10114. (defun org-refile-cache-put (set &rest identifiers)
  10115. "Push the refile targets SET into the cache, under IDENTIFIERS."
  10116. (let* ((key (sha1 (prin1-to-string identifiers)))
  10117. (entry (assoc key org-refile-cache)))
  10118. (if entry
  10119. (setcdr entry set)
  10120. (push (cons key set) org-refile-cache))))
  10121. (defun org-refile-cache-get (&rest identifiers)
  10122. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  10123. (cond
  10124. ((not org-refile-cache) nil)
  10125. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  10126. (t
  10127. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  10128. org-refile-cache))))
  10129. (and set (org-refile-cache-check-set set) set)))))
  10130. (defun org-refile-get-targets (&optional default-buffer excluded-entries)
  10131. "Produce a table with refile targets."
  10132. (let ((case-fold-search nil)
  10133. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  10134. (entries (or org-refile-targets '((nil . (:level . 1)))))
  10135. targets tgs txt re files f desc descre fast-path-p level pos0)
  10136. (message "Getting targets...")
  10137. (with-current-buffer (or default-buffer (current-buffer))
  10138. (while (setq entry (pop entries))
  10139. (setq files (car entry) desc (cdr entry))
  10140. (setq fast-path-p nil)
  10141. (cond
  10142. ((null files) (setq files (list (current-buffer))))
  10143. ((eq files 'org-agenda-files)
  10144. (setq files (org-agenda-files 'unrestricted)))
  10145. ((and (symbolp files) (fboundp files))
  10146. (setq files (funcall files)))
  10147. ((and (symbolp files) (boundp files))
  10148. (setq files (symbol-value files))))
  10149. (if (stringp files) (setq files (list files)))
  10150. (cond
  10151. ((eq (car desc) :tag)
  10152. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  10153. ((eq (car desc) :todo)
  10154. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  10155. ((eq (car desc) :regexp)
  10156. (setq descre (cdr desc)))
  10157. ((eq (car desc) :level)
  10158. (setq descre (concat "^\\*\\{" (number-to-string
  10159. (if org-odd-levels-only
  10160. (1- (* 2 (cdr desc)))
  10161. (cdr desc)))
  10162. "\\}[ \t]")))
  10163. ((eq (car desc) :maxlevel)
  10164. (setq fast-path-p t)
  10165. (setq descre (concat "^\\*\\{1," (number-to-string
  10166. (if org-odd-levels-only
  10167. (1- (* 2 (cdr desc)))
  10168. (cdr desc)))
  10169. "\\}[ \t]")))
  10170. (t (error "Bad refiling target description %s" desc)))
  10171. (while (setq f (pop files))
  10172. (with-current-buffer
  10173. (if (bufferp f) f (org-get-agenda-file-buffer f))
  10174. (or
  10175. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  10176. (progn
  10177. (if (bufferp f) (setq f (buffer-file-name
  10178. (buffer-base-buffer f))))
  10179. (setq f (and f (expand-file-name f)))
  10180. (if (eq org-refile-use-outline-path 'file)
  10181. (push (list (file-name-nondirectory f) f nil nil) tgs))
  10182. (save-excursion
  10183. (save-restriction
  10184. (widen)
  10185. (goto-char (point-min))
  10186. (while (re-search-forward descre nil t)
  10187. (goto-char (setq pos0 (point-at-bol)))
  10188. (catch 'next
  10189. (when org-refile-target-verify-function
  10190. (save-match-data
  10191. (or (funcall org-refile-target-verify-function)
  10192. (throw 'next t))))
  10193. (when (and (looking-at org-complex-heading-regexp)
  10194. (not (member (match-string 4) excluded-entries))
  10195. (match-string 4))
  10196. (setq level (org-reduced-level
  10197. (- (match-end 1) (match-beginning 1)))
  10198. txt (org-link-display-format (match-string 4))
  10199. txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
  10200. re (format org-complex-heading-regexp-format
  10201. (regexp-quote (match-string 4))))
  10202. (when org-refile-use-outline-path
  10203. (setq txt (mapconcat
  10204. 'org-protect-slash
  10205. (append
  10206. (if (eq org-refile-use-outline-path
  10207. 'file)
  10208. (list (file-name-nondirectory
  10209. (buffer-file-name
  10210. (buffer-base-buffer))))
  10211. (if (eq org-refile-use-outline-path
  10212. 'full-file-path)
  10213. (list (buffer-file-name
  10214. (buffer-base-buffer)))))
  10215. (org-get-outline-path fast-path-p
  10216. level txt)
  10217. (list txt))
  10218. "/")))
  10219. (push (list txt f re (org-refile-marker (point)))
  10220. tgs)))
  10221. (when (= (point) pos0)
  10222. ;; verification function has not moved point
  10223. (goto-char (point-at-eol))))))))
  10224. (when org-refile-use-cache
  10225. (org-refile-cache-put tgs (buffer-file-name) descre))
  10226. (setq targets (append tgs targets))
  10227. ))))
  10228. (message "Getting targets...done")
  10229. (nreverse targets)))
  10230. (defun org-protect-slash (s)
  10231. (while (string-match "/" s)
  10232. (setq s (replace-match "\\" t t s)))
  10233. s)
  10234. (defvar org-olpa (make-vector 20 nil))
  10235. (defun org-get-outline-path (&optional fastp level heading)
  10236. "Return the outline path to the current entry, as a list.
  10237. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  10238. routine which makes outline path derivations for an entire file,
  10239. avoiding backtracing. Refile target collection makes use of that."
  10240. (if fastp
  10241. (progn
  10242. (if (> level 19)
  10243. (error "Outline path failure, more than 19 levels"))
  10244. (loop for i from level upto 19 do
  10245. (aset org-olpa i nil))
  10246. (prog1
  10247. (delq nil (append org-olpa nil))
  10248. (aset org-olpa level heading)))
  10249. (let (rtn case-fold-search)
  10250. (save-excursion
  10251. (save-restriction
  10252. (widen)
  10253. (while (org-up-heading-safe)
  10254. (when (looking-at org-complex-heading-regexp)
  10255. (push (org-match-string-no-properties 4) rtn)))
  10256. rtn)))))
  10257. (defun org-format-outline-path (path &optional width prefix separator)
  10258. "Format the outline path PATH for display.
  10259. WIDTH is the maximum number of characters that is available.
  10260. PREFIX is a prefix to be included in the returned string,
  10261. such as the file name.
  10262. SEPARATOR is inserted between the different parts of the path,
  10263. the default is \"/\"."
  10264. (setq width (or width 79))
  10265. (if prefix (setq width (- width (length prefix))))
  10266. (if (not path)
  10267. (or prefix "")
  10268. (let* ((nsteps (length path))
  10269. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  10270. (maxwidth (if (<= total-width width)
  10271. 10000 ;; everything fits
  10272. ;; we need to shorten the level headings
  10273. (/ (- width nsteps) nsteps)))
  10274. (org-odd-levels-only nil)
  10275. (n 0)
  10276. (total (1+ (length prefix))))
  10277. (setq maxwidth (max maxwidth 10))
  10278. (concat prefix
  10279. (if prefix (or separator "/"))
  10280. (mapconcat
  10281. (lambda (h)
  10282. (setq n (1+ n))
  10283. (if (and (= n nsteps) (< maxwidth 10000))
  10284. (setq maxwidth (- total-width total)))
  10285. (if (< (length h) maxwidth)
  10286. (progn (setq total (+ total (length h) 1)) h)
  10287. (setq h (substring h 0 (- maxwidth 2))
  10288. total (+ total maxwidth 1))
  10289. (if (string-match "[ \t]+\\'" h)
  10290. (setq h (substring h 0 (match-beginning 0))))
  10291. (setq h (concat h "..")))
  10292. (org-add-props h nil 'face
  10293. (nth (% (1- n) org-n-level-faces)
  10294. org-level-faces))
  10295. h)
  10296. path (or separator "/"))))))
  10297. (defun org-display-outline-path (&optional file current separator just-return-string)
  10298. "Display the current outline path in the echo area.
  10299. If FILE is non-nil, prepend the output with the file name.
  10300. If CURRENT is non-nil, append the current heading to the output.
  10301. SEPARATOR is passed through to `org-format-outline-path'. It separates
  10302. the different parts of the path and defaults to \"/\".
  10303. If JUST-RETURN-STRING is non-nil, return a string, don't display a message."
  10304. (interactive "P")
  10305. (let* (case-fold-search
  10306. message-log-max ; Don't populate the *Messages* buffer
  10307. (bfn (buffer-file-name (buffer-base-buffer)))
  10308. (path (and (derived-mode-p 'org-mode) (org-get-outline-path)))
  10309. res)
  10310. (if current (setq path (append path
  10311. (save-excursion
  10312. (org-back-to-heading t)
  10313. (if (looking-at org-complex-heading-regexp)
  10314. (list (match-string 4)))))))
  10315. (setq res
  10316. (org-format-outline-path
  10317. path
  10318. (1- (frame-width))
  10319. (and file bfn (concat (file-name-nondirectory bfn) separator))
  10320. separator))
  10321. (if just-return-string
  10322. (org-no-properties res)
  10323. (message "%s" res))))
  10324. (defvar org-refile-history nil
  10325. "History for refiling operations.")
  10326. (defvar org-after-refile-insert-hook nil
  10327. "Hook run after `org-refile' has inserted its stuff at the new location.
  10328. Note that this is still *before* the stuff will be removed from
  10329. the *old* location.")
  10330. (defvar org-capture-last-stored-marker)
  10331. (defvar org-refile-keep nil
  10332. "Non-nil means `org-refile' will copy instead of refile.")
  10333. (defun org-copy ()
  10334. "Like `org-refile', but copy."
  10335. (interactive)
  10336. (let ((org-refile-keep t))
  10337. (funcall 'org-refile nil nil nil "Copy")))
  10338. (defun org-refile (&optional goto default-buffer rfloc msg)
  10339. "Move the entry or entries at point to another heading.
  10340. The list of target headings is compiled using the information in
  10341. `org-refile-targets', which see.
  10342. At the target location, the entry is filed as a subitem of the target
  10343. heading. Depending on `org-reverse-note-order', the new subitem will
  10344. either be the first or the last subitem.
  10345. If there is an active region, all entries in that region will be moved.
  10346. However, the region must fulfill the requirement that the first heading
  10347. is the first one sets the top-level of the moved text - at most siblings
  10348. below it are allowed.
  10349. With prefix arg GOTO, the command will only visit the target location
  10350. and not actually move anything.
  10351. With a double prefix arg \\[universal-argument] \\[universal-argument], \
  10352. go to the location where the last refiling operation has put the subtree.
  10353. With a prefix argument of `2', refile to the running clock.
  10354. RFLOC can be a refile location obtained in a different way.
  10355. MSG is a string to replace \"Refile\" in the default prompt with
  10356. another verb. E.g. `org-copy' sets this parameter to \"Copy\".
  10357. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  10358. If you are using target caching (see `org-refile-use-cache'),
  10359. you have to clear the target cache in order to find new targets.
  10360. This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
  10361. prefix argument (`C-u C-u C-u C-c C-w')."
  10362. (interactive "P")
  10363. (if (member goto '(0 (64)))
  10364. (org-refile-cache-clear)
  10365. (let* ((actionmsg (or msg "Refile"))
  10366. (cbuf (current-buffer))
  10367. (regionp (org-region-active-p))
  10368. (region-start (and regionp (region-beginning)))
  10369. (region-end (and regionp (region-end)))
  10370. (region-length (and regionp (- region-end region-start)))
  10371. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  10372. pos it nbuf file re level reversed)
  10373. (setq last-command nil)
  10374. (when regionp
  10375. (goto-char region-start)
  10376. (or (bolp) (goto-char (point-at-bol)))
  10377. (setq region-start (point))
  10378. (unless (or (org-kill-is-subtree-p
  10379. (buffer-substring region-start region-end))
  10380. (prog1 org-refile-active-region-within-subtree
  10381. (org-toggle-heading)))
  10382. (error "The region is not a (sequence of) subtree(s)")))
  10383. (if (equal goto '(16))
  10384. (org-refile-goto-last-stored)
  10385. (when (or
  10386. (and (equal goto 2)
  10387. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  10388. (prog1
  10389. (setq it (list (or org-clock-heading "running clock")
  10390. (buffer-file-name
  10391. (marker-buffer org-clock-hd-marker))
  10392. ""
  10393. (marker-position org-clock-hd-marker)))
  10394. (setq goto nil)))
  10395. (setq it (or rfloc
  10396. (let (heading-text)
  10397. (save-excursion
  10398. (unless goto
  10399. (org-back-to-heading t)
  10400. (setq heading-text
  10401. (nth 4 (org-heading-components))))
  10402. (org-refile-get-location
  10403. (cond (goto "Goto")
  10404. (regionp (concat actionmsg " region to"))
  10405. (t (concat actionmsg " subtree \""
  10406. heading-text "\" to")))
  10407. default-buffer
  10408. (and (not (equal '(4) goto))
  10409. org-refile-allow-creating-parent-nodes)
  10410. goto))))))
  10411. (setq file (nth 1 it)
  10412. re (nth 2 it)
  10413. pos (nth 3 it))
  10414. (if (and (not goto)
  10415. pos
  10416. (equal (buffer-file-name) file)
  10417. (if regionp
  10418. (and (>= pos region-start)
  10419. (<= pos region-end))
  10420. (and (>= pos (point))
  10421. (< pos (save-excursion
  10422. (org-end-of-subtree t t))))))
  10423. (error "Cannot refile to position inside the tree or region"))
  10424. (setq nbuf (or (find-buffer-visiting file)
  10425. (find-file-noselect file)))
  10426. (if goto
  10427. (progn
  10428. (org-pop-to-buffer-same-window nbuf)
  10429. (goto-char pos)
  10430. (org-show-context 'org-goto))
  10431. (if regionp
  10432. (progn
  10433. (org-kill-new (buffer-substring region-start region-end))
  10434. (org-save-markers-in-region region-start region-end))
  10435. (org-copy-subtree 1 nil t))
  10436. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  10437. (find-file-noselect file)))
  10438. (setq reversed (org-notes-order-reversed-p))
  10439. (save-excursion
  10440. (save-restriction
  10441. (widen)
  10442. (if pos
  10443. (progn
  10444. (goto-char pos)
  10445. (looking-at org-outline-regexp)
  10446. (setq level (org-get-valid-level (funcall outline-level) 1))
  10447. (goto-char
  10448. (if reversed
  10449. (or (outline-next-heading) (point-max))
  10450. (or (save-excursion (org-get-next-sibling))
  10451. (org-end-of-subtree t t)
  10452. (point-max)))))
  10453. (setq level 1)
  10454. (if (not reversed)
  10455. (goto-char (point-max))
  10456. (goto-char (point-min))
  10457. (or (outline-next-heading) (goto-char (point-max)))))
  10458. (if (not (bolp)) (newline))
  10459. (org-paste-subtree level)
  10460. (when org-log-refile
  10461. (org-add-log-setup 'refile nil nil 'findpos
  10462. org-log-refile)
  10463. (unless (eq org-log-refile 'note)
  10464. (save-excursion (org-add-log-note))))
  10465. (and org-auto-align-tags
  10466. (let ((org-loop-over-headlines-in-active-region nil))
  10467. (org-set-tags nil t)))
  10468. (with-demoted-errors
  10469. (bookmark-set "org-refile-last-stored"))
  10470. ;; If we are refiling for capture, make sure that the
  10471. ;; last-capture pointers point here
  10472. (when (org-bound-and-true-p org-refile-for-capture)
  10473. (with-demoted-errors
  10474. (bookmark-set "org-capture-last-stored-marker"))
  10475. (move-marker org-capture-last-stored-marker (point)))
  10476. (if (fboundp 'deactivate-mark) (deactivate-mark))
  10477. (run-hooks 'org-after-refile-insert-hook))))
  10478. (unless org-refile-keep
  10479. (if regionp
  10480. (delete-region (point) (+ (point) region-length))
  10481. (org-cut-subtree)))
  10482. (when (featurep 'org-inlinetask)
  10483. (org-inlinetask-remove-END-maybe))
  10484. (setq org-markers-to-move nil)
  10485. (message (concat actionmsg " to \"%s\" in file %s: done") (car it) file)))))))
  10486. (defun org-refile-goto-last-stored ()
  10487. "Go to the location where the last refile was stored."
  10488. (interactive)
  10489. (bookmark-jump "org-refile-last-stored")
  10490. (message "This is the location of the last refile"))
  10491. (defun org-refile-get-location (&optional prompt default-buffer new-nodes
  10492. no-exclude)
  10493. "Prompt the user for a refile location, using PROMPT.
  10494. PROMPT should not be suffixed with a colon and a space, because
  10495. this function appends the default value from
  10496. `org-refile-history' automatically, if that is not empty.
  10497. When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
  10498. this is used for the GOTO interface."
  10499. (let ((org-refile-targets org-refile-targets)
  10500. (org-refile-use-outline-path org-refile-use-outline-path)
  10501. excluded-entries)
  10502. (when (and (derived-mode-p 'org-mode)
  10503. (not org-refile-use-cache)
  10504. (not no-exclude))
  10505. (org-map-tree
  10506. (lambda()
  10507. (setq excluded-entries
  10508. (append excluded-entries (list (org-get-heading t t)))))))
  10509. (setq org-refile-target-table
  10510. (org-refile-get-targets default-buffer excluded-entries)))
  10511. (unless org-refile-target-table
  10512. (error "No refile targets"))
  10513. (let* ((cbuf (current-buffer))
  10514. (partial-completion-mode nil)
  10515. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  10516. (cfunc (if (and org-refile-use-outline-path
  10517. org-outline-path-complete-in-steps)
  10518. 'org-olpath-completing-read
  10519. 'org-icompleting-read))
  10520. (extra (if org-refile-use-outline-path "/" ""))
  10521. (cbnex (concat (buffer-name) extra))
  10522. (filename (and cfn (expand-file-name cfn)))
  10523. (tbl (mapcar
  10524. (lambda (x)
  10525. (if (and (not (member org-refile-use-outline-path
  10526. '(file full-file-path)))
  10527. (not (equal filename (nth 1 x))))
  10528. (cons (concat (car x) extra " ("
  10529. (file-name-nondirectory (nth 1 x)) ")")
  10530. (cdr x))
  10531. (cons (concat (car x) extra) (cdr x))))
  10532. org-refile-target-table))
  10533. (completion-ignore-case t)
  10534. cdef
  10535. (prompt (concat prompt
  10536. (or (and (car org-refile-history)
  10537. (concat " (default " (car org-refile-history) ")"))
  10538. (and (assoc cbnex tbl) (setq cdef cbnex)
  10539. (concat " (default " cbnex ")"))) ": "))
  10540. pa answ parent-target child parent old-hist)
  10541. (setq old-hist org-refile-history)
  10542. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  10543. nil 'org-refile-history (or cdef (car org-refile-history))))
  10544. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  10545. (org-refile-check-position pa)
  10546. (if pa
  10547. (progn
  10548. (when (or (not org-refile-history)
  10549. (not (eq old-hist org-refile-history))
  10550. (not (equal (car pa) (car org-refile-history))))
  10551. (setq org-refile-history
  10552. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  10553. org-refile-history
  10554. (cdr org-refile-history))))
  10555. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  10556. (pop org-refile-history)))
  10557. pa)
  10558. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  10559. (progn
  10560. (setq parent (match-string 1 answ)
  10561. child (match-string 2 answ))
  10562. (setq parent-target (or (assoc parent tbl)
  10563. (assoc (concat parent "/") tbl)))
  10564. (when (and parent-target
  10565. (or (eq new-nodes t)
  10566. (and (eq new-nodes 'confirm)
  10567. (y-or-n-p (format "Create new node \"%s\"? "
  10568. child)))))
  10569. (org-refile-new-child parent-target child)))
  10570. (error "Invalid target location")))))
  10571. (declare-function org-string-nw-p "org-macs" (s))
  10572. (defun org-refile-check-position (refile-pointer)
  10573. "Check if the refile pointer matches the headline to which it points."
  10574. (let* ((file (nth 1 refile-pointer))
  10575. (re (nth 2 refile-pointer))
  10576. (pos (nth 3 refile-pointer))
  10577. buffer)
  10578. (if (and (not (markerp pos)) (not file))
  10579. (error "Please save the buffer to a file before refiling")
  10580. (when (org-string-nw-p re)
  10581. (setq buffer (if (markerp pos)
  10582. (marker-buffer pos)
  10583. (or (find-buffer-visiting file)
  10584. (find-file-noselect file))))
  10585. (with-current-buffer buffer
  10586. (save-excursion
  10587. (save-restriction
  10588. (widen)
  10589. (goto-char pos)
  10590. (beginning-of-line 1)
  10591. (unless (org-looking-at-p re)
  10592. (error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling")))))))))
  10593. (defun org-refile-new-child (parent-target child)
  10594. "Use refile target PARENT-TARGET to add new CHILD below it."
  10595. (unless parent-target
  10596. (error "Cannot find parent for new node"))
  10597. (let ((file (nth 1 parent-target))
  10598. (pos (nth 3 parent-target))
  10599. level)
  10600. (with-current-buffer (or (find-buffer-visiting file)
  10601. (find-file-noselect file))
  10602. (save-excursion
  10603. (save-restriction
  10604. (widen)
  10605. (if pos
  10606. (goto-char pos)
  10607. (goto-char (point-max))
  10608. (if (not (bolp)) (newline)))
  10609. (when (looking-at org-outline-regexp)
  10610. (setq level (funcall outline-level))
  10611. (org-end-of-subtree t t))
  10612. (org-back-over-empty-lines)
  10613. (insert "\n" (make-string
  10614. (if pos (org-get-valid-level level 1) 1) ?*)
  10615. " " child "\n")
  10616. (beginning-of-line 0)
  10617. (list (concat (car parent-target) "/" child) file "" (point)))))))
  10618. (defun org-olpath-completing-read (prompt collection &rest args)
  10619. "Read an outline path like a file name."
  10620. (let ((thetable collection)
  10621. (org-completion-use-ido nil) ; does not work with ido.
  10622. (org-completion-use-iswitchb nil)) ; or iswitchb
  10623. (apply
  10624. 'org-icompleting-read prompt
  10625. (lambda (string predicate &optional flag)
  10626. (let (rtn r f (l (length string)))
  10627. (cond
  10628. ((eq flag nil)
  10629. ;; try completion
  10630. (try-completion string thetable))
  10631. ((eq flag t)
  10632. ;; all-completions
  10633. (setq rtn (all-completions string thetable predicate))
  10634. (mapcar
  10635. (lambda (x)
  10636. (setq r (substring x l))
  10637. (if (string-match " ([^)]*)$" x)
  10638. (setq f (match-string 0 x))
  10639. (setq f ""))
  10640. (if (string-match "/" r)
  10641. (concat string (substring r 0 (match-end 0)) f)
  10642. x))
  10643. rtn))
  10644. ((eq flag 'lambda)
  10645. ;; exact match?
  10646. (assoc string thetable)))))
  10647. args)))
  10648. ;;;; Dynamic blocks
  10649. (defun org-find-dblock (name)
  10650. "Find the first dynamic block with name NAME in the buffer.
  10651. If not found, stay at current position and return nil."
  10652. (let ((case-fold-search t) pos)
  10653. (save-excursion
  10654. (goto-char (point-min))
  10655. (setq pos (and (re-search-forward
  10656. (concat "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+" name "\\>") nil t)
  10657. (match-beginning 0))))
  10658. (if pos (goto-char pos))
  10659. pos))
  10660. (defconst org-dblock-start-re
  10661. "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  10662. "Matches the start line of a dynamic block, with parameters.")
  10663. (defconst org-dblock-end-re "^[ \t]*#\\+\\(?:END\\|end\\)\\([: \t\r\n]\\|$\\)"
  10664. "Matches the end of a dynamic block.")
  10665. (defun org-create-dblock (plist)
  10666. "Create a dynamic block section, with parameters taken from PLIST.
  10667. PLIST must contain a :name entry which is used as name of the block."
  10668. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  10669. (end-of-line 1)
  10670. (newline))
  10671. (let ((col (current-column))
  10672. (name (plist-get plist :name)))
  10673. (insert "#+BEGIN: " name)
  10674. (while plist
  10675. (if (eq (car plist) :name)
  10676. (setq plist (cddr plist))
  10677. (insert " " (prin1-to-string (pop plist)))))
  10678. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  10679. (beginning-of-line -2)))
  10680. (defun org-prepare-dblock ()
  10681. "Prepare dynamic block for refresh.
  10682. This empties the block, puts the cursor at the insert position and returns
  10683. the property list including an extra property :name with the block name."
  10684. (unless (looking-at org-dblock-start-re)
  10685. (error "Not at a dynamic block"))
  10686. (let* ((begdel (1+ (match-end 0)))
  10687. (name (org-no-properties (match-string 1)))
  10688. (params (append (list :name name)
  10689. (read (concat "(" (match-string 3) ")")))))
  10690. (save-excursion
  10691. (beginning-of-line 1)
  10692. (skip-chars-forward " \t")
  10693. (setq params (plist-put params :indentation-column (current-column))))
  10694. (unless (re-search-forward org-dblock-end-re nil t)
  10695. (error "Dynamic block not terminated"))
  10696. (setq params
  10697. (append params
  10698. (list :content (buffer-substring
  10699. begdel (match-beginning 0)))))
  10700. (delete-region begdel (match-beginning 0))
  10701. (goto-char begdel)
  10702. (open-line 1)
  10703. params))
  10704. (defun org-map-dblocks (&optional command)
  10705. "Apply COMMAND to all dynamic blocks in the current buffer.
  10706. If COMMAND is not given, use `org-update-dblock'."
  10707. (let ((cmd (or command 'org-update-dblock)))
  10708. (save-excursion
  10709. (goto-char (point-min))
  10710. (while (re-search-forward org-dblock-start-re nil t)
  10711. (goto-char (match-beginning 0))
  10712. (save-excursion
  10713. (condition-case nil
  10714. (funcall cmd)
  10715. (error (message "Error during update of dynamic block"))))
  10716. (unless (re-search-forward org-dblock-end-re nil t)
  10717. (error "Dynamic block not terminated"))))))
  10718. (defun org-dblock-update (&optional arg)
  10719. "User command for updating dynamic blocks.
  10720. Update the dynamic block at point. With prefix ARG, update all dynamic
  10721. blocks in the buffer."
  10722. (interactive "P")
  10723. (if arg
  10724. (org-update-all-dblocks)
  10725. (or (looking-at org-dblock-start-re)
  10726. (org-beginning-of-dblock))
  10727. (org-update-dblock)))
  10728. (defun org-update-dblock ()
  10729. "Update the dynamic block at point.
  10730. This means to empty the block, parse for parameters and then call
  10731. the correct writing function."
  10732. (interactive)
  10733. (save-window-excursion
  10734. (let* ((pos (point))
  10735. (line (org-current-line))
  10736. (params (org-prepare-dblock))
  10737. (name (plist-get params :name))
  10738. (indent (plist-get params :indentation-column))
  10739. (cmd (intern (concat "org-dblock-write:" name))))
  10740. (message "Updating dynamic block `%s' at line %d..." name line)
  10741. (funcall cmd params)
  10742. (message "Updating dynamic block `%s' at line %d...done" name line)
  10743. (goto-char pos)
  10744. (when (and indent (> indent 0))
  10745. (setq indent (make-string indent ?\ ))
  10746. (save-excursion
  10747. (org-beginning-of-dblock)
  10748. (forward-line 1)
  10749. (while (not (looking-at org-dblock-end-re))
  10750. (insert indent)
  10751. (beginning-of-line 2))
  10752. (when (looking-at org-dblock-end-re)
  10753. (and (looking-at "[ \t]+")
  10754. (replace-match ""))
  10755. (insert indent)))))))
  10756. (defun org-beginning-of-dblock ()
  10757. "Find the beginning of the dynamic block at point.
  10758. Error if there is no such block at point."
  10759. (let ((pos (point))
  10760. beg)
  10761. (end-of-line 1)
  10762. (if (and (re-search-backward org-dblock-start-re nil t)
  10763. (setq beg (match-beginning 0))
  10764. (re-search-forward org-dblock-end-re nil t)
  10765. (> (match-end 0) pos))
  10766. (goto-char beg)
  10767. (goto-char pos)
  10768. (error "Not in a dynamic block"))))
  10769. (defun org-update-all-dblocks ()
  10770. "Update all dynamic blocks in the buffer.
  10771. This function can be used in a hook."
  10772. (interactive)
  10773. (when (derived-mode-p 'org-mode)
  10774. (org-map-dblocks 'org-update-dblock)))
  10775. ;;;; Completion
  10776. (defun org-get-export-keywords ()
  10777. "Return a list of all currently understood export keywords.
  10778. Export keywords include options, block names, attributes and
  10779. keywords relative to each registered export back-end."
  10780. (delq nil
  10781. (let (keywords)
  10782. (mapc
  10783. (lambda (back-end)
  10784. (let ((props (cdr back-end)))
  10785. ;; Back-end name (for keywords, like #+LATEX:)
  10786. (push (upcase (symbol-name (car back-end))) keywords)
  10787. ;; Back-end options.
  10788. (mapc (lambda (option) (push (cadr option) keywords))
  10789. (plist-get (cdr back-end) :options-alist))))
  10790. (org-bound-and-true-p org-export-registered-backends))
  10791. keywords)))
  10792. (defconst org-options-keywords
  10793. '("ARCHIVE:" "AUTHOR:" "BIND:" "CATEGORY:" "COLUMNS:" "CREATOR:" "DATE:"
  10794. "DESCRIPTION:" "DRAWERS:" "EMAIL:" "EXCLUDE_TAGS:" "FILETAGS:" "INCLUDE:"
  10795. "INDEX:" "KEYWORDS:" "LANGUAGE:" "MACRO:" "OPTIONS:" "PROPERTY:"
  10796. "PRIORITIES:" "SELECT_TAGS:" "SEQ_TODO:" "SETUPFILE:" "STARTUP:" "TAGS:"
  10797. "TITLE:" "TODO:" "TYP_TODO:" "SELECT_TAGS:" "EXCLUDE_TAGS:"))
  10798. (defcustom org-structure-template-alist
  10799. '(("s" "#+BEGIN_SRC ?\n\n#+END_SRC"
  10800. "<src lang=\"?\">\n\n</src>")
  10801. ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE"
  10802. "<example>\n?\n</example>")
  10803. ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE"
  10804. "<quote>\n?\n</quote>")
  10805. ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE"
  10806. "<verse>\n?\n</verse>")
  10807. ("V" "#+BEGIN_VERBATIM\n?\n#+END_VERBATIM"
  10808. "<verbatim>\n?\n</verbatim>")
  10809. ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER"
  10810. "<center>\n?\n</center>")
  10811. ("l" "#+BEGIN_LaTeX\n?\n#+END_LaTeX"
  10812. "<literal style=\"latex\">\n?\n</literal>")
  10813. ("L" "#+LaTeX: "
  10814. "<literal style=\"latex\">?</literal>")
  10815. ("h" "#+BEGIN_HTML\n?\n#+END_HTML"
  10816. "<literal style=\"html\">\n?\n</literal>")
  10817. ("H" "#+HTML: "
  10818. "<literal style=\"html\">?</literal>")
  10819. ("a" "#+BEGIN_ASCII\n?\n#+END_ASCII")
  10820. ("A" "#+ASCII: ")
  10821. ("i" "#+INDEX: ?"
  10822. "#+INDEX: ?")
  10823. ("I" "#+INCLUDE: %file ?"
  10824. "<include file=%file markup=\"?\">"))
  10825. "Structure completion elements.
  10826. This is a list of abbreviation keys and values. The value gets inserted
  10827. if you type `<' followed by the key and then press the completion key,
  10828. usually `M-TAB'. %file will be replaced by a file name after prompting
  10829. for the file using completion. The cursor will be placed at the position
  10830. of the `?` in the template.
  10831. There are two templates for each key, the first uses the original Org syntax,
  10832. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  10833. the default when the /org-mtags.el/ module has been loaded. See also the
  10834. variable `org-mtags-prefer-muse-templates'."
  10835. :group 'org-completion
  10836. :type '(repeat
  10837. (string :tag "Key")
  10838. (string :tag "Template")
  10839. (string :tag "Muse Template")))
  10840. (defun org-try-structure-completion ()
  10841. "Try to complete a structure template before point.
  10842. This looks for strings like \"<e\" on an otherwise empty line and
  10843. expands them."
  10844. (let ((l (buffer-substring (point-at-bol) (point)))
  10845. a)
  10846. (when (and (looking-at "[ \t]*$")
  10847. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  10848. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  10849. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  10850. (match-beginning 1)) a)
  10851. t)))
  10852. (defun org-complete-expand-structure-template (start cell)
  10853. "Expand a structure template."
  10854. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  10855. (rpl (nth (if musep 2 1) cell))
  10856. (ind ""))
  10857. (delete-region start (point))
  10858. (when (string-match "\\`#\\+" rpl)
  10859. (cond
  10860. ((bolp))
  10861. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  10862. (setq ind (buffer-substring (point-at-bol) (point))))
  10863. (t (newline))))
  10864. (setq start (point))
  10865. (if (string-match "%file" rpl)
  10866. (setq rpl (replace-match
  10867. (concat
  10868. "\""
  10869. (save-match-data
  10870. (abbreviate-file-name (read-file-name "Include file: ")))
  10871. "\"")
  10872. t t rpl)))
  10873. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  10874. (concat "\n" ind)))
  10875. (insert rpl)
  10876. (if (re-search-backward "\\?" start t) (delete-char 1))))
  10877. ;;;; TODO, DEADLINE, Comments
  10878. (defun org-toggle-comment ()
  10879. "Change the COMMENT state of an entry."
  10880. (interactive)
  10881. (save-excursion
  10882. (org-back-to-heading)
  10883. (let (case-fold-search)
  10884. (cond
  10885. ((looking-at (format org-heading-keyword-regexp-format
  10886. org-comment-string))
  10887. (goto-char (match-end 1))
  10888. (looking-at (concat " +" org-comment-string))
  10889. (replace-match "" t t)
  10890. (when (eolp) (insert " ")))
  10891. ((looking-at org-outline-regexp)
  10892. (goto-char (match-end 0))
  10893. (insert org-comment-string " "))))))
  10894. (defvar org-last-todo-state-is-todo nil
  10895. "This is non-nil when the last TODO state change led to a TODO state.
  10896. If the last change removed the TODO tag or switched to DONE, then
  10897. this is nil.")
  10898. (defvar org-setting-tags nil) ; dynamically skipped
  10899. (defvar org-todo-setup-filter-hook nil
  10900. "Hook for functions that pre-filter todo specs.
  10901. Each function takes a todo spec and returns either nil or the spec
  10902. transformed into canonical form." )
  10903. (defvar org-todo-get-default-hook nil
  10904. "Hook for functions that get a default item for todo.
  10905. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  10906. nil or a string to be used for the todo mark." )
  10907. (defvar org-agenda-headline-snapshot-before-repeat)
  10908. (defun org-current-effective-time ()
  10909. "Return current time adjusted for `org-extend-today-until' variable."
  10910. (let* ((ct (org-current-time))
  10911. (dct (decode-time ct))
  10912. (ct1
  10913. (cond
  10914. (org-use-last-clock-out-time-as-effective-time
  10915. (or (org-clock-get-last-clock-out-time) ct))
  10916. ((and org-use-effective-time (< (nth 2 dct) org-extend-today-until))
  10917. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct)))
  10918. (t ct))))
  10919. ct1))
  10920. (defun org-todo-yesterday (&optional arg)
  10921. "Like `org-todo' but the time of change will be 23:59 of yesterday."
  10922. (interactive "P")
  10923. (if (eq major-mode 'org-agenda-mode)
  10924. (apply 'org-agenda-todo-yesterday arg)
  10925. (let* ((hour (third (decode-time
  10926. (org-current-time))))
  10927. (org-extend-today-until (1+ hour)))
  10928. (org-todo arg))))
  10929. (defvar org-block-entry-blocking ""
  10930. "First entry preventing the TODO state change.")
  10931. (defun org-todo (&optional arg)
  10932. "Change the TODO state of an item.
  10933. The state of an item is given by a keyword at the start of the heading,
  10934. like
  10935. *** TODO Write paper
  10936. *** DONE Call mom
  10937. The different keywords are specified in the variable `org-todo-keywords'.
  10938. By default the available states are \"TODO\" and \"DONE\".
  10939. So for this example: when the item starts with TODO, it is changed to DONE.
  10940. When it starts with DONE, the DONE is removed. And when neither TODO nor
  10941. DONE are present, add TODO at the beginning of the heading.
  10942. With \\[universal-argument] prefix arg, use completion to determine the new \
  10943. state.
  10944. With numeric prefix arg, switch to that state.
  10945. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  10946. keywords (nextset).
  10947. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  10948. With a numeric prefix arg of 0, inhibit note taking for the change.
  10949. For calling through lisp, arg is also interpreted in the following way:
  10950. 'none -> empty state
  10951. \"\"(empty string) -> switch to empty state
  10952. 'done -> switch to DONE
  10953. 'nextset -> switch to the next set of keywords
  10954. 'previousset -> switch to the previous set of keywords
  10955. \"WAITING\" -> switch to the specified keyword, but only if it
  10956. really is a member of `org-todo-keywords'."
  10957. (interactive "P")
  10958. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  10959. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  10960. 'region-start-level 'region))
  10961. org-loop-over-headlines-in-active-region)
  10962. (org-map-entries
  10963. `(org-todo ,arg)
  10964. org-loop-over-headlines-in-active-region
  10965. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  10966. (if (equal arg '(16)) (setq arg 'nextset))
  10967. (let ((org-blocker-hook org-blocker-hook)
  10968. commentp
  10969. case-fold-search)
  10970. (when (equal arg '(64))
  10971. (setq arg nil org-blocker-hook nil))
  10972. (when (and org-blocker-hook
  10973. (or org-inhibit-blocking
  10974. (org-entry-get nil "NOBLOCKING")))
  10975. (setq org-blocker-hook nil))
  10976. (save-excursion
  10977. (catch 'exit
  10978. (org-back-to-heading t)
  10979. (when (looking-at (concat "^\\*+ " org-comment-string))
  10980. (org-toggle-comment)
  10981. (setq commentp t))
  10982. (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
  10983. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
  10984. (looking-at "\\(?: *\\|[ \t]*$\\)"))
  10985. (let* ((match-data (match-data))
  10986. (startpos (point-at-bol))
  10987. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  10988. (org-log-done org-log-done)
  10989. (org-log-repeat org-log-repeat)
  10990. (org-todo-log-states org-todo-log-states)
  10991. (org-inhibit-logging
  10992. (if (equal arg 0)
  10993. (progn (setq arg nil) 'note) org-inhibit-logging))
  10994. (this (match-string 1))
  10995. (hl-pos (match-beginning 0))
  10996. (head (org-get-todo-sequence-head this))
  10997. (ass (assoc head org-todo-kwd-alist))
  10998. (interpret (nth 1 ass))
  10999. (done-word (nth 3 ass))
  11000. (final-done-word (nth 4 ass))
  11001. (org-last-state (or this ""))
  11002. (completion-ignore-case t)
  11003. (member (member this org-todo-keywords-1))
  11004. (tail (cdr member))
  11005. (org-state (cond
  11006. ((and org-todo-key-trigger
  11007. (or (and (equal arg '(4))
  11008. (eq org-use-fast-todo-selection 'prefix))
  11009. (and (not arg) org-use-fast-todo-selection
  11010. (not (eq org-use-fast-todo-selection
  11011. 'prefix)))))
  11012. ;; Use fast selection
  11013. (org-fast-todo-selection))
  11014. ((and (equal arg '(4))
  11015. (or (not org-use-fast-todo-selection)
  11016. (not org-todo-key-trigger)))
  11017. ;; Read a state with completion
  11018. (org-icompleting-read
  11019. "State: " (mapcar 'list org-todo-keywords-1)
  11020. nil t))
  11021. ((eq arg 'right)
  11022. (if this
  11023. (if tail (car tail) nil)
  11024. (car org-todo-keywords-1)))
  11025. ((eq arg 'left)
  11026. (if (equal member org-todo-keywords-1)
  11027. nil
  11028. (if this
  11029. (nth (- (length org-todo-keywords-1)
  11030. (length tail) 2)
  11031. org-todo-keywords-1)
  11032. (org-last org-todo-keywords-1))))
  11033. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  11034. (setq arg nil))) ; hack to fall back to cycling
  11035. (arg
  11036. ;; user or caller requests a specific state
  11037. (cond
  11038. ((equal arg "") nil)
  11039. ((eq arg 'none) nil)
  11040. ((eq arg 'done) (or done-word (car org-done-keywords)))
  11041. ((eq arg 'nextset)
  11042. (or (car (cdr (member head org-todo-heads)))
  11043. (car org-todo-heads)))
  11044. ((eq arg 'previousset)
  11045. (let ((org-todo-heads (reverse org-todo-heads)))
  11046. (or (car (cdr (member head org-todo-heads)))
  11047. (car org-todo-heads))))
  11048. ((car (member arg org-todo-keywords-1)))
  11049. ((stringp arg)
  11050. (error "State `%s' not valid in this file" arg))
  11051. ((nth (1- (prefix-numeric-value arg))
  11052. org-todo-keywords-1))))
  11053. ((null member) (or head (car org-todo-keywords-1)))
  11054. ((equal this final-done-word) nil) ;; -> make empty
  11055. ((null tail) nil) ;; -> first entry
  11056. ((memq interpret '(type priority))
  11057. (if (eq this-command last-command)
  11058. (car tail)
  11059. (if (> (length tail) 0)
  11060. (or done-word (car org-done-keywords))
  11061. nil)))
  11062. (t
  11063. (car tail))))
  11064. (org-state (or
  11065. (run-hook-with-args-until-success
  11066. 'org-todo-get-default-hook org-state org-last-state)
  11067. org-state))
  11068. (next (if org-state (concat " " org-state " ") " "))
  11069. (change-plist (list :type 'todo-state-change :from this :to org-state
  11070. :position startpos))
  11071. dolog now-done-p)
  11072. (when org-blocker-hook
  11073. (setq org-last-todo-state-is-todo
  11074. (not (member this org-done-keywords)))
  11075. (unless (save-excursion
  11076. (save-match-data
  11077. (org-with-wide-buffer
  11078. (run-hook-with-args-until-failure
  11079. 'org-blocker-hook change-plist))))
  11080. (if (org-called-interactively-p 'interactive)
  11081. (user-error "TODO state change from %s to %s blocked (by \"%s\")"
  11082. this org-state org-block-entry-blocking)
  11083. ;; fail silently
  11084. (message "TODO state change from %s to %s blocked (by \"%s\")"
  11085. this org-state org-block-entry-blocking)
  11086. (throw 'exit nil))))
  11087. (store-match-data match-data)
  11088. (replace-match next t t)
  11089. (unless (pos-visible-in-window-p hl-pos)
  11090. (message "TODO state changed to %s" (org-trim next)))
  11091. (unless head
  11092. (setq head (org-get-todo-sequence-head org-state)
  11093. ass (assoc head org-todo-kwd-alist)
  11094. interpret (nth 1 ass)
  11095. done-word (nth 3 ass)
  11096. final-done-word (nth 4 ass)))
  11097. (when (memq arg '(nextset previousset))
  11098. (message "Keyword-Set %d/%d: %s"
  11099. (- (length org-todo-sets) -1
  11100. (length (memq (assoc org-state org-todo-sets) org-todo-sets)))
  11101. (length org-todo-sets)
  11102. (mapconcat 'identity (assoc org-state org-todo-sets) " ")))
  11103. (setq org-last-todo-state-is-todo
  11104. (not (member org-state org-done-keywords)))
  11105. (setq now-done-p (and (member org-state org-done-keywords)
  11106. (not (member this org-done-keywords))))
  11107. (and logging (org-local-logging logging))
  11108. (when (and (or org-todo-log-states org-log-done)
  11109. (not (eq org-inhibit-logging t))
  11110. (not (memq arg '(nextset previousset))))
  11111. ;; we need to look at recording a time and note
  11112. (setq dolog (or (nth 1 (assoc org-state org-todo-log-states))
  11113. (nth 2 (assoc this org-todo-log-states))))
  11114. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  11115. (setq dolog 'time))
  11116. (when (and org-state
  11117. (member org-state org-not-done-keywords)
  11118. (not (member this org-not-done-keywords)))
  11119. ;; This is now a todo state and was not one before
  11120. ;; If there was a CLOSED time stamp, get rid of it.
  11121. (org-add-planning-info nil nil 'closed))
  11122. (when (and now-done-p org-log-done)
  11123. ;; It is now done, and it was not done before
  11124. (org-add-planning-info 'closed (org-current-effective-time))
  11125. (if (and (not dolog) (eq 'note org-log-done))
  11126. (org-add-log-setup 'done org-state this 'findpos 'note)))
  11127. (when (and org-state dolog)
  11128. ;; This is a non-nil state, and we need to log it
  11129. (org-add-log-setup 'state org-state this 'findpos dolog)))
  11130. ;; Fixup tag positioning
  11131. (org-todo-trigger-tag-changes org-state)
  11132. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  11133. (when org-provide-todo-statistics
  11134. (org-update-parent-todo-statistics))
  11135. (run-hooks 'org-after-todo-state-change-hook)
  11136. (if (and arg (not (member org-state org-done-keywords)))
  11137. (setq head (org-get-todo-sequence-head org-state)))
  11138. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  11139. ;; Do we need to trigger a repeat?
  11140. (when now-done-p
  11141. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  11142. ;; This is for the agenda, take a snapshot of the headline.
  11143. (save-match-data
  11144. (setq org-agenda-headline-snapshot-before-repeat
  11145. (org-get-heading))))
  11146. (org-auto-repeat-maybe org-state))
  11147. ;; Fixup cursor location if close to the keyword
  11148. (if (and (outline-on-heading-p)
  11149. (not (bolp))
  11150. (save-excursion (beginning-of-line 1)
  11151. (looking-at org-todo-line-regexp))
  11152. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  11153. (progn
  11154. (goto-char (or (match-end 2) (match-end 1)))
  11155. (and (looking-at " ") (just-one-space))))
  11156. (when org-trigger-hook
  11157. (save-excursion
  11158. (run-hook-with-args 'org-trigger-hook change-plist)))
  11159. (when commentp (org-toggle-comment))))))))
  11160. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  11161. "Block turning an entry into a TODO, using the hierarchy.
  11162. This checks whether the current task should be blocked from state
  11163. changes. Such blocking occurs when:
  11164. 1. The task has children which are not all in a completed state.
  11165. 2. A task has a parent with the property :ORDERED:, and there
  11166. are siblings prior to the current task with incomplete
  11167. status.
  11168. 3. The parent of the task is blocked because it has siblings that should
  11169. be done first, or is child of a block grandparent TODO entry."
  11170. (if (not org-enforce-todo-dependencies)
  11171. t ; if locally turned off don't block
  11172. (catch 'dont-block
  11173. ;; If this is not a todo state change, or if this entry is already DONE,
  11174. ;; do not block
  11175. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11176. (member (plist-get change-plist :from)
  11177. (cons 'done org-done-keywords))
  11178. (member (plist-get change-plist :to)
  11179. (cons 'todo org-not-done-keywords))
  11180. (not (plist-get change-plist :to)))
  11181. (throw 'dont-block t))
  11182. ;; If this task has children, and any are undone, it's blocked
  11183. (save-excursion
  11184. (org-back-to-heading t)
  11185. (let ((this-level (funcall outline-level)))
  11186. (outline-next-heading)
  11187. (let ((child-level (funcall outline-level)))
  11188. (while (and (not (eobp))
  11189. (> child-level this-level))
  11190. ;; this todo has children, check whether they are all
  11191. ;; completed
  11192. (if (and (not (org-entry-is-done-p))
  11193. (org-entry-is-todo-p))
  11194. (progn (setq org-block-entry-blocking (org-get-heading))
  11195. (throw 'dont-block nil)))
  11196. (outline-next-heading)
  11197. (setq child-level (funcall outline-level))))))
  11198. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  11199. ;; any previous siblings are undone, it's blocked
  11200. (save-excursion
  11201. (org-back-to-heading t)
  11202. (let* ((pos (point))
  11203. (parent-pos (and (org-up-heading-safe) (point))))
  11204. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  11205. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11206. (forward-line 1)
  11207. (re-search-forward org-not-done-heading-regexp pos t))
  11208. (setq org-block-entry-blocking (match-string 0))
  11209. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  11210. ;; Search further up the hierarchy, to see if an ancestor is blocked
  11211. (while t
  11212. (goto-char parent-pos)
  11213. (if (not (looking-at org-not-done-heading-regexp))
  11214. (throw 'dont-block t)) ; do not block, parent is not a TODO
  11215. (setq pos (point))
  11216. (setq parent-pos (and (org-up-heading-safe) (point)))
  11217. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  11218. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11219. (forward-line 1)
  11220. (re-search-forward org-not-done-heading-regexp pos t)
  11221. (setq org-block-entry-blocking (org-get-heading)))
  11222. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  11223. (defcustom org-track-ordered-property-with-tag nil
  11224. "Should the ORDERED property also be shown as a tag?
  11225. The ORDERED property decides if an entry should require subtasks to be
  11226. completed in sequence. Since a property is not very visible, setting
  11227. this option means that toggling the ORDERED property with the command
  11228. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  11229. not relevant for the behavior, but it makes things more visible.
  11230. Note that toggling the tag with tags commands will not change the property
  11231. and therefore not influence behavior!
  11232. This can be t, meaning the tag ORDERED should be used, It can also be a
  11233. string to select a different tag for this task."
  11234. :group 'org-todo
  11235. :type '(choice
  11236. (const :tag "No tracking" nil)
  11237. (const :tag "Track with ORDERED tag" t)
  11238. (string :tag "Use other tag")))
  11239. (defun org-toggle-ordered-property ()
  11240. "Toggle the ORDERED property of the current entry.
  11241. For better visibility, you can track the value of this property with a tag.
  11242. See variable `org-track-ordered-property-with-tag'."
  11243. (interactive)
  11244. (let* ((t1 org-track-ordered-property-with-tag)
  11245. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  11246. (save-excursion
  11247. (org-back-to-heading)
  11248. (if (org-entry-get nil "ORDERED")
  11249. (progn
  11250. (org-delete-property "ORDERED" "PROPERTIES")
  11251. (and tag (org-toggle-tag tag 'off))
  11252. (message "Subtasks can be completed in arbitrary order"))
  11253. (org-entry-put nil "ORDERED" "t")
  11254. (and tag (org-toggle-tag tag 'on))
  11255. (message "Subtasks must be completed in sequence")))))
  11256. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  11257. (defun org-block-todo-from-checkboxes (change-plist)
  11258. "Block turning an entry into a TODO, using checkboxes.
  11259. This checks whether the current task should be blocked from state
  11260. changes because there are unchecked boxes in this entry."
  11261. (if (not org-enforce-todo-checkbox-dependencies)
  11262. t ; if locally turned off don't block
  11263. (catch 'dont-block
  11264. ;; If this is not a todo state change, or if this entry is already DONE,
  11265. ;; do not block
  11266. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11267. (member (plist-get change-plist :from)
  11268. (cons 'done org-done-keywords))
  11269. (member (plist-get change-plist :to)
  11270. (cons 'todo org-not-done-keywords))
  11271. (not (plist-get change-plist :to)))
  11272. (throw 'dont-block t))
  11273. ;; If this task has checkboxes that are not checked, it's blocked
  11274. (save-excursion
  11275. (org-back-to-heading t)
  11276. (let ((beg (point)) end)
  11277. (outline-next-heading)
  11278. (setq end (point))
  11279. (goto-char beg)
  11280. (if (org-list-search-forward
  11281. (concat (org-item-beginning-re)
  11282. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
  11283. "\\[[- ]\\]")
  11284. end t)
  11285. (progn
  11286. (if (boundp 'org-blocked-by-checkboxes)
  11287. (setq org-blocked-by-checkboxes t))
  11288. (throw 'dont-block nil)))))
  11289. t))) ; do not block
  11290. (defun org-entry-blocked-p ()
  11291. "Is the current entry blocked?"
  11292. (org-with-silent-modifications
  11293. (if (org-entry-get nil "NOBLOCKING")
  11294. nil ;; Never block this entry
  11295. (not (run-hook-with-args-until-failure
  11296. 'org-blocker-hook
  11297. (list :type 'todo-state-change
  11298. :position (point)
  11299. :from 'todo
  11300. :to 'done))))))
  11301. (defun org-update-statistics-cookies (all)
  11302. "Update the statistics cookie, either from TODO or from checkboxes.
  11303. This should be called with the cursor in a line with a statistics cookie."
  11304. (interactive "P")
  11305. (if all
  11306. (progn
  11307. (org-update-checkbox-count 'all)
  11308. (org-map-entries 'org-update-parent-todo-statistics))
  11309. (if (not (org-at-heading-p))
  11310. (org-update-checkbox-count)
  11311. (let ((pos (point-marker))
  11312. end l1 l2)
  11313. (ignore-errors (org-back-to-heading t))
  11314. (if (not (org-at-heading-p))
  11315. (org-update-checkbox-count)
  11316. (setq l1 (org-outline-level))
  11317. (setq end (save-excursion
  11318. (outline-next-heading)
  11319. (if (org-at-heading-p) (setq l2 (org-outline-level)))
  11320. (point)))
  11321. (if (and (save-excursion
  11322. (re-search-forward
  11323. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  11324. (not (save-excursion (re-search-forward
  11325. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  11326. (org-update-checkbox-count)
  11327. (if (and l2 (> l2 l1))
  11328. (progn
  11329. (goto-char end)
  11330. (org-update-parent-todo-statistics))
  11331. (goto-char pos)
  11332. (beginning-of-line 1)
  11333. (while (re-search-forward
  11334. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  11335. (point-at-eol) t)
  11336. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  11337. (goto-char pos)
  11338. (move-marker pos nil)))))
  11339. (defvar org-entry-property-inherited-from) ;; defined below
  11340. (defun org-update-parent-todo-statistics ()
  11341. "Update any statistics cookie in the parent of the current headline.
  11342. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  11343. the entire subtree and this will travel up the hierarchy and update
  11344. statistics everywhere."
  11345. (let* ((prop (save-excursion (org-up-heading-safe)
  11346. (org-entry-get nil "COOKIE_DATA" 'inherit)))
  11347. (recursive (or (not org-hierarchical-todo-statistics)
  11348. (and prop (string-match "\\<recursive\\>" prop))))
  11349. (lim (or (and prop (marker-position org-entry-property-inherited-from))
  11350. 0))
  11351. (first t)
  11352. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  11353. level ltoggle l1 new ndel
  11354. (cnt-all 0) (cnt-done 0) is-percent kwd
  11355. checkbox-beg ov ovs ove cookie-present)
  11356. (catch 'exit
  11357. (save-excursion
  11358. (beginning-of-line 1)
  11359. (setq ltoggle (funcall outline-level))
  11360. ;; Three situations are to consider:
  11361. ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
  11362. ;; to the top-level ancestor on the headline;
  11363. ;; 2. If parent has "recursive" property, repeat up to the
  11364. ;; headline setting that property, taking inheritance into
  11365. ;; account;
  11366. ;; 3. Else, move up to direct parent and proceed only once.
  11367. (while (and (setq level (org-up-heading-safe))
  11368. (or recursive first)
  11369. (>= (point) lim))
  11370. (setq first nil cookie-present nil)
  11371. (unless (and level
  11372. (not (string-match
  11373. "\\<checkbox\\>"
  11374. (downcase (or (org-entry-get nil "COOKIE_DATA")
  11375. "")))))
  11376. (throw 'exit nil))
  11377. (while (re-search-forward box-re (point-at-eol) t)
  11378. (setq cnt-all 0 cnt-done 0 cookie-present t)
  11379. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  11380. (save-match-data
  11381. (unless (outline-next-heading) (throw 'exit nil))
  11382. (while (and (looking-at org-complex-heading-regexp)
  11383. (> (setq l1 (length (match-string 1))) level))
  11384. (setq kwd (and (or recursive (= l1 ltoggle))
  11385. (match-string 2)))
  11386. (if (or (eq org-provide-todo-statistics 'all-headlines)
  11387. (and (listp org-provide-todo-statistics)
  11388. (or (member kwd org-provide-todo-statistics)
  11389. (member kwd org-done-keywords))))
  11390. (setq cnt-all (1+ cnt-all))
  11391. (if (eq org-provide-todo-statistics t)
  11392. (and kwd (setq cnt-all (1+ cnt-all)))))
  11393. (and (member kwd org-done-keywords)
  11394. (setq cnt-done (1+ cnt-done)))
  11395. (outline-next-heading)))
  11396. (setq new
  11397. (if is-percent
  11398. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  11399. (format "[%d/%d]" cnt-done cnt-all))
  11400. ndel (- (match-end 0) checkbox-beg))
  11401. ;; handle overlays when updating cookie from column view
  11402. (when (setq ov (car (overlays-at checkbox-beg)))
  11403. (setq ovs (overlay-start ov) ove (overlay-end ov))
  11404. (delete-overlay ov))
  11405. (goto-char checkbox-beg)
  11406. (insert new)
  11407. (delete-region (point) (+ (point) ndel))
  11408. (when org-auto-align-tags (org-fix-tags-on-the-fly))
  11409. (when ov (move-overlay ov ovs ove)))
  11410. (when cookie-present
  11411. (run-hook-with-args 'org-after-todo-statistics-hook
  11412. cnt-done (- cnt-all cnt-done))))))
  11413. (run-hooks 'org-todo-statistics-hook)))
  11414. (defvar org-after-todo-statistics-hook nil
  11415. "Hook that is called after a TODO statistics cookie has been updated.
  11416. Each function is called with two arguments: the number of not-done entries
  11417. and the number of done entries.
  11418. For example, the following function, when added to this hook, will switch
  11419. an entry to DONE when all children are done, and back to TODO when new
  11420. entries are set to a TODO status. Note that this hook is only called
  11421. when there is a statistics cookie in the headline!
  11422. (defun org-summary-todo (n-done n-not-done)
  11423. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  11424. (let (org-log-done org-log-states) ; turn off logging
  11425. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  11426. ")
  11427. (defvar org-todo-statistics-hook nil
  11428. "Hook that is run whenever Org thinks TODO statistics should be updated.
  11429. This hook runs even if there is no statistics cookie present, in which case
  11430. `org-after-todo-statistics-hook' would not run.")
  11431. (defun org-todo-trigger-tag-changes (state)
  11432. "Apply the changes defined in `org-todo-state-tags-triggers'."
  11433. (let ((l org-todo-state-tags-triggers)
  11434. changes)
  11435. (when (or (not state) (equal state ""))
  11436. (setq changes (append changes (cdr (assoc "" l)))))
  11437. (when (and (stringp state) (> (length state) 0))
  11438. (setq changes (append changes (cdr (assoc state l)))))
  11439. (when (member state org-not-done-keywords)
  11440. (setq changes (append changes (cdr (assoc 'todo l)))))
  11441. (when (member state org-done-keywords)
  11442. (setq changes (append changes (cdr (assoc 'done l)))))
  11443. (dolist (c changes)
  11444. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  11445. (defun org-local-logging (value)
  11446. "Get logging settings from a property VALUE."
  11447. (let* (words w a)
  11448. ;; directly set the variables, they are already local.
  11449. (setq org-log-done nil
  11450. org-log-repeat nil
  11451. org-todo-log-states nil)
  11452. (setq words (org-split-string value))
  11453. (while (setq w (pop words))
  11454. (cond
  11455. ((setq a (assoc w org-startup-options))
  11456. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  11457. (set (nth 1 a) (nth 2 a))))
  11458. ((setq a (org-extract-log-state-settings w))
  11459. (and (member (car a) org-todo-keywords-1)
  11460. (push a org-todo-log-states)))))))
  11461. (defun org-get-todo-sequence-head (kwd)
  11462. "Return the head of the TODO sequence to which KWD belongs.
  11463. If KWD is not set, check if there is a text property remembering the
  11464. right sequence."
  11465. (let (p)
  11466. (cond
  11467. ((not kwd)
  11468. (or (get-text-property (point-at-bol) 'org-todo-head)
  11469. (progn
  11470. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  11471. nil (point-at-eol)))
  11472. (get-text-property p 'org-todo-head))))
  11473. ((not (member kwd org-todo-keywords-1))
  11474. (car org-todo-keywords-1))
  11475. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  11476. (defun org-fast-todo-selection ()
  11477. "Fast TODO keyword selection with single keys.
  11478. Returns the new TODO keyword, or nil if no state change should occur."
  11479. (let* ((fulltable org-todo-key-alist)
  11480. (done-keywords org-done-keywords) ;; needed for the faces.
  11481. (maxlen (apply 'max (mapcar
  11482. (lambda (x)
  11483. (if (stringp (car x)) (string-width (car x)) 0))
  11484. fulltable)))
  11485. (expert nil)
  11486. (fwidth (+ maxlen 3 1 3))
  11487. (ncol (/ (- (window-width) 4) fwidth))
  11488. tg cnt e c tbl
  11489. groups ingroup)
  11490. (save-excursion
  11491. (save-window-excursion
  11492. (if expert
  11493. (set-buffer (get-buffer-create " *Org todo*"))
  11494. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  11495. (erase-buffer)
  11496. (org-set-local 'org-done-keywords done-keywords)
  11497. (setq tbl fulltable cnt 0)
  11498. (while (setq e (pop tbl))
  11499. (cond
  11500. ((equal e '(:startgroup))
  11501. (push '() groups) (setq ingroup t)
  11502. (when (not (= cnt 0))
  11503. (setq cnt 0)
  11504. (insert "\n"))
  11505. (insert "{ "))
  11506. ((equal e '(:endgroup))
  11507. (setq ingroup nil cnt 0)
  11508. (insert "}\n"))
  11509. ((equal e '(:newline))
  11510. (when (not (= cnt 0))
  11511. (setq cnt 0)
  11512. (insert "\n")
  11513. (setq e (car tbl))
  11514. (while (equal (car tbl) '(:newline))
  11515. (insert "\n")
  11516. (setq tbl (cdr tbl)))))
  11517. (t
  11518. (setq tg (car e) c (cdr e))
  11519. (if ingroup (push tg (car groups)))
  11520. (setq tg (org-add-props tg nil 'face
  11521. (org-get-todo-face tg)))
  11522. (if (and (= cnt 0) (not ingroup)) (insert " "))
  11523. (insert "[" c "] " tg (make-string
  11524. (- fwidth 4 (length tg)) ?\ ))
  11525. (when (= (setq cnt (1+ cnt)) ncol)
  11526. (insert "\n")
  11527. (if ingroup (insert " "))
  11528. (setq cnt 0)))))
  11529. (insert "\n")
  11530. (goto-char (point-min))
  11531. (if (not expert) (org-fit-window-to-buffer))
  11532. (message "[a-z..]:Set [SPC]:clear")
  11533. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  11534. (cond
  11535. ((or (= c ?\C-g)
  11536. (and (= c ?q) (not (rassoc c fulltable))))
  11537. (setq quit-flag t))
  11538. ((= c ?\ ) nil)
  11539. ((setq e (rassoc c fulltable) tg (car e))
  11540. tg)
  11541. (t (setq quit-flag t)))))))
  11542. (defun org-entry-is-todo-p ()
  11543. (member (org-get-todo-state) org-not-done-keywords))
  11544. (defun org-entry-is-done-p ()
  11545. (member (org-get-todo-state) org-done-keywords))
  11546. (defun org-get-todo-state ()
  11547. "Return the TODO keyword of the current subtree."
  11548. (save-excursion
  11549. (org-back-to-heading t)
  11550. (and (looking-at org-todo-line-regexp)
  11551. (match-end 2)
  11552. (match-string 2))))
  11553. (defun org-at-date-range-p (&optional inactive-ok)
  11554. "Is the cursor inside a date range?"
  11555. (interactive)
  11556. (save-excursion
  11557. (catch 'exit
  11558. (let ((pos (point)))
  11559. (skip-chars-backward "^[<\r\n")
  11560. (skip-chars-backward "<[")
  11561. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11562. (>= (match-end 0) pos)
  11563. (throw 'exit t))
  11564. (skip-chars-backward "^<[\r\n")
  11565. (skip-chars-backward "<[")
  11566. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11567. (>= (match-end 0) pos)
  11568. (throw 'exit t)))
  11569. nil)))
  11570. (defun org-get-repeat (&optional tagline)
  11571. "Check if there is a deadline/schedule with repeater in this entry."
  11572. (save-match-data
  11573. (save-excursion
  11574. (org-back-to-heading t)
  11575. (and (re-search-forward (if tagline
  11576. (concat tagline "\\s-*" org-repeat-re)
  11577. org-repeat-re)
  11578. (org-entry-end-position) t)
  11579. (match-string-no-properties 1)))))
  11580. (defvar org-last-changed-timestamp)
  11581. (defvar org-last-inserted-timestamp)
  11582. (defvar org-log-post-message)
  11583. (defvar org-log-note-purpose)
  11584. (defvar org-log-note-how)
  11585. (defvar org-log-note-extra)
  11586. (defun org-auto-repeat-maybe (done-word)
  11587. "Check if the current headline contains a repeated deadline/schedule.
  11588. If yes, set TODO state back to what it was and change the base date
  11589. of repeating deadline/scheduled time stamps to new date.
  11590. This function is run automatically after each state change to a DONE state."
  11591. ;; last-state is dynamically scoped into this function
  11592. (let* ((repeat (org-get-repeat))
  11593. (aa (assoc org-last-state org-todo-kwd-alist))
  11594. (interpret (nth 1 aa))
  11595. (head (nth 2 aa))
  11596. (whata '(("h" . hour) ("d" . day) ("m" . month) ("y" . year)))
  11597. (msg "Entry repeats: ")
  11598. (org-log-done nil)
  11599. (org-todo-log-states nil)
  11600. re type n what ts time to-state)
  11601. (when repeat
  11602. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  11603. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  11604. org-todo-repeat-to-state))
  11605. (unless (and to-state (member to-state org-todo-keywords-1))
  11606. (setq to-state (if (eq interpret 'type) org-last-state head)))
  11607. (org-todo to-state)
  11608. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  11609. (org-entry-put nil "LAST_REPEAT" (format-time-string
  11610. (org-time-stamp-format t t))))
  11611. (when org-log-repeat
  11612. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  11613. (memq 'org-add-log-note post-command-hook))
  11614. ;; OK, we are already setup for some record
  11615. (if (eq org-log-repeat 'note)
  11616. ;; make sure we take a note, not only a time stamp
  11617. (setq org-log-note-how 'note))
  11618. ;; Set up for taking a record
  11619. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  11620. org-last-state
  11621. 'findpos org-log-repeat)))
  11622. (org-back-to-heading t)
  11623. (org-add-planning-info nil nil 'closed)
  11624. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11625. org-deadline-time-regexp "\\)\\|\\("
  11626. org-ts-regexp "\\)"))
  11627. (while (re-search-forward
  11628. re (save-excursion (outline-next-heading) (point)) t)
  11629. (setq type (if (match-end 1) org-scheduled-string
  11630. (if (match-end 3) org-deadline-string "Plain:"))
  11631. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  11632. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts)
  11633. (setq n (string-to-number (match-string 2 ts))
  11634. what (match-string 3 ts))
  11635. (if (equal what "w") (setq n (* n 7) what "d"))
  11636. (if (and (equal what "h") (not (string-match "[0-9]\\{1,2\\}:[0-9]\\{2\\}" ts)))
  11637. (error "Cannot repeat in Repeat in %d hour(s) because no hour has been set" n))
  11638. ;; Preparation, see if we need to modify the start date for the change
  11639. (when (match-end 1)
  11640. (setq time (save-match-data (org-time-string-to-time ts)))
  11641. (cond
  11642. ((equal (match-string 1 ts) ".")
  11643. ;; Shift starting date to today
  11644. (org-timestamp-change
  11645. (- (org-today) (time-to-days time))
  11646. 'day))
  11647. ((equal (match-string 1 ts) "+")
  11648. (let ((nshiftmax 10) (nshift 0))
  11649. (while (or (= nshift 0)
  11650. (<= (time-to-days time)
  11651. (time-to-days (current-time))))
  11652. (when (= (incf nshift) nshiftmax)
  11653. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  11654. (error "Abort")))
  11655. (org-timestamp-change n (cdr (assoc what whata)))
  11656. (org-at-timestamp-p t)
  11657. (setq ts (match-string 1))
  11658. (setq time (save-match-data (org-time-string-to-time ts)))))
  11659. (org-timestamp-change (- n) (cdr (assoc what whata)))
  11660. ;; rematch, so that we have everything in place for the real shift
  11661. (org-at-timestamp-p t)
  11662. (setq ts (match-string 1))
  11663. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts))))
  11664. (save-excursion (org-timestamp-change n (cdr (assoc what whata)) nil t))
  11665. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  11666. (setq org-log-post-message msg)
  11667. (message "%s" msg))))
  11668. (defun org-show-todo-tree (arg)
  11669. "Make a compact tree which shows all headlines marked with TODO.
  11670. The tree will show the lines where the regexp matches, and all higher
  11671. headlines above the match.
  11672. With a \\[universal-argument] prefix, prompt for a regexp to match.
  11673. With a numeric prefix N, construct a sparse tree for the Nth element
  11674. of `org-todo-keywords-1'."
  11675. (interactive "P")
  11676. (let ((case-fold-search nil)
  11677. (kwd-re
  11678. (cond ((null arg) org-not-done-regexp)
  11679. ((equal arg '(4))
  11680. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  11681. (mapcar 'list org-todo-keywords-1))))
  11682. (concat "\\("
  11683. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  11684. "\\)\\>")))
  11685. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  11686. (regexp-quote (nth (1- (prefix-numeric-value arg))
  11687. org-todo-keywords-1)))
  11688. (t (error "Invalid prefix argument: %s" arg)))))
  11689. (message "%d TODO entries found"
  11690. (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
  11691. (defun org-deadline (arg &optional time)
  11692. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  11693. With one universal prefix argument, remove any deadline from the item.
  11694. With two universal prefix arguments, prompt for a warning delay.
  11695. With argument TIME, set the deadline at the corresponding date. TIME
  11696. can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11697. (interactive "P")
  11698. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11699. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11700. 'region-start-level 'region))
  11701. org-loop-over-headlines-in-active-region)
  11702. (org-map-entries
  11703. `(org-deadline ',arg ,time)
  11704. org-loop-over-headlines-in-active-region
  11705. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11706. (let* ((old-date (org-entry-get nil "DEADLINE"))
  11707. (old-date-time (if old-date (org-time-string-to-time old-date)))
  11708. (repeater (and old-date
  11709. (string-match
  11710. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11711. old-date)
  11712. (match-string 1 old-date))))
  11713. (cond
  11714. ((equal arg '(4))
  11715. (when (and old-date org-log-redeadline)
  11716. (org-add-log-setup 'deldeadline nil old-date 'findpos
  11717. org-log-redeadline))
  11718. (org-remove-timestamp-with-keyword org-deadline-string)
  11719. (message "Item no longer has a deadline."))
  11720. ((equal arg '(16))
  11721. (save-excursion
  11722. (if (re-search-forward
  11723. org-deadline-time-regexp
  11724. (save-excursion (outline-next-heading) (point)) t)
  11725. (let* ((rpl0 (match-string 1))
  11726. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  11727. (replace-match
  11728. (concat org-deadline-string
  11729. " <" rpl
  11730. (format " -%dd"
  11731. (abs
  11732. (- (time-to-days
  11733. (save-match-data
  11734. (org-read-date nil t nil "Warn starting from" old-date-time)))
  11735. (time-to-days old-date-time))))
  11736. ">") t t))
  11737. (user-error "No deadline information to update"))))
  11738. (t
  11739. (org-add-planning-info 'deadline time 'closed)
  11740. (when (and old-date org-log-redeadline
  11741. (not (equal old-date
  11742. (substring org-last-inserted-timestamp 1 -1))))
  11743. (org-add-log-setup 'redeadline nil old-date 'findpos
  11744. org-log-redeadline))
  11745. (when repeater
  11746. (save-excursion
  11747. (org-back-to-heading t)
  11748. (when (re-search-forward (concat org-deadline-string " "
  11749. org-last-inserted-timestamp)
  11750. (save-excursion
  11751. (outline-next-heading) (point)) t)
  11752. (goto-char (1- (match-end 0)))
  11753. (insert " " repeater)
  11754. (setq org-last-inserted-timestamp
  11755. (concat (substring org-last-inserted-timestamp 0 -1)
  11756. " " repeater
  11757. (substring org-last-inserted-timestamp -1))))))
  11758. (message "Deadline on %s" org-last-inserted-timestamp))))))
  11759. (defun org-schedule (arg &optional time)
  11760. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  11761. With one universal prefix argument, remove any scheduling date from the item.
  11762. With two universal prefix arguments, prompt for a delay cookie.
  11763. With argument TIME, scheduled at the corresponding date. TIME can
  11764. either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11765. (interactive "P")
  11766. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11767. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11768. 'region-start-level 'region))
  11769. org-loop-over-headlines-in-active-region)
  11770. (org-map-entries
  11771. `(org-schedule ',arg ,time)
  11772. org-loop-over-headlines-in-active-region
  11773. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11774. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  11775. (old-date-time (if old-date (org-time-string-to-time old-date)))
  11776. (repeater (and old-date
  11777. (string-match
  11778. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11779. old-date)
  11780. (match-string 1 old-date))))
  11781. (cond
  11782. ((equal arg '(4))
  11783. (progn
  11784. (when (and old-date org-log-reschedule)
  11785. (org-add-log-setup 'delschedule nil old-date 'findpos
  11786. org-log-reschedule))
  11787. (org-remove-timestamp-with-keyword org-scheduled-string)
  11788. (message "Item is no longer scheduled.")))
  11789. ((equal arg '(16))
  11790. (save-excursion
  11791. (if (re-search-forward
  11792. org-scheduled-time-regexp
  11793. (save-excursion (outline-next-heading) (point)) t)
  11794. (let* ((rpl0 (match-string 1))
  11795. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  11796. (replace-match
  11797. (concat org-scheduled-string
  11798. " <" rpl
  11799. (format " -%dd"
  11800. (abs
  11801. (- (time-to-days
  11802. (save-match-data
  11803. (org-read-date nil t nil "Delay until" old-date-time)))
  11804. (time-to-days old-date-time))))
  11805. ">") t t))
  11806. (user-error "No scheduled information to update"))))
  11807. (t
  11808. (org-add-planning-info 'scheduled time 'closed)
  11809. (when (and old-date org-log-reschedule
  11810. (not (equal old-date
  11811. (substring org-last-inserted-timestamp 1 -1))))
  11812. (org-add-log-setup 'reschedule nil old-date 'findpos
  11813. org-log-reschedule))
  11814. (when repeater
  11815. (save-excursion
  11816. (org-back-to-heading t)
  11817. (when (re-search-forward (concat org-scheduled-string " "
  11818. org-last-inserted-timestamp)
  11819. (save-excursion
  11820. (outline-next-heading) (point)) t)
  11821. (goto-char (1- (match-end 0)))
  11822. (insert " " repeater)
  11823. (setq org-last-inserted-timestamp
  11824. (concat (substring org-last-inserted-timestamp 0 -1)
  11825. " " repeater
  11826. (substring org-last-inserted-timestamp -1))))))
  11827. (message "Scheduled to %s" org-last-inserted-timestamp))))))
  11828. (defun org-get-scheduled-time (pom &optional inherit)
  11829. "Get the scheduled time as a time tuple, of a format suitable
  11830. for calling org-schedule with, or if there is no scheduling,
  11831. returns nil."
  11832. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  11833. (when time
  11834. (apply 'encode-time (org-parse-time-string time)))))
  11835. (defun org-get-deadline-time (pom &optional inherit)
  11836. "Get the deadline as a time tuple, of a format suitable for
  11837. calling org-deadline with, or if there is no scheduling, returns
  11838. nil."
  11839. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  11840. (when time
  11841. (apply 'encode-time (org-parse-time-string time)))))
  11842. (defun org-remove-timestamp-with-keyword (keyword)
  11843. "Remove all time stamps with KEYWORD in the current entry."
  11844. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  11845. beg)
  11846. (save-excursion
  11847. (org-back-to-heading t)
  11848. (setq beg (point))
  11849. (outline-next-heading)
  11850. (while (re-search-backward re beg t)
  11851. (replace-match "")
  11852. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  11853. (equal (char-before) ?\ ))
  11854. (backward-delete-char 1)
  11855. (if (string-match "^[ \t]*$" (buffer-substring
  11856. (point-at-bol) (point-at-eol)))
  11857. (delete-region (point-at-bol)
  11858. (min (point-max) (1+ (point-at-eol))))))))))
  11859. (defun org-add-planning-info (what &optional time &rest remove)
  11860. "Insert new timestamp with keyword in the line directly after the headline.
  11861. WHAT indicates what kind of time stamp to add. TIME indicates the time to use.
  11862. If non is given, the user is prompted for a date.
  11863. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  11864. be removed."
  11865. (interactive)
  11866. (let (org-time-was-given org-end-time-was-given ts
  11867. end default-time default-input)
  11868. (catch 'exit
  11869. (when (and (memq what '(scheduled deadline))
  11870. (or (not time)
  11871. (and (stringp time)
  11872. (string-match "^[-+]+[0-9]" time))))
  11873. ;; Try to get a default date/time from existing timestamp
  11874. (save-excursion
  11875. (org-back-to-heading t)
  11876. (setq end (save-excursion (outline-next-heading) (point)))
  11877. (when (re-search-forward (if (eq what 'scheduled)
  11878. org-scheduled-time-regexp
  11879. org-deadline-time-regexp)
  11880. end t)
  11881. (setq ts (match-string 1)
  11882. default-time
  11883. (apply 'encode-time (org-parse-time-string ts))
  11884. default-input (and ts (org-get-compact-tod ts))))))
  11885. (when what
  11886. (setq time
  11887. (if (stringp time)
  11888. ;; This is a string (relative or absolute), set proper date
  11889. (apply 'encode-time
  11890. (org-read-date-analyze
  11891. time default-time (decode-time default-time)))
  11892. ;; If necessary, get the time from the user
  11893. (or time (org-read-date nil 'to-time nil nil
  11894. default-time default-input)))))
  11895. (when (and org-insert-labeled-timestamps-at-point
  11896. (member what '(scheduled deadline)))
  11897. (insert
  11898. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  11899. (org-insert-time-stamp time org-time-was-given
  11900. nil nil nil (list org-end-time-was-given))
  11901. (setq what nil))
  11902. (save-excursion
  11903. (save-restriction
  11904. (let (col list elt ts buffer-invisibility-spec)
  11905. (org-back-to-heading t)
  11906. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"))
  11907. (goto-char (match-end 1))
  11908. (setq col (current-column))
  11909. (goto-char (match-end 0))
  11910. (if (eobp) (insert "\n") (forward-char 1))
  11911. (when (and (not what)
  11912. (not (looking-at
  11913. (concat "[ \t]*"
  11914. org-keyword-time-not-clock-regexp))))
  11915. ;; Nothing to add, nothing to remove...... :-)
  11916. (throw 'exit nil))
  11917. (if (and (not (looking-at org-outline-regexp))
  11918. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  11919. "[^\r\n]*"))
  11920. (not (equal (match-string 1) org-clock-string)))
  11921. (narrow-to-region (match-beginning 0) (match-end 0))
  11922. (insert-before-markers "\n")
  11923. (backward-char 1)
  11924. (narrow-to-region (point) (point))
  11925. (and org-adapt-indentation (org-indent-to-column col)))
  11926. ;; Check if we have to remove something.
  11927. (setq list (cons what remove))
  11928. (while list
  11929. (setq elt (pop list))
  11930. (when (or (and (eq elt 'scheduled)
  11931. (re-search-forward org-scheduled-time-regexp nil t))
  11932. (and (eq elt 'deadline)
  11933. (re-search-forward org-deadline-time-regexp nil t))
  11934. (and (eq elt 'closed)
  11935. (re-search-forward org-closed-time-regexp nil t)))
  11936. (replace-match "")
  11937. (if (looking-at "--+<[^>]+>") (replace-match ""))))
  11938. (and (looking-at "[ \t]+") (replace-match ""))
  11939. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  11940. (when what
  11941. (insert
  11942. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  11943. (cond ((eq what 'scheduled) org-scheduled-string)
  11944. ((eq what 'deadline) org-deadline-string)
  11945. ((eq what 'closed) org-closed-string))
  11946. " ")
  11947. (setq ts (org-insert-time-stamp
  11948. time
  11949. (or org-time-was-given
  11950. (and (eq what 'closed) org-log-done-with-time))
  11951. (eq what 'closed)
  11952. nil nil (list org-end-time-was-given)))
  11953. (insert
  11954. (if (not (or (bolp) (eq (char-before) ?\ )
  11955. (memq (char-after) '(32 10))
  11956. (eobp))) " " ""))
  11957. (end-of-line 1))
  11958. (goto-char (point-min))
  11959. (widen)
  11960. (if (and (looking-at "[ \t]*\n")
  11961. (equal (char-before) ?\n))
  11962. (delete-region (1- (point)) (point-at-eol)))
  11963. ts))))))
  11964. (defvar org-log-note-marker (make-marker))
  11965. (defvar org-log-note-purpose nil)
  11966. (defvar org-log-note-state nil)
  11967. (defvar org-log-note-previous-state nil)
  11968. (defvar org-log-note-how nil)
  11969. (defvar org-log-note-extra nil)
  11970. (defvar org-log-note-window-configuration nil)
  11971. (defvar org-log-note-return-to (make-marker))
  11972. (defvar org-log-note-effective-time nil
  11973. "Remembered current time so that dynamically scoped
  11974. `org-extend-today-until' affects tha timestamps in state change
  11975. log")
  11976. (defvar org-log-post-message nil
  11977. "Message to be displayed after a log note has been stored.
  11978. The auto-repeater uses this.")
  11979. (defun org-add-note ()
  11980. "Add a note to the current entry.
  11981. This is done in the same way as adding a state change note."
  11982. (interactive)
  11983. (org-add-log-setup 'note nil nil 'findpos nil))
  11984. (defvar org-property-end-re)
  11985. (defun org-add-log-setup (&optional purpose state prev-state
  11986. findpos how extra)
  11987. "Set up the post command hook to take a note.
  11988. If this is about to TODO state change, the new state is expected in STATE.
  11989. When FINDPOS is non-nil, find the correct position for the note in
  11990. the current entry. If not, assume that it can be inserted at point.
  11991. HOW is an indicator what kind of note should be created.
  11992. EXTRA is additional text that will be inserted into the notes buffer."
  11993. (let* ((org-log-into-drawer (org-log-into-drawer))
  11994. (drawer (cond ((stringp org-log-into-drawer)
  11995. org-log-into-drawer)
  11996. (org-log-into-drawer "LOGBOOK"))))
  11997. (save-restriction
  11998. (save-excursion
  11999. (when findpos
  12000. (org-back-to-heading t)
  12001. (narrow-to-region (point) (save-excursion
  12002. (outline-next-heading) (point)))
  12003. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"
  12004. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  12005. "[^\r\n]*\\)?"))
  12006. (goto-char (match-end 0))
  12007. (cond
  12008. (drawer
  12009. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  12010. nil t)
  12011. (progn
  12012. (goto-char (match-end 0))
  12013. (or org-log-states-order-reversed
  12014. (and (re-search-forward org-property-end-re nil t)
  12015. (goto-char (1- (match-beginning 0))))))
  12016. (insert "\n:" drawer ":\n:END:")
  12017. (beginning-of-line 0)
  12018. (org-indent-line)
  12019. (beginning-of-line 2)
  12020. (org-indent-line)
  12021. (end-of-line 0)))
  12022. ((and org-log-state-notes-insert-after-drawers
  12023. (save-excursion
  12024. (forward-line) (looking-at org-drawer-regexp)))
  12025. (forward-line)
  12026. (while (looking-at org-drawer-regexp)
  12027. (goto-char (match-end 0))
  12028. (re-search-forward org-property-end-re (point-max) t)
  12029. (forward-line))
  12030. (forward-line -1)))
  12031. (unless org-log-states-order-reversed
  12032. (and (= (char-after) ?\n) (forward-char 1))
  12033. (org-skip-over-state-notes)
  12034. (skip-chars-backward " \t\n\r")))
  12035. (move-marker org-log-note-marker (point))
  12036. (setq org-log-note-purpose purpose
  12037. org-log-note-state state
  12038. org-log-note-previous-state prev-state
  12039. org-log-note-how how
  12040. org-log-note-extra extra
  12041. org-log-note-effective-time (org-current-effective-time))
  12042. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  12043. (defun org-skip-over-state-notes ()
  12044. "Skip past the list of State notes in an entry."
  12045. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  12046. (when (ignore-errors (goto-char (org-in-item-p)))
  12047. (let* ((struct (org-list-struct))
  12048. (prevs (org-list-prevs-alist struct)))
  12049. (while (looking-at "[ \t]*- State")
  12050. (goto-char (or (org-list-get-next-item (point) struct prevs)
  12051. (org-list-get-item-end (point) struct)))))))
  12052. (defun org-add-log-note (&optional purpose)
  12053. "Pop up a window for taking a note, and add this note later at point."
  12054. (remove-hook 'post-command-hook 'org-add-log-note)
  12055. (setq org-log-note-window-configuration (current-window-configuration))
  12056. (delete-other-windows)
  12057. (move-marker org-log-note-return-to (point))
  12058. (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
  12059. (goto-char org-log-note-marker)
  12060. (org-switch-to-buffer-other-window "*Org Note*")
  12061. (erase-buffer)
  12062. (if (memq org-log-note-how '(time state))
  12063. (let (current-prefix-arg) (org-store-log-note))
  12064. (let ((org-inhibit-startup t)) (org-mode))
  12065. (insert (format "# Insert note for %s.
  12066. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  12067. (cond
  12068. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  12069. ((eq org-log-note-purpose 'done) "closed todo item")
  12070. ((eq org-log-note-purpose 'state)
  12071. (format "state change from \"%s\" to \"%s\""
  12072. (or org-log-note-previous-state "")
  12073. (or org-log-note-state "")))
  12074. ((eq org-log-note-purpose 'reschedule)
  12075. "rescheduling")
  12076. ((eq org-log-note-purpose 'delschedule)
  12077. "no longer scheduled")
  12078. ((eq org-log-note-purpose 'redeadline)
  12079. "changing deadline")
  12080. ((eq org-log-note-purpose 'deldeadline)
  12081. "removing deadline")
  12082. ((eq org-log-note-purpose 'refile)
  12083. "refiling")
  12084. ((eq org-log-note-purpose 'note)
  12085. "this entry")
  12086. (t (error "This should not happen")))))
  12087. (if org-log-note-extra (insert org-log-note-extra))
  12088. (org-set-local 'org-finish-function 'org-store-log-note)
  12089. (run-hooks 'org-log-buffer-setup-hook)))
  12090. (defvar org-note-abort nil) ; dynamically scoped
  12091. (defun org-store-log-note ()
  12092. "Finish taking a log note, and insert it to where it belongs."
  12093. (let ((txt (buffer-string)))
  12094. (kill-buffer (current-buffer))
  12095. (let ((note (cdr (assq org-log-note-purpose org-log-note-headings)))
  12096. lines ind bul)
  12097. (while (string-match "\\`# .*\n[ \t\n]*" txt)
  12098. (setq txt (replace-match "" t t txt)))
  12099. (if (string-match "\\s-+\\'" txt)
  12100. (setq txt (replace-match "" t t txt)))
  12101. (setq lines (org-split-string txt "\n"))
  12102. (when (and note (string-match "\\S-" note))
  12103. (setq note
  12104. (org-replace-escapes
  12105. note
  12106. (list (cons "%u" (user-login-name))
  12107. (cons "%U" user-full-name)
  12108. (cons "%t" (format-time-string
  12109. (org-time-stamp-format 'long 'inactive)
  12110. org-log-note-effective-time))
  12111. (cons "%T" (format-time-string
  12112. (org-time-stamp-format 'long nil)
  12113. org-log-note-effective-time))
  12114. (cons "%d" (format-time-string
  12115. (org-time-stamp-format nil 'inactive)
  12116. org-log-note-effective-time))
  12117. (cons "%D" (format-time-string
  12118. (org-time-stamp-format nil nil)
  12119. org-log-note-effective-time))
  12120. (cons "%s" (if org-log-note-state
  12121. (concat "\"" org-log-note-state "\"")
  12122. ""))
  12123. (cons "%S" (if org-log-note-previous-state
  12124. (concat "\"" org-log-note-previous-state "\"")
  12125. "\"\"")))))
  12126. (if lines (setq note (concat note " \\\\")))
  12127. (push note lines))
  12128. (when (or current-prefix-arg org-note-abort)
  12129. (when org-log-into-drawer
  12130. (org-remove-empty-drawer-at
  12131. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  12132. org-log-note-marker))
  12133. (setq lines nil))
  12134. (when lines
  12135. (with-current-buffer (marker-buffer org-log-note-marker)
  12136. (save-excursion
  12137. (goto-char org-log-note-marker)
  12138. (move-marker org-log-note-marker nil)
  12139. (end-of-line 1)
  12140. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  12141. (setq ind (save-excursion
  12142. (if (ignore-errors (goto-char (org-in-item-p)))
  12143. (let ((struct (org-list-struct)))
  12144. (org-list-get-ind
  12145. (org-list-get-top-point struct) struct))
  12146. (skip-chars-backward " \r\t\n")
  12147. (cond
  12148. ((and (org-at-heading-p)
  12149. org-adapt-indentation)
  12150. (1+ (org-current-level)))
  12151. ((org-at-heading-p) 0)
  12152. (t (org-get-indentation))))))
  12153. (setq bul (org-list-bullet-string "-"))
  12154. (org-indent-line-to ind)
  12155. (insert bul (pop lines))
  12156. (let ((ind-body (+ (length bul) ind)))
  12157. (while lines
  12158. (insert "\n")
  12159. (org-indent-line-to ind-body)
  12160. (insert (pop lines))))
  12161. (message "Note stored")
  12162. (org-back-to-heading t)
  12163. (org-cycle-hide-drawers 'children))
  12164. ;; Fix `buffer-undo-list' when `org-store-log-note' is called
  12165. ;; from within `org-add-log-note' because `buffer-undo-list'
  12166. ;; is then modified outside of `org-with-remote-undo'.
  12167. (when (eq this-command 'org-agenda-todo)
  12168. (setcdr buffer-undo-list (cddr buffer-undo-list)))))))
  12169. ;; Don't add undo information when called from `org-agenda-todo'
  12170. (let ((buffer-undo-list (eq this-command 'org-agenda-todo)))
  12171. (set-window-configuration org-log-note-window-configuration)
  12172. (with-current-buffer (marker-buffer org-log-note-return-to)
  12173. (goto-char org-log-note-return-to))
  12174. (move-marker org-log-note-return-to nil)
  12175. (and org-log-post-message (message "%s" org-log-post-message))))
  12176. (defun org-remove-empty-drawer-at (drawer pos)
  12177. "Remove an empty drawer DRAWER at position POS.
  12178. POS may also be a marker."
  12179. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  12180. (save-excursion
  12181. (save-restriction
  12182. (widen)
  12183. (goto-char pos)
  12184. (if (org-in-regexp
  12185. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  12186. (replace-match ""))))))
  12187. (defvar org-ts-type nil)
  12188. (defun org-sparse-tree (&optional arg type)
  12189. "Create a sparse tree, prompt for the details.
  12190. This command can create sparse trees. You first need to select the type
  12191. of match used to create the tree:
  12192. t Show all TODO entries.
  12193. T Show entries with a specific TODO keyword.
  12194. m Show entries selected by a tags/property match.
  12195. p Enter a property name and its value (both with completion on existing
  12196. names/values) and show entries with that property.
  12197. r Show entries matching a regular expression (`/' can be used as well).
  12198. b Show deadlines and scheduled items before a date.
  12199. a Show deadlines and scheduled items after a date.
  12200. d Show deadlines due within `org-deadline-warning-days'.
  12201. D Show deadlines and scheduled items between a date range."
  12202. (interactive "P")
  12203. (let (ans kwd value ts-type)
  12204. (setq type (or type org-sparse-tree-default-date-type))
  12205. (setq org-ts-type type)
  12206. (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"
  12207. (cond ((eq type 'all) "all timestamps")
  12208. ((eq type 'scheduled) "only scheduled")
  12209. ((eq type 'deadline) "only deadline")
  12210. ((eq type 'active) "only active timestamps")
  12211. ((eq type 'inactive) "only inactive timestamps")
  12212. ((eq type 'scheduled-or-deadline) "scheduled/deadline")
  12213. ((eq type 'closed) "with a closed time-stamp")
  12214. (t "scheduled/deadline")))
  12215. (setq ans (read-char-exclusive))
  12216. (cond
  12217. ((equal ans ?c)
  12218. (org-sparse-tree
  12219. arg (cadr (member type '(scheduled-or-deadline
  12220. all scheduled deadline active inactive closed)))))
  12221. ((equal ans ?d)
  12222. (call-interactively 'org-check-deadlines))
  12223. ((equal ans ?b)
  12224. (call-interactively 'org-check-before-date))
  12225. ((equal ans ?a)
  12226. (call-interactively 'org-check-after-date))
  12227. ((equal ans ?D)
  12228. (call-interactively 'org-check-dates-range))
  12229. ((equal ans ?t)
  12230. (call-interactively 'org-show-todo-tree))
  12231. ((equal ans ?T)
  12232. (org-show-todo-tree '(4)))
  12233. ((member ans '(?T ?m))
  12234. (call-interactively 'org-match-sparse-tree))
  12235. ((member ans '(?p ?P))
  12236. (setq kwd (org-icompleting-read "Property: "
  12237. (mapcar 'list (org-buffer-property-keys))))
  12238. (setq value (org-icompleting-read "Value: "
  12239. (mapcar 'list (org-property-values kwd))))
  12240. (unless (string-match "\\`{.*}\\'" value)
  12241. (setq value (concat "\"" value "\"")))
  12242. (org-match-sparse-tree arg (concat kwd "=" value)))
  12243. ((member ans '(?r ?R ?/))
  12244. (call-interactively 'org-occur))
  12245. (t (error "No such sparse tree command \"%c\"" ans)))))
  12246. (defvar org-occur-highlights nil
  12247. "List of overlays used for occur matches.")
  12248. (make-variable-buffer-local 'org-occur-highlights)
  12249. (defvar org-occur-parameters nil
  12250. "Parameters of the active org-occur calls.
  12251. This is a list, each call to org-occur pushes as cons cell,
  12252. containing the regular expression and the callback, onto the list.
  12253. The list can contain several entries if `org-occur' has been called
  12254. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  12255. will only contain one set of parameters. When the highlights are
  12256. removed (for example with `C-c C-c', or with the next edit (depending
  12257. on `org-remove-highlights-with-change'), this variable is emptied
  12258. as well.")
  12259. (make-variable-buffer-local 'org-occur-parameters)
  12260. (defun org-occur (regexp &optional keep-previous callback)
  12261. "Make a compact tree which shows all matches of REGEXP.
  12262. The tree will show the lines where the regexp matches, and all higher
  12263. headlines above the match. It will also show the heading after the match,
  12264. to make sure editing the matching entry is easy.
  12265. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  12266. call to `org-occur' will be kept, to allow stacking of calls to this
  12267. command.
  12268. If CALLBACK is non-nil, it is a function which is called to confirm
  12269. that the match should indeed be shown."
  12270. (interactive "sRegexp: \nP")
  12271. (when (equal regexp "")
  12272. (error "Regexp cannot be empty"))
  12273. (unless keep-previous
  12274. (org-remove-occur-highlights nil nil t))
  12275. (push (cons regexp callback) org-occur-parameters)
  12276. (let ((cnt 0))
  12277. (save-excursion
  12278. (goto-char (point-min))
  12279. (if (or (not keep-previous) ; do not want to keep
  12280. (not org-occur-highlights)) ; no previous matches
  12281. ;; hide everything
  12282. (org-overview))
  12283. (while (re-search-forward regexp nil t)
  12284. (when (or (not callback)
  12285. (save-match-data (funcall callback)))
  12286. (setq cnt (1+ cnt))
  12287. (when org-highlight-sparse-tree-matches
  12288. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  12289. (org-show-context 'occur-tree))))
  12290. (when org-remove-highlights-with-change
  12291. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  12292. nil 'local))
  12293. (unless org-sparse-tree-open-archived-trees
  12294. (org-hide-archived-subtrees (point-min) (point-max)))
  12295. (run-hooks 'org-occur-hook)
  12296. (if (org-called-interactively-p 'interactive)
  12297. (message "%d match(es) for regexp %s" cnt regexp))
  12298. cnt))
  12299. (defun org-occur-next-match (&optional n reset)
  12300. "Function for `next-error-function' to find sparse tree matches.
  12301. N is the number of matches to move, when negative move backwards.
  12302. RESET is entirely ignored - this function always goes back to the
  12303. starting point when no match is found."
  12304. (let* ((limit (if (< n 0) (point-min) (point-max)))
  12305. (search-func (if (< n 0)
  12306. 'previous-single-char-property-change
  12307. 'next-single-char-property-change))
  12308. (n (abs n))
  12309. (pos (point))
  12310. p1)
  12311. (catch 'exit
  12312. (while (setq p1 (funcall search-func (point) 'org-type))
  12313. (when (equal p1 limit)
  12314. (goto-char pos)
  12315. (error "No more matches"))
  12316. (when (equal (get-char-property p1 'org-type) 'org-occur)
  12317. (setq n (1- n))
  12318. (when (= n 0)
  12319. (goto-char p1)
  12320. (throw 'exit (point))))
  12321. (goto-char p1))
  12322. (goto-char p1)
  12323. (error "No more matches"))))
  12324. (defun org-show-context (&optional key)
  12325. "Make sure point and context are visible.
  12326. How much context is shown depends upon the variables
  12327. `org-show-hierarchy-above', `org-show-following-heading',
  12328. `org-show-entry-below' and `org-show-siblings'."
  12329. (let ((heading-p (org-at-heading-p t))
  12330. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  12331. (following-p (org-get-alist-option org-show-following-heading key))
  12332. (entry-p (org-get-alist-option org-show-entry-below key))
  12333. (siblings-p (org-get-alist-option org-show-siblings key)))
  12334. ;; Show heading or entry text
  12335. (if (and heading-p (not entry-p))
  12336. (org-flag-heading nil) ; only show the heading
  12337. (and (or entry-p (outline-invisible-p) (org-invisible-p2))
  12338. (org-show-hidden-entry))) ; show entire entry
  12339. (when following-p
  12340. ;; Show next sibling, or heading below text
  12341. (save-excursion
  12342. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  12343. (org-flag-heading nil))))
  12344. (when siblings-p (org-show-siblings))
  12345. (when hierarchy-p
  12346. ;; show all higher headings, possibly with siblings
  12347. (save-excursion
  12348. (while (and (condition-case nil
  12349. (progn (org-up-heading-all 1) t)
  12350. (error nil))
  12351. (not (bobp)))
  12352. (org-flag-heading nil)
  12353. (when siblings-p (org-show-siblings)))))
  12354. (unless (eq key 'agenda) (org-fix-ellipsis-at-bol))))
  12355. (defvar org-reveal-start-hook nil
  12356. "Hook run before revealing a location.")
  12357. (defun org-reveal (&optional siblings)
  12358. "Show current entry, hierarchy above it, and the following headline.
  12359. This can be used to show a consistent set of context around locations
  12360. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  12361. not t for the search context.
  12362. With optional argument SIBLINGS, on each level of the hierarchy all
  12363. siblings are shown. This repairs the tree structure to what it would
  12364. look like when opened with hierarchical calls to `org-cycle'.
  12365. With double optional argument \\[universal-argument] \\[universal-argument], \
  12366. go to the parent and show the
  12367. entire tree."
  12368. (interactive "P")
  12369. (run-hooks 'org-reveal-start-hook)
  12370. (let ((org-show-hierarchy-above t)
  12371. (org-show-following-heading t)
  12372. (org-show-siblings (if siblings t org-show-siblings)))
  12373. (org-show-context nil))
  12374. (when (equal siblings '(16))
  12375. (save-excursion
  12376. (when (org-up-heading-safe)
  12377. (org-show-subtree)
  12378. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  12379. (defun org-highlight-new-match (beg end)
  12380. "Highlight from BEG to END and mark the highlight is an occur headline."
  12381. (let ((ov (make-overlay beg end)))
  12382. (overlay-put ov 'face 'secondary-selection)
  12383. (overlay-put ov 'org-type 'org-occur)
  12384. (push ov org-occur-highlights)))
  12385. (defun org-remove-occur-highlights (&optional beg end noremove)
  12386. "Remove the occur highlights from the buffer.
  12387. BEG and END are ignored. If NOREMOVE is nil, remove this function
  12388. from the `before-change-functions' in the current buffer."
  12389. (interactive)
  12390. (unless org-inhibit-highlight-removal
  12391. (mapc 'delete-overlay org-occur-highlights)
  12392. (setq org-occur-highlights nil)
  12393. (setq org-occur-parameters nil)
  12394. (unless noremove
  12395. (remove-hook 'before-change-functions
  12396. 'org-remove-occur-highlights 'local))))
  12397. ;;;; Priorities
  12398. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  12399. "Regular expression matching the priority indicator.")
  12400. (defvar org-remove-priority-next-time nil)
  12401. (defun org-priority-up ()
  12402. "Increase the priority of the current item."
  12403. (interactive)
  12404. (org-priority 'up))
  12405. (defun org-priority-down ()
  12406. "Decrease the priority of the current item."
  12407. (interactive)
  12408. (org-priority 'down))
  12409. (defun org-priority (&optional action show)
  12410. "Change the priority of an item.
  12411. ACTION can be `set', `up', `down', or a character."
  12412. (interactive "P")
  12413. (if (equal action '(4))
  12414. (org-show-priority)
  12415. (unless org-enable-priority-commands
  12416. (error "Priority commands are disabled"))
  12417. (setq action (or action 'set))
  12418. (let (current new news have remove)
  12419. (save-excursion
  12420. (org-back-to-heading t)
  12421. (if (looking-at org-priority-regexp)
  12422. (setq current (string-to-char (match-string 2))
  12423. have t))
  12424. (cond
  12425. ((eq action 'remove)
  12426. (setq remove t new ?\ ))
  12427. ((or (eq action 'set)
  12428. (if (featurep 'xemacs) (characterp action) (integerp action)))
  12429. (if (not (eq action 'set))
  12430. (setq new action)
  12431. (message "Priority %c-%c, SPC to remove: "
  12432. org-highest-priority org-lowest-priority)
  12433. (save-match-data
  12434. (setq new (read-char-exclusive))))
  12435. (if (and (= (upcase org-highest-priority) org-highest-priority)
  12436. (= (upcase org-lowest-priority) org-lowest-priority))
  12437. (setq new (upcase new)))
  12438. (cond ((equal new ?\ ) (setq remove t))
  12439. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  12440. (error "Priority must be between `%c' and `%c'"
  12441. org-highest-priority org-lowest-priority))))
  12442. ((eq action 'up)
  12443. (setq new (if have
  12444. (1- current) ; normal cycling
  12445. ;; last priority was empty
  12446. (if (eq last-command this-command)
  12447. org-lowest-priority ; wrap around empty to lowest
  12448. ;; default
  12449. (if org-priority-start-cycle-with-default
  12450. org-default-priority
  12451. (1- org-default-priority))))))
  12452. ((eq action 'down)
  12453. (setq new (if have
  12454. (1+ current) ; normal cycling
  12455. ;; last priority was empty
  12456. (if (eq last-command this-command)
  12457. org-highest-priority ; wrap around empty to highest
  12458. ;; default
  12459. (if org-priority-start-cycle-with-default
  12460. org-default-priority
  12461. (1+ org-default-priority))))))
  12462. (t (error "Invalid action")))
  12463. (if (or (< (upcase new) org-highest-priority)
  12464. (> (upcase new) org-lowest-priority))
  12465. (if (and (memq action '(up down))
  12466. (not have) (not (eq last-command this-command)))
  12467. ;; `new' is from default priority
  12468. (error
  12469. "The default can not be set, see `org-default-priority' why")
  12470. ;; normal cycling: `new' is beyond highest/lowest priority
  12471. ;; and is wrapped around to the empty priority
  12472. (setq remove t)))
  12473. (setq news (format "%c" new))
  12474. (if have
  12475. (if remove
  12476. (replace-match "" t t nil 1)
  12477. (replace-match news t t nil 2))
  12478. (if remove
  12479. (error "No priority cookie found in line")
  12480. (let ((case-fold-search nil))
  12481. (looking-at org-todo-line-regexp))
  12482. (if (match-end 2)
  12483. (progn
  12484. (goto-char (match-end 2))
  12485. (insert " [#" news "]"))
  12486. (goto-char (match-beginning 3))
  12487. (insert "[#" news "] "))))
  12488. (org-preserve-lc (org-set-tags nil 'align)))
  12489. (if remove
  12490. (message "Priority removed")
  12491. (message "Priority of current item set to %s" news)))))
  12492. (defun org-show-priority ()
  12493. "Show the priority of the current item.
  12494. This priority is composed of the main priority given with the [#A] cookies,
  12495. and by additional input from the age of a schedules or deadline entry."
  12496. (interactive)
  12497. (let ((pri (if (eq major-mode 'org-agenda-mode)
  12498. (org-get-at-bol 'priority)
  12499. (save-excursion
  12500. (save-match-data
  12501. (beginning-of-line)
  12502. (and (looking-at org-heading-regexp)
  12503. (org-get-priority (match-string 0))))))))
  12504. (message "Priority is %d" (if pri pri -1000))))
  12505. (defun org-get-priority (s)
  12506. "Find priority cookie and return priority."
  12507. (save-match-data
  12508. (if (functionp org-get-priority-function)
  12509. (funcall org-get-priority-function)
  12510. (if (not (string-match org-priority-regexp s))
  12511. (* 1000 (- org-lowest-priority org-default-priority))
  12512. (* 1000 (- org-lowest-priority
  12513. (string-to-char (match-string 2 s))))))))
  12514. ;;;; Tags
  12515. (defvar org-agenda-archives-mode)
  12516. (defvar org-map-continue-from nil
  12517. "Position from where mapping should continue.
  12518. Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
  12519. (defvar org-scanner-tags nil
  12520. "The current tag list while the tags scanner is running.")
  12521. (defvar org-trust-scanner-tags nil
  12522. "Should `org-get-tags-at' use the tags for the scanner.
  12523. This is for internal dynamical scoping only.
  12524. When this is non-nil, the function `org-get-tags-at' will return the value
  12525. of `org-scanner-tags' instead of building the list by itself. This
  12526. can lead to large speed-ups when the tags scanner is used in a file with
  12527. many entries, and when the list of tags is retrieved, for example to
  12528. obtain a list of properties. Building the tags list for each entry in such
  12529. a file becomes an N^2 operation - but with this variable set, it scales
  12530. as N.")
  12531. (defun org-scan-tags (action matcher todo-only &optional start-level)
  12532. "Sca headline tags with inheritance and produce output ACTION.
  12533. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  12534. or `agenda' to produce an entry list for an agenda view. It can also be
  12535. a Lisp form or a function that should be called at each matched headline, in
  12536. this case the return value is a list of all return values from these calls.
  12537. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  12538. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  12539. only lines with a not-done TODO keyword are included in the output.
  12540. This should be the same variable that was scoped into
  12541. and set by `org-make-tags-matcher' when it constructed MATCHER.
  12542. START-LEVEL can be a string with asterisks, reducing the scope to
  12543. headlines matching this string."
  12544. (require 'org-agenda)
  12545. (let* ((re (concat "^"
  12546. (if start-level
  12547. ;; Get the correct level to match
  12548. (concat "\\*\\{" (number-to-string start-level) "\\} ")
  12549. org-outline-regexp)
  12550. " *\\(\\<\\("
  12551. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  12552. (org-re "\\)\\>\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  12553. (props (list 'face 'default
  12554. 'done-face 'org-agenda-done
  12555. 'undone-face 'default
  12556. 'mouse-face 'highlight
  12557. 'org-not-done-regexp org-not-done-regexp
  12558. 'org-todo-regexp org-todo-regexp
  12559. 'org-complex-heading-regexp org-complex-heading-regexp
  12560. 'help-echo
  12561. (format "mouse-2 or RET jump to org file %s"
  12562. (abbreviate-file-name
  12563. (or (buffer-file-name (buffer-base-buffer))
  12564. (buffer-name (buffer-base-buffer)))))))
  12565. (case-fold-search nil)
  12566. (org-map-continue-from nil)
  12567. lspos tags tags-list
  12568. (tags-alist (list (cons 0 org-file-tags)))
  12569. (llast 0) rtn rtn1 level category i txt
  12570. todo marker entry priority)
  12571. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  12572. (setq action (list 'lambda nil action)))
  12573. (save-excursion
  12574. (goto-char (point-min))
  12575. (when (eq action 'sparse-tree)
  12576. (org-overview)
  12577. (org-remove-occur-highlights))
  12578. (while (re-search-forward re nil t)
  12579. (setq org-map-continue-from nil)
  12580. (catch :skip
  12581. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  12582. tags (if (match-end 4) (org-match-string-no-properties 4)))
  12583. (goto-char (setq lspos (match-beginning 0)))
  12584. (setq level (org-reduced-level (org-outline-level))
  12585. category (org-get-category))
  12586. (setq i llast llast level)
  12587. ;; remove tag lists from same and sublevels
  12588. (while (>= i level)
  12589. (when (setq entry (assoc i tags-alist))
  12590. (setq tags-alist (delete entry tags-alist)))
  12591. (setq i (1- i)))
  12592. ;; add the next tags
  12593. (when tags
  12594. (setq tags (org-split-string tags ":")
  12595. tags-alist
  12596. (cons (cons level tags) tags-alist)))
  12597. ;; compile tags for current headline
  12598. (setq tags-list
  12599. (if org-use-tag-inheritance
  12600. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  12601. tags)
  12602. org-scanner-tags tags-list)
  12603. (when org-use-tag-inheritance
  12604. (setcdr (car tags-alist)
  12605. (mapcar (lambda (x)
  12606. (setq x (copy-sequence x))
  12607. (org-add-prop-inherited x))
  12608. (cdar tags-alist))))
  12609. (when (and tags org-use-tag-inheritance
  12610. (or (not (eq t org-use-tag-inheritance))
  12611. org-tags-exclude-from-inheritance))
  12612. ;; selective inheritance, remove uninherited ones
  12613. (setcdr (car tags-alist)
  12614. (org-remove-uninherited-tags (cdar tags-alist))))
  12615. (when (and
  12616. ;; eval matcher only when the todo condition is OK
  12617. (and (or (not todo-only) (member todo org-not-done-keywords))
  12618. (let ((case-fold-search t) (org-trust-scanner-tags t))
  12619. (eval matcher)))
  12620. ;; Call the skipper, but return t if it does not skip,
  12621. ;; so that the `and' form continues evaluating
  12622. (progn
  12623. (unless (eq action 'sparse-tree) (org-agenda-skip))
  12624. t)
  12625. ;; Check if timestamps are deselecting this entry
  12626. (or (not todo-only)
  12627. (and (member todo org-not-done-keywords)
  12628. (or (not org-agenda-tags-todo-honor-ignore-options)
  12629. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item))))))
  12630. ;; select this headline
  12631. (cond
  12632. ((eq action 'sparse-tree)
  12633. (and org-highlight-sparse-tree-matches
  12634. (org-get-heading) (match-end 0)
  12635. (org-highlight-new-match
  12636. (match-beginning 1) (match-end 1)))
  12637. (org-show-context 'tags-tree))
  12638. ((eq action 'agenda)
  12639. (setq txt (org-agenda-format-item
  12640. ""
  12641. (concat
  12642. (if (eq org-tags-match-list-sublevels 'indented)
  12643. (make-string (1- level) ?.) "")
  12644. (org-get-heading))
  12645. level category
  12646. tags-list)
  12647. priority (org-get-priority txt))
  12648. (goto-char lspos)
  12649. (setq marker (org-agenda-new-marker))
  12650. (org-add-props txt props
  12651. 'org-marker marker 'org-hd-marker marker 'org-category category
  12652. 'todo-state todo
  12653. 'priority priority 'type "tagsmatch")
  12654. (push txt rtn))
  12655. ((functionp action)
  12656. (setq org-map-continue-from nil)
  12657. (save-excursion
  12658. (setq rtn1 (funcall action))
  12659. (push rtn1 rtn)))
  12660. (t (error "Invalid action")))
  12661. ;; if we are to skip sublevels, jump to end of subtree
  12662. (unless org-tags-match-list-sublevels
  12663. (org-end-of-subtree t)
  12664. (backward-char 1))))
  12665. ;; Get the correct position from where to continue
  12666. (if org-map-continue-from
  12667. (goto-char org-map-continue-from)
  12668. (and (= (point) lspos) (end-of-line 1)))))
  12669. (when (and (eq action 'sparse-tree)
  12670. (not org-sparse-tree-open-archived-trees))
  12671. (org-hide-archived-subtrees (point-min) (point-max)))
  12672. (nreverse rtn)))
  12673. (defun org-remove-uninherited-tags (tags)
  12674. "Remove all tags that are not inherited from the list TAGS."
  12675. (cond
  12676. ((eq org-use-tag-inheritance t)
  12677. (if org-tags-exclude-from-inheritance
  12678. (org-delete-all org-tags-exclude-from-inheritance tags)
  12679. tags))
  12680. ((not org-use-tag-inheritance) nil)
  12681. ((stringp org-use-tag-inheritance)
  12682. (delq nil (mapcar
  12683. (lambda (x)
  12684. (if (and (string-match org-use-tag-inheritance x)
  12685. (not (member x org-tags-exclude-from-inheritance)))
  12686. x nil))
  12687. tags)))
  12688. ((listp org-use-tag-inheritance)
  12689. (delq nil (mapcar
  12690. (lambda (x)
  12691. (if (member x org-use-tag-inheritance) x nil))
  12692. tags)))))
  12693. (defun org-match-sparse-tree (&optional todo-only match)
  12694. "Create a sparse tree according to tags string MATCH.
  12695. MATCH can contain positive and negative selection of tags, like
  12696. \"+WORK+URGENT-WITHBOSS\".
  12697. If optional argument TODO-ONLY is non-nil, only select lines that are
  12698. also TODO lines."
  12699. (interactive "P")
  12700. (org-agenda-prepare-buffers (list (current-buffer)))
  12701. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  12702. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  12703. (defvar org-cached-props nil)
  12704. (defun org-cached-entry-get (pom property)
  12705. (if (or (eq t org-use-property-inheritance)
  12706. (and (stringp org-use-property-inheritance)
  12707. (string-match org-use-property-inheritance property))
  12708. (and (listp org-use-property-inheritance)
  12709. (member property org-use-property-inheritance)))
  12710. ;; Caching is not possible, check it directly
  12711. (org-entry-get pom property 'inherit)
  12712. ;; Get all properties, so that we can do complicated checks easily
  12713. (cdr (assoc property (or org-cached-props
  12714. (setq org-cached-props
  12715. (org-entry-properties pom)))))))
  12716. (defun org-global-tags-completion-table (&optional files)
  12717. "Return the list of all tags in all agenda buffer/files.
  12718. Optional FILES argument is a list of files which can be used
  12719. instead of the agenda files."
  12720. (save-excursion
  12721. (org-uniquify
  12722. (delq nil
  12723. (apply 'append
  12724. (mapcar
  12725. (lambda (file)
  12726. (set-buffer (find-file-noselect file))
  12727. (append (org-get-buffer-tags)
  12728. (mapcar (lambda (x) (if (stringp (car-safe x))
  12729. (list (car-safe x)) nil))
  12730. org-tag-alist)))
  12731. (if (and files (car files))
  12732. files
  12733. (org-agenda-files))))))))
  12734. (defun org-make-tags-matcher (match)
  12735. "Create the TAGS/TODO matcher form for the selection string MATCH.
  12736. The variable `todo-only' is scoped dynamically into this function.
  12737. It will be set to t if the matcher restricts matching to TODO entries,
  12738. otherwise will not be touched.
  12739. Returns a cons of the selection string MATCH and the constructed
  12740. lisp form implementing the matcher. The matcher is to be evaluated
  12741. at an Org entry, with point on the headline, and returns t if the
  12742. entry matches the selection string MATCH. The returned lisp form
  12743. references two variables with information about the entry, which
  12744. must be bound around the form's evaluation: todo, the TODO keyword
  12745. at the entry (or nil of none); and tags-list, the list of all tags
  12746. at the entry including inherited ones. Additionally, the category
  12747. of the entry (if any) must be specified as the text property
  12748. 'org-category on the headline.
  12749. See also `org-scan-tags'.
  12750. "
  12751. (declare (special todo-only))
  12752. (unless (boundp 'todo-only)
  12753. (error "`org-make-tags-matcher' expects todo-only to be scoped in"))
  12754. (unless match
  12755. ;; Get a new match request, with completion
  12756. (let ((org-last-tags-completion-table
  12757. (org-global-tags-completion-table)))
  12758. (setq match (org-completing-read-no-i
  12759. "Match: " 'org-tags-completion-function nil nil nil
  12760. 'org-tags-history))))
  12761. ;; Parse the string and create a lisp form
  12762. (let ((match0 match)
  12763. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  12764. minus tag mm
  12765. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  12766. orterms term orlist re-p str-p level-p level-op time-p
  12767. prop-p pn pv po gv rest)
  12768. ;; Expand group tags
  12769. (setq match (org-tags-expand match))
  12770. (if (string-match "/+" match)
  12771. ;; match contains also a todo-matching request
  12772. (progn
  12773. (setq tagsmatch (substring match 0 (match-beginning 0))
  12774. todomatch (substring match (match-end 0)))
  12775. (if (string-match "^!" todomatch)
  12776. (setq todo-only t todomatch (substring todomatch 1)))
  12777. (if (string-match "^\\s-*$" todomatch)
  12778. (setq todomatch nil)))
  12779. ;; only matching tags
  12780. (setq tagsmatch match todomatch nil))
  12781. ;; Make the tags matcher
  12782. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  12783. (setq tagsmatcher t)
  12784. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  12785. (while (setq term (pop orterms))
  12786. (while (and (equal (substring term -1) "\\") orterms)
  12787. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  12788. (while (string-match re term)
  12789. (setq rest (substring term (match-end 0))
  12790. minus (and (match-end 1)
  12791. (equal (match-string 1 term) "-"))
  12792. tag (save-match-data (replace-regexp-in-string
  12793. "\\\\-" "-"
  12794. (match-string 2 term)))
  12795. re-p (equal (string-to-char tag) ?{)
  12796. level-p (match-end 4)
  12797. prop-p (match-end 5)
  12798. mm (cond
  12799. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  12800. (level-p
  12801. (setq level-op (org-op-to-function (match-string 3 term)))
  12802. `(,level-op level ,(string-to-number
  12803. (match-string 4 term))))
  12804. (prop-p
  12805. (setq pn (match-string 5 term)
  12806. po (match-string 6 term)
  12807. pv (match-string 7 term)
  12808. re-p (equal (string-to-char pv) ?{)
  12809. str-p (equal (string-to-char pv) ?\")
  12810. time-p (save-match-data
  12811. (string-match "^\"[[<].*[]>]\"$" pv))
  12812. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  12813. (if time-p (setq pv (org-matcher-time pv)))
  12814. (setq po (org-op-to-function po (if time-p 'time str-p)))
  12815. (cond
  12816. ((equal pn "CATEGORY")
  12817. (setq gv '(get-text-property (point) 'org-category)))
  12818. ((equal pn "TODO")
  12819. (setq gv 'todo))
  12820. (t
  12821. (setq gv `(org-cached-entry-get nil ,pn))))
  12822. (if re-p
  12823. (if (eq po 'org<>)
  12824. `(not (string-match ,pv (or ,gv "")))
  12825. `(string-match ,pv (or ,gv "")))
  12826. (if str-p
  12827. `(,po (or ,gv "") ,pv)
  12828. `(,po (string-to-number (or ,gv ""))
  12829. ,(string-to-number pv) ))))
  12830. (t `(member ,tag tags-list)))
  12831. mm (if minus (list 'not mm) mm)
  12832. term rest)
  12833. (push mm tagsmatcher))
  12834. (push (if (> (length tagsmatcher) 1)
  12835. (cons 'and tagsmatcher)
  12836. (car tagsmatcher))
  12837. orlist)
  12838. (setq tagsmatcher nil))
  12839. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  12840. (setq tagsmatcher
  12841. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  12842. ;; Make the todo matcher
  12843. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  12844. (setq todomatcher t)
  12845. (setq orterms (org-split-string todomatch "|") orlist nil)
  12846. (while (setq term (pop orterms))
  12847. (while (string-match re term)
  12848. (setq minus (and (match-end 1)
  12849. (equal (match-string 1 term) "-"))
  12850. kwd (match-string 2 term)
  12851. re-p (equal (string-to-char kwd) ?{)
  12852. term (substring term (match-end 0))
  12853. mm (if re-p
  12854. `(string-match ,(substring kwd 1 -1) todo)
  12855. (list 'equal 'todo kwd))
  12856. mm (if minus (list 'not mm) mm))
  12857. (push mm todomatcher))
  12858. (push (if (> (length todomatcher) 1)
  12859. (cons 'and todomatcher)
  12860. (car todomatcher))
  12861. orlist)
  12862. (setq todomatcher nil))
  12863. (setq todomatcher (if (> (length orlist) 1)
  12864. (cons 'or orlist) (car orlist))))
  12865. ;; Return the string and lisp forms of the matcher
  12866. (setq matcher (if todomatcher
  12867. (list 'and tagsmatcher todomatcher)
  12868. tagsmatcher))
  12869. (when todo-only
  12870. (setq matcher (list 'and '(member todo org-not-done-keywords)
  12871. matcher)))
  12872. (cons match0 matcher)))
  12873. (defun org-tags-expand (match &optional single-as-list downcased)
  12874. "Expand group tags in MATCH.
  12875. This replaces every group tag in MATCH with a regexp tag search.
  12876. For example, a group tag \"Work\" defined as { Work : Lab Conf }
  12877. will be replaced like this:
  12878. Work => {\(?:Work\|Lab\|Conf\}
  12879. +Work => +{\(?:Work\|Lab\|Conf\}
  12880. -Work => -{\(?:Work\|Lab\|Conf\}
  12881. Replacing by a regexp preserves the structure of the match.
  12882. E.g., this expansion
  12883. Work|Home => {\(?:Work\|Lab\|Conf\}|Home
  12884. will match anything tagged with \"Lab\" and \"Home\", or tagged
  12885. with \"Conf\" and \"Home\" or tagged with \"Work\" and \"home\".
  12886. When the optional argument SINGLE-AS-LIST is non-nil, MATCH is
  12887. assumed to be a single group tag, and the function will return
  12888. the list of tags in this group.
  12889. When DOWNCASE is non-nil, expand downcased TAGS."
  12890. (if org-group-tags
  12891. (let* ((case-fold-search t)
  12892. (stable org-mode-syntax-table)
  12893. (tal (or org-tag-groups-alist-for-agenda
  12894. org-tag-groups-alist))
  12895. (tal (if downcased (mapcar (lambda(tg) (mapcar 'downcase tg)) tal) tal))
  12896. (tml (mapcar 'car tal))
  12897. (rtnmatch match) rpl)
  12898. ;; @ and _ are allowed as word-components in tags
  12899. (modify-syntax-entry ?@ "w" stable)
  12900. (modify-syntax-entry ?_ "w" stable)
  12901. (while (and tml (string-match
  12902. (concat "\\(?1:[+-]?\\)\\(?2:\\<" (regexp-opt tml) "\\>\\)")
  12903. rtnmatch))
  12904. (let* ((dir (match-string 1 rtnmatch))
  12905. (tag (match-string 2 rtnmatch))
  12906. (tag (if downcased (downcase tag) tag)))
  12907. (setq tml (delete tag tml))
  12908. (setq rpl (append (org-uniquify rpl) (assoc tag tal)))
  12909. (setq rtnmatch
  12910. (replace-match
  12911. (concat dir "{" (regexp-opt rpl) "}") t t rtnmatch))))
  12912. (if single-as-list
  12913. (or (reverse rpl) (list rtnmatch))
  12914. rtnmatch))
  12915. (if single-as-list (list (if downcased (downcase match) match))
  12916. match)))
  12917. (defun org-op-to-function (op &optional stringp)
  12918. "Turn an operator into the appropriate function."
  12919. (setq op
  12920. (cond
  12921. ((equal op "<" ) '(< string< org-time<))
  12922. ((equal op ">" ) '(> org-string> org-time>))
  12923. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  12924. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  12925. ((member op '("=" "==")) '(= string= org-time=))
  12926. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  12927. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  12928. (defun org<> (a b) (not (= a b)))
  12929. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  12930. (defun org-string>= (a b) (not (string< a b)))
  12931. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  12932. (defun org-string<> (a b) (not (string= a b)))
  12933. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  12934. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  12935. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  12936. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  12937. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  12938. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  12939. (defun org-2ft (s)
  12940. "Convert S to a floating point time.
  12941. If S is already a number, just return it. If it is a string, parse
  12942. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  12943. (cond
  12944. ((numberp s) s)
  12945. ((stringp s)
  12946. (condition-case nil
  12947. (float-time (apply 'encode-time (org-parse-time-string s)))
  12948. (error 0.)))
  12949. (t 0.)))
  12950. (defun org-time-today ()
  12951. "Time in seconds today at 0:00.
  12952. Returns the float number of seconds since the beginning of the
  12953. epoch to the beginning of today (00:00)."
  12954. (float-time (apply 'encode-time
  12955. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  12956. (defun org-matcher-time (s)
  12957. "Interpret a time comparison value."
  12958. (save-match-data
  12959. (cond
  12960. ((string= s "<now>") (float-time))
  12961. ((string= s "<today>") (org-time-today))
  12962. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  12963. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  12964. ((string-match "^<\\([-+][0-9]+\\)\\([hdwmy]\\)>$" s)
  12965. (+ (org-time-today)
  12966. (* (string-to-number (match-string 1 s))
  12967. (cdr (assoc (match-string 2 s)
  12968. '(("d" . 86400.0) ("w" . 604800.0)
  12969. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  12970. (t (org-2ft s)))))
  12971. (defun org-match-any-p (re list)
  12972. "Does re match any element of list?"
  12973. (setq list (mapcar (lambda (x) (string-match re x)) list))
  12974. (delq nil list))
  12975. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  12976. (defvar org-tags-overlay (make-overlay 1 1))
  12977. (org-detach-overlay org-tags-overlay)
  12978. (defun org-get-local-tags-at (&optional pos)
  12979. "Get a list of tags defined in the current headline."
  12980. (org-get-tags-at pos 'local))
  12981. (defun org-get-local-tags ()
  12982. "Get a list of tags defined in the current headline."
  12983. (org-get-tags-at nil 'local))
  12984. (defun org-get-tags-at (&optional pos local)
  12985. "Get a list of all headline tags applicable at POS.
  12986. POS defaults to point. If tags are inherited, the list contains
  12987. the targets in the same sequence as the headlines appear, i.e.
  12988. the tags of the current headline come last.
  12989. When LOCAL is non-nil, only return tags from the current headline,
  12990. ignore inherited ones."
  12991. (interactive)
  12992. (if (and org-trust-scanner-tags
  12993. (or (not pos) (equal pos (point)))
  12994. (not local))
  12995. org-scanner-tags
  12996. (let (tags ltags lastpos parent)
  12997. (save-excursion
  12998. (save-restriction
  12999. (widen)
  13000. (goto-char (or pos (point)))
  13001. (save-match-data
  13002. (catch 'done
  13003. (condition-case nil
  13004. (progn
  13005. (org-back-to-heading t)
  13006. (while (not (equal lastpos (point)))
  13007. (setq lastpos (point))
  13008. (when (looking-at
  13009. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  13010. (setq ltags (org-split-string
  13011. (org-match-string-no-properties 1) ":"))
  13012. (when parent
  13013. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  13014. (setq tags (append
  13015. (if parent
  13016. (org-remove-uninherited-tags ltags)
  13017. ltags)
  13018. tags)))
  13019. (or org-use-tag-inheritance (throw 'done t))
  13020. (if local (throw 'done t))
  13021. (or (org-up-heading-safe) (error nil))
  13022. (setq parent t)))
  13023. (error nil)))))
  13024. (if local
  13025. tags
  13026. (reverse (delete-dups
  13027. (reverse (append
  13028. (org-remove-uninherited-tags
  13029. org-file-tags) tags)))))))))
  13030. (defun org-add-prop-inherited (s)
  13031. (add-text-properties 0 (length s) '(inherited t) s)
  13032. s)
  13033. (defun org-toggle-tag (tag &optional onoff)
  13034. "Toggle the tag TAG for the current line.
  13035. If ONOFF is `on' or `off', don't toggle but set to this state."
  13036. (let (res current)
  13037. (save-excursion
  13038. (org-back-to-heading t)
  13039. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  13040. (point-at-eol) t)
  13041. (progn
  13042. (setq current (match-string 1))
  13043. (replace-match ""))
  13044. (setq current ""))
  13045. (setq current (nreverse (org-split-string current ":")))
  13046. (cond
  13047. ((eq onoff 'on)
  13048. (setq res t)
  13049. (or (member tag current) (push tag current)))
  13050. ((eq onoff 'off)
  13051. (or (not (member tag current)) (setq current (delete tag current))))
  13052. (t (if (member tag current)
  13053. (setq current (delete tag current))
  13054. (setq res t)
  13055. (push tag current))))
  13056. (end-of-line 1)
  13057. (if current
  13058. (progn
  13059. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  13060. (org-set-tags nil t))
  13061. (delete-horizontal-space))
  13062. (run-hooks 'org-after-tags-change-hook))
  13063. res))
  13064. (defun org-align-tags-here (to-col)
  13065. ;; Assumes that this is a headline
  13066. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  13067. (beginning-of-line 1)
  13068. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13069. (< pos (match-beginning 2)))
  13070. (progn
  13071. (setq tags-l (- (match-end 2) (match-beginning 2)))
  13072. (goto-char (match-beginning 1))
  13073. (insert " ")
  13074. (delete-region (point) (1+ (match-beginning 2)))
  13075. (setq ncol (max (current-column)
  13076. (1+ col)
  13077. (if (> to-col 0)
  13078. to-col
  13079. (- (abs to-col) tags-l))))
  13080. (setq p (point))
  13081. (insert (make-string (- ncol (current-column)) ?\ ))
  13082. (setq ncol (current-column))
  13083. (when indent-tabs-mode (tabify p (point-at-eol)))
  13084. (org-move-to-column (min ncol col) t))
  13085. (goto-char pos))))
  13086. (defun org-set-tags-command (&optional arg just-align)
  13087. "Call the set-tags command for the current entry."
  13088. (interactive "P")
  13089. (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
  13090. (org-set-tags arg just-align)
  13091. (save-excursion
  13092. (unless (and (org-region-active-p)
  13093. org-loop-over-headlines-in-active-region)
  13094. (org-back-to-heading t))
  13095. (org-set-tags arg just-align))))
  13096. (defun org-set-tags-to (data)
  13097. "Set the tags of the current entry to DATA, replacing the current tags.
  13098. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  13099. If DATA is nil or the empty string, any tags will be removed."
  13100. (interactive "sTags: ")
  13101. (setq data
  13102. (cond
  13103. ((eq data nil) "")
  13104. ((equal data "") "")
  13105. ((stringp data)
  13106. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  13107. ":"))
  13108. ((listp data)
  13109. (concat ":" (mapconcat 'identity data ":") ":"))))
  13110. (when data
  13111. (save-excursion
  13112. (org-back-to-heading t)
  13113. (when (looking-at org-complex-heading-regexp)
  13114. (if (match-end 5)
  13115. (progn
  13116. (goto-char (match-beginning 5))
  13117. (insert data)
  13118. (delete-region (point) (point-at-eol))
  13119. (org-set-tags nil 'align))
  13120. (goto-char (point-at-eol))
  13121. (insert " " data)
  13122. (org-set-tags nil 'align)))
  13123. (beginning-of-line 1)
  13124. (if (looking-at ".*?\\([ \t]+\\)$")
  13125. (delete-region (match-beginning 1) (match-end 1))))))
  13126. (defun org-align-all-tags ()
  13127. "Align the tags i all headings."
  13128. (interactive)
  13129. (save-excursion
  13130. (or (ignore-errors (org-back-to-heading t))
  13131. (outline-next-heading))
  13132. (if (org-at-heading-p)
  13133. (org-set-tags t)
  13134. (message "No headings"))))
  13135. (defvar org-indent-indentation-per-level)
  13136. (defun org-set-tags (&optional arg just-align)
  13137. "Set the tags for the current headline.
  13138. With prefix ARG, realign all tags in headings in the current buffer."
  13139. (interactive "P")
  13140. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  13141. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  13142. 'region-start-level 'region))
  13143. org-loop-over-headlines-in-active-region)
  13144. (org-map-entries
  13145. ;; We don't use ARG and JUST-ALIGN here these args are not
  13146. ;; useful when looping over headlines
  13147. `(org-set-tags)
  13148. org-loop-over-headlines-in-active-region
  13149. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  13150. (let* ((re org-outline-regexp-bol)
  13151. (current (unless arg (org-get-tags-string)))
  13152. (col (current-column))
  13153. (org-setting-tags t)
  13154. table current-tags inherited-tags ; computed below when needed
  13155. tags p0 c0 c1 rpl di tc level)
  13156. (if arg
  13157. (save-excursion
  13158. (goto-char (point-min))
  13159. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  13160. (while (re-search-forward re nil t)
  13161. (org-set-tags nil t)
  13162. (end-of-line 1)))
  13163. (message "All tags realigned to column %d" org-tags-column))
  13164. (if just-align
  13165. (setq tags current)
  13166. ;; Get a new set of tags from the user
  13167. (save-excursion
  13168. (setq table (append org-tag-persistent-alist
  13169. (or org-tag-alist (org-get-buffer-tags))
  13170. (and
  13171. org-complete-tags-always-offer-all-agenda-tags
  13172. (org-global-tags-completion-table
  13173. (org-agenda-files))))
  13174. org-last-tags-completion-table table
  13175. current-tags (org-split-string current ":")
  13176. inherited-tags (nreverse
  13177. (nthcdr (length current-tags)
  13178. (nreverse (org-get-tags-at))))
  13179. tags
  13180. (if (or (eq t org-use-fast-tag-selection)
  13181. (and org-use-fast-tag-selection
  13182. (delq nil (mapcar 'cdr table))))
  13183. (org-fast-tag-selection
  13184. current-tags inherited-tags table
  13185. (if org-fast-tag-selection-include-todo
  13186. org-todo-key-alist))
  13187. (let ((org-add-colon-after-tag-completion (< 1 (length table))))
  13188. (org-trim
  13189. (org-icompleting-read "Tags: "
  13190. 'org-tags-completion-function
  13191. nil nil current 'org-tags-history))))))
  13192. (while (string-match "[-+&]+" tags)
  13193. ;; No boolean logic, just a list
  13194. (setq tags (replace-match ":" t t tags))))
  13195. (setq tags (replace-regexp-in-string "[,]" ":" tags))
  13196. (if org-tags-sort-function
  13197. (setq tags (mapconcat 'identity
  13198. (sort (org-split-string
  13199. tags (org-re "[^[:alnum:]_@#%]+"))
  13200. org-tags-sort-function) ":")))
  13201. (if (string-match "\\`[\t ]*\\'" tags)
  13202. (setq tags "")
  13203. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  13204. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  13205. ;; Insert new tags at the correct column
  13206. (beginning-of-line 1)
  13207. (setq level (or (and (looking-at org-outline-regexp)
  13208. (- (match-end 0) (point) 1))
  13209. 1))
  13210. (cond
  13211. ((and (equal current "") (equal tags "")))
  13212. ((re-search-forward
  13213. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  13214. (point-at-eol) t)
  13215. (if (equal tags "")
  13216. (setq rpl "")
  13217. (goto-char (match-beginning 0))
  13218. (setq c0 (current-column)
  13219. ;; compute offset for the case of org-indent-mode active
  13220. di (if org-indent-mode
  13221. (* (1- org-indent-indentation-per-level) (1- level))
  13222. 0)
  13223. p0 (if (equal (char-before) ?*) (1+ (point)) (point))
  13224. tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
  13225. c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (length tags))))
  13226. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  13227. (replace-match rpl t t)
  13228. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  13229. tags)
  13230. (t (error "Tags alignment failed")))
  13231. (org-move-to-column col)
  13232. (unless just-align
  13233. (run-hooks 'org-after-tags-change-hook))))))
  13234. (defun org-change-tag-in-region (beg end tag off)
  13235. "Add or remove TAG for each entry in the region.
  13236. This works in the agenda, and also in an org-mode buffer."
  13237. (interactive
  13238. (list (region-beginning) (region-end)
  13239. (let ((org-last-tags-completion-table
  13240. (if (derived-mode-p 'org-mode)
  13241. (org-get-buffer-tags)
  13242. (org-global-tags-completion-table))))
  13243. (org-icompleting-read
  13244. "Tag: " 'org-tags-completion-function nil nil nil
  13245. 'org-tags-history))
  13246. (progn
  13247. (message "[s]et or [r]emove? ")
  13248. (equal (read-char-exclusive) ?r))))
  13249. (if (fboundp 'deactivate-mark) (deactivate-mark))
  13250. (let ((agendap (equal major-mode 'org-agenda-mode))
  13251. l1 l2 m buf pos newhead (cnt 0))
  13252. (goto-char end)
  13253. (setq l2 (1- (org-current-line)))
  13254. (goto-char beg)
  13255. (setq l1 (org-current-line))
  13256. (loop for l from l1 to l2 do
  13257. (org-goto-line l)
  13258. (setq m (get-text-property (point) 'org-hd-marker))
  13259. (when (or (and (derived-mode-p 'org-mode) (org-at-heading-p))
  13260. (and agendap m))
  13261. (setq buf (if agendap (marker-buffer m) (current-buffer))
  13262. pos (if agendap m (point)))
  13263. (with-current-buffer buf
  13264. (save-excursion
  13265. (save-restriction
  13266. (goto-char pos)
  13267. (setq cnt (1+ cnt))
  13268. (org-toggle-tag tag (if off 'off 'on))
  13269. (setq newhead (org-get-heading)))))
  13270. (and agendap (org-agenda-change-all-lines newhead m))))
  13271. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  13272. (defun org-tags-completion-function (string predicate &optional flag)
  13273. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  13274. (confirm (lambda (x) (stringp (car x)))))
  13275. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  13276. (setq s1 (match-string 1 string)
  13277. s2 (match-string 2 string))
  13278. (setq s1 "" s2 string))
  13279. (cond
  13280. ((eq flag nil)
  13281. ;; try completion
  13282. (setq rtn (try-completion s2 ctable confirm))
  13283. (if (stringp rtn)
  13284. (setq rtn
  13285. (concat s1 s2 (substring rtn (length s2))
  13286. (if (and org-add-colon-after-tag-completion
  13287. (assoc rtn ctable))
  13288. ":" ""))))
  13289. rtn)
  13290. ((eq flag t)
  13291. ;; all-completions
  13292. (all-completions s2 ctable confirm))
  13293. ((eq flag 'lambda)
  13294. ;; exact match?
  13295. (assoc s2 ctable)))))
  13296. (defun org-fast-tag-insert (kwd tags face &optional end)
  13297. "Insert KDW, and the TAGS, the latter with face FACE.
  13298. Also insert END."
  13299. (insert (format "%-12s" (concat kwd ":"))
  13300. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  13301. (or end "")))
  13302. (defun org-fast-tag-show-exit (flag)
  13303. (save-excursion
  13304. (org-goto-line 3)
  13305. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  13306. (replace-match ""))
  13307. (when flag
  13308. (end-of-line 1)
  13309. (org-move-to-column (- (window-width) 19) t)
  13310. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  13311. (defun org-set-current-tags-overlay (current prefix)
  13312. "Add an overlay to CURRENT tag with PREFIX."
  13313. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  13314. (if (featurep 'xemacs)
  13315. (org-overlay-display org-tags-overlay (concat prefix s)
  13316. 'secondary-selection)
  13317. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  13318. (org-overlay-display org-tags-overlay (concat prefix s)))))
  13319. (defvar org-last-tag-selection-key nil)
  13320. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  13321. "Fast tag selection with single keys.
  13322. CURRENT is the current list of tags in the headline, INHERITED is the
  13323. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  13324. possibly with grouping information. TODO-TABLE is a similar table with
  13325. TODO keywords, should these have keys assigned to them.
  13326. If the keys are nil, a-z are automatically assigned.
  13327. Returns the new tags string, or nil to not change the current settings."
  13328. (let* ((fulltable (append table todo-table))
  13329. (maxlen (apply 'max (mapcar
  13330. (lambda (x)
  13331. (if (stringp (car x)) (string-width (car x)) 0))
  13332. fulltable)))
  13333. (buf (current-buffer))
  13334. (expert (eq org-fast-tag-selection-single-key 'expert))
  13335. (buffer-tags nil)
  13336. (fwidth (+ maxlen 3 1 3))
  13337. (ncol (/ (- (window-width) 4) fwidth))
  13338. (i-face 'org-done)
  13339. (c-face 'org-todo)
  13340. tg cnt e c char c1 c2 ntable tbl rtn
  13341. ov-start ov-end ov-prefix
  13342. (exit-after-next org-fast-tag-selection-single-key)
  13343. (done-keywords org-done-keywords)
  13344. groups ingroup)
  13345. (save-excursion
  13346. (beginning-of-line 1)
  13347. (if (looking-at
  13348. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13349. (setq ov-start (match-beginning 1)
  13350. ov-end (match-end 1)
  13351. ov-prefix "")
  13352. (setq ov-start (1- (point-at-eol))
  13353. ov-end (1+ ov-start))
  13354. (skip-chars-forward "^\n\r")
  13355. (setq ov-prefix
  13356. (concat
  13357. (buffer-substring (1- (point)) (point))
  13358. (if (> (current-column) org-tags-column)
  13359. " "
  13360. (make-string (- org-tags-column (current-column)) ?\ ))))))
  13361. (move-overlay org-tags-overlay ov-start ov-end)
  13362. (save-window-excursion
  13363. (if expert
  13364. (set-buffer (get-buffer-create " *Org tags*"))
  13365. (delete-other-windows)
  13366. (split-window-vertically)
  13367. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  13368. (erase-buffer)
  13369. (org-set-local 'org-done-keywords done-keywords)
  13370. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  13371. (org-fast-tag-insert "Current" current c-face "\n\n")
  13372. (org-fast-tag-show-exit exit-after-next)
  13373. (org-set-current-tags-overlay current ov-prefix)
  13374. (setq tbl fulltable char ?a cnt 0)
  13375. (while (setq e (pop tbl))
  13376. (cond
  13377. ((equal (car e) :startgroup)
  13378. (push '() groups) (setq ingroup t)
  13379. (when (not (= cnt 0))
  13380. (setq cnt 0)
  13381. (insert "\n"))
  13382. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  13383. ((equal (car e) :endgroup)
  13384. (setq ingroup nil cnt 0)
  13385. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  13386. ((equal e '(:newline))
  13387. (when (not (= cnt 0))
  13388. (setq cnt 0)
  13389. (insert "\n")
  13390. (setq e (car tbl))
  13391. (while (equal (car tbl) '(:newline))
  13392. (insert "\n")
  13393. (setq tbl (cdr tbl)))))
  13394. ((equal e '(:grouptags)) nil)
  13395. (t
  13396. (setq tg (copy-sequence (car e)) c2 nil)
  13397. (if (cdr e)
  13398. (setq c (cdr e))
  13399. ;; automatically assign a character.
  13400. (setq c1 (string-to-char
  13401. (downcase (substring
  13402. tg (if (= (string-to-char tg) ?@) 1 0)))))
  13403. (if (or (rassoc c1 ntable) (rassoc c1 table))
  13404. (while (or (rassoc char ntable) (rassoc char table))
  13405. (setq char (1+ char)))
  13406. (setq c2 c1))
  13407. (setq c (or c2 char)))
  13408. (if ingroup (push tg (car groups)))
  13409. (setq tg (org-add-props tg nil 'face
  13410. (cond
  13411. ((not (assoc tg table))
  13412. (org-get-todo-face tg))
  13413. ((member tg current) c-face)
  13414. ((member tg inherited) i-face))))
  13415. (if (equal (caar tbl) :grouptags)
  13416. (org-add-props tg nil 'face 'org-tag-group))
  13417. (if (and (= cnt 0) (not ingroup)) (insert " "))
  13418. (insert "[" c "] " tg (make-string
  13419. (- fwidth 4 (length tg)) ?\ ))
  13420. (push (cons tg c) ntable)
  13421. (when (= (setq cnt (1+ cnt)) ncol)
  13422. (insert "\n")
  13423. (if ingroup (insert " "))
  13424. (setq cnt 0)))))
  13425. (setq ntable (nreverse ntable))
  13426. (insert "\n")
  13427. (goto-char (point-min))
  13428. (if (not expert) (org-fit-window-to-buffer))
  13429. (setq rtn
  13430. (catch 'exit
  13431. (while t
  13432. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  13433. (if (not groups) "no " "")
  13434. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  13435. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  13436. (setq org-last-tag-selection-key c)
  13437. (cond
  13438. ((= c ?\r) (throw 'exit t))
  13439. ((= c ?!)
  13440. (setq groups (not groups))
  13441. (goto-char (point-min))
  13442. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  13443. ((= c ?\C-c)
  13444. (if (not expert)
  13445. (org-fast-tag-show-exit
  13446. (setq exit-after-next (not exit-after-next)))
  13447. (setq expert nil)
  13448. (delete-other-windows)
  13449. (set-window-buffer (split-window-vertically) " *Org tags*")
  13450. (org-switch-to-buffer-other-window " *Org tags*")
  13451. (org-fit-window-to-buffer)))
  13452. ((or (= c ?\C-g)
  13453. (and (= c ?q) (not (rassoc c ntable))))
  13454. (org-detach-overlay org-tags-overlay)
  13455. (setq quit-flag t))
  13456. ((= c ?\ )
  13457. (setq current nil)
  13458. (if exit-after-next (setq exit-after-next 'now)))
  13459. ((= c ?\t)
  13460. (condition-case nil
  13461. (setq tg (org-icompleting-read
  13462. "Tag: "
  13463. (or buffer-tags
  13464. (with-current-buffer buf
  13465. (org-get-buffer-tags)))))
  13466. (quit (setq tg "")))
  13467. (when (string-match "\\S-" tg)
  13468. (add-to-list 'buffer-tags (list tg))
  13469. (if (member tg current)
  13470. (setq current (delete tg current))
  13471. (push tg current)))
  13472. (if exit-after-next (setq exit-after-next 'now)))
  13473. ((setq e (rassoc c todo-table) tg (car e))
  13474. (with-current-buffer buf
  13475. (save-excursion (org-todo tg)))
  13476. (if exit-after-next (setq exit-after-next 'now)))
  13477. ((setq e (rassoc c ntable) tg (car e))
  13478. (if (member tg current)
  13479. (setq current (delete tg current))
  13480. (loop for g in groups do
  13481. (if (member tg g)
  13482. (mapc (lambda (x)
  13483. (setq current (delete x current)))
  13484. g)))
  13485. (push tg current))
  13486. (if exit-after-next (setq exit-after-next 'now))))
  13487. ;; Create a sorted list
  13488. (setq current
  13489. (sort current
  13490. (lambda (a b)
  13491. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  13492. (if (eq exit-after-next 'now) (throw 'exit t))
  13493. (goto-char (point-min))
  13494. (beginning-of-line 2)
  13495. (delete-region (point) (point-at-eol))
  13496. (org-fast-tag-insert "Current" current c-face)
  13497. (org-set-current-tags-overlay current ov-prefix)
  13498. (while (re-search-forward
  13499. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  13500. (setq tg (match-string 1))
  13501. (add-text-properties
  13502. (match-beginning 1) (match-end 1)
  13503. (list 'face
  13504. (cond
  13505. ((member tg current) c-face)
  13506. ((member tg inherited) i-face)
  13507. (t (get-text-property (match-beginning 1) 'face))))))
  13508. (goto-char (point-min)))))
  13509. (org-detach-overlay org-tags-overlay)
  13510. (if rtn
  13511. (mapconcat 'identity current ":")
  13512. nil))))
  13513. (defun org-get-tags-string ()
  13514. "Get the TAGS string in the current headline."
  13515. (unless (org-at-heading-p t)
  13516. (error "Not on a heading"))
  13517. (save-excursion
  13518. (beginning-of-line 1)
  13519. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13520. (org-match-string-no-properties 1)
  13521. "")))
  13522. (defun org-get-tags ()
  13523. "Get the list of tags specified in the current headline."
  13524. (org-split-string (org-get-tags-string) ":"))
  13525. (defun org-get-buffer-tags ()
  13526. "Get a table of all tags used in the buffer, for completion."
  13527. (let (tags)
  13528. (save-excursion
  13529. (goto-char (point-min))
  13530. (while (re-search-forward
  13531. (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
  13532. (when (equal (char-after (point-at-bol 0)) ?*)
  13533. (mapc (lambda (x) (add-to-list 'tags x))
  13534. (org-split-string (org-match-string-no-properties 1) ":")))))
  13535. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  13536. (mapcar 'list tags)))
  13537. ;;;; The mapping API
  13538. (defun org-map-entries (func &optional match scope &rest skip)
  13539. "Call FUNC at each headline selected by MATCH in SCOPE.
  13540. FUNC is a function or a lisp form. The function will be called without
  13541. arguments, with the cursor positioned at the beginning of the headline.
  13542. The return values of all calls to the function will be collected and
  13543. returned as a list.
  13544. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  13545. does not need to preserve point. After evaluation, the cursor will be
  13546. moved to the end of the line (presumably of the headline of the
  13547. processed entry) and search continues from there. Under some
  13548. circumstances, this may not produce the wanted results. For example,
  13549. if you have removed (e.g. archived) the current (sub)tree it could
  13550. mean that the next entry will be skipped entirely. In such cases, you
  13551. can specify the position from where search should continue by making
  13552. FUNC set the variable `org-map-continue-from' to the desired buffer
  13553. position.
  13554. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  13555. Only headlines that are matched by this query will be considered during
  13556. the iteration. When MATCH is nil or t, all headlines will be
  13557. visited by the iteration.
  13558. SCOPE determines the scope of this command. It can be any of:
  13559. nil The current buffer, respecting the restriction if any
  13560. tree The subtree started with the entry at point
  13561. region The entries within the active region, if any
  13562. region-start-level
  13563. The entries within the active region, but only those at
  13564. the same level than the first one.
  13565. file The current buffer, without restriction
  13566. file-with-archives
  13567. The current buffer, and any archives associated with it
  13568. agenda All agenda files
  13569. agenda-with-archives
  13570. All agenda files with any archive files associated with them
  13571. \(file1 file2 ...)
  13572. If this is a list, all files in the list will be scanned
  13573. The remaining args are treated as settings for the skipping facilities of
  13574. the scanner. The following items can be given here:
  13575. archive skip trees with the archive tag
  13576. comment skip trees with the COMMENT keyword
  13577. function or Emacs Lisp form:
  13578. will be used as value for `org-agenda-skip-function', so
  13579. whenever the function returns a position, FUNC will not be
  13580. called for that entry and search will continue from the
  13581. position returned
  13582. If your function needs to retrieve the tags including inherited tags
  13583. at the *current* entry, you can use the value of the variable
  13584. `org-scanner-tags' which will be much faster than getting the value
  13585. with `org-get-tags-at'. If your function gets properties with
  13586. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  13587. to t around the call to `org-entry-properties' to get the same speedup.
  13588. Note that if your function moves around to retrieve tags and properties at
  13589. a *different* entry, you cannot use these techniques."
  13590. (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
  13591. (not (org-region-active-p)))
  13592. (let* ((org-agenda-archives-mode nil) ; just to make sure
  13593. (org-agenda-skip-archived-trees (memq 'archive skip))
  13594. (org-agenda-skip-comment-trees (memq 'comment skip))
  13595. (org-agenda-skip-function
  13596. (car (org-delete-all '(comment archive) skip)))
  13597. (org-tags-match-list-sublevels t)
  13598. (start-level (eq scope 'region-start-level))
  13599. matcher file res
  13600. org-todo-keywords-for-agenda
  13601. org-done-keywords-for-agenda
  13602. org-todo-keyword-alist-for-agenda
  13603. org-drawers-for-agenda
  13604. org-tag-alist-for-agenda
  13605. todo-only)
  13606. (cond
  13607. ((eq match t) (setq matcher t))
  13608. ((eq match nil) (setq matcher t))
  13609. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  13610. (save-window-excursion
  13611. (save-restriction
  13612. (cond ((eq scope 'tree)
  13613. (org-back-to-heading t)
  13614. (org-narrow-to-subtree)
  13615. (setq scope nil))
  13616. ((and (or (eq scope 'region) (eq scope 'region-start-level))
  13617. (org-region-active-p))
  13618. ;; If needed, set start-level to a string like "2"
  13619. (when start-level
  13620. (save-excursion
  13621. (goto-char (region-beginning))
  13622. (unless (org-at-heading-p) (outline-next-heading))
  13623. (setq start-level (org-current-level))))
  13624. (narrow-to-region (region-beginning)
  13625. (save-excursion
  13626. (goto-char (region-end))
  13627. (unless (and (bolp) (org-at-heading-p))
  13628. (outline-next-heading))
  13629. (point)))
  13630. (setq scope nil)))
  13631. (if (not scope)
  13632. (progn
  13633. (org-agenda-prepare-buffers
  13634. (list (buffer-file-name (current-buffer))))
  13635. (setq res (org-scan-tags func matcher todo-only start-level)))
  13636. ;; Get the right scope
  13637. (cond
  13638. ((and scope (listp scope) (symbolp (car scope)))
  13639. (setq scope (eval scope)))
  13640. ((eq scope 'agenda)
  13641. (setq scope (org-agenda-files t)))
  13642. ((eq scope 'agenda-with-archives)
  13643. (setq scope (org-agenda-files t))
  13644. (setq scope (org-add-archive-files scope)))
  13645. ((eq scope 'file)
  13646. (setq scope (list (buffer-file-name))))
  13647. ((eq scope 'file-with-archives)
  13648. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  13649. (org-agenda-prepare-buffers scope)
  13650. (while (setq file (pop scope))
  13651. (with-current-buffer (org-find-base-buffer-visiting file)
  13652. (save-excursion
  13653. (save-restriction
  13654. (widen)
  13655. (goto-char (point-min))
  13656. (setq res (append res (org-scan-tags func matcher todo-only))))))))))
  13657. res)))
  13658. ;;;; Properties
  13659. ;;; Setting and retrieving properties
  13660. (defconst org-special-properties
  13661. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  13662. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED" "FILE" "CLOCKSUM" "CLOCKSUM_T")
  13663. "The special properties valid in Org-mode.
  13664. These are properties that are not defined in the property drawer,
  13665. but in some other way.")
  13666. (defconst org-default-properties
  13667. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  13668. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  13669. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  13670. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  13671. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  13672. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  13673. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  13674. "Some properties that are used by Org-mode for various purposes.
  13675. Being in this list makes sure that they are offered for completion.")
  13676. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  13677. "Regular expression matching the first line of a property drawer.")
  13678. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  13679. "Regular expression matching the last line of a property drawer.")
  13680. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  13681. "Regular expression matching the first line of a property drawer.")
  13682. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  13683. "Regular expression matching the first line of a property drawer.")
  13684. (defconst org-property-drawer-re
  13685. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  13686. org-property-end-re "\\)\n?")
  13687. "Matches an entire property drawer.")
  13688. (defconst org-clock-drawer-re
  13689. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  13690. org-property-end-re "\\)\n?")
  13691. "Matches an entire clock drawer.")
  13692. (defsubst org-re-property (property)
  13693. "Return a regexp matching a PROPERTY line.
  13694. Match group 1 will be set to the value."
  13695. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)"))
  13696. (defsubst org-re-property-keyword (property)
  13697. "Return a regexp matching a PROPERTY line, possibly with no
  13698. value for the property."
  13699. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)?"))
  13700. (defun org-property-action ()
  13701. "Do an action on properties."
  13702. (interactive)
  13703. (let (c)
  13704. (org-at-property-p)
  13705. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  13706. (setq c (read-char-exclusive))
  13707. (cond
  13708. ((equal c ?s)
  13709. (call-interactively 'org-set-property))
  13710. ((equal c ?d)
  13711. (call-interactively 'org-delete-property))
  13712. ((equal c ?D)
  13713. (call-interactively 'org-delete-property-globally))
  13714. ((equal c ?c)
  13715. (call-interactively 'org-compute-property-at-point))
  13716. (t (error "No such property action %c" c)))))
  13717. (defun org-inc-effort ()
  13718. "Increment the value of the effort property in the current entry."
  13719. (interactive)
  13720. (org-set-effort nil t))
  13721. (defvar org-clock-effort) ;; Defined in org-clock.el
  13722. (defvar org-clock-current-task) ;; Defined in org-clock.el
  13723. (defun org-set-effort (&optional value increment)
  13724. "Set the effort property of the current entry.
  13725. With numerical prefix arg, use the nth allowed value, 0 stands for the
  13726. 10th allowed value.
  13727. When INCREMENT is non-nil, set the property to the next allowed value."
  13728. (interactive "P")
  13729. (if (equal value 0) (setq value 10))
  13730. (let* ((completion-ignore-case t)
  13731. (prop org-effort-property)
  13732. (cur (org-entry-get nil prop))
  13733. (allowed (org-property-get-allowed-values nil prop 'table))
  13734. (existing (mapcar 'list (org-property-values prop)))
  13735. (heading (nth 4 (org-heading-components)))
  13736. rpl
  13737. (val (cond
  13738. ((stringp value) value)
  13739. ((and allowed (integerp value))
  13740. (or (car (nth (1- value) allowed))
  13741. (car (org-last allowed))))
  13742. ((and allowed increment)
  13743. (or (caadr (member (list cur) allowed))
  13744. (error "Allowed effort values are not set")))
  13745. (allowed
  13746. (message "Select 1-9,0, [RET%s]: %s"
  13747. (if cur (concat "=" cur) "")
  13748. (mapconcat 'car allowed " "))
  13749. (setq rpl (read-char-exclusive))
  13750. (if (equal rpl ?\r)
  13751. cur
  13752. (setq rpl (- rpl ?0))
  13753. (if (equal rpl 0) (setq rpl 10))
  13754. (if (and (> rpl 0) (<= rpl (length allowed)))
  13755. (car (nth (1- rpl) allowed))
  13756. (org-completing-read "Effort: " allowed nil))))
  13757. (t
  13758. (let (org-completion-use-ido org-completion-use-iswitchb)
  13759. (org-completing-read
  13760. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  13761. (concat "[" cur "]") "")
  13762. ": ")
  13763. existing nil nil "" nil cur))))))
  13764. (unless (equal (org-entry-get nil prop) val)
  13765. (org-entry-put nil prop val))
  13766. (save-excursion
  13767. (org-back-to-heading t)
  13768. (put-text-property (point-at-bol) (point-at-eol) 'org-effort val))
  13769. (when (string= heading org-clock-current-task)
  13770. (setq org-clock-effort (get-text-property (point-at-bol) 'org-effort))
  13771. (org-clock-update-mode-line))
  13772. (message "%s is now %s" prop val)))
  13773. (defun org-at-property-p ()
  13774. "Is cursor inside a property drawer?"
  13775. (save-excursion
  13776. (beginning-of-line 1)
  13777. (when (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))
  13778. (save-match-data ;; Used by calling procedures
  13779. (let ((p (point))
  13780. (range (unless (org-before-first-heading-p)
  13781. (org-get-property-block))))
  13782. (and range (<= (car range) p) (< p (cdr range))))))))
  13783. (defun org-get-property-block (&optional beg end force)
  13784. "Return the (beg . end) range of the body of the property drawer.
  13785. BEG and END are the beginning and end of the current subtree, or of
  13786. the part before the first headline. If they are not given, they will
  13787. be found. If the drawer does not exist and FORCE is non-nil, create
  13788. the drawer."
  13789. (catch 'exit
  13790. (save-excursion
  13791. (let* ((beg (or beg (and (org-before-first-heading-p) (point-min))
  13792. (progn (org-back-to-heading t) (point))))
  13793. (end (or end (and (not (outline-next-heading)) (point-max))
  13794. (point))))
  13795. (goto-char beg)
  13796. (if (re-search-forward org-property-start-re end t)
  13797. (setq beg (1+ (match-end 0)))
  13798. (if force
  13799. (save-excursion
  13800. (org-insert-property-drawer)
  13801. (setq end (progn (outline-next-heading) (point))))
  13802. (throw 'exit nil))
  13803. (goto-char beg)
  13804. (if (re-search-forward org-property-start-re end t)
  13805. (setq beg (1+ (match-end 0)))))
  13806. (if (re-search-forward org-property-end-re end t)
  13807. (setq end (match-beginning 0))
  13808. (or force (throw 'exit nil))
  13809. (goto-char beg)
  13810. (setq end beg)
  13811. (org-indent-line)
  13812. (insert ":END:\n"))
  13813. (cons beg end)))))
  13814. (defun org-entry-properties (&optional pom which specific)
  13815. "Get all properties of the entry at point-or-marker POM.
  13816. This includes the TODO keyword, the tags, time strings for deadline,
  13817. scheduled, and clocking, and any additional properties defined in the
  13818. entry. The return value is an alist, keys may occur multiple times
  13819. if the property key was used several times.
  13820. POM may also be nil, in which case the current entry is used.
  13821. If WHICH is nil or `all', get all properties. If WHICH is
  13822. `special' or `standard', only get that subclass. If WHICH
  13823. is a string only get exactly this property. SPECIFIC can be a string, the
  13824. specific property we are interested in. Specifying it can speed
  13825. things up because then unnecessary parsing is avoided."
  13826. (setq which (or which 'all))
  13827. (org-with-point-at pom
  13828. (let ((clockstr (substring org-clock-string 0 -1))
  13829. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
  13830. (case-fold-search nil)
  13831. beg end range props sum-props key key1 value string clocksum clocksumt)
  13832. (save-excursion
  13833. (when (condition-case nil
  13834. (and (derived-mode-p 'org-mode) (org-back-to-heading t))
  13835. (error nil))
  13836. (setq beg (point))
  13837. (setq sum-props (get-text-property (point) 'org-summaries))
  13838. (setq clocksum (get-text-property (point) :org-clock-minutes)
  13839. clocksumt (get-text-property (point) :org-clock-minutes-today))
  13840. (outline-next-heading)
  13841. (setq end (point))
  13842. (when (memq which '(all special))
  13843. ;; Get the special properties, like TODO and tags
  13844. (goto-char beg)
  13845. (when (and (or (not specific) (string= specific "TODO"))
  13846. (looking-at org-todo-line-regexp) (match-end 2))
  13847. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  13848. (when (and (or (not specific) (string= specific "PRIORITY"))
  13849. (looking-at org-priority-regexp))
  13850. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  13851. (when (or (not specific) (string= specific "FILE"))
  13852. (push (cons "FILE" buffer-file-name) props))
  13853. (when (and (or (not specific) (string= specific "TAGS"))
  13854. (setq value (org-get-tags-string))
  13855. (string-match "\\S-" value))
  13856. (push (cons "TAGS" value) props))
  13857. (when (and (or (not specific) (string= specific "ALLTAGS"))
  13858. (setq value (org-get-tags-at)))
  13859. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
  13860. ":"))
  13861. props))
  13862. (when (or (not specific) (string= specific "BLOCKED"))
  13863. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
  13864. (when (or (not specific)
  13865. (member specific
  13866. '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
  13867. "TIMESTAMP" "TIMESTAMP_IA")))
  13868. (catch 'match
  13869. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  13870. (setq key (if (match-end 1)
  13871. (substring (org-match-string-no-properties 1)
  13872. 0 -1))
  13873. string (if (equal key clockstr)
  13874. (org-trim
  13875. (buffer-substring-no-properties
  13876. (match-beginning 3) (goto-char
  13877. (point-at-eol))))
  13878. (substring (org-match-string-no-properties 3)
  13879. 1 -1)))
  13880. ;; Get the correct property name from the key. This is
  13881. ;; necessary if the user has configured time keywords.
  13882. (setq key1 (concat key ":"))
  13883. (cond
  13884. ((not key)
  13885. (setq key
  13886. (if (= (char-after (match-beginning 3)) ?\[)
  13887. "TIMESTAMP_IA" "TIMESTAMP")))
  13888. ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
  13889. ((equal key1 org-deadline-string) (setq key "DEADLINE"))
  13890. ((equal key1 org-closed-string) (setq key "CLOSED"))
  13891. ((equal key1 org-clock-string) (setq key "CLOCK")))
  13892. (if (and specific (equal key specific) (not (equal key "CLOCK")))
  13893. (progn
  13894. (push (cons key string) props)
  13895. ;; no need to search further if match is found
  13896. (throw 'match t))
  13897. (when (or (equal key "CLOCK") (not (assoc key props)))
  13898. (push (cons key string) props)))))))
  13899. (when (memq which '(all standard))
  13900. ;; Get the standard properties, like :PROP: ...
  13901. (setq range (org-get-property-block beg end))
  13902. (when range
  13903. (goto-char (car range))
  13904. (while (re-search-forward
  13905. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  13906. (cdr range) t)
  13907. (setq key (org-match-string-no-properties 1)
  13908. value (org-trim (or (org-match-string-no-properties 2) "")))
  13909. (unless (member key excluded)
  13910. (push (cons key (or value "")) props)))))
  13911. (if clocksum
  13912. (push (cons "CLOCKSUM"
  13913. (org-columns-number-to-string (/ (float clocksum) 60.)
  13914. 'add_times))
  13915. props))
  13916. (if clocksumt
  13917. (push (cons "CLOCKSUM_T"
  13918. (org-columns-number-to-string (/ (float clocksumt) 60.)
  13919. 'add_times))
  13920. props))
  13921. (unless (assoc "CATEGORY" props)
  13922. (push (cons "CATEGORY" (org-get-category)) props))
  13923. (append sum-props (nreverse props)))))))
  13924. (defun org-entry-get (pom property &optional inherit literal-nil)
  13925. "Get value of PROPERTY for entry or content at point-or-marker POM.
  13926. If INHERIT is non-nil and the entry does not have the property,
  13927. then also check higher levels of the hierarchy.
  13928. If INHERIT is the symbol `selective', use inheritance only if the setting
  13929. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  13930. If the property is present but empty, the return value is the empty string.
  13931. If the property is not present at all, nil is returned.
  13932. If LITERAL-NIL is set, return the string value \"nil\" as a string,
  13933. do not interpret it as the list atom nil. This is used for inheritance
  13934. when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
  13935. (org-with-point-at pom
  13936. (if (and inherit (if (eq inherit 'selective)
  13937. (org-property-inherit-p property)
  13938. t))
  13939. (org-entry-get-with-inheritance property literal-nil)
  13940. (if (member property org-special-properties)
  13941. ;; We need a special property. Use `org-entry-properties' to
  13942. ;; retrieve it, but specify the wanted property
  13943. (cdr (assoc property (org-entry-properties nil 'special property)))
  13944. (let ((range (org-get-property-block)))
  13945. (when (and range (not (eq (car range) (cdr range))))
  13946. (let* ((props (list (or (assoc property org-file-properties)
  13947. (assoc property org-global-properties)
  13948. (assoc property org-global-properties-fixed))))
  13949. (ap (lambda (key)
  13950. (when (re-search-forward
  13951. (org-re-property key) (cdr range) t)
  13952. (setq props
  13953. (org-update-property-plist
  13954. key
  13955. (if (match-end 1)
  13956. (org-match-string-no-properties 1) "")
  13957. props)))))
  13958. val)
  13959. (goto-char (car range))
  13960. (funcall ap property)
  13961. (goto-char (car range))
  13962. (while (funcall ap (concat property "+")))
  13963. (setq val (cdr (assoc property props)))
  13964. (when val (if literal-nil val (org-not-nil val))))))))))
  13965. (defun org-property-or-variable-value (var &optional inherit)
  13966. "Check if there is a property fixing the value of VAR.
  13967. If yes, return this value. If not, return the current value of the variable."
  13968. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  13969. (if (and prop (stringp prop) (string-match "\\S-" prop))
  13970. (read prop)
  13971. (symbol-value var))))
  13972. (defun org-entry-delete (pom property &optional delete-empty-drawer)
  13973. "Delete the property PROPERTY from entry at point-or-marker POM.
  13974. When optional argument DELETE-EMPTY-DRAWER is a string, it defines
  13975. an empty drawer to delete."
  13976. (org-with-point-at pom
  13977. (if (member property org-special-properties)
  13978. nil ; cannot delete these properties.
  13979. (let ((range (org-get-property-block)))
  13980. (if (and range
  13981. (goto-char (car range))
  13982. (re-search-forward
  13983. (org-re-property property)
  13984. (cdr range) t))
  13985. (progn
  13986. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  13987. (and delete-empty-drawer
  13988. (org-remove-empty-drawer-at
  13989. delete-empty-drawer (car range)))
  13990. t)
  13991. nil)))))
  13992. ;; Multi-values properties are properties that contain multiple values
  13993. ;; These values are assumed to be single words, separated by whitespace.
  13994. (defun org-entry-add-to-multivalued-property (pom property value)
  13995. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  13996. (let* ((old (org-entry-get pom property))
  13997. (values (and old (org-split-string old "[ \t]"))))
  13998. (setq value (org-entry-protect-space value))
  13999. (unless (member value values)
  14000. (setq values (append values (list value)))
  14001. (org-entry-put pom property
  14002. (mapconcat 'identity values " ")))))
  14003. (defun org-entry-remove-from-multivalued-property (pom property value)
  14004. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  14005. (let* ((old (org-entry-get pom property))
  14006. (values (and old (org-split-string old "[ \t]"))))
  14007. (setq value (org-entry-protect-space value))
  14008. (when (member value values)
  14009. (setq values (delete value values))
  14010. (org-entry-put pom property
  14011. (mapconcat 'identity values " ")))))
  14012. (defun org-entry-member-in-multivalued-property (pom property value)
  14013. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  14014. (let* ((old (org-entry-get pom property))
  14015. (values (and old (org-split-string old "[ \t]"))))
  14016. (setq value (org-entry-protect-space value))
  14017. (member value values)))
  14018. (defun org-entry-get-multivalued-property (pom property)
  14019. "Return a list of values in a multivalued property."
  14020. (let* ((value (org-entry-get pom property))
  14021. (values (and value (org-split-string value "[ \t]"))))
  14022. (mapcar 'org-entry-restore-space values)))
  14023. (defun org-entry-put-multivalued-property (pom property &rest values)
  14024. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  14025. VALUES should be a list of strings. Spaces will be protected."
  14026. (org-entry-put pom property
  14027. (mapconcat 'org-entry-protect-space values " "))
  14028. (let* ((value (org-entry-get pom property))
  14029. (values (and value (org-split-string value "[ \t]"))))
  14030. (mapcar 'org-entry-restore-space values)))
  14031. (defun org-entry-protect-space (s)
  14032. "Protect spaces and newline in string S."
  14033. (while (string-match " " s)
  14034. (setq s (replace-match "%20" t t s)))
  14035. (while (string-match "\n" s)
  14036. (setq s (replace-match "%0A" t t s)))
  14037. s)
  14038. (defun org-entry-restore-space (s)
  14039. "Restore spaces and newline in string S."
  14040. (while (string-match "%20" s)
  14041. (setq s (replace-match " " t t s)))
  14042. (while (string-match "%0A" s)
  14043. (setq s (replace-match "\n" t t s)))
  14044. s)
  14045. (defvar org-entry-property-inherited-from (make-marker)
  14046. "Marker pointing to the entry from where a property was inherited.
  14047. Each call to `org-entry-get-with-inheritance' will set this marker to the
  14048. location of the entry where the inheritance search matched. If there was
  14049. no match, the marker will point nowhere.
  14050. Note that also `org-entry-get' calls this function, if the INHERIT flag
  14051. is set.")
  14052. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  14053. "Get PROPERTY of entry or content at point, search higher levels if needed.
  14054. The search will stop at the first ancestor which has the property defined.
  14055. If the value found is \"nil\", return nil to show that the property
  14056. should be considered as undefined (this is the meaning of nil here).
  14057. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  14058. (move-marker org-entry-property-inherited-from nil)
  14059. (let (tmp)
  14060. (save-excursion
  14061. (save-restriction
  14062. (widen)
  14063. (catch 'ex
  14064. (while t
  14065. (when (setq tmp (org-entry-get nil property nil 'literal-nil))
  14066. (or (ignore-errors (org-back-to-heading t))
  14067. (goto-char (point-min)))
  14068. (move-marker org-entry-property-inherited-from (point))
  14069. (throw 'ex tmp))
  14070. (or (ignore-errors (org-up-heading-safe))
  14071. (throw 'ex nil))))))
  14072. (setq tmp (or tmp
  14073. (cdr (assoc property org-file-properties))
  14074. (cdr (assoc property org-global-properties))
  14075. (cdr (assoc property org-global-properties-fixed))))
  14076. (if literal-nil tmp (org-not-nil tmp))))
  14077. (defvar org-property-changed-functions nil
  14078. "Hook called when the value of a property has changed.
  14079. Each hook function should accept two arguments, the name of the property
  14080. and the new value.")
  14081. (defun org-entry-put (pom property value)
  14082. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  14083. (org-with-point-at pom
  14084. (org-back-to-heading t)
  14085. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  14086. range)
  14087. (cond
  14088. ((equal property "TODO")
  14089. (when (and (stringp value) (string-match "\\S-" value)
  14090. (not (member value org-todo-keywords-1)))
  14091. (error "\"%s\" is not a valid TODO state" value))
  14092. (if (or (not value)
  14093. (not (string-match "\\S-" value)))
  14094. (setq value 'none))
  14095. (org-todo value)
  14096. (org-set-tags nil 'align))
  14097. ((equal property "PRIORITY")
  14098. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  14099. (string-to-char value) ?\ ))
  14100. (org-set-tags nil 'align))
  14101. ((equal property "CLOCKSUM")
  14102. (if (not (re-search-forward
  14103. (concat org-clock-string ".*\\]--\\(\\[[^]]+\\]\\)") nil t))
  14104. (error "Cannot find a clock log")
  14105. (goto-char (- (match-end 1) 2))
  14106. (cond
  14107. ((eq value 'earlier) (org-timestamp-down))
  14108. ((eq value 'later) (org-timestamp-up)))
  14109. (org-clock-sum-current-item)))
  14110. ((equal property "SCHEDULED")
  14111. (if (re-search-forward org-scheduled-time-regexp end t)
  14112. (cond
  14113. ((eq value 'earlier) (org-timestamp-change -1 'day))
  14114. ((eq value 'later) (org-timestamp-change 1 'day))
  14115. (t (call-interactively 'org-schedule)))
  14116. (call-interactively 'org-schedule)))
  14117. ((equal property "DEADLINE")
  14118. (if (re-search-forward org-deadline-time-regexp end t)
  14119. (cond
  14120. ((eq value 'earlier) (org-timestamp-change -1 'day))
  14121. ((eq value 'later) (org-timestamp-change 1 'day))
  14122. (t (call-interactively 'org-deadline)))
  14123. (call-interactively 'org-deadline)))
  14124. ((member property org-special-properties)
  14125. (error "The %s property can not yet be set with `org-entry-put'"
  14126. property))
  14127. (t ; a non-special property
  14128. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  14129. (setq range (org-get-property-block beg end 'force))
  14130. (goto-char (car range))
  14131. (if (re-search-forward
  14132. (org-re-property-keyword property) (cdr range) t)
  14133. (progn
  14134. (delete-region (match-beginning 0) (match-end 0))
  14135. (goto-char (match-beginning 0)))
  14136. (goto-char (cdr range))
  14137. (insert "\n")
  14138. (backward-char 1)
  14139. (org-indent-line))
  14140. (insert ":" property ":")
  14141. (and value (insert " " value))
  14142. (org-indent-line)))))
  14143. (run-hook-with-args 'org-property-changed-functions property value)))
  14144. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  14145. "Get all property keys in the current buffer.
  14146. With INCLUDE-SPECIALS, also list the special properties that reflect things
  14147. like tags and TODO state.
  14148. With INCLUDE-DEFAULTS, also include properties that has special meaning
  14149. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING
  14150. and others.
  14151. With INCLUDE-COLUMNS, also include property names given in COLUMN
  14152. formats in the current buffer."
  14153. (let (rtn range cfmt s p)
  14154. (save-excursion
  14155. (save-restriction
  14156. (widen)
  14157. (goto-char (point-min))
  14158. (while (re-search-forward org-property-start-re nil t)
  14159. (setq range (org-get-property-block))
  14160. (goto-char (car range))
  14161. (while (re-search-forward
  14162. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  14163. (cdr range) t)
  14164. (add-to-list 'rtn (org-match-string-no-properties 1)))
  14165. (outline-next-heading))))
  14166. (when include-specials
  14167. (setq rtn (append org-special-properties rtn)))
  14168. (when include-defaults
  14169. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  14170. (add-to-list 'rtn org-effort-property))
  14171. (when include-columns
  14172. (save-excursion
  14173. (save-restriction
  14174. (widen)
  14175. (goto-char (point-min))
  14176. (while (re-search-forward
  14177. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  14178. nil t)
  14179. (setq cfmt (match-string 2) s 0)
  14180. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  14181. cfmt s)
  14182. (setq s (match-end 0)
  14183. p (match-string 1 cfmt))
  14184. (unless (or (equal p "ITEM")
  14185. (member p org-special-properties))
  14186. (add-to-list 'rtn (match-string 1 cfmt))))))))
  14187. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  14188. (defun org-property-values (key)
  14189. "Return a list of all values of property KEY in the current buffer."
  14190. (save-excursion
  14191. (save-restriction
  14192. (widen)
  14193. (goto-char (point-min))
  14194. (let ((re (org-re-property key))
  14195. values)
  14196. (while (re-search-forward re nil t)
  14197. (add-to-list 'values (org-trim (match-string 1))))
  14198. (delete "" values)))))
  14199. (defun org-insert-property-drawer ()
  14200. "Insert a property drawer into the current entry."
  14201. (org-back-to-heading t)
  14202. (looking-at org-outline-regexp)
  14203. (let ((indent (if org-adapt-indentation
  14204. (- (match-end 0) (match-beginning 0))
  14205. 0))
  14206. (beg (point))
  14207. (re (concat "^[ \t]*" org-keyword-time-regexp))
  14208. end hiddenp)
  14209. (outline-next-heading)
  14210. (setq end (point))
  14211. (goto-char beg)
  14212. (while (re-search-forward re end t))
  14213. (setq hiddenp (outline-invisible-p))
  14214. (end-of-line 1)
  14215. (and (equal (char-after) ?\n) (forward-char 1))
  14216. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  14217. (if (member (match-string 1) '("CLOCK:" ":END:"))
  14218. ;; just skip this line
  14219. (beginning-of-line 2)
  14220. ;; Drawer start, find the end
  14221. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  14222. (beginning-of-line 1)))
  14223. (org-skip-over-state-notes)
  14224. (skip-chars-backward " \t\n\r")
  14225. (if (eq (char-before) ?*) (forward-char 1))
  14226. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  14227. (beginning-of-line 0)
  14228. (org-indent-to-column indent)
  14229. (beginning-of-line 2)
  14230. (org-indent-to-column indent)
  14231. (beginning-of-line 0)
  14232. (if hiddenp
  14233. (save-excursion
  14234. (org-back-to-heading t)
  14235. (hide-entry))
  14236. (org-flag-drawer t))))
  14237. (defun org-insert-drawer (&optional arg drawer)
  14238. "Insert a drawer at point.
  14239. Optional argument DRAWER, when non-nil, is a string representing
  14240. drawer's name. Otherwise, the user is prompted for a name.
  14241. If a region is active, insert the drawer around that region
  14242. instead.
  14243. Point is left between drawer's boundaries."
  14244. (interactive "P")
  14245. (let* ((logbook (if (stringp org-log-into-drawer) org-log-into-drawer
  14246. "LOGBOOK"))
  14247. ;; SYSTEM-DRAWERS is a list of drawer names that are used
  14248. ;; internally by Org. They are meant to be inserted
  14249. ;; automatically.
  14250. (system-drawers `("CLOCK" ,logbook "PROPERTIES"))
  14251. ;; Remove system drawers from list. Note: For some reason,
  14252. ;; `org-completing-read' ignores the predicate while
  14253. ;; `completing-read' handles it fine.
  14254. (drawer (if arg "PROPERTIES"
  14255. (or drawer
  14256. (completing-read
  14257. "Drawer: " org-drawers
  14258. (lambda (d) (not (member d system-drawers))))))))
  14259. (cond
  14260. ;; With C-u, fall back on `org-insert-property-drawer'
  14261. (arg (org-insert-property-drawer))
  14262. ;; With an active region, insert a drawer at point.
  14263. ((not (org-region-active-p))
  14264. (progn
  14265. (unless (bolp) (insert "\n"))
  14266. (insert (format ":%s:\n\n:END:\n" drawer))
  14267. (forward-line -2)))
  14268. ;; Otherwise, insert the drawer at point
  14269. (t
  14270. (let ((rbeg (region-beginning))
  14271. (rend (copy-marker (region-end))))
  14272. (unwind-protect
  14273. (progn
  14274. (goto-char rbeg)
  14275. (beginning-of-line)
  14276. (when (save-excursion
  14277. (re-search-forward org-outline-regexp-bol rend t))
  14278. (error "Drawers cannot contain headlines"))
  14279. ;; Position point at the beginning of the first
  14280. ;; non-blank line in region. Insert drawer's opening
  14281. ;; there, then indent it.
  14282. (org-skip-whitespace)
  14283. (beginning-of-line)
  14284. (insert ":" drawer ":\n")
  14285. (forward-line -1)
  14286. (indent-for-tab-command)
  14287. ;; Move point to the beginning of the first blank line
  14288. ;; after the last non-blank line in region. Insert
  14289. ;; drawer's closing, then indent it.
  14290. (goto-char rend)
  14291. (skip-chars-backward " \r\t\n")
  14292. (insert "\n:END:")
  14293. (deactivate-mark t)
  14294. (indent-for-tab-command)
  14295. (unless (eolp) (insert "\n")))
  14296. ;; Clear marker, whatever the outcome of insertion is.
  14297. (set-marker rend nil)))))))
  14298. (defvar org-property-set-functions-alist nil
  14299. "Property set function alist.
  14300. Each entry should have the following format:
  14301. (PROPERTY . READ-FUNCTION)
  14302. The read function will be called with the same argument as
  14303. `org-completing-read'.")
  14304. (defun org-set-property-function (property)
  14305. "Get the function that should be used to set PROPERTY.
  14306. This is computed according to `org-property-set-functions-alist'."
  14307. (or (cdr (assoc property org-property-set-functions-alist))
  14308. 'org-completing-read))
  14309. (defun org-read-property-value (property)
  14310. "Read PROPERTY value from user."
  14311. (let* ((completion-ignore-case t)
  14312. (allowed (org-property-get-allowed-values nil property 'table))
  14313. (cur (org-entry-get nil property))
  14314. (prompt (concat property " value"
  14315. (if (and cur (string-match "\\S-" cur))
  14316. (concat " [" cur "]") "") ": "))
  14317. (set-function (org-set-property-function property))
  14318. (val (if allowed
  14319. (funcall set-function prompt allowed nil
  14320. (not (get-text-property 0 'org-unrestricted
  14321. (caar allowed))))
  14322. (let (org-completion-use-ido org-completion-use-iswitchb)
  14323. (funcall set-function prompt
  14324. (mapcar 'list (org-property-values property))
  14325. nil nil "" nil cur)))))
  14326. (if (equal val "")
  14327. cur
  14328. val)))
  14329. (defvar org-last-set-property nil)
  14330. (defvar org-last-set-property-value nil)
  14331. (defun org-read-property-name ()
  14332. "Read a property name."
  14333. (let* ((completion-ignore-case t)
  14334. (keys (org-buffer-property-keys nil t t))
  14335. (default-prop (or (save-excursion
  14336. (save-match-data
  14337. (beginning-of-line)
  14338. (and (looking-at "^\\s-*:\\([^:\n]+\\):")
  14339. (null (string= (match-string 1) "END"))
  14340. (match-string 1))))
  14341. org-last-set-property))
  14342. (property (org-icompleting-read
  14343. (concat "Property"
  14344. (if default-prop (concat " [" default-prop "]") "")
  14345. ": ")
  14346. (mapcar 'list keys)
  14347. nil nil nil nil
  14348. default-prop)))
  14349. (if (member property keys)
  14350. property
  14351. (or (cdr (assoc (downcase property)
  14352. (mapcar (lambda (x) (cons (downcase x) x))
  14353. keys)))
  14354. property))))
  14355. (defun org-set-property-and-value (use-last)
  14356. "Allow to set [PROPERTY]: [value] direction from prompt.
  14357. When use-default, don't even ask, just use the last
  14358. \"[PROPERTY]: [value]\" string from the history."
  14359. (interactive "P")
  14360. (let* ((completion-ignore-case t)
  14361. (pv (or (and use-last org-last-set-property-value)
  14362. (org-completing-read
  14363. "Enter a \"[Property]: [value]\" pair: "
  14364. nil nil nil nil nil
  14365. org-last-set-property-value)))
  14366. prop val)
  14367. (when (string-match "^[ \t]*\\([^:]+\\):[ \t]*\\(.*\\)[ \t]*$" pv)
  14368. (setq prop (match-string 1 pv)
  14369. val (match-string 2 pv))
  14370. (org-set-property prop val))))
  14371. (defun org-set-property (property value)
  14372. "In the current entry, set PROPERTY to VALUE.
  14373. When called interactively, this will prompt for a property name, offering
  14374. completion on existing and default properties. And then it will prompt
  14375. for a value, offering completion either on allowed values (via an inherited
  14376. xxx_ALL property) or on existing values in other instances of this property
  14377. in the current file."
  14378. (interactive (list nil nil))
  14379. (let* ((property (or property (org-read-property-name)))
  14380. (value (or value (org-read-property-value property)))
  14381. (fn (cdr (assoc property org-properties-postprocess-alist))))
  14382. (setq org-last-set-property property)
  14383. (setq org-last-set-property-value (concat property ": " value))
  14384. ;; Possibly postprocess the inserted value:
  14385. (when fn (setq value (funcall fn value)))
  14386. (unless (equal (org-entry-get nil property) value)
  14387. (org-entry-put nil property value))))
  14388. (defun org-delete-property (property &optional delete-empty-drawer)
  14389. "In the current entry, delete PROPERTY.
  14390. When optional argument DELETE-EMPTY-DRAWER is a string, it defines
  14391. an empty drawer to delete."
  14392. (interactive
  14393. (let* ((completion-ignore-case t)
  14394. (prop (org-icompleting-read "Property: "
  14395. (org-entry-properties nil 'standard))))
  14396. (list prop)))
  14397. (message "Property %s %s" property
  14398. (if (org-entry-delete nil property delete-empty-drawer)
  14399. "deleted"
  14400. "was not present in the entry")))
  14401. (defun org-delete-property-globally (property)
  14402. "Remove PROPERTY globally, from all entries."
  14403. (interactive
  14404. (let* ((completion-ignore-case t)
  14405. (prop (org-icompleting-read
  14406. "Globally remove property: "
  14407. (mapcar 'list (org-buffer-property-keys)))))
  14408. (list prop)))
  14409. (save-excursion
  14410. (save-restriction
  14411. (widen)
  14412. (goto-char (point-min))
  14413. (let ((cnt 0))
  14414. (while (re-search-forward
  14415. (org-re-property property)
  14416. nil t)
  14417. (setq cnt (1+ cnt))
  14418. (delete-region (match-beginning 0) (1+ (point-at-eol))))
  14419. (message "Property \"%s\" removed from %d entries" property cnt)))))
  14420. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  14421. (defun org-compute-property-at-point ()
  14422. "Compute the property at point.
  14423. This looks for an enclosing column format, extracts the operator and
  14424. then applies it to the property in the column format's scope."
  14425. (interactive)
  14426. (unless (org-at-property-p)
  14427. (error "Not at a property"))
  14428. (let ((prop (org-match-string-no-properties 2)))
  14429. (org-columns-get-format-and-top-level)
  14430. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  14431. (error "No operator defined for property %s" prop))
  14432. (org-columns-compute prop)))
  14433. (defvar org-property-allowed-value-functions nil
  14434. "Hook for functions supplying allowed values for a specific property.
  14435. The functions must take a single argument, the name of the property, and
  14436. return a flat list of allowed values. If \":ETC\" is one of
  14437. the values, this means that these values are intended as defaults for
  14438. completion, but that other values should be allowed too.
  14439. The functions must return nil if they are not responsible for this
  14440. property.")
  14441. (defun org-property-get-allowed-values (pom property &optional table)
  14442. "Get allowed values for the property PROPERTY.
  14443. When TABLE is non-nil, return an alist that can directly be used for
  14444. completion."
  14445. (let (vals)
  14446. (cond
  14447. ((equal property "TODO")
  14448. (setq vals (org-with-point-at pom
  14449. (append org-todo-keywords-1 '("")))))
  14450. ((equal property "PRIORITY")
  14451. (let ((n org-lowest-priority))
  14452. (while (>= n org-highest-priority)
  14453. (push (char-to-string n) vals)
  14454. (setq n (1- n)))))
  14455. ((member property org-special-properties))
  14456. ((setq vals (run-hook-with-args-until-success
  14457. 'org-property-allowed-value-functions property)))
  14458. (t
  14459. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  14460. (when (and vals (string-match "\\S-" vals))
  14461. (setq vals (car (read-from-string (concat "(" vals ")"))))
  14462. (setq vals (mapcar (lambda (x)
  14463. (cond ((stringp x) x)
  14464. ((numberp x) (number-to-string x))
  14465. ((symbolp x) (symbol-name x))
  14466. (t "???")))
  14467. vals)))))
  14468. (when (member ":ETC" vals)
  14469. (setq vals (remove ":ETC" vals))
  14470. (org-add-props (car vals) '(org-unrestricted t)))
  14471. (if table (mapcar 'list vals) vals)))
  14472. (defun org-property-previous-allowed-value (&optional previous)
  14473. "Switch to the next allowed value for this property."
  14474. (interactive)
  14475. (org-property-next-allowed-value t))
  14476. (defun org-property-next-allowed-value (&optional previous)
  14477. "Switch to the next allowed value for this property."
  14478. (interactive)
  14479. (unless (org-at-property-p)
  14480. (error "Not at a property"))
  14481. (let* ((prop (car (save-match-data (org-split-string (match-string 1) ":"))))
  14482. (key (match-string 2))
  14483. (value (match-string 3))
  14484. (allowed (or (org-property-get-allowed-values (point) key)
  14485. (and (member value '("[ ]" "[-]" "[X]"))
  14486. '("[ ]" "[X]"))))
  14487. (heading (save-match-data (nth 4 (org-heading-components))))
  14488. nval)
  14489. (unless allowed
  14490. (error "Allowed values for this property have not been defined"))
  14491. (if previous (setq allowed (reverse allowed)))
  14492. (if (member value allowed)
  14493. (setq nval (car (cdr (member value allowed)))))
  14494. (setq nval (or nval (car allowed)))
  14495. (if (equal nval value)
  14496. (error "Only one allowed value for this property"))
  14497. (org-at-property-p)
  14498. (replace-match (concat " :" key ": " nval) t t)
  14499. (org-indent-line)
  14500. (beginning-of-line 1)
  14501. (skip-chars-forward " \t")
  14502. (when (equal prop org-effort-property)
  14503. (save-excursion
  14504. (org-back-to-heading t)
  14505. (put-text-property (point-at-bol) (point-at-eol) 'org-effort nval))
  14506. (when (string= org-clock-current-task heading)
  14507. (setq org-clock-effort nval)
  14508. (org-clock-update-mode-line)))
  14509. (run-hook-with-args 'org-property-changed-functions key nval)))
  14510. (defun org-find-olp (path &optional this-buffer)
  14511. "Return a marker pointing to the entry at outline path OLP.
  14512. If anything goes wrong, throw an error.
  14513. You can wrap this call to catch the error like this:
  14514. (condition-case msg
  14515. (org-mobile-locate-entry (match-string 4))
  14516. (error (nth 1 msg)))
  14517. The return value will then be either a string with the error message,
  14518. or a marker if everything is OK.
  14519. If THIS-BUFFER is set, the outline path does not contain a file,
  14520. only headings."
  14521. (let* ((file (if this-buffer buffer-file-name (pop path)))
  14522. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  14523. (level 1)
  14524. (lmin 1)
  14525. (lmax 1)
  14526. limit re end found pos heading cnt flevel)
  14527. (unless buffer (error "File not found :%s" file))
  14528. (with-current-buffer buffer
  14529. (save-excursion
  14530. (save-restriction
  14531. (widen)
  14532. (setq limit (point-max))
  14533. (goto-char (point-min))
  14534. (while (setq heading (pop path))
  14535. (setq re (format org-complex-heading-regexp-format
  14536. (regexp-quote heading)))
  14537. (setq cnt 0 pos (point))
  14538. (while (re-search-forward re end t)
  14539. (setq level (- (match-end 1) (match-beginning 1)))
  14540. (if (and (>= level lmin) (<= level lmax))
  14541. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  14542. (when (= cnt 0) (error "Heading not found on level %d: %s"
  14543. lmax heading))
  14544. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  14545. lmax heading))
  14546. (goto-char found)
  14547. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  14548. (setq end (save-excursion (org-end-of-subtree t t))))
  14549. (when (org-at-heading-p)
  14550. (point-marker)))))))
  14551. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  14552. "Find node HEADING in BUFFER.
  14553. Return a marker to the heading if it was found, or nil if not.
  14554. If POS-ONLY is set, return just the position instead of a marker.
  14555. The heading text must match exact, but it may have a TODO keyword,
  14556. a priority cookie and tags in the standard locations."
  14557. (with-current-buffer (or buffer (current-buffer))
  14558. (save-excursion
  14559. (save-restriction
  14560. (widen)
  14561. (goto-char (point-min))
  14562. (let (case-fold-search)
  14563. (if (re-search-forward
  14564. (format org-complex-heading-regexp-format
  14565. (regexp-quote heading)) nil t)
  14566. (if pos-only
  14567. (match-beginning 0)
  14568. (move-marker (make-marker) (match-beginning 0)))))))))
  14569. (defun org-find-exact-heading-in-directory (heading &optional dir)
  14570. "Find Org node headline HEADING in all .org files in directory DIR.
  14571. When the target headline is found, return a marker to this location."
  14572. (let ((files (directory-files (or dir default-directory)
  14573. nil "\\`[^.#].*\\.org\\'"))
  14574. file visiting m buffer)
  14575. (catch 'found
  14576. (while (setq file (pop files))
  14577. (message "trying %s" file)
  14578. (setq visiting (org-find-base-buffer-visiting file))
  14579. (setq buffer (or visiting (find-file-noselect file)))
  14580. (setq m (org-find-exact-headline-in-buffer
  14581. heading buffer))
  14582. (when (and (not m) (not visiting)) (kill-buffer buffer))
  14583. (and m (throw 'found m))))))
  14584. (defun org-find-entry-with-id (ident)
  14585. "Locate the entry that contains the ID property with exact value IDENT.
  14586. IDENT can be a string, a symbol or a number, this function will search for
  14587. the string representation of it.
  14588. Return the position where this entry starts, or nil if there is no such entry."
  14589. (interactive "sID: ")
  14590. (let ((id (cond
  14591. ((stringp ident) ident)
  14592. ((symbol-name ident) (symbol-name ident))
  14593. ((numberp ident) (number-to-string ident))
  14594. (t (error "IDENT %s must be a string, symbol or number" ident))))
  14595. (case-fold-search nil))
  14596. (save-excursion
  14597. (save-restriction
  14598. (widen)
  14599. (goto-char (point-min))
  14600. (when (re-search-forward
  14601. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  14602. nil t)
  14603. (org-back-to-heading t)
  14604. (point))))))
  14605. ;;;; Timestamps
  14606. (defvar org-last-changed-timestamp nil)
  14607. (defvar org-last-inserted-timestamp nil
  14608. "The last time stamp inserted with `org-insert-time-stamp'.")
  14609. (defvar org-time-was-given) ; dynamically scoped parameter
  14610. (defvar org-end-time-was-given) ; dynamically scoped parameter
  14611. (defvar org-ts-what) ; dynamically scoped parameter
  14612. (defun org-time-stamp (arg &optional inactive)
  14613. "Prompt for a date/time and insert a time stamp.
  14614. If the user specifies a time like HH:MM or if this command is
  14615. called with at least one prefix argument, the time stamp contains
  14616. the date and the time. Otherwise, only the date is be included.
  14617. All parts of a date not specified by the user is filled in from
  14618. the current date/time. So if you just press return without
  14619. typing anything, the time stamp will represent the current
  14620. date/time.
  14621. If there is already a timestamp at the cursor, it will be
  14622. modified.
  14623. With two universal prefix arguments, insert an active timestamp
  14624. with the current time without prompting the user."
  14625. (interactive "P")
  14626. (let* ((ts nil)
  14627. (default-time
  14628. ;; Default time is either today, or, when entering a range,
  14629. ;; the range start.
  14630. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  14631. (save-excursion
  14632. (re-search-backward
  14633. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  14634. (- (point) 20) t)))
  14635. (apply 'encode-time (org-parse-time-string (match-string 1)))
  14636. (current-time)))
  14637. (default-input (and ts (org-get-compact-tod ts)))
  14638. (repeater (save-excursion
  14639. (save-match-data
  14640. (beginning-of-line)
  14641. (when (re-search-forward
  14642. "\\([.+-]+[0-9]+[hdwmy] ?\\)+" ;;\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  14643. (save-excursion (progn (end-of-line) (point))) t)
  14644. (match-string 0)))))
  14645. org-time-was-given org-end-time-was-given time)
  14646. (cond
  14647. ((and (org-at-timestamp-p t)
  14648. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  14649. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  14650. (insert "--")
  14651. (setq time (let ((this-command this-command))
  14652. (org-read-date arg 'totime nil nil
  14653. default-time default-input inactive)))
  14654. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  14655. ((org-at-timestamp-p t)
  14656. (setq time (let ((this-command this-command))
  14657. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  14658. (when (org-at-timestamp-p t) ; just to get the match data
  14659. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  14660. (replace-match "")
  14661. (setq org-last-changed-timestamp
  14662. (org-insert-time-stamp
  14663. time (or org-time-was-given arg)
  14664. inactive nil nil (list org-end-time-was-given)))
  14665. (when repeater (goto-char (1- (point))) (insert " " repeater)
  14666. (setq org-last-changed-timestamp
  14667. (concat (substring org-last-inserted-timestamp 0 -1)
  14668. " " repeater ">"))))
  14669. (message "Timestamp updated"))
  14670. ((equal arg '(16))
  14671. (org-insert-time-stamp (current-time) t))
  14672. (t
  14673. (setq time (let ((this-command this-command))
  14674. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  14675. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  14676. nil nil (list org-end-time-was-given))))))
  14677. ;; FIXME: can we use this for something else, like computing time differences?
  14678. (defun org-get-compact-tod (s)
  14679. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  14680. (let* ((t1 (match-string 1 s))
  14681. (h1 (string-to-number (match-string 2 s)))
  14682. (m1 (string-to-number (match-string 3 s)))
  14683. (t2 (and (match-end 4) (match-string 5 s)))
  14684. (h2 (and t2 (string-to-number (match-string 6 s))))
  14685. (m2 (and t2 (string-to-number (match-string 7 s))))
  14686. dh dm)
  14687. (if (not t2)
  14688. t1
  14689. (setq dh (- h2 h1) dm (- m2 m1))
  14690. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  14691. (concat t1 "+" (number-to-string dh)
  14692. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  14693. (defun org-time-stamp-inactive (&optional arg)
  14694. "Insert an inactive time stamp.
  14695. An inactive time stamp is enclosed in square brackets instead of angle
  14696. brackets. It is inactive in the sense that it does not trigger agenda entries,
  14697. does not link to the calendar and cannot be changed with the S-cursor keys.
  14698. So these are more for recording a certain time/date."
  14699. (interactive "P")
  14700. (org-time-stamp arg 'inactive))
  14701. (defvar org-date-ovl (make-overlay 1 1))
  14702. (overlay-put org-date-ovl 'face 'org-date-selected)
  14703. (org-detach-overlay org-date-ovl)
  14704. (defvar org-ans1) ; dynamically scoped parameter
  14705. (defvar org-ans2) ; dynamically scoped parameter
  14706. (defvar org-plain-time-of-day-regexp) ; defined below
  14707. (defvar org-overriding-default-time nil) ; dynamically scoped
  14708. (defvar org-read-date-overlay nil)
  14709. (defvar org-dcst nil) ; dynamically scoped
  14710. (defvar org-read-date-history nil)
  14711. (defvar org-read-date-final-answer nil)
  14712. (defvar org-read-date-analyze-futurep nil)
  14713. (defvar org-read-date-analyze-forced-year nil)
  14714. (defvar org-read-date-inactive)
  14715. (defvar org-read-date-minibuffer-local-map
  14716. (let ((map (make-sparse-keymap)))
  14717. (set-keymap-parent map minibuffer-local-map)
  14718. (org-defkey map (kbd ".")
  14719. (lambda () (interactive)
  14720. (org-eval-in-calendar '(calendar-goto-today))))
  14721. (org-defkey map [(meta shift left)]
  14722. (lambda () (interactive)
  14723. (org-eval-in-calendar '(calendar-backward-month 1))))
  14724. (org-defkey map [(meta shift right)]
  14725. (lambda () (interactive)
  14726. (org-eval-in-calendar '(calendar-forward-month 1))))
  14727. (org-defkey map [(meta shift up)]
  14728. (lambda () (interactive)
  14729. (org-eval-in-calendar '(calendar-backward-year 1))))
  14730. (org-defkey map [(meta shift down)]
  14731. (lambda () (interactive)
  14732. (org-eval-in-calendar '(calendar-forward-year 1))))
  14733. (org-defkey map [?\e (shift left)]
  14734. (lambda () (interactive)
  14735. (org-eval-in-calendar '(calendar-backward-month 1))))
  14736. (org-defkey map [?\e (shift right)]
  14737. (lambda () (interactive)
  14738. (org-eval-in-calendar '(calendar-forward-month 1))))
  14739. (org-defkey map [?\e (shift up)]
  14740. (lambda () (interactive)
  14741. (org-eval-in-calendar '(calendar-backward-year 1))))
  14742. (org-defkey map [?\e (shift down)]
  14743. (lambda () (interactive)
  14744. (org-eval-in-calendar '(calendar-forward-year 1))))
  14745. (org-defkey map [(shift up)]
  14746. (lambda () (interactive)
  14747. (org-eval-in-calendar '(calendar-backward-week 1))))
  14748. (org-defkey map [(shift down)]
  14749. (lambda () (interactive)
  14750. (org-eval-in-calendar '(calendar-forward-week 1))))
  14751. (org-defkey map [(shift left)]
  14752. (lambda () (interactive)
  14753. (org-eval-in-calendar '(calendar-backward-day 1))))
  14754. (org-defkey map [(shift right)]
  14755. (lambda () (interactive)
  14756. (org-eval-in-calendar '(calendar-forward-day 1))))
  14757. (org-defkey map "!"
  14758. (lambda () (interactive)
  14759. (org-eval-in-calendar '(diary-view-entries))
  14760. (message "")))
  14761. (org-defkey map ">"
  14762. (lambda () (interactive)
  14763. (org-eval-in-calendar '(scroll-calendar-left 1))))
  14764. (org-defkey map "<"
  14765. (lambda () (interactive)
  14766. (org-eval-in-calendar '(scroll-calendar-right 1))))
  14767. (org-defkey map "\C-v"
  14768. (lambda () (interactive)
  14769. (org-eval-in-calendar
  14770. '(calendar-scroll-left-three-months 1))))
  14771. (org-defkey map "\M-v"
  14772. (lambda () (interactive)
  14773. (org-eval-in-calendar
  14774. '(calendar-scroll-right-three-months 1))))
  14775. map)
  14776. "Keymap for minibuffer commands when using `org-read-date'.")
  14777. (defun org-read-date (&optional org-with-time to-time from-string prompt
  14778. default-time default-input inactive)
  14779. "Read a date, possibly a time, and make things smooth for the user.
  14780. The prompt will suggest to enter an ISO date, but you can also enter anything
  14781. which will at least partially be understood by `parse-time-string'.
  14782. Unrecognized parts of the date will default to the current day, month, year,
  14783. hour and minute. If this command is called to replace a timestamp at point,
  14784. or to enter the second timestamp of a range, the default time is taken
  14785. from the existing stamp. Furthermore, the command prefers the future,
  14786. so if you are giving a date where the year is not given, and the day-month
  14787. combination is already past in the current year, it will assume you
  14788. mean next year. For details, see the manual. A few examples:
  14789. 3-2-5 --> 2003-02-05
  14790. feb 15 --> currentyear-02-15
  14791. 2/15 --> currentyear-02-15
  14792. sep 12 9 --> 2009-09-12
  14793. 12:45 --> today 12:45
  14794. 22 sept 0:34 --> currentyear-09-22 0:34
  14795. 12 --> currentyear-currentmonth-12
  14796. Fri --> nearest Friday (today or later)
  14797. etc.
  14798. Furthermore you can specify a relative date by giving, as the *first* thing
  14799. in the input: a plus/minus sign, a number and a letter [hdwmy] to indicate
  14800. change in days weeks, months, years.
  14801. With a single plus or minus, the date is relative to today. With a double
  14802. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  14803. +4d --> four days from today
  14804. +4 --> same as above
  14805. +2w --> two weeks from today
  14806. ++5 --> five days from default date
  14807. The function understands only English month and weekday abbreviations.
  14808. While prompting, a calendar is popped up - you can also select the
  14809. date with the mouse (button 1). The calendar shows a period of three
  14810. months. To scroll it to other months, use the keys `>' and `<'.
  14811. If you don't like the calendar, turn it off with
  14812. \(setq org-read-date-popup-calendar nil)
  14813. With optional argument TO-TIME, the date will immediately be converted
  14814. to an internal time.
  14815. With an optional argument ORG-WITH-TIME, the prompt will suggest to
  14816. also insert a time. Note that when ORG-WITH-TIME is not set, you can
  14817. still enter a time, and this function will inform the calling routine
  14818. about this change. The calling routine may then choose to change the
  14819. format used to insert the time stamp into the buffer to include the time.
  14820. With optional argument FROM-STRING, read from this string instead from
  14821. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  14822. the time/date that is used for everything that is not specified by the
  14823. user."
  14824. (require 'parse-time)
  14825. (let* ((org-time-stamp-rounding-minutes
  14826. (if (equal org-with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  14827. (org-dcst org-display-custom-times)
  14828. (ct (org-current-time))
  14829. (org-def (or org-overriding-default-time default-time ct))
  14830. (org-defdecode (decode-time org-def))
  14831. (dummy (progn
  14832. (when (< (nth 2 org-defdecode) org-extend-today-until)
  14833. (setcar (nthcdr 2 org-defdecode) -1)
  14834. (setcar (nthcdr 1 org-defdecode) 59)
  14835. (setq org-def (apply 'encode-time org-defdecode)
  14836. org-defdecode (decode-time org-def)))))
  14837. (mouse-autoselect-window nil) ; Don't let the mouse jump
  14838. (calendar-frame-setup nil)
  14839. (calendar-setup nil)
  14840. (calendar-move-hook nil)
  14841. (calendar-view-diary-initially-flag nil)
  14842. (calendar-view-holidays-initially-flag nil)
  14843. (timestr (format-time-string
  14844. (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") org-def))
  14845. (prompt (concat (if prompt (concat prompt " ") "")
  14846. (format "Date+time [%s]: " timestr)))
  14847. ans (org-ans0 "") org-ans1 org-ans2 final)
  14848. (cond
  14849. (from-string (setq ans from-string))
  14850. (org-read-date-popup-calendar
  14851. (save-excursion
  14852. (save-window-excursion
  14853. (calendar)
  14854. (org-eval-in-calendar '(setq cursor-type nil) t)
  14855. (unwind-protect
  14856. (progn
  14857. (calendar-forward-day (- (time-to-days org-def)
  14858. (calendar-absolute-from-gregorian
  14859. (calendar-current-date))))
  14860. (org-eval-in-calendar nil t)
  14861. (let* ((old-map (current-local-map))
  14862. (map (copy-keymap calendar-mode-map))
  14863. (minibuffer-local-map
  14864. (copy-keymap org-read-date-minibuffer-local-map)))
  14865. (org-defkey map (kbd "RET") 'org-calendar-select)
  14866. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  14867. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  14868. (unwind-protect
  14869. (progn
  14870. (use-local-map map)
  14871. (setq org-read-date-inactive inactive)
  14872. (add-hook 'post-command-hook 'org-read-date-display)
  14873. (setq org-ans0 (read-string prompt default-input
  14874. 'org-read-date-history nil))
  14875. ;; org-ans0: from prompt
  14876. ;; org-ans1: from mouse click
  14877. ;; org-ans2: from calendar motion
  14878. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  14879. (remove-hook 'post-command-hook 'org-read-date-display)
  14880. (use-local-map old-map)
  14881. (when org-read-date-overlay
  14882. (delete-overlay org-read-date-overlay)
  14883. (setq org-read-date-overlay nil)))))
  14884. (bury-buffer "*Calendar*")))))
  14885. (t ; Naked prompt only
  14886. (unwind-protect
  14887. (setq ans (read-string prompt default-input
  14888. 'org-read-date-history timestr))
  14889. (when org-read-date-overlay
  14890. (delete-overlay org-read-date-overlay)
  14891. (setq org-read-date-overlay nil)))))
  14892. (setq final (org-read-date-analyze ans org-def org-defdecode))
  14893. (when org-read-date-analyze-forced-year
  14894. (message "Year was forced into %s"
  14895. (if org-read-date-force-compatible-dates
  14896. "compatible range (1970-2037)"
  14897. "range representable on this machine"))
  14898. (ding))
  14899. ;; One round trip to get rid of 34th of August and stuff like that....
  14900. (setq final (decode-time (apply 'encode-time final)))
  14901. (setq org-read-date-final-answer ans)
  14902. (if to-time
  14903. (apply 'encode-time final)
  14904. (if (and (boundp 'org-time-was-given) org-time-was-given)
  14905. (format "%04d-%02d-%02d %02d:%02d"
  14906. (nth 5 final) (nth 4 final) (nth 3 final)
  14907. (nth 2 final) (nth 1 final))
  14908. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  14909. (defvar org-def)
  14910. (defvar org-defdecode)
  14911. (defvar org-with-time)
  14912. (defun org-read-date-display ()
  14913. "Display the current date prompt interpretation in the minibuffer."
  14914. (when org-read-date-display-live
  14915. (when org-read-date-overlay
  14916. (delete-overlay org-read-date-overlay))
  14917. (when (minibufferp (current-buffer))
  14918. (save-excursion
  14919. (end-of-line 1)
  14920. (while (not (equal (buffer-substring
  14921. (max (point-min) (- (point) 4)) (point))
  14922. " "))
  14923. (insert " ")))
  14924. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  14925. " " (or org-ans1 org-ans2)))
  14926. (org-end-time-was-given nil)
  14927. (f (org-read-date-analyze ans org-def org-defdecode))
  14928. (fmts (if org-dcst
  14929. org-time-stamp-custom-formats
  14930. org-time-stamp-formats))
  14931. (fmt (if (or org-with-time
  14932. (and (boundp 'org-time-was-given) org-time-was-given))
  14933. (cdr fmts)
  14934. (car fmts)))
  14935. (txt (format-time-string fmt (apply 'encode-time f)))
  14936. (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
  14937. (txt (concat "=> " txt)))
  14938. (when (and org-end-time-was-given
  14939. (string-match org-plain-time-of-day-regexp txt))
  14940. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  14941. org-end-time-was-given
  14942. (substring txt (match-end 0)))))
  14943. (when org-read-date-analyze-futurep
  14944. (setq txt (concat txt " (=>F)")))
  14945. (setq org-read-date-overlay
  14946. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  14947. (org-overlay-display org-read-date-overlay txt 'secondary-selection)))))
  14948. (defun org-read-date-analyze (ans org-def org-defdecode)
  14949. "Analyze the combined answer of the date prompt."
  14950. ;; FIXME: cleanup and comment
  14951. (let ((nowdecode (decode-time (current-time)))
  14952. delta deltan deltaw deltadef year month day
  14953. hour minute second wday pm h2 m2 tl wday1
  14954. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  14955. (setq org-read-date-analyze-futurep nil
  14956. org-read-date-analyze-forced-year nil)
  14957. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  14958. (setq ans "+0"))
  14959. (when (setq delta (org-read-date-get-relative ans (current-time) org-def))
  14960. (setq ans (replace-match "" t t ans)
  14961. deltan (car delta)
  14962. deltaw (nth 1 delta)
  14963. deltadef (nth 2 delta)))
  14964. ;; Check if there is an iso week date in there. If yes, store the
  14965. ;; info and postpone interpreting it until the rest of the parsing
  14966. ;; is done.
  14967. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  14968. (setq iso-year (if (match-end 1)
  14969. (org-small-year-to-year
  14970. (string-to-number (match-string 1 ans))))
  14971. iso-weekday (if (match-end 3)
  14972. (string-to-number (match-string 3 ans)))
  14973. iso-week (string-to-number (match-string 2 ans)))
  14974. (setq ans (replace-match "" t t ans)))
  14975. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  14976. (when (string-match
  14977. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  14978. (setq year (if (match-end 2)
  14979. (string-to-number (match-string 2 ans))
  14980. (progn (setq kill-year t)
  14981. (string-to-number (format-time-string "%Y"))))
  14982. month (string-to-number (match-string 3 ans))
  14983. day (string-to-number (match-string 4 ans)))
  14984. (if (< year 100) (setq year (+ 2000 year)))
  14985. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  14986. t nil ans)))
  14987. ;; Help matching dotted european dates
  14988. (when (string-match
  14989. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\.\\( ?[1-9][0-9]\\{3\\}\\)?" ans)
  14990. (setq year (if (match-end 3) (string-to-number (match-string 3 ans))
  14991. (setq kill-year t)
  14992. (string-to-number (format-time-string "%Y")))
  14993. day (string-to-number (match-string 1 ans))
  14994. month (string-to-number (match-string 2 ans))
  14995. ans (replace-match (format "%04d-%02d-%02d" year month day)
  14996. t nil ans)))
  14997. ;; Help matching american dates, like 5/30 or 5/30/7
  14998. (when (string-match
  14999. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  15000. (setq year (if (match-end 4)
  15001. (string-to-number (match-string 4 ans))
  15002. (progn (setq kill-year t)
  15003. (string-to-number (format-time-string "%Y"))))
  15004. month (string-to-number (match-string 1 ans))
  15005. day (string-to-number (match-string 2 ans)))
  15006. (if (< year 100) (setq year (+ 2000 year)))
  15007. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  15008. t nil ans)))
  15009. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  15010. ;; If there is a time with am/pm, and *no* time without it, we convert
  15011. ;; so that matching will be successful.
  15012. (loop for i from 1 to 2 do ; twice, for end time as well
  15013. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  15014. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  15015. (setq hour (string-to-number (match-string 1 ans))
  15016. minute (if (match-end 3)
  15017. (string-to-number (match-string 3 ans))
  15018. 0)
  15019. pm (equal ?p
  15020. (string-to-char (downcase (match-string 4 ans)))))
  15021. (if (and (= hour 12) (not pm))
  15022. (setq hour 0)
  15023. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  15024. (setq ans (replace-match (format "%02d:%02d" hour minute)
  15025. t t ans))))
  15026. ;; Check if a time range is given as a duration
  15027. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  15028. (setq hour (string-to-number (match-string 1 ans))
  15029. h2 (+ hour (string-to-number (match-string 3 ans)))
  15030. minute (string-to-number (match-string 2 ans))
  15031. m2 (+ minute (if (match-end 5) (string-to-number
  15032. (match-string 5 ans))0)))
  15033. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  15034. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  15035. t t ans)))
  15036. ;; Check if there is a time range
  15037. (when (boundp 'org-end-time-was-given)
  15038. (setq org-time-was-given nil)
  15039. (when (and (string-match org-plain-time-of-day-regexp ans)
  15040. (match-end 8))
  15041. (setq org-end-time-was-given (match-string 8 ans))
  15042. (setq ans (concat (substring ans 0 (match-beginning 7))
  15043. (substring ans (match-end 7))))))
  15044. (setq tl (parse-time-string ans)
  15045. day (or (nth 3 tl) (nth 3 org-defdecode))
  15046. month (or (nth 4 tl)
  15047. (if (and org-read-date-prefer-future
  15048. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  15049. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  15050. (nth 4 org-defdecode)))
  15051. year (or (and (not kill-year) (nth 5 tl))
  15052. (if (and org-read-date-prefer-future
  15053. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  15054. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  15055. (nth 5 org-defdecode)))
  15056. hour (or (nth 2 tl) (nth 2 org-defdecode))
  15057. minute (or (nth 1 tl) (nth 1 org-defdecode))
  15058. second (or (nth 0 tl) 0)
  15059. wday (nth 6 tl))
  15060. (when (and (eq org-read-date-prefer-future 'time)
  15061. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  15062. (equal day (nth 3 nowdecode))
  15063. (equal month (nth 4 nowdecode))
  15064. (equal year (nth 5 nowdecode))
  15065. (nth 2 tl)
  15066. (or (< (nth 2 tl) (nth 2 nowdecode))
  15067. (and (= (nth 2 tl) (nth 2 nowdecode))
  15068. (nth 1 tl)
  15069. (< (nth 1 tl) (nth 1 nowdecode)))))
  15070. (setq day (1+ day)
  15071. futurep t))
  15072. ;; Special date definitions below
  15073. (cond
  15074. (iso-week
  15075. ;; There was an iso week
  15076. (require 'cal-iso)
  15077. (setq futurep nil)
  15078. (setq year (or iso-year year)
  15079. day (or iso-weekday wday 1)
  15080. wday nil ; to make sure that the trigger below does not match
  15081. iso-date (calendar-gregorian-from-absolute
  15082. (calendar-absolute-from-iso
  15083. (list iso-week day year))))
  15084. ; FIXME: Should we also push ISO weeks into the future?
  15085. ; (when (and org-read-date-prefer-future
  15086. ; (not iso-year)
  15087. ; (< (calendar-absolute-from-gregorian iso-date)
  15088. ; (time-to-days (current-time))))
  15089. ; (setq year (1+ year)
  15090. ; iso-date (calendar-gregorian-from-absolute
  15091. ; (calendar-absolute-from-iso
  15092. ; (list iso-week day year)))))
  15093. (setq month (car iso-date)
  15094. year (nth 2 iso-date)
  15095. day (nth 1 iso-date)))
  15096. (deltan
  15097. (setq futurep nil)
  15098. (unless deltadef
  15099. (let ((now (decode-time (current-time))))
  15100. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  15101. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  15102. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  15103. ((equal deltaw "m") (setq month (+ month deltan)))
  15104. ((equal deltaw "y") (setq year (+ year deltan)))))
  15105. ((and wday (not (nth 3 tl)))
  15106. ;; Weekday was given, but no day, so pick that day in the week
  15107. ;; on or after the derived date.
  15108. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  15109. (unless (equal wday wday1)
  15110. (setq day (+ day (% (- wday wday1 -7) 7))))))
  15111. (if (and (boundp 'org-time-was-given)
  15112. (nth 2 tl))
  15113. (setq org-time-was-given t))
  15114. (if (< year 100) (setq year (+ 2000 year)))
  15115. ;; Check of the date is representable
  15116. (if org-read-date-force-compatible-dates
  15117. (progn
  15118. (if (< year 1970)
  15119. (setq year 1970 org-read-date-analyze-forced-year t))
  15120. (if (> year 2037)
  15121. (setq year 2037 org-read-date-analyze-forced-year t)))
  15122. (condition-case nil
  15123. (ignore (encode-time second minute hour day month year))
  15124. (error
  15125. (setq year (nth 5 org-defdecode))
  15126. (setq org-read-date-analyze-forced-year t))))
  15127. (setq org-read-date-analyze-futurep futurep)
  15128. (list second minute hour day month year)))
  15129. (defvar parse-time-weekdays)
  15130. (defun org-read-date-get-relative (s today default)
  15131. "Check string S for special relative date string.
  15132. TODAY and DEFAULT are internal times, for today and for a default.
  15133. Return shift list (N what def-flag)
  15134. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  15135. N is the number of WHATs to shift.
  15136. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  15137. the DEFAULT date rather than TODAY."
  15138. (require 'parse-time)
  15139. (when (and
  15140. (string-match
  15141. (concat
  15142. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  15143. "\\([0-9]+\\)?"
  15144. "\\([hdwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  15145. "\\([ \t]\\|$\\)") s)
  15146. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  15147. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  15148. (string-to-char (substring (match-string 1 s) -1))
  15149. ?+))
  15150. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  15151. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  15152. (what (if (match-end 3) (match-string 3 s) "d"))
  15153. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  15154. (date (if rel default today))
  15155. (wday (nth 6 (decode-time date)))
  15156. delta)
  15157. (if wday1
  15158. (progn
  15159. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  15160. (if (= dir ?-) (setq delta (- delta 7)))
  15161. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  15162. (list delta "d" rel))
  15163. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  15164. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  15165. "Turn a user-specified date into the internal representation.
  15166. The internal representation needed by the calendar is (month day year).
  15167. This is a wrapper to handle the brain-dead convention in calendar that
  15168. user function argument order change dependent on argument order."
  15169. (if (boundp 'calendar-date-style)
  15170. (cond
  15171. ((eq calendar-date-style 'american)
  15172. (list arg1 arg2 arg3))
  15173. ((eq calendar-date-style 'european)
  15174. (list arg2 arg1 arg3))
  15175. ((eq calendar-date-style 'iso)
  15176. (list arg2 arg3 arg1)))
  15177. (org-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  15178. (if (org-bound-and-true-p european-calendar-style)
  15179. (list arg2 arg1 arg3)
  15180. (list arg1 arg2 arg3)))))
  15181. (defun org-eval-in-calendar (form &optional keepdate)
  15182. "Eval FORM in the calendar window and return to current window.
  15183. When KEEPDATE is non-nil, update `org-ans2' from the cursor date,
  15184. otherwise stick to the current value of `org-ans2'."
  15185. (let ((sf (selected-frame))
  15186. (sw (selected-window)))
  15187. (select-window (get-buffer-window "*Calendar*" t))
  15188. (eval form)
  15189. (when (and (not keepdate) (calendar-cursor-to-date))
  15190. (let* ((date (calendar-cursor-to-date))
  15191. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15192. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  15193. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  15194. (select-window sw)
  15195. (org-select-frame-set-input-focus sf)))
  15196. (defun org-calendar-select ()
  15197. "Return to `org-read-date' with the date currently selected.
  15198. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15199. (interactive)
  15200. (when (calendar-cursor-to-date)
  15201. (let* ((date (calendar-cursor-to-date))
  15202. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15203. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15204. (if (active-minibuffer-window) (exit-minibuffer))))
  15205. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  15206. "Insert a date stamp for the date given by the internal TIME.
  15207. WITH-HM means use the stamp format that includes the time of the day.
  15208. INACTIVE means use square brackets instead of angular ones, so that the
  15209. stamp will not contribute to the agenda.
  15210. PRE and POST are optional strings to be inserted before and after the
  15211. stamp.
  15212. The command returns the inserted time stamp."
  15213. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  15214. stamp)
  15215. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  15216. (insert-before-markers (or pre ""))
  15217. (when (listp extra)
  15218. (setq extra (car extra))
  15219. (if (and (stringp extra)
  15220. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  15221. (setq extra (format "-%02d:%02d"
  15222. (string-to-number (match-string 1 extra))
  15223. (string-to-number (match-string 2 extra))))
  15224. (setq extra nil)))
  15225. (when extra
  15226. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  15227. (insert-before-markers (setq stamp (format-time-string fmt time)))
  15228. (insert-before-markers (or post ""))
  15229. (setq org-last-inserted-timestamp stamp)))
  15230. (defun org-toggle-time-stamp-overlays ()
  15231. "Toggle the use of custom time stamp formats."
  15232. (interactive)
  15233. (setq org-display-custom-times (not org-display-custom-times))
  15234. (unless org-display-custom-times
  15235. (let ((p (point-min)) (bmp (buffer-modified-p)))
  15236. (while (setq p (next-single-property-change p 'display))
  15237. (if (and (get-text-property p 'display)
  15238. (eq (get-text-property p 'face) 'org-date))
  15239. (remove-text-properties
  15240. p (setq p (next-single-property-change p 'display))
  15241. '(display t))))
  15242. (set-buffer-modified-p bmp)))
  15243. (if (featurep 'xemacs)
  15244. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  15245. (org-restart-font-lock)
  15246. (setq org-table-may-need-update t)
  15247. (if org-display-custom-times
  15248. (message "Time stamps are overlaid with custom format")
  15249. (message "Time stamp overlays removed")))
  15250. (defun org-display-custom-time (beg end)
  15251. "Overlay modified time stamp format over timestamp between BEG and END."
  15252. (let* ((ts (buffer-substring beg end))
  15253. t1 w1 with-hm tf time str w2 (off 0))
  15254. (save-match-data
  15255. (setq t1 (org-parse-time-string ts t))
  15256. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)?\\'" ts)
  15257. (setq off (- (match-end 0) (match-beginning 0)))))
  15258. (setq end (- end off))
  15259. (setq w1 (- end beg)
  15260. with-hm (and (nth 1 t1) (nth 2 t1))
  15261. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  15262. time (org-fix-decoded-time t1)
  15263. str (org-add-props
  15264. (format-time-string
  15265. (substring tf 1 -1) (apply 'encode-time time))
  15266. nil 'mouse-face 'highlight)
  15267. w2 (length str))
  15268. (if (not (= w2 w1))
  15269. (add-text-properties (1+ beg) (+ 2 beg)
  15270. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  15271. (if (featurep 'xemacs)
  15272. (progn
  15273. (put-text-property beg end 'invisible t)
  15274. (put-text-property beg end 'end-glyph (make-glyph str)))
  15275. (put-text-property beg end 'display str))))
  15276. (defun org-translate-time (string)
  15277. "Translate all timestamps in STRING to custom format.
  15278. But do this only if the variable `org-display-custom-times' is set."
  15279. (when org-display-custom-times
  15280. (save-match-data
  15281. (let* ((start 0)
  15282. (re org-ts-regexp-both)
  15283. t1 with-hm inactive tf time str beg end)
  15284. (while (setq start (string-match re string start))
  15285. (setq beg (match-beginning 0)
  15286. end (match-end 0)
  15287. t1 (save-match-data
  15288. (org-parse-time-string (substring string beg end) t))
  15289. with-hm (and (nth 1 t1) (nth 2 t1))
  15290. inactive (equal (substring string beg (1+ beg)) "[")
  15291. tf (funcall (if with-hm 'cdr 'car)
  15292. org-time-stamp-custom-formats)
  15293. time (org-fix-decoded-time t1)
  15294. str (format-time-string
  15295. (concat
  15296. (if inactive "[" "<") (substring tf 1 -1)
  15297. (if inactive "]" ">"))
  15298. (apply 'encode-time time))
  15299. string (replace-match str t t string)
  15300. start (+ start (length str)))))))
  15301. string)
  15302. (defun org-fix-decoded-time (time)
  15303. "Set 0 instead of nil for the first 6 elements of time.
  15304. Don't touch the rest."
  15305. (let ((n 0))
  15306. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  15307. (define-obsolete-function-alias 'org-days-to-time 'org-time-stamp-to-now "24.4")
  15308. (defun org-time-stamp-to-now (timestamp-string &optional seconds)
  15309. "Difference between TIMESTAMP-STRING and now in days.
  15310. If SECONDS is non-nil, return the difference in seconds."
  15311. (let ((fdiff (if seconds 'org-float-time 'time-to-days)))
  15312. (- (funcall fdiff (org-time-string-to-time timestamp-string))
  15313. (funcall fdiff (current-time)))))
  15314. (defun org-deadline-close (timestamp-string &optional ndays)
  15315. "Is the time in TIMESTAMP-STRING close to the current date?"
  15316. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  15317. (and (< (org-time-stamp-to-now timestamp-string) ndays)
  15318. (not (org-entry-is-done-p))))
  15319. (defun org-get-wdays (ts &optional delay zero-delay)
  15320. "Get the deadline lead time appropriate for timestring TS.
  15321. When DELAY is non-nil, get the delay time for scheduled items
  15322. instead of the deadline lead time. When ZERO-DELAY is non-nil
  15323. and `org-scheduled-delay-days' is 0, enforce 0 as the delay,
  15324. don't try to find the delay cookie in the scheduled timestamp."
  15325. (let ((tv (if delay org-scheduled-delay-days
  15326. org-deadline-warning-days)))
  15327. (cond
  15328. ((or (and delay (< tv 0))
  15329. (and delay zero-delay (<= tv 0))
  15330. (and (not delay) (<= tv 0)))
  15331. ;; Enforce this value no matter what
  15332. (- tv))
  15333. ((string-match "-\\([0-9]+\\)\\([hdwmy]\\)\\(\\'\\|>\\| \\)" ts)
  15334. ;; lead time is specified.
  15335. (floor (* (string-to-number (match-string 1 ts))
  15336. (cdr (assoc (match-string 2 ts)
  15337. '(("d" . 1) ("w" . 7)
  15338. ("m" . 30.4) ("y" . 365.25)
  15339. ("h" . 0.041667)))))))
  15340. ;; go for the default.
  15341. (t tv))))
  15342. (defun org-calendar-select-mouse (ev)
  15343. "Return to `org-read-date' with the date currently selected.
  15344. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15345. (interactive "e")
  15346. (mouse-set-point ev)
  15347. (when (calendar-cursor-to-date)
  15348. (let* ((date (calendar-cursor-to-date))
  15349. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15350. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15351. (if (active-minibuffer-window) (exit-minibuffer))))
  15352. (defun org-check-deadlines (ndays)
  15353. "Check if there are any deadlines due or past due.
  15354. A deadline is considered due if it happens within `org-deadline-warning-days'
  15355. days from today's date. If the deadline appears in an entry marked DONE,
  15356. it is not shown. The prefix arg NDAYS can be used to test that many
  15357. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  15358. (interactive "P")
  15359. (let* ((org-warn-days
  15360. (cond
  15361. ((equal ndays '(4)) 100000)
  15362. (ndays (prefix-numeric-value ndays))
  15363. (t (abs org-deadline-warning-days))))
  15364. (case-fold-search nil)
  15365. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15366. (callback
  15367. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  15368. (message "%d deadlines past-due or due within %d days"
  15369. (org-occur regexp nil callback)
  15370. org-warn-days)))
  15371. (defsubst org-re-timestamp (type)
  15372. "Return a regexp for timestamp TYPE.
  15373. Allowed values for TYPE are:
  15374. all: all timestamps
  15375. active: only active timestamps (<...>)
  15376. inactive: only inactive timestamps ([...])
  15377. scheduled: only scheduled timestamps
  15378. deadline: only deadline timestamps
  15379. closed: only closed time-stamps
  15380. When TYPE is nil, fall back on returning a regexp that matches
  15381. both scheduled and deadline timestamps."
  15382. (cond ((eq type 'all) "\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}\\(?: +[^]+0-9> \n -]+\\)?\\(?: +[0-9]\\{1,2\\}:[0-9]\\{2\\}\\)?\\)")
  15383. ((eq type 'active) org-ts-regexp)
  15384. ((eq type 'inactive) "\\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^ \n>]*?\\)\\]")
  15385. ((eq type 'scheduled) (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>"))
  15386. ((eq type 'deadline) (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15387. ((eq type 'closed) (concat org-closed-string " \\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^ \n>]*?\\)\\]"))
  15388. ((eq type 'scheduled-or-deadline)
  15389. (concat "\\<\\(?:" org-deadline-string "\\|" org-scheduled-string "\\) *<\\([^>]+\\)>"))))
  15390. (defun org-check-before-date (date)
  15391. "Check if there are deadlines or scheduled entries before DATE."
  15392. (interactive (list (org-read-date)))
  15393. (let ((case-fold-search nil)
  15394. (regexp (org-re-timestamp org-ts-type))
  15395. (callback
  15396. (lambda () (time-less-p
  15397. (org-time-string-to-time (match-string 1))
  15398. (org-time-string-to-time date)))))
  15399. (message "%d entries before %s"
  15400. (org-occur regexp nil callback) date)))
  15401. (defun org-check-after-date (date)
  15402. "Check if there are deadlines or scheduled entries after DATE."
  15403. (interactive (list (org-read-date)))
  15404. (let ((case-fold-search nil)
  15405. (regexp (org-re-timestamp org-ts-type))
  15406. (callback
  15407. (lambda () (not
  15408. (time-less-p
  15409. (org-time-string-to-time (match-string 1))
  15410. (org-time-string-to-time date))))))
  15411. (message "%d entries after %s"
  15412. (org-occur regexp nil callback) date)))
  15413. (defun org-check-dates-range (start-date end-date)
  15414. "Check for deadlines/scheduled entries between START-DATE and END-DATE."
  15415. (interactive (list (org-read-date nil nil nil "Range starts")
  15416. (org-read-date nil nil nil "Range end")))
  15417. (let ((case-fold-search nil)
  15418. (regexp (org-re-timestamp org-ts-type))
  15419. (callback
  15420. (lambda ()
  15421. (let ((match (match-string 1)))
  15422. (and
  15423. (not (time-less-p
  15424. (org-time-string-to-time match)
  15425. (org-time-string-to-time start-date)))
  15426. (time-less-p
  15427. (org-time-string-to-time match)
  15428. (org-time-string-to-time end-date)))))))
  15429. (message "%d entries between %s and %s"
  15430. (org-occur regexp nil callback) start-date end-date)))
  15431. (defun org-evaluate-time-range (&optional to-buffer)
  15432. "Evaluate a time range by computing the difference between start and end.
  15433. Normally the result is just printed in the echo area, but with prefix arg
  15434. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  15435. If the time range is actually in a table, the result is inserted into the
  15436. next column.
  15437. For time difference computation, a year is assumed to be exactly 365
  15438. days in order to avoid rounding problems."
  15439. (interactive "P")
  15440. (or
  15441. (org-clock-update-time-maybe)
  15442. (save-excursion
  15443. (unless (org-at-date-range-p t)
  15444. (goto-char (point-at-bol))
  15445. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  15446. (if (not (org-at-date-range-p t))
  15447. (error "Not at a time-stamp range, and none found in current line")))
  15448. (let* ((ts1 (match-string 1))
  15449. (ts2 (match-string 2))
  15450. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  15451. (match-end (match-end 0))
  15452. (time1 (org-time-string-to-time ts1))
  15453. (time2 (org-time-string-to-time ts2))
  15454. (t1 (org-float-time time1))
  15455. (t2 (org-float-time time2))
  15456. (diff (abs (- t2 t1)))
  15457. (negative (< (- t2 t1) 0))
  15458. ;; (ys (floor (* 365 24 60 60)))
  15459. (ds (* 24 60 60))
  15460. (hs (* 60 60))
  15461. (fy "%dy %dd %02d:%02d")
  15462. (fy1 "%dy %dd")
  15463. (fd "%dd %02d:%02d")
  15464. (fd1 "%dd")
  15465. (fh "%02d:%02d")
  15466. y d h m align)
  15467. (if havetime
  15468. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  15469. y 0
  15470. d (floor (/ diff ds)) diff (mod diff ds)
  15471. h (floor (/ diff hs)) diff (mod diff hs)
  15472. m (floor (/ diff 60)))
  15473. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  15474. y 0
  15475. d (floor (+ (/ diff ds) 0.5))
  15476. h 0 m 0))
  15477. (if (not to-buffer)
  15478. (message "%s" (org-make-tdiff-string y d h m))
  15479. (if (org-at-table-p)
  15480. (progn
  15481. (goto-char match-end)
  15482. (setq align t)
  15483. (and (looking-at " *|") (goto-char (match-end 0))))
  15484. (goto-char match-end))
  15485. (if (looking-at
  15486. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  15487. (replace-match ""))
  15488. (if negative (insert " -"))
  15489. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  15490. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  15491. (insert " " (format fh h m))))
  15492. (if align (org-table-align))
  15493. (message "Time difference inserted")))))
  15494. (defun org-make-tdiff-string (y d h m)
  15495. (let ((fmt "")
  15496. (l nil))
  15497. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  15498. l (push y l)))
  15499. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  15500. l (push d l)))
  15501. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  15502. l (push h l)))
  15503. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  15504. l (push m l)))
  15505. (apply 'format fmt (nreverse l))))
  15506. (defun org-time-string-to-time (s &optional buffer pos)
  15507. "Convert a timestamp string into internal time."
  15508. (condition-case errdata
  15509. (apply 'encode-time (org-parse-time-string s))
  15510. (error (error "Bad timestamp `%s'%s\nError was: %s"
  15511. s (if (not (and buffer pos))
  15512. ""
  15513. (format " at %d in buffer `%s'" pos buffer))
  15514. (cdr errdata)))))
  15515. (defun org-time-string-to-seconds (s)
  15516. "Convert a timestamp string to a number of seconds."
  15517. (org-float-time (org-time-string-to-time s)))
  15518. (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
  15519. "Convert a time stamp to an absolute day number.
  15520. If there is a specifier for a cyclic time stamp, get the closest
  15521. date to DAYNR.
  15522. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  15523. The variable `date' is bound by the calendar when this is called."
  15524. (cond
  15525. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  15526. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  15527. daynr
  15528. (+ daynr 1000)))
  15529. ((and daynr (string-match "\\+[0-9]+[hdwmy]" s))
  15530. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  15531. (time-to-days (current-time))) (match-string 0 s)
  15532. prefer show-all))
  15533. (t (time-to-days
  15534. (condition-case errdata
  15535. (apply 'encode-time (org-parse-time-string s))
  15536. (error (error "Bad timestamp `%s'%s\nError was: %s"
  15537. s (if (not (and buffer pos))
  15538. ""
  15539. (format " at %d in buffer `%s'" pos buffer))
  15540. (cdr errdata))))))))
  15541. (defun org-days-to-iso-week (days)
  15542. "Return the iso week number."
  15543. (require 'cal-iso)
  15544. (car (calendar-iso-from-absolute days)))
  15545. (defun org-small-year-to-year (year)
  15546. "Convert 2-digit years into 4-digit years.
  15547. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  15548. The year 2000 cannot be abbreviated. Any year larger than 99
  15549. is returned unchanged."
  15550. (if (< year 38)
  15551. (setq year (+ 2000 year))
  15552. (if (< year 100)
  15553. (setq year (+ 1900 year))))
  15554. year)
  15555. (defun org-time-from-absolute (d)
  15556. "Return the time corresponding to date D.
  15557. D may be an absolute day number, or a calendar-type list (month day year)."
  15558. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  15559. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  15560. (defun org-calendar-holiday ()
  15561. "List of holidays, for Diary display in Org-mode."
  15562. (require 'holidays)
  15563. (let ((hl (funcall
  15564. (if (fboundp 'calendar-check-holidays)
  15565. 'calendar-check-holidays 'check-calendar-holidays) date)))
  15566. (if hl (mapconcat 'identity hl "; "))))
  15567. (defun org-diary-sexp-entry (sexp entry date)
  15568. "Process a SEXP diary ENTRY for DATE."
  15569. (require 'diary-lib)
  15570. (let ((result (if calendar-debug-sexp
  15571. (let ((stack-trace-on-error t))
  15572. (eval (car (read-from-string sexp))))
  15573. (condition-case nil
  15574. (eval (car (read-from-string sexp)))
  15575. (error
  15576. (beep)
  15577. (message "Bad sexp at line %d in %s: %s"
  15578. (org-current-line)
  15579. (buffer-file-name) sexp)
  15580. (sleep-for 2))))))
  15581. (cond ((stringp result) (split-string result "; "))
  15582. ((and (consp result)
  15583. (not (consp (cdr result)))
  15584. (stringp (cdr result))) (cdr result))
  15585. ((and (consp result)
  15586. (stringp (car result))) result)
  15587. (result entry))))
  15588. (defun org-diary-to-ical-string (frombuf)
  15589. "Get iCalendar entries from diary entries in buffer FROMBUF.
  15590. This uses the icalendar.el library."
  15591. (let* ((tmpdir (if (featurep 'xemacs)
  15592. (temp-directory)
  15593. temporary-file-directory))
  15594. (tmpfile (make-temp-name
  15595. (expand-file-name "orgics" tmpdir)))
  15596. buf rtn b e)
  15597. (with-current-buffer frombuf
  15598. (icalendar-export-region (point-min) (point-max) tmpfile)
  15599. (setq buf (find-buffer-visiting tmpfile))
  15600. (set-buffer buf)
  15601. (goto-char (point-min))
  15602. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  15603. (setq b (match-beginning 0)))
  15604. (goto-char (point-max))
  15605. (if (re-search-backward "^END:VEVENT" nil t)
  15606. (setq e (match-end 0)))
  15607. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  15608. (kill-buffer buf)
  15609. (delete-file tmpfile)
  15610. rtn))
  15611. (defun org-closest-date (start current change prefer show-all)
  15612. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  15613. When PREFER is `past', return a date that is either CURRENT or past.
  15614. When PREFER is `future', return a date that is either CURRENT or future.
  15615. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  15616. ;; Make the proper lists from the dates
  15617. (catch 'exit
  15618. (let ((a1 '(("h" . hour)
  15619. ("d" . day)
  15620. ("w" . week)
  15621. ("m" . month)
  15622. ("y" . year)))
  15623. (shour (nth 2 (org-parse-time-string start)))
  15624. dn dw sday cday n1 n2 n0
  15625. d m y y1 y2 date1 date2 nmonths nm ny m2)
  15626. (setq start (org-date-to-gregorian start)
  15627. current (org-date-to-gregorian
  15628. (if show-all
  15629. current
  15630. (time-to-days (current-time))))
  15631. sday (calendar-absolute-from-gregorian start)
  15632. cday (calendar-absolute-from-gregorian current))
  15633. (if (<= cday sday) (throw 'exit sday))
  15634. (if (string-match "\\(\\+[0-9]+\\)\\([hdwmy]\\)" change)
  15635. (setq dn (string-to-number (match-string 1 change))
  15636. dw (cdr (assoc (match-string 2 change) a1)))
  15637. (error "Invalid change specifier: %s" change))
  15638. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  15639. (cond
  15640. ((eq dw 'hour)
  15641. (let ((missing-hours
  15642. (mod (+ (- (* 24 (- cday sday)) shour) org-extend-today-until)
  15643. dn)))
  15644. (setq n1 (if (zerop missing-hours) cday
  15645. (- cday (1+ (floor (/ missing-hours 24)))))
  15646. n2 (+ cday (floor (/ (- dn missing-hours) 24))))))
  15647. ((eq dw 'day)
  15648. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  15649. n2 (+ n1 dn)))
  15650. ((eq dw 'year)
  15651. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  15652. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  15653. (setq date1 (list m d y1)
  15654. n1 (calendar-absolute-from-gregorian date1)
  15655. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  15656. n2 (calendar-absolute-from-gregorian date2)))
  15657. ((eq dw 'month)
  15658. ;; approx number of month between the two dates
  15659. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  15660. ;; How often does dn fit in there?
  15661. (setq d (nth 1 start) m (car start) y (nth 2 start)
  15662. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  15663. m (+ m nm)
  15664. ny (floor (/ m 12))
  15665. y (+ y ny)
  15666. m (- m (* ny 12)))
  15667. (while (> m 12) (setq m (- m 12) y (1+ y)))
  15668. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  15669. (setq m2 (+ m dn) y2 y)
  15670. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  15671. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  15672. (while (<= n2 cday)
  15673. (setq n1 n2 m m2 y y2)
  15674. (setq m2 (+ m dn) y2 y)
  15675. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  15676. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  15677. ;; Make sure n1 is the earlier date
  15678. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  15679. (if show-all
  15680. (cond
  15681. ((eq prefer 'past) (if (= cday n2) n2 n1))
  15682. ((eq prefer 'future) (if (= cday n1) n1 n2))
  15683. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  15684. (cond
  15685. ((eq prefer 'past) (if (= cday n2) n2 n1))
  15686. ((eq prefer 'future) (if (= cday n1) n1 n2))
  15687. (t (if (= cday n1) n1 n2)))))))
  15688. (defun org-date-to-gregorian (date)
  15689. "Turn any specification of DATE into a Gregorian date for the calendar."
  15690. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  15691. ((and (listp date) (= (length date) 3)) date)
  15692. ((stringp date)
  15693. (setq date (org-parse-time-string date))
  15694. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  15695. ((listp date)
  15696. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  15697. (defun org-parse-time-string (s &optional nodefault)
  15698. "Parse the standard Org-mode time string.
  15699. This should be a lot faster than the normal `parse-time-string'.
  15700. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  15701. hour and minute fields will be nil if not given."
  15702. (cond ((string-match org-ts-regexp0 s)
  15703. (list 0
  15704. (if (or (match-beginning 8) (not nodefault))
  15705. (string-to-number (or (match-string 8 s) "0")))
  15706. (if (or (match-beginning 7) (not nodefault))
  15707. (string-to-number (or (match-string 7 s) "0")))
  15708. (string-to-number (match-string 4 s))
  15709. (string-to-number (match-string 3 s))
  15710. (string-to-number (match-string 2 s))
  15711. nil nil nil))
  15712. ((string-match "^<[^>]+>$" s)
  15713. (decode-time (seconds-to-time (org-matcher-time s))))
  15714. (t (error "Not a standard Org-mode time string: %s" s))))
  15715. (defun org-timestamp-up (&optional arg)
  15716. "Increase the date item at the cursor by one.
  15717. If the cursor is on the year, change the year. If it is on the month,
  15718. the day or the time, change that.
  15719. With prefix ARG, change by that many units."
  15720. (interactive "p")
  15721. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  15722. (defun org-timestamp-down (&optional arg)
  15723. "Decrease the date item at the cursor by one.
  15724. If the cursor is on the year, change the year. If it is on the month,
  15725. the day or the time, change that.
  15726. With prefix ARG, change by that many units."
  15727. (interactive "p")
  15728. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  15729. (defun org-timestamp-up-day (&optional arg)
  15730. "Increase the date in the time stamp by one day.
  15731. With prefix ARG, change that many days."
  15732. (interactive "p")
  15733. (if (and (not (org-at-timestamp-p t))
  15734. (org-at-heading-p))
  15735. (org-todo 'up)
  15736. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  15737. (defun org-timestamp-down-day (&optional arg)
  15738. "Decrease the date in the time stamp by one day.
  15739. With prefix ARG, change that many days."
  15740. (interactive "p")
  15741. (if (and (not (org-at-timestamp-p t))
  15742. (org-at-heading-p))
  15743. (org-todo 'down)
  15744. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  15745. (defun org-at-timestamp-p (&optional inactive-ok)
  15746. "Determine if the cursor is in or at a timestamp."
  15747. (interactive)
  15748. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  15749. (pos (point))
  15750. (ans (or (looking-at tsr)
  15751. (save-excursion
  15752. (skip-chars-backward "^[<\n\r\t")
  15753. (if (> (point) (point-min)) (backward-char 1))
  15754. (and (looking-at tsr)
  15755. (> (- (match-end 0) pos) -1))))))
  15756. (and ans
  15757. (boundp 'org-ts-what)
  15758. (setq org-ts-what
  15759. (cond
  15760. ((= pos (match-beginning 0)) 'bracket)
  15761. ;; Point is considered to be "on the bracket" whether
  15762. ;; it's really on it or right after it.
  15763. ((= pos (1- (match-end 0))) 'bracket)
  15764. ((= pos (match-end 0)) 'after)
  15765. ((org-pos-in-match-range pos 2) 'year)
  15766. ((org-pos-in-match-range pos 3) 'month)
  15767. ((org-pos-in-match-range pos 7) 'hour)
  15768. ((org-pos-in-match-range pos 8) 'minute)
  15769. ((or (org-pos-in-match-range pos 4)
  15770. (org-pos-in-match-range pos 5)) 'day)
  15771. ((and (> pos (or (match-end 8) (match-end 5)))
  15772. (< pos (match-end 0)))
  15773. (- pos (or (match-end 8) (match-end 5))))
  15774. (t 'day))))
  15775. ans))
  15776. (defun org-toggle-timestamp-type ()
  15777. "Toggle the type (<active> or [inactive]) of a time stamp."
  15778. (interactive)
  15779. (when (org-at-timestamp-p t)
  15780. (let ((beg (match-beginning 0)) (end (match-end 0))
  15781. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  15782. (save-excursion
  15783. (goto-char beg)
  15784. (while (re-search-forward "[][<>]" end t)
  15785. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  15786. t t)))
  15787. (message "Timestamp is now %sactive"
  15788. (if (equal (char-after beg) ?<) "" "in")))))
  15789. (defun org-at-clock-log-p nil
  15790. "Is the cursor on the clock log line?"
  15791. (save-excursion
  15792. (move-beginning-of-line 1)
  15793. (looking-at "^[ \t]*CLOCK:")))
  15794. (defvar org-clock-history) ; defined in org-clock.el
  15795. (defvar org-clock-adjust-closest nil) ; defined in org-clock.el
  15796. (defun org-timestamp-change (n &optional what updown suppress-tmp-delay)
  15797. "Change the date in the time stamp at point.
  15798. The date will be changed by N times WHAT. WHAT can be `day', `month',
  15799. `year', `minute', `second'. If WHAT is not given, the cursor position
  15800. in the timestamp determines what will be changed.
  15801. When SUPPRESS-TMP-DELAY is non-nil, suppress delays like \"--2d\"."
  15802. (let ((origin (point)) origin-cat
  15803. with-hm inactive
  15804. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  15805. org-ts-what
  15806. extra rem
  15807. ts time time0 fixnext clrgx)
  15808. (if (not (org-at-timestamp-p t))
  15809. (error "Not at a timestamp"))
  15810. (if (and (not what) (eq org-ts-what 'bracket))
  15811. (org-toggle-timestamp-type)
  15812. ;; Point isn't on brackets. Remember the part of the time-stamp
  15813. ;; the point was in. Indeed, size of time-stamps may change,
  15814. ;; but point must be kept in the same category nonetheless.
  15815. (setq origin-cat org-ts-what)
  15816. (if (and (not what) (not (eq org-ts-what 'day))
  15817. org-display-custom-times
  15818. (get-text-property (point) 'display)
  15819. (not (get-text-property (1- (point)) 'display)))
  15820. (setq org-ts-what 'day))
  15821. (setq org-ts-what (or what org-ts-what)
  15822. inactive (= (char-after (match-beginning 0)) ?\[)
  15823. ts (match-string 0))
  15824. (replace-match "")
  15825. (when (string-match
  15826. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?-?[-+][0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)*\\)[]>]"
  15827. ts)
  15828. (setq extra (match-string 1 ts))
  15829. (if suppress-tmp-delay
  15830. (setq extra (replace-regexp-in-string " --[0-9]+[hdwmy]" "" extra))))
  15831. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  15832. (setq with-hm t))
  15833. (setq time0 (org-parse-time-string ts))
  15834. (when (and updown
  15835. (eq org-ts-what 'minute)
  15836. (not current-prefix-arg))
  15837. ;; This looks like s-up and s-down. Change by one rounding step.
  15838. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  15839. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  15840. (setcar (cdr time0) (+ (nth 1 time0)
  15841. (if (> n 0) (- rem) (- dm rem))))))
  15842. (setq time
  15843. (encode-time (or (car time0) 0)
  15844. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  15845. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  15846. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  15847. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  15848. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  15849. (nthcdr 6 time0)))
  15850. (when (and (member org-ts-what '(hour minute))
  15851. extra
  15852. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  15853. (setq extra (org-modify-ts-extra
  15854. extra
  15855. (if (eq org-ts-what 'hour) 2 5)
  15856. n dm)))
  15857. (when (integerp org-ts-what)
  15858. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  15859. (if (eq what 'calendar)
  15860. (let ((cal-date (org-get-date-from-calendar)))
  15861. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  15862. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  15863. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  15864. (setcar time0 (or (car time0) 0))
  15865. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  15866. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  15867. (setq time (apply 'encode-time time0))))
  15868. ;; Insert the new time-stamp, and ensure point stays in the same
  15869. ;; category as before (i.e. not after the last position in that
  15870. ;; category).
  15871. (let ((pos (point)))
  15872. ;; Stay before inserted string. `save-excursion' is of no use.
  15873. (setq org-last-changed-timestamp
  15874. (org-insert-time-stamp time with-hm inactive nil nil extra))
  15875. (goto-char pos))
  15876. (save-match-data
  15877. (looking-at org-ts-regexp3)
  15878. (goto-char (cond
  15879. ;; `day' category ends before `hour' if any, or at
  15880. ;; the end of the day name.
  15881. ((eq origin-cat 'day)
  15882. (min (or (match-beginning 7) (1- (match-end 5))) origin))
  15883. ((eq origin-cat 'hour) (min (match-end 7) origin))
  15884. ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
  15885. ((integerp origin-cat) (min (1- (match-end 0)) origin))
  15886. ;; `year' and `month' have both fixed size: point
  15887. ;; couldn't have moved into another part.
  15888. (t origin))))
  15889. ;; Update clock if on a CLOCK line.
  15890. (org-clock-update-time-maybe)
  15891. ;; Maybe adjust the closest clock in `org-clock-history'
  15892. (when org-clock-adjust-closest
  15893. (if (not (and (org-at-clock-log-p)
  15894. (< 1 (length (delq nil (mapcar 'marker-position
  15895. org-clock-history))))))
  15896. (message "No clock to adjust")
  15897. (cond ((save-excursion ; fix previous clock?
  15898. (re-search-backward org-ts-regexp0 nil t)
  15899. (org-looking-back (concat org-clock-string " \\[")))
  15900. (setq fixnext 1 clrgx (concat org-ts-regexp0 "\\] =>.*$")))
  15901. ((save-excursion ; fix next clock?
  15902. (re-search-backward org-ts-regexp0 nil t)
  15903. (looking-at (concat org-ts-regexp0 "\\] =>")))
  15904. (setq fixnext -1 clrgx (concat org-clock-string " \\[" org-ts-regexp0))))
  15905. (save-window-excursion
  15906. ;; Find closest clock to point, adjust the previous/next one in history
  15907. (let* ((p (save-excursion (org-back-to-heading t)))
  15908. (cl (mapcar (lambda(c) (abs (- (marker-position c) p))) org-clock-history))
  15909. (clfixnth
  15910. (+ fixnext (- (length cl) (or (length (member (apply #'min cl) cl)) 100))))
  15911. (clfixpos (if (> 0 clfixnth) nil (nth clfixnth org-clock-history))))
  15912. (if (not clfixpos)
  15913. (message "No clock to adjust")
  15914. (save-excursion
  15915. (org-goto-marker-or-bmk clfixpos)
  15916. (org-show-subtree)
  15917. (when (re-search-forward clrgx nil t)
  15918. (goto-char (match-beginning 1))
  15919. (let (org-clock-adjust-closest)
  15920. (org-timestamp-change n org-ts-what updown))
  15921. (message "Clock adjusted in %s for heading: %s"
  15922. (file-name-nondirectory (buffer-file-name))
  15923. (org-get-heading t t)))))))))
  15924. ;; Try to recenter the calendar window, if any.
  15925. (if (and org-calendar-follow-timestamp-change
  15926. (get-buffer-window "*Calendar*" t)
  15927. (memq org-ts-what '(day month year)))
  15928. (org-recenter-calendar (time-to-days time))))))
  15929. (defun org-modify-ts-extra (s pos n dm)
  15930. "Change the different parts of the lead-time and repeat fields in timestamp."
  15931. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  15932. ng h m new rem)
  15933. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  15934. (cond
  15935. ((or (org-pos-in-match-range pos 2)
  15936. (org-pos-in-match-range pos 3))
  15937. (setq m (string-to-number (match-string 3 s))
  15938. h (string-to-number (match-string 2 s)))
  15939. (if (org-pos-in-match-range pos 2)
  15940. (setq h (+ h n))
  15941. (setq n (* dm (org-no-warnings (signum n))))
  15942. (when (not (= 0 (setq rem (% m dm))))
  15943. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  15944. (setq m (+ m n)))
  15945. (if (< m 0) (setq m (+ m 60) h (1- h)))
  15946. (if (> m 59) (setq m (- m 60) h (1+ h)))
  15947. (setq h (min 24 (max 0 h)))
  15948. (setq ng 1 new (format "-%02d:%02d" h m)))
  15949. ((org-pos-in-match-range pos 6)
  15950. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  15951. ((org-pos-in-match-range pos 5)
  15952. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  15953. ((org-pos-in-match-range pos 9)
  15954. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  15955. ((org-pos-in-match-range pos 8)
  15956. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  15957. (when ng
  15958. (setq s (concat
  15959. (substring s 0 (match-beginning ng))
  15960. new
  15961. (substring s (match-end ng))))))
  15962. s))
  15963. (defun org-recenter-calendar (date)
  15964. "If the calendar is visible, recenter it to DATE."
  15965. (let ((cwin (get-buffer-window "*Calendar*" t)))
  15966. (when cwin
  15967. (let ((calendar-move-hook nil))
  15968. (with-selected-window cwin
  15969. (calendar-goto-date (if (listp date) date
  15970. (calendar-gregorian-from-absolute date))))))))
  15971. (defun org-goto-calendar (&optional arg)
  15972. "Go to the Emacs calendar at the current date.
  15973. If there is a time stamp in the current line, go to that date.
  15974. A prefix ARG can be used to force the current date."
  15975. (interactive "P")
  15976. (let ((tsr org-ts-regexp) diff
  15977. (calendar-move-hook nil)
  15978. (calendar-view-holidays-initially-flag nil)
  15979. (calendar-view-diary-initially-flag nil))
  15980. (if (or (org-at-timestamp-p)
  15981. (save-excursion
  15982. (beginning-of-line 1)
  15983. (looking-at (concat ".*" tsr))))
  15984. (let ((d1 (time-to-days (current-time)))
  15985. (d2 (time-to-days
  15986. (org-time-string-to-time (match-string 1)))))
  15987. (setq diff (- d2 d1))))
  15988. (calendar)
  15989. (calendar-goto-today)
  15990. (if (and diff (not arg)) (calendar-forward-day diff))))
  15991. (defun org-get-date-from-calendar ()
  15992. "Return a list (month day year) of date at point in calendar."
  15993. (with-current-buffer "*Calendar*"
  15994. (save-match-data
  15995. (calendar-cursor-to-date))))
  15996. (defun org-date-from-calendar ()
  15997. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  15998. If there is already a time stamp at the cursor position, update it."
  15999. (interactive)
  16000. (if (org-at-timestamp-p t)
  16001. (org-timestamp-change 0 'calendar)
  16002. (let ((cal-date (org-get-date-from-calendar)))
  16003. (org-insert-time-stamp
  16004. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  16005. (defcustom org-effort-durations
  16006. `(("h" . 60)
  16007. ("d" . ,(* 60 8))
  16008. ("w" . ,(* 60 8 5))
  16009. ("m" . ,(* 60 8 5 4))
  16010. ("y" . ,(* 60 8 5 40)))
  16011. "Conversion factor to minutes for an effort modifier.
  16012. Each entry has the form (MODIFIER . MINUTES).
  16013. In an effort string, a number followed by MODIFIER is multiplied
  16014. by the specified number of MINUTES to obtain an effort in
  16015. minutes.
  16016. For example, if the value of this variable is ((\"hours\" . 60)), then an
  16017. effort string \"2hours\" is equivalent to 120 minutes."
  16018. :group 'org-agenda
  16019. :version "24.1"
  16020. :type '(alist :key-type (string :tag "Modifier")
  16021. :value-type (number :tag "Minutes")))
  16022. (defun org-minutes-to-clocksum-string (m)
  16023. "Format number of minutes as a clocksum string.
  16024. The format is determined by `org-time-clocksum-format',
  16025. `org-time-clocksum-use-fractional' and
  16026. `org-time-clocksum-fractional-format' and
  16027. `org-time-clocksum-use-effort-durations'."
  16028. (let ((clocksum "")
  16029. (m (round m)) ; Don't allow fractions of minutes
  16030. h d w mo y fmt n)
  16031. (setq h (if org-time-clocksum-use-effort-durations
  16032. (cdr (assoc "h" org-effort-durations)) 60)
  16033. d (if org-time-clocksum-use-effort-durations
  16034. (/ (cdr (assoc "d" org-effort-durations)) h) 24)
  16035. w (if org-time-clocksum-use-effort-durations
  16036. (/ (cdr (assoc "w" org-effort-durations)) (* d h)) 7)
  16037. mo (if org-time-clocksum-use-effort-durations
  16038. (/ (cdr (assoc "m" org-effort-durations)) (* d h)) 30)
  16039. y (if org-time-clocksum-use-effort-durations
  16040. (/ (cdr (assoc "y" org-effort-durations)) (* d h)) 365))
  16041. ;; fractional format
  16042. (if org-time-clocksum-use-fractional
  16043. (cond
  16044. ;; single format string
  16045. ((stringp org-time-clocksum-fractional-format)
  16046. (format org-time-clocksum-fractional-format (/ m (float h))))
  16047. ;; choice of fractional formats for different time units
  16048. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :years))
  16049. (> (/ (truncate m) (* y d h)) 0))
  16050. (format fmt (/ m (* y d (float h)))))
  16051. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :months))
  16052. (> (/ (truncate m) (* mo d h)) 0))
  16053. (format fmt (/ m (* mo d (float h)))))
  16054. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  16055. (> (/ (truncate m) (* w d h)) 0))
  16056. (format fmt (/ m (* w d (float h)))))
  16057. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :days))
  16058. (> (/ (truncate m) (* d h)) 0))
  16059. (format fmt (/ m (* d (float h)))))
  16060. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  16061. (> (/ (truncate m) h) 0))
  16062. (format fmt (/ m (float h))))
  16063. ((setq fmt (plist-get org-time-clocksum-fractional-format :minutes))
  16064. (format fmt m))
  16065. ;; fall back to smallest time unit with a format
  16066. ((setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  16067. (format fmt (/ m (float h))))
  16068. ((setq fmt (plist-get org-time-clocksum-fractional-format :days))
  16069. (format fmt (/ m (* d (float h)))))
  16070. ((setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  16071. (format fmt (/ m (* w d (float h)))))
  16072. ((setq fmt (plist-get org-time-clocksum-fractional-format :months))
  16073. (format fmt (/ m (* mo d (float h)))))
  16074. ((setq fmt (plist-get org-time-clocksum-fractional-format :years))
  16075. (format fmt (/ m (* y d (float h))))))
  16076. ;; standard (non-fractional) format, with single format string
  16077. (if (stringp org-time-clocksum-format)
  16078. (format org-time-clocksum-format (setq n (/ m h)) (- m (* h n)))
  16079. ;; separate formats components
  16080. (and (setq fmt (plist-get org-time-clocksum-format :years))
  16081. (or (> (setq n (/ (truncate m) (* y d h))) 0)
  16082. (plist-get org-time-clocksum-format :require-years))
  16083. (setq clocksum (concat clocksum (format fmt n))
  16084. m (- m (* n y d h))))
  16085. (and (setq fmt (plist-get org-time-clocksum-format :months))
  16086. (or (> (setq n (/ (truncate m) (* mo d h))) 0)
  16087. (plist-get org-time-clocksum-format :require-months))
  16088. (setq clocksum (concat clocksum (format fmt n))
  16089. m (- m (* n mo d h))))
  16090. (and (setq fmt (plist-get org-time-clocksum-format :weeks))
  16091. (or (> (setq n (/ (truncate m) (* w d h))) 0)
  16092. (plist-get org-time-clocksum-format :require-weeks))
  16093. (setq clocksum (concat clocksum (format fmt n))
  16094. m (- m (* n w d h))))
  16095. (and (setq fmt (plist-get org-time-clocksum-format :days))
  16096. (or (> (setq n (/ (truncate m) (* d h))) 0)
  16097. (plist-get org-time-clocksum-format :require-days))
  16098. (setq clocksum (concat clocksum (format fmt n))
  16099. m (- m (* n d h))))
  16100. (and (setq fmt (plist-get org-time-clocksum-format :hours))
  16101. (or (> (setq n (/ (truncate m) h)) 0)
  16102. (plist-get org-time-clocksum-format :require-hours))
  16103. (setq clocksum (concat clocksum (format fmt n))
  16104. m (- m (* n h))))
  16105. (and (setq fmt (plist-get org-time-clocksum-format :minutes))
  16106. (or (> m 0) (plist-get org-time-clocksum-format :require-minutes))
  16107. (setq clocksum (concat clocksum (format fmt m))))
  16108. ;; return formatted time duration
  16109. clocksum))))
  16110. (defalias 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string)
  16111. (make-obsolete 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string
  16112. "Org mode version 8.0")
  16113. (defun org-hours-to-clocksum-string (n)
  16114. (org-minutes-to-clocksum-string (* n 60)))
  16115. (defun org-hh:mm-string-to-minutes (s)
  16116. "Convert a string H:MM to a number of minutes.
  16117. If the string is just a number, interpret it as minutes.
  16118. In fact, the first hh:mm or number in the string will be taken,
  16119. there can be extra stuff in the string.
  16120. If no number is found, the return value is 0."
  16121. (cond
  16122. ((integerp s) s)
  16123. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  16124. (+ (* (string-to-number (match-string 1 s)) 60)
  16125. (string-to-number (match-string 2 s))))
  16126. ((string-match "\\([0-9]+\\)" s)
  16127. (string-to-number (match-string 1 s)))
  16128. (t 0)))
  16129. (defcustom org-image-actual-width t
  16130. "Should we use the actual width of images when inlining them?
  16131. When set to `t', always use the image width.
  16132. When set to a number, use imagemagick (when available) to set
  16133. the image's width to this value.
  16134. When set to a number in a list, try to get the width from any
  16135. #+ATTR.* keyword if it matches a width specification like
  16136. #+ATTR_HTML: :width 300px
  16137. and fall back on that number if none is found.
  16138. When set to nil, try to get the width from an #+ATTR.* keyword
  16139. and fall back on the original width if none is found.
  16140. This requires Emacs >= 24.1, build with imagemagick support."
  16141. :group 'org-appearance
  16142. :version "24.4"
  16143. :package-version '(Org . "8.0")
  16144. :type '(choice
  16145. (const :tag "Use the image width" t)
  16146. (integer :tag "Use a number of pixels")
  16147. (list :tag "Use #+ATTR* or a number of pixels" (integer))
  16148. (const :tag "Use #+ATTR* or don't resize" nil)))
  16149. (defcustom org-agenda-inhibit-startup nil
  16150. "Inhibit startup when preparing agenda buffers.
  16151. When this variable is `t' (the default), the initialization of
  16152. the Org agenda buffers is inhibited: e.g. the visibility state
  16153. is not set, the tables are not re-aligned, etc."
  16154. :type 'boolean
  16155. :version "24.3"
  16156. :group 'org-agenda)
  16157. (defun org-duration-string-to-minutes (s &optional output-to-string)
  16158. "Convert a duration string S to minutes.
  16159. A bare number is interpreted as minutes, modifiers can be set by
  16160. customizing `org-effort-durations' (which see).
  16161. Entries containing a colon are interpreted as H:MM by
  16162. `org-hh:mm-string-to-minutes'."
  16163. (let ((result 0)
  16164. (re (concat "\\([0-9.]+\\) *\\("
  16165. (regexp-opt (mapcar 'car org-effort-durations))
  16166. "\\)")))
  16167. (while (string-match re s)
  16168. (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  16169. (string-to-number (match-string 1 s))))
  16170. (setq s (replace-match "" nil t s)))
  16171. (setq result (floor result))
  16172. (incf result (org-hh:mm-string-to-minutes s))
  16173. (if output-to-string (number-to-string result) result)))
  16174. ;;;; Files
  16175. (defun org-save-all-org-buffers ()
  16176. "Save all Org-mode buffers without user confirmation."
  16177. (interactive)
  16178. (message "Saving all Org-mode buffers...")
  16179. (save-some-buffers t (lambda () (derived-mode-p 'org-mode)))
  16180. (when (featurep 'org-id) (org-id-locations-save))
  16181. (message "Saving all Org-mode buffers... done"))
  16182. (defun org-revert-all-org-buffers ()
  16183. "Revert all Org-mode buffers.
  16184. Prompt for confirmation when there are unsaved changes.
  16185. Be sure you know what you are doing before letting this function
  16186. overwrite your changes.
  16187. This function is useful in a setup where one tracks org files
  16188. with a version control system, to revert on one machine after pulling
  16189. changes from another. I believe the procedure must be like this:
  16190. 1. M-x org-save-all-org-buffers
  16191. 2. Pull changes from the other machine, resolve conflicts
  16192. 3. M-x org-revert-all-org-buffers"
  16193. (interactive)
  16194. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  16195. (error "Abort"))
  16196. (save-excursion
  16197. (save-window-excursion
  16198. (mapc
  16199. (lambda (b)
  16200. (when (and (with-current-buffer b (derived-mode-p 'org-mode))
  16201. (with-current-buffer b buffer-file-name))
  16202. (org-pop-to-buffer-same-window b)
  16203. (revert-buffer t 'no-confirm)))
  16204. (buffer-list))
  16205. (when (and (featurep 'org-id) org-id-track-globally)
  16206. (org-id-locations-load)))))
  16207. ;;;; Agenda files
  16208. ;;;###autoload
  16209. (defun org-switchb (&optional arg)
  16210. "Switch between Org buffers.
  16211. With one prefix argument, restrict available buffers to files.
  16212. With two prefix arguments, restrict available buffers to agenda files.
  16213. Defaults to `iswitchb' for buffer name completion.
  16214. Set `org-completion-use-ido' to make it use ido instead."
  16215. (interactive "P")
  16216. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  16217. ((equal arg '(16)) (org-buffer-list 'agenda))
  16218. (t (org-buffer-list))))
  16219. (org-completion-use-iswitchb org-completion-use-iswitchb)
  16220. (org-completion-use-ido org-completion-use-ido))
  16221. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  16222. (setq org-completion-use-iswitchb t))
  16223. (org-pop-to-buffer-same-window
  16224. (org-icompleting-read "Org buffer: "
  16225. (mapcar 'list (mapcar 'buffer-name blist))
  16226. nil t))))
  16227. ;;; Define some older names previously used for this functionality
  16228. ;;;###autoload
  16229. (defalias 'org-ido-switchb 'org-switchb)
  16230. ;;;###autoload
  16231. (defalias 'org-iswitchb 'org-switchb)
  16232. (defun org-buffer-list (&optional predicate exclude-tmp)
  16233. "Return a list of Org buffers.
  16234. PREDICATE can be `export', `files' or `agenda'.
  16235. export restrict the list to Export buffers.
  16236. files restrict the list to buffers visiting Org files.
  16237. agenda restrict the list to buffers visiting agenda files.
  16238. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  16239. (let* ((bfn nil)
  16240. (agenda-files (and (eq predicate 'agenda)
  16241. (mapcar 'file-truename (org-agenda-files t))))
  16242. (filter
  16243. (cond
  16244. ((eq predicate 'files)
  16245. (lambda (b) (with-current-buffer b (derived-mode-p 'org-mode))))
  16246. ((eq predicate 'export)
  16247. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  16248. ((eq predicate 'agenda)
  16249. (lambda (b)
  16250. (with-current-buffer b
  16251. (and (derived-mode-p 'org-mode)
  16252. (setq bfn (buffer-file-name b))
  16253. (member (file-truename bfn) agenda-files)))))
  16254. (t (lambda (b) (with-current-buffer b
  16255. (or (derived-mode-p 'org-mode)
  16256. (string-match "\*Org .*Export"
  16257. (buffer-name b)))))))))
  16258. (delq nil
  16259. (mapcar
  16260. (lambda(b)
  16261. (if (and (funcall filter b)
  16262. (or (not exclude-tmp)
  16263. (not (string-match "tmp" (buffer-name b)))))
  16264. b
  16265. nil))
  16266. (buffer-list)))))
  16267. (defun org-agenda-files (&optional unrestricted archives)
  16268. "Get the list of agenda files.
  16269. Optional UNRESTRICTED means return the full list even if a restriction
  16270. is currently in place.
  16271. When ARCHIVES is t, include all archive files that are really being
  16272. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  16273. `org-agenda-archives-mode' is t."
  16274. (let ((files
  16275. (cond
  16276. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  16277. ((stringp org-agenda-files) (org-read-agenda-file-list))
  16278. ((listp org-agenda-files) org-agenda-files)
  16279. (t (error "Invalid value of `org-agenda-files'")))))
  16280. (setq files (apply 'append
  16281. (mapcar (lambda (f)
  16282. (if (file-directory-p f)
  16283. (directory-files
  16284. f t org-agenda-file-regexp)
  16285. (list f)))
  16286. files)))
  16287. (when org-agenda-skip-unavailable-files
  16288. (setq files (delq nil
  16289. (mapcar (function
  16290. (lambda (file)
  16291. (and (file-readable-p file) file)))
  16292. files))))
  16293. (when (or (eq archives t)
  16294. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  16295. (setq files (org-add-archive-files files)))
  16296. files))
  16297. (defun org-agenda-file-p (&optional file)
  16298. "Return non-nil, if FILE is an agenda file.
  16299. If FILE is omitted, use the file associated with the current
  16300. buffer."
  16301. (member (or file (buffer-file-name))
  16302. (org-agenda-files t)))
  16303. (defun org-edit-agenda-file-list ()
  16304. "Edit the list of agenda files.
  16305. Depending on setup, this either uses customize to edit the variable
  16306. `org-agenda-files', or it visits the file that is holding the list. In the
  16307. latter case, the buffer is set up in a way that saving it automatically kills
  16308. the buffer and restores the previous window configuration."
  16309. (interactive)
  16310. (if (stringp org-agenda-files)
  16311. (let ((cw (current-window-configuration)))
  16312. (find-file org-agenda-files)
  16313. (org-set-local 'org-window-configuration cw)
  16314. (org-add-hook 'after-save-hook
  16315. (lambda ()
  16316. (set-window-configuration
  16317. (prog1 org-window-configuration
  16318. (kill-buffer (current-buffer))))
  16319. (org-install-agenda-files-menu)
  16320. (message "New agenda file list installed"))
  16321. nil 'local)
  16322. (message "%s" (substitute-command-keys
  16323. "Edit list and finish with \\[save-buffer]")))
  16324. (customize-variable 'org-agenda-files)))
  16325. (defun org-store-new-agenda-file-list (list)
  16326. "Set new value for the agenda file list and save it correctly."
  16327. (if (stringp org-agenda-files)
  16328. (let ((fe (org-read-agenda-file-list t)) b u)
  16329. (while (setq b (find-buffer-visiting org-agenda-files))
  16330. (kill-buffer b))
  16331. (with-temp-file org-agenda-files
  16332. (insert
  16333. (mapconcat
  16334. (lambda (f) ;; Keep un-expanded entries.
  16335. (if (setq u (assoc f fe))
  16336. (cdr u)
  16337. f))
  16338. list "\n")
  16339. "\n")))
  16340. (let ((org-mode-hook nil) (org-inhibit-startup t)
  16341. (org-insert-mode-line-in-empty-file nil))
  16342. (setq org-agenda-files list)
  16343. (customize-save-variable 'org-agenda-files org-agenda-files))))
  16344. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  16345. "Read the list of agenda files from a file.
  16346. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  16347. filenames, used by `org-store-new-agenda-file-list' to write back
  16348. un-expanded file names."
  16349. (when (file-directory-p org-agenda-files)
  16350. (error "`org-agenda-files' cannot be a single directory"))
  16351. (when (stringp org-agenda-files)
  16352. (with-temp-buffer
  16353. (insert-file-contents org-agenda-files)
  16354. (mapcar
  16355. (lambda (f)
  16356. (let ((e (expand-file-name (substitute-in-file-name f)
  16357. org-directory)))
  16358. (if pair-with-expansion
  16359. (cons e f)
  16360. e)))
  16361. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  16362. ;;;###autoload
  16363. (defun org-cycle-agenda-files ()
  16364. "Cycle through the files in `org-agenda-files'.
  16365. If the current buffer visits an agenda file, find the next one in the list.
  16366. If the current buffer does not, find the first agenda file."
  16367. (interactive)
  16368. (let* ((fs (org-agenda-files t))
  16369. (files (append fs (list (car fs))))
  16370. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  16371. file)
  16372. (unless files (error "No agenda files"))
  16373. (catch 'exit
  16374. (while (setq file (pop files))
  16375. (if (equal (file-truename file) tcf)
  16376. (when (car files)
  16377. (find-file (car files))
  16378. (throw 'exit t))))
  16379. (find-file (car fs)))
  16380. (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
  16381. (defun org-agenda-file-to-front (&optional to-end)
  16382. "Move/add the current file to the top of the agenda file list.
  16383. If the file is not present in the list, it is added to the front. If it is
  16384. present, it is moved there. With optional argument TO-END, add/move to the
  16385. end of the list."
  16386. (interactive "P")
  16387. (let ((org-agenda-skip-unavailable-files nil)
  16388. (file-alist (mapcar (lambda (x)
  16389. (cons (file-truename x) x))
  16390. (org-agenda-files t)))
  16391. (ctf (file-truename
  16392. (or buffer-file-name
  16393. (error "Please save the current buffer to a file"))))
  16394. x had)
  16395. (setq x (assoc ctf file-alist) had x)
  16396. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  16397. (if to-end
  16398. (setq file-alist (append (delq x file-alist) (list x)))
  16399. (setq file-alist (cons x (delq x file-alist))))
  16400. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  16401. (org-install-agenda-files-menu)
  16402. (message "File %s to %s of agenda file list"
  16403. (if had "moved" "added") (if to-end "end" "front"))))
  16404. (defun org-remove-file (&optional file)
  16405. "Remove current file from the list of files in variable `org-agenda-files'.
  16406. These are the files which are being checked for agenda entries.
  16407. Optional argument FILE means use this file instead of the current."
  16408. (interactive)
  16409. (let* ((org-agenda-skip-unavailable-files nil)
  16410. (file (or file buffer-file-name
  16411. (error "Current buffer does not visit a file")))
  16412. (true-file (file-truename file))
  16413. (afile (abbreviate-file-name file))
  16414. (files (delq nil (mapcar
  16415. (lambda (x)
  16416. (if (equal true-file
  16417. (file-truename x))
  16418. nil x))
  16419. (org-agenda-files t)))))
  16420. (if (not (= (length files) (length (org-agenda-files t))))
  16421. (progn
  16422. (org-store-new-agenda-file-list files)
  16423. (org-install-agenda-files-menu)
  16424. (message "Removed file: %s" afile))
  16425. (message "File was not in list: %s (not removed)" afile))))
  16426. (defun org-file-menu-entry (file)
  16427. (vector file (list 'find-file file) t))
  16428. (defun org-check-agenda-file (file)
  16429. "Make sure FILE exists. If not, ask user what to do."
  16430. (when (not (file-exists-p file))
  16431. (message "Non-existent agenda file %s. [R]emove from list or [A]bort?"
  16432. (abbreviate-file-name file))
  16433. (let ((r (downcase (read-char-exclusive))))
  16434. (cond
  16435. ((equal r ?r)
  16436. (org-remove-file file)
  16437. (throw 'nextfile t))
  16438. (t (error "Abort"))))))
  16439. (defun org-get-agenda-file-buffer (file)
  16440. "Get a buffer visiting FILE. If the buffer needs to be created, add
  16441. it to the list of buffers which might be released later."
  16442. (let ((buf (org-find-base-buffer-visiting file)))
  16443. (if buf
  16444. buf ; just return it
  16445. ;; Make a new buffer and remember it
  16446. (setq buf (find-file-noselect file))
  16447. (if buf (push buf org-agenda-new-buffers))
  16448. buf)))
  16449. (defun org-release-buffers (blist)
  16450. "Release all buffers in list, asking the user for confirmation when needed.
  16451. When a buffer is unmodified, it is just killed. When modified, it is saved
  16452. \(if the user agrees) and then killed."
  16453. (let (buf file)
  16454. (while (setq buf (pop blist))
  16455. (setq file (buffer-file-name buf))
  16456. (when (and (buffer-modified-p buf)
  16457. file
  16458. (y-or-n-p (format "Save file %s? " file)))
  16459. (with-current-buffer buf (save-buffer)))
  16460. (kill-buffer buf))))
  16461. (defun org-agenda-prepare-buffers (files)
  16462. "Create buffers for all agenda files, protect archived trees and comments."
  16463. (interactive)
  16464. (let ((pa '(:org-archived t))
  16465. (pc '(:org-comment t))
  16466. (pall '(:org-archived t :org-comment t))
  16467. (inhibit-read-only t)
  16468. (org-inhibit-startup org-agenda-inhibit-startup)
  16469. (rea (concat ":" org-archive-tag ":"))
  16470. file re)
  16471. (setq org-tag-alist-for-agenda nil
  16472. org-tag-groups-alist-for-agenda nil)
  16473. (save-excursion
  16474. (save-restriction
  16475. (while (setq file (pop files))
  16476. (catch 'nextfile
  16477. (if (bufferp file)
  16478. (set-buffer file)
  16479. (org-check-agenda-file file)
  16480. (set-buffer (org-get-agenda-file-buffer file)))
  16481. (widen)
  16482. (org-set-regexps-and-options-for-tags)
  16483. (org-refresh-category-properties)
  16484. (org-refresh-properties org-effort-property 'org-effort)
  16485. (org-refresh-properties "APPT_WARNTIME" 'org-appt-warntime)
  16486. (setq org-todo-keywords-for-agenda
  16487. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  16488. (setq org-done-keywords-for-agenda
  16489. (append org-done-keywords-for-agenda org-done-keywords))
  16490. (setq org-todo-keyword-alist-for-agenda
  16491. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  16492. (setq org-drawers-for-agenda
  16493. (append org-drawers-for-agenda org-drawers))
  16494. (unless (equal org-tag-alist-for-agenda org-tag-alist)
  16495. (setq org-tag-alist-for-agenda
  16496. (append org-tag-alist-for-agenda org-tag-alist)))
  16497. (if org-group-tags
  16498. (setq org-tag-groups-alist-for-agenda
  16499. (org-uniquify-alist
  16500. (append org-tag-groups-alist-for-agenda org-tag-groups-alist))))
  16501. (org-with-silent-modifications
  16502. (save-excursion
  16503. (remove-text-properties (point-min) (point-max) pall)
  16504. (when org-agenda-skip-archived-trees
  16505. (goto-char (point-min))
  16506. (while (re-search-forward rea nil t)
  16507. (if (org-at-heading-p t)
  16508. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  16509. (goto-char (point-min))
  16510. (setq re (format org-heading-keyword-regexp-format
  16511. org-comment-string))
  16512. (while (re-search-forward re nil t)
  16513. (add-text-properties
  16514. (match-beginning 0) (org-end-of-subtree t) pc))))))))
  16515. (setq org-todo-keywords-for-agenda
  16516. (org-uniquify org-todo-keywords-for-agenda))
  16517. (setq org-todo-keyword-alist-for-agenda
  16518. (org-uniquify org-todo-keyword-alist-for-agenda))))
  16519. ;;;; CDLaTeX minor mode
  16520. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  16521. "Keymap for the minor `org-cdlatex-mode'.")
  16522. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  16523. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  16524. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  16525. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  16526. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  16527. (defvar org-cdlatex-texmathp-advice-is-done nil
  16528. "Flag remembering if we have applied the advice to texmathp already.")
  16529. (define-minor-mode org-cdlatex-mode
  16530. "Toggle the minor `org-cdlatex-mode'.
  16531. This mode supports entering LaTeX environment and math in LaTeX fragments
  16532. in Org-mode.
  16533. \\{org-cdlatex-mode-map}"
  16534. nil " OCDL" nil
  16535. (when org-cdlatex-mode
  16536. (require 'cdlatex)
  16537. (run-hooks 'cdlatex-mode-hook)
  16538. (cdlatex-compute-tables))
  16539. (unless org-cdlatex-texmathp-advice-is-done
  16540. (setq org-cdlatex-texmathp-advice-is-done t)
  16541. (defadvice texmathp (around org-math-always-on activate)
  16542. "Always return t in org-mode buffers.
  16543. This is because we want to insert math symbols without dollars even outside
  16544. the LaTeX math segments. If Orgmode thinks that point is actually inside
  16545. an embedded LaTeX fragment, let texmathp do its job.
  16546. \\[org-cdlatex-mode-map]"
  16547. (interactive)
  16548. (let (p)
  16549. (cond
  16550. ((not (derived-mode-p 'org-mode)) ad-do-it)
  16551. ((eq this-command 'cdlatex-math-symbol)
  16552. (setq ad-return-value t
  16553. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  16554. (t
  16555. (let ((p (org-inside-LaTeX-fragment-p)))
  16556. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  16557. (setq ad-return-value t
  16558. texmathp-why '("Org-mode embedded math" . 0))
  16559. (if p ad-do-it)))))))))
  16560. (defun turn-on-org-cdlatex ()
  16561. "Unconditionally turn on `org-cdlatex-mode'."
  16562. (org-cdlatex-mode 1))
  16563. (defun org-try-cdlatex-tab ()
  16564. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  16565. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  16566. - inside a LaTeX fragment, or
  16567. - after the first word in a line, where an abbreviation expansion could
  16568. insert a LaTeX environment."
  16569. (when org-cdlatex-mode
  16570. (cond
  16571. ;; Before any word on the line: No expansion possible.
  16572. ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
  16573. ;; Just after first word on the line: Expand it. Make sure it
  16574. ;; cannot happen on headlines, though.
  16575. ((save-excursion
  16576. (skip-chars-backward "a-zA-Z0-9*")
  16577. (skip-chars-backward " \t")
  16578. (and (bolp) (not (org-at-heading-p))))
  16579. (cdlatex-tab) t)
  16580. ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
  16581. (defun org-cdlatex-underscore-caret (&optional arg)
  16582. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  16583. Revert to the normal definition outside of these fragments."
  16584. (interactive "P")
  16585. (if (org-inside-LaTeX-fragment-p)
  16586. (call-interactively 'cdlatex-sub-superscript)
  16587. (let (org-cdlatex-mode)
  16588. (call-interactively (key-binding (vector last-input-event))))))
  16589. (defun org-cdlatex-math-modify (&optional arg)
  16590. "Execute `cdlatex-math-modify' in LaTeX fragments.
  16591. Revert to the normal definition outside of these fragments."
  16592. (interactive "P")
  16593. (if (org-inside-LaTeX-fragment-p)
  16594. (call-interactively 'cdlatex-math-modify)
  16595. (let (org-cdlatex-mode)
  16596. (call-interactively (key-binding (vector last-input-event))))))
  16597. ;;;; LaTeX fragments
  16598. (defvar org-latex-regexps
  16599. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  16600. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  16601. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  16602. ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  16603. ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  16604. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  16605. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  16606. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  16607. "Regular expressions for matching embedded LaTeX.")
  16608. (defun org-inside-LaTeX-fragment-p ()
  16609. "Test if point is inside a LaTeX fragment.
  16610. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  16611. sequence appearing also before point.
  16612. Even though the matchers for math are configurable, this function assumes
  16613. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  16614. delimiters are skipped when they have been removed by customization.
  16615. The return value is nil, or a cons cell with the delimiter and the
  16616. position of this delimiter.
  16617. This function does a reasonably good job, but can locally be fooled by
  16618. for example currency specifications. For example it will assume being in
  16619. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  16620. fragments that are properly closed, but during editing, we have to live
  16621. with the uncertainty caused by missing closing delimiters. This function
  16622. looks only before point, not after."
  16623. (catch 'exit
  16624. (let ((pos (point))
  16625. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  16626. (lim (progn
  16627. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  16628. (point)))
  16629. dd-on str (start 0) m re)
  16630. (goto-char pos)
  16631. (when dodollar
  16632. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  16633. re (nth 1 (assoc "$" org-latex-regexps)))
  16634. (while (string-match re str start)
  16635. (cond
  16636. ((= (match-end 0) (length str))
  16637. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  16638. ((= (match-end 0) (- (length str) 5))
  16639. (throw 'exit nil))
  16640. (t (setq start (match-end 0))))))
  16641. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  16642. (goto-char pos)
  16643. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  16644. (and (match-beginning 2) (throw 'exit nil))
  16645. ;; count $$
  16646. (while (re-search-backward "\\$\\$" lim t)
  16647. (setq dd-on (not dd-on)))
  16648. (goto-char pos)
  16649. (if dd-on (cons "$$" m))))))
  16650. (defun org-inside-latex-macro-p ()
  16651. "Is point inside a LaTeX macro or its arguments?"
  16652. (save-match-data
  16653. (org-in-regexp
  16654. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  16655. (defvar org-latex-fragment-image-overlays nil
  16656. "List of overlays carrying the images of latex fragments.")
  16657. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  16658. (defun org-remove-latex-fragment-image-overlays ()
  16659. "Remove all overlays with LaTeX fragment images in current buffer."
  16660. (mapc 'delete-overlay org-latex-fragment-image-overlays)
  16661. (setq org-latex-fragment-image-overlays nil))
  16662. (defun org-preview-latex-fragment (&optional subtree)
  16663. "Preview the LaTeX fragment at point, or all locally or globally.
  16664. If the cursor is in a LaTeX fragment, create the image and overlay
  16665. it over the source code. If there is no fragment at point, display
  16666. all fragments in the current text, from one headline to the next. With
  16667. prefix SUBTREE, display all fragments in the current subtree. With a
  16668. double prefix arg \\[universal-argument] \\[universal-argument], or when \
  16669. the cursor is before the first headline,
  16670. display all fragments in the buffer.
  16671. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  16672. (interactive "P")
  16673. (unless buffer-file-name
  16674. (error "Can't preview LaTeX fragment in a non-file buffer"))
  16675. (org-remove-latex-fragment-image-overlays)
  16676. (save-excursion
  16677. (save-restriction
  16678. (let (beg end at msg)
  16679. (cond
  16680. ((or (equal subtree '(16))
  16681. (not (save-excursion
  16682. (re-search-backward org-outline-regexp-bol nil t))))
  16683. (setq beg (point-min) end (point-max)
  16684. msg "Creating images for buffer...%s"))
  16685. ((equal subtree '(4))
  16686. (org-back-to-heading)
  16687. (setq beg (point) end (org-end-of-subtree t)
  16688. msg "Creating images for subtree...%s"))
  16689. (t
  16690. (if (setq at (org-inside-LaTeX-fragment-p))
  16691. (goto-char (max (point-min) (- (cdr at) 2)))
  16692. (org-back-to-heading))
  16693. (setq beg (point) end (progn (outline-next-heading) (point))
  16694. msg (if at "Creating image...%s"
  16695. "Creating images for entry...%s"))))
  16696. (message msg "")
  16697. (narrow-to-region beg end)
  16698. (goto-char beg)
  16699. (org-format-latex
  16700. (concat org-latex-preview-ltxpng-directory (file-name-sans-extension
  16701. (file-name-nondirectory
  16702. buffer-file-name)))
  16703. default-directory 'overlays msg at 'forbuffer
  16704. org-latex-create-formula-image-program)
  16705. (message msg "done. Use `C-c C-c' to remove images.")))))
  16706. (defun org-format-latex (prefix &optional dir overlays msg at
  16707. forbuffer processing-type)
  16708. "Replace LaTeX fragments with links to an image, and produce images.
  16709. Some of the options can be changed using the variable
  16710. `org-format-latex-options'."
  16711. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  16712. (let* ((prefixnodir (file-name-nondirectory prefix))
  16713. (absprefix (expand-file-name prefix dir))
  16714. (todir (file-name-directory absprefix))
  16715. (opt org-format-latex-options)
  16716. (optnew org-format-latex-options)
  16717. (matchers (plist-get opt :matchers))
  16718. (re-list org-latex-regexps)
  16719. (cnt 0) txt hash link beg end re e checkdir
  16720. string
  16721. m n block-type block linkfile movefile ov)
  16722. ;; Check the different regular expressions
  16723. (while (setq e (pop re-list))
  16724. (setq m (car e) re (nth 1 e) n (nth 2 e) block-type (nth 3 e)
  16725. block (if block-type "\n\n" ""))
  16726. (when (member m matchers)
  16727. (goto-char (point-min))
  16728. (while (re-search-forward re nil t)
  16729. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  16730. (or (not overlays)
  16731. (not (eq (get-char-property (match-beginning n)
  16732. 'org-overlay-type)
  16733. 'org-latex-overlay))))
  16734. (cond
  16735. ((eq processing-type 'verbatim))
  16736. ((eq processing-type 'mathjax)
  16737. ;; Prepare for MathJax processing.
  16738. (setq string (match-string n))
  16739. (when (member m '("$" "$1"))
  16740. (save-excursion
  16741. (delete-region (match-beginning n) (match-end n))
  16742. (goto-char (match-beginning n))
  16743. (insert (concat "\\(" (substring string 1 -1) "\\)")))))
  16744. ((or (eq processing-type 'dvipng)
  16745. (eq processing-type 'imagemagick))
  16746. ;; Process to an image.
  16747. (setq txt (match-string n)
  16748. beg (match-beginning n) end (match-end n)
  16749. cnt (1+ cnt))
  16750. (let ((face (face-at-point))
  16751. (fg (plist-get opt :foreground))
  16752. (bg (plist-get opt :background))
  16753. ;; Ensure full list is printed.
  16754. print-length print-level)
  16755. (when forbuffer
  16756. ;; Get the colors from the face at point.
  16757. (goto-char beg)
  16758. (when (eq fg 'auto)
  16759. (setq fg (face-attribute face :foreground nil 'default)))
  16760. (when (eq bg 'auto)
  16761. (setq bg (face-attribute face :background nil 'default)))
  16762. (setq optnew (copy-sequence opt))
  16763. (plist-put optnew :foreground fg)
  16764. (plist-put optnew :background bg))
  16765. (setq hash (sha1 (prin1-to-string
  16766. (list org-format-latex-header
  16767. org-latex-default-packages-alist
  16768. org-latex-packages-alist
  16769. org-format-latex-options
  16770. forbuffer txt fg bg)))
  16771. linkfile (format "%s_%s.png" prefix hash)
  16772. movefile (format "%s_%s.png" absprefix hash)))
  16773. (setq link (concat block "[[file:" linkfile "]]" block))
  16774. (if msg (message msg cnt))
  16775. (goto-char beg)
  16776. (unless checkdir ; Ensure the directory exists.
  16777. (setq checkdir t)
  16778. (or (file-directory-p todir) (make-directory todir t)))
  16779. (unless (file-exists-p movefile)
  16780. (org-create-formula-image
  16781. txt movefile optnew forbuffer processing-type))
  16782. (if overlays
  16783. (progn
  16784. (mapc (lambda (o)
  16785. (if (eq (overlay-get o 'org-overlay-type)
  16786. 'org-latex-overlay)
  16787. (delete-overlay o)))
  16788. (overlays-in beg end))
  16789. (setq ov (make-overlay beg end))
  16790. (overlay-put ov 'org-overlay-type 'org-latex-overlay)
  16791. (if (featurep 'xemacs)
  16792. (progn
  16793. (overlay-put ov 'invisible t)
  16794. (overlay-put
  16795. ov 'end-glyph
  16796. (make-glyph (vector 'png :file movefile))))
  16797. (overlay-put
  16798. ov 'display
  16799. (list 'image :type 'png :file movefile :ascent 'center)))
  16800. (push ov org-latex-fragment-image-overlays)
  16801. (goto-char end))
  16802. (delete-region beg end)
  16803. (insert (org-add-props link
  16804. (list 'org-latex-src
  16805. (replace-regexp-in-string
  16806. "\"" "" txt)
  16807. 'org-latex-src-embed-type
  16808. (if block-type 'paragraph 'character))))))
  16809. ((eq processing-type 'mathml)
  16810. ;; Process to MathML
  16811. (unless (save-match-data (org-format-latex-mathml-available-p))
  16812. (error "LaTeX to MathML converter not configured"))
  16813. (setq txt (match-string n)
  16814. beg (match-beginning n) end (match-end n)
  16815. cnt (1+ cnt))
  16816. (if msg (message msg cnt))
  16817. (goto-char beg)
  16818. (delete-region beg end)
  16819. (insert (org-format-latex-as-mathml
  16820. txt block-type prefix dir)))
  16821. (t
  16822. (error "Unknown conversion type %s for latex fragments"
  16823. processing-type)))))))))
  16824. (defun org-create-math-formula (latex-frag &optional mathml-file)
  16825. "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
  16826. Use `org-latex-to-mathml-convert-command'. If the conversion is
  16827. sucessful, return the portion between \"<math...> </math>\"
  16828. elements otherwise return nil. When MATHML-FILE is specified,
  16829. write the results in to that file. When invoked as an
  16830. interactive command, prompt for LATEX-FRAG, with initial value
  16831. set to the current active region and echo the results for user
  16832. inspection."
  16833. (interactive (list (let ((frag (when (org-region-active-p)
  16834. (buffer-substring-no-properties
  16835. (region-beginning) (region-end)))))
  16836. (read-string "LaTeX Fragment: " frag nil frag))))
  16837. (unless latex-frag (error "Invalid latex-frag"))
  16838. (let* ((tmp-in-file (file-relative-name
  16839. (make-temp-name (expand-file-name "ltxmathml-in"))))
  16840. (ignore (write-region latex-frag nil tmp-in-file))
  16841. (tmp-out-file (file-relative-name
  16842. (make-temp-name (expand-file-name "ltxmathml-out"))))
  16843. (cmd (format-spec
  16844. org-latex-to-mathml-convert-command
  16845. `((?j . ,(shell-quote-argument
  16846. (expand-file-name org-latex-to-mathml-jar-file)))
  16847. (?I . ,(shell-quote-argument tmp-in-file))
  16848. (?o . ,(shell-quote-argument tmp-out-file)))))
  16849. mathml shell-command-output)
  16850. (when (org-called-interactively-p 'any)
  16851. (unless (org-format-latex-mathml-available-p)
  16852. (error "LaTeX to MathML converter not configured")))
  16853. (message "Running %s" cmd)
  16854. (setq shell-command-output (shell-command-to-string cmd))
  16855. (setq mathml
  16856. (when (file-readable-p tmp-out-file)
  16857. (with-current-buffer (find-file-noselect tmp-out-file t)
  16858. (goto-char (point-min))
  16859. (when (re-search-forward
  16860. (concat
  16861. (regexp-quote
  16862. "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">")
  16863. "\\(.\\|\n\\)*"
  16864. (regexp-quote "</math>")) nil t)
  16865. (prog1 (match-string 0) (kill-buffer))))))
  16866. (cond
  16867. (mathml
  16868. (setq mathml
  16869. (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
  16870. (when mathml-file
  16871. (write-region mathml nil mathml-file))
  16872. (when (org-called-interactively-p 'any)
  16873. (message mathml)))
  16874. ((message "LaTeX to MathML conversion failed")
  16875. (message shell-command-output)))
  16876. (delete-file tmp-in-file)
  16877. (when (file-exists-p tmp-out-file)
  16878. (delete-file tmp-out-file))
  16879. mathml))
  16880. (defun org-format-latex-as-mathml (latex-frag latex-frag-type
  16881. prefix &optional dir)
  16882. "Use `org-create-math-formula' but check local cache first."
  16883. (let* ((absprefix (expand-file-name prefix dir))
  16884. (print-length nil) (print-level nil)
  16885. (formula-id (concat
  16886. "formula-"
  16887. (sha1
  16888. (prin1-to-string
  16889. (list latex-frag
  16890. org-latex-to-mathml-convert-command)))))
  16891. (formula-cache (format "%s-%s.mathml" absprefix formula-id))
  16892. (formula-cache-dir (file-name-directory formula-cache)))
  16893. (unless (file-directory-p formula-cache-dir)
  16894. (make-directory formula-cache-dir t))
  16895. (unless (file-exists-p formula-cache)
  16896. (org-create-math-formula latex-frag formula-cache))
  16897. (if (file-exists-p formula-cache)
  16898. ;; Successful conversion. Return the link to MathML file.
  16899. (org-add-props
  16900. (format "[[file:%s]]" (file-relative-name formula-cache dir))
  16901. (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
  16902. 'org-latex-src-embed-type (if latex-frag-type
  16903. 'paragraph 'character)))
  16904. ;; Failed conversion. Return the LaTeX fragment verbatim
  16905. latex-frag)))
  16906. (defun org-create-formula-image (string tofile options buffer &optional type)
  16907. "Create an image from LaTeX source using dvipng or convert.
  16908. This function calls either `org-create-formula-image-with-dvipng'
  16909. or `org-create-formula-image-with-imagemagick' depending on the
  16910. value of `org-latex-create-formula-image-program' or on the value
  16911. of the optional TYPE variable.
  16912. Note: ultimately these two function should be combined as they
  16913. share a good deal of logic."
  16914. (org-check-external-command
  16915. "latex" "needed to convert LaTeX fragments to images")
  16916. (funcall
  16917. (case (or type org-latex-create-formula-image-program)
  16918. ('dvipng
  16919. (org-check-external-command
  16920. "dvipng" "needed to convert LaTeX fragments to images")
  16921. #'org-create-formula-image-with-dvipng)
  16922. ('imagemagick
  16923. (org-check-external-command
  16924. "convert" "you need to install imagemagick")
  16925. #'org-create-formula-image-with-imagemagick)
  16926. (t (error
  16927. "invalid value of `org-latex-create-formula-image-program'")))
  16928. string tofile options buffer))
  16929. (declare-function org-export--get-global-options "ox" (&optional backend))
  16930. (declare-function org-export--get-inbuffer-options "ox" (&optional backend))
  16931. (defun org-create-formula--latex-header ()
  16932. "Return LaTeX header appropriate for previewing a LaTeX snippet."
  16933. (org-latex-guess-inputenc
  16934. (org-splice-latex-header
  16935. org-format-latex-header
  16936. org-latex-default-packages-alist
  16937. org-latex-packages-alist t
  16938. (plist-get
  16939. (org-combine-plists
  16940. (org-export--get-global-options 'latex)
  16941. (org-export--get-inbuffer-options 'latex))
  16942. :latex-header))))
  16943. ;; This function borrows from Ganesh Swami's latex2png.el
  16944. (defun org-create-formula-image-with-dvipng (string tofile options buffer)
  16945. "This calls dvipng."
  16946. (require 'ox-latex)
  16947. (let* ((tmpdir (if (featurep 'xemacs)
  16948. (temp-directory)
  16949. temporary-file-directory))
  16950. (texfilebase (make-temp-name
  16951. (expand-file-name "orgtex" tmpdir)))
  16952. (texfile (concat texfilebase ".tex"))
  16953. (dvifile (concat texfilebase ".dvi"))
  16954. (pngfile (concat texfilebase ".png"))
  16955. (fnh (if (featurep 'xemacs)
  16956. (font-height (face-font 'default))
  16957. (face-attribute 'default :height nil)))
  16958. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  16959. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  16960. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  16961. "Black"))
  16962. (bg (or (plist-get options (if buffer :background :html-background))
  16963. "Transparent")))
  16964. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground))
  16965. (unless (string= fg "Transparent") (setq fg (org-dvipng-color-format fg))))
  16966. (if (eq bg 'default) (setq bg (org-dvipng-color :background))
  16967. (unless (string= bg "Transparent") (setq bg (org-dvipng-color-format bg))))
  16968. (let ((latex-header (org-create-formula--latex-header)))
  16969. (with-temp-file texfile
  16970. (insert latex-header)
  16971. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")))
  16972. (let ((dir default-directory))
  16973. (condition-case nil
  16974. (progn
  16975. (cd tmpdir)
  16976. (call-process "latex" nil nil nil texfile))
  16977. (error nil))
  16978. (cd dir))
  16979. (if (not (file-exists-p dvifile))
  16980. (progn (message "Failed to create dvi file from %s" texfile) nil)
  16981. (condition-case nil
  16982. (if (featurep 'xemacs)
  16983. (call-process "dvipng" nil nil nil
  16984. "-fg" fg "-bg" bg
  16985. "-T" "tight"
  16986. "-o" pngfile
  16987. dvifile)
  16988. (call-process "dvipng" nil nil nil
  16989. "-fg" fg "-bg" bg
  16990. "-D" dpi
  16991. ;;"-x" scale "-y" scale
  16992. "-T" "tight"
  16993. "-o" pngfile
  16994. dvifile))
  16995. (error nil))
  16996. (if (not (file-exists-p pngfile))
  16997. (if org-format-latex-signal-error
  16998. (error "Failed to create png file from %s" texfile)
  16999. (message "Failed to create png file from %s" texfile)
  17000. nil)
  17001. ;; Use the requested file name and clean up
  17002. (copy-file pngfile tofile 'replace)
  17003. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png" ".out") do
  17004. (if (file-exists-p (concat texfilebase e))
  17005. (delete-file (concat texfilebase e))))
  17006. pngfile))))
  17007. (declare-function org-latex-compile "ox-latex" (texfile &optional snippet))
  17008. (defun org-create-formula-image-with-imagemagick (string tofile options buffer)
  17009. "This calls convert, which is included into imagemagick."
  17010. (require 'ox-latex)
  17011. (let* ((tmpdir (if (featurep 'xemacs)
  17012. (temp-directory)
  17013. temporary-file-directory))
  17014. (texfilebase (make-temp-name
  17015. (expand-file-name "orgtex" tmpdir)))
  17016. (texfile (concat texfilebase ".tex"))
  17017. (pdffile (concat texfilebase ".pdf"))
  17018. (pngfile (concat texfilebase ".png"))
  17019. (fnh (if (featurep 'xemacs)
  17020. (font-height (face-font 'default))
  17021. (face-attribute 'default :height nil)))
  17022. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  17023. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  17024. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  17025. "black"))
  17026. (bg (or (plist-get options (if buffer :background :html-background))
  17027. "white")))
  17028. (if (eq fg 'default) (setq fg (org-latex-color :foreground))
  17029. (setq fg (org-latex-color-format fg)))
  17030. (if (eq bg 'default) (setq bg (org-latex-color :background))
  17031. (setq bg (org-latex-color-format
  17032. (if (string= bg "Transparent") "white" bg))))
  17033. (let ((latex-header (org-create-formula--latex-header)))
  17034. (with-temp-file texfile
  17035. (insert latex-header)
  17036. (insert "\n\\begin{document}\n"
  17037. "\\definecolor{fg}{rgb}{" fg "}\n"
  17038. "\\definecolor{bg}{rgb}{" bg "}\n"
  17039. "\n\\pagecolor{bg}\n"
  17040. "\n{\\color{fg}\n"
  17041. string
  17042. "\n}\n"
  17043. "\n\\end{document}\n")))
  17044. (org-latex-compile texfile t)
  17045. (if (not (file-exists-p pdffile))
  17046. (progn (message "Failed to create pdf file from %s" texfile) nil)
  17047. (condition-case nil
  17048. (if (featurep 'xemacs)
  17049. (call-process "convert" nil nil nil
  17050. "-density" "96"
  17051. "-trim"
  17052. "-antialias"
  17053. pdffile
  17054. "-quality" "100"
  17055. ;; "-sharpen" "0x1.0"
  17056. pngfile)
  17057. (call-process "convert" nil nil nil
  17058. "-density" dpi
  17059. "-trim"
  17060. "-antialias"
  17061. pdffile
  17062. "-quality" "100"
  17063. ;; "-sharpen" "0x1.0"
  17064. pngfile))
  17065. (error nil))
  17066. (if (not (file-exists-p pngfile))
  17067. (if org-format-latex-signal-error
  17068. (error "Failed to create png file from %s" texfile)
  17069. (message "Failed to create png file from %s" texfile)
  17070. nil)
  17071. ;; Use the requested file name and clean up
  17072. (copy-file pngfile tofile 'replace)
  17073. (loop for e in '(".pdf" ".tex" ".aux" ".log" ".png") do
  17074. (if (file-exists-p (concat texfilebase e))
  17075. (delete-file (concat texfilebase e))))
  17076. pngfile))))
  17077. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  17078. "Fill a LaTeX header template TPL.
  17079. In the template, the following place holders will be recognized:
  17080. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  17081. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  17082. [PACKAGES] \\usepackage statements for PKG
  17083. [NO-PACKAGES] do not include PKG
  17084. [EXTRA] the string EXTRA
  17085. [NO-EXTRA] do not include EXTRA
  17086. For backward compatibility, if both the positive and the negative place
  17087. holder is missing, the positive one (without the \"NO-\") will be
  17088. assumed to be present at the end of the template.
  17089. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  17090. EXTRA is a string.
  17091. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  17092. (let (rpl (end ""))
  17093. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  17094. (setq rpl (if (or (match-end 1) (not def-pkg))
  17095. "" (org-latex-packages-to-string def-pkg snippets-p t))
  17096. tpl (replace-match rpl t t tpl))
  17097. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  17098. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  17099. (setq rpl (if (or (match-end 1) (not pkg))
  17100. "" (org-latex-packages-to-string pkg snippets-p t))
  17101. tpl (replace-match rpl t t tpl))
  17102. (if pkg (setq end
  17103. (concat end "\n"
  17104. (org-latex-packages-to-string pkg snippets-p)))))
  17105. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  17106. (setq rpl (if (or (match-end 1) (not extra))
  17107. "" (concat extra "\n"))
  17108. tpl (replace-match rpl t t tpl))
  17109. (if (and extra (string-match "\\S-" extra))
  17110. (setq end (concat end "\n" extra))))
  17111. (if (string-match "\\S-" end)
  17112. (concat tpl "\n" end)
  17113. tpl)))
  17114. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  17115. "Turn an alist of packages into a string with the \\usepackage macros."
  17116. (setq pkg (mapconcat (lambda(p)
  17117. (cond
  17118. ((stringp p) p)
  17119. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  17120. (format "%% Package %s omitted" (cadr p)))
  17121. ((equal "" (car p))
  17122. (format "\\usepackage{%s}" (cadr p)))
  17123. (t
  17124. (format "\\usepackage[%s]{%s}"
  17125. (car p) (cadr p)))))
  17126. pkg
  17127. "\n"))
  17128. (if newline (concat pkg "\n") pkg))
  17129. (defun org-dvipng-color (attr)
  17130. "Return a RGB color specification for dvipng."
  17131. (apply 'format "rgb %s %s %s"
  17132. (mapcar 'org-normalize-color
  17133. (if (featurep 'xemacs)
  17134. (color-rgb-components
  17135. (face-property 'default
  17136. (cond ((eq attr :foreground) 'foreground)
  17137. ((eq attr :background) 'background))))
  17138. (color-values (face-attribute 'default attr nil))))))
  17139. (defun org-dvipng-color-format (color-name)
  17140. "Convert COLOR-NAME to a RGB color value for dvipng."
  17141. (apply 'format "rgb %s %s %s"
  17142. (mapcar 'org-normalize-color
  17143. (color-values color-name))))
  17144. (defun org-latex-color (attr)
  17145. "Return a RGB color for the LaTeX color package."
  17146. (apply 'format "%s,%s,%s"
  17147. (mapcar 'org-normalize-color
  17148. (if (featurep 'xemacs)
  17149. (color-rgb-components
  17150. (face-property 'default
  17151. (cond ((eq attr :foreground) 'foreground)
  17152. ((eq attr :background) 'background))))
  17153. (color-values (face-attribute 'default attr nil))))))
  17154. (defun org-latex-color-format (color-name)
  17155. "Convert COLOR-NAME to a RGB color value."
  17156. (apply 'format "%s,%s,%s"
  17157. (mapcar 'org-normalize-color
  17158. (color-values color-name))))
  17159. (defun org-normalize-color (value)
  17160. "Return string to be used as color value for an RGB component."
  17161. (format "%g" (/ value 65535.0)))
  17162. ;; Image display
  17163. (defvar org-inline-image-overlays nil)
  17164. (make-variable-buffer-local 'org-inline-image-overlays)
  17165. (defun org-toggle-inline-images (&optional include-linked)
  17166. "Toggle the display of inline images.
  17167. INCLUDE-LINKED is passed to `org-display-inline-images'."
  17168. (interactive "P")
  17169. (if org-inline-image-overlays
  17170. (progn
  17171. (org-remove-inline-images)
  17172. (message "Inline image display turned off"))
  17173. (org-display-inline-images include-linked)
  17174. (if (and (org-called-interactively-p)
  17175. org-inline-image-overlays)
  17176. (message "%d images displayed inline"
  17177. (length org-inline-image-overlays))
  17178. (message "No images to display inline"))))
  17179. (defun org-redisplay-inline-images ()
  17180. "Refresh the display of inline images."
  17181. (interactive)
  17182. (if (not org-inline-image-overlays)
  17183. (org-toggle-inline-images)
  17184. (org-toggle-inline-images)
  17185. (org-toggle-inline-images)))
  17186. (defun org-display-inline-images (&optional include-linked refresh beg end)
  17187. "Display inline images.
  17188. Normally only links without a description part are inlined, because this
  17189. is how it will work for export. When INCLUDE-LINKED is set, also links
  17190. with a description part will be inlined. This can be nice for a quick
  17191. look at those images, but it does not reflect what exported files will look
  17192. like.
  17193. When REFRESH is set, refresh existing images between BEG and END.
  17194. This will create new image displays only if necessary.
  17195. BEG and END default to the buffer boundaries."
  17196. (interactive "P")
  17197. (unless refresh
  17198. (org-remove-inline-images)
  17199. (if (fboundp 'clear-image-cache) (clear-image-cache)))
  17200. (save-excursion
  17201. (save-restriction
  17202. (widen)
  17203. (setq beg (or beg (point-min)) end (or end (point-max)))
  17204. (goto-char beg)
  17205. (let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([^]\n]+?"
  17206. (substring (org-image-file-name-regexp) 0 -2)
  17207. "\\)\\]" (if include-linked "" "\\]")))
  17208. (case-fold-search t)
  17209. old file ov img type attrwidth width)
  17210. (while (re-search-forward re end t)
  17211. (setq old (get-char-property-and-overlay (match-beginning 1)
  17212. 'org-image-overlay)
  17213. file (expand-file-name
  17214. (concat (or (match-string 3) "") (match-string 4))))
  17215. (when (image-type-available-p 'imagemagick)
  17216. (setq attrwidth (if (or (listp org-image-actual-width)
  17217. (null org-image-actual-width))
  17218. (save-excursion
  17219. (save-match-data
  17220. (when (re-search-backward
  17221. "#\\+attr.*:width[ \t]+\\([^ ]+\\)"
  17222. (save-excursion
  17223. (re-search-backward "^[ \t]*$\\|\\`" nil t)) t)
  17224. (string-to-number (match-string 1))))))
  17225. width (cond ((eq org-image-actual-width t) nil)
  17226. ((null org-image-actual-width) attrwidth)
  17227. ((numberp org-image-actual-width)
  17228. org-image-actual-width)
  17229. ((listp org-image-actual-width)
  17230. (or attrwidth (car org-image-actual-width))))
  17231. type (if width 'imagemagick)))
  17232. (when (file-exists-p file)
  17233. (if (and (car-safe old) refresh)
  17234. (image-refresh (overlay-get (cdr old) 'display))
  17235. (setq img (save-match-data (create-image file type nil :width width)))
  17236. (when img
  17237. (setq ov (make-overlay (match-beginning 0) (match-end 0)))
  17238. (overlay-put ov 'display img)
  17239. (overlay-put ov 'face 'default)
  17240. (overlay-put ov 'org-image-overlay t)
  17241. (overlay-put ov 'modification-hooks
  17242. (list 'org-display-inline-remove-overlay))
  17243. (push ov org-inline-image-overlays)))))))))
  17244. (define-obsolete-function-alias
  17245. 'org-display-inline-modification-hook 'org-display-inline-remove-overlay "24.3")
  17246. (defun org-display-inline-remove-overlay (ov after beg end &optional len)
  17247. "Remove inline-display overlay if a corresponding region is modified."
  17248. (let ((inhibit-modification-hooks t))
  17249. (when (and ov after)
  17250. (delete ov org-inline-image-overlays)
  17251. (delete-overlay ov))))
  17252. (defun org-remove-inline-images ()
  17253. "Remove inline display of images."
  17254. (interactive)
  17255. (mapc 'delete-overlay org-inline-image-overlays)
  17256. (setq org-inline-image-overlays nil))
  17257. ;;;; Key bindings
  17258. ;; Outline functions from `outline-mode-prefix-map'
  17259. ;; that can be remapped in Org:
  17260. (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
  17261. (define-key org-mode-map [remap show-subtree] 'org-show-subtree)
  17262. (define-key org-mode-map [remap outline-forward-same-level]
  17263. 'org-forward-heading-same-level)
  17264. (define-key org-mode-map [remap outline-backward-same-level]
  17265. 'org-backward-heading-same-level)
  17266. (define-key org-mode-map [remap show-branches]
  17267. 'org-kill-note-or-show-branches)
  17268. (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
  17269. (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
  17270. (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
  17271. ;; Outline functions from `outline-mode-prefix-map' that can not
  17272. ;; be remapped in Org:
  17273. ;;
  17274. ;; - the column "key binding" shows whether the Outline function is still
  17275. ;; available in Org mode on the same key that it has been bound to in
  17276. ;; Outline mode:
  17277. ;; - "overridden": key used for a different functionality in Org mode
  17278. ;; - else: key still bound to the same Outline function in Org mode
  17279. ;;
  17280. ;; | Outline function | key binding | Org replacement |
  17281. ;; |------------------------------------+-------------+-----------------------|
  17282. ;; | `outline-next-visible-heading' | `C-c C-n' | still same function |
  17283. ;; | `outline-previous-visible-heading' | `C-c C-p' | still same function |
  17284. ;; | `outline-up-heading' | `C-c C-u' | still same function |
  17285. ;; | `outline-move-subtree-up' | overridden | better: org-shiftup |
  17286. ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
  17287. ;; | `show-entry' | overridden | no replacement |
  17288. ;; | `show-children' | `C-c C-i' | visibility cycling |
  17289. ;; | `show-branches' | `C-c C-k' | still same function |
  17290. ;; | `show-subtree' | overridden | visibility cycling |
  17291. ;; | `show-all' | overridden | no replacement |
  17292. ;; | `hide-subtree' | overridden | visibility cycling |
  17293. ;; | `hide-body' | overridden | no replacement |
  17294. ;; | `hide-entry' | overridden | visibility cycling |
  17295. ;; | `hide-leaves' | overridden | no replacement |
  17296. ;; | `hide-sublevels' | overridden | no replacement |
  17297. ;; | `hide-other' | overridden | no replacement |
  17298. ;; Make `C-c C-x' a prefix key
  17299. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  17300. ;; TAB key with modifiers
  17301. (org-defkey org-mode-map "\C-i" 'org-cycle)
  17302. (org-defkey org-mode-map [(tab)] 'org-cycle)
  17303. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  17304. (org-defkey org-mode-map "\M-\t" 'pcomplete)
  17305. ;; The following line is necessary under Suse GNU/Linux
  17306. (unless (featurep 'xemacs)
  17307. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  17308. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  17309. (define-key org-mode-map [backtab] 'org-shifttab)
  17310. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  17311. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  17312. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  17313. ;; Cursor keys with modifiers
  17314. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  17315. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  17316. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  17317. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  17318. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  17319. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  17320. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  17321. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  17322. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  17323. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  17324. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  17325. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  17326. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  17327. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  17328. (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
  17329. (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
  17330. ;; Babel keys
  17331. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  17332. (mapc (lambda (pair)
  17333. (define-key org-babel-map (car pair) (cdr pair)))
  17334. org-babel-key-bindings)
  17335. ;;; Extra keys for tty access.
  17336. ;; We only set them when really needed because otherwise the
  17337. ;; menus don't show the simple keys
  17338. (when (or org-use-extra-keys
  17339. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  17340. (not window-system))
  17341. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  17342. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  17343. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  17344. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  17345. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  17346. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  17347. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  17348. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  17349. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  17350. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  17351. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  17352. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  17353. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  17354. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  17355. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  17356. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  17357. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  17358. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  17359. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  17360. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  17361. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  17362. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  17363. (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
  17364. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  17365. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  17366. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  17367. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  17368. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  17369. ;; All the other keys
  17370. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  17371. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  17372. (if (boundp 'narrow-map)
  17373. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  17374. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  17375. (if (boundp 'narrow-map)
  17376. (org-defkey narrow-map "b" 'org-narrow-to-block)
  17377. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  17378. (if (boundp 'narrow-map)
  17379. (org-defkey narrow-map "e" 'org-narrow-to-element)
  17380. (org-defkey org-mode-map "\C-xne" 'org-narrow-to-element))
  17381. (org-defkey org-mode-map "\C-\M-t" 'org-transpose-element)
  17382. (org-defkey org-mode-map "\M-}" 'org-forward-element)
  17383. (org-defkey org-mode-map "\M-{" 'org-backward-element)
  17384. (org-defkey org-mode-map "\C-c\C-^" 'org-up-element)
  17385. (org-defkey org-mode-map "\C-c\C-_" 'org-down-element)
  17386. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-heading-same-level)
  17387. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-heading-same-level)
  17388. (org-defkey org-mode-map "\C-c\M-f" 'org-next-block)
  17389. (org-defkey org-mode-map "\C-c\M-b" 'org-previous-block)
  17390. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  17391. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  17392. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  17393. (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
  17394. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  17395. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  17396. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  17397. (org-defkey org-mode-map "\C-c\C-xq" 'org-toggle-tags-groups)
  17398. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  17399. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  17400. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  17401. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  17402. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  17403. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  17404. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  17405. (org-defkey org-mode-map "\C-c\M-w" 'org-copy)
  17406. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  17407. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  17408. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  17409. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  17410. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  17411. (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
  17412. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  17413. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  17414. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  17415. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  17416. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  17417. (org-defkey org-mode-map "\C-c\C-\M-l" 'org-insert-all-links)
  17418. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  17419. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  17420. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  17421. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  17422. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  17423. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  17424. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  17425. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  17426. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  17427. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  17428. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  17429. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  17430. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  17431. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  17432. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  17433. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  17434. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  17435. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  17436. (org-defkey org-mode-map "\C-c^" 'org-sort)
  17437. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  17438. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  17439. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  17440. (org-defkey org-mode-map "\C-o" 'org-ctrl-o)
  17441. (org-defkey org-mode-map "\C-m" 'org-return)
  17442. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  17443. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  17444. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  17445. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  17446. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  17447. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  17448. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  17449. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  17450. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  17451. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  17452. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  17453. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  17454. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  17455. (org-defkey org-mode-map "\C-c\C-e" 'org-export-dispatch)
  17456. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  17457. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  17458. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  17459. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  17460. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  17461. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  17462. (org-defkey org-mode-map "\M-h" 'org-mark-element)
  17463. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  17464. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  17465. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  17466. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  17467. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  17468. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  17469. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  17470. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-in-last)
  17471. (org-defkey org-mode-map "\C-c\C-x\C-z" 'org-resolve-clocks)
  17472. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  17473. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  17474. (org-defkey org-mode-map "\C-c\C-x\C-q" 'org-clock-cancel)
  17475. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  17476. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  17477. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  17478. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  17479. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  17480. (org-defkey org-mode-map "\C-c\C-x\C-\M-v" 'org-redisplay-inline-images)
  17481. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  17482. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  17483. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  17484. (org-defkey org-mode-map "\C-c\C-xP" 'org-set-property-and-value)
  17485. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  17486. (org-defkey org-mode-map "\C-c\C-xE" 'org-inc-effort)
  17487. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  17488. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  17489. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  17490. (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
  17491. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  17492. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  17493. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  17494. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  17495. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  17496. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  17497. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  17498. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  17499. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  17500. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  17501. (when (featurep 'xemacs)
  17502. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  17503. (defconst org-speed-commands-default
  17504. '(
  17505. ("Outline Navigation")
  17506. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  17507. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  17508. ("f" . (org-speed-move-safe 'org-forward-heading-same-level))
  17509. ("b" . (org-speed-move-safe 'org-backward-heading-same-level))
  17510. ("F" . org-next-block)
  17511. ("B" . org-previous-block)
  17512. ("u" . (org-speed-move-safe 'outline-up-heading))
  17513. ("j" . org-goto)
  17514. ("g" . (org-refile t))
  17515. ("Outline Visibility")
  17516. ("c" . org-cycle)
  17517. ("C" . org-shifttab)
  17518. (" " . org-display-outline-path)
  17519. ("s" . org-narrow-to-subtree)
  17520. ("=" . org-columns)
  17521. ("Outline Structure Editing")
  17522. ("U" . org-shiftmetaup)
  17523. ("D" . org-shiftmetadown)
  17524. ("r" . org-metaright)
  17525. ("l" . org-metaleft)
  17526. ("R" . org-shiftmetaright)
  17527. ("L" . org-shiftmetaleft)
  17528. ("i" . (progn (forward-char 1) (call-interactively
  17529. 'org-insert-heading-respect-content)))
  17530. ("^" . org-sort)
  17531. ("w" . org-refile)
  17532. ("a" . org-archive-subtree-default-with-confirmation)
  17533. ("@" . org-mark-subtree)
  17534. ("#" . org-toggle-comment)
  17535. ("Clock Commands")
  17536. ("I" . org-clock-in)
  17537. ("O" . org-clock-out)
  17538. ("Meta Data Editing")
  17539. ("t" . org-todo)
  17540. ("," . (org-priority))
  17541. ("0" . (org-priority ?\ ))
  17542. ("1" . (org-priority ?A))
  17543. ("2" . (org-priority ?B))
  17544. ("3" . (org-priority ?C))
  17545. (":" . org-set-tags-command)
  17546. ("e" . org-set-effort)
  17547. ("E" . org-inc-effort)
  17548. ("W" . (lambda(m) (interactive "sMinutes before warning: ")
  17549. (org-entry-put (point) "APPT_WARNTIME" m)))
  17550. ("Agenda Views etc")
  17551. ("v" . org-agenda)
  17552. ("/" . org-sparse-tree)
  17553. ("Misc")
  17554. ("o" . org-open-at-point)
  17555. ("?" . org-speed-command-help)
  17556. ("<" . (org-agenda-set-restriction-lock 'subtree))
  17557. (">" . (org-agenda-remove-restriction-lock))
  17558. )
  17559. "The default speed commands.")
  17560. (defun org-print-speed-command (e)
  17561. (if (> (length (car e)) 1)
  17562. (progn
  17563. (princ "\n")
  17564. (princ (car e))
  17565. (princ "\n")
  17566. (princ (make-string (length (car e)) ?-))
  17567. (princ "\n"))
  17568. (princ (car e))
  17569. (princ " ")
  17570. (if (symbolp (cdr e))
  17571. (princ (symbol-name (cdr e)))
  17572. (prin1 (cdr e)))
  17573. (princ "\n")))
  17574. (defun org-speed-command-help ()
  17575. "Show the available speed commands."
  17576. (interactive)
  17577. (if (not org-use-speed-commands)
  17578. (error "Speed commands are not activated, customize `org-use-speed-commands'")
  17579. (with-output-to-temp-buffer "*Help*"
  17580. (princ "User-defined Speed commands\n===========================\n")
  17581. (mapc 'org-print-speed-command org-speed-commands-user)
  17582. (princ "\n")
  17583. (princ "Built-in Speed commands\n=======================\n")
  17584. (mapc 'org-print-speed-command org-speed-commands-default))
  17585. (with-current-buffer "*Help*"
  17586. (setq truncate-lines t))))
  17587. (defun org-speed-move-safe (cmd)
  17588. "Execute CMD, but make sure that the cursor always ends up in a headline.
  17589. If not, return to the original position and throw an error."
  17590. (interactive)
  17591. (let ((pos (point)))
  17592. (call-interactively cmd)
  17593. (unless (and (bolp) (org-at-heading-p))
  17594. (goto-char pos)
  17595. (error "Boundary reached while executing %s" cmd))))
  17596. (defvar org-self-insert-command-undo-counter 0)
  17597. (defvar org-table-auto-blank-field) ; defined in org-table.el
  17598. (defvar org-speed-command nil)
  17599. (define-obsolete-function-alias
  17600. 'org-speed-command-default-hook 'org-speed-command-activate "24.3")
  17601. (defun org-speed-command-activate (keys)
  17602. "Hook for activating single-letter speed commands.
  17603. `org-speed-commands-default' specifies a minimal command set.
  17604. Use `org-speed-commands-user' for further customization."
  17605. (when (or (and (bolp) (looking-at org-outline-regexp))
  17606. (and (functionp org-use-speed-commands)
  17607. (funcall org-use-speed-commands)))
  17608. (cdr (assoc keys (append org-speed-commands-user
  17609. org-speed-commands-default)))))
  17610. (define-obsolete-function-alias
  17611. 'org-babel-speed-command-hook 'org-babel-speed-command-activate "24.3")
  17612. (defun org-babel-speed-command-activate (keys)
  17613. "Hook for activating single-letter code block commands."
  17614. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  17615. (cdr (assoc keys org-babel-key-bindings))))
  17616. (defcustom org-speed-command-hook
  17617. '(org-speed-command-default-hook org-babel-speed-command-hook)
  17618. "Hook for activating speed commands at strategic locations.
  17619. Hook functions are called in sequence until a valid handler is
  17620. found.
  17621. Each hook takes a single argument, a user-pressed command key
  17622. which is also a `self-insert-command' from the global map.
  17623. Within the hook, examine the cursor position and the command key
  17624. and return nil or a valid handler as appropriate. Handler could
  17625. be one of an interactive command, a function, or a form.
  17626. Set `org-use-speed-commands' to non-nil value to enable this
  17627. hook. The default setting is `org-speed-command-activate'."
  17628. :group 'org-structure
  17629. :version "24.1"
  17630. :type 'hook)
  17631. (defun org-self-insert-command (N)
  17632. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  17633. If the cursor is in a table looking at whitespace, the whitespace is
  17634. overwritten, and the table is not marked as requiring realignment."
  17635. (interactive "p")
  17636. (org-check-before-invisible-edit 'insert)
  17637. (cond
  17638. ((and org-use-speed-commands
  17639. (setq org-speed-command
  17640. (run-hook-with-args-until-success
  17641. 'org-speed-command-hook (this-command-keys))))
  17642. (cond
  17643. ((commandp org-speed-command)
  17644. (setq this-command org-speed-command)
  17645. (call-interactively org-speed-command))
  17646. ((functionp org-speed-command)
  17647. (funcall org-speed-command))
  17648. ((and org-speed-command (listp org-speed-command))
  17649. (eval org-speed-command))
  17650. (t (let (org-use-speed-commands)
  17651. (call-interactively 'org-self-insert-command)))))
  17652. ((and
  17653. (org-table-p)
  17654. (progn
  17655. ;; check if we blank the field, and if that triggers align
  17656. (and (featurep 'org-table) org-table-auto-blank-field
  17657. (member last-command
  17658. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  17659. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  17660. ;; got extra space, this field does not determine column width
  17661. (let (org-table-may-need-update) (org-table-blank-field))
  17662. ;; no extra space, this field may determine column width
  17663. (org-table-blank-field)))
  17664. t)
  17665. (eq N 1)
  17666. (looking-at "[^|\n]* |"))
  17667. (let (org-table-may-need-update)
  17668. (goto-char (1- (match-end 0)))
  17669. (backward-delete-char 1)
  17670. (goto-char (match-beginning 0))
  17671. (self-insert-command N)))
  17672. (t
  17673. (setq org-table-may-need-update t)
  17674. (self-insert-command N)
  17675. (org-fix-tags-on-the-fly)
  17676. (if org-self-insert-cluster-for-undo
  17677. (if (not (eq last-command 'org-self-insert-command))
  17678. (setq org-self-insert-command-undo-counter 1)
  17679. (if (>= org-self-insert-command-undo-counter 20)
  17680. (setq org-self-insert-command-undo-counter 1)
  17681. (and (> org-self-insert-command-undo-counter 0)
  17682. buffer-undo-list (listp buffer-undo-list)
  17683. (not (cadr buffer-undo-list)) ; remove nil entry
  17684. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  17685. (setq org-self-insert-command-undo-counter
  17686. (1+ org-self-insert-command-undo-counter))))))))
  17687. (defun org-check-before-invisible-edit (kind)
  17688. "Check is editing if kind KIND would be dangerous with invisible text around.
  17689. The detailed reaction depends on the user option `org-catch-invisible-edits'."
  17690. ;; First, try to get out of here as quickly as possible, to reduce overhead
  17691. (if (and org-catch-invisible-edits
  17692. (or (not (boundp 'visible-mode)) (not visible-mode))
  17693. (or (get-char-property (point) 'invisible)
  17694. (get-char-property (max (point-min) (1- (point))) 'invisible)))
  17695. ;; OK, we need to take a closer look
  17696. (let* ((invisible-at-point (get-char-property (point) 'invisible))
  17697. (invisible-before-point (if (bobp) nil (get-char-property
  17698. (1- (point)) 'invisible)))
  17699. (border-and-ok-direction
  17700. (or
  17701. ;; Check if we are acting predictably before invisible text
  17702. (and invisible-at-point (not invisible-before-point)
  17703. (memq kind '(insert delete-backward)))
  17704. ;; Check if we are acting predictably after invisible text
  17705. ;; This works not well, and I have turned it off. It seems
  17706. ;; better to always show and stop after invisible text.
  17707. ;; (and (not invisible-at-point) invisible-before-point
  17708. ;; (memq kind '(insert delete)))
  17709. )))
  17710. (when (or (memq invisible-at-point '(outline org-hide-block t))
  17711. (memq invisible-before-point '(outline org-hide-block t)))
  17712. (if (eq org-catch-invisible-edits 'error)
  17713. (error "Editing in invisible areas is prohibited - make visible first"))
  17714. (if (and org-custom-properties-overlays
  17715. (y-or-n-p "Display invisible properties in this buffer? "))
  17716. (org-toggle-custom-properties-visibility)
  17717. ;; Make the area visible
  17718. (save-excursion
  17719. (if invisible-before-point
  17720. (goto-char (previous-single-char-property-change
  17721. (point) 'invisible)))
  17722. (org-cycle))
  17723. (cond
  17724. ((eq org-catch-invisible-edits 'show)
  17725. ;; That's it, we do the edit after showing
  17726. (message
  17727. "Unfolding invisible region around point before editing")
  17728. (sit-for 1))
  17729. ((and (eq org-catch-invisible-edits 'smart)
  17730. border-and-ok-direction)
  17731. (message "Unfolding invisible region around point before editing"))
  17732. (t
  17733. ;; Don't do the edit, make the user repeat it in full visibility
  17734. (error "Edit in invisible region aborted, repeat to confirm with text visible"))))))))
  17735. (defun org-fix-tags-on-the-fly ()
  17736. (when (and (equal (char-after (point-at-bol)) ?*)
  17737. (org-at-heading-p))
  17738. (org-align-tags-here org-tags-column)))
  17739. (defun org-delete-backward-char (N)
  17740. "Like `delete-backward-char', insert whitespace at field end in tables.
  17741. When deleting backwards, in tables this function will insert whitespace in
  17742. front of the next \"|\" separator, to keep the table aligned. The table will
  17743. still be marked for re-alignment if the field did fill the entire column,
  17744. because, in this case the deletion might narrow the column."
  17745. (interactive "p")
  17746. (save-match-data
  17747. (org-check-before-invisible-edit 'delete-backward)
  17748. (if (and (org-table-p)
  17749. (eq N 1)
  17750. (string-match "|" (buffer-substring (point-at-bol) (point)))
  17751. (looking-at ".*?|"))
  17752. (let ((pos (point))
  17753. (noalign (looking-at "[^|\n\r]* |"))
  17754. (c org-table-may-need-update))
  17755. (backward-delete-char N)
  17756. (if (not overwrite-mode)
  17757. (progn
  17758. (skip-chars-forward "^|")
  17759. (insert " ")
  17760. (goto-char (1- pos))))
  17761. ;; noalign: if there were two spaces at the end, this field
  17762. ;; does not determine the width of the column.
  17763. (if noalign (setq org-table-may-need-update c)))
  17764. (backward-delete-char N)
  17765. (org-fix-tags-on-the-fly))))
  17766. (defun org-delete-char (N)
  17767. "Like `delete-char', but insert whitespace at field end in tables.
  17768. When deleting characters, in tables this function will insert whitespace in
  17769. front of the next \"|\" separator, to keep the table aligned. The table will
  17770. still be marked for re-alignment if the field did fill the entire column,
  17771. because, in this case the deletion might narrow the column."
  17772. (interactive "p")
  17773. (save-match-data
  17774. (org-check-before-invisible-edit 'delete)
  17775. (if (and (org-table-p)
  17776. (not (bolp))
  17777. (not (= (char-after) ?|))
  17778. (eq N 1))
  17779. (if (looking-at ".*?|")
  17780. (let ((pos (point))
  17781. (noalign (looking-at "[^|\n\r]* |"))
  17782. (c org-table-may-need-update))
  17783. (replace-match
  17784. (concat (substring (match-string 0) 1 -1) " |") nil t)
  17785. (goto-char pos)
  17786. ;; noalign: if there were two spaces at the end, this field
  17787. ;; does not determine the width of the column.
  17788. (if noalign (setq org-table-may-need-update c)))
  17789. (delete-char N))
  17790. (delete-char N)
  17791. (org-fix-tags-on-the-fly))))
  17792. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  17793. (put 'org-self-insert-command 'delete-selection t)
  17794. (put 'orgtbl-self-insert-command 'delete-selection t)
  17795. (put 'org-delete-char 'delete-selection 'supersede)
  17796. (put 'org-delete-backward-char 'delete-selection 'supersede)
  17797. (put 'org-yank 'delete-selection 'yank)
  17798. ;; Make `flyspell-mode' delay after some commands
  17799. (put 'org-self-insert-command 'flyspell-delayed t)
  17800. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  17801. (put 'org-delete-char 'flyspell-delayed t)
  17802. (put 'org-delete-backward-char 'flyspell-delayed t)
  17803. ;; Make pabbrev-mode expand after org-mode commands
  17804. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  17805. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  17806. ;; How to do this: Measure non-white length of current string
  17807. ;; If equal to column width, we should realign.
  17808. (defun org-remap (map &rest commands)
  17809. "In MAP, remap the functions given in COMMANDS.
  17810. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  17811. (let (new old)
  17812. (while commands
  17813. (setq old (pop commands) new (pop commands))
  17814. (if (fboundp 'command-remapping)
  17815. (org-defkey map (vector 'remap old) new)
  17816. (substitute-key-definition old new map global-map)))))
  17817. (defun org-transpose-words ()
  17818. "Transpose words for Org.
  17819. This uses the `org-mode-transpose-word-syntax-table' syntax
  17820. table, which interprets characters in `org-emphasis-alist' as
  17821. word constituants."
  17822. (interactive)
  17823. (with-syntax-table org-mode-transpose-word-syntax-table
  17824. (call-interactively 'transpose-words)))
  17825. (org-remap org-mode-map 'transpose-words 'org-transpose-words)
  17826. (when (eq org-enable-table-editor 'optimized)
  17827. ;; If the user wants maximum table support, we need to hijack
  17828. ;; some standard editing functions
  17829. (org-remap org-mode-map
  17830. 'self-insert-command 'org-self-insert-command
  17831. 'delete-char 'org-delete-char
  17832. 'delete-backward-char 'org-delete-backward-char)
  17833. (org-defkey org-mode-map "|" 'org-force-self-insert))
  17834. (defvar org-ctrl-c-ctrl-c-hook nil
  17835. "Hook for functions attaching themselves to `C-c C-c'.
  17836. This can be used to add additional functionality to the C-c C-c
  17837. key which executes context-dependent commands. This hook is run
  17838. before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
  17839. run after the last test.
  17840. Each function will be called with no arguments. The function
  17841. must check if the context is appropriate for it to act. If yes,
  17842. it should do its thing and then return a non-nil value. If the
  17843. context is wrong, just do nothing and return nil.")
  17844. (defvar org-ctrl-c-ctrl-c-final-hook nil
  17845. "Hook for functions attaching themselves to `C-c C-c'.
  17846. This can be used to add additional functionality to the C-c C-c
  17847. key which executes context-dependent commands. This hook is run
  17848. after any other test, while `org-ctrl-c-ctrl-c-hook' is run
  17849. before the first test.
  17850. Each function will be called with no arguments. The function
  17851. must check if the context is appropriate for it to act. If yes,
  17852. it should do its thing and then return a non-nil value. If the
  17853. context is wrong, just do nothing and return nil.")
  17854. (defvar org-tab-first-hook nil
  17855. "Hook for functions to attach themselves to TAB.
  17856. See `org-ctrl-c-ctrl-c-hook' for more information.
  17857. This hook runs as the first action when TAB is pressed, even before
  17858. `org-cycle' messes around with the `outline-regexp' to cater for
  17859. inline tasks and plain list item folding.
  17860. If any function in this hook returns t, any other actions that
  17861. would have been caused by TAB (such as table field motion or visibility
  17862. cycling) will not occur.")
  17863. (defvar org-tab-after-check-for-table-hook nil
  17864. "Hook for functions to attach themselves to TAB.
  17865. See `org-ctrl-c-ctrl-c-hook' for more information.
  17866. This hook runs after it has been established that the cursor is not in a
  17867. table, but before checking if the cursor is in a headline or if global cycling
  17868. should be done.
  17869. If any function in this hook returns t, not other actions like visibility
  17870. cycling will be done.")
  17871. (defvar org-tab-after-check-for-cycling-hook nil
  17872. "Hook for functions to attach themselves to TAB.
  17873. See `org-ctrl-c-ctrl-c-hook' for more information.
  17874. This hook runs after it has been established that not table field motion and
  17875. not visibility should be done because of current context. This is probably
  17876. the place where a package like yasnippets can hook in.")
  17877. (defvar org-tab-before-tab-emulation-hook nil
  17878. "Hook for functions to attach themselves to TAB.
  17879. See `org-ctrl-c-ctrl-c-hook' for more information.
  17880. This hook runs after every other options for TAB have been exhausted, but
  17881. before indentation and \t insertion takes place.")
  17882. (defvar org-metaleft-hook nil
  17883. "Hook for functions attaching themselves to `M-left'.
  17884. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17885. (defvar org-metaright-hook nil
  17886. "Hook for functions attaching themselves to `M-right'.
  17887. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17888. (defvar org-metaup-hook nil
  17889. "Hook for functions attaching themselves to `M-up'.
  17890. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17891. (defvar org-metadown-hook nil
  17892. "Hook for functions attaching themselves to `M-down'.
  17893. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17894. (defvar org-shiftmetaleft-hook nil
  17895. "Hook for functions attaching themselves to `M-S-left'.
  17896. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17897. (defvar org-shiftmetaright-hook nil
  17898. "Hook for functions attaching themselves to `M-S-right'.
  17899. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17900. (defvar org-shiftmetaup-hook nil
  17901. "Hook for functions attaching themselves to `M-S-up'.
  17902. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17903. (defvar org-shiftmetadown-hook nil
  17904. "Hook for functions attaching themselves to `M-S-down'.
  17905. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17906. (defvar org-metareturn-hook nil
  17907. "Hook for functions attaching themselves to `M-RET'.
  17908. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17909. (defvar org-shiftup-hook nil
  17910. "Hook for functions attaching themselves to `S-up'.
  17911. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17912. (defvar org-shiftup-final-hook nil
  17913. "Hook for functions attaching themselves to `S-up'.
  17914. This one runs after all other options except shift-select have been excluded.
  17915. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17916. (defvar org-shiftdown-hook nil
  17917. "Hook for functions attaching themselves to `S-down'.
  17918. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17919. (defvar org-shiftdown-final-hook nil
  17920. "Hook for functions attaching themselves to `S-down'.
  17921. This one runs after all other options except shift-select have been excluded.
  17922. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17923. (defvar org-shiftleft-hook nil
  17924. "Hook for functions attaching themselves to `S-left'.
  17925. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17926. (defvar org-shiftleft-final-hook nil
  17927. "Hook for functions attaching themselves to `S-left'.
  17928. This one runs after all other options except shift-select have been excluded.
  17929. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17930. (defvar org-shiftright-hook nil
  17931. "Hook for functions attaching themselves to `S-right'.
  17932. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17933. (defvar org-shiftright-final-hook nil
  17934. "Hook for functions attaching themselves to `S-right'.
  17935. This one runs after all other options except shift-select have been excluded.
  17936. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17937. (defun org-modifier-cursor-error ()
  17938. "Throw an error, a modified cursor command was applied in wrong context."
  17939. (error "This command is active in special context like tables, headlines or items"))
  17940. (defun org-shiftselect-error ()
  17941. "Throw an error because Shift-Cursor command was applied in wrong context."
  17942. (if (and (boundp 'shift-select-mode) shift-select-mode)
  17943. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  17944. (error "This command works only in special context like headlines or timestamps")))
  17945. (defun org-call-for-shift-select (cmd)
  17946. (let ((this-command-keys-shift-translated t))
  17947. (call-interactively cmd)))
  17948. (defun org-shifttab (&optional arg)
  17949. "Global visibility cycling or move to previous table field.
  17950. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  17951. on context.
  17952. See the individual commands for more information."
  17953. (interactive "P")
  17954. (cond
  17955. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  17956. ((integerp arg)
  17957. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  17958. (message "Content view to level: %d" arg)
  17959. (org-content (prefix-numeric-value arg2))
  17960. (setq org-cycle-global-status 'overview)))
  17961. (t (call-interactively 'org-global-cycle))))
  17962. (defun org-shiftmetaleft ()
  17963. "Promote subtree or delete table column.
  17964. Calls `org-promote-subtree', `org-outdent-item-tree', or
  17965. `org-table-delete-column', depending on context. See the
  17966. individual commands for more information."
  17967. (interactive)
  17968. (cond
  17969. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  17970. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  17971. ((org-at-heading-p) (call-interactively 'org-promote-subtree))
  17972. ((if (not (org-region-active-p)) (org-at-item-p)
  17973. (save-excursion (goto-char (region-beginning))
  17974. (org-at-item-p)))
  17975. (call-interactively 'org-outdent-item-tree))
  17976. (t (org-modifier-cursor-error))))
  17977. (defun org-shiftmetaright ()
  17978. "Demote subtree or insert table column.
  17979. Calls `org-demote-subtree', `org-indent-item-tree', or
  17980. `org-table-insert-column', depending on context. See the
  17981. individual commands for more information."
  17982. (interactive)
  17983. (cond
  17984. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  17985. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  17986. ((org-at-heading-p) (call-interactively 'org-demote-subtree))
  17987. ((if (not (org-region-active-p)) (org-at-item-p)
  17988. (save-excursion (goto-char (region-beginning))
  17989. (org-at-item-p)))
  17990. (call-interactively 'org-indent-item-tree))
  17991. (t (org-modifier-cursor-error))))
  17992. (defun org-shiftmetaup (&optional arg)
  17993. "Move subtree up or kill table row.
  17994. Calls `org-move-subtree-up' or `org-table-kill-row' or
  17995. `org-move-item-up' or `org-timestamp-up', depending on context.
  17996. See the individual commands for more information."
  17997. (interactive "P")
  17998. (cond
  17999. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  18000. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  18001. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  18002. ((org-at-item-p) (call-interactively 'org-move-item-up))
  18003. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  18004. (call-interactively 'org-timestamp-up)))
  18005. (t (call-interactively 'org-drag-line-backward))))
  18006. (defun org-shiftmetadown (&optional arg)
  18007. "Move subtree down or insert table row.
  18008. Calls `org-move-subtree-down' or `org-table-insert-row' or
  18009. `org-move-item-down' or `org-timestamp-up', depending on context.
  18010. See the individual commands for more information."
  18011. (interactive "P")
  18012. (cond
  18013. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  18014. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  18015. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  18016. ((org-at-item-p) (call-interactively 'org-move-item-down))
  18017. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  18018. (call-interactively 'org-timestamp-down)))
  18019. (t (call-interactively 'org-drag-line-forward))))
  18020. (defsubst org-hidden-tree-error ()
  18021. (error
  18022. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  18023. (defun org-metaleft (&optional arg)
  18024. "Promote heading or move table column to left.
  18025. Calls `org-do-promote' or `org-table-move-column', depending on context.
  18026. With no specific context, calls the Emacs default `backward-word'.
  18027. See the individual commands for more information."
  18028. (interactive "P")
  18029. (cond
  18030. ((run-hook-with-args-until-success 'org-metaleft-hook))
  18031. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  18032. ((org-with-limited-levels
  18033. (or (org-at-heading-p)
  18034. (and (org-region-active-p)
  18035. (save-excursion
  18036. (goto-char (region-beginning))
  18037. (org-at-heading-p)))))
  18038. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18039. (call-interactively 'org-do-promote))
  18040. ;; At an inline task.
  18041. ((org-at-heading-p)
  18042. (call-interactively 'org-inlinetask-promote))
  18043. ((or (org-at-item-p)
  18044. (and (org-region-active-p)
  18045. (save-excursion
  18046. (goto-char (region-beginning))
  18047. (org-at-item-p))))
  18048. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18049. (call-interactively 'org-outdent-item))
  18050. (t (call-interactively 'backward-word))))
  18051. (defun org-metaright (&optional arg)
  18052. "Demote a subtree, a list item or move table column to right.
  18053. In front of a drawer or a block keyword, indent it correctly.
  18054. With no specific context, calls the Emacs default `forward-word'.
  18055. See the individual commands for more information."
  18056. (interactive "P")
  18057. (cond
  18058. ((run-hook-with-args-until-success 'org-metaright-hook))
  18059. ((org-at-table-p) (call-interactively 'org-table-move-column))
  18060. ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
  18061. ((org-at-block-p) (call-interactively 'org-indent-block))
  18062. ((org-with-limited-levels
  18063. (or (org-at-heading-p)
  18064. (and (org-region-active-p)
  18065. (save-excursion
  18066. (goto-char (region-beginning))
  18067. (org-at-heading-p)))))
  18068. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18069. (call-interactively 'org-do-demote))
  18070. ;; At an inline task.
  18071. ((org-at-heading-p)
  18072. (call-interactively 'org-inlinetask-demote))
  18073. ((or (org-at-item-p)
  18074. (and (org-region-active-p)
  18075. (save-excursion
  18076. (goto-char (region-beginning))
  18077. (org-at-item-p))))
  18078. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18079. (call-interactively 'org-indent-item))
  18080. (t (call-interactively 'forward-word))))
  18081. (defun org-check-for-hidden (what)
  18082. "Check if there are hidden headlines/items in the current visual line.
  18083. WHAT can be either `headlines' or `items'. If the current line is
  18084. an outline or item heading and it has a folded subtree below it,
  18085. this function returns t, nil otherwise."
  18086. (let ((re (cond
  18087. ((eq what 'headlines) org-outline-regexp-bol)
  18088. ((eq what 'items) (org-item-beginning-re))
  18089. (t (error "This should not happen"))))
  18090. beg end)
  18091. (save-excursion
  18092. (catch 'exit
  18093. (unless (org-region-active-p)
  18094. (setq beg (point-at-bol))
  18095. (beginning-of-line 2)
  18096. (while (and (not (eobp)) ;; this is like `next-line'
  18097. (get-char-property (1- (point)) 'invisible))
  18098. (beginning-of-line 2))
  18099. (setq end (point))
  18100. (goto-char beg)
  18101. (goto-char (point-at-eol))
  18102. (setq end (max end (point)))
  18103. (while (re-search-forward re end t)
  18104. (if (get-char-property (match-beginning 0) 'invisible)
  18105. (throw 'exit t))))
  18106. nil))))
  18107. (defun org-metaup (&optional arg)
  18108. "Move subtree up or move table row up.
  18109. Calls `org-move-subtree-up' or `org-table-move-row' or
  18110. `org-move-item-up', depending on context. See the individual commands
  18111. for more information."
  18112. (interactive "P")
  18113. (cond
  18114. ((run-hook-with-args-until-success 'org-metaup-hook))
  18115. ((org-region-active-p)
  18116. (let* ((a (min (region-beginning) (region-end)))
  18117. (b (1- (max (region-beginning) (region-end))))
  18118. (c (save-excursion (goto-char a)
  18119. (move-beginning-of-line 0)))
  18120. (d (save-excursion (goto-char a)
  18121. (move-end-of-line 0) (point))))
  18122. (transpose-regions a b c d)
  18123. (goto-char c)))
  18124. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  18125. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  18126. ((org-at-item-p) (call-interactively 'org-move-item-up))
  18127. (t (org-drag-element-backward))))
  18128. (defun org-metadown (&optional arg)
  18129. "Move subtree down or move table row down.
  18130. Calls `org-move-subtree-down' or `org-table-move-row' or
  18131. `org-move-item-down', depending on context. See the individual
  18132. commands for more information."
  18133. (interactive "P")
  18134. (cond
  18135. ((run-hook-with-args-until-success 'org-metadown-hook))
  18136. ((org-region-active-p)
  18137. (let* ((a (min (region-beginning) (region-end)))
  18138. (b (max (region-beginning) (region-end)))
  18139. (c (save-excursion (goto-char b)
  18140. (move-beginning-of-line 1)))
  18141. (d (save-excursion (goto-char b)
  18142. (move-end-of-line 1) (1+ (point)))))
  18143. (transpose-regions a b c d)
  18144. (goto-char d)))
  18145. ((org-at-table-p) (call-interactively 'org-table-move-row))
  18146. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  18147. ((org-at-item-p) (call-interactively 'org-move-item-down))
  18148. (t (org-drag-element-forward))))
  18149. (defun org-shiftup (&optional arg)
  18150. "Increase item in timestamp or increase priority of current headline.
  18151. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  18152. depending on context. See the individual commands for more information."
  18153. (interactive "P")
  18154. (cond
  18155. ((run-hook-with-args-until-success 'org-shiftup-hook))
  18156. ((and org-support-shift-select (org-region-active-p))
  18157. (org-call-for-shift-select 'previous-line))
  18158. ((org-at-timestamp-p t)
  18159. (call-interactively (if org-edit-timestamp-down-means-later
  18160. 'org-timestamp-down 'org-timestamp-up)))
  18161. ((and (not (eq org-support-shift-select 'always))
  18162. org-enable-priority-commands
  18163. (org-at-heading-p))
  18164. (call-interactively 'org-priority-up))
  18165. ((and (not org-support-shift-select) (org-at-item-p))
  18166. (call-interactively 'org-previous-item))
  18167. ((org-clocktable-try-shift 'up arg))
  18168. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  18169. (org-support-shift-select
  18170. (org-call-for-shift-select 'previous-line))
  18171. (t (org-shiftselect-error))))
  18172. (defun org-shiftdown (&optional arg)
  18173. "Decrease item in timestamp or decrease priority of current headline.
  18174. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  18175. depending on context. See the individual commands for more information."
  18176. (interactive "P")
  18177. (cond
  18178. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  18179. ((and org-support-shift-select (org-region-active-p))
  18180. (org-call-for-shift-select 'next-line))
  18181. ((org-at-timestamp-p t)
  18182. (call-interactively (if org-edit-timestamp-down-means-later
  18183. 'org-timestamp-up 'org-timestamp-down)))
  18184. ((and (not (eq org-support-shift-select 'always))
  18185. org-enable-priority-commands
  18186. (org-at-heading-p))
  18187. (call-interactively 'org-priority-down))
  18188. ((and (not org-support-shift-select) (org-at-item-p))
  18189. (call-interactively 'org-next-item))
  18190. ((org-clocktable-try-shift 'down arg))
  18191. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  18192. (org-support-shift-select
  18193. (org-call-for-shift-select 'next-line))
  18194. (t (org-shiftselect-error))))
  18195. (defun org-shiftright (&optional arg)
  18196. "Cycle the thing at point or in the current line, depending on context.
  18197. Depending on context, this does one of the following:
  18198. - switch a timestamp at point one day into the future
  18199. - on a headline, switch to the next TODO keyword.
  18200. - on an item, switch entire list to the next bullet type
  18201. - on a property line, switch to the next allowed value
  18202. - on a clocktable definition line, move time block into the future"
  18203. (interactive "P")
  18204. (cond
  18205. ((run-hook-with-args-until-success 'org-shiftright-hook))
  18206. ((and org-support-shift-select (org-region-active-p))
  18207. (org-call-for-shift-select 'forward-char))
  18208. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  18209. ((and (not (eq org-support-shift-select 'always))
  18210. (org-at-heading-p))
  18211. (let ((org-inhibit-logging
  18212. (not org-treat-S-cursor-todo-selection-as-state-change))
  18213. (org-inhibit-blocking
  18214. (not org-treat-S-cursor-todo-selection-as-state-change)))
  18215. (org-call-with-arg 'org-todo 'right)))
  18216. ((or (and org-support-shift-select
  18217. (not (eq org-support-shift-select 'always))
  18218. (org-at-item-bullet-p))
  18219. (and (not org-support-shift-select) (org-at-item-p)))
  18220. (org-call-with-arg 'org-cycle-list-bullet nil))
  18221. ((and (not (eq org-support-shift-select 'always))
  18222. (org-at-property-p))
  18223. (call-interactively 'org-property-next-allowed-value))
  18224. ((org-clocktable-try-shift 'right arg))
  18225. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  18226. (org-support-shift-select
  18227. (org-call-for-shift-select 'forward-char))
  18228. (t (org-shiftselect-error))))
  18229. (defun org-shiftleft (&optional arg)
  18230. "Cycle the thing at point or in the current line, depending on context.
  18231. Depending on context, this does one of the following:
  18232. - switch a timestamp at point one day into the past
  18233. - on a headline, switch to the previous TODO keyword.
  18234. - on an item, switch entire list to the previous bullet type
  18235. - on a property line, switch to the previous allowed value
  18236. - on a clocktable definition line, move time block into the past"
  18237. (interactive "P")
  18238. (cond
  18239. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  18240. ((and org-support-shift-select (org-region-active-p))
  18241. (org-call-for-shift-select 'backward-char))
  18242. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  18243. ((and (not (eq org-support-shift-select 'always))
  18244. (org-at-heading-p))
  18245. (let ((org-inhibit-logging
  18246. (not org-treat-S-cursor-todo-selection-as-state-change))
  18247. (org-inhibit-blocking
  18248. (not org-treat-S-cursor-todo-selection-as-state-change)))
  18249. (org-call-with-arg 'org-todo 'left)))
  18250. ((or (and org-support-shift-select
  18251. (not (eq org-support-shift-select 'always))
  18252. (org-at-item-bullet-p))
  18253. (and (not org-support-shift-select) (org-at-item-p)))
  18254. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  18255. ((and (not (eq org-support-shift-select 'always))
  18256. (org-at-property-p))
  18257. (call-interactively 'org-property-previous-allowed-value))
  18258. ((org-clocktable-try-shift 'left arg))
  18259. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  18260. (org-support-shift-select
  18261. (org-call-for-shift-select 'backward-char))
  18262. (t (org-shiftselect-error))))
  18263. (defun org-shiftcontrolright ()
  18264. "Switch to next TODO set."
  18265. (interactive)
  18266. (cond
  18267. ((and org-support-shift-select (org-region-active-p))
  18268. (org-call-for-shift-select 'forward-word))
  18269. ((and (not (eq org-support-shift-select 'always))
  18270. (org-at-heading-p))
  18271. (org-call-with-arg 'org-todo 'nextset))
  18272. (org-support-shift-select
  18273. (org-call-for-shift-select 'forward-word))
  18274. (t (org-shiftselect-error))))
  18275. (defun org-shiftcontrolleft ()
  18276. "Switch to previous TODO set."
  18277. (interactive)
  18278. (cond
  18279. ((and org-support-shift-select (org-region-active-p))
  18280. (org-call-for-shift-select 'backward-word))
  18281. ((and (not (eq org-support-shift-select 'always))
  18282. (org-at-heading-p))
  18283. (org-call-with-arg 'org-todo 'previousset))
  18284. (org-support-shift-select
  18285. (org-call-for-shift-select 'backward-word))
  18286. (t (org-shiftselect-error))))
  18287. (defun org-shiftcontrolup (&optional n)
  18288. "Change timestamps synchronously up in CLOCK log lines.
  18289. Optional argument N tells to change by that many units."
  18290. (interactive "P")
  18291. (cond ((and (not org-support-shift-select)
  18292. (org-at-clock-log-p)
  18293. (org-at-timestamp-p t))
  18294. (org-clock-timestamps-up n))
  18295. (t (org-shiftselect-error))))
  18296. (defun org-shiftcontroldown (&optional n)
  18297. "Change timestamps synchronously down in CLOCK log lines.
  18298. Optional argument N tells to change by that many units."
  18299. (interactive "P")
  18300. (cond ((and (not org-support-shift-select)
  18301. (org-at-clock-log-p)
  18302. (org-at-timestamp-p t))
  18303. (org-clock-timestamps-down n))
  18304. (t (org-shiftselect-error))))
  18305. (defun org-ctrl-c-ret ()
  18306. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  18307. (interactive)
  18308. (cond
  18309. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  18310. (t (call-interactively 'org-insert-heading))))
  18311. (defun org-find-visible ()
  18312. (let ((s (point)))
  18313. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  18314. (get-char-property s 'invisible)))
  18315. s))
  18316. (defun org-find-invisible ()
  18317. (let ((s (point)))
  18318. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  18319. (not (get-char-property s 'invisible))))
  18320. s))
  18321. (defun org-copy-visible (beg end)
  18322. "Copy the visible parts of the region."
  18323. (interactive "r")
  18324. (let (snippets s)
  18325. (save-excursion
  18326. (save-restriction
  18327. (narrow-to-region beg end)
  18328. (setq s (goto-char (point-min)))
  18329. (while (not (= (point) (point-max)))
  18330. (goto-char (org-find-invisible))
  18331. (push (buffer-substring s (point)) snippets)
  18332. (setq s (goto-char (org-find-visible))))))
  18333. (kill-new (apply 'concat (nreverse snippets)))))
  18334. (defun org-copy-special ()
  18335. "Copy region in table or copy current subtree.
  18336. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  18337. See the individual commands for more information."
  18338. (interactive)
  18339. (call-interactively
  18340. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  18341. (defun org-cut-special ()
  18342. "Cut region in table or cut current subtree.
  18343. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  18344. See the individual commands for more information."
  18345. (interactive)
  18346. (call-interactively
  18347. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  18348. (defun org-paste-special (arg)
  18349. "Paste rectangular region into table, or past subtree relative to level.
  18350. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  18351. See the individual commands for more information."
  18352. (interactive "P")
  18353. (if (org-at-table-p)
  18354. (org-table-paste-rectangle)
  18355. (org-paste-subtree arg)))
  18356. (defsubst org-in-fixed-width-region-p ()
  18357. "Is point in a fixed-width region?"
  18358. (save-match-data
  18359. (eq 'fixed-width (org-element-type (org-element-at-point)))))
  18360. (defun org-edit-special (&optional arg)
  18361. "Call a special editor for the element at point.
  18362. When at a table, call the formula editor with `org-table-edit-formulas'.
  18363. When in a source code block, call `org-edit-src-code'.
  18364. When in a fixed-width region, call `org-edit-fixed-width-region'.
  18365. When at an #+INCLUDE keyword, visit the included file.
  18366. On a link, call `ffap' to visit the link at point.
  18367. Otherwise, return a user error."
  18368. (interactive "P")
  18369. (let ((element (org-element-at-point)))
  18370. (assert (not buffer-read-only) nil
  18371. "Buffer is read-only: %s" (buffer-name))
  18372. (case (org-element-type element)
  18373. (src-block
  18374. (if (not arg) (org-edit-src-code)
  18375. (let* ((info (org-babel-get-src-block-info))
  18376. (lang (nth 0 info))
  18377. (params (nth 2 info))
  18378. (session (cdr (assq :session params))))
  18379. (if (not session) (org-edit-src-code)
  18380. ;; At a src-block with a session and function called with
  18381. ;; an ARG: switch to the buffer related to the inferior
  18382. ;; process.
  18383. (switch-to-buffer
  18384. (funcall (intern (concat "org-babel-prep-session:" lang))
  18385. session params))))))
  18386. (keyword
  18387. (if (member (org-element-property :key element) '("INCLUDE" "SETUPFILE"))
  18388. (find-file
  18389. (org-remove-double-quotes
  18390. (car (org-split-string (org-element-property :value element)))))
  18391. (user-error "No special environment to edit here")))
  18392. (table
  18393. (if (eq (org-element-property :type element) 'table.el)
  18394. (org-edit-src-code)
  18395. (call-interactively 'org-table-edit-formulas)))
  18396. ;; Only Org tables contain `table-row' type elements.
  18397. (table-row (call-interactively 'org-table-edit-formulas))
  18398. ((example-block export-block) (org-edit-src-code))
  18399. (fixed-width (org-edit-fixed-width-region))
  18400. (otherwise
  18401. ;; No notable element at point. Though, we may be at a link,
  18402. ;; which is an object. Thus, scan deeper.
  18403. (if (eq (org-element-type (org-element-context element)) 'link)
  18404. (call-interactively 'ffap)
  18405. (user-error "No special environment to edit here"))))))
  18406. (defvar org-table-coordinate-overlays) ; defined in org-table.el
  18407. (defun org-ctrl-c-ctrl-c (&optional arg)
  18408. "Set tags in headline, or update according to changed information at point.
  18409. This command does many different things, depending on context:
  18410. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  18411. this is what we do.
  18412. - If the cursor is on a statistics cookie, update it.
  18413. - If the cursor is in a headline, prompt for tags and insert them
  18414. into the current line, aligned to `org-tags-column'. When called
  18415. with prefix arg, realign all tags in the current buffer.
  18416. - If the cursor is in one of the special #+KEYWORD lines, this
  18417. triggers scanning the buffer for these lines and updating the
  18418. information.
  18419. - If the cursor is inside a table, realign the table. This command
  18420. works even if the automatic table editor has been turned off.
  18421. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  18422. the entire table.
  18423. - If the cursor is at a footnote reference or definition, jump to
  18424. the corresponding definition or references, respectively.
  18425. - If the cursor is a the beginning of a dynamic block, update it.
  18426. - If the current buffer is a capture buffer, close note and file it.
  18427. - If the cursor is on a <<<target>>>, update radio targets and
  18428. corresponding links in this buffer.
  18429. - If the cursor is on a numbered item in a plain list, renumber the
  18430. ordered list.
  18431. - If the cursor is on a checkbox, toggle it.
  18432. - If the cursor is on a code block, evaluate it. The variable
  18433. `org-confirm-babel-evaluate' can be used to control prompting
  18434. before code block evaluation, by default every code block
  18435. evaluation requires confirmation. Code block evaluation can be
  18436. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  18437. (interactive "P")
  18438. (cond
  18439. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  18440. org-occur-highlights
  18441. org-latex-fragment-image-overlays)
  18442. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  18443. (org-remove-occur-highlights)
  18444. (org-remove-latex-fragment-image-overlays)
  18445. (message "Temporary highlights/overlays removed from current buffer"))
  18446. ((and (local-variable-p 'org-finish-function (current-buffer))
  18447. (fboundp org-finish-function))
  18448. (funcall org-finish-function))
  18449. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  18450. (t
  18451. (let* ((context (org-element-context)) (type (org-element-type context)))
  18452. ;; Test if point is within a blank line.
  18453. (if (save-excursion (beginning-of-line) (looking-at "[ \t]*$"))
  18454. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  18455. (user-error "C-c C-c can do nothing useful at this location"))
  18456. ;; For convenience: at the first line of a paragraph on the
  18457. ;; same line as an item, apply function on that item instead.
  18458. (when (eq type 'paragraph)
  18459. (let ((parent (org-element-property :parent context)))
  18460. (when (and (eq (org-element-type parent) 'item)
  18461. (= (point-at-bol) (org-element-property :begin parent)))
  18462. (setq context parent type 'item))))
  18463. ;; Act according to type of element or object at point.
  18464. (case type
  18465. (clock (org-clock-update-time-maybe))
  18466. (dynamic-block
  18467. (save-excursion
  18468. (goto-char (org-element-property :post-affiliated context))
  18469. (org-update-dblock)))
  18470. (footnote-definition
  18471. (goto-char (org-element-property :post-affiliated context))
  18472. (call-interactively 'org-footnote-action))
  18473. (footnote-reference (call-interactively 'org-footnote-action))
  18474. ((headline inlinetask)
  18475. (save-excursion (goto-char (org-element-property :begin context))
  18476. (call-interactively 'org-set-tags)))
  18477. (item
  18478. ;; At an item: a double C-u set checkbox to "[-]"
  18479. ;; unconditionally, whereas a single one will toggle its
  18480. ;; presence. Without an universal argument, if the item
  18481. ;; has a checkbox, toggle it. Otherwise repair the list.
  18482. (let* ((box (org-element-property :checkbox context))
  18483. (struct (org-element-property :structure context))
  18484. (old-struct (copy-tree struct))
  18485. (parents (org-list-parents-alist struct))
  18486. (prevs (org-list-prevs-alist struct))
  18487. (orderedp (org-not-nil (org-entry-get nil "ORDERED"))))
  18488. (org-list-set-checkbox
  18489. (org-element-property :begin context) struct
  18490. (cond ((equal arg '(16)) "[-]")
  18491. ((and (not box) (equal arg '(4))) "[ ]")
  18492. ((or (not box) (equal arg '(4))) nil)
  18493. ((eq box 'on) "[ ]")
  18494. (t "[X]")))
  18495. ;; Mimic `org-list-write-struct' but with grabbing
  18496. ;; a return value from `org-list-struct-fix-box'.
  18497. (org-list-struct-fix-ind struct parents 2)
  18498. (org-list-struct-fix-item-end struct)
  18499. (org-list-struct-fix-bul struct prevs)
  18500. (org-list-struct-fix-ind struct parents)
  18501. (let ((block-item
  18502. (org-list-struct-fix-box struct parents prevs orderedp)))
  18503. (if (and box (equal struct old-struct))
  18504. (if (equal arg '(16))
  18505. (message "Checkboxes already reset")
  18506. (user-error "Cannot toggle this checkbox: %s"
  18507. (if (eq box 'on)
  18508. "all subitems checked"
  18509. "unchecked subitems")))
  18510. (org-list-struct-apply-struct struct old-struct)
  18511. (org-update-checkbox-count-maybe))
  18512. (when block-item
  18513. (message "Checkboxes were removed due to empty box at line %d"
  18514. (org-current-line block-item))))))
  18515. (keyword
  18516. (let ((org-inhibit-startup-visibility-stuff t)
  18517. (org-startup-align-all-tables nil))
  18518. (when (boundp 'org-table-coordinate-overlays)
  18519. (mapc 'delete-overlay org-table-coordinate-overlays)
  18520. (setq org-table-coordinate-overlays nil))
  18521. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  18522. (message "Local setup has been refreshed"))
  18523. (plain-list
  18524. ;; At a plain list, with a double C-u argument, set
  18525. ;; checkboxes of each item to "[-]", whereas a single one
  18526. ;; will toggle their presence according to the state of the
  18527. ;; first item in the list. Without an argument, repair the
  18528. ;; list.
  18529. (let* ((begin (org-element-property :contents-begin context))
  18530. (beginm (move-marker (make-marker) begin))
  18531. (struct (org-element-property :structure context))
  18532. (old-struct (copy-tree struct))
  18533. (first-box (save-excursion
  18534. (goto-char begin)
  18535. (looking-at org-list-full-item-re)
  18536. (match-string-no-properties 3)))
  18537. (new-box (cond ((equal arg '(16)) "[-]")
  18538. ((equal arg '(4)) (unless first-box "[ ]"))
  18539. ((equal first-box "[X]") "[ ]")
  18540. (t "[X]"))))
  18541. (cond
  18542. (arg
  18543. (mapc (lambda (pos) (org-list-set-checkbox pos struct new-box))
  18544. (org-list-get-all-items
  18545. begin struct (org-list-prevs-alist struct))))
  18546. ((and first-box (eq (point) begin))
  18547. ;; For convenience, when point is at bol on the first
  18548. ;; item of the list and no argument is provided, simply
  18549. ;; toggle checkbox of that item, if any.
  18550. (org-list-set-checkbox begin struct new-box)))
  18551. (org-list-write-struct
  18552. struct (org-list-parents-alist struct) old-struct)
  18553. (org-update-checkbox-count-maybe)
  18554. (save-excursion (goto-char beginm) (org-list-send-list 'maybe))))
  18555. ((property-drawer node-property)
  18556. (call-interactively 'org-property-action))
  18557. ((radio-target target)
  18558. (call-interactively 'org-update-radio-target-regexp))
  18559. (statistics-cookie
  18560. (call-interactively 'org-update-statistics-cookies))
  18561. ((table table-cell table-row)
  18562. ;; At a table, recalculate every field and align it. Also
  18563. ;; send the table if necessary. If the table has
  18564. ;; a `table.el' type, just give up. At a table row or
  18565. ;; cell, maybe recalculate line but always align table.
  18566. (if (eq (org-element-property :type context) 'table.el)
  18567. (message "Use C-c ' to edit table.el tables")
  18568. (let ((org-enable-table-editor t))
  18569. (if (or (eq type 'table)
  18570. ;; Check if point is at a TBLFM line.
  18571. (and (eq type 'table-row)
  18572. (= (point) (org-element-property :end context))))
  18573. (save-excursion
  18574. (if (org-at-TBLFM-p) (org-calc-current-TBLFM)
  18575. (goto-char (org-element-property :contents-begin context))
  18576. (org-call-with-arg 'org-table-recalculate (or arg t))
  18577. (orgtbl-send-table 'maybe)))
  18578. (org-table-maybe-eval-formula)
  18579. (cond (arg (call-interactively 'org-table-recalculate))
  18580. ((org-table-maybe-recalculate-line))
  18581. (t (org-table-align)))))))
  18582. (timestamp (org-timestamp-change 0 'day))
  18583. (otherwise
  18584. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  18585. (user-error
  18586. "C-c C-c can do nothing useful at this location")))))))))
  18587. (defun org-mode-restart ()
  18588. "Restart Org-mode, to scan again for special lines.
  18589. Also updates the keyword regular expressions."
  18590. (interactive)
  18591. (org-mode)
  18592. (message "Org-mode restarted"))
  18593. (defun org-kill-note-or-show-branches ()
  18594. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  18595. (interactive)
  18596. (if (not org-finish-function)
  18597. (progn
  18598. (hide-subtree)
  18599. (call-interactively 'show-branches))
  18600. (let ((org-note-abort t))
  18601. (funcall org-finish-function))))
  18602. (defun org-ctrl-o (n)
  18603. "Insert a new row in tables, call `open-line' elsewhere."
  18604. (interactive "*p")
  18605. (if (org-at-table-p)
  18606. (org-table-insert-row)
  18607. (open-line n)))
  18608. (defun org-return (&optional indent)
  18609. "Goto next table row or insert a newline.
  18610. Calls `org-table-next-row' or `newline', depending on context.
  18611. See the individual commands for more information."
  18612. (interactive)
  18613. (let (org-ts-what)
  18614. (cond
  18615. ((or (bobp) (org-in-src-block-p))
  18616. (if indent (newline-and-indent) (newline)))
  18617. ((org-at-table-p)
  18618. (org-table-justify-field-maybe)
  18619. (call-interactively 'org-table-next-row))
  18620. ;; when `newline-and-indent' is called within a list, make sure
  18621. ;; text moved stays inside the item.
  18622. ((and (org-in-item-p) indent)
  18623. (if (and (org-at-item-p) (>= (point) (match-end 0)))
  18624. (progn
  18625. (save-match-data (newline))
  18626. (org-indent-line-to (length (match-string 0))))
  18627. (let ((ind (org-get-indentation)))
  18628. (newline)
  18629. (if (org-looking-back org-list-end-re)
  18630. (org-indent-line)
  18631. (org-indent-line-to ind)))))
  18632. ((and org-return-follows-link
  18633. (org-at-timestamp-p t)
  18634. (not (eq org-ts-what 'after)))
  18635. (org-follow-timestamp-link))
  18636. ((and org-return-follows-link
  18637. (let ((tprop (get-text-property (point) 'face)))
  18638. (or (eq tprop 'org-link)
  18639. (and (listp tprop) (memq 'org-link tprop)))))
  18640. (call-interactively 'org-open-at-point))
  18641. ((and (org-at-heading-p)
  18642. (looking-at
  18643. (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
  18644. (org-show-entry)
  18645. (end-of-line 1)
  18646. (newline))
  18647. (t (if indent (newline-and-indent) (newline))))))
  18648. (defun org-return-indent ()
  18649. "Goto next table row or insert a newline and indent.
  18650. Calls `org-table-next-row' or `newline-and-indent', depending on
  18651. context. See the individual commands for more information."
  18652. (interactive)
  18653. (org-return t))
  18654. (defun org-ctrl-c-star ()
  18655. "Compute table, or change heading status of lines.
  18656. Calls `org-table-recalculate' or `org-toggle-heading',
  18657. depending on context."
  18658. (interactive)
  18659. (cond
  18660. ((org-at-table-p)
  18661. (call-interactively 'org-table-recalculate))
  18662. (t
  18663. ;; Convert all lines in region to list items
  18664. (call-interactively 'org-toggle-heading))))
  18665. (defun org-ctrl-c-minus ()
  18666. "Insert separator line in table or modify bullet status of line.
  18667. Also turns a plain line or a region of lines into list items.
  18668. Calls `org-table-insert-hline', `org-toggle-item', or
  18669. `org-cycle-list-bullet', depending on context."
  18670. (interactive)
  18671. (cond
  18672. ((org-at-table-p)
  18673. (call-interactively 'org-table-insert-hline))
  18674. ((org-region-active-p)
  18675. (call-interactively 'org-toggle-item))
  18676. ((org-in-item-p)
  18677. (call-interactively 'org-cycle-list-bullet))
  18678. (t
  18679. (call-interactively 'org-toggle-item))))
  18680. (defun org-toggle-item (arg)
  18681. "Convert headings or normal lines to items, items to normal lines.
  18682. If there is no active region, only the current line is considered.
  18683. If the first non blank line in the region is a headline, convert
  18684. all headlines to items, shifting text accordingly.
  18685. If it is an item, convert all items to normal lines.
  18686. If it is normal text, change region into a list of items.
  18687. With a prefix argument ARG, change the region in a single item."
  18688. (interactive "P")
  18689. (let ((shift-text
  18690. (function
  18691. ;; Shift text in current section to IND, from point to END.
  18692. ;; The function leaves point to END line.
  18693. (lambda (ind end)
  18694. (let ((min-i 1000) (end (copy-marker end)))
  18695. ;; First determine the minimum indentation (MIN-I) of
  18696. ;; the text.
  18697. (save-excursion
  18698. (catch 'exit
  18699. (while (< (point) end)
  18700. (let ((i (org-get-indentation)))
  18701. (cond
  18702. ;; Skip blank lines and inline tasks.
  18703. ((looking-at "^[ \t]*$"))
  18704. ((looking-at org-outline-regexp-bol))
  18705. ;; We can't find less than 0 indentation.
  18706. ((zerop i) (throw 'exit (setq min-i 0)))
  18707. ((< i min-i) (setq min-i i))))
  18708. (forward-line))))
  18709. ;; Then indent each line so that a line indented to
  18710. ;; MIN-I becomes indented to IND. Ignore blank lines
  18711. ;; and inline tasks in the process.
  18712. (let ((delta (- ind min-i)))
  18713. (while (< (point) end)
  18714. (unless (or (looking-at "^[ \t]*$")
  18715. (looking-at org-outline-regexp-bol))
  18716. (org-indent-line-to (+ (org-get-indentation) delta)))
  18717. (forward-line)))))))
  18718. (skip-blanks
  18719. (function
  18720. ;; Return beginning of first non-blank line, starting from
  18721. ;; line at POS.
  18722. (lambda (pos)
  18723. (save-excursion
  18724. (goto-char pos)
  18725. (skip-chars-forward " \r\t\n")
  18726. (point-at-bol)))))
  18727. beg end)
  18728. ;; Determine boundaries of changes.
  18729. (if (org-region-active-p)
  18730. (setq beg (funcall skip-blanks (region-beginning))
  18731. end (copy-marker (region-end)))
  18732. (setq beg (funcall skip-blanks (point-at-bol))
  18733. end (copy-marker (point-at-eol))))
  18734. ;; Depending on the starting line, choose an action on the text
  18735. ;; between BEG and END.
  18736. (org-with-limited-levels
  18737. (save-excursion
  18738. (goto-char beg)
  18739. (cond
  18740. ;; Case 1. Start at an item: de-itemize. Note that it only
  18741. ;; happens when a region is active: `org-ctrl-c-minus'
  18742. ;; would call `org-cycle-list-bullet' otherwise.
  18743. ((org-at-item-p)
  18744. (while (< (point) end)
  18745. (when (org-at-item-p)
  18746. (skip-chars-forward " \t")
  18747. (delete-region (point) (match-end 0)))
  18748. (forward-line)))
  18749. ;; Case 2. Start at an heading: convert to items.
  18750. ((org-at-heading-p)
  18751. (let* ((bul (org-list-bullet-string "-"))
  18752. (bul-len (length bul))
  18753. ;; Indentation of the first heading. It should be
  18754. ;; relative to the indentation of its parent, if any.
  18755. (start-ind (save-excursion
  18756. (cond
  18757. ((not org-adapt-indentation) 0)
  18758. ((not (outline-previous-heading)) 0)
  18759. (t (length (match-string 0))))))
  18760. ;; Level of first heading. Further headings will be
  18761. ;; compared to it to determine hierarchy in the list.
  18762. (ref-level (org-reduced-level (org-outline-level))))
  18763. (while (< (point) end)
  18764. (let* ((level (org-reduced-level (org-outline-level)))
  18765. (delta (max 0 (- level ref-level))))
  18766. ;; If current headline is less indented than the first
  18767. ;; one, set it as reference, in order to preserve
  18768. ;; subtrees.
  18769. (when (< level ref-level) (setq ref-level level))
  18770. (replace-match bul t t)
  18771. (org-indent-line-to (+ start-ind (* delta bul-len)))
  18772. ;; Ensure all text down to END (or SECTION-END) belongs
  18773. ;; to the newly created item.
  18774. (let ((section-end (save-excursion
  18775. (or (outline-next-heading) (point)))))
  18776. (forward-line)
  18777. (funcall shift-text
  18778. (+ start-ind (* (1+ delta) bul-len))
  18779. (min end section-end)))))))
  18780. ;; Case 3. Normal line with ARG: make the first line of region
  18781. ;; an item, and shift indentation of others lines to
  18782. ;; set them as item's body.
  18783. (arg (let* ((bul (org-list-bullet-string "-"))
  18784. (bul-len (length bul))
  18785. (ref-ind (org-get-indentation)))
  18786. (skip-chars-forward " \t")
  18787. (insert bul)
  18788. (forward-line)
  18789. (while (< (point) end)
  18790. ;; Ensure that lines less indented than first one
  18791. ;; still get included in item body.
  18792. (funcall shift-text
  18793. (+ ref-ind bul-len)
  18794. (min end (save-excursion (or (outline-next-heading)
  18795. (point)))))
  18796. (forward-line))))
  18797. ;; Case 4. Normal line without ARG: turn each non-item line
  18798. ;; into an item.
  18799. (t
  18800. (while (< (point) end)
  18801. (unless (or (org-at-heading-p) (org-at-item-p))
  18802. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  18803. (replace-match
  18804. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  18805. (forward-line))))))))
  18806. (defun org-toggle-heading (&optional nstars)
  18807. "Convert headings to normal text, or items or text to headings.
  18808. If there is no active region, only convert the current line.
  18809. With a \\[universal-argument] prefix, convert the whole list at
  18810. point into heading.
  18811. In a region:
  18812. - If the first non blank line is a headline, remove the stars
  18813. from all headlines in the region.
  18814. - If it is a normal line, turn each and every normal line (i.e.,
  18815. not an heading or an item) in the region into headings. If you
  18816. want to convert only the first line of this region, use one
  18817. universal prefix argument.
  18818. - If it is a plain list item, turn all plain list items into headings.
  18819. When converting a line into a heading, the number of stars is chosen
  18820. such that the lines become children of the current entry. However,
  18821. when a numeric prefix argument is given, its value determines the
  18822. number of stars to add."
  18823. (interactive "P")
  18824. (let ((skip-blanks
  18825. (function
  18826. ;; Return beginning of first non-blank line, starting from
  18827. ;; line at POS.
  18828. (lambda (pos)
  18829. (save-excursion
  18830. (goto-char pos)
  18831. (while (org-at-comment-p) (forward-line))
  18832. (skip-chars-forward " \r\t\n")
  18833. (point-at-bol)))))
  18834. beg end toggled)
  18835. ;; Determine boundaries of changes. If a universal prefix has
  18836. ;; been given, put the list in a region. If region ends at a bol,
  18837. ;; do not consider the last line to be in the region.
  18838. (when (and current-prefix-arg (org-at-item-p))
  18839. (if (listp current-prefix-arg) (setq current-prefix-arg 1))
  18840. (org-mark-element))
  18841. (if (org-region-active-p)
  18842. (setq beg (funcall skip-blanks (region-beginning))
  18843. end (copy-marker (save-excursion
  18844. (goto-char (region-end))
  18845. (if (bolp) (point) (point-at-eol)))))
  18846. (setq beg (funcall skip-blanks (point-at-bol))
  18847. end (copy-marker (point-at-eol))))
  18848. ;; Ensure inline tasks don't count as headings.
  18849. (org-with-limited-levels
  18850. (save-excursion
  18851. (goto-char beg)
  18852. (cond
  18853. ;; Case 1. Started at an heading: de-star headings.
  18854. ((org-at-heading-p)
  18855. (while (< (point) end)
  18856. (when (org-at-heading-p t)
  18857. (looking-at org-outline-regexp) (replace-match "")
  18858. (setq toggled t))
  18859. (forward-line)))
  18860. ;; Case 2. Started at an item: change items into headlines.
  18861. ;; One star will be added by `org-list-to-subtree'.
  18862. ((org-at-item-p)
  18863. (let* ((stars (make-string
  18864. ;; subtract the star that will be added again by
  18865. ;; `org-list-to-subtree'
  18866. (if (numberp nstars) (1- nstars)
  18867. (or (org-current-level) 0))
  18868. ?*))
  18869. (add-stars
  18870. (cond (nstars "") ; stars from prefix only
  18871. ((equal stars "") "") ; before first heading
  18872. (org-odd-levels-only "*") ; inside heading, odd
  18873. (t "")))) ; inside heading, oddeven
  18874. (while (< (point) end)
  18875. (when (org-at-item-p)
  18876. ;; Pay attention to cases when region ends before list.
  18877. (let* ((struct (org-list-struct))
  18878. (list-end (min (org-list-get-bottom-point struct) (1+ end))))
  18879. (save-restriction
  18880. (narrow-to-region (point) list-end)
  18881. (insert
  18882. (org-list-to-subtree
  18883. (org-list-parse-list t)
  18884. '(:istart (concat stars add-stars (funcall get-stars depth))
  18885. :icount (concat stars add-stars (funcall get-stars depth)))))))
  18886. (setq toggled t))
  18887. (forward-line))))
  18888. ;; Case 3. Started at normal text: make every line an heading,
  18889. ;; skipping headlines and items.
  18890. (t (let* ((stars
  18891. (make-string
  18892. (if (numberp nstars) nstars (or (org-current-level) 0)) ?*))
  18893. (add-stars
  18894. (cond (nstars "") ; stars from prefix only
  18895. ((equal stars "") "*") ; before first heading
  18896. (org-odd-levels-only "**") ; inside heading, odd
  18897. (t "*"))) ; inside heading, oddeven
  18898. (rpl (concat stars add-stars " "))
  18899. (lend (if (listp nstars) (save-excursion (end-of-line) (point)))))
  18900. (while (< (point) (if (equal nstars '(4)) lend end))
  18901. (when (and (not (or (org-at-heading-p) (org-at-item-p) (org-at-comment-p)))
  18902. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  18903. (replace-match (concat rpl (match-string 2))) (setq toggled t))
  18904. (forward-line)))))))
  18905. (unless toggled (message "Cannot toggle heading from here"))))
  18906. (defun org-meta-return (&optional arg)
  18907. "Insert a new heading or wrap a region in a table.
  18908. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  18909. See the individual commands for more information."
  18910. (interactive "P")
  18911. (cond
  18912. ((run-hook-with-args-until-success 'org-metareturn-hook))
  18913. ((or (org-at-drawer-p) (org-at-property-p))
  18914. (newline-and-indent))
  18915. ((org-at-table-p)
  18916. (call-interactively 'org-table-wrap-region))
  18917. (t (call-interactively 'org-insert-heading))))
  18918. ;;; Menu entries
  18919. (defsubst org-in-subtree-not-table-p ()
  18920. "Are we in a subtree and not in a table?"
  18921. (and (not (org-before-first-heading-p))
  18922. (not (org-at-table-p))))
  18923. ;; Define the Org-mode menus
  18924. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  18925. '("Tbl"
  18926. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  18927. ["Next Field" org-cycle (org-at-table-p)]
  18928. ["Previous Field" org-shifttab (org-at-table-p)]
  18929. ["Next Row" org-return (org-at-table-p)]
  18930. "--"
  18931. ["Blank Field" org-table-blank-field (org-at-table-p)]
  18932. ["Edit Field" org-table-edit-field (org-at-table-p)]
  18933. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  18934. "--"
  18935. ("Column"
  18936. ["Move Column Left" org-metaleft (org-at-table-p)]
  18937. ["Move Column Right" org-metaright (org-at-table-p)]
  18938. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  18939. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  18940. ("Row"
  18941. ["Move Row Up" org-metaup (org-at-table-p)]
  18942. ["Move Row Down" org-metadown (org-at-table-p)]
  18943. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  18944. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  18945. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  18946. "--"
  18947. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  18948. ("Rectangle"
  18949. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  18950. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  18951. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  18952. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  18953. "--"
  18954. ("Calculate"
  18955. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  18956. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  18957. ["Edit Formulas" org-edit-special (org-at-table-p)]
  18958. "--"
  18959. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  18960. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  18961. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  18962. "--"
  18963. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  18964. "--"
  18965. ["Sum Column/Rectangle" org-table-sum
  18966. (or (org-at-table-p) (org-region-active-p))]
  18967. ["Which Column?" org-table-current-column (org-at-table-p)])
  18968. ["Debug Formulas"
  18969. org-table-toggle-formula-debugger
  18970. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  18971. ["Show Col/Row Numbers"
  18972. org-table-toggle-coordinate-overlays
  18973. :style toggle
  18974. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  18975. "--"
  18976. ["Create" org-table-create (and (not (org-at-table-p))
  18977. org-enable-table-editor)]
  18978. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  18979. ["Import from File" org-table-import (not (org-at-table-p))]
  18980. ["Export to File" org-table-export (org-at-table-p)]
  18981. "--"
  18982. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  18983. (easy-menu-define org-org-menu org-mode-map "Org menu"
  18984. '("Org"
  18985. ("Show/Hide"
  18986. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  18987. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  18988. ["Sparse Tree..." org-sparse-tree t]
  18989. ["Reveal Context" org-reveal t]
  18990. ["Show All" show-all t]
  18991. "--"
  18992. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  18993. "--"
  18994. ["New Heading" org-insert-heading t]
  18995. ("Navigate Headings"
  18996. ["Up" outline-up-heading t]
  18997. ["Next" outline-next-visible-heading t]
  18998. ["Previous" outline-previous-visible-heading t]
  18999. ["Next Same Level" outline-forward-same-level t]
  19000. ["Previous Same Level" outline-backward-same-level t]
  19001. "--"
  19002. ["Jump" org-goto t])
  19003. ("Edit Structure"
  19004. ["Refile Subtree" org-refile (org-in-subtree-not-table-p)]
  19005. "--"
  19006. ["Move Subtree Up" org-shiftmetaup (org-in-subtree-not-table-p)]
  19007. ["Move Subtree Down" org-shiftmetadown (org-in-subtree-not-table-p)]
  19008. "--"
  19009. ["Copy Subtree" org-copy-special (org-in-subtree-not-table-p)]
  19010. ["Cut Subtree" org-cut-special (org-in-subtree-not-table-p)]
  19011. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  19012. "--"
  19013. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  19014. "--"
  19015. ["Copy visible text" org-copy-visible t]
  19016. "--"
  19017. ["Promote Heading" org-metaleft (org-in-subtree-not-table-p)]
  19018. ["Promote Subtree" org-shiftmetaleft (org-in-subtree-not-table-p)]
  19019. ["Demote Heading" org-metaright (org-in-subtree-not-table-p)]
  19020. ["Demote Subtree" org-shiftmetaright (org-in-subtree-not-table-p)]
  19021. "--"
  19022. ["Sort Region/Children" org-sort t]
  19023. "--"
  19024. ["Convert to odd levels" org-convert-to-odd-levels t]
  19025. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  19026. ("Editing"
  19027. ["Emphasis..." org-emphasize t]
  19028. ["Edit Source Example" org-edit-special t]
  19029. "--"
  19030. ["Footnote new/jump" org-footnote-action t]
  19031. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  19032. ("Archive"
  19033. ["Archive (default method)" org-archive-subtree-default (org-in-subtree-not-table-p)]
  19034. "--"
  19035. ["Move Subtree to Archive file" org-advertized-archive-subtree (org-in-subtree-not-table-p)]
  19036. ["Toggle ARCHIVE tag" org-toggle-archive-tag (org-in-subtree-not-table-p)]
  19037. ["Move subtree to Archive sibling" org-archive-to-archive-sibling (org-in-subtree-not-table-p)]
  19038. )
  19039. "--"
  19040. ("Hyperlinks"
  19041. ["Store Link (Global)" org-store-link t]
  19042. ["Find existing link to here" org-occur-link-in-agenda-files t]
  19043. ["Insert Link" org-insert-link t]
  19044. ["Follow Link" org-open-at-point t]
  19045. "--"
  19046. ["Next link" org-next-link t]
  19047. ["Previous link" org-previous-link t]
  19048. "--"
  19049. ["Descriptive Links"
  19050. org-toggle-link-display
  19051. :style radio
  19052. :selected org-descriptive-links
  19053. ]
  19054. ["Literal Links"
  19055. org-toggle-link-display
  19056. :style radio
  19057. :selected (not org-descriptive-links)])
  19058. "--"
  19059. ("TODO Lists"
  19060. ["TODO/DONE/-" org-todo t]
  19061. ("Select keyword"
  19062. ["Next keyword" org-shiftright (org-at-heading-p)]
  19063. ["Previous keyword" org-shiftleft (org-at-heading-p)]
  19064. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  19065. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
  19066. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
  19067. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  19068. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  19069. "--"
  19070. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  19071. :selected org-enforce-todo-dependencies :style toggle :active t]
  19072. "Settings for tree at point"
  19073. ["Do Children sequentially" org-toggle-ordered-property :style radio
  19074. :selected (org-entry-get nil "ORDERED")
  19075. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  19076. ["Do Children parallel" org-toggle-ordered-property :style radio
  19077. :selected (not (org-entry-get nil "ORDERED"))
  19078. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  19079. "--"
  19080. ["Set Priority" org-priority t]
  19081. ["Priority Up" org-shiftup t]
  19082. ["Priority Down" org-shiftdown t]
  19083. "--"
  19084. ["Get news from all feeds" org-feed-update-all t]
  19085. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  19086. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  19087. ("TAGS and Properties"
  19088. ["Set Tags" org-set-tags-command (not (org-before-first-heading-p))]
  19089. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  19090. "--"
  19091. ["Set property" org-set-property (not (org-before-first-heading-p))]
  19092. ["Column view of properties" org-columns t]
  19093. ["Insert Column View DBlock" org-insert-columns-dblock t])
  19094. ("Dates and Scheduling"
  19095. ["Timestamp" org-time-stamp (not (org-before-first-heading-p))]
  19096. ["Timestamp (inactive)" org-time-stamp-inactive (not (org-before-first-heading-p))]
  19097. ("Change Date"
  19098. ["1 Day Later" org-shiftright (org-at-timestamp-p)]
  19099. ["1 Day Earlier" org-shiftleft (org-at-timestamp-p)]
  19100. ["1 ... Later" org-shiftup (org-at-timestamp-p)]
  19101. ["1 ... Earlier" org-shiftdown (org-at-timestamp-p)])
  19102. ["Compute Time Range" org-evaluate-time-range t]
  19103. ["Schedule Item" org-schedule (not (org-before-first-heading-p))]
  19104. ["Deadline" org-deadline (not (org-before-first-heading-p))]
  19105. "--"
  19106. ["Custom time format" org-toggle-time-stamp-overlays
  19107. :style radio :selected org-display-custom-times]
  19108. "--"
  19109. ["Goto Calendar" org-goto-calendar t]
  19110. ["Date from Calendar" org-date-from-calendar t]
  19111. "--"
  19112. ["Start/Restart Timer" org-timer-start t]
  19113. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  19114. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  19115. ["Insert Timer String" org-timer t]
  19116. ["Insert Timer Item" org-timer-item t])
  19117. ("Logging work"
  19118. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  19119. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  19120. ["Clock out" org-clock-out t]
  19121. ["Clock cancel" org-clock-cancel t]
  19122. "--"
  19123. ["Mark as default task" org-clock-mark-default-task t]
  19124. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  19125. ["Goto running clock" org-clock-goto t]
  19126. "--"
  19127. ["Display times" org-clock-display t]
  19128. ["Create clock table" org-clock-report t]
  19129. "--"
  19130. ["Record DONE time"
  19131. (progn (setq org-log-done (not org-log-done))
  19132. (message "Switching to %s will %s record a timestamp"
  19133. (car org-done-keywords)
  19134. (if org-log-done "automatically" "not")))
  19135. :style toggle :selected org-log-done])
  19136. "--"
  19137. ["Agenda Command..." org-agenda t]
  19138. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  19139. ("File List for Agenda")
  19140. ("Special views current file"
  19141. ["TODO Tree" org-show-todo-tree t]
  19142. ["Check Deadlines" org-check-deadlines t]
  19143. ["Timeline" org-timeline t]
  19144. ["Tags/Property tree" org-match-sparse-tree t])
  19145. "--"
  19146. ["Export/Publish..." org-export-dispatch t]
  19147. ("LaTeX"
  19148. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  19149. :selected org-cdlatex-mode]
  19150. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  19151. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  19152. ["Modify math symbol" org-cdlatex-math-modify
  19153. (org-inside-LaTeX-fragment-p)]
  19154. ["Insert citation" org-reftex-citation t]
  19155. "--"
  19156. ["Template for BEAMER" (org-beamer-insert-options-template) t])
  19157. "--"
  19158. ("MobileOrg"
  19159. ["Push Files and Views" org-mobile-push t]
  19160. ["Get Captured and Flagged" org-mobile-pull t]
  19161. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  19162. "--"
  19163. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  19164. "--"
  19165. ("Documentation"
  19166. ["Show Version" org-version t]
  19167. ["Info Documentation" org-info t])
  19168. ("Customize"
  19169. ["Browse Org Group" org-customize t]
  19170. "--"
  19171. ["Expand This Menu" org-create-customize-menu
  19172. (fboundp 'customize-menu-create)])
  19173. ["Send bug report" org-submit-bug-report t]
  19174. "--"
  19175. ("Refresh/Reload"
  19176. ["Refresh setup current buffer" org-mode-restart t]
  19177. ["Reload Org (after update)" org-reload t]
  19178. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x !"])
  19179. ))
  19180. (defun org-info (&optional node)
  19181. "Read documentation for Org-mode in the info system.
  19182. With optional NODE, go directly to that node."
  19183. (interactive)
  19184. (info (format "(org)%s" (or node ""))))
  19185. ;;;###autoload
  19186. (defun org-submit-bug-report ()
  19187. "Submit a bug report on Org-mode via mail.
  19188. Don't hesitate to report any problems or inaccurate documentation.
  19189. If you don't have setup sending mail from (X)Emacs, please copy the
  19190. output buffer into your mail program, as it gives us important
  19191. information about your Org-mode version and configuration."
  19192. (interactive)
  19193. (require 'reporter)
  19194. (org-load-modules-maybe)
  19195. (org-require-autoloaded-modules)
  19196. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  19197. (reporter-submit-bug-report
  19198. "emacs-orgmode@gnu.org"
  19199. (org-version nil 'full)
  19200. (let (list)
  19201. (save-window-excursion
  19202. (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
  19203. (delete-other-windows)
  19204. (erase-buffer)
  19205. (insert "You are about to submit a bug report to the Org-mode mailing list.
  19206. We would like to add your full Org-mode and Outline configuration to the
  19207. bug report. This greatly simplifies the work of the maintainer and
  19208. other experts on the mailing list.
  19209. HOWEVER, some variables you have customized may contain private
  19210. information. The names of customers, colleagues, or friends, might
  19211. appear in the form of file names, tags, todo states, or search strings.
  19212. If you answer yes to the prompt, you might want to check and remove
  19213. such private information before sending the email.")
  19214. (add-text-properties (point-min) (point-max) '(face org-warning))
  19215. (when (yes-or-no-p "Include your Org-mode configuration ")
  19216. (mapatoms
  19217. (lambda (v)
  19218. (and (boundp v)
  19219. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  19220. (or (and (symbol-value v)
  19221. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  19222. (and
  19223. (get v 'custom-type) (get v 'standard-value)
  19224. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  19225. (push v list)))))
  19226. (kill-buffer (get-buffer "*Warn about privacy*"))
  19227. list))
  19228. nil nil
  19229. "Remember to cover the basics, that is, what you expected to happen and
  19230. what in fact did happen. You don't know how to make a good report? See
  19231. http://orgmode.org/manual/Feedback.html#Feedback
  19232. Your bug report will be posted to the Org-mode mailing list.
  19233. ------------------------------------------------------------------------")
  19234. (save-excursion
  19235. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  19236. (replace-match "\\1Bug: \\3 [\\2]")))))
  19237. (defun org-install-agenda-files-menu ()
  19238. (let ((bl (buffer-list)))
  19239. (save-excursion
  19240. (while bl
  19241. (set-buffer (pop bl))
  19242. (if (derived-mode-p 'org-mode) (setq bl nil)))
  19243. (when (derived-mode-p 'org-mode)
  19244. (easy-menu-change
  19245. '("Org") "File List for Agenda"
  19246. (append
  19247. (list
  19248. ["Edit File List" (org-edit-agenda-file-list) t]
  19249. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  19250. ["Remove Current File from List" org-remove-file t]
  19251. ["Cycle through agenda files" org-cycle-agenda-files t]
  19252. ["Occur in all agenda files" org-occur-in-agenda-files t]
  19253. "--")
  19254. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  19255. ;;;; Documentation
  19256. (defun org-require-autoloaded-modules ()
  19257. (interactive)
  19258. (mapc 'require
  19259. '(org-agenda org-archive org-attach org-clock org-colview org-id
  19260. org-remember org-table org-timer)))
  19261. ;;;###autoload
  19262. (defun org-reload (&optional uncompiled)
  19263. "Reload all org lisp files.
  19264. With prefix arg UNCOMPILED, load the uncompiled versions."
  19265. (interactive "P")
  19266. (require 'loadhist)
  19267. (let* ((org-dir (org-find-library-dir "org"))
  19268. (contrib-dir (or (org-find-library-dir "org-contribdir") org-dir))
  19269. (feature-re "^\\(org\\|ob\\|ox\\)\\(-.*\\)?")
  19270. (remove-re (mapconcat 'identity
  19271. (mapcar (lambda (f) (concat "^" f "$"))
  19272. (list (if (featurep 'xemacs)
  19273. "org-colview"
  19274. "org-colview-xemacs")
  19275. "org" "org-loaddefs" "org-version"))
  19276. "\\|"))
  19277. (feats (delete-dups
  19278. (mapcar 'file-name-sans-extension
  19279. (mapcar 'file-name-nondirectory
  19280. (delq nil
  19281. (mapcar 'feature-file
  19282. features))))))
  19283. (lfeat (append
  19284. (sort
  19285. (setq feats
  19286. (delq nil (mapcar
  19287. (lambda (f)
  19288. (if (and (string-match feature-re f)
  19289. (not (string-match remove-re f)))
  19290. f nil))
  19291. feats)))
  19292. 'string-lessp)
  19293. (list "org-version" "org")))
  19294. (load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  19295. (load-suffixes (if uncompiled (reverse load-suffixes) load-suffixes))
  19296. load-uncore load-misses)
  19297. (setq load-misses
  19298. (delq 't
  19299. (mapcar (lambda (f)
  19300. (or (org-load-noerror-mustsuffix (concat org-dir f))
  19301. (and (string= org-dir contrib-dir)
  19302. (org-load-noerror-mustsuffix (concat contrib-dir f)))
  19303. (and (org-load-noerror-mustsuffix (concat (org-find-library-dir f) f))
  19304. (add-to-list 'load-uncore f 'append)
  19305. 't)
  19306. f))
  19307. lfeat)))
  19308. (if load-uncore
  19309. (message "The following feature%s found in load-path, please check if that's correct:\n%s"
  19310. (if (> (length load-uncore) 1) "s were" " was") load-uncore))
  19311. (if load-misses
  19312. (message "Some error occured while reloading Org feature%s\n%s\nPlease check *Messages*!\n%s"
  19313. (if (> (length load-misses) 1) "s" "") load-misses (org-version nil 'full))
  19314. (message "Successfully reloaded Org\n%s" (org-version nil 'full)))))
  19315. ;;;###autoload
  19316. (defun org-customize ()
  19317. "Call the customize function with org as argument."
  19318. (interactive)
  19319. (org-load-modules-maybe)
  19320. (org-require-autoloaded-modules)
  19321. (customize-browse 'org))
  19322. (defun org-create-customize-menu ()
  19323. "Create a full customization menu for Org-mode, insert it into the menu."
  19324. (interactive)
  19325. (org-load-modules-maybe)
  19326. (org-require-autoloaded-modules)
  19327. (if (fboundp 'customize-menu-create)
  19328. (progn
  19329. (easy-menu-change
  19330. '("Org") "Customize"
  19331. `(["Browse Org group" org-customize t]
  19332. "--"
  19333. ,(customize-menu-create 'org)
  19334. ["Set" Custom-set t]
  19335. ["Save" Custom-save t]
  19336. ["Reset to Current" Custom-reset-current t]
  19337. ["Reset to Saved" Custom-reset-saved t]
  19338. ["Reset to Standard Settings" Custom-reset-standard t]))
  19339. (message "\"Org\"-menu now contains full customization menu"))
  19340. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  19341. ;;;; Miscellaneous stuff
  19342. ;;; Generally useful functions
  19343. (defun org-get-at-bol (property)
  19344. "Get text property PROPERTY at beginning of line."
  19345. (get-text-property (point-at-bol) property))
  19346. (defun org-find-text-property-in-string (prop s)
  19347. "Return the first non-nil value of property PROP in string S."
  19348. (or (get-text-property 0 prop s)
  19349. (get-text-property (or (next-single-property-change 0 prop s) 0)
  19350. prop s)))
  19351. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  19352. "Display the given MESSAGE as a warning."
  19353. (if (fboundp 'display-warning)
  19354. (display-warning 'org message
  19355. (if (featurep 'xemacs) 'warning :warning))
  19356. (let ((buf (get-buffer-create "*Org warnings*")))
  19357. (with-current-buffer buf
  19358. (goto-char (point-max))
  19359. (insert "Warning (Org): " message)
  19360. (unless (bolp)
  19361. (newline)))
  19362. (display-buffer buf)
  19363. (sit-for 0))))
  19364. (defun org-eval (form)
  19365. "Eval FORM and return result."
  19366. (condition-case error
  19367. (eval form)
  19368. (error (format "%%![Error: %s]" error))))
  19369. (defun org-in-clocktable-p ()
  19370. "Check if the cursor is in a clocktable."
  19371. (let ((pos (point)) start)
  19372. (save-excursion
  19373. (end-of-line 1)
  19374. (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
  19375. (setq start (match-beginning 0))
  19376. (re-search-forward "^[ \t]*#\\+END:.*" nil t)
  19377. (>= (match-end 0) pos)
  19378. start))))
  19379. (defun org-in-commented-line ()
  19380. "Is point in a line starting with `#'?"
  19381. (equal (char-after (point-at-bol)) ?#))
  19382. (defun org-in-indented-comment-line ()
  19383. "Is point in a line starting with `#' after some white space?"
  19384. (save-excursion
  19385. (save-match-data
  19386. (goto-char (point-at-bol))
  19387. (looking-at "[ \t]*#"))))
  19388. (defun org-in-verbatim-emphasis ()
  19389. (save-match-data
  19390. (and (org-in-regexp org-emph-re 2)
  19391. (>= (point) (match-beginning 3))
  19392. (<= (point) (match-end 4))
  19393. (member (match-string 3) '("=" "~")))))
  19394. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  19395. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  19396. (if (and marker (marker-buffer marker)
  19397. (buffer-live-p (marker-buffer marker)))
  19398. (progn
  19399. (org-pop-to-buffer-same-window (marker-buffer marker))
  19400. (if (or (> marker (point-max)) (< marker (point-min)))
  19401. (widen))
  19402. (goto-char marker)
  19403. (org-show-context 'org-goto))
  19404. (if bookmark
  19405. (bookmark-jump bookmark)
  19406. (error "Cannot find location"))))
  19407. (defun org-quote-csv-field (s)
  19408. "Quote field for inclusion in CSV material."
  19409. (if (string-match "[\",]" s)
  19410. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  19411. s))
  19412. (defun org-force-self-insert (N)
  19413. "Needed to enforce self-insert under remapping."
  19414. (interactive "p")
  19415. (self-insert-command N))
  19416. (defun org-string-width (s)
  19417. "Compute width of string, ignoring invisible characters.
  19418. This ignores character with invisibility property `org-link', and also
  19419. characters with property `org-cwidth', because these will become invisible
  19420. upon the next fontification round."
  19421. (let (b l)
  19422. (when (or (eq t buffer-invisibility-spec)
  19423. (assq 'org-link buffer-invisibility-spec))
  19424. (while (setq b (text-property-any 0 (length s)
  19425. 'invisible 'org-link s))
  19426. (setq s (concat (substring s 0 b)
  19427. (substring s (or (next-single-property-change
  19428. b 'invisible s) (length s)))))))
  19429. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  19430. (setq s (concat (substring s 0 b)
  19431. (substring s (or (next-single-property-change
  19432. b 'org-cwidth s) (length s))))))
  19433. (setq l (string-width s) b -1)
  19434. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  19435. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  19436. l))
  19437. (defun org-shorten-string (s maxlength)
  19438. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  19439. If the string is shorter or has length MAXLENGTH, just return the
  19440. original string. If it is longer, the functions finds a space in the
  19441. string, breaks this string off at that locations and adds three dots
  19442. as ellipsis. Including the ellipsis, the string will not be longer
  19443. than MAXLENGTH. If finding a good breaking point in the string does
  19444. not work, the string is just chopped off in the middle of a word
  19445. if necessary."
  19446. (if (<= (length s) maxlength)
  19447. s
  19448. (let* ((n (max (- maxlength 4) 1))
  19449. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  19450. (if (string-match re s)
  19451. (concat (match-string 1 s) "...")
  19452. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  19453. (defun org-get-indentation (&optional line)
  19454. "Get the indentation of the current line, interpreting tabs.
  19455. When LINE is given, assume it represents a line and compute its indentation."
  19456. (if line
  19457. (if (string-match "^ *" (org-remove-tabs line))
  19458. (match-end 0))
  19459. (save-excursion
  19460. (beginning-of-line 1)
  19461. (skip-chars-forward " \t")
  19462. (current-column))))
  19463. (defun org-get-string-indentation (s)
  19464. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  19465. (let ((n -1) (i 0) (w tab-width) c)
  19466. (catch 'exit
  19467. (while (< (setq n (1+ n)) (length s))
  19468. (setq c (aref s n))
  19469. (cond ((= c ?\ ) (setq i (1+ i)))
  19470. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  19471. (t (throw 'exit t)))))
  19472. i))
  19473. (defun org-remove-tabs (s &optional width)
  19474. "Replace tabulators in S with spaces.
  19475. Assumes that s is a single line, starting in column 0."
  19476. (setq width (or width tab-width))
  19477. (while (string-match "\t" s)
  19478. (setq s (replace-match
  19479. (make-string
  19480. (- (* width (/ (+ (match-beginning 0) width) width))
  19481. (match-beginning 0)) ?\ )
  19482. t t s)))
  19483. s)
  19484. (defun org-fix-indentation (line ind)
  19485. "Fix indentation in LINE.
  19486. IND is a cons cell with target and minimum indentation.
  19487. If the current indentation in LINE is smaller than the minimum,
  19488. leave it alone. If it is larger than ind, set it to the target."
  19489. (let* ((l (org-remove-tabs line))
  19490. (i (org-get-indentation l))
  19491. (i1 (car ind)) (i2 (cdr ind)))
  19492. (if (>= i i2) (setq l (substring line i2)))
  19493. (if (> i1 0)
  19494. (concat (make-string i1 ?\ ) l)
  19495. l)))
  19496. (defun org-remove-indentation (code &optional n)
  19497. "Remove the maximum common indentation from the lines in CODE.
  19498. N may optionally be the number of spaces to remove."
  19499. (with-temp-buffer
  19500. (insert code)
  19501. (org-do-remove-indentation n)
  19502. (buffer-string)))
  19503. (defun org-do-remove-indentation (&optional n)
  19504. "Remove the maximum common indentation from the buffer."
  19505. (untabify (point-min) (point-max))
  19506. (let ((min 10000) re)
  19507. (if n
  19508. (setq min n)
  19509. (goto-char (point-min))
  19510. (while (re-search-forward "^ *[^ \n]" nil t)
  19511. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  19512. (unless (or (= min 0) (= min 10000))
  19513. (setq re (format "^ \\{%d\\}" min))
  19514. (goto-char (point-min))
  19515. (while (re-search-forward re nil t)
  19516. (replace-match "")
  19517. (end-of-line 1))
  19518. min)))
  19519. (defun org-fill-template (template alist)
  19520. "Find each %key of ALIST in TEMPLATE and replace it."
  19521. (let ((case-fold-search nil)
  19522. entry key value)
  19523. (setq alist (sort (copy-sequence alist)
  19524. (lambda (a b) (< (length (car a)) (length (car b))))))
  19525. (while (setq entry (pop alist))
  19526. (setq template
  19527. (replace-regexp-in-string
  19528. (concat "%" (regexp-quote (car entry)))
  19529. (or (cdr entry) "") template t t)))
  19530. template))
  19531. (defun org-base-buffer (buffer)
  19532. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  19533. (if (not buffer)
  19534. buffer
  19535. (or (buffer-base-buffer buffer)
  19536. buffer)))
  19537. (defun org-trim (s)
  19538. "Remove whitespace at beginning and end of string."
  19539. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  19540. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  19541. s)
  19542. (defun org-wrap (string &optional width lines)
  19543. "Wrap string to either a number of lines, or a width in characters.
  19544. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  19545. that costs. If there is a word longer than WIDTH, the text is actually
  19546. wrapped to the length of that word.
  19547. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  19548. many lines, whatever width that takes.
  19549. The return value is a list of lines, without newlines at the end."
  19550. (let* ((words (org-split-string string "[ \t\n]+"))
  19551. (maxword (apply 'max (mapcar 'org-string-width words)))
  19552. w ll)
  19553. (cond (width
  19554. (org-do-wrap words (max maxword width)))
  19555. (lines
  19556. (setq w maxword)
  19557. (setq ll (org-do-wrap words maxword))
  19558. (if (<= (length ll) lines)
  19559. ll
  19560. (setq ll words)
  19561. (while (> (length ll) lines)
  19562. (setq w (1+ w))
  19563. (setq ll (org-do-wrap words w)))
  19564. ll))
  19565. (t (error "Cannot wrap this")))))
  19566. (defun org-do-wrap (words width)
  19567. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  19568. (let (lines line)
  19569. (while words
  19570. (setq line (pop words))
  19571. (while (and words (< (+ (length line) (length (car words))) width))
  19572. (setq line (concat line " " (pop words))))
  19573. (setq lines (push line lines)))
  19574. (nreverse lines)))
  19575. (defun org-split-string (string &optional separators)
  19576. "Splits STRING into substrings at SEPARATORS.
  19577. No empty strings are returned if there are matches at the beginning
  19578. and end of string."
  19579. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  19580. (start 0)
  19581. notfirst
  19582. (list nil))
  19583. (while (and (string-match rexp string
  19584. (if (and notfirst
  19585. (= start (match-beginning 0))
  19586. (< start (length string)))
  19587. (1+ start) start))
  19588. (< (match-beginning 0) (length string)))
  19589. (setq notfirst t)
  19590. (or (eq (match-beginning 0) 0)
  19591. (and (eq (match-beginning 0) (match-end 0))
  19592. (eq (match-beginning 0) start))
  19593. (setq list
  19594. (cons (substring string start (match-beginning 0))
  19595. list)))
  19596. (setq start (match-end 0)))
  19597. (or (eq start (length string))
  19598. (setq list
  19599. (cons (substring string start)
  19600. list)))
  19601. (nreverse list)))
  19602. (defun org-quote-vert (s)
  19603. "Replace \"|\" with \"\\vert\"."
  19604. (while (string-match "|" s)
  19605. (setq s (replace-match "\\vert" t t s)))
  19606. s)
  19607. (defun org-uuidgen-p (s)
  19608. "Is S an ID created by UUIDGEN?"
  19609. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  19610. (defun org-in-src-block-p (&optional inside)
  19611. "Whether point is in a code source block.
  19612. When INSIDE is non-nil, don't consider we are within a src block
  19613. when point is at #+BEGIN_SRC or #+END_SRC."
  19614. (let ((case-fold-search t) ov)
  19615. (or (and (setq ov (overlays-at (point)))
  19616. (memq 'org-block-background
  19617. (overlay-properties (car ov))))
  19618. (and (not inside)
  19619. (save-match-data
  19620. (save-excursion
  19621. (beginning-of-line)
  19622. (looking-at ".*#\\+\\(begin\\|end\\)_src")))))))
  19623. (defun org-context ()
  19624. "Return a list of contexts of the current cursor position.
  19625. If several contexts apply, all are returned.
  19626. Each context entry is a list with a symbol naming the context, and
  19627. two positions indicating start and end of the context. Possible
  19628. contexts are:
  19629. :headline anywhere in a headline
  19630. :headline-stars on the leading stars in a headline
  19631. :todo-keyword on a TODO keyword (including DONE) in a headline
  19632. :tags on the TAGS in a headline
  19633. :priority on the priority cookie in a headline
  19634. :item on the first line of a plain list item
  19635. :item-bullet on the bullet/number of a plain list item
  19636. :checkbox on the checkbox in a plain list item
  19637. :table in an org-mode table
  19638. :table-special on a special filed in a table
  19639. :table-table in a table.el table
  19640. :clocktable in a clocktable
  19641. :src-block in a source block
  19642. :link on a hyperlink
  19643. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT, QUOTE.
  19644. :target on a <<target>>
  19645. :radio-target on a <<<radio-target>>>
  19646. :latex-fragment on a LaTeX fragment
  19647. :latex-preview on a LaTeX fragment with overlaid preview image
  19648. This function expects the position to be visible because it uses font-lock
  19649. faces as a help to recognize the following contexts: :table-special, :link,
  19650. and :keyword."
  19651. (let* ((f (get-text-property (point) 'face))
  19652. (faces (if (listp f) f (list f)))
  19653. (case-fold-search t)
  19654. (p (point)) clist o)
  19655. ;; First the large context
  19656. (cond
  19657. ((org-at-heading-p t)
  19658. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  19659. (when (progn
  19660. (beginning-of-line 1)
  19661. (looking-at org-todo-line-tags-regexp))
  19662. (push (org-point-in-group p 1 :headline-stars) clist)
  19663. (push (org-point-in-group p 2 :todo-keyword) clist)
  19664. (push (org-point-in-group p 4 :tags) clist))
  19665. (goto-char p)
  19666. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  19667. (if (looking-at "\\[#[A-Z0-9]\\]")
  19668. (push (org-point-in-group p 0 :priority) clist)))
  19669. ((org-at-item-p)
  19670. (push (org-point-in-group p 2 :item-bullet) clist)
  19671. (push (list :item (point-at-bol)
  19672. (save-excursion (org-end-of-item) (point)))
  19673. clist)
  19674. (and (org-at-item-checkbox-p)
  19675. (push (org-point-in-group p 0 :checkbox) clist)))
  19676. ((org-at-table-p)
  19677. (push (list :table (org-table-begin) (org-table-end)) clist)
  19678. (if (memq 'org-formula faces)
  19679. (push (list :table-special
  19680. (previous-single-property-change p 'face)
  19681. (next-single-property-change p 'face)) clist)))
  19682. ((org-at-table-p 'any)
  19683. (push (list :table-table) clist)))
  19684. (goto-char p)
  19685. (let ((case-fold-search t))
  19686. ;; New the "medium" contexts: clocktables, source blocks
  19687. (cond ((org-in-clocktable-p)
  19688. (push (list :clocktable
  19689. (and (or (looking-at "#\\+BEGIN: clocktable")
  19690. (search-backward "#+BEGIN: clocktable" nil t))
  19691. (match-beginning 0))
  19692. (and (re-search-forward "#\\+END:?" nil t)
  19693. (match-end 0))) clist))
  19694. ((org-in-src-block-p)
  19695. (push (list :src-block
  19696. (and (or (looking-at "#\\+BEGIN_SRC")
  19697. (search-backward "#+BEGIN_SRC" nil t))
  19698. (match-beginning 0))
  19699. (and (search-forward "#+END_SRC" nil t)
  19700. (match-beginning 0))) clist))))
  19701. (goto-char p)
  19702. ;; Now the small context
  19703. (cond
  19704. ((org-at-timestamp-p)
  19705. (push (org-point-in-group p 0 :timestamp) clist))
  19706. ((memq 'org-link faces)
  19707. (push (list :link
  19708. (previous-single-property-change p 'face)
  19709. (next-single-property-change p 'face)) clist))
  19710. ((memq 'org-special-keyword faces)
  19711. (push (list :keyword
  19712. (previous-single-property-change p 'face)
  19713. (next-single-property-change p 'face)) clist))
  19714. ((org-at-target-p)
  19715. (push (org-point-in-group p 0 :target) clist)
  19716. (goto-char (1- (match-beginning 0)))
  19717. (if (looking-at org-radio-target-regexp)
  19718. (push (org-point-in-group p 0 :radio-target) clist))
  19719. (goto-char p))
  19720. ((setq o (car (delq nil
  19721. (mapcar
  19722. (lambda (x)
  19723. (if (memq x org-latex-fragment-image-overlays) x))
  19724. (overlays-at (point))))))
  19725. (push (list :latex-fragment
  19726. (overlay-start o) (overlay-end o)) clist)
  19727. (push (list :latex-preview
  19728. (overlay-start o) (overlay-end o)) clist))
  19729. ((org-inside-LaTeX-fragment-p)
  19730. ;; FIXME: positions wrong.
  19731. (push (list :latex-fragment (point) (point)) clist)))
  19732. (setq clist (nreverse (delq nil clist)))
  19733. clist))
  19734. ;; FIXME: Compare with at-regexp-p Do we need both?
  19735. (defun org-in-regexp (re &optional nlines visually)
  19736. "Check if point is inside a match of regexp.
  19737. Normally only the current line is checked, but you can include NLINES extra
  19738. lines both before and after point into the search.
  19739. If VISUALLY is set, require that the cursor is not after the match but
  19740. really on, so that the block visually is on the match."
  19741. (catch 'exit
  19742. (let ((pos (point))
  19743. (eol (point-at-eol (+ 1 (or nlines 0))))
  19744. (inc (if visually 1 0)))
  19745. (save-excursion
  19746. (beginning-of-line (- 1 (or nlines 0)))
  19747. (while (re-search-forward re eol t)
  19748. (if (and (<= (match-beginning 0) pos)
  19749. (>= (+ inc (match-end 0)) pos))
  19750. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  19751. (defun org-at-regexp-p (regexp)
  19752. "Is point inside a match of REGEXP in the current line?"
  19753. (catch 'exit
  19754. (save-excursion
  19755. (let ((pos (point)) (end (point-at-eol)))
  19756. (beginning-of-line 1)
  19757. (while (re-search-forward regexp end t)
  19758. (if (and (<= (match-beginning 0) pos)
  19759. (>= (match-end 0) pos))
  19760. (throw 'exit t)))
  19761. nil))))
  19762. (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
  19763. "Non-nil when point is between matches of START-RE and END-RE.
  19764. Also return a non-nil value when point is on one of the matches.
  19765. Optional arguments LIM-UP and LIM-DOWN bound the search; they are
  19766. buffer positions. Default values are the positions of headlines
  19767. surrounding the point.
  19768. The functions returns a cons cell whose car (resp. cdr) is the
  19769. position before START-RE (resp. after END-RE)."
  19770. (save-match-data
  19771. (let ((pos (point))
  19772. (limit-up (or lim-up (save-excursion (outline-previous-heading))))
  19773. (limit-down (or lim-down (save-excursion (outline-next-heading))))
  19774. beg end)
  19775. (save-excursion
  19776. ;; Point is on a block when on START-RE or if START-RE can be
  19777. ;; found before it...
  19778. (and (or (org-at-regexp-p start-re)
  19779. (re-search-backward start-re limit-up t))
  19780. (setq beg (match-beginning 0))
  19781. ;; ... and END-RE after it...
  19782. (goto-char (match-end 0))
  19783. (re-search-forward end-re limit-down t)
  19784. (> (setq end (match-end 0)) pos)
  19785. ;; ... without another START-RE in-between.
  19786. (goto-char (match-beginning 0))
  19787. (not (re-search-backward start-re (1+ beg) t))
  19788. ;; Return value.
  19789. (cons beg end))))))
  19790. (defun org-in-block-p (names)
  19791. "Non-nil when point belongs to a block whose name belongs to NAMES.
  19792. NAMES is a list of strings containing names of blocks.
  19793. Return first block name matched, or nil. Beware that in case of
  19794. nested blocks, the returned name may not belong to the closest
  19795. block from point."
  19796. (save-match-data
  19797. (catch 'exit
  19798. (let ((case-fold-search t)
  19799. (lim-up (save-excursion (outline-previous-heading)))
  19800. (lim-down (save-excursion (outline-next-heading))))
  19801. (mapc (lambda (name)
  19802. (let ((n (regexp-quote name)))
  19803. (when (org-between-regexps-p
  19804. (concat "^[ \t]*#\\+begin_" n)
  19805. (concat "^[ \t]*#\\+end_" n)
  19806. lim-up lim-down)
  19807. (throw 'exit n))))
  19808. names))
  19809. nil)))
  19810. (defun org-occur-in-agenda-files (regexp &optional nlines)
  19811. "Call `multi-occur' with buffers for all agenda files."
  19812. (interactive "sOrg-files matching: \np")
  19813. (let* ((files (org-agenda-files))
  19814. (tnames (mapcar 'file-truename files))
  19815. (extra org-agenda-text-search-extra-files)
  19816. f)
  19817. (when (eq (car extra) 'agenda-archives)
  19818. (setq extra (cdr extra))
  19819. (setq files (org-add-archive-files files)))
  19820. (while (setq f (pop extra))
  19821. (unless (member (file-truename f) tnames)
  19822. (add-to-list 'files f 'append)
  19823. (add-to-list 'tnames (file-truename f) 'append)))
  19824. (multi-occur
  19825. (mapcar (lambda (x)
  19826. (with-current-buffer
  19827. (or (get-file-buffer x) (find-file-noselect x))
  19828. (widen)
  19829. (current-buffer)))
  19830. files)
  19831. regexp)))
  19832. (if (boundp 'occur-mode-find-occurrence-hook)
  19833. ;; Emacs 23
  19834. (add-hook 'occur-mode-find-occurrence-hook
  19835. (lambda ()
  19836. (when (derived-mode-p 'org-mode)
  19837. (org-reveal))))
  19838. ;; Emacs 22
  19839. (defadvice occur-mode-goto-occurrence
  19840. (after org-occur-reveal activate)
  19841. (and (derived-mode-p 'org-mode) (org-reveal)))
  19842. (defadvice occur-mode-goto-occurrence-other-window
  19843. (after org-occur-reveal activate)
  19844. (and (derived-mode-p 'org-mode) (org-reveal)))
  19845. (defadvice occur-mode-display-occurrence
  19846. (after org-occur-reveal activate)
  19847. (when (derived-mode-p 'org-mode)
  19848. (let ((pos (occur-mode-find-occurrence)))
  19849. (with-current-buffer (marker-buffer pos)
  19850. (save-excursion
  19851. (goto-char pos)
  19852. (org-reveal)))))))
  19853. (defun org-occur-link-in-agenda-files ()
  19854. "Create a link and search for it in the agendas.
  19855. The link is not stored in `org-stored-links', it is just created
  19856. for the search purpose."
  19857. (interactive)
  19858. (let ((link (condition-case nil
  19859. (org-store-link nil)
  19860. (error "Unable to create a link to here"))))
  19861. (org-occur-in-agenda-files (regexp-quote link))))
  19862. (defun org-reverse-string (string)
  19863. "Return the reverse of STRING."
  19864. (apply 'string (reverse (string-to-list string))))
  19865. (defun org-uniquify (list)
  19866. "Remove duplicate elements from LIST."
  19867. (let (res)
  19868. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  19869. res))
  19870. (defun org-uniquify-alist (alist)
  19871. "Merge duplicate elements of an alist.
  19872. For example, in this alist:
  19873. \(org-uniquify-alist '((a 1) (b 2) (a 3)))
  19874. => '((a 1 3) (b 2))
  19875. merge (a 1) and (a 3) into (a 1 3) and return the new alist."
  19876. (let (rtn)
  19877. (mapc
  19878. (lambda (e)
  19879. (let (n)
  19880. (if (not (assoc (car e) rtn))
  19881. (push e rtn)
  19882. (setq n (cons (car e) (append (cdr (assoc (car e) rtn)) (cdr e))))
  19883. (setq rtn (assq-delete-all (car e) rtn))
  19884. (push n rtn))))
  19885. alist)
  19886. rtn))
  19887. (defun org-delete-all (elts list)
  19888. "Remove all elements in ELTS from LIST."
  19889. (while elts
  19890. (setq list (delete (pop elts) list)))
  19891. list)
  19892. (defun org-count (cl-item cl-seq)
  19893. "Count the number of occurrences of ITEM in SEQ.
  19894. Taken from `count' in cl-seq.el with all keyword arguments removed."
  19895. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  19896. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  19897. (while (< cl-start cl-end)
  19898. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  19899. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  19900. (setq cl-start (1+ cl-start)))
  19901. cl-count))
  19902. (defun org-remove-if (predicate seq)
  19903. "Remove everything from SEQ that fulfills PREDICATE."
  19904. (let (res e)
  19905. (while seq
  19906. (setq e (pop seq))
  19907. (if (not (funcall predicate e)) (push e res)))
  19908. (nreverse res)))
  19909. (defun org-remove-if-not (predicate seq)
  19910. "Remove everything from SEQ that does not fulfill PREDICATE."
  19911. (let (res e)
  19912. (while seq
  19913. (setq e (pop seq))
  19914. (if (funcall predicate e) (push e res)))
  19915. (nreverse res)))
  19916. (defun org-reduce (cl-func cl-seq &rest cl-keys)
  19917. "Reduce two-argument FUNCTION across SEQ.
  19918. Taken from `reduce' in cl-seq.el with all keyword arguments but
  19919. \":initial-value\" removed."
  19920. (let ((cl-accum (cond ((memq :initial-value cl-keys)
  19921. (cadr (memq :initial-value cl-keys)))
  19922. (cl-seq (pop cl-seq))
  19923. (t (funcall cl-func)))))
  19924. (while cl-seq
  19925. (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
  19926. cl-accum))
  19927. (defun org-back-over-empty-lines ()
  19928. "Move backwards over whitespace, to the beginning of the first empty line.
  19929. Returns the number of empty lines passed."
  19930. (let ((pos (point)))
  19931. (if (cdr (assoc 'heading org-blank-before-new-entry))
  19932. (skip-chars-backward " \t\n\r")
  19933. (unless (eobp)
  19934. (forward-line -1)))
  19935. (beginning-of-line 2)
  19936. (goto-char (min (point) pos))
  19937. (count-lines (point) pos)))
  19938. (defun org-skip-whitespace ()
  19939. (skip-chars-forward " \t\n\r"))
  19940. (defun org-point-in-group (point group &optional context)
  19941. "Check if POINT is in match-group GROUP.
  19942. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  19943. match. If the match group does not exist or point is not inside it,
  19944. return nil."
  19945. (and (match-beginning group)
  19946. (>= point (match-beginning group))
  19947. (<= point (match-end group))
  19948. (if context
  19949. (list context (match-beginning group) (match-end group))
  19950. t)))
  19951. (defun org-switch-to-buffer-other-window (&rest args)
  19952. "Switch to buffer in a second window on the current frame.
  19953. In particular, do not allow pop-up frames.
  19954. Returns the newly created buffer."
  19955. (org-no-popups
  19956. (apply 'switch-to-buffer-other-window args)))
  19957. (defun org-combine-plists (&rest plists)
  19958. "Create a single property list from all plists in PLISTS.
  19959. The process starts by copying the first list, and then setting properties
  19960. from the other lists. Settings in the last list are the most significant
  19961. ones and overrule settings in the other lists."
  19962. (let ((rtn (copy-sequence (pop plists)))
  19963. p v ls)
  19964. (while plists
  19965. (setq ls (pop plists))
  19966. (while ls
  19967. (setq p (pop ls) v (pop ls))
  19968. (setq rtn (plist-put rtn p v))))
  19969. rtn))
  19970. (defun org-replace-escapes (string table)
  19971. "Replace %-escapes in STRING with values in TABLE.
  19972. TABLE is an association list with keys like \"%a\" and string values.
  19973. The sequences in STRING may contain normal field width and padding information,
  19974. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  19975. so values can contain further %-escapes if they are define later in TABLE."
  19976. (let ((tbl (copy-alist table))
  19977. (case-fold-search nil)
  19978. (pchg 0)
  19979. e re rpl)
  19980. (while (setq e (pop tbl))
  19981. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  19982. (when (and (cdr e) (string-match re (cdr e)))
  19983. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  19984. (safe "SREF"))
  19985. (add-text-properties 0 3 (list 'sref sref) safe)
  19986. (setcdr e (replace-match safe t t (cdr e)))))
  19987. (while (string-match re string)
  19988. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  19989. (cdr e)))
  19990. (setq string (replace-match rpl t t string))))
  19991. (while (setq pchg (next-property-change pchg string))
  19992. (let ((sref (get-text-property pchg 'sref string)))
  19993. (when (and sref (string-match "SREF" string pchg))
  19994. (setq string (replace-match sref t t string)))))
  19995. string))
  19996. (defun org-sublist (list start end)
  19997. "Return a section of LIST, from START to END.
  19998. Counting starts at 1."
  19999. (let (rtn (c start))
  20000. (setq list (nthcdr (1- start) list))
  20001. (while (and list (<= c end))
  20002. (push (pop list) rtn)
  20003. (setq c (1+ c)))
  20004. (nreverse rtn)))
  20005. (defun org-find-base-buffer-visiting (file)
  20006. "Like `find-buffer-visiting' but always return the base buffer and
  20007. not an indirect buffer."
  20008. (let ((buf (or (get-file-buffer file)
  20009. (find-buffer-visiting file))))
  20010. (if buf
  20011. (or (buffer-base-buffer buf) buf)
  20012. nil)))
  20013. (defun org-image-file-name-regexp (&optional extensions)
  20014. "Return regexp matching the file names of images.
  20015. If EXTENSIONS is given, only match these."
  20016. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  20017. (image-file-name-regexp)
  20018. (let ((image-file-name-extensions
  20019. (or extensions
  20020. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  20021. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  20022. (concat "\\."
  20023. (regexp-opt (nconc (mapcar 'upcase
  20024. image-file-name-extensions)
  20025. image-file-name-extensions)
  20026. t)
  20027. "\\'"))))
  20028. (defun org-file-image-p (file &optional extensions)
  20029. "Return non-nil if FILE is an image."
  20030. (save-match-data
  20031. (string-match (org-image-file-name-regexp extensions) file)))
  20032. (defun org-get-cursor-date (&optional with-time)
  20033. "Return the date at cursor in as a time.
  20034. This works in the calendar and in the agenda, anywhere else it just
  20035. returns the current time.
  20036. If WITH-TIME is non-nil, returns the time of the event at point (in
  20037. the agenda) or the current time of the day."
  20038. (let (date day defd tp tm hod mod)
  20039. (when with-time
  20040. (setq tp (get-text-property (point) 'time))
  20041. (when (and tp (string-match "\\([0-9][0-9]\\):\\([0-9][0-9]\\)" tp))
  20042. (setq hod (string-to-number (match-string 1 tp))
  20043. mod (string-to-number (match-string 2 tp))))
  20044. (or tp (setq hod (nth 2 (decode-time (current-time)))
  20045. mod (nth 1 (decode-time (current-time))))))
  20046. (cond
  20047. ((eq major-mode 'calendar-mode)
  20048. (setq date (calendar-cursor-to-date)
  20049. defd (encode-time 0 (or mod 0) (or hod 0)
  20050. (nth 1 date) (nth 0 date) (nth 2 date))))
  20051. ((eq major-mode 'org-agenda-mode)
  20052. (setq day (get-text-property (point) 'day))
  20053. (if day
  20054. (setq date (calendar-gregorian-from-absolute day)
  20055. defd (encode-time 0 (or mod 0) (or hod 0)
  20056. (nth 1 date) (nth 0 date) (nth 2 date))))))
  20057. (or defd (current-time))))
  20058. (defun org-mark-subtree (&optional up)
  20059. "Mark the current subtree.
  20060. This puts point at the start of the current subtree, and mark at
  20061. the end. If a numeric prefix UP is given, move up into the
  20062. hierarchy of headlines by UP levels before marking the subtree."
  20063. (interactive "P")
  20064. (org-with-limited-levels
  20065. (cond ((org-at-heading-p) (beginning-of-line))
  20066. ((org-before-first-heading-p) (error "Not in a subtree"))
  20067. (t (outline-previous-visible-heading 1))))
  20068. (when up (while (and (> up 0) (org-up-heading-safe)) (decf up)))
  20069. (if (org-called-interactively-p 'any)
  20070. (call-interactively 'org-mark-element)
  20071. (org-mark-element)))
  20072. ;;; Indentation
  20073. (defun org-indent-line ()
  20074. "Indent line depending on context."
  20075. (interactive)
  20076. (let* ((pos (point))
  20077. (itemp (org-at-item-p))
  20078. (case-fold-search t)
  20079. (org-drawer-regexp (or org-drawer-regexp "\000"))
  20080. (inline-task-p (and (featurep 'org-inlinetask)
  20081. (org-inlinetask-in-task-p)))
  20082. (inline-re (and inline-task-p
  20083. (org-inlinetask-outline-regexp)))
  20084. column)
  20085. (if (and orgstruct-is-++ (eq pos (point)))
  20086. (let ((indent-line-function (cadadr (assoc 'indent-line-function org-fb-vars))))
  20087. (indent-according-to-mode))
  20088. (beginning-of-line 1)
  20089. (cond
  20090. ;; Headings
  20091. ((looking-at org-outline-regexp) (setq column 0))
  20092. ;; Footnote definition
  20093. ((looking-at org-footnote-definition-re) (setq column 0))
  20094. ;; Literal examples
  20095. ((looking-at "[ \t]*:\\( \\|$\\)")
  20096. (setq column (org-get-indentation))) ; do nothing
  20097. ;; Lists
  20098. ((ignore-errors (goto-char (org-in-item-p)))
  20099. (setq column (if itemp
  20100. (org-get-indentation)
  20101. (org-list-item-body-column (point))))
  20102. (goto-char pos))
  20103. ;; Drawers
  20104. ((and (looking-at "[ \t]*:END:")
  20105. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  20106. (save-excursion
  20107. (goto-char (1- (match-beginning 1)))
  20108. (setq column (current-column))))
  20109. ;; Special blocks
  20110. ((and (looking-at "[ \t]*#\\+end_\\([a-z]+\\)")
  20111. (save-excursion
  20112. (re-search-backward
  20113. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  20114. (setq column (org-get-indentation (match-string 0))))
  20115. ((and (not (looking-at "[ \t]*#\\+begin_"))
  20116. (org-between-regexps-p "^[ \t]*#\\+begin_" "[ \t]*#\\+end_"))
  20117. (save-excursion
  20118. (re-search-backward "^[ \t]*#\\+begin_\\([a-z]+\\)" nil t))
  20119. (setq column
  20120. (cond ((equal (downcase (match-string 1)) "src")
  20121. ;; src blocks: let `org-edit-src-exit' handle them
  20122. (org-get-indentation))
  20123. ((equal (downcase (match-string 1)) "example")
  20124. (max (org-get-indentation)
  20125. (org-get-indentation (match-string 0))))
  20126. (t
  20127. (org-get-indentation (match-string 0))))))
  20128. ;; This line has nothing special, look at the previous relevant
  20129. ;; line to compute indentation
  20130. (t
  20131. (beginning-of-line 0)
  20132. (while (and (not (bobp))
  20133. (not (looking-at org-table-line-regexp))
  20134. (not (looking-at org-drawer-regexp))
  20135. ;; When point started in an inline task, do not move
  20136. ;; above task starting line.
  20137. (not (and inline-task-p (looking-at inline-re)))
  20138. ;; Skip drawers, blocks, empty lines, verbatim,
  20139. ;; comments, tables, footnotes definitions, lists,
  20140. ;; inline tasks.
  20141. (or (and (looking-at "[ \t]*:END:")
  20142. (re-search-backward org-drawer-regexp nil t))
  20143. (and (looking-at "[ \t]*#\\+end_")
  20144. (re-search-backward "[ \t]*#\\+begin_"nil t))
  20145. (looking-at "[ \t]*[\n:#|]")
  20146. (looking-at org-footnote-definition-re)
  20147. (and (not inline-task-p)
  20148. (featurep 'org-inlinetask)
  20149. (org-inlinetask-in-task-p)
  20150. (or (org-inlinetask-goto-beginning) t))))
  20151. (beginning-of-line 0))
  20152. (cond
  20153. ;; There was a list item above.
  20154. ((save-excursion
  20155. (and (ignore-errors (goto-char (org-in-item-p)))
  20156. (goto-char
  20157. (org-list-get-top-point (org-list-struct)))))
  20158. (looking-at org-list-full-item-re)
  20159. (setq column (length (match-string 0))))
  20160. ;; There was an heading above.
  20161. ((looking-at "\\*+[ \t]+")
  20162. (if (not org-adapt-indentation)
  20163. (setq column 0)
  20164. (goto-char (match-end 0))
  20165. (setq column (current-column))))
  20166. ;; A drawer had started and is unfinished
  20167. ((looking-at org-drawer-regexp)
  20168. (goto-char (1- (match-beginning 1)))
  20169. (setq column (current-column)))
  20170. ;; Else, nothing noticeable found: get indentation and go on.
  20171. (t (setq column (org-get-indentation))))))
  20172. ;; Now apply indentation and move cursor accordingly
  20173. (goto-char pos)
  20174. (if (<= (current-column) (current-indentation))
  20175. (org-indent-line-to column)
  20176. (save-excursion (org-indent-line-to column)))
  20177. ;; Special polishing for properties, see `org-property-format'
  20178. (setq column (current-column))
  20179. (beginning-of-line 1)
  20180. (if (looking-at
  20181. "\\([ \t]*\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  20182. (replace-match (concat (match-string 1)
  20183. (format org-property-format
  20184. (match-string 2) (match-string 3)))
  20185. t t))
  20186. (org-move-to-column column))))
  20187. (defun org-indent-drawer ()
  20188. "Indent the drawer at point."
  20189. (interactive)
  20190. (let ((p (point))
  20191. (e (and (save-excursion (re-search-forward ":END:" nil t))
  20192. (match-end 0)))
  20193. (folded
  20194. (save-excursion
  20195. (end-of-line)
  20196. (when (overlays-at (point))
  20197. (member 'invisible (overlay-properties
  20198. (car (overlays-at (point)))))))))
  20199. (when folded (org-cycle))
  20200. (indent-for-tab-command)
  20201. (while (and (move-beginning-of-line 2) (< (point) e))
  20202. (indent-for-tab-command))
  20203. (goto-char p)
  20204. (when folded (org-cycle)))
  20205. (message "Drawer at point indented"))
  20206. (defun org-indent-block ()
  20207. "Indent the block at point."
  20208. (interactive)
  20209. (let ((p (point))
  20210. (case-fold-search t)
  20211. (e (and (save-excursion (re-search-forward "#\\+end_?\\(?:[a-z]+\\)?" nil t))
  20212. (match-end 0)))
  20213. (folded
  20214. (save-excursion
  20215. (end-of-line)
  20216. (when (overlays-at (point))
  20217. (member 'invisible (overlay-properties
  20218. (car (overlays-at (point)))))))))
  20219. (when folded (org-cycle))
  20220. (indent-for-tab-command)
  20221. (while (and (move-beginning-of-line 2) (< (point) e))
  20222. (indent-for-tab-command))
  20223. (goto-char p)
  20224. (when folded (org-cycle)))
  20225. (message "Block at point indented"))
  20226. (defun org-indent-region (start end)
  20227. "Indent region."
  20228. (interactive "r")
  20229. (save-excursion
  20230. (let ((line-end (org-current-line end)))
  20231. (goto-char start)
  20232. (while (< (org-current-line) line-end)
  20233. (cond ((org-in-src-block-p) (org-src-native-tab-command-maybe))
  20234. (t (call-interactively 'org-indent-line)))
  20235. (move-beginning-of-line 2)))))
  20236. ;;; Filling
  20237. ;; We use our own fill-paragraph and auto-fill functions.
  20238. ;; `org-fill-paragraph' relies on adaptive filling and context
  20239. ;; checking. Appropriate `fill-prefix' is computed with
  20240. ;; `org-adaptive-fill-function'.
  20241. ;; `org-auto-fill-function' takes care of auto-filling. It calls
  20242. ;; `do-auto-fill' only on valid areas with `fill-prefix' shadowed with
  20243. ;; `org-adaptive-fill-function' value. Internally,
  20244. ;; `org-comment-line-break-function' breaks the line.
  20245. ;; `org-setup-filling' installs filling and auto-filling related
  20246. ;; variables during `org-mode' initialization.
  20247. (defun org-setup-filling ()
  20248. (interactive)
  20249. ;; Prevent auto-fill from inserting unwanted new items.
  20250. (when (boundp 'fill-nobreak-predicate)
  20251. (org-set-local
  20252. 'fill-nobreak-predicate
  20253. (org-uniquify
  20254. (append fill-nobreak-predicate
  20255. '(org-fill-paragraph-separate-nobreak-p
  20256. org-fill-line-break-nobreak-p
  20257. org-fill-paragraph-with-timestamp-nobreak-p)))))
  20258. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  20259. (org-set-local 'auto-fill-inhibit-regexp nil)
  20260. (org-set-local 'adaptive-fill-function 'org-adaptive-fill-function)
  20261. (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
  20262. (org-set-local 'comment-line-break-function 'org-comment-line-break-function))
  20263. (defvar org-element-paragraph-separate) ; org-element.el
  20264. (defun org-fill-paragraph-separate-nobreak-p ()
  20265. "Non-nil when a line break at point would insert a new item."
  20266. (looking-at (substring org-element-paragraph-separate 1)))
  20267. (defun org-fill-line-break-nobreak-p ()
  20268. "Non-nil when a line break at point would create an Org line break."
  20269. (save-excursion
  20270. (skip-chars-backward "[ \t]")
  20271. (skip-chars-backward "\\\\")
  20272. (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
  20273. (defun org-fill-paragraph-with-timestamp-nobreak-p ()
  20274. "Non-nil when a line break at point would insert a new item."
  20275. (and (org-at-timestamp-p t)
  20276. (not (looking-at org-ts-regexp-both))))
  20277. (declare-function message-in-body-p "message" ())
  20278. (defvar orgtbl-line-start-regexp) ; From org-table.el
  20279. (defun org-adaptive-fill-function ()
  20280. "Compute a fill prefix for the current line.
  20281. Return fill prefix, as a string, or nil if current line isn't
  20282. meant to be filled."
  20283. (let (prefix)
  20284. (catch 'exit
  20285. (when (derived-mode-p 'message-mode)
  20286. (save-excursion
  20287. (beginning-of-line)
  20288. (cond ((or (not (message-in-body-p))
  20289. (looking-at orgtbl-line-start-regexp))
  20290. (throw 'exit nil))
  20291. ((looking-at message-cite-prefix-regexp)
  20292. (throw 'exit (match-string-no-properties 0)))
  20293. ((looking-at org-outline-regexp)
  20294. (throw 'exit (make-string (length (match-string 0)) ? ))))))
  20295. (org-with-wide-buffer
  20296. (let* ((p (line-beginning-position))
  20297. (element (save-excursion (beginning-of-line)
  20298. (org-element-at-point)))
  20299. (type (org-element-type element))
  20300. (post-affiliated (org-element-property :post-affiliated element)))
  20301. (unless (and post-affiliated (< p post-affiliated))
  20302. (case type
  20303. (comment (looking-at "[ \t]*# ?") (match-string 0))
  20304. (footnote-definition "")
  20305. ((item plain-list)
  20306. (make-string (org-list-item-body-column
  20307. (or post-affiliated
  20308. (org-element-property :begin element)))
  20309. ? ))
  20310. (paragraph
  20311. ;; Fill prefix is usually the same as the current line,
  20312. ;; except if the paragraph is at the beginning of an item.
  20313. (let ((parent (org-element-property :parent element)))
  20314. (cond ((eq (org-element-type parent) 'item)
  20315. (make-string (org-list-item-body-column
  20316. (org-element-property :begin parent))
  20317. ? ))
  20318. ((save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  20319. (match-string 0))
  20320. (t ""))))
  20321. (comment-block
  20322. ;; Only fill contents if P is within block boundaries.
  20323. (let* ((cbeg (save-excursion (goto-char post-affiliated)
  20324. (forward-line)
  20325. (point)))
  20326. (cend (save-excursion
  20327. (goto-char (org-element-property :end element))
  20328. (skip-chars-backward " \r\t\n")
  20329. (line-beginning-position))))
  20330. (when (and (>= p cbeg) (< p cend))
  20331. (if (save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  20332. (match-string 0)
  20333. "")))))))))))
  20334. (declare-function message-goto-body "message" ())
  20335. (defvar message-cite-prefix-regexp) ; From message.el
  20336. (defun org-fill-paragraph (&optional justify)
  20337. "Fill element at point, when applicable.
  20338. This function only applies to comment blocks, comments, example
  20339. blocks and paragraphs. Also, as a special case, re-align table
  20340. when point is at one.
  20341. If JUSTIFY is non-nil (interactively, with prefix argument),
  20342. justify as well. If `sentence-end-double-space' is non-nil, then
  20343. period followed by one space does not end a sentence, so don't
  20344. break a line there. The variable `fill-column' controls the
  20345. width for filling.
  20346. For convenience, when point is at a plain list, an item or
  20347. a footnote definition, try to fill the first paragraph within."
  20348. (interactive)
  20349. (if (and (derived-mode-p 'message-mode)
  20350. (or (not (message-in-body-p))
  20351. (save-excursion (move-beginning-of-line 1)
  20352. (looking-at message-cite-prefix-regexp))))
  20353. ;; First ensure filling is correct in message-mode.
  20354. (let ((fill-paragraph-function
  20355. (cadadr (assoc 'fill-paragraph-function org-fb-vars)))
  20356. (fill-prefix (cadadr (assoc 'fill-prefix org-fb-vars)))
  20357. (paragraph-start (cadadr (assoc 'paragraph-start org-fb-vars)))
  20358. (paragraph-separate
  20359. (cadadr (assoc 'paragraph-separate org-fb-vars))))
  20360. (fill-paragraph nil))
  20361. (with-syntax-table org-mode-transpose-word-syntax-table
  20362. ;; Move to end of line in order to get the first paragraph
  20363. ;; within a plain list or a footnote definition.
  20364. (let ((element (save-excursion (end-of-line) (org-element-at-point))))
  20365. ;; First check if point is in a blank line at the beginning of
  20366. ;; the buffer. In that case, ignore filling.
  20367. (case (org-element-type element)
  20368. ;; Use major mode filling function is src blocks.
  20369. (src-block (org-babel-do-key-sequence-in-edit-buffer (kbd "M-q")))
  20370. ;; Align Org tables, leave table.el tables as-is.
  20371. (table-row (org-table-align) t)
  20372. (table
  20373. (when (eq (org-element-property :type element) 'org)
  20374. (org-table-align))
  20375. t)
  20376. (paragraph
  20377. ;; Paragraphs may contain `line-break' type objects.
  20378. (let ((beg (max (point-min)
  20379. (org-element-property :contents-begin element)))
  20380. (end (min (point-max)
  20381. (org-element-property :contents-end element))))
  20382. ;; Do nothing if point is at an affiliated keyword.
  20383. (if (< (line-end-position) beg) t
  20384. (when (derived-mode-p 'message-mode)
  20385. ;; In `message-mode', do not fill following citation
  20386. ;; in current paragraph nor text before message body.
  20387. (let ((body-start (save-excursion (message-goto-body))))
  20388. (when body-start (setq beg (max body-start beg))))
  20389. (when (save-excursion
  20390. (re-search-forward
  20391. (concat "^" message-cite-prefix-regexp) end t))
  20392. (setq end (match-beginning 0))))
  20393. ;; Fill paragraph, taking line breaks into account.
  20394. ;; For that, slice the paragraph using line breaks as
  20395. ;; separators, and fill the parts in reverse order to
  20396. ;; avoid messing with markers.
  20397. (save-excursion
  20398. (goto-char end)
  20399. (mapc
  20400. (lambda (pos)
  20401. (fill-region-as-paragraph pos (point) justify)
  20402. (goto-char pos))
  20403. ;; Find the list of ending positions for line breaks
  20404. ;; in the current paragraph. Add paragraph
  20405. ;; beginning to include first slice.
  20406. (nreverse
  20407. (cons beg
  20408. (org-element-map
  20409. (org-element--parse-objects
  20410. beg end nil (org-element-restriction 'paragraph))
  20411. 'line-break
  20412. (lambda (lb) (org-element-property :end lb)))))))
  20413. t)))
  20414. ;; Contents of `comment-block' type elements should be
  20415. ;; filled as plain text, but only if point is within block
  20416. ;; markers.
  20417. (comment-block
  20418. (let* ((case-fold-search t)
  20419. (beg (save-excursion
  20420. (goto-char (org-element-property :begin element))
  20421. (re-search-forward "^[ \t]*#\\+begin_comment" nil t)
  20422. (forward-line)
  20423. (point)))
  20424. (end (save-excursion
  20425. (goto-char (org-element-property :end element))
  20426. (re-search-backward "^[ \t]*#\\+end_comment" nil t)
  20427. (line-beginning-position))))
  20428. (when (and (>= (point) beg) (< (point) end))
  20429. (fill-region-as-paragraph
  20430. (save-excursion
  20431. (end-of-line)
  20432. (re-search-backward "^[ \t]*$" beg 'move)
  20433. (line-beginning-position))
  20434. (save-excursion
  20435. (beginning-of-line)
  20436. (re-search-forward "^[ \t]*$" end 'move)
  20437. (line-beginning-position))
  20438. justify)))
  20439. t)
  20440. ;; Fill comments.
  20441. (comment (fill-comment-paragraph justify))
  20442. ;; Ignore every other element.
  20443. (otherwise t))))))
  20444. (defun org-auto-fill-function ()
  20445. "Auto-fill function."
  20446. ;; Check if auto-filling is meaningful.
  20447. (let ((fc (current-fill-column)))
  20448. (when (and fc (> (current-column) fc))
  20449. (let* ((fill-prefix (org-adaptive-fill-function))
  20450. ;; Enforce empty fill prefix, if required. Otherwise, it
  20451. ;; will be computed again.
  20452. (adaptive-fill-mode (not (equal fill-prefix ""))))
  20453. (when fill-prefix (do-auto-fill))))))
  20454. (defun org-comment-line-break-function (&optional soft)
  20455. "Break line at point and indent, continuing comment if within one.
  20456. The inserted newline is marked hard if variable
  20457. `use-hard-newlines' is true, unless optional argument SOFT is
  20458. non-nil."
  20459. (if soft (insert-and-inherit ?\n) (newline 1))
  20460. (save-excursion (forward-char -1) (delete-horizontal-space))
  20461. (delete-horizontal-space)
  20462. (indent-to-left-margin)
  20463. (insert-before-markers-and-inherit fill-prefix))
  20464. ;;; Comments
  20465. ;; Org comments syntax is quite complex. It requires the entire line
  20466. ;; to be just a comment. Also, even with the right syntax at the
  20467. ;; beginning of line, some some elements (i.e. verse-block or
  20468. ;; example-block) don't accept comments. Usual Emacs comment commands
  20469. ;; cannot cope with those requirements. Therefore, Org replaces them.
  20470. ;; Org still relies on `comment-dwim', but cannot trust
  20471. ;; `comment-only-p'. So, `comment-region-function' and
  20472. ;; `uncomment-region-function' both point
  20473. ;; to`org-comment-or-uncomment-region'. Eventually,
  20474. ;; `org-insert-comment' takes care of insertion of comments at the
  20475. ;; beginning of line.
  20476. ;; `org-setup-comments-handling' install comments related variables
  20477. ;; during `org-mode' initialization.
  20478. (defun org-setup-comments-handling ()
  20479. (interactive)
  20480. (org-set-local 'comment-use-syntax nil)
  20481. (org-set-local 'comment-start "# ")
  20482. (org-set-local 'comment-start-skip "^\\s-*#\\(?: \\|$\\)")
  20483. (org-set-local 'comment-insert-comment-function 'org-insert-comment)
  20484. (org-set-local 'comment-region-function 'org-comment-or-uncomment-region)
  20485. (org-set-local 'uncomment-region-function 'org-comment-or-uncomment-region))
  20486. (defun org-insert-comment ()
  20487. "Insert an empty comment above current line.
  20488. If the line is empty, insert comment at its beginning."
  20489. (beginning-of-line)
  20490. (if (looking-at "\\s-*$") (replace-match "") (open-line 1))
  20491. (org-indent-line)
  20492. (insert "# "))
  20493. (defvar comment-empty-lines) ; From newcomment.el.
  20494. (defun org-comment-or-uncomment-region (beg end &rest ignore)
  20495. "Comment or uncomment each non-blank line in the region.
  20496. Uncomment each non-blank line between BEG and END if it only
  20497. contains commented lines. Otherwise, comment them."
  20498. (save-restriction
  20499. ;; Restrict region
  20500. (narrow-to-region (save-excursion (goto-char beg)
  20501. (skip-chars-forward " \r\t\n" end)
  20502. (line-beginning-position))
  20503. (save-excursion (goto-char end)
  20504. (skip-chars-backward " \r\t\n" beg)
  20505. (line-end-position)))
  20506. (let ((uncommentp
  20507. ;; UNCOMMENTP is non-nil when every non blank line between
  20508. ;; BEG and END is a comment.
  20509. (save-excursion
  20510. (goto-char (point-min))
  20511. (while (and (not (eobp))
  20512. (let ((element (org-element-at-point)))
  20513. (and (eq (org-element-type element) 'comment)
  20514. (goto-char (min (point-max)
  20515. (org-element-property
  20516. :end element)))))))
  20517. (eobp))))
  20518. (if uncommentp
  20519. ;; Only blank lines and comments in region: uncomment it.
  20520. (save-excursion
  20521. (goto-char (point-min))
  20522. (while (not (eobp))
  20523. (when (looking-at "[ \t]*\\(#\\(?: \\|$\\)\\)")
  20524. (replace-match "" nil nil nil 1))
  20525. (forward-line)))
  20526. ;; Comment each line in region.
  20527. (let ((min-indent (point-max)))
  20528. ;; First find the minimum indentation across all lines.
  20529. (save-excursion
  20530. (goto-char (point-min))
  20531. (while (and (not (eobp)) (not (zerop min-indent)))
  20532. (unless (looking-at "[ \t]*$")
  20533. (setq min-indent (min min-indent (current-indentation))))
  20534. (forward-line)))
  20535. ;; Then loop over all lines.
  20536. (save-excursion
  20537. (goto-char (point-min))
  20538. (while (not (eobp))
  20539. (unless (and (not comment-empty-lines) (looking-at "[ \t]*$"))
  20540. ;; Don't get fooled by invisible text (e.g. link path)
  20541. ;; when moving to column MIN-INDENT.
  20542. (let ((buffer-invisibility-spec nil))
  20543. (org-move-to-column min-indent t))
  20544. (insert comment-start))
  20545. (forward-line))))))))
  20546. ;;; Planning
  20547. ;; This section contains tools to operate on timestamp objects, as
  20548. ;; returned by, e.g. `org-element-context'.
  20549. (defun org-timestamp-has-time-p (timestamp)
  20550. "Non-nil when TIMESTAMP has a time specified."
  20551. (org-element-property :hour-start timestamp))
  20552. (defun org-timestamp-format (timestamp format &optional end utc)
  20553. "Format a TIMESTAMP element into a string.
  20554. FORMAT is a format specifier to be passed to
  20555. `format-time-string'.
  20556. When optional argument END is non-nil, use end of date-range or
  20557. time-range, if possible.
  20558. When optional argument UTC is non-nil, time will be expressed as
  20559. Universal Time."
  20560. (format-time-string
  20561. format
  20562. (apply 'encode-time
  20563. (cons 0
  20564. (mapcar
  20565. (lambda (prop) (or (org-element-property prop timestamp) 0))
  20566. (if end '(:minute-end :hour-end :day-end :month-end :year-end)
  20567. '(:minute-start :hour-start :day-start :month-start
  20568. :year-start)))))
  20569. utc))
  20570. (defun org-timestamp-split-range (timestamp &optional end)
  20571. "Extract a timestamp object from a date or time range.
  20572. TIMESTAMP is a timestamp object. END, when non-nil, means extract
  20573. the end of the range. Otherwise, extract its start.
  20574. Return a new timestamp object sharing the same parent as
  20575. TIMESTAMP."
  20576. (let ((type (org-element-property :type timestamp)))
  20577. (if (memq type '(active inactive diary)) timestamp
  20578. (let ((split-ts (list 'timestamp (copy-sequence (nth 1 timestamp)))))
  20579. ;; Set new type.
  20580. (org-element-put-property
  20581. split-ts :type (if (eq type 'active-range) 'active 'inactive))
  20582. ;; Copy start properties over end properties if END is
  20583. ;; non-nil. Otherwise, copy end properties over `start' ones.
  20584. (let ((p-alist '((:minute-start . :minute-end)
  20585. (:hour-start . :hour-end)
  20586. (:day-start . :day-end)
  20587. (:month-start . :month-end)
  20588. (:year-start . :year-end))))
  20589. (dolist (p-cell p-alist)
  20590. (org-element-put-property
  20591. split-ts
  20592. (funcall (if end 'car 'cdr) p-cell)
  20593. (org-element-property
  20594. (funcall (if end 'cdr 'car) p-cell) split-ts)))
  20595. ;; Eventually refresh `:raw-value'.
  20596. (org-element-put-property split-ts :raw-value nil)
  20597. (org-element-put-property
  20598. split-ts :raw-value (org-element-interpret-data split-ts)))))))
  20599. (defun org-timestamp-translate (timestamp &optional boundary)
  20600. "Apply `org-translate-time' on a TIMESTAMP object.
  20601. When optional argument BOUNDARY is non-nil, it is either the
  20602. symbol `start' or `end'. In this case, only translate the
  20603. starting or ending part of TIMESTAMP if it is a date or time
  20604. range. Otherwise, translate both parts."
  20605. (if (and (not boundary)
  20606. (memq (org-element-property :type timestamp)
  20607. '(active-range inactive-range)))
  20608. (concat
  20609. (org-translate-time
  20610. (org-element-property :raw-value
  20611. (org-timestamp-split-range timestamp)))
  20612. "--"
  20613. (org-translate-time
  20614. (org-element-property :raw-value
  20615. (org-timestamp-split-range timestamp t))))
  20616. (org-translate-time
  20617. (org-element-property
  20618. :raw-value
  20619. (if (not boundary) timestamp
  20620. (org-timestamp-split-range timestamp (eq boundary 'end)))))))
  20621. ;;; Other stuff.
  20622. (defun org-toggle-fixed-width-section (arg)
  20623. "Toggle the fixed-width export.
  20624. If there is no active region, the QUOTE keyword at the current headline is
  20625. inserted or removed. When present, it causes the text between this headline
  20626. and the next to be exported as fixed-width text, and unmodified.
  20627. If there is an active region, this command adds or removes a colon as the
  20628. first character of this line. If the first character of a line is a colon,
  20629. this line is also exported in fixed-width font."
  20630. (interactive "P")
  20631. (let* ((cc 0)
  20632. (regionp (org-region-active-p))
  20633. (beg (if regionp (region-beginning) (point)))
  20634. (end (if regionp (region-end)))
  20635. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  20636. (case-fold-search nil)
  20637. (re "[ \t]*\\(:\\(?: \\|$\\)\\)")
  20638. off)
  20639. (if regionp
  20640. (save-excursion
  20641. (goto-char beg)
  20642. (setq cc (current-column))
  20643. (beginning-of-line 1)
  20644. (setq off (looking-at re))
  20645. (while (> nlines 0)
  20646. (setq nlines (1- nlines))
  20647. (beginning-of-line 1)
  20648. (cond
  20649. (arg
  20650. (org-move-to-column cc t)
  20651. (insert ": \n")
  20652. (forward-line -1))
  20653. ((and off (looking-at re))
  20654. (replace-match "" t t nil 1))
  20655. ((not off) (org-move-to-column cc t) (insert ": ")))
  20656. (forward-line 1)))
  20657. (save-excursion
  20658. (org-back-to-heading)
  20659. (cond
  20660. ((looking-at (format org-heading-keyword-regexp-format
  20661. org-quote-string))
  20662. (goto-char (match-end 1))
  20663. (looking-at (concat " +" org-quote-string))
  20664. (replace-match "" t t)
  20665. (when (eolp) (insert " ")))
  20666. ((looking-at org-outline-regexp)
  20667. (goto-char (match-end 0))
  20668. (insert org-quote-string " ")))))))
  20669. (defun org-reftex-citation ()
  20670. "Use reftex-citation to insert a citation into the buffer.
  20671. This looks for a line like
  20672. #+BIBLIOGRAPHY: foo plain option:-d
  20673. and derives from it that foo.bib is the bibliography file relevant
  20674. for this document. It then installs the necessary environment for RefTeX
  20675. to work in this buffer and calls `reftex-citation' to insert a citation
  20676. into the buffer.
  20677. Export of such citations to both LaTeX and HTML is handled by the contributed
  20678. package org-exp-bibtex by Taru Karttunen."
  20679. (interactive)
  20680. (let ((reftex-docstruct-symbol 'rds)
  20681. (reftex-cite-format "\\cite{%l}")
  20682. rds bib)
  20683. (save-excursion
  20684. (save-restriction
  20685. (widen)
  20686. (let ((case-fold-search t)
  20687. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  20688. (if (not (save-excursion
  20689. (or (re-search-forward re nil t)
  20690. (re-search-backward re nil t))))
  20691. (error "No bibliography defined in file")
  20692. (setq bib (concat (match-string 1) ".bib")
  20693. rds (list (list 'bib bib)))))))
  20694. (call-interactively 'reftex-citation)))
  20695. ;;;; Functions extending outline functionality
  20696. (defun org-beginning-of-line (&optional arg)
  20697. "Go to the beginning of the current line. If that is invisible, continue
  20698. to a visible line beginning. This makes the function of C-a more intuitive.
  20699. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  20700. first attempt, and only move to after the tags when the cursor is already
  20701. beyond the end of the headline."
  20702. (interactive "P")
  20703. (let ((pos (point))
  20704. (special (if (consp org-special-ctrl-a/e)
  20705. (car org-special-ctrl-a/e)
  20706. org-special-ctrl-a/e))
  20707. refpos)
  20708. (if (org-bound-and-true-p visual-line-mode)
  20709. (beginning-of-visual-line 1)
  20710. (beginning-of-line 1))
  20711. (if (and arg (fboundp 'move-beginning-of-line))
  20712. (call-interactively 'move-beginning-of-line)
  20713. (if (bobp)
  20714. nil
  20715. (backward-char 1)
  20716. (if (org-truely-invisible-p)
  20717. (while (and (not (bobp)) (org-truely-invisible-p))
  20718. (backward-char 1)
  20719. (beginning-of-line 1))
  20720. (forward-char 1))))
  20721. (when special
  20722. (cond
  20723. ((and (looking-at org-complex-heading-regexp)
  20724. (= (char-after (match-end 1)) ?\ ))
  20725. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  20726. (point-at-eol)))
  20727. (goto-char
  20728. (if (eq special t)
  20729. (cond ((> pos refpos) refpos)
  20730. ((= pos (point)) refpos)
  20731. (t (point)))
  20732. (cond ((> pos (point)) (point))
  20733. ((not (eq last-command this-command)) (point))
  20734. (t refpos)))))
  20735. ((org-at-item-p)
  20736. ;; Being at an item and not looking at an the item means point
  20737. ;; was previously moved to beginning of a visual line, which
  20738. ;; doesn't contain the item. Therefore, do nothing special,
  20739. ;; just stay here.
  20740. (when (looking-at org-list-full-item-re)
  20741. ;; Set special position at first white space character after
  20742. ;; bullet, and check-box, if any.
  20743. (let ((after-bullet
  20744. (let ((box (match-end 3)))
  20745. (if (not box) (match-end 1)
  20746. (let ((after (char-after box)))
  20747. (if (and after (= after ? )) (1+ box) box))))))
  20748. ;; Special case: Move point to special position when
  20749. ;; currently after it or at beginning of line.
  20750. (if (eq special t)
  20751. (when (or (> pos after-bullet) (= (point) pos))
  20752. (goto-char after-bullet))
  20753. ;; Reversed case: Move point to special position when
  20754. ;; point was already at beginning of line and command is
  20755. ;; repeated.
  20756. (when (and (= (point) pos) (eq last-command this-command))
  20757. (goto-char after-bullet))))))))
  20758. (org-no-warnings
  20759. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  20760. (defun org-end-of-line (&optional arg)
  20761. "Go to the end of the line.
  20762. If this is a headline, and `org-special-ctrl-a/e' is set, ignore
  20763. tags on the first attempt, and only move to after the tags when
  20764. the cursor is already beyond the end of the headline."
  20765. (interactive "P")
  20766. (let ((special (if (consp org-special-ctrl-a/e) (cdr org-special-ctrl-a/e)
  20767. org-special-ctrl-a/e))
  20768. (move-fun (cond ((org-bound-and-true-p visual-line-mode)
  20769. 'end-of-visual-line)
  20770. ((fboundp 'move-end-of-line) 'move-end-of-line)
  20771. (t 'end-of-line))))
  20772. (if (or (not special) arg) (call-interactively move-fun)
  20773. (let* ((element (save-excursion (beginning-of-line)
  20774. (org-element-at-point)))
  20775. (type (org-element-type element)))
  20776. (cond
  20777. ((memq type '(headline inlinetask))
  20778. (let ((pos (point)))
  20779. (beginning-of-line 1)
  20780. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  20781. (if (eq special t)
  20782. (if (or (< pos (match-beginning 1)) (= pos (match-end 0)))
  20783. (goto-char (match-beginning 1))
  20784. (goto-char (match-end 0)))
  20785. (if (or (< pos (match-end 0))
  20786. (not (eq this-command last-command)))
  20787. (goto-char (match-end 0))
  20788. (goto-char (match-beginning 1))))
  20789. (call-interactively move-fun))))
  20790. ((org-element-property :hiddenp element)
  20791. ;; If element is hidden, `move-end-of-line' would put point
  20792. ;; after it. Use `end-of-line' to stay on current line.
  20793. (call-interactively 'end-of-line))
  20794. (t (call-interactively move-fun)))))
  20795. (org-no-warnings (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  20796. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  20797. (define-key org-mode-map "\C-e" 'org-end-of-line)
  20798. (defun org-backward-sentence (&optional arg)
  20799. "Go to beginning of sentence, or beginning of table field.
  20800. This will call `backward-sentence' or `org-table-beginning-of-field',
  20801. depending on context."
  20802. (interactive "P")
  20803. (cond
  20804. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  20805. (t (call-interactively 'backward-sentence))))
  20806. (defun org-forward-sentence (&optional arg)
  20807. "Go to end of sentence, or end of table field.
  20808. This will call `forward-sentence' or `org-table-end-of-field',
  20809. depending on context."
  20810. (interactive "P")
  20811. (cond
  20812. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  20813. (t (call-interactively 'forward-sentence))))
  20814. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  20815. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  20816. (defun org-kill-line (&optional arg)
  20817. "Kill line, to tags or end of line."
  20818. (interactive "P")
  20819. (cond
  20820. ((or (not org-special-ctrl-k)
  20821. (bolp)
  20822. (not (org-at-heading-p)))
  20823. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  20824. org-ctrl-k-protect-subtree)
  20825. (if (or (eq org-ctrl-k-protect-subtree 'error)
  20826. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  20827. (error "C-k aborted - would kill hidden subtree")))
  20828. (call-interactively
  20829. (if (org-bound-and-true-p visual-line-mode) 'kill-visual-line 'kill-line)))
  20830. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  20831. (kill-region (point) (match-beginning 1))
  20832. (org-set-tags nil t))
  20833. (t (kill-region (point) (point-at-eol)))))
  20834. (define-key org-mode-map "\C-k" 'org-kill-line)
  20835. (defun org-yank (&optional arg)
  20836. "Yank. If the kill is a subtree, treat it specially.
  20837. This command will look at the current kill and check if is a single
  20838. subtree, or a series of subtrees[1]. If it passes the test, and if the
  20839. cursor is at the beginning of a line or after the stars of a currently
  20840. empty headline, then the yank is handled specially. How exactly depends
  20841. on the value of the following variables, both set by default.
  20842. org-yank-folded-subtrees
  20843. When set, the subtree(s) will be folded after insertion, but only
  20844. if doing so would now swallow text after the yanked text.
  20845. org-yank-adjusted-subtrees
  20846. When set, the subtree will be promoted or demoted in order to
  20847. fit into the local outline tree structure, which means that the level
  20848. will be adjusted so that it becomes the smaller one of the two
  20849. *visible* surrounding headings.
  20850. Any prefix to this command will cause `yank' to be called directly with
  20851. no special treatment. In particular, a simple \\[universal-argument] prefix \
  20852. will just
  20853. plainly yank the text as it is.
  20854. \[1] The test checks if the first non-white line is a heading
  20855. and if there are no other headings with fewer stars."
  20856. (interactive "P")
  20857. (org-yank-generic 'yank arg))
  20858. (defun org-yank-generic (command arg)
  20859. "Perform some yank-like command.
  20860. This function implements the behavior described in the `org-yank'
  20861. documentation. However, it has been generalized to work for any
  20862. interactive command with similar behavior."
  20863. ;; pretend to be command COMMAND
  20864. (setq this-command command)
  20865. (if arg
  20866. (call-interactively command)
  20867. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  20868. (and (org-kill-is-subtree-p)
  20869. (or (bolp)
  20870. (and (looking-at "[ \t]*$")
  20871. (string-match
  20872. "\\`\\*+\\'"
  20873. (buffer-substring (point-at-bol) (point)))))))
  20874. swallowp)
  20875. (cond
  20876. ((and subtreep org-yank-folded-subtrees)
  20877. (let ((beg (point))
  20878. end)
  20879. (if (and subtreep org-yank-adjusted-subtrees)
  20880. (org-paste-subtree nil nil 'for-yank)
  20881. (call-interactively command))
  20882. (setq end (point))
  20883. (goto-char beg)
  20884. (when (and (bolp) subtreep
  20885. (not (setq swallowp
  20886. (org-yank-folding-would-swallow-text beg end))))
  20887. (org-with-limited-levels
  20888. (or (looking-at org-outline-regexp)
  20889. (re-search-forward org-outline-regexp-bol end t))
  20890. (while (and (< (point) end) (looking-at org-outline-regexp))
  20891. (hide-subtree)
  20892. (org-cycle-show-empty-lines 'folded)
  20893. (condition-case nil
  20894. (outline-forward-same-level 1)
  20895. (error (goto-char end))))))
  20896. (when swallowp
  20897. (message
  20898. "Inserted text not folded because that would swallow text"))
  20899. (goto-char end)
  20900. (skip-chars-forward " \t\n\r")
  20901. (beginning-of-line 1)
  20902. (push-mark beg 'nomsg)))
  20903. ((and subtreep org-yank-adjusted-subtrees)
  20904. (let ((beg (point-at-bol)))
  20905. (org-paste-subtree nil nil 'for-yank)
  20906. (push-mark beg 'nomsg)))
  20907. (t
  20908. (call-interactively command))))))
  20909. (defun org-yank-folding-would-swallow-text (beg end)
  20910. "Would hide-subtree at BEG swallow any text after END?"
  20911. (let (level)
  20912. (org-with-limited-levels
  20913. (save-excursion
  20914. (goto-char beg)
  20915. (when (or (looking-at org-outline-regexp)
  20916. (re-search-forward org-outline-regexp-bol end t))
  20917. (setq level (org-outline-level)))
  20918. (goto-char end)
  20919. (skip-chars-forward " \t\r\n\v\f")
  20920. (if (or (eobp)
  20921. (and (bolp) (looking-at org-outline-regexp)
  20922. (<= (org-outline-level) level)))
  20923. nil ; Nothing would be swallowed
  20924. t))))) ; something would swallow
  20925. (define-key org-mode-map "\C-y" 'org-yank)
  20926. (defun org-truely-invisible-p ()
  20927. "Check if point is at a character currently not visible.
  20928. This version does not only check the character property, but also
  20929. `visible-mode'."
  20930. ;; Early versions of noutline don't have `outline-invisible-p'.
  20931. (if (org-bound-and-true-p visible-mode)
  20932. nil
  20933. (outline-invisible-p)))
  20934. (defun org-invisible-p2 ()
  20935. "Check if point is at a character currently not visible."
  20936. (save-excursion
  20937. (if (and (eolp) (not (bobp))) (backward-char 1))
  20938. ;; Early versions of noutline don't have `outline-invisible-p'.
  20939. (outline-invisible-p)))
  20940. (defun org-back-to-heading (&optional invisible-ok)
  20941. "Call `outline-back-to-heading', but provide a better error message."
  20942. (condition-case nil
  20943. (outline-back-to-heading invisible-ok)
  20944. (error (error "Before first headline at position %d in buffer %s"
  20945. (point) (current-buffer)))))
  20946. (defun org-before-first-heading-p ()
  20947. "Before first heading?"
  20948. (save-excursion
  20949. (end-of-line)
  20950. (null (re-search-backward org-outline-regexp-bol nil t))))
  20951. (defun org-at-heading-p (&optional ignored)
  20952. (outline-on-heading-p t))
  20953. ;; Compatibility alias with Org versions < 7.8.03
  20954. (defalias 'org-on-heading-p 'org-at-heading-p)
  20955. (defun org-at-comment-p nil
  20956. "Is cursor in a line starting with a # character?"
  20957. (save-excursion
  20958. (beginning-of-line)
  20959. (looking-at "^#")))
  20960. (defun org-at-drawer-p nil
  20961. "Is cursor at a drawer keyword?"
  20962. (save-excursion
  20963. (move-beginning-of-line 1)
  20964. (looking-at org-drawer-regexp)))
  20965. (defun org-at-block-p nil
  20966. "Is cursor at a block keyword?"
  20967. (save-excursion
  20968. (move-beginning-of-line 1)
  20969. (looking-at org-block-regexp)))
  20970. (defun org-point-at-end-of-empty-headline ()
  20971. "If point is at the end of an empty headline, return t, else nil.
  20972. If the heading only contains a TODO keyword, it is still still considered
  20973. empty."
  20974. (and (looking-at "[ \t]*$")
  20975. (when org-todo-line-regexp
  20976. (save-excursion
  20977. (beginning-of-line 1)
  20978. (let ((case-fold-search nil))
  20979. (looking-at org-todo-line-regexp)
  20980. (string= (match-string 3) ""))))))
  20981. (defun org-at-heading-or-item-p ()
  20982. (or (org-at-heading-p) (org-at-item-p)))
  20983. (defun org-at-target-p ()
  20984. (or (org-in-regexp org-radio-target-regexp)
  20985. (org-in-regexp org-target-regexp)))
  20986. ;; Compatibility alias with Org versions < 7.8.03
  20987. (defalias 'org-on-target-p 'org-at-target-p)
  20988. (defun org-up-heading-all (arg)
  20989. "Move to the heading line of which the present line is a subheading.
  20990. This function considers both visible and invisible heading lines.
  20991. With argument, move up ARG levels."
  20992. (if (fboundp 'outline-up-heading-all)
  20993. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  20994. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  20995. (defun org-up-heading-safe ()
  20996. "Move to the heading line of which the present line is a subheading.
  20997. This version will not throw an error. It will return the level of the
  20998. headline found, or nil if no higher level is found.
  20999. Also, this function will be a lot faster than `outline-up-heading',
  21000. because it relies on stars being the outline starters. This can really
  21001. make a significant difference in outlines with very many siblings."
  21002. (let (start-level re)
  21003. (org-back-to-heading t)
  21004. (setq start-level (funcall outline-level))
  21005. (if (equal start-level 1)
  21006. nil
  21007. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  21008. (if (re-search-backward re nil t)
  21009. (funcall outline-level)))))
  21010. (defun org-first-sibling-p ()
  21011. "Is this heading the first child of its parents?"
  21012. (interactive)
  21013. (let ((re org-outline-regexp-bol)
  21014. level l)
  21015. (unless (org-at-heading-p t)
  21016. (error "Not at a heading"))
  21017. (setq level (funcall outline-level))
  21018. (save-excursion
  21019. (if (not (re-search-backward re nil t))
  21020. t
  21021. (setq l (funcall outline-level))
  21022. (< l level)))))
  21023. (defun org-goto-sibling (&optional previous)
  21024. "Goto the next sibling, even if it is invisible.
  21025. When PREVIOUS is set, go to the previous sibling instead. Returns t
  21026. when a sibling was found. When none is found, return nil and don't
  21027. move point."
  21028. (let ((fun (if previous 're-search-backward 're-search-forward))
  21029. (pos (point))
  21030. (re org-outline-regexp-bol)
  21031. level l)
  21032. (when (condition-case nil (org-back-to-heading t) (error nil))
  21033. (setq level (funcall outline-level))
  21034. (catch 'exit
  21035. (or previous (forward-char 1))
  21036. (while (funcall fun re nil t)
  21037. (setq l (funcall outline-level))
  21038. (when (< l level) (goto-char pos) (throw 'exit nil))
  21039. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  21040. (goto-char pos)
  21041. nil))))
  21042. (defun org-show-siblings ()
  21043. "Show all siblings of the current headline."
  21044. (save-excursion
  21045. (while (org-goto-sibling) (org-flag-heading nil)))
  21046. (save-excursion
  21047. (while (org-goto-sibling 'previous)
  21048. (org-flag-heading nil))))
  21049. (defun org-goto-first-child ()
  21050. "Goto the first child, even if it is invisible.
  21051. Return t when a child was found. Otherwise don't move point and
  21052. return nil."
  21053. (let (level (pos (point)) (re org-outline-regexp-bol))
  21054. (when (condition-case nil (org-back-to-heading t) (error nil))
  21055. (setq level (outline-level))
  21056. (forward-char 1)
  21057. (if (and (re-search-forward re nil t) (> (outline-level) level))
  21058. (progn (goto-char (match-beginning 0)) t)
  21059. (goto-char pos) nil))))
  21060. (defun org-show-hidden-entry ()
  21061. "Show an entry where even the heading is hidden."
  21062. (save-excursion
  21063. (org-show-entry)))
  21064. (defun org-flag-heading (flag &optional entry)
  21065. "Flag the current heading. FLAG non-nil means make invisible.
  21066. When ENTRY is non-nil, show the entire entry."
  21067. (save-excursion
  21068. (org-back-to-heading t)
  21069. ;; Check if we should show the entire entry
  21070. (if entry
  21071. (progn
  21072. (org-show-entry)
  21073. (save-excursion
  21074. (and (outline-next-heading)
  21075. (org-flag-heading nil))))
  21076. (outline-flag-region (max (point-min) (1- (point)))
  21077. (save-excursion (outline-end-of-heading) (point))
  21078. flag))))
  21079. (defun org-get-next-sibling ()
  21080. "Move to next heading of the same level, and return point.
  21081. If there is no such heading, return nil.
  21082. This is like outline-next-sibling, but invisible headings are ok."
  21083. (let ((level (funcall outline-level)))
  21084. (outline-next-heading)
  21085. (while (and (not (eobp)) (> (funcall outline-level) level))
  21086. (outline-next-heading))
  21087. (if (or (eobp) (< (funcall outline-level) level))
  21088. nil
  21089. (point))))
  21090. (defun org-get-last-sibling ()
  21091. "Move to previous heading of the same level, and return point.
  21092. If there is no such heading, return nil."
  21093. (let ((opoint (point))
  21094. (level (funcall outline-level)))
  21095. (outline-previous-heading)
  21096. (when (and (/= (point) opoint) (outline-on-heading-p t))
  21097. (while (and (> (funcall outline-level) level)
  21098. (not (bobp)))
  21099. (outline-previous-heading))
  21100. (if (< (funcall outline-level) level)
  21101. nil
  21102. (point)))))
  21103. (defun org-end-of-subtree (&optional invisible-ok to-heading)
  21104. "Goto to the end of a subtree."
  21105. ;; This contains an exact copy of the original function, but it uses
  21106. ;; `org-back-to-heading', to make it work also in invisible
  21107. ;; trees. And is uses an invisible-ok argument.
  21108. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  21109. ;; Furthermore, when used inside Org, finding the end of a large subtree
  21110. ;; with many children and grandchildren etc, this can be much faster
  21111. ;; than the outline version.
  21112. (org-back-to-heading invisible-ok)
  21113. (let ((first t)
  21114. (level (funcall outline-level)))
  21115. (if (and (derived-mode-p 'org-mode) (< level 1000))
  21116. ;; A true heading (not a plain list item), in Org-mode
  21117. ;; This means we can easily find the end by looking
  21118. ;; only for the right number of stars. Using a regexp to do
  21119. ;; this is so much faster than using a Lisp loop.
  21120. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  21121. (forward-char 1)
  21122. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  21123. ;; something else, do it the slow way
  21124. (while (and (not (eobp))
  21125. (or first (> (funcall outline-level) level)))
  21126. (setq first nil)
  21127. (outline-next-heading)))
  21128. (unless to-heading
  21129. (if (memq (preceding-char) '(?\n ?\^M))
  21130. (progn
  21131. ;; Go to end of line before heading
  21132. (forward-char -1)
  21133. (if (memq (preceding-char) '(?\n ?\^M))
  21134. ;; leave blank line before heading
  21135. (forward-char -1))))))
  21136. (point))
  21137. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  21138. "Use Org version in org-mode, for dramatic speed-up."
  21139. (if (derived-mode-p 'org-mode)
  21140. (progn
  21141. (org-end-of-subtree nil t)
  21142. (unless (eobp) (backward-char 1)))
  21143. ad-do-it))
  21144. (defun org-end-of-meta-data-and-drawers ()
  21145. "Jump to the first text after meta data and drawers in the current entry.
  21146. This will move over empty lines, lines with planning time stamps,
  21147. clocking lines, and drawers."
  21148. (org-back-to-heading t)
  21149. (let ((end (save-excursion (outline-next-heading) (point)))
  21150. (re (concat "\\(" org-drawer-regexp "\\)"
  21151. "\\|" "[ \t]*" org-keyword-time-regexp)))
  21152. (forward-line 1)
  21153. (while (re-search-forward re end t)
  21154. (if (not (match-end 1))
  21155. ;; empty or planning line
  21156. (forward-line 1)
  21157. ;; a drawer, find the end
  21158. (re-search-forward "^[ \t]*:END:" end 'move)
  21159. (forward-line 1)))
  21160. (and (re-search-forward "[^\n]" nil t) (backward-char 1))
  21161. (point)))
  21162. (defun org-forward-heading-same-level (arg &optional invisible-ok)
  21163. "Move forward to the ARG'th subheading at same level as this one.
  21164. Stop at the first and last subheadings of a superior heading.
  21165. Normally this only looks at visible headings, but when INVISIBLE-OK is
  21166. non-nil it will also look at invisible ones."
  21167. (interactive "p")
  21168. (if (not (ignore-errors (org-back-to-heading invisible-ok)))
  21169. (if (and arg (< arg 0))
  21170. (goto-char (point-min))
  21171. (outline-next-heading))
  21172. (org-at-heading-p)
  21173. (let ((level (- (match-end 0) (match-beginning 0) 1))
  21174. (f (if (and arg (< arg 0))
  21175. 're-search-backward
  21176. 're-search-forward))
  21177. (count (if arg (abs arg) 1))
  21178. (result (point)))
  21179. (forward-char (if (and arg (< arg 0)) -1 1))
  21180. (while (and (> count 0)
  21181. (funcall f org-outline-regexp-bol nil 'move))
  21182. (let ((l (- (match-end 0) (match-beginning 0) 1)))
  21183. (cond ((< l level) (setq count 0))
  21184. ((and (= l level)
  21185. (or invisible-ok
  21186. (progn
  21187. (goto-char (line-beginning-position))
  21188. (not (outline-invisible-p)))))
  21189. (setq count (1- count))
  21190. (when (eq l level)
  21191. (setq result (point)))))))
  21192. (goto-char result))
  21193. (beginning-of-line 1)))
  21194. (defun org-backward-heading-same-level (arg &optional invisible-ok)
  21195. "Move backward to the ARG'th subheading at same level as this one.
  21196. Stop at the first and last subheadings of a superior heading."
  21197. (interactive "p")
  21198. (org-forward-heading-same-level (if arg (- arg) -1) invisible-ok))
  21199. (defun org-next-block (arg &optional backward block-regexp)
  21200. "Jump to the next block.
  21201. With a prefix argument ARG, jump forward ARG many source blocks.
  21202. When BACKWARD is non-nil, jump to the previous block.
  21203. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  21204. (interactive "p")
  21205. (let ((re (or block-regexp org-block-regexp))
  21206. (re-search-fn (or (and backward 're-search-backward)
  21207. 're-search-forward)))
  21208. (if (looking-at re) (forward-char 1))
  21209. (condition-case nil
  21210. (funcall re-search-fn re nil nil arg)
  21211. (error (error "No %s code blocks" (if backward "previous" "further" ))))
  21212. (goto-char (match-beginning 0)) (org-show-context)))
  21213. (defun org-previous-block (arg &optional block-regexp)
  21214. "Jump to the previous block.
  21215. With a prefix argument ARG, jump backward ARG many source blocks.
  21216. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  21217. (interactive "p")
  21218. (org-next-block arg t block-regexp))
  21219. (defun org-forward-element ()
  21220. "Move forward by one element.
  21221. Move to the next element at the same level, when possible."
  21222. (interactive)
  21223. (cond ((eobp) (user-error "Cannot move further down"))
  21224. ((org-with-limited-levels (org-at-heading-p))
  21225. (let ((origin (point)))
  21226. (goto-char (org-end-of-subtree nil t))
  21227. (unless (org-with-limited-levels (org-at-heading-p))
  21228. (goto-char origin)
  21229. (user-error "Cannot move further down"))))
  21230. (t
  21231. (let* ((elem (org-element-at-point))
  21232. (end (org-element-property :end elem))
  21233. (parent (org-element-property :parent elem)))
  21234. (if (and parent (= (org-element-property :contents-end parent) end))
  21235. (goto-char (org-element-property :end parent))
  21236. (goto-char end))))))
  21237. (defun org-backward-element ()
  21238. "Move backward by one element.
  21239. Move to the previous element at the same level, when possible."
  21240. (interactive)
  21241. (cond ((bobp) (user-error "Cannot move further up"))
  21242. ((org-with-limited-levels (org-at-heading-p))
  21243. ;; At a headline, move to the previous one, if any, or stay
  21244. ;; here.
  21245. (let ((origin (point)))
  21246. (org-with-limited-levels (org-backward-heading-same-level 1))
  21247. ;; When current headline has no sibling above, move to its
  21248. ;; parent.
  21249. (when (= (point) origin)
  21250. (or (org-with-limited-levels (org-up-heading-safe))
  21251. (progn (goto-char origin)
  21252. (user-error "Cannot move further up"))))))
  21253. (t
  21254. (let* ((trail (org-element-at-point 'keep-trail))
  21255. (elem (car trail))
  21256. (prev-elem (nth 1 trail))
  21257. (beg (org-element-property :begin elem)))
  21258. (cond
  21259. ;; Move to beginning of current element if point isn't
  21260. ;; there already.
  21261. ((/= (point) beg) (goto-char beg))
  21262. (prev-elem (goto-char (org-element-property :begin prev-elem)))
  21263. ((org-before-first-heading-p) (goto-char (point-min)))
  21264. (t (org-back-to-heading)))))))
  21265. (defun org-up-element ()
  21266. "Move to upper element."
  21267. (interactive)
  21268. (if (org-with-limited-levels (org-at-heading-p))
  21269. (unless (org-up-heading-safe) (error "No surrounding element"))
  21270. (let* ((elem (org-element-at-point))
  21271. (parent (org-element-property :parent elem)))
  21272. (if parent (goto-char (org-element-property :begin parent))
  21273. (if (org-with-limited-levels (org-before-first-heading-p))
  21274. (error "No surrounding element")
  21275. (org-with-limited-levels (org-back-to-heading)))))))
  21276. (defvar org-element-greater-elements)
  21277. (defun org-down-element ()
  21278. "Move to inner element."
  21279. (interactive)
  21280. (let ((element (org-element-at-point)))
  21281. (cond
  21282. ((memq (org-element-type element) '(plain-list table))
  21283. (goto-char (org-element-property :contents-begin element))
  21284. (forward-char))
  21285. ((memq (org-element-type element) org-element-greater-elements)
  21286. ;; If contents are hidden, first disclose them.
  21287. (when (org-element-property :hiddenp element) (org-cycle))
  21288. (goto-char (or (org-element-property :contents-begin element)
  21289. (error "No content for this element"))))
  21290. (t (error "No inner element")))))
  21291. (defun org-drag-element-backward ()
  21292. "Move backward element at point."
  21293. (interactive)
  21294. (if (org-with-limited-levels (org-at-heading-p)) (org-move-subtree-up)
  21295. (let* ((trail (org-element-at-point 'keep-trail))
  21296. (elem (car trail))
  21297. (prev-elem (nth 1 trail)))
  21298. ;; Error out if no previous element or previous element is
  21299. ;; a parent of the current one.
  21300. (if (or (not prev-elem) (org-element-nested-p elem prev-elem))
  21301. (error "Cannot drag element backward")
  21302. (let ((pos (point)))
  21303. (org-element-swap-A-B prev-elem elem)
  21304. (goto-char (+ (org-element-property :begin prev-elem)
  21305. (- pos (org-element-property :begin elem)))))))))
  21306. (defun org-drag-element-forward ()
  21307. "Move forward element at point."
  21308. (interactive)
  21309. (let* ((pos (point))
  21310. (elem (org-element-at-point)))
  21311. (when (= (point-max) (org-element-property :end elem))
  21312. (error "Cannot drag element forward"))
  21313. (goto-char (org-element-property :end elem))
  21314. (let ((next-elem (org-element-at-point)))
  21315. (when (or (org-element-nested-p elem next-elem)
  21316. (and (eq (org-element-type next-elem) 'headline)
  21317. (not (eq (org-element-type elem) 'headline))))
  21318. (goto-char pos)
  21319. (error "Cannot drag element forward"))
  21320. ;; Compute new position of point: it's shifted by NEXT-ELEM
  21321. ;; body's length (without final blanks) and by the length of
  21322. ;; blanks between ELEM and NEXT-ELEM.
  21323. (let ((size-next (- (save-excursion
  21324. (goto-char (org-element-property :end next-elem))
  21325. (skip-chars-backward " \r\t\n")
  21326. (forward-line)
  21327. ;; Small correction if buffer doesn't end
  21328. ;; with a newline character.
  21329. (if (and (eolp) (not (bolp))) (1+ (point)) (point)))
  21330. (org-element-property :begin next-elem)))
  21331. (size-blank (- (org-element-property :end elem)
  21332. (save-excursion
  21333. (goto-char (org-element-property :end elem))
  21334. (skip-chars-backward " \r\t\n")
  21335. (forward-line)
  21336. (point)))))
  21337. (org-element-swap-A-B elem next-elem)
  21338. (goto-char (+ pos size-next size-blank))))))
  21339. (defun org-drag-line-forward (arg)
  21340. "Drag the line at point ARG lines forward."
  21341. (interactive "p")
  21342. (dotimes (n (abs arg))
  21343. (let ((c (current-column)))
  21344. (if (< 0 arg)
  21345. (progn
  21346. (beginning-of-line 2)
  21347. (transpose-lines 1)
  21348. (beginning-of-line 0))
  21349. (transpose-lines 1)
  21350. (beginning-of-line -1))
  21351. (org-move-to-column c))))
  21352. (defun org-drag-line-backward (arg)
  21353. "Drag the line at point ARG lines backward."
  21354. (interactive "p")
  21355. (org-drag-line-forward (- arg)))
  21356. (defun org-mark-element ()
  21357. "Put point at beginning of this element, mark at end.
  21358. Interactively, if this command is repeated or (in Transient Mark
  21359. mode) if the mark is active, it marks the next element after the
  21360. ones already marked."
  21361. (interactive)
  21362. (let (deactivate-mark)
  21363. (if (and (org-called-interactively-p 'any)
  21364. (or (and (eq last-command this-command) (mark t))
  21365. (and transient-mark-mode mark-active)))
  21366. (set-mark
  21367. (save-excursion
  21368. (goto-char (mark))
  21369. (goto-char (org-element-property :end (org-element-at-point)))))
  21370. (let ((element (org-element-at-point)))
  21371. (end-of-line)
  21372. (push-mark (org-element-property :end element) t t)
  21373. (goto-char (org-element-property :begin element))))))
  21374. (defun org-narrow-to-element ()
  21375. "Narrow buffer to current element."
  21376. (interactive)
  21377. (let ((elem (org-element-at-point)))
  21378. (cond
  21379. ((eq (car elem) 'headline)
  21380. (narrow-to-region
  21381. (org-element-property :begin elem)
  21382. (org-element-property :end elem)))
  21383. ((memq (car elem) org-element-greater-elements)
  21384. (narrow-to-region
  21385. (org-element-property :contents-begin elem)
  21386. (org-element-property :contents-end elem)))
  21387. (t
  21388. (narrow-to-region
  21389. (org-element-property :begin elem)
  21390. (org-element-property :end elem))))))
  21391. (defun org-transpose-element ()
  21392. "Transpose current and previous elements, keeping blank lines between.
  21393. Point is moved after both elements."
  21394. (interactive)
  21395. (org-skip-whitespace)
  21396. (let ((end (org-element-property :end (org-element-at-point))))
  21397. (org-drag-element-backward)
  21398. (goto-char end)))
  21399. (defun org-unindent-buffer ()
  21400. "Un-indent the visible part of the buffer.
  21401. Relative indentation (between items, inside blocks, etc.) isn't
  21402. modified."
  21403. (interactive)
  21404. (unless (eq major-mode 'org-mode)
  21405. (error "Cannot un-indent a buffer not in Org mode"))
  21406. (let* ((parse-tree (org-element-parse-buffer 'greater-element))
  21407. unindent-tree ; For byte-compiler.
  21408. (unindent-tree
  21409. (function
  21410. (lambda (contents)
  21411. (mapc
  21412. (lambda (element)
  21413. (if (memq (org-element-type element) '(headline section))
  21414. (funcall unindent-tree (org-element-contents element))
  21415. (save-excursion
  21416. (save-restriction
  21417. (narrow-to-region
  21418. (org-element-property :begin element)
  21419. (org-element-property :end element))
  21420. (org-do-remove-indentation)))))
  21421. (reverse contents))))))
  21422. (funcall unindent-tree (org-element-contents parse-tree))))
  21423. (defun org-show-subtree ()
  21424. "Show everything after this heading at deeper levels."
  21425. (interactive)
  21426. (outline-flag-region
  21427. (point)
  21428. (save-excursion
  21429. (org-end-of-subtree t t))
  21430. nil))
  21431. (defun org-show-entry ()
  21432. "Show the body directly following this heading.
  21433. Show the heading too, if it is currently invisible."
  21434. (interactive)
  21435. (save-excursion
  21436. (condition-case nil
  21437. (progn
  21438. (org-back-to-heading t)
  21439. (outline-flag-region
  21440. (max (point-min) (1- (point)))
  21441. (save-excursion
  21442. (if (re-search-forward
  21443. (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
  21444. (match-beginning 1)
  21445. (point-max)))
  21446. nil)
  21447. (org-cycle-hide-drawers 'children))
  21448. (error nil))))
  21449. (defun org-make-options-regexp (kwds &optional extra)
  21450. "Make a regular expression for keyword lines."
  21451. (concat
  21452. "^#\\+\\("
  21453. (mapconcat 'regexp-quote kwds "\\|")
  21454. (if extra (concat "\\|" extra))
  21455. "\\):[ \t]*\\(.*\\)"))
  21456. ;; Make isearch reveal the necessary context
  21457. (defun org-isearch-end ()
  21458. "Reveal context after isearch exits."
  21459. (when isearch-success ; only if search was successful
  21460. (if (featurep 'xemacs)
  21461. ;; Under XEmacs, the hook is run in the correct place,
  21462. ;; we directly show the context.
  21463. (org-show-context 'isearch)
  21464. ;; In Emacs the hook runs *before* restoring the overlays.
  21465. ;; So we have to use a one-time post-command-hook to do this.
  21466. ;; (Emacs 22 has a special variable, see function `org-mode')
  21467. (unless (and (boundp 'isearch-mode-end-hook-quit)
  21468. isearch-mode-end-hook-quit)
  21469. ;; Only when the isearch was not quitted.
  21470. (org-add-hook 'post-command-hook 'org-isearch-post-command
  21471. 'append 'local)))
  21472. (org-fix-ellipsis-at-bol)))
  21473. (defun org-isearch-post-command ()
  21474. "Remove self from hook, and show context."
  21475. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  21476. (org-show-context 'isearch))
  21477. ;;;; Integration with and fixes for other packages
  21478. ;;; Imenu support
  21479. (defvar org-imenu-markers nil
  21480. "All markers currently used by Imenu.")
  21481. (make-variable-buffer-local 'org-imenu-markers)
  21482. (defun org-imenu-new-marker (&optional pos)
  21483. "Return a new marker for use by Imenu, and remember the marker."
  21484. (let ((m (make-marker)))
  21485. (move-marker m (or pos (point)))
  21486. (push m org-imenu-markers)
  21487. m))
  21488. (defun org-imenu-get-tree ()
  21489. "Produce the index for Imenu."
  21490. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  21491. (setq org-imenu-markers nil)
  21492. (let* ((n org-imenu-depth)
  21493. (re (concat "^" (org-get-limited-outline-regexp)))
  21494. (subs (make-vector (1+ n) nil))
  21495. (last-level 0)
  21496. m level head0 head)
  21497. (save-excursion
  21498. (save-restriction
  21499. (widen)
  21500. (goto-char (point-max))
  21501. (while (re-search-backward re nil t)
  21502. (setq level (org-reduced-level (funcall outline-level)))
  21503. (when (and (<= level n)
  21504. (looking-at org-complex-heading-regexp)
  21505. (setq head0 (org-match-string-no-properties 4)))
  21506. (setq head (org-link-display-format head0)
  21507. m (org-imenu-new-marker))
  21508. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  21509. (if (>= level last-level)
  21510. (push (cons head m) (aref subs level))
  21511. (push (cons head (aref subs (1+ level))) (aref subs level))
  21512. (loop for i from (1+ level) to n do (aset subs i nil)))
  21513. (setq last-level level)))))
  21514. (aref subs 1)))
  21515. (eval-after-load "imenu"
  21516. '(progn
  21517. (add-hook 'imenu-after-jump-hook
  21518. (lambda ()
  21519. (if (derived-mode-p 'org-mode)
  21520. (org-show-context 'org-goto))))))
  21521. (defun org-link-display-format (link)
  21522. "Replace a link with its the description.
  21523. If there is no description, use the link target."
  21524. (save-match-data
  21525. (if (string-match org-bracket-link-analytic-regexp link)
  21526. (replace-match (if (match-end 5)
  21527. (match-string 5 link)
  21528. (concat (match-string 1 link)
  21529. (match-string 3 link)))
  21530. nil t link)
  21531. link)))
  21532. (defun org-toggle-link-display ()
  21533. "Toggle the literal or descriptive display of links."
  21534. (interactive)
  21535. (if org-descriptive-links
  21536. (progn (org-remove-from-invisibility-spec '(org-link))
  21537. (org-restart-font-lock)
  21538. (setq org-descriptive-links nil))
  21539. (progn (add-to-invisibility-spec '(org-link))
  21540. (org-restart-font-lock)
  21541. (setq org-descriptive-links t))))
  21542. ;; Speedbar support
  21543. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  21544. "Overlay marking the agenda restriction line in speedbar.")
  21545. (overlay-put org-speedbar-restriction-lock-overlay
  21546. 'face 'org-agenda-restriction-lock)
  21547. (overlay-put org-speedbar-restriction-lock-overlay
  21548. 'help-echo "Agendas are currently limited to this item.")
  21549. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  21550. (defun org-speedbar-set-agenda-restriction ()
  21551. "Restrict future agenda commands to the location at point in speedbar.
  21552. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  21553. (interactive)
  21554. (require 'org-agenda)
  21555. (let (p m tp np dir txt)
  21556. (cond
  21557. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  21558. 'org-imenu t))
  21559. (setq m (get-text-property p 'org-imenu-marker))
  21560. (with-current-buffer (marker-buffer m)
  21561. (goto-char m)
  21562. (org-agenda-set-restriction-lock 'subtree)))
  21563. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  21564. 'speedbar-function 'speedbar-find-file))
  21565. (setq tp (previous-single-property-change
  21566. (1+ p) 'speedbar-function)
  21567. np (next-single-property-change
  21568. tp 'speedbar-function)
  21569. dir (speedbar-line-directory)
  21570. txt (buffer-substring-no-properties (or tp (point-min))
  21571. (or np (point-max))))
  21572. (with-current-buffer (find-file-noselect
  21573. (let ((default-directory dir))
  21574. (expand-file-name txt)))
  21575. (unless (derived-mode-p 'org-mode)
  21576. (error "Cannot restrict to non-Org-mode file"))
  21577. (org-agenda-set-restriction-lock 'file)))
  21578. (t (error "Don't know how to restrict Org-mode's agenda")))
  21579. (move-overlay org-speedbar-restriction-lock-overlay
  21580. (point-at-bol) (point-at-eol))
  21581. (setq current-prefix-arg nil)
  21582. (org-agenda-maybe-redo)))
  21583. (eval-after-load "speedbar"
  21584. '(progn
  21585. (speedbar-add-supported-extension ".org")
  21586. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  21587. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  21588. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  21589. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  21590. (add-hook 'speedbar-visiting-tag-hook
  21591. (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
  21592. ;;; Fixes and Hacks for problems with other packages
  21593. ;; Make flyspell not check words in links, to not mess up our keymap
  21594. (defvar org-element-affiliated-keywords) ; From org-element.el
  21595. (defvar org-element-block-name-alist) ; From org-element.el
  21596. (defun org-mode-flyspell-verify ()
  21597. "Don't let flyspell put overlays at active buttons, or on
  21598. {todo,all-time,additional-option-like}-keywords."
  21599. (require 'org-element) ; For `org-element-affiliated-keywords'
  21600. (let ((pos (max (1- (point)) (point-min)))
  21601. (word (thing-at-point 'word)))
  21602. (and (not (get-text-property pos 'keymap))
  21603. (not (get-text-property pos 'org-no-flyspell))
  21604. (not (member word org-todo-keywords-1))
  21605. (not (member word org-all-time-keywords))
  21606. (not (member word org-options-keywords))
  21607. (not (member word (mapcar 'car org-startup-options)))
  21608. (not (member-ignore-case word org-element-affiliated-keywords))
  21609. (not (member-ignore-case word (org-get-export-keywords)))
  21610. (not (member-ignore-case
  21611. word (mapcar 'car org-element-block-name-alist)))
  21612. (not (member-ignore-case word '("BEGIN" "END" "ATTR"))))))
  21613. (defun org-remove-flyspell-overlays-in (beg end)
  21614. "Remove flyspell overlays in region."
  21615. (and (org-bound-and-true-p flyspell-mode)
  21616. (fboundp 'flyspell-delete-region-overlays)
  21617. (flyspell-delete-region-overlays beg end))
  21618. (add-text-properties beg end '(org-no-flyspell t)))
  21619. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  21620. (eval-after-load "bookmark"
  21621. '(if (boundp 'bookmark-after-jump-hook)
  21622. ;; We can use the hook
  21623. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  21624. ;; Hook not available, use advice
  21625. (defadvice bookmark-jump (after org-make-visible activate)
  21626. "Make the position visible."
  21627. (org-bookmark-jump-unhide))))
  21628. ;; Make sure saveplace shows the location if it was hidden
  21629. (eval-after-load "saveplace"
  21630. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  21631. "Make the position visible."
  21632. (org-bookmark-jump-unhide)))
  21633. ;; Make sure ecb shows the location if it was hidden
  21634. (eval-after-load "ecb"
  21635. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  21636. "Make hierarchy visible when jumping into location from ECB tree buffer."
  21637. (if (derived-mode-p 'org-mode)
  21638. (org-show-context))))
  21639. (defun org-bookmark-jump-unhide ()
  21640. "Unhide the current position, to show the bookmark location."
  21641. (and (derived-mode-p 'org-mode)
  21642. (or (outline-invisible-p)
  21643. (save-excursion (goto-char (max (point-min) (1- (point))))
  21644. (outline-invisible-p)))
  21645. (org-show-context 'bookmark-jump)))
  21646. ;; Make session.el ignore our circular variable
  21647. (eval-after-load "session"
  21648. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  21649. ;;;; Finish up
  21650. (provide 'org)
  21651. (run-hooks 'org-load-hook)
  21652. ;;; org.el ends here