org.el 942 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680216812168221683216842168521686216872168821689216902169121692216932169421695216962169721698216992170021701217022170321704217052170621707217082170921710217112171221713217142171521716217172171821719217202172121722217232172421725217262172721728217292173021731217322173321734217352173621737217382173921740217412174221743217442174521746217472174821749217502175121752217532175421755217562175721758217592176021761217622176321764217652176621767217682176921770217712177221773217742177521776217772177821779217802178121782217832178421785217862178721788217892179021791217922179321794217952179621797217982179921800218012180221803218042180521806218072180821809218102181121812218132181421815218162181721818218192182021821218222182321824218252182621827218282182921830218312183221833218342183521836218372183821839218402184121842218432184421845218462184721848218492185021851218522185321854218552185621857218582185921860218612186221863218642186521866218672186821869218702187121872218732187421875218762187721878218792188021881218822188321884218852188621887218882188921890218912189221893218942189521896218972189821899219002190121902219032190421905219062190721908219092191021911219122191321914219152191621917219182191921920219212192221923219242192521926219272192821929219302193121932219332193421935219362193721938219392194021941219422194321944219452194621947219482194921950219512195221953219542195521956219572195821959219602196121962219632196421965219662196721968219692197021971219722197321974219752197621977219782197921980219812198221983219842198521986219872198821989219902199121992219932199421995219962199721998219992200022001220022200322004220052200622007220082200922010220112201222013220142201522016220172201822019220202202122022220232202422025220262202722028220292203022031220322203322034220352203622037220382203922040220412204222043220442204522046220472204822049220502205122052220532205422055220562205722058220592206022061220622206322064220652206622067220682206922070220712207222073220742207522076220772207822079220802208122082220832208422085220862208722088220892209022091220922209322094220952209622097220982209922100221012210222103221042210522106221072210822109221102211122112221132211422115221162211722118221192212022121221222212322124221252212622127221282212922130221312213222133221342213522136221372213822139221402214122142221432214422145221462214722148221492215022151221522215322154221552215622157221582215922160221612216222163221642216522166221672216822169221702217122172221732217422175221762217722178221792218022181221822218322184221852218622187221882218922190221912219222193221942219522196221972219822199222002220122202222032220422205222062220722208222092221022211222122221322214222152221622217222182221922220222212222222223222242222522226222272222822229222302223122232222332223422235222362223722238222392224022241222422224322244222452224622247222482224922250222512225222253222542225522256222572225822259222602226122262222632226422265222662226722268222692227022271222722227322274222752227622277222782227922280222812228222283222842228522286222872228822289222902229122292222932229422295222962229722298222992230022301223022230322304223052230622307223082230922310223112231222313223142231522316223172231822319223202232122322223232232422325223262232722328223292233022331223322233322334223352233622337223382233922340223412234222343223442234522346223472234822349223502235122352223532235422355223562235722358223592236022361223622236322364223652236622367223682236922370223712237222373223742237522376223772237822379223802238122382223832238422385223862238722388223892239022391223922239322394223952239622397223982239922400224012240222403224042240522406224072240822409224102241122412224132241422415224162241722418224192242022421224222242322424224252242622427224282242922430224312243222433224342243522436224372243822439224402244122442224432244422445224462244722448224492245022451224522245322454224552245622457224582245922460224612246222463224642246522466224672246822469224702247122472224732247422475224762247722478224792248022481224822248322484224852248622487224882248922490224912249222493224942249522496224972249822499225002250122502225032250422505225062250722508225092251022511225122251322514225152251622517225182251922520225212252222523225242252522526225272252822529225302253122532225332253422535225362253722538225392254022541225422254322544225452254622547225482254922550225512255222553225542255522556225572255822559225602256122562225632256422565225662256722568225692257022571225722257322574225752257622577225782257922580225812258222583225842258522586225872258822589225902259122592225932259422595225962259722598225992260022601226022260322604226052260622607226082260922610226112261222613226142261522616226172261822619226202262122622226232262422625226262262722628226292263022631226322263322634226352263622637226382263922640226412264222643226442264522646226472264822649226502265122652226532265422655226562265722658226592266022661226622266322664226652266622667226682266922670226712267222673226742267522676226772267822679226802268122682226832268422685226862268722688226892269022691226922269322694226952269622697226982269922700227012270222703227042270522706227072270822709227102271122712227132271422715227162271722718227192272022721227222272322724227252272622727227282272922730227312273222733227342273522736227372273822739227402274122742227432274422745227462274722748227492275022751227522275322754227552275622757227582275922760227612276222763227642276522766227672276822769227702277122772227732277422775227762277722778227792278022781227822278322784227852278622787227882278922790227912279222793227942279522796227972279822799228002280122802228032280422805228062280722808228092281022811228122281322814228152281622817228182281922820228212282222823228242282522826228272282822829228302283122832228332283422835228362283722838228392284022841228422284322844228452284622847228482284922850228512285222853228542285522856228572285822859228602286122862228632286422865228662286722868228692287022871228722287322874228752287622877228782287922880228812288222883228842288522886228872288822889228902289122892228932289422895228962289722898228992290022901229022290322904229052290622907229082290922910229112291222913229142291522916229172291822919229202292122922229232292422925229262292722928229292293022931229322293322934229352293622937229382293922940229412294222943229442294522946229472294822949229502295122952229532295422955229562295722958229592296022961229622296322964229652296622967229682296922970229712297222973229742297522976229772297822979229802298122982229832298422985229862298722988229892299022991229922299322994229952299622997229982299923000230012300223003230042300523006230072300823009230102301123012230132301423015230162301723018230192302023021230222302323024230252302623027230282302923030230312303223033230342303523036230372303823039230402304123042230432304423045230462304723048230492305023051230522305323054230552305623057230582305923060230612306223063230642306523066230672306823069230702307123072230732307423075230762307723078230792308023081230822308323084230852308623087230882308923090230912309223093230942309523096230972309823099231002310123102231032310423105231062310723108231092311023111231122311323114231152311623117231182311923120231212312223123231242312523126231272312823129231302313123132231332313423135231362313723138231392314023141231422314323144231452314623147231482314923150231512315223153231542315523156231572315823159231602316123162231632316423165231662316723168231692317023171231722317323174231752317623177231782317923180231812318223183231842318523186231872318823189231902319123192231932319423195231962319723198231992320023201232022320323204232052320623207232082320923210232112321223213232142321523216232172321823219232202322123222232232322423225232262322723228232292323023231232322323323234232352323623237232382323923240232412324223243232442324523246232472324823249232502325123252232532325423255232562325723258232592326023261232622326323264232652326623267232682326923270232712327223273232742327523276232772327823279232802328123282232832328423285232862328723288232892329023291232922329323294232952329623297232982329923300233012330223303233042330523306233072330823309233102331123312233132331423315233162331723318233192332023321233222332323324233252332623327233282332923330233312333223333233342333523336233372333823339233402334123342233432334423345233462334723348233492335023351233522335323354233552335623357233582335923360233612336223363233642336523366233672336823369233702337123372233732337423375233762337723378233792338023381233822338323384233852338623387233882338923390233912339223393233942339523396233972339823399234002340123402234032340423405234062340723408234092341023411234122341323414234152341623417234182341923420234212342223423234242342523426234272342823429234302343123432234332343423435234362343723438234392344023441234422344323444234452344623447234482344923450234512345223453234542345523456234572345823459234602346123462234632346423465234662346723468234692347023471234722347323474234752347623477234782347923480234812348223483234842348523486234872348823489234902349123492234932349423495234962349723498234992350023501235022350323504235052350623507235082350923510235112351223513235142351523516235172351823519235202352123522235232352423525235262352723528235292353023531235322353323534235352353623537235382353923540235412354223543235442354523546235472354823549235502355123552235532355423555235562355723558235592356023561235622356323564235652356623567235682356923570235712357223573235742357523576235772357823579235802358123582235832358423585235862358723588235892359023591235922359323594235952359623597235982359923600236012360223603236042360523606236072360823609236102361123612236132361423615236162361723618236192362023621236222362323624236252362623627236282362923630236312363223633236342363523636236372363823639236402364123642236432364423645236462364723648236492365023651236522365323654236552365623657236582365923660236612366223663236642366523666236672366823669236702367123672236732367423675236762367723678236792368023681236822368323684236852368623687236882368923690236912369223693236942369523696236972369823699237002370123702237032370423705237062370723708237092371023711237122371323714237152371623717237182371923720237212372223723237242372523726237272372823729237302373123732237332373423735237362373723738237392374023741237422374323744237452374623747237482374923750237512375223753237542375523756237572375823759237602376123762237632376423765237662376723768237692377023771237722377323774237752377623777237782377923780237812378223783237842378523786237872378823789237902379123792237932379423795237962379723798237992380023801238022380323804238052380623807238082380923810238112381223813238142381523816238172381823819238202382123822238232382423825238262382723828238292383023831238322383323834238352383623837238382383923840238412384223843238442384523846238472384823849238502385123852238532385423855238562385723858238592386023861238622386323864238652386623867238682386923870238712387223873238742387523876238772387823879238802388123882238832388423885238862388723888238892389023891238922389323894238952389623897238982389923900239012390223903239042390523906239072390823909239102391123912239132391423915239162391723918239192392023921239222392323924239252392623927239282392923930239312393223933239342393523936239372393823939239402394123942239432394423945239462394723948239492395023951239522395323954239552395623957239582395923960239612396223963239642396523966239672396823969239702397123972239732397423975239762397723978239792398023981239822398323984239852398623987239882398923990239912399223993239942399523996239972399823999240002400124002240032400424005240062400724008240092401024011240122401324014240152401624017240182401924020240212402224023240242402524026240272402824029240302403124032240332403424035240362403724038240392404024041240422404324044240452404624047240482404924050240512405224053240542405524056240572405824059240602406124062240632406424065240662406724068240692407024071240722407324074240752407624077240782407924080240812408224083240842408524086240872408824089240902409124092240932409424095240962409724098240992410024101241022410324104241052410624107241082410924110241112411224113241142411524116241172411824119241202412124122241232412424125241262412724128241292413024131241322413324134241352413624137241382413924140241412414224143241442414524146241472414824149241502415124152241532415424155241562415724158241592416024161241622416324164241652416624167241682416924170241712417224173241742417524176241772417824179241802418124182241832418424185241862418724188241892419024191241922419324194241952419624197241982419924200242012420224203242042420524206242072420824209242102421124212242132421424215242162421724218242192422024221242222422324224242252422624227242282422924230242312423224233242342423524236242372423824239242402424124242242432424424245242462424724248242492425024251242522425324254242552425624257242582425924260242612426224263242642426524266242672426824269242702427124272242732427424275242762427724278242792428024281242822428324284242852428624287242882428924290242912429224293242942429524296242972429824299243002430124302243032430424305243062430724308243092431024311243122431324314243152431624317243182431924320243212432224323243242432524326243272432824329243302433124332243332433424335243362433724338243392434024341243422434324344243452434624347243482434924350243512435224353243542435524356243572435824359243602436124362243632436424365243662436724368243692437024371243722437324374243752437624377243782437924380243812438224383243842438524386243872438824389243902439124392243932439424395243962439724398243992440024401244022440324404244052440624407244082440924410244112441224413244142441524416244172441824419244202442124422244232442424425244262442724428244292443024431244322443324434244352443624437244382443924440244412444224443244442444524446244472444824449244502445124452244532445424455244562445724458244592446024461244622446324464244652446624467244682446924470244712447224473244742447524476244772447824479244802448124482244832448424485244862448724488244892449024491244922449324494244952449624497244982449924500245012450224503245042450524506245072450824509245102451124512245132451424515245162451724518245192452024521245222452324524245252452624527245282452924530245312453224533245342453524536245372453824539245402454124542245432454424545245462454724548245492455024551245522455324554245552455624557245582455924560245612456224563245642456524566245672456824569245702457124572245732457424575245762457724578245792458024581245822458324584245852458624587245882458924590245912459224593245942459524596245972459824599246002460124602246032460424605246062460724608246092461024611246122461324614246152461624617246182461924620246212462224623246242462524626246272462824629246302463124632246332463424635246362463724638246392464024641246422464324644246452464624647246482464924650246512465224653246542465524656246572465824659246602466124662246632466424665246662466724668246692467024671246722467324674246752467624677246782467924680246812468224683246842468524686246872468824689246902469124692246932469424695246962469724698246992470024701247022470324704247052470624707247082470924710247112471224713247142471524716247172471824719247202472124722247232472424725247262472724728247292473024731247322473324734247352473624737247382473924740247412474224743247442474524746247472474824749247502475124752247532475424755247562475724758247592476024761247622476324764247652476624767247682476924770247712477224773247742477524776247772477824779247802478124782247832478424785247862478724788247892479024791247922479324794247952479624797247982479924800248012480224803248042480524806248072480824809248102481124812248132481424815248162481724818248192482024821248222482324824248252482624827248282482924830248312483224833248342483524836248372483824839
  1. ;;; org.el --- Outline-based notes management and organizer
  2. ;; Carstens outline-mode for keeping track of everything.
  3. ;; Copyright (C) 2004-2014 Free Software Foundation, Inc.
  4. ;;
  5. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  6. ;; Maintainer: Carsten Dominik <carsten at orgmode 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. ;;; Commentary:
  24. ;;
  25. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  26. ;; project planning with a fast and effective plain-text system.
  27. ;;
  28. ;; Org-mode develops organizational tasks around NOTES files that contain
  29. ;; information about projects as plain text. Org-mode is implemented on
  30. ;; top of outline-mode, which makes it possible to keep the content of
  31. ;; large files well structured. Visibility cycling and structure editing
  32. ;; help to work with the tree. Tables are easily created with a built-in
  33. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  34. ;; and scheduling. It dynamically compiles entries into an agenda that
  35. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  36. ;; Plain text URL-like links connect to websites, emails, Usenet
  37. ;; messages, BBDB entries, and any files related to the projects. For
  38. ;; printing and sharing of notes, an Org-mode file can be exported as a
  39. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  40. ;; iCalendar file. It can also serve as a publishing tool for a set of
  41. ;; linked webpages.
  42. ;;
  43. ;; Installation and Activation
  44. ;; ---------------------------
  45. ;; See the corresponding sections in the manual at
  46. ;;
  47. ;; http://orgmode.org/org.html#Installation
  48. ;;
  49. ;; Documentation
  50. ;; -------------
  51. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  52. ;; distribution also contains a PDF version of it. At the homepage of
  53. ;; Org-mode, you can read the same text online as HTML. There is also an
  54. ;; excellent reference card made by Philip Rooke. This card can be found
  55. ;; in the etc/ directory of Emacs 22.
  56. ;;
  57. ;; A list of recent changes can be found at
  58. ;; http://orgmode.org/Changes.html
  59. ;;
  60. ;;; Code:
  61. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  62. (defvar org-table-formula-constants-local nil
  63. "Local version of `org-table-formula-constants'.")
  64. (make-variable-buffer-local 'org-table-formula-constants-local)
  65. ;;;; Require other packages
  66. (eval-when-compile
  67. (require 'cl)
  68. (require 'gnus-sum))
  69. (require 'calendar)
  70. (require 'find-func)
  71. (require 'format-spec)
  72. (or (equal this-command 'eval-buffer)
  73. (condition-case nil
  74. (load (concat (file-name-directory load-file-name)
  75. "org-loaddefs.el")
  76. nil t t t)
  77. (error
  78. (message "WARNING: No org-loaddefs.el file could be found from where org.el is loaded.")
  79. (sit-for 3)
  80. (message "You need to run \"make\" or \"make autoloads\" from Org lisp directory")
  81. (sit-for 3))))
  82. (require 'org-macs)
  83. (require 'org-compat)
  84. (declare-function cdlatex-environment "ext:cdlatex" (environment item))
  85. ;; `org-outline-regexp' ought to be a defconst but is let-bound in
  86. ;; some places -- e.g. see the macro `org-with-limited-levels'.
  87. ;;
  88. ;; In Org buffers, the value of `outline-regexp' is that of
  89. ;; `org-outline-regexp'. The only function still directly relying on
  90. ;; `outline-regexp' is `org-overview' so that `org-cycle' can do its
  91. ;; job when `orgstruct-mode' is active.
  92. (defvar org-outline-regexp "\\*+ "
  93. "Regexp to match Org headlines.")
  94. (defvar org-outline-regexp-bol "^\\*+ "
  95. "Regexp to match Org headlines.
  96. This is similar to `org-outline-regexp' but additionally makes
  97. sure that we are at the beginning of the line.")
  98. (defvar org-heading-regexp "^\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  99. "Matches a headline, putting stars and text into groups.
  100. Stars are put in group 1 and the trimmed body in group 2.")
  101. ;; Emacs 22 calendar compatibility: Make sure the new variables are available
  102. (unless (boundp 'calendar-view-holidays-initially-flag)
  103. (org-defvaralias 'calendar-view-holidays-initially-flag
  104. 'view-calendar-holidays-initially))
  105. (unless (boundp 'calendar-view-diary-initially-flag)
  106. (org-defvaralias 'calendar-view-diary-initially-flag
  107. 'view-diary-entries-initially))
  108. (unless (boundp 'diary-fancy-buffer)
  109. (org-defvaralias 'diary-fancy-buffer 'fancy-diary-buffer))
  110. (declare-function org-add-archive-files "org-archive" (files))
  111. (declare-function org-inlinetask-at-task-p "org-inlinetask" ())
  112. (declare-function org-inlinetask-outline-regexp "org-inlinetask" ())
  113. (declare-function org-inlinetask-toggle-visibility "org-inlinetask" ())
  114. (declare-function org-pop-to-buffer-same-window "org-compat" (&optional buffer-or-name norecord label))
  115. (declare-function org-clock-get-last-clock-out-time "org-clock" ())
  116. (declare-function org-clock-timestamps-up "org-clock" (&optional n))
  117. (declare-function org-clock-timestamps-down "org-clock" (&optional n))
  118. (declare-function org-clock-remove-overlays "org-clock" (&optional beg end noremove))
  119. (declare-function org-clock-sum "org-clock" (&optional tstart tend headline-filter propname))
  120. (declare-function org-clock-sum-current-item "org-clock" (&optional tstart))
  121. (declare-function org-clock-update-time-maybe "org-clock" ())
  122. (declare-function org-clocktable-shift "org-clock" (dir n))
  123. (declare-function org-babel-tangle-file "ob-tangle" (file &optional target-file lang))
  124. (declare-function org-babel-do-in-edit-buffer "ob-core" (&rest body))
  125. (declare-function orgtbl-mode "org-table" (&optional arg))
  126. (declare-function org-clock-out "org-clock" (&optional switch-to-state fail-quietly at-time))
  127. (declare-function org-beamer-mode "ox-beamer" ())
  128. (declare-function org-table-blank-field "org-table" ())
  129. (declare-function org-table-edit-field "org-table" (arg))
  130. (declare-function org-table-insert-row "org-table" (&optional arg))
  131. (declare-function org-table-justify-field-maybe "org-table" (&optional new))
  132. (declare-function org-table-calc-current-TBLFM "org-table" (&optional arg))
  133. (declare-function org-id-get-create "org-id" (&optional force))
  134. (declare-function org-add-archive-files "org-archive" (files))
  135. (declare-function org-id-find-id-file "org-id" (id))
  136. (declare-function org-tags-view "org-agenda" (&optional todo-only match))
  137. (declare-function org-agenda-list "org-agenda" (&optional arg start-day span))
  138. (declare-function org-agenda-redo "org-agenda" (&optional all))
  139. (declare-function org-table-align "org-table" ())
  140. (declare-function org-table-begin "org-table" (&optional table-type))
  141. (declare-function org-table-blank-field "org-table" ())
  142. (declare-function org-table-end "org-table" (&optional table-type))
  143. (declare-function org-table-end-of-field "org-table" (&optional n))
  144. (declare-function org-table-insert-row "org-table" (&optional arg))
  145. (declare-function org-table-paste-rectangle "org-table" ())
  146. (declare-function org-table-maybe-eval-formula "org-table" ())
  147. (declare-function org-table-maybe-recalculate-line "org-table" ())
  148. (declare-function orgtbl-ascii-plot "org-table" (&optional ask))
  149. (declare-function org-plot/gnuplot "org-plot" (&optional params))
  150. (declare-function org-element-at-point "org-element" ())
  151. (declare-function org-element-cache-reset "org-element" (&optional all))
  152. (declare-function org-element-cache-refresh "org-element" (pos))
  153. (declare-function org-element-contents "org-element" (element))
  154. (declare-function org-element-context "org-element" (&optional element))
  155. (declare-function org-element-interpret-data "org-element"
  156. (data &optional parent))
  157. (declare-function org-element-nested-p "org-element" (elem-a elem-b))
  158. (declare-function org-element-parse-buffer "org-element"
  159. (&optional granularity visible-only))
  160. (declare-function org-element-property "org-element" (property element))
  161. (declare-function org-element-put-property "org-element"
  162. (element property value))
  163. (declare-function org-element-swap-A-B "org-element" (elem-a elem-b))
  164. (declare-function org-element-parse-buffer "org-element"
  165. (&optional granularity visible-only))
  166. (declare-function org-element-type "org-element" (element))
  167. (defsubst org-uniquify (list)
  168. "Non-destructively remove duplicate elements from LIST."
  169. (let ((res (copy-sequence list))) (delete-dups res)))
  170. (defsubst org-get-at-bol (property)
  171. "Get text property PROPERTY at the beginning of line."
  172. (get-text-property (point-at-bol) property))
  173. (defsubst org-trim (s)
  174. "Remove whitespace at the beginning and the end of string S."
  175. (replace-regexp-in-string
  176. "\\`[ \t\n\r]+" ""
  177. (replace-regexp-in-string "[ \t\n\r]+\\'" "" s)))
  178. ;; load languages based on value of `org-babel-load-languages'
  179. (defvar org-babel-load-languages)
  180. ;;;###autoload
  181. (defun org-babel-do-load-languages (sym value)
  182. "Load the languages defined in `org-babel-load-languages'."
  183. (set-default sym value)
  184. (mapc (lambda (pair)
  185. (let ((active (cdr pair)) (lang (symbol-name (car pair))))
  186. (if active
  187. (progn
  188. (require (intern (concat "ob-" lang))))
  189. (progn
  190. (funcall 'fmakunbound
  191. (intern (concat "org-babel-execute:" lang)))
  192. (funcall 'fmakunbound
  193. (intern (concat "org-babel-expand-body:" lang)))))))
  194. org-babel-load-languages))
  195. (declare-function org-babel-tangle-file "ob-tangle" (file &optional target-file lang))
  196. ;;;###autoload
  197. (defun org-babel-load-file (file &optional compile)
  198. "Load Emacs Lisp source code blocks in the Org-mode FILE.
  199. This function exports the source code using `org-babel-tangle'
  200. and then loads the resulting file using `load-file'. With prefix
  201. arg (noninteractively: 2nd arg) COMPILE the tangled Emacs Lisp
  202. file to byte-code before it is loaded."
  203. (interactive "fFile to load: \nP")
  204. (let* ((age (lambda (file)
  205. (float-time
  206. (time-subtract (current-time)
  207. (nth 5 (or (file-attributes (file-truename file))
  208. (file-attributes file)))))))
  209. (base-name (file-name-sans-extension file))
  210. (exported-file (concat base-name ".el")))
  211. ;; tangle if the org-mode file is newer than the elisp file
  212. (unless (and (file-exists-p exported-file)
  213. (> (funcall age file) (funcall age exported-file)))
  214. (setq exported-file
  215. (car (org-babel-tangle-file file exported-file "emacs-lisp"))))
  216. (message "%s %s"
  217. (if compile
  218. (progn (byte-compile-file exported-file 'load)
  219. "Compiled and loaded")
  220. (progn (load-file exported-file) "Loaded"))
  221. exported-file)))
  222. (defcustom org-babel-load-languages '((emacs-lisp . t))
  223. "Languages which can be evaluated in Org-mode buffers.
  224. This list can be used to load support for any of the languages
  225. below, note that each language will depend on a different set of
  226. system executables and/or Emacs modes. When a language is
  227. \"loaded\", then code blocks in that language can be evaluated
  228. with `org-babel-execute-src-block' bound by default to C-c
  229. C-c (note the `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can
  230. be set to remove code block evaluation from the C-c C-c
  231. keybinding. By default only Emacs Lisp (which has no
  232. requirements) is loaded."
  233. :group 'org-babel
  234. :set 'org-babel-do-load-languages
  235. :version "24.1"
  236. :type '(alist :tag "Babel Languages"
  237. :key-type
  238. (choice
  239. (const :tag "Awk" awk)
  240. (const :tag "C" C)
  241. (const :tag "R" R)
  242. (const :tag "Asymptote" asymptote)
  243. (const :tag "Calc" calc)
  244. (const :tag "Clojure" clojure)
  245. (const :tag "CSS" css)
  246. (const :tag "Ditaa" ditaa)
  247. (const :tag "Dot" dot)
  248. (const :tag "Emacs Lisp" emacs-lisp)
  249. (const :tag "Forth" forth)
  250. (const :tag "Fortran" fortran)
  251. (const :tag "Gnuplot" gnuplot)
  252. (const :tag "Haskell" haskell)
  253. (const :tag "IO" io)
  254. (const :tag "J" J)
  255. (const :tag "Java" java)
  256. (const :tag "Javascript" js)
  257. (const :tag "LaTeX" latex)
  258. (const :tag "Ledger" ledger)
  259. (const :tag "Lilypond" lilypond)
  260. (const :tag "Lisp" lisp)
  261. (const :tag "Makefile" makefile)
  262. (const :tag "Maxima" maxima)
  263. (const :tag "Matlab" matlab)
  264. (const :tag "Mscgen" mscgen)
  265. (const :tag "Ocaml" ocaml)
  266. (const :tag "Octave" octave)
  267. (const :tag "Org" org)
  268. (const :tag "Perl" perl)
  269. (const :tag "Pico Lisp" picolisp)
  270. (const :tag "PlantUML" plantuml)
  271. (const :tag "Python" python)
  272. (const :tag "Ruby" ruby)
  273. (const :tag "Sass" sass)
  274. (const :tag "Scala" scala)
  275. (const :tag "Scheme" scheme)
  276. (const :tag "Screen" screen)
  277. (const :tag "Shell Script" shell)
  278. (const :tag "Shen" shen)
  279. (const :tag "Sql" sql)
  280. (const :tag "Sqlite" sqlite)
  281. (const :tag "ebnf2ps" ebnf2ps))
  282. :value-type (boolean :tag "Activate" :value t)))
  283. ;;;; Customization variables
  284. (defcustom org-clone-delete-id nil
  285. "Remove ID property of clones of a subtree.
  286. When non-nil, clones of a subtree don't inherit the ID property.
  287. Otherwise they inherit the ID property with a new unique
  288. identifier."
  289. :type 'boolean
  290. :version "24.1"
  291. :group 'org-id)
  292. ;;; Version
  293. (org-check-version)
  294. ;;;###autoload
  295. (defun org-version (&optional here full message)
  296. "Show the org-mode version.
  297. Interactively, or when MESSAGE is non-nil, show it in echo area.
  298. With prefix argument, or when HERE is non-nil, insert it at point.
  299. In non-interactive uses, a reduced version string is output unless
  300. FULL is given."
  301. (interactive (list current-prefix-arg t (not current-prefix-arg)))
  302. (let* ((org-dir (ignore-errors (org-find-library-dir "org")))
  303. (save-load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  304. (load-suffixes (list ".el"))
  305. (org-install-dir (ignore-errors (org-find-library-dir "org-loaddefs")))
  306. (org-trash (or
  307. (and (fboundp 'org-release) (fboundp 'org-git-version))
  308. (org-load-noerror-mustsuffix (concat org-dir "org-version"))))
  309. (load-suffixes save-load-suffixes)
  310. (org-version (org-release))
  311. (git-version (org-git-version))
  312. (version (format "Org-mode version %s (%s @ %s)"
  313. org-version
  314. git-version
  315. (if org-install-dir
  316. (if (string= org-dir org-install-dir)
  317. org-install-dir
  318. (concat "mixed installation! " org-install-dir " and " org-dir))
  319. "org-loaddefs.el can not be found!")))
  320. (version1 (if full version org-version)))
  321. (when here (insert version1))
  322. (when message (message "%s" version1))
  323. version1))
  324. (defconst org-version (org-version))
  325. ;;; Syntax Constants
  326. ;;;; Block
  327. (defconst org-block-regexp
  328. "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
  329. "Regular expression for hiding blocks.")
  330. (defconst org-dblock-start-re
  331. "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  332. "Matches the start line of a dynamic block, with parameters.")
  333. (defconst org-dblock-end-re "^[ \t]*#\\+\\(?:END\\|end\\)\\([: \t\r\n]\\|$\\)"
  334. "Matches the end of a dynamic block.")
  335. ;;;; Clock and Planning
  336. (defconst org-clock-string "CLOCK:"
  337. "String used as prefix for timestamps clocking work hours on an item.")
  338. (defconst org-closed-string "CLOSED:"
  339. "String used as the prefix for timestamps logging closing a TODO entry.")
  340. (defconst org-deadline-string "DEADLINE:"
  341. "String to mark deadline entries.
  342. A deadline is this string, followed by a time stamp. Should be a word,
  343. terminated by a colon. You can insert a schedule keyword and
  344. a timestamp with \\[org-deadline].")
  345. (defconst org-scheduled-string "SCHEDULED:"
  346. "String to mark scheduled TODO entries.
  347. A schedule is this string, followed by a time stamp. Should be a word,
  348. terminated by a colon. You can insert a schedule keyword and
  349. a timestamp with \\[org-schedule].")
  350. (defconst org-ds-keyword-length
  351. (+ 2
  352. (apply #'max
  353. (mapcar #'length
  354. (list org-deadline-string org-scheduled-string
  355. org-clock-string org-closed-string))))
  356. "Maximum length of the DEADLINE and SCHEDULED keywords.")
  357. (defconst org-planning-line-re
  358. (concat "^[ \t]*"
  359. (regexp-opt
  360. (list org-closed-string org-deadline-string org-scheduled-string)
  361. t))
  362. "Matches a line with planning info.
  363. Matched keyword is in group 1.")
  364. (defconst org-clock-line-re
  365. (concat "^[ \t]*" org-clock-string)
  366. "Matches a line with clock info.")
  367. (defconst org-deadline-regexp (concat "\\<" org-deadline-string)
  368. "Matches the DEADLINE keyword.")
  369. (defconst org-deadline-time-regexp
  370. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  371. "Matches the DEADLINE keyword together with a time stamp.")
  372. (defconst org-deadline-time-hour-regexp
  373. (concat "\\<" org-deadline-string
  374. " *<\\([^>]+[0-9]\\{1,2\\}:[0-9]\\{2\\}[0-9-+:hdwmy \t.]*\\)>")
  375. "Matches the DEADLINE keyword together with a time-and-hour stamp.")
  376. (defconst org-deadline-line-regexp
  377. (concat "\\<\\(" org-deadline-string "\\).*")
  378. "Matches the DEADLINE keyword and the rest of the line.")
  379. (defconst org-scheduled-regexp (concat "\\<" org-scheduled-string)
  380. "Matches the SCHEDULED keyword.")
  381. (defconst org-scheduled-time-regexp
  382. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  383. "Matches the SCHEDULED keyword together with a time stamp.")
  384. (defconst org-scheduled-time-hour-regexp
  385. (concat "\\<" org-scheduled-string
  386. " *<\\([^>]+[0-9]\\{1,2\\}:[0-9]\\{2\\}[0-9-+:hdwmy \t.]*\\)>")
  387. "Matches the SCHEDULED keyword together with a time-and-hour stamp.")
  388. (defconst org-closed-time-regexp
  389. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  390. "Matches the CLOSED keyword together with a time stamp.")
  391. (defconst org-keyword-time-regexp
  392. (concat "\\<"
  393. (regexp-opt
  394. (list org-scheduled-string org-deadline-string org-closed-string
  395. org-clock-string)
  396. t)
  397. " *[[<]\\([^]>]+\\)[]>]")
  398. "Matches any of the 4 keywords, together with the time stamp.")
  399. (defconst org-keyword-time-not-clock-regexp
  400. (concat
  401. "\\<"
  402. (regexp-opt
  403. (list org-scheduled-string org-deadline-string org-closed-string) t)
  404. " *[[<]\\([^]>]+\\)[]>]")
  405. "Matches any of the 3 keywords, together with the time stamp.")
  406. (defconst org-maybe-keyword-time-regexp
  407. (concat "\\(\\<"
  408. (regexp-opt
  409. (list org-scheduled-string org-deadline-string org-closed-string
  410. org-clock-string)
  411. t)
  412. "\\)?"
  413. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]"
  414. "\\|"
  415. "<%%([^\r\n>]*>\\)")
  416. "Matches a timestamp, possibly preceded by a keyword.")
  417. (defconst org-all-time-keywords
  418. (mapcar (lambda (w) (substring w 0 -1))
  419. (list org-scheduled-string org-deadline-string
  420. org-clock-string org-closed-string))
  421. "List of time keywords.")
  422. ;;;; Drawer
  423. (defconst org-drawer-regexp "^[ \t]*:\\(\\(?:\\w\\|[-_]\\)+\\):[ \t]*$"
  424. "Matches first or last line of a hidden block.
  425. Group 1 contains drawer's name or \"END\".")
  426. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  427. "Regular expression matching the first line of a property drawer.")
  428. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  429. "Regular expression matching the last line of a property drawer.")
  430. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  431. "Regular expression matching the first line of a clock drawer.")
  432. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  433. "Regular expression matching the last line of a clock drawer.")
  434. (defconst org-property-drawer-re
  435. (concat "^[ \t]*:PROPERTIES:[ \t]*\n"
  436. "\\(?:[ \t]*:\\S-+:\\(?: .*\\)?[ \t]*\n\\)*"
  437. "[ \t]*:END:[ \t]*$")
  438. "Matches an entire property drawer.")
  439. (defconst org-clock-drawer-re
  440. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*?\\("
  441. org-clock-drawer-end-re "\\)\n?")
  442. "Matches an entire clock drawer.")
  443. ;;;; Headline
  444. (defconst org-heading-keyword-regexp-format
  445. "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  446. "Printf format for a regexp matching a headline with some keyword.
  447. This regexp will match the headline of any node which has the
  448. exact keyword that is put into the format. The keyword isn't in
  449. any group by default, but the stars and the body are.")
  450. (defconst org-heading-keyword-maybe-regexp-format
  451. "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
  452. "Printf format for a regexp matching a headline, possibly with some keyword.
  453. This regexp can match any headline with the specified keyword, or
  454. without a keyword. The keyword isn't in any group by default,
  455. but the stars and the body are.")
  456. (defconst org-archive-tag "ARCHIVE"
  457. "The tag that marks a subtree as archived.
  458. An archived subtree does not open during visibility cycling, and does
  459. not contribute to the agenda listings.")
  460. (defconst org-comment-string "COMMENT"
  461. "Entries starting with this keyword will never be exported.
  462. An entry can be toggled between COMMENT and normal with
  463. \\[org-toggle-comment].")
  464. ;;;; LaTeX Environments and Fragments
  465. (defconst org-latex-regexps
  466. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  467. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  468. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  469. ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  470. ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  471. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  472. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  473. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  474. "Regular expressions for matching embedded LaTeX.")
  475. ;;;; Node Property
  476. (defconst org-effort-property "Effort"
  477. "The property that is being used to keep track of effort estimates.
  478. Effort estimates given in this property need to have the format H:MM.")
  479. ;;;; Table
  480. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  481. "Detect an org-type or table-type table.")
  482. (defconst org-table-line-regexp "^[ \t]*|"
  483. "Detect an org-type table line.")
  484. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  485. "Detect an org-type table line.")
  486. (defconst org-table-hline-regexp "^[ \t]*|-"
  487. "Detect an org-type table hline.")
  488. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  489. "Detect a table-type table hline.")
  490. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  491. "Detect the first line outside a table when searching from within it.
  492. This works for both table types.")
  493. (defconst org-TBLFM-regexp "^[ \t]*#\\+TBLFM: "
  494. "Detect a #+TBLFM line.")
  495. ;;;; Timestamp
  496. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
  497. "Regular expression for fast time stamp matching.")
  498. (defconst org-ts-regexp-inactive
  499. "\\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)\\]"
  500. "Regular expression for fast inactive time stamp matching.")
  501. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
  502. "Regular expression for fast time stamp matching.")
  503. (defconst org-ts-regexp0
  504. "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\( +[^]+0-9>\r\n -]+\\)?\\( +\\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  505. "Regular expression matching time strings for analysis.
  506. This one does not require the space after the date, so it can be used
  507. on a string that terminates immediately after the date.")
  508. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  509. "Regular expression matching time strings for analysis.")
  510. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  511. "Regular expression matching time stamps, with groups.")
  512. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  513. "Regular expression matching time stamps (also [..]), with groups.")
  514. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  515. "Regular expression matching a time stamp range.")
  516. (defconst org-tr-regexp-both
  517. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  518. "Regular expression matching a time stamp range.")
  519. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  520. org-ts-regexp "\\)?")
  521. "Regular expression matching a time stamp or time stamp range.")
  522. (defconst org-tsr-regexp-both
  523. (concat org-ts-regexp-both "\\(--?-?"
  524. org-ts-regexp-both "\\)?")
  525. "Regular expression matching a time stamp or time stamp range.
  526. The time stamps may be either active or inactive.")
  527. (defconst org-repeat-re
  528. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)"
  529. "Regular expression for specifying repeated events.
  530. After a match, group 1 contains the repeat expression.")
  531. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  532. "Formats for `format-time-string' which are used for time stamps.")
  533. ;;; The custom variables
  534. (defgroup org nil
  535. "Outline-based notes management and organizer."
  536. :tag "Org"
  537. :group 'outlines
  538. :group 'calendar)
  539. (defcustom org-mode-hook nil
  540. "Mode hook for Org-mode, run after the mode was turned on."
  541. :group 'org
  542. :type 'hook)
  543. (defcustom org-load-hook nil
  544. "Hook that is run after org.el has been loaded."
  545. :group 'org
  546. :type 'hook)
  547. (defcustom org-log-buffer-setup-hook nil
  548. "Hook that is run after an Org log buffer is created."
  549. :group 'org
  550. :version "24.1"
  551. :type 'hook)
  552. (defvar org-modules) ; defined below
  553. (defvar org-modules-loaded nil
  554. "Have the modules been loaded already?")
  555. (defun org-load-modules-maybe (&optional force)
  556. "Load all extensions listed in `org-modules'."
  557. (when (or force (not org-modules-loaded))
  558. (mapc (lambda (ext)
  559. (condition-case nil (require ext)
  560. (error (message "Problems while trying to load feature `%s'" ext))))
  561. org-modules)
  562. (setq org-modules-loaded t)))
  563. (defun org-set-modules (var value)
  564. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  565. (set var value)
  566. (when (featurep 'org)
  567. (org-load-modules-maybe 'force)
  568. (org-element-cache-reset 'all)))
  569. (defcustom org-modules '(org-w3m org-bbdb org-bibtex org-docview org-gnus org-info org-irc org-mhe org-rmail)
  570. "Modules that should always be loaded together with org.el.
  571. If a description starts with <C>, the file is not part of Emacs
  572. and loading it will require that you have downloaded and properly
  573. installed the Org mode distribution.
  574. You can also use this system to load external packages (i.e. neither Org
  575. core modules, nor modules from the CONTRIB directory). Just add symbols
  576. to the end of the list. If the package is called org-xyz.el, then you need
  577. to add the symbol `xyz', and the package must have a call to:
  578. \(provide 'org-xyz)
  579. For export specific modules, see also `org-export-backends'."
  580. :group 'org
  581. :set 'org-set-modules
  582. :version "24.4"
  583. :package-version '(Org . "8.0")
  584. :type
  585. '(set :greedy t
  586. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  587. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  588. (const :tag " crypt: Encryption of subtrees" org-crypt)
  589. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  590. (const :tag " docview: Links to doc-view buffers" org-docview)
  591. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  592. (const :tag " habit: Track your consistency with habits" org-habit)
  593. (const :tag " id: Global IDs for identifying entries" org-id)
  594. (const :tag " info: Links to Info nodes" org-info)
  595. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  596. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  597. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  598. (const :tag " mouse: Additional mouse support" org-mouse)
  599. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  600. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  601. (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  602. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  603. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  604. (const :tag "C bullets: Add overlays to headlines stars" org-bullets)
  605. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  606. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  607. (const :tag "C collector: Collect properties into tables" org-collector)
  608. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  609. (const :tag "C drill: Flashcards and spaced repetition for Org-mode" org-drill)
  610. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  611. (const :tag "C eshell Support for links to working directories in eshell" org-eshell)
  612. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  613. (const :tag "C eval: Include command output as text" org-eval)
  614. (const :tag "C eww: Store link to url of eww" org-eww)
  615. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  616. (const :tag "C favtable: Lookup table of favorite references and links" org-favtable)
  617. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  618. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  619. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  620. (const :tag "C jira: Add a jira:ticket protocol to Org-mode" org-jira)
  621. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  622. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  623. (const :tag "C mac-link: Grab links and url from various mac Applications" org-mac-link)
  624. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  625. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  626. (const :tag "C mew: Links to Mew folders/messages" org-mew)
  627. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  628. (const :tag "C notmuch: Provide org links to notmuch searches or messages" org-notmuch)
  629. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  630. (const :tag "C registry: A registry for Org-mode links" org-registry)
  631. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  632. (const :tag "C secretary: Team management with org-mode" org-secretary)
  633. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  634. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  635. (const :tag "C track: Keep up with Org-mode development" org-track)
  636. (const :tag "C velocity Something like Notational Velocity for Org" org-velocity)
  637. (const :tag "C vm: Links to VM folders/messages" org-vm)
  638. (const :tag "C wikinodes: CamelCase wiki-like links" org-wikinodes)
  639. (const :tag "C wl: Links to Wanderlust folders/messages" org-wl)
  640. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  641. (defvar org-export--registered-backends) ; From ox.el.
  642. (declare-function org-export-derived-backend-p "ox" (backend &rest backends))
  643. (declare-function org-export-backend-name "ox" (backend))
  644. (defcustom org-export-backends '(ascii html icalendar latex)
  645. "List of export back-ends that should be always available.
  646. If a description starts with <C>, the file is not part of Emacs
  647. and loading it will require that you have downloaded and properly
  648. installed the Org mode distribution.
  649. Unlike to `org-modules', libraries in this list will not be
  650. loaded along with Org, but only once the export framework is
  651. needed.
  652. This variable needs to be set before org.el is loaded. If you
  653. need to make a change while Emacs is running, use the customize
  654. interface or run the following code, where VAL stands for the new
  655. value of the variable, after updating it:
  656. \(progn
  657. \(setq org-export--registered-backends
  658. \(org-remove-if-not
  659. \(lambda (backend)
  660. \(let ((name (org-export-backend-name backend)))
  661. \(or (memq name val)
  662. \(catch 'parentp
  663. \(dolist (b val)
  664. \(and (org-export-derived-backend-p b name)
  665. \(throw 'parentp t)))))))
  666. org-export--registered-backends))
  667. \(let ((new-list (mapcar 'org-export-backend-name
  668. org-export--registered-backends)))
  669. \(dolist (backend val)
  670. \(cond
  671. \((not (load (format \"ox-%s\" backend) t t))
  672. \(message \"Problems while trying to load export back-end `%s'\"
  673. backend))
  674. \((not (memq backend new-list)) (push backend new-list))))
  675. \(set-default 'org-export-backends new-list)))
  676. Adding a back-end to this list will also pull the back-end it
  677. depends on, if any."
  678. :group 'org
  679. :group 'org-export
  680. :version "24.4"
  681. :package-version '(Org . "8.0")
  682. :initialize 'custom-initialize-set
  683. :set (lambda (var val)
  684. (if (not (featurep 'ox)) (set-default var val)
  685. ;; Any back-end not required anymore (not present in VAL and not
  686. ;; a parent of any back-end in the new value) is removed from the
  687. ;; list of registered back-ends.
  688. (setq org-export--registered-backends
  689. (org-remove-if-not
  690. (lambda (backend)
  691. (let ((name (org-export-backend-name backend)))
  692. (or (memq name val)
  693. (catch 'parentp
  694. (dolist (b val)
  695. (and (org-export-derived-backend-p b name)
  696. (throw 'parentp t)))))))
  697. org-export--registered-backends))
  698. ;; Now build NEW-LIST of both new back-ends and required
  699. ;; parents.
  700. (let ((new-list (mapcar 'org-export-backend-name
  701. org-export--registered-backends)))
  702. (dolist (backend val)
  703. (cond
  704. ((not (load (format "ox-%s" backend) t t))
  705. (message "Problems while trying to load export back-end `%s'"
  706. backend))
  707. ((not (memq backend new-list)) (push backend new-list))))
  708. ;; Set VAR to that list with fixed dependencies.
  709. (set-default var new-list))))
  710. :type '(set :greedy t
  711. (const :tag " ascii Export buffer to ASCII format" ascii)
  712. (const :tag " beamer Export buffer to Beamer presentation" beamer)
  713. (const :tag " html Export buffer to HTML format" html)
  714. (const :tag " icalendar Export buffer to iCalendar format" icalendar)
  715. (const :tag " latex Export buffer to LaTeX format" latex)
  716. (const :tag " man Export buffer to MAN format" man)
  717. (const :tag " md Export buffer to Markdown format" md)
  718. (const :tag " odt Export buffer to ODT format" odt)
  719. (const :tag " org Export buffer to Org format" org)
  720. (const :tag " texinfo Export buffer to Texinfo format" texinfo)
  721. (const :tag "C confluence Export buffer to Confluence Wiki format" confluence)
  722. (const :tag "C deck Export buffer to deck.js presentations" deck)
  723. (const :tag "C freemind Export buffer to Freemind mindmap format" freemind)
  724. (const :tag "C groff Export buffer to Groff format" groff)
  725. (const :tag "C koma-letter Export buffer to KOMA Scrlttrl2 format" koma-letter)
  726. (const :tag "C RSS 2.0 Export buffer to RSS 2.0 format" rss)
  727. (const :tag "C s5 Export buffer to s5 presentations" s5)
  728. (const :tag "C taskjuggler Export buffer to TaskJuggler format" taskjuggler)))
  729. (eval-after-load 'ox
  730. '(mapc
  731. (lambda (backend)
  732. (condition-case nil (require (intern (format "ox-%s" backend)))
  733. (error (message "Problems while trying to load export back-end `%s'"
  734. backend))))
  735. org-export-backends))
  736. (defcustom org-support-shift-select nil
  737. "Non-nil means make shift-cursor commands select text when possible.
  738. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
  739. start selecting a region, or enlarge regions started in this way.
  740. In Org-mode, in special contexts, these same keys are used for
  741. other purposes, important enough to compete with shift selection.
  742. Org tries to balance these needs by supporting `shift-select-mode'
  743. outside these special contexts, under control of this variable.
  744. The default of this variable is nil, to avoid confusing behavior. Shifted
  745. cursor keys will then execute Org commands in the following contexts:
  746. - on a headline, changing TODO state (left/right) and priority (up/down)
  747. - on a time stamp, changing the time
  748. - in a plain list item, changing the bullet type
  749. - in a property definition line, switching between allowed values
  750. - in the BEGIN line of a clock table (changing the time block).
  751. Outside these contexts, the commands will throw an error.
  752. When this variable is t and the cursor is not in a special
  753. context, Org-mode will support shift-selection for making and
  754. enlarging regions. To make this more effective, the bullet
  755. cycling will no longer happen anywhere in an item line, but only
  756. if the cursor is exactly on the bullet.
  757. If you set this variable to the symbol `always', then the keys
  758. will not be special in headlines, property lines, and item lines,
  759. to make shift selection work there as well. If this is what you
  760. want, you can use the following alternative commands: `C-c C-t'
  761. and `C-c ,' to change TODO state and priority, `C-u C-u C-c C-t'
  762. can be used to switch TODO sets, `C-c -' to cycle item bullet
  763. types, and properties can be edited by hand or in column view.
  764. However, when the cursor is on a timestamp, shift-cursor commands
  765. will still edit the time stamp - this is just too good to give up.
  766. XEmacs user should have this variable set to nil, because
  767. `shift-select-mode' is in Emacs 23 or later only."
  768. :group 'org
  769. :type '(choice
  770. (const :tag "Never" nil)
  771. (const :tag "When outside special context" t)
  772. (const :tag "Everywhere except timestamps" always)))
  773. (defcustom org-loop-over-headlines-in-active-region nil
  774. "Shall some commands act upon headlines in the active region?
  775. When set to `t', some commands will be performed in all headlines
  776. within the active region.
  777. When set to `start-level', some commands will be performed in all
  778. headlines within the active region, provided that these headlines
  779. are of the same level than the first one.
  780. When set to a string, those commands will be performed on the
  781. matching headlines within the active region. Such string must be
  782. a tags/property/todo match as it is used in the agenda tags view.
  783. The list of commands is: `org-schedule', `org-deadline',
  784. `org-todo', `org-archive-subtree', `org-archive-set-tag' and
  785. `org-archive-to-archive-sibling'. The archiving commands skip
  786. already archived entries."
  787. :type '(choice (const :tag "Don't loop" nil)
  788. (const :tag "All headlines in active region" t)
  789. (const :tag "In active region, headlines at the same level than the first one" start-level)
  790. (string :tag "Tags/Property/Todo matcher"))
  791. :version "24.1"
  792. :group 'org-todo
  793. :group 'org-archive)
  794. (defgroup org-startup nil
  795. "Options concerning startup of Org-mode."
  796. :tag "Org Startup"
  797. :group 'org)
  798. (defcustom org-startup-folded t
  799. "Non-nil means entering Org-mode will switch to OVERVIEW.
  800. This can also be configured on a per-file basis by adding one of
  801. the following lines anywhere in the buffer:
  802. #+STARTUP: fold (or `overview', this is equivalent)
  803. #+STARTUP: nofold (or `showall', this is equivalent)
  804. #+STARTUP: content
  805. #+STARTUP: showeverything
  806. By default, this option is ignored when Org opens agenda files
  807. for the first time. If you want the agenda to honor the startup
  808. option, set `org-agenda-inhibit-startup' to nil."
  809. :group 'org-startup
  810. :type '(choice
  811. (const :tag "nofold: show all" nil)
  812. (const :tag "fold: overview" t)
  813. (const :tag "content: all headlines" content)
  814. (const :tag "show everything, even drawers" showeverything)))
  815. (defcustom org-startup-truncated t
  816. "Non-nil means entering Org-mode will set `truncate-lines'.
  817. This is useful since some lines containing links can be very long and
  818. uninteresting. Also tables look terrible when wrapped."
  819. :group 'org-startup
  820. :type 'boolean)
  821. (defcustom org-startup-indented nil
  822. "Non-nil means turn on `org-indent-mode' on startup.
  823. This can also be configured on a per-file basis by adding one of
  824. the following lines anywhere in the buffer:
  825. #+STARTUP: indent
  826. #+STARTUP: noindent"
  827. :group 'org-structure
  828. :type '(choice
  829. (const :tag "Not" nil)
  830. (const :tag "Globally (slow on startup in large files)" t)))
  831. (defcustom org-use-sub-superscripts t
  832. "Non-nil means interpret \"_\" and \"^\" for display.
  833. If you want to control how Org exports those characters, see
  834. `org-export-with-sub-superscripts'. `org-use-sub-superscripts'
  835. used to be an alias for `org-export-with-sub-superscripts' in
  836. Org <8.0, it is not anymore.
  837. When this option is turned on, you can use TeX-like syntax for
  838. sub- and superscripts within the buffer. Several characters after
  839. \"_\" or \"^\" will be considered as a single item - so grouping
  840. with {} is normally not needed. For example, the following things
  841. will be parsed as single sub- or superscripts:
  842. 10^24 or 10^tau several digits will be considered 1 item.
  843. 10^-12 or 10^-tau a leading sign with digits or a word
  844. x^2-y^3 will be read as x^2 - y^3, because items are
  845. terminated by almost any nonword/nondigit char.
  846. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  847. Still, ambiguity is possible. So when in doubt, use {} to enclose
  848. the sub/superscript. If you set this variable to the symbol `{}',
  849. the braces are *required* in order to trigger interpretations as
  850. sub/superscript. This can be helpful in documents that need \"_\"
  851. frequently in plain text."
  852. :group 'org-startup
  853. :version "24.4"
  854. :package-version '(Org . "8.0")
  855. :type '(choice
  856. (const :tag "Always interpret" t)
  857. (const :tag "Only with braces" {})
  858. (const :tag "Never interpret" nil)))
  859. (defcustom org-startup-with-beamer-mode nil
  860. "Non-nil means turn on `org-beamer-mode' on startup.
  861. This can also be configured on a per-file basis by adding one of
  862. the following lines anywhere in the buffer:
  863. #+STARTUP: beamer"
  864. :group 'org-startup
  865. :version "24.1"
  866. :type 'boolean)
  867. (defcustom org-startup-align-all-tables nil
  868. "Non-nil means align all tables when visiting a file.
  869. This is useful when the column width in tables is forced with <N> cookies
  870. in table fields. Such tables will look correct only after the first re-align.
  871. This can also be configured on a per-file basis by adding one of
  872. the following lines anywhere in the buffer:
  873. #+STARTUP: align
  874. #+STARTUP: noalign"
  875. :group 'org-startup
  876. :type 'boolean)
  877. (defcustom org-startup-with-inline-images nil
  878. "Non-nil means show inline images when loading a new Org file.
  879. This can also be configured on a per-file basis by adding one of
  880. the following lines anywhere in the buffer:
  881. #+STARTUP: inlineimages
  882. #+STARTUP: noinlineimages"
  883. :group 'org-startup
  884. :version "24.1"
  885. :type 'boolean)
  886. (defcustom org-startup-with-latex-preview nil
  887. "Non-nil means preview LaTeX fragments when loading a new Org file.
  888. This can also be configured on a per-file basis by adding one of
  889. the following lines anywhere in the buffer:
  890. #+STARTUP: latexpreview
  891. #+STARTUP: nolatexpreview"
  892. :group 'org-startup
  893. :version "24.4"
  894. :package-version '(Org . "8.0")
  895. :type 'boolean)
  896. (defcustom org-insert-mode-line-in-empty-file nil
  897. "Non-nil means insert the first line setting Org-mode in empty files.
  898. When the function `org-mode' is called interactively in an empty file, this
  899. normally means that the file name does not automatically trigger Org-mode.
  900. To ensure that the file will always be in Org-mode in the future, a
  901. line enforcing Org-mode will be inserted into the buffer, if this option
  902. has been set."
  903. :group 'org-startup
  904. :type 'boolean)
  905. (defcustom org-replace-disputed-keys nil
  906. "Non-nil means use alternative key bindings for some keys.
  907. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  908. These keys are also used by other packages like shift-selection-mode'
  909. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  910. If you want to use Org-mode together with one of these other modes,
  911. or more generally if you would like to move some Org-mode commands to
  912. other keys, set this variable and configure the keys with the variable
  913. `org-disputed-keys'.
  914. This option is only relevant at load-time of Org-mode, and must be set
  915. *before* org.el is loaded. Changing it requires a restart of Emacs to
  916. become effective."
  917. :group 'org-startup
  918. :type 'boolean)
  919. (defcustom org-use-extra-keys nil
  920. "Non-nil means use extra key sequence definitions for certain commands.
  921. This happens automatically if you run XEmacs or if `window-system'
  922. is nil. This variable lets you do the same manually. You must
  923. set it before loading org.
  924. Example: on Carbon Emacs 22 running graphically, with an external
  925. keyboard on a Powerbook, the default way of setting M-left might
  926. not work for either Alt or ESC. Setting this variable will make
  927. it work for ESC."
  928. :group 'org-startup
  929. :type 'boolean)
  930. (org-defvaralias 'org-CUA-compatible 'org-replace-disputed-keys)
  931. (defcustom org-disputed-keys
  932. '(([(shift up)] . [(meta p)])
  933. ([(shift down)] . [(meta n)])
  934. ([(shift left)] . [(meta -)])
  935. ([(shift right)] . [(meta +)])
  936. ([(control shift right)] . [(meta shift +)])
  937. ([(control shift left)] . [(meta shift -)]))
  938. "Keys for which Org-mode and other modes compete.
  939. This is an alist, cars are the default keys, second element specifies
  940. the alternative to use when `org-replace-disputed-keys' is t.
  941. Keys can be specified in any syntax supported by `define-key'.
  942. The value of this option takes effect only at Org-mode's startup,
  943. therefore you'll have to restart Emacs to apply it after changing."
  944. :group 'org-startup
  945. :type 'alist)
  946. (defun org-key (key)
  947. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  948. Or return the original if not disputed.
  949. Also apply the translations defined in `org-xemacs-key-equivalents'."
  950. (when org-replace-disputed-keys
  951. (let* ((nkey (key-description key))
  952. (x (org-find-if (lambda (x)
  953. (equal (key-description (car x)) nkey))
  954. org-disputed-keys)))
  955. (setq key (if x (cdr x) key))))
  956. (when (featurep 'xemacs)
  957. (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
  958. key)
  959. (defun org-find-if (predicate seq)
  960. (catch 'exit
  961. (while seq
  962. (if (funcall predicate (car seq))
  963. (throw 'exit (car seq))
  964. (pop seq)))))
  965. (defun org-defkey (keymap key def)
  966. "Define a key, possibly translated, as returned by `org-key'."
  967. (define-key keymap (org-key key) def))
  968. (defcustom org-ellipsis nil
  969. "The ellipsis to use in the Org-mode outline.
  970. When nil, just use the standard three dots.
  971. When a string, use that string instead.
  972. When a face, use the standard 3 dots, but with the specified face.
  973. The change affects only Org-mode (which will then use its own display table).
  974. Changing this requires executing \\[org-mode] in a buffer to become
  975. effective."
  976. :group 'org-startup
  977. :type '(choice (const :tag "Default" nil)
  978. (face :tag "Face" :value org-warning)
  979. (string :tag "String" :value "...#")))
  980. (defvar org-display-table nil
  981. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  982. (defgroup org-keywords nil
  983. "Keywords in Org-mode."
  984. :tag "Org Keywords"
  985. :group 'org)
  986. (defcustom org-closed-keep-when-no-todo nil
  987. "Remove CLOSED: time-stamp when switching back to a non-todo state?"
  988. :group 'org-todo
  989. :group 'org-keywords
  990. :version "24.4"
  991. :package-version '(Org . "8.0")
  992. :type 'boolean)
  993. (defgroup org-structure nil
  994. "Options concerning the general structure of Org-mode files."
  995. :tag "Org Structure"
  996. :group 'org)
  997. (defgroup org-reveal-location nil
  998. "Options about how to make context of a location visible."
  999. :tag "Org Reveal Location"
  1000. :group 'org-structure)
  1001. (defconst org-context-choice
  1002. '(choice
  1003. (const :tag "Always" t)
  1004. (const :tag "Never" nil)
  1005. (repeat :greedy t :tag "Individual contexts"
  1006. (cons
  1007. (choice :tag "Context"
  1008. (const agenda)
  1009. (const org-goto)
  1010. (const occur-tree)
  1011. (const tags-tree)
  1012. (const link-search)
  1013. (const mark-goto)
  1014. (const bookmark-jump)
  1015. (const isearch)
  1016. (const default))
  1017. (boolean))))
  1018. "Contexts for the reveal options.")
  1019. (defcustom org-show-hierarchy-above '((default . t))
  1020. "Non-nil means show full hierarchy when revealing a location.
  1021. Org-mode often shows locations in an org-mode file which might have
  1022. been invisible before. When this is set, the hierarchy of headings
  1023. above the exposed location is shown.
  1024. Turning this off for example for sparse trees makes them very compact.
  1025. Instead of t, this can also be an alist specifying this option for different
  1026. contexts. Valid contexts are
  1027. agenda when exposing an entry from the agenda
  1028. org-goto when using the command `org-goto' on key C-c C-j
  1029. occur-tree when using the command `org-occur' on key C-c /
  1030. tags-tree when constructing a sparse tree based on tags matches
  1031. link-search when exposing search matches associated with a link
  1032. mark-goto when exposing the jump goal of a mark
  1033. bookmark-jump when exposing a bookmark location
  1034. isearch when exiting from an incremental search
  1035. default default for all contexts not set explicitly"
  1036. :group 'org-reveal-location
  1037. :type org-context-choice)
  1038. (defcustom org-show-following-heading '((default . nil))
  1039. "Non-nil means show following heading when revealing a location.
  1040. Org-mode often shows locations in an org-mode file which might have
  1041. been invisible before. When this is set, the heading following the
  1042. match is shown.
  1043. Turning this off for example for sparse trees makes them very compact,
  1044. but makes it harder to edit the location of the match. In such a case,
  1045. use the command \\[org-reveal] to show more context.
  1046. Instead of t, this can also be an alist specifying this option for different
  1047. contexts. See `org-show-hierarchy-above' for valid contexts."
  1048. :group 'org-reveal-location
  1049. :type org-context-choice)
  1050. (defcustom org-show-siblings '((default . nil) (isearch t) (bookmark-jump t))
  1051. "Non-nil means show all sibling heading when revealing a location.
  1052. Org-mode often shows locations in an org-mode file which might have
  1053. been invisible before. When this is set, the sibling of the current entry
  1054. heading are all made visible. If `org-show-hierarchy-above' is t,
  1055. the same happens on each level of the hierarchy above the current entry.
  1056. By default this is on for the isearch context, off for all other contexts.
  1057. Turning this off for example for sparse trees makes them very compact,
  1058. but makes it harder to edit the location of the match. In such a case,
  1059. use the command \\[org-reveal] to show more context.
  1060. Instead of t, this can also be an alist specifying this option for different
  1061. contexts. See `org-show-hierarchy-above' for valid contexts."
  1062. :group 'org-reveal-location
  1063. :type org-context-choice
  1064. :version "24.4"
  1065. :package-version '(Org . "8.0"))
  1066. (defcustom org-show-entry-below '((default . nil))
  1067. "Non-nil means show the entry below a headline when revealing a location.
  1068. Org-mode often shows locations in an org-mode file which might have
  1069. been invisible before. When this is set, the text below the headline that is
  1070. exposed is also shown.
  1071. By default this is off for all contexts.
  1072. Instead of t, this can also be an alist specifying this option for different
  1073. contexts. See `org-show-hierarchy-above' for valid contexts."
  1074. :group 'org-reveal-location
  1075. :type org-context-choice)
  1076. (defcustom org-indirect-buffer-display 'other-window
  1077. "How should indirect tree buffers be displayed?
  1078. This applies to indirect buffers created with the commands
  1079. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  1080. Valid values are:
  1081. current-window Display in the current window
  1082. other-window Just display in another window.
  1083. dedicated-frame Create one new frame, and re-use it each time.
  1084. new-frame Make a new frame each time. Note that in this case
  1085. previously-made indirect buffers are kept, and you need to
  1086. kill these buffers yourself."
  1087. :group 'org-structure
  1088. :group 'org-agenda-windows
  1089. :type '(choice
  1090. (const :tag "In current window" current-window)
  1091. (const :tag "In current frame, other window" other-window)
  1092. (const :tag "Each time a new frame" new-frame)
  1093. (const :tag "One dedicated frame" dedicated-frame)))
  1094. (defcustom org-use-speed-commands nil
  1095. "Non-nil means activate single letter commands at beginning of a headline.
  1096. This may also be a function to test for appropriate locations where speed
  1097. commands should be active.
  1098. For example, to activate speed commands when the point is on any
  1099. star at the beginning of the headline, you can do this:
  1100. (setq org-use-speed-commands
  1101. (lambda () (and (looking-at org-outline-regexp) (looking-back \"^\\**\"))))"
  1102. :group 'org-structure
  1103. :type '(choice
  1104. (const :tag "Never" nil)
  1105. (const :tag "At beginning of headline stars" t)
  1106. (function)))
  1107. (defcustom org-speed-commands-user nil
  1108. "Alist of additional speed commands.
  1109. This list will be checked before `org-speed-commands-default'
  1110. when the variable `org-use-speed-commands' is non-nil
  1111. and when the cursor is at the beginning of a headline.
  1112. The car if each entry is a string with a single letter, which must
  1113. be assigned to `self-insert-command' in the global map.
  1114. The cdr is either a command to be called interactively, a function
  1115. to be called, or a form to be evaluated.
  1116. An entry that is just a list with a single string will be interpreted
  1117. as a descriptive headline that will be added when listing the speed
  1118. commands in the Help buffer using the `?' speed command."
  1119. :group 'org-structure
  1120. :type '(repeat :value ("k" . ignore)
  1121. (choice :value ("k" . ignore)
  1122. (list :tag "Descriptive Headline" (string :tag "Headline"))
  1123. (cons :tag "Letter and Command"
  1124. (string :tag "Command letter")
  1125. (choice
  1126. (function)
  1127. (sexp))))))
  1128. (defcustom org-bookmark-names-plist
  1129. '(:last-capture "org-capture-last-stored"
  1130. :last-refile "org-refile-last-stored"
  1131. :last-capture-marker "org-capture-last-stored-marker")
  1132. "Names for bookmarks automatically set by some Org commands.
  1133. This can provide strings as names for a number of bookmarks Org sets
  1134. automatically. The following keys are currently implemented:
  1135. :last-capture
  1136. :last-capture-marker
  1137. :last-refile
  1138. When a key does not show up in the property list, the corresponding bookmark
  1139. is not set."
  1140. :group 'org-structure
  1141. :type 'plist)
  1142. (defgroup org-cycle nil
  1143. "Options concerning visibility cycling in Org-mode."
  1144. :tag "Org Cycle"
  1145. :group 'org-structure)
  1146. (defcustom org-cycle-skip-children-state-if-no-children t
  1147. "Non-nil means skip CHILDREN state in entries that don't have any."
  1148. :group 'org-cycle
  1149. :type 'boolean)
  1150. (defcustom org-cycle-max-level nil
  1151. "Maximum level which should still be subject to visibility cycling.
  1152. Levels higher than this will, for cycling, be treated as text, not a headline.
  1153. When `org-odd-levels-only' is set, a value of N in this variable actually
  1154. means 2N-1 stars as the limiting headline.
  1155. When nil, cycle all levels.
  1156. Note that the limiting level of cycling is also influenced by
  1157. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  1158. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  1159. than its value."
  1160. :group 'org-cycle
  1161. :type '(choice
  1162. (const :tag "No limit" nil)
  1163. (integer :tag "Maximum level")))
  1164. (defcustom org-hide-block-startup nil
  1165. "Non-nil means entering Org-mode will fold all blocks.
  1166. This can also be set in on a per-file basis with
  1167. #+STARTUP: hideblocks
  1168. #+STARTUP: showblocks"
  1169. :group 'org-startup
  1170. :group 'org-cycle
  1171. :type 'boolean)
  1172. (defcustom org-cycle-global-at-bob nil
  1173. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  1174. This makes it possible to do global cycling without having to use S-TAB or
  1175. \\[universal-argument] TAB. For this special case to work, the first line
  1176. of the buffer must not be a headline -- it may be empty or some other text.
  1177. When used in this way, `org-cycle-hook' is disabled temporarily to make
  1178. sure the cursor stays at the beginning of the buffer. When this option is
  1179. nil, don't do anything special at the beginning of the buffer."
  1180. :group 'org-cycle
  1181. :type 'boolean)
  1182. (defcustom org-cycle-level-after-item/entry-creation t
  1183. "Non-nil means cycle entry level or item indentation in new empty entries.
  1184. When the cursor is at the end of an empty headline, i.e., with only stars
  1185. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  1186. and then all possible ancestor states, before returning to the original state.
  1187. This makes data entry extremely fast: M-RET to create a new headline,
  1188. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  1189. When the cursor is at the end of an empty plain list item, one TAB will
  1190. make it a subitem, two or more tabs will back up to make this an item
  1191. higher up in the item hierarchy."
  1192. :group 'org-cycle
  1193. :type 'boolean)
  1194. (defcustom org-cycle-emulate-tab t
  1195. "Where should `org-cycle' emulate TAB.
  1196. nil Never
  1197. white Only in completely white lines
  1198. whitestart Only at the beginning of lines, before the first non-white char
  1199. t Everywhere except in headlines
  1200. exc-hl-bol Everywhere except at the start of a headline
  1201. If TAB is used in a place where it does not emulate TAB, the current subtree
  1202. visibility is cycled."
  1203. :group 'org-cycle
  1204. :type '(choice (const :tag "Never" nil)
  1205. (const :tag "Only in completely white lines" white)
  1206. (const :tag "Before first char in a line" whitestart)
  1207. (const :tag "Everywhere except in headlines" t)
  1208. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)))
  1209. (defcustom org-cycle-separator-lines 2
  1210. "Number of empty lines needed to keep an empty line between collapsed trees.
  1211. If you leave an empty line between the end of a subtree and the following
  1212. headline, this empty line is hidden when the subtree is folded.
  1213. Org-mode will leave (exactly) one empty line visible if the number of
  1214. empty lines is equal or larger to the number given in this variable.
  1215. So the default 2 means at least 2 empty lines after the end of a subtree
  1216. are needed to produce free space between a collapsed subtree and the
  1217. following headline.
  1218. If the number is negative, and the number of empty lines is at least -N,
  1219. all empty lines are shown.
  1220. Special case: when 0, never leave empty lines in collapsed view."
  1221. :group 'org-cycle
  1222. :type 'integer)
  1223. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  1224. (defcustom org-pre-cycle-hook nil
  1225. "Hook that is run before visibility cycling is happening.
  1226. The function(s) in this hook must accept a single argument which indicates
  1227. the new state that will be set right after running this hook. The
  1228. argument is a symbol. Before a global state change, it can have the values
  1229. `overview', `content', or `all'. Before a local state change, it can have
  1230. the values `folded', `children', or `subtree'."
  1231. :group 'org-cycle
  1232. :type 'hook)
  1233. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  1234. org-cycle-hide-drawers
  1235. org-cycle-hide-inline-tasks
  1236. org-cycle-show-empty-lines
  1237. org-optimize-window-after-visibility-change)
  1238. "Hook that is run after `org-cycle' has changed the buffer visibility.
  1239. The function(s) in this hook must accept a single argument which indicates
  1240. the new state that was set by the most recent `org-cycle' command. The
  1241. argument is a symbol. After a global state change, it can have the values
  1242. `overview', `contents', or `all'. After a local state change, it can have
  1243. the values `folded', `children', or `subtree'."
  1244. :group 'org-cycle
  1245. :type 'hook)
  1246. (defgroup org-edit-structure nil
  1247. "Options concerning structure editing in Org-mode."
  1248. :tag "Org Edit Structure"
  1249. :group 'org-structure)
  1250. (defcustom org-odd-levels-only nil
  1251. "Non-nil means skip even levels and only use odd levels for the outline.
  1252. This has the effect that two stars are being added/taken away in
  1253. promotion/demotion commands. It also influences how levels are
  1254. handled by the exporters.
  1255. Changing it requires restart of `font-lock-mode' to become effective
  1256. for fontification also in regions already fontified.
  1257. You may also set this on a per-file basis by adding one of the following
  1258. lines to the buffer:
  1259. #+STARTUP: odd
  1260. #+STARTUP: oddeven"
  1261. :group 'org-edit-structure
  1262. :group 'org-appearance
  1263. :type 'boolean)
  1264. (defcustom org-adapt-indentation t
  1265. "Non-nil means adapt indentation to outline node level.
  1266. When this variable is set, Org assumes that you write outlines by
  1267. indenting text in each node to align with the headline (after the
  1268. stars). The following issues are influenced by this variable:
  1269. - The indentation is increased by one space in a demotion
  1270. command, and decreased by one in a promotion command. However,
  1271. in the latter case, if shifting some line in the entry body
  1272. would alter document structure (e.g., insert a new headline),
  1273. indentation is not changed at all.
  1274. - Property drawers and planning information is inserted indented
  1275. when this variable is set. When nil, they will not be indented.
  1276. - TAB indents a line relative to current level. The lines below
  1277. a headline will be indented when this variable is set.
  1278. Note that this is all about true indentation, by adding and
  1279. removing space characters. See also `org-indent.el' which does
  1280. level-dependent indentation in a virtual way, i.e. at display
  1281. time in Emacs."
  1282. :group 'org-edit-structure
  1283. :type 'boolean)
  1284. (defcustom org-special-ctrl-a/e nil
  1285. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  1286. When t, `C-a' will bring back the cursor to the beginning of the
  1287. headline text, i.e. after the stars and after a possible TODO
  1288. keyword. In an item, this will be the position after bullet and
  1289. check-box, if any. When the cursor is already at that position,
  1290. another `C-a' will bring it to the beginning of the line.
  1291. `C-e' will jump to the end of the headline, ignoring the presence
  1292. of tags in the headline. A second `C-e' will then jump to the
  1293. true end of the line, after any tags. This also means that, when
  1294. this variable is non-nil, `C-e' also will never jump beyond the
  1295. end of the heading of a folded section, i.e. not after the
  1296. ellipses.
  1297. When set to the symbol `reversed', the first `C-a' or `C-e' works
  1298. normally, going to the true line boundary first. Only a directly
  1299. following, identical keypress will bring the cursor to the
  1300. special positions.
  1301. This may also be a cons cell where the behavior for `C-a' and
  1302. `C-e' is set separately."
  1303. :group 'org-edit-structure
  1304. :type '(choice
  1305. (const :tag "off" nil)
  1306. (const :tag "on: after stars/bullet and before tags first" t)
  1307. (const :tag "reversed: true line boundary first" reversed)
  1308. (cons :tag "Set C-a and C-e separately"
  1309. (choice :tag "Special C-a"
  1310. (const :tag "off" nil)
  1311. (const :tag "on: after stars/bullet first" t)
  1312. (const :tag "reversed: before stars/bullet first" reversed))
  1313. (choice :tag "Special C-e"
  1314. (const :tag "off" nil)
  1315. (const :tag "on: before tags first" t)
  1316. (const :tag "reversed: after tags first" reversed)))))
  1317. (org-defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e)
  1318. (defcustom org-special-ctrl-k nil
  1319. "Non-nil means `C-k' will behave specially in headlines.
  1320. When nil, `C-k' will call the default `kill-line' command.
  1321. When t, the following will happen while the cursor is in the headline:
  1322. - When the cursor is at the beginning of a headline, kill the entire
  1323. line and possible the folded subtree below the line.
  1324. - When in the middle of the headline text, kill the headline up to the tags.
  1325. - When after the headline text, kill the tags."
  1326. :group 'org-edit-structure
  1327. :type 'boolean)
  1328. (defcustom org-ctrl-k-protect-subtree nil
  1329. "Non-nil means, do not delete a hidden subtree with C-k.
  1330. When set to the symbol `error', simply throw an error when C-k is
  1331. used to kill (part-of) a headline that has hidden text behind it.
  1332. Any other non-nil value will result in a query to the user, if it is
  1333. OK to kill that hidden subtree. When nil, kill without remorse."
  1334. :group 'org-edit-structure
  1335. :version "24.1"
  1336. :type '(choice
  1337. (const :tag "Do not protect hidden subtrees" nil)
  1338. (const :tag "Protect hidden subtrees with a security query" t)
  1339. (const :tag "Never kill a hidden subtree with C-k" error)))
  1340. (defcustom org-special-ctrl-o t
  1341. "Non-nil means, make `C-o' insert a row in tables."
  1342. :group 'org-edit-structure
  1343. :type 'boolean)
  1344. (defcustom org-catch-invisible-edits nil
  1345. "Check if in invisible region before inserting or deleting a character.
  1346. Valid values are:
  1347. nil Do not check, so just do invisible edits.
  1348. error Throw an error and do nothing.
  1349. show Make point visible, and do the requested edit.
  1350. show-and-error Make point visible, then throw an error and abort the edit.
  1351. smart Make point visible, and do insertion/deletion if it is
  1352. adjacent to visible text and the change feels predictable.
  1353. Never delete a previously invisible character or add in the
  1354. middle or right after an invisible region. Basically, this
  1355. allows insertion and backward-delete right before ellipses.
  1356. FIXME: maybe in this case we should not even show?"
  1357. :group 'org-edit-structure
  1358. :version "24.1"
  1359. :type '(choice
  1360. (const :tag "Do not check" nil)
  1361. (const :tag "Throw error when trying to edit" error)
  1362. (const :tag "Unhide, but do not do the edit" show-and-error)
  1363. (const :tag "Show invisible part and do the edit" show)
  1364. (const :tag "Be smart and do the right thing" smart)))
  1365. (defcustom org-yank-folded-subtrees t
  1366. "Non-nil means when yanking subtrees, fold them.
  1367. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  1368. it starts with a heading and all other headings in it are either children
  1369. or siblings, then fold all the subtrees. However, do this only if no
  1370. text after the yank would be swallowed into a folded tree by this action."
  1371. :group 'org-edit-structure
  1372. :type 'boolean)
  1373. (defcustom org-yank-adjusted-subtrees nil
  1374. "Non-nil means when yanking subtrees, adjust the level.
  1375. With this setting, `org-paste-subtree' is used to insert the subtree, see
  1376. this function for details."
  1377. :group 'org-edit-structure
  1378. :type 'boolean)
  1379. (defcustom org-M-RET-may-split-line '((default . t))
  1380. "Non-nil means M-RET will split the line at the cursor position.
  1381. When nil, it will go to the end of the line before making a
  1382. new line.
  1383. You may also set this option in a different way for different
  1384. contexts. Valid contexts are:
  1385. headline when creating a new headline
  1386. item when creating a new item
  1387. table in a table field
  1388. default the value to be used for all contexts not explicitly
  1389. customized"
  1390. :group 'org-structure
  1391. :group 'org-table
  1392. :type '(choice
  1393. (const :tag "Always" t)
  1394. (const :tag "Never" nil)
  1395. (repeat :greedy t :tag "Individual contexts"
  1396. (cons
  1397. (choice :tag "Context"
  1398. (const headline)
  1399. (const item)
  1400. (const table)
  1401. (const default))
  1402. (boolean)))))
  1403. (defcustom org-insert-heading-respect-content nil
  1404. "Non-nil means insert new headings after the current subtree.
  1405. When nil, the new heading is created directly after the current line.
  1406. The commands \\[org-insert-heading-respect-content] and \\[org-insert-todo-heading-respect-content] turn
  1407. this variable on for the duration of the command."
  1408. :group 'org-structure
  1409. :type 'boolean)
  1410. (defcustom org-blank-before-new-entry '((heading . auto)
  1411. (plain-list-item . auto))
  1412. "Should `org-insert-heading' leave a blank line before new heading/item?
  1413. The value is an alist, with `heading' and `plain-list-item' as CAR,
  1414. and a boolean flag as CDR. The cdr may also be the symbol `auto', in
  1415. which case Org will look at the surrounding headings/items and try to
  1416. make an intelligent decision whether to insert a blank line or not.
  1417. For plain lists, if `org-list-empty-line-terminates-plain-lists' is set,
  1418. the setting here is ignored and no empty line is inserted to avoid breaking
  1419. the list structure."
  1420. :group 'org-edit-structure
  1421. :type '(list
  1422. (cons (const heading)
  1423. (choice (const :tag "Never" nil)
  1424. (const :tag "Always" t)
  1425. (const :tag "Auto" auto)))
  1426. (cons (const plain-list-item)
  1427. (choice (const :tag "Never" nil)
  1428. (const :tag "Always" t)
  1429. (const :tag "Auto" auto)))))
  1430. (defcustom org-insert-heading-hook nil
  1431. "Hook being run after inserting a new heading."
  1432. :group 'org-edit-structure
  1433. :type 'hook)
  1434. (defcustom org-enable-fixed-width-editor t
  1435. "Non-nil means lines starting with \":\" are treated as fixed-width.
  1436. This currently only means they are never auto-wrapped.
  1437. When nil, such lines will be treated like ordinary lines."
  1438. :group 'org-edit-structure
  1439. :type 'boolean)
  1440. (defcustom org-goto-auto-isearch t
  1441. "Non-nil means typing characters in `org-goto' starts incremental search.
  1442. When nil, you can use these keybindings to navigate the buffer:
  1443. q Quit the org-goto interface
  1444. n Go to the next visible heading
  1445. p Go to the previous visible heading
  1446. f Go one heading forward on same level
  1447. b Go one heading backward on same level
  1448. u Go one heading up"
  1449. :group 'org-edit-structure
  1450. :type 'boolean)
  1451. (defgroup org-sparse-trees nil
  1452. "Options concerning sparse trees in Org-mode."
  1453. :tag "Org Sparse Trees"
  1454. :group 'org-structure)
  1455. (defcustom org-highlight-sparse-tree-matches t
  1456. "Non-nil means highlight all matches that define a sparse tree.
  1457. The highlights will automatically disappear the next time the buffer is
  1458. changed by an edit command."
  1459. :group 'org-sparse-trees
  1460. :type 'boolean)
  1461. (defcustom org-remove-highlights-with-change t
  1462. "Non-nil means any change to the buffer will remove temporary highlights.
  1463. Such highlights are created by `org-occur' and `org-clock-display'.
  1464. When nil, `C-c C-c' needs to be used to get rid of the highlights.
  1465. The highlights created by `org-toggle-latex-fragment' always need
  1466. `C-c C-x C-l' to be removed."
  1467. :group 'org-sparse-trees
  1468. :group 'org-time
  1469. :type 'boolean)
  1470. (defcustom org-occur-hook '(org-first-headline-recenter)
  1471. "Hook that is run after `org-occur' has constructed a sparse tree.
  1472. This can be used to recenter the window to show as much of the structure
  1473. as possible."
  1474. :group 'org-sparse-trees
  1475. :type 'hook)
  1476. (defgroup org-imenu-and-speedbar nil
  1477. "Options concerning imenu and speedbar in Org-mode."
  1478. :tag "Org Imenu and Speedbar"
  1479. :group 'org-structure)
  1480. (defcustom org-imenu-depth 2
  1481. "The maximum level for Imenu access to Org-mode headlines.
  1482. This also applied for speedbar access."
  1483. :group 'org-imenu-and-speedbar
  1484. :type 'integer)
  1485. (defgroup org-table nil
  1486. "Options concerning tables in Org-mode."
  1487. :tag "Org Table"
  1488. :group 'org)
  1489. (defcustom org-enable-table-editor 'optimized
  1490. "Non-nil means lines starting with \"|\" are handled by the table editor.
  1491. When nil, such lines will be treated like ordinary lines.
  1492. When equal to the symbol `optimized', the table editor will be optimized to
  1493. do the following:
  1494. - Automatic overwrite mode in front of whitespace in table fields.
  1495. This makes the structure of the table stay in tact as long as the edited
  1496. field does not exceed the column width.
  1497. - Minimize the number of realigns. Normally, the table is aligned each time
  1498. TAB or RET are pressed to move to another field. With optimization this
  1499. happens only if changes to a field might have changed the column width.
  1500. Optimization requires replacing the functions `self-insert-command',
  1501. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  1502. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  1503. very good at guessing when a re-align will be necessary, but you can always
  1504. force one with \\[org-ctrl-c-ctrl-c].
  1505. If you would like to use the optimized version in Org-mode, but the
  1506. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  1507. This variable can be used to turn on and off the table editor during a session,
  1508. but in order to toggle optimization, a restart is required.
  1509. See also the variable `org-table-auto-blank-field'."
  1510. :group 'org-table
  1511. :type '(choice
  1512. (const :tag "off" nil)
  1513. (const :tag "on" t)
  1514. (const :tag "on, optimized" optimized)))
  1515. (defcustom org-self-insert-cluster-for-undo (or (featurep 'xemacs)
  1516. (version<= emacs-version "24.1"))
  1517. "Non-nil means cluster self-insert commands for undo when possible.
  1518. If this is set, then, like in the Emacs command loop, 20 consecutive
  1519. characters will be undone together.
  1520. This is configurable, because there is some impact on typing performance."
  1521. :group 'org-table
  1522. :type 'boolean)
  1523. (defcustom org-table-tab-recognizes-table.el t
  1524. "Non-nil means TAB will automatically notice a table.el table.
  1525. When it sees such a table, it moves point into it and - if necessary -
  1526. calls `table-recognize-table'."
  1527. :group 'org-table-editing
  1528. :type 'boolean)
  1529. (defgroup org-link nil
  1530. "Options concerning links in Org-mode."
  1531. :tag "Org Link"
  1532. :group 'org)
  1533. (defvar org-link-abbrev-alist-local nil
  1534. "Buffer-local version of `org-link-abbrev-alist', which see.
  1535. The value of this is taken from the #+LINK lines.")
  1536. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  1537. (defcustom org-link-abbrev-alist nil
  1538. "Alist of link abbreviations.
  1539. The car of each element is a string, to be replaced at the start of a link.
  1540. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1541. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  1542. [[linkkey:tag][description]]
  1543. The 'linkkey' must be a word word, starting with a letter, followed
  1544. by letters, numbers, '-' or '_'.
  1545. If REPLACE is a string, the tag will simply be appended to create the link.
  1546. If the string contains \"%s\", the tag will be inserted there. If the string
  1547. contains \"%h\", it will cause a url-encoded version of the tag to be inserted
  1548. at that point (see the function `url-hexify-string'). If the string contains
  1549. the specifier \"%(my-function)\", then the custom function `my-function' will
  1550. be invoked: this function takes the tag as its only argument and must return
  1551. a string.
  1552. REPLACE may also be a function that will be called with the tag as the
  1553. only argument to create the link, which should be returned as a string.
  1554. See the manual for examples."
  1555. :group 'org-link
  1556. :type '(repeat
  1557. (cons
  1558. (string :tag "Protocol")
  1559. (choice
  1560. (string :tag "Format")
  1561. (function)))))
  1562. (defcustom org-descriptive-links t
  1563. "Non-nil means Org will display descriptive links.
  1564. E.g. [[http://orgmode.org][Org website]] will be displayed as
  1565. \"Org Website\", hiding the link itself and just displaying its
  1566. description. When set to `nil', Org will display the full links
  1567. literally.
  1568. You can interactively set the value of this variable by calling
  1569. `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
  1570. :group 'org-link
  1571. :type 'boolean)
  1572. (defcustom org-link-file-path-type 'adaptive
  1573. "How the path name in file links should be stored.
  1574. Valid values are:
  1575. relative Relative to the current directory, i.e. the directory of the file
  1576. into which the link is being inserted.
  1577. absolute Absolute path, if possible with ~ for home directory.
  1578. noabbrev Absolute path, no abbreviation of home directory.
  1579. adaptive Use relative path for files in the current directory and sub-
  1580. directories of it. For other files, use an absolute path."
  1581. :group 'org-link
  1582. :type '(choice
  1583. (const relative)
  1584. (const absolute)
  1585. (const noabbrev)
  1586. (const adaptive)))
  1587. (defvaralias 'org-activate-links 'org-highlight-links)
  1588. (defcustom org-highlight-links '(bracket angle plain radio tag date footnote)
  1589. "Types of links that should be highlighted in Org-mode files.
  1590. This is a list of symbols, each one of them leading to the
  1591. highlighting of a certain link type.
  1592. You can still open links that are not highlighted.
  1593. In principle, it does not hurt to turn on highlighting for all
  1594. link types. There may be a small gain when turning off unused
  1595. link types. The types are:
  1596. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1597. angle Links in angular brackets that may contain whitespace like
  1598. <bbdb:Carsten Dominik>.
  1599. plain Plain links in normal text, no whitespace, like http://google.com.
  1600. radio Text that is matched by a radio target, see manual for details.
  1601. tag Tag settings in a headline (link to tag search).
  1602. date Time stamps (link to calendar).
  1603. footnote Footnote labels.
  1604. If you set this variable during an Emacs session, use `org-mode-restart'
  1605. in the Org buffer so that the change takes effect."
  1606. :group 'org-link
  1607. :group 'org-appearance
  1608. :type '(set :greedy t
  1609. (const :tag "Double bracket links" bracket)
  1610. (const :tag "Angular bracket links" angle)
  1611. (const :tag "Plain text links" plain)
  1612. (const :tag "Radio target matches" radio)
  1613. (const :tag "Tags" tag)
  1614. (const :tag "Timestamps" date)
  1615. (const :tag "Footnotes" footnote)))
  1616. (defcustom org-make-link-description-function nil
  1617. "Function to use for generating link descriptions from links.
  1618. When nil, the link location will be used. This function must take
  1619. two parameters: the first one is the link, the second one is the
  1620. description generated by `org-insert-link'. The function should
  1621. return the description to use."
  1622. :group 'org-link
  1623. :type '(choice (const nil) (function)))
  1624. (defgroup org-link-store nil
  1625. "Options concerning storing links in Org-mode."
  1626. :tag "Org Store Link"
  1627. :group 'org-link)
  1628. (defcustom org-url-hexify-p t
  1629. "When non-nil, hexify URL when creating a link."
  1630. :type 'boolean
  1631. :version "24.3"
  1632. :group 'org-link-store)
  1633. (defcustom org-email-link-description-format "Email %c: %.30s"
  1634. "Format of the description part of a link to an email or usenet message.
  1635. The following %-escapes will be replaced by corresponding information:
  1636. %F full \"From\" field
  1637. %f name, taken from \"From\" field, address if no name
  1638. %T full \"To\" field
  1639. %t first name in \"To\" field, address if no name
  1640. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1641. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1642. %s subject
  1643. %d date
  1644. %m message-id.
  1645. You may use normal field width specification between the % and the letter.
  1646. This is for example useful to limit the length of the subject.
  1647. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1648. :group 'org-link-store
  1649. :type 'string)
  1650. (defcustom org-from-is-user-regexp
  1651. (let (r1 r2)
  1652. (when (and user-mail-address (not (string= user-mail-address "")))
  1653. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1654. (when (and user-full-name (not (string= user-full-name "")))
  1655. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1656. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1657. "Regexp matched against the \"From:\" header of an email or usenet message.
  1658. It should match if the message is from the user him/herself."
  1659. :group 'org-link-store
  1660. :type 'regexp)
  1661. (defcustom org-context-in-file-links t
  1662. "Non-nil means file links from `org-store-link' contain context.
  1663. A search string will be added to the file name with :: as separator and
  1664. used to find the context when the link is activated by the command
  1665. `org-open-at-point'. When this option is t, the entire active region
  1666. will be placed in the search string of the file link. If set to a
  1667. positive integer, only the first n lines of context will be stored.
  1668. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1669. negates this setting for the duration of the command."
  1670. :group 'org-link-store
  1671. :type '(choice boolean integer))
  1672. (defcustom org-keep-stored-link-after-insertion nil
  1673. "Non-nil means keep link in list for entire session.
  1674. The command `org-store-link' adds a link pointing to the current
  1675. location to an internal list. These links accumulate during a session.
  1676. The command `org-insert-link' can be used to insert links into any
  1677. Org-mode file (offering completion for all stored links). When this
  1678. option is nil, every link which has been inserted once using \\[org-insert-link]
  1679. will be removed from the list, to make completing the unused links
  1680. more efficient."
  1681. :group 'org-link-store
  1682. :type 'boolean)
  1683. (defgroup org-link-follow nil
  1684. "Options concerning following links in Org-mode."
  1685. :tag "Org Follow Link"
  1686. :group 'org-link)
  1687. (defcustom org-link-translation-function nil
  1688. "Function to translate links with different syntax to Org syntax.
  1689. This can be used to translate links created for example by the Planner
  1690. or emacs-wiki packages to Org syntax.
  1691. The function must accept two parameters, a TYPE containing the link
  1692. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1693. which is everything after the link protocol. It should return a cons
  1694. with possibly modified values of type and path.
  1695. Org contains a function for this, so if you set this variable to
  1696. `org-translate-link-from-planner', you should be able follow many
  1697. links created by planner."
  1698. :group 'org-link-follow
  1699. :type '(choice (const nil) (function)))
  1700. (defcustom org-follow-link-hook nil
  1701. "Hook that is run after a link has been followed."
  1702. :group 'org-link-follow
  1703. :type 'hook)
  1704. (defcustom org-tab-follows-link nil
  1705. "Non-nil means on links TAB will follow the link.
  1706. Needs to be set before org.el is loaded.
  1707. This really should not be used, it does not make sense, and the
  1708. implementation is bad."
  1709. :group 'org-link-follow
  1710. :type 'boolean)
  1711. (defcustom org-return-follows-link nil
  1712. "Non-nil means on links RET will follow the link.
  1713. In tables, the special behavior of RET has precedence."
  1714. :group 'org-link-follow
  1715. :type 'boolean)
  1716. (defcustom org-mouse-1-follows-link
  1717. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1718. "Non-nil means mouse-1 on a link will follow the link.
  1719. A longer mouse click will still set point. Does not work on XEmacs.
  1720. Needs to be set before org.el is loaded."
  1721. :group 'org-link-follow
  1722. :version "24.4"
  1723. :package-version '(Org . "8.3")
  1724. :type '(choice
  1725. (const :tag "A double click follows the link" double)
  1726. (const :tag "Unconditionally follow the link with mouse-1" t)
  1727. (integer :tag "mouse-1 click does not follow the link if longer than N ms" 450)))
  1728. (defcustom org-mark-ring-length 4
  1729. "Number of different positions to be recorded in the ring.
  1730. Changing this requires a restart of Emacs to work correctly."
  1731. :group 'org-link-follow
  1732. :type 'integer)
  1733. (defcustom org-link-search-must-match-exact-headline 'query-to-create
  1734. "Non-nil means internal links in Org files must exactly match a headline.
  1735. When nil, the link search tries to match a phrase with all words
  1736. in the search text."
  1737. :group 'org-link-follow
  1738. :version "24.1"
  1739. :type '(choice
  1740. (const :tag "Use fuzzy text search" nil)
  1741. (const :tag "Match only exact headline" t)
  1742. (const :tag "Match exact headline or query to create it"
  1743. query-to-create)))
  1744. (defcustom org-link-frame-setup
  1745. '((vm . vm-visit-folder-other-frame)
  1746. (vm-imap . vm-visit-imap-folder-other-frame)
  1747. (gnus . org-gnus-no-new-news)
  1748. (file . find-file-other-window)
  1749. (wl . wl-other-frame))
  1750. "Setup the frame configuration for following links.
  1751. When following a link with Emacs, it may often be useful to display
  1752. this link in another window or frame. This variable can be used to
  1753. set this up for the different types of links.
  1754. For VM, use any of
  1755. `vm-visit-folder'
  1756. `vm-visit-folder-other-window'
  1757. `vm-visit-folder-other-frame'
  1758. For Gnus, use any of
  1759. `gnus'
  1760. `gnus-other-frame'
  1761. `org-gnus-no-new-news'
  1762. For FILE, use any of
  1763. `find-file'
  1764. `find-file-other-window'
  1765. `find-file-other-frame'
  1766. For Wanderlust use any of
  1767. `wl'
  1768. `wl-other-frame'
  1769. For the calendar, use the variable `calendar-setup'.
  1770. For BBDB, it is currently only possible to display the matches in
  1771. another window."
  1772. :group 'org-link-follow
  1773. :type '(list
  1774. (cons (const vm)
  1775. (choice
  1776. (const vm-visit-folder)
  1777. (const vm-visit-folder-other-window)
  1778. (const vm-visit-folder-other-frame)))
  1779. (cons (const vm-imap)
  1780. (choice
  1781. (const vm-visit-imap-folder)
  1782. (const vm-visit-imap-folder-other-window)
  1783. (const vm-visit-imap-folder-other-frame)))
  1784. (cons (const gnus)
  1785. (choice
  1786. (const gnus)
  1787. (const gnus-other-frame)
  1788. (const org-gnus-no-new-news)))
  1789. (cons (const file)
  1790. (choice
  1791. (const find-file)
  1792. (const find-file-other-window)
  1793. (const find-file-other-frame)))
  1794. (cons (const wl)
  1795. (choice
  1796. (const wl)
  1797. (const wl-other-frame)))))
  1798. (defcustom org-display-internal-link-with-indirect-buffer nil
  1799. "Non-nil means use indirect buffer to display infile links.
  1800. Activating internal links (from one location in a file to another location
  1801. in the same file) normally just jumps to the location. When the link is
  1802. activated with a \\[universal-argument] prefix (or with mouse-3), the link \
  1803. is displayed in
  1804. another window. When this option is set, the other window actually displays
  1805. an indirect buffer clone of the current buffer, to avoid any visibility
  1806. changes to the current buffer."
  1807. :group 'org-link-follow
  1808. :type 'boolean)
  1809. (defcustom org-open-non-existing-files nil
  1810. "Non-nil means `org-open-file' will open non-existing files.
  1811. When nil, an error will be generated.
  1812. This variable applies only to external applications because they
  1813. might choke on non-existing files. If the link is to a file that
  1814. will be opened in Emacs, the variable is ignored."
  1815. :group 'org-link-follow
  1816. :type 'boolean)
  1817. (defcustom org-open-directory-means-index-dot-org nil
  1818. "Non-nil means a link to a directory really means to index.org.
  1819. When nil, following a directory link will run dired or open a finder/explorer
  1820. window on that directory."
  1821. :group 'org-link-follow
  1822. :type 'boolean)
  1823. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1824. "Non-nil means ask for confirmation before executing shell links.
  1825. Shell links can be dangerous: just think about a link
  1826. [[shell:rm -rf ~/*][Google Search]]
  1827. This link would show up in your Org-mode document as \"Google Search\",
  1828. but really it would remove your entire home directory.
  1829. Therefore we advise against setting this variable to nil.
  1830. Just change it to `y-or-n-p' if you want to confirm with a
  1831. single keystroke rather than having to type \"yes\"."
  1832. :group 'org-link-follow
  1833. :type '(choice
  1834. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1835. (const :tag "with y-or-n (faster)" y-or-n-p)
  1836. (const :tag "no confirmation (dangerous)" nil)))
  1837. (put 'org-confirm-shell-link-function
  1838. 'safe-local-variable
  1839. (lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1840. (defcustom org-confirm-shell-link-not-regexp ""
  1841. "A regexp to skip confirmation for shell links."
  1842. :group 'org-link-follow
  1843. :version "24.1"
  1844. :type 'regexp)
  1845. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1846. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1847. Elisp links can be dangerous: just think about a link
  1848. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1849. This link would show up in your Org-mode document as \"Google Search\",
  1850. but really it would remove your entire home directory.
  1851. Therefore we advise against setting this variable to nil.
  1852. Just change it to `y-or-n-p' if you want to confirm with a
  1853. single keystroke rather than having to type \"yes\"."
  1854. :group 'org-link-follow
  1855. :type '(choice
  1856. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1857. (const :tag "with y-or-n (faster)" y-or-n-p)
  1858. (const :tag "no confirmation (dangerous)" nil)))
  1859. (put 'org-confirm-shell-link-function
  1860. 'safe-local-variable
  1861. (lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1862. (defcustom org-confirm-elisp-link-not-regexp ""
  1863. "A regexp to skip confirmation for Elisp links."
  1864. :group 'org-link-follow
  1865. :version "24.1"
  1866. :type 'regexp)
  1867. (defconst org-file-apps-defaults-gnu
  1868. '((remote . emacs)
  1869. (system . mailcap)
  1870. (t . mailcap))
  1871. "Default file applications on a UNIX or GNU/Linux system.
  1872. See `org-file-apps'.")
  1873. (defconst org-file-apps-defaults-macosx
  1874. '((remote . emacs)
  1875. (t . "open %s")
  1876. (system . "open %s")
  1877. ("ps.gz" . "gv %s")
  1878. ("eps.gz" . "gv %s")
  1879. ("dvi" . "xdvi %s")
  1880. ("fig" . "xfig %s"))
  1881. "Default file applications on a MacOS X system.
  1882. The system \"open\" is known as a default, but we use X11 applications
  1883. for some files for which the OS does not have a good default.
  1884. See `org-file-apps'.")
  1885. (defconst org-file-apps-defaults-windowsnt
  1886. (list
  1887. '(remote . emacs)
  1888. (cons t
  1889. (list (if (featurep 'xemacs)
  1890. 'mswindows-shell-execute
  1891. 'w32-shell-execute)
  1892. "open" 'file))
  1893. (cons 'system
  1894. (list (if (featurep 'xemacs)
  1895. 'mswindows-shell-execute
  1896. 'w32-shell-execute)
  1897. "open" 'file)))
  1898. "Default file applications on a Windows NT system.
  1899. The system \"open\" is used for most files.
  1900. See `org-file-apps'.")
  1901. (defcustom org-file-apps
  1902. '((auto-mode . emacs)
  1903. ("\\.mm\\'" . default)
  1904. ("\\.x?html?\\'" . default)
  1905. ("\\.pdf\\'" . default))
  1906. "External applications for opening `file:path' items in a document.
  1907. Org-mode uses system defaults for different file types, but
  1908. you can use this variable to set the application for a given file
  1909. extension. The entries in this list are cons cells where the car identifies
  1910. files and the cdr the corresponding command. Possible values for the
  1911. file identifier are
  1912. \"string\" A string as a file identifier can be interpreted in different
  1913. ways, depending on its contents:
  1914. - Alphanumeric characters only:
  1915. Match links with this file extension.
  1916. Example: (\"pdf\" . \"evince %s\")
  1917. to open PDFs with evince.
  1918. - Regular expression: Match links where the
  1919. filename matches the regexp. If you want to
  1920. use groups here, use shy groups.
  1921. Example: (\"\\.x?html\\'\" . \"firefox %s\")
  1922. (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1923. to open *.html and *.xhtml with firefox.
  1924. - Regular expression which contains (non-shy) groups:
  1925. Match links where the whole link, including \"::\", and
  1926. anything after that, matches the regexp.
  1927. In a custom command string, %1, %2, etc. are replaced with
  1928. the parts of the link that were matched by the groups.
  1929. For backwards compatibility, if a command string is given
  1930. that does not use any of the group matches, this case is
  1931. handled identically to the second one (i.e. match against
  1932. file name only).
  1933. In a custom lisp form, you can access the group matches with
  1934. (match-string n link).
  1935. Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
  1936. to open [[file:document.pdf::5]] with evince at page 5.
  1937. `directory' Matches a directory
  1938. `remote' Matches a remote file, accessible through tramp or efs.
  1939. Remote files most likely should be visited through Emacs
  1940. because external applications cannot handle such paths.
  1941. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1942. so all files Emacs knows how to handle. Using this with
  1943. command `emacs' will open most files in Emacs. Beware that this
  1944. will also open html files inside Emacs, unless you add
  1945. (\"html\" . default) to the list as well.
  1946. t Default for files not matched by any of the other options.
  1947. `system' The system command to open files, like `open' on Windows
  1948. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1949. that will be selected if you call `C-c C-o' with a double
  1950. \\[universal-argument] \\[universal-argument] prefix.
  1951. Possible values for the command are:
  1952. `emacs' The file will be visited by the current Emacs process.
  1953. `default' Use the default application for this file type, which is the
  1954. association for t in the list, most likely in the system-specific
  1955. part.
  1956. This can be used to overrule an unwanted setting in the
  1957. system-specific variable.
  1958. `system' Use the system command for opening files, like \"open\".
  1959. This command is specified by the entry whose car is `system'.
  1960. Most likely, the system-specific version of this variable
  1961. does define this command, but you can overrule/replace it
  1962. here.
  1963. string A command to be executed by a shell; %s will be replaced
  1964. by the path to the file.
  1965. sexp A Lisp form which will be evaluated. The file path will
  1966. be available in the Lisp variable `file'.
  1967. For more examples, see the system specific constants
  1968. `org-file-apps-defaults-macosx'
  1969. `org-file-apps-defaults-windowsnt'
  1970. `org-file-apps-defaults-gnu'."
  1971. :group 'org-link-follow
  1972. :type '(repeat
  1973. (cons (choice :value ""
  1974. (string :tag "Extension")
  1975. (const :tag "System command to open files" system)
  1976. (const :tag "Default for unrecognized files" t)
  1977. (const :tag "Remote file" remote)
  1978. (const :tag "Links to a directory" directory)
  1979. (const :tag "Any files that have Emacs modes"
  1980. auto-mode))
  1981. (choice :value ""
  1982. (const :tag "Visit with Emacs" emacs)
  1983. (const :tag "Use default" default)
  1984. (const :tag "Use the system command" system)
  1985. (string :tag "Command")
  1986. (sexp :tag "Lisp form")))))
  1987. (defcustom org-doi-server-url "http://dx.doi.org/"
  1988. "The URL of the DOI server."
  1989. :type 'string
  1990. :version "24.3"
  1991. :group 'org-link-follow)
  1992. (defgroup org-refile nil
  1993. "Options concerning refiling entries in Org-mode."
  1994. :tag "Org Refile"
  1995. :group 'org)
  1996. (defcustom org-directory "~/org"
  1997. "Directory with org files.
  1998. This is just a default location to look for Org files. There is no need
  1999. at all to put your files into this directory. It is only used in the
  2000. following situations:
  2001. 1. When a capture template specifies a target file that is not an
  2002. absolute path. The path will then be interpreted relative to
  2003. `org-directory'
  2004. 2. When a capture note is filed away in an interactive way (when exiting the
  2005. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  2006. with `org-directory' as the default path."
  2007. :group 'org-refile
  2008. :group 'org-capture
  2009. :type 'directory)
  2010. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  2011. "Default target for storing notes.
  2012. Used as a fall back file for org-capture.el, for templates that
  2013. do not specify a target file."
  2014. :group 'org-refile
  2015. :group 'org-capture
  2016. :type '(choice
  2017. (const :tag "Default from remember-data-file" nil)
  2018. file))
  2019. (defcustom org-goto-interface 'outline
  2020. "The default interface to be used for `org-goto'.
  2021. Allowed values are:
  2022. outline The interface shows an outline of the relevant file
  2023. and the correct heading is found by moving through
  2024. the outline or by searching with incremental search.
  2025. outline-path-completion Headlines in the current buffer are offered via
  2026. completion. This is the interface also used by
  2027. the refile command."
  2028. :group 'org-refile
  2029. :type '(choice
  2030. (const :tag "Outline" outline)
  2031. (const :tag "Outline-path-completion" outline-path-completion)))
  2032. (defcustom org-goto-max-level 5
  2033. "Maximum target level when running `org-goto' with refile interface."
  2034. :group 'org-refile
  2035. :type 'integer)
  2036. (defcustom org-reverse-note-order nil
  2037. "Non-nil means store new notes at the beginning of a file or entry.
  2038. When nil, new notes will be filed to the end of a file or entry.
  2039. This can also be a list with cons cells of regular expressions that
  2040. are matched against file names, and values."
  2041. :group 'org-capture
  2042. :group 'org-refile
  2043. :type '(choice
  2044. (const :tag "Reverse always" t)
  2045. (const :tag "Reverse never" nil)
  2046. (repeat :tag "By file name regexp"
  2047. (cons regexp boolean))))
  2048. (defcustom org-log-refile nil
  2049. "Information to record when a task is refiled.
  2050. Possible values are:
  2051. nil Don't add anything
  2052. time Add a time stamp to the task
  2053. note Prompt for a note and add it with template `org-log-note-headings'
  2054. This option can also be set with on a per-file-basis with
  2055. #+STARTUP: nologrefile
  2056. #+STARTUP: logrefile
  2057. #+STARTUP: lognoterefile
  2058. You can have local logging settings for a subtree by setting the LOGGING
  2059. property to one or more of these keywords.
  2060. When bulk-refiling from the agenda, the value `note' is forbidden and
  2061. will temporarily be changed to `time'."
  2062. :group 'org-refile
  2063. :group 'org-progress
  2064. :version "24.1"
  2065. :type '(choice
  2066. (const :tag "No logging" nil)
  2067. (const :tag "Record timestamp" time)
  2068. (const :tag "Record timestamp with note." note)))
  2069. (defcustom org-refile-targets nil
  2070. "Targets for refiling entries with \\[org-refile].
  2071. This is a list of cons cells. Each cell contains:
  2072. - a specification of the files to be considered, either a list of files,
  2073. or a symbol whose function or variable value will be used to retrieve
  2074. a file name or a list of file names. If you use `org-agenda-files' for
  2075. that, all agenda files will be scanned for targets. Nil means consider
  2076. headings in the current buffer.
  2077. - A specification of how to find candidate refile targets. This may be
  2078. any of:
  2079. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  2080. This tag has to be present in all target headlines, inheritance will
  2081. not be considered.
  2082. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  2083. todo keyword.
  2084. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  2085. headlines that are refiling targets.
  2086. - a cons cell (:level . N). Any headline of level N is considered a target.
  2087. Note that, when `org-odd-levels-only' is set, level corresponds to
  2088. order in hierarchy, not to the number of stars.
  2089. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  2090. Note that, when `org-odd-levels-only' is set, level corresponds to
  2091. order in hierarchy, not to the number of stars.
  2092. Each element of this list generates a set of possible targets.
  2093. The union of these sets is presented (with completion) to
  2094. the user by `org-refile'.
  2095. You can set the variable `org-refile-target-verify-function' to a function
  2096. to verify each headline found by the simple criteria above.
  2097. When this variable is nil, all top-level headlines in the current buffer
  2098. are used, equivalent to the value `((nil . (:level . 1))'."
  2099. :group 'org-refile
  2100. :type '(repeat
  2101. (cons
  2102. (choice :value org-agenda-files
  2103. (const :tag "All agenda files" org-agenda-files)
  2104. (const :tag "Current buffer" nil)
  2105. (function) (variable) (file))
  2106. (choice :tag "Identify target headline by"
  2107. (cons :tag "Specific tag" (const :value :tag) (string))
  2108. (cons :tag "TODO keyword" (const :value :todo) (string))
  2109. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  2110. (cons :tag "Level number" (const :value :level) (integer))
  2111. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  2112. (defcustom org-refile-target-verify-function nil
  2113. "Function to verify if the headline at point should be a refile target.
  2114. The function will be called without arguments, with point at the
  2115. beginning of the headline. It should return t and leave point
  2116. where it is if the headline is a valid target for refiling.
  2117. If the target should not be selected, the function must return nil.
  2118. In addition to this, it may move point to a place from where the search
  2119. should be continued. For example, the function may decide that the entire
  2120. subtree of the current entry should be excluded and move point to the end
  2121. of the subtree."
  2122. :group 'org-refile
  2123. :type '(choice
  2124. (const nil)
  2125. (function)))
  2126. (defcustom org-refile-use-cache nil
  2127. "Non-nil means cache refile targets to speed up the process.
  2128. The cache for a particular file will be updated automatically when
  2129. the buffer has been killed, or when any of the marker used for flagging
  2130. refile targets no longer points at a live buffer.
  2131. If you have added new entries to a buffer that might themselves be targets,
  2132. you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
  2133. find that easier, `C-u C-u C-u C-c C-w'."
  2134. :group 'org-refile
  2135. :version "24.1"
  2136. :type 'boolean)
  2137. (defcustom org-refile-use-outline-path nil
  2138. "Non-nil means provide refile targets as paths.
  2139. So a level 3 headline will be available as level1/level2/level3.
  2140. When the value is `file', also include the file name (without directory)
  2141. into the path. In this case, you can also stop the completion after
  2142. the file name, to get entries inserted as top level in the file.
  2143. When `full-file-path', include the full file path."
  2144. :group 'org-refile
  2145. :type '(choice
  2146. (const :tag "Not" nil)
  2147. (const :tag "Yes" t)
  2148. (const :tag "Start with file name" file)
  2149. (const :tag "Start with full file path" full-file-path)))
  2150. (defcustom org-outline-path-complete-in-steps t
  2151. "Non-nil means complete the outline path in hierarchical steps.
  2152. When Org-mode uses the refile interface to select an outline path
  2153. \(see variable `org-refile-use-outline-path'), the completion of
  2154. the path can be done is a single go, or if can be done in steps down
  2155. the headline hierarchy. Going in steps is probably the best if you
  2156. do not use a special completion package like `ido' or `icicles'.
  2157. However, when using these packages, going in one step can be very
  2158. fast, while still showing the whole path to the entry."
  2159. :group 'org-refile
  2160. :type 'boolean)
  2161. (defcustom org-refile-allow-creating-parent-nodes nil
  2162. "Non-nil means allow to create new nodes as refile targets.
  2163. New nodes are then created by adding \"/new node name\" to the completion
  2164. of an existing node. When the value of this variable is `confirm',
  2165. new node creation must be confirmed by the user (recommended).
  2166. When nil, the completion must match an existing entry.
  2167. Note that, if the new heading is not seen by the criteria
  2168. listed in `org-refile-targets', multiple instances of the same
  2169. heading would be created by trying again to file under the new
  2170. heading."
  2171. :group 'org-refile
  2172. :type '(choice
  2173. (const :tag "Never" nil)
  2174. (const :tag "Always" t)
  2175. (const :tag "Prompt for confirmation" confirm)))
  2176. (defcustom org-refile-active-region-within-subtree nil
  2177. "Non-nil means also refile active region within a subtree.
  2178. By default `org-refile' doesn't allow refiling regions if they
  2179. don't contain a set of subtrees, but it might be convenient to
  2180. do so sometimes: in that case, the first line of the region is
  2181. converted to a headline before refiling."
  2182. :group 'org-refile
  2183. :version "24.1"
  2184. :type 'boolean)
  2185. (defgroup org-todo nil
  2186. "Options concerning TODO items in Org-mode."
  2187. :tag "Org TODO"
  2188. :group 'org)
  2189. (defgroup org-progress nil
  2190. "Options concerning Progress logging in Org-mode."
  2191. :tag "Org Progress"
  2192. :group 'org-time)
  2193. (defvar org-todo-interpretation-widgets
  2194. '((:tag "Sequence (cycling hits every state)" sequence)
  2195. (:tag "Type (cycling directly to DONE)" type))
  2196. "The available interpretation symbols for customizing `org-todo-keywords'.
  2197. Interested libraries should add to this list.")
  2198. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  2199. "List of TODO entry keyword sequences and their interpretation.
  2200. \\<org-mode-map>This is a list of sequences.
  2201. Each sequence starts with a symbol, either `sequence' or `type',
  2202. indicating if the keywords should be interpreted as a sequence of
  2203. action steps, or as different types of TODO items. The first
  2204. keywords are states requiring action - these states will select a headline
  2205. for inclusion into the global TODO list Org-mode produces. If one of
  2206. the \"keywords\" is the vertical bar, \"|\", the remaining keywords
  2207. signify that no further action is necessary. If \"|\" is not found,
  2208. the last keyword is treated as the only DONE state of the sequence.
  2209. The command \\[org-todo] cycles an entry through these states, and one
  2210. additional state where no keyword is present. For details about this
  2211. cycling, see the manual.
  2212. TODO keywords and interpretation can also be set on a per-file basis with
  2213. the special #+SEQ_TODO and #+TYP_TODO lines.
  2214. Each keyword can optionally specify a character for fast state selection
  2215. \(in combination with the variable `org-use-fast-todo-selection')
  2216. and specifiers for state change logging, using the same syntax that
  2217. is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says that
  2218. the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  2219. indicates to record a time stamp each time this state is selected.
  2220. Each keyword may also specify if a timestamp or a note should be
  2221. recorded when entering or leaving the state, by adding additional
  2222. characters in the parenthesis after the keyword. This looks like this:
  2223. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  2224. record only the time of the state change. With X and Y being either
  2225. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  2226. Y when leaving the state if and only if the *target* state does not
  2227. define X. You may omit any of the fast-selection key or X or /Y,
  2228. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  2229. For backward compatibility, this variable may also be just a list
  2230. of keywords. In this case the interpretation (sequence or type) will be
  2231. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  2232. :group 'org-todo
  2233. :group 'org-keywords
  2234. :type '(choice
  2235. (repeat :tag "Old syntax, just keywords"
  2236. (string :tag "Keyword"))
  2237. (repeat :tag "New syntax"
  2238. (cons
  2239. (choice
  2240. :tag "Interpretation"
  2241. ;;Quick and dirty way to see
  2242. ;;`org-todo-interpretations'. This takes the
  2243. ;;place of item arguments
  2244. :convert-widget
  2245. (lambda (widget)
  2246. (widget-put widget
  2247. :args (mapcar
  2248. (lambda (x)
  2249. (widget-convert
  2250. (cons 'const x)))
  2251. org-todo-interpretation-widgets))
  2252. widget))
  2253. (repeat
  2254. (string :tag "Keyword"))))))
  2255. (defvar org-todo-keywords-1 nil
  2256. "All TODO and DONE keywords active in a buffer.")
  2257. (make-variable-buffer-local 'org-todo-keywords-1)
  2258. (defvar org-todo-keywords-for-agenda nil)
  2259. (defvar org-done-keywords-for-agenda nil)
  2260. (defvar org-todo-keyword-alist-for-agenda nil)
  2261. (defvar org-tag-alist-for-agenda nil
  2262. "Alist of all tags from all agenda files.")
  2263. (defvar org-tag-groups-alist-for-agenda nil
  2264. "Alist of all groups tags from all current agenda files.")
  2265. (defvar org-tag-groups-alist nil)
  2266. (make-variable-buffer-local 'org-tag-groups-alist)
  2267. (defvar org-agenda-contributing-files nil)
  2268. (defvar org-not-done-keywords nil)
  2269. (make-variable-buffer-local 'org-not-done-keywords)
  2270. (defvar org-done-keywords nil)
  2271. (make-variable-buffer-local 'org-done-keywords)
  2272. (defvar org-todo-heads nil)
  2273. (make-variable-buffer-local 'org-todo-heads)
  2274. (defvar org-todo-sets nil)
  2275. (make-variable-buffer-local 'org-todo-sets)
  2276. (defvar org-todo-log-states nil)
  2277. (make-variable-buffer-local 'org-todo-log-states)
  2278. (defvar org-todo-kwd-alist nil)
  2279. (make-variable-buffer-local 'org-todo-kwd-alist)
  2280. (defvar org-todo-key-alist nil)
  2281. (make-variable-buffer-local 'org-todo-key-alist)
  2282. (defvar org-todo-key-trigger nil)
  2283. (make-variable-buffer-local 'org-todo-key-trigger)
  2284. (defcustom org-todo-interpretation 'sequence
  2285. "Controls how TODO keywords are interpreted.
  2286. This variable is in principle obsolete and is only used for
  2287. backward compatibility, if the interpretation of todo keywords is
  2288. not given already in `org-todo-keywords'. See that variable for
  2289. more information."
  2290. :group 'org-todo
  2291. :group 'org-keywords
  2292. :type '(choice (const sequence)
  2293. (const type)))
  2294. (defcustom org-use-fast-todo-selection t
  2295. "Non-nil means use the fast todo selection scheme with C-c C-t.
  2296. This variable describes if and under what circumstances the cycling
  2297. mechanism for TODO keywords will be replaced by a single-key, direct
  2298. selection scheme.
  2299. When nil, fast selection is never used.
  2300. When the symbol `prefix', it will be used when `org-todo' is called
  2301. with a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and
  2302. `C-u t' in an agenda buffer.
  2303. When t, fast selection is used by default. In this case, the prefix
  2304. argument forces cycling instead.
  2305. In all cases, the special interface is only used if access keys have
  2306. actually been assigned by the user, i.e. if keywords in the configuration
  2307. are followed by a letter in parenthesis, like TODO(t)."
  2308. :group 'org-todo
  2309. :type '(choice
  2310. (const :tag "Never" nil)
  2311. (const :tag "By default" t)
  2312. (const :tag "Only with C-u C-c C-t" prefix)))
  2313. (defcustom org-provide-todo-statistics t
  2314. "Non-nil means update todo statistics after insert and toggle.
  2315. ALL-HEADLINES means update todo statistics by including headlines
  2316. with no TODO keyword as well, counting them as not done.
  2317. A list of TODO keywords means the same, but skip keywords that are
  2318. not in this list.
  2319. When set to a list of two lists, the first list contains keywords
  2320. to consider as TODO keywords, the second list contains keywords
  2321. to consider as DONE keywords.
  2322. When this is set, todo statistics is updated in the parent of the
  2323. current entry each time a todo state is changed."
  2324. :group 'org-todo
  2325. :type '(choice
  2326. (const :tag "Yes, only for TODO entries" t)
  2327. (const :tag "Yes, including all entries" all-headlines)
  2328. (repeat :tag "Yes, for TODOs in this list"
  2329. (string :tag "TODO keyword"))
  2330. (list :tag "Yes, for TODOs and DONEs in these lists"
  2331. (repeat (string :tag "TODO keyword"))
  2332. (repeat (string :tag "DONE keyword")))
  2333. (other :tag "No TODO statistics" nil)))
  2334. (defcustom org-hierarchical-todo-statistics t
  2335. "Non-nil means TODO statistics covers just direct children.
  2336. When nil, all entries in the subtree are considered.
  2337. This has only an effect if `org-provide-todo-statistics' is set.
  2338. To set this to nil for only a single subtree, use a COOKIE_DATA
  2339. property and include the word \"recursive\" into the value."
  2340. :group 'org-todo
  2341. :type 'boolean)
  2342. (defcustom org-after-todo-state-change-hook nil
  2343. "Hook which is run after the state of a TODO item was changed.
  2344. The new state (a string with a TODO keyword, or nil) is available in the
  2345. Lisp variable `org-state'."
  2346. :group 'org-todo
  2347. :type 'hook)
  2348. (defvar org-blocker-hook nil
  2349. "Hook for functions that are allowed to block a state change.
  2350. Functions in this hook should not modify the buffer.
  2351. Each function gets as its single argument a property list,
  2352. see `org-trigger-hook' for more information about this list.
  2353. If any of the functions in this hook returns nil, the state change
  2354. is blocked.")
  2355. (defvar org-trigger-hook nil
  2356. "Hook for functions that are triggered by a state change.
  2357. Each function gets as its single argument a property list with at
  2358. least the following elements:
  2359. (:type type-of-change :position pos-at-entry-start
  2360. :from old-state :to new-state)
  2361. Depending on the type, more properties may be present.
  2362. This mechanism is currently implemented for:
  2363. TODO state changes
  2364. ------------------
  2365. :type todo-state-change
  2366. :from previous state (keyword as a string), or nil, or a symbol
  2367. 'todo' or 'done', to indicate the general type of state.
  2368. :to new state, like in :from")
  2369. (defcustom org-enforce-todo-dependencies nil
  2370. "Non-nil means undone TODO entries will block switching the parent to DONE.
  2371. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  2372. be blocked if any prior sibling is not yet done.
  2373. Finally, if the parent is blocked because of ordered siblings of its own,
  2374. the child will also be blocked."
  2375. :set (lambda (var val)
  2376. (set var val)
  2377. (if val
  2378. (add-hook 'org-blocker-hook
  2379. 'org-block-todo-from-children-or-siblings-or-parent)
  2380. (remove-hook 'org-blocker-hook
  2381. 'org-block-todo-from-children-or-siblings-or-parent)))
  2382. :group 'org-todo
  2383. :type 'boolean)
  2384. (defcustom org-enforce-todo-checkbox-dependencies nil
  2385. "Non-nil means unchecked boxes will block switching the parent to DONE.
  2386. When this is nil, checkboxes have no influence on switching TODO states.
  2387. When non-nil, you first need to check off all check boxes before the TODO
  2388. entry can be switched to DONE.
  2389. This variable needs to be set before org.el is loaded, and you need to
  2390. restart Emacs after a change to make the change effective. The only way
  2391. to change is while Emacs is running is through the customize interface."
  2392. :set (lambda (var val)
  2393. (set var val)
  2394. (if val
  2395. (add-hook 'org-blocker-hook
  2396. 'org-block-todo-from-checkboxes)
  2397. (remove-hook 'org-blocker-hook
  2398. 'org-block-todo-from-checkboxes)))
  2399. :group 'org-todo
  2400. :type 'boolean)
  2401. (defcustom org-treat-insert-todo-heading-as-state-change nil
  2402. "Non-nil means inserting a TODO heading is treated as state change.
  2403. So when the command \\[org-insert-todo-heading] is used, state change
  2404. logging will apply if appropriate. When nil, the new TODO item will
  2405. be inserted directly, and no logging will take place."
  2406. :group 'org-todo
  2407. :type 'boolean)
  2408. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  2409. "Non-nil means switching TODO states with S-cursor counts as state change.
  2410. This is the default behavior. However, setting this to nil allows a
  2411. convenient way to select a TODO state and bypass any logging associated
  2412. with that."
  2413. :group 'org-todo
  2414. :type 'boolean)
  2415. (defcustom org-todo-state-tags-triggers nil
  2416. "Tag changes that should be triggered by TODO state changes.
  2417. This is a list. Each entry is
  2418. (state-change (tag . flag) .......)
  2419. State-change can be a string with a state, and empty string to indicate the
  2420. state that has no TODO keyword, or it can be one of the symbols `todo'
  2421. or `done', meaning any not-done or done state, respectively."
  2422. :group 'org-todo
  2423. :group 'org-tags
  2424. :type '(repeat
  2425. (cons (choice :tag "When changing to"
  2426. (const :tag "Not-done state" todo)
  2427. (const :tag "Done state" done)
  2428. (string :tag "State"))
  2429. (repeat
  2430. (cons :tag "Tag action"
  2431. (string :tag "Tag")
  2432. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  2433. (defcustom org-log-done nil
  2434. "Information to record when a task moves to the DONE state.
  2435. Possible values are:
  2436. nil Don't add anything, just change the keyword
  2437. time Add a time stamp to the task
  2438. note Prompt for a note and add it with template `org-log-note-headings'
  2439. This option can also be set with on a per-file-basis with
  2440. #+STARTUP: nologdone
  2441. #+STARTUP: logdone
  2442. #+STARTUP: lognotedone
  2443. You can have local logging settings for a subtree by setting the LOGGING
  2444. property to one or more of these keywords."
  2445. :group 'org-todo
  2446. :group 'org-progress
  2447. :type '(choice
  2448. (const :tag "No logging" nil)
  2449. (const :tag "Record CLOSED timestamp" time)
  2450. (const :tag "Record CLOSED timestamp with note." note)))
  2451. ;; Normalize old uses of org-log-done.
  2452. (cond
  2453. ((eq org-log-done t) (setq org-log-done 'time))
  2454. ((and (listp org-log-done) (memq 'done org-log-done))
  2455. (setq org-log-done 'note)))
  2456. (defcustom org-log-reschedule nil
  2457. "Information to record when the scheduling date of a tasks is modified.
  2458. Possible values are:
  2459. nil Don't add anything, just change the date
  2460. time Add a time stamp to the task
  2461. note Prompt for a note and add it with template `org-log-note-headings'
  2462. This option can also be set with on a per-file-basis with
  2463. #+STARTUP: nologreschedule
  2464. #+STARTUP: logreschedule
  2465. #+STARTUP: lognotereschedule"
  2466. :group 'org-todo
  2467. :group 'org-progress
  2468. :type '(choice
  2469. (const :tag "No logging" nil)
  2470. (const :tag "Record timestamp" time)
  2471. (const :tag "Record timestamp with note." note)))
  2472. (defcustom org-log-redeadline nil
  2473. "Information to record when the deadline date of a tasks is modified.
  2474. Possible values are:
  2475. nil Don't add anything, just change the date
  2476. time Add a time stamp to the task
  2477. note Prompt for a note and add it with template `org-log-note-headings'
  2478. This option can also be set with on a per-file-basis with
  2479. #+STARTUP: nologredeadline
  2480. #+STARTUP: logredeadline
  2481. #+STARTUP: lognoteredeadline
  2482. You can have local logging settings for a subtree by setting the LOGGING
  2483. property to one or more of these keywords."
  2484. :group 'org-todo
  2485. :group 'org-progress
  2486. :type '(choice
  2487. (const :tag "No logging" nil)
  2488. (const :tag "Record timestamp" time)
  2489. (const :tag "Record timestamp with note." note)))
  2490. (defcustom org-log-note-clock-out nil
  2491. "Non-nil means record a note when clocking out of an item.
  2492. This can also be configured on a per-file basis by adding one of
  2493. the following lines anywhere in the buffer:
  2494. #+STARTUP: lognoteclock-out
  2495. #+STARTUP: nolognoteclock-out"
  2496. :group 'org-todo
  2497. :group 'org-progress
  2498. :type 'boolean)
  2499. (defcustom org-log-done-with-time t
  2500. "Non-nil means the CLOSED time stamp will contain date and time.
  2501. When nil, only the date will be recorded."
  2502. :group 'org-progress
  2503. :type 'boolean)
  2504. (defcustom org-log-note-headings
  2505. '((done . "CLOSING NOTE %t")
  2506. (state . "State %-12s from %-12S %t")
  2507. (note . "Note taken on %t")
  2508. (reschedule . "Rescheduled from %S on %t")
  2509. (delschedule . "Not scheduled, was %S on %t")
  2510. (redeadline . "New deadline from %S on %t")
  2511. (deldeadline . "Removed deadline, was %S on %t")
  2512. (refile . "Refiled on %t")
  2513. (clock-out . ""))
  2514. "Headings for notes added to entries.
  2515. The value is an alist, with the car being a symbol indicating the note
  2516. context, and the cdr is the heading to be used. The heading may also be the
  2517. empty string.
  2518. %t in the heading will be replaced by a time stamp.
  2519. %T will be an active time stamp instead the default inactive one
  2520. %d will be replaced by a short-format time stamp.
  2521. %D will be replaced by an active short-format time stamp.
  2522. %s will be replaced by the new TODO state, in double quotes.
  2523. %S will be replaced by the old TODO state, in double quotes.
  2524. %u will be replaced by the user name.
  2525. %U will be replaced by the full user name.
  2526. In fact, it is not a good idea to change the `state' entry, because
  2527. agenda log mode depends on the format of these entries."
  2528. :group 'org-todo
  2529. :group 'org-progress
  2530. :type '(list :greedy t
  2531. (cons (const :tag "Heading when closing an item" done) string)
  2532. (cons (const :tag
  2533. "Heading when changing todo state (todo sequence only)"
  2534. state) string)
  2535. (cons (const :tag "Heading when just taking a note" note) string)
  2536. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2537. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2538. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2539. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2540. (cons (const :tag "Heading when refiling" refile) string)
  2541. (cons (const :tag "Heading when clocking out" clock-out) string)))
  2542. (unless (assq 'note org-log-note-headings)
  2543. (push '(note . "%t") org-log-note-headings))
  2544. (defcustom org-log-into-drawer nil
  2545. "Non-nil means insert state change notes and time stamps into a drawer.
  2546. When nil, state changes notes will be inserted after the headline and
  2547. any scheduling and clock lines, but not inside a drawer.
  2548. The value of this variable should be the name of the drawer to use.
  2549. LOGBOOK is proposed as the default drawer for this purpose, you can
  2550. also set this to a string to define the drawer of your choice.
  2551. A value of t is also allowed, representing \"LOGBOOK\".
  2552. A value of t or nil can also be set with on a per-file-basis with
  2553. #+STARTUP: logdrawer
  2554. #+STARTUP: nologdrawer
  2555. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2556. will be ignored.
  2557. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2558. a subtree."
  2559. :group 'org-todo
  2560. :group 'org-progress
  2561. :type '(choice
  2562. (const :tag "Not into a drawer" nil)
  2563. (const :tag "LOGBOOK" t)
  2564. (string :tag "Other")))
  2565. (org-defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer)
  2566. (defun org-log-into-drawer ()
  2567. "Return the value of `org-log-into-drawer', but let properties overrule.
  2568. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  2569. used instead of the default value."
  2570. (let ((p (org-entry-get nil "LOG_INTO_DRAWER" 'inherit t)))
  2571. (cond
  2572. ((not p) org-log-into-drawer)
  2573. ((equal p "nil") nil)
  2574. ((equal p "t") "LOGBOOK")
  2575. (t p))))
  2576. (defcustom org-log-state-notes-insert-after-drawers nil
  2577. "Non-nil means insert state change notes after any drawers in entry.
  2578. Only the drawers that *immediately* follow the headline and the
  2579. deadline/scheduled line are skipped.
  2580. When nil, insert notes right after the heading and perhaps the line
  2581. with deadline/scheduling if present.
  2582. This variable will have no effect if `org-log-into-drawer' is
  2583. set."
  2584. :group 'org-todo
  2585. :group 'org-progress
  2586. :type 'boolean)
  2587. (defcustom org-log-states-order-reversed t
  2588. "Non-nil means the latest state note will be directly after heading.
  2589. When nil, the state change notes will be ordered according to time.
  2590. This option can also be set with on a per-file-basis with
  2591. #+STARTUP: logstatesreversed
  2592. #+STARTUP: nologstatesreversed"
  2593. :group 'org-todo
  2594. :group 'org-progress
  2595. :type 'boolean)
  2596. (defcustom org-todo-repeat-to-state nil
  2597. "The TODO state to which a repeater should return the repeating task.
  2598. By default this is the first task in a TODO sequence, or the previous state
  2599. in a TODO_TYP set. But you can specify another task here.
  2600. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2601. :group 'org-todo
  2602. :version "24.1"
  2603. :type '(choice (const :tag "Head of sequence" nil)
  2604. (string :tag "Specific state")))
  2605. (defcustom org-log-repeat 'time
  2606. "Non-nil means record moving through the DONE state when triggering repeat.
  2607. An auto-repeating task is immediately switched back to TODO when
  2608. marked DONE. If you are not logging state changes (by adding \"@\"
  2609. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2610. record a closing note, there will be no record of the task moving
  2611. through DONE. This variable forces taking a note anyway.
  2612. nil Don't force a record
  2613. time Record a time stamp
  2614. note Prompt for a note and add it with template `org-log-note-headings'
  2615. This option can also be set with on a per-file-basis with
  2616. #+STARTUP: nologrepeat
  2617. #+STARTUP: logrepeat
  2618. #+STARTUP: lognoterepeat
  2619. You can have local logging settings for a subtree by setting the LOGGING
  2620. property to one or more of these keywords."
  2621. :group 'org-todo
  2622. :group 'org-progress
  2623. :type '(choice
  2624. (const :tag "Don't force a record" nil)
  2625. (const :tag "Force recording the DONE state" time)
  2626. (const :tag "Force recording a note with the DONE state" note)))
  2627. (defgroup org-priorities nil
  2628. "Priorities in Org-mode."
  2629. :tag "Org Priorities"
  2630. :group 'org-todo)
  2631. (defcustom org-enable-priority-commands t
  2632. "Non-nil means priority commands are active.
  2633. When nil, these commands will be disabled, so that you never accidentally
  2634. set a priority."
  2635. :group 'org-priorities
  2636. :type 'boolean)
  2637. (defcustom org-highest-priority ?A
  2638. "The highest priority of TODO items. A character like ?A, ?B etc.
  2639. Must have a smaller ASCII number than `org-lowest-priority'."
  2640. :group 'org-priorities
  2641. :type 'character)
  2642. (defcustom org-lowest-priority ?C
  2643. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2644. Must have a larger ASCII number than `org-highest-priority'."
  2645. :group 'org-priorities
  2646. :type 'character)
  2647. (defcustom org-default-priority ?B
  2648. "The default priority of TODO items.
  2649. This is the priority an item gets if no explicit priority is given.
  2650. When starting to cycle on an empty priority the first step in the cycle
  2651. depends on `org-priority-start-cycle-with-default'. The resulting first
  2652. step priority must not exceed the range from `org-highest-priority' to
  2653. `org-lowest-priority' which means that `org-default-priority' has to be
  2654. in this range exclusive or inclusive the range boundaries. Else the
  2655. first step refuses to set the default and the second will fall back
  2656. to (depending on the command used) the highest or lowest priority."
  2657. :group 'org-priorities
  2658. :type 'character)
  2659. (defcustom org-priority-start-cycle-with-default t
  2660. "Non-nil means start with default priority when starting to cycle.
  2661. When this is nil, the first step in the cycle will be (depending on the
  2662. command used) one higher or lower than the default priority.
  2663. See also `org-default-priority'."
  2664. :group 'org-priorities
  2665. :type 'boolean)
  2666. (defcustom org-get-priority-function nil
  2667. "Function to extract the priority from a string.
  2668. The string is normally the headline. If this is nil Org computes the
  2669. priority from the priority cookie like [#A] in the headline. It returns
  2670. an integer, increasing by 1000 for each priority level.
  2671. The user can set a different function here, which should take a string
  2672. as an argument and return the numeric priority."
  2673. :group 'org-priorities
  2674. :version "24.1"
  2675. :type '(choice
  2676. (const nil)
  2677. (function)))
  2678. (defgroup org-time nil
  2679. "Options concerning time stamps and deadlines in Org-mode."
  2680. :tag "Org Time"
  2681. :group 'org)
  2682. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2683. "Number of minutes to round time stamps to.
  2684. These are two values, the first applies when first creating a time stamp.
  2685. The second applies when changing it with the commands `S-up' and `S-down'.
  2686. When changing the time stamp, this means that it will change in steps
  2687. of N minutes, as given by the second value.
  2688. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2689. numbers should be factors of 60, so for example 5, 10, 15.
  2690. When this is larger than 1, you can still force an exact time stamp by using
  2691. a double prefix argument to a time stamp command like `C-c .' or `C-c !',
  2692. and by using a prefix arg to `S-up/down' to specify the exact number
  2693. of minutes to shift."
  2694. :group 'org-time
  2695. :get (lambda (var) ; Make sure both elements are there
  2696. (if (integerp (default-value var))
  2697. (list (default-value var) 5)
  2698. (default-value var)))
  2699. :type '(list
  2700. (integer :tag "when inserting times")
  2701. (integer :tag "when modifying times")))
  2702. ;; Normalize old customizations of this variable.
  2703. (when (integerp org-time-stamp-rounding-minutes)
  2704. (setq org-time-stamp-rounding-minutes
  2705. (list org-time-stamp-rounding-minutes
  2706. org-time-stamp-rounding-minutes)))
  2707. (defcustom org-display-custom-times nil
  2708. "Non-nil means overlay custom formats over all time stamps.
  2709. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2710. To turn this on on a per-file basis, insert anywhere in the file:
  2711. #+STARTUP: customtime"
  2712. :group 'org-time
  2713. :set 'set-default
  2714. :type 'sexp)
  2715. (make-variable-buffer-local 'org-display-custom-times)
  2716. (defcustom org-time-stamp-custom-formats
  2717. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2718. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2719. These are overlaid over the default ISO format if the variable
  2720. `org-display-custom-times' is set. Time like %H:%M should be at the
  2721. end of the second format. The custom formats are also honored by export
  2722. commands, if custom time display is turned on at the time of export."
  2723. :group 'org-time
  2724. :type 'sexp)
  2725. (defun org-time-stamp-format (&optional long inactive)
  2726. "Get the right format for a time string."
  2727. (let ((f (if long (cdr org-time-stamp-formats)
  2728. (car org-time-stamp-formats))))
  2729. (if inactive
  2730. (concat "[" (substring f 1 -1) "]")
  2731. f)))
  2732. (defcustom org-time-clocksum-format
  2733. '(:days "%dd " :hours "%d" :require-hours t :minutes ":%02d" :require-minutes t)
  2734. "The format string used when creating CLOCKSUM lines.
  2735. This is also used when Org mode generates a time duration.
  2736. The value can be a single format string containing two
  2737. %-sequences, which will be filled with the number of hours and
  2738. minutes in that order.
  2739. Alternatively, the value can be a plist associating any of the
  2740. keys :years, :months, :weeks, :days, :hours or :minutes with
  2741. format strings. The time duration is formatted using only the
  2742. time components that are needed and concatenating the results.
  2743. If a time unit in absent, it falls back to the next smallest
  2744. unit.
  2745. The keys :require-years, :require-months, :require-days,
  2746. :require-weeks, :require-hours, :require-minutes are also
  2747. meaningful. A non-nil value for these keys indicates that the
  2748. corresponding time component should always be included, even if
  2749. its value is 0.
  2750. For example,
  2751. \(:days \"%dd\" :hours \"%d\" :require-hours t :minutes \":%02d\"
  2752. :require-minutes t)
  2753. means durations longer than a day will be expressed in days,
  2754. hours and minutes, and durations less than a day will always be
  2755. expressed in hours and minutes (even for durations less than an
  2756. hour).
  2757. The value
  2758. \(:days \"%dd\" :minutes \"%dm\")
  2759. means durations longer than a day will be expressed in days and
  2760. minutes, and durations less than a day will be expressed entirely
  2761. in minutes (even for durations longer than an hour)."
  2762. :group 'org-time
  2763. :group 'org-clock
  2764. :version "24.4"
  2765. :package-version '(Org . "8.0")
  2766. :type '(choice (string :tag "Format string")
  2767. (set :tag "Plist"
  2768. (group :inline t (const :tag "Years" :years)
  2769. (string :tag "Format string"))
  2770. (group :inline t
  2771. (const :tag "Always show years" :require-years)
  2772. (const t))
  2773. (group :inline t (const :tag "Months" :months)
  2774. (string :tag "Format string"))
  2775. (group :inline t
  2776. (const :tag "Always show months" :require-months)
  2777. (const t))
  2778. (group :inline t (const :tag "Weeks" :weeks)
  2779. (string :tag "Format string"))
  2780. (group :inline t
  2781. (const :tag "Always show weeks" :require-weeks)
  2782. (const t))
  2783. (group :inline t (const :tag "Days" :days)
  2784. (string :tag "Format string"))
  2785. (group :inline t
  2786. (const :tag "Always show days" :require-days)
  2787. (const t))
  2788. (group :inline t (const :tag "Hours" :hours)
  2789. (string :tag "Format string"))
  2790. (group :inline t
  2791. (const :tag "Always show hours" :require-hours)
  2792. (const t))
  2793. (group :inline t (const :tag "Minutes" :minutes)
  2794. (string :tag "Format string"))
  2795. (group :inline t
  2796. (const :tag "Always show minutes" :require-minutes)
  2797. (const t)))))
  2798. (defcustom org-time-clocksum-use-fractional nil
  2799. "When non-nil, \\[org-clock-display] uses fractional times.
  2800. See `org-time-clocksum-format' for more on time clock formats."
  2801. :group 'org-time
  2802. :group 'org-clock
  2803. :version "24.3"
  2804. :type 'boolean)
  2805. (defcustom org-time-clocksum-use-effort-durations nil
  2806. "When non-nil, \\[org-clock-display] uses effort durations.
  2807. E.g. by default, one day is considered to be a 8 hours effort,
  2808. so a task that has been clocked for 16 hours will be displayed
  2809. as during 2 days in the clock display or in the clocktable.
  2810. See `org-effort-durations' on how to set effort durations
  2811. and `org-time-clocksum-format' for more on time clock formats."
  2812. :group 'org-time
  2813. :group 'org-clock
  2814. :version "24.4"
  2815. :package-version '(Org . "8.0")
  2816. :type 'boolean)
  2817. (defcustom org-time-clocksum-fractional-format "%.2f"
  2818. "The format string used when creating CLOCKSUM lines,
  2819. or when Org mode generates a time duration, if
  2820. `org-time-clocksum-use-fractional' is enabled.
  2821. The value can be a single format string containing one
  2822. %-sequence, which will be filled with the number of hours as
  2823. a float.
  2824. Alternatively, the value can be a plist associating any of the
  2825. keys :years, :months, :weeks, :days, :hours or :minutes with
  2826. a format string. The time duration is formatted using the
  2827. largest time unit which gives a non-zero integer part. If all
  2828. specified formats have zero integer part, the smallest time unit
  2829. is used."
  2830. :group 'org-time
  2831. :type '(choice (string :tag "Format string")
  2832. (set (group :inline t (const :tag "Years" :years)
  2833. (string :tag "Format string"))
  2834. (group :inline t (const :tag "Months" :months)
  2835. (string :tag "Format string"))
  2836. (group :inline t (const :tag "Weeks" :weeks)
  2837. (string :tag "Format string"))
  2838. (group :inline t (const :tag "Days" :days)
  2839. (string :tag "Format string"))
  2840. (group :inline t (const :tag "Hours" :hours)
  2841. (string :tag "Format string"))
  2842. (group :inline t (const :tag "Minutes" :minutes)
  2843. (string :tag "Format string")))))
  2844. (defcustom org-deadline-warning-days 14
  2845. "Number of days before expiration during which a deadline becomes active.
  2846. This variable governs the display in sparse trees and in the agenda.
  2847. When 0 or negative, it means use this number (the absolute value of it)
  2848. even if a deadline has a different individual lead time specified.
  2849. Custom commands can set this variable in the options section."
  2850. :group 'org-time
  2851. :group 'org-agenda-daily/weekly
  2852. :type 'integer)
  2853. (defcustom org-scheduled-delay-days 0
  2854. "Number of days before a scheduled item becomes active.
  2855. This variable governs the display in sparse trees and in the agenda.
  2856. The default value (i.e. 0) means: don't delay scheduled item.
  2857. When negative, it means use this number (the absolute value of it)
  2858. even if a scheduled item has a different individual delay time
  2859. specified.
  2860. Custom commands can set this variable in the options section."
  2861. :group 'org-time
  2862. :group 'org-agenda-daily/weekly
  2863. :version "24.4"
  2864. :package-version '(Org . "8.0")
  2865. :type 'integer)
  2866. (defcustom org-read-date-prefer-future t
  2867. "Non-nil means assume future for incomplete date input from user.
  2868. This affects the following situations:
  2869. 1. The user gives a month but not a year.
  2870. For example, if it is April and you enter \"feb 2\", this will be read
  2871. as Feb 2, *next* year. \"May 5\", however, will be this year.
  2872. 2. The user gives a day, but no month.
  2873. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2874. read this as the third of *next* month. However, if you enter \"17\",
  2875. it will be considered as *this* month.
  2876. If you set this variable to the symbol `time', then also the following
  2877. will work:
  2878. 3. If the user gives a time.
  2879. If the time is before now, it will be interpreted as tomorrow.
  2880. Currently none of this works for ISO week specifications.
  2881. When this option is nil, the current day, month and year will always be
  2882. used as defaults.
  2883. See also `org-agenda-jump-prefer-future'."
  2884. :group 'org-time
  2885. :type '(choice
  2886. (const :tag "Never" nil)
  2887. (const :tag "Check month and day" t)
  2888. (const :tag "Check month, day, and time" time)))
  2889. (defcustom org-agenda-jump-prefer-future 'org-read-date-prefer-future
  2890. "Should the agenda jump command prefer the future for incomplete dates?
  2891. The default is to do the same as configured in `org-read-date-prefer-future'.
  2892. But you can also set a deviating value here.
  2893. This may t or nil, or the symbol `org-read-date-prefer-future'."
  2894. :group 'org-agenda
  2895. :group 'org-time
  2896. :version "24.1"
  2897. :type '(choice
  2898. (const :tag "Use org-read-date-prefer-future"
  2899. org-read-date-prefer-future)
  2900. (const :tag "Never" nil)
  2901. (const :tag "Always" t)))
  2902. (defcustom org-read-date-force-compatible-dates t
  2903. "Should date/time prompt force dates that are guaranteed to work in Emacs?
  2904. Depending on the system Emacs is running on, certain dates cannot
  2905. be represented with the type used internally to represent time.
  2906. Dates between 1970-1-1 and 2038-1-1 can always be represented
  2907. correctly. Some systems allow for earlier dates, some for later,
  2908. some for both. One way to find out it to insert any date into an
  2909. Org buffer, putting the cursor on the year and hitting S-up and
  2910. S-down to test the range.
  2911. When this variable is set to t, the date/time prompt will not let
  2912. you specify dates outside the 1970-2037 range, so it is certain that
  2913. these dates will work in whatever version of Emacs you are
  2914. running, and also that you can move a file from one Emacs implementation
  2915. to another. WHenever Org is forcing the year for you, it will display
  2916. a message and beep.
  2917. When this variable is nil, Org will check if the date is
  2918. representable in the specific Emacs implementation you are using.
  2919. If not, it will force a year, usually the current year, and beep
  2920. to remind you. Currently this setting is not recommended because
  2921. the likelihood that you will open your Org files in an Emacs that
  2922. has limited date range is not negligible.
  2923. A workaround for this problem is to use diary sexp dates for time
  2924. stamps outside of this range."
  2925. :group 'org-time
  2926. :version "24.1"
  2927. :type 'boolean)
  2928. (defcustom org-read-date-display-live t
  2929. "Non-nil means display current interpretation of date prompt live.
  2930. This display will be in an overlay, in the minibuffer."
  2931. :group 'org-time
  2932. :type 'boolean)
  2933. (defcustom org-read-date-popup-calendar t
  2934. "Non-nil means pop up a calendar when prompting for a date.
  2935. In the calendar, the date can be selected with mouse-1. However, the
  2936. minibuffer will also be active, and you can simply enter the date as well.
  2937. When nil, only the minibuffer will be available."
  2938. :group 'org-time
  2939. :type 'boolean)
  2940. (org-defvaralias 'org-popup-calendar-for-date-prompt
  2941. 'org-read-date-popup-calendar)
  2942. (make-obsolete-variable
  2943. 'org-read-date-minibuffer-setup-hook
  2944. "Set `org-read-date-minibuffer-local-map' instead." "24.4")
  2945. (defcustom org-read-date-minibuffer-setup-hook nil
  2946. "Hook to be used to set up keys for the date/time interface.
  2947. Add key definitions to `minibuffer-local-map', which will be a
  2948. temporary copy.
  2949. WARNING: This option is obsolete, you should use
  2950. `org-read-date-minibuffer-local-map' to set up keys."
  2951. :group 'org-time
  2952. :type 'hook)
  2953. (defcustom org-extend-today-until 0
  2954. "The hour when your day really ends. Must be an integer.
  2955. This has influence for the following applications:
  2956. - When switching the agenda to \"today\". It it is still earlier than
  2957. the time given here, the day recognized as TODAY is actually yesterday.
  2958. - When a date is read from the user and it is still before the time given
  2959. here, the current date and time will be assumed to be yesterday, 23:59.
  2960. Also, timestamps inserted in capture templates follow this rule.
  2961. IMPORTANT: This is a feature whose implementation is and likely will
  2962. remain incomplete. Really, it is only here because past midnight seems to
  2963. be the favorite working time of John Wiegley :-)"
  2964. :group 'org-time
  2965. :type 'integer)
  2966. (defcustom org-use-effective-time nil
  2967. "If non-nil, consider `org-extend-today-until' when creating timestamps.
  2968. For example, if `org-extend-today-until' is 8, and it's 4am, then the
  2969. \"effective time\" of any timestamps between midnight and 8am will be
  2970. 23:59 of the previous day."
  2971. :group 'org-time
  2972. :version "24.1"
  2973. :type 'boolean)
  2974. (defcustom org-use-last-clock-out-time-as-effective-time nil
  2975. "When non-nil, use the last clock out time for `org-todo'.
  2976. Note that this option has precedence over the combined use of
  2977. `org-use-effective-time' and `org-extend-today-until'."
  2978. :group 'org-time
  2979. :version "24.4"
  2980. :package-version '(Org . "8.0")
  2981. :type 'boolean)
  2982. (defcustom org-edit-timestamp-down-means-later nil
  2983. "Non-nil means S-down will increase the time in a time stamp.
  2984. When nil, S-up will increase."
  2985. :group 'org-time
  2986. :type 'boolean)
  2987. (defcustom org-calendar-follow-timestamp-change t
  2988. "Non-nil means make the calendar window follow timestamp changes.
  2989. When a timestamp is modified and the calendar window is visible, it will be
  2990. moved to the new date."
  2991. :group 'org-time
  2992. :type 'boolean)
  2993. (defgroup org-tags nil
  2994. "Options concerning tags in Org-mode."
  2995. :tag "Org Tags"
  2996. :group 'org)
  2997. (defcustom org-tag-alist nil
  2998. "List of tags allowed in Org-mode files.
  2999. When this list is nil, Org-mode will base TAG input on what is already in the
  3000. buffer.
  3001. The value of this variable is an alist, the car of each entry must be a
  3002. keyword as a string, the cdr may be a character that is used to select
  3003. that tag through the fast-tag-selection interface.
  3004. See the manual for details."
  3005. :group 'org-tags
  3006. :type '(repeat
  3007. (choice
  3008. (cons (string :tag "Tag name")
  3009. (character :tag "Access char"))
  3010. (list :tag "Start radio group"
  3011. (const :startgroup)
  3012. (option (string :tag "Group description")))
  3013. (list :tag "Group tags delimiter"
  3014. (const :grouptags))
  3015. (list :tag "End radio group"
  3016. (const :endgroup)
  3017. (option (string :tag "Group description")))
  3018. (const :tag "New line" (:newline)))))
  3019. (defcustom org-tag-persistent-alist nil
  3020. "List of tags that will always appear in all Org-mode files.
  3021. This is in addition to any in buffer settings or customizations
  3022. of `org-tag-alist'.
  3023. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  3024. The value of this variable is an alist, the car of each entry must be a
  3025. keyword as a string, the cdr may be a character that is used to select
  3026. that tag through the fast-tag-selection interface.
  3027. See the manual for details.
  3028. To disable these tags on a per-file basis, insert anywhere in the file:
  3029. #+STARTUP: noptag"
  3030. :group 'org-tags
  3031. :type '(repeat
  3032. (choice
  3033. (cons (string :tag "Tag name")
  3034. (character :tag "Access char"))
  3035. (const :tag "Start radio group" (:startgroup))
  3036. (const :tag "Group tags delimiter" (:grouptags))
  3037. (const :tag "End radio group" (:endgroup))
  3038. (const :tag "New line" (:newline)))))
  3039. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  3040. "If non-nil, always offer completion for all tags of all agenda files.
  3041. Instead of customizing this variable directly, you might want to
  3042. set it locally for capture buffers, because there no list of
  3043. tags in that file can be created dynamically (there are none).
  3044. (add-hook 'org-capture-mode-hook
  3045. (lambda ()
  3046. (set (make-local-variable
  3047. 'org-complete-tags-always-offer-all-agenda-tags)
  3048. t)))"
  3049. :group 'org-tags
  3050. :version "24.1"
  3051. :type 'boolean)
  3052. (defvar org-file-tags nil
  3053. "List of tags that can be inherited by all entries in the file.
  3054. The tags will be inherited if the variable `org-use-tag-inheritance'
  3055. says they should be.
  3056. This variable is populated from #+FILETAGS lines.")
  3057. (defcustom org-use-fast-tag-selection 'auto
  3058. "Non-nil means use fast tag selection scheme.
  3059. This is a special interface to select and deselect tags with single keys.
  3060. When nil, fast selection is never used.
  3061. When the symbol `auto', fast selection is used if and only if selection
  3062. characters for tags have been configured, either through the variable
  3063. `org-tag-alist' or through a #+TAGS line in the buffer.
  3064. When t, fast selection is always used and selection keys are assigned
  3065. automatically if necessary."
  3066. :group 'org-tags
  3067. :type '(choice
  3068. (const :tag "Always" t)
  3069. (const :tag "Never" nil)
  3070. (const :tag "When selection characters are configured" auto)))
  3071. (defcustom org-fast-tag-selection-single-key nil
  3072. "Non-nil means fast tag selection exits after first change.
  3073. When nil, you have to press RET to exit it.
  3074. During fast tag selection, you can toggle this flag with `C-c'.
  3075. This variable can also have the value `expert'. In this case, the window
  3076. displaying the tags menu is not even shown, until you press C-c again."
  3077. :group 'org-tags
  3078. :type '(choice
  3079. (const :tag "No" nil)
  3080. (const :tag "Yes" t)
  3081. (const :tag "Expert" expert)))
  3082. (defvar org-fast-tag-selection-include-todo nil
  3083. "Non-nil means fast tags selection interface will also offer TODO states.
  3084. This is an undocumented feature, you should not rely on it.")
  3085. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  3086. "The column to which tags should be indented in a headline.
  3087. If this number is positive, it specifies the column. If it is negative,
  3088. it means that the tags should be flushright to that column. For example,
  3089. -80 works well for a normal 80 character screen.
  3090. When 0, place tags directly after headline text, with only one space in
  3091. between."
  3092. :group 'org-tags
  3093. :type 'integer)
  3094. (defcustom org-auto-align-tags t
  3095. "Non-nil keeps tags aligned when modifying headlines.
  3096. Some operations (i.e. demoting) change the length of a headline and
  3097. therefore shift the tags around. With this option turned on, after
  3098. each such operation the tags are again aligned to `org-tags-column'."
  3099. :group 'org-tags
  3100. :type 'boolean)
  3101. (defcustom org-use-tag-inheritance t
  3102. "Non-nil means tags in levels apply also for sublevels.
  3103. When nil, only the tags directly given in a specific line apply there.
  3104. This may also be a list of tags that should be inherited, or a regexp that
  3105. matches tags that should be inherited. Additional control is possible
  3106. with the variable `org-tags-exclude-from-inheritance' which gives an
  3107. explicit list of tags to be excluded from inheritance, even if the value of
  3108. `org-use-tag-inheritance' would select it for inheritance.
  3109. If this option is t, a match early-on in a tree can lead to a large
  3110. number of matches in the subtree when constructing the agenda or creating
  3111. a sparse tree. If you only want to see the first match in a tree during
  3112. a search, check out the variable `org-tags-match-list-sublevels'."
  3113. :group 'org-tags
  3114. :type '(choice
  3115. (const :tag "Not" nil)
  3116. (const :tag "Always" t)
  3117. (repeat :tag "Specific tags" (string :tag "Tag"))
  3118. (regexp :tag "Tags matched by regexp")))
  3119. (defcustom org-tags-exclude-from-inheritance nil
  3120. "List of tags that should never be inherited.
  3121. This is a way to exclude a few tags from inheritance. For way to do
  3122. the opposite, to actively allow inheritance for selected tags,
  3123. see the variable `org-use-tag-inheritance'."
  3124. :group 'org-tags
  3125. :type '(repeat (string :tag "Tag")))
  3126. (defun org-tag-inherit-p (tag)
  3127. "Check if TAG is one that should be inherited."
  3128. (cond
  3129. ((member tag org-tags-exclude-from-inheritance) nil)
  3130. ((eq org-use-tag-inheritance t) t)
  3131. ((not org-use-tag-inheritance) nil)
  3132. ((stringp org-use-tag-inheritance)
  3133. (string-match org-use-tag-inheritance tag))
  3134. ((listp org-use-tag-inheritance)
  3135. (member tag org-use-tag-inheritance))
  3136. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  3137. (defcustom org-tags-match-list-sublevels t
  3138. "Non-nil means list also sublevels of headlines matching a search.
  3139. This variable applies to tags/property searches, and also to stuck
  3140. projects because this search is based on a tags match as well.
  3141. When set to the symbol `indented', sublevels are indented with
  3142. leading dots.
  3143. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  3144. the sublevels of a headline matching a tag search often also match
  3145. the same search. Listing all of them can create very long lists.
  3146. Setting this variable to nil causes subtrees of a match to be skipped.
  3147. This variable is semi-obsolete and probably should always be true. It
  3148. is better to limit inheritance to certain tags using the variables
  3149. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  3150. :group 'org-tags
  3151. :type '(choice
  3152. (const :tag "No, don't list them" nil)
  3153. (const :tag "Yes, do list them" t)
  3154. (const :tag "List them, indented with leading dots" indented)))
  3155. (defcustom org-tags-sort-function nil
  3156. "When set, tags are sorted using this function as a comparator."
  3157. :group 'org-tags
  3158. :type '(choice
  3159. (const :tag "No sorting" nil)
  3160. (const :tag "Alphabetical" string<)
  3161. (const :tag "Reverse alphabetical" string>)
  3162. (function :tag "Custom function" nil)))
  3163. (defvar org-tags-history nil
  3164. "History of minibuffer reads for tags.")
  3165. (defvar org-last-tags-completion-table nil
  3166. "The last used completion table for tags.")
  3167. (defvar org-after-tags-change-hook nil
  3168. "Hook that is run after the tags in a line have changed.")
  3169. (defgroup org-properties nil
  3170. "Options concerning properties in Org-mode."
  3171. :tag "Org Properties"
  3172. :group 'org)
  3173. (defcustom org-property-format "%-10s %s"
  3174. "How property key/value pairs should be formatted by `indent-line'.
  3175. When `indent-line' hits a property definition, it will format the line
  3176. according to this format, mainly to make sure that the values are
  3177. lined-up with respect to each other."
  3178. :group 'org-properties
  3179. :type 'string)
  3180. (defcustom org-properties-postprocess-alist nil
  3181. "Alist of properties and functions to adjust inserted values.
  3182. Elements of this alist must be of the form
  3183. ([string] [function])
  3184. where [string] must be a property name and [function] must be a
  3185. lambda expression: this lambda expression must take one argument,
  3186. the value to adjust, and return the new value as a string.
  3187. For example, this element will allow the property \"Remaining\"
  3188. to be updated wrt the relation between the \"Effort\" property
  3189. and the clock summary:
  3190. ((\"Remaining\" (lambda(value)
  3191. (let ((clocksum (org-clock-sum-current-item))
  3192. (effort (org-duration-string-to-minutes
  3193. (org-entry-get (point) \"Effort\"))))
  3194. (org-minutes-to-clocksum-string (- effort clocksum))))))"
  3195. :group 'org-properties
  3196. :version "24.1"
  3197. :type '(alist :key-type (string :tag "Property")
  3198. :value-type (function :tag "Function")))
  3199. (defcustom org-use-property-inheritance nil
  3200. "Non-nil means properties apply also for sublevels.
  3201. This setting is chiefly used during property searches. Turning it on can
  3202. cause significant overhead when doing a search, which is why it is not
  3203. on by default.
  3204. When nil, only the properties directly given in the current entry count.
  3205. When t, every property is inherited. The value may also be a list of
  3206. properties that should have inheritance, or a regular expression matching
  3207. properties that should be inherited.
  3208. However, note that some special properties use inheritance under special
  3209. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  3210. and the properties ending in \"_ALL\" when they are used as descriptor
  3211. for valid values of a property.
  3212. Note for programmers:
  3213. When querying an entry with `org-entry-get', you can control if inheritance
  3214. should be used. By default, `org-entry-get' looks only at the local
  3215. properties. You can request inheritance by setting the inherit argument
  3216. to t (to force inheritance) or to `selective' (to respect the setting
  3217. in this variable)."
  3218. :group 'org-properties
  3219. :type '(choice
  3220. (const :tag "Not" nil)
  3221. (const :tag "Always" t)
  3222. (repeat :tag "Specific properties" (string :tag "Property"))
  3223. (regexp :tag "Properties matched by regexp")))
  3224. (defun org-property-inherit-p (property)
  3225. "Check if PROPERTY is one that should be inherited."
  3226. (cond
  3227. ((eq org-use-property-inheritance t) t)
  3228. ((not org-use-property-inheritance) nil)
  3229. ((stringp org-use-property-inheritance)
  3230. (string-match org-use-property-inheritance property))
  3231. ((listp org-use-property-inheritance)
  3232. (member property org-use-property-inheritance))
  3233. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  3234. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  3235. "The default column format, if no other format has been defined.
  3236. This variable can be set on the per-file basis by inserting a line
  3237. #+COLUMNS: %25ITEM ....."
  3238. :group 'org-properties
  3239. :type 'string)
  3240. (defcustom org-columns-ellipses ".."
  3241. "The ellipses to be used when a field in column view is truncated.
  3242. When this is the empty string, as many characters as possible are shown,
  3243. but then there will be no visual indication that the field has been truncated.
  3244. When this is a string of length N, the last N characters of a truncated
  3245. field are replaced by this string. If the column is narrower than the
  3246. ellipses string, only part of the ellipses string will be shown."
  3247. :group 'org-properties
  3248. :type 'string)
  3249. (defcustom org-columns-modify-value-for-display-function nil
  3250. "Function that modifies values for display in column view.
  3251. For example, it can be used to cut out a certain part from a time stamp.
  3252. The function must take 2 arguments:
  3253. column-title The title of the column (*not* the property name)
  3254. value The value that should be modified.
  3255. The function should return the value that should be displayed,
  3256. or nil if the normal value should be used."
  3257. :group 'org-properties
  3258. :type '(choice (const nil) (function)))
  3259. (defconst org-global-properties-fixed
  3260. '(("VISIBILITY_ALL" . "folded children content all")
  3261. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  3262. "List of property/value pairs that can be inherited by any entry.
  3263. These are fixed values, for the preset properties. The user variable
  3264. that can be used to add to this list is `org-global-properties'.
  3265. The entries in this list are cons cells where the car is a property
  3266. name and cdr is a string with the value. If the value represents
  3267. multiple items like an \"_ALL\" property, separate the items by
  3268. spaces.")
  3269. (defcustom org-global-properties nil
  3270. "List of property/value pairs that can be inherited by any entry.
  3271. This list will be combined with the constant `org-global-properties-fixed'.
  3272. The entries in this list are cons cells where the car is a property
  3273. name and cdr is a string with the value.
  3274. You can set buffer-local values for the same purpose in the variable
  3275. `org-file-properties' this by adding lines like
  3276. #+PROPERTY: NAME VALUE"
  3277. :group 'org-properties
  3278. :type '(repeat
  3279. (cons (string :tag "Property")
  3280. (string :tag "Value"))))
  3281. (defvar org-file-properties nil
  3282. "List of property/value pairs that can be inherited by any entry.
  3283. Valid for the current buffer.
  3284. This variable is populated from #+PROPERTY lines.")
  3285. (make-variable-buffer-local 'org-file-properties)
  3286. (defgroup org-agenda nil
  3287. "Options concerning agenda views in Org-mode."
  3288. :tag "Org Agenda"
  3289. :group 'org)
  3290. (defvar org-category nil
  3291. "Variable used by org files to set a category for agenda display.
  3292. Such files should use a file variable to set it, for example
  3293. # -*- mode: org; org-category: \"ELisp\"
  3294. or contain a special line
  3295. #+CATEGORY: ELisp
  3296. If the file does not specify a category, then file's base name
  3297. is used instead.")
  3298. (make-variable-buffer-local 'org-category)
  3299. (put 'org-category 'safe-local-variable (lambda (x) (or (symbolp x) (stringp x))))
  3300. (defcustom org-agenda-files nil
  3301. "The files to be used for agenda display.
  3302. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  3303. \\[org-remove-file]. You can also use customize to edit the list.
  3304. If an entry is a directory, all files in that directory that are matched by
  3305. `org-agenda-file-regexp' will be part of the file list.
  3306. If the value of the variable is not a list but a single file name, then
  3307. the list of agenda files is actually stored and maintained in that file, one
  3308. agenda file per line. In this file paths can be given relative to
  3309. `org-directory'. Tilde expansion and environment variable substitution
  3310. are also made."
  3311. :group 'org-agenda
  3312. :type '(choice
  3313. (repeat :tag "List of files and directories" file)
  3314. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  3315. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  3316. "Regular expression to match files for `org-agenda-files'.
  3317. If any element in the list in that variable contains a directory instead
  3318. of a normal file, all files in that directory that are matched by this
  3319. regular expression will be included."
  3320. :group 'org-agenda
  3321. :type 'regexp)
  3322. (defcustom org-agenda-text-search-extra-files nil
  3323. "List of extra files to be searched by text search commands.
  3324. These files will be searched in addition to the agenda files by the
  3325. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  3326. Note that these files will only be searched for text search commands,
  3327. not for the other agenda views like todo lists, tag searches or the weekly
  3328. agenda. This variable is intended to list notes and possibly archive files
  3329. that should also be searched by these two commands.
  3330. In fact, if the first element in the list is the symbol `agenda-archives',
  3331. then all archive files of all agenda files will be added to the search
  3332. scope."
  3333. :group 'org-agenda
  3334. :type '(set :greedy t
  3335. (const :tag "Agenda Archives" agenda-archives)
  3336. (repeat :inline t (file))))
  3337. (org-defvaralias 'org-agenda-multi-occur-extra-files
  3338. 'org-agenda-text-search-extra-files)
  3339. (defcustom org-agenda-skip-unavailable-files nil
  3340. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  3341. A nil value means to remove them, after a query, from the list."
  3342. :group 'org-agenda
  3343. :type 'boolean)
  3344. (defcustom org-calendar-to-agenda-key [?c]
  3345. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  3346. The command `org-calendar-goto-agenda' will be bound to this key. The
  3347. default is the character `c' because then `c' can be used to switch back and
  3348. forth between agenda and calendar."
  3349. :group 'org-agenda
  3350. :type 'sexp)
  3351. (defcustom org-calendar-insert-diary-entry-key [?i]
  3352. "The key to be installed in `calendar-mode-map' for adding diary entries.
  3353. This option is irrelevant until `org-agenda-diary-file' has been configured
  3354. to point to an Org-mode file. When that is the case, the command
  3355. `org-agenda-diary-entry' will be bound to the key given here, by default
  3356. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  3357. if you want to continue doing this, you need to change this to a different
  3358. key."
  3359. :group 'org-agenda
  3360. :type 'sexp)
  3361. (defcustom org-agenda-diary-file 'diary-file
  3362. "File to which to add new entries with the `i' key in agenda and calendar.
  3363. When this is the symbol `diary-file', the functionality in the Emacs
  3364. calendar will be used to add entries to the `diary-file'. But when this
  3365. points to a file, `org-agenda-diary-entry' will be used instead."
  3366. :group 'org-agenda
  3367. :type '(choice
  3368. (const :tag "The standard Emacs diary file" diary-file)
  3369. (file :tag "Special Org file diary entries")))
  3370. (eval-after-load "calendar"
  3371. '(progn
  3372. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  3373. 'org-calendar-goto-agenda)
  3374. (add-hook 'calendar-mode-hook
  3375. (lambda ()
  3376. (unless (eq org-agenda-diary-file 'diary-file)
  3377. (define-key calendar-mode-map
  3378. org-calendar-insert-diary-entry-key
  3379. 'org-agenda-diary-entry))))))
  3380. (defgroup org-latex nil
  3381. "Options for embedding LaTeX code into Org-mode."
  3382. :tag "Org LaTeX"
  3383. :group 'org)
  3384. (defcustom org-format-latex-options
  3385. '(:foreground default :background default :scale 1.0
  3386. :html-foreground "Black" :html-background "Transparent"
  3387. :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  3388. "Options for creating images from LaTeX fragments.
  3389. This is a property list with the following properties:
  3390. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  3391. `default' means use the foreground of the default face.
  3392. `auto' means use the foreground from the text face.
  3393. :background the background color, or \"Transparent\".
  3394. `default' means use the background of the default face.
  3395. `auto' means use the background from the text face.
  3396. :scale a scaling factor for the size of the images, to get more pixels
  3397. :html-foreground, :html-background, :html-scale
  3398. the same numbers for HTML export.
  3399. :matchers a list indicating which matchers should be used to
  3400. find LaTeX fragments. Valid members of this list are:
  3401. \"begin\" find environments
  3402. \"$1\" find single characters surrounded by $.$
  3403. \"$\" find math expressions surrounded by $...$
  3404. \"$$\" find math expressions surrounded by $$....$$
  3405. \"\\(\" find math expressions surrounded by \\(...\\)
  3406. \"\\=\\[\" find math expressions surrounded by \\=\\[...\\]"
  3407. :group 'org-latex
  3408. :type 'plist)
  3409. (defcustom org-format-latex-signal-error t
  3410. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  3411. When nil, just push out a message."
  3412. :group 'org-latex
  3413. :version "24.1"
  3414. :type 'boolean)
  3415. (defcustom org-latex-to-mathml-jar-file nil
  3416. "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
  3417. Use this to specify additional executable file say a jar file.
  3418. When using MathToWeb as the converter, specify the full-path to
  3419. your mathtoweb.jar file."
  3420. :group 'org-latex
  3421. :version "24.1"
  3422. :type '(choice
  3423. (const :tag "None" nil)
  3424. (file :tag "JAR file" :must-match t)))
  3425. (defcustom org-latex-to-mathml-convert-command nil
  3426. "Command to convert LaTeX fragments to MathML.
  3427. Replace format-specifiers in the command as noted below and use
  3428. `shell-command' to convert LaTeX to MathML.
  3429. %j: Executable file in fully expanded form as specified by
  3430. `org-latex-to-mathml-jar-file'.
  3431. %I: Input LaTeX file in fully expanded form
  3432. %o: Output MathML file
  3433. This command is used by `org-create-math-formula'.
  3434. When using MathToWeb as the converter, set this to
  3435. \"java -jar %j -unicode -force -df %o %I\"."
  3436. :group 'org-latex
  3437. :version "24.1"
  3438. :type '(choice
  3439. (const :tag "None" nil)
  3440. (string :tag "\nShell command")))
  3441. (defcustom org-latex-create-formula-image-program 'dvipng
  3442. "Program to convert LaTeX fragments with.
  3443. dvipng Process the LaTeX fragments to dvi file, then convert
  3444. dvi files to png files using dvipng.
  3445. This will also include processing of non-math environments.
  3446. imagemagick Convert the LaTeX fragments to pdf files and use imagemagick
  3447. to convert pdf files to png files"
  3448. :group 'org-latex
  3449. :version "24.1"
  3450. :type '(choice
  3451. (const :tag "dvipng" dvipng)
  3452. (const :tag "imagemagick" imagemagick)))
  3453. (defcustom org-latex-preview-ltxpng-directory "ltxpng/"
  3454. "Path to store latex preview images.
  3455. A relative path here creates many directories relative to the
  3456. processed org files paths. An absolute path puts all preview
  3457. images at the same place."
  3458. :group 'org-latex
  3459. :version "24.3"
  3460. :type 'string)
  3461. (defun org-format-latex-mathml-available-p ()
  3462. "Return t if `org-latex-to-mathml-convert-command' is usable."
  3463. (save-match-data
  3464. (when (and (boundp 'org-latex-to-mathml-convert-command)
  3465. org-latex-to-mathml-convert-command)
  3466. (let ((executable (car (split-string
  3467. org-latex-to-mathml-convert-command))))
  3468. (when (executable-find executable)
  3469. (if (string-match
  3470. "%j" org-latex-to-mathml-convert-command)
  3471. (file-readable-p org-latex-to-mathml-jar-file)
  3472. t))))))
  3473. (defcustom org-format-latex-header "\\documentclass{article}
  3474. \\usepackage[usenames]{color}
  3475. \[PACKAGES]
  3476. \[DEFAULT-PACKAGES]
  3477. \\pagestyle{empty} % do not remove
  3478. % The settings below are copied from fullpage.sty
  3479. \\setlength{\\textwidth}{\\paperwidth}
  3480. \\addtolength{\\textwidth}{-3cm}
  3481. \\setlength{\\oddsidemargin}{1.5cm}
  3482. \\addtolength{\\oddsidemargin}{-2.54cm}
  3483. \\setlength{\\evensidemargin}{\\oddsidemargin}
  3484. \\setlength{\\textheight}{\\paperheight}
  3485. \\addtolength{\\textheight}{-\\headheight}
  3486. \\addtolength{\\textheight}{-\\headsep}
  3487. \\addtolength{\\textheight}{-\\footskip}
  3488. \\addtolength{\\textheight}{-3cm}
  3489. \\setlength{\\topmargin}{1.5cm}
  3490. \\addtolength{\\topmargin}{-2.54cm}"
  3491. "The document header used for processing LaTeX fragments.
  3492. It is imperative that this header make sure that no page number
  3493. appears on the page. The package defined in the variables
  3494. `org-latex-default-packages-alist' and `org-latex-packages-alist'
  3495. will either replace the placeholder \"[PACKAGES]\" in this
  3496. header, or they will be appended."
  3497. :group 'org-latex
  3498. :type 'string)
  3499. (defun org-set-packages-alist (var val)
  3500. "Set the packages alist and make sure it has 3 elements per entry."
  3501. (set var (mapcar (lambda (x)
  3502. (if (and (consp x) (= (length x) 2))
  3503. (list (car x) (nth 1 x) t)
  3504. x))
  3505. val)))
  3506. (defun org-get-packages-alist (var)
  3507. "Get the packages alist and make sure it has 3 elements per entry."
  3508. (mapcar (lambda (x)
  3509. (if (and (consp x) (= (length x) 2))
  3510. (list (car x) (nth 1 x) t)
  3511. x))
  3512. (default-value var)))
  3513. (defcustom org-latex-default-packages-alist
  3514. '(("AUTO" "inputenc" t)
  3515. ("T1" "fontenc" t)
  3516. ("" "fixltx2e" nil)
  3517. ("" "graphicx" t)
  3518. ("" "longtable" nil)
  3519. ("" "float" nil)
  3520. ("" "wrapfig" nil)
  3521. ("" "rotating" nil)
  3522. ("normalem" "ulem" t)
  3523. ("" "amsmath" t)
  3524. ("" "textcomp" t)
  3525. ("" "marvosym" t)
  3526. ("" "wasysym" t)
  3527. ("" "amssymb" t)
  3528. ("" "capt-of" nil)
  3529. ("" "hyperref" nil)
  3530. "\\tolerance=1000")
  3531. "Alist of default packages to be inserted in the header.
  3532. Change this only if one of the packages here causes an
  3533. incompatibility with another package you are using.
  3534. The packages in this list are needed by one part or another of
  3535. Org mode to function properly:
  3536. - inputenc, fontenc: for basic font and character selection
  3537. - fixltx2e: Important patches of LaTeX itself
  3538. - graphicx: for including images
  3539. - longtable: For multipage tables
  3540. - float, wrapfig: for figure placement
  3541. - rotating: for sideways figures and tables
  3542. - ulem: for underline and strike-through
  3543. - amsmath: for subscript and superscript and math environments
  3544. - textcomp, marvosymb, wasysym, amssymb: for various symbols used
  3545. for interpreting the entities in `org-entities'. You can skip
  3546. some of these packages if you don't use any of their symbols.
  3547. - capt-of: for captions outside of floats
  3548. - hyperref: for cross references
  3549. Therefore you should not modify this variable unless you know
  3550. what you are doing. The one reason to change it anyway is that
  3551. you might be loading some other package that conflicts with one
  3552. of the default packages. Each element is either a cell or
  3553. a string.
  3554. A cell is of the format:
  3555. \( \"options\" \"package\" SNIPPET-FLAG).
  3556. If SNIPPET-FLAG is non-nil, the package also needs to be included
  3557. when compiling LaTeX snippets into images for inclusion into
  3558. non-LaTeX output.
  3559. A string will be inserted as-is in the header of the document."
  3560. :group 'org-latex
  3561. :group 'org-export-latex
  3562. :set 'org-set-packages-alist
  3563. :get 'org-get-packages-alist
  3564. :version "24.1"
  3565. :type '(repeat
  3566. (choice
  3567. (list :tag "options/package pair"
  3568. (string :tag "options")
  3569. (string :tag "package")
  3570. (boolean :tag "Snippet"))
  3571. (string :tag "A line of LaTeX"))))
  3572. (defcustom org-latex-packages-alist nil
  3573. "Alist of packages to be inserted in every LaTeX header.
  3574. These will be inserted after `org-latex-default-packages-alist'.
  3575. Each element is either a cell or a string.
  3576. A cell is of the format:
  3577. \(\"options\" \"package\" SNIPPET-FLAG)
  3578. SNIPPET-FLAG, when non-nil, indicates that this package is also
  3579. needed when turning LaTeX snippets into images for inclusion into
  3580. non-LaTeX output.
  3581. A string will be inserted as-is in the header of the document.
  3582. Make sure that you only list packages here which:
  3583. - you want in every file;
  3584. - do not conflict with the setup in `org-format-latex-header';
  3585. - do not conflict with the default packages in
  3586. `org-latex-default-packages-alist'."
  3587. :group 'org-latex
  3588. :group 'org-export-latex
  3589. :set 'org-set-packages-alist
  3590. :get 'org-get-packages-alist
  3591. :type '(repeat
  3592. (choice
  3593. (list :tag "options/package pair"
  3594. (string :tag "options")
  3595. (string :tag "package")
  3596. (boolean :tag "Snippet"))
  3597. (string :tag "A line of LaTeX"))))
  3598. (defgroup org-appearance nil
  3599. "Settings for Org-mode appearance."
  3600. :tag "Org Appearance"
  3601. :group 'org)
  3602. (defcustom org-level-color-stars-only nil
  3603. "Non-nil means fontify only the stars in each headline.
  3604. When nil, the entire headline is fontified.
  3605. Changing it requires restart of `font-lock-mode' to become effective
  3606. also in regions already fontified."
  3607. :group 'org-appearance
  3608. :type 'boolean)
  3609. (defcustom org-hide-leading-stars nil
  3610. "Non-nil means hide the first N-1 stars in a headline.
  3611. This works by using the face `org-hide' for these stars. This
  3612. face is white for a light background, and black for a dark
  3613. background. You may have to customize the face `org-hide' to
  3614. make this work.
  3615. Changing it requires restart of `font-lock-mode' to become effective
  3616. also in regions already fontified.
  3617. You may also set this on a per-file basis by adding one of the following
  3618. lines to the buffer:
  3619. #+STARTUP: hidestars
  3620. #+STARTUP: showstars"
  3621. :group 'org-appearance
  3622. :type 'boolean)
  3623. (defcustom org-hidden-keywords nil
  3624. "List of symbols corresponding to keywords to be hidden the org buffer.
  3625. For example, a value '(title) for this list will make the document's title
  3626. appear in the buffer without the initial #+TITLE: keyword."
  3627. :group 'org-appearance
  3628. :version "24.1"
  3629. :type '(set (const :tag "#+AUTHOR" author)
  3630. (const :tag "#+DATE" date)
  3631. (const :tag "#+EMAIL" email)
  3632. (const :tag "#+TITLE" title)))
  3633. (defcustom org-custom-properties nil
  3634. "List of properties (as strings) with a special meaning.
  3635. The default use of these custom properties is to let the user
  3636. hide them with `org-toggle-custom-properties-visibility'."
  3637. :group 'org-properties
  3638. :group 'org-appearance
  3639. :version "24.3"
  3640. :type '(repeat (string :tag "Property Name")))
  3641. (defcustom org-fontify-done-headline nil
  3642. "Non-nil means change the face of a headline if it is marked DONE.
  3643. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3644. When this is non-nil, the headline after the keyword is set to the
  3645. `org-headline-done' as an additional indication."
  3646. :group 'org-appearance
  3647. :type 'boolean)
  3648. (defcustom org-fontify-emphasized-text t
  3649. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3650. Changing this variable requires a restart of Emacs to take effect."
  3651. :group 'org-appearance
  3652. :type 'boolean)
  3653. (defcustom org-fontify-whole-heading-line nil
  3654. "Non-nil means fontify the whole line for headings.
  3655. This is useful when setting a background color for the
  3656. org-level-* faces."
  3657. :group 'org-appearance
  3658. :type 'boolean)
  3659. (defcustom org-highlight-latex-and-related nil
  3660. "Non-nil means highlight LaTeX related syntax in the buffer.
  3661. When non nil, the value should be a list containing any of the
  3662. following symbols:
  3663. `latex' Highlight LaTeX snippets and environments.
  3664. `script' Highlight subscript and superscript.
  3665. `entities' Highlight entities."
  3666. :group 'org-appearance
  3667. :version "24.4"
  3668. :package-version '(Org . "8.0")
  3669. :type '(choice
  3670. (const :tag "No highlighting" nil)
  3671. (set :greedy t :tag "Highlight"
  3672. (const :tag "LaTeX snippets and environments" latex)
  3673. (const :tag "Subscript and superscript" script)
  3674. (const :tag "Entities" entities))))
  3675. (defcustom org-hide-emphasis-markers nil
  3676. "Non-nil mean font-lock should hide the emphasis marker characters."
  3677. :group 'org-appearance
  3678. :type 'boolean)
  3679. (defcustom org-hide-macro-markers nil
  3680. "Non-nil mean font-lock should hide the brackets marking macro calls."
  3681. :group 'org-appearance
  3682. :type 'boolean)
  3683. (defcustom org-pretty-entities nil
  3684. "Non-nil means show entities as UTF8 characters.
  3685. When nil, the \\name form remains in the buffer."
  3686. :group 'org-appearance
  3687. :version "24.1"
  3688. :type 'boolean)
  3689. (defcustom org-pretty-entities-include-sub-superscripts t
  3690. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  3691. :group 'org-appearance
  3692. :version "24.1"
  3693. :type 'boolean)
  3694. (defvar org-emph-re nil
  3695. "Regular expression for matching emphasis.
  3696. After a match, the match groups contain these elements:
  3697. 0 The match of the full regular expression, including the characters
  3698. before and after the proper match
  3699. 1 The character before the proper match, or empty at beginning of line
  3700. 2 The proper match, including the leading and trailing markers
  3701. 3 The leading marker like * or /, indicating the type of highlighting
  3702. 4 The text between the emphasis markers, not including the markers
  3703. 5 The character after the match, empty at the end of a line")
  3704. (defvar org-verbatim-re nil
  3705. "Regular expression for matching verbatim text.")
  3706. (defvar org-emphasis-regexp-components) ; defined just below
  3707. (defvar org-emphasis-alist) ; defined just below
  3708. (defun org-set-emph-re (var val)
  3709. "Set variable and compute the emphasis regular expression."
  3710. (set var val)
  3711. (when (and (boundp 'org-emphasis-alist)
  3712. (boundp 'org-emphasis-regexp-components)
  3713. org-emphasis-alist org-emphasis-regexp-components)
  3714. (let* ((e org-emphasis-regexp-components)
  3715. (pre (car e))
  3716. (post (nth 1 e))
  3717. (border (nth 2 e))
  3718. (body (nth 3 e))
  3719. (nl (nth 4 e))
  3720. (body1 (concat body "*?"))
  3721. (markers (mapconcat 'car org-emphasis-alist ""))
  3722. (vmarkers (mapconcat
  3723. (lambda (x) (if (eq (nth 2 x) 'verbatim) (car x) ""))
  3724. org-emphasis-alist "")))
  3725. ;; make sure special characters appear at the right position in the class
  3726. (if (string-match "\\^" markers)
  3727. (setq markers (concat (replace-match "" t t markers) "^")))
  3728. (if (string-match "-" markers)
  3729. (setq markers (concat (replace-match "" t t markers) "-")))
  3730. (if (string-match "\\^" vmarkers)
  3731. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3732. (if (string-match "-" vmarkers)
  3733. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3734. (if (> nl 0)
  3735. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3736. (int-to-string nl) "\\}")))
  3737. ;; Make the regexp
  3738. (setq org-emph-re
  3739. (concat "\\([" pre "]\\|^\\)"
  3740. "\\("
  3741. "\\([" markers "]\\)"
  3742. "\\("
  3743. "[^" border "]\\|"
  3744. "[^" border "]"
  3745. body1
  3746. "[^" border "]"
  3747. "\\)"
  3748. "\\3\\)"
  3749. "\\([" post "]\\|$\\)"))
  3750. (setq org-verbatim-re
  3751. (concat "\\([" pre "]\\|^\\)"
  3752. "\\("
  3753. "\\([" vmarkers "]\\)"
  3754. "\\("
  3755. "[^" border "]\\|"
  3756. "[^" border "]"
  3757. body1
  3758. "[^" border "]"
  3759. "\\)"
  3760. "\\3\\)"
  3761. "\\([" post "]\\|$\\)")))))
  3762. ;; This used to be a defcustom (Org <8.0) but allowing the users to
  3763. ;; set this option proved cumbersome. See this message/thread:
  3764. ;; http://article.gmane.org/gmane.emacs.orgmode/68681
  3765. (defvar org-emphasis-regexp-components
  3766. '(" \t('\"{" "- \t.,:!?;'\")}\\[" " \t\r\n,\"'" "." 1)
  3767. "Components used to build the regular expression for emphasis.
  3768. This is a list with five entries. Terminology: In an emphasis string
  3769. like \" *strong word* \", we call the initial space PREMATCH, the final
  3770. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3771. and \"trong wor\" is the body. The different components in this variable
  3772. specify what is allowed/forbidden in each part:
  3773. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3774. post Chars allowed as postmatch. End of line will be allowed too.
  3775. border The chars *forbidden* as border characters.
  3776. body-regexp A regexp like \".\" to match a body character. Don't use
  3777. non-shy groups here, and don't allow newline here.
  3778. newline The maximum number of newlines allowed in an emphasis exp.
  3779. You need to reload Org or to restart Emacs after customizing this.")
  3780. (defcustom org-emphasis-alist
  3781. `(("*" bold)
  3782. ("/" italic)
  3783. ("_" underline)
  3784. ("=" org-verbatim verbatim)
  3785. ("~" org-code verbatim)
  3786. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))))
  3787. "Alist of characters and faces to emphasize text.
  3788. Text starting and ending with a special character will be emphasized,
  3789. for example *bold*, _underlined_ and /italic/. This variable sets the
  3790. marker characters and the face to be used by font-lock for highlighting
  3791. in Org-mode Emacs buffers.
  3792. You need to reload Org or to restart Emacs after customizing this."
  3793. :group 'org-appearance
  3794. :set 'org-set-emph-re
  3795. :version "24.4"
  3796. :package-version '(Org . "8.0")
  3797. :type '(repeat
  3798. (list
  3799. (string :tag "Marker character")
  3800. (choice
  3801. (face :tag "Font-lock-face")
  3802. (plist :tag "Face property list"))
  3803. (option (const verbatim)))))
  3804. (defvar org-protecting-blocks
  3805. '("src" "example" "latex" "ascii" "html" "ditaa" "dot" "r" "R")
  3806. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3807. This is needed for font-lock setup.")
  3808. ;;; Miscellaneous options
  3809. (defgroup org-completion nil
  3810. "Completion in Org-mode."
  3811. :tag "Org Completion"
  3812. :group 'org)
  3813. (defcustom org-completion-use-ido nil
  3814. "Non-nil means use ido completion wherever possible.
  3815. Note that `ido-mode' must be active for this variable to be relevant.
  3816. If you decide to turn this variable on, you might well want to turn off
  3817. `org-outline-path-complete-in-steps'.
  3818. See also `org-completion-use-iswitchb'."
  3819. :group 'org-completion
  3820. :type 'boolean)
  3821. (defcustom org-completion-use-iswitchb nil
  3822. "Non-nil means use iswitchb completion wherever possible.
  3823. Note that `iswitchb-mode' must be active for this variable to be relevant.
  3824. If you decide to turn this variable on, you might well want to turn off
  3825. `org-outline-path-complete-in-steps'.
  3826. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  3827. :group 'org-completion
  3828. :type 'boolean)
  3829. (defcustom org-completion-fallback-command 'hippie-expand
  3830. "The expansion command called by \\[pcomplete] in normal context.
  3831. Normal means, no org-mode-specific context."
  3832. :group 'org-completion
  3833. :type 'function)
  3834. ;;; Functions and variables from their packages
  3835. ;; Declared here to avoid compiler warnings
  3836. ;; XEmacs only
  3837. (defvar outline-mode-menu-heading)
  3838. (defvar outline-mode-menu-show)
  3839. (defvar outline-mode-menu-hide)
  3840. (defvar zmacs-regions) ; XEmacs regions
  3841. ;; Emacs only
  3842. (defvar mark-active)
  3843. ;; Various packages
  3844. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  3845. (declare-function calendar-forward-day "cal-move" (arg))
  3846. (declare-function calendar-goto-date "cal-move" (date))
  3847. (declare-function calendar-goto-today "cal-move" ())
  3848. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3849. (defvar calc-embedded-close-formula)
  3850. (defvar calc-embedded-open-formula)
  3851. (declare-function cdlatex-tab "ext:cdlatex" ())
  3852. (declare-function cdlatex-compute-tables "ext:cdlatex" ())
  3853. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3854. (defvar font-lock-unfontify-region-function)
  3855. (declare-function iswitchb-read-buffer "iswitchb"
  3856. (prompt &optional default require-match start matches-set))
  3857. (defvar iswitchb-temp-buflist)
  3858. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3859. (defvar org-agenda-tags-todo-honor-ignore-options)
  3860. (declare-function org-agenda-skip "org-agenda" ())
  3861. (declare-function
  3862. org-agenda-format-item "org-agenda"
  3863. (extra txt &optional level category tags dotime noprefix remove-re habitp))
  3864. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3865. (declare-function org-agenda-change-all-lines "org-agenda"
  3866. (newhead hdmarker &optional fixface just-this))
  3867. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3868. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3869. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  3870. (beg end))
  3871. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3872. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3873. "org-agenda" (&optional end))
  3874. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3875. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  3876. (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
  3877. (declare-function org-inlinetask-goto-end "org-inlinetask" ())
  3878. (declare-function org-indent-mode "org-indent" (&optional arg))
  3879. (declare-function parse-time-string "parse-time" (string))
  3880. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3881. (declare-function orgtbl-send-table "org-table" (&optional maybe))
  3882. (defvar remember-data-file)
  3883. (defvar texmathp-why)
  3884. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3885. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3886. (declare-function calc-eval "calc" (str &optional separator &rest args))
  3887. ;;;###autoload
  3888. (defun turn-on-orgtbl ()
  3889. "Unconditionally turn on `orgtbl-mode'."
  3890. (require 'org-table)
  3891. (orgtbl-mode 1))
  3892. (defun org-at-table-p (&optional table-type)
  3893. "Return t if the cursor is inside an org-type table.
  3894. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  3895. (if org-enable-table-editor
  3896. (save-excursion
  3897. (beginning-of-line 1)
  3898. (looking-at (if table-type org-table-any-line-regexp
  3899. org-table-line-regexp)))
  3900. nil))
  3901. (defsubst org-table-p () (org-at-table-p))
  3902. (defun org-at-table.el-p ()
  3903. "Return t if and only if we are at a table.el table."
  3904. (and (org-at-table-p 'any)
  3905. (save-excursion
  3906. (goto-char (org-table-begin 'any))
  3907. (looking-at org-table1-hline-regexp))))
  3908. (defun org-table-recognize-table.el ()
  3909. "If there is a table.el table nearby, recognize it and move into it."
  3910. (if org-table-tab-recognizes-table.el
  3911. (if (org-at-table.el-p)
  3912. (progn
  3913. (beginning-of-line 1)
  3914. (if (looking-at org-table-dataline-regexp)
  3915. nil
  3916. (if (looking-at org-table1-hline-regexp)
  3917. (progn
  3918. (beginning-of-line 2)
  3919. (if (looking-at org-table-any-border-regexp)
  3920. (beginning-of-line -1)))))
  3921. (if (re-search-forward "|" (org-table-end t) t)
  3922. (progn
  3923. (require 'table)
  3924. (if (table--at-cell-p (point))
  3925. t
  3926. (message "recognizing table.el table...")
  3927. (table-recognize-table)
  3928. (message "recognizing table.el table...done")))
  3929. (error "This should not happen"))
  3930. t)
  3931. nil)
  3932. nil))
  3933. (defun org-at-table-hline-p ()
  3934. "Return t if the cursor is inside a hline in a table."
  3935. (if org-enable-table-editor
  3936. (save-excursion
  3937. (beginning-of-line 1)
  3938. (looking-at org-table-hline-regexp))
  3939. nil))
  3940. (defun org-table-map-tables (function &optional quietly)
  3941. "Apply FUNCTION to the start of all tables in the buffer."
  3942. (save-excursion
  3943. (save-restriction
  3944. (widen)
  3945. (goto-char (point-min))
  3946. (while (re-search-forward org-table-any-line-regexp nil t)
  3947. (unless quietly
  3948. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
  3949. (beginning-of-line 1)
  3950. (when (and (looking-at org-table-line-regexp)
  3951. ;; Exclude tables in src/example/verbatim/clocktable blocks
  3952. (not (org-in-block-p '("src" "example" "verbatim" "clocktable"))))
  3953. (save-excursion (funcall function))
  3954. (or (looking-at org-table-line-regexp)
  3955. (forward-char 1)))
  3956. (re-search-forward org-table-any-border-regexp nil 1))))
  3957. (unless quietly (message "Mapping tables: done")))
  3958. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock" (beg end))
  3959. (declare-function org-clock-update-mode-line "org-clock" ())
  3960. (declare-function org-resolve-clocks "org-clock"
  3961. (&optional also-non-dangling-p prompt last-valid))
  3962. (defun org-at-TBLFM-p (&optional pos)
  3963. "Return t when point (or POS) is in #+TBLFM line."
  3964. (save-excursion
  3965. (let ((pos pos)))
  3966. (goto-char (or pos (point)))
  3967. (beginning-of-line 1)
  3968. (looking-at org-TBLFM-regexp)))
  3969. (defvar org-clock-start-time)
  3970. (defvar org-clock-marker (make-marker)
  3971. "Marker recording the last clock-in.")
  3972. (defvar org-clock-hd-marker (make-marker)
  3973. "Marker recording the last clock-in, but the headline position.")
  3974. (defvar org-clock-heading ""
  3975. "The heading of the current clock entry.")
  3976. (defun org-clock-is-active ()
  3977. "Return the buffer where the clock is currently running.
  3978. Return nil if no clock is running."
  3979. (marker-buffer org-clock-marker))
  3980. (defun org-check-running-clock ()
  3981. "Check if the current buffer contains the running clock.
  3982. If yes, offer to stop it and to save the buffer with the changes."
  3983. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3984. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3985. (buffer-name))))
  3986. (org-clock-out)
  3987. (when (y-or-n-p "Save changed buffer?")
  3988. (save-buffer))))
  3989. (defun org-clocktable-try-shift (dir n)
  3990. "Check if this line starts a clock table, if yes, shift the time block."
  3991. (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
  3992. (org-clocktable-shift dir n)))
  3993. ;;;###autoload
  3994. (defun org-clock-persistence-insinuate ()
  3995. "Set up hooks for clock persistence."
  3996. (require 'org-clock)
  3997. (add-hook 'org-mode-hook 'org-clock-load)
  3998. (add-hook 'kill-emacs-hook 'org-clock-save))
  3999. (defgroup org-archive nil
  4000. "Options concerning archiving in Org-mode."
  4001. :tag "Org Archive"
  4002. :group 'org-structure)
  4003. (defcustom org-archive-location "%s_archive::"
  4004. "The location where subtrees should be archived.
  4005. The value of this variable is a string, consisting of two parts,
  4006. separated by a double-colon. The first part is a filename and
  4007. the second part is a headline.
  4008. When the filename is omitted, archiving happens in the same file.
  4009. %s in the filename will be replaced by the current file
  4010. name (without the directory part). Archiving to a different file
  4011. is useful to keep archived entries from contributing to the
  4012. Org-mode Agenda.
  4013. The archived entries will be filed as subtrees of the specified
  4014. headline. When the headline is omitted, the subtrees are simply
  4015. filed away at the end of the file, as top-level entries. Also in
  4016. the heading you can use %s to represent the file name, this can be
  4017. useful when using the same archive for a number of different files.
  4018. Here are a few examples:
  4019. \"%s_archive::\"
  4020. If the current file is Projects.org, archive in file
  4021. Projects.org_archive, as top-level trees. This is the default.
  4022. \"::* Archived Tasks\"
  4023. Archive in the current file, under the top-level headline
  4024. \"* Archived Tasks\".
  4025. \"~/org/archive.org::\"
  4026. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  4027. \"~/org/archive.org::* From %s\"
  4028. Archive in file ~/org/archive.org (absolute path), under headlines
  4029. \"From FILENAME\" where file name is the current file name.
  4030. \"~/org/datetree.org::datetree/* Finished Tasks\"
  4031. The \"datetree/\" string is special, signifying to archive
  4032. items to the datetree. Items are placed in either the CLOSED
  4033. date of the item, or the current date if there is no CLOSED date.
  4034. The heading will be a subentry to the current date. There doesn't
  4035. need to be a heading, but there always needs to be a slash after
  4036. datetree. For example, to store archived items directly in the
  4037. datetree, use \"~/org/datetree.org::datetree/\".
  4038. \"basement::** Finished Tasks\"
  4039. Archive in file ./basement (relative path), as level 3 trees
  4040. below the level 2 heading \"** Finished Tasks\".
  4041. You may set this option on a per-file basis by adding to the buffer a
  4042. line like
  4043. #+ARCHIVE: basement::** Finished Tasks
  4044. You may also define it locally for a subtree by setting an ARCHIVE property
  4045. in the entry. If such a property is found in an entry, or anywhere up
  4046. the hierarchy, it will be used."
  4047. :group 'org-archive
  4048. :type 'string)
  4049. (defcustom org-agenda-skip-archived-trees t
  4050. "Non-nil means the agenda will skip any items located in archived trees.
  4051. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  4052. variable is no longer recommended, you should leave it at the value t.
  4053. Instead, use the key `v' to cycle the archives-mode in the agenda."
  4054. :group 'org-archive
  4055. :group 'org-agenda-skip
  4056. :type 'boolean)
  4057. (defcustom org-columns-skip-archived-trees t
  4058. "Non-nil means ignore archived trees when creating column view."
  4059. :group 'org-archive
  4060. :group 'org-properties
  4061. :type 'boolean)
  4062. (defcustom org-cycle-open-archived-trees nil
  4063. "Non-nil means `org-cycle' will open archived trees.
  4064. An archived tree is a tree marked with the tag ARCHIVE.
  4065. When nil, archived trees will stay folded. You can still open them with
  4066. normal outline commands like `show-all', but not with the cycling commands."
  4067. :group 'org-archive
  4068. :group 'org-cycle
  4069. :type 'boolean)
  4070. (defcustom org-sparse-tree-open-archived-trees nil
  4071. "Non-nil means sparse tree construction shows matches in archived trees.
  4072. When nil, matches in these trees are highlighted, but the trees are kept in
  4073. collapsed state."
  4074. :group 'org-archive
  4075. :group 'org-sparse-trees
  4076. :type 'boolean)
  4077. (defcustom org-sparse-tree-default-date-type 'scheduled-or-deadline
  4078. "The default date type when building a sparse tree.
  4079. When this is nil, a date is a scheduled or a deadline timestamp.
  4080. Otherwise, these types are allowed:
  4081. all: all timestamps
  4082. active: only active timestamps (<...>)
  4083. inactive: only inactive timestamps (<...)
  4084. scheduled: only scheduled timestamps
  4085. deadline: only deadline timestamps"
  4086. :type '(choice (const :tag "Scheduled or deadline" scheduled-or-deadline)
  4087. (const :tag "All timestamps" all)
  4088. (const :tag "Only active timestamps" active)
  4089. (const :tag "Only inactive timestamps" inactive)
  4090. (const :tag "Only scheduled timestamps" scheduled)
  4091. (const :tag "Only deadline timestamps" deadline)
  4092. (const :tag "Only closed timestamps" closed))
  4093. :version "24.3"
  4094. :group 'org-sparse-trees)
  4095. (defun org-cycle-hide-archived-subtrees (state)
  4096. "Re-hide all archived subtrees after a visibility state change."
  4097. (when (and (not org-cycle-open-archived-trees)
  4098. (not (memq state '(overview folded))))
  4099. (save-excursion
  4100. (let* ((globalp (memq state '(contents all)))
  4101. (beg (if globalp (point-min) (point)))
  4102. (end (if globalp (point-max) (org-end-of-subtree t))))
  4103. (org-hide-archived-subtrees beg end)
  4104. (goto-char beg)
  4105. (if (looking-at (concat ".*:" org-archive-tag ":"))
  4106. (message "%s" (substitute-command-keys
  4107. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  4108. (defun org-force-cycle-archived ()
  4109. "Cycle subtree even if it is archived."
  4110. (interactive)
  4111. (setq this-command 'org-cycle)
  4112. (let ((org-cycle-open-archived-trees t))
  4113. (call-interactively 'org-cycle)))
  4114. (defun org-hide-archived-subtrees (beg end)
  4115. "Re-hide all archived subtrees after a visibility state change."
  4116. (save-excursion
  4117. (let* ((re (concat ":" org-archive-tag ":")))
  4118. (goto-char beg)
  4119. (while (re-search-forward re end t)
  4120. (when (org-at-heading-p)
  4121. (org-flag-subtree t)
  4122. (org-end-of-subtree t))))))
  4123. (declare-function outline-end-of-heading "outline" ())
  4124. (declare-function outline-flag-region "outline" (from to flag))
  4125. (defun org-flag-subtree (flag)
  4126. (save-excursion
  4127. (org-back-to-heading t)
  4128. (outline-end-of-heading)
  4129. (outline-flag-region (point)
  4130. (progn (org-end-of-subtree t) (point))
  4131. flag)))
  4132. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  4133. ;; Declare Column View Code
  4134. (declare-function org-columns-number-to-string "org-colview" (n fmt &optional printf))
  4135. (declare-function org-columns-get-format-and-top-level "org-colview" ())
  4136. (declare-function org-columns-compute "org-colview" (property))
  4137. ;; Declare ID code
  4138. (declare-function org-id-store-link "org-id")
  4139. (declare-function org-id-locations-load "org-id")
  4140. (declare-function org-id-locations-save "org-id")
  4141. (defvar org-id-track-globally)
  4142. ;;; Variables for pre-computed regular expressions, all buffer local
  4143. (defvar org-todo-regexp nil
  4144. "Matches any of the TODO state keywords.")
  4145. (make-variable-buffer-local 'org-todo-regexp)
  4146. (defvar org-not-done-regexp nil
  4147. "Matches any of the TODO state keywords except the last one.")
  4148. (make-variable-buffer-local 'org-not-done-regexp)
  4149. (defvar org-not-done-heading-regexp nil
  4150. "Matches a TODO headline that is not done.")
  4151. (make-variable-buffer-local 'org-not-done-regexp)
  4152. (defvar org-todo-line-regexp nil
  4153. "Matches a headline and puts TODO state into group 2 if present.")
  4154. (make-variable-buffer-local 'org-todo-line-regexp)
  4155. (defvar org-complex-heading-regexp nil
  4156. "Matches a headline and puts everything into groups:
  4157. group 1: the stars
  4158. group 2: The todo keyword, maybe
  4159. group 3: Priority cookie
  4160. group 4: True headline
  4161. group 5: Tags")
  4162. (make-variable-buffer-local 'org-complex-heading-regexp)
  4163. (defvar org-complex-heading-regexp-format nil
  4164. "Printf format to make regexp to match an exact headline.
  4165. This regexp will match the headline of any node which has the
  4166. exact headline text that is put into the format, but may have any
  4167. TODO state, priority and tags.")
  4168. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  4169. (defvar org-todo-line-tags-regexp nil
  4170. "Matches a headline and puts TODO state into group 2 if present.
  4171. Also put tags into group 4 if tags are present.")
  4172. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  4173. (defconst org-plain-time-of-day-regexp
  4174. (concat
  4175. "\\(\\<[012]?[0-9]"
  4176. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4177. "\\(--?"
  4178. "\\(\\<[012]?[0-9]"
  4179. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4180. "\\)?")
  4181. "Regular expression to match a plain time or time range.
  4182. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4183. groups carry important information:
  4184. 0 the full match
  4185. 1 the first time, range or not
  4186. 8 the second time, if it is a range.")
  4187. (defconst org-plain-time-extension-regexp
  4188. (concat
  4189. "\\(\\<[012]?[0-9]"
  4190. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4191. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  4192. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  4193. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4194. groups carry important information:
  4195. 0 the full match
  4196. 7 hours of duration
  4197. 9 minutes of duration")
  4198. (defconst org-stamp-time-of-day-regexp
  4199. (concat
  4200. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  4201. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  4202. "\\(--?"
  4203. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  4204. "Regular expression to match a timestamp time or time range.
  4205. After a match, the following groups carry important information:
  4206. 0 the full match
  4207. 1 date plus weekday, for back referencing to make sure both times are on the same day
  4208. 2 the first time, range or not
  4209. 4 the second time, if it is a range.")
  4210. (defconst org-startup-options
  4211. '(("fold" org-startup-folded t)
  4212. ("overview" org-startup-folded t)
  4213. ("nofold" org-startup-folded nil)
  4214. ("showall" org-startup-folded nil)
  4215. ("showeverything" org-startup-folded showeverything)
  4216. ("content" org-startup-folded content)
  4217. ("indent" org-startup-indented t)
  4218. ("noindent" org-startup-indented nil)
  4219. ("hidestars" org-hide-leading-stars t)
  4220. ("showstars" org-hide-leading-stars nil)
  4221. ("odd" org-odd-levels-only t)
  4222. ("oddeven" org-odd-levels-only nil)
  4223. ("align" org-startup-align-all-tables t)
  4224. ("noalign" org-startup-align-all-tables nil)
  4225. ("inlineimages" org-startup-with-inline-images t)
  4226. ("noinlineimages" org-startup-with-inline-images nil)
  4227. ("latexpreview" org-startup-with-latex-preview t)
  4228. ("nolatexpreview" org-startup-with-latex-preview nil)
  4229. ("customtime" org-display-custom-times t)
  4230. ("logdone" org-log-done time)
  4231. ("lognotedone" org-log-done note)
  4232. ("nologdone" org-log-done nil)
  4233. ("lognoteclock-out" org-log-note-clock-out t)
  4234. ("nolognoteclock-out" org-log-note-clock-out nil)
  4235. ("logrepeat" org-log-repeat state)
  4236. ("lognoterepeat" org-log-repeat note)
  4237. ("logdrawer" org-log-into-drawer t)
  4238. ("nologdrawer" org-log-into-drawer nil)
  4239. ("logstatesreversed" org-log-states-order-reversed t)
  4240. ("nologstatesreversed" org-log-states-order-reversed nil)
  4241. ("nologrepeat" org-log-repeat nil)
  4242. ("logreschedule" org-log-reschedule time)
  4243. ("lognotereschedule" org-log-reschedule note)
  4244. ("nologreschedule" org-log-reschedule nil)
  4245. ("logredeadline" org-log-redeadline time)
  4246. ("lognoteredeadline" org-log-redeadline note)
  4247. ("nologredeadline" org-log-redeadline nil)
  4248. ("logrefile" org-log-refile time)
  4249. ("lognoterefile" org-log-refile note)
  4250. ("nologrefile" org-log-refile nil)
  4251. ("fninline" org-footnote-define-inline t)
  4252. ("nofninline" org-footnote-define-inline nil)
  4253. ("fnlocal" org-footnote-section nil)
  4254. ("fnauto" org-footnote-auto-label t)
  4255. ("fnprompt" org-footnote-auto-label nil)
  4256. ("fnconfirm" org-footnote-auto-label confirm)
  4257. ("fnplain" org-footnote-auto-label plain)
  4258. ("fnadjust" org-footnote-auto-adjust t)
  4259. ("nofnadjust" org-footnote-auto-adjust nil)
  4260. ("constcgs" constants-unit-system cgs)
  4261. ("constSI" constants-unit-system SI)
  4262. ("noptag" org-tag-persistent-alist nil)
  4263. ("hideblocks" org-hide-block-startup t)
  4264. ("nohideblocks" org-hide-block-startup nil)
  4265. ("beamer" org-startup-with-beamer-mode t)
  4266. ("entitiespretty" org-pretty-entities t)
  4267. ("entitiesplain" org-pretty-entities nil))
  4268. "Variable associated with STARTUP options for org-mode.
  4269. Each element is a list of three items: the startup options (as written
  4270. in the #+STARTUP line), the corresponding variable, and the value to set
  4271. this variable to if the option is found. An optional forth element PUSH
  4272. means to push this value onto the list in the variable.")
  4273. (defcustom org-group-tags t
  4274. "When non-nil (the default), use group tags.
  4275. This can be turned on/off through `org-toggle-tags-groups'."
  4276. :group 'org-tags
  4277. :group 'org-startup
  4278. :type 'boolean)
  4279. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4280. (defun org-toggle-tags-groups ()
  4281. "Toggle support for group tags.
  4282. Support for group tags is controlled by the option
  4283. `org-group-tags', which is non-nil by default."
  4284. (interactive)
  4285. (setq org-group-tags (not org-group-tags))
  4286. (cond ((and (derived-mode-p 'org-agenda-mode)
  4287. org-group-tags)
  4288. (org-agenda-redo))
  4289. ((derived-mode-p 'org-mode)
  4290. (let ((org-inhibit-startup t)) (org-mode))))
  4291. (message "Groups tags support has been turned %s"
  4292. (if org-group-tags "on" "off")))
  4293. (defun org-set-regexps-and-options (&optional tags-only)
  4294. "Precompute regular expressions used in the current buffer.
  4295. When optional argument TAGS-ONLY is non-nil, only compute tags
  4296. related expressions."
  4297. (when (derived-mode-p 'org-mode)
  4298. (let ((alist (org--setup-collect-keywords
  4299. (org-make-options-regexp
  4300. (append '("FILETAGS" "TAGS" "SETUPFILE")
  4301. (and (not tags-only)
  4302. '("ARCHIVE" "CATEGORY" "COLUMNS" "CONSTANTS"
  4303. "LINK" "PRIORITIES" "PROPERTY" "SEQ_TODO"
  4304. "STARTUP" "TODO" "TYP_TODO")))))))
  4305. (org--setup-process-tags
  4306. (cdr (assq 'tags alist)) (cdr (assq 'filetags alist)))
  4307. (unless tags-only
  4308. ;; File properties.
  4309. (org-set-local 'org-file-properties (cdr (assq 'property alist)))
  4310. ;; Archive location.
  4311. (let ((archive (cdr (assq 'archive alist))))
  4312. (when archive (org-set-local 'org-archive-location archive)))
  4313. ;; Category.
  4314. (let ((cat (org-string-nw-p (cdr (assq 'category alist)))))
  4315. (when cat
  4316. (org-set-local 'org-category (intern cat))
  4317. (org-set-local 'org-file-properties
  4318. (org--update-property-plist
  4319. "CATEGORY" cat org-file-properties))))
  4320. ;; Columns.
  4321. (let ((column (cdr (assq 'columns alist))))
  4322. (when column (org-set-local 'org-columns-default-format column)))
  4323. ;; Constants.
  4324. (setq org-table-formula-constants-local (cdr (assq 'constants alist)))
  4325. ;; Link abbreviations.
  4326. (let ((links (cdr (assq 'link alist))))
  4327. (when links (setq org-link-abbrev-alist-local (nreverse links))))
  4328. ;; Priorities.
  4329. (let ((priorities (cdr (assq 'priorities alist))))
  4330. (when priorities
  4331. (org-set-local 'org-highest-priority (nth 0 priorities))
  4332. (org-set-local 'org-lowest-priority (nth 1 priorities))
  4333. (org-set-local 'org-default-priority (nth 2 priorities))))
  4334. ;; Startup options.
  4335. (let ((startup (cdr (assq 'startup alist))))
  4336. (dolist (option startup)
  4337. (let ((entry (assoc-string option org-startup-options t)))
  4338. (when entry
  4339. (let ((var (nth 1 entry))
  4340. (val (nth 2 entry)))
  4341. (if (not (nth 3 entry)) (org-set-local var val)
  4342. (unless (listp (symbol-value var))
  4343. (org-set-local var nil))
  4344. (add-to-list var val)))))))
  4345. ;; TODO keywords.
  4346. (org-set-local 'org-todo-kwd-alist nil)
  4347. (org-set-local 'org-todo-key-alist nil)
  4348. (org-set-local 'org-todo-key-trigger nil)
  4349. (org-set-local 'org-todo-keywords-1 nil)
  4350. (org-set-local 'org-done-keywords nil)
  4351. (org-set-local 'org-todo-heads nil)
  4352. (org-set-local 'org-todo-sets nil)
  4353. (org-set-local 'org-todo-log-states nil)
  4354. (let ((todo-sequences
  4355. (or (nreverse (cdr (assq 'todo alist)))
  4356. (let ((d (default-value 'org-todo-keywords)))
  4357. (if (not (stringp (car d))) d
  4358. ;; XXX: Backward compatibility code.
  4359. (list (cons org-todo-interpretation d)))))))
  4360. (dolist (sequence todo-sequences)
  4361. (let* ((sequence (or (run-hook-with-args-until-success
  4362. 'org-todo-setup-filter-hook sequence)
  4363. sequence))
  4364. (sequence-type (car sequence))
  4365. (keywords (cdr sequence))
  4366. (sep (member "|" keywords))
  4367. names alist)
  4368. (dolist (k (remove "|" keywords))
  4369. (unless (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$"
  4370. k)
  4371. (error "Invalid TODO keyword %s" k))
  4372. (let ((name (match-string 1 k))
  4373. (key (match-string 2 k))
  4374. (log (org-extract-log-state-settings k)))
  4375. (push name names)
  4376. (push (cons name (and key (string-to-char key))) alist)
  4377. (when log (push log org-todo-log-states))))
  4378. (let* ((names (nreverse names))
  4379. (done (if sep (org-remove-keyword-keys (cdr sep))
  4380. (last names)))
  4381. (head (car names))
  4382. (tail (list sequence-type head (car done) (org-last done))))
  4383. (add-to-list 'org-todo-heads head 'append)
  4384. (push names org-todo-sets)
  4385. (setq org-done-keywords (append org-done-keywords done nil))
  4386. (setq org-todo-keywords-1 (append org-todo-keywords-1 names nil))
  4387. (setq org-todo-key-alist
  4388. (append org-todo-key-alist
  4389. (and alist
  4390. (append '((:startgroup))
  4391. (nreverse alist)
  4392. '((:endgroup))))))
  4393. (dolist (k names) (push (cons k tail) org-todo-kwd-alist))))))
  4394. (setq org-todo-sets (nreverse org-todo-sets)
  4395. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  4396. org-todo-key-trigger (delq nil (mapcar #'cdr org-todo-key-alist))
  4397. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist))
  4398. ;; Compute the regular expressions and other local variables.
  4399. ;; Using `org-outline-regexp-bol' would complicate them much,
  4400. ;; because of the fixed white space at the end of that string.
  4401. (if (not org-done-keywords)
  4402. (setq org-done-keywords
  4403. (and org-todo-keywords-1 (last org-todo-keywords-1))))
  4404. (setq org-not-done-keywords
  4405. (org-delete-all org-done-keywords
  4406. (copy-sequence org-todo-keywords-1))
  4407. org-todo-regexp (regexp-opt org-todo-keywords-1 t)
  4408. org-not-done-regexp (regexp-opt org-not-done-keywords t)
  4409. org-not-done-heading-regexp
  4410. (format org-heading-keyword-regexp-format org-not-done-regexp)
  4411. org-todo-line-regexp
  4412. (format org-heading-keyword-maybe-regexp-format org-todo-regexp)
  4413. org-complex-heading-regexp
  4414. (concat "^\\(\\*+\\)"
  4415. "\\(?: +" org-todo-regexp "\\)?"
  4416. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4417. "\\(?: +\\(.*?\\)\\)??"
  4418. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?")
  4419. "[ \t]*$")
  4420. org-complex-heading-regexp-format
  4421. (concat "^\\(\\*+\\)"
  4422. "\\(?: +" org-todo-regexp "\\)?"
  4423. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4424. "\\(?: +"
  4425. ;; Stats cookies can be stuck to body.
  4426. "\\(?:\\[[0-9%%/]+\\] *\\)*"
  4427. "\\(%s\\)"
  4428. "\\(?: *\\[[0-9%%/]+\\]\\)*"
  4429. "\\)"
  4430. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?")
  4431. "[ \t]*$")
  4432. org-todo-line-tags-regexp
  4433. (concat "^\\(\\*+\\)"
  4434. "\\(?: +" org-todo-regexp "\\)?"
  4435. "\\(?: +\\(.*?\\)\\)??"
  4436. (org-re "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?")
  4437. "[ \t]*$"))
  4438. (org-compute-latex-and-related-regexp)))))
  4439. (defun org--setup-collect-keywords (regexp &optional files alist)
  4440. "Return setup keywords values as an alist.
  4441. REGEXP matches a subset of setup keywords. FILES is a list of
  4442. file names already visited. It is used to avoid circular setup
  4443. files. ALIST, when non-nil, is the alist computed so far.
  4444. Return value contains the following keys: `archive', `category',
  4445. `columns', `constants', `filetags', `link', `priorities',
  4446. `property', `startup', `tags' and `todo'."
  4447. (org-with-wide-buffer
  4448. (goto-char (point-min))
  4449. (let ((case-fold-search t))
  4450. (while (re-search-forward regexp nil t)
  4451. (let ((element (org-element-at-point)))
  4452. (when (eq (org-element-type element) 'keyword)
  4453. (let ((key (org-element-property :key element))
  4454. (value (org-element-property :value element)))
  4455. (cond
  4456. ((equal key "ARCHIVE")
  4457. (when (org-string-nw-p value)
  4458. (push (cons 'archive value) alist)))
  4459. ((equal key "CATEGORY") (push (cons 'category value) alist))
  4460. ((equal key "COLUMNS") (push (cons 'columns value) alist))
  4461. ((equal key "CONSTANTS")
  4462. (let* ((constants (assq 'constants alist))
  4463. (store (cdr constants)))
  4464. (dolist (pair (org-split-string value))
  4465. (when (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)"
  4466. pair)
  4467. (let* ((name (match-string 1 pair))
  4468. (value (match-string 2 pair))
  4469. (old (assoc name store)))
  4470. (if old (setcdr old value)
  4471. (push (cons name value) store)))))
  4472. (if constants (setcdr constants store)
  4473. (push (cons 'constants store) alist))))
  4474. ((equal key "FILETAGS")
  4475. (when (org-string-nw-p value)
  4476. (let ((old (assq 'filetags alist))
  4477. (new (apply #'nconc
  4478. (mapcar (lambda (x) (org-split-string x ":"))
  4479. (org-split-string value)))))
  4480. (if old (setcdr old (append new (cdr old)))
  4481. (push (cons 'filetags new) alist)))))
  4482. ((equal key "LINK")
  4483. (when (string-match "\\`\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  4484. (let ((links (assq 'link alist))
  4485. (pair (cons (org-match-string-no-properties 1 value)
  4486. (org-match-string-no-properties 2 value))))
  4487. (if links (push pair (cdr links))
  4488. (push (list 'link pair) alist)))))
  4489. ((equal key "PRIORITIES")
  4490. (push (cons 'priorities
  4491. (let ((prio (org-split-string value)))
  4492. (if (< (length prio) 3) '(?A ?C ?B)
  4493. (mapcar #'string-to-char prio))))
  4494. alist))
  4495. ((equal key "PROPERTY")
  4496. (when (string-match "\\(\\S-+\\)[ \t]+\\(.*\\)" value)
  4497. (let* ((property (assq 'property alist))
  4498. (value (org--update-property-plist
  4499. (org-match-string-no-properties 1 value)
  4500. (org-match-string-no-properties 2 value)
  4501. (cdr property))))
  4502. (if property (setcdr property value)
  4503. (push (cons 'property value) alist)))))
  4504. ((equal key "STARTUP")
  4505. (let ((startup (assq 'startup alist)))
  4506. (if startup
  4507. (setcdr startup
  4508. (append (cdr startup) (org-split-string value)))
  4509. (push (cons 'startup (org-split-string value)) alist))))
  4510. ((equal key "TAGS")
  4511. (let ((tag-cell (assq 'tags alist)))
  4512. (if tag-cell
  4513. (setcdr tag-cell
  4514. (append (cdr tag-cell)
  4515. '("\\n")
  4516. (org-split-string value)))
  4517. (push (cons 'tags (org-split-string value)) alist))))
  4518. ((member key '("TODO" "SEQ_TODO" "TYP_TODO"))
  4519. (let ((todo (assq 'todo alist))
  4520. (value (cons (if (equal key "TYP_TODO") 'type 'sequence)
  4521. (org-split-string value))))
  4522. (if todo (push value (cdr todo))
  4523. (push (list 'todo value) alist))))
  4524. ((equal key "SETUPFILE")
  4525. (unless buffer-read-only ; Do not check in Gnus messages.
  4526. (let ((f (and (org-string-nw-p value)
  4527. (expand-file-name
  4528. (org-remove-double-quotes value)))))
  4529. (when (and f (file-readable-p f) (not (member f files)))
  4530. (with-temp-buffer
  4531. (insert-file-contents f)
  4532. (setq alist
  4533. ;; Fake Org mode to benefit from cache
  4534. ;; without recurring needlessly.
  4535. (let ((major-mode 'org-mode))
  4536. (org--setup-collect-keywords
  4537. regexp (cons f files) alist)))))))))))))))
  4538. alist)
  4539. (defun org--setup-process-tags (tags filetags)
  4540. "Precompute variables used for tags.
  4541. TAGS is a list of tags and tag group symbols, as strings.
  4542. FILETAGS is a list of tags, as strings."
  4543. ;; Process the file tags.
  4544. (org-set-local 'org-file-tags
  4545. (mapcar #'org-add-prop-inherited filetags))
  4546. ;; Provide default tags if no local tags are found.
  4547. (when (and (not tags) org-tag-alist)
  4548. (setq tags
  4549. (mapcar (lambda (tag)
  4550. (case (car tag)
  4551. (:startgroup "{")
  4552. (:endgroup "}")
  4553. (:grouptags ":")
  4554. (:newline "\\n")
  4555. (otherwise (concat (car tag)
  4556. (and (characterp (cdr tag))
  4557. (format "(%c)" (cdr tag)))))))
  4558. org-tag-alist)))
  4559. ;; Process the tags.
  4560. (org-set-local 'org-tag-groups-alist nil)
  4561. (org-set-local 'org-tag-alist nil)
  4562. (let (group-flag)
  4563. (while tags
  4564. (let ((e (car tags)))
  4565. (setq tags (cdr tags))
  4566. (cond
  4567. ((equal e "{")
  4568. (push '(:startgroup) org-tag-alist)
  4569. (when (equal (nth 1 tags) ":") (setq group-flag t)))
  4570. ((equal e "}")
  4571. (push '(:endgroup) org-tag-alist)
  4572. (setq group-flag nil))
  4573. ((equal e ":")
  4574. (push '(:grouptags) org-tag-alist)
  4575. (setq group-flag 'append))
  4576. ((equal e "\\n") (push '(:newline) org-tag-alist))
  4577. ((string-match
  4578. (org-re "\\`\\([[:alnum:]_@#%]+\\)\\(?:(\\(.\\))\\)?\\'") e)
  4579. (let ((tag (match-string 1 e))
  4580. (key (and (match-beginning 2)
  4581. (string-to-char (match-string 2 e)))))
  4582. (cond ((eq group-flag 'append)
  4583. (setcar org-tag-groups-alist
  4584. (append (car org-tag-groups-alist) (list tag))))
  4585. (group-flag (push (list tag) org-tag-groups-alist)))
  4586. (unless (assoc tag org-tag-alist)
  4587. (push (cons tag key) org-tag-alist))))))))
  4588. (setq org-tag-alist (nreverse org-tag-alist)))
  4589. (defun org-file-contents (file &optional noerror)
  4590. "Return the contents of FILE, as a string."
  4591. (if (and file (file-readable-p file))
  4592. (with-temp-buffer
  4593. (insert-file-contents file)
  4594. (buffer-string))
  4595. (funcall (if noerror 'message 'error)
  4596. "Cannot read file \"%s\"%s"
  4597. file
  4598. (let ((from (buffer-file-name (buffer-base-buffer))))
  4599. (if from (concat " (referenced in file \"" from "\")") "")))))
  4600. (defun org-extract-log-state-settings (x)
  4601. "Extract the log state setting from a TODO keyword string.
  4602. This will extract info from a string like \"WAIT(w@/!)\"."
  4603. (let (kw key log1 log2)
  4604. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4605. (setq kw (match-string 1 x)
  4606. key (and (match-end 2) (match-string 2 x))
  4607. log1 (and (match-end 3) (match-string 3 x))
  4608. log2 (and (match-end 4) (match-string 4 x)))
  4609. (and (or log1 log2)
  4610. (list kw
  4611. (and log1 (if (equal log1 "!") 'time 'note))
  4612. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4613. (defun org-remove-keyword-keys (list)
  4614. "Remove a pair of parenthesis at the end of each string in LIST."
  4615. (mapcar (lambda (x)
  4616. (if (string-match "(.*)$" x)
  4617. (substring x 0 (match-beginning 0))
  4618. x))
  4619. list))
  4620. (defun org-assign-fast-keys (alist)
  4621. "Assign fast keys to a keyword-key alist.
  4622. Respect keys that are already there."
  4623. (let (new e (alt ?0))
  4624. (while (setq e (pop alist))
  4625. (if (or (memq (car e) '(:newline :grouptags :endgroup :startgroup))
  4626. (cdr e)) ;; Key already assigned.
  4627. (push e new)
  4628. (let ((clist (string-to-list (downcase (car e))))
  4629. (used (append new alist)))
  4630. (when (= (car clist) ?@)
  4631. (pop clist))
  4632. (while (and clist (rassoc (car clist) used))
  4633. (pop clist))
  4634. (unless clist
  4635. (while (rassoc alt used)
  4636. (incf alt)))
  4637. (push (cons (car e) (or (car clist) alt)) new))))
  4638. (nreverse new)))
  4639. ;;; Some variables used in various places
  4640. (defvar org-window-configuration nil
  4641. "Used in various places to store a window configuration.")
  4642. (defvar org-selected-window nil
  4643. "Used in various places to store a window configuration.")
  4644. (defvar org-finish-function nil
  4645. "Function to be called when `C-c C-c' is used.
  4646. This is for getting out of special buffers like capture.")
  4647. ;; FIXME: Occasionally check by commenting these, to make sure
  4648. ;; no other functions uses these, forgetting to let-bind them.
  4649. (org-no-warnings (defvar entry)) ;; unprefixed, from calendar.el
  4650. (defvar org-last-state)
  4651. (org-no-warnings (defvar date)) ;; unprefixed, from calendar.el
  4652. ;; Defined somewhere in this file, but used before definition.
  4653. (defvar org-entities) ;; defined in org-entities.el
  4654. (defvar org-struct-menu)
  4655. (defvar org-org-menu)
  4656. (defvar org-tbl-menu)
  4657. ;;;; Define the Org-mode
  4658. ;; We use a before-change function to check if a table might need
  4659. ;; an update.
  4660. (defvar org-table-may-need-update t
  4661. "Indicates that a table might need an update.
  4662. This variable is set by `org-before-change-function'.
  4663. `org-table-align' sets it back to nil.")
  4664. (defun org-before-change-function (beg end)
  4665. "Every change indicates that a table might need an update."
  4666. (setq org-table-may-need-update t))
  4667. (defvar org-mode-map)
  4668. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4669. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4670. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4671. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4672. (defvar org-table-buffer-is-an nil)
  4673. (defvar bidi-paragraph-direction)
  4674. (defvar buffer-face-mode-face)
  4675. (require 'outline)
  4676. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4677. (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"))
  4678. (require 'noutline "noutline" 'noerror) ;; stock XEmacs does not have it
  4679. ;; Other stuff we need.
  4680. (require 'time-date)
  4681. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  4682. (require 'easymenu)
  4683. (require 'overlay)
  4684. ;; (require 'org-macs) moved higher up in the file before it is first used
  4685. (require 'org-entities)
  4686. ;; (require 'org-compat) moved higher up in the file before it is first used
  4687. (require 'org-faces)
  4688. (require 'org-list)
  4689. (require 'org-pcomplete)
  4690. (require 'org-src)
  4691. (require 'org-footnote)
  4692. (require 'org-macro)
  4693. ;; babel
  4694. (require 'ob)
  4695. ;;;###autoload
  4696. (define-derived-mode org-mode outline-mode "Org"
  4697. "Outline-based notes management and organizer, alias
  4698. \"Carsten's outline-mode for keeping track of everything.\"
  4699. Org-mode develops organizational tasks around a NOTES file which
  4700. contains information about projects as plain text. Org-mode is
  4701. implemented on top of outline-mode, which is ideal to keep the content
  4702. of large files well structured. It supports ToDo items, deadlines and
  4703. time stamps, which magically appear in the diary listing of the Emacs
  4704. calendar. Tables are easily created with a built-in table editor.
  4705. Plain text URL-like links connect to websites, emails (VM), Usenet
  4706. messages (Gnus), BBDB entries, and any files related to the project.
  4707. For printing and sharing of notes, an Org-mode file (or a part of it)
  4708. can be exported as a structured ASCII or HTML file.
  4709. The following commands are available:
  4710. \\{org-mode-map}"
  4711. ;; Get rid of Outline menus, they are not needed
  4712. ;; Need to do this here because define-derived-mode sets up
  4713. ;; the keymap so late. Still, it is a waste to call this each time
  4714. ;; we switch another buffer into org-mode.
  4715. (if (featurep 'xemacs)
  4716. (when (boundp 'outline-mode-menu-heading)
  4717. ;; Assume this is Greg's port, it uses easymenu
  4718. (easy-menu-remove outline-mode-menu-heading)
  4719. (easy-menu-remove outline-mode-menu-show)
  4720. (easy-menu-remove outline-mode-menu-hide))
  4721. (define-key org-mode-map [menu-bar headings] 'undefined)
  4722. (define-key org-mode-map [menu-bar hide] 'undefined)
  4723. (define-key org-mode-map [menu-bar show] 'undefined))
  4724. (org-load-modules-maybe)
  4725. (easy-menu-add org-org-menu)
  4726. (easy-menu-add org-tbl-menu)
  4727. (org-install-agenda-files-menu)
  4728. (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4729. (add-to-invisibility-spec '(org-cwidth))
  4730. (add-to-invisibility-spec '(org-hide-block . t))
  4731. (when (featurep 'xemacs)
  4732. (org-set-local 'line-move-ignore-invisible t))
  4733. (org-set-local 'outline-regexp org-outline-regexp)
  4734. (org-set-local 'outline-level 'org-outline-level)
  4735. (setq bidi-paragraph-direction 'left-to-right)
  4736. (when (and org-ellipsis
  4737. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4738. (fboundp 'make-glyph-code))
  4739. (unless org-display-table
  4740. (setq org-display-table (make-display-table)))
  4741. (set-display-table-slot
  4742. org-display-table 4
  4743. (vconcat (mapcar
  4744. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4745. org-ellipsis)))
  4746. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4747. (setq buffer-display-table org-display-table))
  4748. (org-set-regexps-and-options)
  4749. (org-set-font-lock-defaults)
  4750. (when (and org-tag-faces (not org-tags-special-faces-re))
  4751. ;; tag faces set outside customize.... force initialization.
  4752. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4753. ;; Calc embedded
  4754. (org-set-local 'calc-embedded-open-mode "# ")
  4755. ;; Modify a few syntax entries
  4756. (modify-syntax-entry ?@ "w")
  4757. (modify-syntax-entry ?\" "\"")
  4758. (modify-syntax-entry ?\\ "_")
  4759. (modify-syntax-entry ?~ "_")
  4760. (if org-startup-truncated (setq truncate-lines t))
  4761. (when org-startup-indented (require 'org-indent) (org-indent-mode 1))
  4762. (org-set-local 'font-lock-unfontify-region-function
  4763. 'org-unfontify-region)
  4764. ;; Activate before-change-function
  4765. (org-set-local 'org-table-may-need-update t)
  4766. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4767. 'local)
  4768. ;; Check for running clock before killing a buffer
  4769. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4770. ;; Initialize macros templates.
  4771. (org-macro-initialize-templates)
  4772. ;; Initialize radio targets.
  4773. (org-update-radio-target-regexp)
  4774. ;; Indentation.
  4775. (org-set-local 'indent-line-function 'org-indent-line)
  4776. (org-set-local 'indent-region-function 'org-indent-region)
  4777. ;; Filling and auto-filling.
  4778. (org-setup-filling)
  4779. ;; Comments.
  4780. (org-setup-comments-handling)
  4781. ;; Initialize cache.
  4782. (org-element-cache-reset)
  4783. ;; Beginning/end of defun
  4784. (org-set-local 'beginning-of-defun-function 'org-backward-element)
  4785. (org-set-local 'end-of-defun-function
  4786. (lambda ()
  4787. (if (not (org-at-heading-p))
  4788. (org-forward-element)
  4789. (org-forward-element)
  4790. (forward-char -1))))
  4791. ;; Next error for sparse trees
  4792. (org-set-local 'next-error-function 'org-occur-next-match)
  4793. ;; Make sure dependence stuff works reliably, even for users who set it
  4794. ;; too late :-(
  4795. (if org-enforce-todo-dependencies
  4796. (add-hook 'org-blocker-hook
  4797. 'org-block-todo-from-children-or-siblings-or-parent)
  4798. (remove-hook 'org-blocker-hook
  4799. 'org-block-todo-from-children-or-siblings-or-parent))
  4800. (if org-enforce-todo-checkbox-dependencies
  4801. (add-hook 'org-blocker-hook
  4802. 'org-block-todo-from-checkboxes)
  4803. (remove-hook 'org-blocker-hook
  4804. 'org-block-todo-from-checkboxes))
  4805. ;; Align options lines
  4806. (org-set-local
  4807. 'align-mode-rules-list
  4808. '((org-in-buffer-settings
  4809. (regexp . "^[ \t]*#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4810. (modes . '(org-mode)))))
  4811. ;; Imenu
  4812. (org-set-local 'imenu-create-index-function
  4813. 'org-imenu-get-tree)
  4814. ;; Make isearch reveal context
  4815. (if (or (featurep 'xemacs)
  4816. (not (boundp 'outline-isearch-open-invisible-function)))
  4817. ;; Emacs 21 and XEmacs make use of the hook
  4818. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4819. ;; Emacs 22 deals with this through a special variable
  4820. (org-set-local 'outline-isearch-open-invisible-function
  4821. (lambda (&rest ignore) (org-show-context 'isearch))))
  4822. ;; Setup the pcomplete hooks
  4823. (set (make-local-variable 'pcomplete-command-completion-function)
  4824. 'org-pcomplete-initial)
  4825. (set (make-local-variable 'pcomplete-command-name-function)
  4826. 'org-command-at-point)
  4827. (set (make-local-variable 'pcomplete-default-completion-function)
  4828. 'ignore)
  4829. (set (make-local-variable 'pcomplete-parse-arguments-function)
  4830. 'org-parse-arguments)
  4831. (set (make-local-variable 'pcomplete-termination-string) "")
  4832. (when (>= emacs-major-version 23)
  4833. (set (make-local-variable 'buffer-face-mode-face) 'org-default))
  4834. ;; If empty file that did not turn on org-mode automatically, make it to.
  4835. (if (and org-insert-mode-line-in-empty-file
  4836. (org-called-interactively-p 'any)
  4837. (= (point-min) (point-max)))
  4838. (insert "# -*- mode: org -*-\n\n"))
  4839. (unless org-inhibit-startup
  4840. (org-unmodified
  4841. (and org-startup-with-beamer-mode (org-beamer-mode))
  4842. (when org-startup-align-all-tables
  4843. (org-table-map-tables 'org-table-align 'quietly))
  4844. (when org-startup-with-inline-images
  4845. (org-display-inline-images))
  4846. (when org-startup-with-latex-preview
  4847. (org-toggle-latex-fragment))
  4848. (unless org-inhibit-startup-visibility-stuff
  4849. (org-set-startup-visibility))
  4850. (org-refresh-effort-properties)))
  4851. ;; Try to set org-hide correctly
  4852. (let ((foreground (org-find-invisible-foreground)))
  4853. (if foreground
  4854. (set-face-foreground 'org-hide foreground))))
  4855. ;; Update `customize-package-emacs-version-alist'
  4856. (add-to-list 'customize-package-emacs-version-alist
  4857. '(Org ("6.21b" . "23.1") ("6.33x" . "23.2")
  4858. ("7.8.11" . "24.1") ("7.9.4" . "24.3")
  4859. ("8.2.6" . "24.4") ("8.3" . "25.1")))
  4860. (defvar org-mode-transpose-word-syntax-table
  4861. (let ((st (make-syntax-table text-mode-syntax-table)))
  4862. (mapc (lambda(c) (modify-syntax-entry
  4863. (string-to-char (car c)) "w p" st))
  4864. org-emphasis-alist)
  4865. st))
  4866. (when (fboundp 'abbrev-table-put)
  4867. (abbrev-table-put org-mode-abbrev-table
  4868. :parents (list text-mode-abbrev-table)))
  4869. (defun org-find-invisible-foreground ()
  4870. (let ((candidates (remove
  4871. "unspecified-bg"
  4872. (nconc
  4873. (list (face-background 'default)
  4874. (face-background 'org-default))
  4875. (mapcar
  4876. (lambda (alist)
  4877. (when (boundp alist)
  4878. (cdr (assoc 'background-color (symbol-value alist)))))
  4879. '(default-frame-alist initial-frame-alist window-system-default-frame-alist))
  4880. (list (face-foreground 'org-hide))))))
  4881. (car (remove nil candidates))))
  4882. (defun org-current-time (&optional rounding-minutes past)
  4883. "Current time, possibly rounded to ROUNDING-MINUTES.
  4884. When ROUNDING-MINUTES is not an integer, fall back on the car of
  4885. `org-time-stamp-rounding-minutes'. When PAST is non-nil, ensure
  4886. the rounding returns a past time."
  4887. (let ((r (or (and (integerp rounding-minutes) rounding-minutes)
  4888. (car org-time-stamp-rounding-minutes)))
  4889. (time (decode-time)) res)
  4890. (if (< r 1)
  4891. (current-time)
  4892. (setq res
  4893. (apply 'encode-time
  4894. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4895. (nthcdr 2 time))))
  4896. (if (and past (< (org-float-time (time-subtract (current-time) res)) 0))
  4897. (seconds-to-time (- (org-float-time res) (* r 60)))
  4898. res))))
  4899. (defun org-today ()
  4900. "Return today date, considering `org-extend-today-until'."
  4901. (time-to-days
  4902. (time-subtract (current-time)
  4903. (list 0 (* 3600 org-extend-today-until) 0))))
  4904. ;;;; Font-Lock stuff, including the activators
  4905. (defvar org-mouse-map (make-sparse-keymap))
  4906. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4907. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4908. (when org-mouse-1-follows-link
  4909. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4910. (when org-tab-follows-link
  4911. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4912. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4913. (require 'font-lock)
  4914. (defconst org-non-link-chars "]\t\n\r<>")
  4915. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "file+emacs"
  4916. "file+sys" "news" "shell" "elisp" "doi" "message"
  4917. "help"))
  4918. (defvar org-link-types-re nil
  4919. "Matches a link that has a url-like prefix like \"http:\"")
  4920. (defvar org-link-re-with-space nil
  4921. "Matches a link with spaces, optional angular brackets around it.")
  4922. (defvar org-link-re-with-space2 nil
  4923. "Matches a link with spaces, optional angular brackets around it.")
  4924. (defvar org-link-re-with-space3 nil
  4925. "Matches a link with spaces, only for internal part in bracket links.")
  4926. (defvar org-angle-link-re nil
  4927. "Matches link with angular brackets, spaces are allowed.")
  4928. (defvar org-plain-link-re nil
  4929. "Matches plain link, without spaces.")
  4930. (defvar org-bracket-link-regexp nil
  4931. "Matches a link in double brackets.")
  4932. (defvar org-bracket-link-analytic-regexp nil
  4933. "Regular expression used to analyze links.
  4934. Here is what the match groups contain after a match:
  4935. 1: http:
  4936. 2: http
  4937. 3: path
  4938. 4: [desc]
  4939. 5: desc")
  4940. (defvar org-bracket-link-analytic-regexp++ nil
  4941. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4942. (defvar org-any-link-re nil
  4943. "Regular expression matching any link.")
  4944. (defconst org-match-sexp-depth 3
  4945. "Number of stacked braces for sub/superscript matching.")
  4946. (defun org-create-multibrace-regexp (left right n)
  4947. "Create a regular expression which will match a balanced sexp.
  4948. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4949. as single character strings.
  4950. The regexp returned will match the entire expression including the
  4951. delimiters. It will also define a single group which contains the
  4952. match except for the outermost delimiters. The maximum depth of
  4953. stacked delimiters is N. Escaping delimiters is not possible."
  4954. (let* ((nothing (concat "[^" left right "]*?"))
  4955. (or "\\|")
  4956. (re nothing)
  4957. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4958. (while (> n 1)
  4959. (setq n (1- n)
  4960. re (concat re or next)
  4961. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4962. (concat left "\\(" re "\\)" right)))
  4963. (defconst org-match-substring-regexp
  4964. (concat
  4965. "\\(\\S-\\)\\([_^]\\)\\("
  4966. "\\(?:" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4967. "\\|"
  4968. "\\(?:" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  4969. "\\|"
  4970. "\\(?:\\*\\|[+-]?[[:alnum:].,\\]*[[:alnum:]]\\)\\)")
  4971. "The regular expression matching a sub- or superscript.")
  4972. (defconst org-match-substring-with-braces-regexp
  4973. (concat
  4974. "\\(\\S-\\)\\([_^]\\)"
  4975. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)")
  4976. "The regular expression matching a sub- or superscript, forcing braces.")
  4977. (defun org-make-link-regexps ()
  4978. "Update the link regular expressions.
  4979. This should be called after the variable `org-link-types' has changed."
  4980. (let ((types-re (regexp-opt org-link-types t)))
  4981. (setq org-link-types-re
  4982. (concat "\\`" types-re ":")
  4983. org-link-re-with-space
  4984. (concat "<?" types-re ":"
  4985. "\\([^" org-non-link-chars " ]"
  4986. "[^" org-non-link-chars "]*"
  4987. "[^" org-non-link-chars " ]\\)>?")
  4988. org-link-re-with-space2
  4989. (concat "<?" types-re ":"
  4990. "\\([^" org-non-link-chars " ]"
  4991. "[^\t\n\r]*"
  4992. "[^" org-non-link-chars " ]\\)>?")
  4993. org-link-re-with-space3
  4994. (concat "<?" types-re ":"
  4995. "\\([^" org-non-link-chars " ]"
  4996. "[^\t\n\r]*\\)")
  4997. org-angle-link-re
  4998. (concat "<" types-re ":"
  4999. "\\([^" org-non-link-chars " ]"
  5000. "[^" org-non-link-chars "]*"
  5001. "\\)>")
  5002. org-plain-link-re
  5003. (concat
  5004. "\\<" types-re ":"
  5005. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  5006. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  5007. org-bracket-link-regexp
  5008. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  5009. org-bracket-link-analytic-regexp
  5010. (concat
  5011. "\\[\\["
  5012. "\\(" types-re ":\\)?"
  5013. "\\([^]]+\\)"
  5014. "\\]"
  5015. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  5016. "\\]")
  5017. org-bracket-link-analytic-regexp++
  5018. (concat
  5019. "\\[\\["
  5020. "\\(" (regexp-opt (cons "coderef" org-link-types) t) ":\\)?"
  5021. "\\([^]]+\\)"
  5022. "\\]"
  5023. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  5024. "\\]")
  5025. org-any-link-re
  5026. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  5027. org-angle-link-re "\\)\\|\\("
  5028. org-plain-link-re "\\)"))))
  5029. (org-make-link-regexps)
  5030. (defvar org-emph-face nil)
  5031. (defun org-do-emphasis-faces (limit)
  5032. "Run through the buffer and emphasize strings."
  5033. (let (rtn a)
  5034. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  5035. (let* ((border (char-after (match-beginning 3)))
  5036. (bre (regexp-quote (char-to-string border))))
  5037. (if (and (not (= border (char-after (match-beginning 4))))
  5038. (not (save-match-data
  5039. (string-match (concat bre ".*" bre)
  5040. (replace-regexp-in-string
  5041. "\n" " "
  5042. (substring (match-string 2) 1 -1))))))
  5043. (progn
  5044. (setq rtn t)
  5045. (setq a (assoc (match-string 3) org-emphasis-alist))
  5046. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  5047. 'face
  5048. (nth 1 a))
  5049. (and (nth 2 a)
  5050. (org-remove-flyspell-overlays-in
  5051. (match-beginning 0) (match-end 0)))
  5052. (add-text-properties (match-beginning 2) (match-end 2)
  5053. '(font-lock-multiline t org-emphasis t))
  5054. (when org-hide-emphasis-markers
  5055. (add-text-properties (match-end 4) (match-beginning 5)
  5056. '(invisible org-link))
  5057. (add-text-properties (match-beginning 3) (match-end 3)
  5058. '(invisible org-link))))))
  5059. (goto-char (1+ (match-beginning 0))))
  5060. rtn))
  5061. (defun org-emphasize (&optional char)
  5062. "Insert or change an emphasis, i.e. a font like bold or italic.
  5063. If there is an active region, change that region to a new emphasis.
  5064. If there is no region, just insert the marker characters and position
  5065. the cursor between them.
  5066. CHAR should be the marker character. If it is a space, it means to
  5067. remove the emphasis of the selected region.
  5068. If CHAR is not given (for example in an interactive call) it will be
  5069. prompted for."
  5070. (interactive)
  5071. (let ((erc org-emphasis-regexp-components)
  5072. (prompt "")
  5073. (string "") beg end move c s)
  5074. (if (org-region-active-p)
  5075. (setq beg (region-beginning) end (region-end)
  5076. string (buffer-substring beg end))
  5077. (setq move t))
  5078. (unless char
  5079. (message "Emphasis marker or tag: [%s]"
  5080. (mapconcat (lambda(e) (car e)) org-emphasis-alist ""))
  5081. (setq char (read-char-exclusive)))
  5082. (if (equal char ?\ )
  5083. (setq s "" move nil)
  5084. (unless (assoc (char-to-string char) org-emphasis-alist)
  5085. (user-error "No such emphasis marker: \"%c\"" char))
  5086. (setq s (char-to-string char)))
  5087. (while (and (> (length string) 1)
  5088. (equal (substring string 0 1) (substring string -1))
  5089. (assoc (substring string 0 1) org-emphasis-alist))
  5090. (setq string (substring string 1 -1)))
  5091. (setq string (concat s string s))
  5092. (if beg (delete-region beg end))
  5093. (unless (or (bolp)
  5094. (string-match (concat "[" (nth 0 erc) "\n]")
  5095. (char-to-string (char-before (point)))))
  5096. (insert " "))
  5097. (unless (or (eobp)
  5098. (string-match (concat "[" (nth 1 erc) "\n]")
  5099. (char-to-string (char-after (point)))))
  5100. (insert " ") (backward-char 1))
  5101. (insert string)
  5102. (and move (backward-char 1))))
  5103. (defconst org-nonsticky-props
  5104. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text htmlize-link))
  5105. (defsubst org-rear-nonsticky-at (pos)
  5106. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  5107. (defun org-activate-plain-links (limit)
  5108. "Add link properties for plain links."
  5109. (let (f hl)
  5110. (when (and (re-search-forward (concat org-plain-link-re) limit t)
  5111. (not (member 'org-tag
  5112. (get-text-property (1- (match-beginning 0)) 'face)))
  5113. (not (org-in-src-block-p)))
  5114. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5115. (setq f (get-text-property (match-beginning 0) 'face))
  5116. (setq hl (org-match-string-no-properties 0))
  5117. (if (or (eq f 'org-tag)
  5118. (and (listp f) (memq 'org-tag f)))
  5119. nil
  5120. (add-text-properties (match-beginning 0) (match-end 0)
  5121. (list 'mouse-face 'highlight
  5122. 'face 'org-link
  5123. 'htmlize-link `(:uri ,hl)
  5124. 'keymap org-mouse-map))
  5125. (org-rear-nonsticky-at (match-end 0)))
  5126. t)))
  5127. (defun org-activate-code (limit)
  5128. (if (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t)
  5129. (progn
  5130. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5131. (remove-text-properties (match-beginning 0) (match-end 0)
  5132. '(display t invisible t intangible t))
  5133. t)))
  5134. (defcustom org-src-fontify-natively t
  5135. "When non-nil, fontify code in code blocks."
  5136. :type 'boolean
  5137. :version "24.4"
  5138. :package-version '(Org . "8.3")
  5139. :group 'org-appearance
  5140. :group 'org-babel)
  5141. (defcustom org-allow-promoting-top-level-subtree nil
  5142. "When non-nil, allow promoting a top level subtree.
  5143. The leading star of the top level headline will be replaced
  5144. by a #."
  5145. :type 'boolean
  5146. :version "24.1"
  5147. :group 'org-appearance)
  5148. (defun org-fontify-meta-lines-and-blocks (limit)
  5149. (condition-case nil
  5150. (org-fontify-meta-lines-and-blocks-1 limit)
  5151. (error (message "org-mode fontification error"))))
  5152. (defun org-fontify-meta-lines-and-blocks-1 (limit)
  5153. "Fontify #+ lines and blocks."
  5154. (let ((case-fold-search t))
  5155. (if (re-search-forward
  5156. "^\\([ \t]*#\\(\\(\\+[a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  5157. limit t)
  5158. (let ((beg (match-beginning 0))
  5159. (block-start (match-end 0))
  5160. (block-end nil)
  5161. (lang (match-string 7))
  5162. (beg1 (line-beginning-position 2))
  5163. (dc1 (downcase (match-string 2)))
  5164. (dc3 (downcase (match-string 3)))
  5165. end end1 quoting block-type ovl)
  5166. (cond
  5167. ((member dc1 '("+html:" "+ascii:" "+latex:"))
  5168. ;; a single line of backend-specific content
  5169. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5170. (remove-text-properties (match-beginning 0) (match-end 0)
  5171. '(display t invisible t intangible t))
  5172. (add-text-properties (match-beginning 1) (match-end 3)
  5173. '(font-lock-fontified t face org-meta-line))
  5174. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  5175. '(font-lock-fontified t face org-block))
  5176. ; for backend-specific code
  5177. t)
  5178. ((and (match-end 4) (equal dc3 "+begin"))
  5179. ;; Truly a block
  5180. (setq block-type (downcase (match-string 5))
  5181. quoting (member block-type org-protecting-blocks))
  5182. (when (re-search-forward
  5183. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  5184. nil t) ;; on purpose, we look further than LIMIT
  5185. (setq end (min (point-max) (match-end 0))
  5186. end1 (min (point-max) (1- (match-beginning 0))))
  5187. (setq block-end (match-beginning 0))
  5188. (when quoting
  5189. (org-remove-flyspell-overlays-in beg1 end1)
  5190. (remove-text-properties beg end
  5191. '(display t invisible t intangible t)))
  5192. (add-text-properties
  5193. beg end '(font-lock-fontified t font-lock-multiline t))
  5194. (add-text-properties beg beg1 '(face org-meta-line))
  5195. (org-remove-flyspell-overlays-in beg beg1)
  5196. (add-text-properties ; For end_src
  5197. end1 (min (point-max) (1+ end)) '(face org-meta-line))
  5198. (org-remove-flyspell-overlays-in end1 end)
  5199. (cond
  5200. ((and lang (not (string= lang "")) org-src-fontify-natively)
  5201. (org-src-font-lock-fontify-block lang block-start block-end)
  5202. (add-text-properties beg1 block-end '(src-block t)))
  5203. (quoting
  5204. (add-text-properties beg1 (min (point-max) (1+ end1))
  5205. '(face org-block))) ; end of source block
  5206. ((not org-fontify-quote-and-verse-blocks))
  5207. ((string= block-type "quote")
  5208. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-quote)))
  5209. ((string= block-type "verse")
  5210. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-verse))))
  5211. (add-text-properties beg beg1 '(face org-block-begin-line))
  5212. (add-text-properties (min (point-max) (1+ end)) (min (point-max) (1+ end1))
  5213. '(face org-block-end-line))
  5214. t))
  5215. ((member dc1 '("+title:" "+author:" "+email:" "+date:"))
  5216. (org-remove-flyspell-overlays-in
  5217. (match-beginning 0)
  5218. (if (equal "+title:" dc1) (match-end 2) (match-end 0)))
  5219. (add-text-properties
  5220. beg (match-end 3)
  5221. (if (member (intern (substring dc1 1 -1)) org-hidden-keywords)
  5222. '(font-lock-fontified t invisible t)
  5223. '(font-lock-fontified t face org-document-info-keyword)))
  5224. (add-text-properties
  5225. (match-beginning 6) (min (point-max) (1+ (match-end 6)))
  5226. (if (string-equal dc1 "+title:")
  5227. '(font-lock-fontified t face org-document-title)
  5228. '(font-lock-fontified t face org-document-info))))
  5229. ((or (equal dc1 "+results")
  5230. (member dc1 '("+begin:" "+end:" "+caption:" "+label:"
  5231. "+orgtbl:" "+tblfm:" "+tblname:" "+results:"
  5232. "+call:" "+header:" "+headers:" "+name:"))
  5233. (and (match-end 4) (equal dc3 "+attr")))
  5234. (org-remove-flyspell-overlays-in
  5235. (match-beginning 0)
  5236. (if (equal "+caption:" dc1) (match-end 2) (match-end 0)))
  5237. (add-text-properties
  5238. beg (match-end 0)
  5239. '(font-lock-fontified t face org-meta-line))
  5240. t)
  5241. ((member dc3 '(" " ""))
  5242. (org-remove-flyspell-overlays-in beg (match-end 0))
  5243. (add-text-properties
  5244. beg (match-end 0)
  5245. '(font-lock-fontified t face font-lock-comment-face)))
  5246. (t ;; just any other in-buffer setting, but not indented
  5247. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5248. (add-text-properties
  5249. beg (match-end 0)
  5250. '(font-lock-fontified t face org-meta-line))
  5251. t))))))
  5252. (defun org-fontify-drawers (limit)
  5253. "Fontify drawers."
  5254. (when (re-search-forward org-drawer-regexp limit t)
  5255. (add-text-properties
  5256. (match-beginning 0) (match-end 0)
  5257. '(font-lock-fontified t face org-special-keyword))
  5258. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5259. t))
  5260. (defun org-fontify-macros (limit)
  5261. "Fontify macros."
  5262. (when (re-search-forward "\\({{{\\).+?\\(}}}\\)" limit t)
  5263. (add-text-properties
  5264. (match-beginning 0) (match-end 0)
  5265. '(font-lock-fontified t face org-macro))
  5266. (when org-hide-macro-markers
  5267. (add-text-properties (match-end 2) (match-beginning 2)
  5268. '(invisible t))
  5269. (add-text-properties (match-beginning 1) (match-end 1)
  5270. '(invisible t)))
  5271. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5272. t))
  5273. (defun org-activate-angle-links (limit)
  5274. "Add text properties for angle links."
  5275. (if (and (re-search-forward org-angle-link-re limit t)
  5276. (not (org-in-src-block-p)))
  5277. (progn
  5278. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5279. (add-text-properties (match-beginning 0) (match-end 0)
  5280. (list 'mouse-face 'highlight
  5281. 'keymap org-mouse-map))
  5282. (org-rear-nonsticky-at (match-end 0))
  5283. t)))
  5284. (defun org-activate-footnote-links (limit)
  5285. "Add text properties for footnotes."
  5286. (let ((fn (org-footnote-next-reference-or-definition limit)))
  5287. (when fn
  5288. (let* ((beg (nth 1 fn))
  5289. (end (nth 2 fn))
  5290. (label (car fn))
  5291. (referencep (/= (line-beginning-position) beg)))
  5292. (when (and referencep (nth 3 fn))
  5293. (save-excursion
  5294. (goto-char beg)
  5295. (search-forward (or label "fn:"))
  5296. (org-remove-flyspell-overlays-in beg (match-end 0))))
  5297. (add-text-properties beg end
  5298. (list 'mouse-face 'highlight
  5299. 'keymap org-mouse-map
  5300. 'help-echo
  5301. (if referencep "Footnote reference"
  5302. "Footnote definition")
  5303. 'font-lock-fontified t
  5304. 'font-lock-multiline t
  5305. 'face 'org-footnote))))))
  5306. (defun org-activate-bracket-links (limit)
  5307. "Add text properties for bracketed links."
  5308. (if (and (re-search-forward org-bracket-link-regexp limit t)
  5309. (not (org-in-src-block-p)))
  5310. (let* ((hl (org-match-string-no-properties 1))
  5311. (help (concat "LINK: " (save-match-data (org-link-unescape hl))))
  5312. (ip (org-maybe-intangible
  5313. (list 'invisible 'org-link
  5314. 'keymap org-mouse-map 'mouse-face 'highlight
  5315. 'font-lock-multiline t 'help-echo help
  5316. 'htmlize-link `(:uri ,hl))))
  5317. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  5318. 'font-lock-multiline t 'help-echo help
  5319. 'htmlize-link `(:uri ,hl))))
  5320. ;; We need to remove the invisible property here. Table narrowing
  5321. ;; may have made some of this invisible.
  5322. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5323. (remove-text-properties (match-beginning 0) (match-end 0)
  5324. '(invisible nil))
  5325. (if (match-end 3)
  5326. (progn
  5327. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  5328. (org-rear-nonsticky-at (match-beginning 3))
  5329. (add-text-properties (match-beginning 3) (match-end 3) vp)
  5330. (org-rear-nonsticky-at (match-end 3))
  5331. (add-text-properties (match-end 3) (match-end 0) ip)
  5332. (org-rear-nonsticky-at (match-end 0)))
  5333. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  5334. (org-rear-nonsticky-at (match-beginning 1))
  5335. (add-text-properties (match-beginning 1) (match-end 1) vp)
  5336. (org-rear-nonsticky-at (match-end 1))
  5337. (add-text-properties (match-end 1) (match-end 0) ip)
  5338. (org-rear-nonsticky-at (match-end 0)))
  5339. t)))
  5340. (defun org-activate-dates (limit)
  5341. "Add text properties for dates."
  5342. (if (and (re-search-forward org-tsr-regexp-both limit t)
  5343. (not (equal (char-before (match-beginning 0)) 91)))
  5344. (progn
  5345. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5346. (add-text-properties (match-beginning 0) (match-end 0)
  5347. (list 'mouse-face 'highlight
  5348. 'keymap org-mouse-map))
  5349. (org-rear-nonsticky-at (match-end 0))
  5350. (when org-display-custom-times
  5351. (if (match-end 3)
  5352. (org-display-custom-time (match-beginning 3) (match-end 3)))
  5353. (org-display-custom-time (match-beginning 1) (match-end 1)))
  5354. t)))
  5355. (defvar org-target-link-regexp nil
  5356. "Regular expression matching radio targets in plain text.")
  5357. (make-variable-buffer-local 'org-target-link-regexp)
  5358. (defconst org-target-regexp (let ((border "[^<>\n\r \t]"))
  5359. (format "<<\\(%s\\|%s[^<>\n\r]*%s\\)>>"
  5360. border border border))
  5361. "Regular expression matching a link target.")
  5362. (defconst org-radio-target-regexp (format "<%s>" org-target-regexp)
  5363. "Regular expression matching a radio target.")
  5364. (defconst org-any-target-regexp
  5365. (format "%s\\|%s" org-radio-target-regexp org-target-regexp)
  5366. "Regular expression matching any target.")
  5367. (defun org-activate-target-links (limit)
  5368. "Add text properties for target matches."
  5369. (when org-target-link-regexp
  5370. (let ((case-fold-search t))
  5371. (if (re-search-forward org-target-link-regexp limit t)
  5372. (progn
  5373. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5374. (add-text-properties (match-beginning 1) (match-end 1)
  5375. (list 'mouse-face 'highlight
  5376. 'keymap org-mouse-map
  5377. 'help-echo "Radio target link"
  5378. 'org-linked-text t))
  5379. (org-rear-nonsticky-at (match-end 1))
  5380. t)))))
  5381. (defun org-update-radio-target-regexp ()
  5382. "Find all radio targets in this file and update the regular expression.
  5383. Also refresh fontification if needed."
  5384. (interactive)
  5385. (let ((old-regexp org-target-link-regexp)
  5386. (before-re "\\(?:^\\|[^[:alnum:]]\\)\\(")
  5387. (after-re "\\)\\(?:$\\|[^[:alnum:]]\\)")
  5388. (targets
  5389. (org-with-wide-buffer
  5390. (goto-char (point-min))
  5391. (let (rtn)
  5392. (while (re-search-forward org-radio-target-regexp nil t)
  5393. ;; Make sure point is really within the object.
  5394. (backward-char)
  5395. (let ((obj (org-element-context)))
  5396. (when (eq (org-element-type obj) 'radio-target)
  5397. (add-to-list 'rtn (org-element-property :value obj)))))
  5398. rtn))))
  5399. (setq org-target-link-regexp
  5400. (and targets
  5401. (concat before-re
  5402. (mapconcat
  5403. (lambda (x)
  5404. (replace-regexp-in-string
  5405. " +" "\\s-+" (regexp-quote x) t t))
  5406. targets
  5407. "\\|")
  5408. after-re)))
  5409. (unless (equal old-regexp org-target-link-regexp)
  5410. ;; Clean-up cache.
  5411. (let ((regexp (cond ((not old-regexp) org-target-link-regexp)
  5412. ((not org-target-link-regexp) old-regexp)
  5413. (t
  5414. (concat before-re
  5415. (mapconcat
  5416. (lambda (re)
  5417. (substring re (length before-re)
  5418. (- (length after-re))))
  5419. (list old-regexp org-target-link-regexp)
  5420. "\\|")
  5421. after-re)))))
  5422. (org-with-wide-buffer
  5423. (goto-char (point-min))
  5424. (while (re-search-forward regexp nil t)
  5425. (org-element-cache-refresh (match-beginning 1)))))
  5426. ;; Re fontify buffer.
  5427. (when (memq 'radio org-highlight-links)
  5428. (org-restart-font-lock)))))
  5429. (defun org-hide-wide-columns (limit)
  5430. (let (s e)
  5431. (setq s (text-property-any (point) (or limit (point-max))
  5432. 'org-cwidth t))
  5433. (when s
  5434. (setq e (next-single-property-change s 'org-cwidth))
  5435. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  5436. (goto-char e)
  5437. t)))
  5438. (defvar org-latex-and-related-regexp nil
  5439. "Regular expression for highlighting LaTeX, entities and sub/superscript.")
  5440. (defun org-compute-latex-and-related-regexp ()
  5441. "Compute regular expression for LaTeX, entities and sub/superscript.
  5442. Result depends on variable `org-highlight-latex-and-related'."
  5443. (org-set-local
  5444. 'org-latex-and-related-regexp
  5445. (let* ((re-sub
  5446. (cond ((not (memq 'script org-highlight-latex-and-related)) nil)
  5447. ((eq org-use-sub-superscripts '{})
  5448. (list org-match-substring-with-braces-regexp))
  5449. (org-use-sub-superscripts (list org-match-substring-regexp))))
  5450. (re-latex
  5451. (when (memq 'latex org-highlight-latex-and-related)
  5452. (let ((matchers (plist-get org-format-latex-options :matchers)))
  5453. (delq nil
  5454. (mapcar (lambda (x)
  5455. (and (member (car x) matchers) (nth 1 x)))
  5456. org-latex-regexps)))))
  5457. (re-entities
  5458. (when (memq 'entities org-highlight-latex-and-related)
  5459. (list "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))))
  5460. (mapconcat 'identity (append re-latex re-entities re-sub) "\\|"))))
  5461. (defun org-do-latex-and-related (limit)
  5462. "Highlight LaTeX snippets and environments, entities and sub/superscript.
  5463. LIMIT bounds the search for syntax to highlight. Stop at first
  5464. highlighted object, if any. Return t if some highlighting was
  5465. done, nil otherwise."
  5466. (when (org-string-nw-p org-latex-and-related-regexp)
  5467. (catch 'found
  5468. (while (re-search-forward org-latex-and-related-regexp limit t)
  5469. (unless (memq (car-safe (get-text-property (1+ (match-beginning 0))
  5470. 'face))
  5471. '(org-code org-verbatim underline))
  5472. (let ((offset (if (memq (char-after (1+ (match-beginning 0)))
  5473. '(?_ ?^))
  5474. 1
  5475. 0)))
  5476. (font-lock-prepend-text-property
  5477. (+ offset (match-beginning 0)) (match-end 0)
  5478. 'face 'org-latex-and-related)
  5479. (add-text-properties (+ offset (match-beginning 0)) (match-end 0)
  5480. '(font-lock-multiline t)))
  5481. (throw 'found t)))
  5482. nil)))
  5483. (defun org-restart-font-lock ()
  5484. "Restart `font-lock-mode', to force refontification."
  5485. (when (and (boundp 'font-lock-mode) font-lock-mode)
  5486. (font-lock-mode -1)
  5487. (font-lock-mode 1)))
  5488. (defun org-activate-tags (limit)
  5489. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t)
  5490. (progn
  5491. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5492. (add-text-properties (match-beginning 1) (match-end 1)
  5493. (list 'mouse-face 'highlight
  5494. 'keymap org-mouse-map))
  5495. (org-rear-nonsticky-at (match-end 1))
  5496. t)))
  5497. (defun org-outline-level ()
  5498. "Compute the outline level of the heading at point.
  5499. If this is called at a normal headline, the level is the number of stars.
  5500. Use `org-reduced-level' to remove the effect of `org-odd-levels'."
  5501. (save-excursion
  5502. (if (not (ignore-errors (org-back-to-heading t)))
  5503. 0
  5504. (looking-at org-outline-regexp)
  5505. (1- (- (match-end 0) (match-beginning 0))))))
  5506. (defvar org-font-lock-keywords nil)
  5507. (defsubst org-re-property (property &optional literal allow-null)
  5508. "Return a regexp matching a PROPERTY line.
  5509. When optional argument LITERAL is non-nil, do not quote PROPERTY.
  5510. This is useful when PROPERTY is a regexp. When ALLOW-NULL is
  5511. non-nil, match properties even without a value.
  5512. Match group 3 is set to the value when it exists. If there is no
  5513. value and ALLOW-NULL is non-nil, it is set to the empty string."
  5514. (concat
  5515. "^\\(?4:[ \t]*\\)"
  5516. (format "\\(?1::\\(?2:%s\\):\\)"
  5517. (if literal property (regexp-quote property)))
  5518. (if allow-null
  5519. "\\(?:\\(?3:$\\)\\|[ \t]+\\(?3:.*?\\)\\)\\(?5:[ \t]*\\)$"
  5520. "[ \t]+\\(?3:[^ \r\t\n]+.*?\\)\\(?5:[ \t]*\\)$")))
  5521. (defconst org-property-re
  5522. (org-re-property "\\S-+" 'literal t)
  5523. "Regular expression matching a property line.
  5524. There are four matching groups:
  5525. 1: :PROPKEY: including the leading and trailing colon,
  5526. 2: PROPKEY without the leading and trailing colon,
  5527. 3: PROPVAL without leading or trailing spaces,
  5528. 4: the indentation of the current line,
  5529. 5: trailing whitespace.")
  5530. (defvar org-font-lock-hook nil
  5531. "Functions to be called for special font lock stuff.")
  5532. (defvar org-font-lock-set-keywords-hook nil
  5533. "Functions that can manipulate `org-font-lock-extra-keywords'.
  5534. This is called after `org-font-lock-extra-keywords' is defined, but before
  5535. it is installed to be used by font lock. This can be useful if something
  5536. needs to be inserted at a specific position in the font-lock sequence.")
  5537. (defun org-font-lock-hook (limit)
  5538. "Run `org-font-lock-hook' within LIMIT."
  5539. (run-hook-with-args 'org-font-lock-hook limit))
  5540. (defun org-set-font-lock-defaults ()
  5541. "Set font lock defaults for the current buffer."
  5542. (let* ((em org-fontify-emphasized-text)
  5543. (lk org-highlight-links)
  5544. (org-font-lock-extra-keywords
  5545. (list
  5546. ;; Call the hook
  5547. '(org-font-lock-hook)
  5548. ;; Headlines
  5549. `(,(if org-fontify-whole-heading-line
  5550. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  5551. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  5552. (1 (org-get-level-face 1))
  5553. (2 (org-get-level-face 2))
  5554. (3 (org-get-level-face 3)))
  5555. ;; Table lines
  5556. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  5557. (1 'org-table t))
  5558. ;; Table internals
  5559. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  5560. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  5561. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  5562. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  5563. ;; Drawers
  5564. '(org-fontify-drawers)
  5565. ;; Properties
  5566. (list org-property-re
  5567. '(1 'org-special-keyword t)
  5568. '(3 'org-property-value t))
  5569. ;; Links
  5570. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  5571. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  5572. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  5573. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  5574. (if (memq 'radio lk) '(org-activate-target-links (1 'org-link t)))
  5575. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  5576. (if (memq 'footnote lk) '(org-activate-footnote-links))
  5577. ;; Targets.
  5578. (list org-any-target-regexp '(0 'org-target t))
  5579. ;; Diary sexps.
  5580. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  5581. ;; Macro
  5582. '(org-fontify-macros)
  5583. '(org-hide-wide-columns (0 nil append))
  5584. ;; TODO keyword
  5585. (list (format org-heading-keyword-regexp-format
  5586. org-todo-regexp)
  5587. '(2 (org-get-todo-face 2) t))
  5588. ;; DONE
  5589. (if org-fontify-done-headline
  5590. (list (format org-heading-keyword-regexp-format
  5591. (concat
  5592. "\\(?:"
  5593. (mapconcat 'regexp-quote org-done-keywords "\\|")
  5594. "\\)"))
  5595. '(2 'org-headline-done t))
  5596. nil)
  5597. ;; Priorities
  5598. '(org-font-lock-add-priority-faces)
  5599. ;; Tags
  5600. '(org-font-lock-add-tag-faces)
  5601. ;; Tags groups
  5602. (if (and org-group-tags org-tag-groups-alist)
  5603. (list (concat org-outline-regexp-bol ".+\\(:"
  5604. (regexp-opt (mapcar 'car org-tag-groups-alist))
  5605. ":\\).*$")
  5606. '(1 'org-tag-group prepend)))
  5607. ;; Special keywords
  5608. (list (concat "\\<" org-comment-string) '(0 'org-special-keyword t))
  5609. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  5610. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  5611. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  5612. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  5613. ;; Emphasis
  5614. (if em
  5615. (if (featurep 'xemacs)
  5616. '(org-do-emphasis-faces (0 nil append))
  5617. '(org-do-emphasis-faces)))
  5618. ;; Checkboxes
  5619. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  5620. 1 'org-checkbox prepend)
  5621. (if (cdr (assq 'checkbox org-list-automatic-rules))
  5622. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5623. (0 (org-get-checkbox-statistics-face) t)))
  5624. ;; Description list items
  5625. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  5626. 1 'org-list-dt prepend)
  5627. ;; ARCHIVEd headings
  5628. (list (concat
  5629. org-outline-regexp-bol
  5630. "\\(.*:" org-archive-tag ":.*\\)")
  5631. '(1 'org-archived prepend))
  5632. ;; Specials
  5633. '(org-do-latex-and-related)
  5634. '(org-fontify-entities)
  5635. '(org-raise-scripts)
  5636. ;; Code
  5637. '(org-activate-code (1 'org-code t))
  5638. ;; COMMENT
  5639. (list (format
  5640. "^\\*\\(?: +%s\\)?\\(?: +\\[#[A-Z0-9]\\]\\)? +\\(?9:%s\\)\\(?: \\|$\\)"
  5641. org-todo-regexp
  5642. org-comment-string)
  5643. '(9 'org-special-keyword t))
  5644. ;; Blocks and meta lines
  5645. '(org-fontify-meta-lines-and-blocks))))
  5646. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5647. (run-hooks 'org-font-lock-set-keywords-hook)
  5648. ;; Now set the full font-lock-keywords
  5649. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  5650. (org-set-local 'font-lock-defaults
  5651. '(org-font-lock-keywords t nil nil backward-paragraph))
  5652. (kill-local-variable 'font-lock-keywords) nil))
  5653. (defun org-toggle-pretty-entities ()
  5654. "Toggle the composition display of entities as UTF8 characters."
  5655. (interactive)
  5656. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  5657. (org-restart-font-lock)
  5658. (if org-pretty-entities
  5659. (message "Entities are now displayed as UTF8 characters")
  5660. (save-restriction
  5661. (widen)
  5662. (org-decompose-region (point-min) (point-max))
  5663. (message "Entities are now displayed as plain text"))))
  5664. (defvar org-custom-properties-overlays nil
  5665. "List of overlays used for custom properties.")
  5666. (make-variable-buffer-local 'org-custom-properties-overlays)
  5667. (defun org-toggle-custom-properties-visibility ()
  5668. "Display or hide properties in `org-custom-properties'."
  5669. (interactive)
  5670. (if org-custom-properties-overlays
  5671. (progn (mapc #'delete-overlay org-custom-properties-overlays)
  5672. (setq org-custom-properties-overlays nil))
  5673. (when org-custom-properties
  5674. (org-with-wide-buffer
  5675. (goto-char (point-min))
  5676. (let ((regexp (org-re-property (regexp-opt org-custom-properties) t t)))
  5677. (while (re-search-forward regexp nil t)
  5678. (let ((end (cdr (save-match-data (org-get-property-block)))))
  5679. (when (and end (< (point) end))
  5680. ;; Hide first custom property in current drawer.
  5681. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5682. (overlay-put o 'invisible t)
  5683. (overlay-put o 'org-custom-property t)
  5684. (push o org-custom-properties-overlays))
  5685. ;; Hide additional custom properties in the same drawer.
  5686. (while (re-search-forward regexp end t)
  5687. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5688. (overlay-put o 'invisible t)
  5689. (overlay-put o 'org-custom-property t)
  5690. (push o org-custom-properties-overlays)))))
  5691. ;; Each entry is limited to a single property drawer.
  5692. (outline-next-heading)))))))
  5693. (defun org-fontify-entities (limit)
  5694. "Find an entity to fontify."
  5695. (let (ee)
  5696. (when org-pretty-entities
  5697. (catch 'match
  5698. (while (re-search-forward
  5699. "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]\n]\\)"
  5700. limit t)
  5701. (if (and (not (org-at-comment-p))
  5702. (setq ee (org-entity-get (match-string 1)))
  5703. (= (length (nth 6 ee)) 1))
  5704. (let*
  5705. ((end (if (equal (match-string 2) "{}")
  5706. (match-end 2)
  5707. (match-end 1))))
  5708. (add-text-properties
  5709. (match-beginning 0) end
  5710. (list 'font-lock-fontified t))
  5711. (compose-region (match-beginning 0) end
  5712. (nth 6 ee) nil)
  5713. (backward-char 1)
  5714. (throw 'match t))))
  5715. nil))))
  5716. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5717. "Fontify string S like in Org-mode."
  5718. (with-temp-buffer
  5719. (insert s)
  5720. (let ((org-odd-levels-only odd-levels))
  5721. (org-mode)
  5722. (font-lock-ensure)
  5723. (buffer-string))))
  5724. (defvar org-m nil)
  5725. (defvar org-l nil)
  5726. (defvar org-f nil)
  5727. (defun org-get-level-face (n)
  5728. "Get the right face for match N in font-lock matching of headlines."
  5729. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5730. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5731. (if org-cycle-level-faces
  5732. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5733. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5734. (cond
  5735. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5736. ((eq n 2) org-f)
  5737. (t (if org-level-color-stars-only nil org-f))))
  5738. (defun org-get-todo-face (kwd)
  5739. "Get the right face for a TODO keyword KWD.
  5740. If KWD is a number, get the corresponding match group."
  5741. (if (numberp kwd) (setq kwd (match-string kwd)))
  5742. (or (org-face-from-face-or-color
  5743. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5744. (and (member kwd org-done-keywords) 'org-done)
  5745. 'org-todo))
  5746. (defun org-face-from-face-or-color (context inherit face-or-color)
  5747. "Create a face list that inherits INHERIT, but sets the foreground color.
  5748. When FACE-OR-COLOR is not a string, just return it."
  5749. (if (stringp face-or-color)
  5750. (list :inherit inherit
  5751. (cdr (assoc context org-faces-easy-properties))
  5752. face-or-color)
  5753. face-or-color))
  5754. (defun org-font-lock-add-tag-faces (limit)
  5755. "Add the special tag faces."
  5756. (when (and org-tag-faces org-tags-special-faces-re)
  5757. (while (re-search-forward org-tags-special-faces-re limit t)
  5758. (add-text-properties (match-beginning 1) (match-end 1)
  5759. (list 'face (org-get-tag-face 1)
  5760. 'font-lock-fontified t))
  5761. (backward-char 1))))
  5762. (defun org-font-lock-add-priority-faces (limit)
  5763. "Add the special priority faces."
  5764. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  5765. (when (save-match-data (org-at-heading-p))
  5766. (add-text-properties
  5767. (match-beginning 0) (match-end 0)
  5768. (list 'face (or (org-face-from-face-or-color
  5769. 'priority 'org-priority
  5770. (cdr (assoc (char-after (match-beginning 1))
  5771. org-priority-faces)))
  5772. 'org-priority)
  5773. 'font-lock-fontified t)))))
  5774. (defun org-get-tag-face (kwd)
  5775. "Get the right face for a TODO keyword KWD.
  5776. If KWD is a number, get the corresponding match group."
  5777. (if (numberp kwd) (setq kwd (match-string kwd)))
  5778. (or (org-face-from-face-or-color
  5779. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  5780. 'org-tag))
  5781. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5782. "Remove fontification and activation overlays from links."
  5783. (font-lock-default-unfontify-region beg end)
  5784. (let* ((buffer-undo-list t)
  5785. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5786. (inhibit-modification-hooks t)
  5787. deactivate-mark buffer-file-name buffer-file-truename)
  5788. (org-decompose-region beg end)
  5789. (remove-text-properties beg end
  5790. '(mouse-face t keymap t org-linked-text t
  5791. invisible t intangible t
  5792. org-emphasis t))
  5793. (org-remove-font-lock-display-properties beg end)))
  5794. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5795. ((raise 0.3) (height 0.7))
  5796. ((raise -0.5))
  5797. ((raise 0.5)))
  5798. "Display properties for showing superscripts and subscripts.")
  5799. (defun org-remove-font-lock-display-properties (beg end)
  5800. "Remove specific display properties that have been added by font lock.
  5801. The will remove the raise properties that are used to show superscripts
  5802. and subscripts."
  5803. (let (next prop)
  5804. (while (< beg end)
  5805. (setq next (next-single-property-change beg 'display nil end)
  5806. prop (get-text-property beg 'display))
  5807. (if (member prop org-script-display)
  5808. (put-text-property beg next 'display nil))
  5809. (setq beg next))))
  5810. (defun org-raise-scripts (limit)
  5811. "Add raise properties to sub/superscripts."
  5812. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts)
  5813. (if (re-search-forward
  5814. (if (eq org-use-sub-superscripts t)
  5815. org-match-substring-regexp
  5816. org-match-substring-with-braces-regexp)
  5817. limit t)
  5818. (let* ((pos (point)) table-p comment-p
  5819. (mpos (match-beginning 3))
  5820. (emph-p (get-text-property mpos 'org-emphasis))
  5821. (link-p (get-text-property mpos 'mouse-face))
  5822. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5823. (goto-char (point-at-bol))
  5824. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5825. comment-p (org-looking-at-p "^[ \t]*#[ +]"))
  5826. (goto-char pos)
  5827. ;; Handle a_b^c
  5828. (if (member (char-after) '(?_ ?^)) (goto-char (1- pos)))
  5829. (if (or comment-p emph-p link-p keyw-p)
  5830. t
  5831. (put-text-property (match-beginning 3) (match-end 0)
  5832. 'display
  5833. (if (equal (char-after (match-beginning 2)) ?^)
  5834. (nth (if table-p 3 1) org-script-display)
  5835. (nth (if table-p 2 0) org-script-display)))
  5836. (add-text-properties (match-beginning 2) (match-end 2)
  5837. (list 'invisible t
  5838. 'org-dwidth t 'org-dwidth-n 1))
  5839. (if (and (eq (char-after (match-beginning 3)) ?{)
  5840. (eq (char-before (match-end 3)) ?}))
  5841. (progn
  5842. (add-text-properties
  5843. (match-beginning 3) (1+ (match-beginning 3))
  5844. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5845. (add-text-properties
  5846. (1- (match-end 3)) (match-end 3)
  5847. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))))
  5848. t)))))
  5849. ;;;; Visibility cycling, including org-goto and indirect buffer
  5850. ;;; Cycling
  5851. (defvar org-cycle-global-status nil)
  5852. (make-variable-buffer-local 'org-cycle-global-status)
  5853. (put 'org-cycle-global-status 'org-state t)
  5854. (defvar org-cycle-subtree-status nil)
  5855. (make-variable-buffer-local 'org-cycle-subtree-status)
  5856. (put 'org-cycle-subtree-status 'org-state t)
  5857. (defvar org-inlinetask-min-level)
  5858. (defun org-unlogged-message (&rest args)
  5859. "Display a message, but avoid logging it in the *Messages* buffer."
  5860. (let ((message-log-max nil))
  5861. (apply 'message args)))
  5862. ;;;###autoload
  5863. (defun org-cycle (&optional arg)
  5864. "TAB-action and visibility cycling for Org-mode.
  5865. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5866. is outline visibility cycling, but it also invokes other actions
  5867. in special contexts.
  5868. - When this function is called with a prefix argument, rotate the entire
  5869. buffer through 3 states (global cycling)
  5870. 1. OVERVIEW: Show only top-level headlines.
  5871. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5872. 3. SHOW ALL: Show everything.
  5873. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  5874. determined by the variable `org-startup-folded', and by any VISIBILITY
  5875. properties in the buffer.
  5876. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  5877. including any drawers.
  5878. - When inside a table, re-align the table and move to the next field.
  5879. - When point is at the beginning of a headline, rotate the subtree started
  5880. by this line through 3 different states (local cycling)
  5881. 1. FOLDED: Only the main headline is shown.
  5882. 2. CHILDREN: The main headline and the direct children are shown.
  5883. From this state, you can move to one of the children
  5884. and zoom in further.
  5885. 3. SUBTREE: Show the entire subtree, including body text.
  5886. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5887. - When point is at the beginning of an empty headline and the variable
  5888. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5889. of the headline by demoting and promoting it to likely levels. This
  5890. speeds up creation document structure by pressing TAB once or several
  5891. times right after creating a new headline.
  5892. - When there is a numeric prefix, go up to a heading with level ARG, do
  5893. a `show-subtree' and return to the previous cursor position. If ARG
  5894. is negative, go up that many levels.
  5895. - When point is not at the beginning of a headline, execute the global
  5896. binding for TAB, which is re-indenting the line. See the option
  5897. `org-cycle-emulate-tab' for details.
  5898. - Special case: if point is at the beginning of the buffer and there is
  5899. no headline in line 1, this function will act as if called with prefix arg
  5900. (C-u TAB, same as S-TAB) also when called without prefix arg.
  5901. But only if also the variable `org-cycle-global-at-bob' is t."
  5902. (interactive "P")
  5903. (org-load-modules-maybe)
  5904. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5905. (and org-cycle-level-after-item/entry-creation
  5906. (or (org-cycle-level)
  5907. (org-cycle-item-indentation))))
  5908. (let* ((limit-level
  5909. (or org-cycle-max-level
  5910. (and (boundp 'org-inlinetask-min-level)
  5911. org-inlinetask-min-level
  5912. (1- org-inlinetask-min-level))))
  5913. (nstars (and limit-level
  5914. (if org-odd-levels-only
  5915. (and limit-level (1- (* limit-level 2)))
  5916. limit-level)))
  5917. (org-outline-regexp
  5918. (if (not (derived-mode-p 'org-mode))
  5919. outline-regexp
  5920. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
  5921. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5922. (not (looking-at org-outline-regexp))))
  5923. (org-cycle-hook
  5924. (if bob-special
  5925. (delq 'org-optimize-window-after-visibility-change
  5926. (copy-sequence org-cycle-hook))
  5927. org-cycle-hook))
  5928. (pos (point)))
  5929. (if (or bob-special (equal arg '(4)))
  5930. ;; special case: use global cycling
  5931. (setq arg t))
  5932. (cond
  5933. ((equal arg '(16))
  5934. (setq last-command 'dummy)
  5935. (org-set-startup-visibility)
  5936. (org-unlogged-message "Startup visibility, plus VISIBILITY properties"))
  5937. ((equal arg '(64))
  5938. (show-all)
  5939. (org-unlogged-message "Entire buffer visible, including drawers"))
  5940. ;; Try cdlatex TAB completion
  5941. ((org-try-cdlatex-tab))
  5942. ;; Table: enter it or move to the next field.
  5943. ((org-at-table-p 'any)
  5944. (if (org-at-table.el-p)
  5945. (message "Use C-c ' to edit table.el tables")
  5946. (if arg (org-table-edit-field t)
  5947. (org-table-justify-field-maybe)
  5948. (call-interactively 'org-table-next-field))))
  5949. ((run-hook-with-args-until-success
  5950. 'org-tab-after-check-for-table-hook))
  5951. ;; Global cycling: delegate to `org-cycle-internal-global'.
  5952. ((eq arg t) (org-cycle-internal-global))
  5953. ;; Drawers: delegate to `org-flag-drawer'.
  5954. ((save-excursion
  5955. (beginning-of-line 1)
  5956. (looking-at org-drawer-regexp))
  5957. (org-flag-drawer ; toggle block visibility
  5958. (not (get-char-property (match-end 0) 'invisible))))
  5959. ;; Show-subtree, ARG levels up from here.
  5960. ((integerp arg)
  5961. (save-excursion
  5962. (org-back-to-heading)
  5963. (outline-up-heading (if (< arg 0) (- arg)
  5964. (- (funcall outline-level) arg)))
  5965. (org-show-subtree)))
  5966. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  5967. ((and (featurep 'org-inlinetask)
  5968. (org-inlinetask-at-task-p)
  5969. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5970. (org-inlinetask-toggle-visibility))
  5971. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  5972. ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
  5973. (save-excursion (beginning-of-line 1)
  5974. (looking-at org-outline-regexp)))
  5975. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5976. (org-cycle-internal-local))
  5977. ;; From there: TAB emulation and template completion.
  5978. (buffer-read-only (org-back-to-heading))
  5979. ((run-hook-with-args-until-success
  5980. 'org-tab-after-check-for-cycling-hook))
  5981. ((org-try-structure-completion))
  5982. ((run-hook-with-args-until-success
  5983. 'org-tab-before-tab-emulation-hook))
  5984. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5985. (or (not (bolp))
  5986. (not (looking-at org-outline-regexp))))
  5987. (call-interactively (global-key-binding "\t")))
  5988. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5989. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5990. (or (and (eq org-cycle-emulate-tab 'white)
  5991. (= (match-end 0) (point-at-eol)))
  5992. (and (eq org-cycle-emulate-tab 'whitestart)
  5993. (>= (match-end 0) pos))))
  5994. t
  5995. (eq org-cycle-emulate-tab t))
  5996. (call-interactively (global-key-binding "\t")))
  5997. (t (save-excursion
  5998. (org-back-to-heading)
  5999. (org-cycle)))))))
  6000. (defun org-cycle-internal-global ()
  6001. "Do the global cycling action."
  6002. ;; Hack to avoid display of messages for .org attachments in Gnus
  6003. (let ((ga (string-match "\\*fontification" (buffer-name))))
  6004. (cond
  6005. ((and (eq last-command this-command)
  6006. (eq org-cycle-global-status 'overview))
  6007. ;; We just created the overview - now do table of contents
  6008. ;; This can be slow in very large buffers, so indicate action
  6009. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  6010. (unless ga (org-unlogged-message "CONTENTS..."))
  6011. (org-content)
  6012. (unless ga (org-unlogged-message "CONTENTS...done"))
  6013. (setq org-cycle-global-status 'contents)
  6014. (run-hook-with-args 'org-cycle-hook 'contents))
  6015. ((and (eq last-command this-command)
  6016. (eq org-cycle-global-status 'contents))
  6017. ;; We just showed the table of contents - now show everything
  6018. (run-hook-with-args 'org-pre-cycle-hook 'all)
  6019. (show-all)
  6020. (unless ga (org-unlogged-message "SHOW ALL"))
  6021. (setq org-cycle-global-status 'all)
  6022. (run-hook-with-args 'org-cycle-hook 'all))
  6023. (t
  6024. ;; Default action: go to overview
  6025. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  6026. (org-overview)
  6027. (unless ga (org-unlogged-message "OVERVIEW"))
  6028. (setq org-cycle-global-status 'overview)
  6029. (run-hook-with-args 'org-cycle-hook 'overview)))))
  6030. (defvar org-called-with-limited-levels nil
  6031. "Non-nil when `org-with-limited-levels' is currently active.")
  6032. (defun org-cycle-internal-local ()
  6033. "Do the local cycling action."
  6034. (let ((goal-column 0) eoh eol eos has-children children-skipped struct)
  6035. ;; First, determine end of headline (EOH), end of subtree or item
  6036. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  6037. (save-excursion
  6038. (if (org-at-item-p)
  6039. (progn
  6040. (beginning-of-line)
  6041. (setq struct (org-list-struct))
  6042. (setq eoh (point-at-eol))
  6043. (setq eos (org-list-get-item-end-before-blank (point) struct))
  6044. (setq has-children (org-list-has-child-p (point) struct)))
  6045. (org-back-to-heading)
  6046. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  6047. (setq eos (save-excursion (org-end-of-subtree t t)
  6048. (when (bolp) (backward-char)) (point)))
  6049. (setq has-children
  6050. (or (save-excursion
  6051. (let ((level (funcall outline-level)))
  6052. (outline-next-heading)
  6053. (and (org-at-heading-p t)
  6054. (> (funcall outline-level) level))))
  6055. (save-excursion
  6056. (org-list-search-forward (org-item-beginning-re) eos t)))))
  6057. ;; Determine end invisible part of buffer (EOL)
  6058. (beginning-of-line 2)
  6059. ;; XEmacs doesn't have `next-single-char-property-change'
  6060. (if (featurep 'xemacs)
  6061. (while (and (not (eobp)) ;; this is like `next-line'
  6062. (get-char-property (1- (point)) 'invisible))
  6063. (beginning-of-line 2))
  6064. (while (and (not (eobp)) ;; this is like `next-line'
  6065. (get-char-property (1- (point)) 'invisible))
  6066. (goto-char (next-single-char-property-change (point) 'invisible))
  6067. (and (eolp) (beginning-of-line 2))))
  6068. (setq eol (point)))
  6069. ;; Find out what to do next and set `this-command'
  6070. (cond
  6071. ((= eos eoh)
  6072. ;; Nothing is hidden behind this heading
  6073. (unless (org-before-first-heading-p)
  6074. (run-hook-with-args 'org-pre-cycle-hook 'empty))
  6075. (org-unlogged-message "EMPTY ENTRY")
  6076. (setq org-cycle-subtree-status nil)
  6077. (save-excursion
  6078. (goto-char eos)
  6079. (outline-next-heading)
  6080. (if (outline-invisible-p) (org-flag-heading nil))))
  6081. ((and (or (>= eol eos)
  6082. (not (string-match "\\S-" (buffer-substring eol eos))))
  6083. (or has-children
  6084. (not (setq children-skipped
  6085. org-cycle-skip-children-state-if-no-children))))
  6086. ;; Entire subtree is hidden in one line: children view
  6087. (unless (org-before-first-heading-p)
  6088. (run-hook-with-args 'org-pre-cycle-hook 'children))
  6089. (if (org-at-item-p)
  6090. (org-list-set-item-visibility (point-at-bol) struct 'children)
  6091. (org-show-entry)
  6092. (org-with-limited-levels (show-children))
  6093. ;; FIXME: This slows down the func way too much.
  6094. ;; How keep drawers hidden in subtree anyway?
  6095. ;; (when (memq 'org-cycle-hide-drawers org-cycle-hook)
  6096. ;; (org-cycle-hide-drawers 'subtree))
  6097. ;; Fold every list in subtree to top-level items.
  6098. (when (eq org-cycle-include-plain-lists 'integrate)
  6099. (save-excursion
  6100. (org-back-to-heading)
  6101. (while (org-list-search-forward (org-item-beginning-re) eos t)
  6102. (beginning-of-line 1)
  6103. (let* ((struct (org-list-struct))
  6104. (prevs (org-list-prevs-alist struct))
  6105. (end (org-list-get-bottom-point struct)))
  6106. (mapc (lambda (e) (org-list-set-item-visibility e struct 'folded))
  6107. (org-list-get-all-items (point) struct prevs))
  6108. (goto-char (if (< end eos) end eos)))))))
  6109. (org-unlogged-message "CHILDREN")
  6110. (save-excursion
  6111. (goto-char eos)
  6112. (outline-next-heading)
  6113. (if (outline-invisible-p) (org-flag-heading nil)))
  6114. (setq org-cycle-subtree-status 'children)
  6115. (unless (org-before-first-heading-p)
  6116. (run-hook-with-args 'org-cycle-hook 'children)))
  6117. ((or children-skipped
  6118. (and (eq last-command this-command)
  6119. (eq org-cycle-subtree-status 'children)))
  6120. ;; We just showed the children, or no children are there,
  6121. ;; now show everything.
  6122. (unless (org-before-first-heading-p)
  6123. (run-hook-with-args 'org-pre-cycle-hook 'subtree))
  6124. (outline-flag-region eoh eos nil)
  6125. (org-unlogged-message
  6126. (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  6127. (setq org-cycle-subtree-status 'subtree)
  6128. (unless (org-before-first-heading-p)
  6129. (run-hook-with-args 'org-cycle-hook 'subtree)))
  6130. (t
  6131. ;; Default action: hide the subtree.
  6132. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  6133. (outline-flag-region eoh eos t)
  6134. (org-unlogged-message "FOLDED")
  6135. (setq org-cycle-subtree-status 'folded)
  6136. (unless (org-before-first-heading-p)
  6137. (run-hook-with-args 'org-cycle-hook 'folded))))))
  6138. ;;;###autoload
  6139. (defun org-global-cycle (&optional arg)
  6140. "Cycle the global visibility. For details see `org-cycle'.
  6141. With \\[universal-argument] prefix arg, switch to startup visibility.
  6142. With a numeric prefix, show all headlines up to that level."
  6143. (interactive "P")
  6144. (let ((org-cycle-include-plain-lists
  6145. (if (derived-mode-p 'org-mode) org-cycle-include-plain-lists nil)))
  6146. (cond
  6147. ((integerp arg)
  6148. (show-all)
  6149. (hide-sublevels arg)
  6150. (setq org-cycle-global-status 'contents))
  6151. ((equal arg '(4))
  6152. (org-set-startup-visibility)
  6153. (org-unlogged-message "Startup visibility, plus VISIBILITY properties."))
  6154. (t
  6155. (org-cycle '(4))))))
  6156. (defun org-set-startup-visibility ()
  6157. "Set the visibility required by startup options and properties."
  6158. (cond
  6159. ((eq org-startup-folded t)
  6160. (org-overview))
  6161. ((eq org-startup-folded 'content)
  6162. (org-content))
  6163. ((or (eq org-startup-folded 'showeverything)
  6164. (eq org-startup-folded nil))
  6165. (show-all)))
  6166. (unless (eq org-startup-folded 'showeverything)
  6167. (if org-hide-block-startup (org-hide-block-all))
  6168. (org-set-visibility-according-to-property 'no-cleanup)
  6169. (org-cycle-hide-archived-subtrees 'all)
  6170. (org-cycle-hide-drawers 'all)
  6171. (org-cycle-show-empty-lines t)))
  6172. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  6173. "Switch subtree visibilities according to :VISIBILITY: property."
  6174. (interactive)
  6175. (let (org-show-entry-below state)
  6176. (save-excursion
  6177. (goto-char (point-min))
  6178. (while (re-search-forward
  6179. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  6180. nil t)
  6181. (setq state (match-string 1))
  6182. (save-excursion
  6183. (org-back-to-heading t)
  6184. (hide-subtree)
  6185. (org-reveal)
  6186. (cond
  6187. ((equal state '("fold" "folded"))
  6188. (hide-subtree))
  6189. ((equal state "children")
  6190. (org-show-hidden-entry)
  6191. (show-children))
  6192. ((equal state "content")
  6193. (save-excursion
  6194. (save-restriction
  6195. (org-narrow-to-subtree)
  6196. (org-content))))
  6197. ((member state '("all" "showall"))
  6198. (show-subtree)))))
  6199. (unless no-cleanup
  6200. (org-cycle-hide-archived-subtrees 'all)
  6201. (org-cycle-hide-drawers 'all)
  6202. (org-cycle-show-empty-lines 'all)))))
  6203. ;; This function uses outline-regexp instead of the more fundamental
  6204. ;; org-outline-regexp so that org-cycle-global works outside of Org
  6205. ;; buffers, where outline-regexp is needed.
  6206. (defun org-overview ()
  6207. "Switch to overview mode, showing only top-level headlines.
  6208. This shows all headlines with a level equal or greater than the level
  6209. of the first headline in the buffer. This is important, because if the
  6210. first headline is not level one, then (hide-sublevels 1) gives confusing
  6211. results."
  6212. (interactive)
  6213. (save-excursion
  6214. (let ((level
  6215. (save-excursion
  6216. (goto-char (point-min))
  6217. (if (re-search-forward (concat "^" outline-regexp) nil t)
  6218. (progn
  6219. (goto-char (match-beginning 0))
  6220. (funcall outline-level))))))
  6221. (and level (hide-sublevels level)))))
  6222. (defun org-content (&optional arg)
  6223. "Show all headlines in the buffer, like a table of contents.
  6224. With numerical argument N, show content up to level N."
  6225. (interactive "P")
  6226. (org-overview)
  6227. (save-excursion
  6228. ;; Visit all headings and show their offspring
  6229. (and (integerp arg) (org-overview))
  6230. (goto-char (point-max))
  6231. (catch 'exit
  6232. (while (and (progn (condition-case nil
  6233. (outline-previous-visible-heading 1)
  6234. (error (goto-char (point-min))))
  6235. t)
  6236. (looking-at org-outline-regexp))
  6237. (if (integerp arg)
  6238. (show-children (1- arg))
  6239. (show-branches))
  6240. (if (bobp) (throw 'exit nil))))))
  6241. (defun org-optimize-window-after-visibility-change (state)
  6242. "Adjust the window after a change in outline visibility.
  6243. This function is the default value of the hook `org-cycle-hook'."
  6244. (when (get-buffer-window (current-buffer))
  6245. (cond
  6246. ((eq state 'content) nil)
  6247. ((eq state 'all) nil)
  6248. ((eq state 'folded) nil)
  6249. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  6250. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  6251. (defun org-remove-empty-overlays-at (pos)
  6252. "Remove outline overlays that do not contain non-white stuff."
  6253. (mapc
  6254. (lambda (o)
  6255. (and (eq 'outline (overlay-get o 'invisible))
  6256. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  6257. (overlay-end o))))
  6258. (delete-overlay o)))
  6259. (overlays-at pos)))
  6260. (defun org-clean-visibility-after-subtree-move ()
  6261. "Fix visibility issues after moving a subtree."
  6262. ;; First, find a reasonable region to look at:
  6263. ;; Start two siblings above, end three below
  6264. (let* ((beg (save-excursion
  6265. (and (org-get-last-sibling)
  6266. (org-get-last-sibling))
  6267. (point)))
  6268. (end (save-excursion
  6269. (and (org-get-next-sibling)
  6270. (org-get-next-sibling)
  6271. (org-get-next-sibling))
  6272. (if (org-at-heading-p)
  6273. (point-at-eol)
  6274. (point))))
  6275. (level (looking-at "\\*+"))
  6276. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  6277. (save-excursion
  6278. (save-restriction
  6279. (narrow-to-region beg end)
  6280. (when re
  6281. ;; Properly fold already folded siblings
  6282. (goto-char (point-min))
  6283. (while (re-search-forward re nil t)
  6284. (if (and (not (outline-invisible-p))
  6285. (save-excursion
  6286. (goto-char (point-at-eol)) (outline-invisible-p)))
  6287. (hide-entry))))
  6288. (org-cycle-show-empty-lines 'overview)
  6289. (org-cycle-hide-drawers 'overview)))))
  6290. (defun org-cycle-show-empty-lines (state)
  6291. "Show empty lines above all visible headlines.
  6292. The region to be covered depends on STATE when called through
  6293. `org-cycle-hook'. Lisp program can use t for STATE to get the
  6294. entire buffer covered. Note that an empty line is only shown if there
  6295. are at least `org-cycle-separator-lines' empty lines before the headline."
  6296. (when (not (= org-cycle-separator-lines 0))
  6297. (save-excursion
  6298. (let* ((n (abs org-cycle-separator-lines))
  6299. (re (cond
  6300. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  6301. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  6302. (t (let ((ns (number-to-string (- n 2))))
  6303. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  6304. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  6305. beg end b e)
  6306. (cond
  6307. ((memq state '(overview contents t))
  6308. (setq beg (point-min) end (point-max)))
  6309. ((memq state '(children folded))
  6310. (setq beg (point) end (progn (org-end-of-subtree t t)
  6311. (beginning-of-line 2)
  6312. (point)))))
  6313. (when beg
  6314. (goto-char beg)
  6315. (while (re-search-forward re end t)
  6316. (unless (get-char-property (match-end 1) 'invisible)
  6317. (setq e (match-end 1))
  6318. (if (< org-cycle-separator-lines 0)
  6319. (setq b (save-excursion
  6320. (goto-char (match-beginning 0))
  6321. (org-back-over-empty-lines)
  6322. (if (save-excursion
  6323. (goto-char (max (point-min) (1- (point))))
  6324. (org-at-heading-p))
  6325. (1- (point))
  6326. (point))))
  6327. (setq b (match-beginning 1)))
  6328. (outline-flag-region b e nil)))))))
  6329. ;; Never hide empty lines at the end of the file.
  6330. (save-excursion
  6331. (goto-char (point-max))
  6332. (outline-previous-heading)
  6333. (outline-end-of-heading)
  6334. (if (and (looking-at "[ \t\n]+")
  6335. (= (match-end 0) (point-max)))
  6336. (outline-flag-region (point) (match-end 0) nil))))
  6337. (defun org-show-empty-lines-in-parent ()
  6338. "Move to the parent and re-show empty lines before visible headlines."
  6339. (save-excursion
  6340. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  6341. (org-cycle-show-empty-lines context))))
  6342. (defun org-files-list ()
  6343. "Return `org-agenda-files' list, plus all open org-mode files.
  6344. This is useful for operations that need to scan all of a user's
  6345. open and agenda-wise Org files."
  6346. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  6347. (dolist (buf (buffer-list))
  6348. (with-current-buffer buf
  6349. (if (and (derived-mode-p 'org-mode) (buffer-file-name))
  6350. (let ((file (expand-file-name (buffer-file-name))))
  6351. (unless (member file files)
  6352. (push file files))))))
  6353. files))
  6354. (defsubst org-entry-beginning-position ()
  6355. "Return the beginning position of the current entry."
  6356. (save-excursion (outline-back-to-heading t) (point)))
  6357. (defsubst org-entry-end-position ()
  6358. "Return the end position of the current entry."
  6359. (save-excursion (outline-next-heading) (point)))
  6360. (defun org-cycle-hide-drawers (state &optional exceptions)
  6361. "Re-hide all drawers after a visibility state change.
  6362. When non-nil, optional argument EXCEPTIONS is a list of strings
  6363. specifying which drawers should not be hidden."
  6364. (when (and (derived-mode-p 'org-mode)
  6365. (not (memq state '(overview folded contents))))
  6366. (save-excursion
  6367. (let* ((globalp (memq state '(contents all)))
  6368. (beg (if globalp (point-min) (point)))
  6369. (end (if globalp (point-max)
  6370. (if (eq state 'children)
  6371. (save-excursion (outline-next-heading) (point))
  6372. (org-end-of-subtree t)))))
  6373. (goto-char beg)
  6374. (while (re-search-forward org-drawer-regexp (max end (point)) t)
  6375. (unless (member-ignore-case (match-string 1) exceptions)
  6376. (let ((drawer (org-element-at-point)))
  6377. (when (memq (org-element-type drawer) '(drawer property-drawer))
  6378. (org-flag-drawer t drawer)
  6379. ;; Make sure to skip drawer entirely or we might flag
  6380. ;; it another time when matching its ending line with
  6381. ;; `org-drawer-regexp'.
  6382. (goto-char (org-element-property :end drawer))))))))))
  6383. (defun org-cycle-hide-inline-tasks (state)
  6384. "Re-hide inline tasks when switching to 'contents or 'children
  6385. visibility state."
  6386. (case state
  6387. (contents
  6388. (when (org-bound-and-true-p org-inlinetask-min-level)
  6389. (hide-sublevels (1- org-inlinetask-min-level))))
  6390. (children
  6391. (when (featurep 'org-inlinetask)
  6392. (save-excursion
  6393. (while (and (outline-next-heading)
  6394. (org-inlinetask-at-task-p))
  6395. (org-inlinetask-toggle-visibility)
  6396. (org-inlinetask-goto-end)))))))
  6397. (defun org-flag-drawer (flag &optional element)
  6398. "When FLAG is non-nil, hide the drawer we are at.
  6399. Otherwise make it visible. When optional argument ELEMENT is
  6400. a parsed drawer, as returned by `org-element-at-point', hide or
  6401. show that drawer instead."
  6402. (when (save-excursion
  6403. (beginning-of-line)
  6404. (org-looking-at-p org-drawer-regexp))
  6405. (let ((drawer (or element (org-element-at-point))))
  6406. (when (memq (org-element-type drawer) '(drawer property-drawer))
  6407. (let ((post (org-element-property :post-affiliated drawer)))
  6408. (save-excursion
  6409. (outline-flag-region
  6410. (progn (goto-char post) (line-end-position))
  6411. (progn (goto-char (org-element-property :end drawer))
  6412. (skip-chars-backward " \r\t\n")
  6413. (line-end-position))
  6414. flag))
  6415. ;; When the drawer is hidden away, make sure point lies in
  6416. ;; a visible part of the buffer.
  6417. (when (and flag (> (line-beginning-position) post))
  6418. (goto-char post)))))))
  6419. (defun org-subtree-end-visible-p ()
  6420. "Is the end of the current subtree visible?"
  6421. (pos-visible-in-window-p
  6422. (save-excursion (org-end-of-subtree t) (point))))
  6423. (defun org-first-headline-recenter ()
  6424. "Move cursor to the first headline and recenter the headline."
  6425. (goto-char (point-min))
  6426. (when (re-search-forward (concat "^\\(" org-outline-regexp "\\)") nil t)
  6427. (set-window-start (selected-window) (point-at-bol))))
  6428. ;;; Saving and restoring visibility
  6429. (defun org-outline-overlay-data (&optional use-markers)
  6430. "Return a list of the locations of all outline overlays.
  6431. These are overlays with the `invisible' property value `outline'.
  6432. The return value is a list of cons cells, with start and stop
  6433. positions for each overlay.
  6434. If USE-MARKERS is set, return the positions as markers."
  6435. (let (beg end)
  6436. (save-excursion
  6437. (save-restriction
  6438. (widen)
  6439. (delq nil
  6440. (mapcar (lambda (o)
  6441. (when (eq (overlay-get o 'invisible) 'outline)
  6442. (setq beg (overlay-start o)
  6443. end (overlay-end o))
  6444. (and beg end (> end beg)
  6445. (if use-markers
  6446. (cons (copy-marker beg)
  6447. (copy-marker end t))
  6448. (cons beg end)))))
  6449. (overlays-in (point-min) (point-max))))))))
  6450. (defun org-set-outline-overlay-data (data)
  6451. "Create visibility overlays for all positions in DATA.
  6452. DATA should have been made by `org-outline-overlay-data'."
  6453. (let (o)
  6454. (save-excursion
  6455. (save-restriction
  6456. (widen)
  6457. (show-all)
  6458. (mapc (lambda (c)
  6459. (outline-flag-region (car c) (cdr c) t))
  6460. data)))))
  6461. ;;; Folding of blocks
  6462. (defvar org-hide-block-overlays nil
  6463. "Overlays hiding blocks.")
  6464. (make-variable-buffer-local 'org-hide-block-overlays)
  6465. (defun org-block-map (function &optional start end)
  6466. "Call FUNCTION at the head of all source blocks in the current buffer.
  6467. Optional arguments START and END can be used to limit the range."
  6468. (let ((start (or start (point-min)))
  6469. (end (or end (point-max))))
  6470. (save-excursion
  6471. (goto-char start)
  6472. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  6473. (save-excursion
  6474. (save-match-data
  6475. (goto-char (match-beginning 0))
  6476. (funcall function)))))))
  6477. (defun org-hide-block-toggle-all ()
  6478. "Toggle the visibility of all blocks in the current buffer."
  6479. (org-block-map 'org-hide-block-toggle))
  6480. (defun org-hide-block-all ()
  6481. "Fold all blocks in the current buffer."
  6482. (interactive)
  6483. (org-show-block-all)
  6484. (org-block-map 'org-hide-block-toggle-maybe))
  6485. (defun org-show-block-all ()
  6486. "Unfold all blocks in the current buffer."
  6487. (interactive)
  6488. (mapc 'delete-overlay org-hide-block-overlays)
  6489. (setq org-hide-block-overlays nil))
  6490. (defun org-hide-block-toggle-maybe ()
  6491. "Toggle visibility of block at point.
  6492. Unlike to `org-hide-block-toggle', this function does not throw
  6493. an error. Return a non-nil value when toggling is successful."
  6494. (interactive)
  6495. (ignore-errors (org-hide-block-toggle)))
  6496. (defun org-hide-block-toggle (&optional force)
  6497. "Toggle the visibility of the current block.
  6498. When optional argument FORCE is `off', make block visible. If it
  6499. is non-nil, hide it unconditionally. Throw an error when not at
  6500. a block. Return a non-nil value when toggling is successful."
  6501. (interactive)
  6502. (let ((element (org-element-at-point)))
  6503. (unless (memq (org-element-type element)
  6504. '(center-block comment-block dynamic-block example-block
  6505. export-block quote-block special-block
  6506. src-block verse-block))
  6507. (user-error "Not at a block"))
  6508. (let* ((start (save-excursion
  6509. (goto-char (org-element-property :post-affiliated element))
  6510. (line-end-position)))
  6511. (end (save-excursion
  6512. (goto-char (org-element-property :end element))
  6513. (skip-chars-backward " \r\t\n")
  6514. (line-end-position)))
  6515. (overlays (overlays-at start)))
  6516. (cond
  6517. ;; Do nothing when not before or at the block opening line or
  6518. ;; at the block closing line.
  6519. ((let ((eol (line-end-position))) (and (> eol start) (/= eol end))) nil)
  6520. ((and (not (eq force 'off))
  6521. (not (memq t (mapcar
  6522. (lambda (o)
  6523. (eq (overlay-get o 'invisible) 'org-hide-block))
  6524. overlays))))
  6525. (let ((ov (make-overlay start end)))
  6526. (overlay-put ov 'invisible 'org-hide-block)
  6527. ;; Make the block accessible to `isearch'.
  6528. (overlay-put
  6529. ov 'isearch-open-invisible
  6530. (lambda (ov)
  6531. (when (memq ov org-hide-block-overlays)
  6532. (setq org-hide-block-overlays (delq ov org-hide-block-overlays)))
  6533. (when (eq (overlay-get ov 'invisible) 'org-hide-block)
  6534. (delete-overlay ov))))
  6535. (push ov org-hide-block-overlays)
  6536. ;; When the block is hidden away, make sure point is left in
  6537. ;; a visible part of the buffer.
  6538. (when (> (line-beginning-position) start)
  6539. (goto-char start)
  6540. (beginning-of-line))
  6541. ;; Signal successful toggling.
  6542. t))
  6543. ((or (not force) (eq force 'off))
  6544. (dolist (ov overlays t)
  6545. (when (memq ov org-hide-block-overlays)
  6546. (setq org-hide-block-overlays (delq ov org-hide-block-overlays)))
  6547. (when (eq (overlay-get ov 'invisible) 'org-hide-block)
  6548. (delete-overlay ov))))))))
  6549. ;; org-tab-after-check-for-cycling-hook
  6550. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  6551. ;; Remove overlays when changing major mode
  6552. (add-hook 'org-mode-hook
  6553. (lambda () (org-add-hook 'change-major-mode-hook
  6554. 'org-show-block-all 'append 'local)))
  6555. ;;; Org-goto
  6556. (defvar org-goto-window-configuration nil)
  6557. (defvar org-goto-marker nil)
  6558. (defvar org-goto-map)
  6559. (defun org-goto-map ()
  6560. "Set the keymap `org-goto'."
  6561. (setq org-goto-map
  6562. (let ((map (make-sparse-keymap)))
  6563. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command
  6564. mouse-drag-region universal-argument org-occur))
  6565. cmd)
  6566. (while (setq cmd (pop cmds))
  6567. (substitute-key-definition cmd cmd map global-map)))
  6568. (suppress-keymap map)
  6569. (org-defkey map "\C-m" 'org-goto-ret)
  6570. (org-defkey map [(return)] 'org-goto-ret)
  6571. (org-defkey map [(left)] 'org-goto-left)
  6572. (org-defkey map [(right)] 'org-goto-right)
  6573. (org-defkey map [(control ?g)] 'org-goto-quit)
  6574. (org-defkey map "\C-i" 'org-cycle)
  6575. (org-defkey map [(tab)] 'org-cycle)
  6576. (org-defkey map [(down)] 'outline-next-visible-heading)
  6577. (org-defkey map [(up)] 'outline-previous-visible-heading)
  6578. (if org-goto-auto-isearch
  6579. (if (fboundp 'define-key-after)
  6580. (define-key-after map [t] 'org-goto-local-auto-isearch)
  6581. nil)
  6582. (org-defkey map "q" 'org-goto-quit)
  6583. (org-defkey map "n" 'outline-next-visible-heading)
  6584. (org-defkey map "p" 'outline-previous-visible-heading)
  6585. (org-defkey map "f" 'outline-forward-same-level)
  6586. (org-defkey map "b" 'outline-backward-same-level)
  6587. (org-defkey map "u" 'outline-up-heading))
  6588. (org-defkey map "/" 'org-occur)
  6589. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  6590. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  6591. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  6592. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  6593. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  6594. map)))
  6595. (defconst org-goto-help
  6596. "Browse buffer copy, to find location or copy text.%s
  6597. RET=jump to location C-g=quit and return to previous location
  6598. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  6599. (defvar org-goto-start-pos) ; dynamically scoped parameter
  6600. (defun org-goto (&optional alternative-interface)
  6601. "Look up a different location in the current file, keeping current visibility.
  6602. When you want look-up or go to a different location in a
  6603. document, the fastest way is often to fold the entire buffer and
  6604. then dive into the tree. This method has the disadvantage, that
  6605. the previous location will be folded, which may not be what you
  6606. want.
  6607. This command works around this by showing a copy of the current
  6608. buffer in an indirect buffer, in overview mode. You can dive
  6609. into the tree in that copy, use org-occur and incremental search
  6610. to find a location. When pressing RET or `Q', the command
  6611. returns to the original buffer in which the visibility is still
  6612. unchanged. After RET it will also jump to the location selected
  6613. in the indirect buffer and expose the headline hierarchy above.
  6614. With a prefix argument, use the alternative interface: e.g. if
  6615. `org-goto-interface' is 'outline use 'outline-path-completion."
  6616. (interactive "P")
  6617. (org-goto-map)
  6618. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  6619. (org-refile-use-outline-path t)
  6620. (org-refile-target-verify-function nil)
  6621. (interface
  6622. (if (not alternative-interface)
  6623. org-goto-interface
  6624. (if (eq org-goto-interface 'outline)
  6625. 'outline-path-completion
  6626. 'outline)))
  6627. (org-goto-start-pos (point))
  6628. (selected-point
  6629. (if (eq interface 'outline)
  6630. (car (org-get-location (current-buffer) org-goto-help))
  6631. (let ((pa (org-refile-get-location "Goto" nil nil t)))
  6632. (org-refile-check-position pa)
  6633. (nth 3 pa)))))
  6634. (if selected-point
  6635. (progn
  6636. (org-mark-ring-push org-goto-start-pos)
  6637. (goto-char selected-point)
  6638. (if (or (outline-invisible-p) (org-invisible-p2))
  6639. (org-show-context 'org-goto)))
  6640. (message "Quit"))))
  6641. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  6642. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  6643. (defvar org-goto-local-auto-isearch-map) ; defined below
  6644. (defun org-get-location (buf help)
  6645. "Let the user select a location in the Org-mode buffer BUF.
  6646. This function uses a recursive edit. It returns the selected position
  6647. or nil."
  6648. (org-no-popups
  6649. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  6650. (isearch-hide-immediately nil)
  6651. (isearch-search-fun-function
  6652. (lambda () 'org-goto-local-search-headings))
  6653. (org-goto-selected-point org-goto-exit-command))
  6654. (save-excursion
  6655. (save-window-excursion
  6656. (delete-other-windows)
  6657. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  6658. (org-pop-to-buffer-same-window
  6659. (condition-case nil
  6660. (make-indirect-buffer (current-buffer) "*org-goto*")
  6661. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  6662. (with-output-to-temp-buffer "*Org Help*"
  6663. (princ (format help (if org-goto-auto-isearch
  6664. " Just type for auto-isearch."
  6665. " n/p/f/b/u to navigate, q to quit."))))
  6666. (org-fit-window-to-buffer (get-buffer-window "*Org Help*"))
  6667. (setq buffer-read-only nil)
  6668. (let ((org-startup-truncated t)
  6669. (org-startup-folded nil)
  6670. (org-startup-align-all-tables nil))
  6671. (org-mode)
  6672. (org-overview))
  6673. (setq buffer-read-only t)
  6674. (if (and (boundp 'org-goto-start-pos)
  6675. (integer-or-marker-p org-goto-start-pos))
  6676. (let ((org-show-hierarchy-above t)
  6677. (org-show-siblings t)
  6678. (org-show-following-heading t))
  6679. (goto-char org-goto-start-pos)
  6680. (and (outline-invisible-p) (org-show-context)))
  6681. (goto-char (point-min)))
  6682. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  6683. (message "Select location and press RET")
  6684. (use-local-map org-goto-map)
  6685. (recursive-edit)))
  6686. (kill-buffer "*org-goto*")
  6687. (cons org-goto-selected-point org-goto-exit-command))))
  6688. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  6689. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  6690. ;; `isearch-other-control-char' was removed in Emacs 24.4.
  6691. (if (fboundp 'isearch-other-control-char)
  6692. (progn
  6693. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  6694. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char))
  6695. (define-key org-goto-local-auto-isearch-map "\C-i" nil)
  6696. (define-key org-goto-local-auto-isearch-map "\C-m" nil)
  6697. (define-key org-goto-local-auto-isearch-map [return] nil))
  6698. (defun org-goto-local-search-headings (string bound noerror)
  6699. "Search and make sure that any matches are in headlines."
  6700. (catch 'return
  6701. (while (if isearch-forward
  6702. (search-forward string bound noerror)
  6703. (search-backward string bound noerror))
  6704. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  6705. (and (member :headline context)
  6706. (not (member :tags context))))
  6707. (throw 'return (point))))))
  6708. (defun org-goto-local-auto-isearch ()
  6709. "Start isearch."
  6710. (interactive)
  6711. (goto-char (point-min))
  6712. (let ((keys (this-command-keys)))
  6713. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  6714. (isearch-mode t)
  6715. (isearch-process-search-char (string-to-char keys)))))
  6716. (defun org-goto-ret (&optional arg)
  6717. "Finish `org-goto' by going to the new location."
  6718. (interactive "P")
  6719. (setq org-goto-selected-point (point)
  6720. org-goto-exit-command 'return)
  6721. (throw 'exit nil))
  6722. (defun org-goto-left ()
  6723. "Finish `org-goto' by going to the new location."
  6724. (interactive)
  6725. (if (org-at-heading-p)
  6726. (progn
  6727. (beginning-of-line 1)
  6728. (setq org-goto-selected-point (point)
  6729. org-goto-exit-command 'left)
  6730. (throw 'exit nil))
  6731. (user-error "Not on a heading")))
  6732. (defun org-goto-right ()
  6733. "Finish `org-goto' by going to the new location."
  6734. (interactive)
  6735. (if (org-at-heading-p)
  6736. (progn
  6737. (setq org-goto-selected-point (point)
  6738. org-goto-exit-command 'right)
  6739. (throw 'exit nil))
  6740. (user-error "Not on a heading")))
  6741. (defun org-goto-quit ()
  6742. "Finish `org-goto' without cursor motion."
  6743. (interactive)
  6744. (setq org-goto-selected-point nil)
  6745. (setq org-goto-exit-command 'quit)
  6746. (throw 'exit nil))
  6747. ;;; Indirect buffer display of subtrees
  6748. (defvar org-indirect-dedicated-frame nil
  6749. "This is the frame being used for indirect tree display.")
  6750. (defvar org-last-indirect-buffer nil)
  6751. (defun org-tree-to-indirect-buffer (&optional arg)
  6752. "Create indirect buffer and narrow it to current subtree.
  6753. With a numerical prefix ARG, go up to this level and then take that tree.
  6754. If ARG is negative, go up that many levels.
  6755. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  6756. indirect buffer previously made with this command, to avoid proliferation of
  6757. indirect buffers. However, when you call the command with a \
  6758. \\[universal-argument] prefix, or
  6759. when `org-indirect-buffer-display' is `new-frame', the last buffer
  6760. is kept so that you can work with several indirect buffers at the same time.
  6761. If `org-indirect-buffer-display' is `dedicated-frame', the \
  6762. \\[universal-argument] prefix also
  6763. requests that a new frame be made for the new buffer, so that the dedicated
  6764. frame is not changed."
  6765. (interactive "P")
  6766. (let ((cbuf (current-buffer))
  6767. (cwin (selected-window))
  6768. (pos (point))
  6769. beg end level heading ibuf)
  6770. (save-excursion
  6771. (org-back-to-heading t)
  6772. (when (numberp arg)
  6773. (setq level (org-outline-level))
  6774. (if (< arg 0) (setq arg (+ level arg)))
  6775. (while (> (setq level (org-outline-level)) arg)
  6776. (org-up-heading-safe)))
  6777. (setq beg (point)
  6778. heading (org-get-heading))
  6779. (org-end-of-subtree t t)
  6780. (if (org-at-heading-p) (backward-char 1))
  6781. (setq end (point)))
  6782. (if (and (buffer-live-p org-last-indirect-buffer)
  6783. (not (eq org-indirect-buffer-display 'new-frame))
  6784. (not arg))
  6785. (kill-buffer org-last-indirect-buffer))
  6786. (setq ibuf (org-get-indirect-buffer cbuf heading)
  6787. org-last-indirect-buffer ibuf)
  6788. (cond
  6789. ((or (eq org-indirect-buffer-display 'new-frame)
  6790. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  6791. (select-frame (make-frame))
  6792. (delete-other-windows)
  6793. (org-pop-to-buffer-same-window ibuf)
  6794. (org-set-frame-title heading))
  6795. ((eq org-indirect-buffer-display 'dedicated-frame)
  6796. (raise-frame
  6797. (select-frame (or (and org-indirect-dedicated-frame
  6798. (frame-live-p org-indirect-dedicated-frame)
  6799. org-indirect-dedicated-frame)
  6800. (setq org-indirect-dedicated-frame (make-frame)))))
  6801. (delete-other-windows)
  6802. (org-pop-to-buffer-same-window ibuf)
  6803. (org-set-frame-title (concat "Indirect: " heading)))
  6804. ((eq org-indirect-buffer-display 'current-window)
  6805. (org-pop-to-buffer-same-window ibuf))
  6806. ((eq org-indirect-buffer-display 'other-window)
  6807. (pop-to-buffer ibuf))
  6808. (t (error "Invalid value")))
  6809. (if (featurep 'xemacs)
  6810. (save-excursion (org-mode) (turn-on-font-lock)))
  6811. (narrow-to-region beg end)
  6812. (show-all)
  6813. (goto-char pos)
  6814. (run-hook-with-args 'org-cycle-hook 'all)
  6815. (and (window-live-p cwin) (select-window cwin))))
  6816. (defun org-get-indirect-buffer (&optional buffer heading)
  6817. (setq buffer (or buffer (current-buffer)))
  6818. (let ((n 1) (base (buffer-name buffer)) bname)
  6819. (while (buffer-live-p
  6820. (get-buffer
  6821. (setq bname
  6822. (concat base "-"
  6823. (if heading (concat heading "-" (number-to-string n))
  6824. (number-to-string n))))))
  6825. (setq n (1+ n)))
  6826. (condition-case nil
  6827. (make-indirect-buffer buffer bname 'clone)
  6828. (error (make-indirect-buffer buffer bname)))))
  6829. (defun org-set-frame-title (title)
  6830. "Set the title of the current frame to the string TITLE."
  6831. ;; FIXME: how to name a single frame in XEmacs???
  6832. (unless (featurep 'xemacs)
  6833. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  6834. ;;;; Structure editing
  6835. ;;; Inserting headlines
  6836. (defun org-previous-line-empty-p (&optional next)
  6837. "Is the previous line a blank line?
  6838. When NEXT is non-nil, check the next line instead."
  6839. (save-excursion
  6840. (and (not (bobp))
  6841. (or (beginning-of-line (if next 2 0)) t)
  6842. (save-match-data
  6843. (looking-at "[ \t]*$")))))
  6844. (defun org-insert-heading (&optional arg invisible-ok)
  6845. "Insert a new heading or an item with the same depth at point.
  6846. If point is at the beginning of a heading or a list item, insert
  6847. a new heading or a new item above the current one. If point is
  6848. at the beginning of a normal line, turn the line into a heading.
  6849. If point is in the middle of a headline or a list item, split the
  6850. headline or the item and create a new headline/item with the text
  6851. in the current line after point \(see `org-M-RET-may-split-line'
  6852. on how to modify this behavior).
  6853. With one universal prefix argument, set the user option
  6854. `org-insert-heading-respect-content' to t for the duration of
  6855. the command. This modifies the behavior described above in this
  6856. ways: on list items and at the beginning of normal lines, force
  6857. the insertion of a heading after the current subtree.
  6858. With two universal prefix arguments, insert the heading at the
  6859. end of the grandparent subtree. For example, if point is within
  6860. a 2nd-level heading, then it will insert a 2nd-level heading at
  6861. the end of the 1st-level parent heading.
  6862. If point is at the beginning of a headline, insert a sibling
  6863. before the current headline. If point is not at the beginning,
  6864. split the line and create a new headline with the text in the
  6865. current line after point \(see `org-M-RET-may-split-line' on how
  6866. to modify this behavior).
  6867. If point is at the beginning of a normal line, turn this line
  6868. into a heading.
  6869. When INVISIBLE-OK is set, stop at invisible headlines when going
  6870. back. This is important for non-interactive uses of the
  6871. command."
  6872. (interactive "P")
  6873. (if (org-called-interactively-p 'any) (org-reveal))
  6874. (let ((itemp (org-in-item-p))
  6875. (may-split (org-get-alist-option org-M-RET-may-split-line 'headline))
  6876. (respect-content (or org-insert-heading-respect-content
  6877. (equal arg '(4))))
  6878. (initial-content "")
  6879. (adjust-empty-lines t))
  6880. (cond
  6881. ((or (= (buffer-size) 0)
  6882. (and (not (save-excursion
  6883. (and (ignore-errors (org-back-to-heading invisible-ok))
  6884. (org-at-heading-p))))
  6885. (or arg (not itemp))))
  6886. ;; At beginning of buffer or so high up that only a heading
  6887. ;; makes sense.
  6888. (cond ((and (bolp) (not respect-content)) (insert "* "))
  6889. ((not respect-content)
  6890. (unless may-split (end-of-line))
  6891. (insert "\n* "))
  6892. ((re-search-forward org-outline-regexp-bol nil t)
  6893. (beginning-of-line)
  6894. (insert "* \n")
  6895. (backward-char))
  6896. (t (goto-char (point-max))
  6897. (insert "\n* ")))
  6898. (run-hooks 'org-insert-heading-hook))
  6899. ((and itemp (not (member arg '((4) (16)))) (org-insert-item)))
  6900. (t
  6901. ;; Maybe move at the end of the subtree
  6902. (when (equal arg '(16))
  6903. (org-up-heading-safe)
  6904. (org-end-of-subtree t))
  6905. ;; Insert a heading
  6906. (save-restriction
  6907. (widen)
  6908. (let* ((level nil)
  6909. (on-heading (org-at-heading-p))
  6910. (empty-line-p (if on-heading
  6911. (org-previous-line-empty-p)
  6912. ;; We will decide later
  6913. nil))
  6914. ;; Get a level string to fall back on
  6915. (fix-level
  6916. (if (org-before-first-heading-p) "*"
  6917. (save-excursion
  6918. (org-back-to-heading t)
  6919. (if (org-previous-line-empty-p) (setq empty-line-p t))
  6920. (looking-at org-outline-regexp)
  6921. (make-string (1- (length (match-string 0))) ?*))))
  6922. (stars
  6923. (save-excursion
  6924. (condition-case nil
  6925. (progn
  6926. (org-back-to-heading invisible-ok)
  6927. (when (and (not on-heading)
  6928. (featurep 'org-inlinetask)
  6929. (integerp org-inlinetask-min-level)
  6930. (>= (length (match-string 0))
  6931. org-inlinetask-min-level))
  6932. ;; Find a heading level before the inline task
  6933. (while (and (setq level (org-up-heading-safe))
  6934. (>= level org-inlinetask-min-level)))
  6935. (if (org-at-heading-p)
  6936. (org-back-to-heading invisible-ok)
  6937. (error "This should not happen")))
  6938. (unless (and (save-excursion
  6939. (save-match-data
  6940. (org-backward-heading-same-level
  6941. 1 invisible-ok))
  6942. (= (point) (match-beginning 0)))
  6943. (not (org-previous-line-empty-p t)))
  6944. (setq empty-line-p (or empty-line-p
  6945. (org-previous-line-empty-p))))
  6946. (match-string 0))
  6947. (error (or fix-level "* ")))))
  6948. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6949. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  6950. pos hide-previous previous-pos)
  6951. ;; If we insert after content, move there and clean up whitespace
  6952. (when (and respect-content
  6953. (not (org-looking-at-p org-outline-regexp-bol)))
  6954. (if (not (org-before-first-heading-p))
  6955. (org-end-of-subtree nil t)
  6956. (re-search-forward org-outline-regexp-bol)
  6957. (beginning-of-line 0))
  6958. (skip-chars-backward " \r\n")
  6959. (and (not (looking-back "^\\*+"))
  6960. (looking-at "[ \t]+") (replace-match ""))
  6961. (unless (eobp) (forward-char 1))
  6962. (when (looking-at "^\\*")
  6963. (unless (bobp) (backward-char 1))
  6964. (insert "\n")))
  6965. ;; If we are splitting, grab the text that should be moved to the new headline
  6966. (when may-split
  6967. (if (org-on-heading-p)
  6968. ;; This is a heading, we split intelligently (keeping tags)
  6969. (let ((pos (point)))
  6970. (goto-char (point-at-bol))
  6971. (unless (looking-at org-complex-heading-regexp)
  6972. (error "This should not happen"))
  6973. (when (and (match-beginning 4)
  6974. (> pos (match-beginning 4))
  6975. (< pos (match-end 4)))
  6976. (setq initial-content (buffer-substring pos (match-end 4)))
  6977. (goto-char pos)
  6978. (delete-region (point) (match-end 4))
  6979. (if (looking-at "[ \t]*$")
  6980. (replace-match "")
  6981. (insert (make-string (length initial-content) ?\ )))
  6982. (setq initial-content (org-trim initial-content)))
  6983. (goto-char pos))
  6984. ;; a normal line
  6985. (setq initial-content
  6986. (org-trim (buffer-substring (point) (point-at-eol))))
  6987. (delete-region (point) (point-at-eol))))
  6988. ;; If we are at the beginning of the line, insert before it. Else after
  6989. (cond
  6990. ((and (bolp) (looking-at "[ \t]*$")))
  6991. ((and (bolp) (not (looking-at "[ \t]*$")))
  6992. (open-line 1))
  6993. (t
  6994. (goto-char (point-at-eol))
  6995. (insert "\n")))
  6996. ;; Insert the new heading
  6997. (insert stars)
  6998. (just-one-space)
  6999. (insert initial-content)
  7000. (when adjust-empty-lines
  7001. (if (or (not blank)
  7002. (and blank (not (org-previous-line-empty-p))))
  7003. (org-N-empty-lines-before-current (if blank 1 0))))
  7004. (run-hooks 'org-insert-heading-hook)))))))
  7005. (defun org-N-empty-lines-before-current (N)
  7006. "Make the number of empty lines before current exactly N.
  7007. So this will delete or add empty lines."
  7008. (save-excursion
  7009. (beginning-of-line)
  7010. (let ((p (point)))
  7011. (skip-chars-backward " \r\t\n")
  7012. (unless (bolp) (forward-line))
  7013. (delete-region (point) p))
  7014. (when (> N 0) (insert (make-string N ?\n)))))
  7015. (defun org-get-heading (&optional no-tags no-todo)
  7016. "Return the heading of the current entry, without the stars.
  7017. When NO-TAGS is non-nil, don't include tags.
  7018. When NO-TODO is non-nil, don't include TODO keywords."
  7019. (save-excursion
  7020. (org-back-to-heading t)
  7021. (cond
  7022. ((and no-tags no-todo)
  7023. (looking-at org-complex-heading-regexp)
  7024. (match-string 4))
  7025. (no-tags
  7026. (looking-at (concat org-outline-regexp
  7027. "\\(.*?\\)"
  7028. "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
  7029. (match-string 1))
  7030. (no-todo
  7031. (looking-at org-todo-line-regexp)
  7032. (match-string 3))
  7033. (t (looking-at org-heading-regexp)
  7034. (match-string 2)))))
  7035. (defvar orgstruct-mode) ; defined below
  7036. (defun org-heading-components ()
  7037. "Return the components of the current heading.
  7038. This is a list with the following elements:
  7039. - the level as an integer
  7040. - the reduced level, different if `org-odd-levels-only' is set.
  7041. - the TODO keyword, or nil
  7042. - the priority character, like ?A, or nil if no priority is given
  7043. - the headline text itself, or the tags string if no headline text
  7044. - the tags string, or nil."
  7045. (save-excursion
  7046. (org-back-to-heading t)
  7047. (if (let (case-fold-search)
  7048. (looking-at
  7049. (if orgstruct-mode
  7050. org-heading-regexp
  7051. org-complex-heading-regexp)))
  7052. (if orgstruct-mode
  7053. (list (length (match-string 1))
  7054. (org-reduced-level (length (match-string 1)))
  7055. nil
  7056. nil
  7057. (match-string 2)
  7058. nil)
  7059. (list (length (match-string 1))
  7060. (org-reduced-level (length (match-string 1)))
  7061. (org-match-string-no-properties 2)
  7062. (and (match-end 3) (aref (match-string 3) 2))
  7063. (org-match-string-no-properties 4)
  7064. (org-match-string-no-properties 5))))))
  7065. (defun org-get-entry ()
  7066. "Get the entry text, after heading, entire subtree."
  7067. (save-excursion
  7068. (org-back-to-heading t)
  7069. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  7070. (defun org-insert-heading-after-current ()
  7071. "Insert a new heading with same level as current, after current subtree."
  7072. (interactive)
  7073. (org-back-to-heading)
  7074. (org-insert-heading)
  7075. (org-move-subtree-down)
  7076. (end-of-line 1))
  7077. (defun org-insert-heading-respect-content (&optional invisible-ok)
  7078. "Insert heading with `org-insert-heading-respect-content' set to t."
  7079. (interactive)
  7080. (org-insert-heading '(4) invisible-ok))
  7081. (defun org-insert-todo-heading-respect-content (&optional force-state)
  7082. "Insert TODO heading with `org-insert-heading-respect-content' set to t."
  7083. (interactive)
  7084. (org-insert-todo-heading force-state '(4)))
  7085. (defun org-insert-todo-heading (arg &optional force-heading)
  7086. "Insert a new heading with the same level and TODO state as current heading.
  7087. If the heading has no TODO state, or if the state is DONE, use the first
  7088. state (TODO by default). Also with one prefix arg, force first state. With
  7089. two prefix args, force inserting at the end of the parent subtree."
  7090. (interactive "P")
  7091. (when (or force-heading (not (org-insert-item 'checkbox)))
  7092. (org-insert-heading (or (and (equal arg '(16)) '(16))
  7093. force-heading))
  7094. (save-excursion
  7095. (org-back-to-heading)
  7096. (outline-previous-heading)
  7097. (looking-at org-todo-line-regexp))
  7098. (let*
  7099. ((new-mark-x
  7100. (if (or (equal arg '(4))
  7101. (not (match-beginning 2))
  7102. (member (match-string 2) org-done-keywords))
  7103. (car org-todo-keywords-1)
  7104. (match-string 2)))
  7105. (new-mark
  7106. (or
  7107. (run-hook-with-args-until-success
  7108. 'org-todo-get-default-hook new-mark-x nil)
  7109. new-mark-x)))
  7110. (beginning-of-line 1)
  7111. (and (looking-at org-outline-regexp) (goto-char (match-end 0))
  7112. (if org-treat-insert-todo-heading-as-state-change
  7113. (org-todo new-mark)
  7114. (insert new-mark " "))))
  7115. (when org-provide-todo-statistics
  7116. (org-update-parent-todo-statistics))))
  7117. (defun org-insert-subheading (arg)
  7118. "Insert a new subheading and demote it.
  7119. Works for outline headings and for plain lists alike."
  7120. (interactive "P")
  7121. (org-insert-heading arg)
  7122. (cond
  7123. ((org-at-heading-p) (org-do-demote))
  7124. ((org-at-item-p) (org-indent-item))))
  7125. (defun org-insert-todo-subheading (arg)
  7126. "Insert a new subheading with TODO keyword or checkbox and demote it.
  7127. Works for outline headings and for plain lists alike."
  7128. (interactive "P")
  7129. (org-insert-todo-heading arg)
  7130. (cond
  7131. ((org-at-heading-p) (org-do-demote))
  7132. ((org-at-item-p) (org-indent-item))))
  7133. ;;; Promotion and Demotion
  7134. (defvar org-after-demote-entry-hook nil
  7135. "Hook run after an entry has been demoted.
  7136. The cursor will be at the beginning of the entry.
  7137. When a subtree is being demoted, the hook will be called for each node.")
  7138. (defvar org-after-promote-entry-hook nil
  7139. "Hook run after an entry has been promoted.
  7140. The cursor will be at the beginning of the entry.
  7141. When a subtree is being promoted, the hook will be called for each node.")
  7142. (defun org-promote-subtree ()
  7143. "Promote the entire subtree.
  7144. See also `org-promote'."
  7145. (interactive)
  7146. (save-excursion
  7147. (org-with-limited-levels (org-map-tree 'org-promote)))
  7148. (org-fix-position-after-promote))
  7149. (defun org-demote-subtree ()
  7150. "Demote the entire subtree. See `org-demote'.
  7151. See also `org-promote'."
  7152. (interactive)
  7153. (save-excursion
  7154. (org-with-limited-levels (org-map-tree 'org-demote)))
  7155. (org-fix-position-after-promote))
  7156. (defun org-do-promote ()
  7157. "Promote the current heading higher up the tree.
  7158. If the region is active in `transient-mark-mode', promote all headings
  7159. in the region."
  7160. (interactive)
  7161. (save-excursion
  7162. (if (org-region-active-p)
  7163. (org-map-region 'org-promote (region-beginning) (region-end))
  7164. (org-promote)))
  7165. (org-fix-position-after-promote))
  7166. (defun org-do-demote ()
  7167. "Demote the current heading lower down the tree.
  7168. If the region is active in `transient-mark-mode', demote all headings
  7169. in the region."
  7170. (interactive)
  7171. (save-excursion
  7172. (if (org-region-active-p)
  7173. (org-map-region 'org-demote (region-beginning) (region-end))
  7174. (org-demote)))
  7175. (org-fix-position-after-promote))
  7176. (defun org-fix-position-after-promote ()
  7177. "Make sure that after pro/demotion cursor position is right."
  7178. (let ((pos (point)))
  7179. (when (save-excursion
  7180. (beginning-of-line 1)
  7181. (looking-at org-todo-line-regexp)
  7182. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  7183. (cond ((eobp) (insert " "))
  7184. ((eolp) (insert " "))
  7185. ((equal (char-after) ?\ ) (forward-char 1))))))
  7186. (defun org-current-level ()
  7187. "Return the level of the current entry, or nil if before the first headline.
  7188. The level is the number of stars at the beginning of the headline."
  7189. (save-excursion
  7190. (org-with-limited-levels
  7191. (if (ignore-errors (org-back-to-heading t))
  7192. (funcall outline-level)))))
  7193. (defun org-get-previous-line-level ()
  7194. "Return the outline depth of the last headline before the current line.
  7195. Returns 0 for the first headline in the buffer, and nil if before the
  7196. first headline."
  7197. (and (org-current-level)
  7198. (or (and (/= (line-beginning-position) (point-min))
  7199. (save-excursion (beginning-of-line 0) (org-current-level)))
  7200. 0)))
  7201. (defun org-reduced-level (l)
  7202. "Compute the effective level of a heading.
  7203. This takes into account the setting of `org-odd-levels-only'."
  7204. (cond
  7205. ((zerop l) 0)
  7206. (org-odd-levels-only (1+ (floor (/ l 2))))
  7207. (t l)))
  7208. (defun org-level-increment ()
  7209. "Return the number of stars that will be added or removed at a
  7210. time to headlines when structure editing, based on the value of
  7211. `org-odd-levels-only'."
  7212. (if org-odd-levels-only 2 1))
  7213. (defun org-get-valid-level (level &optional change)
  7214. "Rectify a level change under the influence of `org-odd-levels-only'
  7215. LEVEL is a current level, CHANGE is by how much the level should be
  7216. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  7217. even level numbers will become the next higher odd number."
  7218. (if org-odd-levels-only
  7219. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  7220. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  7221. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  7222. (max 1 (+ level (or change 0)))))
  7223. (if (boundp 'define-obsolete-function-alias)
  7224. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  7225. (define-obsolete-function-alias 'org-get-legal-level
  7226. 'org-get-valid-level)
  7227. (define-obsolete-function-alias 'org-get-legal-level
  7228. 'org-get-valid-level "23.1")))
  7229. (defun org-promote ()
  7230. "Promote the current heading higher up the tree."
  7231. (org-with-wide-buffer
  7232. (org-back-to-heading t)
  7233. (let* ((after-change-functions (remq 'flyspell-after-change-function
  7234. after-change-functions))
  7235. (level (save-match-data (funcall outline-level)))
  7236. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  7237. (diff (abs (- level (length up-head) -1))))
  7238. (cond
  7239. ((and (= level 1) org-allow-promoting-top-level-subtree)
  7240. (replace-match "# " nil t))
  7241. ((= level 1)
  7242. (user-error "Cannot promote to level 0. UNDO to recover if necessary"))
  7243. (t (replace-match up-head nil t)))
  7244. (unless (= level 1)
  7245. (when org-auto-align-tags (org-set-tags nil 'ignore-column))
  7246. (when org-adapt-indentation (org-fixup-indentation (- diff))))
  7247. (run-hooks 'org-after-promote-entry-hook))))
  7248. (defun org-demote ()
  7249. "Demote the current heading lower down the tree."
  7250. (org-with-wide-buffer
  7251. (org-back-to-heading t)
  7252. (let* ((after-change-functions (remq 'flyspell-after-change-function
  7253. after-change-functions))
  7254. (level (save-match-data (funcall outline-level)))
  7255. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  7256. (diff (abs (- level (length down-head) -1))))
  7257. (replace-match down-head nil t)
  7258. (when org-auto-align-tags (org-set-tags nil 'ignore-column))
  7259. (when org-adapt-indentation (org-fixup-indentation diff))
  7260. (run-hooks 'org-after-demote-entry-hook))))
  7261. (defun org-cycle-level ()
  7262. "Cycle the level of an empty headline through possible states.
  7263. This goes first to child, then to parent, level, then up the hierarchy.
  7264. After top level, it switches back to sibling level."
  7265. (interactive)
  7266. (let ((org-adapt-indentation nil))
  7267. (when (org-point-at-end-of-empty-headline)
  7268. (setq this-command 'org-cycle-level) ; Only needed for caching
  7269. (let ((cur-level (org-current-level))
  7270. (prev-level (org-get-previous-line-level)))
  7271. (cond
  7272. ;; If first headline in file, promote to top-level.
  7273. ((= prev-level 0)
  7274. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7275. do (org-do-promote)))
  7276. ;; If same level as prev, demote one.
  7277. ((= prev-level cur-level)
  7278. (org-do-demote))
  7279. ;; If parent is top-level, promote to top level if not already.
  7280. ((= prev-level 1)
  7281. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7282. do (org-do-promote)))
  7283. ;; If top-level, return to prev-level.
  7284. ((= cur-level 1)
  7285. (loop repeat (/ (- prev-level 1) (org-level-increment))
  7286. do (org-do-demote)))
  7287. ;; If less than prev-level, promote one.
  7288. ((< cur-level prev-level)
  7289. (org-do-promote))
  7290. ;; If deeper than prev-level, promote until higher than
  7291. ;; prev-level.
  7292. ((> cur-level prev-level)
  7293. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  7294. do (org-do-promote))))
  7295. t))))
  7296. (defun org-map-tree (fun)
  7297. "Call FUN for every heading underneath the current one."
  7298. (org-back-to-heading)
  7299. (let ((level (funcall outline-level)))
  7300. (save-excursion
  7301. (funcall fun)
  7302. (while (and (progn
  7303. (outline-next-heading)
  7304. (> (funcall outline-level) level))
  7305. (not (eobp)))
  7306. (funcall fun)))))
  7307. (defun org-map-region (fun beg end)
  7308. "Call FUN for every heading between BEG and END."
  7309. (let ((org-ignore-region t))
  7310. (save-excursion
  7311. (setq end (copy-marker end))
  7312. (goto-char beg)
  7313. (if (and (re-search-forward org-outline-regexp-bol nil t)
  7314. (< (point) end))
  7315. (funcall fun))
  7316. (while (and (progn
  7317. (outline-next-heading)
  7318. (< (point) end))
  7319. (not (eobp)))
  7320. (funcall fun)))))
  7321. (defun org-fixup-indentation (diff)
  7322. "Change the indentation in the current entry by DIFF.
  7323. DIFF is an integer. Indentation is done according to the
  7324. following rules:
  7325. - Planning information and property drawers are always indented
  7326. according to the new level of the headline;
  7327. - Footnote definitions and their contents are ignored;
  7328. - Inlinetasks' boundaries are not shifted;
  7329. - Empty lines are ignored;
  7330. - Other lines' indentation are shifted by DIFF columns, unless
  7331. it would introduce a structural change in the document, in
  7332. which case no shifting is done at all.
  7333. Assume point is at a heading or an inlinetask beginning."
  7334. (org-with-wide-buffer
  7335. (narrow-to-region (line-beginning-position)
  7336. (save-excursion
  7337. (if (org-with-limited-levels (org-at-heading-p))
  7338. (org-with-limited-levels (outline-next-heading))
  7339. (org-inlinetask-goto-end))
  7340. (point)))
  7341. (forward-line)
  7342. ;; Indent properly planning info and property drawer.
  7343. (when (org-looking-at-p org-planning-line-re)
  7344. (org-indent-line)
  7345. (forward-line))
  7346. (when (looking-at org-property-drawer-re)
  7347. (goto-char (match-end 0))
  7348. (forward-line)
  7349. (save-excursion (org-indent-region (match-beginning 0) (match-end 0))))
  7350. (catch 'no-shift
  7351. (when (zerop diff) (throw 'no-shift nil))
  7352. ;; If DIFF is negative, first check if a shift is possible at all
  7353. ;; (e.g., it doesn't break structure). This can only happen if
  7354. ;; some contents are not properly indented.
  7355. (let ((case-fold-search t))
  7356. (when (< diff 0)
  7357. (let ((diff (- diff))
  7358. (forbidden-re (concat org-outline-regexp
  7359. "\\|"
  7360. (substring org-footnote-definition-re 1))))
  7361. (save-excursion
  7362. (while (not (eobp))
  7363. (cond
  7364. ((org-looking-at-p "[ \t]*$") (forward-line))
  7365. ((and (org-looking-at-p org-footnote-definition-re)
  7366. (let ((e (org-element-at-point)))
  7367. (and (eq (org-element-type e) 'footnote-definition)
  7368. (goto-char (org-element-property :end e))))))
  7369. ((org-looking-at-p org-outline-regexp) (forward-line))
  7370. ;; Give up if shifting would move before column 0 or
  7371. ;; if it would introduce a headline or a footnote
  7372. ;; definition.
  7373. (t
  7374. (skip-chars-forward " \t")
  7375. (let ((ind (current-column)))
  7376. (when (or (< ind diff)
  7377. (and (= ind diff) (org-looking-at-p forbidden-re)))
  7378. (throw 'no-shift nil)))
  7379. ;; Ignore contents of example blocks and source
  7380. ;; blocks if their indentation is meant to be
  7381. ;; preserved. Jump to block's closing line.
  7382. (beginning-of-line)
  7383. (or (and (org-looking-at-p "[ \t]*#\\+BEGIN_\\(EXAMPLE\\|SRC\\)")
  7384. (let ((e (org-element-at-point)))
  7385. (and (memq (org-element-type e)
  7386. '(example-block src-block))
  7387. (or org-src-preserve-indentation
  7388. (org-element-property :preserve-indent e))
  7389. (goto-char (org-element-property :end e))
  7390. (progn (skip-chars-backward " \r\t\n")
  7391. (beginning-of-line)
  7392. t))))
  7393. (forward-line))))))))
  7394. ;; Shift lines but footnote definitions, inlinetasks boundaries
  7395. ;; by DIFF. Also skip contents of source or example blocks
  7396. ;; when indentation is meant to be preserved.
  7397. (while (not (eobp))
  7398. (cond
  7399. ((and (org-looking-at-p org-footnote-definition-re)
  7400. (let ((e (org-element-at-point)))
  7401. (and (eq (org-element-type e) 'footnote-definition)
  7402. (goto-char (org-element-property :end e))))))
  7403. ((org-looking-at-p org-outline-regexp) (forward-line))
  7404. ((org-looking-at-p "[ \t]*$") (forward-line))
  7405. (t
  7406. (org-indent-line-to (+ (org-get-indentation) diff))
  7407. (beginning-of-line)
  7408. (or (and (org-looking-at-p "[ \t]*#\\+BEGIN_\\(EXAMPLE\\|SRC\\)")
  7409. (let ((e (org-element-at-point)))
  7410. (and (memq (org-element-type e)
  7411. '(example-block src-block))
  7412. (or org-src-preserve-indentation
  7413. (org-element-property :preserve-indent e))
  7414. (goto-char (org-element-property :end e))
  7415. (progn (skip-chars-backward " \r\t\n")
  7416. (beginning-of-line)
  7417. t))))
  7418. (forward-line)))))))))
  7419. (defun org-convert-to-odd-levels ()
  7420. "Convert an org-mode file with all levels allowed to one with odd levels.
  7421. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  7422. level 5 etc."
  7423. (interactive)
  7424. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  7425. (let ((outline-level 'org-outline-level)
  7426. (org-odd-levels-only nil) n)
  7427. (save-excursion
  7428. (goto-char (point-min))
  7429. (while (re-search-forward "^\\*\\*+ " nil t)
  7430. (setq n (- (length (match-string 0)) 2))
  7431. (while (>= (setq n (1- n)) 0)
  7432. (org-demote))
  7433. (end-of-line 1))))))
  7434. (defun org-convert-to-oddeven-levels ()
  7435. "Convert an org-mode file with only odd levels to one with odd/even levels.
  7436. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  7437. file contains a section with an even level, conversion would
  7438. destroy the structure of the file. An error is signaled in this
  7439. case."
  7440. (interactive)
  7441. (goto-char (point-min))
  7442. ;; First check if there are no even levels
  7443. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  7444. (org-show-context t)
  7445. (error "Not all levels are odd in this file. Conversion not possible"))
  7446. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  7447. (let ((outline-regexp org-outline-regexp)
  7448. (outline-level 'org-outline-level)
  7449. (org-odd-levels-only nil) n)
  7450. (save-excursion
  7451. (goto-char (point-min))
  7452. (while (re-search-forward "^\\*\\*+ " nil t)
  7453. (setq n (/ (1- (length (match-string 0))) 2))
  7454. (while (>= (setq n (1- n)) 0)
  7455. (org-promote))
  7456. (end-of-line 1))))))
  7457. (defun org-tr-level (n)
  7458. "Make N odd if required."
  7459. (if org-odd-levels-only (1+ (/ n 2)) n))
  7460. ;;; Vertical tree motion, cutting and pasting of subtrees
  7461. (defun org-move-subtree-up (&optional arg)
  7462. "Move the current subtree up past ARG headlines of the same level."
  7463. (interactive "p")
  7464. (org-move-subtree-down (- (prefix-numeric-value arg))))
  7465. (defun org-move-subtree-down (&optional arg)
  7466. "Move the current subtree down past ARG headlines of the same level."
  7467. (interactive "p")
  7468. (setq arg (prefix-numeric-value arg))
  7469. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  7470. 'org-get-last-sibling))
  7471. (ins-point (make-marker))
  7472. (cnt (abs arg))
  7473. (col (current-column))
  7474. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  7475. ;; Select the tree
  7476. (org-back-to-heading)
  7477. (setq beg0 (point))
  7478. (save-excursion
  7479. (setq ne-beg (org-back-over-empty-lines))
  7480. (setq beg (point)))
  7481. (save-match-data
  7482. (save-excursion (outline-end-of-heading)
  7483. (setq folded (outline-invisible-p)))
  7484. (progn (org-end-of-subtree nil t)
  7485. (unless (eobp) (backward-char))))
  7486. (outline-next-heading)
  7487. (setq ne-end (org-back-over-empty-lines))
  7488. (setq end (point))
  7489. (goto-char beg0)
  7490. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  7491. ;; include less whitespace
  7492. (save-excursion
  7493. (goto-char beg)
  7494. (forward-line (- ne-beg ne-end))
  7495. (setq beg (point))))
  7496. ;; Find insertion point, with error handling
  7497. (while (> cnt 0)
  7498. (or (and (funcall movfunc) (looking-at org-outline-regexp))
  7499. (progn (goto-char beg0)
  7500. (user-error "Cannot move past superior level or buffer limit")))
  7501. (setq cnt (1- cnt)))
  7502. (if (> arg 0)
  7503. ;; Moving forward - still need to move over subtree
  7504. (progn (org-end-of-subtree t t)
  7505. (save-excursion
  7506. (org-back-over-empty-lines)
  7507. (or (bolp) (newline)))))
  7508. (setq ne-ins (org-back-over-empty-lines))
  7509. (move-marker ins-point (point))
  7510. (setq txt (buffer-substring beg end))
  7511. (org-save-markers-in-region beg end)
  7512. (delete-region beg end)
  7513. (org-remove-empty-overlays-at beg)
  7514. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  7515. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  7516. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  7517. (let ((bbb (point)))
  7518. (insert-before-markers txt)
  7519. (org-reinstall-markers-in-region bbb)
  7520. (move-marker ins-point bbb))
  7521. (or (bolp) (insert "\n"))
  7522. (setq ins-end (point))
  7523. (goto-char ins-point)
  7524. (org-skip-whitespace)
  7525. (when (and (< arg 0)
  7526. (org-first-sibling-p)
  7527. (> ne-ins ne-beg))
  7528. ;; Move whitespace back to beginning
  7529. (save-excursion
  7530. (goto-char ins-end)
  7531. (let ((kill-whole-line t))
  7532. (kill-line (- ne-ins ne-beg)) (point)))
  7533. (insert (make-string (- ne-ins ne-beg) ?\n)))
  7534. (move-marker ins-point nil)
  7535. (if folded
  7536. (hide-subtree)
  7537. (org-show-entry)
  7538. (show-children)
  7539. (org-cycle-hide-drawers 'children))
  7540. (org-clean-visibility-after-subtree-move)
  7541. ;; move back to the initial column we were at
  7542. (move-to-column col)))
  7543. (defvar org-subtree-clip ""
  7544. "Clipboard for cut and paste of subtrees.
  7545. This is actually only a copy of the kill, because we use the normal kill
  7546. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  7547. (defvar org-subtree-clip-folded nil
  7548. "Was the last copied subtree folded?
  7549. This is used to fold the tree back after pasting.")
  7550. (defun org-cut-subtree (&optional n)
  7551. "Cut the current subtree into the clipboard.
  7552. With prefix arg N, cut this many sequential subtrees.
  7553. This is a short-hand for marking the subtree and then cutting it."
  7554. (interactive "p")
  7555. (org-copy-subtree n 'cut))
  7556. (defun org-copy-subtree (&optional n cut force-store-markers nosubtrees)
  7557. "Copy the current subtree it in the clipboard.
  7558. With prefix arg N, copy this many sequential subtrees.
  7559. This is a short-hand for marking the subtree and then copying it.
  7560. If CUT is non-nil, actually cut the subtree.
  7561. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  7562. of some markers in the region, even if CUT is non-nil. This is
  7563. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  7564. (interactive "p")
  7565. (let (beg end folded (beg0 (point)))
  7566. (if (org-called-interactively-p 'any)
  7567. (org-back-to-heading nil) ; take what looks like a subtree
  7568. (org-back-to-heading t)) ; take what is really there
  7569. (setq beg (point))
  7570. (skip-chars-forward " \t\r\n")
  7571. (save-match-data
  7572. (if nosubtrees
  7573. (outline-next-heading)
  7574. (save-excursion (outline-end-of-heading)
  7575. (setq folded (outline-invisible-p)))
  7576. (ignore-errors (org-forward-heading-same-level (1- n) t))
  7577. (org-end-of-subtree t t)))
  7578. (setq end (point))
  7579. (goto-char beg0)
  7580. (when (> end beg)
  7581. (setq org-subtree-clip-folded folded)
  7582. (when (or cut force-store-markers)
  7583. (org-save-markers-in-region beg end))
  7584. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  7585. (setq org-subtree-clip (current-kill 0))
  7586. (message "%s: Subtree(s) with %d characters"
  7587. (if cut "Cut" "Copied")
  7588. (length org-subtree-clip)))))
  7589. (defun org-paste-subtree (&optional level tree for-yank remove)
  7590. "Paste the clipboard as a subtree, with modification of headline level.
  7591. The entire subtree is promoted or demoted in order to match a new headline
  7592. level.
  7593. If the cursor is at the beginning of a headline, the same level as
  7594. that headline is used to paste the tree.
  7595. If not, the new level is derived from the *visible* headings
  7596. before and after the insertion point, and taken to be the inferior headline
  7597. level of the two. So if the previous visible heading is level 3 and the
  7598. next is level 4 (or vice versa), level 4 will be used for insertion.
  7599. This makes sure that the subtree remains an independent subtree and does
  7600. not swallow low level entries.
  7601. You can also force a different level, either by using a numeric prefix
  7602. argument, or by inserting the heading marker by hand. For example, if the
  7603. cursor is after \"*****\", then the tree will be shifted to level 5.
  7604. If optional TREE is given, use this text instead of the kill ring.
  7605. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  7606. move back over whitespace before inserting, and move point to the end of
  7607. the inserted text when done.
  7608. When REMOVE is non-nil, remove the subtree from the clipboard."
  7609. (interactive "P")
  7610. (setq tree (or tree (and kill-ring (current-kill 0))))
  7611. (unless (org-kill-is-subtree-p tree)
  7612. (user-error "%s"
  7613. (substitute-command-keys
  7614. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  7615. (org-with-limited-levels
  7616. (let* ((visp (not (outline-invisible-p)))
  7617. (txt tree)
  7618. (^re_ "\\(\\*+\\)[ \t]*")
  7619. (old-level (if (string-match org-outline-regexp-bol txt)
  7620. (- (match-end 0) (match-beginning 0) 1)
  7621. -1))
  7622. (force-level (cond (level (prefix-numeric-value level))
  7623. ((and (looking-at "[ \t]*$")
  7624. (string-match
  7625. "^\\*+$" (buffer-substring
  7626. (point-at-bol) (point))))
  7627. (- (match-end 0) (match-beginning 0)))
  7628. ((and (bolp)
  7629. (looking-at org-outline-regexp))
  7630. (- (match-end 0) (point) 1))))
  7631. (previous-level (save-excursion
  7632. (condition-case nil
  7633. (progn
  7634. (outline-previous-visible-heading 1)
  7635. (if (looking-at ^re_)
  7636. (- (match-end 0) (match-beginning 0) 1)
  7637. 1))
  7638. (error 1))))
  7639. (next-level (save-excursion
  7640. (condition-case nil
  7641. (progn
  7642. (or (looking-at org-outline-regexp)
  7643. (outline-next-visible-heading 1))
  7644. (if (looking-at ^re_)
  7645. (- (match-end 0) (match-beginning 0) 1)
  7646. 1))
  7647. (error 1))))
  7648. (new-level (or force-level (max previous-level next-level)))
  7649. (shift (if (or (= old-level -1)
  7650. (= new-level -1)
  7651. (= old-level new-level))
  7652. 0
  7653. (- new-level old-level)))
  7654. (delta (if (> shift 0) -1 1))
  7655. (func (if (> shift 0) 'org-demote 'org-promote))
  7656. (org-odd-levels-only nil)
  7657. beg end newend)
  7658. ;; Remove the forced level indicator
  7659. (if force-level
  7660. (delete-region (point-at-bol) (point)))
  7661. ;; Paste
  7662. (beginning-of-line (if (bolp) 1 2))
  7663. (setq beg (point))
  7664. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  7665. (insert-before-markers txt)
  7666. (unless (string-match "\n\\'" txt) (insert "\n"))
  7667. (setq newend (point))
  7668. (org-reinstall-markers-in-region beg)
  7669. (setq end (point))
  7670. (goto-char beg)
  7671. (skip-chars-forward " \t\n\r")
  7672. (setq beg (point))
  7673. (if (and (outline-invisible-p) visp)
  7674. (save-excursion (outline-show-heading)))
  7675. ;; Shift if necessary
  7676. (unless (= shift 0)
  7677. (save-restriction
  7678. (narrow-to-region beg end)
  7679. (while (not (= shift 0))
  7680. (org-map-region func (point-min) (point-max))
  7681. (setq shift (+ delta shift)))
  7682. (goto-char (point-min))
  7683. (setq newend (point-max))))
  7684. (when (or (org-called-interactively-p 'interactive) for-yank)
  7685. (message "Clipboard pasted as level %d subtree" new-level))
  7686. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  7687. kill-ring
  7688. (eq org-subtree-clip (current-kill 0))
  7689. org-subtree-clip-folded)
  7690. ;; The tree was folded before it was killed/copied
  7691. (hide-subtree))
  7692. (and for-yank (goto-char newend))
  7693. (and remove (setq kill-ring (cdr kill-ring))))))
  7694. (defun org-kill-is-subtree-p (&optional txt)
  7695. "Check if the current kill is an outline subtree, or a set of trees.
  7696. Returns nil if kill does not start with a headline, or if the first
  7697. headline level is not the largest headline level in the tree.
  7698. So this will actually accept several entries of equal levels as well,
  7699. which is OK for `org-paste-subtree'.
  7700. If optional TXT is given, check this string instead of the current kill."
  7701. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  7702. (re (org-get-limited-outline-regexp))
  7703. (^re (concat "^" re))
  7704. (start-level (and kill
  7705. (string-match
  7706. (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
  7707. kill)
  7708. (- (match-end 2) (match-beginning 2) 1)))
  7709. (start (1+ (or (match-beginning 2) -1))))
  7710. (if (not start-level)
  7711. (progn
  7712. nil) ;; does not even start with a heading
  7713. (catch 'exit
  7714. (while (setq start (string-match ^re kill (1+ start)))
  7715. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  7716. (throw 'exit nil)))
  7717. t))))
  7718. (defvar org-markers-to-move nil
  7719. "Markers that should be moved with a cut-and-paste operation.
  7720. Those markers are stored together with their positions relative to
  7721. the start of the region.")
  7722. (defun org-save-markers-in-region (beg end)
  7723. "Check markers in region.
  7724. If these markers are between BEG and END, record their position relative
  7725. to BEG, so that after moving the block of text, we can put the markers back
  7726. into place.
  7727. This function gets called just before an entry or tree gets cut from the
  7728. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  7729. called immediately, to move the markers with the entries."
  7730. (setq org-markers-to-move nil)
  7731. (when (featurep 'org-clock)
  7732. (org-clock-save-markers-for-cut-and-paste beg end))
  7733. (when (featurep 'org-agenda)
  7734. (org-agenda-save-markers-for-cut-and-paste beg end)))
  7735. (defun org-check-and-save-marker (marker beg end)
  7736. "Check if MARKER is between BEG and END.
  7737. If yes, remember the marker and the distance to BEG."
  7738. (when (and (marker-buffer marker)
  7739. (equal (marker-buffer marker) (current-buffer)))
  7740. (if (and (>= marker beg) (< marker end))
  7741. (push (cons marker (- marker beg)) org-markers-to-move))))
  7742. (defun org-reinstall-markers-in-region (beg)
  7743. "Move all remembered markers to their position relative to BEG."
  7744. (mapc (lambda (x)
  7745. (move-marker (car x) (+ beg (cdr x))))
  7746. org-markers-to-move)
  7747. (setq org-markers-to-move nil))
  7748. (defun org-narrow-to-subtree ()
  7749. "Narrow buffer to the current subtree."
  7750. (interactive)
  7751. (save-excursion
  7752. (save-match-data
  7753. (org-with-limited-levels
  7754. (narrow-to-region
  7755. (progn (org-back-to-heading t) (point))
  7756. (progn (org-end-of-subtree t t)
  7757. (if (and (org-at-heading-p) (not (eobp))) (backward-char 1))
  7758. (point)))))))
  7759. (defun org-narrow-to-block ()
  7760. "Narrow buffer to the current block."
  7761. (interactive)
  7762. (let* ((case-fold-search t)
  7763. (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
  7764. "^[ \t]*#\\+end_.*")))
  7765. (if blockp
  7766. (narrow-to-region (car blockp) (cdr blockp))
  7767. (user-error "Not in a block"))))
  7768. (defun org-clone-subtree-with-time-shift (n &optional shift)
  7769. "Clone the task (subtree) at point N times.
  7770. The clones will be inserted as siblings.
  7771. In interactive use, the user will be prompted for the number of
  7772. clones to be produced. If the entry has a timestamp, the user
  7773. will also be prompted for a time shift, which may be a repeater
  7774. as used in time stamps, for example `+3d'. To disable this,
  7775. you can call the function with a universal prefix argument.
  7776. When a valid repeater is given and the entry contains any time
  7777. stamps, the clones will become a sequence in time, with time
  7778. stamps in the subtree shifted for each clone produced. If SHIFT
  7779. is nil or the empty string, time stamps will be left alone. The
  7780. ID property of the original subtree is removed.
  7781. If the original subtree did contain time stamps with a repeater,
  7782. the following will happen:
  7783. - the repeater will be removed in each clone
  7784. - an additional clone will be produced, with the current, unshifted
  7785. date(s) in the entry.
  7786. - the original entry will be placed *after* all the clones, with
  7787. repeater intact.
  7788. - the start days in the repeater in the original entry will be shifted
  7789. to past the last clone.
  7790. In this way you can spell out a number of instances of a repeating task,
  7791. and still retain the repeater to cover future instances of the task."
  7792. (interactive "nNumber of clones to produce: ")
  7793. (let ((shift
  7794. (or shift
  7795. (if (and (not (equal current-prefix-arg '(4)))
  7796. (save-excursion
  7797. (re-search-forward org-ts-regexp-both
  7798. (save-excursion
  7799. (org-end-of-subtree t)
  7800. (point)) t)))
  7801. (read-from-minibuffer
  7802. "Date shift per clone (e.g. +1w, empty to copy unchanged): ")
  7803. ""))) ;; No time shift
  7804. (n-no-remove -1)
  7805. (drawer-re org-drawer-regexp)
  7806. (org-clock-re (format "^[ \t]*%s.*$" org-clock-string))
  7807. beg end template task idprop
  7808. shift-n shift-what doshift nmin nmax)
  7809. (if (not (and (integerp n) (> n 0)))
  7810. (user-error "Invalid number of replications %s" n))
  7811. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  7812. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([hdwmy]\\)[ \t]*\\'"
  7813. shift)))
  7814. (user-error "Invalid shift specification %s" shift))
  7815. (when doshift
  7816. (setq shift-n (string-to-number (match-string 1 shift))
  7817. shift-what (cdr (assoc (match-string 2 shift)
  7818. '(("d" . day) ("w" . week)
  7819. ("m" . month) ("y" . year))))))
  7820. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  7821. (setq nmin 1 nmax n)
  7822. (org-back-to-heading t)
  7823. (setq beg (point))
  7824. (setq idprop (org-entry-get nil "ID"))
  7825. (org-end-of-subtree t t)
  7826. (or (bolp) (insert "\n"))
  7827. (setq end (point))
  7828. (setq template (buffer-substring beg end))
  7829. (when (and doshift
  7830. (string-match "<[^<>\n]+ [.+]?\\+[0-9]+[hdwmy][^<>\n]*>" template))
  7831. (delete-region beg end)
  7832. (setq end beg)
  7833. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  7834. (goto-char end)
  7835. (loop for n from nmin to nmax do
  7836. ;; prepare clone
  7837. (with-temp-buffer
  7838. (insert template)
  7839. (org-mode)
  7840. (goto-char (point-min))
  7841. (org-show-subtree)
  7842. (and idprop (if org-clone-delete-id
  7843. (org-entry-delete nil "ID")
  7844. (org-id-get-create t)))
  7845. (unless (= n 0)
  7846. (while (re-search-forward org-clock-re nil t)
  7847. (kill-whole-line))
  7848. (goto-char (point-min))
  7849. (while (re-search-forward drawer-re nil t)
  7850. (org-remove-empty-drawer-at (point))))
  7851. (goto-char (point-min))
  7852. (when doshift
  7853. (while (re-search-forward org-ts-regexp-both nil t)
  7854. (org-timestamp-change (* n shift-n) shift-what))
  7855. (unless (= n n-no-remove)
  7856. (goto-char (point-min))
  7857. (while (re-search-forward org-ts-regexp nil t)
  7858. (save-excursion
  7859. (goto-char (match-beginning 0))
  7860. (if (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[hdwmy]\\)")
  7861. (delete-region (match-beginning 1) (match-end 1)))))))
  7862. (setq task (buffer-string)))
  7863. (insert task))
  7864. (goto-char beg)))
  7865. ;;; Outline Sorting
  7866. (defun org-sort (with-case)
  7867. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  7868. Optional argument WITH-CASE means sort case-sensitively."
  7869. (interactive "P")
  7870. (cond
  7871. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  7872. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  7873. (t
  7874. (org-call-with-arg 'org-sort-entries with-case))))
  7875. (defun org-sort-remove-invisible (s)
  7876. "Remove invisible links from string S."
  7877. (remove-text-properties 0 (length s) org-rm-props s)
  7878. (while (string-match org-bracket-link-regexp s)
  7879. (setq s (replace-match (if (match-end 2)
  7880. (match-string 3 s)
  7881. (match-string 1 s)) t t s)))
  7882. (let ((st (format " %s " s)))
  7883. (while (string-match org-emph-re st)
  7884. (setq st (replace-match (format " %s " (match-string 4 st)) t t st)))
  7885. (setq s (substring st 1 -1)))
  7886. s)
  7887. (defvar org-priority-regexp) ; defined later in the file
  7888. (defvar org-after-sorting-entries-or-items-hook nil
  7889. "Hook that is run after a bunch of entries or items have been sorted.
  7890. When children are sorted, the cursor is in the parent line when this
  7891. hook gets called. When a region or a plain list is sorted, the cursor
  7892. will be in the first entry of the sorted region/list.")
  7893. (defun org-sort-entries
  7894. (&optional with-case sorting-type getkey-func compare-func property)
  7895. "Sort entries on a certain level of an outline tree.
  7896. If there is an active region, the entries in the region are sorted.
  7897. Else, if the cursor is before the first entry, sort the top-level items.
  7898. Else, the children of the entry at point are sorted.
  7899. Sorting can be alphabetically, numerically, by date/time as given by
  7900. a time stamp, by a property, by priority order, or by a custom function.
  7901. The command prompts for the sorting type unless it has been given to the
  7902. function through the SORTING-TYPE argument, which needs to be a character,
  7903. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?o ?O ?r ?R ?f ?F ?k ?K). Here is
  7904. the precise meaning of each character:
  7905. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  7906. c By creation time, which is assumed to be the first inactive time stamp
  7907. at the beginning of a line.
  7908. d By deadline date/time.
  7909. k By clocking time.
  7910. n Numerically, by converting the beginning of the entry/item to a number.
  7911. o By order of TODO keywords.
  7912. p By priority according to the cookie.
  7913. r By the value of a property.
  7914. s By scheduled date/time.
  7915. t By date/time, either the first active time stamp in the entry, or, if
  7916. none exist, by the first inactive one.
  7917. Capital letters will reverse the sort order.
  7918. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  7919. called with point at the beginning of the record. It must return either
  7920. a string or a number that should serve as the sorting key for that record.
  7921. Comparing entries ignores case by default. However, with an optional argument
  7922. WITH-CASE, the sorting considers case as well.
  7923. Sorting is done against the visible part of the headlines, it ignores hidden
  7924. links.
  7925. When sorting is done, call `org-after-sorting-entries-or-items-hook'."
  7926. (interactive "P")
  7927. (let ((case-func (if with-case 'identity 'downcase))
  7928. (cmstr
  7929. ;; The clock marker is lost when using `sort-subr', let's
  7930. ;; store the clocking string.
  7931. (when (equal (marker-buffer org-clock-marker) (current-buffer))
  7932. (save-excursion
  7933. (goto-char org-clock-marker)
  7934. (looking-back "^.*") (match-string-no-properties 0))))
  7935. start beg end stars re re2
  7936. txt what tmp)
  7937. ;; Find beginning and end of region to sort
  7938. (cond
  7939. ((org-region-active-p)
  7940. ;; we will sort the region
  7941. (setq end (region-end)
  7942. what "region")
  7943. (goto-char (region-beginning))
  7944. (if (not (org-at-heading-p)) (outline-next-heading))
  7945. (setq start (point)))
  7946. ((or (org-at-heading-p)
  7947. (ignore-errors (progn (org-back-to-heading) t)))
  7948. ;; we will sort the children of the current headline
  7949. (org-back-to-heading)
  7950. (setq start (point)
  7951. end (progn (org-end-of-subtree t t)
  7952. (or (bolp) (insert "\n"))
  7953. (when (>= (org-back-over-empty-lines) 1)
  7954. (forward-line 1))
  7955. (point))
  7956. what "children")
  7957. (goto-char start)
  7958. (show-subtree)
  7959. (outline-next-heading))
  7960. (t
  7961. ;; we will sort the top-level entries in this file
  7962. (goto-char (point-min))
  7963. (or (org-at-heading-p) (outline-next-heading))
  7964. (setq start (point))
  7965. (goto-char (point-max))
  7966. (beginning-of-line 1)
  7967. (when (looking-at ".*?\\S-")
  7968. ;; File ends in a non-white line
  7969. (end-of-line 1)
  7970. (insert "\n"))
  7971. (setq end (point-max))
  7972. (setq what "top-level")
  7973. (goto-char start)
  7974. (show-all)))
  7975. (setq beg (point))
  7976. (when (>= beg end) (goto-char start) (user-error "Nothing to sort"))
  7977. (looking-at "\\(\\*+\\)")
  7978. (setq stars (match-string 1)
  7979. re (concat "^" (regexp-quote stars) " +")
  7980. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  7981. txt (buffer-substring beg end))
  7982. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  7983. (if (and (not (equal stars "*")) (string-match re2 txt))
  7984. (user-error "Region to sort contains a level above the first entry"))
  7985. (unless sorting-type
  7986. (message
  7987. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  7988. [t]ime [s]cheduled [d]eadline [c]reated cloc[k]ing
  7989. A/N/P/R/O/F/T/S/D/C/K means reversed:"
  7990. what)
  7991. (setq sorting-type (read-char-exclusive))
  7992. (unless getkey-func
  7993. (and (= (downcase sorting-type) ?f)
  7994. (setq getkey-func
  7995. (org-icompleting-read "Sort using function: "
  7996. obarray 'fboundp t nil nil))
  7997. (setq getkey-func (intern getkey-func))))
  7998. (and (= (downcase sorting-type) ?r)
  7999. (not property)
  8000. (setq property
  8001. (org-icompleting-read "Property: "
  8002. (mapcar 'list (org-buffer-property-keys t))
  8003. nil t))))
  8004. (when (member sorting-type '(?k ?K)) (org-clock-sum))
  8005. (message "Sorting entries...")
  8006. (save-restriction
  8007. (narrow-to-region start end)
  8008. (let ((dcst (downcase sorting-type))
  8009. (case-fold-search nil)
  8010. (now (current-time)))
  8011. (sort-subr
  8012. (/= dcst sorting-type)
  8013. ;; This function moves to the beginning character of the "record" to
  8014. ;; be sorted.
  8015. (lambda nil
  8016. (if (re-search-forward re nil t)
  8017. (goto-char (match-beginning 0))
  8018. (goto-char (point-max))))
  8019. ;; This function moves to the last character of the "record" being
  8020. ;; sorted.
  8021. (lambda nil
  8022. (save-match-data
  8023. (condition-case nil
  8024. (outline-forward-same-level 1)
  8025. (error
  8026. (goto-char (point-max))))))
  8027. ;; This function returns the value that gets sorted against.
  8028. (lambda nil
  8029. (cond
  8030. ((= dcst ?n)
  8031. (if (looking-at org-complex-heading-regexp)
  8032. (string-to-number (org-sort-remove-invisible (match-string 4)))
  8033. nil))
  8034. ((= dcst ?a)
  8035. (if (looking-at org-complex-heading-regexp)
  8036. (funcall case-func (org-sort-remove-invisible (match-string 4)))
  8037. nil))
  8038. ((= dcst ?k)
  8039. (or (get-text-property (point) :org-clock-minutes) 0))
  8040. ((= dcst ?t)
  8041. (let ((end (save-excursion (outline-next-heading) (point))))
  8042. (if (or (re-search-forward org-ts-regexp end t)
  8043. (re-search-forward org-ts-regexp-both end t))
  8044. (org-time-string-to-seconds (match-string 0))
  8045. (org-float-time now))))
  8046. ((= dcst ?c)
  8047. (let ((end (save-excursion (outline-next-heading) (point))))
  8048. (if (re-search-forward
  8049. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  8050. end t)
  8051. (org-time-string-to-seconds (match-string 0))
  8052. (org-float-time now))))
  8053. ((= dcst ?s)
  8054. (let ((end (save-excursion (outline-next-heading) (point))))
  8055. (if (re-search-forward org-scheduled-time-regexp end t)
  8056. (org-time-string-to-seconds (match-string 1))
  8057. (org-float-time now))))
  8058. ((= dcst ?d)
  8059. (let ((end (save-excursion (outline-next-heading) (point))))
  8060. (if (re-search-forward org-deadline-time-regexp end t)
  8061. (org-time-string-to-seconds (match-string 1))
  8062. (org-float-time now))))
  8063. ((= dcst ?p)
  8064. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  8065. (string-to-char (match-string 2))
  8066. org-default-priority))
  8067. ((= dcst ?r)
  8068. (or (org-entry-get nil property) ""))
  8069. ((= dcst ?o)
  8070. (if (looking-at org-complex-heading-regexp)
  8071. (let* ((m (match-string 2))
  8072. (s (if (member m org-done-keywords) '- '+)))
  8073. (- 99 (funcall s (length (member m org-todo-keywords-1)))))))
  8074. ((= dcst ?f)
  8075. (if getkey-func
  8076. (progn
  8077. (setq tmp (funcall getkey-func))
  8078. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  8079. tmp)
  8080. (error "Invalid key function `%s'" getkey-func)))
  8081. (t (error "Invalid sorting type `%c'" sorting-type))))
  8082. nil
  8083. (cond
  8084. ((= dcst ?a) 'string<)
  8085. ((= dcst ?f) compare-func)
  8086. ((member dcst '(?p ?t ?s ?d ?c ?k)) '<)))))
  8087. (run-hooks 'org-after-sorting-entries-or-items-hook)
  8088. ;; Reset the clock marker if needed
  8089. (when cmstr
  8090. (save-excursion
  8091. (goto-char start)
  8092. (search-forward cmstr nil t)
  8093. (move-marker org-clock-marker (point))))
  8094. (message "Sorting entries...done")))
  8095. (defun org-do-sort (table what &optional with-case sorting-type)
  8096. "Sort TABLE of WHAT according to SORTING-TYPE.
  8097. The user will be prompted for the SORTING-TYPE if the call to this
  8098. function does not specify it.
  8099. WHAT is only for the prompt, to indicate what is being sorted.
  8100. The sorting key will be extracted from the car of the elements of
  8101. the table.
  8102. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  8103. (unless sorting-type
  8104. (message
  8105. "Sort %s: [a]lphabetic, [n]umeric, [t]ime. A/N/T means reversed:"
  8106. what)
  8107. (setq sorting-type (read-char-exclusive)))
  8108. (let ((dcst (downcase sorting-type))
  8109. extractfun comparefun)
  8110. ;; Define the appropriate functions
  8111. (cond
  8112. ((= dcst ?n)
  8113. (setq extractfun 'string-to-number
  8114. comparefun (if (= dcst sorting-type) '< '>)))
  8115. ((= dcst ?a)
  8116. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  8117. (lambda(x) (downcase (org-sort-remove-invisible x))))
  8118. comparefun (if (= dcst sorting-type)
  8119. 'string<
  8120. (lambda (a b) (and (not (string< a b))
  8121. (not (string= a b)))))))
  8122. ((= dcst ?t)
  8123. (setq extractfun
  8124. (lambda (x)
  8125. (cond ((or (string-match org-ts-regexp x)
  8126. (string-match org-ts-regexp-both x))
  8127. (org-float-time
  8128. (org-time-string-to-time (match-string 0 x))))
  8129. ((string-match "[0-9]\\{1,2\\}:[0-9]\\{2\\}" x)
  8130. (org-hh:mm-string-to-minutes x))
  8131. (t 0)))
  8132. comparefun (if (= dcst sorting-type) '< '>)))
  8133. (t (error "Invalid sorting type `%c'" sorting-type)))
  8134. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  8135. table)
  8136. (lambda (a b) (funcall comparefun (car a) (car b))))))
  8137. ;;; The orgstruct minor mode
  8138. ;; Define a minor mode which can be used in other modes in order to
  8139. ;; integrate the org-mode structure editing commands.
  8140. ;; This is really a hack, because the org-mode structure commands use
  8141. ;; keys which normally belong to the major mode. Here is how it
  8142. ;; works: The minor mode defines all the keys necessary to operate the
  8143. ;; structure commands, but wraps the commands into a function which
  8144. ;; tests if the cursor is currently at a headline or a plain list
  8145. ;; item. If that is the case, the structure command is used,
  8146. ;; temporarily setting many Org-mode variables like regular
  8147. ;; expressions for filling etc. However, when any of those keys is
  8148. ;; used at a different location, function uses `key-binding' to look
  8149. ;; up if the key has an associated command in another currently active
  8150. ;; keymap (minor modes, major mode, global), and executes that
  8151. ;; command. There might be problems if any of the keys is otherwise
  8152. ;; used as a prefix key.
  8153. (defcustom orgstruct-heading-prefix-regexp ""
  8154. "Regexp that matches the custom prefix of Org headlines in
  8155. orgstruct(++)-mode."
  8156. :group 'org
  8157. :version "24.4"
  8158. :package-version '(Org . "8.3")
  8159. :type 'regexp)
  8160. ;;;###autoload(put 'orgstruct-heading-prefix-regexp 'safe-local-variable 'stringp)
  8161. (defcustom orgstruct-setup-hook nil
  8162. "Hook run after orgstruct-mode-map is filled."
  8163. :group 'org
  8164. :version "24.4"
  8165. :package-version '(Org . "8.0")
  8166. :type 'hook)
  8167. (defvar orgstruct-initialized nil)
  8168. (defvar org-local-vars nil
  8169. "List of local variables, for use by `orgstruct-mode'.")
  8170. ;;;###autoload
  8171. (define-minor-mode orgstruct-mode
  8172. "Toggle the minor mode `orgstruct-mode'.
  8173. This mode is for using Org-mode structure commands in other
  8174. modes. The following keys behave as if Org-mode were active, if
  8175. the cursor is on a headline, or on a plain list item (both as
  8176. defined by Org-mode)."
  8177. nil " OrgStruct" (make-sparse-keymap)
  8178. (funcall (if orgstruct-mode
  8179. 'add-to-invisibility-spec
  8180. 'remove-from-invisibility-spec)
  8181. '(outline . t))
  8182. (when orgstruct-mode
  8183. (org-load-modules-maybe)
  8184. (unless orgstruct-initialized
  8185. (orgstruct-setup)
  8186. (setq orgstruct-initialized t))))
  8187. ;;;###autoload
  8188. (defun turn-on-orgstruct ()
  8189. "Unconditionally turn on `orgstruct-mode'."
  8190. (orgstruct-mode 1))
  8191. (defvar org-fb-vars nil)
  8192. (make-variable-buffer-local 'org-fb-vars)
  8193. (defun orgstruct++-mode (&optional arg)
  8194. "Toggle `orgstruct-mode', the enhanced version of it.
  8195. In addition to setting orgstruct-mode, this also exports all
  8196. indentation and autofilling variables from org-mode into the
  8197. buffer. It will also recognize item context in multiline items."
  8198. (interactive "P")
  8199. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  8200. (if (< arg 1)
  8201. (progn (orgstruct-mode -1)
  8202. (mapc (lambda(v)
  8203. (org-set-local (car v)
  8204. (if (eq (car-safe (cadr v)) 'quote) (cadadr v) (cadr v))))
  8205. org-fb-vars))
  8206. (orgstruct-mode 1)
  8207. (setq org-fb-vars nil)
  8208. (unless org-local-vars
  8209. (setq org-local-vars (org-get-local-variables)))
  8210. (let (var val)
  8211. (mapc
  8212. (lambda (x)
  8213. (when (string-match
  8214. "^\\(paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|fill-prefix\\|indent-\\)"
  8215. (symbol-name (car x)))
  8216. (setq var (car x) val (nth 1 x))
  8217. (push (list var `(quote ,(eval var))) org-fb-vars)
  8218. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  8219. org-local-vars)
  8220. (org-set-local 'orgstruct-is-++ t))))
  8221. (defvar orgstruct-is-++ nil
  8222. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  8223. (make-variable-buffer-local 'orgstruct-is-++)
  8224. ;;;###autoload
  8225. (defun turn-on-orgstruct++ ()
  8226. "Unconditionally turn on `orgstruct++-mode'."
  8227. (orgstruct++-mode 1))
  8228. (defun orgstruct-error ()
  8229. "Error when there is no default binding for a structure key."
  8230. (interactive)
  8231. (funcall (if (fboundp 'user-error)
  8232. 'user-error
  8233. 'error)
  8234. "This key has no function outside structure elements"))
  8235. (defun orgstruct-setup ()
  8236. "Setup orgstruct keymap."
  8237. (dolist (cell '((org-demote . t)
  8238. (org-metaleft . t)
  8239. (org-metaright . t)
  8240. (org-promote . t)
  8241. (org-shiftmetaleft . t)
  8242. (org-shiftmetaright . t)
  8243. org-backward-element
  8244. org-backward-heading-same-level
  8245. org-ctrl-c-ret
  8246. org-ctrl-c-minus
  8247. org-ctrl-c-star
  8248. org-cycle
  8249. org-forward-heading-same-level
  8250. org-insert-heading
  8251. org-insert-heading-respect-content
  8252. org-kill-note-or-show-branches
  8253. org-mark-subtree
  8254. org-meta-return
  8255. org-metadown
  8256. org-metaup
  8257. org-narrow-to-subtree
  8258. org-promote-subtree
  8259. org-reveal
  8260. org-shiftdown
  8261. org-shiftleft
  8262. org-shiftmetadown
  8263. org-shiftmetaup
  8264. org-shiftright
  8265. org-shifttab
  8266. org-shifttab
  8267. org-shiftup
  8268. org-show-subtree
  8269. org-sort
  8270. org-up-element
  8271. outline-demote
  8272. outline-next-visible-heading
  8273. outline-previous-visible-heading
  8274. outline-promote
  8275. outline-up-heading
  8276. show-children))
  8277. (let ((f (or (car-safe cell) cell))
  8278. (disable-when-heading-prefix (cdr-safe cell)))
  8279. (when (fboundp f)
  8280. (let ((new-bindings))
  8281. (dolist (binding (nconc (where-is-internal f org-mode-map)
  8282. (where-is-internal f outline-mode-map)))
  8283. (push binding new-bindings)
  8284. ;; TODO use local-function-key-map
  8285. (dolist (rep '(("<tab>" . "TAB")
  8286. ("<return>" . "RET")
  8287. ("<escape>" . "ESC")
  8288. ("<delete>" . "DEL")))
  8289. (setq binding (read-kbd-macro
  8290. (let ((case-fold-search))
  8291. (replace-regexp-in-string
  8292. (regexp-quote (cdr rep))
  8293. (car rep)
  8294. (key-description binding)))))
  8295. (pushnew binding new-bindings :test 'equal)))
  8296. (dolist (binding new-bindings)
  8297. (let ((key (lookup-key orgstruct-mode-map binding)))
  8298. (when (or (not key) (numberp key))
  8299. (ignore-errors
  8300. (org-defkey orgstruct-mode-map
  8301. binding
  8302. (orgstruct-make-binding
  8303. f binding disable-when-heading-prefix))))))))))
  8304. (run-hooks 'orgstruct-setup-hook))
  8305. (defun orgstruct-make-binding (fun key disable-when-heading-prefix)
  8306. "Create a function for binding in the structure minor mode.
  8307. FUN is the command to call inside a table. KEY is the key that
  8308. should be checked in for a command to execute outside of tables.
  8309. Non-nil `disable-when-heading-prefix' means to disable the command
  8310. if `orgstruct-heading-prefix-regexp' is not empty."
  8311. (let ((name (concat "orgstruct-hijacker-" (symbol-name fun))))
  8312. (let ((nname name)
  8313. (i 0))
  8314. (while (fboundp (intern nname))
  8315. (setq nname (format "%s-%d" name (setq i (1+ i)))))
  8316. (setq name (intern nname)))
  8317. (eval
  8318. (let ((bindings '((org-heading-regexp
  8319. (concat "^"
  8320. orgstruct-heading-prefix-regexp
  8321. "\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ ]*$"))
  8322. (org-outline-regexp
  8323. (concat orgstruct-heading-prefix-regexp "\\*+ "))
  8324. (org-outline-regexp-bol
  8325. (concat "^" org-outline-regexp))
  8326. (outline-regexp org-outline-regexp)
  8327. (outline-heading-end-regexp "\n")
  8328. (outline-level 'org-outline-level)
  8329. (outline-heading-alist))))
  8330. `(defun ,name (arg)
  8331. ,(concat "In Structure, run `" (symbol-name fun) "'.\n"
  8332. "Outside of structure, run the binding of `"
  8333. (key-description key) "'."
  8334. (when disable-when-heading-prefix
  8335. (concat
  8336. "\nIf `orgstruct-heading-prefix-regexp' is not empty, this command will always fall\n"
  8337. "back to the default binding due to limitations of Org's implementation of\n"
  8338. "`" (symbol-name fun) "'.")))
  8339. (interactive "p")
  8340. (let* ((disable
  8341. ,(and disable-when-heading-prefix
  8342. '(not (string= orgstruct-heading-prefix-regexp ""))))
  8343. (fallback
  8344. (or disable
  8345. (not
  8346. (let* ,bindings
  8347. (org-context-p 'headline 'item
  8348. ,(when (memq fun
  8349. '(org-insert-heading
  8350. org-insert-heading-respect-content
  8351. org-meta-return))
  8352. '(when orgstruct-is-++
  8353. 'item-body))))))))
  8354. (if fallback
  8355. (let* ((orgstruct-mode)
  8356. (binding
  8357. (let ((key ,key))
  8358. (catch 'exit
  8359. (dolist
  8360. (rep
  8361. '(nil
  8362. ("<\\([^>]*\\)tab>" . "\\1TAB")
  8363. ("<\\([^>]*\\)return>" . "\\1RET")
  8364. ("<\\([^>]*\\)escape>" . "\\1ESC")
  8365. ("<\\([^>]*\\)delete>" . "\\1DEL"))
  8366. nil)
  8367. (when rep
  8368. (setq key (read-kbd-macro
  8369. (let ((case-fold-search))
  8370. (replace-regexp-in-string
  8371. (car rep)
  8372. (cdr rep)
  8373. (key-description key))))))
  8374. (when (key-binding key)
  8375. (throw 'exit (key-binding key))))))))
  8376. (if (keymapp binding)
  8377. (org-set-transient-map binding)
  8378. (let ((func (or binding
  8379. (unless disable
  8380. 'orgstruct-error))))
  8381. (when func
  8382. (call-interactively func)))))
  8383. (org-run-like-in-org-mode
  8384. (lambda ()
  8385. (interactive)
  8386. (let* ,bindings
  8387. (call-interactively ',fun)))))))))
  8388. name))
  8389. (defun org-contextualize-keys (alist contexts)
  8390. "Return valid elements in ALIST depending on CONTEXTS.
  8391. `org-agenda-custom-commands' or `org-capture-templates' are the
  8392. values used for ALIST, and `org-agenda-custom-commands-contexts'
  8393. or `org-capture-templates-contexts' are the associated contexts
  8394. definitions."
  8395. (let ((contexts
  8396. ;; normalize contexts
  8397. (mapcar
  8398. (lambda(c) (cond ((listp (cadr c))
  8399. (list (car c) (car c) (cadr c)))
  8400. ((string= "" (cadr c))
  8401. (list (car c) (car c) (caddr c)))
  8402. (t c))) contexts))
  8403. (a alist) c r s)
  8404. ;; loop over all commands or templates
  8405. (while (setq c (pop a))
  8406. (let (vrules repl)
  8407. (cond
  8408. ((not (assoc (car c) contexts))
  8409. (push c r))
  8410. ((and (assoc (car c) contexts)
  8411. (setq vrules (org-contextualize-validate-key
  8412. (car c) contexts)))
  8413. (mapc (lambda (vr)
  8414. (when (not (equal (car vr) (cadr vr)))
  8415. (setq repl vr))) vrules)
  8416. (if (not repl) (push c r)
  8417. (push (cadr repl) s)
  8418. (push
  8419. (cons (car c)
  8420. (cdr (or (assoc (cadr repl) alist)
  8421. (error "Undefined key `%s' as contextual replacement for `%s'"
  8422. (cadr repl) (car c)))))
  8423. r))))))
  8424. ;; Return limited ALIST, possibly with keys modified, and deduplicated
  8425. (delq
  8426. nil
  8427. (delete-dups
  8428. (mapcar (lambda (x)
  8429. (let ((tpl (car x)))
  8430. (when (not (delq
  8431. nil
  8432. (mapcar (lambda(y)
  8433. (equal y tpl)) s))) x)))
  8434. (reverse r))))))
  8435. (defun org-contextualize-validate-key (key contexts)
  8436. "Check CONTEXTS for agenda or capture KEY."
  8437. (let (r rr res)
  8438. (while (setq r (pop contexts))
  8439. (mapc
  8440. (lambda (rr)
  8441. (when
  8442. (and (equal key (car r))
  8443. (if (functionp rr) (funcall rr)
  8444. (or (and (eq (car rr) 'in-file)
  8445. (buffer-file-name)
  8446. (string-match (cdr rr) (buffer-file-name)))
  8447. (and (eq (car rr) 'in-mode)
  8448. (string-match (cdr rr) (symbol-name major-mode)))
  8449. (and (eq (car rr) 'in-buffer)
  8450. (string-match (cdr rr) (buffer-name)))
  8451. (when (and (eq (car rr) 'not-in-file)
  8452. (buffer-file-name))
  8453. (not (string-match (cdr rr) (buffer-file-name))))
  8454. (when (eq (car rr) 'not-in-mode)
  8455. (not (string-match (cdr rr) (symbol-name major-mode))))
  8456. (when (eq (car rr) 'not-in-buffer)
  8457. (not (string-match (cdr rr) (buffer-name)))))))
  8458. (push r res)))
  8459. (car (last r))))
  8460. (delete-dups (delq nil res))))
  8461. (defun org-context-p (&rest contexts)
  8462. "Check if local context is any of CONTEXTS.
  8463. Possible values in the list of contexts are `table', `headline', and `item'."
  8464. (let ((pos (point)))
  8465. (goto-char (point-at-bol))
  8466. (prog1 (or (and (memq 'table contexts)
  8467. (looking-at "[ \t]*|"))
  8468. (and (memq 'headline contexts)
  8469. (looking-at org-outline-regexp))
  8470. (and (memq 'item contexts)
  8471. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  8472. (and (memq 'item-body contexts)
  8473. (org-in-item-p)))
  8474. (goto-char pos))))
  8475. (defun org-get-local-variables ()
  8476. "Return a list of all local variables in an Org mode buffer."
  8477. (let (varlist)
  8478. (with-current-buffer (get-buffer-create "*Org tmp*")
  8479. (erase-buffer)
  8480. (org-mode)
  8481. (setq varlist (buffer-local-variables)))
  8482. (kill-buffer "*Org tmp*")
  8483. (delq nil
  8484. (mapcar
  8485. (lambda (x)
  8486. (setq x
  8487. (if (symbolp x)
  8488. (list x)
  8489. (list (car x) (cdr x))))
  8490. (if (and (not (get (car x) 'org-state))
  8491. (string-match
  8492. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|indent-\\)"
  8493. (symbol-name (car x))))
  8494. x nil))
  8495. varlist))))
  8496. (defun org-clone-local-variables (from-buffer &optional regexp)
  8497. "Clone local variables from FROM-BUFFER.
  8498. Optional argument REGEXP selects variables to clone."
  8499. (mapc
  8500. (lambda (pair)
  8501. (and (symbolp (car pair))
  8502. (or (null regexp)
  8503. (string-match regexp (symbol-name (car pair))))
  8504. (set (make-local-variable (car pair))
  8505. (cdr pair))))
  8506. (buffer-local-variables from-buffer)))
  8507. ;;;###autoload
  8508. (defun org-run-like-in-org-mode (cmd)
  8509. "Run a command, pretending that the current buffer is in Org-mode.
  8510. This will temporarily bind local variables that are typically bound in
  8511. Org-mode to the values they have in Org-mode, and then interactively
  8512. call CMD."
  8513. (org-load-modules-maybe)
  8514. (unless org-local-vars
  8515. (setq org-local-vars (org-get-local-variables)))
  8516. (let (binds)
  8517. (dolist (var org-local-vars)
  8518. (when (or (not (boundp (car var)))
  8519. (eq (symbol-value (car var))
  8520. (default-value (car var))))
  8521. (push (list (car var) `(quote ,(cadr var))) binds)))
  8522. (eval `(let ,binds
  8523. (call-interactively (quote ,cmd))))))
  8524. (defun org-get-category (&optional pos force-refresh)
  8525. "Get the category applying to position POS."
  8526. (save-match-data
  8527. (if force-refresh (org-refresh-category-properties))
  8528. (let ((pos (or pos (point))))
  8529. (or (get-text-property pos 'org-category)
  8530. (progn (org-refresh-category-properties)
  8531. (get-text-property pos 'org-category))))))
  8532. ;;; Refresh properties
  8533. (defun org-refresh-properties (dprop tprop)
  8534. "Refresh buffer text properties.
  8535. DPROP is the drawer property and TPROP is either the
  8536. corresponding text property to set, or an alist with each element
  8537. being a text property (as a symbol) and a function to apply to
  8538. the value of the drawer property."
  8539. (let ((case-fold-search t)
  8540. (inhibit-read-only t))
  8541. (org-with-silent-modifications
  8542. (save-excursion
  8543. (save-restriction
  8544. (widen)
  8545. (goto-char (point-min))
  8546. (while (re-search-forward (concat "^[ \t]*:" dprop ": +\\(.*\\)[ \t]*$") nil t)
  8547. (org-refresh-property tprop (org-match-string-no-properties 1))))))))
  8548. (defun org-refresh-property (tprop p)
  8549. "Refresh the buffer text property TPROP from the drawer property P.
  8550. The refresh happens only for the current tree (not subtree)."
  8551. (save-excursion
  8552. (org-back-to-heading t)
  8553. ;; tprop is a text property symbol
  8554. (if (symbolp tprop)
  8555. (put-text-property
  8556. (point) (or (outline-next-heading) (point-max)) tprop p)
  8557. ;; tprop is an alist with (properties . function) elements
  8558. (mapc (lambda(al)
  8559. (save-excursion
  8560. (put-text-property
  8561. (point-at-bol) (or (outline-next-heading) (point-max))
  8562. (car al)
  8563. (funcall (cdr al) p))))
  8564. tprop))))
  8565. (defun org-refresh-category-properties ()
  8566. "Refresh category text properties in the buffer."
  8567. (let ((case-fold-search t)
  8568. (inhibit-read-only t)
  8569. (def-cat (cond
  8570. ((null org-category)
  8571. (if buffer-file-name
  8572. (file-name-sans-extension
  8573. (file-name-nondirectory buffer-file-name))
  8574. "???"))
  8575. ((symbolp org-category) (symbol-name org-category))
  8576. (t org-category)))
  8577. beg end cat pos optionp)
  8578. (org-with-silent-modifications
  8579. (save-excursion
  8580. (save-restriction
  8581. (widen)
  8582. (goto-char (point-min))
  8583. (put-text-property (point) (point-max) 'org-category def-cat)
  8584. (while (re-search-forward
  8585. "^[ \t]*\\(\\(?:#\\+\\|:\\)CATEGORY:\\)\\(.*\\)" nil t)
  8586. (setq pos (match-end 0)
  8587. optionp (equal (char-after (match-beginning 0)) ?#)
  8588. cat (org-trim (match-string 2)))
  8589. (if optionp
  8590. (setq beg (point-at-bol) end (point-max))
  8591. (org-back-to-heading t)
  8592. (setq beg (point) end (org-end-of-subtree t t)))
  8593. (put-text-property beg end 'org-category cat)
  8594. (goto-char pos)))))))
  8595. (defun org-refresh-stats-properties ()
  8596. "Refresh stats text properties in the buffer."
  8597. (let (stats)
  8598. (org-with-silent-modifications
  8599. (save-excursion
  8600. (save-restriction
  8601. (widen)
  8602. (goto-char (point-min))
  8603. (while (re-search-forward
  8604. (concat org-outline-regexp-bol ".*"
  8605. "\\(?:\\[\\([0-9]+\\)%\\|\\([0-9]+\\)/\\([0-9]+\\)\\]\\)")
  8606. nil t)
  8607. (setq stats (cond ((equal (match-string 3) "0") 0)
  8608. ((match-string 2)
  8609. (/ (* (string-to-number (match-string 2)) 100)
  8610. (string-to-number (match-string 3))))
  8611. (t (string-to-number (match-string 1)))))
  8612. (org-back-to-heading t)
  8613. (put-text-property (point) (progn (org-end-of-subtree t t) (point))
  8614. 'org-stats stats)))))))
  8615. (defun org-refresh-effort-properties ()
  8616. "Refresh effort properties"
  8617. (org-refresh-properties
  8618. org-effort-property
  8619. '((effort . identity)
  8620. (effort-minutes . org-duration-string-to-minutes))))
  8621. ;;;; Link Stuff
  8622. ;;; Link abbreviations
  8623. (defun org-link-expand-abbrev (link)
  8624. "Apply replacements as defined in `org-link-abbrev-alist'."
  8625. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  8626. (let* ((key (match-string 1 link))
  8627. (as (or (assoc key org-link-abbrev-alist-local)
  8628. (assoc key org-link-abbrev-alist)))
  8629. (tag (and (match-end 2) (match-string 3 link)))
  8630. rpl)
  8631. (if (not as)
  8632. link
  8633. (setq rpl (cdr as))
  8634. (cond
  8635. ((symbolp rpl) (funcall rpl tag))
  8636. ((string-match "%(\\([^)]+\\))" rpl)
  8637. (replace-match
  8638. (save-match-data
  8639. (funcall (intern-soft (match-string 1 rpl)) tag)) t t rpl))
  8640. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  8641. ((string-match "%h" rpl)
  8642. (replace-match (url-hexify-string (or tag "")) t t rpl))
  8643. (t (concat rpl tag)))))
  8644. link))
  8645. ;;; Storing and inserting links
  8646. (defvar org-insert-link-history nil
  8647. "Minibuffer history for links inserted with `org-insert-link'.")
  8648. (defvar org-stored-links nil
  8649. "Contains the links stored with `org-store-link'.")
  8650. (defvar org-store-link-plist nil
  8651. "Plist with info about the most recently link created with `org-store-link'.")
  8652. (defvar org-link-protocols nil
  8653. "Link protocols added to Org-mode using `org-add-link-type'.")
  8654. (defvar org-store-link-functions nil
  8655. "List of functions that are called to create and store a link.
  8656. Each function will be called in turn until one returns a non-nil
  8657. value. Each function should check if it is responsible for creating
  8658. this link (for example by looking at the major mode).
  8659. If not, it must exit and return nil.
  8660. If yes, it should return a non-nil value after a calling
  8661. `org-store-link-props' with a list of properties and values.
  8662. Special properties are:
  8663. :type The link prefix, like \"http\". This must be given.
  8664. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  8665. This is obligatory as well.
  8666. :description Optional default description for the second pair
  8667. of brackets in an Org-mode link. The user can still change
  8668. this when inserting this link into an Org-mode buffer.
  8669. In addition to these, any additional properties can be specified
  8670. and then used in capture templates.")
  8671. (defun org-add-link-type (type &optional follow export)
  8672. "Add TYPE to the list of `org-link-types'.
  8673. Re-compute all regular expressions depending on `org-link-types'
  8674. FOLLOW and EXPORT are two functions.
  8675. FOLLOW should take the link path as the single argument and do whatever
  8676. is necessary to follow the link, for example find a file or display
  8677. a mail message.
  8678. EXPORT should format the link path for export to one of the export formats.
  8679. It should be a function accepting three arguments:
  8680. path the path of the link, the text after the prefix (like \"http:\")
  8681. desc the description of the link, if any, or a description added by
  8682. org-export-normalize-links if there is none
  8683. format the export format, a symbol like `html' or `latex' or `ascii'..
  8684. The function may use the FORMAT information to return different values
  8685. depending on the format. The return value will be put literally into
  8686. the exported file. If the return value is nil, this means Org should
  8687. do what it normally does with links which do not have EXPORT defined.
  8688. Org mode has a built-in default for exporting links. If you are happy with
  8689. this default, there is no need to define an export function for the link
  8690. type. For a simple example of an export function, see `org-bbdb.el'."
  8691. (add-to-list 'org-link-types type t)
  8692. (org-make-link-regexps)
  8693. (org-element-update-syntax)
  8694. (if (assoc type org-link-protocols)
  8695. (setcdr (assoc type org-link-protocols) (list follow export))
  8696. (push (list type follow export) org-link-protocols)))
  8697. (defvar org-agenda-buffer-name) ; Defined in org-agenda.el
  8698. (defvar org-id-link-to-org-use-id) ; Defined in org-id.el
  8699. ;;;###autoload
  8700. (defun org-store-link (arg)
  8701. "\\<org-mode-map>Store an org-link to the current location.
  8702. This link is added to `org-stored-links' and can later be inserted
  8703. into an org-buffer with \\[org-insert-link].
  8704. For some link types, a prefix arg is interpreted.
  8705. For links to Usenet articles, arg negates `org-gnus-prefer-web-links'.
  8706. For file links, arg negates `org-context-in-file-links'.
  8707. A double prefix arg force skipping storing functions that are not
  8708. part of Org's core.
  8709. A triple prefix arg force storing a link for each line in the
  8710. active region."
  8711. (interactive "P")
  8712. (org-load-modules-maybe)
  8713. (if (and (equal arg '(64)) (org-region-active-p))
  8714. (save-excursion
  8715. (let ((end (region-end)))
  8716. (goto-char (region-beginning))
  8717. (set-mark (point))
  8718. (while (< (point-at-eol) end)
  8719. (move-end-of-line 1) (activate-mark)
  8720. (let (current-prefix-arg)
  8721. (call-interactively 'org-store-link))
  8722. (move-beginning-of-line 2)
  8723. (set-mark (point)))))
  8724. (org-with-limited-levels
  8725. (setq org-store-link-plist nil)
  8726. (let (link cpltxt desc description search
  8727. txt custom-id agenda-link sfuns sfunsn)
  8728. (cond
  8729. ;; Store a link using an external link type
  8730. ((and (not (equal arg '(16)))
  8731. (setq sfuns
  8732. (delq
  8733. nil (mapcar (lambda (f)
  8734. (let (fs) (if (funcall f) (push f fs))))
  8735. org-store-link-functions))
  8736. sfunsn (mapcar (lambda (fu) (symbol-name (car fu))) sfuns))
  8737. (or (and (cdr sfuns)
  8738. (funcall (intern
  8739. (completing-read
  8740. "Which function for creating the link? "
  8741. sfunsn nil t (car sfunsn)))))
  8742. (funcall (caar sfuns)))
  8743. (setq link (plist-get org-store-link-plist :link)
  8744. desc (or (plist-get org-store-link-plist
  8745. :description) link))))
  8746. ;; Store a link from a source code buffer
  8747. ((org-src-edit-buffer-p)
  8748. (let (label gc)
  8749. (while (or (not label)
  8750. (save-excursion
  8751. (save-restriction
  8752. (widen)
  8753. (goto-char (point-min))
  8754. (re-search-forward
  8755. (regexp-quote (format org-coderef-label-format label))
  8756. nil t))))
  8757. (when label (message "Label exists already") (sit-for 2))
  8758. (setq label (read-string "Code line label: " label)))
  8759. (end-of-line 1)
  8760. (setq link (format org-coderef-label-format label))
  8761. (setq gc (- 79 (length link)))
  8762. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  8763. (insert link)
  8764. (setq link (concat "(" label ")") desc nil)))
  8765. ;; We are in the agenda, link to referenced location
  8766. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  8767. (let ((m (or (get-text-property (point) 'org-hd-marker)
  8768. (get-text-property (point) 'org-marker))))
  8769. (when m
  8770. (org-with-point-at m
  8771. (setq agenda-link
  8772. (if (org-called-interactively-p 'any)
  8773. (call-interactively 'org-store-link)
  8774. (org-store-link nil)))))))
  8775. ((eq major-mode 'calendar-mode)
  8776. (let ((cd (calendar-cursor-to-date)))
  8777. (setq link
  8778. (format-time-string
  8779. (car org-time-stamp-formats)
  8780. (apply 'encode-time
  8781. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  8782. nil nil nil))))
  8783. (org-store-link-props :type "calendar" :date cd)))
  8784. ((eq major-mode 'help-mode)
  8785. (setq link (concat "help:" (save-excursion
  8786. (goto-char (point-min))
  8787. (looking-at "^[^ ]+")
  8788. (match-string 0))))
  8789. (org-store-link-props :type "help"))
  8790. ((eq major-mode 'w3-mode)
  8791. (setq cpltxt (if (and (buffer-name)
  8792. (not (string-match "Untitled" (buffer-name))))
  8793. (buffer-name)
  8794. (url-view-url t))
  8795. link (url-view-url t))
  8796. (org-store-link-props :type "w3" :url (url-view-url t)))
  8797. ((eq major-mode 'image-mode)
  8798. (setq cpltxt (concat "file:"
  8799. (abbreviate-file-name buffer-file-name))
  8800. link cpltxt)
  8801. (org-store-link-props :type "image" :file buffer-file-name))
  8802. ;; In dired, store a link to the file of the current line
  8803. ((derived-mode-p 'dired-mode)
  8804. (let ((file (dired-get-filename nil t)))
  8805. (setq file (if file
  8806. (abbreviate-file-name
  8807. (expand-file-name (dired-get-filename nil t)))
  8808. ;; otherwise, no file so use current directory.
  8809. default-directory))
  8810. (setq cpltxt (concat "file:" file)
  8811. link cpltxt)))
  8812. ((setq search (run-hook-with-args-until-success
  8813. 'org-create-file-search-functions))
  8814. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  8815. "::" search))
  8816. (setq cpltxt (or description link)))
  8817. ((and (buffer-file-name (buffer-base-buffer)) (derived-mode-p 'org-mode))
  8818. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  8819. (cond
  8820. ;; Store a link using the target at point
  8821. ((org-in-regexp "[^<]<<\\([^<>]+\\)>>[^>]" 1)
  8822. (setq cpltxt
  8823. (concat "file:"
  8824. (abbreviate-file-name
  8825. (buffer-file-name (buffer-base-buffer)))
  8826. "::" (match-string 1))
  8827. link cpltxt))
  8828. ((and (featurep 'org-id)
  8829. (or (eq org-id-link-to-org-use-id t)
  8830. (and (org-called-interactively-p 'any)
  8831. (or (eq org-id-link-to-org-use-id 'create-if-interactive)
  8832. (and (eq org-id-link-to-org-use-id
  8833. 'create-if-interactive-and-no-custom-id)
  8834. (not custom-id))))
  8835. (and org-id-link-to-org-use-id (org-entry-get nil "ID"))))
  8836. ;; Store a link using the ID at point
  8837. (setq link (condition-case nil
  8838. (prog1 (org-id-store-link)
  8839. (setq desc (or (plist-get org-store-link-plist
  8840. :description)
  8841. "")))
  8842. (error
  8843. ;; Probably before first headline, link only to file
  8844. (concat "file:"
  8845. (abbreviate-file-name
  8846. (buffer-file-name (buffer-base-buffer))))))))
  8847. (t
  8848. ;; Just link to current headline
  8849. (setq cpltxt (concat "file:"
  8850. (abbreviate-file-name
  8851. (buffer-file-name (buffer-base-buffer)))))
  8852. ;; Add a context search string
  8853. (when (org-xor org-context-in-file-links arg)
  8854. (let* ((ee (org-element-at-point))
  8855. (et (org-element-type ee))
  8856. (ev (plist-get (cadr ee) :value))
  8857. (ek (plist-get (cadr ee) :key))
  8858. (eok (and (stringp ek) (string-match "name" ek))))
  8859. (setq txt (cond
  8860. ((org-at-heading-p) nil)
  8861. ((and (eq et 'keyword) eok) ev)
  8862. ((org-region-active-p)
  8863. (buffer-substring (region-beginning) (region-end)))))
  8864. (when (or (null txt) (string-match "\\S-" txt))
  8865. (setq cpltxt
  8866. (concat cpltxt "::"
  8867. (condition-case nil
  8868. (org-make-org-heading-search-string txt)
  8869. (error "")))
  8870. desc (or (and (eq et 'keyword) eok ev)
  8871. (nth 4 (ignore-errors (org-heading-components)))
  8872. "NONE")))))
  8873. (if (string-match "::\\'" cpltxt)
  8874. (setq cpltxt (substring cpltxt 0 -2)))
  8875. (setq link cpltxt))))
  8876. ((buffer-file-name (buffer-base-buffer))
  8877. ;; Just link to this file here.
  8878. (setq cpltxt (concat "file:"
  8879. (abbreviate-file-name
  8880. (buffer-file-name (buffer-base-buffer)))))
  8881. ;; Add a context string.
  8882. (when (org-xor org-context-in-file-links arg)
  8883. (setq txt (if (org-region-active-p)
  8884. (buffer-substring (region-beginning) (region-end))
  8885. (buffer-substring (point-at-bol) (point-at-eol))))
  8886. ;; Only use search option if there is some text.
  8887. (when (string-match "\\S-" txt)
  8888. (setq cpltxt
  8889. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  8890. desc "NONE")))
  8891. (setq link cpltxt))
  8892. ((org-called-interactively-p 'interactive)
  8893. (user-error "No method for storing a link from this buffer"))
  8894. (t (setq link nil)))
  8895. ;; We're done setting link and desc, clean up
  8896. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  8897. (setq link (or link cpltxt)
  8898. desc (or desc cpltxt))
  8899. (cond ((equal desc "NONE") (setq desc nil))
  8900. ((and desc (string-match org-bracket-link-analytic-regexp desc))
  8901. (let ((d0 (match-string 3 desc))
  8902. (p0 (match-string 5 desc)))
  8903. (setq desc
  8904. (replace-regexp-in-string
  8905. org-bracket-link-regexp
  8906. (concat (or p0 d0)
  8907. (if (equal (length (match-string 0 desc))
  8908. (length desc)) "*" "")) desc)))))
  8909. ;; Return the link
  8910. (if (not (and (or (org-called-interactively-p 'any)
  8911. executing-kbd-macro) link))
  8912. (or agenda-link (and link (org-make-link-string link desc)))
  8913. (push (list link desc) org-stored-links)
  8914. (message "Stored: %s" (or desc link))
  8915. (when custom-id
  8916. (setq link (concat "file:" (abbreviate-file-name
  8917. (buffer-file-name)) "::#" custom-id))
  8918. (push (list link desc) org-stored-links))
  8919. (car org-stored-links))))))
  8920. (defun org-store-link-props (&rest plist)
  8921. "Store link properties, extract names and addresses."
  8922. (let (x adr)
  8923. (when (setq x (plist-get plist :from))
  8924. (setq adr (mail-extract-address-components x))
  8925. (setq plist (plist-put plist :fromname (car adr)))
  8926. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  8927. (when (setq x (plist-get plist :to))
  8928. (setq adr (mail-extract-address-components x))
  8929. (setq plist (plist-put plist :toname (car adr)))
  8930. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  8931. (let ((from (plist-get plist :from))
  8932. (to (plist-get plist :to)))
  8933. (when (and from to org-from-is-user-regexp)
  8934. (setq plist
  8935. (plist-put plist :fromto
  8936. (if (string-match org-from-is-user-regexp from)
  8937. (concat "to %t")
  8938. (concat "from %f"))))))
  8939. (setq org-store-link-plist plist))
  8940. (defun org-add-link-props (&rest plist)
  8941. "Add these properties to the link property list."
  8942. (let (key value)
  8943. (while plist
  8944. (setq key (pop plist) value (pop plist))
  8945. (setq org-store-link-plist
  8946. (plist-put org-store-link-plist key value)))))
  8947. (defun org-email-link-description (&optional fmt)
  8948. "Return the description part of an email link.
  8949. This takes information from `org-store-link-plist' and formats it
  8950. according to FMT (default from `org-email-link-description-format')."
  8951. (setq fmt (or fmt org-email-link-description-format))
  8952. (let* ((p org-store-link-plist)
  8953. (to (plist-get p :toaddress))
  8954. (from (plist-get p :fromaddress))
  8955. (table
  8956. (list
  8957. (cons "%c" (plist-get p :fromto))
  8958. (cons "%F" (plist-get p :from))
  8959. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  8960. (cons "%T" (plist-get p :to))
  8961. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  8962. (cons "%s" (plist-get p :subject))
  8963. (cons "%d" (plist-get p :date))
  8964. (cons "%m" (plist-get p :message-id)))))
  8965. (when (string-match "%c" fmt)
  8966. ;; Check if the user wrote this message
  8967. (if (and org-from-is-user-regexp from to
  8968. (save-match-data (string-match org-from-is-user-regexp from)))
  8969. (setq fmt (replace-match "to %t" t t fmt))
  8970. (setq fmt (replace-match "from %f" t t fmt))))
  8971. (org-replace-escapes fmt table)))
  8972. (defun org-make-org-heading-search-string (&optional string)
  8973. "Make search string for the current headline or STRING."
  8974. (let ((s (or string
  8975. (and (derived-mode-p 'org-mode)
  8976. (save-excursion
  8977. (org-back-to-heading t)
  8978. (org-element-property :raw-value (org-element-at-point))))))
  8979. (lines org-context-in-file-links))
  8980. (or string (setq s (concat "*" s))) ; Add * for headlines
  8981. (setq s (replace-regexp-in-string "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" "" s))
  8982. (when (and string (integerp lines) (> lines 0))
  8983. (let ((slines (org-split-string s "\n")))
  8984. (when (< lines (length slines))
  8985. (setq s (mapconcat
  8986. 'identity
  8987. (reverse (nthcdr (- (length slines) lines)
  8988. (reverse slines))) "\n")))))
  8989. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  8990. (defun org-make-link-string (link &optional description)
  8991. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  8992. (unless (string-match "\\S-" link)
  8993. (error "Empty link"))
  8994. (when (and description
  8995. (stringp description)
  8996. (not (string-match "\\S-" description)))
  8997. (setq description nil))
  8998. (when (stringp description)
  8999. ;; Remove brackets from the description, they are fatal.
  9000. (while (string-match "\\[" description)
  9001. (setq description (replace-match "{" t t description)))
  9002. (while (string-match "\\]" description)
  9003. (setq description (replace-match "}" t t description))))
  9004. (when (equal link description)
  9005. ;; No description needed, it is identical
  9006. (setq description nil))
  9007. (when (and (not description)
  9008. (not (string-match (org-image-file-name-regexp) link))
  9009. (not (equal link (org-link-escape link))))
  9010. (setq description (org-extract-attributes link)))
  9011. (setq link
  9012. (cond ((string-match (org-image-file-name-regexp) link) link)
  9013. ((string-match org-link-types-re link)
  9014. (concat (match-string 1 link)
  9015. (org-link-escape (substring link (match-end 1)))))
  9016. (t (org-link-escape link))))
  9017. (concat "[[" link "]"
  9018. (if description (concat "[" description "]") "")
  9019. "]"))
  9020. (defconst org-link-escape-chars
  9021. ;;%20 %5B %5D
  9022. '(?\ ?\[ ?\])
  9023. "List of characters that should be escaped in a link when stored to Org.
  9024. This is the list that is used for internal purposes.")
  9025. (defconst org-link-escape-chars-browser
  9026. ;;%20 %22
  9027. '(?\ ?\")
  9028. "List of characters to be escaped before handing over to the browser.
  9029. If you consider using this constant then you probably want to use
  9030. the function `org-link-escape-browser' instead. See there why
  9031. this constant is a candidate to be removed once Org drops support
  9032. for Emacs 24.1 and 24.2.")
  9033. (defun org-link-escape (text &optional table merge)
  9034. "Return percent escaped representation of TEXT.
  9035. TEXT is a string with the text to escape.
  9036. Optional argument TABLE is a list with characters that should be
  9037. escaped. When nil, `org-link-escape-chars' is used.
  9038. If optional argument MERGE is set, merge TABLE into
  9039. `org-link-escape-chars'."
  9040. ;; Don't escape chars in internal links
  9041. (if (string-match "^\\*[[:alnum:]]+" text)
  9042. text
  9043. (cond
  9044. ((and table merge)
  9045. (mapc (lambda (defchr)
  9046. (unless (member defchr table)
  9047. (setq table (cons defchr table))))
  9048. org-link-escape-chars))
  9049. ((null table)
  9050. (setq table org-link-escape-chars)))
  9051. (mapconcat
  9052. (lambda (char)
  9053. (if (or (member char table)
  9054. (and (or (< char 32) (= char ?\%) (> char 126))
  9055. org-url-hexify-p))
  9056. (mapconcat (lambda (sequence-element)
  9057. (format "%%%.2X" sequence-element))
  9058. (or (encode-coding-char char 'utf-8)
  9059. (error "Unable to percent escape character: %s"
  9060. (char-to-string char))) "")
  9061. (char-to-string char))) text "")))
  9062. (defun org-link-escape-browser (text)
  9063. "Escape some characters before handing over to the browser.
  9064. This function is a candidate to be removed together with the
  9065. constant `org-link-escape-chars-browser' once Org drops support
  9066. for Emacs 24.1 and 24.2. All calls to this function will have to
  9067. be replaced with `url-encode-url' which is available since Emacs
  9068. 24.3.1."
  9069. ;; Example with the Org link
  9070. ;; [[http://lists.gnu.org/archive/cgi-bin/namazu.cgi?idxname=emacs-orgmode&query=%252Bsubject:"Release+8.2"]]
  9071. ;; to open the browser with +subject:"Release 8.2" filled into the
  9072. ;; query field: In this case the variable TEXT contains the
  9073. ;; unescaped [...]=%2Bsubject:"Release+8.2". Then `url-encode-url'
  9074. ;; converts correctly to [...]=%2Bsubject:%22Release+8.2%22 or
  9075. ;; `org-link-escape' with `org-link-escape-chars-browser' converts
  9076. ;; wrongly to [...]=%252Bsubject:%22Release+8.2%22.
  9077. (if (fboundp 'url-encode-url)
  9078. (url-encode-url text)
  9079. (if (org-string-match-p
  9080. (concat "[[:nonascii:]" org-link-escape-chars-browser "]")
  9081. text)
  9082. (org-link-escape text org-link-escape-chars-browser)
  9083. text)))
  9084. (defun org-link-unescape (str)
  9085. "Unhex hexified Unicode strings as returned from the JavaScript function
  9086. encodeURIComponent. E.g. `%C3%B6' is the german o-Umlaut."
  9087. (unless (and (null str) (string= "" str))
  9088. (let ((pos 0) (case-fold-search t) unhexed)
  9089. (while (setq pos (string-match "\\(%[0-9a-f][0-9a-f]\\)+" str pos))
  9090. (setq unhexed (org-link-unescape-compound (match-string 0 str)))
  9091. (setq str (replace-match unhexed t t str))
  9092. (setq pos (+ pos (length unhexed))))))
  9093. str)
  9094. (defun org-link-unescape-compound (hex)
  9095. "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German o-Umlaut.
  9096. Note: this function also decodes single byte encodings like
  9097. `%E1' (a-acute) if not followed by another `%[A-F0-9]{2}' group."
  9098. (save-match-data
  9099. (let* ((bytes (cdr (split-string hex "%")))
  9100. (ret "")
  9101. (eat 0)
  9102. (sum 0))
  9103. (while bytes
  9104. (let* ((val (string-to-number (pop bytes) 16))
  9105. (shift-xor
  9106. (if (= 0 eat)
  9107. (cond
  9108. ((>= val 252) (cons 6 252))
  9109. ((>= val 248) (cons 5 248))
  9110. ((>= val 240) (cons 4 240))
  9111. ((>= val 224) (cons 3 224))
  9112. ((>= val 192) (cons 2 192))
  9113. (t (cons 0 0)))
  9114. (cons 6 128))))
  9115. (if (>= val 192) (setq eat (car shift-xor)))
  9116. (setq val (logxor val (cdr shift-xor)))
  9117. (setq sum (+ (lsh sum (car shift-xor)) val))
  9118. (if (> eat 0) (setq eat (- eat 1)))
  9119. (cond
  9120. ((= 0 eat) ;multi byte
  9121. (setq ret (concat ret (org-char-to-string sum)))
  9122. (setq sum 0))
  9123. ((not bytes) ; single byte(s)
  9124. (setq ret (org-link-unescape-single-byte-sequence hex))))))
  9125. ret)))
  9126. (defun org-link-unescape-single-byte-sequence (hex)
  9127. "Unhexify hex-encoded single byte character sequences."
  9128. (mapconcat (lambda (byte)
  9129. (char-to-string (string-to-number byte 16)))
  9130. (cdr (split-string hex "%")) ""))
  9131. (defun org-xor (a b)
  9132. "Exclusive or."
  9133. (if a (not b) b))
  9134. (defun org-fixup-message-id-for-http (s)
  9135. "Replace special characters in a message id, so it can be used in an http query."
  9136. (when (string-match "%" s)
  9137. (setq s (mapconcat (lambda (c)
  9138. (if (eq c ?%)
  9139. "%25"
  9140. (char-to-string c)))
  9141. s "")))
  9142. (while (string-match "<" s)
  9143. (setq s (replace-match "%3C" t t s)))
  9144. (while (string-match ">" s)
  9145. (setq s (replace-match "%3E" t t s)))
  9146. (while (string-match "@" s)
  9147. (setq s (replace-match "%40" t t s)))
  9148. s)
  9149. (defun org-link-prettify (link)
  9150. "Return a human-readable representation of LINK.
  9151. The car of LINK must be a raw link.
  9152. The cdr of LINK must be either a link description or nil."
  9153. (let ((desc (or (cadr link) "<no description>")))
  9154. (concat (format "%-45s" (substring desc 0 (min (length desc) 40)))
  9155. "<" (car link) ">")))
  9156. ;;;###autoload
  9157. (defun org-insert-link-global ()
  9158. "Insert a link like Org-mode does.
  9159. This command can be called in any mode to insert a link in Org-mode syntax."
  9160. (interactive)
  9161. (org-load-modules-maybe)
  9162. (org-run-like-in-org-mode 'org-insert-link))
  9163. (defun org-insert-all-links (arg &optional pre post)
  9164. "Insert all links in `org-stored-links'.
  9165. When a universal prefix, do not delete the links from `org-stored-links'.
  9166. When `ARG' is a number, insert the last N link(s).
  9167. `PRE' and `POST' are optional arguments to define a string to
  9168. prepend or to append."
  9169. (interactive "P")
  9170. (let ((org-keep-stored-link-after-insertion (equal arg '(4)))
  9171. (links (copy-seq org-stored-links))
  9172. (pr (or pre "- "))
  9173. (po (or post "\n"))
  9174. (cnt 1) l)
  9175. (if (null org-stored-links)
  9176. (message "No link to insert")
  9177. (while (and (or (listp arg) (>= arg cnt))
  9178. (setq l (if (listp arg)
  9179. (pop links)
  9180. (pop org-stored-links))))
  9181. (setq cnt (1+ cnt))
  9182. (insert pr)
  9183. (org-insert-link nil (car l) (or (cadr l) "<no description>"))
  9184. (insert po)))))
  9185. (defun org-insert-last-stored-link (arg)
  9186. "Insert the last link stored in `org-stored-links'."
  9187. (interactive "p")
  9188. (org-insert-all-links arg "" "\n"))
  9189. (defun org-link-fontify-links-to-this-file ()
  9190. "Fontify links to the current file in `org-stored-links'."
  9191. (let ((f (buffer-file-name)) a b)
  9192. (setq a (mapcar (lambda(l)
  9193. (let ((ll (car l)))
  9194. (when (and (string-match "^file:\\(.+\\)::" ll)
  9195. (equal f (expand-file-name (match-string 1 ll))))
  9196. ll)))
  9197. org-stored-links))
  9198. (when (featurep 'org-id)
  9199. (setq b (mapcar (lambda(l)
  9200. (let ((ll (car l)))
  9201. (when (and (string-match "^id:\\(.+\\)$" ll)
  9202. (equal f (expand-file-name
  9203. (or (org-id-find-id-file
  9204. (match-string 1 ll)) ""))))
  9205. ll)))
  9206. org-stored-links)))
  9207. (mapcar (lambda(l)
  9208. (put-text-property 0 (length l) 'face 'font-lock-comment-face l))
  9209. (delq nil (append a b)))))
  9210. (defvar org-link-links-in-this-file nil)
  9211. (defun org-insert-link (&optional complete-file link-location default-description)
  9212. "Insert a link. At the prompt, enter the link.
  9213. Completion can be used to insert any of the link protocol prefixes like
  9214. http or ftp in use.
  9215. The history can be used to select a link previously stored with
  9216. `org-store-link'. When the empty string is entered (i.e. if you just
  9217. press RET at the prompt), the link defaults to the most recently
  9218. stored link. As SPC triggers completion in the minibuffer, you need to
  9219. use M-SPC or C-q SPC to force the insertion of a space character.
  9220. You will also be prompted for a description, and if one is given, it will
  9221. be displayed in the buffer instead of the link.
  9222. If there is already a link at point, this command will allow you to edit link
  9223. and description parts.
  9224. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  9225. be selected using completion. The path to the file will be relative to the
  9226. current directory if the file is in the current directory or a subdirectory.
  9227. Otherwise, the link will be the absolute path as completed in the minibuffer
  9228. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  9229. option `org-link-file-path-type'.
  9230. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  9231. the current directory or below.
  9232. With three \\[universal-argument] prefixes, negate the meaning of
  9233. `org-keep-stored-link-after-insertion'.
  9234. If `org-make-link-description-function' is non-nil, this function will be
  9235. called with the link target, and the result will be the default
  9236. link description.
  9237. If the LINK-LOCATION parameter is non-nil, this value will be
  9238. used as the link location instead of reading one interactively.
  9239. If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
  9240. be used as the default description."
  9241. (interactive "P")
  9242. (let* ((wcf (current-window-configuration))
  9243. (origbuf (current-buffer))
  9244. (region (if (org-region-active-p)
  9245. (buffer-substring (region-beginning) (region-end))))
  9246. (remove (and region (list (region-beginning) (region-end))))
  9247. (desc region)
  9248. tmphist ; byte-compile incorrectly complains about this
  9249. (link link-location)
  9250. (abbrevs org-link-abbrev-alist-local)
  9251. entry file all-prefixes auto-desc)
  9252. (cond
  9253. (link-location) ; specified by arg, just use it.
  9254. ((org-in-regexp org-bracket-link-regexp 1)
  9255. ;; We do have a link at point, and we are going to edit it.
  9256. (setq remove (list (match-beginning 0) (match-end 0)))
  9257. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  9258. (setq link (read-string "Link: "
  9259. (org-link-unescape
  9260. (org-match-string-no-properties 1)))))
  9261. ((or (org-in-regexp org-angle-link-re)
  9262. (org-in-regexp org-plain-link-re))
  9263. ;; Convert to bracket link
  9264. (setq remove (list (match-beginning 0) (match-end 0))
  9265. link (read-string "Link: "
  9266. (org-remove-angle-brackets (match-string 0)))))
  9267. ((member complete-file '((4) (16)))
  9268. ;; Completing read for file names.
  9269. (setq link (org-file-complete-link complete-file)))
  9270. (t
  9271. ;; Read link, with completion for stored links.
  9272. (org-link-fontify-links-to-this-file)
  9273. (org-switch-to-buffer-other-window "*Org Links*")
  9274. (with-current-buffer "*Org Links*"
  9275. (erase-buffer)
  9276. (insert "Insert a link.
  9277. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  9278. (when org-stored-links
  9279. (insert "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  9280. (insert (mapconcat 'org-link-prettify
  9281. (reverse org-stored-links) "\n")))
  9282. (goto-char (point-min)))
  9283. (let ((cw (selected-window)))
  9284. (select-window (get-buffer-window "*Org Links*" 'visible))
  9285. (with-current-buffer "*Org Links*" (setq truncate-lines t))
  9286. (unless (pos-visible-in-window-p (point-max))
  9287. (org-fit-window-to-buffer))
  9288. (and (window-live-p cw) (select-window cw)))
  9289. ;; Fake a link history, containing the stored links.
  9290. (setq tmphist (append (mapcar 'car org-stored-links)
  9291. org-insert-link-history))
  9292. (setq all-prefixes (append (mapcar 'car abbrevs)
  9293. (mapcar 'car org-link-abbrev-alist)
  9294. org-link-types))
  9295. (unwind-protect
  9296. (progn
  9297. (setq link
  9298. (org-completing-read
  9299. "Link: "
  9300. (append
  9301. (mapcar (lambda (x) (concat x ":"))
  9302. all-prefixes)
  9303. (mapcar 'car org-stored-links))
  9304. nil nil nil
  9305. 'tmphist
  9306. (caar org-stored-links)))
  9307. (if (not (string-match "\\S-" link))
  9308. (user-error "No link selected"))
  9309. (mapc (lambda(l)
  9310. (when (equal link (cadr l)) (setq link (car l) auto-desc t)))
  9311. org-stored-links)
  9312. (if (or (member link all-prefixes)
  9313. (and (equal ":" (substring link -1))
  9314. (member (substring link 0 -1) all-prefixes)
  9315. (setq link (substring link 0 -1))))
  9316. (setq link (with-current-buffer origbuf
  9317. (org-link-try-special-completion link)))))
  9318. (set-window-configuration wcf)
  9319. (kill-buffer "*Org Links*"))
  9320. (setq entry (assoc link org-stored-links))
  9321. (or entry (push link org-insert-link-history))
  9322. (setq desc (or desc (nth 1 entry)))))
  9323. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  9324. (not org-keep-stored-link-after-insertion))
  9325. (setq org-stored-links (delq (assoc link org-stored-links)
  9326. org-stored-links)))
  9327. (if (and (string-match org-plain-link-re link)
  9328. (not (string-match org-ts-regexp link)))
  9329. ;; URL-like link, normalize the use of angular brackets.
  9330. (setq link (org-remove-angle-brackets link)))
  9331. ;; Check if we are linking to the current file with a search
  9332. ;; option If yes, simplify the link by using only the search
  9333. ;; option.
  9334. (when (and buffer-file-name
  9335. (string-match "^file:\\(.+?\\)::\\(.+\\)" link))
  9336. (let* ((path (match-string 1 link))
  9337. (case-fold-search nil)
  9338. (search (match-string 2 link)))
  9339. (save-match-data
  9340. (if (equal (file-truename buffer-file-name) (file-truename path))
  9341. ;; We are linking to this same file, with a search option
  9342. (setq link search)))))
  9343. ;; Check if we can/should use a relative path. If yes, simplify the link
  9344. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  9345. (let* ((type (match-string 1 link))
  9346. (path (match-string 2 link))
  9347. (origpath path)
  9348. (case-fold-search nil))
  9349. (cond
  9350. ((or (eq org-link-file-path-type 'absolute)
  9351. (equal complete-file '(16)))
  9352. (setq path (abbreviate-file-name (expand-file-name path))))
  9353. ((eq org-link-file-path-type 'noabbrev)
  9354. (setq path (expand-file-name path)))
  9355. ((eq org-link-file-path-type 'relative)
  9356. (setq path (file-relative-name path)))
  9357. (t
  9358. (save-match-data
  9359. (if (string-match (concat "^" (regexp-quote
  9360. (expand-file-name
  9361. (file-name-as-directory
  9362. default-directory))))
  9363. (expand-file-name path))
  9364. ;; We are linking a file with relative path name.
  9365. (setq path (substring (expand-file-name path)
  9366. (match-end 0)))
  9367. (setq path (abbreviate-file-name (expand-file-name path)))))))
  9368. (setq link (concat type path))
  9369. (if (equal desc origpath)
  9370. (setq desc path))))
  9371. (if org-make-link-description-function
  9372. (setq desc
  9373. (or (condition-case nil
  9374. (funcall org-make-link-description-function link desc)
  9375. (error (progn (message "Can't get link description from `%s'"
  9376. (symbol-name org-make-link-description-function))
  9377. (sit-for 2) nil)))
  9378. (read-string "Description: " default-description)))
  9379. (if default-description (setq desc default-description)
  9380. (setq desc (or (and auto-desc desc)
  9381. (read-string "Description: " desc)))))
  9382. (unless (string-match "\\S-" desc) (setq desc nil))
  9383. (if remove (apply 'delete-region remove))
  9384. (insert (org-make-link-string link desc))))
  9385. (defun org-link-try-special-completion (type)
  9386. "If there is completion support for link type TYPE, offer it."
  9387. (let ((fun (intern (concat "org-" type "-complete-link"))))
  9388. (if (functionp fun)
  9389. (funcall fun)
  9390. (read-string "Link (no completion support): " (concat type ":")))))
  9391. (defun org-file-complete-link (&optional arg)
  9392. "Create a file link using completion."
  9393. (let (file link)
  9394. (setq file (org-iread-file-name "File: "))
  9395. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  9396. (pwd1 (file-name-as-directory (abbreviate-file-name
  9397. (expand-file-name ".")))))
  9398. (cond
  9399. ((equal arg '(16))
  9400. (setq link (concat
  9401. "file:"
  9402. (abbreviate-file-name (expand-file-name file)))))
  9403. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  9404. (setq link (concat "file:" (match-string 1 file))))
  9405. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  9406. (expand-file-name file))
  9407. (setq link (concat
  9408. "file:" (match-string 1 (expand-file-name file)))))
  9409. (t (setq link (concat "file:" file)))))
  9410. link))
  9411. (defun org-iread-file-name (&rest args)
  9412. "Read-file-name using `ido-mode' speedup if available.
  9413. ARGS are arguments that may be passed to `ido-read-file-name' or `read-file-name'.
  9414. See `read-file-name' for a description of parameters."
  9415. (org-without-partial-completion
  9416. (if (and org-completion-use-ido
  9417. (fboundp 'ido-read-file-name)
  9418. (boundp 'ido-mode) ido-mode
  9419. (listp (second args)))
  9420. (let ((ido-enter-matching-directory nil))
  9421. (apply 'ido-read-file-name args))
  9422. (apply 'read-file-name args))))
  9423. (defun org-completing-read (&rest args)
  9424. "Completing-read with SPACE being a normal character."
  9425. (let ((enable-recursive-minibuffers t)
  9426. (minibuffer-local-completion-map
  9427. (copy-keymap minibuffer-local-completion-map)))
  9428. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  9429. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  9430. (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
  9431. (apply 'org-icompleting-read args)))
  9432. (defun org-completing-read-no-i (&rest args)
  9433. (let (org-completion-use-ido org-completion-use-iswitchb)
  9434. (apply 'org-completing-read args)))
  9435. (defun org-iswitchb-completing-read (prompt choices &rest args)
  9436. "Use iswitch as a completing-read replacement to choose from choices.
  9437. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  9438. from."
  9439. (let* ((iswitchb-use-virtual-buffers nil)
  9440. (iswitchb-make-buflist-hook
  9441. (lambda ()
  9442. (setq iswitchb-temp-buflist choices))))
  9443. (iswitchb-read-buffer prompt)))
  9444. (defun org-icompleting-read (&rest args)
  9445. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  9446. (org-without-partial-completion
  9447. (if (and org-completion-use-ido
  9448. (fboundp 'ido-completing-read)
  9449. (boundp 'ido-mode) ido-mode
  9450. (listp (second args)))
  9451. (let ((ido-enter-matching-directory nil))
  9452. (apply 'ido-completing-read (concat (car args))
  9453. (if (consp (car (nth 1 args)))
  9454. (mapcar 'car (nth 1 args))
  9455. (nth 1 args))
  9456. (cddr args)))
  9457. (if (and org-completion-use-iswitchb
  9458. (boundp 'iswitchb-mode) iswitchb-mode
  9459. (listp (second args)))
  9460. (apply 'org-iswitchb-completing-read (concat (car args))
  9461. (if (consp (car (nth 1 args)))
  9462. (mapcar 'car (nth 1 args))
  9463. (nth 1 args))
  9464. (cddr args))
  9465. (apply 'completing-read args)))))
  9466. (defun org-extract-attributes (s)
  9467. "Extract the attributes cookie from a string and set as text property."
  9468. (let (a attr (start 0) key value)
  9469. (save-match-data
  9470. (when (string-match "{{\\([^}]+\\)}}$" s)
  9471. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  9472. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  9473. (setq key (match-string 1 a) value (match-string 2 a)
  9474. start (match-end 0)
  9475. attr (plist-put attr (intern key) value))))
  9476. (org-add-props s nil 'org-attr attr))
  9477. s))
  9478. ;;; Opening/following a link
  9479. (defvar org-link-search-failed nil)
  9480. (defvar org-open-link-functions nil
  9481. "Hook for functions finding a plain text link.
  9482. These functions must take a single argument, the link content.
  9483. They will be called for links that look like [[link text][description]]
  9484. when LINK TEXT does not have a protocol like \"http:\" and does not look
  9485. like a filename (e.g. \"./blue.png\").
  9486. These functions will be called *before* Org attempts to resolve the
  9487. link by doing text searches in the current buffer - so if you want a
  9488. link \"[[target]]\" to still find \"<<target>>\", your function should
  9489. handle this as a special case.
  9490. When the function does handle the link, it must return a non-nil value.
  9491. If it decides that it is not responsible for this link, it must return
  9492. nil to indicate that that Org-mode can continue with other options
  9493. like exact and fuzzy text search.")
  9494. (defun org-next-link (&optional search-backward)
  9495. "Move forward to the next link.
  9496. If the link is in hidden text, expose it."
  9497. (interactive "P")
  9498. (when (and org-link-search-failed (eq this-command last-command))
  9499. (goto-char (point-min))
  9500. (message "Link search wrapped back to beginning of buffer"))
  9501. (setq org-link-search-failed nil)
  9502. (let* ((pos (point))
  9503. (ct (org-context))
  9504. (a (assoc :link ct))
  9505. (srch-fun (if search-backward 're-search-backward 're-search-forward)))
  9506. (cond (a (goto-char (nth (if search-backward 1 2) a)))
  9507. ((looking-at org-any-link-re)
  9508. ;; Don't stay stuck at link without an org-link face
  9509. (forward-char (if search-backward -1 1))))
  9510. (if (funcall srch-fun org-any-link-re nil t)
  9511. (progn
  9512. (goto-char (match-beginning 0))
  9513. (if (outline-invisible-p) (org-show-context)))
  9514. (goto-char pos)
  9515. (setq org-link-search-failed t)
  9516. (message "No further link found"))))
  9517. (defun org-previous-link ()
  9518. "Move backward to the previous link.
  9519. If the link is in hidden text, expose it."
  9520. (interactive)
  9521. (funcall 'org-next-link t))
  9522. (defun org-translate-link (s)
  9523. "Translate a link string if a translation function has been defined."
  9524. (if (and org-link-translation-function
  9525. (fboundp org-link-translation-function)
  9526. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  9527. (progn
  9528. (setq s (funcall org-link-translation-function
  9529. (match-string 1 s) (match-string 2 s)))
  9530. (concat (car s) ":" (cdr s)))
  9531. s))
  9532. (defun org-translate-link-from-planner (type path)
  9533. "Translate a link from Emacs Planner syntax so that Org can follow it.
  9534. This is still an experimental function, your mileage may vary."
  9535. (cond
  9536. ((member type '("http" "https" "news" "ftp"))
  9537. ;; standard Internet links are the same.
  9538. nil)
  9539. ((and (equal type "irc") (string-match "^//" path))
  9540. ;; Planner has two / at the beginning of an irc link, we have 1.
  9541. ;; We should have zero, actually....
  9542. (setq path (substring path 1)))
  9543. ((and (equal type "lisp") (string-match "^/" path))
  9544. ;; Planner has a slash, we do not.
  9545. (setq type "elisp" path (substring path 1)))
  9546. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  9547. ;; A typical message link. Planner has the id after the final slash,
  9548. ;; we separate it with a hash mark
  9549. (setq path (concat (match-string 1 path) "#"
  9550. (org-remove-angle-brackets (match-string 2 path))))))
  9551. (cons type path))
  9552. (defun org-find-file-at-mouse (ev)
  9553. "Open file link or URL at mouse."
  9554. (interactive "e")
  9555. (mouse-set-point ev)
  9556. (org-open-at-point 'in-emacs))
  9557. (defun org-open-at-mouse (ev)
  9558. "Open file link or URL at mouse.
  9559. See the docstring of `org-open-file' for details."
  9560. (interactive "e")
  9561. (mouse-set-point ev)
  9562. (if (eq major-mode 'org-agenda-mode)
  9563. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  9564. (org-open-at-point))
  9565. (defvar org-window-config-before-follow-link nil
  9566. "The window configuration before following a link.
  9567. This is saved in case the need arises to restore it.")
  9568. (defvar org-open-link-marker (make-marker)
  9569. "Marker pointing to the location where `org-open-at-point' was called.")
  9570. ;;;###autoload
  9571. (defun org-open-at-point-global ()
  9572. "Follow a link like Org-mode does.
  9573. This command can be called in any mode to follow a link that has
  9574. Org-mode syntax."
  9575. (interactive)
  9576. (org-run-like-in-org-mode 'org-open-at-point))
  9577. ;;;###autoload
  9578. (defun org-open-link-from-string (s &optional arg reference-buffer)
  9579. "Open a link in the string S, as if it was in Org-mode."
  9580. (interactive "sLink: \nP")
  9581. (let ((reference-buffer (or reference-buffer (current-buffer))))
  9582. (with-temp-buffer
  9583. (let ((org-inhibit-startup (not reference-buffer)))
  9584. (org-mode)
  9585. (insert s)
  9586. (goto-char (point-min))
  9587. (when reference-buffer
  9588. (setq org-link-abbrev-alist-local
  9589. (with-current-buffer reference-buffer
  9590. org-link-abbrev-alist-local)))
  9591. (org-open-at-point arg reference-buffer)))))
  9592. (defvar org-open-at-point-functions nil
  9593. "Hook that is run when following a link at point.
  9594. Functions in this hook must return t if they identify and follow
  9595. a link at point. If they don't find anything interesting at point,
  9596. they must return nil.")
  9597. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  9598. (defvar clean-buffer-list-kill-buffer-names) ; Defined in midnight.el
  9599. (defun org-open-at-point (&optional arg reference-buffer)
  9600. "Open link, timestamp, footnote or tags at point.
  9601. When point is on a link, follow it. Normally, files will be
  9602. opened by an appropriate application. If the optional prefix
  9603. argument ARG is non-nil, Emacs will visit the file. With
  9604. a double prefix argument, try to open outside of Emacs, in the
  9605. application the system uses for this file type.
  9606. When point is on a timestamp, open the agenda at the day
  9607. specified.
  9608. When point is a footnote definition, move to the first reference
  9609. found. If it is on a reference, move to the associated
  9610. definition.
  9611. When point is on a headline, display a list of every link in the
  9612. entry, so it is possible to pick one, or all, of them. If point
  9613. is on a tag, call `org-tags-view' instead.
  9614. When optional argument REFERENCE-BUFFER is non-nil, it should
  9615. specify a buffer from where the link search should happen. This
  9616. is used internally by `org-open-link-from-string'.
  9617. On top of syntactically correct links, this function will open
  9618. the link at point in comments or comment blocks and the first
  9619. link in a property drawer line."
  9620. (interactive "P")
  9621. ;; On a code block, open block's results.
  9622. (unless (call-interactively 'org-babel-open-src-block-result)
  9623. (org-load-modules-maybe)
  9624. (move-marker org-open-link-marker (point))
  9625. (setq org-window-config-before-follow-link (current-window-configuration))
  9626. (org-remove-occur-highlights nil nil t)
  9627. (unless (run-hook-with-args-until-success 'org-open-at-point-functions)
  9628. (let* ((context (org-element-context)) type value)
  9629. ;; On an unsupported type, check if point is contained within
  9630. ;; a support one.
  9631. (while (and (not (memq (setq type (org-element-type context))
  9632. '(comment comment-block
  9633. headline inlinetask link
  9634. footnote-definition footnote-reference
  9635. node-property timestamp)))
  9636. (setq context (org-element-property :parent context))))
  9637. (setq value (org-element-property :value context))
  9638. (cond
  9639. ;; Blank lines at the beginning of buffer: bail out.
  9640. ((not context) (user-error "No link found"))
  9641. ;; Exception n°1: links in property drawers
  9642. ((eq type 'node-property)
  9643. (org-open-link-from-string
  9644. (and (string-match org-any-link-re value)
  9645. (match-string-no-properties 0 value))))
  9646. ;; Exception n°2: links in comments.
  9647. ((memq type '(comment comment-block))
  9648. (save-excursion
  9649. (skip-chars-forward "\\S-" (point-at-eol))
  9650. (let ((string-rear (replace-regexp-in-string
  9651. "^[ \t]*# [ \t]*" ""
  9652. (buffer-substring (point) (line-beginning-position))))
  9653. (string-front (buffer-substring (point) (line-end-position))))
  9654. (with-temp-buffer
  9655. (let ((org-inhibit-startup t)) (org-mode))
  9656. (insert value)
  9657. (goto-char (point-min))
  9658. (when (and (search-forward string-rear nil t)
  9659. (search-forward string-front (line-end-position) t))
  9660. (goto-char (match-beginning 0))
  9661. (org-open-at-point)
  9662. (when (string= string-rear "") (forward-char)))))))
  9663. ;; On a headline or an inlinetask, but not on a timestamp,
  9664. ;; a link, a footnote reference or on tags.
  9665. ((and (memq type '(headline inlinetask))
  9666. ;; Not on tags.
  9667. (progn (save-excursion (beginning-of-line)
  9668. (looking-at org-complex-heading-regexp))
  9669. (or (not (match-beginning 5))
  9670. (< (point) (match-beginning 5)))))
  9671. (let* ((data (org-offer-links-in-entry (current-buffer) (point) arg))
  9672. (links (car data))
  9673. (links-end (cdr data)))
  9674. (if links
  9675. (dolist (link (if (stringp links) (list links) links))
  9676. (search-forward link nil links-end)
  9677. (goto-char (match-beginning 0))
  9678. (org-open-at-point))
  9679. (require 'org-attach)
  9680. (org-attach-reveal 'if-exists))))
  9681. ;; Do nothing on white spaces after an object, unless point
  9682. ;; is right after it.
  9683. ((> (point)
  9684. (save-excursion
  9685. (goto-char (org-element-property :end context))
  9686. (skip-chars-backward " \t")
  9687. (point)))
  9688. (user-error "No link found"))
  9689. ((eq type 'timestamp) (org-follow-timestamp-link))
  9690. ;; On tags within a headline or an inlinetask.
  9691. ((and (memq type '(headline inlinetask))
  9692. (progn (save-excursion (beginning-of-line)
  9693. (looking-at org-complex-heading-regexp))
  9694. (and (match-beginning 5)
  9695. (>= (point) (match-beginning 5)))))
  9696. (org-tags-view arg (substring (match-string 5) 0 -1)))
  9697. ((eq type 'link)
  9698. ;; When link is located within the description of another
  9699. ;; link (e.g., an inline image), always open the parent
  9700. ;; link.
  9701. (let*((link (let ((up (org-element-property :parent context)))
  9702. (if (eq (org-element-type up) 'link) up context)))
  9703. (type (org-element-property :type link))
  9704. (path (org-link-unescape (org-element-property :path link))))
  9705. ;; Switch back to REFERENCE-BUFFER needed when called in
  9706. ;; a temporary buffer through `org-open-link-from-string'.
  9707. (with-current-buffer (or reference-buffer (current-buffer))
  9708. (cond
  9709. ((equal type "file")
  9710. (if (string-match "[*?{]" (file-name-nondirectory path))
  9711. (dired path)
  9712. ;; Look into `org-link-protocols' in order to find
  9713. ;; a DEDICATED-FUNCTION to open file. The function
  9714. ;; will be applied on raw link instead of parsed
  9715. ;; link due to the limitation in `org-add-link-type'
  9716. ;; ("open" function called with a single argument).
  9717. ;; If no such function is found, fallback to
  9718. ;; `org-open-file'.
  9719. ;;
  9720. ;; Note : "file+emacs" and "file+sys" types are
  9721. ;; hard-coded in order to escape the previous
  9722. ;; limitation.
  9723. (let* ((option (org-element-property :search-option link))
  9724. (app (org-element-property :application link))
  9725. (dedicated-function
  9726. (nth 1 (assoc app org-link-protocols))))
  9727. (if dedicated-function
  9728. (funcall dedicated-function
  9729. (concat path
  9730. (and option (concat "::" option))))
  9731. (apply 'org-open-file
  9732. path
  9733. (cond (arg)
  9734. ((equal app "emacs") 'emacs)
  9735. ((equal app "sys") 'system))
  9736. (cond ((not option) nil)
  9737. ((org-string-match-p "\\`[0-9]+\\'" option)
  9738. (list (string-to-number option)))
  9739. (t (list nil
  9740. (org-link-unescape option)))))))))
  9741. ((assoc type org-link-protocols)
  9742. (funcall (nth 1 (assoc type org-link-protocols)) path))
  9743. ((equal type "help")
  9744. (let ((f-or-v (intern path)))
  9745. (cond ((fboundp f-or-v) (describe-function f-or-v))
  9746. ((boundp f-or-v) (describe-variable f-or-v))
  9747. (t (error "Not a known function or variable")))))
  9748. ((member type '("http" "https" "ftp" "mailto" "news"))
  9749. (browse-url (org-link-escape-browser (concat type ":" path))))
  9750. ((equal type "doi")
  9751. (browse-url
  9752. (org-link-escape-browser (concat org-doi-server-url path))))
  9753. ((equal type "message") (browse-url (concat type ":" path)))
  9754. ((equal type "shell")
  9755. (let ((buf (generate-new-buffer "*Org Shell Output*"))
  9756. (cmd path))
  9757. (if (or (and (org-string-nw-p
  9758. org-confirm-shell-link-not-regexp)
  9759. (string-match
  9760. org-confirm-shell-link-not-regexp cmd))
  9761. (not org-confirm-shell-link-function)
  9762. (funcall org-confirm-shell-link-function
  9763. (format "Execute \"%s\" in shell? "
  9764. (org-add-props cmd nil
  9765. 'face 'org-warning))))
  9766. (progn
  9767. (message "Executing %s" cmd)
  9768. (shell-command cmd buf)
  9769. (when (featurep 'midnight)
  9770. (setq clean-buffer-list-kill-buffer-names
  9771. (cons buf
  9772. clean-buffer-list-kill-buffer-names))))
  9773. (user-error "Abort"))))
  9774. ((equal type "elisp")
  9775. (let ((cmd path))
  9776. (if (or (and (org-string-nw-p
  9777. org-confirm-elisp-link-not-regexp)
  9778. (org-string-match-p
  9779. org-confirm-elisp-link-not-regexp cmd))
  9780. (not org-confirm-elisp-link-function)
  9781. (funcall org-confirm-elisp-link-function
  9782. (format "Execute \"%s\" as elisp? "
  9783. (org-add-props cmd nil
  9784. 'face 'org-warning))))
  9785. (message "%s => %s" cmd
  9786. (if (eq (string-to-char cmd) ?\()
  9787. (eval (read cmd))
  9788. (call-interactively (read cmd))))
  9789. (user-error "Abort"))))
  9790. ((equal type "id")
  9791. (require 'ord-id)
  9792. (funcall (nth 1 (assoc "id" org-link-protocols)) path))
  9793. ((member type '("coderef" "custom-id" "fuzzy" "radio"))
  9794. (unless (run-hook-with-args-until-success
  9795. 'org-open-link-functions path)
  9796. (if (not arg) (org-mark-ring-push)
  9797. (switch-to-buffer-other-window
  9798. (org-get-buffer-for-internal-link (current-buffer))))
  9799. (let ((cmd `(org-link-search
  9800. ,(if (member type '("custom-id" "coderef"))
  9801. (org-element-property :raw-link link)
  9802. path)
  9803. ,(cond ((equal arg '(4)) 'occur)
  9804. ((equal arg '(16)) 'org-occur))
  9805. ,(org-element-property :begin link))))
  9806. (condition-case nil
  9807. (let ((org-link-search-inhibit-query t))
  9808. (eval cmd))
  9809. (error (progn (widen) (eval cmd)))))))
  9810. (t (browse-url-at-point))))))
  9811. ;; On a footnote reference or at a footnote definition's label.
  9812. ((or (eq type 'footnote-reference)
  9813. (and (eq type 'footnote-definition)
  9814. (save-excursion
  9815. ;; Do not validate action when point is on the
  9816. ;; spaces right after the footnote label, in
  9817. ;; order to be on par with behaviour on links.
  9818. (skip-chars-forward " \t")
  9819. (let ((begin
  9820. (org-element-property :contents-begin context)))
  9821. (if begin (< (point) begin)
  9822. (= (org-element-property :post-affiliated context)
  9823. (line-beginning-position)))))))
  9824. (org-footnote-action))
  9825. (t (user-error "No link found")))))
  9826. (move-marker org-open-link-marker nil)
  9827. (run-hook-with-args 'org-follow-link-hook)))
  9828. (defun org-offer-links-in-entry (buffer marker &optional nth zero)
  9829. "Offer links in the current entry and return the selected link.
  9830. If there is only one link, return it.
  9831. If NTH is an integer, return the NTH link found.
  9832. If ZERO is a string, check also this string for a link, and if
  9833. there is one, return it."
  9834. (with-current-buffer buffer
  9835. (save-excursion
  9836. (save-restriction
  9837. (widen)
  9838. (goto-char marker)
  9839. (let ((cnt ?0)
  9840. (in-emacs (if (integerp nth) nil nth))
  9841. have-zero end links link c)
  9842. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  9843. (push (match-string 0 zero) links)
  9844. (setq cnt (1- cnt) have-zero t))
  9845. (save-excursion
  9846. (org-back-to-heading t)
  9847. (setq end (save-excursion (outline-next-heading) (point)))
  9848. (while (re-search-forward org-any-link-re end t)
  9849. (push (match-string 0) links))
  9850. (setq links (org-uniquify (reverse links))))
  9851. (cond
  9852. ((null links)
  9853. (message "No links"))
  9854. ((equal (length links) 1)
  9855. (setq link (car links)))
  9856. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  9857. (setq link (nth (if have-zero nth (1- nth)) links)))
  9858. (t ; we have to select a link
  9859. (save-excursion
  9860. (save-window-excursion
  9861. (delete-other-windows)
  9862. (with-output-to-temp-buffer "*Select Link*"
  9863. (mapc (lambda (l)
  9864. (if (not (string-match org-bracket-link-regexp l))
  9865. (princ (format "[%c] %s\n" (incf cnt)
  9866. (org-remove-angle-brackets l)))
  9867. (if (match-end 3)
  9868. (princ (format "[%c] %s (%s)\n" (incf cnt)
  9869. (match-string 3 l) (match-string 1 l)))
  9870. (princ (format "[%c] %s\n" (incf cnt)
  9871. (match-string 1 l))))))
  9872. links))
  9873. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  9874. (message "Select link to open, RET to open all:")
  9875. (setq c (read-char-exclusive))
  9876. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  9877. (when (equal c ?q) (user-error "Abort"))
  9878. (if (equal c ?\C-m)
  9879. (setq link links)
  9880. (setq nth (- c ?0))
  9881. (if have-zero (setq nth (1+ nth)))
  9882. (unless (and (integerp nth) (>= (length links) nth))
  9883. (user-error "Invalid link selection"))
  9884. (setq link (nth (1- nth) links)))))
  9885. (cons link end))))))
  9886. ;; TODO: These functions are deprecated since `org-open-at-point'
  9887. ;; hard-codes behaviour for "file+emacs" and "file+sys" types.
  9888. (defun org-open-file-with-system (path)
  9889. "Open file at PATH using the system way of opening it."
  9890. (org-open-file path 'system))
  9891. (defun org-open-file-with-emacs (path)
  9892. "Open file at PATH in Emacs."
  9893. (org-open-file path 'emacs))
  9894. ;;; File search
  9895. (defvar org-create-file-search-functions nil
  9896. "List of functions to construct the right search string for a file link.
  9897. These functions are called in turn with point at the location to
  9898. which the link should point.
  9899. A function in the hook should first test if it would like to
  9900. handle this file type, for example by checking the `major-mode'
  9901. or the file extension. If it decides not to handle this file, it
  9902. should just return nil to give other functions a chance. If it
  9903. does handle the file, it must return the search string to be used
  9904. when following the link. The search string will be part of the
  9905. file link, given after a double colon, and `org-open-at-point'
  9906. will automatically search for it. If special measures must be
  9907. taken to make the search successful, another function should be
  9908. added to the companion hook `org-execute-file-search-functions',
  9909. which see.
  9910. A function in this hook may also use `setq' to set the variable
  9911. `description' to provide a suggestion for the descriptive text to
  9912. be used for this link when it gets inserted into an Org-mode
  9913. buffer with \\[org-insert-link].")
  9914. (defvar org-execute-file-search-functions nil
  9915. "List of functions to execute a file search triggered by a link.
  9916. Functions added to this hook must accept a single argument, the
  9917. search string that was part of the file link, the part after the
  9918. double colon. The function must first check if it would like to
  9919. handle this search, for example by checking the `major-mode' or
  9920. the file extension. If it decides not to handle this search, it
  9921. should just return nil to give other functions a chance. If it
  9922. does handle the search, it must return a non-nil value to keep
  9923. other functions from trying.
  9924. Each function can access the current prefix argument through the
  9925. variable `current-prefix-arg'. Note that a single prefix is used
  9926. to force opening a link in Emacs, so it may be good to only use a
  9927. numeric or double prefix to guide the search function.
  9928. In case this is needed, a function in this hook can also restore
  9929. the window configuration before `org-open-at-point' was called using:
  9930. (set-window-configuration org-window-config-before-follow-link)")
  9931. (defun org-link-search (s &optional type avoid-pos stealth)
  9932. "Search for a link search option.
  9933. If S is surrounded by forward slashes, it is interpreted as a
  9934. regular expression. In org-mode files, this will create an `org-occur'
  9935. sparse tree. In ordinary files, `occur' will be used to list matches.
  9936. If the current buffer is in `dired-mode', grep will be used to search
  9937. in all files. If AVOID-POS is given, ignore matches near that position.
  9938. When optional argument STEALTH is non-nil, do not modify
  9939. visibility around point, thus ignoring
  9940. `org-show-hierarchy-above', `org-show-following-heading' and
  9941. `org-show-siblings' variables."
  9942. (let ((case-fold-search t)
  9943. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  9944. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  9945. (append '(("") (" ") ("\t") ("\n"))
  9946. org-emphasis-alist)
  9947. "\\|") "\\)"))
  9948. (pos (point))
  9949. (pre nil) (post nil)
  9950. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  9951. (cond
  9952. ;; First check if there are any special search functions
  9953. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  9954. ;; Now try the builtin stuff
  9955. ((and (equal (string-to-char s0) ?#)
  9956. (> (length s0) 1)
  9957. (save-excursion
  9958. (goto-char (point-min))
  9959. (and
  9960. (re-search-forward
  9961. (concat "^[ \t]*:CUSTOM_ID:[ \t]+"
  9962. (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  9963. (setq type 'dedicated
  9964. pos (match-beginning 0))))
  9965. ;; There is an exact target for this
  9966. (goto-char pos)
  9967. (org-back-to-heading t)))
  9968. ((save-excursion
  9969. (goto-char (point-min))
  9970. (and
  9971. (re-search-forward
  9972. (concat "<<" (regexp-quote s0) ">>") nil t)
  9973. (setq type 'dedicated
  9974. pos (match-beginning 0))))
  9975. ;; There is an exact target for this
  9976. (goto-char pos))
  9977. ((save-excursion
  9978. (goto-char (point-min))
  9979. (and
  9980. (re-search-forward
  9981. (format "^[ \t]*#\\+NAME: %s" (regexp-quote s0)) nil t)
  9982. (setq type 'dedicated pos (match-beginning 0))))
  9983. ;; Found an element with a matching #+name affiliated keyword.
  9984. (goto-char pos))
  9985. ((and (string-match "^(\\(.*\\))$" s0)
  9986. (save-excursion
  9987. (goto-char (point-min))
  9988. (and
  9989. (re-search-forward
  9990. (concat "[^[]" (regexp-quote
  9991. (format org-coderef-label-format
  9992. (match-string 1 s0))))
  9993. nil t)
  9994. (setq type 'dedicated
  9995. pos (1+ (match-beginning 0))))))
  9996. ;; There is a coderef target for this
  9997. (goto-char pos))
  9998. ((string-match "^/\\(.*\\)/$" s)
  9999. ;; A regular expression
  10000. (cond
  10001. ((derived-mode-p 'org-mode)
  10002. (org-occur (match-string 1 s)))
  10003. (t (org-do-occur (match-string 1 s)))))
  10004. ((and (derived-mode-p 'org-mode) org-link-search-must-match-exact-headline)
  10005. (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
  10006. (goto-char (point-min))
  10007. (cond
  10008. ((let (case-fold-search)
  10009. (re-search-forward (format org-complex-heading-regexp-format
  10010. (regexp-quote s))
  10011. nil t))
  10012. ;; OK, found a match
  10013. (setq type 'dedicated)
  10014. (goto-char (match-beginning 0)))
  10015. ((and (not org-link-search-inhibit-query)
  10016. (eq org-link-search-must-match-exact-headline 'query-to-create)
  10017. (y-or-n-p "No match - create this as a new heading? "))
  10018. (goto-char (point-max))
  10019. (or (bolp) (newline))
  10020. (insert "* " s "\n")
  10021. (beginning-of-line 0))
  10022. (t
  10023. (goto-char pos)
  10024. (error "No match"))))
  10025. (t
  10026. ;; A normal search string
  10027. (when (equal (string-to-char s) ?*)
  10028. ;; Anchor on headlines, post may include tags.
  10029. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  10030. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
  10031. s (substring s 1)))
  10032. (remove-text-properties
  10033. 0 (length s)
  10034. '(face nil mouse-face nil keymap nil fontified nil) s)
  10035. ;; Make a series of regular expressions to find a match
  10036. (setq words (org-split-string s "[ \n\r\t]+")
  10037. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  10038. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  10039. "\\)" markers)
  10040. re2a_ (concat "\\(" (mapconcat 'downcase words
  10041. "[ \t\r\n]+") "\\)[ \t\r\n]")
  10042. re2a (concat "[ \t\r\n]" re2a_)
  10043. re4_ (concat "\\(" (mapconcat 'downcase words
  10044. "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  10045. re4 (concat "[^a-zA-Z_]" re4_)
  10046. re1 (concat pre re2 post)
  10047. re3 (concat pre (if pre re4_ re4) post)
  10048. re5 (concat pre ".*" re4)
  10049. re2 (concat pre re2)
  10050. re2a (concat pre (if pre re2a_ re2a))
  10051. re4 (concat pre (if pre re4_ re4))
  10052. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  10053. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  10054. re5 "\\)"))
  10055. (cond
  10056. ((eq type 'org-occur) (org-occur reall))
  10057. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  10058. (t (goto-char (point-min))
  10059. (setq type 'fuzzy)
  10060. (if (or (and (org-search-not-self 1 re0 nil t)
  10061. (setq type 'dedicated))
  10062. (org-search-not-self 1 re1 nil t)
  10063. (org-search-not-self 1 re2 nil t)
  10064. (org-search-not-self 1 re2a nil t)
  10065. (org-search-not-self 1 re3 nil t)
  10066. (org-search-not-self 1 re4 nil t)
  10067. (org-search-not-self 1 re5 nil t))
  10068. (goto-char (match-beginning 1))
  10069. (goto-char pos)
  10070. (error "No match"))))))
  10071. (and (derived-mode-p 'org-mode)
  10072. (not stealth)
  10073. (org-show-context 'link-search))
  10074. type))
  10075. (defun org-search-not-self (group &rest args)
  10076. "Execute `re-search-forward', but only accept matches that do not
  10077. enclose the position of `org-open-link-marker'."
  10078. (let ((m org-open-link-marker))
  10079. (catch 'exit
  10080. (while (apply 're-search-forward args)
  10081. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  10082. (goto-char (match-end group))
  10083. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  10084. (> (match-beginning 0) (marker-position m))
  10085. (< (match-end 0) (marker-position m)))
  10086. (save-match-data
  10087. (or (not (org-in-regexp
  10088. org-bracket-link-analytic-regexp 1))
  10089. (not (match-end 4)) ; no description
  10090. (and (<= (match-beginning 4) (point))
  10091. (>= (match-end 4) (point))))))
  10092. (throw 'exit (point))))))))
  10093. (defun org-get-buffer-for-internal-link (buffer)
  10094. "Return a buffer to be used for displaying the link target of internal links."
  10095. (cond
  10096. ((not org-display-internal-link-with-indirect-buffer)
  10097. buffer)
  10098. ((string-match "(Clone)$" (buffer-name buffer))
  10099. (message "Buffer is already a clone, not making another one")
  10100. ;; we also do not modify visibility in this case
  10101. buffer)
  10102. (t ; make a new indirect buffer for displaying the link
  10103. (let* ((bn (buffer-name buffer))
  10104. (ibn (concat bn "(Clone)"))
  10105. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  10106. (with-current-buffer ib (org-overview))
  10107. ib))))
  10108. (defun org-do-occur (regexp &optional cleanup)
  10109. "Call the Emacs command `occur'.
  10110. If CLEANUP is non-nil, remove the printout of the regular expression
  10111. in the *Occur* buffer. This is useful if the regex is long and not useful
  10112. to read."
  10113. (occur regexp)
  10114. (when cleanup
  10115. (let ((cwin (selected-window)) win beg end)
  10116. (when (setq win (get-buffer-window "*Occur*"))
  10117. (select-window win))
  10118. (goto-char (point-min))
  10119. (when (re-search-forward "match[a-z]+" nil t)
  10120. (setq beg (match-end 0))
  10121. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  10122. (setq end (1- (match-beginning 0)))))
  10123. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  10124. (goto-char (point-min))
  10125. (select-window cwin))))
  10126. ;;; The mark ring for links jumps
  10127. (defvar org-mark-ring nil
  10128. "Mark ring for positions before jumps in Org-mode.")
  10129. (defvar org-mark-ring-last-goto nil
  10130. "Last position in the mark ring used to go back.")
  10131. ;; Fill and close the ring
  10132. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  10133. (loop for i from 1 to org-mark-ring-length do
  10134. (push (make-marker) org-mark-ring))
  10135. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  10136. org-mark-ring)
  10137. (defun org-mark-ring-push (&optional pos buffer)
  10138. "Put the current position or POS into the mark ring and rotate it."
  10139. (interactive)
  10140. (setq pos (or pos (point)))
  10141. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  10142. (move-marker (car org-mark-ring)
  10143. (or pos (point))
  10144. (or buffer (current-buffer)))
  10145. (message "%s"
  10146. (substitute-command-keys
  10147. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  10148. (defun org-mark-ring-goto (&optional n)
  10149. "Jump to the previous position in the mark ring.
  10150. With prefix arg N, jump back that many stored positions. When
  10151. called several times in succession, walk through the entire ring.
  10152. Org-mode commands jumping to a different position in the current file,
  10153. or to another Org-mode file, automatically push the old position
  10154. onto the ring."
  10155. (interactive "p")
  10156. (let (p m)
  10157. (if (eq last-command this-command)
  10158. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  10159. (setq p org-mark-ring))
  10160. (setq org-mark-ring-last-goto p)
  10161. (setq m (car p))
  10162. (org-pop-to-buffer-same-window (marker-buffer m))
  10163. (goto-char m)
  10164. (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  10165. (defun org-remove-angle-brackets (s)
  10166. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  10167. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  10168. s)
  10169. (defun org-add-angle-brackets (s)
  10170. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  10171. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  10172. s)
  10173. (defun org-remove-double-quotes (s)
  10174. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  10175. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  10176. s)
  10177. ;;; Following specific links
  10178. (defun org-follow-timestamp-link ()
  10179. "Open an agenda view for the time-stamp date/range at point."
  10180. (cond
  10181. ((org-at-date-range-p t)
  10182. (let ((org-agenda-start-on-weekday)
  10183. (t1 (match-string 1))
  10184. (t2 (match-string 2)) tt1 tt2)
  10185. (setq tt1 (time-to-days (org-time-string-to-time t1))
  10186. tt2 (time-to-days (org-time-string-to-time t2)))
  10187. (let ((org-agenda-buffer-tmp-name
  10188. (format "*Org Agenda(a:%s)"
  10189. (concat (substring t1 0 10) "--" (substring t2 0 10)))))
  10190. (org-agenda-list nil tt1 (1+ (- tt2 tt1))))))
  10191. ((org-at-timestamp-p t)
  10192. (let ((org-agenda-buffer-tmp-name
  10193. (format "*Org Agenda(a:%s)" (substring (match-string 1) 0 10))))
  10194. (org-agenda-list nil (time-to-days (org-time-string-to-time
  10195. (substring (match-string 1) 0 10)))
  10196. 1)))
  10197. (t (error "This should not happen"))))
  10198. ;;; Following file links
  10199. (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
  10200. (declare-function mailcap-extension-to-mime "mailcap" (extn))
  10201. (declare-function mailcap-mime-info
  10202. "mailcap" (string &optional request no-decode))
  10203. (defvar org-wait nil)
  10204. (defun org-open-file (path &optional in-emacs line search)
  10205. "Open the file at PATH.
  10206. First, this expands any special file name abbreviations. Then the
  10207. configuration variable `org-file-apps' is checked if it contains an
  10208. entry for this file type, and if yes, the corresponding command is launched.
  10209. If no application is found, Emacs simply visits the file.
  10210. With optional prefix argument IN-EMACS, Emacs will visit the file.
  10211. With a double \\[universal-argument] \\[universal-argument] \
  10212. prefix arg, Org tries to avoid opening in Emacs
  10213. and to use an external application to visit the file.
  10214. Optional LINE specifies a line to go to, optional SEARCH a string
  10215. to search for. If LINE or SEARCH is given, the file will be
  10216. opened in Emacs, unless an entry from org-file-apps that makes
  10217. use of groups in a regexp matches.
  10218. If you want to change the way frames are used when following a
  10219. link, please customize `org-link-frame-setup'.
  10220. If the file does not exist, an error is thrown."
  10221. (let* ((file (if (equal path "")
  10222. buffer-file-name
  10223. (substitute-in-file-name (expand-file-name path))))
  10224. (file-apps (append org-file-apps (org-default-apps)))
  10225. (apps (org-remove-if
  10226. 'org-file-apps-entry-match-against-dlink-p file-apps))
  10227. (apps-dlink (org-remove-if-not
  10228. 'org-file-apps-entry-match-against-dlink-p file-apps))
  10229. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  10230. (dirp (if remp nil (file-directory-p file)))
  10231. (file (if (and dirp org-open-directory-means-index-dot-org)
  10232. (concat (file-name-as-directory file) "index.org")
  10233. file))
  10234. (a-m-a-p (assq 'auto-mode apps))
  10235. (dfile (downcase file))
  10236. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  10237. (link (cond ((and (eq line nil)
  10238. (eq search nil))
  10239. file)
  10240. (line
  10241. (concat file "::" (number-to-string line)))
  10242. (search
  10243. (concat file "::" search))))
  10244. (dlink (downcase link))
  10245. (old-buffer (current-buffer))
  10246. (old-pos (point))
  10247. (old-mode major-mode)
  10248. ext cmd link-match-data)
  10249. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  10250. (setq ext (match-string 1 dfile))
  10251. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  10252. (setq ext (match-string 1 dfile))))
  10253. (cond
  10254. ((member in-emacs '((16) system))
  10255. (setq cmd (cdr (assoc 'system apps))))
  10256. (in-emacs (setq cmd 'emacs))
  10257. (t
  10258. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  10259. (and dirp (cdr (assoc 'directory apps)))
  10260. ; first, try matching against apps-dlink
  10261. ; if we get a match here, store the match data for later
  10262. (let ((match (assoc-default dlink apps-dlink
  10263. 'string-match)))
  10264. (if match
  10265. (progn (setq link-match-data (match-data))
  10266. match)
  10267. (progn (setq in-emacs (or in-emacs line search))
  10268. nil))) ; if we have no match in apps-dlink,
  10269. ; always open the file in emacs if line or search
  10270. ; is given (for backwards compatibility)
  10271. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  10272. 'string-match)
  10273. (cdr (assoc ext apps))
  10274. (cdr (assoc t apps))))))
  10275. (when (eq cmd 'system)
  10276. (setq cmd (cdr (assoc 'system apps))))
  10277. (when (eq cmd 'default)
  10278. (setq cmd (cdr (assoc t apps))))
  10279. (when (eq cmd 'mailcap)
  10280. (require 'mailcap)
  10281. (mailcap-parse-mailcaps)
  10282. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  10283. (command (mailcap-mime-info mime-type)))
  10284. (if (stringp command)
  10285. (setq cmd command)
  10286. (setq cmd 'emacs))))
  10287. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  10288. (not (file-exists-p file))
  10289. (not org-open-non-existing-files))
  10290. (user-error "No such file: %s" file))
  10291. (cond
  10292. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  10293. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  10294. (while (string-match "['\"]%s['\"]" cmd)
  10295. (setq cmd (replace-match "%s" t t cmd)))
  10296. (while (string-match "%s" cmd)
  10297. (setq cmd (replace-match
  10298. (save-match-data
  10299. (shell-quote-argument
  10300. (convert-standard-filename file)))
  10301. t t cmd)))
  10302. ;; Replace "%1", "%2" etc. in command with group matches from regex
  10303. (save-match-data
  10304. (let ((match-index 1)
  10305. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  10306. (set-match-data link-match-data)
  10307. (while (<= match-index number-of-groups)
  10308. (let ((regex (concat "%" (number-to-string match-index)))
  10309. (replace-with (match-string match-index dlink)))
  10310. (while (string-match regex cmd)
  10311. (setq cmd (replace-match replace-with t t cmd))))
  10312. (setq match-index (+ match-index 1)))))
  10313. (save-window-excursion
  10314. (message "Running %s...done" cmd)
  10315. (start-process-shell-command cmd nil cmd)
  10316. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))))
  10317. ((or (stringp cmd)
  10318. (eq cmd 'emacs))
  10319. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  10320. (widen)
  10321. (if line (progn (org-goto-line line)
  10322. (if (derived-mode-p 'org-mode)
  10323. (org-reveal)))
  10324. (if search (org-link-search search))))
  10325. ((consp cmd)
  10326. (let ((file (convert-standard-filename file)))
  10327. (save-match-data
  10328. (set-match-data link-match-data)
  10329. (eval cmd))))
  10330. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  10331. (and (derived-mode-p 'org-mode) (eq old-mode 'org-mode)
  10332. (or (not (equal old-buffer (current-buffer)))
  10333. (not (equal old-pos (point))))
  10334. (org-mark-ring-push old-pos old-buffer))))
  10335. (defun org-file-apps-entry-match-against-dlink-p (entry)
  10336. "This function returns non-nil if `entry' uses a regular
  10337. expression which should be matched against the whole link by
  10338. org-open-file.
  10339. It assumes that is the case when the entry uses a regular
  10340. expression which has at least one grouping construct and the
  10341. action is either a lisp form or a command string containing
  10342. '%1', i.e. using at least one subexpression match as a
  10343. parameter."
  10344. (let ((selector (car entry))
  10345. (action (cdr entry)))
  10346. (if (stringp selector)
  10347. (and (> (regexp-opt-depth selector) 0)
  10348. (or (and (stringp action)
  10349. (string-match "%[0-9]" action))
  10350. (consp action)))
  10351. nil)))
  10352. (defun org-default-apps ()
  10353. "Return the default applications for this operating system."
  10354. (cond
  10355. ((eq system-type 'darwin)
  10356. org-file-apps-defaults-macosx)
  10357. ((eq system-type 'windows-nt)
  10358. org-file-apps-defaults-windowsnt)
  10359. (t org-file-apps-defaults-gnu)))
  10360. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  10361. "Convert extensions to regular expressions in the cars of LIST.
  10362. Also, weed out any non-string entries, because the return value is used
  10363. only for regexp matching.
  10364. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  10365. point to the symbol `emacs', indicating that the file should
  10366. be opened in Emacs."
  10367. (append
  10368. (delq nil
  10369. (mapcar (lambda (x)
  10370. (if (not (stringp (car x)))
  10371. nil
  10372. (if (string-match "\\W" (car x))
  10373. x
  10374. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  10375. list))
  10376. (if add-auto-mode
  10377. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  10378. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  10379. (defun org-file-remote-p (file)
  10380. "Test whether FILE specifies a location on a remote system.
  10381. Return non-nil if the location is indeed remote.
  10382. For example, the filename \"/user@host:/foo\" specifies a location
  10383. on the system \"/user@host:\"."
  10384. (cond ((fboundp 'file-remote-p)
  10385. (file-remote-p file))
  10386. ((fboundp 'tramp-handle-file-remote-p)
  10387. (tramp-handle-file-remote-p file))
  10388. ((and (boundp 'ange-ftp-name-format)
  10389. (string-match (car ange-ftp-name-format) file))
  10390. t)))
  10391. ;;;; Refiling
  10392. (defun org-get-org-file ()
  10393. "Read a filename, with default directory `org-directory'."
  10394. (let ((default (or org-default-notes-file remember-data-file)))
  10395. (read-file-name (format "File name [%s]: " default)
  10396. (file-name-as-directory org-directory)
  10397. default)))
  10398. (defun org-notes-order-reversed-p ()
  10399. "Check if the current file should receive notes in reversed order."
  10400. (cond
  10401. ((not org-reverse-note-order) nil)
  10402. ((eq t org-reverse-note-order) t)
  10403. ((not (listp org-reverse-note-order)) nil)
  10404. (t (catch 'exit
  10405. (let ((all org-reverse-note-order)
  10406. entry)
  10407. (while (setq entry (pop all))
  10408. (if (string-match (car entry) buffer-file-name)
  10409. (throw 'exit (cdr entry))))
  10410. nil)))))
  10411. (defvar org-refile-target-table nil
  10412. "The list of refile targets, created by `org-refile'.")
  10413. (defvar org-agenda-new-buffers nil
  10414. "Buffers created to visit agenda files.")
  10415. (defvar org-refile-cache nil
  10416. "Cache for refile targets.")
  10417. (defvar org-refile-markers nil
  10418. "All the markers used for caching refile locations.")
  10419. (defun org-refile-marker (pos)
  10420. "Get a new refile marker, but only if caching is in use."
  10421. (if (not org-refile-use-cache)
  10422. pos
  10423. (let ((m (make-marker)))
  10424. (move-marker m pos)
  10425. (push m org-refile-markers)
  10426. m)))
  10427. (defun org-refile-cache-clear ()
  10428. "Clear the refile cache and disable all the markers."
  10429. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  10430. (setq org-refile-markers nil)
  10431. (setq org-refile-cache nil)
  10432. (message "Refile cache has been cleared"))
  10433. (defun org-refile-cache-check-set (set)
  10434. "Check if all the markers in the cache still have live buffers."
  10435. (let (marker)
  10436. (catch 'exit
  10437. (while (and set (setq marker (nth 3 (pop set))))
  10438. ;; If `org-refile-use-outline-path' is 'file, marker may be nil
  10439. (when (and marker (null (marker-buffer marker)))
  10440. (message "Please regenerate the refile cache with `C-0 C-c C-w'")
  10441. (sit-for 3)
  10442. (throw 'exit nil)))
  10443. t)))
  10444. (defun org-refile-cache-put (set &rest identifiers)
  10445. "Push the refile targets SET into the cache, under IDENTIFIERS."
  10446. (let* ((key (sha1 (prin1-to-string identifiers)))
  10447. (entry (assoc key org-refile-cache)))
  10448. (if entry
  10449. (setcdr entry set)
  10450. (push (cons key set) org-refile-cache))))
  10451. (defun org-refile-cache-get (&rest identifiers)
  10452. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  10453. (cond
  10454. ((not org-refile-cache) nil)
  10455. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  10456. (t
  10457. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  10458. org-refile-cache))))
  10459. (and set (org-refile-cache-check-set set) set)))))
  10460. (defun org-refile-get-targets (&optional default-buffer excluded-entries)
  10461. "Produce a table with refile targets."
  10462. (let ((case-fold-search nil)
  10463. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  10464. (entries (or org-refile-targets '((nil . (:level . 1)))))
  10465. targets tgs txt re files f desc descre fast-path-p level pos0)
  10466. (message "Getting targets...")
  10467. (with-current-buffer (or default-buffer (current-buffer))
  10468. (while (setq entry (pop entries))
  10469. (setq files (car entry) desc (cdr entry))
  10470. (setq fast-path-p nil)
  10471. (cond
  10472. ((null files) (setq files (list (current-buffer))))
  10473. ((eq files 'org-agenda-files)
  10474. (setq files (org-agenda-files 'unrestricted)))
  10475. ((and (symbolp files) (fboundp files))
  10476. (setq files (funcall files)))
  10477. ((and (symbolp files) (boundp files))
  10478. (setq files (symbol-value files))))
  10479. (if (stringp files) (setq files (list files)))
  10480. (cond
  10481. ((eq (car desc) :tag)
  10482. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  10483. ((eq (car desc) :todo)
  10484. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  10485. ((eq (car desc) :regexp)
  10486. (setq descre (cdr desc)))
  10487. ((eq (car desc) :level)
  10488. (setq descre (concat "^\\*\\{" (number-to-string
  10489. (if org-odd-levels-only
  10490. (1- (* 2 (cdr desc)))
  10491. (cdr desc)))
  10492. "\\}[ \t]")))
  10493. ((eq (car desc) :maxlevel)
  10494. (setq fast-path-p t)
  10495. (setq descre (concat "^\\*\\{1," (number-to-string
  10496. (if org-odd-levels-only
  10497. (1- (* 2 (cdr desc)))
  10498. (cdr desc)))
  10499. "\\}[ \t]")))
  10500. (t (error "Bad refiling target description %s" desc)))
  10501. (while (setq f (pop files))
  10502. (with-current-buffer
  10503. (if (bufferp f) f (org-get-agenda-file-buffer f))
  10504. (or
  10505. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  10506. (progn
  10507. (if (bufferp f) (setq f (buffer-file-name
  10508. (buffer-base-buffer f))))
  10509. (setq f (and f (expand-file-name f)))
  10510. (if (eq org-refile-use-outline-path 'file)
  10511. (push (list (file-name-nondirectory f) f nil nil) tgs))
  10512. (save-excursion
  10513. (save-restriction
  10514. (widen)
  10515. (goto-char (point-min))
  10516. (while (re-search-forward descre nil t)
  10517. (goto-char (setq pos0 (point-at-bol)))
  10518. (catch 'next
  10519. (when org-refile-target-verify-function
  10520. (save-match-data
  10521. (or (funcall org-refile-target-verify-function)
  10522. (throw 'next t))))
  10523. (when (and (looking-at org-complex-heading-regexp)
  10524. (not (member (match-string 4) excluded-entries))
  10525. (match-string 4))
  10526. (setq level (org-reduced-level
  10527. (- (match-end 1) (match-beginning 1)))
  10528. txt (org-link-display-format (match-string 4))
  10529. txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
  10530. re (format org-complex-heading-regexp-format
  10531. (regexp-quote (match-string 4))))
  10532. (when org-refile-use-outline-path
  10533. (setq txt (mapconcat
  10534. 'org-protect-slash
  10535. (append
  10536. (if (eq org-refile-use-outline-path
  10537. 'file)
  10538. (list (file-name-nondirectory
  10539. (buffer-file-name
  10540. (buffer-base-buffer))))
  10541. (if (eq org-refile-use-outline-path
  10542. 'full-file-path)
  10543. (list (buffer-file-name
  10544. (buffer-base-buffer)))))
  10545. (org-get-outline-path fast-path-p
  10546. level txt)
  10547. (list txt))
  10548. "/")))
  10549. (push (list txt f re (org-refile-marker (point)))
  10550. tgs)))
  10551. (when (= (point) pos0)
  10552. ;; verification function has not moved point
  10553. (goto-char (point-at-eol))))))))
  10554. (when org-refile-use-cache
  10555. (org-refile-cache-put tgs (buffer-file-name) descre))
  10556. (setq targets (append tgs targets))))))
  10557. (message "Getting targets...done")
  10558. (nreverse targets)))
  10559. (defun org-protect-slash (s)
  10560. (while (string-match "/" s)
  10561. (setq s (replace-match "\\" t t s)))
  10562. s)
  10563. (defvar org-olpa (make-vector 20 nil))
  10564. (defun org-get-outline-path (&optional fastp level heading)
  10565. "Return the outline path to the current entry, as a list.
  10566. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  10567. routine which makes outline path derivations for an entire file,
  10568. avoiding backtracing. Refile target collection makes use of that."
  10569. (if fastp
  10570. (progn
  10571. (if (> level 19)
  10572. (error "Outline path failure, more than 19 levels"))
  10573. (loop for i from level upto 19 do
  10574. (aset org-olpa i nil))
  10575. (prog1
  10576. (delq nil (append org-olpa nil))
  10577. (aset org-olpa level heading)))
  10578. (let (rtn case-fold-search)
  10579. (save-excursion
  10580. (save-restriction
  10581. (widen)
  10582. (while (org-up-heading-safe)
  10583. (when (looking-at org-complex-heading-regexp)
  10584. (push (org-trim
  10585. (replace-regexp-in-string
  10586. ;; Remove statistical/checkboxes cookies
  10587. "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" ""
  10588. (org-match-string-no-properties 4)))
  10589. rtn)))
  10590. rtn)))))
  10591. (defun org-format-outline-path (path &optional width prefix separator)
  10592. "Format the outline path PATH for display.
  10593. WIDTH is the maximum number of characters that is available.
  10594. PREFIX is a prefix to be included in the returned string,
  10595. such as the file name.
  10596. SEPARATOR is inserted between the different parts of the path,
  10597. the default is \"/\"."
  10598. (setq width (or width 79))
  10599. (if prefix (setq width (- width (length prefix))))
  10600. (if (not path)
  10601. (or prefix "")
  10602. (let* ((nsteps (length path))
  10603. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  10604. (maxwidth (if (<= total-width width)
  10605. 10000 ;; everything fits
  10606. ;; we need to shorten the level headings
  10607. (/ (- width nsteps) nsteps)))
  10608. (org-odd-levels-only nil)
  10609. (n 0)
  10610. (total (1+ (length prefix))))
  10611. (setq maxwidth (max maxwidth 10))
  10612. (concat prefix
  10613. (if prefix (or separator "/"))
  10614. (mapconcat
  10615. (lambda (h)
  10616. (setq n (1+ n))
  10617. (if (and (= n nsteps) (< maxwidth 10000))
  10618. (setq maxwidth (- total-width total)))
  10619. (if (< (length h) maxwidth)
  10620. (progn (setq total (+ total (length h) 1)) h)
  10621. (setq h (substring h 0 (- maxwidth 2))
  10622. total (+ total maxwidth 1))
  10623. (if (string-match "[ \t]+\\'" h)
  10624. (setq h (substring h 0 (match-beginning 0))))
  10625. (setq h (concat h "..")))
  10626. (org-add-props h nil 'face
  10627. (nth (% (1- n) org-n-level-faces)
  10628. org-level-faces))
  10629. h)
  10630. path (or separator "/"))))))
  10631. (defun org-display-outline-path (&optional file current separator just-return-string)
  10632. "Display the current outline path in the echo area.
  10633. If FILE is non-nil, prepend the output with the file name.
  10634. If CURRENT is non-nil, append the current heading to the output.
  10635. SEPARATOR is passed through to `org-format-outline-path'. It separates
  10636. the different parts of the path and defaults to \"/\".
  10637. If JUST-RETURN-STRING is non-nil, return a string, don't display a message."
  10638. (interactive "P")
  10639. (let* (case-fold-search
  10640. (bfn (buffer-file-name (buffer-base-buffer)))
  10641. (path (and (derived-mode-p 'org-mode) (org-get-outline-path)))
  10642. res)
  10643. (if current (setq path (append path
  10644. (save-excursion
  10645. (org-back-to-heading t)
  10646. (if (looking-at org-complex-heading-regexp)
  10647. (list (match-string 4)))))))
  10648. (setq res
  10649. (org-format-outline-path
  10650. path
  10651. (1- (frame-width))
  10652. (and file bfn (concat (file-name-nondirectory bfn) separator))
  10653. separator))
  10654. (if just-return-string
  10655. (org-no-properties res)
  10656. (org-unlogged-message "%s" res))))
  10657. (defvar org-refile-history nil
  10658. "History for refiling operations.")
  10659. (defvar org-after-refile-insert-hook nil
  10660. "Hook run after `org-refile' has inserted its stuff at the new location.
  10661. Note that this is still *before* the stuff will be removed from
  10662. the *old* location.")
  10663. (defvar org-capture-last-stored-marker)
  10664. (defvar org-refile-keep nil
  10665. "Non-nil means `org-refile' will copy instead of refile.")
  10666. (defun org-copy ()
  10667. "Like `org-refile', but copy."
  10668. (interactive)
  10669. (let ((org-refile-keep t))
  10670. (funcall 'org-refile nil nil nil "Copy")))
  10671. (defun org-refile (&optional arg default-buffer rfloc msg)
  10672. "Move the entry or entries at point to another heading.
  10673. The list of target headings is compiled using the information in
  10674. `org-refile-targets', which see.
  10675. At the target location, the entry is filed as a subitem of the
  10676. target heading. Depending on `org-reverse-note-order', the new
  10677. subitem will either be the first or the last subitem.
  10678. If there is an active region, all entries in that region will be
  10679. refiled. However, the region must fulfill the requirement that
  10680. the first heading sets the top-level of the moved text.
  10681. With prefix arg ARG, the command will only visit the target
  10682. location and not actually move anything.
  10683. With a double prefix arg \\[universal-argument] \\[universal-argument], go to the location where the last
  10684. refiling operation has put the subtree.
  10685. With a numeric prefix argument of `2', refile to the running clock.
  10686. With a numeric prefix argument of `3', emulate `org-refile-keep'
  10687. being set to `t' and copy to the target location, don't move it.
  10688. Beware that keeping refiled entries may result in duplicated ID
  10689. properties.
  10690. RFLOC can be a refile location obtained in a different way.
  10691. MSG is a string to replace \"Refile\" in the default prompt with
  10692. another verb. E.g. `org-copy' sets this parameter to \"Copy\".
  10693. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  10694. If you are using target caching (see `org-refile-use-cache'), you
  10695. have to clear the target cache in order to find new targets.
  10696. This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
  10697. prefix argument (`C-u C-u C-u C-c C-w')."
  10698. (interactive "P")
  10699. (if (member arg '(0 (64)))
  10700. (org-refile-cache-clear)
  10701. (let* ((actionmsg (cond (msg msg)
  10702. ((equal arg 3) "Refile (and keep)")
  10703. (t "Refile")))
  10704. (cbuf (current-buffer))
  10705. (regionp (org-region-active-p))
  10706. (region-start (and regionp (region-beginning)))
  10707. (region-end (and regionp (region-end)))
  10708. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  10709. (org-refile-keep (if (equal arg 3) t org-refile-keep))
  10710. pos it nbuf file re level reversed)
  10711. (setq last-command nil)
  10712. (when regionp
  10713. (goto-char region-start)
  10714. (or (bolp) (goto-char (point-at-bol)))
  10715. (setq region-start (point))
  10716. (unless (or (org-kill-is-subtree-p
  10717. (buffer-substring region-start region-end))
  10718. (prog1 org-refile-active-region-within-subtree
  10719. (let ((s (point-at-eol)))
  10720. (org-toggle-heading)
  10721. (setq region-end (+ (- (point-at-eol) s) region-end)))))
  10722. (user-error "The region is not a (sequence of) subtree(s)")))
  10723. (if (equal arg '(16))
  10724. (org-refile-goto-last-stored)
  10725. (when (or
  10726. (and (equal arg 2)
  10727. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  10728. (prog1
  10729. (setq it (list (or org-clock-heading "running clock")
  10730. (buffer-file-name
  10731. (marker-buffer org-clock-hd-marker))
  10732. ""
  10733. (marker-position org-clock-hd-marker)))
  10734. (setq arg nil)))
  10735. (setq it (or rfloc
  10736. (let (heading-text)
  10737. (save-excursion
  10738. (unless (and arg (listp arg))
  10739. (org-back-to-heading t)
  10740. (setq heading-text
  10741. (replace-regexp-in-string
  10742. org-bracket-link-regexp
  10743. "\\3"
  10744. (nth 4 (org-heading-components)))))
  10745. (org-refile-get-location
  10746. (cond ((and arg (listp arg)) "Goto")
  10747. (regionp (concat actionmsg " region to"))
  10748. (t (concat actionmsg " subtree \""
  10749. heading-text "\" to")))
  10750. default-buffer
  10751. (and (not (equal '(4) arg))
  10752. org-refile-allow-creating-parent-nodes)
  10753. arg))))))
  10754. (setq file (nth 1 it)
  10755. re (nth 2 it)
  10756. pos (nth 3 it))
  10757. (if (and (not arg)
  10758. pos
  10759. (equal (buffer-file-name) file)
  10760. (if regionp
  10761. (and (>= pos region-start)
  10762. (<= pos region-end))
  10763. (and (>= pos (point))
  10764. (< pos (save-excursion
  10765. (org-end-of-subtree t t))))))
  10766. (error "Cannot refile to position inside the tree or region"))
  10767. (setq nbuf (or (find-buffer-visiting file)
  10768. (find-file-noselect file)))
  10769. (if (and arg (not (equal arg 3)))
  10770. (progn
  10771. (org-pop-to-buffer-same-window nbuf)
  10772. (goto-char pos)
  10773. (org-show-context 'org-goto))
  10774. (if regionp
  10775. (progn
  10776. (org-kill-new (buffer-substring region-start region-end))
  10777. (org-save-markers-in-region region-start region-end))
  10778. (org-copy-subtree 1 nil t))
  10779. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  10780. (find-file-noselect file)))
  10781. (setq reversed (org-notes-order-reversed-p))
  10782. (save-excursion
  10783. (save-restriction
  10784. (widen)
  10785. (if pos
  10786. (progn
  10787. (goto-char pos)
  10788. (looking-at org-outline-regexp)
  10789. (setq level (org-get-valid-level (funcall outline-level) 1))
  10790. (goto-char
  10791. (if reversed
  10792. (or (outline-next-heading) (point-max))
  10793. (or (save-excursion (org-get-next-sibling))
  10794. (org-end-of-subtree t t)
  10795. (point-max)))))
  10796. (setq level 1)
  10797. (if (not reversed)
  10798. (goto-char (point-max))
  10799. (goto-char (point-min))
  10800. (or (outline-next-heading) (goto-char (point-max)))))
  10801. (if (not (bolp)) (newline))
  10802. (org-paste-subtree level nil nil t)
  10803. (when org-log-refile
  10804. (org-add-log-setup 'refile nil nil 'findpos org-log-refile)
  10805. (unless (eq org-log-refile 'note)
  10806. (save-excursion (org-add-log-note))))
  10807. (and org-auto-align-tags
  10808. (let ((org-loop-over-headlines-in-active-region nil))
  10809. (org-set-tags nil t)))
  10810. (let ((bookmark-name (plist-get org-bookmark-names-plist
  10811. :last-refile)))
  10812. (when bookmark-name
  10813. (with-demoted-errors
  10814. (bookmark-set bookmark-name))))
  10815. ;; If we are refiling for capture, make sure that the
  10816. ;; last-capture pointers point here
  10817. (when (org-bound-and-true-p org-refile-for-capture)
  10818. (let ((bookmark-name (plist-get org-bookmark-names-plist
  10819. :last-capture-marker)))
  10820. (when bookmark-name
  10821. (with-demoted-errors
  10822. (bookmark-set bookmark-name))))
  10823. (move-marker org-capture-last-stored-marker (point)))
  10824. (if (fboundp 'deactivate-mark) (deactivate-mark))
  10825. (run-hooks 'org-after-refile-insert-hook))))
  10826. (unless org-refile-keep
  10827. (if regionp
  10828. (delete-region (point) (+ (point) (- region-end region-start)))
  10829. (delete-region
  10830. (and (org-back-to-heading t) (point))
  10831. (min (1+ (buffer-size)) (org-end-of-subtree t t) (point)))))
  10832. (when (featurep 'org-inlinetask)
  10833. (org-inlinetask-remove-END-maybe))
  10834. (setq org-markers-to-move nil)
  10835. (message (concat actionmsg " to \"%s\" in file %s: done") (car it) file)))))))
  10836. (defun org-refile-goto-last-stored ()
  10837. "Go to the location where the last refile was stored."
  10838. (interactive)
  10839. (bookmark-jump (plist-get org-bookmark-names-plist :last-refile))
  10840. (message "This is the location of the last refile"))
  10841. (defun org-refile--get-location (refloc tbl)
  10842. "When user refile to REFLOC, find the associated target in TBL.
  10843. Also check `org-refile-target-table'."
  10844. (car (delq
  10845. nil
  10846. (mapcar
  10847. (lambda (r) (or (assoc r tbl)
  10848. (assoc r org-refile-target-table)))
  10849. (list (replace-regexp-in-string "/$" "" refloc)
  10850. (replace-regexp-in-string "\\([^/]\\)$" "\\1/" refloc))))))
  10851. (defun org-refile-get-location (&optional prompt default-buffer new-nodes
  10852. no-exclude)
  10853. "Prompt the user for a refile location, using PROMPT.
  10854. PROMPT should not be suffixed with a colon and a space, because
  10855. this function appends the default value from
  10856. `org-refile-history' automatically, if that is not empty.
  10857. When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
  10858. this is used for the GOTO interface."
  10859. (let ((org-refile-targets org-refile-targets)
  10860. (org-refile-use-outline-path org-refile-use-outline-path)
  10861. excluded-entries)
  10862. (when (and (derived-mode-p 'org-mode)
  10863. (not org-refile-use-cache)
  10864. (not no-exclude))
  10865. (org-map-tree
  10866. (lambda()
  10867. (setq excluded-entries
  10868. (append excluded-entries (list (org-get-heading t t)))))))
  10869. (setq org-refile-target-table
  10870. (org-refile-get-targets default-buffer excluded-entries)))
  10871. (unless org-refile-target-table
  10872. (user-error "No refile targets"))
  10873. (let* ((cbuf (current-buffer))
  10874. (partial-completion-mode nil)
  10875. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  10876. (cfunc (if (and org-refile-use-outline-path
  10877. org-outline-path-complete-in-steps)
  10878. 'org-olpath-completing-read
  10879. 'org-icompleting-read))
  10880. (extra (if org-refile-use-outline-path "/" ""))
  10881. (cbnex (concat (buffer-name) extra))
  10882. (filename (and cfn (expand-file-name cfn)))
  10883. (tbl (mapcar
  10884. (lambda (x)
  10885. (if (and (not (member org-refile-use-outline-path
  10886. '(file full-file-path)))
  10887. (not (equal filename (nth 1 x))))
  10888. (cons (concat (car x) extra " ("
  10889. (file-name-nondirectory (nth 1 x)) ")")
  10890. (cdr x))
  10891. (cons (concat (car x) extra) (cdr x))))
  10892. org-refile-target-table))
  10893. (completion-ignore-case t)
  10894. cdef
  10895. (prompt (concat prompt
  10896. (or (and (car org-refile-history)
  10897. (concat " (default " (car org-refile-history) ")"))
  10898. (and (assoc cbnex tbl) (setq cdef cbnex)
  10899. (concat " (default " cbnex ")"))) ": "))
  10900. pa answ parent-target child parent old-hist)
  10901. (setq old-hist org-refile-history)
  10902. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  10903. nil 'org-refile-history (or cdef (car org-refile-history))))
  10904. (if (setq pa (org-refile--get-location answ tbl))
  10905. (progn
  10906. (org-refile-check-position pa)
  10907. (when (or (not org-refile-history)
  10908. (not (eq old-hist org-refile-history))
  10909. (not (equal (car pa) (car org-refile-history))))
  10910. (setq org-refile-history
  10911. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  10912. org-refile-history
  10913. (cdr org-refile-history))))
  10914. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  10915. (pop org-refile-history)))
  10916. pa)
  10917. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  10918. (progn
  10919. (setq parent (match-string 1 answ)
  10920. child (match-string 2 answ))
  10921. (setq parent-target (org-refile--get-location parent tbl))
  10922. (when (and parent-target
  10923. (or (eq new-nodes t)
  10924. (and (eq new-nodes 'confirm)
  10925. (y-or-n-p (format "Create new node \"%s\"? "
  10926. child)))))
  10927. (org-refile-new-child parent-target child)))
  10928. (user-error "Invalid target location")))))
  10929. (declare-function org-string-nw-p "org-macs" (s))
  10930. (defun org-refile-check-position (refile-pointer)
  10931. "Check if the refile pointer matches the headline to which it points."
  10932. (let* ((file (nth 1 refile-pointer))
  10933. (re (nth 2 refile-pointer))
  10934. (pos (nth 3 refile-pointer))
  10935. buffer)
  10936. (if (and (not (markerp pos)) (not file))
  10937. (user-error "Please indicate a target file in the refile path")
  10938. (when (org-string-nw-p re)
  10939. (setq buffer (if (markerp pos)
  10940. (marker-buffer pos)
  10941. (or (find-buffer-visiting file)
  10942. (find-file-noselect file))))
  10943. (with-current-buffer buffer
  10944. (save-excursion
  10945. (save-restriction
  10946. (widen)
  10947. (goto-char pos)
  10948. (beginning-of-line 1)
  10949. (unless (org-looking-at-p re)
  10950. (user-error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling")))))))))
  10951. (defun org-refile-new-child (parent-target child)
  10952. "Use refile target PARENT-TARGET to add new CHILD below it."
  10953. (unless parent-target
  10954. (error "Cannot find parent for new node"))
  10955. (let ((file (nth 1 parent-target))
  10956. (pos (nth 3 parent-target))
  10957. level)
  10958. (with-current-buffer (or (find-buffer-visiting file)
  10959. (find-file-noselect file))
  10960. (save-excursion
  10961. (save-restriction
  10962. (widen)
  10963. (if pos
  10964. (goto-char pos)
  10965. (goto-char (point-max))
  10966. (if (not (bolp)) (newline)))
  10967. (when (looking-at org-outline-regexp)
  10968. (setq level (funcall outline-level))
  10969. (org-end-of-subtree t t))
  10970. (org-back-over-empty-lines)
  10971. (insert "\n" (make-string
  10972. (if pos (org-get-valid-level level 1) 1) ?*)
  10973. " " child "\n")
  10974. (beginning-of-line 0)
  10975. (list (concat (car parent-target) "/" child) file "" (point)))))))
  10976. (defun org-olpath-completing-read (prompt collection &rest args)
  10977. "Read an outline path like a file name."
  10978. (let ((thetable collection)
  10979. (org-completion-use-ido nil) ; does not work with ido.
  10980. (org-completion-use-iswitchb nil)) ; or iswitchb
  10981. (apply
  10982. 'org-icompleting-read prompt
  10983. (lambda (string predicate &optional flag)
  10984. (let (rtn r f (l (length string)))
  10985. (cond
  10986. ((eq flag nil)
  10987. ;; try completion
  10988. (try-completion string thetable))
  10989. ((eq flag t)
  10990. ;; all-completions
  10991. (setq rtn (all-completions string thetable predicate))
  10992. (mapcar
  10993. (lambda (x)
  10994. (setq r (substring x l))
  10995. (if (string-match " ([^)]*)$" x)
  10996. (setq f (match-string 0 x))
  10997. (setq f ""))
  10998. (if (string-match "/" r)
  10999. (concat string (substring r 0 (match-end 0)) f)
  11000. x))
  11001. rtn))
  11002. ((eq flag 'lambda)
  11003. ;; exact match?
  11004. (assoc string thetable)))))
  11005. args)))
  11006. ;;;; Dynamic blocks
  11007. (defun org-find-dblock (name)
  11008. "Find the first dynamic block with name NAME in the buffer.
  11009. If not found, stay at current position and return nil."
  11010. (let ((case-fold-search t) pos)
  11011. (save-excursion
  11012. (goto-char (point-min))
  11013. (setq pos (and (re-search-forward
  11014. (concat "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+" name "\\>") nil t)
  11015. (match-beginning 0))))
  11016. (if pos (goto-char pos))
  11017. pos))
  11018. (defun org-create-dblock (plist)
  11019. "Create a dynamic block section, with parameters taken from PLIST.
  11020. PLIST must contain a :name entry which is used as the name of the block."
  11021. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  11022. (end-of-line 1)
  11023. (newline))
  11024. (let ((col (current-column))
  11025. (name (plist-get plist :name)))
  11026. (insert "#+BEGIN: " name)
  11027. (while plist
  11028. (if (eq (car plist) :name)
  11029. (setq plist (cddr plist))
  11030. (insert " " (prin1-to-string (pop plist)))))
  11031. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  11032. (beginning-of-line -2)))
  11033. (defun org-prepare-dblock ()
  11034. "Prepare dynamic block for refresh.
  11035. This empties the block, puts the cursor at the insert position and returns
  11036. the property list including an extra property :name with the block name."
  11037. (unless (looking-at org-dblock-start-re)
  11038. (user-error "Not at a dynamic block"))
  11039. (let* ((begdel (1+ (match-end 0)))
  11040. (name (org-no-properties (match-string 1)))
  11041. (params (append (list :name name)
  11042. (read (concat "(" (match-string 3) ")")))))
  11043. (save-excursion
  11044. (beginning-of-line 1)
  11045. (skip-chars-forward " \t")
  11046. (setq params (plist-put params :indentation-column (current-column))))
  11047. (unless (re-search-forward org-dblock-end-re nil t)
  11048. (error "Dynamic block not terminated"))
  11049. (setq params
  11050. (append params
  11051. (list :content (buffer-substring
  11052. begdel (match-beginning 0)))))
  11053. (delete-region begdel (match-beginning 0))
  11054. (goto-char begdel)
  11055. (open-line 1)
  11056. params))
  11057. (defun org-map-dblocks (&optional command)
  11058. "Apply COMMAND to all dynamic blocks in the current buffer.
  11059. If COMMAND is not given, use `org-update-dblock'."
  11060. (let ((cmd (or command 'org-update-dblock)))
  11061. (save-excursion
  11062. (goto-char (point-min))
  11063. (while (re-search-forward org-dblock-start-re nil t)
  11064. (goto-char (match-beginning 0))
  11065. (save-excursion
  11066. (condition-case nil
  11067. (funcall cmd)
  11068. (error (message "Error during update of dynamic block"))))
  11069. (unless (re-search-forward org-dblock-end-re nil t)
  11070. (error "Dynamic block not terminated"))))))
  11071. (defun org-dblock-update (&optional arg)
  11072. "User command for updating dynamic blocks.
  11073. Update the dynamic block at point. With prefix ARG, update all dynamic
  11074. blocks in the buffer."
  11075. (interactive "P")
  11076. (if arg
  11077. (org-update-all-dblocks)
  11078. (or (looking-at org-dblock-start-re)
  11079. (org-beginning-of-dblock))
  11080. (org-update-dblock)))
  11081. (defun org-update-dblock ()
  11082. "Update the dynamic block at point.
  11083. This means to empty the block, parse for parameters and then call
  11084. the correct writing function."
  11085. (interactive)
  11086. (save-excursion
  11087. (let* ((win (selected-window))
  11088. (pos (point))
  11089. (line (org-current-line))
  11090. (params (org-prepare-dblock))
  11091. (name (plist-get params :name))
  11092. (indent (plist-get params :indentation-column))
  11093. (cmd (intern (concat "org-dblock-write:" name))))
  11094. (message "Updating dynamic block `%s' at line %d..." name line)
  11095. (funcall cmd params)
  11096. (message "Updating dynamic block `%s' at line %d...done" name line)
  11097. (goto-char pos)
  11098. (when (and indent (> indent 0))
  11099. (setq indent (make-string indent ?\ ))
  11100. (save-excursion
  11101. (select-window win)
  11102. (org-beginning-of-dblock)
  11103. (forward-line 1)
  11104. (while (not (looking-at org-dblock-end-re))
  11105. (insert indent)
  11106. (beginning-of-line 2))
  11107. (when (looking-at org-dblock-end-re)
  11108. (and (looking-at "[ \t]+")
  11109. (replace-match ""))
  11110. (insert indent)))))))
  11111. (defun org-beginning-of-dblock ()
  11112. "Find the beginning of the dynamic block at point.
  11113. Error if there is no such block at point."
  11114. (let ((pos (point))
  11115. beg)
  11116. (end-of-line 1)
  11117. (if (and (re-search-backward org-dblock-start-re nil t)
  11118. (setq beg (match-beginning 0))
  11119. (re-search-forward org-dblock-end-re nil t)
  11120. (> (match-end 0) pos))
  11121. (goto-char beg)
  11122. (goto-char pos)
  11123. (error "Not in a dynamic block"))))
  11124. (defun org-update-all-dblocks ()
  11125. "Update all dynamic blocks in the buffer.
  11126. This function can be used in a hook."
  11127. (interactive)
  11128. (when (derived-mode-p 'org-mode)
  11129. (org-map-dblocks 'org-update-dblock)))
  11130. ;;;; Completion
  11131. (declare-function org-export-backend-name "org-export" (cl-x))
  11132. (declare-function org-export-backend-options "org-export" (cl-x))
  11133. (defun org-get-export-keywords ()
  11134. "Return a list of all currently understood export keywords.
  11135. Export keywords include options, block names, attributes and
  11136. keywords relative to each registered export back-end."
  11137. (let (keywords)
  11138. (dolist (backend
  11139. (org-bound-and-true-p org-export--registered-backends)
  11140. (delq nil keywords))
  11141. ;; Back-end name (for keywords, like #+LATEX:)
  11142. (push (upcase (symbol-name (org-export-backend-name backend))) keywords)
  11143. (dolist (option-entry (org-export-backend-options backend))
  11144. ;; Back-end options.
  11145. (push (nth 1 option-entry) keywords)))))
  11146. (defconst org-options-keywords
  11147. '("ARCHIVE:" "AUTHOR:" "BIND:" "CATEGORY:" "COLUMNS:" "CREATOR:" "DATE:"
  11148. "DESCRIPTION:" "DRAWERS:" "EMAIL:" "EXCLUDE_TAGS:" "FILETAGS:" "INCLUDE:"
  11149. "INDEX:" "KEYWORDS:" "LANGUAGE:" "MACRO:" "OPTIONS:" "PROPERTY:"
  11150. "PRIORITIES:" "SELECT_TAGS:" "SEQ_TODO:" "SETUPFILE:" "STARTUP:" "TAGS:"
  11151. "TITLE:" "TODO:" "TYP_TODO:" "SELECT_TAGS:" "EXCLUDE_TAGS:"))
  11152. (defcustom org-structure-template-alist
  11153. '(("s" "#+BEGIN_SRC ?\n\n#+END_SRC")
  11154. ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE")
  11155. ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE")
  11156. ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE")
  11157. ("V" "#+BEGIN_VERBATIM\n?\n#+END_VERBATIM")
  11158. ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER")
  11159. ("l" "#+BEGIN_LaTeX\n?\n#+END_LaTeX")
  11160. ("L" "#+LaTeX: ")
  11161. ("h" "#+BEGIN_HTML\n?\n#+END_HTML")
  11162. ("H" "#+HTML: ")
  11163. ("a" "#+BEGIN_ASCII\n?\n#+END_ASCII")
  11164. ("A" "#+ASCII: ")
  11165. ("i" "#+INDEX: ?")
  11166. ("I" "#+INCLUDE: %file ?"))
  11167. "Structure completion elements.
  11168. This is a list of abbreviation keys and values. The value gets inserted
  11169. if you type `<' followed by the key and then press the completion key,
  11170. usually `TAB'. %file will be replaced by a file name after prompting
  11171. for the file using completion. The cursor will be placed at the position
  11172. of the `?` in the template.
  11173. There are two templates for each key, the first uses the original Org syntax,
  11174. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  11175. the default when the /org-mtags.el/ module has been loaded. See also the
  11176. variable `org-mtags-prefer-muse-templates'."
  11177. :group 'org-completion
  11178. :type '(repeat
  11179. (list
  11180. (string :tag "Key")
  11181. (string :tag "Template")))
  11182. :version "25.1"
  11183. :package-version '(Org . "8.3"))
  11184. (defun org-try-structure-completion ()
  11185. "Try to complete a structure template before point.
  11186. This looks for strings like \"<e\" on an otherwise empty line and
  11187. expands them."
  11188. (let ((l (buffer-substring (point-at-bol) (point)))
  11189. a)
  11190. (when (and (looking-at "[ \t]*$")
  11191. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  11192. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  11193. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  11194. (match-beginning 1)) a)
  11195. t)))
  11196. (defun org-complete-expand-structure-template (start cell)
  11197. "Expand a structure template."
  11198. (let ((rpl (nth 1 cell))
  11199. (ind ""))
  11200. (delete-region start (point))
  11201. (when (string-match "\\`[ \t]*#\\+" rpl)
  11202. (cond
  11203. ((bolp))
  11204. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  11205. (setq ind (buffer-substring (point-at-bol) (point))))
  11206. (t (newline))))
  11207. (setq start (point))
  11208. (if (string-match "%file" rpl)
  11209. (setq rpl (replace-match
  11210. (concat
  11211. "\""
  11212. (save-match-data
  11213. (abbreviate-file-name (read-file-name "Include file: ")))
  11214. "\"")
  11215. t t rpl)))
  11216. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  11217. (concat "\n" ind)))
  11218. (insert rpl)
  11219. (if (re-search-backward "\\?" start t) (delete-char 1))))
  11220. ;;;; TODO, DEADLINE, Comments
  11221. (defun org-toggle-comment ()
  11222. "Change the COMMENT state of an entry."
  11223. (interactive)
  11224. (save-excursion
  11225. (org-back-to-heading)
  11226. (looking-at org-complex-heading-regexp)
  11227. (goto-char (or (match-end 3) (match-end 2) (match-end 1)))
  11228. (skip-chars-forward " \t")
  11229. (unless (memq (char-before) '(?\s ?\t)) (insert " "))
  11230. (if (org-in-commented-heading-p t)
  11231. (delete-region (point)
  11232. (progn (search-forward " " (line-end-position) 'move)
  11233. (skip-chars-forward " \t")
  11234. (point)))
  11235. (insert org-comment-string)
  11236. (unless (eolp) (insert " ")))))
  11237. (defvar org-last-todo-state-is-todo nil
  11238. "This is non-nil when the last TODO state change led to a TODO state.
  11239. If the last change removed the TODO tag or switched to DONE, then
  11240. this is nil.")
  11241. (defvar org-setting-tags nil) ; dynamically skipped
  11242. (defvar org-todo-setup-filter-hook nil
  11243. "Hook for functions that pre-filter todo specs.
  11244. Each function takes a todo spec and returns either nil or the spec
  11245. transformed into canonical form." )
  11246. (defvar org-todo-get-default-hook nil
  11247. "Hook for functions that get a default item for todo.
  11248. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  11249. nil or a string to be used for the todo mark." )
  11250. (defvar org-agenda-headline-snapshot-before-repeat)
  11251. (defun org-current-effective-time ()
  11252. "Return current time adjusted for `org-extend-today-until' variable."
  11253. (let* ((ct (org-current-time))
  11254. (dct (decode-time ct))
  11255. (ct1
  11256. (cond
  11257. (org-use-last-clock-out-time-as-effective-time
  11258. (or (org-clock-get-last-clock-out-time) ct))
  11259. ((and org-use-effective-time (< (nth 2 dct) org-extend-today-until))
  11260. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct)))
  11261. (t ct))))
  11262. ct1))
  11263. (defun org-todo-yesterday (&optional arg)
  11264. "Like `org-todo' but the time of change will be 23:59 of yesterday."
  11265. (interactive "P")
  11266. (if (eq major-mode 'org-agenda-mode)
  11267. (apply 'org-agenda-todo-yesterday arg)
  11268. (let* ((hour (third (decode-time
  11269. (org-current-time))))
  11270. (org-extend-today-until (1+ hour)))
  11271. (org-todo arg))))
  11272. (defvar org-block-entry-blocking ""
  11273. "First entry preventing the TODO state change.")
  11274. (defun org-cancel-repeater ()
  11275. "Cancel a repeater by setting its numeric value to zero."
  11276. (interactive)
  11277. (save-excursion
  11278. (org-back-to-heading t)
  11279. (let ((bound1 (point))
  11280. (bound0 (save-excursion (outline-next-heading) (point))))
  11281. (when (re-search-forward
  11282. (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11283. org-deadline-time-regexp "\\)\\|\\("
  11284. org-ts-regexp "\\)")
  11285. bound0 t)
  11286. (if (re-search-backward "[ \t]+\\(?:[.+]\\)?\\+\\([0-9]+\\)[hdwmy]" bound1 t)
  11287. (replace-match "0" t nil nil 1))))))
  11288. (defun org-todo (&optional arg)
  11289. "Change the TODO state of an item.
  11290. The state of an item is given by a keyword at the start of the heading,
  11291. like
  11292. *** TODO Write paper
  11293. *** DONE Call mom
  11294. The different keywords are specified in the variable `org-todo-keywords'.
  11295. By default the available states are \"TODO\" and \"DONE\".
  11296. So for this example: when the item starts with TODO, it is changed to DONE.
  11297. When it starts with DONE, the DONE is removed. And when neither TODO nor
  11298. DONE are present, add TODO at the beginning of the heading.
  11299. With \\[universal-argument] prefix arg, use completion to determine the new \
  11300. state.
  11301. With numeric prefix arg, switch to that state.
  11302. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  11303. keywords (nextset).
  11304. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  11305. With a numeric prefix arg of 0, inhibit note taking for the change.
  11306. With a numeric prefix arg of -1, cancel repeater to allow marking as DONE.
  11307. When called through ELisp, arg is also interpreted in the following way:
  11308. 'none -> empty state
  11309. \"\"(empty string) -> switch to empty state
  11310. 'done -> switch to DONE
  11311. 'nextset -> switch to the next set of keywords
  11312. 'previousset -> switch to the previous set of keywords
  11313. \"WAITING\" -> switch to the specified keyword, but only if it
  11314. really is a member of `org-todo-keywords'."
  11315. (interactive "P")
  11316. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11317. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11318. 'region-start-level 'region))
  11319. org-loop-over-headlines-in-active-region)
  11320. (org-map-entries
  11321. `(org-todo ,arg)
  11322. org-loop-over-headlines-in-active-region
  11323. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11324. (if (equal arg '(16)) (setq arg 'nextset))
  11325. (when (equal arg -1) (org-cancel-repeater) (setq arg nil))
  11326. (let ((org-blocker-hook org-blocker-hook)
  11327. commentp
  11328. case-fold-search)
  11329. (when (equal arg '(64))
  11330. (setq arg nil org-blocker-hook nil))
  11331. (when (and org-blocker-hook
  11332. (or org-inhibit-blocking
  11333. (org-entry-get nil "NOBLOCKING")))
  11334. (setq org-blocker-hook nil))
  11335. (save-excursion
  11336. (catch 'exit
  11337. (org-back-to-heading t)
  11338. (when (org-in-commented-heading-p t)
  11339. (org-toggle-comment)
  11340. (setq commentp t))
  11341. (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
  11342. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
  11343. (looking-at "\\(?: *\\|[ \t]*$\\)"))
  11344. (let* ((match-data (match-data))
  11345. (startpos (point-at-bol))
  11346. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  11347. (org-log-done org-log-done)
  11348. (org-log-repeat org-log-repeat)
  11349. (org-todo-log-states org-todo-log-states)
  11350. (org-inhibit-logging
  11351. (if (equal arg 0)
  11352. (progn (setq arg nil) 'note) org-inhibit-logging))
  11353. (this (match-string 1))
  11354. (hl-pos (match-beginning 0))
  11355. (head (org-get-todo-sequence-head this))
  11356. (ass (assoc head org-todo-kwd-alist))
  11357. (interpret (nth 1 ass))
  11358. (done-word (nth 3 ass))
  11359. (final-done-word (nth 4 ass))
  11360. (org-last-state (or this ""))
  11361. (completion-ignore-case t)
  11362. (member (member this org-todo-keywords-1))
  11363. (tail (cdr member))
  11364. (org-state (cond
  11365. ((and org-todo-key-trigger
  11366. (or (and (equal arg '(4))
  11367. (eq org-use-fast-todo-selection 'prefix))
  11368. (and (not arg) org-use-fast-todo-selection
  11369. (not (eq org-use-fast-todo-selection
  11370. 'prefix)))))
  11371. ;; Use fast selection
  11372. (org-fast-todo-selection))
  11373. ((and (equal arg '(4))
  11374. (or (not org-use-fast-todo-selection)
  11375. (not org-todo-key-trigger)))
  11376. ;; Read a state with completion
  11377. (org-icompleting-read
  11378. "State: " (mapcar 'list org-todo-keywords-1)
  11379. nil t))
  11380. ((eq arg 'right)
  11381. (if this
  11382. (if tail (car tail) nil)
  11383. (car org-todo-keywords-1)))
  11384. ((eq arg 'left)
  11385. (if (equal member org-todo-keywords-1)
  11386. nil
  11387. (if this
  11388. (nth (- (length org-todo-keywords-1)
  11389. (length tail) 2)
  11390. org-todo-keywords-1)
  11391. (org-last org-todo-keywords-1))))
  11392. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  11393. (setq arg nil))) ; hack to fall back to cycling
  11394. (arg
  11395. ;; user or caller requests a specific state
  11396. (cond
  11397. ((equal arg "") nil)
  11398. ((eq arg 'none) nil)
  11399. ((eq arg 'done) (or done-word (car org-done-keywords)))
  11400. ((eq arg 'nextset)
  11401. (or (car (cdr (member head org-todo-heads)))
  11402. (car org-todo-heads)))
  11403. ((eq arg 'previousset)
  11404. (let ((org-todo-heads (reverse org-todo-heads)))
  11405. (or (car (cdr (member head org-todo-heads)))
  11406. (car org-todo-heads))))
  11407. ((car (member arg org-todo-keywords-1)))
  11408. ((stringp arg)
  11409. (user-error "State `%s' not valid in this file" arg))
  11410. ((nth (1- (prefix-numeric-value arg))
  11411. org-todo-keywords-1))))
  11412. ((null member) (or head (car org-todo-keywords-1)))
  11413. ((equal this final-done-word) nil) ;; -> make empty
  11414. ((null tail) nil) ;; -> first entry
  11415. ((memq interpret '(type priority))
  11416. (if (eq this-command last-command)
  11417. (car tail)
  11418. (if (> (length tail) 0)
  11419. (or done-word (car org-done-keywords))
  11420. nil)))
  11421. (t
  11422. (car tail))))
  11423. (org-state (or
  11424. (run-hook-with-args-until-success
  11425. 'org-todo-get-default-hook org-state org-last-state)
  11426. org-state))
  11427. (next (if org-state (concat " " org-state " ") " "))
  11428. (change-plist (list :type 'todo-state-change :from this :to org-state
  11429. :position startpos))
  11430. dolog now-done-p)
  11431. (when org-blocker-hook
  11432. (setq org-last-todo-state-is-todo
  11433. (not (member this org-done-keywords)))
  11434. (unless (save-excursion
  11435. (save-match-data
  11436. (org-with-wide-buffer
  11437. (run-hook-with-args-until-failure
  11438. 'org-blocker-hook change-plist))))
  11439. (if (org-called-interactively-p 'interactive)
  11440. (user-error "TODO state change from %s to %s blocked (by \"%s\")"
  11441. this org-state org-block-entry-blocking)
  11442. ;; fail silently
  11443. (message "TODO state change from %s to %s blocked (by \"%s\")"
  11444. this org-state org-block-entry-blocking)
  11445. (throw 'exit nil))))
  11446. (store-match-data match-data)
  11447. (replace-match next t t)
  11448. (cond ((equal this org-state)
  11449. (message "TODO state was already %s" (org-trim next)))
  11450. ((pos-visible-in-window-p hl-pos)
  11451. (message "TODO state changed to %s" (org-trim next))))
  11452. (unless head
  11453. (setq head (org-get-todo-sequence-head org-state)
  11454. ass (assoc head org-todo-kwd-alist)
  11455. interpret (nth 1 ass)
  11456. done-word (nth 3 ass)
  11457. final-done-word (nth 4 ass)))
  11458. (when (memq arg '(nextset previousset))
  11459. (message "Keyword-Set %d/%d: %s"
  11460. (- (length org-todo-sets) -1
  11461. (length (memq (assoc org-state org-todo-sets) org-todo-sets)))
  11462. (length org-todo-sets)
  11463. (mapconcat 'identity (assoc org-state org-todo-sets) " ")))
  11464. (setq org-last-todo-state-is-todo
  11465. (not (member org-state org-done-keywords)))
  11466. (setq now-done-p (and (member org-state org-done-keywords)
  11467. (not (member this org-done-keywords))))
  11468. (and logging (org-local-logging logging))
  11469. (when (and (or org-todo-log-states org-log-done)
  11470. (not (eq org-inhibit-logging t))
  11471. (not (memq arg '(nextset previousset))))
  11472. ;; we need to look at recording a time and note
  11473. (setq dolog (or (nth 1 (assoc org-state org-todo-log-states))
  11474. (nth 2 (assoc this org-todo-log-states))))
  11475. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  11476. (setq dolog 'time))
  11477. (when (or (and (not org-state) (not org-closed-keep-when-no-todo))
  11478. (and org-state
  11479. (member org-state org-not-done-keywords)
  11480. (not (member this org-not-done-keywords))))
  11481. ;; This is now a todo state and was not one before
  11482. ;; If there was a CLOSED time stamp, get rid of it.
  11483. (org-add-planning-info nil nil 'closed))
  11484. (when (and now-done-p org-log-done)
  11485. ;; It is now done, and it was not done before
  11486. (org-add-planning-info 'closed (org-current-effective-time))
  11487. (if (and (not dolog) (eq 'note org-log-done))
  11488. (org-add-log-setup 'done org-state this 'findpos 'note)))
  11489. (when (and org-state dolog)
  11490. ;; This is a non-nil state, and we need to log it
  11491. (org-add-log-setup 'state org-state this 'findpos dolog)))
  11492. ;; Fixup tag positioning
  11493. (org-todo-trigger-tag-changes org-state)
  11494. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  11495. (when org-provide-todo-statistics
  11496. (org-update-parent-todo-statistics))
  11497. (run-hooks 'org-after-todo-state-change-hook)
  11498. (if (and arg (not (member org-state org-done-keywords)))
  11499. (setq head (org-get-todo-sequence-head org-state)))
  11500. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  11501. ;; Do we need to trigger a repeat?
  11502. (when now-done-p
  11503. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  11504. ;; This is for the agenda, take a snapshot of the headline.
  11505. (save-match-data
  11506. (setq org-agenda-headline-snapshot-before-repeat
  11507. (org-get-heading))))
  11508. (org-auto-repeat-maybe org-state))
  11509. ;; Fixup cursor location if close to the keyword
  11510. (if (and (outline-on-heading-p)
  11511. (not (bolp))
  11512. (save-excursion (beginning-of-line 1)
  11513. (looking-at org-todo-line-regexp))
  11514. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  11515. (progn
  11516. (goto-char (or (match-end 2) (match-end 1)))
  11517. (and (looking-at " ") (just-one-space))))
  11518. (when org-trigger-hook
  11519. (save-excursion
  11520. (run-hook-with-args 'org-trigger-hook change-plist)))
  11521. (when commentp (org-toggle-comment))))))))
  11522. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  11523. "Block turning an entry into a TODO, using the hierarchy.
  11524. This checks whether the current task should be blocked from state
  11525. changes. Such blocking occurs when:
  11526. 1. The task has children which are not all in a completed state.
  11527. 2. A task has a parent with the property :ORDERED:, and there
  11528. are siblings prior to the current task with incomplete
  11529. status.
  11530. 3. The parent of the task is blocked because it has siblings that should
  11531. be done first, or is child of a block grandparent TODO entry."
  11532. (if (not org-enforce-todo-dependencies)
  11533. t ; if locally turned off don't block
  11534. (catch 'dont-block
  11535. ;; If this is not a todo state change, or if this entry is already DONE,
  11536. ;; do not block
  11537. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11538. (member (plist-get change-plist :from)
  11539. (cons 'done org-done-keywords))
  11540. (member (plist-get change-plist :to)
  11541. (cons 'todo org-not-done-keywords))
  11542. (not (plist-get change-plist :to)))
  11543. (throw 'dont-block t))
  11544. ;; If this task has children, and any are undone, it's blocked
  11545. (save-excursion
  11546. (org-back-to-heading t)
  11547. (let ((this-level (funcall outline-level)))
  11548. (outline-next-heading)
  11549. (let ((child-level (funcall outline-level)))
  11550. (while (and (not (eobp))
  11551. (> child-level this-level))
  11552. ;; this todo has children, check whether they are all
  11553. ;; completed
  11554. (if (and (not (org-entry-is-done-p))
  11555. (org-entry-is-todo-p))
  11556. (progn (setq org-block-entry-blocking (org-get-heading))
  11557. (throw 'dont-block nil)))
  11558. (outline-next-heading)
  11559. (setq child-level (funcall outline-level))))))
  11560. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  11561. ;; any previous siblings are undone, it's blocked
  11562. (save-excursion
  11563. (org-back-to-heading t)
  11564. (let* ((pos (point))
  11565. (parent-pos (and (org-up-heading-safe) (point))))
  11566. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  11567. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11568. (forward-line 1)
  11569. (re-search-forward org-not-done-heading-regexp pos t))
  11570. (setq org-block-entry-blocking (match-string 0))
  11571. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  11572. ;; Search further up the hierarchy, to see if an ancestor is blocked
  11573. (while t
  11574. (goto-char parent-pos)
  11575. (if (not (looking-at org-not-done-heading-regexp))
  11576. (throw 'dont-block t)) ; do not block, parent is not a TODO
  11577. (setq pos (point))
  11578. (setq parent-pos (and (org-up-heading-safe) (point)))
  11579. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  11580. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11581. (forward-line 1)
  11582. (re-search-forward org-not-done-heading-regexp pos t)
  11583. (setq org-block-entry-blocking (org-get-heading)))
  11584. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  11585. (defcustom org-track-ordered-property-with-tag nil
  11586. "Should the ORDERED property also be shown as a tag?
  11587. The ORDERED property decides if an entry should require subtasks to be
  11588. completed in sequence. Since a property is not very visible, setting
  11589. this option means that toggling the ORDERED property with the command
  11590. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  11591. not relevant for the behavior, but it makes things more visible.
  11592. Note that toggling the tag with tags commands will not change the property
  11593. and therefore not influence behavior!
  11594. This can be t, meaning the tag ORDERED should be used, It can also be a
  11595. string to select a different tag for this task."
  11596. :group 'org-todo
  11597. :type '(choice
  11598. (const :tag "No tracking" nil)
  11599. (const :tag "Track with ORDERED tag" t)
  11600. (string :tag "Use other tag")))
  11601. (defun org-toggle-ordered-property ()
  11602. "Toggle the ORDERED property of the current entry.
  11603. For better visibility, you can track the value of this property with a tag.
  11604. See variable `org-track-ordered-property-with-tag'."
  11605. (interactive)
  11606. (let* ((t1 org-track-ordered-property-with-tag)
  11607. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  11608. (save-excursion
  11609. (org-back-to-heading)
  11610. (if (org-entry-get nil "ORDERED")
  11611. (progn
  11612. (org-delete-property "ORDERED")
  11613. (and tag (org-toggle-tag tag 'off))
  11614. (message "Subtasks can be completed in arbitrary order"))
  11615. (org-entry-put nil "ORDERED" "t")
  11616. (and tag (org-toggle-tag tag 'on))
  11617. (message "Subtasks must be completed in sequence")))))
  11618. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  11619. (defun org-block-todo-from-checkboxes (change-plist)
  11620. "Block turning an entry into a TODO, using checkboxes.
  11621. This checks whether the current task should be blocked from state
  11622. changes because there are unchecked boxes in this entry."
  11623. (if (not org-enforce-todo-checkbox-dependencies)
  11624. t ; if locally turned off don't block
  11625. (catch 'dont-block
  11626. ;; If this is not a todo state change, or if this entry is already DONE,
  11627. ;; do not block
  11628. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11629. (member (plist-get change-plist :from)
  11630. (cons 'done org-done-keywords))
  11631. (member (plist-get change-plist :to)
  11632. (cons 'todo org-not-done-keywords))
  11633. (not (plist-get change-plist :to)))
  11634. (throw 'dont-block t))
  11635. ;; If this task has checkboxes that are not checked, it's blocked
  11636. (save-excursion
  11637. (org-back-to-heading t)
  11638. (let ((beg (point)) end)
  11639. (outline-next-heading)
  11640. (setq end (point))
  11641. (goto-char beg)
  11642. (if (org-list-search-forward
  11643. (concat (org-item-beginning-re)
  11644. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
  11645. "\\[[- ]\\]")
  11646. end t)
  11647. (progn
  11648. (if (boundp 'org-blocked-by-checkboxes)
  11649. (setq org-blocked-by-checkboxes t))
  11650. (throw 'dont-block nil)))))
  11651. t))) ; do not block
  11652. (defun org-entry-blocked-p ()
  11653. "Is the current entry blocked?"
  11654. (org-with-silent-modifications
  11655. (if (org-entry-get nil "NOBLOCKING")
  11656. nil ;; Never block this entry
  11657. (not (run-hook-with-args-until-failure
  11658. 'org-blocker-hook
  11659. (list :type 'todo-state-change
  11660. :position (point)
  11661. :from 'todo
  11662. :to 'done))))))
  11663. (defun org-update-statistics-cookies (all)
  11664. "Update the statistics cookie, either from TODO or from checkboxes.
  11665. This should be called with the cursor in a line with a statistics cookie."
  11666. (interactive "P")
  11667. (if all
  11668. (progn
  11669. (org-update-checkbox-count 'all)
  11670. (org-map-entries 'org-update-parent-todo-statistics))
  11671. (if (not (org-at-heading-p))
  11672. (org-update-checkbox-count)
  11673. (let ((pos (point-marker))
  11674. end l1 l2)
  11675. (ignore-errors (org-back-to-heading t))
  11676. (if (not (org-at-heading-p))
  11677. (org-update-checkbox-count)
  11678. (setq l1 (org-outline-level))
  11679. (setq end (save-excursion
  11680. (outline-next-heading)
  11681. (if (org-at-heading-p) (setq l2 (org-outline-level)))
  11682. (point)))
  11683. (if (and (save-excursion
  11684. (re-search-forward
  11685. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  11686. (not (save-excursion (re-search-forward
  11687. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  11688. (org-update-checkbox-count)
  11689. (if (and l2 (> l2 l1))
  11690. (progn
  11691. (goto-char end)
  11692. (org-update-parent-todo-statistics))
  11693. (goto-char pos)
  11694. (beginning-of-line 1)
  11695. (while (re-search-forward
  11696. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  11697. (point-at-eol) t)
  11698. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  11699. (goto-char pos)
  11700. (move-marker pos nil)))))
  11701. (defvar org-entry-property-inherited-from) ;; defined below
  11702. (defun org-update-parent-todo-statistics ()
  11703. "Update any statistics cookie in the parent of the current headline.
  11704. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  11705. the entire subtree and this will travel up the hierarchy and update
  11706. statistics everywhere."
  11707. (let* ((prop (save-excursion (org-up-heading-safe)
  11708. (org-entry-get nil "COOKIE_DATA" 'inherit)))
  11709. (recursive (or (not org-hierarchical-todo-statistics)
  11710. (and prop (string-match "\\<recursive\\>" prop))))
  11711. (lim (or (and prop (marker-position org-entry-property-inherited-from))
  11712. 0))
  11713. (first t)
  11714. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  11715. level ltoggle l1 new ndel
  11716. (cnt-all 0) (cnt-done 0) is-percent kwd
  11717. checkbox-beg ov ovs ove cookie-present)
  11718. (catch 'exit
  11719. (save-excursion
  11720. (beginning-of-line 1)
  11721. (setq ltoggle (funcall outline-level))
  11722. ;; Three situations are to consider:
  11723. ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
  11724. ;; to the top-level ancestor on the headline;
  11725. ;; 2. If parent has "recursive" property, repeat up to the
  11726. ;; headline setting that property, taking inheritance into
  11727. ;; account;
  11728. ;; 3. Else, move up to direct parent and proceed only once.
  11729. (while (and (setq level (org-up-heading-safe))
  11730. (or recursive first)
  11731. (>= (point) lim))
  11732. (setq first nil cookie-present nil)
  11733. (unless (and level
  11734. (not (string-match
  11735. "\\<checkbox\\>"
  11736. (downcase (or (org-entry-get nil "COOKIE_DATA")
  11737. "")))))
  11738. (throw 'exit nil))
  11739. (while (re-search-forward box-re (point-at-eol) t)
  11740. (setq cnt-all 0 cnt-done 0 cookie-present t)
  11741. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  11742. (save-match-data
  11743. (unless (outline-next-heading) (throw 'exit nil))
  11744. (while (and (looking-at org-complex-heading-regexp)
  11745. (> (setq l1 (length (match-string 1))) level))
  11746. (setq kwd (and (or recursive (= l1 ltoggle))
  11747. (match-string 2)))
  11748. (if (or (eq org-provide-todo-statistics 'all-headlines)
  11749. (and (eq org-provide-todo-statistics t)
  11750. (or (member kwd org-done-keywords)))
  11751. (and (listp org-provide-todo-statistics)
  11752. (stringp (car org-provide-todo-statistics))
  11753. (or (member kwd org-provide-todo-statistics)
  11754. (member kwd org-done-keywords)))
  11755. (and (listp org-provide-todo-statistics)
  11756. (listp (car org-provide-todo-statistics))
  11757. (or (member kwd (car org-provide-todo-statistics))
  11758. (and (member kwd org-done-keywords)
  11759. (member kwd (cadr org-provide-todo-statistics))))))
  11760. (setq cnt-all (1+ cnt-all))
  11761. (if (eq org-provide-todo-statistics t)
  11762. (and kwd (setq cnt-all (1+ cnt-all)))))
  11763. (when (or (and (member org-provide-todo-statistics '(t all-headlines))
  11764. (member kwd org-done-keywords))
  11765. (and (listp org-provide-todo-statistics)
  11766. (listp (car org-provide-todo-statistics))
  11767. (member kwd org-done-keywords)
  11768. (member kwd (cadr org-provide-todo-statistics)))
  11769. (and (listp org-provide-todo-statistics)
  11770. (stringp (car org-provide-todo-statistics))
  11771. (member kwd org-done-keywords)))
  11772. (setq cnt-done (1+ cnt-done)))
  11773. (outline-next-heading)))
  11774. (setq new
  11775. (if is-percent
  11776. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  11777. (format "[%d/%d]" cnt-done cnt-all))
  11778. ndel (- (match-end 0) checkbox-beg))
  11779. ;; handle overlays when updating cookie from column view
  11780. (when (setq ov (car (overlays-at checkbox-beg)))
  11781. (setq ovs (overlay-start ov) ove (overlay-end ov))
  11782. (delete-overlay ov))
  11783. (goto-char checkbox-beg)
  11784. (insert new)
  11785. (delete-region (point) (+ (point) ndel))
  11786. (when org-auto-align-tags (org-fix-tags-on-the-fly))
  11787. (when ov (move-overlay ov ovs ove)))
  11788. (when cookie-present
  11789. (run-hook-with-args 'org-after-todo-statistics-hook
  11790. cnt-done (- cnt-all cnt-done))))))
  11791. (run-hooks 'org-todo-statistics-hook)))
  11792. (defvar org-after-todo-statistics-hook nil
  11793. "Hook that is called after a TODO statistics cookie has been updated.
  11794. Each function is called with two arguments: the number of not-done entries
  11795. and the number of done entries.
  11796. For example, the following function, when added to this hook, will switch
  11797. an entry to DONE when all children are done, and back to TODO when new
  11798. entries are set to a TODO status. Note that this hook is only called
  11799. when there is a statistics cookie in the headline!
  11800. (defun org-summary-todo (n-done n-not-done)
  11801. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  11802. (let (org-log-done org-log-states) ; turn off logging
  11803. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  11804. ")
  11805. (defvar org-todo-statistics-hook nil
  11806. "Hook that is run whenever Org thinks TODO statistics should be updated.
  11807. This hook runs even if there is no statistics cookie present, in which case
  11808. `org-after-todo-statistics-hook' would not run.")
  11809. (defun org-todo-trigger-tag-changes (state)
  11810. "Apply the changes defined in `org-todo-state-tags-triggers'."
  11811. (let ((l org-todo-state-tags-triggers)
  11812. changes)
  11813. (when (or (not state) (equal state ""))
  11814. (setq changes (append changes (cdr (assoc "" l)))))
  11815. (when (and (stringp state) (> (length state) 0))
  11816. (setq changes (append changes (cdr (assoc state l)))))
  11817. (when (member state org-not-done-keywords)
  11818. (setq changes (append changes (cdr (assoc 'todo l)))))
  11819. (when (member state org-done-keywords)
  11820. (setq changes (append changes (cdr (assoc 'done l)))))
  11821. (dolist (c changes)
  11822. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  11823. (defun org-local-logging (value)
  11824. "Get logging settings from a property VALUE."
  11825. (let* (words w a)
  11826. ;; directly set the variables, they are already local.
  11827. (setq org-log-done nil
  11828. org-log-repeat nil
  11829. org-todo-log-states nil)
  11830. (setq words (org-split-string value))
  11831. (while (setq w (pop words))
  11832. (cond
  11833. ((setq a (assoc w org-startup-options))
  11834. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  11835. (set (nth 1 a) (nth 2 a))))
  11836. ((setq a (org-extract-log-state-settings w))
  11837. (and (member (car a) org-todo-keywords-1)
  11838. (push a org-todo-log-states)))))))
  11839. (defun org-get-todo-sequence-head (kwd)
  11840. "Return the head of the TODO sequence to which KWD belongs.
  11841. If KWD is not set, check if there is a text property remembering the
  11842. right sequence."
  11843. (let (p)
  11844. (cond
  11845. ((not kwd)
  11846. (or (get-text-property (point-at-bol) 'org-todo-head)
  11847. (progn
  11848. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  11849. nil (point-at-eol)))
  11850. (get-text-property p 'org-todo-head))))
  11851. ((not (member kwd org-todo-keywords-1))
  11852. (car org-todo-keywords-1))
  11853. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  11854. (defun org-fast-todo-selection ()
  11855. "Fast TODO keyword selection with single keys.
  11856. Returns the new TODO keyword, or nil if no state change should occur."
  11857. (let* ((fulltable org-todo-key-alist)
  11858. (done-keywords org-done-keywords) ;; needed for the faces.
  11859. (maxlen (apply 'max (mapcar
  11860. (lambda (x)
  11861. (if (stringp (car x)) (string-width (car x)) 0))
  11862. fulltable)))
  11863. (expert nil)
  11864. (fwidth (+ maxlen 3 1 3))
  11865. (ncol (/ (- (window-width) 4) fwidth))
  11866. tg cnt e c tbl
  11867. groups ingroup)
  11868. (save-excursion
  11869. (save-window-excursion
  11870. (if expert
  11871. (set-buffer (get-buffer-create " *Org todo*"))
  11872. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  11873. (erase-buffer)
  11874. (org-set-local 'org-done-keywords done-keywords)
  11875. (setq tbl fulltable cnt 0)
  11876. (while (setq e (pop tbl))
  11877. (cond
  11878. ((equal e '(:startgroup))
  11879. (push '() groups) (setq ingroup t)
  11880. (when (not (= cnt 0))
  11881. (setq cnt 0)
  11882. (insert "\n"))
  11883. (insert "{ "))
  11884. ((equal e '(:endgroup))
  11885. (setq ingroup nil cnt 0)
  11886. (insert "}\n"))
  11887. ((equal e '(:newline))
  11888. (when (not (= cnt 0))
  11889. (setq cnt 0)
  11890. (insert "\n")
  11891. (setq e (car tbl))
  11892. (while (equal (car tbl) '(:newline))
  11893. (insert "\n")
  11894. (setq tbl (cdr tbl)))))
  11895. (t
  11896. (setq tg (car e) c (cdr e))
  11897. (if ingroup (push tg (car groups)))
  11898. (setq tg (org-add-props tg nil 'face
  11899. (org-get-todo-face tg)))
  11900. (if (and (= cnt 0) (not ingroup)) (insert " "))
  11901. (insert "[" c "] " tg (make-string
  11902. (- fwidth 4 (length tg)) ?\ ))
  11903. (when (= (setq cnt (1+ cnt)) ncol)
  11904. (insert "\n")
  11905. (if ingroup (insert " "))
  11906. (setq cnt 0)))))
  11907. (insert "\n")
  11908. (goto-char (point-min))
  11909. (if (not expert) (org-fit-window-to-buffer))
  11910. (message "[a-z..]:Set [SPC]:clear")
  11911. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  11912. (cond
  11913. ((or (= c ?\C-g)
  11914. (and (= c ?q) (not (rassoc c fulltable))))
  11915. (setq quit-flag t))
  11916. ((= c ?\ ) nil)
  11917. ((setq e (rassoc c fulltable) tg (car e))
  11918. tg)
  11919. (t (setq quit-flag t)))))))
  11920. (defun org-entry-is-todo-p ()
  11921. (member (org-get-todo-state) org-not-done-keywords))
  11922. (defun org-entry-is-done-p ()
  11923. (member (org-get-todo-state) org-done-keywords))
  11924. (defun org-get-todo-state ()
  11925. "Return the TODO keyword of the current subtree."
  11926. (save-excursion
  11927. (org-back-to-heading t)
  11928. (and (looking-at org-todo-line-regexp)
  11929. (match-end 2)
  11930. (match-string 2))))
  11931. (defun org-at-date-range-p (&optional inactive-ok)
  11932. "Is the cursor inside a date range?"
  11933. (interactive)
  11934. (save-excursion
  11935. (catch 'exit
  11936. (let ((pos (point)))
  11937. (skip-chars-backward "^[<\r\n")
  11938. (skip-chars-backward "<[")
  11939. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11940. (>= (match-end 0) pos)
  11941. (throw 'exit t))
  11942. (skip-chars-backward "^<[\r\n")
  11943. (skip-chars-backward "<[")
  11944. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11945. (>= (match-end 0) pos)
  11946. (throw 'exit t)))
  11947. nil)))
  11948. (defun org-get-repeat (&optional tagline)
  11949. "Check if there is a deadline/schedule with repeater in this entry."
  11950. (save-match-data
  11951. (save-excursion
  11952. (org-back-to-heading t)
  11953. (and (re-search-forward (if tagline
  11954. (concat tagline "\\s-*" org-repeat-re)
  11955. org-repeat-re)
  11956. (org-entry-end-position) t)
  11957. (match-string-no-properties 1)))))
  11958. (defvar org-last-changed-timestamp)
  11959. (defvar org-last-inserted-timestamp)
  11960. (defvar org-log-post-message)
  11961. (defvar org-log-note-purpose)
  11962. (defvar org-log-note-how nil)
  11963. (defvar org-log-note-extra)
  11964. (defun org-auto-repeat-maybe (done-word)
  11965. "Check if the current headline contains a repeated deadline/schedule.
  11966. If yes, set TODO state back to what it was and change the base date
  11967. of repeating deadline/scheduled time stamps to new date.
  11968. This function is run automatically after each state change to a DONE state."
  11969. ;; last-state is dynamically scoped into this function
  11970. (let* ((repeat (org-get-repeat))
  11971. (aa (assoc org-last-state org-todo-kwd-alist))
  11972. (interpret (nth 1 aa))
  11973. (head (nth 2 aa))
  11974. (whata '(("h" . hour) ("d" . day) ("m" . month) ("y" . year)))
  11975. (msg "Entry repeats: ")
  11976. (org-log-done nil)
  11977. (org-todo-log-states nil)
  11978. re type n what ts time to-state)
  11979. (when (and repeat (not (zerop (string-to-number (substring repeat 1)))))
  11980. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  11981. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  11982. org-todo-repeat-to-state))
  11983. (unless (and to-state (member to-state org-todo-keywords-1))
  11984. (setq to-state (if (eq interpret 'type) org-last-state head)))
  11985. (org-todo to-state)
  11986. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  11987. (org-entry-put nil "LAST_REPEAT" (format-time-string
  11988. (org-time-stamp-format t t))))
  11989. (when org-log-repeat
  11990. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  11991. (memq 'org-add-log-note post-command-hook))
  11992. ;; OK, we are already setup for some record
  11993. (if (eq org-log-repeat 'note)
  11994. ;; make sure we take a note, not only a time stamp
  11995. (setq org-log-note-how 'note))
  11996. ;; Set up for taking a record
  11997. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  11998. org-last-state
  11999. 'findpos org-log-repeat)))
  12000. (org-back-to-heading t)
  12001. (org-add-planning-info nil nil 'closed)
  12002. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  12003. org-deadline-time-regexp "\\)\\|\\("
  12004. org-ts-regexp "\\)"))
  12005. (while (re-search-forward
  12006. re (save-excursion (outline-next-heading) (point)) t)
  12007. (setq type (if (match-end 1) org-scheduled-string
  12008. (if (match-end 3) org-deadline-string "Plain:"))
  12009. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  12010. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts)
  12011. (setq n (string-to-number (match-string 2 ts))
  12012. what (match-string 3 ts))
  12013. (if (equal what "w") (setq n (* n 7) what "d"))
  12014. (if (and (equal what "h") (not (string-match "[0-9]\\{1,2\\}:[0-9]\\{2\\}" ts)))
  12015. (user-error "Cannot repeat in Repeat in %d hour(s) because no hour has been set" n))
  12016. ;; Preparation, see if we need to modify the start date for the change
  12017. (when (match-end 1)
  12018. (setq time (save-match-data (org-time-string-to-time ts)))
  12019. (cond
  12020. ((equal (match-string 1 ts) ".")
  12021. ;; Shift starting date to today
  12022. (org-timestamp-change
  12023. (- (org-today) (time-to-days time))
  12024. 'day))
  12025. ((equal (match-string 1 ts) "+")
  12026. (let ((nshiftmax 10) (nshift 0))
  12027. (while (or (= nshift 0)
  12028. (<= (time-to-days time)
  12029. (time-to-days (current-time))))
  12030. (when (= (incf nshift) nshiftmax)
  12031. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  12032. (user-error "Abort")))
  12033. (org-timestamp-change n (cdr (assoc what whata)))
  12034. (org-at-timestamp-p t)
  12035. (setq ts (match-string 1))
  12036. (setq time (save-match-data (org-time-string-to-time ts)))))
  12037. (org-timestamp-change (- n) (cdr (assoc what whata)))
  12038. ;; rematch, so that we have everything in place for the real shift
  12039. (org-at-timestamp-p t)
  12040. (setq ts (match-string 1))
  12041. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts))))
  12042. (save-excursion (org-timestamp-change n (cdr (assoc what whata)) nil t))
  12043. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  12044. (setq org-log-post-message msg)
  12045. (message "%s" msg))))
  12046. (defun org-show-todo-tree (arg)
  12047. "Make a compact tree which shows all headlines marked with TODO.
  12048. The tree will show the lines where the regexp matches, and all higher
  12049. headlines above the match.
  12050. With a \\[universal-argument] prefix, prompt for a regexp to match.
  12051. With a numeric prefix N, construct a sparse tree for the Nth element
  12052. of `org-todo-keywords-1'."
  12053. (interactive "P")
  12054. (let ((case-fold-search nil)
  12055. (kwd-re
  12056. (cond ((null arg) org-not-done-regexp)
  12057. ((equal arg '(4))
  12058. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  12059. (mapcar 'list org-todo-keywords-1))))
  12060. (concat "\\("
  12061. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  12062. "\\)\\>")))
  12063. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  12064. (regexp-quote (nth (1- (prefix-numeric-value arg))
  12065. org-todo-keywords-1)))
  12066. (t (user-error "Invalid prefix argument: %s" arg)))))
  12067. (message "%d TODO entries found"
  12068. (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
  12069. (defun org-deadline (arg &optional time)
  12070. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  12071. With one universal prefix argument, remove any deadline from the item.
  12072. With two universal prefix arguments, prompt for a warning delay.
  12073. With argument TIME, set the deadline at the corresponding date. TIME
  12074. can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  12075. (interactive "P")
  12076. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  12077. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  12078. 'region-start-level 'region))
  12079. org-loop-over-headlines-in-active-region)
  12080. (org-map-entries
  12081. `(org-deadline ',arg ,time)
  12082. org-loop-over-headlines-in-active-region
  12083. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  12084. (let* ((old-date (org-entry-get nil "DEADLINE"))
  12085. (old-date-time (if old-date (org-time-string-to-time old-date)))
  12086. (repeater (and old-date
  12087. (string-match
  12088. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  12089. old-date)
  12090. (match-string 1 old-date))))
  12091. (cond
  12092. ((equal arg '(4))
  12093. (when (and old-date org-log-redeadline)
  12094. (org-add-log-setup 'deldeadline nil old-date 'findpos
  12095. org-log-redeadline))
  12096. (org-remove-timestamp-with-keyword org-deadline-string)
  12097. (message "Item no longer has a deadline."))
  12098. ((equal arg '(16))
  12099. (save-excursion
  12100. (org-back-to-heading t)
  12101. (if (re-search-forward
  12102. org-deadline-time-regexp
  12103. (save-excursion (outline-next-heading) (point)) t)
  12104. (let* ((rpl0 (match-string 1))
  12105. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  12106. (replace-match
  12107. (concat org-deadline-string
  12108. " <" rpl
  12109. (format " -%dd"
  12110. (abs
  12111. (- (time-to-days
  12112. (save-match-data
  12113. (org-read-date nil t nil "Warn starting from" old-date-time)))
  12114. (time-to-days old-date-time))))
  12115. ">") t t))
  12116. (user-error "No deadline information to update"))))
  12117. (t
  12118. (org-add-planning-info 'deadline time 'closed)
  12119. (when (and old-date org-log-redeadline
  12120. (not (equal old-date
  12121. (substring org-last-inserted-timestamp 1 -1))))
  12122. (org-add-log-setup 'redeadline nil old-date 'findpos
  12123. org-log-redeadline))
  12124. (when repeater
  12125. (save-excursion
  12126. (org-back-to-heading t)
  12127. (when (re-search-forward (concat org-deadline-string " "
  12128. org-last-inserted-timestamp)
  12129. (save-excursion
  12130. (outline-next-heading) (point)) t)
  12131. (goto-char (1- (match-end 0)))
  12132. (insert " " repeater)
  12133. (setq org-last-inserted-timestamp
  12134. (concat (substring org-last-inserted-timestamp 0 -1)
  12135. " " repeater
  12136. (substring org-last-inserted-timestamp -1))))))
  12137. (message "Deadline on %s" org-last-inserted-timestamp))))))
  12138. (defun org-schedule (arg &optional time)
  12139. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  12140. With one universal prefix argument, remove any scheduling date from the item.
  12141. With two universal prefix arguments, prompt for a delay cookie.
  12142. With argument TIME, scheduled at the corresponding date. TIME can
  12143. either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  12144. (interactive "P")
  12145. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  12146. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  12147. 'region-start-level 'region))
  12148. org-loop-over-headlines-in-active-region)
  12149. (org-map-entries
  12150. `(org-schedule ',arg ,time)
  12151. org-loop-over-headlines-in-active-region
  12152. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  12153. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  12154. (old-date-time (if old-date (org-time-string-to-time old-date)))
  12155. (repeater (and old-date
  12156. (string-match
  12157. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  12158. old-date)
  12159. (match-string 1 old-date))))
  12160. (cond
  12161. ((equal arg '(4))
  12162. (progn
  12163. (when (and old-date org-log-reschedule)
  12164. (org-add-log-setup 'delschedule nil old-date 'findpos
  12165. org-log-reschedule))
  12166. (org-remove-timestamp-with-keyword org-scheduled-string)
  12167. (message "Item is no longer scheduled.")))
  12168. ((equal arg '(16))
  12169. (save-excursion
  12170. (org-back-to-heading t)
  12171. (if (re-search-forward
  12172. org-scheduled-time-regexp
  12173. (save-excursion (outline-next-heading) (point)) t)
  12174. (let* ((rpl0 (match-string 1))
  12175. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  12176. (replace-match
  12177. (concat org-scheduled-string
  12178. " <" rpl
  12179. (format " -%dd"
  12180. (abs
  12181. (- (time-to-days
  12182. (save-match-data
  12183. (org-read-date nil t nil "Delay until" old-date-time)))
  12184. (time-to-days old-date-time))))
  12185. ">") t t))
  12186. (user-error "No scheduled information to update"))))
  12187. (t
  12188. (org-add-planning-info 'scheduled time 'closed)
  12189. (when (and old-date org-log-reschedule
  12190. (not (equal old-date
  12191. (substring org-last-inserted-timestamp 1 -1))))
  12192. (org-add-log-setup 'reschedule nil old-date 'findpos
  12193. org-log-reschedule))
  12194. (when repeater
  12195. (save-excursion
  12196. (org-back-to-heading t)
  12197. (when (re-search-forward (concat org-scheduled-string " "
  12198. org-last-inserted-timestamp)
  12199. (save-excursion
  12200. (outline-next-heading) (point)) t)
  12201. (goto-char (1- (match-end 0)))
  12202. (insert " " repeater)
  12203. (setq org-last-inserted-timestamp
  12204. (concat (substring org-last-inserted-timestamp 0 -1)
  12205. " " repeater
  12206. (substring org-last-inserted-timestamp -1))))))
  12207. (message "Scheduled to %s" org-last-inserted-timestamp))))))
  12208. (defun org-get-scheduled-time (pom &optional inherit)
  12209. "Get the scheduled time as a time tuple, of a format suitable
  12210. for calling org-schedule with, or if there is no scheduling,
  12211. returns nil."
  12212. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  12213. (when time
  12214. (apply 'encode-time (org-parse-time-string time)))))
  12215. (defun org-get-deadline-time (pom &optional inherit)
  12216. "Get the deadline as a time tuple, of a format suitable for
  12217. calling org-deadline with, or if there is no scheduling, returns
  12218. nil."
  12219. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  12220. (when time
  12221. (apply 'encode-time (org-parse-time-string time)))))
  12222. (defun org-remove-timestamp-with-keyword (keyword)
  12223. "Remove all time stamps with KEYWORD in the current entry."
  12224. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  12225. beg)
  12226. (save-excursion
  12227. (org-back-to-heading t)
  12228. (setq beg (point))
  12229. (outline-next-heading)
  12230. (while (re-search-backward re beg t)
  12231. (replace-match "")
  12232. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  12233. (equal (char-before) ?\ ))
  12234. (backward-delete-char 1)
  12235. (if (string-match "^[ \t]*$" (buffer-substring
  12236. (point-at-bol) (point-at-eol)))
  12237. (delete-region (point-at-bol)
  12238. (min (point-max) (1+ (point-at-eol))))))))))
  12239. (defvar org-time-was-given) ; dynamically scoped parameter
  12240. (defvar org-end-time-was-given) ; dynamically scoped parameter
  12241. (defun org-add-planning-info (what &optional time &rest remove)
  12242. "Insert new timestamp with keyword in the planning line.
  12243. WHAT indicates what kind of time stamp to add. It is a symbol
  12244. among `closed', `deadline', `scheduled' and nil. TIME indicates
  12245. the time to use. If none is given, the user is prompted for
  12246. a date. REMOVE indicates what kind of entries to remove. An old
  12247. WHAT entry will also be removed."
  12248. (let (org-time-was-given org-end-time-was-given default-time default-input)
  12249. (catch 'exit
  12250. (when (and (memq what '(scheduled deadline))
  12251. (or (not time)
  12252. (and (stringp time)
  12253. (string-match "^[-+]+[0-9]" time))))
  12254. ;; Try to get a default date/time from existing timestamp
  12255. (save-excursion
  12256. (org-back-to-heading t)
  12257. (let ((end (save-excursion (outline-next-heading) (point))) ts)
  12258. (when (re-search-forward (if (eq what 'scheduled)
  12259. org-scheduled-time-regexp
  12260. org-deadline-time-regexp)
  12261. end t)
  12262. (setq ts (match-string 1)
  12263. default-time (apply 'encode-time (org-parse-time-string ts))
  12264. default-input (and ts (org-get-compact-tod ts)))))))
  12265. (when what
  12266. (setq time
  12267. (if (stringp time)
  12268. ;; This is a string (relative or absolute), set
  12269. ;; proper date.
  12270. (apply #'encode-time
  12271. (org-read-date-analyze
  12272. time default-time (decode-time default-time)))
  12273. ;; If necessary, get the time from the user
  12274. (or time (org-read-date nil 'to-time nil nil
  12275. default-time default-input)))))
  12276. (org-with-wide-buffer
  12277. (org-back-to-heading t)
  12278. (forward-line)
  12279. (unless (bolp) (insert "\n"))
  12280. (cond ((org-looking-at-p org-planning-line-re)
  12281. ;; Move to current indentation.
  12282. (skip-chars-forward " \t")
  12283. ;; Check if we have to remove something.
  12284. (dolist (type (if what (cons what remove) remove))
  12285. (when (save-excursion
  12286. (re-search-forward
  12287. (case type
  12288. (closed org-closed-time-regexp)
  12289. (deadline org-deadline-time-regexp)
  12290. (scheduled org-scheduled-time-regexp)
  12291. (otherwise (error "Invalid planning type: %s" type)))
  12292. (line-end-position) t))
  12293. (replace-match "")
  12294. (when (looking-at "--+<[^>]+>") (replace-match ""))))
  12295. ;; Remove leading white spaces.
  12296. (when (looking-at "[ \t]+") (replace-match "")))
  12297. ((not what) (throw 'exit nil)) ; Nothing to do.
  12298. (t (insert-before-markers "\n")
  12299. (backward-char 1)
  12300. (when org-adapt-indentation
  12301. (org-indent-to-column (1+ (org-outline-level))))))
  12302. (when what
  12303. ;; Insert planning keyword.
  12304. (insert (case what
  12305. (closed org-closed-string)
  12306. (deadline org-deadline-string)
  12307. (scheduled org-scheduled-string)
  12308. (otherwise (error "Invalid planning type: %s" what)))
  12309. " ")
  12310. ;; Insert associated timestamp.
  12311. (let ((ts (org-insert-time-stamp
  12312. time
  12313. (or org-time-was-given
  12314. (and (eq what 'closed) org-log-done-with-time))
  12315. (eq what 'closed)
  12316. nil nil (list org-end-time-was-given))))
  12317. (unless (eolp) (insert " "))
  12318. ts))))))
  12319. (defvar org-log-note-marker (make-marker))
  12320. (defvar org-log-note-purpose nil)
  12321. (defvar org-log-note-state nil)
  12322. (defvar org-log-note-previous-state nil)
  12323. (defvar org-log-note-extra nil)
  12324. (defvar org-log-note-window-configuration nil)
  12325. (defvar org-log-note-return-to (make-marker))
  12326. (defvar org-log-note-effective-time nil
  12327. "Remembered current time so that dynamically scoped
  12328. `org-extend-today-until' affects tha timestamps in state change
  12329. log")
  12330. (defvar org-log-post-message nil
  12331. "Message to be displayed after a log note has been stored.
  12332. The auto-repeater uses this.")
  12333. (defun org-add-note ()
  12334. "Add a note to the current entry.
  12335. This is done in the same way as adding a state change note."
  12336. (interactive)
  12337. (org-add-log-setup 'note nil nil 'findpos nil))
  12338. (defun org-log-beginning (&optional create)
  12339. "Return expected start of log notes in current entry.
  12340. When optional argument CREATE is non-nil, the function creates
  12341. a drawer to store notes, if necessary. Returned position ignores
  12342. narrowing."
  12343. (org-with-wide-buffer
  12344. (org-back-to-heading t)
  12345. ;; Skip planning info and property drawer.
  12346. (forward-line)
  12347. (when (org-looking-at-p org-planning-line-re) (forward-line))
  12348. (when (looking-at org-property-drawer-re)
  12349. (goto-char (match-end 0))
  12350. (forward-line))
  12351. (if (org-at-heading-p) (point)
  12352. (let ((end (save-excursion (outline-next-heading) (point)))
  12353. (drawer (cond ((stringp org-log-into-drawer) org-log-into-drawer)
  12354. (org-log-into-drawer "LOGBOOK"))))
  12355. (cond
  12356. (drawer
  12357. (let ((regexp (concat "^[ \t]*:" (regexp-quote drawer) ":[ \t]*$"))
  12358. (case-fold-search t))
  12359. (catch 'exit
  12360. ;; Try to find existing drawer.
  12361. (while (re-search-forward regexp end t)
  12362. (let ((element (org-element-at-point)))
  12363. (when (eq (org-element-type element) 'drawer)
  12364. (let ((cend (org-element-property :contents-end element)))
  12365. (when (and (not org-log-states-order-reversed) cend)
  12366. (goto-char cend)))
  12367. (throw 'exit nil))))
  12368. ;; No drawer found. Create one, if permitted.
  12369. (when create
  12370. (unless (bolp) (insert "\n"))
  12371. (let ((beg (point)))
  12372. (insert ":" drawer ":\n:END:\n")
  12373. (org-indent-region beg (point)))
  12374. (end-of-line -1)))))
  12375. (org-log-state-notes-insert-after-drawers
  12376. (while (and (looking-at org-drawer-regexp)
  12377. (progn (goto-char (match-end 0))
  12378. (re-search-forward org-property-end-re end t)))
  12379. (forward-line)))))
  12380. (if (bolp) (point) (line-beginning-position 2)))))
  12381. (defun org-add-log-setup (&optional purpose state prev-state findpos how extra)
  12382. "Set up the post command hook to take a note.
  12383. If this is about to TODO state change, the new state is expected in STATE.
  12384. When FINDPOS is non-nil, find the correct position for the note in
  12385. the current entry. If not, assume that it can be inserted at point.
  12386. HOW is an indicator what kind of note should be created.
  12387. EXTRA is additional text that will be inserted into the notes buffer."
  12388. (org-with-wide-buffer
  12389. (when findpos
  12390. (goto-char (org-log-beginning t))
  12391. (unless org-log-states-order-reversed
  12392. (org-skip-over-state-notes)
  12393. (skip-chars-backward " \t\n\r")
  12394. (forward-line)))
  12395. (move-marker org-log-note-marker (point))
  12396. (setq org-log-note-purpose purpose
  12397. org-log-note-state state
  12398. org-log-note-previous-state prev-state
  12399. org-log-note-how how
  12400. org-log-note-extra extra
  12401. org-log-note-effective-time (org-current-effective-time))
  12402. (add-hook 'post-command-hook 'org-add-log-note 'append)))
  12403. (defun org-skip-over-state-notes ()
  12404. "Skip past the list of State notes in an entry."
  12405. (when (ignore-errors (goto-char (org-in-item-p)))
  12406. (let* ((struct (org-list-struct))
  12407. (prevs (org-list-prevs-alist struct))
  12408. (regexp
  12409. (concat "[ \t]*- +"
  12410. (replace-regexp-in-string
  12411. " +" " +"
  12412. (org-replace-escapes
  12413. (regexp-quote (cdr (assq 'state org-log-note-headings)))
  12414. `(("%d" . ,org-ts-regexp-inactive)
  12415. ("%D" . ,org-ts-regexp)
  12416. ("%s" . "\"\\S-+\"")
  12417. ("%S" . "\"\\S-+\"")
  12418. ("%t" . ,org-ts-regexp-inactive)
  12419. ("%T" . ,org-ts-regexp)
  12420. ("%u" . ".*?")
  12421. ("%U" . ".*?")))))))
  12422. (while (org-looking-at-p regexp)
  12423. (goto-char (or (org-list-get-next-item (point) struct prevs)
  12424. (org-list-get-item-end (point) struct)))))))
  12425. (defun org-add-log-note (&optional purpose)
  12426. "Pop up a window for taking a note, and add this note later at point."
  12427. (remove-hook 'post-command-hook 'org-add-log-note)
  12428. (setq org-log-note-window-configuration (current-window-configuration))
  12429. (delete-other-windows)
  12430. (move-marker org-log-note-return-to (point))
  12431. (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
  12432. (goto-char org-log-note-marker)
  12433. (org-switch-to-buffer-other-window "*Org Note*")
  12434. (erase-buffer)
  12435. (if (memq org-log-note-how '(time state))
  12436. (let (current-prefix-arg) (org-store-log-note))
  12437. (let ((org-inhibit-startup t)) (org-mode))
  12438. (insert (format "# Insert note for %s.
  12439. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  12440. (cond
  12441. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  12442. ((eq org-log-note-purpose 'done) "closed todo item")
  12443. ((eq org-log-note-purpose 'state)
  12444. (format "state change from \"%s\" to \"%s\""
  12445. (or org-log-note-previous-state "")
  12446. (or org-log-note-state "")))
  12447. ((eq org-log-note-purpose 'reschedule)
  12448. "rescheduling")
  12449. ((eq org-log-note-purpose 'delschedule)
  12450. "no longer scheduled")
  12451. ((eq org-log-note-purpose 'redeadline)
  12452. "changing deadline")
  12453. ((eq org-log-note-purpose 'deldeadline)
  12454. "removing deadline")
  12455. ((eq org-log-note-purpose 'refile)
  12456. "refiling")
  12457. ((eq org-log-note-purpose 'note)
  12458. "this entry")
  12459. (t (error "This should not happen")))))
  12460. (if org-log-note-extra (insert org-log-note-extra))
  12461. (org-set-local 'org-finish-function 'org-store-log-note)
  12462. (run-hooks 'org-log-buffer-setup-hook)))
  12463. (defvar org-note-abort nil) ; dynamically scoped
  12464. (defun org-store-log-note ()
  12465. "Finish taking a log note, and insert it to where it belongs."
  12466. (let ((txt (buffer-string)))
  12467. (kill-buffer (current-buffer))
  12468. (let ((note (cdr (assq org-log-note-purpose org-log-note-headings))) lines)
  12469. (while (string-match "\\`# .*\n[ \t\n]*" txt)
  12470. (setq txt (replace-match "" t t txt)))
  12471. (if (string-match "\\s-+\\'" txt)
  12472. (setq txt (replace-match "" t t txt)))
  12473. (setq lines (org-split-string txt "\n"))
  12474. (when (and note (string-match "\\S-" note))
  12475. (setq note
  12476. (org-replace-escapes
  12477. note
  12478. (list (cons "%u" (user-login-name))
  12479. (cons "%U" user-full-name)
  12480. (cons "%t" (format-time-string
  12481. (org-time-stamp-format 'long 'inactive)
  12482. org-log-note-effective-time))
  12483. (cons "%T" (format-time-string
  12484. (org-time-stamp-format 'long nil)
  12485. org-log-note-effective-time))
  12486. (cons "%d" (format-time-string
  12487. (org-time-stamp-format nil 'inactive)
  12488. org-log-note-effective-time))
  12489. (cons "%D" (format-time-string
  12490. (org-time-stamp-format nil nil)
  12491. org-log-note-effective-time))
  12492. (cons "%s" (if org-log-note-state
  12493. (concat "\"" org-log-note-state "\"")
  12494. ""))
  12495. (cons "%S" (if org-log-note-previous-state
  12496. (concat "\"" org-log-note-previous-state "\"")
  12497. "\"\"")))))
  12498. (when lines (setq note (concat note " \\\\")))
  12499. (push note lines))
  12500. (when (or current-prefix-arg org-note-abort)
  12501. (when (org-log-into-drawer)
  12502. (org-remove-empty-drawer-at org-log-note-marker))
  12503. (setq lines nil))
  12504. (when lines
  12505. (with-current-buffer (marker-buffer org-log-note-marker)
  12506. (save-excursion
  12507. (goto-char org-log-note-marker)
  12508. (move-marker org-log-note-marker nil)
  12509. ;; Make sure point is at the beginning of an empty line.
  12510. (cond ((not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  12511. ((looking-at "[ \t]*\\S-") (save-excursion (insert "\n"))))
  12512. ;; In an existing list, add a new item at the top level.
  12513. ;; Otherwise, indent line like a regular one.
  12514. (let ((itemp (org-in-item-p)))
  12515. (if itemp
  12516. (org-indent-line-to
  12517. (let ((struct (save-excursion
  12518. (goto-char itemp) (org-list-struct))))
  12519. (org-list-get-ind (org-list-get-top-point struct) struct)))
  12520. (org-indent-line)))
  12521. (insert (org-list-bullet-string "-") (pop lines))
  12522. (let ((ind (org-list-item-body-column (line-beginning-position))))
  12523. (dolist (line lines)
  12524. (insert "\n")
  12525. (org-indent-line-to ind)
  12526. (insert line)))
  12527. (message "Note stored")
  12528. (org-back-to-heading t)
  12529. (org-cycle-hide-drawers 'children))
  12530. ;; Fix `buffer-undo-list' when `org-store-log-note' is called
  12531. ;; from within `org-add-log-note' because `buffer-undo-list'
  12532. ;; is then modified outside of `org-with-remote-undo'.
  12533. (when (eq this-command 'org-agenda-todo)
  12534. (setcdr buffer-undo-list (cddr buffer-undo-list)))))))
  12535. ;; Don't add undo information when called from `org-agenda-todo'
  12536. (let ((buffer-undo-list (eq this-command 'org-agenda-todo)))
  12537. (set-window-configuration org-log-note-window-configuration)
  12538. (with-current-buffer (marker-buffer org-log-note-return-to)
  12539. (goto-char org-log-note-return-to))
  12540. (move-marker org-log-note-return-to nil)
  12541. (and org-log-post-message (message "%s" org-log-post-message))))
  12542. (defun org-remove-empty-drawer-at (pos)
  12543. "Remove an empty drawer at position POS.
  12544. POS may also be a marker."
  12545. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  12546. (org-with-wide-buffer
  12547. (goto-char pos)
  12548. (let ((drawer (org-element-at-point)))
  12549. (when (and (memq (org-element-type drawer) '(drawer property-drawer))
  12550. (not (org-element-property :contents-begin drawer)))
  12551. (delete-region (org-element-property :begin drawer)
  12552. (progn (goto-char (org-element-property :end drawer))
  12553. (skip-chars-backward " \r\t\n")
  12554. (forward-line)
  12555. (point))))))))
  12556. (defvar org-ts-type nil)
  12557. (defun org-sparse-tree (&optional arg type)
  12558. "Create a sparse tree, prompt for the details.
  12559. This command can create sparse trees. You first need to select the type
  12560. of match used to create the tree:
  12561. t Show all TODO entries.
  12562. T Show entries with a specific TODO keyword.
  12563. m Show entries selected by a tags/property match.
  12564. p Enter a property name and its value (both with completion on existing
  12565. names/values) and show entries with that property.
  12566. r Show entries matching a regular expression (`/' can be used as well).
  12567. b Show deadlines and scheduled items before a date.
  12568. a Show deadlines and scheduled items after a date.
  12569. d Show deadlines due within `org-deadline-warning-days'.
  12570. D Show deadlines and scheduled items between a date range."
  12571. (interactive "P")
  12572. (setq type (or type org-sparse-tree-default-date-type))
  12573. (setq org-ts-type type)
  12574. (message "Sparse tree: [/]regexp [t]odo [T]odo-kwd [m]atch [p]roperty
  12575. [d]eadlines [b]efore-date [a]fter-date [D]ates range
  12576. [c]ycle through date types: %s"
  12577. (case type
  12578. (all "all timestamps")
  12579. (scheduled "only scheduled")
  12580. (deadline "only deadline")
  12581. (active "only active timestamps")
  12582. (inactive "only inactive timestamps")
  12583. (scheduled-or-deadline "scheduled/deadline")
  12584. (closed "with a closed time-stamp")
  12585. (otherwise "scheduled/deadline")))
  12586. (let ((answer (read-char-exclusive)))
  12587. (case answer
  12588. (?c
  12589. (org-sparse-tree
  12590. arg
  12591. (cadr (memq type '(scheduled-or-deadline all scheduled deadline active
  12592. inactive closed)))))
  12593. (?d (call-interactively 'org-check-deadlines))
  12594. (?b (call-interactively 'org-check-before-date))
  12595. (?a (call-interactively 'org-check-after-date))
  12596. (?D (call-interactively 'org-check-dates-range))
  12597. (?t (call-interactively 'org-show-todo-tree))
  12598. (?T (org-show-todo-tree '(4)))
  12599. (?m (call-interactively 'org-match-sparse-tree))
  12600. ((?p ?P)
  12601. (let* ((kwd (org-icompleting-read
  12602. "Property: " (mapcar 'list (org-buffer-property-keys))))
  12603. (value (org-icompleting-read
  12604. "Value: " (mapcar 'list (org-property-values kwd)))))
  12605. (unless (string-match "\\`{.*}\\'" value)
  12606. (setq value (concat "\"" value "\"")))
  12607. (org-match-sparse-tree arg (concat kwd "=" value))))
  12608. ((?r ?R ?/) (call-interactively 'org-occur))
  12609. (otherwise (user-error "No such sparse tree command \"%c\"" answer)))))
  12610. (defvar org-occur-highlights nil
  12611. "List of overlays used for occur matches.")
  12612. (make-variable-buffer-local 'org-occur-highlights)
  12613. (defvar org-occur-parameters nil
  12614. "Parameters of the active org-occur calls.
  12615. This is a list, each call to org-occur pushes as cons cell,
  12616. containing the regular expression and the callback, onto the list.
  12617. The list can contain several entries if `org-occur' has been called
  12618. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  12619. will only contain one set of parameters. When the highlights are
  12620. removed (for example with `C-c C-c', or with the next edit (depending
  12621. on `org-remove-highlights-with-change'), this variable is emptied
  12622. as well.")
  12623. (make-variable-buffer-local 'org-occur-parameters)
  12624. (defun org-occur (regexp &optional keep-previous callback)
  12625. "Make a compact tree which shows all matches of REGEXP.
  12626. The tree will show the lines where the regexp matches, and all higher
  12627. headlines above the match. It will also show the heading after the match,
  12628. to make sure editing the matching entry is easy.
  12629. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  12630. call to `org-occur' will be kept, to allow stacking of calls to this
  12631. command.
  12632. If CALLBACK is non-nil, it is a function which is called to confirm
  12633. that the match should indeed be shown."
  12634. (interactive "sRegexp: \nP")
  12635. (when (equal regexp "")
  12636. (user-error "Regexp cannot be empty"))
  12637. (unless keep-previous
  12638. (org-remove-occur-highlights nil nil t))
  12639. (push (cons regexp callback) org-occur-parameters)
  12640. (let ((cnt 0))
  12641. (save-excursion
  12642. (goto-char (point-min))
  12643. (if (or (not keep-previous) ; do not want to keep
  12644. (not org-occur-highlights)) ; no previous matches
  12645. ;; hide everything
  12646. (org-overview))
  12647. (while (re-search-forward regexp nil t)
  12648. (when (or (not callback)
  12649. (save-match-data (funcall callback)))
  12650. (setq cnt (1+ cnt))
  12651. (when org-highlight-sparse-tree-matches
  12652. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  12653. (org-show-context 'occur-tree))))
  12654. (when org-remove-highlights-with-change
  12655. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  12656. nil 'local))
  12657. (unless org-sparse-tree-open-archived-trees
  12658. (org-hide-archived-subtrees (point-min) (point-max)))
  12659. (run-hooks 'org-occur-hook)
  12660. (if (org-called-interactively-p 'interactive)
  12661. (message "%d match(es) for regexp %s" cnt regexp))
  12662. cnt))
  12663. (defun org-occur-next-match (&optional n reset)
  12664. "Function for `next-error-function' to find sparse tree matches.
  12665. N is the number of matches to move, when negative move backwards.
  12666. RESET is entirely ignored - this function always goes back to the
  12667. starting point when no match is found."
  12668. (let* ((limit (if (< n 0) (point-min) (point-max)))
  12669. (search-func (if (< n 0)
  12670. 'previous-single-char-property-change
  12671. 'next-single-char-property-change))
  12672. (n (abs n))
  12673. (pos (point))
  12674. p1)
  12675. (catch 'exit
  12676. (while (setq p1 (funcall search-func (point) 'org-type))
  12677. (when (equal p1 limit)
  12678. (goto-char pos)
  12679. (user-error "No more matches"))
  12680. (when (equal (get-char-property p1 'org-type) 'org-occur)
  12681. (setq n (1- n))
  12682. (when (= n 0)
  12683. (goto-char p1)
  12684. (throw 'exit (point))))
  12685. (goto-char p1))
  12686. (goto-char p1)
  12687. (user-error "No more matches"))))
  12688. (defun org-show-context (&optional key)
  12689. "Make sure point and context are visible.
  12690. How much context is shown depends upon the variables
  12691. `org-show-hierarchy-above', `org-show-following-heading',
  12692. `org-show-entry-below' and `org-show-siblings'."
  12693. (let ((heading-p (org-at-heading-p t))
  12694. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  12695. (following-p (org-get-alist-option org-show-following-heading key))
  12696. (entry-p (org-get-alist-option org-show-entry-below key))
  12697. (siblings-p (org-get-alist-option org-show-siblings key)))
  12698. ;; Show heading or entry text
  12699. (if (and heading-p (not entry-p))
  12700. (org-flag-heading nil) ; only show the heading
  12701. (and (or entry-p (outline-invisible-p) (org-invisible-p2))
  12702. (org-show-hidden-entry))) ; show entire entry
  12703. (when following-p
  12704. ;; Show next sibling, or heading below text
  12705. (save-excursion
  12706. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  12707. (org-flag-heading nil))))
  12708. (when siblings-p (org-show-siblings))
  12709. (when hierarchy-p
  12710. ;; show all higher headings, possibly with siblings
  12711. (save-excursion
  12712. (while (and (ignore-errors (progn (org-up-heading-all 1) t))
  12713. (not (bobp)))
  12714. (org-flag-heading nil)
  12715. (when siblings-p (org-show-siblings)))))))
  12716. (defvar org-reveal-start-hook nil
  12717. "Hook run before revealing a location.")
  12718. (defun org-reveal (&optional siblings)
  12719. "Show current entry, hierarchy above it, and the following headline.
  12720. This can be used to show a consistent set of context around locations
  12721. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  12722. not t for the search context.
  12723. With optional argument SIBLINGS, on each level of the hierarchy all
  12724. siblings are shown. This repairs the tree structure to what it would
  12725. look like when opened with hierarchical calls to `org-cycle'.
  12726. With double optional argument \\[universal-argument] \\[universal-argument], \
  12727. go to the parent and show the
  12728. entire tree."
  12729. (interactive "P")
  12730. (run-hooks 'org-reveal-start-hook)
  12731. (let ((org-show-hierarchy-above t)
  12732. (org-show-following-heading t)
  12733. (org-show-siblings (if siblings t org-show-siblings)))
  12734. (org-show-context nil))
  12735. (when (equal siblings '(16))
  12736. (save-excursion
  12737. (when (org-up-heading-safe)
  12738. (org-show-subtree)
  12739. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  12740. (defun org-highlight-new-match (beg end)
  12741. "Highlight from BEG to END and mark the highlight is an occur headline."
  12742. (let ((ov (make-overlay beg end)))
  12743. (overlay-put ov 'face 'secondary-selection)
  12744. (overlay-put ov 'org-type 'org-occur)
  12745. (push ov org-occur-highlights)))
  12746. (defun org-remove-occur-highlights (&optional beg end noremove)
  12747. "Remove the occur highlights from the buffer.
  12748. BEG and END are ignored. If NOREMOVE is nil, remove this function
  12749. from the `before-change-functions' in the current buffer."
  12750. (interactive)
  12751. (unless org-inhibit-highlight-removal
  12752. (mapc 'delete-overlay org-occur-highlights)
  12753. (setq org-occur-highlights nil)
  12754. (setq org-occur-parameters nil)
  12755. (unless noremove
  12756. (remove-hook 'before-change-functions
  12757. 'org-remove-occur-highlights 'local))))
  12758. ;;;; Priorities
  12759. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  12760. "Regular expression matching the priority indicator.")
  12761. (defvar org-remove-priority-next-time nil)
  12762. (defun org-priority-up ()
  12763. "Increase the priority of the current item."
  12764. (interactive)
  12765. (org-priority 'up))
  12766. (defun org-priority-down ()
  12767. "Decrease the priority of the current item."
  12768. (interactive)
  12769. (org-priority 'down))
  12770. (defun org-priority (&optional action show)
  12771. "Change the priority of an item.
  12772. ACTION can be `set', `up', `down', or a character."
  12773. (interactive "P")
  12774. (if (equal action '(4))
  12775. (org-show-priority)
  12776. (unless org-enable-priority-commands
  12777. (user-error "Priority commands are disabled"))
  12778. (setq action (or action 'set))
  12779. (let (current new news have remove)
  12780. (save-excursion
  12781. (org-back-to-heading t)
  12782. (if (looking-at org-priority-regexp)
  12783. (setq current (string-to-char (match-string 2))
  12784. have t))
  12785. (cond
  12786. ((eq action 'remove)
  12787. (setq remove t new ?\ ))
  12788. ((or (eq action 'set)
  12789. (if (featurep 'xemacs) (characterp action) (integerp action)))
  12790. (if (not (eq action 'set))
  12791. (setq new action)
  12792. (message "Priority %c-%c, SPC to remove: "
  12793. org-highest-priority org-lowest-priority)
  12794. (save-match-data
  12795. (setq new (read-char-exclusive))))
  12796. (if (and (= (upcase org-highest-priority) org-highest-priority)
  12797. (= (upcase org-lowest-priority) org-lowest-priority))
  12798. (setq new (upcase new)))
  12799. (cond ((equal new ?\ ) (setq remove t))
  12800. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  12801. (user-error "Priority must be between `%c' and `%c'"
  12802. org-highest-priority org-lowest-priority))))
  12803. ((eq action 'up)
  12804. (setq new (if have
  12805. (1- current) ; normal cycling
  12806. ;; last priority was empty
  12807. (if (eq last-command this-command)
  12808. org-lowest-priority ; wrap around empty to lowest
  12809. ;; default
  12810. (if org-priority-start-cycle-with-default
  12811. org-default-priority
  12812. (1- org-default-priority))))))
  12813. ((eq action 'down)
  12814. (setq new (if have
  12815. (1+ current) ; normal cycling
  12816. ;; last priority was empty
  12817. (if (eq last-command this-command)
  12818. org-highest-priority ; wrap around empty to highest
  12819. ;; default
  12820. (if org-priority-start-cycle-with-default
  12821. org-default-priority
  12822. (1+ org-default-priority))))))
  12823. (t (user-error "Invalid action")))
  12824. (if (or (< (upcase new) org-highest-priority)
  12825. (> (upcase new) org-lowest-priority))
  12826. (if (and (memq action '(up down))
  12827. (not have) (not (eq last-command this-command)))
  12828. ;; `new' is from default priority
  12829. (error
  12830. "The default can not be set, see `org-default-priority' why")
  12831. ;; normal cycling: `new' is beyond highest/lowest priority
  12832. ;; and is wrapped around to the empty priority
  12833. (setq remove t)))
  12834. (setq news (format "%c" new))
  12835. (if have
  12836. (if remove
  12837. (replace-match "" t t nil 1)
  12838. (replace-match news t t nil 2))
  12839. (if remove
  12840. (user-error "No priority cookie found in line")
  12841. (let ((case-fold-search nil))
  12842. (looking-at org-todo-line-regexp))
  12843. (if (match-end 2)
  12844. (progn
  12845. (goto-char (match-end 2))
  12846. (insert " [#" news "]"))
  12847. (goto-char (match-beginning 3))
  12848. (insert "[#" news "] "))))
  12849. (org-set-tags nil 'align))
  12850. (if remove
  12851. (message "Priority removed")
  12852. (message "Priority of current item set to %s" news)))))
  12853. (defun org-show-priority ()
  12854. "Show the priority of the current item.
  12855. This priority is composed of the main priority given with the [#A] cookies,
  12856. and by additional input from the age of a schedules or deadline entry."
  12857. (interactive)
  12858. (let ((pri (if (eq major-mode 'org-agenda-mode)
  12859. (org-get-at-bol 'priority)
  12860. (save-excursion
  12861. (save-match-data
  12862. (beginning-of-line)
  12863. (and (looking-at org-heading-regexp)
  12864. (org-get-priority (match-string 0))))))))
  12865. (message "Priority is %d" (if pri pri -1000))))
  12866. (defun org-get-priority (s)
  12867. "Find priority cookie and return priority."
  12868. (save-match-data
  12869. (if (functionp org-get-priority-function)
  12870. (funcall org-get-priority-function)
  12871. (if (not (string-match org-priority-regexp s))
  12872. (* 1000 (- org-lowest-priority org-default-priority))
  12873. (* 1000 (- org-lowest-priority
  12874. (string-to-char (match-string 2 s))))))))
  12875. ;;;; Tags
  12876. (defvar org-agenda-archives-mode)
  12877. (defvar org-map-continue-from nil
  12878. "Position from where mapping should continue.
  12879. Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
  12880. (defvar org-scanner-tags nil
  12881. "The current tag list while the tags scanner is running.")
  12882. (defvar org-trust-scanner-tags nil
  12883. "Should `org-get-tags-at' use the tags for the scanner.
  12884. This is for internal dynamical scoping only.
  12885. When this is non-nil, the function `org-get-tags-at' will return the value
  12886. of `org-scanner-tags' instead of building the list by itself. This
  12887. can lead to large speed-ups when the tags scanner is used in a file with
  12888. many entries, and when the list of tags is retrieved, for example to
  12889. obtain a list of properties. Building the tags list for each entry in such
  12890. a file becomes an N^2 operation - but with this variable set, it scales
  12891. as N.")
  12892. (defun org-scan-tags (action matcher todo-only &optional start-level)
  12893. "Scan headline tags with inheritance and produce output ACTION.
  12894. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  12895. or `agenda' to produce an entry list for an agenda view. It can also be
  12896. a Lisp form or a function that should be called at each matched headline, in
  12897. this case the return value is a list of all return values from these calls.
  12898. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  12899. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  12900. only lines with a not-done TODO keyword are included in the output.
  12901. This should be the same variable that was scoped into
  12902. and set by `org-make-tags-matcher' when it constructed MATCHER.
  12903. START-LEVEL can be a string with asterisks, reducing the scope to
  12904. headlines matching this string."
  12905. (require 'org-agenda)
  12906. (let* ((re (concat "^"
  12907. (if start-level
  12908. ;; Get the correct level to match
  12909. (concat "\\*\\{" (number-to-string start-level) "\\} ")
  12910. org-outline-regexp)
  12911. " *\\(\\<\\("
  12912. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  12913. (org-re "\\)\\>\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  12914. (props (list 'face 'default
  12915. 'done-face 'org-agenda-done
  12916. 'undone-face 'default
  12917. 'mouse-face 'highlight
  12918. 'org-not-done-regexp org-not-done-regexp
  12919. 'org-todo-regexp org-todo-regexp
  12920. 'org-complex-heading-regexp org-complex-heading-regexp
  12921. 'help-echo
  12922. (format "mouse-2 or RET jump to org file %s"
  12923. (abbreviate-file-name
  12924. (or (buffer-file-name (buffer-base-buffer))
  12925. (buffer-name (buffer-base-buffer)))))))
  12926. (org-map-continue-from nil)
  12927. lspos tags tags-list
  12928. (tags-alist (list (cons 0 org-file-tags)))
  12929. (llast 0) rtn rtn1 level category i txt
  12930. todo marker entry priority)
  12931. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  12932. (setq action (list 'lambda nil action)))
  12933. (save-excursion
  12934. (goto-char (point-min))
  12935. (when (eq action 'sparse-tree)
  12936. (org-overview)
  12937. (org-remove-occur-highlights))
  12938. (while (let (case-fold-search)
  12939. (re-search-forward re nil t))
  12940. (setq org-map-continue-from nil)
  12941. (catch :skip
  12942. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  12943. tags (if (match-end 4) (org-match-string-no-properties 4)))
  12944. (goto-char (setq lspos (match-beginning 0)))
  12945. (setq level (org-reduced-level (org-outline-level))
  12946. category (org-get-category))
  12947. (setq i llast llast level)
  12948. ;; remove tag lists from same and sublevels
  12949. (while (>= i level)
  12950. (when (setq entry (assoc i tags-alist))
  12951. (setq tags-alist (delete entry tags-alist)))
  12952. (setq i (1- i)))
  12953. ;; add the next tags
  12954. (when tags
  12955. (setq tags (org-split-string tags ":")
  12956. tags-alist
  12957. (cons (cons level tags) tags-alist)))
  12958. ;; compile tags for current headline
  12959. (setq tags-list
  12960. (if org-use-tag-inheritance
  12961. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  12962. tags)
  12963. org-scanner-tags tags-list)
  12964. (when org-use-tag-inheritance
  12965. (setcdr (car tags-alist)
  12966. (mapcar (lambda (x)
  12967. (setq x (copy-sequence x))
  12968. (org-add-prop-inherited x))
  12969. (cdar tags-alist))))
  12970. (when (and tags org-use-tag-inheritance
  12971. (or (not (eq t org-use-tag-inheritance))
  12972. org-tags-exclude-from-inheritance))
  12973. ;; selective inheritance, remove uninherited ones
  12974. (setcdr (car tags-alist)
  12975. (org-remove-uninherited-tags (cdar tags-alist))))
  12976. (when (and
  12977. ;; eval matcher only when the todo condition is OK
  12978. (and (or (not todo-only) (member todo org-not-done-keywords))
  12979. (let ((case-fold-search t) (org-trust-scanner-tags t))
  12980. (eval matcher)))
  12981. ;; Call the skipper, but return t if it does not skip,
  12982. ;; so that the `and' form continues evaluating
  12983. (progn
  12984. (unless (eq action 'sparse-tree) (org-agenda-skip))
  12985. t)
  12986. ;; Check if timestamps are deselecting this entry
  12987. (or (not todo-only)
  12988. (and (member todo org-not-done-keywords)
  12989. (or (not org-agenda-tags-todo-honor-ignore-options)
  12990. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item))))))
  12991. ;; select this headline
  12992. (cond
  12993. ((eq action 'sparse-tree)
  12994. (and org-highlight-sparse-tree-matches
  12995. (org-get-heading) (match-end 0)
  12996. (org-highlight-new-match
  12997. (match-beginning 1) (match-end 1)))
  12998. (org-show-context 'tags-tree))
  12999. ((eq action 'agenda)
  13000. (setq txt (org-agenda-format-item
  13001. ""
  13002. (concat
  13003. (if (eq org-tags-match-list-sublevels 'indented)
  13004. (make-string (1- level) ?.) "")
  13005. (org-get-heading))
  13006. level category
  13007. tags-list)
  13008. priority (org-get-priority txt))
  13009. (goto-char lspos)
  13010. (setq marker (org-agenda-new-marker))
  13011. (org-add-props txt props
  13012. 'org-marker marker 'org-hd-marker marker 'org-category category
  13013. 'todo-state todo
  13014. 'priority priority 'type "tagsmatch")
  13015. (push txt rtn))
  13016. ((functionp action)
  13017. (setq org-map-continue-from nil)
  13018. (save-excursion
  13019. (setq rtn1 (funcall action))
  13020. (push rtn1 rtn)))
  13021. (t (user-error "Invalid action")))
  13022. ;; if we are to skip sublevels, jump to end of subtree
  13023. (unless org-tags-match-list-sublevels
  13024. (org-end-of-subtree t)
  13025. (backward-char 1))))
  13026. ;; Get the correct position from where to continue
  13027. (if org-map-continue-from
  13028. (goto-char org-map-continue-from)
  13029. (and (= (point) lspos) (end-of-line 1)))))
  13030. (when (and (eq action 'sparse-tree)
  13031. (not org-sparse-tree-open-archived-trees))
  13032. (org-hide-archived-subtrees (point-min) (point-max)))
  13033. (nreverse rtn)))
  13034. (defun org-remove-uninherited-tags (tags)
  13035. "Remove all tags that are not inherited from the list TAGS."
  13036. (cond
  13037. ((eq org-use-tag-inheritance t)
  13038. (if org-tags-exclude-from-inheritance
  13039. (org-delete-all org-tags-exclude-from-inheritance tags)
  13040. tags))
  13041. ((not org-use-tag-inheritance) nil)
  13042. ((stringp org-use-tag-inheritance)
  13043. (delq nil (mapcar
  13044. (lambda (x)
  13045. (if (and (string-match org-use-tag-inheritance x)
  13046. (not (member x org-tags-exclude-from-inheritance)))
  13047. x nil))
  13048. tags)))
  13049. ((listp org-use-tag-inheritance)
  13050. (delq nil (mapcar
  13051. (lambda (x)
  13052. (if (member x org-use-tag-inheritance) x nil))
  13053. tags)))))
  13054. (defun org-match-sparse-tree (&optional todo-only match)
  13055. "Create a sparse tree according to tags string MATCH.
  13056. MATCH can contain positive and negative selection of tags, like
  13057. \"+WORK+URGENT-WITHBOSS\".
  13058. If optional argument TODO-ONLY is non-nil, only select lines that are
  13059. also TODO lines."
  13060. (interactive "P")
  13061. (org-agenda-prepare-buffers (list (current-buffer)))
  13062. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  13063. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  13064. (defvar org-cached-props nil)
  13065. (defun org-cached-entry-get (pom property)
  13066. (if (or (eq t org-use-property-inheritance)
  13067. (and (stringp org-use-property-inheritance)
  13068. (string-match org-use-property-inheritance property))
  13069. (and (listp org-use-property-inheritance)
  13070. (member property org-use-property-inheritance)))
  13071. ;; Caching is not possible, check it directly
  13072. (org-entry-get pom property 'inherit)
  13073. ;; Get all properties, so that we can do complicated checks easily
  13074. (cdr (assoc property (or org-cached-props
  13075. (setq org-cached-props
  13076. (org-entry-properties pom)))))))
  13077. (defun org-global-tags-completion-table (&optional files)
  13078. "Return the list of all tags in all agenda buffer/files.
  13079. Optional FILES argument is a list of files which can be used
  13080. instead of the agenda files."
  13081. (save-excursion
  13082. (org-uniquify
  13083. (delq nil
  13084. (apply 'append
  13085. (mapcar
  13086. (lambda (file)
  13087. (set-buffer (find-file-noselect file))
  13088. (append (org-get-buffer-tags)
  13089. (mapcar (lambda (x) (if (stringp (car-safe x))
  13090. (list (car-safe x)) nil))
  13091. org-tag-alist)))
  13092. (if (and files (car files))
  13093. files
  13094. (org-agenda-files))))))))
  13095. (defun org-make-tags-matcher (match)
  13096. "Create the TAGS/TODO matcher form for the selection string MATCH.
  13097. The variable `todo-only' is scoped dynamically into this function.
  13098. It will be set to t if the matcher restricts matching to TODO entries,
  13099. otherwise will not be touched.
  13100. Returns a cons of the selection string MATCH and the constructed
  13101. lisp form implementing the matcher. The matcher is to be evaluated
  13102. at an Org entry, with point on the headline, and returns t if the
  13103. entry matches the selection string MATCH. The returned lisp form
  13104. references two variables with information about the entry, which
  13105. must be bound around the form's evaluation: todo, the TODO keyword
  13106. at the entry (or nil of none); and tags-list, the list of all tags
  13107. at the entry including inherited ones. Additionally, the category
  13108. of the entry (if any) must be specified as the text property
  13109. 'org-category on the headline.
  13110. See also `org-scan-tags'.
  13111. "
  13112. (declare (special todo-only))
  13113. (unless (boundp 'todo-only)
  13114. (error "`org-make-tags-matcher' expects todo-only to be scoped in"))
  13115. (unless match
  13116. ;; Get a new match request, with completion against the global
  13117. ;; tags table and the local tags in current buffer
  13118. (let ((org-last-tags-completion-table
  13119. (org-uniquify
  13120. (delq nil (append (org-get-buffer-tags)
  13121. (org-global-tags-completion-table))))))
  13122. (setq match (org-completing-read-no-i
  13123. "Match: " 'org-tags-completion-function nil nil nil
  13124. 'org-tags-history))))
  13125. ;; Parse the string and create a lisp form
  13126. (let ((match0 match)
  13127. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  13128. minus tag mm
  13129. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  13130. orterms term orlist re-p str-p level-p level-op time-p
  13131. prop-p pn pv po gv rest (start 0) (ss 0))
  13132. ;; Expand group tags
  13133. (setq match (org-tags-expand match))
  13134. ;; Check if there is a TODO part of this match, which would be the
  13135. ;; part after a "/". TO make sure that this slash is not part of
  13136. ;; a property value to be matched against, we also check that there
  13137. ;; is no " after that slash.
  13138. ;; First, find the last slash
  13139. (while (string-match "/+" match ss)
  13140. (setq start (match-beginning 0) ss (match-end 0)))
  13141. (if (and (string-match "/+" match start)
  13142. (not (save-match-data (string-match "\"" match start))))
  13143. ;; match contains also a todo-matching request
  13144. (progn
  13145. (setq tagsmatch (substring match 0 (match-beginning 0))
  13146. todomatch (substring match (match-end 0)))
  13147. (if (string-match "^!" todomatch)
  13148. (setq todo-only t todomatch (substring todomatch 1)))
  13149. (if (string-match "^\\s-*$" todomatch)
  13150. (setq todomatch nil)))
  13151. ;; only matching tags
  13152. (setq tagsmatch match todomatch nil))
  13153. ;; Make the tags matcher
  13154. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  13155. (setq tagsmatcher t)
  13156. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  13157. (while (setq term (pop orterms))
  13158. (while (and (equal (substring term -1) "\\") orterms)
  13159. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  13160. (while (string-match re term)
  13161. (setq rest (substring term (match-end 0))
  13162. minus (and (match-end 1)
  13163. (equal (match-string 1 term) "-"))
  13164. tag (save-match-data (replace-regexp-in-string
  13165. "\\\\-" "-"
  13166. (match-string 2 term)))
  13167. re-p (equal (string-to-char tag) ?{)
  13168. level-p (match-end 4)
  13169. prop-p (match-end 5)
  13170. mm (cond
  13171. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  13172. (level-p
  13173. (setq level-op (org-op-to-function (match-string 3 term)))
  13174. `(,level-op level ,(string-to-number
  13175. (match-string 4 term))))
  13176. (prop-p
  13177. (setq pn (match-string 5 term)
  13178. po (match-string 6 term)
  13179. pv (match-string 7 term)
  13180. re-p (equal (string-to-char pv) ?{)
  13181. str-p (equal (string-to-char pv) ?\")
  13182. time-p (save-match-data
  13183. (string-match "^\"[[<].*[]>]\"$" pv))
  13184. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  13185. (if time-p (setq pv (org-matcher-time pv)))
  13186. (setq po (org-op-to-function po (if time-p 'time str-p)))
  13187. (cond
  13188. ((equal pn "CATEGORY")
  13189. (setq gv '(get-text-property (point) 'org-category)))
  13190. ((equal pn "TODO")
  13191. (setq gv 'todo))
  13192. (t
  13193. (setq gv `(org-cached-entry-get nil ,pn))))
  13194. (if re-p
  13195. (if (eq po 'org<>)
  13196. `(not (string-match ,pv (or ,gv "")))
  13197. `(string-match ,pv (or ,gv "")))
  13198. (if str-p
  13199. `(,po (or ,gv "") ,pv)
  13200. `(,po (string-to-number (or ,gv ""))
  13201. ,(string-to-number pv) ))))
  13202. (t `(member ,tag tags-list)))
  13203. mm (if minus (list 'not mm) mm)
  13204. term rest)
  13205. (push mm tagsmatcher))
  13206. (push (if (> (length tagsmatcher) 1)
  13207. (cons 'and tagsmatcher)
  13208. (car tagsmatcher))
  13209. orlist)
  13210. (setq tagsmatcher nil))
  13211. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  13212. (setq tagsmatcher
  13213. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  13214. ;; Make the todo matcher
  13215. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  13216. (setq todomatcher t)
  13217. (setq orterms (org-split-string todomatch "|") orlist nil)
  13218. (while (setq term (pop orterms))
  13219. (while (string-match re term)
  13220. (setq minus (and (match-end 1)
  13221. (equal (match-string 1 term) "-"))
  13222. kwd (match-string 2 term)
  13223. re-p (equal (string-to-char kwd) ?{)
  13224. term (substring term (match-end 0))
  13225. mm (if re-p
  13226. `(string-match ,(substring kwd 1 -1) todo)
  13227. (list 'equal 'todo kwd))
  13228. mm (if minus (list 'not mm) mm))
  13229. (push mm todomatcher))
  13230. (push (if (> (length todomatcher) 1)
  13231. (cons 'and todomatcher)
  13232. (car todomatcher))
  13233. orlist)
  13234. (setq todomatcher nil))
  13235. (setq todomatcher (if (> (length orlist) 1)
  13236. (cons 'or orlist) (car orlist))))
  13237. ;; Return the string and lisp forms of the matcher
  13238. (setq matcher (if todomatcher
  13239. (list 'and tagsmatcher todomatcher)
  13240. tagsmatcher))
  13241. (when todo-only
  13242. (setq matcher (list 'and '(member todo org-not-done-keywords)
  13243. matcher)))
  13244. (cons match0 matcher)))
  13245. (defun org-tags-expand (match &optional single-as-list downcased)
  13246. "Expand group tags in MATCH.
  13247. This replaces every group tag in MATCH with a regexp tag search.
  13248. For example, a group tag \"Work\" defined as { Work : Lab Conf }
  13249. will be replaced like this:
  13250. Work => {\\(?:Work\\|Lab\\|Conf\\)}
  13251. +Work => +{\\(?:Work\\|Lab\\|Conf\\)}
  13252. -Work => -{\\(?:Work\\|Lab\\|Conf\\)}
  13253. Replacing by a regexp preserves the structure of the match.
  13254. E.g., this expansion
  13255. Work|Home => {\\(?:Work\\|Lab\\|Conf\\}|Home
  13256. will match anything tagged with \"Lab\" and \"Home\", or tagged
  13257. with \"Conf\" and \"Home\" or tagged with \"Work\" and \"home\".
  13258. When the optional argument SINGLE-AS-LIST is non-nil, MATCH is
  13259. assumed to be a single group tag, and the function will return
  13260. the list of tags in this group.
  13261. When DOWNCASE is non-nil, expand downcased TAGS."
  13262. (if org-group-tags
  13263. (let* ((case-fold-search t)
  13264. (stable org-mode-syntax-table)
  13265. (tal (or org-tag-groups-alist-for-agenda
  13266. org-tag-groups-alist))
  13267. (tal (if downcased
  13268. (mapcar (lambda(tg) (mapcar 'downcase tg)) tal) tal))
  13269. (tml (mapcar 'car tal))
  13270. (rtnmatch match) rpl)
  13271. ;; @ and _ are allowed as word-components in tags
  13272. (modify-syntax-entry ?@ "w" stable)
  13273. (modify-syntax-entry ?_ "w" stable)
  13274. (while (and tml
  13275. (with-syntax-table stable
  13276. (string-match
  13277. (concat "\\(?1:[+-]?\\)\\(?2:\\<"
  13278. (regexp-opt tml) "\\>\\)") rtnmatch)))
  13279. (let* ((dir (match-string 1 rtnmatch))
  13280. (tag (match-string 2 rtnmatch))
  13281. (tag (if downcased (downcase tag) tag)))
  13282. (setq tml (delete tag tml))
  13283. (when (not (get-text-property 0 'grouptag (match-string 2 rtnmatch)))
  13284. (setq rpl (append (org-uniquify rpl) (assoc tag tal)))
  13285. (setq rpl (concat dir "{\\<" (regexp-opt rpl) "\\>}"))
  13286. (if (stringp rpl) (org-add-props rpl '(grouptag t)))
  13287. (setq rtnmatch (replace-match rpl t t rtnmatch)))))
  13288. (if single-as-list
  13289. (or (reverse rpl) (list rtnmatch))
  13290. rtnmatch))
  13291. (if single-as-list (list (if downcased (downcase match) match))
  13292. match)))
  13293. (defun org-op-to-function (op &optional stringp)
  13294. "Turn an operator into the appropriate function."
  13295. (setq op
  13296. (cond
  13297. ((equal op "<" ) '(< string< org-time<))
  13298. ((equal op ">" ) '(> org-string> org-time>))
  13299. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  13300. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  13301. ((member op '("=" "==")) '(= string= org-time=))
  13302. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  13303. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  13304. (defun org<> (a b) (not (= a b)))
  13305. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  13306. (defun org-string>= (a b) (not (string< a b)))
  13307. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  13308. (defun org-string<> (a b) (not (string= a b)))
  13309. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  13310. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  13311. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  13312. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  13313. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  13314. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  13315. (defun org-2ft (s)
  13316. "Convert S to a floating point time.
  13317. If S is already a number, just return it. If it is a string, parse
  13318. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  13319. (cond
  13320. ((numberp s) s)
  13321. ((stringp s)
  13322. (condition-case nil
  13323. (float-time (apply 'encode-time (org-parse-time-string s)))
  13324. (error 0.)))
  13325. (t 0.)))
  13326. (defun org-time-today ()
  13327. "Time in seconds today at 0:00.
  13328. Returns the float number of seconds since the beginning of the
  13329. epoch to the beginning of today (00:00)."
  13330. (float-time (apply 'encode-time
  13331. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  13332. (defun org-matcher-time (s)
  13333. "Interpret a time comparison value."
  13334. (save-match-data
  13335. (cond
  13336. ((string= s "<now>") (float-time))
  13337. ((string= s "<today>") (org-time-today))
  13338. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  13339. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  13340. ((string-match "^<\\([-+][0-9]+\\)\\([hdwmy]\\)>$" s)
  13341. (+ (org-time-today)
  13342. (* (string-to-number (match-string 1 s))
  13343. (cdr (assoc (match-string 2 s)
  13344. '(("d" . 86400.0) ("w" . 604800.0)
  13345. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  13346. (t (org-2ft s)))))
  13347. (defun org-match-any-p (re list)
  13348. "Does re match any element of list?"
  13349. (setq list (mapcar (lambda (x) (string-match re x)) list))
  13350. (delq nil list))
  13351. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  13352. (defvar org-tags-overlay (make-overlay 1 1))
  13353. (org-detach-overlay org-tags-overlay)
  13354. (defun org-get-local-tags-at (&optional pos)
  13355. "Get a list of tags defined in the current headline."
  13356. (org-get-tags-at pos 'local))
  13357. (defun org-get-local-tags ()
  13358. "Get a list of tags defined in the current headline."
  13359. (org-get-tags-at nil 'local))
  13360. (defun org-get-tags-at (&optional pos local)
  13361. "Get a list of all headline tags applicable at POS.
  13362. POS defaults to point. If tags are inherited, the list contains
  13363. the targets in the same sequence as the headlines appear, i.e.
  13364. the tags of the current headline come last.
  13365. When LOCAL is non-nil, only return tags from the current headline,
  13366. ignore inherited ones."
  13367. (interactive)
  13368. (if (and org-trust-scanner-tags
  13369. (or (not pos) (equal pos (point)))
  13370. (not local))
  13371. org-scanner-tags
  13372. (let (tags ltags lastpos parent)
  13373. (save-excursion
  13374. (save-restriction
  13375. (widen)
  13376. (goto-char (or pos (point)))
  13377. (save-match-data
  13378. (catch 'done
  13379. (condition-case nil
  13380. (progn
  13381. (org-back-to-heading t)
  13382. (while (not (equal lastpos (point)))
  13383. (setq lastpos (point))
  13384. (when (looking-at
  13385. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  13386. (setq ltags (org-split-string
  13387. (org-match-string-no-properties 1) ":"))
  13388. (when parent
  13389. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  13390. (setq tags (append
  13391. (if parent
  13392. (org-remove-uninherited-tags ltags)
  13393. ltags)
  13394. tags)))
  13395. (or org-use-tag-inheritance (throw 'done t))
  13396. (if local (throw 'done t))
  13397. (or (org-up-heading-safe) (error nil))
  13398. (setq parent t)))
  13399. (error nil)))))
  13400. (if local
  13401. tags
  13402. (reverse (delete-dups
  13403. (reverse (append
  13404. (org-remove-uninherited-tags
  13405. org-file-tags) tags)))))))))
  13406. (defun org-add-prop-inherited (s)
  13407. (add-text-properties 0 (length s) '(inherited t) s)
  13408. s)
  13409. (defun org-toggle-tag (tag &optional onoff)
  13410. "Toggle the tag TAG for the current line.
  13411. If ONOFF is `on' or `off', don't toggle but set to this state."
  13412. (let (res current)
  13413. (save-excursion
  13414. (org-back-to-heading t)
  13415. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  13416. (point-at-eol) t)
  13417. (progn
  13418. (setq current (match-string 1))
  13419. (replace-match ""))
  13420. (setq current ""))
  13421. (setq current (nreverse (org-split-string current ":")))
  13422. (cond
  13423. ((eq onoff 'on)
  13424. (setq res t)
  13425. (or (member tag current) (push tag current)))
  13426. ((eq onoff 'off)
  13427. (or (not (member tag current)) (setq current (delete tag current))))
  13428. (t (if (member tag current)
  13429. (setq current (delete tag current))
  13430. (setq res t)
  13431. (push tag current))))
  13432. (end-of-line 1)
  13433. (if current
  13434. (progn
  13435. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  13436. (org-set-tags nil t))
  13437. (delete-horizontal-space))
  13438. (run-hooks 'org-after-tags-change-hook))
  13439. res))
  13440. (defun org-align-tags-here (to-col)
  13441. ;; Assumes that this is a headline
  13442. "Align tags on the current headline to TO-COL."
  13443. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  13444. (beginning-of-line 1)
  13445. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13446. (< pos (match-beginning 2)))
  13447. (progn
  13448. (setq tags-l (- (match-end 2) (match-beginning 2)))
  13449. (goto-char (match-beginning 1))
  13450. (insert " ")
  13451. (delete-region (point) (1+ (match-beginning 2)))
  13452. (setq ncol (max (current-column)
  13453. (1+ col)
  13454. (if (> to-col 0)
  13455. to-col
  13456. (- (abs to-col) tags-l))))
  13457. (setq p (point))
  13458. (insert (make-string (- ncol (current-column)) ?\ ))
  13459. (setq ncol (current-column))
  13460. (when indent-tabs-mode (tabify p (point-at-eol)))
  13461. (org-move-to-column (min ncol col)))
  13462. (goto-char pos))))
  13463. (defun org-set-tags-command (&optional arg just-align)
  13464. "Call the set-tags command for the current entry."
  13465. (interactive "P")
  13466. (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
  13467. (org-set-tags arg just-align)
  13468. (save-excursion
  13469. (unless (and (org-region-active-p)
  13470. org-loop-over-headlines-in-active-region)
  13471. (org-back-to-heading t))
  13472. (org-set-tags arg just-align))))
  13473. (defun org-set-tags-to (data)
  13474. "Set the tags of the current entry to DATA, replacing the current tags.
  13475. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  13476. If DATA is nil or the empty string, any tags will be removed."
  13477. (interactive "sTags: ")
  13478. (setq data
  13479. (cond
  13480. ((eq data nil) "")
  13481. ((equal data "") "")
  13482. ((stringp data)
  13483. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  13484. ":"))
  13485. ((listp data)
  13486. (concat ":" (mapconcat 'identity data ":") ":"))))
  13487. (when data
  13488. (save-excursion
  13489. (org-back-to-heading t)
  13490. (when (looking-at org-complex-heading-regexp)
  13491. (if (match-end 5)
  13492. (progn
  13493. (goto-char (match-beginning 5))
  13494. (insert data)
  13495. (delete-region (point) (point-at-eol))
  13496. (org-set-tags nil 'align))
  13497. (goto-char (point-at-eol))
  13498. (insert " " data)
  13499. (org-set-tags nil 'align)))
  13500. (beginning-of-line 1)
  13501. (if (looking-at ".*?\\([ \t]+\\)$")
  13502. (delete-region (match-beginning 1) (match-end 1))))))
  13503. (defun org-align-all-tags ()
  13504. "Align the tags i all headings."
  13505. (interactive)
  13506. (save-excursion
  13507. (or (ignore-errors (org-back-to-heading t))
  13508. (outline-next-heading))
  13509. (if (org-at-heading-p)
  13510. (org-set-tags t)
  13511. (message "No headings"))))
  13512. (defvar org-indent-indentation-per-level)
  13513. (defun org-set-tags (&optional arg just-align)
  13514. "Set the tags for the current headline.
  13515. With prefix ARG, realign all tags in headings in the current buffer.
  13516. When JUST-ALIGN is non-nil, only align tags."
  13517. (interactive "P")
  13518. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  13519. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  13520. 'region-start-level 'region))
  13521. org-loop-over-headlines-in-active-region)
  13522. (org-map-entries
  13523. ;; We don't use ARG and JUST-ALIGN here because these args
  13524. ;; are not useful when looping over headlines.
  13525. `(org-set-tags)
  13526. org-loop-over-headlines-in-active-region
  13527. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  13528. (let* ((re org-outline-regexp-bol)
  13529. (current (unless arg (org-get-tags-string)))
  13530. (col (current-column))
  13531. (org-setting-tags t)
  13532. table current-tags inherited-tags ; computed below when needed
  13533. tags p0 c0 c1 rpl di tc level)
  13534. (if arg
  13535. (save-excursion
  13536. (goto-char (point-min))
  13537. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  13538. (while (re-search-forward re nil t)
  13539. (org-set-tags nil t)
  13540. (end-of-line 1)))
  13541. (message "All tags realigned to column %d" org-tags-column))
  13542. (if just-align
  13543. (setq tags current)
  13544. ;; Get a new set of tags from the user
  13545. (save-excursion
  13546. (setq table (append org-tag-persistent-alist
  13547. (or org-tag-alist (org-get-buffer-tags))
  13548. (and
  13549. org-complete-tags-always-offer-all-agenda-tags
  13550. (org-global-tags-completion-table
  13551. (org-agenda-files))))
  13552. org-last-tags-completion-table table
  13553. current-tags (org-split-string current ":")
  13554. inherited-tags (nreverse
  13555. (nthcdr (length current-tags)
  13556. (nreverse (org-get-tags-at))))
  13557. tags
  13558. (if (or (eq t org-use-fast-tag-selection)
  13559. (and org-use-fast-tag-selection
  13560. (delq nil (mapcar 'cdr table))))
  13561. (org-fast-tag-selection
  13562. current-tags inherited-tags table
  13563. (if org-fast-tag-selection-include-todo
  13564. org-todo-key-alist))
  13565. (let ((org-add-colon-after-tag-completion (< 1 (length table))))
  13566. (org-trim
  13567. (org-icompleting-read "Tags: "
  13568. 'org-tags-completion-function
  13569. nil nil current 'org-tags-history))))))
  13570. (while (string-match "[-+&]+" tags)
  13571. ;; No boolean logic, just a list
  13572. (setq tags (replace-match ":" t t tags))))
  13573. (setq tags (replace-regexp-in-string "[,]" ":" tags))
  13574. (if org-tags-sort-function
  13575. (setq tags (mapconcat 'identity
  13576. (sort (org-split-string
  13577. tags (org-re "[^[:alnum:]_@#%]+"))
  13578. org-tags-sort-function) ":")))
  13579. (if (string-match "\\`[\t ]*\\'" tags)
  13580. (setq tags "")
  13581. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  13582. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  13583. ;; Insert new tags at the correct column
  13584. (beginning-of-line 1)
  13585. (setq level (or (and (looking-at org-outline-regexp)
  13586. (- (match-end 0) (point) 1))
  13587. 1))
  13588. (cond
  13589. ((and (equal current "") (equal tags "")))
  13590. ((re-search-forward
  13591. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  13592. (point-at-eol) t)
  13593. (if (equal tags "")
  13594. (setq rpl "")
  13595. (goto-char (match-beginning 0))
  13596. (setq c0 (current-column)
  13597. ;; compute offset for the case of org-indent-mode active
  13598. di (if (org-bound-and-true-p org-indent-mode)
  13599. (* (1- org-indent-indentation-per-level) (1- level))
  13600. 0)
  13601. p0 (if (equal (char-before) ?*) (1+ (point)) (point))
  13602. tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
  13603. c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (string-width tags))))
  13604. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  13605. (replace-match rpl t t)
  13606. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  13607. tags)
  13608. (t (error "Tags alignment failed")))
  13609. (org-move-to-column col)
  13610. (unless just-align
  13611. (run-hooks 'org-after-tags-change-hook))))))
  13612. (defun org-change-tag-in-region (beg end tag off)
  13613. "Add or remove TAG for each entry in the region.
  13614. This works in the agenda, and also in an org-mode buffer."
  13615. (interactive
  13616. (list (region-beginning) (region-end)
  13617. (let ((org-last-tags-completion-table
  13618. (if (derived-mode-p 'org-mode)
  13619. (org-uniquify
  13620. (delq nil (append (org-get-buffer-tags)
  13621. (org-global-tags-completion-table))))
  13622. (org-global-tags-completion-table))))
  13623. (org-icompleting-read
  13624. "Tag: " 'org-tags-completion-function nil nil nil
  13625. 'org-tags-history))
  13626. (progn
  13627. (message "[s]et or [r]emove? ")
  13628. (equal (read-char-exclusive) ?r))))
  13629. (if (fboundp 'deactivate-mark) (deactivate-mark))
  13630. (let ((agendap (equal major-mode 'org-agenda-mode))
  13631. l1 l2 m buf pos newhead (cnt 0))
  13632. (goto-char end)
  13633. (setq l2 (1- (org-current-line)))
  13634. (goto-char beg)
  13635. (setq l1 (org-current-line))
  13636. (loop for l from l1 to l2 do
  13637. (org-goto-line l)
  13638. (setq m (get-text-property (point) 'org-hd-marker))
  13639. (when (or (and (derived-mode-p 'org-mode) (org-at-heading-p))
  13640. (and agendap m))
  13641. (setq buf (if agendap (marker-buffer m) (current-buffer))
  13642. pos (if agendap m (point)))
  13643. (with-current-buffer buf
  13644. (save-excursion
  13645. (save-restriction
  13646. (goto-char pos)
  13647. (setq cnt (1+ cnt))
  13648. (org-toggle-tag tag (if off 'off 'on))
  13649. (setq newhead (org-get-heading)))))
  13650. (and agendap (org-agenda-change-all-lines newhead m))))
  13651. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  13652. (defun org-tags-completion-function (string predicate &optional flag)
  13653. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  13654. (confirm (lambda (x) (stringp (car x)))))
  13655. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  13656. (setq s1 (match-string 1 string)
  13657. s2 (match-string 2 string))
  13658. (setq s1 "" s2 string))
  13659. (cond
  13660. ((eq flag nil)
  13661. ;; try completion
  13662. (setq rtn (try-completion s2 ctable confirm))
  13663. (if (stringp rtn)
  13664. (setq rtn
  13665. (concat s1 s2 (substring rtn (length s2))
  13666. (if (and org-add-colon-after-tag-completion
  13667. (assoc rtn ctable))
  13668. ":" ""))))
  13669. rtn)
  13670. ((eq flag t)
  13671. ;; all-completions
  13672. (all-completions s2 ctable confirm))
  13673. ((eq flag 'lambda)
  13674. ;; exact match?
  13675. (assoc s2 ctable)))))
  13676. (defun org-fast-tag-insert (kwd tags face &optional end)
  13677. "Insert KDW, and the TAGS, the latter with face FACE.
  13678. Also insert END."
  13679. (insert (format "%-12s" (concat kwd ":"))
  13680. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  13681. (or end "")))
  13682. (defun org-fast-tag-show-exit (flag)
  13683. (save-excursion
  13684. (org-goto-line 3)
  13685. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  13686. (replace-match ""))
  13687. (when flag
  13688. (end-of-line 1)
  13689. (org-move-to-column (- (window-width) 19) t)
  13690. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  13691. (defun org-set-current-tags-overlay (current prefix)
  13692. "Add an overlay to CURRENT tag with PREFIX."
  13693. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  13694. (if (featurep 'xemacs)
  13695. (org-overlay-display org-tags-overlay (concat prefix s)
  13696. 'secondary-selection)
  13697. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  13698. (org-overlay-display org-tags-overlay (concat prefix s)))))
  13699. (defvar org-last-tag-selection-key nil)
  13700. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  13701. "Fast tag selection with single keys.
  13702. CURRENT is the current list of tags in the headline, INHERITED is the
  13703. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  13704. possibly with grouping information. TODO-TABLE is a similar table with
  13705. TODO keywords, should these have keys assigned to them.
  13706. If the keys are nil, a-z are automatically assigned.
  13707. Returns the new tags string, or nil to not change the current settings."
  13708. (let* ((fulltable (append table todo-table))
  13709. (maxlen (apply 'max (mapcar
  13710. (lambda (x)
  13711. (if (stringp (car x)) (string-width (car x)) 0))
  13712. fulltable)))
  13713. (buf (current-buffer))
  13714. (expert (eq org-fast-tag-selection-single-key 'expert))
  13715. (buffer-tags nil)
  13716. (fwidth (+ maxlen 3 1 3))
  13717. (ncol (/ (- (window-width) 4) fwidth))
  13718. (i-face 'org-done)
  13719. (c-face 'org-todo)
  13720. tg cnt e c char c1 c2 ntable tbl rtn
  13721. ov-start ov-end ov-prefix
  13722. (exit-after-next org-fast-tag-selection-single-key)
  13723. (done-keywords org-done-keywords)
  13724. groups ingroup)
  13725. (save-excursion
  13726. (beginning-of-line 1)
  13727. (if (looking-at
  13728. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13729. (setq ov-start (match-beginning 1)
  13730. ov-end (match-end 1)
  13731. ov-prefix "")
  13732. (setq ov-start (1- (point-at-eol))
  13733. ov-end (1+ ov-start))
  13734. (skip-chars-forward "^\n\r")
  13735. (setq ov-prefix
  13736. (concat
  13737. (buffer-substring (1- (point)) (point))
  13738. (if (> (current-column) org-tags-column)
  13739. " "
  13740. (make-string (- org-tags-column (current-column)) ?\ ))))))
  13741. (move-overlay org-tags-overlay ov-start ov-end)
  13742. (save-window-excursion
  13743. (if expert
  13744. (set-buffer (get-buffer-create " *Org tags*"))
  13745. (delete-other-windows)
  13746. (set-window-buffer (split-window-vertically) (get-buffer-create " *Org tags*"))
  13747. (org-switch-to-buffer-other-window " *Org tags*"))
  13748. (erase-buffer)
  13749. (org-set-local 'org-done-keywords done-keywords)
  13750. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  13751. (org-fast-tag-insert "Current" current c-face "\n\n")
  13752. (org-fast-tag-show-exit exit-after-next)
  13753. (org-set-current-tags-overlay current ov-prefix)
  13754. (setq tbl fulltable char ?a cnt 0)
  13755. (while (setq e (pop tbl))
  13756. (cond
  13757. ((equal (car e) :startgroup)
  13758. (push '() groups) (setq ingroup t)
  13759. (when (not (= cnt 0))
  13760. (setq cnt 0)
  13761. (insert "\n"))
  13762. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  13763. ((equal (car e) :endgroup)
  13764. (setq ingroup nil cnt 0)
  13765. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  13766. ((equal e '(:newline))
  13767. (when (not (= cnt 0))
  13768. (setq cnt 0)
  13769. (insert "\n")
  13770. (setq e (car tbl))
  13771. (while (equal (car tbl) '(:newline))
  13772. (insert "\n")
  13773. (setq tbl (cdr tbl)))))
  13774. ((equal e '(:grouptags)) nil)
  13775. (t
  13776. (setq tg (copy-sequence (car e)) c2 nil)
  13777. (if (cdr e)
  13778. (setq c (cdr e))
  13779. ;; automatically assign a character.
  13780. (setq c1 (string-to-char
  13781. (downcase (substring
  13782. tg (if (= (string-to-char tg) ?@) 1 0)))))
  13783. (if (or (rassoc c1 ntable) (rassoc c1 table))
  13784. (while (or (rassoc char ntable) (rassoc char table))
  13785. (setq char (1+ char)))
  13786. (setq c2 c1))
  13787. (setq c (or c2 char)))
  13788. (if ingroup (push tg (car groups)))
  13789. (setq tg (org-add-props tg nil 'face
  13790. (cond
  13791. ((not (assoc tg table))
  13792. (org-get-todo-face tg))
  13793. ((member tg current) c-face)
  13794. ((member tg inherited) i-face))))
  13795. (if (equal (caar tbl) :grouptags)
  13796. (org-add-props tg nil 'face 'org-tag-group))
  13797. (if (and (= cnt 0) (not ingroup)) (insert " "))
  13798. (insert "[" c "] " tg (make-string
  13799. (- fwidth 4 (length tg)) ?\ ))
  13800. (push (cons tg c) ntable)
  13801. (when (= (setq cnt (1+ cnt)) ncol)
  13802. (insert "\n")
  13803. (if ingroup (insert " "))
  13804. (setq cnt 0)))))
  13805. (setq ntable (nreverse ntable))
  13806. (insert "\n")
  13807. (goto-char (point-min))
  13808. (if (not expert) (org-fit-window-to-buffer))
  13809. (setq rtn
  13810. (catch 'exit
  13811. (while t
  13812. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  13813. (if (not groups) "no " "")
  13814. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  13815. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  13816. (setq org-last-tag-selection-key c)
  13817. (cond
  13818. ((= c ?\r) (throw 'exit t))
  13819. ((= c ?!)
  13820. (setq groups (not groups))
  13821. (goto-char (point-min))
  13822. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  13823. ((= c ?\C-c)
  13824. (if (not expert)
  13825. (org-fast-tag-show-exit
  13826. (setq exit-after-next (not exit-after-next)))
  13827. (setq expert nil)
  13828. (delete-other-windows)
  13829. (set-window-buffer (split-window-vertically) " *Org tags*")
  13830. (org-switch-to-buffer-other-window " *Org tags*")
  13831. (org-fit-window-to-buffer)))
  13832. ((or (= c ?\C-g)
  13833. (and (= c ?q) (not (rassoc c ntable))))
  13834. (org-detach-overlay org-tags-overlay)
  13835. (setq quit-flag t))
  13836. ((= c ?\ )
  13837. (setq current nil)
  13838. (if exit-after-next (setq exit-after-next 'now)))
  13839. ((= c ?\t)
  13840. (condition-case nil
  13841. (setq tg (org-icompleting-read
  13842. "Tag: "
  13843. (or buffer-tags
  13844. (with-current-buffer buf
  13845. (org-get-buffer-tags)))))
  13846. (quit (setq tg "")))
  13847. (when (string-match "\\S-" tg)
  13848. (add-to-list 'buffer-tags (list tg))
  13849. (if (member tg current)
  13850. (setq current (delete tg current))
  13851. (push tg current)))
  13852. (if exit-after-next (setq exit-after-next 'now)))
  13853. ((setq e (rassoc c todo-table) tg (car e))
  13854. (with-current-buffer buf
  13855. (save-excursion (org-todo tg)))
  13856. (if exit-after-next (setq exit-after-next 'now)))
  13857. ((setq e (rassoc c ntable) tg (car e))
  13858. (if (member tg current)
  13859. (setq current (delete tg current))
  13860. (loop for g in groups do
  13861. (if (member tg g)
  13862. (mapc (lambda (x)
  13863. (setq current (delete x current)))
  13864. g)))
  13865. (push tg current))
  13866. (if exit-after-next (setq exit-after-next 'now))))
  13867. ;; Create a sorted list
  13868. (setq current
  13869. (sort current
  13870. (lambda (a b)
  13871. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  13872. (if (eq exit-after-next 'now) (throw 'exit t))
  13873. (goto-char (point-min))
  13874. (beginning-of-line 2)
  13875. (delete-region (point) (point-at-eol))
  13876. (org-fast-tag-insert "Current" current c-face)
  13877. (org-set-current-tags-overlay current ov-prefix)
  13878. (while (re-search-forward
  13879. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  13880. (setq tg (match-string 1))
  13881. (add-text-properties
  13882. (match-beginning 1) (match-end 1)
  13883. (list 'face
  13884. (cond
  13885. ((member tg current) c-face)
  13886. ((member tg inherited) i-face)
  13887. (t (get-text-property (match-beginning 1) 'face))))))
  13888. (goto-char (point-min)))))
  13889. (org-detach-overlay org-tags-overlay)
  13890. (if rtn
  13891. (mapconcat 'identity current ":")
  13892. nil))))
  13893. (defun org-get-tags-string ()
  13894. "Get the TAGS string in the current headline."
  13895. (unless (org-at-heading-p t)
  13896. (user-error "Not on a heading"))
  13897. (save-excursion
  13898. (beginning-of-line 1)
  13899. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13900. (org-match-string-no-properties 1)
  13901. "")))
  13902. (defun org-get-tags ()
  13903. "Get the list of tags specified in the current headline."
  13904. (org-split-string (org-get-tags-string) ":"))
  13905. (defun org-get-buffer-tags ()
  13906. "Get a table of all tags used in the buffer, for completion."
  13907. (org-with-wide-buffer
  13908. (goto-char (point-min))
  13909. (let ((tag-re (concat org-outline-regexp-bol
  13910. "\\(?:.*?[ \t]\\)?"
  13911. (org-re ":\\([[:alnum:]_@#%:]+\\):[ \t]*$")))
  13912. tags)
  13913. (while (re-search-forward tag-re nil t)
  13914. (dolist (tag (org-split-string (org-match-string-no-properties 1) ":"))
  13915. (push tag tags)))
  13916. (mapcar #'list (append org-file-tags (org-uniquify tags))))))
  13917. ;;;; The mapping API
  13918. (defun org-map-entries (func &optional match scope &rest skip)
  13919. "Call FUNC at each headline selected by MATCH in SCOPE.
  13920. FUNC is a function or a lisp form. The function will be called without
  13921. arguments, with the cursor positioned at the beginning of the headline.
  13922. The return values of all calls to the function will be collected and
  13923. returned as a list.
  13924. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  13925. does not need to preserve point. After evaluation, the cursor will be
  13926. moved to the end of the line (presumably of the headline of the
  13927. processed entry) and search continues from there. Under some
  13928. circumstances, this may not produce the wanted results. For example,
  13929. if you have removed (e.g. archived) the current (sub)tree it could
  13930. mean that the next entry will be skipped entirely. In such cases, you
  13931. can specify the position from where search should continue by making
  13932. FUNC set the variable `org-map-continue-from' to the desired buffer
  13933. position.
  13934. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  13935. Only headlines that are matched by this query will be considered during
  13936. the iteration. When MATCH is nil or t, all headlines will be
  13937. visited by the iteration.
  13938. SCOPE determines the scope of this command. It can be any of:
  13939. nil The current buffer, respecting the restriction if any
  13940. tree The subtree started with the entry at point
  13941. region The entries within the active region, if any
  13942. region-start-level
  13943. The entries within the active region, but only those at
  13944. the same level than the first one.
  13945. file The current buffer, without restriction
  13946. file-with-archives
  13947. The current buffer, and any archives associated with it
  13948. agenda All agenda files
  13949. agenda-with-archives
  13950. All agenda files with any archive files associated with them
  13951. \(file1 file2 ...)
  13952. If this is a list, all files in the list will be scanned
  13953. The remaining args are treated as settings for the skipping facilities of
  13954. the scanner. The following items can be given here:
  13955. archive skip trees with the archive tag
  13956. comment skip trees with the COMMENT keyword
  13957. function or Emacs Lisp form:
  13958. will be used as value for `org-agenda-skip-function', so
  13959. whenever the function returns a position, FUNC will not be
  13960. called for that entry and search will continue from the
  13961. position returned
  13962. If your function needs to retrieve the tags including inherited tags
  13963. at the *current* entry, you can use the value of the variable
  13964. `org-scanner-tags' which will be much faster than getting the value
  13965. with `org-get-tags-at'. If your function gets properties with
  13966. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  13967. to t around the call to `org-entry-properties' to get the same speedup.
  13968. Note that if your function moves around to retrieve tags and properties at
  13969. a *different* entry, you cannot use these techniques."
  13970. (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
  13971. (not (org-region-active-p)))
  13972. (let* ((org-agenda-archives-mode nil) ; just to make sure
  13973. (org-agenda-skip-archived-trees (memq 'archive skip))
  13974. (org-agenda-skip-comment-trees (memq 'comment skip))
  13975. (org-agenda-skip-function
  13976. (car (org-delete-all '(comment archive) skip)))
  13977. (org-tags-match-list-sublevels t)
  13978. (start-level (eq scope 'region-start-level))
  13979. matcher file res
  13980. org-todo-keywords-for-agenda
  13981. org-done-keywords-for-agenda
  13982. org-todo-keyword-alist-for-agenda
  13983. org-tag-alist-for-agenda
  13984. todo-only)
  13985. (cond
  13986. ((eq match t) (setq matcher t))
  13987. ((eq match nil) (setq matcher t))
  13988. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  13989. (save-excursion
  13990. (save-restriction
  13991. (cond ((eq scope 'tree)
  13992. (org-back-to-heading t)
  13993. (org-narrow-to-subtree)
  13994. (setq scope nil))
  13995. ((and (or (eq scope 'region) (eq scope 'region-start-level))
  13996. (org-region-active-p))
  13997. ;; If needed, set start-level to a string like "2"
  13998. (when start-level
  13999. (save-excursion
  14000. (goto-char (region-beginning))
  14001. (unless (org-at-heading-p) (outline-next-heading))
  14002. (setq start-level (org-current-level))))
  14003. (narrow-to-region (region-beginning)
  14004. (save-excursion
  14005. (goto-char (region-end))
  14006. (unless (and (bolp) (org-at-heading-p))
  14007. (outline-next-heading))
  14008. (point)))
  14009. (setq scope nil)))
  14010. (if (not scope)
  14011. (progn
  14012. (org-agenda-prepare-buffers
  14013. (list (buffer-file-name (current-buffer))))
  14014. (setq res (org-scan-tags func matcher todo-only start-level)))
  14015. ;; Get the right scope
  14016. (cond
  14017. ((and scope (listp scope) (symbolp (car scope)))
  14018. (setq scope (eval scope)))
  14019. ((eq scope 'agenda)
  14020. (setq scope (org-agenda-files t)))
  14021. ((eq scope 'agenda-with-archives)
  14022. (setq scope (org-agenda-files t))
  14023. (setq scope (org-add-archive-files scope)))
  14024. ((eq scope 'file)
  14025. (setq scope (list (buffer-file-name))))
  14026. ((eq scope 'file-with-archives)
  14027. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  14028. (org-agenda-prepare-buffers scope)
  14029. (while (setq file (pop scope))
  14030. (with-current-buffer (org-find-base-buffer-visiting file)
  14031. (save-excursion
  14032. (save-restriction
  14033. (widen)
  14034. (goto-char (point-min))
  14035. (setq res (append res (org-scan-tags func matcher todo-only))))))))))
  14036. res)))
  14037. ;;; Properties API
  14038. (defconst org-special-properties
  14039. '("ALLTAGS" "BLOCKED" "CLOCKSUM" "CLOCKSUM_T" "CLOSED" "DEADLINE" "FILE"
  14040. "ITEM" "PRIORITY" "SCHEDULED" "TAGS" "TIMESTAMP" "TIMESTAMP_IA" "TODO")
  14041. "The special properties valid in Org mode.
  14042. These are properties that are not defined in the property drawer,
  14043. but in some other way.")
  14044. (defconst org-default-properties
  14045. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  14046. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  14047. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  14048. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  14049. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE" "UNNUMBERED"
  14050. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  14051. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  14052. "Some properties that are used by Org mode for various purposes.
  14053. Being in this list makes sure that they are offered for completion.")
  14054. (defun org--update-property-plist (key val props)
  14055. "Associate KEY to VAL in alist PROPS.
  14056. Modifications are made by side-effect. Return new alist."
  14057. (let* ((appending (string= (substring key -1) "+"))
  14058. (key (if appending (substring key 0 -1) key))
  14059. (old (assoc-string key props t)))
  14060. (if (not old) (cons (cons key val) props)
  14061. (setcdr old (if appending (concat (cdr old) " " val) val))
  14062. props)))
  14063. (defun org-get-property-block (&optional beg force)
  14064. "Return the (beg . end) range of the body of the property drawer.
  14065. BEG is the beginning of the current subtree, or of the part
  14066. before the first headline. If it is not given, it will be found.
  14067. If the drawer does not exist, create it if FORCE is non-nil, or
  14068. return nil."
  14069. (org-with-wide-buffer
  14070. (when beg (goto-char beg))
  14071. (unless (org-before-first-heading-p)
  14072. (let ((beg (cond (beg)
  14073. ((or (not (featurep 'org-inlinetask))
  14074. (org-inlinetask-in-task-p))
  14075. (org-back-to-heading t))
  14076. (t (org-with-limited-levels (org-back-to-heading t))))))
  14077. (forward-line)
  14078. (when (org-looking-at-p org-planning-line-re) (forward-line))
  14079. (cond ((looking-at org-property-drawer-re)
  14080. (forward-line)
  14081. (cons (point) (progn (goto-char (match-end 0))
  14082. (line-beginning-position))))
  14083. (force
  14084. (goto-char beg)
  14085. (org-insert-property-drawer)
  14086. (let ((pos (save-excursion (search-forward ":END:")
  14087. (line-beginning-position))))
  14088. (cons pos pos))))))))
  14089. (defun org-at-property-p ()
  14090. "Non-nil when point is inside a property drawer.
  14091. See `org-property-re' for match data, if applicable."
  14092. (save-excursion
  14093. (beginning-of-line)
  14094. (and (looking-at org-property-re)
  14095. (let ((property-drawer (save-match-data (org-get-property-block))))
  14096. (and property-drawer (< (point) (cdr property-drawer)))))))
  14097. (defun org-property-action ()
  14098. "Do an action on properties."
  14099. (interactive)
  14100. (unless (org-at-property-p) (user-error "Not at a property"))
  14101. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  14102. (let ((c (read-char-exclusive)))
  14103. (case c
  14104. (?s (call-interactively #'org-set-property))
  14105. (?d (call-interactively #'org-delete-property))
  14106. (?D (call-interactively #'org-delete-property-globally))
  14107. (?c (call-interactively #'org-compute-property-at-point))
  14108. (otherwise (user-error "No such property action %c" c)))))
  14109. (defun org-inc-effort ()
  14110. "Increment the value of the effort property in the current entry."
  14111. (interactive)
  14112. (org-set-effort nil t))
  14113. (defvar org-clock-effort) ; Defined in org-clock.el.
  14114. (defvar org-clock-current-task) ; Defined in org-clock.el.
  14115. (defun org-set-effort (&optional value increment)
  14116. "Set the effort property of the current entry.
  14117. With numerical prefix arg, use the nth allowed value, 0 stands for the
  14118. 10th allowed value.
  14119. When INCREMENT is non-nil, set the property to the next allowed value."
  14120. (interactive "P")
  14121. (if (equal value 0) (setq value 10))
  14122. (let* ((completion-ignore-case t)
  14123. (prop org-effort-property)
  14124. (cur (org-entry-get nil prop))
  14125. (allowed (org-property-get-allowed-values nil prop 'table))
  14126. (existing (mapcar 'list (org-property-values prop)))
  14127. (heading (nth 4 (org-heading-components)))
  14128. rpl
  14129. (val (cond
  14130. ((stringp value) value)
  14131. ((and allowed (integerp value))
  14132. (or (car (nth (1- value) allowed))
  14133. (car (org-last allowed))))
  14134. ((and allowed increment)
  14135. (or (caadr (member (list cur) allowed))
  14136. (user-error "Allowed effort values are not set")))
  14137. (allowed
  14138. (message "Select 1-9,0, [RET%s]: %s"
  14139. (if cur (concat "=" cur) "")
  14140. (mapconcat 'car allowed " "))
  14141. (setq rpl (read-char-exclusive))
  14142. (if (equal rpl ?\r)
  14143. cur
  14144. (setq rpl (- rpl ?0))
  14145. (if (equal rpl 0) (setq rpl 10))
  14146. (if (and (> rpl 0) (<= rpl (length allowed)))
  14147. (car (nth (1- rpl) allowed))
  14148. (org-completing-read "Effort: " allowed nil))))
  14149. (t
  14150. (let (org-completion-use-ido org-completion-use-iswitchb)
  14151. (org-completing-read
  14152. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  14153. (concat "[" cur "]") "")
  14154. ": ")
  14155. existing nil nil "" nil cur))))))
  14156. (unless (equal (org-entry-get nil prop) val)
  14157. (org-entry-put nil prop val))
  14158. (org-refresh-property
  14159. '((effort . identity)
  14160. (effort-minutes . org-duration-string-to-minutes))
  14161. val)
  14162. (when (string= heading org-clock-current-task)
  14163. (setq org-clock-effort (get-text-property (point-at-bol) 'effort))
  14164. (org-clock-update-mode-line))
  14165. (message "%s is now %s" prop val)))
  14166. (defun org-entry-properties (&optional pom which)
  14167. "Get all properties of the current entry.
  14168. When POM is a buffer position, get all properties from the entry
  14169. there instead.
  14170. This includes the TODO keyword, the tags, time strings for
  14171. deadline, scheduled, and clocking, and any additional properties
  14172. defined in the entry.
  14173. If WHICH is nil or `all', get all properties. If WHICH is
  14174. `special' or `standard', only get that subclass. If WHICH is
  14175. a string, only get that property.
  14176. Return value is an alist. Keys are properties, as upcased
  14177. strings."
  14178. (org-with-point-at pom
  14179. (when (and (derived-mode-p 'org-mode)
  14180. (ignore-errors (org-back-to-heading t)))
  14181. (catch 'exit
  14182. (let* ((beg (point))
  14183. (specific (and (stringp which) (upcase which)))
  14184. (which (cond ((not specific) which)
  14185. ((member specific org-special-properties) 'special)
  14186. (t 'standard)))
  14187. props)
  14188. ;; Get the special properties, like TODO and TAGS.
  14189. (when (memq which '(nil all special))
  14190. (when (or (not specific) (string= specific "CLOCKSUM"))
  14191. (let ((clocksum (get-text-property (point) :org-clock-minutes)))
  14192. (when clocksum
  14193. (push (cons "CLOCKSUM"
  14194. (org-columns-number-to-string
  14195. (/ (float clocksum) 60.) 'add_times))
  14196. props)))
  14197. (when specific (throw 'exit props)))
  14198. (when (or (not specific) (string= specific "CLOCKSUM_T"))
  14199. (let ((clocksumt (get-text-property (point)
  14200. :org-clock-minutes-today)))
  14201. (when clocksumt
  14202. (push (cons "CLOCKSUM_T"
  14203. (org-columns-number-to-string
  14204. (/ (float clocksumt) 60.) 'add_times))
  14205. props)))
  14206. (when specific (throw 'exit props)))
  14207. (when (or (not specific) (string= specific "ITEM"))
  14208. (when (looking-at org-complex-heading-regexp)
  14209. (push (cons "ITEM"
  14210. (concat
  14211. (org-match-string-no-properties 1)
  14212. (let ((title (org-match-string-no-properties 4)))
  14213. (when (org-string-nw-p title)
  14214. (concat " " (org-remove-tabs title))))))
  14215. props))
  14216. (when specific (throw 'exit props)))
  14217. (when (or (not specific) (string= specific "TODO"))
  14218. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  14219. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  14220. (when specific (throw 'exit props)))
  14221. (when (or (not specific) (string= specific "PRIORITY"))
  14222. (when (looking-at org-priority-regexp)
  14223. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  14224. (when specific (throw 'exit props)))
  14225. (when (or (not specific) (string= specific "FILE"))
  14226. (push (cons "FILE" (buffer-file-name (buffer-base-buffer)))
  14227. props)
  14228. (when specific (throw 'exit props)))
  14229. (when (or (not specific) (string= specific "TAGS"))
  14230. (let ((value (org-string-nw-p (org-get-tags-string))))
  14231. (when value (push (cons "TAGS" value) props)))
  14232. (when specific (throw 'exit props)))
  14233. (when (or (not specific) (string= specific "ALLTAGS"))
  14234. (let ((value (org-get-tags-at)))
  14235. (when value
  14236. (push (cons "ALLTAGS"
  14237. (format ":%s:" (mapconcat #'identity value ":")))
  14238. props)))
  14239. (when specific (throw 'exit props)))
  14240. (when (or (not specific) (string= specific "BLOCKED"))
  14241. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props)
  14242. (when specific (throw 'exit props)))
  14243. (when (or (not specific)
  14244. (member specific '("CLOSED" "DEADLINE" "SCHEDULED")))
  14245. (forward-line)
  14246. (when (org-looking-at-p org-planning-line-re)
  14247. (end-of-line)
  14248. (let ((bol (line-beginning-position))
  14249. ;; Backward compatibility: time keywords used to
  14250. ;; be configurable (before 8.3). Make sure we
  14251. ;; get the correct keyword.
  14252. (key-assoc `(("CLOSED" . ,org-closed-string)
  14253. ("DEADLINE" . ,org-deadline-string)
  14254. ("SCHEDULED" . ,org-scheduled-string))))
  14255. (dolist (pair (if specific (list (assoc specific key-assoc))
  14256. key-assoc))
  14257. (save-excursion
  14258. (when (search-backward (cdr pair) bol t)
  14259. (goto-char (match-end 0))
  14260. (skip-chars-forward " \t")
  14261. (and (looking-at org-ts-regexp-both)
  14262. (push (cons (car pair)
  14263. (org-match-string-no-properties 0))
  14264. props)))))))
  14265. (when specific (throw 'exit props)))
  14266. (when (or (not specific)
  14267. (member specific '("TIMESTAMP" "TIMESTAMP_IA")))
  14268. (let ((find-ts
  14269. (lambda (end ts)
  14270. (let ((regexp (if (or (string= specific "TIMESTAMP")
  14271. (assoc "TIMESTAMP_IA" ts))
  14272. org-ts-regexp
  14273. org-ts-regexp-both)))
  14274. (catch 'next
  14275. (while (re-search-forward regexp end t)
  14276. (backward-char)
  14277. (let ((object (org-element-context)))
  14278. ;; Accept to match timestamps in node
  14279. ;; properties, too.
  14280. (when (memq (org-element-type object)
  14281. '(node-property timestamp))
  14282. (let ((type
  14283. (org-element-property :type object)))
  14284. (cond
  14285. ((and (memq type '(active active-range))
  14286. (not (equal specific "TIMESTAMP_IA")))
  14287. (unless (assoc "TIMESTAMP" ts)
  14288. (push (cons "TIMESTAMP"
  14289. (org-element-property
  14290. :raw-value object))
  14291. ts)
  14292. (when specific (throw 'exit ts))))
  14293. ((and (memq type '(inactive inactive-range))
  14294. (not (string= specific "TIMESTAMP")))
  14295. (unless (assoc "TIMESTAMP_IA" ts)
  14296. (push (cons "TIMESTAMP_IA"
  14297. (org-element-property
  14298. :raw-value object))
  14299. ts)
  14300. (when specific (throw 'exit ts))))))
  14301. ;; Both timestamp types are found,
  14302. ;; move to next part.
  14303. (when (= (length ts) 2) (throw 'next ts)))))
  14304. ts)))))
  14305. (goto-char beg)
  14306. ;; First look for timestamps within headline.
  14307. (let ((ts (funcall find-ts (line-end-position) nil)))
  14308. (if (= (length ts) 2) (setq props (nconc ts props))
  14309. (forward-line)
  14310. ;; Then find timestamps in the section, skipping
  14311. ;; planning line.
  14312. (when (org-looking-at-p org-planning-line-re)
  14313. (forward-line))
  14314. (let ((end (save-excursion (outline-next-heading))))
  14315. (setq props (nconc (funcall find-ts end ts) props))))))))
  14316. ;; Get the standard properties, like :PROP:.
  14317. (when (memq which '(nil all standard))
  14318. ;; If we are looking after a specific property, delegate
  14319. ;; to `org-entry-get', which is faster. However, make an
  14320. ;; exception for "CATEGORY", since it can be also set
  14321. ;; through keywords (i.e. #+CATEGORY).
  14322. (if (and specific (not (equal specific "CATEGORY")))
  14323. (let ((value (org-entry-get beg specific nil t)))
  14324. (throw 'exit (and value (list (cons specific value)))))
  14325. (let ((range (org-get-property-block beg)))
  14326. (when range
  14327. (let ((end (cdr range)) seen-base)
  14328. (goto-char (car range))
  14329. ;; Unlike to `org--update-property-plist', we
  14330. ;; handle the case where base values is found
  14331. ;; after its extension. We also forbid standard
  14332. ;; properties to be named as special properties.
  14333. (while (re-search-forward org-property-re end t)
  14334. (let* ((key (upcase (org-match-string-no-properties 2)))
  14335. (extendp (org-string-match-p "\\+\\'" key))
  14336. (key-base (if extendp (substring key 0 -1) key))
  14337. (value (org-match-string-no-properties 3)))
  14338. (cond
  14339. ((member-ignore-case key-base org-special-properties))
  14340. (extendp
  14341. (setq props
  14342. (org--update-property-plist key value props)))
  14343. ((member key seen-base))
  14344. (t (push key seen-base)
  14345. (let ((p (assoc-string key props t)))
  14346. (if p (setcdr p (concat value " " (cdr p)))
  14347. (push (cons key value) props))))))))))))
  14348. (unless (assoc "CATEGORY" props)
  14349. (push (cons "CATEGORY" (org-get-category beg)) props)
  14350. (when (string= specific "CATEGORY") (throw 'exit props)))
  14351. ;; Return value.
  14352. (append (get-text-property beg 'org-summaries) props))))))
  14353. (defun org-entry-get (pom property &optional inherit literal-nil)
  14354. "Get value of PROPERTY for entry or content at point-or-marker POM.
  14355. If INHERIT is non-nil and the entry does not have the property,
  14356. then also check higher levels of the hierarchy. If INHERIT is
  14357. the symbol `selective', use inheritance only if the setting in
  14358. `org-use-property-inheritance' selects PROPERTY for inheritance.
  14359. If the property is present but empty, the return value is the
  14360. empty string. If the property is not present at all, nil is
  14361. returned. In any other case, return the value as a string.
  14362. Search is case-insensitive.
  14363. If LITERAL-NIL is set, return the string value \"nil\" as
  14364. a string, do not interpret it as the list atom nil. This is used
  14365. for inheritance when a \"nil\" value can supersede a non-nil
  14366. value higher up the hierarchy."
  14367. (org-with-point-at pom
  14368. (cond
  14369. ((and inherit
  14370. (or (not (eq inherit 'selective)) (org-property-inherit-p property)))
  14371. (org-entry-get-with-inheritance property literal-nil))
  14372. ((member-ignore-case property org-special-properties)
  14373. ;; We need a special property. Use `org-entry-properties' to
  14374. ;; retrieve it, but specify the wanted property.
  14375. (cdr (assoc-string property (org-entry-properties nil property))))
  14376. (t
  14377. (let ((range (org-get-property-block)))
  14378. (when range
  14379. (let* ((case-fold-search t)
  14380. (end (cdr range))
  14381. (props
  14382. (let ((global
  14383. (or (assoc-string property org-file-properties t)
  14384. (assoc-string property org-global-properties t)
  14385. (assoc-string
  14386. property org-global-properties-fixed t))))
  14387. ;; Make sure to not re-use GLOBAL as
  14388. ;; `org--update-property-plist' would alter it by
  14389. ;; side-effect.
  14390. (and global (list (cons property (cdr global))))))
  14391. (find-value
  14392. (lambda (key)
  14393. (when (re-search-forward (org-re-property key nil t) end t)
  14394. (setq props
  14395. (org--update-property-plist
  14396. key (org-match-string-no-properties 3) props))))))
  14397. (goto-char (car range))
  14398. ;; Find base value.
  14399. (save-excursion (funcall find-value property))
  14400. ;; Find additional values.
  14401. (let ((property+ (concat property "+")))
  14402. (while (funcall find-value property+)))
  14403. ;; Return final value.
  14404. (let ((val (cdr (assoc-string property props t))))
  14405. (if literal-nil val (org-not-nil val))))))))))
  14406. (defun org-property-or-variable-value (var &optional inherit)
  14407. "Check if there is a property fixing the value of VAR.
  14408. If yes, return this value. If not, return the current value of the variable."
  14409. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  14410. (if (and prop (stringp prop) (string-match "\\S-" prop))
  14411. (read prop)
  14412. (symbol-value var))))
  14413. (defun org-entry-delete (pom property)
  14414. "Delete the property PROPERTY from entry at point-or-marker POM."
  14415. (unless (member property org-special-properties)
  14416. (org-with-point-at pom
  14417. (let ((range (org-get-property-block)))
  14418. (when range
  14419. (let ((begin (car range))
  14420. (end (copy-marker (cdr range))))
  14421. (goto-char begin)
  14422. (when (re-search-forward (org-re-property property nil t) end t)
  14423. (delete-region (match-beginning 0) (line-beginning-position 2))
  14424. ;; If drawer is empty, remove it altogether.
  14425. (when (= begin end)
  14426. (delete-region (line-beginning-position 0)
  14427. (line-beginning-position 2)))
  14428. (set-marker end nil))))))))
  14429. ;; Multi-values properties are properties that contain multiple values
  14430. ;; These values are assumed to be single words, separated by whitespace.
  14431. (defun org-entry-add-to-multivalued-property (pom property value)
  14432. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  14433. (let* ((old (org-entry-get pom property))
  14434. (values (and old (org-split-string old "[ \t]"))))
  14435. (setq value (org-entry-protect-space value))
  14436. (unless (member value values)
  14437. (setq values (append values (list value)))
  14438. (org-entry-put pom property
  14439. (mapconcat 'identity values " ")))))
  14440. (defun org-entry-remove-from-multivalued-property (pom property value)
  14441. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  14442. (let* ((old (org-entry-get pom property))
  14443. (values (and old (org-split-string old "[ \t]"))))
  14444. (setq value (org-entry-protect-space value))
  14445. (when (member value values)
  14446. (setq values (delete value values))
  14447. (org-entry-put pom property
  14448. (mapconcat 'identity values " ")))))
  14449. (defun org-entry-member-in-multivalued-property (pom property value)
  14450. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  14451. (let* ((old (org-entry-get pom property))
  14452. (values (and old (org-split-string old "[ \t]"))))
  14453. (setq value (org-entry-protect-space value))
  14454. (member value values)))
  14455. (defun org-entry-get-multivalued-property (pom property)
  14456. "Return a list of values in a multivalued property."
  14457. (let* ((value (org-entry-get pom property))
  14458. (values (and value (org-split-string value "[ \t]"))))
  14459. (mapcar 'org-entry-restore-space values)))
  14460. (defun org-entry-put-multivalued-property (pom property &rest values)
  14461. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  14462. VALUES should be a list of strings. Spaces will be protected."
  14463. (org-entry-put pom property
  14464. (mapconcat 'org-entry-protect-space values " "))
  14465. (let* ((value (org-entry-get pom property))
  14466. (values (and value (org-split-string value "[ \t]"))))
  14467. (mapcar 'org-entry-restore-space values)))
  14468. (defun org-entry-protect-space (s)
  14469. "Protect spaces and newline in string S."
  14470. (while (string-match " " s)
  14471. (setq s (replace-match "%20" t t s)))
  14472. (while (string-match "\n" s)
  14473. (setq s (replace-match "%0A" t t s)))
  14474. s)
  14475. (defun org-entry-restore-space (s)
  14476. "Restore spaces and newline in string S."
  14477. (while (string-match "%20" s)
  14478. (setq s (replace-match " " t t s)))
  14479. (while (string-match "%0A" s)
  14480. (setq s (replace-match "\n" t t s)))
  14481. s)
  14482. (defvar org-entry-property-inherited-from (make-marker)
  14483. "Marker pointing to the entry from where a property was inherited.
  14484. Each call to `org-entry-get-with-inheritance' will set this marker to the
  14485. location of the entry where the inheritance search matched. If there was
  14486. no match, the marker will point nowhere.
  14487. Note that also `org-entry-get' calls this function, if the INHERIT flag
  14488. is set.")
  14489. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  14490. "Get PROPERTY of entry or content at point, search higher levels if needed.
  14491. The search will stop at the first ancestor which has the property defined.
  14492. If the value found is \"nil\", return nil to show that the property
  14493. should be considered as undefined (this is the meaning of nil here).
  14494. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  14495. (move-marker org-entry-property-inherited-from nil)
  14496. (let (value)
  14497. (org-with-wide-buffer
  14498. (catch 'exit
  14499. (while t
  14500. (when (setq value (org-entry-get nil property nil literal-nil))
  14501. (org-back-to-heading t)
  14502. (move-marker org-entry-property-inherited-from (point))
  14503. (throw 'exit nil))
  14504. (or (org-up-heading-safe) (throw 'exit nil)))))
  14505. (unless value
  14506. (setq value
  14507. (cdr (or (assoc-string property org-file-properties t)
  14508. (assoc-string property org-global-properties t)
  14509. (assoc-string property org-global-properties-fixed t)))))
  14510. (if literal-nil value (org-not-nil value))))
  14511. (defvar org-property-changed-functions nil
  14512. "Hook called when the value of a property has changed.
  14513. Each hook function should accept two arguments, the name of the property
  14514. and the new value.")
  14515. (defun org-entry-put (pom property value)
  14516. "Set PROPERTY to VALUE for entry at point-or-marker POM.
  14517. If the value is `nil', it is converted to the empty string. If
  14518. it is not a string, an error is raised.
  14519. PROPERTY can be any regular property (see
  14520. `org-special-properties'). It can also be \"TODO\",
  14521. \"PRIORITY\", \"SCHEDULED\" and \"DEADLINE\".
  14522. For the last two properties, VALUE may have any of the special
  14523. values \"earlier\" and \"later\". The function then increases or
  14524. decreases scheduled or deadline date by one day."
  14525. (cond ((null value) (setq value ""))
  14526. ((not (stringp value)) (error "Properties values should be strings")))
  14527. (org-with-point-at pom
  14528. (if (or (not (featurep 'org-inlinetask)) (org-inlinetask-in-task-p))
  14529. (org-back-to-heading t)
  14530. (org-with-limited-levels (org-back-to-heading t)))
  14531. (let ((beg (point)))
  14532. (cond
  14533. ((equal property "TODO")
  14534. (cond ((not (org-string-nw-p value)) (setq value 'none))
  14535. ((not (member value org-todo-keywords-1))
  14536. (user-error "\"%s\" is not a valid TODO state" value)))
  14537. (org-todo value)
  14538. (org-set-tags nil 'align))
  14539. ((equal property "PRIORITY")
  14540. (org-priority (if (org-string-nw-p value) (string-to-char value) ?\s))
  14541. (org-set-tags nil 'align))
  14542. ((equal property "SCHEDULED")
  14543. (forward-line)
  14544. (if (and (org-looking-at-p org-planning-line-re)
  14545. (re-search-forward
  14546. org-scheduled-time-regexp (line-end-position) t))
  14547. (cond ((string= value "earlier") (org-timestamp-change -1 'day))
  14548. ((string= value "later") (org-timestamp-change 1 'day))
  14549. ((string= value "") (org-schedule '(4)))
  14550. (t (org-schedule nil value)))
  14551. (if (member value '("earlier" "later" ""))
  14552. (call-interactively #'org-schedule)
  14553. (org-schedule nil value))))
  14554. ((equal property "DEADLINE")
  14555. (forward-line)
  14556. (if (and (org-looking-at-p org-planning-line-re)
  14557. (re-search-forward
  14558. org-deadline-time-regexp (line-end-position) t))
  14559. (cond ((string= value "earlier") (org-timestamp-change -1 'day))
  14560. ((string= value "later") (org-timestamp-change 1 'day))
  14561. ((string= value "") (org-deadline '(4)))
  14562. (t (org-deadline nil value)))
  14563. (if (member value '("earlier" "later" ""))
  14564. (call-interactively #'org-deadline)
  14565. (org-deadline nil value))))
  14566. ((member property org-special-properties)
  14567. (error "The %s property cannot be set with `org-entry-put'" property))
  14568. (t
  14569. (let* ((range (org-get-property-block beg 'force))
  14570. (end (cdr range))
  14571. (case-fold-search t))
  14572. (goto-char (car range))
  14573. (if (re-search-forward (org-re-property property nil t) end t)
  14574. (progn (delete-region (match-beginning 0) (match-end 0))
  14575. (goto-char (match-beginning 0)))
  14576. (goto-char end)
  14577. (insert "\n")
  14578. (backward-char))
  14579. (insert ":" property ":")
  14580. (when value (insert " " value))
  14581. (org-indent-line)))))
  14582. (run-hook-with-args 'org-property-changed-functions property value)))
  14583. (defun org-buffer-property-keys (&optional specials defaults columns)
  14584. "Get all property keys in the current buffer.
  14585. When SPECIALS is non-nil, also list the special properties that
  14586. reflect things like tags and TODO state.
  14587. When DEFAULTS is non-nil, also include properties that has
  14588. special meaning internally: ARCHIVE, CATEGORY, SUMMARY,
  14589. DESCRIPTION, LOCATION, and LOGGING and others.
  14590. When COLUMNS in non-nil, also include property names given in
  14591. COLUMN formats in the current buffer."
  14592. (let ((case-fold-search t)
  14593. (props (append
  14594. (and specials org-special-properties)
  14595. (and defaults (cons org-effort-property org-default-properties))
  14596. nil)))
  14597. (org-with-wide-buffer
  14598. (goto-char (point-min))
  14599. (while (re-search-forward org-property-start-re nil t)
  14600. (let ((range (org-get-property-block)))
  14601. (catch 'skip
  14602. (unless range
  14603. (when (and (not (org-before-first-heading-p))
  14604. (y-or-n-p (format "Malformed drawer at %d, repair?"
  14605. (line-beginning-position))))
  14606. (org-get-property-block nil t))
  14607. (throw 'skip nil))
  14608. (goto-char (car range))
  14609. (let ((begin (car range))
  14610. (end (cdr range)))
  14611. ;; Make sure that found property block is not located
  14612. ;; before current point, as it would generate an infloop.
  14613. ;; It can happen, for example, in the following
  14614. ;; situation:
  14615. ;;
  14616. ;; * Headline
  14617. ;; :PROPERTIES:
  14618. ;; ...
  14619. ;; :END:
  14620. ;; *************** Inlinetask
  14621. ;; #+BEGIN_EXAMPLE
  14622. ;; :PROPERTIES:
  14623. ;; #+END_EXAMPLE
  14624. ;;
  14625. (if (< begin (point)) (throw 'skip nil) (goto-char begin))
  14626. (while (< (point) end)
  14627. (let ((p (progn (looking-at org-property-re)
  14628. (org-match-string-no-properties 2))))
  14629. ;; Only add true property name, not extension symbol.
  14630. (add-to-list 'props
  14631. (if (not (org-string-match-p "\\+\\'" p)) p
  14632. (substring p 0 -1))))
  14633. (forward-line))))
  14634. (outline-next-heading)))
  14635. (when columns
  14636. (goto-char (point-min))
  14637. (while (re-search-forward "^[ \t]*\\(?:#\\+\\|:\\)COLUMNS:" nil t)
  14638. (let ((element (org-element-at-point)))
  14639. (when (memq (org-element-type element) '(keyword node-property))
  14640. (let ((value (org-element-property :value element))
  14641. (start 0))
  14642. (while (string-match "%[0-9]*\\(\\S-+\\)" value start)
  14643. (setq start (match-end 0))
  14644. (let ((p (org-match-string-no-properties 1 value)))
  14645. (unless (member-ignore-case p org-special-properties)
  14646. (add-to-list 'props p))))))))))
  14647. (sort props (lambda (a b) (string< (upcase a) (upcase b))))))
  14648. (defun org-property-values (key)
  14649. "List all non-nil values of property KEY in current buffer."
  14650. (org-with-wide-buffer
  14651. (goto-char (point-min))
  14652. (let ((case-fold-search t)
  14653. (re (org-re-property key))
  14654. values)
  14655. (while (re-search-forward re nil t)
  14656. (add-to-list 'values (org-entry-get (point) key)))
  14657. values)))
  14658. (defun org-insert-property-drawer ()
  14659. "Insert a property drawer into the current entry."
  14660. (org-with-wide-buffer
  14661. (if (or (not (featurep 'org-inlinetask)) (org-inlinetask-in-task-p))
  14662. (org-back-to-heading t)
  14663. (org-with-limited-levels (org-back-to-heading t)))
  14664. (forward-line)
  14665. (when (org-looking-at-p org-planning-line-re) (forward-line))
  14666. (unless (org-looking-at-p org-property-drawer-re)
  14667. (let ((inhibit-read-only t))
  14668. (unless (bolp) (insert "\n"))
  14669. (let ((begin (point)))
  14670. (insert ":PROPERTIES:\n:END:\n")
  14671. (org-indent-region begin (point)))))))
  14672. (defun org-insert-drawer (&optional arg drawer)
  14673. "Insert a drawer at point.
  14674. When optional argument ARG is non-nil, insert a property drawer.
  14675. Optional argument DRAWER, when non-nil, is a string representing
  14676. drawer's name. Otherwise, the user is prompted for a name.
  14677. If a region is active, insert the drawer around that region
  14678. instead.
  14679. Point is left between drawer's boundaries."
  14680. (interactive "P")
  14681. (let* ((drawer (if arg "PROPERTIES"
  14682. (or drawer (read-from-minibuffer "Drawer: ")))))
  14683. (cond
  14684. ;; With C-u, fall back on `org-insert-property-drawer'
  14685. (arg (org-insert-property-drawer))
  14686. ;; Check validity of suggested drawer's name.
  14687. ((not (org-string-match-p org-drawer-regexp (format ":%s:" drawer)))
  14688. (user-error "Invalid drawer name"))
  14689. ;; With an active region, insert a drawer at point.
  14690. ((not (org-region-active-p))
  14691. (progn
  14692. (unless (bolp) (insert "\n"))
  14693. (insert (format ":%s:\n\n:END:\n" drawer))
  14694. (forward-line -2)))
  14695. ;; Otherwise, insert the drawer at point
  14696. (t
  14697. (let ((rbeg (region-beginning))
  14698. (rend (copy-marker (region-end))))
  14699. (unwind-protect
  14700. (progn
  14701. (goto-char rbeg)
  14702. (beginning-of-line)
  14703. (when (save-excursion
  14704. (re-search-forward org-outline-regexp-bol rend t))
  14705. (user-error "Drawers cannot contain headlines"))
  14706. ;; Position point at the beginning of the first
  14707. ;; non-blank line in region. Insert drawer's opening
  14708. ;; there, then indent it.
  14709. (org-skip-whitespace)
  14710. (beginning-of-line)
  14711. (insert ":" drawer ":\n")
  14712. (forward-line -1)
  14713. (indent-for-tab-command)
  14714. ;; Move point to the beginning of the first blank line
  14715. ;; after the last non-blank line in region. Insert
  14716. ;; drawer's closing, then indent it.
  14717. (goto-char rend)
  14718. (skip-chars-backward " \r\t\n")
  14719. (insert "\n:END:")
  14720. (deactivate-mark t)
  14721. (indent-for-tab-command)
  14722. (unless (eolp) (insert "\n")))
  14723. ;; Clear marker, whatever the outcome of insertion is.
  14724. (set-marker rend nil)))))))
  14725. (defvar org-property-set-functions-alist nil
  14726. "Property set function alist.
  14727. Each entry should have the following format:
  14728. (PROPERTY . READ-FUNCTION)
  14729. The read function will be called with the same argument as
  14730. `org-completing-read'.")
  14731. (defun org-set-property-function (property)
  14732. "Get the function that should be used to set PROPERTY.
  14733. This is computed according to `org-property-set-functions-alist'."
  14734. (or (cdr (assoc property org-property-set-functions-alist))
  14735. 'org-completing-read))
  14736. (defun org-read-property-value (property)
  14737. "Read PROPERTY value from user."
  14738. (let* ((completion-ignore-case t)
  14739. (allowed (org-property-get-allowed-values nil property 'table))
  14740. (cur (org-entry-get nil property))
  14741. (prompt (concat property " value"
  14742. (if (and cur (string-match "\\S-" cur))
  14743. (concat " [" cur "]") "") ": "))
  14744. (set-function (org-set-property-function property))
  14745. (val (if allowed
  14746. (funcall set-function prompt allowed nil
  14747. (not (get-text-property 0 'org-unrestricted
  14748. (caar allowed))))
  14749. (let (org-completion-use-ido org-completion-use-iswitchb)
  14750. (funcall set-function prompt
  14751. (mapcar 'list (org-property-values property))
  14752. nil nil "" nil cur)))))
  14753. (org-trim val)))
  14754. (defvar org-last-set-property nil)
  14755. (defvar org-last-set-property-value nil)
  14756. (defun org-read-property-name ()
  14757. "Read a property name."
  14758. (let* ((completion-ignore-case t)
  14759. (keys (org-buffer-property-keys nil t t))
  14760. (default-prop (or (save-excursion
  14761. (save-match-data
  14762. (beginning-of-line)
  14763. (and (looking-at "^\\s-*:\\([^:\n]+\\):")
  14764. (null (string= (match-string 1) "END"))
  14765. (match-string 1))))
  14766. org-last-set-property))
  14767. (property (org-icompleting-read
  14768. (concat "Property"
  14769. (if default-prop (concat " [" default-prop "]") "")
  14770. ": ")
  14771. (mapcar 'list keys)
  14772. nil nil nil nil
  14773. default-prop)))
  14774. (if (member property keys)
  14775. property
  14776. (or (cdr (assoc (downcase property)
  14777. (mapcar (lambda (x) (cons (downcase x) x))
  14778. keys)))
  14779. property))))
  14780. (defun org-set-property-and-value (use-last)
  14781. "Allow to set [PROPERTY]: [value] direction from prompt.
  14782. When use-default, don't even ask, just use the last
  14783. \"[PROPERTY]: [value]\" string from the history."
  14784. (interactive "P")
  14785. (let* ((completion-ignore-case t)
  14786. (pv (or (and use-last org-last-set-property-value)
  14787. (org-completing-read
  14788. "Enter a \"[Property]: [value]\" pair: "
  14789. nil nil nil nil nil
  14790. org-last-set-property-value)))
  14791. prop val)
  14792. (when (string-match "^[ \t]*\\([^:]+\\):[ \t]*\\(.*\\)[ \t]*$" pv)
  14793. (setq prop (match-string 1 pv)
  14794. val (match-string 2 pv))
  14795. (org-set-property prop val))))
  14796. (defun org-set-property (property value)
  14797. "In the current entry, set PROPERTY to VALUE.
  14798. When called interactively, this will prompt for a property name, offering
  14799. completion on existing and default properties. And then it will prompt
  14800. for a value, offering completion either on allowed values (via an inherited
  14801. xxx_ALL property) or on existing values in other instances of this property
  14802. in the current file."
  14803. (interactive (list nil nil))
  14804. (let* ((property (or property (org-read-property-name)))
  14805. (value (or value (org-read-property-value property)))
  14806. (fn (cdr (assoc property org-properties-postprocess-alist))))
  14807. (setq org-last-set-property property)
  14808. (setq org-last-set-property-value (concat property ": " value))
  14809. ;; Possibly postprocess the inserted value:
  14810. (when fn (setq value (funcall fn value)))
  14811. (unless (equal (org-entry-get nil property) value)
  14812. (org-entry-put nil property value))))
  14813. (defun org-delete-property (property)
  14814. "In the current entry, delete PROPERTY."
  14815. (interactive
  14816. (let* ((completion-ignore-case t)
  14817. (cat (org-entry-get (point) "CATEGORY"))
  14818. (props0 (org-entry-properties nil 'standard))
  14819. (props (if cat props0
  14820. (delete `("CATEGORY" . ,(org-get-category)) props0)))
  14821. (prop (if (< 1 (length props))
  14822. (org-icompleting-read "Property: " props nil t)
  14823. (caar props))))
  14824. (list prop)))
  14825. (if (not property)
  14826. (message "No property to delete in this entry")
  14827. (org-entry-delete nil property)
  14828. (message "Property \"%s\" deleted" property)))
  14829. (defun org-delete-property-globally (property)
  14830. "Remove PROPERTY globally, from all entries."
  14831. (interactive
  14832. (let* ((completion-ignore-case t)
  14833. (prop (org-icompleting-read
  14834. "Globally remove property: "
  14835. (mapcar 'list (org-buffer-property-keys)))))
  14836. (list prop)))
  14837. (save-excursion
  14838. (save-restriction
  14839. (widen)
  14840. (goto-char (point-min))
  14841. (let ((cnt 0))
  14842. (while (re-search-forward
  14843. (org-re-property property)
  14844. nil t)
  14845. (setq cnt (1+ cnt))
  14846. (delete-region (match-beginning 0) (1+ (point-at-eol))))
  14847. (message "Property \"%s\" removed from %d entries" property cnt)))))
  14848. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  14849. (defun org-compute-property-at-point ()
  14850. "Compute the property at point.
  14851. This looks for an enclosing column format, extracts the operator and
  14852. then applies it to the property in the column format's scope."
  14853. (interactive)
  14854. (unless (org-at-property-p)
  14855. (user-error "Not at a property"))
  14856. (let ((prop (org-match-string-no-properties 2)))
  14857. (org-columns-get-format-and-top-level)
  14858. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  14859. (user-error "No operator defined for property %s" prop))
  14860. (org-columns-compute prop)))
  14861. (defvar org-property-allowed-value-functions nil
  14862. "Hook for functions supplying allowed values for a specific property.
  14863. The functions must take a single argument, the name of the property, and
  14864. return a flat list of allowed values. If \":ETC\" is one of
  14865. the values, this means that these values are intended as defaults for
  14866. completion, but that other values should be allowed too.
  14867. The functions must return nil if they are not responsible for this
  14868. property.")
  14869. (defun org-property-get-allowed-values (pom property &optional table)
  14870. "Get allowed values for the property PROPERTY.
  14871. When TABLE is non-nil, return an alist that can directly be used for
  14872. completion."
  14873. (let (vals)
  14874. (cond
  14875. ((equal property "TODO")
  14876. (setq vals (org-with-point-at pom
  14877. (append org-todo-keywords-1 '("")))))
  14878. ((equal property "PRIORITY")
  14879. (let ((n org-lowest-priority))
  14880. (while (>= n org-highest-priority)
  14881. (push (char-to-string n) vals)
  14882. (setq n (1- n)))))
  14883. ((member property org-special-properties))
  14884. ((setq vals (run-hook-with-args-until-success
  14885. 'org-property-allowed-value-functions property)))
  14886. (t
  14887. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  14888. (when (and vals (string-match "\\S-" vals))
  14889. (setq vals (car (read-from-string (concat "(" vals ")"))))
  14890. (setq vals (mapcar (lambda (x)
  14891. (cond ((stringp x) x)
  14892. ((numberp x) (number-to-string x))
  14893. ((symbolp x) (symbol-name x))
  14894. (t "???")))
  14895. vals)))))
  14896. (when (member ":ETC" vals)
  14897. (setq vals (remove ":ETC" vals))
  14898. (org-add-props (car vals) '(org-unrestricted t)))
  14899. (if table (mapcar 'list vals) vals)))
  14900. (defun org-property-previous-allowed-value (&optional previous)
  14901. "Switch to the next allowed value for this property."
  14902. (interactive)
  14903. (org-property-next-allowed-value t))
  14904. (defun org-property-next-allowed-value (&optional previous)
  14905. "Switch to the next allowed value for this property."
  14906. (interactive)
  14907. (unless (org-at-property-p)
  14908. (user-error "Not at a property"))
  14909. (let* ((prop (car (save-match-data (org-split-string (match-string 1) ":"))))
  14910. (key (match-string 2))
  14911. (value (match-string 3))
  14912. (allowed (or (org-property-get-allowed-values (point) key)
  14913. (and (member value '("[ ]" "[-]" "[X]"))
  14914. '("[ ]" "[X]"))))
  14915. (heading (save-match-data (nth 4 (org-heading-components))))
  14916. nval)
  14917. (unless allowed
  14918. (user-error "Allowed values for this property have not been defined"))
  14919. (if previous (setq allowed (reverse allowed)))
  14920. (if (member value allowed)
  14921. (setq nval (car (cdr (member value allowed)))))
  14922. (setq nval (or nval (car allowed)))
  14923. (if (equal nval value)
  14924. (user-error "Only one allowed value for this property"))
  14925. (org-at-property-p)
  14926. (replace-match (concat " :" key ": " nval) t t)
  14927. (org-indent-line)
  14928. (beginning-of-line 1)
  14929. (skip-chars-forward " \t")
  14930. (when (equal prop org-effort-property)
  14931. (org-refresh-property
  14932. '((effort . identity)
  14933. (effort-minutes . org-duration-string-to-minutes))
  14934. nval)
  14935. (when (string= org-clock-current-task heading)
  14936. (setq org-clock-effort nval)
  14937. (org-clock-update-mode-line)))
  14938. (run-hook-with-args 'org-property-changed-functions key nval)))
  14939. (defun org-find-olp (path &optional this-buffer)
  14940. "Return a marker pointing to the entry at outline path OLP.
  14941. If anything goes wrong, throw an error.
  14942. You can wrap this call to catch the error like this:
  14943. (condition-case msg
  14944. (org-mobile-locate-entry (match-string 4))
  14945. (error (nth 1 msg)))
  14946. The return value will then be either a string with the error message,
  14947. or a marker if everything is OK.
  14948. If THIS-BUFFER is set, the outline path does not contain a file,
  14949. only headings."
  14950. (let* ((file (if this-buffer buffer-file-name (pop path)))
  14951. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  14952. (level 1)
  14953. (lmin 1)
  14954. (lmax 1)
  14955. limit re end found pos heading cnt flevel)
  14956. (unless buffer (error "File not found :%s" file))
  14957. (with-current-buffer buffer
  14958. (save-excursion
  14959. (save-restriction
  14960. (widen)
  14961. (setq limit (point-max))
  14962. (goto-char (point-min))
  14963. (while (setq heading (pop path))
  14964. (setq re (format org-complex-heading-regexp-format
  14965. (regexp-quote heading)))
  14966. (setq cnt 0 pos (point))
  14967. (while (re-search-forward re end t)
  14968. (setq level (- (match-end 1) (match-beginning 1)))
  14969. (if (and (>= level lmin) (<= level lmax))
  14970. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  14971. (when (= cnt 0) (error "Heading not found on level %d: %s"
  14972. lmax heading))
  14973. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  14974. lmax heading))
  14975. (goto-char found)
  14976. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  14977. (setq end (save-excursion (org-end-of-subtree t t))))
  14978. (when (org-at-heading-p)
  14979. (point-marker)))))))
  14980. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  14981. "Find node HEADING in BUFFER.
  14982. Return a marker to the heading if it was found, or nil if not.
  14983. If POS-ONLY is set, return just the position instead of a marker.
  14984. The heading text must match exact, but it may have a TODO keyword,
  14985. a priority cookie and tags in the standard locations."
  14986. (with-current-buffer (or buffer (current-buffer))
  14987. (save-excursion
  14988. (save-restriction
  14989. (widen)
  14990. (goto-char (point-min))
  14991. (let (case-fold-search)
  14992. (if (re-search-forward
  14993. (format org-complex-heading-regexp-format
  14994. (regexp-quote heading)) nil t)
  14995. (if pos-only
  14996. (match-beginning 0)
  14997. (move-marker (make-marker) (match-beginning 0)))))))))
  14998. (defun org-find-exact-heading-in-directory (heading &optional dir)
  14999. "Find Org node headline HEADING in all .org files in directory DIR.
  15000. When the target headline is found, return a marker to this location."
  15001. (let ((files (directory-files (or dir default-directory)
  15002. t "\\`[^.#].*\\.org\\'"))
  15003. file visiting m buffer)
  15004. (catch 'found
  15005. (while (setq file (pop files))
  15006. (message "trying %s" file)
  15007. (setq visiting (org-find-base-buffer-visiting file))
  15008. (setq buffer (or visiting (find-file-noselect file)))
  15009. (setq m (org-find-exact-headline-in-buffer
  15010. heading buffer))
  15011. (when (and (not m) (not visiting)) (kill-buffer buffer))
  15012. (and m (throw 'found m))))))
  15013. (defun org-find-entry-with-id (ident)
  15014. "Locate the entry that contains the ID property with exact value IDENT.
  15015. IDENT can be a string, a symbol or a number, this function will search for
  15016. the string representation of it.
  15017. Return the position where this entry starts, or nil if there is no such entry."
  15018. (interactive "sID: ")
  15019. (let ((id (cond
  15020. ((stringp ident) ident)
  15021. ((symbol-name ident) (symbol-name ident))
  15022. ((numberp ident) (number-to-string ident))
  15023. (t (error "IDENT %s must be a string, symbol or number" ident))))
  15024. (case-fold-search nil))
  15025. (save-excursion
  15026. (save-restriction
  15027. (widen)
  15028. (goto-char (point-min))
  15029. (when (re-search-forward
  15030. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  15031. nil t)
  15032. (org-back-to-heading t)
  15033. (point))))))
  15034. ;;;; Timestamps
  15035. (defvar org-last-changed-timestamp nil)
  15036. (defvar org-last-inserted-timestamp nil
  15037. "The last time stamp inserted with `org-insert-time-stamp'.")
  15038. (defvar org-ts-what) ; dynamically scoped parameter
  15039. (defun org-time-stamp (arg &optional inactive)
  15040. "Prompt for a date/time and insert a time stamp.
  15041. If the user specifies a time like HH:MM or if this command is
  15042. called with at least one prefix argument, the time stamp contains
  15043. the date and the time. Otherwise, only the date is be included.
  15044. All parts of a date not specified by the user is filled in from
  15045. the current date/time. So if you just press return without
  15046. typing anything, the time stamp will represent the current
  15047. date/time.
  15048. If there is already a timestamp at the cursor, it will be
  15049. modified.
  15050. With two universal prefix arguments, insert an active timestamp
  15051. with the current time without prompting the user.
  15052. When called from lisp, the timestamp is inactive if INACTIVE is
  15053. non-nil."
  15054. (interactive "P")
  15055. (let* ((ts nil)
  15056. (default-time
  15057. ;; Default time is either today, or, when entering a range,
  15058. ;; the range start.
  15059. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  15060. (save-excursion
  15061. (re-search-backward
  15062. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  15063. (- (point) 20) t)))
  15064. (apply 'encode-time (org-parse-time-string (match-string 1)))
  15065. (current-time)))
  15066. (default-input (and ts (org-get-compact-tod ts)))
  15067. (repeater (save-excursion
  15068. (save-match-data
  15069. (beginning-of-line)
  15070. (when (re-search-forward
  15071. "\\([.+-]+[0-9]+[hdwmy] ?\\)+" ;;\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  15072. (save-excursion (progn (end-of-line) (point))) t)
  15073. (match-string 0)))))
  15074. org-time-was-given org-end-time-was-given time)
  15075. (cond
  15076. ((and (org-at-timestamp-p t)
  15077. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  15078. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  15079. (insert "--")
  15080. (setq time (let ((this-command this-command))
  15081. (org-read-date arg 'totime nil nil
  15082. default-time default-input inactive)))
  15083. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  15084. ((org-at-timestamp-p t)
  15085. (setq time (let ((this-command this-command))
  15086. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  15087. (when (org-at-timestamp-p t) ; just to get the match data
  15088. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  15089. (replace-match "")
  15090. (setq org-last-changed-timestamp
  15091. (org-insert-time-stamp
  15092. time (or org-time-was-given arg)
  15093. inactive nil nil (list org-end-time-was-given)))
  15094. (when repeater (goto-char (1- (point))) (insert " " repeater)
  15095. (setq org-last-changed-timestamp
  15096. (concat (substring org-last-inserted-timestamp 0 -1)
  15097. " " repeater ">"))))
  15098. (message "Timestamp updated"))
  15099. ((equal arg '(16))
  15100. (org-insert-time-stamp (current-time) t inactive))
  15101. (t
  15102. (setq time (let ((this-command this-command))
  15103. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  15104. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  15105. nil nil (list org-end-time-was-given))))))
  15106. ;; FIXME: can we use this for something else, like computing time differences?
  15107. (defun org-get-compact-tod (s)
  15108. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  15109. (let* ((t1 (match-string 1 s))
  15110. (h1 (string-to-number (match-string 2 s)))
  15111. (m1 (string-to-number (match-string 3 s)))
  15112. (t2 (and (match-end 4) (match-string 5 s)))
  15113. (h2 (and t2 (string-to-number (match-string 6 s))))
  15114. (m2 (and t2 (string-to-number (match-string 7 s))))
  15115. dh dm)
  15116. (if (not t2)
  15117. t1
  15118. (setq dh (- h2 h1) dm (- m2 m1))
  15119. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  15120. (concat t1 "+" (number-to-string dh)
  15121. (and (/= 0 dm) (format ":%02d" dm)))))))
  15122. (defun org-time-stamp-inactive (&optional arg)
  15123. "Insert an inactive time stamp.
  15124. An inactive time stamp is enclosed in square brackets instead of angle
  15125. brackets. It is inactive in the sense that it does not trigger agenda entries,
  15126. does not link to the calendar and cannot be changed with the S-cursor keys.
  15127. So these are more for recording a certain time/date."
  15128. (interactive "P")
  15129. (org-time-stamp arg 'inactive))
  15130. (defvar org-date-ovl (make-overlay 1 1))
  15131. (overlay-put org-date-ovl 'face 'org-date-selected)
  15132. (org-detach-overlay org-date-ovl)
  15133. (defvar org-ans1) ; dynamically scoped parameter
  15134. (defvar org-ans2) ; dynamically scoped parameter
  15135. (defvar org-plain-time-of-day-regexp) ; defined below
  15136. (defvar org-overriding-default-time nil) ; dynamically scoped
  15137. (defvar org-read-date-overlay nil)
  15138. (defvar org-dcst nil) ; dynamically scoped
  15139. (defvar org-read-date-history nil)
  15140. (defvar org-read-date-final-answer nil)
  15141. (defvar org-read-date-analyze-futurep nil)
  15142. (defvar org-read-date-analyze-forced-year nil)
  15143. (defvar org-read-date-inactive)
  15144. (defvar org-read-date-minibuffer-local-map
  15145. (let* ((map (make-sparse-keymap)))
  15146. (set-keymap-parent map minibuffer-local-map)
  15147. (org-defkey map (kbd ".")
  15148. (lambda () (interactive)
  15149. ;; Are we at the beginning of the prompt?
  15150. (if (looking-back "^[^:]+: ")
  15151. (org-eval-in-calendar '(calendar-goto-today))
  15152. (insert "."))))
  15153. (org-defkey map (kbd "C-.")
  15154. (lambda () (interactive)
  15155. (org-eval-in-calendar '(calendar-goto-today))))
  15156. (org-defkey map [(meta shift left)]
  15157. (lambda () (interactive)
  15158. (org-eval-in-calendar '(calendar-backward-month 1))))
  15159. (org-defkey map [(meta shift right)]
  15160. (lambda () (interactive)
  15161. (org-eval-in-calendar '(calendar-forward-month 1))))
  15162. (org-defkey map [(meta shift up)]
  15163. (lambda () (interactive)
  15164. (org-eval-in-calendar '(calendar-backward-year 1))))
  15165. (org-defkey map [(meta shift down)]
  15166. (lambda () (interactive)
  15167. (org-eval-in-calendar '(calendar-forward-year 1))))
  15168. (org-defkey map [?\e (shift left)]
  15169. (lambda () (interactive)
  15170. (org-eval-in-calendar '(calendar-backward-month 1))))
  15171. (org-defkey map [?\e (shift right)]
  15172. (lambda () (interactive)
  15173. (org-eval-in-calendar '(calendar-forward-month 1))))
  15174. (org-defkey map [?\e (shift up)]
  15175. (lambda () (interactive)
  15176. (org-eval-in-calendar '(calendar-backward-year 1))))
  15177. (org-defkey map [?\e (shift down)]
  15178. (lambda () (interactive)
  15179. (org-eval-in-calendar '(calendar-forward-year 1))))
  15180. (org-defkey map [(shift up)]
  15181. (lambda () (interactive)
  15182. (org-eval-in-calendar '(calendar-backward-week 1))))
  15183. (org-defkey map [(shift down)]
  15184. (lambda () (interactive)
  15185. (org-eval-in-calendar '(calendar-forward-week 1))))
  15186. (org-defkey map [(shift left)]
  15187. (lambda () (interactive)
  15188. (org-eval-in-calendar '(calendar-backward-day 1))))
  15189. (org-defkey map [(shift right)]
  15190. (lambda () (interactive)
  15191. (org-eval-in-calendar '(calendar-forward-day 1))))
  15192. (org-defkey map "!"
  15193. (lambda () (interactive)
  15194. (org-eval-in-calendar '(diary-view-entries))
  15195. (message "")))
  15196. (org-defkey map ">"
  15197. (lambda () (interactive)
  15198. (org-eval-in-calendar '(calendar-scroll-left 1))))
  15199. (org-defkey map "<"
  15200. (lambda () (interactive)
  15201. (org-eval-in-calendar '(calendar-scroll-right 1))))
  15202. (org-defkey map "\C-v"
  15203. (lambda () (interactive)
  15204. (org-eval-in-calendar
  15205. '(calendar-scroll-left-three-months 1))))
  15206. (org-defkey map "\M-v"
  15207. (lambda () (interactive)
  15208. (org-eval-in-calendar
  15209. '(calendar-scroll-right-three-months 1))))
  15210. map)
  15211. "Keymap for minibuffer commands when using `org-read-date'.")
  15212. (defvar org-def)
  15213. (defvar org-defdecode)
  15214. (defvar org-with-time)
  15215. (defun org-read-date (&optional org-with-time to-time from-string prompt
  15216. default-time default-input inactive)
  15217. "Read a date, possibly a time, and make things smooth for the user.
  15218. The prompt will suggest to enter an ISO date, but you can also enter anything
  15219. which will at least partially be understood by `parse-time-string'.
  15220. Unrecognized parts of the date will default to the current day, month, year,
  15221. hour and minute. If this command is called to replace a timestamp at point,
  15222. or to enter the second timestamp of a range, the default time is taken
  15223. from the existing stamp. Furthermore, the command prefers the future,
  15224. so if you are giving a date where the year is not given, and the day-month
  15225. combination is already past in the current year, it will assume you
  15226. mean next year. For details, see the manual. A few examples:
  15227. 3-2-5 --> 2003-02-05
  15228. feb 15 --> currentyear-02-15
  15229. 2/15 --> currentyear-02-15
  15230. sep 12 9 --> 2009-09-12
  15231. 12:45 --> today 12:45
  15232. 22 sept 0:34 --> currentyear-09-22 0:34
  15233. 12 --> currentyear-currentmonth-12
  15234. Fri --> nearest Friday after today
  15235. -Tue --> last Tuesday
  15236. etc.
  15237. Furthermore you can specify a relative date by giving, as the *first* thing
  15238. in the input: a plus/minus sign, a number and a letter [hdwmy] to indicate
  15239. change in days weeks, months, years.
  15240. With a single plus or minus, the date is relative to today. With a double
  15241. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  15242. +4d --> four days from today
  15243. +4 --> same as above
  15244. +2w --> two weeks from today
  15245. ++5 --> five days from default date
  15246. The function understands only English month and weekday abbreviations.
  15247. While prompting, a calendar is popped up - you can also select the
  15248. date with the mouse (button 1). The calendar shows a period of three
  15249. months. To scroll it to other months, use the keys `>' and `<'.
  15250. If you don't like the calendar, turn it off with
  15251. \(setq org-read-date-popup-calendar nil)
  15252. With optional argument TO-TIME, the date will immediately be converted
  15253. to an internal time.
  15254. With an optional argument ORG-WITH-TIME, the prompt will suggest to
  15255. also insert a time. Note that when ORG-WITH-TIME is not set, you can
  15256. still enter a time, and this function will inform the calling routine
  15257. about this change. The calling routine may then choose to change the
  15258. format used to insert the time stamp into the buffer to include the time.
  15259. With optional argument FROM-STRING, read from this string instead from
  15260. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  15261. the time/date that is used for everything that is not specified by the
  15262. user."
  15263. (require 'parse-time)
  15264. (let* ((org-time-stamp-rounding-minutes
  15265. (if (equal org-with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  15266. (org-dcst org-display-custom-times)
  15267. (ct (org-current-time))
  15268. (org-def (or org-overriding-default-time default-time ct))
  15269. (org-defdecode (decode-time org-def))
  15270. (dummy (progn
  15271. (when (< (nth 2 org-defdecode) org-extend-today-until)
  15272. (setcar (nthcdr 2 org-defdecode) -1)
  15273. (setcar (nthcdr 1 org-defdecode) 59)
  15274. (setq org-def (apply 'encode-time org-defdecode)
  15275. org-defdecode (decode-time org-def)))))
  15276. (mouse-autoselect-window nil) ; Don't let the mouse jump
  15277. (calendar-frame-setup nil)
  15278. (calendar-setup nil)
  15279. (calendar-move-hook nil)
  15280. (calendar-view-diary-initially-flag nil)
  15281. (calendar-view-holidays-initially-flag nil)
  15282. (timestr (format-time-string
  15283. (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") org-def))
  15284. (prompt (concat (if prompt (concat prompt " ") "")
  15285. (format "Date+time [%s]: " timestr)))
  15286. ans (org-ans0 "") org-ans1 org-ans2 final)
  15287. (cond
  15288. (from-string (setq ans from-string))
  15289. (org-read-date-popup-calendar
  15290. (save-excursion
  15291. (save-window-excursion
  15292. (calendar)
  15293. (org-eval-in-calendar '(setq cursor-type nil) t)
  15294. (unwind-protect
  15295. (progn
  15296. (calendar-forward-day (- (time-to-days org-def)
  15297. (calendar-absolute-from-gregorian
  15298. (calendar-current-date))))
  15299. (org-eval-in-calendar nil t)
  15300. (let* ((old-map (current-local-map))
  15301. (map (copy-keymap calendar-mode-map))
  15302. (minibuffer-local-map
  15303. (copy-keymap org-read-date-minibuffer-local-map)))
  15304. (org-defkey map (kbd "RET") 'org-calendar-select)
  15305. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  15306. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  15307. (unwind-protect
  15308. (progn
  15309. (use-local-map map)
  15310. (setq org-read-date-inactive inactive)
  15311. (add-hook 'post-command-hook 'org-read-date-display)
  15312. (setq org-ans0 (read-string prompt default-input
  15313. 'org-read-date-history nil))
  15314. ;; org-ans0: from prompt
  15315. ;; org-ans1: from mouse click
  15316. ;; org-ans2: from calendar motion
  15317. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  15318. (remove-hook 'post-command-hook 'org-read-date-display)
  15319. (use-local-map old-map)
  15320. (when org-read-date-overlay
  15321. (delete-overlay org-read-date-overlay)
  15322. (setq org-read-date-overlay nil)))))
  15323. (bury-buffer "*Calendar*")))))
  15324. (t ; Naked prompt only
  15325. (unwind-protect
  15326. (setq ans (read-string prompt default-input
  15327. 'org-read-date-history timestr))
  15328. (when org-read-date-overlay
  15329. (delete-overlay org-read-date-overlay)
  15330. (setq org-read-date-overlay nil)))))
  15331. (setq final (org-read-date-analyze ans org-def org-defdecode))
  15332. (when org-read-date-analyze-forced-year
  15333. (message "Year was forced into %s"
  15334. (if org-read-date-force-compatible-dates
  15335. "compatible range (1970-2037)"
  15336. "range representable on this machine"))
  15337. (ding))
  15338. ;; One round trip to get rid of 34th of August and stuff like that....
  15339. (setq final (decode-time (apply 'encode-time final)))
  15340. (setq org-read-date-final-answer ans)
  15341. (if to-time
  15342. (apply 'encode-time final)
  15343. (if (and (boundp 'org-time-was-given) org-time-was-given)
  15344. (format "%04d-%02d-%02d %02d:%02d"
  15345. (nth 5 final) (nth 4 final) (nth 3 final)
  15346. (nth 2 final) (nth 1 final))
  15347. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  15348. (defun org-read-date-display ()
  15349. "Display the current date prompt interpretation in the minibuffer."
  15350. (when org-read-date-display-live
  15351. (when org-read-date-overlay
  15352. (delete-overlay org-read-date-overlay))
  15353. (when (minibufferp (current-buffer))
  15354. (save-excursion
  15355. (end-of-line 1)
  15356. (while (not (equal (buffer-substring
  15357. (max (point-min) (- (point) 4)) (point))
  15358. " "))
  15359. (insert " ")))
  15360. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  15361. " " (or org-ans1 org-ans2)))
  15362. (org-end-time-was-given nil)
  15363. (f (org-read-date-analyze ans org-def org-defdecode))
  15364. (fmts (if org-dcst
  15365. org-time-stamp-custom-formats
  15366. org-time-stamp-formats))
  15367. (fmt (if (or org-with-time
  15368. (and (boundp 'org-time-was-given) org-time-was-given))
  15369. (cdr fmts)
  15370. (car fmts)))
  15371. (txt (format-time-string fmt (apply 'encode-time f)))
  15372. (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
  15373. (txt (concat "=> " txt)))
  15374. (when (and org-end-time-was-given
  15375. (string-match org-plain-time-of-day-regexp txt))
  15376. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  15377. org-end-time-was-given
  15378. (substring txt (match-end 0)))))
  15379. (when org-read-date-analyze-futurep
  15380. (setq txt (concat txt " (=>F)")))
  15381. (setq org-read-date-overlay
  15382. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  15383. (org-overlay-display org-read-date-overlay txt 'secondary-selection)))))
  15384. (defun org-read-date-analyze (ans org-def org-defdecode)
  15385. "Analyze the combined answer of the date prompt."
  15386. ;; FIXME: cleanup and comment
  15387. (let ((nowdecode (decode-time (current-time)))
  15388. delta deltan deltaw deltadef year month day
  15389. hour minute second wday pm h2 m2 tl wday1
  15390. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  15391. (setq org-read-date-analyze-futurep nil
  15392. org-read-date-analyze-forced-year nil)
  15393. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  15394. (setq ans "+0"))
  15395. (when (setq delta (org-read-date-get-relative ans (current-time) org-def))
  15396. (setq ans (replace-match "" t t ans)
  15397. deltan (car delta)
  15398. deltaw (nth 1 delta)
  15399. deltadef (nth 2 delta)))
  15400. ;; Check if there is an iso week date in there. If yes, store the
  15401. ;; info and postpone interpreting it until the rest of the parsing
  15402. ;; is done.
  15403. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  15404. (setq iso-year (if (match-end 1)
  15405. (org-small-year-to-year
  15406. (string-to-number (match-string 1 ans))))
  15407. iso-weekday (if (match-end 3)
  15408. (string-to-number (match-string 3 ans)))
  15409. iso-week (string-to-number (match-string 2 ans)))
  15410. (setq ans (replace-match "" t t ans)))
  15411. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  15412. (when (string-match
  15413. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  15414. (setq year (if (match-end 2)
  15415. (string-to-number (match-string 2 ans))
  15416. (progn (setq kill-year t)
  15417. (string-to-number (format-time-string "%Y"))))
  15418. month (string-to-number (match-string 3 ans))
  15419. day (string-to-number (match-string 4 ans)))
  15420. (if (< year 100) (setq year (+ 2000 year)))
  15421. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  15422. t nil ans)))
  15423. ;; Help matching dotted european dates
  15424. (when (string-match
  15425. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\.\\( ?[1-9][0-9]\\{3\\}\\)?" ans)
  15426. (setq year (if (match-end 3) (string-to-number (match-string 3 ans))
  15427. (setq kill-year t)
  15428. (string-to-number (format-time-string "%Y")))
  15429. day (string-to-number (match-string 1 ans))
  15430. month (string-to-number (match-string 2 ans))
  15431. ans (replace-match (format "%04d-%02d-%02d" year month day)
  15432. t nil ans)))
  15433. ;; Help matching american dates, like 5/30 or 5/30/7
  15434. (when (string-match
  15435. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  15436. (setq year (if (match-end 4)
  15437. (string-to-number (match-string 4 ans))
  15438. (progn (setq kill-year t)
  15439. (string-to-number (format-time-string "%Y"))))
  15440. month (string-to-number (match-string 1 ans))
  15441. day (string-to-number (match-string 2 ans)))
  15442. (if (< year 100) (setq year (+ 2000 year)))
  15443. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  15444. t nil ans)))
  15445. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  15446. ;; If there is a time with am/pm, and *no* time without it, we convert
  15447. ;; so that matching will be successful.
  15448. (loop for i from 1 to 2 do ; twice, for end time as well
  15449. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  15450. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  15451. (setq hour (string-to-number (match-string 1 ans))
  15452. minute (if (match-end 3)
  15453. (string-to-number (match-string 3 ans))
  15454. 0)
  15455. pm (equal ?p
  15456. (string-to-char (downcase (match-string 4 ans)))))
  15457. (if (and (= hour 12) (not pm))
  15458. (setq hour 0)
  15459. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  15460. (setq ans (replace-match (format "%02d:%02d" hour minute)
  15461. t t ans))))
  15462. ;; Check if a time range is given as a duration
  15463. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  15464. (setq hour (string-to-number (match-string 1 ans))
  15465. h2 (+ hour (string-to-number (match-string 3 ans)))
  15466. minute (string-to-number (match-string 2 ans))
  15467. m2 (+ minute (if (match-end 5) (string-to-number
  15468. (match-string 5 ans))0)))
  15469. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  15470. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  15471. t t ans)))
  15472. ;; Check if there is a time range
  15473. (when (boundp 'org-end-time-was-given)
  15474. (setq org-time-was-given nil)
  15475. (when (and (string-match org-plain-time-of-day-regexp ans)
  15476. (match-end 8))
  15477. (setq org-end-time-was-given (match-string 8 ans))
  15478. (setq ans (concat (substring ans 0 (match-beginning 7))
  15479. (substring ans (match-end 7))))))
  15480. (setq tl (parse-time-string ans)
  15481. day (or (nth 3 tl) (nth 3 org-defdecode))
  15482. month (or (nth 4 tl)
  15483. (if (and org-read-date-prefer-future
  15484. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  15485. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  15486. (nth 4 org-defdecode)))
  15487. year (or (and (not kill-year) (nth 5 tl))
  15488. (if (and org-read-date-prefer-future
  15489. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  15490. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  15491. (nth 5 org-defdecode)))
  15492. hour (or (nth 2 tl) (nth 2 org-defdecode))
  15493. minute (or (nth 1 tl) (nth 1 org-defdecode))
  15494. second (or (nth 0 tl) 0)
  15495. wday (nth 6 tl))
  15496. (when (and (eq org-read-date-prefer-future 'time)
  15497. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  15498. (equal day (nth 3 nowdecode))
  15499. (equal month (nth 4 nowdecode))
  15500. (equal year (nth 5 nowdecode))
  15501. (nth 2 tl)
  15502. (or (< (nth 2 tl) (nth 2 nowdecode))
  15503. (and (= (nth 2 tl) (nth 2 nowdecode))
  15504. (nth 1 tl)
  15505. (< (nth 1 tl) (nth 1 nowdecode)))))
  15506. (setq day (1+ day)
  15507. futurep t))
  15508. ;; Special date definitions below
  15509. (cond
  15510. (iso-week
  15511. ;; There was an iso week
  15512. (require 'cal-iso)
  15513. (setq futurep nil)
  15514. (setq year (or iso-year year)
  15515. day (or iso-weekday wday 1)
  15516. wday nil ; to make sure that the trigger below does not match
  15517. iso-date (calendar-gregorian-from-absolute
  15518. (calendar-absolute-from-iso
  15519. (list iso-week day year))))
  15520. ; FIXME: Should we also push ISO weeks into the future?
  15521. ; (when (and org-read-date-prefer-future
  15522. ; (not iso-year)
  15523. ; (< (calendar-absolute-from-gregorian iso-date)
  15524. ; (time-to-days (current-time))))
  15525. ; (setq year (1+ year)
  15526. ; iso-date (calendar-gregorian-from-absolute
  15527. ; (calendar-absolute-from-iso
  15528. ; (list iso-week day year)))))
  15529. (setq month (car iso-date)
  15530. year (nth 2 iso-date)
  15531. day (nth 1 iso-date)))
  15532. (deltan
  15533. (setq futurep nil)
  15534. (unless deltadef
  15535. (let ((now (decode-time (current-time))))
  15536. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  15537. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  15538. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  15539. ((equal deltaw "m") (setq month (+ month deltan)))
  15540. ((equal deltaw "y") (setq year (+ year deltan)))))
  15541. ((and wday (not (nth 3 tl)))
  15542. ;; Weekday was given, but no day, so pick that day in the week
  15543. ;; on or after the derived date.
  15544. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  15545. (unless (equal wday wday1)
  15546. (setq day (+ day (% (- wday wday1 -7) 7))))))
  15547. (if (and (boundp 'org-time-was-given)
  15548. (nth 2 tl))
  15549. (setq org-time-was-given t))
  15550. (if (< year 100) (setq year (+ 2000 year)))
  15551. ;; Check of the date is representable
  15552. (if org-read-date-force-compatible-dates
  15553. (progn
  15554. (if (< year 1970)
  15555. (setq year 1970 org-read-date-analyze-forced-year t))
  15556. (if (> year 2037)
  15557. (setq year 2037 org-read-date-analyze-forced-year t)))
  15558. (condition-case nil
  15559. (ignore (encode-time second minute hour day month year))
  15560. (error
  15561. (setq year (nth 5 org-defdecode))
  15562. (setq org-read-date-analyze-forced-year t))))
  15563. (setq org-read-date-analyze-futurep futurep)
  15564. (list second minute hour day month year)))
  15565. (defvar parse-time-weekdays)
  15566. (defun org-read-date-get-relative (s today default)
  15567. "Check string S for special relative date string.
  15568. TODAY and DEFAULT are internal times, for today and for a default.
  15569. Return shift list (N what def-flag)
  15570. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  15571. N is the number of WHATs to shift.
  15572. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  15573. the DEFAULT date rather than TODAY."
  15574. (require 'parse-time)
  15575. (when (and
  15576. (string-match
  15577. (concat
  15578. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  15579. "\\([0-9]+\\)?"
  15580. "\\([hdwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  15581. "\\([ \t]\\|$\\)") s)
  15582. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  15583. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  15584. (string-to-char (substring (match-string 1 s) -1))
  15585. ?+))
  15586. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  15587. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  15588. (what (if (match-end 3) (match-string 3 s) "d"))
  15589. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  15590. (date (if rel default today))
  15591. (wday (nth 6 (decode-time date)))
  15592. delta)
  15593. (if wday1
  15594. (progn
  15595. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  15596. (if (= delta 0) (setq delta 7))
  15597. (if (= dir ?-)
  15598. (progn
  15599. (setq delta (- delta 7))
  15600. (if (= delta 0) (setq delta -7))))
  15601. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  15602. (list delta "d" rel))
  15603. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  15604. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  15605. "Turn a user-specified date into the internal representation.
  15606. The internal representation needed by the calendar is (month day year).
  15607. This is a wrapper to handle the brain-dead convention in calendar that
  15608. user function argument order change dependent on argument order."
  15609. (if (boundp 'calendar-date-style)
  15610. (cond
  15611. ((eq calendar-date-style 'american)
  15612. (list arg1 arg2 arg3))
  15613. ((eq calendar-date-style 'european)
  15614. (list arg2 arg1 arg3))
  15615. ((eq calendar-date-style 'iso)
  15616. (list arg2 arg3 arg1)))
  15617. (org-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  15618. (if (org-bound-and-true-p european-calendar-style)
  15619. (list arg2 arg1 arg3)
  15620. (list arg1 arg2 arg3)))))
  15621. (defun org-eval-in-calendar (form &optional keepdate)
  15622. "Eval FORM in the calendar window and return to current window.
  15623. When KEEPDATE is non-nil, update `org-ans2' from the cursor date,
  15624. otherwise stick to the current value of `org-ans2'."
  15625. (let ((sf (selected-frame))
  15626. (sw (selected-window)))
  15627. (select-window (get-buffer-window "*Calendar*" t))
  15628. (eval form)
  15629. (when (and (not keepdate) (calendar-cursor-to-date))
  15630. (let* ((date (calendar-cursor-to-date))
  15631. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15632. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  15633. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  15634. (select-window sw)
  15635. (org-select-frame-set-input-focus sf)))
  15636. (defun org-calendar-select ()
  15637. "Return to `org-read-date' with the date currently selected.
  15638. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15639. (interactive)
  15640. (when (calendar-cursor-to-date)
  15641. (let* ((date (calendar-cursor-to-date))
  15642. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15643. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15644. (if (active-minibuffer-window) (exit-minibuffer))))
  15645. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  15646. "Insert a date stamp for the date given by the internal TIME.
  15647. WITH-HM means use the stamp format that includes the time of the day.
  15648. INACTIVE means use square brackets instead of angular ones, so that the
  15649. stamp will not contribute to the agenda.
  15650. PRE and POST are optional strings to be inserted before and after the
  15651. stamp.
  15652. The command returns the inserted time stamp."
  15653. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  15654. stamp)
  15655. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  15656. (insert-before-markers (or pre ""))
  15657. (when (listp extra)
  15658. (setq extra (car extra))
  15659. (if (and (stringp extra)
  15660. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  15661. (setq extra (format "-%02d:%02d"
  15662. (string-to-number (match-string 1 extra))
  15663. (string-to-number (match-string 2 extra))))
  15664. (setq extra nil)))
  15665. (when extra
  15666. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  15667. (insert-before-markers (setq stamp (format-time-string fmt time)))
  15668. (insert-before-markers (or post ""))
  15669. (setq org-last-inserted-timestamp stamp)))
  15670. (defun org-toggle-time-stamp-overlays ()
  15671. "Toggle the use of custom time stamp formats."
  15672. (interactive)
  15673. (setq org-display-custom-times (not org-display-custom-times))
  15674. (unless org-display-custom-times
  15675. (let ((p (point-min)) (bmp (buffer-modified-p)))
  15676. (while (setq p (next-single-property-change p 'display))
  15677. (if (and (get-text-property p 'display)
  15678. (eq (get-text-property p 'face) 'org-date))
  15679. (remove-text-properties
  15680. p (setq p (next-single-property-change p 'display))
  15681. '(display t))))
  15682. (set-buffer-modified-p bmp)))
  15683. (if (featurep 'xemacs)
  15684. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  15685. (org-restart-font-lock)
  15686. (setq org-table-may-need-update t)
  15687. (if org-display-custom-times
  15688. (message "Time stamps are overlaid with custom format")
  15689. (message "Time stamp overlays removed")))
  15690. (defun org-display-custom-time (beg end)
  15691. "Overlay modified time stamp format over timestamp between BEG and END."
  15692. (let* ((ts (buffer-substring beg end))
  15693. t1 w1 with-hm tf time str w2 (off 0))
  15694. (save-match-data
  15695. (setq t1 (org-parse-time-string ts t))
  15696. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)?\\'" ts)
  15697. (setq off (- (match-end 0) (match-beginning 0)))))
  15698. (setq end (- end off))
  15699. (setq w1 (- end beg)
  15700. with-hm (and (nth 1 t1) (nth 2 t1))
  15701. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  15702. time (org-fix-decoded-time t1)
  15703. str (org-add-props
  15704. (format-time-string
  15705. (substring tf 1 -1) (apply 'encode-time time))
  15706. nil 'mouse-face 'highlight)
  15707. w2 (length str))
  15708. (if (not (= w2 w1))
  15709. (add-text-properties (1+ beg) (+ 2 beg)
  15710. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  15711. (if (featurep 'xemacs)
  15712. (progn
  15713. (put-text-property beg end 'invisible t)
  15714. (put-text-property beg end 'end-glyph (make-glyph str)))
  15715. (put-text-property beg end 'display str))))
  15716. (defun org-translate-time (string)
  15717. "Translate all timestamps in STRING to custom format.
  15718. But do this only if the variable `org-display-custom-times' is set."
  15719. (when org-display-custom-times
  15720. (save-match-data
  15721. (let* ((start 0)
  15722. (re org-ts-regexp-both)
  15723. t1 with-hm inactive tf time str beg end)
  15724. (while (setq start (string-match re string start))
  15725. (setq beg (match-beginning 0)
  15726. end (match-end 0)
  15727. t1 (save-match-data
  15728. (org-parse-time-string (substring string beg end) t))
  15729. with-hm (and (nth 1 t1) (nth 2 t1))
  15730. inactive (equal (substring string beg (1+ beg)) "[")
  15731. tf (funcall (if with-hm 'cdr 'car)
  15732. org-time-stamp-custom-formats)
  15733. time (org-fix-decoded-time t1)
  15734. str (format-time-string
  15735. (concat
  15736. (if inactive "[" "<") (substring tf 1 -1)
  15737. (if inactive "]" ">"))
  15738. (apply 'encode-time time))
  15739. string (replace-match str t t string)
  15740. start (+ start (length str)))))))
  15741. string)
  15742. (defun org-fix-decoded-time (time)
  15743. "Set 0 instead of nil for the first 6 elements of time.
  15744. Don't touch the rest."
  15745. (let ((n 0))
  15746. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  15747. (define-obsolete-function-alias 'org-days-to-time 'org-time-stamp-to-now "24.4")
  15748. (defun org-time-stamp-to-now (timestamp-string &optional seconds)
  15749. "Difference between TIMESTAMP-STRING and now in days.
  15750. If SECONDS is non-nil, return the difference in seconds."
  15751. (let ((fdiff (if seconds 'org-float-time 'time-to-days)))
  15752. (- (funcall fdiff (org-time-string-to-time timestamp-string))
  15753. (funcall fdiff (current-time)))))
  15754. (defun org-deadline-close (timestamp-string &optional ndays)
  15755. "Is the time in TIMESTAMP-STRING close to the current date?"
  15756. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  15757. (and (< (org-time-stamp-to-now timestamp-string) ndays)
  15758. (not (org-entry-is-done-p))))
  15759. (defun org-get-wdays (ts &optional delay zero-delay)
  15760. "Get the deadline lead time appropriate for timestring TS.
  15761. When DELAY is non-nil, get the delay time for scheduled items
  15762. instead of the deadline lead time. When ZERO-DELAY is non-nil
  15763. and `org-scheduled-delay-days' is 0, enforce 0 as the delay,
  15764. don't try to find the delay cookie in the scheduled timestamp."
  15765. (let ((tv (if delay org-scheduled-delay-days
  15766. org-deadline-warning-days)))
  15767. (cond
  15768. ((or (and delay (< tv 0))
  15769. (and delay zero-delay (<= tv 0))
  15770. (and (not delay) (<= tv 0)))
  15771. ;; Enforce this value no matter what
  15772. (- tv))
  15773. ((string-match "-\\([0-9]+\\)\\([hdwmy]\\)\\(\\'\\|>\\| \\)" ts)
  15774. ;; lead time is specified.
  15775. (floor (* (string-to-number (match-string 1 ts))
  15776. (cdr (assoc (match-string 2 ts)
  15777. '(("d" . 1) ("w" . 7)
  15778. ("m" . 30.4) ("y" . 365.25)
  15779. ("h" . 0.041667)))))))
  15780. ;; go for the default.
  15781. (t tv))))
  15782. (defun org-calendar-select-mouse (ev)
  15783. "Return to `org-read-date' with the date currently selected.
  15784. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15785. (interactive "e")
  15786. (mouse-set-point ev)
  15787. (when (calendar-cursor-to-date)
  15788. (let* ((date (calendar-cursor-to-date))
  15789. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15790. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15791. (if (active-minibuffer-window) (exit-minibuffer))))
  15792. (defun org-check-deadlines (ndays)
  15793. "Check if there are any deadlines due or past due.
  15794. A deadline is considered due if it happens within `org-deadline-warning-days'
  15795. days from today's date. If the deadline appears in an entry marked DONE,
  15796. it is not shown. The prefix arg NDAYS can be used to test that many
  15797. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  15798. (interactive "P")
  15799. (let* ((org-warn-days
  15800. (cond
  15801. ((equal ndays '(4)) 100000)
  15802. (ndays (prefix-numeric-value ndays))
  15803. (t (abs org-deadline-warning-days))))
  15804. (case-fold-search nil)
  15805. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15806. (callback
  15807. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  15808. (message "%d deadlines past-due or due within %d days"
  15809. (org-occur regexp nil callback)
  15810. org-warn-days)))
  15811. (defsubst org-re-timestamp (type)
  15812. "Return a regexp for timestamp TYPE.
  15813. Allowed values for TYPE are:
  15814. all: all timestamps
  15815. active: only active timestamps (<...>)
  15816. inactive: only inactive timestamps ([...])
  15817. scheduled: only scheduled timestamps
  15818. deadline: only deadline timestamps
  15819. closed: only closed time-stamps
  15820. When TYPE is nil, fall back on returning a regexp that matches
  15821. both scheduled and deadline timestamps."
  15822. (cond ((eq type 'all) "\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}\\(?: +[^]+0-9> \n -]+\\)?\\(?: +[0-9]\\{1,2\\}:[0-9]\\{2\\}\\)?\\)")
  15823. ((eq type 'active) org-ts-regexp)
  15824. ((eq type 'inactive) "\\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^ \n>]*?\\)\\]")
  15825. ((eq type 'scheduled) (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>"))
  15826. ((eq type 'deadline) (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15827. ((eq type 'closed) (concat org-closed-string " \\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^ \n>]*?\\)\\]"))
  15828. ((eq type 'scheduled-or-deadline)
  15829. (concat "\\<\\(?:" org-deadline-string "\\|" org-scheduled-string "\\) *<\\([^>]+\\)>"))))
  15830. (defun org-check-before-date (date)
  15831. "Check if there are deadlines or scheduled entries before DATE."
  15832. (interactive (list (org-read-date)))
  15833. (let ((case-fold-search nil)
  15834. (regexp (org-re-timestamp org-ts-type))
  15835. (callback
  15836. (lambda () (time-less-p
  15837. (org-time-string-to-time (match-string 1))
  15838. (org-time-string-to-time date)))))
  15839. (message "%d entries before %s"
  15840. (org-occur regexp nil callback) date)))
  15841. (defun org-check-after-date (date)
  15842. "Check if there are deadlines or scheduled entries after DATE."
  15843. (interactive (list (org-read-date)))
  15844. (let ((case-fold-search nil)
  15845. (regexp (org-re-timestamp org-ts-type))
  15846. (callback
  15847. (lambda () (not
  15848. (time-less-p
  15849. (org-time-string-to-time (match-string 1))
  15850. (org-time-string-to-time date))))))
  15851. (message "%d entries after %s"
  15852. (org-occur regexp nil callback) date)))
  15853. (defun org-check-dates-range (start-date end-date)
  15854. "Check for deadlines/scheduled entries between START-DATE and END-DATE."
  15855. (interactive (list (org-read-date nil nil nil "Range starts")
  15856. (org-read-date nil nil nil "Range end")))
  15857. (let ((case-fold-search nil)
  15858. (regexp (org-re-timestamp org-ts-type))
  15859. (callback
  15860. (lambda ()
  15861. (let ((match (match-string 1)))
  15862. (and
  15863. (not (time-less-p
  15864. (org-time-string-to-time match)
  15865. (org-time-string-to-time start-date)))
  15866. (time-less-p
  15867. (org-time-string-to-time match)
  15868. (org-time-string-to-time end-date)))))))
  15869. (message "%d entries between %s and %s"
  15870. (org-occur regexp nil callback) start-date end-date)))
  15871. (defun org-evaluate-time-range (&optional to-buffer)
  15872. "Evaluate a time range by computing the difference between start and end.
  15873. Normally the result is just printed in the echo area, but with prefix arg
  15874. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  15875. If the time range is actually in a table, the result is inserted into the
  15876. next column.
  15877. For time difference computation, a year is assumed to be exactly 365
  15878. days in order to avoid rounding problems."
  15879. (interactive "P")
  15880. (or
  15881. (org-clock-update-time-maybe)
  15882. (save-excursion
  15883. (unless (org-at-date-range-p t)
  15884. (goto-char (point-at-bol))
  15885. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  15886. (if (not (org-at-date-range-p t))
  15887. (user-error "Not at a time-stamp range, and none found in current line")))
  15888. (let* ((ts1 (match-string 1))
  15889. (ts2 (match-string 2))
  15890. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  15891. (match-end (match-end 0))
  15892. (time1 (org-time-string-to-time ts1))
  15893. (time2 (org-time-string-to-time ts2))
  15894. (t1 (org-float-time time1))
  15895. (t2 (org-float-time time2))
  15896. (diff (abs (- t2 t1)))
  15897. (negative (< (- t2 t1) 0))
  15898. ;; (ys (floor (* 365 24 60 60)))
  15899. (ds (* 24 60 60))
  15900. (hs (* 60 60))
  15901. (fy "%dy %dd %02d:%02d")
  15902. (fy1 "%dy %dd")
  15903. (fd "%dd %02d:%02d")
  15904. (fd1 "%dd")
  15905. (fh "%02d:%02d")
  15906. y d h m align)
  15907. (if havetime
  15908. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  15909. y 0
  15910. d (floor (/ diff ds)) diff (mod diff ds)
  15911. h (floor (/ diff hs)) diff (mod diff hs)
  15912. m (floor (/ diff 60)))
  15913. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  15914. y 0
  15915. d (floor (+ (/ diff ds) 0.5))
  15916. h 0 m 0))
  15917. (if (not to-buffer)
  15918. (message "%s" (org-make-tdiff-string y d h m))
  15919. (if (org-at-table-p)
  15920. (progn
  15921. (goto-char match-end)
  15922. (setq align t)
  15923. (and (looking-at " *|") (goto-char (match-end 0))))
  15924. (goto-char match-end))
  15925. (if (looking-at
  15926. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  15927. (replace-match ""))
  15928. (if negative (insert " -"))
  15929. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  15930. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  15931. (insert " " (format fh h m))))
  15932. (if align (org-table-align))
  15933. (message "Time difference inserted")))))
  15934. (defun org-make-tdiff-string (y d h m)
  15935. (let ((fmt "")
  15936. (l nil))
  15937. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  15938. l (push y l)))
  15939. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  15940. l (push d l)))
  15941. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  15942. l (push h l)))
  15943. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  15944. l (push m l)))
  15945. (apply 'format fmt (nreverse l))))
  15946. (defun org-time-string-to-time (s &optional buffer pos)
  15947. "Convert a timestamp string into internal time."
  15948. (condition-case errdata
  15949. (apply 'encode-time (org-parse-time-string s))
  15950. (error (error "Bad timestamp `%s'%s\nError was: %s"
  15951. s (if (not (and buffer pos))
  15952. ""
  15953. (format " at %d in buffer `%s'" pos buffer))
  15954. (cdr errdata)))))
  15955. (defun org-time-string-to-seconds (s)
  15956. "Convert a timestamp string to a number of seconds."
  15957. (org-float-time (org-time-string-to-time s)))
  15958. (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
  15959. "Convert a time stamp to an absolute day number.
  15960. If there is a specifier for a cyclic time stamp, get the closest
  15961. date to DAYNR.
  15962. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  15963. The variable `date' is bound by the calendar when this is called."
  15964. (cond
  15965. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  15966. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  15967. daynr
  15968. (+ daynr 1000)))
  15969. ((and daynr (string-match "\\+[0-9]+[hdwmy]" s))
  15970. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  15971. (time-to-days (current-time))) (match-string 0 s)
  15972. prefer show-all))
  15973. (t (time-to-days
  15974. (condition-case errdata
  15975. (apply 'encode-time (org-parse-time-string s))
  15976. (error (error "Bad timestamp `%s'%s\nError was: %s"
  15977. s (if (not (and buffer pos))
  15978. ""
  15979. (format " at %d in buffer `%s'" pos buffer))
  15980. (cdr errdata))))))))
  15981. (defun org-days-to-iso-week (days)
  15982. "Return the iso week number."
  15983. (require 'cal-iso)
  15984. (car (calendar-iso-from-absolute days)))
  15985. (defun org-small-year-to-year (year)
  15986. "Convert 2-digit years into 4-digit years.
  15987. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2037.
  15988. The year 2000 cannot be abbreviated. Any year larger than 99
  15989. is returned unchanged."
  15990. (if (< year 38)
  15991. (setq year (+ 2000 year))
  15992. (if (< year 100)
  15993. (setq year (+ 1900 year))))
  15994. year)
  15995. (defun org-time-from-absolute (d)
  15996. "Return the time corresponding to date D.
  15997. D may be an absolute day number, or a calendar-type list (month day year)."
  15998. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  15999. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  16000. (defun org-calendar-holiday ()
  16001. "List of holidays, for Diary display in Org-mode."
  16002. (require 'holidays)
  16003. (let ((hl (funcall
  16004. (if (fboundp 'calendar-check-holidays)
  16005. 'calendar-check-holidays 'check-calendar-holidays) date)))
  16006. (if hl (mapconcat 'identity hl "; "))))
  16007. (defun org-diary-sexp-entry (sexp entry date)
  16008. "Process a SEXP diary ENTRY for DATE."
  16009. (require 'diary-lib)
  16010. (let ((result (if calendar-debug-sexp
  16011. (let ((stack-trace-on-error t))
  16012. (eval (car (read-from-string sexp))))
  16013. (condition-case nil
  16014. (eval (car (read-from-string sexp)))
  16015. (error
  16016. (beep)
  16017. (message "Bad sexp at line %d in %s: %s"
  16018. (org-current-line)
  16019. (buffer-file-name) sexp)
  16020. (sleep-for 2))))))
  16021. (cond ((stringp result) (split-string result "; "))
  16022. ((and (consp result)
  16023. (not (consp (cdr result)))
  16024. (stringp (cdr result))) (cdr result))
  16025. ((and (consp result)
  16026. (stringp (car result))) result)
  16027. (result entry))))
  16028. (defun org-diary-to-ical-string (frombuf)
  16029. "Get iCalendar entries from diary entries in buffer FROMBUF.
  16030. This uses the icalendar.el library."
  16031. (let* ((tmpdir (if (featurep 'xemacs)
  16032. (temp-directory)
  16033. temporary-file-directory))
  16034. (tmpfile (make-temp-name
  16035. (expand-file-name "orgics" tmpdir)))
  16036. buf rtn b e)
  16037. (with-current-buffer frombuf
  16038. (icalendar-export-region (point-min) (point-max) tmpfile)
  16039. (setq buf (find-buffer-visiting tmpfile))
  16040. (set-buffer buf)
  16041. (goto-char (point-min))
  16042. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  16043. (setq b (match-beginning 0)))
  16044. (goto-char (point-max))
  16045. (if (re-search-backward "^END:VEVENT" nil t)
  16046. (setq e (match-end 0)))
  16047. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  16048. (kill-buffer buf)
  16049. (delete-file tmpfile)
  16050. rtn))
  16051. (defun org-closest-date (start current change prefer show-all)
  16052. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  16053. When PREFER is `past', return a date that is either CURRENT or past.
  16054. When PREFER is `future', return a date that is either CURRENT or future.
  16055. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  16056. ;; Make the proper lists from the dates
  16057. (catch 'exit
  16058. (let ((a1 '(("h" . hour)
  16059. ("d" . day)
  16060. ("w" . week)
  16061. ("m" . month)
  16062. ("y" . year)))
  16063. (shour (nth 2 (org-parse-time-string start)))
  16064. dn dw sday cday n1 n2 n0
  16065. d m y y1 y2 date1 date2 nmonths nm ny m2)
  16066. (setq start (org-date-to-gregorian start)
  16067. current (org-date-to-gregorian
  16068. (if show-all
  16069. current
  16070. (time-to-days (current-time))))
  16071. sday (calendar-absolute-from-gregorian start)
  16072. cday (calendar-absolute-from-gregorian current))
  16073. (if (<= cday sday) (throw 'exit sday))
  16074. (if (string-match "\\(\\+[0-9]+\\)\\([hdwmy]\\)" change)
  16075. (setq dn (string-to-number (match-string 1 change))
  16076. dw (cdr (assoc (match-string 2 change) a1)))
  16077. (user-error "Invalid change specifier: %s" change))
  16078. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  16079. (cond
  16080. ((eq dw 'hour)
  16081. (let ((missing-hours
  16082. (mod (+ (- (* 24 (- cday sday)) shour) org-extend-today-until)
  16083. dn)))
  16084. (setq n1 (if (zerop missing-hours) cday
  16085. (- cday (1+ (floor (/ missing-hours 24)))))
  16086. n2 (+ cday (floor (/ (- dn missing-hours) 24))))))
  16087. ((eq dw 'day)
  16088. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  16089. n2 (+ n1 dn)))
  16090. ((eq dw 'year)
  16091. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  16092. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  16093. (setq date1 (list m d y1)
  16094. n1 (calendar-absolute-from-gregorian date1)
  16095. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  16096. n2 (calendar-absolute-from-gregorian date2)))
  16097. ((eq dw 'month)
  16098. ;; approx number of month between the two dates
  16099. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  16100. ;; How often does dn fit in there?
  16101. (setq d (nth 1 start) m (car start) y (nth 2 start)
  16102. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  16103. m (+ m nm)
  16104. ny (floor (/ m 12))
  16105. y (+ y ny)
  16106. m (- m (* ny 12)))
  16107. (while (> m 12) (setq m (- m 12) y (1+ y)))
  16108. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  16109. (setq m2 (+ m dn) y2 y)
  16110. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  16111. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  16112. (while (<= n2 cday)
  16113. (setq n1 n2 m m2 y y2)
  16114. (setq m2 (+ m dn) y2 y)
  16115. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  16116. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  16117. ;; Make sure n1 is the earlier date
  16118. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  16119. (if show-all
  16120. (cond
  16121. ((eq prefer 'past) (if (= cday n2) n2 n1))
  16122. ((eq prefer 'future) (if (= cday n1) n1 n2))
  16123. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  16124. (cond
  16125. ((eq prefer 'past) (if (= cday n2) n2 n1))
  16126. ((eq prefer 'future) (if (= cday n1) n1 n2))
  16127. (t (if (= cday n1) n1 n2)))))))
  16128. (defun org-date-to-gregorian (date)
  16129. "Turn any specification of DATE into a Gregorian date for the calendar."
  16130. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  16131. ((and (listp date) (= (length date) 3)) date)
  16132. ((stringp date)
  16133. (setq date (org-parse-time-string date))
  16134. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  16135. ((listp date)
  16136. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  16137. (defun org-parse-time-string (s &optional nodefault)
  16138. "Parse the standard Org-mode time string.
  16139. This should be a lot faster than the normal `parse-time-string'.
  16140. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  16141. hour and minute fields will be nil if not given."
  16142. (cond ((string-match org-ts-regexp0 s)
  16143. (list 0
  16144. (if (or (match-beginning 8) (not nodefault))
  16145. (string-to-number (or (match-string 8 s) "0")))
  16146. (if (or (match-beginning 7) (not nodefault))
  16147. (string-to-number (or (match-string 7 s) "0")))
  16148. (string-to-number (match-string 4 s))
  16149. (string-to-number (match-string 3 s))
  16150. (string-to-number (match-string 2 s))
  16151. nil nil nil))
  16152. ((string-match "^<[^>]+>$" s)
  16153. (decode-time (seconds-to-time (org-matcher-time s))))
  16154. (t (error "Not a standard Org-mode time string: %s" s))))
  16155. (defun org-timestamp-up (&optional arg)
  16156. "Increase the date item at the cursor by one.
  16157. If the cursor is on the year, change the year. If it is on the month,
  16158. the day or the time, change that.
  16159. With prefix ARG, change by that many units."
  16160. (interactive "p")
  16161. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  16162. (defun org-timestamp-down (&optional arg)
  16163. "Decrease the date item at the cursor by one.
  16164. If the cursor is on the year, change the year. If it is on the month,
  16165. the day or the time, change that.
  16166. With prefix ARG, change by that many units."
  16167. (interactive "p")
  16168. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  16169. (defun org-timestamp-up-day (&optional arg)
  16170. "Increase the date in the time stamp by one day.
  16171. With prefix ARG, change that many days."
  16172. (interactive "p")
  16173. (if (and (not (org-at-timestamp-p t))
  16174. (org-at-heading-p))
  16175. (org-todo 'up)
  16176. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  16177. (defun org-timestamp-down-day (&optional arg)
  16178. "Decrease the date in the time stamp by one day.
  16179. With prefix ARG, change that many days."
  16180. (interactive "p")
  16181. (if (and (not (org-at-timestamp-p t))
  16182. (org-at-heading-p))
  16183. (org-todo 'down)
  16184. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  16185. (defun org-at-timestamp-p (&optional inactive-ok)
  16186. "Determine if the cursor is in or at a timestamp."
  16187. (interactive)
  16188. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  16189. (pos (point))
  16190. (ans (or (looking-at tsr)
  16191. (save-excursion
  16192. (skip-chars-backward "^[<\n\r\t")
  16193. (if (> (point) (point-min)) (backward-char 1))
  16194. (and (looking-at tsr)
  16195. (> (- (match-end 0) pos) -1))))))
  16196. (and ans
  16197. (boundp 'org-ts-what)
  16198. (setq org-ts-what
  16199. (cond
  16200. ((= pos (match-beginning 0)) 'bracket)
  16201. ;; Point is considered to be "on the bracket" whether
  16202. ;; it's really on it or right after it.
  16203. ((= pos (1- (match-end 0))) 'bracket)
  16204. ((= pos (match-end 0)) 'after)
  16205. ((org-pos-in-match-range pos 2) 'year)
  16206. ((org-pos-in-match-range pos 3) 'month)
  16207. ((org-pos-in-match-range pos 7) 'hour)
  16208. ((org-pos-in-match-range pos 8) 'minute)
  16209. ((or (org-pos-in-match-range pos 4)
  16210. (org-pos-in-match-range pos 5)) 'day)
  16211. ((and (> pos (or (match-end 8) (match-end 5)))
  16212. (< pos (match-end 0)))
  16213. (- pos (or (match-end 8) (match-end 5))))
  16214. (t 'day))))
  16215. ans))
  16216. (defun org-toggle-timestamp-type ()
  16217. "Toggle the type (<active> or [inactive]) of a time stamp."
  16218. (interactive)
  16219. (when (org-at-timestamp-p t)
  16220. (let ((beg (match-beginning 0)) (end (match-end 0))
  16221. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  16222. (save-excursion
  16223. (goto-char beg)
  16224. (while (re-search-forward "[][<>]" end t)
  16225. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  16226. t t)))
  16227. (message "Timestamp is now %sactive"
  16228. (if (equal (char-after beg) ?<) "" "in")))))
  16229. (defun org-at-clock-log-p nil
  16230. "Is the cursor on the clock log line?"
  16231. (save-excursion
  16232. (move-beginning-of-line 1)
  16233. (looking-at org-clock-line-re)))
  16234. (defvar org-clock-history) ; defined in org-clock.el
  16235. (defvar org-clock-adjust-closest nil) ; defined in org-clock.el
  16236. (defun org-timestamp-change (n &optional what updown suppress-tmp-delay)
  16237. "Change the date in the time stamp at point.
  16238. The date will be changed by N times WHAT. WHAT can be `day', `month',
  16239. `year', `minute', `second'. If WHAT is not given, the cursor position
  16240. in the timestamp determines what will be changed.
  16241. When SUPPRESS-TMP-DELAY is non-nil, suppress delays like \"--2d\"."
  16242. (let ((origin (point)) origin-cat
  16243. with-hm inactive
  16244. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  16245. org-ts-what
  16246. extra rem
  16247. ts time time0 fixnext clrgx)
  16248. (if (not (org-at-timestamp-p t))
  16249. (user-error "Not at a timestamp"))
  16250. (if (and (not what) (eq org-ts-what 'bracket))
  16251. (org-toggle-timestamp-type)
  16252. ;; Point isn't on brackets. Remember the part of the time-stamp
  16253. ;; the point was in. Indeed, size of time-stamps may change,
  16254. ;; but point must be kept in the same category nonetheless.
  16255. (setq origin-cat org-ts-what)
  16256. (if (and (not what) (not (eq org-ts-what 'day))
  16257. org-display-custom-times
  16258. (get-text-property (point) 'display)
  16259. (not (get-text-property (1- (point)) 'display)))
  16260. (setq org-ts-what 'day))
  16261. (setq org-ts-what (or what org-ts-what)
  16262. inactive (= (char-after (match-beginning 0)) ?\[)
  16263. ts (match-string 0))
  16264. (replace-match "")
  16265. (when (string-match
  16266. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?-?[-+][0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)*\\)[]>]"
  16267. ts)
  16268. (setq extra (match-string 1 ts))
  16269. (if suppress-tmp-delay
  16270. (setq extra (replace-regexp-in-string " --[0-9]+[hdwmy]" "" extra))))
  16271. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  16272. (setq with-hm t))
  16273. (setq time0 (org-parse-time-string ts))
  16274. (when (and updown
  16275. (eq org-ts-what 'minute)
  16276. (not current-prefix-arg))
  16277. ;; This looks like s-up and s-down. Change by one rounding step.
  16278. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  16279. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  16280. (setcar (cdr time0) (+ (nth 1 time0)
  16281. (if (> n 0) (- rem) (- dm rem))))))
  16282. (setq time
  16283. (encode-time (or (car time0) 0)
  16284. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  16285. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  16286. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  16287. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  16288. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  16289. (nthcdr 6 time0)))
  16290. (when (and (member org-ts-what '(hour minute))
  16291. extra
  16292. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  16293. (setq extra (org-modify-ts-extra
  16294. extra
  16295. (if (eq org-ts-what 'hour) 2 5)
  16296. n dm)))
  16297. (when (integerp org-ts-what)
  16298. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  16299. (if (eq what 'calendar)
  16300. (let ((cal-date (org-get-date-from-calendar)))
  16301. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  16302. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  16303. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  16304. (setcar time0 (or (car time0) 0))
  16305. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  16306. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  16307. (setq time (apply 'encode-time time0))))
  16308. ;; Insert the new time-stamp, and ensure point stays in the same
  16309. ;; category as before (i.e. not after the last position in that
  16310. ;; category).
  16311. (let ((pos (point)))
  16312. ;; Stay before inserted string. `save-excursion' is of no use.
  16313. (setq org-last-changed-timestamp
  16314. (org-insert-time-stamp time with-hm inactive nil nil extra))
  16315. (goto-char pos))
  16316. (save-match-data
  16317. (looking-at org-ts-regexp3)
  16318. (goto-char (cond
  16319. ;; `day' category ends before `hour' if any, or at
  16320. ;; the end of the day name.
  16321. ((eq origin-cat 'day)
  16322. (min (or (match-beginning 7) (1- (match-end 5))) origin))
  16323. ((eq origin-cat 'hour) (min (match-end 7) origin))
  16324. ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
  16325. ((integerp origin-cat) (min (1- (match-end 0)) origin))
  16326. ;; `year' and `month' have both fixed size: point
  16327. ;; couldn't have moved into another part.
  16328. (t origin))))
  16329. ;; Update clock if on a CLOCK line.
  16330. (org-clock-update-time-maybe)
  16331. ;; Maybe adjust the closest clock in `org-clock-history'
  16332. (when org-clock-adjust-closest
  16333. (if (not (and (org-at-clock-log-p)
  16334. (< 1 (length (delq nil (mapcar 'marker-position
  16335. org-clock-history))))))
  16336. (message "No clock to adjust")
  16337. (cond ((save-excursion ; fix previous clock?
  16338. (re-search-backward org-ts-regexp0 nil t)
  16339. (org-looking-back (concat org-clock-string " \\[")))
  16340. (setq fixnext 1 clrgx (concat org-ts-regexp0 "\\] =>.*$")))
  16341. ((save-excursion ; fix next clock?
  16342. (re-search-backward org-ts-regexp0 nil t)
  16343. (looking-at (concat org-ts-regexp0 "\\] =>")))
  16344. (setq fixnext -1 clrgx (concat org-clock-string " \\[" org-ts-regexp0))))
  16345. (save-window-excursion
  16346. ;; Find closest clock to point, adjust the previous/next one in history
  16347. (let* ((p (save-excursion (org-back-to-heading t)))
  16348. (cl (mapcar (lambda(c) (abs (- (marker-position c) p))) org-clock-history))
  16349. (clfixnth
  16350. (+ fixnext (- (length cl) (or (length (member (apply 'min cl) cl)) 100))))
  16351. (clfixpos (if (> 0 clfixnth) nil (nth clfixnth org-clock-history))))
  16352. (if (not clfixpos)
  16353. (message "No clock to adjust")
  16354. (save-excursion
  16355. (org-goto-marker-or-bmk clfixpos)
  16356. (org-show-subtree)
  16357. (when (re-search-forward clrgx nil t)
  16358. (goto-char (match-beginning 1))
  16359. (let (org-clock-adjust-closest)
  16360. (org-timestamp-change n org-ts-what updown))
  16361. (message "Clock adjusted in %s for heading: %s"
  16362. (file-name-nondirectory (buffer-file-name))
  16363. (org-get-heading t t)))))))))
  16364. ;; Try to recenter the calendar window, if any.
  16365. (if (and org-calendar-follow-timestamp-change
  16366. (get-buffer-window "*Calendar*" t)
  16367. (memq org-ts-what '(day month year)))
  16368. (org-recenter-calendar (time-to-days time))))))
  16369. (defun org-modify-ts-extra (s pos n dm)
  16370. "Change the different parts of the lead-time and repeat fields in timestamp."
  16371. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  16372. ng h m new rem)
  16373. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  16374. (cond
  16375. ((or (org-pos-in-match-range pos 2)
  16376. (org-pos-in-match-range pos 3))
  16377. (setq m (string-to-number (match-string 3 s))
  16378. h (string-to-number (match-string 2 s)))
  16379. (if (org-pos-in-match-range pos 2)
  16380. (setq h (+ h n))
  16381. (setq n (* dm (org-no-warnings (signum n))))
  16382. (when (not (= 0 (setq rem (% m dm))))
  16383. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  16384. (setq m (+ m n)))
  16385. (if (< m 0) (setq m (+ m 60) h (1- h)))
  16386. (if (> m 59) (setq m (- m 60) h (1+ h)))
  16387. (setq h (min 24 (max 0 h)))
  16388. (setq ng 1 new (format "-%02d:%02d" h m)))
  16389. ((org-pos-in-match-range pos 6)
  16390. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  16391. ((org-pos-in-match-range pos 5)
  16392. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  16393. ((org-pos-in-match-range pos 9)
  16394. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  16395. ((org-pos-in-match-range pos 8)
  16396. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  16397. (when ng
  16398. (setq s (concat
  16399. (substring s 0 (match-beginning ng))
  16400. new
  16401. (substring s (match-end ng))))))
  16402. s))
  16403. (defun org-recenter-calendar (date)
  16404. "If the calendar is visible, recenter it to DATE."
  16405. (let ((cwin (get-buffer-window "*Calendar*" t)))
  16406. (when cwin
  16407. (let ((calendar-move-hook nil))
  16408. (with-selected-window cwin
  16409. (calendar-goto-date (if (listp date) date
  16410. (calendar-gregorian-from-absolute date))))))))
  16411. (defun org-goto-calendar (&optional arg)
  16412. "Go to the Emacs calendar at the current date.
  16413. If there is a time stamp in the current line, go to that date.
  16414. A prefix ARG can be used to force the current date."
  16415. (interactive "P")
  16416. (let ((tsr org-ts-regexp) diff
  16417. (calendar-move-hook nil)
  16418. (calendar-view-holidays-initially-flag nil)
  16419. (calendar-view-diary-initially-flag nil))
  16420. (if (or (org-at-timestamp-p)
  16421. (save-excursion
  16422. (beginning-of-line 1)
  16423. (looking-at (concat ".*" tsr))))
  16424. (let ((d1 (time-to-days (current-time)))
  16425. (d2 (time-to-days
  16426. (org-time-string-to-time (match-string 1)))))
  16427. (setq diff (- d2 d1))))
  16428. (calendar)
  16429. (calendar-goto-today)
  16430. (if (and diff (not arg)) (calendar-forward-day diff))))
  16431. (defun org-get-date-from-calendar ()
  16432. "Return a list (month day year) of date at point in calendar."
  16433. (with-current-buffer "*Calendar*"
  16434. (save-match-data
  16435. (calendar-cursor-to-date))))
  16436. (defun org-date-from-calendar ()
  16437. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  16438. If there is already a time stamp at the cursor position, update it."
  16439. (interactive)
  16440. (if (org-at-timestamp-p t)
  16441. (org-timestamp-change 0 'calendar)
  16442. (let ((cal-date (org-get-date-from-calendar)))
  16443. (org-insert-time-stamp
  16444. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  16445. (defcustom org-effort-durations
  16446. `(("min" . 1)
  16447. ("h" . 60)
  16448. ("d" . ,(* 60 8))
  16449. ("w" . ,(* 60 8 5))
  16450. ("m" . ,(* 60 8 5 4))
  16451. ("y" . ,(* 60 8 5 40)))
  16452. "Conversion factor to minutes for an effort modifier.
  16453. Each entry has the form (MODIFIER . MINUTES).
  16454. In an effort string, a number followed by MODIFIER is multiplied
  16455. by the specified number of MINUTES to obtain an effort in
  16456. minutes.
  16457. For example, if the value of this variable is ((\"hours\" . 60)), then an
  16458. effort string \"2hours\" is equivalent to 120 minutes."
  16459. :group 'org-agenda
  16460. :version "25.1"
  16461. :package-version '(Org . "8.3")
  16462. :type '(alist :key-type (string :tag "Modifier")
  16463. :value-type (number :tag "Minutes")))
  16464. (defun org-minutes-to-clocksum-string (m)
  16465. "Format number of minutes as a clocksum string.
  16466. The format is determined by `org-time-clocksum-format',
  16467. `org-time-clocksum-use-fractional' and
  16468. `org-time-clocksum-fractional-format' and
  16469. `org-time-clocksum-use-effort-durations'."
  16470. (let ((clocksum "")
  16471. (m (round m)) ; Don't allow fractions of minutes
  16472. h d w mo y fmt n)
  16473. (setq h (if org-time-clocksum-use-effort-durations
  16474. (cdr (assoc "h" org-effort-durations)) 60)
  16475. d (if org-time-clocksum-use-effort-durations
  16476. (/ (cdr (assoc "d" org-effort-durations)) h) 24)
  16477. w (if org-time-clocksum-use-effort-durations
  16478. (/ (cdr (assoc "w" org-effort-durations)) (* d h)) 7)
  16479. mo (if org-time-clocksum-use-effort-durations
  16480. (/ (cdr (assoc "m" org-effort-durations)) (* d h)) 30)
  16481. y (if org-time-clocksum-use-effort-durations
  16482. (/ (cdr (assoc "y" org-effort-durations)) (* d h)) 365))
  16483. ;; fractional format
  16484. (if org-time-clocksum-use-fractional
  16485. (cond
  16486. ;; single format string
  16487. ((stringp org-time-clocksum-fractional-format)
  16488. (format org-time-clocksum-fractional-format (/ m (float h))))
  16489. ;; choice of fractional formats for different time units
  16490. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :years))
  16491. (> (/ (truncate m) (* y d h)) 0))
  16492. (format fmt (/ m (* y d (float h)))))
  16493. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :months))
  16494. (> (/ (truncate m) (* mo d h)) 0))
  16495. (format fmt (/ m (* mo d (float h)))))
  16496. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  16497. (> (/ (truncate m) (* w d h)) 0))
  16498. (format fmt (/ m (* w d (float h)))))
  16499. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :days))
  16500. (> (/ (truncate m) (* d h)) 0))
  16501. (format fmt (/ m (* d (float h)))))
  16502. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  16503. (> (/ (truncate m) h) 0))
  16504. (format fmt (/ m (float h))))
  16505. ((setq fmt (plist-get org-time-clocksum-fractional-format :minutes))
  16506. (format fmt m))
  16507. ;; fall back to smallest time unit with a format
  16508. ((setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  16509. (format fmt (/ m (float h))))
  16510. ((setq fmt (plist-get org-time-clocksum-fractional-format :days))
  16511. (format fmt (/ m (* d (float h)))))
  16512. ((setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  16513. (format fmt (/ m (* w d (float h)))))
  16514. ((setq fmt (plist-get org-time-clocksum-fractional-format :months))
  16515. (format fmt (/ m (* mo d (float h)))))
  16516. ((setq fmt (plist-get org-time-clocksum-fractional-format :years))
  16517. (format fmt (/ m (* y d (float h))))))
  16518. ;; standard (non-fractional) format, with single format string
  16519. (if (stringp org-time-clocksum-format)
  16520. (format org-time-clocksum-format (setq n (/ m h)) (- m (* h n)))
  16521. ;; separate formats components
  16522. (and (setq fmt (plist-get org-time-clocksum-format :years))
  16523. (or (> (setq n (/ (truncate m) (* y d h))) 0)
  16524. (plist-get org-time-clocksum-format :require-years))
  16525. (setq clocksum (concat clocksum (format fmt n))
  16526. m (- m (* n y d h))))
  16527. (and (setq fmt (plist-get org-time-clocksum-format :months))
  16528. (or (> (setq n (/ (truncate m) (* mo d h))) 0)
  16529. (plist-get org-time-clocksum-format :require-months))
  16530. (setq clocksum (concat clocksum (format fmt n))
  16531. m (- m (* n mo d h))))
  16532. (and (setq fmt (plist-get org-time-clocksum-format :weeks))
  16533. (or (> (setq n (/ (truncate m) (* w d h))) 0)
  16534. (plist-get org-time-clocksum-format :require-weeks))
  16535. (setq clocksum (concat clocksum (format fmt n))
  16536. m (- m (* n w d h))))
  16537. (and (setq fmt (plist-get org-time-clocksum-format :days))
  16538. (or (> (setq n (/ (truncate m) (* d h))) 0)
  16539. (plist-get org-time-clocksum-format :require-days))
  16540. (setq clocksum (concat clocksum (format fmt n))
  16541. m (- m (* n d h))))
  16542. (and (setq fmt (plist-get org-time-clocksum-format :hours))
  16543. (or (> (setq n (/ (truncate m) h)) 0)
  16544. (plist-get org-time-clocksum-format :require-hours))
  16545. (setq clocksum (concat clocksum (format fmt n))
  16546. m (- m (* n h))))
  16547. (and (setq fmt (plist-get org-time-clocksum-format :minutes))
  16548. (or (> m 0) (plist-get org-time-clocksum-format :require-minutes))
  16549. (setq clocksum (concat clocksum (format fmt m))))
  16550. ;; return formatted time duration
  16551. clocksum))))
  16552. (defalias 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string)
  16553. (make-obsolete 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string
  16554. "Org mode version 8.0")
  16555. (defun org-hours-to-clocksum-string (n)
  16556. (org-minutes-to-clocksum-string (* n 60)))
  16557. (defun org-hh:mm-string-to-minutes (s)
  16558. "Convert a string H:MM to a number of minutes.
  16559. If the string is just a number, interpret it as minutes.
  16560. In fact, the first hh:mm or number in the string will be taken,
  16561. there can be extra stuff in the string.
  16562. If no number is found, the return value is 0."
  16563. (cond
  16564. ((integerp s) s)
  16565. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  16566. (+ (* (string-to-number (match-string 1 s)) 60)
  16567. (string-to-number (match-string 2 s))))
  16568. ((string-match "\\([0-9]+\\)" s)
  16569. (string-to-number (match-string 1 s)))
  16570. (t 0)))
  16571. (defcustom org-image-actual-width t
  16572. "Should we use the actual width of images when inlining them?
  16573. When set to `t', always use the image width.
  16574. When set to a number, use imagemagick (when available) to set
  16575. the image's width to this value.
  16576. When set to a number in a list, try to get the width from any
  16577. #+ATTR.* keyword if it matches a width specification like
  16578. #+ATTR_HTML: :width 300px
  16579. and fall back on that number if none is found.
  16580. When set to nil, try to get the width from an #+ATTR.* keyword
  16581. and fall back on the original width if none is found.
  16582. This requires Emacs >= 24.1, build with imagemagick support."
  16583. :group 'org-appearance
  16584. :version "24.4"
  16585. :package-version '(Org . "8.0")
  16586. :type '(choice
  16587. (const :tag "Use the image width" t)
  16588. (integer :tag "Use a number of pixels")
  16589. (list :tag "Use #+ATTR* or a number of pixels" (integer))
  16590. (const :tag "Use #+ATTR* or don't resize" nil)))
  16591. (defcustom org-agenda-inhibit-startup nil
  16592. "Inhibit startup when preparing agenda buffers.
  16593. When this variable is `t', the initialization of the Org agenda
  16594. buffers is inhibited: e.g. the visibility state is not set, the
  16595. tables are not re-aligned, etc."
  16596. :type 'boolean
  16597. :version "24.3"
  16598. :group 'org-agenda)
  16599. (define-obsolete-variable-alias
  16600. 'org-agenda-ignore-drawer-properties
  16601. 'org-agenda-ignore-properties "25.1")
  16602. (defcustom org-agenda-ignore-properties nil
  16603. "Avoid updating text properties when building the agenda.
  16604. Properties are used to prepare buffers for effort estimates,
  16605. appointments, statistics and subtree-local categories.
  16606. If you don't use these in the agenda, you can add them to this
  16607. list and agenda building will be a bit faster.
  16608. The value is a list, with zero or more of the symbols `effort', `appt',
  16609. `stats' or `category'."
  16610. :type '(set :greedy t
  16611. (const effort)
  16612. (const appt)
  16613. (const stats)
  16614. (const category))
  16615. :version "25.1"
  16616. :package-version '(Org . "8.3")
  16617. :group 'org-agenda)
  16618. (defun org-duration-string-to-minutes (s &optional output-to-string)
  16619. "Convert a duration string S to minutes.
  16620. A bare number is interpreted as minutes, modifiers can be set by
  16621. customizing `org-effort-durations' (which see).
  16622. Entries containing a colon are interpreted as H:MM by
  16623. `org-hh:mm-string-to-minutes'."
  16624. (let ((result 0)
  16625. (re (concat "\\([0-9.]+\\) *\\("
  16626. (regexp-opt (mapcar 'car org-effort-durations))
  16627. "\\)")))
  16628. (while (string-match re s)
  16629. (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  16630. (string-to-number (match-string 1 s))))
  16631. (setq s (replace-match "" nil t s)))
  16632. (setq result (floor result))
  16633. (incf result (org-hh:mm-string-to-minutes s))
  16634. (if output-to-string (number-to-string result) result)))
  16635. ;;;; Files
  16636. (defun org-save-all-org-buffers ()
  16637. "Save all Org-mode buffers without user confirmation."
  16638. (interactive)
  16639. (message "Saving all Org-mode buffers...")
  16640. (save-some-buffers t (lambda () (derived-mode-p 'org-mode)))
  16641. (when (featurep 'org-id) (org-id-locations-save))
  16642. (message "Saving all Org-mode buffers... done"))
  16643. (defun org-revert-all-org-buffers ()
  16644. "Revert all Org-mode buffers.
  16645. Prompt for confirmation when there are unsaved changes.
  16646. Be sure you know what you are doing before letting this function
  16647. overwrite your changes.
  16648. This function is useful in a setup where one tracks org files
  16649. with a version control system, to revert on one machine after pulling
  16650. changes from another. I believe the procedure must be like this:
  16651. 1. M-x org-save-all-org-buffers
  16652. 2. Pull changes from the other machine, resolve conflicts
  16653. 3. M-x org-revert-all-org-buffers"
  16654. (interactive)
  16655. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  16656. (user-error "Abort"))
  16657. (save-excursion
  16658. (save-window-excursion
  16659. (mapc
  16660. (lambda (b)
  16661. (when (and (with-current-buffer b (derived-mode-p 'org-mode))
  16662. (with-current-buffer b buffer-file-name))
  16663. (org-pop-to-buffer-same-window b)
  16664. (revert-buffer t 'no-confirm)))
  16665. (buffer-list))
  16666. (when (and (featurep 'org-id) org-id-track-globally)
  16667. (org-id-locations-load)))))
  16668. ;;;; Agenda files
  16669. ;;;###autoload
  16670. (defun org-switchb (&optional arg)
  16671. "Switch between Org buffers.
  16672. With one prefix argument, restrict available buffers to files.
  16673. With two prefix arguments, restrict available buffers to agenda files.
  16674. Defaults to `iswitchb' for buffer name completion.
  16675. Set `org-completion-use-ido' to make it use ido instead."
  16676. (interactive "P")
  16677. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  16678. ((equal arg '(16)) (org-buffer-list 'agenda))
  16679. (t (org-buffer-list))))
  16680. (org-completion-use-iswitchb org-completion-use-iswitchb)
  16681. (org-completion-use-ido org-completion-use-ido))
  16682. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  16683. (setq org-completion-use-iswitchb t))
  16684. (org-pop-to-buffer-same-window
  16685. (org-icompleting-read "Org buffer: "
  16686. (mapcar 'list (mapcar 'buffer-name blist))
  16687. nil t))))
  16688. ;;; Define some older names previously used for this functionality
  16689. ;;;###autoload
  16690. (defalias 'org-ido-switchb 'org-switchb)
  16691. ;;;###autoload
  16692. (defalias 'org-iswitchb 'org-switchb)
  16693. (defun org-buffer-list (&optional predicate exclude-tmp)
  16694. "Return a list of Org buffers.
  16695. PREDICATE can be `export', `files' or `agenda'.
  16696. export restrict the list to Export buffers.
  16697. files restrict the list to buffers visiting Org files.
  16698. agenda restrict the list to buffers visiting agenda files.
  16699. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  16700. (let* ((bfn nil)
  16701. (agenda-files (and (eq predicate 'agenda)
  16702. (mapcar 'file-truename (org-agenda-files t))))
  16703. (filter
  16704. (cond
  16705. ((eq predicate 'files)
  16706. (lambda (b) (with-current-buffer b (derived-mode-p 'org-mode))))
  16707. ((eq predicate 'export)
  16708. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  16709. ((eq predicate 'agenda)
  16710. (lambda (b)
  16711. (with-current-buffer b
  16712. (and (derived-mode-p 'org-mode)
  16713. (setq bfn (buffer-file-name b))
  16714. (member (file-truename bfn) agenda-files)))))
  16715. (t (lambda (b) (with-current-buffer b
  16716. (or (derived-mode-p 'org-mode)
  16717. (string-match "\*Org .*Export"
  16718. (buffer-name b)))))))))
  16719. (delq nil
  16720. (mapcar
  16721. (lambda(b)
  16722. (if (and (funcall filter b)
  16723. (or (not exclude-tmp)
  16724. (not (string-match "tmp" (buffer-name b)))))
  16725. b
  16726. nil))
  16727. (buffer-list)))))
  16728. (defun org-agenda-files (&optional unrestricted archives)
  16729. "Get the list of agenda files.
  16730. Optional UNRESTRICTED means return the full list even if a restriction
  16731. is currently in place.
  16732. When ARCHIVES is t, include all archive files that are really being
  16733. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  16734. `org-agenda-archives-mode' is t."
  16735. (let ((files
  16736. (cond
  16737. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  16738. ((stringp org-agenda-files) (org-read-agenda-file-list))
  16739. ((listp org-agenda-files) org-agenda-files)
  16740. (t (error "Invalid value of `org-agenda-files'")))))
  16741. (setq files (apply 'append
  16742. (mapcar (lambda (f)
  16743. (if (file-directory-p f)
  16744. (directory-files
  16745. f t org-agenda-file-regexp)
  16746. (list f)))
  16747. files)))
  16748. (when org-agenda-skip-unavailable-files
  16749. (setq files (delq nil
  16750. (mapcar (function
  16751. (lambda (file)
  16752. (and (file-readable-p file) file)))
  16753. files))))
  16754. (when (or (eq archives t)
  16755. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  16756. (setq files (org-add-archive-files files)))
  16757. files))
  16758. (defun org-agenda-file-p (&optional file)
  16759. "Return non-nil, if FILE is an agenda file.
  16760. If FILE is omitted, use the file associated with the current
  16761. buffer."
  16762. (let ((fname (or file (buffer-file-name))))
  16763. (and fname
  16764. (member (file-truename fname)
  16765. (mapcar #'file-truename (org-agenda-files t))))))
  16766. (defun org-edit-agenda-file-list ()
  16767. "Edit the list of agenda files.
  16768. Depending on setup, this either uses customize to edit the variable
  16769. `org-agenda-files', or it visits the file that is holding the list. In the
  16770. latter case, the buffer is set up in a way that saving it automatically kills
  16771. the buffer and restores the previous window configuration."
  16772. (interactive)
  16773. (if (stringp org-agenda-files)
  16774. (let ((cw (current-window-configuration)))
  16775. (find-file org-agenda-files)
  16776. (org-set-local 'org-window-configuration cw)
  16777. (org-add-hook 'after-save-hook
  16778. (lambda ()
  16779. (set-window-configuration
  16780. (prog1 org-window-configuration
  16781. (kill-buffer (current-buffer))))
  16782. (org-install-agenda-files-menu)
  16783. (message "New agenda file list installed"))
  16784. nil 'local)
  16785. (message "%s" (substitute-command-keys
  16786. "Edit list and finish with \\[save-buffer]")))
  16787. (customize-variable 'org-agenda-files)))
  16788. (defun org-store-new-agenda-file-list (list)
  16789. "Set new value for the agenda file list and save it correctly."
  16790. (if (stringp org-agenda-files)
  16791. (let ((fe (org-read-agenda-file-list t)) b u)
  16792. (while (setq b (find-buffer-visiting org-agenda-files))
  16793. (kill-buffer b))
  16794. (with-temp-file org-agenda-files
  16795. (insert
  16796. (mapconcat
  16797. (lambda (f) ;; Keep un-expanded entries.
  16798. (if (setq u (assoc f fe))
  16799. (cdr u)
  16800. f))
  16801. list "\n")
  16802. "\n")))
  16803. (let ((org-mode-hook nil) (org-inhibit-startup t)
  16804. (org-insert-mode-line-in-empty-file nil))
  16805. (setq org-agenda-files list)
  16806. (customize-save-variable 'org-agenda-files org-agenda-files))))
  16807. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  16808. "Read the list of agenda files from a file.
  16809. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  16810. filenames, used by `org-store-new-agenda-file-list' to write back
  16811. un-expanded file names."
  16812. (when (file-directory-p org-agenda-files)
  16813. (error "`org-agenda-files' cannot be a single directory"))
  16814. (when (stringp org-agenda-files)
  16815. (with-temp-buffer
  16816. (insert-file-contents org-agenda-files)
  16817. (mapcar
  16818. (lambda (f)
  16819. (let ((e (expand-file-name (substitute-in-file-name f)
  16820. org-directory)))
  16821. (if pair-with-expansion
  16822. (cons e f)
  16823. e)))
  16824. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  16825. ;;;###autoload
  16826. (defun org-cycle-agenda-files ()
  16827. "Cycle through the files in `org-agenda-files'.
  16828. If the current buffer visits an agenda file, find the next one in the list.
  16829. If the current buffer does not, find the first agenda file."
  16830. (interactive)
  16831. (let* ((fs (org-agenda-files t))
  16832. (files (append fs (list (car fs))))
  16833. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  16834. file)
  16835. (unless files (user-error "No agenda files"))
  16836. (catch 'exit
  16837. (while (setq file (pop files))
  16838. (if (equal (file-truename file) tcf)
  16839. (when (car files)
  16840. (find-file (car files))
  16841. (throw 'exit t))))
  16842. (find-file (car fs)))
  16843. (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
  16844. (defun org-agenda-file-to-front (&optional to-end)
  16845. "Move/add the current file to the top of the agenda file list.
  16846. If the file is not present in the list, it is added to the front. If it is
  16847. present, it is moved there. With optional argument TO-END, add/move to the
  16848. end of the list."
  16849. (interactive "P")
  16850. (let ((org-agenda-skip-unavailable-files nil)
  16851. (file-alist (mapcar (lambda (x)
  16852. (cons (file-truename x) x))
  16853. (org-agenda-files t)))
  16854. (ctf (file-truename
  16855. (or buffer-file-name
  16856. (user-error "Please save the current buffer to a file"))))
  16857. x had)
  16858. (setq x (assoc ctf file-alist) had x)
  16859. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  16860. (if to-end
  16861. (setq file-alist (append (delq x file-alist) (list x)))
  16862. (setq file-alist (cons x (delq x file-alist))))
  16863. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  16864. (org-install-agenda-files-menu)
  16865. (message "File %s to %s of agenda file list"
  16866. (if had "moved" "added") (if to-end "end" "front"))))
  16867. (defun org-remove-file (&optional file)
  16868. "Remove current file from the list of files in variable `org-agenda-files'.
  16869. These are the files which are being checked for agenda entries.
  16870. Optional argument FILE means use this file instead of the current."
  16871. (interactive)
  16872. (let* ((org-agenda-skip-unavailable-files nil)
  16873. (file (or file buffer-file-name
  16874. (user-error "Current buffer does not visit a file")))
  16875. (true-file (file-truename file))
  16876. (afile (abbreviate-file-name file))
  16877. (files (delq nil (mapcar
  16878. (lambda (x)
  16879. (if (equal true-file
  16880. (file-truename x))
  16881. nil x))
  16882. (org-agenda-files t)))))
  16883. (if (not (= (length files) (length (org-agenda-files t))))
  16884. (progn
  16885. (org-store-new-agenda-file-list files)
  16886. (org-install-agenda-files-menu)
  16887. (message "Removed from Org Agenda list: %s" afile))
  16888. (message "File was not in list: %s (not removed)" afile))))
  16889. (defun org-file-menu-entry (file)
  16890. (vector file (list 'find-file file) t))
  16891. (defun org-check-agenda-file (file)
  16892. "Make sure FILE exists. If not, ask user what to do."
  16893. (when (not (file-exists-p file))
  16894. (message "Non-existent agenda file %s. [R]emove from list or [A]bort?"
  16895. (abbreviate-file-name file))
  16896. (let ((r (downcase (read-char-exclusive))))
  16897. (cond
  16898. ((equal r ?r)
  16899. (org-remove-file file)
  16900. (throw 'nextfile t))
  16901. (t (user-error "Abort"))))))
  16902. (defun org-get-agenda-file-buffer (file)
  16903. "Get an agenda buffer visiting FILE.
  16904. If the buffer needs to be created, add it to the list of buffers
  16905. which might be released later."
  16906. (let ((buf (org-find-base-buffer-visiting file)))
  16907. (if buf
  16908. buf ; just return it
  16909. ;; Make a new buffer and remember it
  16910. (setq buf (find-file-noselect file))
  16911. (if buf (push buf org-agenda-new-buffers))
  16912. buf)))
  16913. (defun org-release-buffers (blist)
  16914. "Release all buffers in list, asking the user for confirmation when needed.
  16915. When a buffer is unmodified, it is just killed. When modified, it is saved
  16916. \(if the user agrees) and then killed."
  16917. (let (buf file)
  16918. (while (setq buf (pop blist))
  16919. (setq file (buffer-file-name buf))
  16920. (when (and (buffer-modified-p buf)
  16921. file
  16922. (y-or-n-p (format "Save file %s? " file)))
  16923. (with-current-buffer buf (save-buffer)))
  16924. (kill-buffer buf))))
  16925. (defun org-agenda-prepare-buffers (files)
  16926. "Create buffers for all agenda files, protect archived trees and comments."
  16927. (interactive)
  16928. (let ((pa '(:org-archived t))
  16929. (pc '(:org-comment t))
  16930. (pall '(:org-archived t :org-comment t))
  16931. (inhibit-read-only t)
  16932. (org-inhibit-startup org-agenda-inhibit-startup)
  16933. (rea (concat ":" org-archive-tag ":"))
  16934. file re pos)
  16935. (setq org-tag-alist-for-agenda nil
  16936. org-tag-groups-alist-for-agenda nil)
  16937. (save-excursion
  16938. (save-restriction
  16939. (while (setq file (pop files))
  16940. (catch 'nextfile
  16941. (if (bufferp file)
  16942. (set-buffer file)
  16943. (org-check-agenda-file file)
  16944. (set-buffer (org-get-agenda-file-buffer file)))
  16945. (widen)
  16946. (org-set-regexps-and-options 'tags-only)
  16947. (setq pos (point))
  16948. (or (memq 'category org-agenda-ignore-properties)
  16949. (org-refresh-category-properties))
  16950. (or (memq 'stats org-agenda-ignore-properties)
  16951. (org-refresh-stats-properties))
  16952. (or (memq 'effort org-agenda-ignore-properties)
  16953. (org-refresh-effort-properties))
  16954. (or (memq 'appt org-agenda-ignore-properties)
  16955. (org-refresh-properties "APPT_WARNTIME" 'org-appt-warntime))
  16956. (setq org-todo-keywords-for-agenda
  16957. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  16958. (setq org-done-keywords-for-agenda
  16959. (append org-done-keywords-for-agenda org-done-keywords))
  16960. (setq org-todo-keyword-alist-for-agenda
  16961. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  16962. (setq org-tag-alist-for-agenda
  16963. (org-uniquify
  16964. (append org-tag-alist-for-agenda
  16965. org-tag-alist
  16966. org-tag-persistent-alist)))
  16967. (if org-group-tags
  16968. (setq org-tag-groups-alist-for-agenda
  16969. (org-uniquify-alist
  16970. (append org-tag-groups-alist-for-agenda org-tag-groups-alist))))
  16971. (org-with-silent-modifications
  16972. (save-excursion
  16973. (remove-text-properties (point-min) (point-max) pall)
  16974. (when org-agenda-skip-archived-trees
  16975. (goto-char (point-min))
  16976. (while (re-search-forward rea nil t)
  16977. (if (org-at-heading-p t)
  16978. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  16979. (goto-char (point-min))
  16980. (setq re (format "^\\* .*\\<%s\\>" org-comment-string))
  16981. (while (re-search-forward re nil t)
  16982. (when (save-match-data (org-in-commented-heading-p t))
  16983. (add-text-properties
  16984. (match-beginning 0) (org-end-of-subtree t) pc)))))
  16985. (goto-char pos)))))
  16986. (setq org-todo-keywords-for-agenda
  16987. (org-uniquify org-todo-keywords-for-agenda))
  16988. (setq org-todo-keyword-alist-for-agenda
  16989. (org-uniquify org-todo-keyword-alist-for-agenda))))
  16990. ;;;; CDLaTeX minor mode
  16991. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  16992. "Keymap for the minor `org-cdlatex-mode'.")
  16993. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  16994. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  16995. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  16996. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  16997. (org-defkey org-cdlatex-mode-map "\C-c{" 'org-cdlatex-environment-indent)
  16998. (defvar org-cdlatex-texmathp-advice-is-done nil
  16999. "Flag remembering if we have applied the advice to texmathp already.")
  17000. (define-minor-mode org-cdlatex-mode
  17001. "Toggle the minor `org-cdlatex-mode'.
  17002. This mode supports entering LaTeX environment and math in LaTeX fragments
  17003. in Org-mode.
  17004. \\{org-cdlatex-mode-map}"
  17005. nil " OCDL" nil
  17006. (when org-cdlatex-mode
  17007. (require 'cdlatex)
  17008. (run-hooks 'cdlatex-mode-hook)
  17009. (cdlatex-compute-tables))
  17010. (unless org-cdlatex-texmathp-advice-is-done
  17011. (setq org-cdlatex-texmathp-advice-is-done t)
  17012. (defadvice texmathp (around org-math-always-on activate)
  17013. "Always return t in org-mode buffers.
  17014. This is because we want to insert math symbols without dollars even outside
  17015. the LaTeX math segments. If Orgmode thinks that point is actually inside
  17016. an embedded LaTeX fragment, let texmathp do its job.
  17017. \\[org-cdlatex-mode-map]"
  17018. (interactive)
  17019. (let (p)
  17020. (cond
  17021. ((not (derived-mode-p 'org-mode)) ad-do-it)
  17022. ((eq this-command 'cdlatex-math-symbol)
  17023. (setq ad-return-value t
  17024. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  17025. (t
  17026. (let ((p (org-inside-LaTeX-fragment-p)))
  17027. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  17028. (setq ad-return-value t
  17029. texmathp-why '("Org-mode embedded math" . 0))
  17030. (if p ad-do-it)))))))))
  17031. (defun turn-on-org-cdlatex ()
  17032. "Unconditionally turn on `org-cdlatex-mode'."
  17033. (org-cdlatex-mode 1))
  17034. (defun org-try-cdlatex-tab ()
  17035. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  17036. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  17037. - inside a LaTeX fragment, or
  17038. - after the first word in a line, where an abbreviation expansion could
  17039. insert a LaTeX environment."
  17040. (when org-cdlatex-mode
  17041. (cond
  17042. ;; Before any word on the line: No expansion possible.
  17043. ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
  17044. ;; Just after first word on the line: Expand it. Make sure it
  17045. ;; cannot happen on headlines, though.
  17046. ((save-excursion
  17047. (skip-chars-backward "a-zA-Z0-9*")
  17048. (skip-chars-backward " \t")
  17049. (and (bolp) (not (org-at-heading-p))))
  17050. (cdlatex-tab) t)
  17051. ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
  17052. (defun org-cdlatex-underscore-caret (&optional arg)
  17053. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  17054. Revert to the normal definition outside of these fragments."
  17055. (interactive "P")
  17056. (if (org-inside-LaTeX-fragment-p)
  17057. (call-interactively 'cdlatex-sub-superscript)
  17058. (let (org-cdlatex-mode)
  17059. (call-interactively (key-binding (vector last-input-event))))))
  17060. (defun org-cdlatex-math-modify (&optional arg)
  17061. "Execute `cdlatex-math-modify' in LaTeX fragments.
  17062. Revert to the normal definition outside of these fragments."
  17063. (interactive "P")
  17064. (if (org-inside-LaTeX-fragment-p)
  17065. (call-interactively 'cdlatex-math-modify)
  17066. (let (org-cdlatex-mode)
  17067. (call-interactively (key-binding (vector last-input-event))))))
  17068. (defun org-cdlatex-environment-indent (&optional environment item)
  17069. "Execute `cdlatex-environment' and indent the inserted environment."
  17070. (interactive)
  17071. (cdlatex-environment environment item)
  17072. (let ((element (org-element-at-point)))
  17073. (org-indent-region (org-element-property :begin element)
  17074. (org-element-property :end element))))
  17075. ;;;; LaTeX fragments
  17076. (defun org-inside-LaTeX-fragment-p ()
  17077. "Test if point is inside a LaTeX fragment.
  17078. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  17079. sequence appearing also before point.
  17080. Even though the matchers for math are configurable, this function assumes
  17081. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  17082. delimiters are skipped when they have been removed by customization.
  17083. The return value is nil, or a cons cell with the delimiter and the
  17084. position of this delimiter.
  17085. This function does a reasonably good job, but can locally be fooled by
  17086. for example currency specifications. For example it will assume being in
  17087. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  17088. fragments that are properly closed, but during editing, we have to live
  17089. with the uncertainty caused by missing closing delimiters. This function
  17090. looks only before point, not after."
  17091. (catch 'exit
  17092. (let ((pos (point))
  17093. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  17094. (lim (progn
  17095. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  17096. (point)))
  17097. dd-on str (start 0) m re)
  17098. (goto-char pos)
  17099. (when dodollar
  17100. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  17101. re (nth 1 (assoc "$" org-latex-regexps)))
  17102. (while (string-match re str start)
  17103. (cond
  17104. ((= (match-end 0) (length str))
  17105. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  17106. ((= (match-end 0) (- (length str) 5))
  17107. (throw 'exit nil))
  17108. (t (setq start (match-end 0))))))
  17109. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  17110. (goto-char pos)
  17111. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  17112. (and (match-beginning 2) (throw 'exit nil))
  17113. ;; count $$
  17114. (while (re-search-backward "\\$\\$" lim t)
  17115. (setq dd-on (not dd-on)))
  17116. (goto-char pos)
  17117. (if dd-on (cons "$$" m))))))
  17118. (defun org-inside-latex-macro-p ()
  17119. "Is point inside a LaTeX macro or its arguments?"
  17120. (save-match-data
  17121. (org-in-regexp
  17122. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  17123. (defvar org-latex-fragment-image-overlays nil
  17124. "List of overlays carrying the images of latex fragments.")
  17125. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  17126. (defun org-remove-latex-fragment-image-overlays ()
  17127. "Remove all overlays with LaTeX fragment images in current buffer."
  17128. (mapc 'delete-overlay org-latex-fragment-image-overlays)
  17129. (setq org-latex-fragment-image-overlays nil))
  17130. (define-obsolete-function-alias
  17131. 'org-preview-latex-fragment 'org-toggle-latex-fragment "24.4")
  17132. (defun org-toggle-latex-fragment (&optional subtree)
  17133. "Preview the LaTeX fragment at point, or all locally or globally.
  17134. If the cursor is in a LaTeX fragment, create the image and overlay
  17135. it over the source code. If there is no fragment at point, display
  17136. all fragments in the current text, from one headline to the next. With
  17137. prefix SUBTREE, display all fragments in the current subtree. With a
  17138. double prefix arg \\[universal-argument] \\[universal-argument], or when \
  17139. the cursor is before the first headline,
  17140. display all fragments in the buffer.
  17141. The images can be removed again with \\[org-toggle-latex-fragment]."
  17142. (interactive "P")
  17143. (unless (buffer-file-name (buffer-base-buffer))
  17144. (user-error "Can't preview LaTeX fragment in a non-file buffer"))
  17145. (if org-latex-fragment-image-overlays
  17146. (progn (org-remove-latex-fragment-image-overlays)
  17147. (message "LaTeX fragments images removed"))
  17148. (when (display-graphic-p)
  17149. (org-remove-latex-fragment-image-overlays)
  17150. (org-with-wide-buffer
  17151. (let (beg end msg)
  17152. (cond
  17153. ((equal subtree '(16))
  17154. (setq beg (point-min) end (point-max)
  17155. msg "Creating images for buffer...%s"))
  17156. ((equal subtree '(4))
  17157. (org-back-to-heading)
  17158. (setq beg (point) end (org-end-of-subtree t)
  17159. msg "Creating images for subtree...%s"))
  17160. ((let ((context (org-element-context)))
  17161. (when (memq (org-element-type context)
  17162. '(latex-environment latex-fragment))
  17163. (setq beg (org-element-property :begin context)
  17164. end (org-element-property :end context)
  17165. msg "Creating image...%s"))))
  17166. ((org-before-first-heading-p)
  17167. (setq beg (point-min) end (point-max)
  17168. msg "Creating images for buffer...%s"))
  17169. (t
  17170. (org-back-to-heading)
  17171. (setq beg (point) end (progn (outline-next-heading) (point))
  17172. msg "Creating images for entry...%s")))
  17173. (message msg "")
  17174. (narrow-to-region beg end)
  17175. (goto-char beg)
  17176. (org-format-latex
  17177. (concat org-latex-preview-ltxpng-directory
  17178. (file-name-sans-extension
  17179. (file-name-nondirectory
  17180. (buffer-file-name (buffer-base-buffer)))))
  17181. default-directory 'overlays msg 'forbuffer
  17182. org-latex-create-formula-image-program)
  17183. (message msg "done. Use `C-c C-x C-l' to remove images."))))))
  17184. (defun org-format-latex
  17185. (prefix &optional dir overlays msg forbuffer processing-type)
  17186. "Replace LaTeX fragments with links to an image, and produce images.
  17187. Some of the options can be changed using the variable
  17188. `org-format-latex-options'."
  17189. (when (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  17190. (unless (eq processing-type 'verbatim)
  17191. (let* ((math-regexp "\\$\\|\\\\[([]\\|^[ \t]*\\\\begin{[A-Za-z0-9*]+}")
  17192. (cnt 0)
  17193. checkdir-flag)
  17194. (goto-char (point-min))
  17195. (while (re-search-forward math-regexp nil t)
  17196. (unless (and overlays
  17197. (eq (get-char-property (point) 'org-overlay-type)
  17198. 'org-latex-overlay))
  17199. (let* ((context (org-element-context))
  17200. (type (org-element-type context)))
  17201. (when (memq type '(latex-environment latex-fragment))
  17202. (let ((block-type (eq type 'latex-environment))
  17203. (value (org-element-property :value context))
  17204. (beg (org-element-property :begin context))
  17205. (end (save-excursion
  17206. (goto-char (org-element-property :end context))
  17207. (skip-chars-backward " \r\t\n")
  17208. (point))))
  17209. (case processing-type
  17210. (mathjax
  17211. ;; Prepare for MathJax processing.
  17212. (if (eq (char-after beg) ?$)
  17213. (save-excursion
  17214. (delete-region beg end)
  17215. (insert "\\(" (substring value 1 -1) "\\)"))
  17216. (goto-char end)))
  17217. ((dvipng imagemagick)
  17218. ;; Process to an image.
  17219. (incf cnt)
  17220. (goto-char beg)
  17221. (let* ((face (face-at-point))
  17222. ;; Get the colors from the face at point.
  17223. (fg
  17224. (let ((color (plist-get org-format-latex-options
  17225. :foreground)))
  17226. (if (and forbuffer (eq color 'auto))
  17227. (face-attribute face :foreground nil 'default)
  17228. color)))
  17229. (bg
  17230. (let ((color (plist-get org-format-latex-options
  17231. :background)))
  17232. (if (and forbuffer (eq color 'auto))
  17233. (face-attribute face :background nil 'default)
  17234. color)))
  17235. (hash (sha1 (prin1-to-string
  17236. (list org-format-latex-header
  17237. org-latex-default-packages-alist
  17238. org-latex-packages-alist
  17239. org-format-latex-options
  17240. forbuffer value fg bg))))
  17241. (absprefix (expand-file-name prefix dir))
  17242. (linkfile (format "%s_%s.png" prefix hash))
  17243. (movefile (format "%s_%s.png" absprefix hash))
  17244. (sep (and block-type "\n\n"))
  17245. (link (concat sep "[[file:" linkfile "]]" sep))
  17246. (options
  17247. (org-combine-plists
  17248. org-format-latex-options
  17249. `(:foreground ,fg :background ,bg))))
  17250. (when msg (message msg cnt))
  17251. (unless checkdir-flag ; Ensure the directory exists.
  17252. (setq checkdir-flag t)
  17253. (let ((todir (file-name-directory absprefix)))
  17254. (unless (file-directory-p todir)
  17255. (make-directory todir t))))
  17256. (unless (file-exists-p movefile)
  17257. (org-create-formula-image
  17258. value movefile options forbuffer processing-type))
  17259. (if overlays
  17260. (progn
  17261. (dolist (o (overlays-in beg end))
  17262. (when (eq (overlay-get o 'org-overlay-type)
  17263. 'org-latex-overlay)
  17264. (delete-overlay o)))
  17265. (let ((ov (make-overlay beg end)))
  17266. (overlay-put ov
  17267. 'org-overlay-type
  17268. 'org-latex-overlay)
  17269. (if (featurep 'xemacs)
  17270. (progn
  17271. (overlay-put ov 'invisible t)
  17272. (overlay-put
  17273. ov 'end-glyph
  17274. (make-glyph
  17275. (vector 'png :file movefile))))
  17276. (overlay-put
  17277. ov 'display
  17278. (list 'image
  17279. :type 'png
  17280. :file movefile
  17281. :ascent 'center)))
  17282. (push ov org-latex-fragment-image-overlays))
  17283. (goto-char end))
  17284. (delete-region beg end)
  17285. (insert
  17286. (org-add-props link
  17287. (list 'org-latex-src
  17288. (replace-regexp-in-string "\"" "" value)
  17289. 'org-latex-src-embed-type
  17290. (if block-type 'paragraph 'character)))))))
  17291. (mathml
  17292. ;; Process to MathML.
  17293. (unless (org-format-latex-mathml-available-p)
  17294. (user-error "LaTeX to MathML converter not configured"))
  17295. (incf cnt)
  17296. (when msg (message msg cnt))
  17297. (goto-char beg)
  17298. (delete-region beg end)
  17299. (insert (org-format-latex-as-mathml
  17300. value block-type prefix dir)))
  17301. (otherwise
  17302. (error "Unknown conversion type %s for LaTeX fragments"
  17303. processing-type)))))))))))
  17304. (defun org-create-math-formula (latex-frag &optional mathml-file)
  17305. "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
  17306. Use `org-latex-to-mathml-convert-command'. If the conversion is
  17307. sucessful, return the portion between \"<math...> </math>\"
  17308. elements otherwise return nil. When MATHML-FILE is specified,
  17309. write the results in to that file. When invoked as an
  17310. interactive command, prompt for LATEX-FRAG, with initial value
  17311. set to the current active region and echo the results for user
  17312. inspection."
  17313. (interactive (list (let ((frag (when (org-region-active-p)
  17314. (buffer-substring-no-properties
  17315. (region-beginning) (region-end)))))
  17316. (read-string "LaTeX Fragment: " frag nil frag))))
  17317. (unless latex-frag (user-error "Invalid LaTeX fragment"))
  17318. (let* ((tmp-in-file (file-relative-name
  17319. (make-temp-name (expand-file-name "ltxmathml-in"))))
  17320. (ignore (write-region latex-frag nil tmp-in-file))
  17321. (tmp-out-file (file-relative-name
  17322. (make-temp-name (expand-file-name "ltxmathml-out"))))
  17323. (cmd (format-spec
  17324. org-latex-to-mathml-convert-command
  17325. `((?j . ,(shell-quote-argument
  17326. (expand-file-name org-latex-to-mathml-jar-file)))
  17327. (?I . ,(shell-quote-argument tmp-in-file))
  17328. (?o . ,(shell-quote-argument tmp-out-file)))))
  17329. mathml shell-command-output)
  17330. (when (org-called-interactively-p 'any)
  17331. (unless (org-format-latex-mathml-available-p)
  17332. (user-error "LaTeX to MathML converter not configured")))
  17333. (message "Running %s" cmd)
  17334. (setq shell-command-output (shell-command-to-string cmd))
  17335. (setq mathml
  17336. (when (file-readable-p tmp-out-file)
  17337. (with-current-buffer (find-file-noselect tmp-out-file t)
  17338. (goto-char (point-min))
  17339. (when (re-search-forward
  17340. (concat
  17341. (regexp-quote
  17342. "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">")
  17343. "\\(.\\|\n\\)*"
  17344. (regexp-quote "</math>")) nil t)
  17345. (prog1 (match-string 0) (kill-buffer))))))
  17346. (cond
  17347. (mathml
  17348. (setq mathml
  17349. (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
  17350. (when mathml-file
  17351. (write-region mathml nil mathml-file))
  17352. (when (org-called-interactively-p 'any)
  17353. (message mathml)))
  17354. ((message "LaTeX to MathML conversion failed")
  17355. (message shell-command-output)))
  17356. (delete-file tmp-in-file)
  17357. (when (file-exists-p tmp-out-file)
  17358. (delete-file tmp-out-file))
  17359. mathml))
  17360. (defun org-format-latex-as-mathml (latex-frag latex-frag-type
  17361. prefix &optional dir)
  17362. "Use `org-create-math-formula' but check local cache first."
  17363. (let* ((absprefix (expand-file-name prefix dir))
  17364. (print-length nil) (print-level nil)
  17365. (formula-id (concat
  17366. "formula-"
  17367. (sha1
  17368. (prin1-to-string
  17369. (list latex-frag
  17370. org-latex-to-mathml-convert-command)))))
  17371. (formula-cache (format "%s-%s.mathml" absprefix formula-id))
  17372. (formula-cache-dir (file-name-directory formula-cache)))
  17373. (unless (file-directory-p formula-cache-dir)
  17374. (make-directory formula-cache-dir t))
  17375. (unless (file-exists-p formula-cache)
  17376. (org-create-math-formula latex-frag formula-cache))
  17377. (if (file-exists-p formula-cache)
  17378. ;; Successful conversion. Return the link to MathML file.
  17379. (org-add-props
  17380. (format "[[file:%s]]" (file-relative-name formula-cache dir))
  17381. (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
  17382. 'org-latex-src-embed-type (if latex-frag-type
  17383. 'paragraph 'character)))
  17384. ;; Failed conversion. Return the LaTeX fragment verbatim
  17385. latex-frag)))
  17386. (defun org-create-formula-image (string tofile options buffer &optional type)
  17387. "Create an image from LaTeX source using dvipng or convert.
  17388. This function calls either `org-create-formula-image-with-dvipng'
  17389. or `org-create-formula-image-with-imagemagick' depending on the
  17390. value of `org-latex-create-formula-image-program' or on the value
  17391. of the optional TYPE variable.
  17392. Note: ultimately these two function should be combined as they
  17393. share a good deal of logic."
  17394. (org-check-external-command
  17395. "latex" "needed to convert LaTeX fragments to images")
  17396. (funcall
  17397. (case (or type org-latex-create-formula-image-program)
  17398. ('dvipng
  17399. (org-check-external-command
  17400. "dvipng" "needed to convert LaTeX fragments to images")
  17401. 'org-create-formula-image-with-dvipng)
  17402. ('imagemagick
  17403. (org-check-external-command
  17404. "convert" "you need to install imagemagick")
  17405. 'org-create-formula-image-with-imagemagick)
  17406. (t (error
  17407. "Invalid value of `org-latex-create-formula-image-program'")))
  17408. string tofile options buffer))
  17409. (declare-function org-export-get-backend "ox" (name))
  17410. (declare-function org-export--get-global-options "ox" (&optional backend))
  17411. (declare-function org-export--get-inbuffer-options "ox" (&optional backend))
  17412. (declare-function org-latex-guess-inputenc "ox-latex" (header))
  17413. (declare-function org-latex-guess-babel-language "ox-latex" (header info))
  17414. (defun org-create-formula--latex-header ()
  17415. "Return LaTeX header appropriate for previewing a LaTeX snippet."
  17416. (let ((info (org-combine-plists (org-export--get-global-options
  17417. (org-export-get-backend 'latex))
  17418. (org-export--get-inbuffer-options
  17419. (org-export-get-backend 'latex)))))
  17420. (org-latex-guess-babel-language
  17421. (org-latex-guess-inputenc
  17422. (org-splice-latex-header
  17423. org-format-latex-header
  17424. org-latex-default-packages-alist
  17425. org-latex-packages-alist t
  17426. (plist-get info :latex-header)))
  17427. info)))
  17428. ;; This function borrows from Ganesh Swami's latex2png.el
  17429. (defun org-create-formula-image-with-dvipng (string tofile options buffer)
  17430. "This calls dvipng."
  17431. (require 'ox-latex)
  17432. (let* ((tmpdir (if (featurep 'xemacs)
  17433. (temp-directory)
  17434. temporary-file-directory))
  17435. (texfilebase (make-temp-name
  17436. (expand-file-name "orgtex" tmpdir)))
  17437. (texfile (concat texfilebase ".tex"))
  17438. (dvifile (concat texfilebase ".dvi"))
  17439. (pngfile (concat texfilebase ".png"))
  17440. (fnh (if (featurep 'xemacs)
  17441. (font-height (face-font 'default))
  17442. (face-attribute 'default :height nil)))
  17443. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  17444. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  17445. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  17446. "Black"))
  17447. (bg (or (plist-get options (if buffer :background :html-background))
  17448. "Transparent")))
  17449. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground))
  17450. (unless (string= fg "Transparent") (setq fg (org-dvipng-color-format fg))))
  17451. (if (eq bg 'default) (setq bg (org-dvipng-color :background))
  17452. (unless (string= bg "Transparent") (setq bg (org-dvipng-color-format bg))))
  17453. (let ((latex-header (org-create-formula--latex-header)))
  17454. (with-temp-file texfile
  17455. (insert latex-header)
  17456. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")))
  17457. (let ((dir default-directory))
  17458. (ignore-errors
  17459. (cd tmpdir)
  17460. (call-process "latex" nil nil nil texfile))
  17461. (cd dir))
  17462. (if (not (file-exists-p dvifile))
  17463. (progn (message "Failed to create dvi file from %s" texfile) nil)
  17464. (ignore-errors
  17465. (if (featurep 'xemacs)
  17466. (call-process "dvipng" nil nil nil
  17467. "-fg" fg "-bg" bg
  17468. "-T" "tight"
  17469. "-o" pngfile
  17470. dvifile)
  17471. (call-process "dvipng" nil nil nil
  17472. "-fg" fg "-bg" bg
  17473. "-D" dpi
  17474. ;;"-x" scale "-y" scale
  17475. "-T" "tight"
  17476. "-o" pngfile
  17477. dvifile)))
  17478. (if (not (file-exists-p pngfile))
  17479. (if org-format-latex-signal-error
  17480. (error "Failed to create png file from %s" texfile)
  17481. (message "Failed to create png file from %s" texfile)
  17482. nil)
  17483. ;; Use the requested file name and clean up
  17484. (copy-file pngfile tofile 'replace)
  17485. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png" ".out") do
  17486. (if (file-exists-p (concat texfilebase e))
  17487. (delete-file (concat texfilebase e))))
  17488. pngfile))))
  17489. (declare-function org-latex-compile "ox-latex" (texfile &optional snippet))
  17490. (defun org-create-formula-image-with-imagemagick (string tofile options buffer)
  17491. "This calls convert, which is included into imagemagick."
  17492. (require 'ox-latex)
  17493. (let* ((tmpdir (if (featurep 'xemacs)
  17494. (temp-directory)
  17495. temporary-file-directory))
  17496. (texfilebase (make-temp-name
  17497. (expand-file-name "orgtex" tmpdir)))
  17498. (texfile (concat texfilebase ".tex"))
  17499. (pdffile (concat texfilebase ".pdf"))
  17500. (pngfile (concat texfilebase ".png"))
  17501. (fnh (if (featurep 'xemacs)
  17502. (font-height (face-font 'default))
  17503. (face-attribute 'default :height nil)))
  17504. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  17505. (dpi (number-to-string (* scale (floor (if buffer fnh 120.)))))
  17506. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  17507. "black"))
  17508. (bg (or (plist-get options (if buffer :background :html-background))
  17509. "white")))
  17510. (if (eq fg 'default) (setq fg (org-latex-color :foreground))
  17511. (setq fg (org-latex-color-format fg)))
  17512. (if (eq bg 'default) (setq bg (org-latex-color :background))
  17513. (setq bg (org-latex-color-format
  17514. (if (string= bg "Transparent") "white" bg))))
  17515. (let ((latex-header (org-create-formula--latex-header)))
  17516. (with-temp-file texfile
  17517. (insert latex-header)
  17518. (insert "\n\\begin{document}\n"
  17519. "\\definecolor{fg}{rgb}{" fg "}\n"
  17520. "\\definecolor{bg}{rgb}{" bg "}\n"
  17521. "\n\\pagecolor{bg}\n"
  17522. "\n{\\color{fg}\n"
  17523. string
  17524. "\n}\n"
  17525. "\n\\end{document}\n")))
  17526. (org-latex-compile texfile t)
  17527. (if (not (file-exists-p pdffile))
  17528. (progn (message "Failed to create pdf file from %s" texfile) nil)
  17529. (ignore-errors
  17530. (if (featurep 'xemacs)
  17531. (call-process "convert" nil nil nil
  17532. "-density" "96"
  17533. "-trim"
  17534. "-antialias"
  17535. pdffile
  17536. "-quality" "100"
  17537. ;; "-sharpen" "0x1.0"
  17538. pngfile)
  17539. (call-process "convert" nil nil nil
  17540. "-density" dpi
  17541. "-trim"
  17542. "-antialias"
  17543. pdffile
  17544. "-quality" "100"
  17545. ;; "-sharpen" "0x1.0"
  17546. pngfile)))
  17547. (if (not (file-exists-p pngfile))
  17548. (if org-format-latex-signal-error
  17549. (error "Failed to create png file from %s" texfile)
  17550. (message "Failed to create png file from %s" texfile)
  17551. nil)
  17552. ;; Use the requested file name and clean up
  17553. (copy-file pngfile tofile 'replace)
  17554. (loop for e in '(".pdf" ".tex" ".aux" ".log" ".png") do
  17555. (if (file-exists-p (concat texfilebase e))
  17556. (delete-file (concat texfilebase e))))
  17557. pngfile))))
  17558. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  17559. "Fill a LaTeX header template TPL.
  17560. In the template, the following place holders will be recognized:
  17561. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  17562. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  17563. [PACKAGES] \\usepackage statements for PKG
  17564. [NO-PACKAGES] do not include PKG
  17565. [EXTRA] the string EXTRA
  17566. [NO-EXTRA] do not include EXTRA
  17567. For backward compatibility, if both the positive and the negative place
  17568. holder is missing, the positive one (without the \"NO-\") will be
  17569. assumed to be present at the end of the template.
  17570. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  17571. EXTRA is a string.
  17572. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  17573. (let (rpl (end ""))
  17574. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  17575. (setq rpl (if (or (match-end 1) (not def-pkg))
  17576. "" (org-latex-packages-to-string def-pkg snippets-p t))
  17577. tpl (replace-match rpl t t tpl))
  17578. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  17579. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  17580. (setq rpl (if (or (match-end 1) (not pkg))
  17581. "" (org-latex-packages-to-string pkg snippets-p t))
  17582. tpl (replace-match rpl t t tpl))
  17583. (if pkg (setq end
  17584. (concat end "\n"
  17585. (org-latex-packages-to-string pkg snippets-p)))))
  17586. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  17587. (setq rpl (if (or (match-end 1) (not extra))
  17588. "" (concat extra "\n"))
  17589. tpl (replace-match rpl t t tpl))
  17590. (if (and extra (string-match "\\S-" extra))
  17591. (setq end (concat end "\n" extra))))
  17592. (if (string-match "\\S-" end)
  17593. (concat tpl "\n" end)
  17594. tpl)))
  17595. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  17596. "Turn an alist of packages into a string with the \\usepackage macros."
  17597. (setq pkg (mapconcat (lambda(p)
  17598. (cond
  17599. ((stringp p) p)
  17600. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  17601. (format "%% Package %s omitted" (cadr p)))
  17602. ((equal "" (car p))
  17603. (format "\\usepackage{%s}" (cadr p)))
  17604. (t
  17605. (format "\\usepackage[%s]{%s}"
  17606. (car p) (cadr p)))))
  17607. pkg
  17608. "\n"))
  17609. (if newline (concat pkg "\n") pkg))
  17610. (defun org-dvipng-color (attr)
  17611. "Return a RGB color specification for dvipng."
  17612. (apply 'format "rgb %s %s %s"
  17613. (mapcar 'org-normalize-color
  17614. (if (featurep 'xemacs)
  17615. (color-rgb-components
  17616. (face-property 'default
  17617. (cond ((eq attr :foreground) 'foreground)
  17618. ((eq attr :background) 'background))))
  17619. (color-values (face-attribute 'default attr nil))))))
  17620. (defun org-dvipng-color-format (color-name)
  17621. "Convert COLOR-NAME to a RGB color value for dvipng."
  17622. (apply 'format "rgb %s %s %s"
  17623. (mapcar 'org-normalize-color
  17624. (color-values color-name))))
  17625. (defun org-latex-color (attr)
  17626. "Return a RGB color for the LaTeX color package."
  17627. (apply 'format "%s,%s,%s"
  17628. (mapcar 'org-normalize-color
  17629. (if (featurep 'xemacs)
  17630. (color-rgb-components
  17631. (face-property 'default
  17632. (cond ((eq attr :foreground) 'foreground)
  17633. ((eq attr :background) 'background))))
  17634. (color-values (face-attribute 'default attr nil))))))
  17635. (defun org-latex-color-format (color-name)
  17636. "Convert COLOR-NAME to a RGB color value."
  17637. (apply 'format "%s,%s,%s"
  17638. (mapcar 'org-normalize-color
  17639. (color-values color-name))))
  17640. (defun org-normalize-color (value)
  17641. "Return string to be used as color value for an RGB component."
  17642. (format "%g" (/ value 65535.0)))
  17643. ;; Image display
  17644. (defvar org-inline-image-overlays nil)
  17645. (make-variable-buffer-local 'org-inline-image-overlays)
  17646. (defun org-toggle-inline-images (&optional include-linked)
  17647. "Toggle the display of inline images.
  17648. INCLUDE-LINKED is passed to `org-display-inline-images'."
  17649. (interactive "P")
  17650. (if org-inline-image-overlays
  17651. (progn
  17652. (org-remove-inline-images)
  17653. (message "Inline image display turned off"))
  17654. (org-display-inline-images include-linked)
  17655. (if (and (org-called-interactively-p)
  17656. org-inline-image-overlays)
  17657. (message "%d images displayed inline"
  17658. (length org-inline-image-overlays))
  17659. (message "No images to display inline"))))
  17660. (defun org-redisplay-inline-images ()
  17661. "Refresh the display of inline images."
  17662. (interactive)
  17663. (if (not org-inline-image-overlays)
  17664. (org-toggle-inline-images)
  17665. (org-toggle-inline-images)
  17666. (org-toggle-inline-images)))
  17667. (defun org-display-inline-images (&optional include-linked refresh beg end)
  17668. "Display inline images.
  17669. An inline image is a link which follows either of these
  17670. conventions:
  17671. 1. Its path is a file with an extension matching return value
  17672. from `image-file-name-regexp' and it has no contents.
  17673. 2. Its description consists in a single link of the previous
  17674. type.
  17675. When optional argument INCLUDE-LINKED is non-nil, also links with
  17676. a text description part will be inlined. This can be nice for
  17677. a quick look at those images, but it does not reflect what
  17678. exported files will look like.
  17679. When optional argument REFRESH is non-nil, refresh existing
  17680. images between BEG and END. This will create new image displays
  17681. only if necessary. BEG and END default to the buffer
  17682. boundaries."
  17683. (interactive "P")
  17684. (when (display-graphic-p)
  17685. (unless refresh
  17686. (org-remove-inline-images)
  17687. (when (fboundp 'clear-image-cache) (clear-image-cache)))
  17688. (org-with-wide-buffer
  17689. (goto-char (or beg (point-min)))
  17690. (let ((case-fold-search t)
  17691. (file-extension-re (org-image-file-name-regexp)))
  17692. (while (re-search-forward "[][]\\[\\(?:file\\|[./~]\\)" end t)
  17693. (let ((link (save-match-data (org-element-context))))
  17694. ;; Check if we're at an inline image.
  17695. (when (and (equal (org-element-property :type link) "file")
  17696. (or include-linked
  17697. (not (org-element-property :contents-begin link)))
  17698. (let ((parent (org-element-property :parent link)))
  17699. (or (not (eq (org-element-type parent) 'link))
  17700. (not (cdr (org-element-contents parent)))))
  17701. (org-string-match-p file-extension-re
  17702. (org-element-property :path link)))
  17703. (let ((file (expand-file-name (org-element-property :path link))))
  17704. (when (file-exists-p file)
  17705. (let ((width
  17706. ;; Apply `org-image-actual-width' specifications.
  17707. (cond
  17708. ((not (image-type-available-p 'imagemagick)) nil)
  17709. ((eq org-image-actual-width t) nil)
  17710. ((listp org-image-actual-width)
  17711. (or
  17712. ;; First try to find a width among
  17713. ;; attributes associated to the paragraph
  17714. ;; containing link.
  17715. (let ((paragraph
  17716. (let ((e link))
  17717. (while (and (setq e (org-element-property
  17718. :parent e))
  17719. (not (eq (org-element-type e)
  17720. 'paragraph))))
  17721. e)))
  17722. (when paragraph
  17723. (save-excursion
  17724. (goto-char (org-element-property :begin paragraph))
  17725. (when
  17726. (re-search-forward
  17727. "^[ \t]*#\\+attr_.*?: +.*?:width +\\(\\S-+\\)"
  17728. (org-element-property
  17729. :post-affiliated paragraph)
  17730. t)
  17731. (string-to-number (match-string 1))))))
  17732. ;; Otherwise, fall-back to provided number.
  17733. (car org-image-actual-width)))
  17734. ((numberp org-image-actual-width)
  17735. org-image-actual-width)))
  17736. (old (get-char-property-and-overlay
  17737. (org-element-property :begin link)
  17738. 'org-image-overlay)))
  17739. (if (and (car-safe old) refresh)
  17740. (image-refresh (overlay-get (cdr old) 'display))
  17741. (let ((image (create-image file
  17742. (and width 'imagemagick)
  17743. nil
  17744. :width width)))
  17745. (when image
  17746. (let* ((link
  17747. ;; If inline image is the description
  17748. ;; of another link, be sure to
  17749. ;; consider the latter as the one to
  17750. ;; apply the overlay on.
  17751. (let ((parent
  17752. (org-element-property :parent link)))
  17753. (if (eq (org-element-type parent) 'link)
  17754. parent
  17755. link)))
  17756. (ov (make-overlay
  17757. (org-element-property :begin link)
  17758. (progn
  17759. (goto-char
  17760. (org-element-property :end link))
  17761. (skip-chars-backward " \t")
  17762. (point)))))
  17763. (overlay-put ov 'display image)
  17764. (overlay-put ov 'face 'default)
  17765. (overlay-put ov 'org-image-overlay t)
  17766. (overlay-put
  17767. ov 'modification-hooks
  17768. (list 'org-display-inline-remove-overlay))
  17769. (push ov org-inline-image-overlays)))))))))))))))
  17770. (define-obsolete-function-alias
  17771. 'org-display-inline-modification-hook 'org-display-inline-remove-overlay "24.3")
  17772. (defun org-display-inline-remove-overlay (ov after beg end &optional len)
  17773. "Remove inline-display overlay if a corresponding region is modified."
  17774. (let ((inhibit-modification-hooks t))
  17775. (when (and ov after)
  17776. (delete ov org-inline-image-overlays)
  17777. (delete-overlay ov))))
  17778. (defun org-remove-inline-images ()
  17779. "Remove inline display of images."
  17780. (interactive)
  17781. (mapc 'delete-overlay org-inline-image-overlays)
  17782. (setq org-inline-image-overlays nil))
  17783. ;;;; Key bindings
  17784. ;; Outline functions from `outline-mode-prefix-map'
  17785. ;; that can be remapped in Org:
  17786. (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
  17787. (define-key org-mode-map [remap show-subtree] 'org-show-subtree)
  17788. (define-key org-mode-map [remap outline-forward-same-level]
  17789. 'org-forward-heading-same-level)
  17790. (define-key org-mode-map [remap outline-backward-same-level]
  17791. 'org-backward-heading-same-level)
  17792. (define-key org-mode-map [remap show-branches]
  17793. 'org-kill-note-or-show-branches)
  17794. (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
  17795. (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
  17796. (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
  17797. ;; Outline functions from `outline-mode-prefix-map' that can not
  17798. ;; be remapped in Org:
  17799. ;;
  17800. ;; - the column "key binding" shows whether the Outline function is still
  17801. ;; available in Org mode on the same key that it has been bound to in
  17802. ;; Outline mode:
  17803. ;; - "overridden": key used for a different functionality in Org mode
  17804. ;; - else: key still bound to the same Outline function in Org mode
  17805. ;;
  17806. ;; | Outline function | key binding | Org replacement |
  17807. ;; |------------------------------------+-------------+-----------------------|
  17808. ;; | `outline-next-visible-heading' | `C-c C-n' | still same function |
  17809. ;; | `outline-previous-visible-heading' | `C-c C-p' | still same function |
  17810. ;; | `outline-up-heading' | `C-c C-u' | still same function |
  17811. ;; | `outline-move-subtree-up' | overridden | better: org-shiftup |
  17812. ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
  17813. ;; | `show-entry' | overridden | no replacement |
  17814. ;; | `show-children' | `C-c C-i' | visibility cycling |
  17815. ;; | `show-branches' | `C-c C-k' | still same function |
  17816. ;; | `show-subtree' | overridden | visibility cycling |
  17817. ;; | `show-all' | overridden | no replacement |
  17818. ;; | `hide-subtree' | overridden | visibility cycling |
  17819. ;; | `hide-body' | overridden | no replacement |
  17820. ;; | `hide-entry' | overridden | visibility cycling |
  17821. ;; | `hide-leaves' | overridden | no replacement |
  17822. ;; | `hide-sublevels' | overridden | no replacement |
  17823. ;; | `hide-other' | overridden | no replacement |
  17824. ;; Make `C-c C-x' a prefix key
  17825. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  17826. ;; TAB key with modifiers
  17827. (org-defkey org-mode-map "\C-i" 'org-cycle)
  17828. (org-defkey org-mode-map [(tab)] 'org-cycle)
  17829. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  17830. (org-defkey org-mode-map "\M-\t" 'pcomplete)
  17831. ;; The following line is necessary under Suse GNU/Linux
  17832. (unless (featurep 'xemacs)
  17833. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  17834. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  17835. (define-key org-mode-map [backtab] 'org-shifttab)
  17836. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  17837. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  17838. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  17839. ;; Cursor keys with modifiers
  17840. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  17841. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  17842. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  17843. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  17844. (org-defkey org-mode-map [(control meta shift right)] 'org-increase-number-at-point)
  17845. (org-defkey org-mode-map [(control meta shift left)] 'org-decrease-number-at-point)
  17846. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  17847. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  17848. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  17849. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  17850. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  17851. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  17852. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  17853. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  17854. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  17855. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  17856. (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
  17857. (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
  17858. ;; Babel keys
  17859. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  17860. (mapc (lambda (pair)
  17861. (define-key org-babel-map (car pair) (cdr pair)))
  17862. org-babel-key-bindings)
  17863. ;;; Extra keys for tty access.
  17864. ;; We only set them when really needed because otherwise the
  17865. ;; menus don't show the simple keys
  17866. (when (or org-use-extra-keys
  17867. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  17868. (not window-system))
  17869. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  17870. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  17871. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  17872. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  17873. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  17874. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  17875. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  17876. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  17877. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  17878. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  17879. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  17880. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  17881. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  17882. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  17883. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  17884. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  17885. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  17886. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  17887. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  17888. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  17889. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  17890. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  17891. (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
  17892. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  17893. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  17894. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  17895. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  17896. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  17897. ;; All the other keys
  17898. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  17899. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  17900. (if (boundp 'narrow-map)
  17901. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  17902. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  17903. (if (boundp 'narrow-map)
  17904. (org-defkey narrow-map "b" 'org-narrow-to-block)
  17905. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  17906. (if (boundp 'narrow-map)
  17907. (org-defkey narrow-map "e" 'org-narrow-to-element)
  17908. (org-defkey org-mode-map "\C-xne" 'org-narrow-to-element))
  17909. (org-defkey org-mode-map "\C-\M-t" 'org-transpose-element)
  17910. (org-defkey org-mode-map "\M-}" 'org-forward-element)
  17911. (org-defkey org-mode-map "\M-{" 'org-backward-element)
  17912. (org-defkey org-mode-map "\C-c\C-^" 'org-up-element)
  17913. (org-defkey org-mode-map "\C-c\C-_" 'org-down-element)
  17914. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-heading-same-level)
  17915. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-heading-same-level)
  17916. (org-defkey org-mode-map "\C-c\M-f" 'org-next-block)
  17917. (org-defkey org-mode-map "\C-c\M-b" 'org-previous-block)
  17918. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  17919. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  17920. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  17921. (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
  17922. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  17923. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  17924. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  17925. (org-defkey org-mode-map "\C-c\C-xq" 'org-toggle-tags-groups)
  17926. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  17927. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  17928. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  17929. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  17930. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  17931. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  17932. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  17933. (org-defkey org-mode-map "\C-c\M-w" 'org-copy)
  17934. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  17935. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  17936. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  17937. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  17938. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  17939. (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
  17940. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  17941. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  17942. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  17943. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  17944. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  17945. (org-defkey org-mode-map "\C-c\M-l" 'org-insert-last-stored-link)
  17946. (org-defkey org-mode-map "\C-c\C-\M-l" 'org-insert-all-links)
  17947. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  17948. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  17949. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  17950. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  17951. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  17952. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  17953. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  17954. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  17955. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  17956. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  17957. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  17958. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  17959. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  17960. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  17961. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  17962. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  17963. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  17964. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  17965. (org-defkey org-mode-map "\C-c^" 'org-sort)
  17966. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  17967. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  17968. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  17969. (org-defkey org-mode-map [remap open-line] 'org-open-line)
  17970. (org-defkey org-mode-map [remap comment-dwim] 'org-comment-dwim)
  17971. (org-defkey org-mode-map [remap forward-paragraph] 'org-forward-paragraph)
  17972. (org-defkey org-mode-map [remap backward-paragraph] 'org-backward-paragraph)
  17973. (org-defkey org-mode-map "\C-m" 'org-return)
  17974. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  17975. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  17976. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  17977. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  17978. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  17979. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  17980. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  17981. (org-defkey org-mode-map "\C-c\"a" 'orgtbl-ascii-plot)
  17982. (org-defkey org-mode-map "\C-c\"g" 'org-plot/gnuplot)
  17983. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  17984. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  17985. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  17986. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  17987. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  17988. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  17989. (org-defkey org-mode-map "\C-c\C-e" 'org-export-dispatch)
  17990. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width)
  17991. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  17992. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  17993. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  17994. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  17995. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  17996. (org-defkey org-mode-map "\M-h" 'org-mark-element)
  17997. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  17998. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  17999. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  18000. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  18001. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  18002. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  18003. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  18004. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-in-last)
  18005. (org-defkey org-mode-map "\C-c\C-x\C-z" 'org-resolve-clocks)
  18006. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  18007. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  18008. (org-defkey org-mode-map "\C-c\C-x\C-q" 'org-clock-cancel)
  18009. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  18010. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  18011. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  18012. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-toggle-latex-fragment)
  18013. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  18014. (org-defkey org-mode-map "\C-c\C-x\C-\M-v" 'org-redisplay-inline-images)
  18015. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  18016. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  18017. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  18018. (org-defkey org-mode-map "\C-c\C-xP" 'org-set-property-and-value)
  18019. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  18020. (org-defkey org-mode-map "\C-c\C-xE" 'org-inc-effort)
  18021. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  18022. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  18023. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  18024. (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
  18025. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  18026. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  18027. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  18028. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  18029. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  18030. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  18031. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  18032. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  18033. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  18034. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  18035. (when (featurep 'xemacs)
  18036. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  18037. (defconst org-speed-commands-default
  18038. '(
  18039. ("Outline Navigation")
  18040. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  18041. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  18042. ("f" . (org-speed-move-safe 'org-forward-heading-same-level))
  18043. ("b" . (org-speed-move-safe 'org-backward-heading-same-level))
  18044. ("F" . org-next-block)
  18045. ("B" . org-previous-block)
  18046. ("u" . (org-speed-move-safe 'outline-up-heading))
  18047. ("j" . org-goto)
  18048. ("g" . (org-refile t))
  18049. ("Outline Visibility")
  18050. ("c" . org-cycle)
  18051. ("C" . org-shifttab)
  18052. (" " . org-display-outline-path)
  18053. ("s" . org-narrow-to-subtree)
  18054. ("=" . org-columns)
  18055. ("Outline Structure Editing")
  18056. ("U" . org-metaup)
  18057. ("D" . org-metadown)
  18058. ("r" . org-metaright)
  18059. ("l" . org-metaleft)
  18060. ("R" . org-shiftmetaright)
  18061. ("L" . org-shiftmetaleft)
  18062. ("i" . (progn (forward-char 1) (call-interactively
  18063. 'org-insert-heading-respect-content)))
  18064. ("^" . org-sort)
  18065. ("w" . org-refile)
  18066. ("a" . org-archive-subtree-default-with-confirmation)
  18067. ("@" . org-mark-subtree)
  18068. ("#" . org-toggle-comment)
  18069. ("Clock Commands")
  18070. ("I" . org-clock-in)
  18071. ("O" . org-clock-out)
  18072. ("Meta Data Editing")
  18073. ("t" . org-todo)
  18074. ("," . (org-priority))
  18075. ("0" . (org-priority ?\ ))
  18076. ("1" . (org-priority ?A))
  18077. ("2" . (org-priority ?B))
  18078. ("3" . (org-priority ?C))
  18079. (":" . org-set-tags-command)
  18080. ("e" . org-set-effort)
  18081. ("E" . org-inc-effort)
  18082. ("W" . (lambda(m) (interactive "sMinutes before warning: ")
  18083. (org-entry-put (point) "APPT_WARNTIME" m)))
  18084. ("Agenda Views etc")
  18085. ("v" . org-agenda)
  18086. ("/" . org-sparse-tree)
  18087. ("Misc")
  18088. ("o" . org-open-at-point)
  18089. ("?" . org-speed-command-help)
  18090. ("<" . (org-agenda-set-restriction-lock 'subtree))
  18091. (">" . (org-agenda-remove-restriction-lock))
  18092. )
  18093. "The default speed commands.")
  18094. (defun org-print-speed-command (e)
  18095. (if (> (length (car e)) 1)
  18096. (progn
  18097. (princ "\n")
  18098. (princ (car e))
  18099. (princ "\n")
  18100. (princ (make-string (length (car e)) ?-))
  18101. (princ "\n"))
  18102. (princ (car e))
  18103. (princ " ")
  18104. (if (symbolp (cdr e))
  18105. (princ (symbol-name (cdr e)))
  18106. (prin1 (cdr e)))
  18107. (princ "\n")))
  18108. (defun org-speed-command-help ()
  18109. "Show the available speed commands."
  18110. (interactive)
  18111. (if (not org-use-speed-commands)
  18112. (user-error "Speed commands are not activated, customize `org-use-speed-commands'")
  18113. (with-output-to-temp-buffer "*Help*"
  18114. (princ "User-defined Speed commands\n===========================\n")
  18115. (mapc 'org-print-speed-command org-speed-commands-user)
  18116. (princ "\n")
  18117. (princ "Built-in Speed commands\n=======================\n")
  18118. (mapc 'org-print-speed-command org-speed-commands-default))
  18119. (with-current-buffer "*Help*"
  18120. (setq truncate-lines t))))
  18121. (defun org-speed-move-safe (cmd)
  18122. "Execute CMD, but make sure that the cursor always ends up in a headline.
  18123. If not, return to the original position and throw an error."
  18124. (interactive)
  18125. (let ((pos (point)))
  18126. (call-interactively cmd)
  18127. (unless (and (bolp) (org-at-heading-p))
  18128. (goto-char pos)
  18129. (error "Boundary reached while executing %s" cmd))))
  18130. (defvar org-self-insert-command-undo-counter 0)
  18131. (defvar org-table-auto-blank-field) ; defined in org-table.el
  18132. (defvar org-speed-command nil)
  18133. (define-obsolete-function-alias
  18134. 'org-speed-command-default-hook 'org-speed-command-activate "24.3")
  18135. (defun org-speed-command-activate (keys)
  18136. "Hook for activating single-letter speed commands.
  18137. `org-speed-commands-default' specifies a minimal command set.
  18138. Use `org-speed-commands-user' for further customization."
  18139. (when (or (and (bolp) (looking-at org-outline-regexp))
  18140. (and (functionp org-use-speed-commands)
  18141. (funcall org-use-speed-commands)))
  18142. (cdr (assoc keys (append org-speed-commands-user
  18143. org-speed-commands-default)))))
  18144. (define-obsolete-function-alias
  18145. 'org-babel-speed-command-hook 'org-babel-speed-command-activate "24.3")
  18146. (defun org-babel-speed-command-activate (keys)
  18147. "Hook for activating single-letter code block commands."
  18148. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  18149. (cdr (assoc keys org-babel-key-bindings))))
  18150. (defcustom org-speed-command-hook
  18151. '(org-speed-command-default-hook org-babel-speed-command-hook)
  18152. "Hook for activating speed commands at strategic locations.
  18153. Hook functions are called in sequence until a valid handler is
  18154. found.
  18155. Each hook takes a single argument, a user-pressed command key
  18156. which is also a `self-insert-command' from the global map.
  18157. Within the hook, examine the cursor position and the command key
  18158. and return nil or a valid handler as appropriate. Handler could
  18159. be one of an interactive command, a function, or a form.
  18160. Set `org-use-speed-commands' to non-nil value to enable this
  18161. hook. The default setting is `org-speed-command-activate'."
  18162. :group 'org-structure
  18163. :version "24.1"
  18164. :type 'hook)
  18165. (defun org-self-insert-command (N)
  18166. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  18167. If the cursor is in a table looking at whitespace, the whitespace is
  18168. overwritten, and the table is not marked as requiring realignment."
  18169. (interactive "p")
  18170. (org-check-before-invisible-edit 'insert)
  18171. (cond
  18172. ((and org-use-speed-commands
  18173. (let ((kv (this-command-keys-vector)))
  18174. (setq org-speed-command
  18175. (run-hook-with-args-until-success
  18176. 'org-speed-command-hook
  18177. (make-string 1 (aref kv (1- (length kv))))))))
  18178. (cond
  18179. ((commandp org-speed-command)
  18180. (setq this-command org-speed-command)
  18181. (call-interactively org-speed-command))
  18182. ((functionp org-speed-command)
  18183. (funcall org-speed-command))
  18184. ((and org-speed-command (listp org-speed-command))
  18185. (eval org-speed-command))
  18186. (t (let (org-use-speed-commands)
  18187. (call-interactively 'org-self-insert-command)))))
  18188. ((and
  18189. (org-table-p)
  18190. (progn
  18191. ;; check if we blank the field, and if that triggers align
  18192. (and (featurep 'org-table) org-table-auto-blank-field
  18193. (member last-command
  18194. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas-expand))
  18195. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  18196. ;; got extra space, this field does not determine column width
  18197. (let (org-table-may-need-update) (org-table-blank-field))
  18198. ;; no extra space, this field may determine column width
  18199. (org-table-blank-field)))
  18200. t)
  18201. (eq N 1)
  18202. (looking-at "[^|\n]* |"))
  18203. (let (org-table-may-need-update)
  18204. (goto-char (1- (match-end 0)))
  18205. (backward-delete-char 1)
  18206. (goto-char (match-beginning 0))
  18207. (self-insert-command N)))
  18208. (t
  18209. (setq org-table-may-need-update t)
  18210. (self-insert-command N)
  18211. (org-fix-tags-on-the-fly)
  18212. (if org-self-insert-cluster-for-undo
  18213. (if (not (eq last-command 'org-self-insert-command))
  18214. (setq org-self-insert-command-undo-counter 1)
  18215. (if (>= org-self-insert-command-undo-counter 20)
  18216. (setq org-self-insert-command-undo-counter 1)
  18217. (and (> org-self-insert-command-undo-counter 0)
  18218. buffer-undo-list (listp buffer-undo-list)
  18219. (not (cadr buffer-undo-list)) ; remove nil entry
  18220. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  18221. (setq org-self-insert-command-undo-counter
  18222. (1+ org-self-insert-command-undo-counter))))))))
  18223. (defun org-check-before-invisible-edit (kind)
  18224. "Check is editing if kind KIND would be dangerous with invisible text around.
  18225. The detailed reaction depends on the user option `org-catch-invisible-edits'."
  18226. ;; First, try to get out of here as quickly as possible, to reduce overhead
  18227. (if (and org-catch-invisible-edits
  18228. (or (not (boundp 'visible-mode)) (not visible-mode))
  18229. (or (get-char-property (point) 'invisible)
  18230. (get-char-property (max (point-min) (1- (point))) 'invisible)))
  18231. ;; OK, we need to take a closer look
  18232. (let* ((invisible-at-point (get-char-property (point) 'invisible))
  18233. (invisible-before-point (if (bobp) nil (get-char-property
  18234. (1- (point)) 'invisible)))
  18235. (border-and-ok-direction
  18236. (or
  18237. ;; Check if we are acting predictably before invisible text
  18238. (and invisible-at-point (not invisible-before-point)
  18239. (memq kind '(insert delete-backward)))
  18240. ;; Check if we are acting predictably after invisible text
  18241. ;; This works not well, and I have turned it off. It seems
  18242. ;; better to always show and stop after invisible text.
  18243. ;; (and (not invisible-at-point) invisible-before-point
  18244. ;; (memq kind '(insert delete)))
  18245. )))
  18246. (when (or (memq invisible-at-point '(outline org-hide-block t))
  18247. (memq invisible-before-point '(outline org-hide-block t)))
  18248. (if (eq org-catch-invisible-edits 'error)
  18249. (user-error "Editing in invisible areas is prohibited, make them visible first"))
  18250. (if (and org-custom-properties-overlays
  18251. (y-or-n-p "Display invisible properties in this buffer? "))
  18252. (org-toggle-custom-properties-visibility)
  18253. ;; Make the area visible
  18254. (save-excursion
  18255. (if invisible-before-point
  18256. (goto-char (previous-single-char-property-change
  18257. (point) 'invisible)))
  18258. (show-subtree))
  18259. (cond
  18260. ((eq org-catch-invisible-edits 'show)
  18261. ;; That's it, we do the edit after showing
  18262. (message
  18263. "Unfolding invisible region around point before editing")
  18264. (sit-for 1))
  18265. ((and (eq org-catch-invisible-edits 'smart)
  18266. border-and-ok-direction)
  18267. (message "Unfolding invisible region around point before editing"))
  18268. (t
  18269. ;; Don't do the edit, make the user repeat it in full visibility
  18270. (user-error "Edit in invisible region aborted, repeat to confirm with text visible"))))))))
  18271. (defun org-fix-tags-on-the-fly ()
  18272. (when (and (equal (char-after (point-at-bol)) ?*)
  18273. (org-at-heading-p))
  18274. (org-align-tags-here org-tags-column)))
  18275. (defun org-delete-backward-char (N)
  18276. "Like `delete-backward-char', insert whitespace at field end in tables.
  18277. When deleting backwards, in tables this function will insert whitespace in
  18278. front of the next \"|\" separator, to keep the table aligned. The table will
  18279. still be marked for re-alignment if the field did fill the entire column,
  18280. because, in this case the deletion might narrow the column."
  18281. (interactive "p")
  18282. (save-match-data
  18283. (org-check-before-invisible-edit 'delete-backward)
  18284. (if (and (org-table-p)
  18285. (eq N 1)
  18286. (string-match "|" (buffer-substring (point-at-bol) (point)))
  18287. (looking-at ".*?|"))
  18288. (let ((pos (point))
  18289. (noalign (looking-at "[^|\n\r]* |"))
  18290. (c org-table-may-need-update))
  18291. (backward-delete-char N)
  18292. (if (not overwrite-mode)
  18293. (progn
  18294. (skip-chars-forward "^|")
  18295. (insert " ")
  18296. (goto-char (1- pos))))
  18297. ;; noalign: if there were two spaces at the end, this field
  18298. ;; does not determine the width of the column.
  18299. (if noalign (setq org-table-may-need-update c)))
  18300. (backward-delete-char N)
  18301. (org-fix-tags-on-the-fly))))
  18302. (defun org-delete-char (N)
  18303. "Like `delete-char', but insert whitespace at field end in tables.
  18304. When deleting characters, in tables this function will insert whitespace in
  18305. front of the next \"|\" separator, to keep the table aligned. The table will
  18306. still be marked for re-alignment if the field did fill the entire column,
  18307. because, in this case the deletion might narrow the column."
  18308. (interactive "p")
  18309. (save-match-data
  18310. (org-check-before-invisible-edit 'delete)
  18311. (if (and (org-table-p)
  18312. (not (bolp))
  18313. (not (= (char-after) ?|))
  18314. (eq N 1))
  18315. (if (looking-at ".*?|")
  18316. (let ((pos (point))
  18317. (noalign (looking-at "[^|\n\r]* |"))
  18318. (c org-table-may-need-update))
  18319. (replace-match
  18320. (concat (substring (match-string 0) 1 -1) " |") nil t)
  18321. (goto-char pos)
  18322. ;; noalign: if there were two spaces at the end, this field
  18323. ;; does not determine the width of the column.
  18324. (if noalign (setq org-table-may-need-update c)))
  18325. (delete-char N))
  18326. (delete-char N)
  18327. (org-fix-tags-on-the-fly))))
  18328. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  18329. (put 'org-self-insert-command 'delete-selection
  18330. (lambda ()
  18331. (not (run-hook-with-args-until-success
  18332. 'self-insert-uses-region-functions))))
  18333. (put 'orgtbl-self-insert-command 'delete-selection
  18334. (lambda ()
  18335. (not (run-hook-with-args-until-success
  18336. 'self-insert-uses-region-functions))))
  18337. (put 'org-delete-char 'delete-selection 'supersede)
  18338. (put 'org-delete-backward-char 'delete-selection 'supersede)
  18339. (put 'org-yank 'delete-selection 'yank)
  18340. ;; Make `flyspell-mode' delay after some commands
  18341. (put 'org-self-insert-command 'flyspell-delayed t)
  18342. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  18343. (put 'org-delete-char 'flyspell-delayed t)
  18344. (put 'org-delete-backward-char 'flyspell-delayed t)
  18345. ;; Make pabbrev-mode expand after org-mode commands
  18346. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  18347. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  18348. (defun org-remap (map &rest commands)
  18349. "In MAP, remap the functions given in COMMANDS.
  18350. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  18351. (let (new old)
  18352. (while commands
  18353. (setq old (pop commands) new (pop commands))
  18354. (if (fboundp 'command-remapping)
  18355. (org-defkey map (vector 'remap old) new)
  18356. (substitute-key-definition old new map global-map)))))
  18357. (defun org-transpose-words ()
  18358. "Transpose words for Org.
  18359. This uses the `org-mode-transpose-word-syntax-table' syntax
  18360. table, which interprets characters in `org-emphasis-alist' as
  18361. word constituents."
  18362. (interactive)
  18363. (with-syntax-table org-mode-transpose-word-syntax-table
  18364. (call-interactively 'transpose-words)))
  18365. (org-remap org-mode-map 'transpose-words 'org-transpose-words)
  18366. (when (eq org-enable-table-editor 'optimized)
  18367. ;; If the user wants maximum table support, we need to hijack
  18368. ;; some standard editing functions
  18369. (org-remap org-mode-map
  18370. 'self-insert-command 'org-self-insert-command
  18371. 'delete-char 'org-delete-char
  18372. 'delete-backward-char 'org-delete-backward-char)
  18373. (org-defkey org-mode-map "|" 'org-force-self-insert))
  18374. (defvar org-ctrl-c-ctrl-c-hook nil
  18375. "Hook for functions attaching themselves to `C-c C-c'.
  18376. This can be used to add additional functionality to the C-c C-c
  18377. key which executes context-dependent commands. This hook is run
  18378. before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
  18379. run after the last test.
  18380. Each function will be called with no arguments. The function
  18381. must check if the context is appropriate for it to act. If yes,
  18382. it should do its thing and then return a non-nil value. If the
  18383. context is wrong, just do nothing and return nil.")
  18384. (defvar org-ctrl-c-ctrl-c-final-hook nil
  18385. "Hook for functions attaching themselves to `C-c C-c'.
  18386. This can be used to add additional functionality to the C-c C-c
  18387. key which executes context-dependent commands. This hook is run
  18388. after any other test, while `org-ctrl-c-ctrl-c-hook' is run
  18389. before the first test.
  18390. Each function will be called with no arguments. The function
  18391. must check if the context is appropriate for it to act. If yes,
  18392. it should do its thing and then return a non-nil value. If the
  18393. context is wrong, just do nothing and return nil.")
  18394. (defvar org-tab-first-hook nil
  18395. "Hook for functions to attach themselves to TAB.
  18396. See `org-ctrl-c-ctrl-c-hook' for more information.
  18397. This hook runs as the first action when TAB is pressed, even before
  18398. `org-cycle' messes around with the `outline-regexp' to cater for
  18399. inline tasks and plain list item folding.
  18400. If any function in this hook returns t, any other actions that
  18401. would have been caused by TAB (such as table field motion or visibility
  18402. cycling) will not occur.")
  18403. (defvar org-tab-after-check-for-table-hook nil
  18404. "Hook for functions to attach themselves to TAB.
  18405. See `org-ctrl-c-ctrl-c-hook' for more information.
  18406. This hook runs after it has been established that the cursor is not in a
  18407. table, but before checking if the cursor is in a headline or if global cycling
  18408. should be done.
  18409. If any function in this hook returns t, not other actions like visibility
  18410. cycling will be done.")
  18411. (defvar org-tab-after-check-for-cycling-hook nil
  18412. "Hook for functions to attach themselves to TAB.
  18413. See `org-ctrl-c-ctrl-c-hook' for more information.
  18414. This hook runs after it has been established that not table field motion and
  18415. not visibility should be done because of current context. This is probably
  18416. the place where a package like yasnippets can hook in.")
  18417. (defvar org-tab-before-tab-emulation-hook nil
  18418. "Hook for functions to attach themselves to TAB.
  18419. See `org-ctrl-c-ctrl-c-hook' for more information.
  18420. This hook runs after every other options for TAB have been exhausted, but
  18421. before indentation and \t insertion takes place.")
  18422. (defvar org-metaleft-hook nil
  18423. "Hook for functions attaching themselves to `M-left'.
  18424. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18425. (defvar org-metaright-hook nil
  18426. "Hook for functions attaching themselves to `M-right'.
  18427. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18428. (defvar org-metaup-hook nil
  18429. "Hook for functions attaching themselves to `M-up'.
  18430. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18431. (defvar org-metadown-hook nil
  18432. "Hook for functions attaching themselves to `M-down'.
  18433. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18434. (defvar org-shiftmetaleft-hook nil
  18435. "Hook for functions attaching themselves to `M-S-left'.
  18436. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18437. (defvar org-shiftmetaright-hook nil
  18438. "Hook for functions attaching themselves to `M-S-right'.
  18439. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18440. (defvar org-shiftmetaup-hook nil
  18441. "Hook for functions attaching themselves to `M-S-up'.
  18442. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18443. (defvar org-shiftmetadown-hook nil
  18444. "Hook for functions attaching themselves to `M-S-down'.
  18445. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18446. (defvar org-metareturn-hook nil
  18447. "Hook for functions attaching themselves to `M-RET'.
  18448. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18449. (defvar org-shiftup-hook nil
  18450. "Hook for functions attaching themselves to `S-up'.
  18451. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18452. (defvar org-shiftup-final-hook nil
  18453. "Hook for functions attaching themselves to `S-up'.
  18454. This one runs after all other options except shift-select have been excluded.
  18455. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18456. (defvar org-shiftdown-hook nil
  18457. "Hook for functions attaching themselves to `S-down'.
  18458. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18459. (defvar org-shiftdown-final-hook nil
  18460. "Hook for functions attaching themselves to `S-down'.
  18461. This one runs after all other options except shift-select have been excluded.
  18462. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18463. (defvar org-shiftleft-hook nil
  18464. "Hook for functions attaching themselves to `S-left'.
  18465. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18466. (defvar org-shiftleft-final-hook nil
  18467. "Hook for functions attaching themselves to `S-left'.
  18468. This one runs after all other options except shift-select have been excluded.
  18469. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18470. (defvar org-shiftright-hook nil
  18471. "Hook for functions attaching themselves to `S-right'.
  18472. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18473. (defvar org-shiftright-final-hook nil
  18474. "Hook for functions attaching themselves to `S-right'.
  18475. This one runs after all other options except shift-select have been excluded.
  18476. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18477. (defun org-modifier-cursor-error ()
  18478. "Throw an error, a modified cursor command was applied in wrong context."
  18479. (user-error "This command is active in special context like tables, headlines or items"))
  18480. (defun org-shiftselect-error ()
  18481. "Throw an error because Shift-Cursor command was applied in wrong context."
  18482. (if (and (boundp 'shift-select-mode) shift-select-mode)
  18483. (user-error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  18484. (user-error "This command works only in special context like headlines or timestamps")))
  18485. (defun org-call-for-shift-select (cmd)
  18486. (let ((this-command-keys-shift-translated t))
  18487. (call-interactively cmd)))
  18488. (defun org-shifttab (&optional arg)
  18489. "Global visibility cycling or move to previous table field.
  18490. Call `org-table-previous-field' within a table.
  18491. When ARG is nil, cycle globally through visibility states.
  18492. When ARG is a numeric prefix, show contents of this level."
  18493. (interactive "P")
  18494. (cond
  18495. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  18496. ((integerp arg)
  18497. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  18498. (message "Content view to level: %d" arg)
  18499. (org-content (prefix-numeric-value arg2))
  18500. (org-cycle-show-empty-lines t)
  18501. (setq org-cycle-global-status 'overview)))
  18502. (t (call-interactively 'org-global-cycle))))
  18503. (defun org-shiftmetaleft ()
  18504. "Promote subtree or delete table column.
  18505. Calls `org-promote-subtree', `org-outdent-item-tree', or
  18506. `org-table-delete-column', depending on context. See the
  18507. individual commands for more information."
  18508. (interactive)
  18509. (cond
  18510. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  18511. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  18512. ((org-at-heading-p) (call-interactively 'org-promote-subtree))
  18513. ((if (not (org-region-active-p)) (org-at-item-p)
  18514. (save-excursion (goto-char (region-beginning))
  18515. (org-at-item-p)))
  18516. (call-interactively 'org-outdent-item-tree))
  18517. (t (org-modifier-cursor-error))))
  18518. (defun org-shiftmetaright ()
  18519. "Demote subtree or insert table column.
  18520. Calls `org-demote-subtree', `org-indent-item-tree', or
  18521. `org-table-insert-column', depending on context. See the
  18522. individual commands for more information."
  18523. (interactive)
  18524. (cond
  18525. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  18526. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  18527. ((org-at-heading-p) (call-interactively 'org-demote-subtree))
  18528. ((if (not (org-region-active-p)) (org-at-item-p)
  18529. (save-excursion (goto-char (region-beginning))
  18530. (org-at-item-p)))
  18531. (call-interactively 'org-indent-item-tree))
  18532. (t (org-modifier-cursor-error))))
  18533. (defun org-shiftmetaup (&optional arg)
  18534. "Drag the line at point up.
  18535. In a table, kill the current row.
  18536. On a clock timestamp, update the value of the timestamp like `S-<up>'
  18537. but also adjust the previous clocked item in the clock history.
  18538. Everywhere else, drag the line at point up."
  18539. (interactive "P")
  18540. (cond
  18541. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  18542. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  18543. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  18544. (call-interactively 'org-timestamp-up)))
  18545. (t (call-interactively 'org-drag-line-backward))))
  18546. (defun org-shiftmetadown (&optional arg)
  18547. "Drag the line at point down.
  18548. In a table, insert an empty row at the current line.
  18549. On a clock timestamp, update the value of the timestamp like `S-<down>'
  18550. but also adjust the previous clocked item in the clock history.
  18551. Everywhere else, drag the line at point down."
  18552. (interactive "P")
  18553. (cond
  18554. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  18555. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  18556. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  18557. (call-interactively 'org-timestamp-down)))
  18558. (t (call-interactively 'org-drag-line-forward))))
  18559. (defsubst org-hidden-tree-error ()
  18560. (user-error
  18561. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  18562. (defun org-metaleft (&optional arg)
  18563. "Promote heading or move table column to left.
  18564. Calls `org-do-promote' or `org-table-move-column', depending on context.
  18565. With no specific context, calls the Emacs default `backward-word'.
  18566. See the individual commands for more information."
  18567. (interactive "P")
  18568. (cond
  18569. ((run-hook-with-args-until-success 'org-metaleft-hook))
  18570. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  18571. ((org-with-limited-levels
  18572. (or (org-at-heading-p)
  18573. (and (org-region-active-p)
  18574. (save-excursion
  18575. (goto-char (region-beginning))
  18576. (org-at-heading-p)))))
  18577. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18578. (call-interactively 'org-do-promote))
  18579. ;; At an inline task.
  18580. ((org-at-heading-p)
  18581. (call-interactively 'org-inlinetask-promote))
  18582. ((or (org-at-item-p)
  18583. (and (org-region-active-p)
  18584. (save-excursion
  18585. (goto-char (region-beginning))
  18586. (org-at-item-p))))
  18587. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18588. (call-interactively 'org-outdent-item))
  18589. (t (call-interactively 'backward-word))))
  18590. (defun org-metaright (&optional arg)
  18591. "Demote a subtree, a list item or move table column to right.
  18592. In front of a drawer or a block keyword, indent it correctly.
  18593. With no specific context, calls the Emacs default `forward-word'.
  18594. See the individual commands for more information."
  18595. (interactive "P")
  18596. (cond
  18597. ((run-hook-with-args-until-success 'org-metaright-hook))
  18598. ((org-at-table-p) (call-interactively 'org-table-move-column))
  18599. ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
  18600. ((org-at-block-p) (call-interactively 'org-indent-block))
  18601. ((org-with-limited-levels
  18602. (or (org-at-heading-p)
  18603. (and (org-region-active-p)
  18604. (save-excursion
  18605. (goto-char (region-beginning))
  18606. (org-at-heading-p)))))
  18607. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18608. (call-interactively 'org-do-demote))
  18609. ;; At an inline task.
  18610. ((org-at-heading-p)
  18611. (call-interactively 'org-inlinetask-demote))
  18612. ((or (org-at-item-p)
  18613. (and (org-region-active-p)
  18614. (save-excursion
  18615. (goto-char (region-beginning))
  18616. (org-at-item-p))))
  18617. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18618. (call-interactively 'org-indent-item))
  18619. (t (call-interactively 'forward-word))))
  18620. (defun org-check-for-hidden (what)
  18621. "Check if there are hidden headlines/items in the current visual line.
  18622. WHAT can be either `headlines' or `items'. If the current line is
  18623. an outline or item heading and it has a folded subtree below it,
  18624. this function returns t, nil otherwise."
  18625. (let ((re (cond
  18626. ((eq what 'headlines) org-outline-regexp-bol)
  18627. ((eq what 'items) (org-item-beginning-re))
  18628. (t (error "This should not happen"))))
  18629. beg end)
  18630. (save-excursion
  18631. (catch 'exit
  18632. (unless (org-region-active-p)
  18633. (setq beg (point-at-bol))
  18634. (beginning-of-line 2)
  18635. (while (and (not (eobp)) ;; this is like `next-line'
  18636. (get-char-property (1- (point)) 'invisible))
  18637. (beginning-of-line 2))
  18638. (setq end (point))
  18639. (goto-char beg)
  18640. (goto-char (point-at-eol))
  18641. (setq end (max end (point)))
  18642. (while (re-search-forward re end t)
  18643. (if (get-char-property (match-beginning 0) 'invisible)
  18644. (throw 'exit t))))
  18645. nil))))
  18646. (defun org-metaup (&optional arg)
  18647. "Move subtree up or move table row up.
  18648. Calls `org-move-subtree-up' or `org-table-move-row' or
  18649. `org-move-item-up', depending on context. See the individual commands
  18650. for more information."
  18651. (interactive "P")
  18652. (cond
  18653. ((run-hook-with-args-until-success 'org-metaup-hook))
  18654. ((org-region-active-p)
  18655. (let* ((a (min (region-beginning) (region-end)))
  18656. (b (1- (max (region-beginning) (region-end))))
  18657. (c (save-excursion (goto-char a)
  18658. (move-beginning-of-line 0)))
  18659. (d (save-excursion (goto-char a)
  18660. (move-end-of-line 0) (point))))
  18661. (transpose-regions a b c d)
  18662. (goto-char c)))
  18663. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  18664. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  18665. ((org-at-item-p) (call-interactively 'org-move-item-up))
  18666. (t (org-drag-element-backward))))
  18667. (defun org-metadown (&optional arg)
  18668. "Move subtree down or move table row down.
  18669. Calls `org-move-subtree-down' or `org-table-move-row' or
  18670. `org-move-item-down', depending on context. See the individual
  18671. commands for more information."
  18672. (interactive "P")
  18673. (cond
  18674. ((run-hook-with-args-until-success 'org-metadown-hook))
  18675. ((org-region-active-p)
  18676. (let* ((a (min (region-beginning) (region-end)))
  18677. (b (max (region-beginning) (region-end)))
  18678. (c (save-excursion (goto-char b)
  18679. (move-beginning-of-line 1)))
  18680. (d (save-excursion (goto-char b)
  18681. (move-end-of-line 1) (1+ (point)))))
  18682. (transpose-regions a b c d)
  18683. (goto-char d)))
  18684. ((org-at-table-p) (call-interactively 'org-table-move-row))
  18685. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  18686. ((org-at-item-p) (call-interactively 'org-move-item-down))
  18687. (t (org-drag-element-forward))))
  18688. (defun org-shiftup (&optional arg)
  18689. "Increase item in timestamp or increase priority of current headline.
  18690. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  18691. depending on context. See the individual commands for more information."
  18692. (interactive "P")
  18693. (cond
  18694. ((run-hook-with-args-until-success 'org-shiftup-hook))
  18695. ((and org-support-shift-select (org-region-active-p))
  18696. (org-call-for-shift-select 'previous-line))
  18697. ((org-at-timestamp-p t)
  18698. (call-interactively (if org-edit-timestamp-down-means-later
  18699. 'org-timestamp-down 'org-timestamp-up)))
  18700. ((and (not (eq org-support-shift-select 'always))
  18701. org-enable-priority-commands
  18702. (org-at-heading-p))
  18703. (call-interactively 'org-priority-up))
  18704. ((and (not org-support-shift-select) (org-at-item-p))
  18705. (call-interactively 'org-previous-item))
  18706. ((org-clocktable-try-shift 'up arg))
  18707. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  18708. (org-support-shift-select
  18709. (org-call-for-shift-select 'previous-line))
  18710. (t (org-shiftselect-error))))
  18711. (defun org-shiftdown (&optional arg)
  18712. "Decrease item in timestamp or decrease priority of current headline.
  18713. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  18714. depending on context. See the individual commands for more information."
  18715. (interactive "P")
  18716. (cond
  18717. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  18718. ((and org-support-shift-select (org-region-active-p))
  18719. (org-call-for-shift-select 'next-line))
  18720. ((org-at-timestamp-p t)
  18721. (call-interactively (if org-edit-timestamp-down-means-later
  18722. 'org-timestamp-up 'org-timestamp-down)))
  18723. ((and (not (eq org-support-shift-select 'always))
  18724. org-enable-priority-commands
  18725. (org-at-heading-p))
  18726. (call-interactively 'org-priority-down))
  18727. ((and (not org-support-shift-select) (org-at-item-p))
  18728. (call-interactively 'org-next-item))
  18729. ((org-clocktable-try-shift 'down arg))
  18730. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  18731. (org-support-shift-select
  18732. (org-call-for-shift-select 'next-line))
  18733. (t (org-shiftselect-error))))
  18734. (defun org-shiftright (&optional arg)
  18735. "Cycle the thing at point or in the current line, depending on context.
  18736. Depending on context, this does one of the following:
  18737. - switch a timestamp at point one day into the future
  18738. - on a headline, switch to the next TODO keyword.
  18739. - on an item, switch entire list to the next bullet type
  18740. - on a property line, switch to the next allowed value
  18741. - on a clocktable definition line, move time block into the future"
  18742. (interactive "P")
  18743. (cond
  18744. ((run-hook-with-args-until-success 'org-shiftright-hook))
  18745. ((and org-support-shift-select (org-region-active-p))
  18746. (org-call-for-shift-select 'forward-char))
  18747. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  18748. ((and (not (eq org-support-shift-select 'always))
  18749. (org-at-heading-p))
  18750. (let ((org-inhibit-logging
  18751. (not org-treat-S-cursor-todo-selection-as-state-change))
  18752. (org-inhibit-blocking
  18753. (not org-treat-S-cursor-todo-selection-as-state-change)))
  18754. (org-call-with-arg 'org-todo 'right)))
  18755. ((or (and org-support-shift-select
  18756. (not (eq org-support-shift-select 'always))
  18757. (org-at-item-bullet-p))
  18758. (and (not org-support-shift-select) (org-at-item-p)))
  18759. (org-call-with-arg 'org-cycle-list-bullet nil))
  18760. ((and (not (eq org-support-shift-select 'always))
  18761. (org-at-property-p))
  18762. (call-interactively 'org-property-next-allowed-value))
  18763. ((org-clocktable-try-shift 'right arg))
  18764. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  18765. (org-support-shift-select
  18766. (org-call-for-shift-select 'forward-char))
  18767. (t (org-shiftselect-error))))
  18768. (defun org-shiftleft (&optional arg)
  18769. "Cycle the thing at point or in the current line, depending on context.
  18770. Depending on context, this does one of the following:
  18771. - switch a timestamp at point one day into the past
  18772. - on a headline, switch to the previous TODO keyword.
  18773. - on an item, switch entire list to the previous bullet type
  18774. - on a property line, switch to the previous allowed value
  18775. - on a clocktable definition line, move time block into the past"
  18776. (interactive "P")
  18777. (cond
  18778. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  18779. ((and org-support-shift-select (org-region-active-p))
  18780. (org-call-for-shift-select 'backward-char))
  18781. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  18782. ((and (not (eq org-support-shift-select 'always))
  18783. (org-at-heading-p))
  18784. (let ((org-inhibit-logging
  18785. (not org-treat-S-cursor-todo-selection-as-state-change))
  18786. (org-inhibit-blocking
  18787. (not org-treat-S-cursor-todo-selection-as-state-change)))
  18788. (org-call-with-arg 'org-todo 'left)))
  18789. ((or (and org-support-shift-select
  18790. (not (eq org-support-shift-select 'always))
  18791. (org-at-item-bullet-p))
  18792. (and (not org-support-shift-select) (org-at-item-p)))
  18793. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  18794. ((and (not (eq org-support-shift-select 'always))
  18795. (org-at-property-p))
  18796. (call-interactively 'org-property-previous-allowed-value))
  18797. ((org-clocktable-try-shift 'left arg))
  18798. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  18799. (org-support-shift-select
  18800. (org-call-for-shift-select 'backward-char))
  18801. (t (org-shiftselect-error))))
  18802. (defun org-shiftcontrolright ()
  18803. "Switch to next TODO set."
  18804. (interactive)
  18805. (cond
  18806. ((and org-support-shift-select (org-region-active-p))
  18807. (org-call-for-shift-select 'forward-word))
  18808. ((and (not (eq org-support-shift-select 'always))
  18809. (org-at-heading-p))
  18810. (org-call-with-arg 'org-todo 'nextset))
  18811. (org-support-shift-select
  18812. (org-call-for-shift-select 'forward-word))
  18813. (t (org-shiftselect-error))))
  18814. (defun org-shiftcontrolleft ()
  18815. "Switch to previous TODO set."
  18816. (interactive)
  18817. (cond
  18818. ((and org-support-shift-select (org-region-active-p))
  18819. (org-call-for-shift-select 'backward-word))
  18820. ((and (not (eq org-support-shift-select 'always))
  18821. (org-at-heading-p))
  18822. (org-call-with-arg 'org-todo 'previousset))
  18823. (org-support-shift-select
  18824. (org-call-for-shift-select 'backward-word))
  18825. (t (org-shiftselect-error))))
  18826. (defun org-shiftcontrolup (&optional n)
  18827. "Change timestamps synchronously up in CLOCK log lines.
  18828. Optional argument N tells to change by that many units."
  18829. (interactive "P")
  18830. (if (and (org-at-clock-log-p) (org-at-timestamp-p t))
  18831. (let (org-support-shift-select)
  18832. (org-clock-timestamps-up n))
  18833. (user-error "Not at a clock log")))
  18834. (defun org-shiftcontroldown (&optional n)
  18835. "Change timestamps synchronously down in CLOCK log lines.
  18836. Optional argument N tells to change by that many units."
  18837. (interactive "P")
  18838. (if (and (org-at-clock-log-p) (org-at-timestamp-p t))
  18839. (let (org-support-shift-select)
  18840. (org-clock-timestamps-down n))
  18841. (user-error "Not at a clock log")))
  18842. (defun org-increase-number-at-point (&optional inc)
  18843. "Increment the number at point.
  18844. With an optional prefix numeric argument INC, increment using
  18845. this numeric value."
  18846. (interactive "p")
  18847. (if (not (number-at-point))
  18848. (user-error "Not on a number")
  18849. (unless inc (setq inc 1))
  18850. (let ((pos (point))
  18851. (beg (skip-chars-backward "-+^/*0-9eE."))
  18852. (end (skip-chars-forward "-+^/*0-9eE^.")) nap)
  18853. (setq nap (buffer-substring-no-properties
  18854. (+ pos beg) (+ pos beg end)))
  18855. (delete-region (+ pos beg) (+ pos beg end))
  18856. (insert (calc-eval (concat (number-to-string inc) "+" nap))))
  18857. (when (org-at-table-p)
  18858. (org-table-align)
  18859. (org-table-end-of-field 1))))
  18860. (defun org-decrease-number-at-point (&optional inc)
  18861. "Decrement the number at point.
  18862. With an optional prefix numeric argument INC, decrement using
  18863. this numeric value."
  18864. (interactive "p")
  18865. (org-increase-number-at-point (- inc)))
  18866. (defun org-ctrl-c-ret ()
  18867. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  18868. (interactive)
  18869. (cond
  18870. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  18871. (t (call-interactively 'org-insert-heading))))
  18872. (defun org-find-visible ()
  18873. (let ((s (point)))
  18874. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  18875. (get-char-property s 'invisible)))
  18876. s))
  18877. (defun org-find-invisible ()
  18878. (let ((s (point)))
  18879. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  18880. (not (get-char-property s 'invisible))))
  18881. s))
  18882. (defun org-copy-visible (beg end)
  18883. "Copy the visible parts of the region."
  18884. (interactive "r")
  18885. (let (snippets s)
  18886. (save-excursion
  18887. (save-restriction
  18888. (narrow-to-region beg end)
  18889. (setq s (goto-char (point-min)))
  18890. (while (not (= (point) (point-max)))
  18891. (goto-char (org-find-invisible))
  18892. (push (buffer-substring s (point)) snippets)
  18893. (setq s (goto-char (org-find-visible))))))
  18894. (kill-new (apply 'concat (nreverse snippets)))))
  18895. (defun org-copy-special ()
  18896. "Copy region in table or copy current subtree.
  18897. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  18898. See the individual commands for more information."
  18899. (interactive)
  18900. (call-interactively
  18901. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  18902. (defun org-cut-special ()
  18903. "Cut region in table or cut current subtree.
  18904. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  18905. See the individual commands for more information."
  18906. (interactive)
  18907. (call-interactively
  18908. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  18909. (defun org-paste-special (arg)
  18910. "Paste rectangular region into table, or past subtree relative to level.
  18911. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  18912. See the individual commands for more information."
  18913. (interactive "P")
  18914. (if (org-at-table-p)
  18915. (org-table-paste-rectangle)
  18916. (org-paste-subtree arg)))
  18917. (defsubst org-in-fixed-width-region-p ()
  18918. "Is point in a fixed-width region?"
  18919. (save-match-data
  18920. (eq 'fixed-width (org-element-type (org-element-at-point)))))
  18921. (defun org-edit-special (&optional arg)
  18922. "Call a special editor for the element at point.
  18923. When at a table, call the formula editor with `org-table-edit-formulas'.
  18924. When in a source code block, call `org-edit-src-code'.
  18925. When in a fixed-width region, call `org-edit-fixed-width-region'.
  18926. When in an export block, call `org-edit-export-block'.
  18927. When at an #+INCLUDE keyword, visit the included file.
  18928. On a link, call `ffap' to visit the link at point.
  18929. Otherwise, return a user error."
  18930. (interactive "P")
  18931. (let ((element (org-element-at-point)))
  18932. (assert (not buffer-read-only) nil
  18933. "Buffer is read-only: %s" (buffer-name))
  18934. (case (org-element-type element)
  18935. (src-block
  18936. (if (not arg) (org-edit-src-code)
  18937. (let* ((info (org-babel-get-src-block-info))
  18938. (lang (nth 0 info))
  18939. (params (nth 2 info))
  18940. (session (cdr (assq :session params))))
  18941. (if (not session) (org-edit-src-code)
  18942. ;; At a src-block with a session and function called with
  18943. ;; an ARG: switch to the buffer related to the inferior
  18944. ;; process.
  18945. (switch-to-buffer
  18946. (funcall (intern (concat "org-babel-prep-session:" lang))
  18947. session params))))))
  18948. (keyword
  18949. (if (member (org-element-property :key element) '("INCLUDE" "SETUPFILE"))
  18950. (org-open-link-from-string
  18951. (format "[[%s]]"
  18952. (expand-file-name
  18953. (let ((value (org-element-property :value element)))
  18954. (cond ((not (org-string-nw-p value))
  18955. (user-error "No file to edit"))
  18956. ((string-match "\\`\"\\(.*?\\)\"" value)
  18957. (match-string 1 value))
  18958. ((string-match "\\`[^ \t\"]\\S-*" value)
  18959. (match-string 0 value))
  18960. (t (user-error "No valid file specified")))))))
  18961. (user-error "No special environment to edit here")))
  18962. (table
  18963. (if (eq (org-element-property :type element) 'table.el)
  18964. (org-edit-table.el)
  18965. (call-interactively 'org-table-edit-formulas)))
  18966. ;; Only Org tables contain `table-row' type elements.
  18967. (table-row (call-interactively 'org-table-edit-formulas))
  18968. ((example-block src-block) (org-edit-src-code))
  18969. (export-block (org-edit-export-block))
  18970. (fixed-width (org-edit-fixed-width-region))
  18971. (otherwise
  18972. ;; No notable element at point. Though, we may be at a link,
  18973. ;; which is an object. Thus, scan deeper.
  18974. (if (eq (org-element-type (org-element-context element)) 'link)
  18975. (call-interactively 'ffap)
  18976. (user-error "No special environment to edit here"))))))
  18977. (defvar org-table-coordinate-overlays) ; defined in org-table.el
  18978. (defun org-ctrl-c-ctrl-c (&optional arg)
  18979. "Set tags in headline, or update according to changed information at point.
  18980. This command does many different things, depending on context:
  18981. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  18982. this is what we do.
  18983. - If the cursor is on a statistics cookie, update it.
  18984. - If the cursor is in a headline, prompt for tags and insert them
  18985. into the current line, aligned to `org-tags-column'. When called
  18986. with prefix arg, realign all tags in the current buffer.
  18987. - If the cursor is in one of the special #+KEYWORD lines, this
  18988. triggers scanning the buffer for these lines and updating the
  18989. information.
  18990. - If the cursor is inside a table, realign the table. This command
  18991. works even if the automatic table editor has been turned off.
  18992. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  18993. the entire table.
  18994. - If the cursor is at a footnote reference or definition, jump to
  18995. the corresponding definition or references, respectively.
  18996. - If the cursor is a the beginning of a dynamic block, update it.
  18997. - If the current buffer is a capture buffer, close note and file it.
  18998. - If the cursor is on a <<<target>>>, update radio targets and
  18999. corresponding links in this buffer.
  19000. - If the cursor is on a numbered item in a plain list, renumber the
  19001. ordered list.
  19002. - If the cursor is on a checkbox, toggle it.
  19003. - If the cursor is on a code block, evaluate it. The variable
  19004. `org-confirm-babel-evaluate' can be used to control prompting
  19005. before code block evaluation, by default every code block
  19006. evaluation requires confirmation. Code block evaluation can be
  19007. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  19008. (interactive "P")
  19009. (cond
  19010. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  19011. org-occur-highlights)
  19012. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  19013. (org-remove-occur-highlights)
  19014. (message "Temporary highlights/overlays removed from current buffer"))
  19015. ((and (local-variable-p 'org-finish-function (current-buffer))
  19016. (fboundp org-finish-function))
  19017. (funcall org-finish-function))
  19018. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  19019. (t
  19020. (let* ((context (org-element-context)) (type (org-element-type context)))
  19021. ;; Test if point is within a blank line.
  19022. (if (save-excursion (beginning-of-line) (looking-at "[ \t]*$"))
  19023. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  19024. (user-error "C-c C-c can do nothing useful at this location"))
  19025. (case type
  19026. ;; When at a link, act according to the parent instead.
  19027. (link (setq context (org-element-property :parent context))
  19028. (setq type (org-element-type context)))
  19029. ;; Unsupported object types: refer to the first supported
  19030. ;; element or object containing it.
  19031. ((bold code entity export-snippet inline-babel-call inline-src-block
  19032. italic latex-fragment line-break macro strike-through subscript
  19033. superscript underline verbatim)
  19034. (while (and (setq context (org-element-property :parent context))
  19035. (not (memq (setq type (org-element-type context))
  19036. '(radio-target paragraph verse-block
  19037. table-cell)))))))
  19038. ;; For convenience: at the first line of a paragraph on the
  19039. ;; same line as an item, apply function on that item instead.
  19040. (when (eq type 'paragraph)
  19041. (let ((parent (org-element-property :parent context)))
  19042. (when (and (eq (org-element-type parent) 'item)
  19043. (= (point-at-bol) (org-element-property :begin parent)))
  19044. (setq context parent type 'item))))
  19045. ;; Act according to type of element or object at point.
  19046. (case type
  19047. (clock (org-clock-update-time-maybe))
  19048. (dynamic-block
  19049. (save-excursion
  19050. (goto-char (org-element-property :post-affiliated context))
  19051. (org-update-dblock)))
  19052. (footnote-definition
  19053. (goto-char (org-element-property :post-affiliated context))
  19054. (call-interactively 'org-footnote-action))
  19055. (footnote-reference (call-interactively 'org-footnote-action))
  19056. ((headline inlinetask)
  19057. (save-excursion (goto-char (org-element-property :begin context))
  19058. (call-interactively 'org-set-tags)))
  19059. (item
  19060. ;; At an item: a double C-u set checkbox to "[-]"
  19061. ;; unconditionally, whereas a single one will toggle its
  19062. ;; presence. Without an universal argument, if the item
  19063. ;; has a checkbox, toggle it. Otherwise repair the list.
  19064. (let* ((box (org-element-property :checkbox context))
  19065. (struct (org-element-property :structure context))
  19066. (old-struct (copy-tree struct))
  19067. (parents (org-list-parents-alist struct))
  19068. (prevs (org-list-prevs-alist struct))
  19069. (orderedp (org-not-nil (org-entry-get nil "ORDERED"))))
  19070. (org-list-set-checkbox
  19071. (org-element-property :begin context) struct
  19072. (cond ((equal arg '(16)) "[-]")
  19073. ((and (not box) (equal arg '(4))) "[ ]")
  19074. ((or (not box) (equal arg '(4))) nil)
  19075. ((eq box 'on) "[ ]")
  19076. (t "[X]")))
  19077. ;; Mimic `org-list-write-struct' but with grabbing
  19078. ;; a return value from `org-list-struct-fix-box'.
  19079. (org-list-struct-fix-ind struct parents 2)
  19080. (org-list-struct-fix-item-end struct)
  19081. (org-list-struct-fix-bul struct prevs)
  19082. (org-list-struct-fix-ind struct parents)
  19083. (let ((block-item
  19084. (org-list-struct-fix-box struct parents prevs orderedp)))
  19085. (if (and box (equal struct old-struct))
  19086. (if (equal arg '(16))
  19087. (message "Checkboxes already reset")
  19088. (user-error "Cannot toggle this checkbox: %s"
  19089. (if (eq box 'on)
  19090. "all subitems checked"
  19091. "unchecked subitems")))
  19092. (org-list-struct-apply-struct struct old-struct)
  19093. (org-update-checkbox-count-maybe))
  19094. (when block-item
  19095. (message "Checkboxes were removed due to empty box at line %d"
  19096. (org-current-line block-item))))))
  19097. (keyword
  19098. (let ((org-inhibit-startup-visibility-stuff t)
  19099. (org-startup-align-all-tables nil))
  19100. (when (boundp 'org-table-coordinate-overlays)
  19101. (mapc 'delete-overlay org-table-coordinate-overlays)
  19102. (setq org-table-coordinate-overlays nil))
  19103. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  19104. (message "Local setup has been refreshed"))
  19105. (plain-list
  19106. ;; At a plain list, with a double C-u argument, set
  19107. ;; checkboxes of each item to "[-]", whereas a single one
  19108. ;; will toggle their presence according to the state of the
  19109. ;; first item in the list. Without an argument, repair the
  19110. ;; list.
  19111. (let* ((begin (org-element-property :contents-begin context))
  19112. (beginm (move-marker (make-marker) begin))
  19113. (struct (org-element-property :structure context))
  19114. (old-struct (copy-tree struct))
  19115. (first-box (save-excursion
  19116. (goto-char begin)
  19117. (looking-at org-list-full-item-re)
  19118. (match-string-no-properties 3)))
  19119. (new-box (cond ((equal arg '(16)) "[-]")
  19120. ((equal arg '(4)) (unless first-box "[ ]"))
  19121. ((equal first-box "[X]") "[ ]")
  19122. (t "[X]"))))
  19123. (cond
  19124. (arg
  19125. (mapc (lambda (pos) (org-list-set-checkbox pos struct new-box))
  19126. (org-list-get-all-items
  19127. begin struct (org-list-prevs-alist struct))))
  19128. ((and first-box (eq (point) begin))
  19129. ;; For convenience, when point is at bol on the first
  19130. ;; item of the list and no argument is provided, simply
  19131. ;; toggle checkbox of that item, if any.
  19132. (org-list-set-checkbox begin struct new-box)))
  19133. (org-list-write-struct
  19134. struct (org-list-parents-alist struct) old-struct)
  19135. (org-update-checkbox-count-maybe)
  19136. (save-excursion (goto-char beginm) (org-list-send-list 'maybe))))
  19137. ((property-drawer node-property)
  19138. (call-interactively 'org-property-action))
  19139. ((radio-target target)
  19140. (call-interactively 'org-update-radio-target-regexp))
  19141. (statistics-cookie
  19142. (call-interactively 'org-update-statistics-cookies))
  19143. ((table table-cell table-row)
  19144. ;; At a table, recalculate every field and align it. Also
  19145. ;; send the table if necessary. If the table has
  19146. ;; a `table.el' type, just give up. At a table row or
  19147. ;; cell, maybe recalculate line but always align table.
  19148. (if (eq (org-element-property :type context) 'table.el)
  19149. (message "Use C-c ' to edit table.el tables")
  19150. (let ((org-enable-table-editor t))
  19151. (if (or (eq type 'table)
  19152. ;; Check if point is at a TBLFM line.
  19153. (and (eq type 'table-row)
  19154. (= (point) (org-element-property :end context))))
  19155. (save-excursion
  19156. (if (org-at-TBLFM-p)
  19157. (progn (require 'org-table)
  19158. (org-table-calc-current-TBLFM))
  19159. (goto-char (org-element-property :contents-begin context))
  19160. (org-call-with-arg 'org-table-recalculate (or arg t))
  19161. (orgtbl-send-table 'maybe)))
  19162. (org-table-maybe-eval-formula)
  19163. (cond (arg (call-interactively 'org-table-recalculate))
  19164. ((org-table-maybe-recalculate-line))
  19165. (t (org-table-align)))))))
  19166. (timestamp (org-timestamp-change 0 'day))
  19167. (otherwise
  19168. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  19169. (user-error
  19170. "C-c C-c can do nothing useful at this location")))))))))
  19171. (defun org-mode-restart ()
  19172. (interactive)
  19173. (let ((indent-status (org-bound-and-true-p org-indent-mode)))
  19174. (funcall major-mode)
  19175. (hack-local-variables)
  19176. (when (and indent-status (not (org-bound-and-true-p org-indent-mode)))
  19177. (org-indent-mode -1)))
  19178. (message "%s restarted" major-mode))
  19179. (defun org-kill-note-or-show-branches ()
  19180. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  19181. (interactive)
  19182. (if (not org-finish-function)
  19183. (progn
  19184. (hide-subtree)
  19185. (call-interactively 'show-branches))
  19186. (let ((org-note-abort t))
  19187. (funcall org-finish-function))))
  19188. (defun org-open-line (n)
  19189. "Insert a new row in tables, call `open-line' elsewhere.
  19190. If `org-special-ctrl-o' is nil, just call `open-line' everywhere."
  19191. (interactive "*p")
  19192. (cond
  19193. ((not org-special-ctrl-o)
  19194. (open-line n))
  19195. ((org-at-table-p)
  19196. (org-table-insert-row))
  19197. (t
  19198. (open-line n))))
  19199. (defun org-return (&optional indent)
  19200. "Goto next table row or insert a newline.
  19201. Calls `org-table-next-row' or `newline', depending on context.
  19202. When optional INDENT argument is non-nil, call
  19203. `newline-and-indent' instead of `newline'.
  19204. When `org-return-follows-link' is non-nil and point is on
  19205. a timestamp or a link, call `org-open-at-point'. However, it
  19206. will not happen if point is in a table or on a \"dead\"
  19207. object (e.g., within a comment). In these case, you need to use
  19208. `org-open-at-point' directly."
  19209. (interactive)
  19210. (if (and (save-excursion
  19211. (beginning-of-line)
  19212. (looking-at org-todo-line-regexp))
  19213. (match-beginning 3)
  19214. (>= (point) (match-beginning 3)))
  19215. ;; Point is on headline tags. Do not break them: add a newline
  19216. ;; after the headline instead.
  19217. (progn (org-show-entry)
  19218. (end-of-line)
  19219. (if indent (newline-and-indent) (newline)))
  19220. (let* ((context (if org-return-follows-link (org-element-context)
  19221. (org-element-at-point)))
  19222. (type (org-element-type context)))
  19223. (cond
  19224. ;; In a table, call `org-table-next-row'.
  19225. ((or (and (eq type 'table)
  19226. (>= (point) (org-element-property :contents-begin context))
  19227. (< (point) (org-element-property :contents-end context)))
  19228. (org-element-lineage context '(table-row table-cell) t))
  19229. (org-table-justify-field-maybe)
  19230. (call-interactively #'org-table-next-row))
  19231. ;; On a link or a timestamp but not on white spaces after it,
  19232. ;; call `org-open-line' if `org-return-follows-link' allows it.
  19233. ((and org-return-follows-link
  19234. (memq type '(link timestamp))
  19235. (< (point)
  19236. (save-excursion (goto-char (org-element-property :end context))
  19237. (skip-chars-backward " \t")
  19238. (point))))
  19239. (call-interactively #'org-open-at-point))
  19240. ;; In a list, make sure indenting keeps trailing text within.
  19241. ((and indent
  19242. (not (eolp))
  19243. (org-element-lineage context '(item plain-list) t))
  19244. (let ((trailing-data
  19245. (delete-and-extract-region (point) (line-end-position))))
  19246. (newline-and-indent)
  19247. (save-excursion (insert trailing-data))))
  19248. (t (if indent (newline-and-indent) (newline)))))))
  19249. (defun org-return-indent ()
  19250. "Goto next table row or insert a newline and indent.
  19251. Calls `org-table-next-row' or `newline-and-indent', depending on
  19252. context. See the individual commands for more information."
  19253. (interactive)
  19254. (org-return t))
  19255. (defun org-ctrl-c-star ()
  19256. "Compute table, or change heading status of lines.
  19257. Calls `org-table-recalculate' or `org-toggle-heading',
  19258. depending on context."
  19259. (interactive)
  19260. (cond
  19261. ((org-at-table-p)
  19262. (call-interactively 'org-table-recalculate))
  19263. (t
  19264. ;; Convert all lines in region to list items
  19265. (call-interactively 'org-toggle-heading))))
  19266. (defun org-ctrl-c-minus ()
  19267. "Insert separator line in table or modify bullet status of line.
  19268. Also turns a plain line or a region of lines into list items.
  19269. Calls `org-table-insert-hline', `org-toggle-item', or
  19270. `org-cycle-list-bullet', depending on context."
  19271. (interactive)
  19272. (cond
  19273. ((org-at-table-p)
  19274. (call-interactively 'org-table-insert-hline))
  19275. ((org-region-active-p)
  19276. (call-interactively 'org-toggle-item))
  19277. ((org-in-item-p)
  19278. (call-interactively 'org-cycle-list-bullet))
  19279. (t
  19280. (call-interactively 'org-toggle-item))))
  19281. (defun org-toggle-item (arg)
  19282. "Convert headings or normal lines to items, items to normal lines.
  19283. If there is no active region, only the current line is considered.
  19284. If the first non blank line in the region is a headline, convert
  19285. all headlines to items, shifting text accordingly.
  19286. If it is an item, convert all items to normal lines.
  19287. If it is normal text, change region into a list of items.
  19288. With a prefix argument ARG, change the region in a single item."
  19289. (interactive "P")
  19290. (let ((shift-text
  19291. (function
  19292. ;; Shift text in current section to IND, from point to END.
  19293. ;; The function leaves point to END line.
  19294. (lambda (ind end)
  19295. (let ((min-i 1000) (end (copy-marker end)))
  19296. ;; First determine the minimum indentation (MIN-I) of
  19297. ;; the text.
  19298. (save-excursion
  19299. (catch 'exit
  19300. (while (< (point) end)
  19301. (let ((i (org-get-indentation)))
  19302. (cond
  19303. ;; Skip blank lines and inline tasks.
  19304. ((looking-at "^[ \t]*$"))
  19305. ((looking-at org-outline-regexp-bol))
  19306. ;; We can't find less than 0 indentation.
  19307. ((zerop i) (throw 'exit (setq min-i 0)))
  19308. ((< i min-i) (setq min-i i))))
  19309. (forward-line))))
  19310. ;; Then indent each line so that a line indented to
  19311. ;; MIN-I becomes indented to IND. Ignore blank lines
  19312. ;; and inline tasks in the process.
  19313. (let ((delta (- ind min-i)))
  19314. (while (< (point) end)
  19315. (unless (or (looking-at "^[ \t]*$")
  19316. (looking-at org-outline-regexp-bol))
  19317. (org-indent-line-to (+ (org-get-indentation) delta)))
  19318. (forward-line)))))))
  19319. (skip-blanks
  19320. (function
  19321. ;; Return beginning of first non-blank line, starting from
  19322. ;; line at POS.
  19323. (lambda (pos)
  19324. (save-excursion
  19325. (goto-char pos)
  19326. (skip-chars-forward " \r\t\n")
  19327. (point-at-bol)))))
  19328. beg end)
  19329. ;; Determine boundaries of changes.
  19330. (if (org-region-active-p)
  19331. (setq beg (funcall skip-blanks (region-beginning))
  19332. end (copy-marker (region-end)))
  19333. (setq beg (funcall skip-blanks (point-at-bol))
  19334. end (copy-marker (point-at-eol))))
  19335. ;; Depending on the starting line, choose an action on the text
  19336. ;; between BEG and END.
  19337. (org-with-limited-levels
  19338. (save-excursion
  19339. (goto-char beg)
  19340. (cond
  19341. ;; Case 1. Start at an item: de-itemize. Note that it only
  19342. ;; happens when a region is active: `org-ctrl-c-minus'
  19343. ;; would call `org-cycle-list-bullet' otherwise.
  19344. ((org-at-item-p)
  19345. (while (< (point) end)
  19346. (when (org-at-item-p)
  19347. (skip-chars-forward " \t")
  19348. (delete-region (point) (match-end 0)))
  19349. (forward-line)))
  19350. ;; Case 2. Start at an heading: convert to items.
  19351. ((org-at-heading-p)
  19352. (let* ((bul (org-list-bullet-string "-"))
  19353. (bul-len (length bul))
  19354. (done (org-entry-is-done-p))
  19355. (todo (org-entry-is-todo-p))
  19356. ;; Indentation of the first heading. It should be
  19357. ;; relative to the indentation of its parent, if any.
  19358. (start-ind (save-excursion
  19359. (cond
  19360. ((not org-adapt-indentation) 0)
  19361. ((not (outline-previous-heading)) 0)
  19362. (t (length (match-string 0))))))
  19363. ;; Level of first heading. Further headings will be
  19364. ;; compared to it to determine hierarchy in the list.
  19365. (ref-level (org-reduced-level (org-outline-level))))
  19366. (when (or done todo) (org-todo ""))
  19367. (while (< (point) end)
  19368. (let* ((level (org-reduced-level (org-outline-level)))
  19369. (delta (max 0 (- level ref-level))))
  19370. ;; If current headline is less indented than the first
  19371. ;; one, set it as reference, in order to preserve
  19372. ;; subtrees.
  19373. (when (< level ref-level) (setq ref-level level))
  19374. (replace-match bul t t)
  19375. (org-indent-line-to (+ start-ind (* delta bul-len)))
  19376. (when (or done todo)
  19377. (let* ((struct (org-list-struct))
  19378. (old (copy-tree struct)))
  19379. (org-list-set-checkbox (line-beginning-position)
  19380. struct
  19381. (if done "[X]" "[ ]"))
  19382. (org-list-write-struct struct
  19383. (org-list-parents-alist struct)
  19384. old)))
  19385. ;; Ensure all text down to END (or SECTION-END) belongs
  19386. ;; to the newly created item.
  19387. (let ((section-end (save-excursion
  19388. (or (outline-next-heading) (point)))))
  19389. (forward-line)
  19390. (funcall shift-text
  19391. (+ start-ind (* (1+ delta) bul-len))
  19392. (min end section-end)))))))
  19393. ;; Case 3. Normal line with ARG: make the first line of region
  19394. ;; an item, and shift indentation of others lines to
  19395. ;; set them as item's body.
  19396. (arg (let* ((bul (org-list-bullet-string "-"))
  19397. (bul-len (length bul))
  19398. (ref-ind (org-get-indentation)))
  19399. (skip-chars-forward " \t")
  19400. (insert bul)
  19401. (forward-line)
  19402. (while (< (point) end)
  19403. ;; Ensure that lines less indented than first one
  19404. ;; still get included in item body.
  19405. (funcall shift-text
  19406. (+ ref-ind bul-len)
  19407. (min end (save-excursion (or (outline-next-heading)
  19408. (point)))))
  19409. (forward-line))))
  19410. ;; Case 4. Normal line without ARG: turn each non-item line
  19411. ;; into an item.
  19412. (t
  19413. (while (< (point) end)
  19414. (unless (or (org-at-heading-p) (org-at-item-p))
  19415. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  19416. (replace-match
  19417. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  19418. (forward-line))))))))
  19419. (defun org-toggle-heading (&optional nstars)
  19420. "Convert headings to normal text, or items or text to headings.
  19421. If there is no active region, only convert the current line.
  19422. With a \\[universal-argument] prefix, convert the whole list at
  19423. point into heading.
  19424. In a region:
  19425. - If the first non blank line is a headline, remove the stars
  19426. from all headlines in the region.
  19427. - If it is a normal line, turn each and every normal line (i.e.,
  19428. not an heading or an item) in the region into headings. If you
  19429. want to convert only the first line of this region, use one
  19430. universal prefix argument.
  19431. - If it is a plain list item, turn all plain list items into headings.
  19432. When converting a line into a heading, the number of stars is chosen
  19433. such that the lines become children of the current entry. However,
  19434. when a numeric prefix argument is given, its value determines the
  19435. number of stars to add."
  19436. (interactive "P")
  19437. (let ((skip-blanks
  19438. (function
  19439. ;; Return beginning of first non-blank line, starting from
  19440. ;; line at POS.
  19441. (lambda (pos)
  19442. (save-excursion
  19443. (goto-char pos)
  19444. (while (org-at-comment-p) (forward-line))
  19445. (skip-chars-forward " \r\t\n")
  19446. (point-at-bol)))))
  19447. beg end toggled)
  19448. ;; Determine boundaries of changes. If a universal prefix has
  19449. ;; been given, put the list in a region. If region ends at a bol,
  19450. ;; do not consider the last line to be in the region.
  19451. (when (and current-prefix-arg (org-at-item-p))
  19452. (if (listp current-prefix-arg) (setq current-prefix-arg 1))
  19453. (org-mark-element))
  19454. (if (org-region-active-p)
  19455. (setq beg (funcall skip-blanks (region-beginning))
  19456. end (copy-marker (save-excursion
  19457. (goto-char (region-end))
  19458. (if (bolp) (point) (point-at-eol)))))
  19459. (setq beg (funcall skip-blanks (point-at-bol))
  19460. end (copy-marker (point-at-eol))))
  19461. ;; Ensure inline tasks don't count as headings.
  19462. (org-with-limited-levels
  19463. (save-excursion
  19464. (goto-char beg)
  19465. (cond
  19466. ;; Case 1. Started at an heading: de-star headings.
  19467. ((org-at-heading-p)
  19468. (while (< (point) end)
  19469. (when (org-at-heading-p t)
  19470. (looking-at org-outline-regexp) (replace-match "")
  19471. (setq toggled t))
  19472. (forward-line)))
  19473. ;; Case 2. Started at an item: change items into headlines.
  19474. ;; One star will be added by `org-list-to-subtree'.
  19475. ((org-at-item-p)
  19476. (let* ((stars (make-string
  19477. ;; subtract the star that will be added again by
  19478. ;; `org-list-to-subtree'
  19479. (if (numberp nstars) (1- nstars)
  19480. (or (org-current-level) 0))
  19481. ?*))
  19482. (add-stars
  19483. (cond (nstars "") ; stars from prefix only
  19484. ((equal stars "") "") ; before first heading
  19485. (org-odd-levels-only "*") ; inside heading, odd
  19486. (t "")))) ; inside heading, oddeven
  19487. (while (< (point) end)
  19488. (when (org-at-item-p)
  19489. ;; Pay attention to cases when region ends before list.
  19490. (let* ((struct (org-list-struct))
  19491. (list-end (min (org-list-get-bottom-point struct) (1+ end))))
  19492. (save-restriction
  19493. (narrow-to-region (point) list-end)
  19494. (insert
  19495. (org-list-to-subtree
  19496. (org-list-parse-list t)
  19497. '(:istart (concat stars add-stars (funcall get-stars depth))
  19498. :icount (concat stars add-stars (funcall get-stars depth)))))))
  19499. (setq toggled t))
  19500. (forward-line))))
  19501. ;; Case 3. Started at normal text: make every line an heading,
  19502. ;; skipping headlines and items.
  19503. (t (let* ((stars
  19504. (make-string
  19505. (if (numberp nstars) nstars (or (org-current-level) 0)) ?*))
  19506. (add-stars
  19507. (cond (nstars "") ; stars from prefix only
  19508. ((equal stars "") "*") ; before first heading
  19509. (org-odd-levels-only "**") ; inside heading, odd
  19510. (t "*"))) ; inside heading, oddeven
  19511. (rpl (concat stars add-stars " "))
  19512. (lend (if (listp nstars) (save-excursion (end-of-line) (point)))))
  19513. (while (< (point) (if (equal nstars '(4)) lend end))
  19514. (when (and (not (or (org-at-heading-p) (org-at-item-p) (org-at-comment-p)))
  19515. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  19516. (replace-match (concat rpl (match-string 2))) (setq toggled t))
  19517. (forward-line)))))))
  19518. (unless toggled (message "Cannot toggle heading from here"))))
  19519. (defun org-meta-return (&optional arg)
  19520. "Insert a new heading or wrap a region in a table.
  19521. Calls `org-insert-heading' or `org-table-wrap-region', depending
  19522. on context. See the individual commands for more information."
  19523. (interactive "P")
  19524. (org-check-before-invisible-edit 'insert)
  19525. (or (run-hook-with-args-until-success 'org-metareturn-hook)
  19526. (let* ((element (org-element-at-point))
  19527. (type (org-element-type element)))
  19528. (when (eq type 'table-row)
  19529. (setq element (org-element-property :parent element))
  19530. (setq type 'table))
  19531. (if (and (eq type 'table)
  19532. (eq (org-element-property :type element) 'org)
  19533. (>= (point) (org-element-property :contents-begin element))
  19534. (< (point) (org-element-property :contents-end element)))
  19535. (call-interactively 'org-table-wrap-region)
  19536. (call-interactively 'org-insert-heading)))))
  19537. ;;; Menu entries
  19538. (defsubst org-in-subtree-not-table-p ()
  19539. "Are we in a subtree and not in a table?"
  19540. (and (not (org-before-first-heading-p))
  19541. (not (org-at-table-p))))
  19542. ;; Define the Org-mode menus
  19543. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  19544. '("Tbl"
  19545. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  19546. ["Next Field" org-cycle (org-at-table-p)]
  19547. ["Previous Field" org-shifttab (org-at-table-p)]
  19548. ["Next Row" org-return (org-at-table-p)]
  19549. "--"
  19550. ["Blank Field" org-table-blank-field (org-at-table-p)]
  19551. ["Edit Field" org-table-edit-field (org-at-table-p)]
  19552. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  19553. "--"
  19554. ("Column"
  19555. ["Move Column Left" org-metaleft (org-at-table-p)]
  19556. ["Move Column Right" org-metaright (org-at-table-p)]
  19557. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  19558. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  19559. ("Row"
  19560. ["Move Row Up" org-metaup (org-at-table-p)]
  19561. ["Move Row Down" org-metadown (org-at-table-p)]
  19562. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  19563. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  19564. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  19565. "--"
  19566. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  19567. ("Rectangle"
  19568. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  19569. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  19570. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  19571. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  19572. "--"
  19573. ("Calculate"
  19574. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  19575. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  19576. ["Edit Formulas" org-edit-special (org-at-table-p)]
  19577. "--"
  19578. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  19579. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  19580. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  19581. "--"
  19582. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  19583. "--"
  19584. ["Sum Column/Rectangle" org-table-sum
  19585. (or (org-at-table-p) (org-region-active-p))]
  19586. ["Which Column?" org-table-current-column (org-at-table-p)])
  19587. ["Debug Formulas"
  19588. org-table-toggle-formula-debugger
  19589. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  19590. ["Show Col/Row Numbers"
  19591. org-table-toggle-coordinate-overlays
  19592. :style toggle
  19593. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  19594. "--"
  19595. ["Create" org-table-create (and (not (org-at-table-p))
  19596. org-enable-table-editor)]
  19597. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  19598. ["Import from File" org-table-import (not (org-at-table-p))]
  19599. ["Export to File" org-table-export (org-at-table-p)]
  19600. "--"
  19601. ["Create/Convert from/to table.el" org-table-create-with-table.el t]
  19602. "--"
  19603. ("Plot"
  19604. ["Ascii plot" orgtbl-ascii-plot :active (org-at-table-p) :keys "C-c \" a"]
  19605. ["Gnuplot" org-plot/gnuplot :active (org-at-table-p) :keys "C-c \" g"])))
  19606. (easy-menu-define org-org-menu org-mode-map "Org menu"
  19607. '("Org"
  19608. ("Show/Hide"
  19609. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  19610. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  19611. ["Sparse Tree..." org-sparse-tree t]
  19612. ["Reveal Context" org-reveal t]
  19613. ["Show All" show-all t]
  19614. "--"
  19615. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  19616. "--"
  19617. ["New Heading" org-insert-heading t]
  19618. ("Navigate Headings"
  19619. ["Up" outline-up-heading t]
  19620. ["Next" outline-next-visible-heading t]
  19621. ["Previous" outline-previous-visible-heading t]
  19622. ["Next Same Level" outline-forward-same-level t]
  19623. ["Previous Same Level" outline-backward-same-level t]
  19624. "--"
  19625. ["Jump" org-goto t])
  19626. ("Edit Structure"
  19627. ["Refile Subtree" org-refile (org-in-subtree-not-table-p)]
  19628. "--"
  19629. ["Move Subtree Up" org-metaup (org-at-heading-p)]
  19630. ["Move Subtree Down" org-metadown (org-at-heading-p)]
  19631. "--"
  19632. ["Copy Subtree" org-copy-special (org-in-subtree-not-table-p)]
  19633. ["Cut Subtree" org-cut-special (org-in-subtree-not-table-p)]
  19634. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  19635. "--"
  19636. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  19637. "--"
  19638. ["Copy visible text" org-copy-visible t]
  19639. "--"
  19640. ["Promote Heading" org-metaleft (org-in-subtree-not-table-p)]
  19641. ["Promote Subtree" org-shiftmetaleft (org-in-subtree-not-table-p)]
  19642. ["Demote Heading" org-metaright (org-in-subtree-not-table-p)]
  19643. ["Demote Subtree" org-shiftmetaright (org-in-subtree-not-table-p)]
  19644. "--"
  19645. ["Sort Region/Children" org-sort t]
  19646. "--"
  19647. ["Convert to odd levels" org-convert-to-odd-levels t]
  19648. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  19649. ("Editing"
  19650. ["Emphasis..." org-emphasize t]
  19651. ["Edit Source Example" org-edit-special t]
  19652. "--"
  19653. ["Footnote new/jump" org-footnote-action t]
  19654. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  19655. ("Archive"
  19656. ["Archive (default method)" org-archive-subtree-default (org-in-subtree-not-table-p)]
  19657. "--"
  19658. ["Move Subtree to Archive file" org-advertized-archive-subtree (org-in-subtree-not-table-p)]
  19659. ["Toggle ARCHIVE tag" org-toggle-archive-tag (org-in-subtree-not-table-p)]
  19660. ["Move subtree to Archive sibling" org-archive-to-archive-sibling (org-in-subtree-not-table-p)]
  19661. )
  19662. "--"
  19663. ("Hyperlinks"
  19664. ["Store Link (Global)" org-store-link t]
  19665. ["Find existing link to here" org-occur-link-in-agenda-files t]
  19666. ["Insert Link" org-insert-link t]
  19667. ["Follow Link" org-open-at-point t]
  19668. "--"
  19669. ["Next link" org-next-link t]
  19670. ["Previous link" org-previous-link t]
  19671. "--"
  19672. ["Descriptive Links"
  19673. org-toggle-link-display
  19674. :style radio
  19675. :selected org-descriptive-links
  19676. ]
  19677. ["Literal Links"
  19678. org-toggle-link-display
  19679. :style radio
  19680. :selected (not org-descriptive-links)])
  19681. "--"
  19682. ("TODO Lists"
  19683. ["TODO/DONE/-" org-todo t]
  19684. ("Select keyword"
  19685. ["Next keyword" org-shiftright (org-at-heading-p)]
  19686. ["Previous keyword" org-shiftleft (org-at-heading-p)]
  19687. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  19688. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
  19689. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
  19690. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  19691. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  19692. "--"
  19693. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  19694. :selected org-enforce-todo-dependencies :style toggle :active t]
  19695. "Settings for tree at point"
  19696. ["Do Children sequentially" org-toggle-ordered-property :style radio
  19697. :selected (org-entry-get nil "ORDERED")
  19698. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  19699. ["Do Children parallel" org-toggle-ordered-property :style radio
  19700. :selected (not (org-entry-get nil "ORDERED"))
  19701. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  19702. "--"
  19703. ["Set Priority" org-priority t]
  19704. ["Priority Up" org-shiftup t]
  19705. ["Priority Down" org-shiftdown t]
  19706. "--"
  19707. ["Get news from all feeds" org-feed-update-all t]
  19708. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  19709. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  19710. ("TAGS and Properties"
  19711. ["Set Tags" org-set-tags-command (not (org-before-first-heading-p))]
  19712. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  19713. "--"
  19714. ["Set property" org-set-property (not (org-before-first-heading-p))]
  19715. ["Column view of properties" org-columns t]
  19716. ["Insert Column View DBlock" org-insert-columns-dblock t])
  19717. ("Dates and Scheduling"
  19718. ["Timestamp" org-time-stamp (not (org-before-first-heading-p))]
  19719. ["Timestamp (inactive)" org-time-stamp-inactive (not (org-before-first-heading-p))]
  19720. ("Change Date"
  19721. ["1 Day Later" org-shiftright (org-at-timestamp-p)]
  19722. ["1 Day Earlier" org-shiftleft (org-at-timestamp-p)]
  19723. ["1 ... Later" org-shiftup (org-at-timestamp-p)]
  19724. ["1 ... Earlier" org-shiftdown (org-at-timestamp-p)])
  19725. ["Compute Time Range" org-evaluate-time-range t]
  19726. ["Schedule Item" org-schedule (not (org-before-first-heading-p))]
  19727. ["Deadline" org-deadline (not (org-before-first-heading-p))]
  19728. "--"
  19729. ["Custom time format" org-toggle-time-stamp-overlays
  19730. :style radio :selected org-display-custom-times]
  19731. "--"
  19732. ["Goto Calendar" org-goto-calendar t]
  19733. ["Date from Calendar" org-date-from-calendar t]
  19734. "--"
  19735. ["Start/Restart Timer" org-timer-start t]
  19736. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  19737. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  19738. ["Insert Timer String" org-timer t]
  19739. ["Insert Timer Item" org-timer-item t])
  19740. ("Logging work"
  19741. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  19742. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  19743. ["Clock out" org-clock-out t]
  19744. ["Clock cancel" org-clock-cancel t]
  19745. "--"
  19746. ["Mark as default task" org-clock-mark-default-task t]
  19747. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  19748. ["Goto running clock" org-clock-goto t]
  19749. "--"
  19750. ["Display times" org-clock-display t]
  19751. ["Create clock table" org-clock-report t]
  19752. "--"
  19753. ["Record DONE time"
  19754. (progn (setq org-log-done (not org-log-done))
  19755. (message "Switching to %s will %s record a timestamp"
  19756. (car org-done-keywords)
  19757. (if org-log-done "automatically" "not")))
  19758. :style toggle :selected org-log-done])
  19759. "--"
  19760. ["Agenda Command..." org-agenda t]
  19761. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  19762. ("File List for Agenda")
  19763. ("Special views current file"
  19764. ["TODO Tree" org-show-todo-tree t]
  19765. ["Check Deadlines" org-check-deadlines t]
  19766. ["Timeline" org-timeline t]
  19767. ["Tags/Property tree" org-match-sparse-tree t])
  19768. "--"
  19769. ["Export/Publish..." org-export-dispatch t]
  19770. ("LaTeX"
  19771. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  19772. :selected org-cdlatex-mode]
  19773. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  19774. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  19775. ["Modify math symbol" org-cdlatex-math-modify
  19776. (org-inside-LaTeX-fragment-p)]
  19777. ["Insert citation" org-reftex-citation t]
  19778. "--"
  19779. ["Template for BEAMER" (org-beamer-insert-options-template) t])
  19780. "--"
  19781. ("MobileOrg"
  19782. ["Push Files and Views" org-mobile-push t]
  19783. ["Get Captured and Flagged" org-mobile-pull t]
  19784. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  19785. "--"
  19786. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  19787. "--"
  19788. ("Documentation"
  19789. ["Show Version" org-version t]
  19790. ["Info Documentation" org-info t])
  19791. ("Customize"
  19792. ["Browse Org Group" org-customize t]
  19793. "--"
  19794. ["Expand This Menu" org-create-customize-menu
  19795. (fboundp 'customize-menu-create)])
  19796. ["Send bug report" org-submit-bug-report t]
  19797. "--"
  19798. ("Refresh/Reload"
  19799. ["Refresh setup current buffer" org-mode-restart t]
  19800. ["Reload Org (after update)" org-reload t]
  19801. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x !"])
  19802. ))
  19803. (defun org-info (&optional node)
  19804. "Read documentation for Org-mode in the info system.
  19805. With optional NODE, go directly to that node."
  19806. (interactive)
  19807. (info (format "(org)%s" (or node ""))))
  19808. ;;;###autoload
  19809. (defun org-submit-bug-report ()
  19810. "Submit a bug report on Org-mode via mail.
  19811. Don't hesitate to report any problems or inaccurate documentation.
  19812. If you don't have setup sending mail from (X)Emacs, please copy the
  19813. output buffer into your mail program, as it gives us important
  19814. information about your Org-mode version and configuration."
  19815. (interactive)
  19816. (require 'reporter)
  19817. (org-load-modules-maybe)
  19818. (org-require-autoloaded-modules)
  19819. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  19820. (reporter-submit-bug-report
  19821. "emacs-orgmode@gnu.org"
  19822. (org-version nil 'full)
  19823. (let (list)
  19824. (save-window-excursion
  19825. (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
  19826. (delete-other-windows)
  19827. (erase-buffer)
  19828. (insert "You are about to submit a bug report to the Org-mode mailing list.
  19829. We would like to add your full Org-mode and Outline configuration to the
  19830. bug report. This greatly simplifies the work of the maintainer and
  19831. other experts on the mailing list.
  19832. HOWEVER, some variables you have customized may contain private
  19833. information. The names of customers, colleagues, or friends, might
  19834. appear in the form of file names, tags, todo states, or search strings.
  19835. If you answer yes to the prompt, you might want to check and remove
  19836. such private information before sending the email.")
  19837. (add-text-properties (point-min) (point-max) '(face org-warning))
  19838. (when (yes-or-no-p "Include your Org-mode configuration ")
  19839. (mapatoms
  19840. (lambda (v)
  19841. (and (boundp v)
  19842. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  19843. (or (and (symbol-value v)
  19844. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  19845. (and
  19846. (get v 'custom-type) (get v 'standard-value)
  19847. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  19848. (push v list)))))
  19849. (kill-buffer (get-buffer "*Warn about privacy*"))
  19850. list))
  19851. nil nil
  19852. "Remember to cover the basics, that is, what you expected to happen and
  19853. what in fact did happen. You don't know how to make a good report? See
  19854. http://orgmode.org/manual/Feedback.html#Feedback
  19855. Your bug report will be posted to the Org-mode mailing list.
  19856. ------------------------------------------------------------------------")
  19857. (save-excursion
  19858. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  19859. (replace-match "\\1Bug: \\3 [\\2]")))))
  19860. (defun org-install-agenda-files-menu ()
  19861. (let ((bl (buffer-list)))
  19862. (save-excursion
  19863. (while bl
  19864. (set-buffer (pop bl))
  19865. (if (derived-mode-p 'org-mode) (setq bl nil)))
  19866. (when (derived-mode-p 'org-mode)
  19867. (easy-menu-change
  19868. '("Org") "File List for Agenda"
  19869. (append
  19870. (list
  19871. ["Edit File List" (org-edit-agenda-file-list) t]
  19872. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  19873. ["Remove Current File from List" org-remove-file t]
  19874. ["Cycle through agenda files" org-cycle-agenda-files t]
  19875. ["Occur in all agenda files" org-occur-in-agenda-files t]
  19876. "--")
  19877. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  19878. ;;;; Documentation
  19879. (defun org-require-autoloaded-modules ()
  19880. (interactive)
  19881. (mapc 'require
  19882. '(org-agenda org-archive org-attach org-clock org-colview org-id
  19883. org-table org-timer)))
  19884. ;;;###autoload
  19885. (defun org-reload (&optional uncompiled)
  19886. "Reload all org lisp files.
  19887. With prefix arg UNCOMPILED, load the uncompiled versions."
  19888. (interactive "P")
  19889. (require 'loadhist)
  19890. (let* ((org-dir (org-find-library-dir "org"))
  19891. (contrib-dir (or (org-find-library-dir "org-contribdir") org-dir))
  19892. (feature-re "^\\(org\\|ob\\|ox\\)\\(-.*\\)?")
  19893. (remove-re (mapconcat 'identity
  19894. (mapcar (lambda (f) (concat "^" f "$"))
  19895. (list (if (featurep 'xemacs)
  19896. "org-colview"
  19897. "org-colview-xemacs")
  19898. "org" "org-loaddefs" "org-version"))
  19899. "\\|"))
  19900. (feats (delete-dups
  19901. (mapcar 'file-name-sans-extension
  19902. (mapcar 'file-name-nondirectory
  19903. (delq nil
  19904. (mapcar 'feature-file
  19905. features))))))
  19906. (lfeat (append
  19907. (sort
  19908. (setq feats
  19909. (delq nil (mapcar
  19910. (lambda (f)
  19911. (if (and (string-match feature-re f)
  19912. (not (string-match remove-re f)))
  19913. f nil))
  19914. feats)))
  19915. 'string-lessp)
  19916. (list "org-version" "org")))
  19917. (load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  19918. (load-suffixes (if uncompiled (reverse load-suffixes) load-suffixes))
  19919. load-uncore load-misses)
  19920. (setq load-misses
  19921. (delq 't
  19922. (mapcar (lambda (f)
  19923. (or (org-load-noerror-mustsuffix (concat org-dir f))
  19924. (and (string= org-dir contrib-dir)
  19925. (org-load-noerror-mustsuffix (concat contrib-dir f)))
  19926. (and (org-load-noerror-mustsuffix (concat (org-find-library-dir f) f))
  19927. (add-to-list 'load-uncore f 'append)
  19928. 't)
  19929. f))
  19930. lfeat)))
  19931. (if load-uncore
  19932. (message "The following feature%s found in load-path, please check if that's correct:\n%s"
  19933. (if (> (length load-uncore) 1) "s were" " was") load-uncore))
  19934. (if load-misses
  19935. (message "Some error occurred while reloading Org feature%s\n%s\nPlease check *Messages*!\n%s"
  19936. (if (> (length load-misses) 1) "s" "") load-misses (org-version nil 'full))
  19937. (message "Successfully reloaded Org\n%s" (org-version nil 'full)))))
  19938. ;;;###autoload
  19939. (defun org-customize ()
  19940. "Call the customize function with org as argument."
  19941. (interactive)
  19942. (org-load-modules-maybe)
  19943. (org-require-autoloaded-modules)
  19944. (customize-browse 'org))
  19945. (defun org-create-customize-menu ()
  19946. "Create a full customization menu for Org-mode, insert it into the menu."
  19947. (interactive)
  19948. (org-load-modules-maybe)
  19949. (org-require-autoloaded-modules)
  19950. (if (fboundp 'customize-menu-create)
  19951. (progn
  19952. (easy-menu-change
  19953. '("Org") "Customize"
  19954. `(["Browse Org group" org-customize t]
  19955. "--"
  19956. ,(customize-menu-create 'org)
  19957. ["Set" Custom-set t]
  19958. ["Save" Custom-save t]
  19959. ["Reset to Current" Custom-reset-current t]
  19960. ["Reset to Saved" Custom-reset-saved t]
  19961. ["Reset to Standard Settings" Custom-reset-standard t]))
  19962. (message "\"Org\"-menu now contains full customization menu"))
  19963. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  19964. ;;;; Miscellaneous stuff
  19965. ;;; Generally useful functions
  19966. (defsubst org-get-at-eol (property n)
  19967. "Get text property PROPERTY at the end of line less N characters."
  19968. (get-text-property (- (point-at-eol) n) property))
  19969. (defun org-find-text-property-in-string (prop s)
  19970. "Return the first non-nil value of property PROP in string S."
  19971. (or (get-text-property 0 prop s)
  19972. (get-text-property (or (next-single-property-change 0 prop s) 0)
  19973. prop s)))
  19974. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  19975. "Display the given MESSAGE as a warning."
  19976. (if (fboundp 'display-warning)
  19977. (display-warning 'org message
  19978. (if (featurep 'xemacs) 'warning :warning))
  19979. (let ((buf (get-buffer-create "*Org warnings*")))
  19980. (with-current-buffer buf
  19981. (goto-char (point-max))
  19982. (insert "Warning (Org): " message)
  19983. (unless (bolp)
  19984. (newline)))
  19985. (display-buffer buf)
  19986. (sit-for 0))))
  19987. (defun org-eval (form)
  19988. "Eval FORM and return result."
  19989. (condition-case error
  19990. (eval form)
  19991. (error (format "%%![Error: %s]" error))))
  19992. (defun org-in-clocktable-p ()
  19993. "Check if the cursor is in a clocktable."
  19994. (let ((pos (point)) start)
  19995. (save-excursion
  19996. (end-of-line 1)
  19997. (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
  19998. (setq start (match-beginning 0))
  19999. (re-search-forward "^[ \t]*#\\+END:.*" nil t)
  20000. (>= (match-end 0) pos)
  20001. start))))
  20002. (defun org-in-verbatim-emphasis ()
  20003. (save-match-data
  20004. (and (org-in-regexp org-emph-re 2)
  20005. (>= (point) (match-beginning 3))
  20006. (<= (point) (match-end 4))
  20007. (member (match-string 3) '("=" "~")))))
  20008. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  20009. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  20010. (if (and marker (marker-buffer marker)
  20011. (buffer-live-p (marker-buffer marker)))
  20012. (progn
  20013. (org-pop-to-buffer-same-window (marker-buffer marker))
  20014. (if (or (> marker (point-max)) (< marker (point-min)))
  20015. (widen))
  20016. (goto-char marker)
  20017. (org-show-context 'org-goto))
  20018. (if bookmark
  20019. (bookmark-jump bookmark)
  20020. (error "Cannot find location"))))
  20021. (defun org-quote-csv-field (s)
  20022. "Quote field for inclusion in CSV material."
  20023. (if (string-match "[\",]" s)
  20024. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  20025. s))
  20026. (defun org-force-self-insert (N)
  20027. "Needed to enforce self-insert under remapping."
  20028. (interactive "p")
  20029. (self-insert-command N))
  20030. (defun org-string-width (s)
  20031. "Compute width of string, ignoring invisible characters.
  20032. This ignores character with invisibility property `org-link', and also
  20033. characters with property `org-cwidth', because these will become invisible
  20034. upon the next fontification round."
  20035. (let (b l)
  20036. (when (or (eq t buffer-invisibility-spec)
  20037. (assq 'org-link buffer-invisibility-spec))
  20038. (while (setq b (text-property-any 0 (length s)
  20039. 'invisible 'org-link s))
  20040. (setq s (concat (substring s 0 b)
  20041. (substring s (or (next-single-property-change
  20042. b 'invisible s) (length s)))))))
  20043. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  20044. (setq s (concat (substring s 0 b)
  20045. (substring s (or (next-single-property-change
  20046. b 'org-cwidth s) (length s))))))
  20047. (setq l (string-width s) b -1)
  20048. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  20049. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  20050. l))
  20051. (defun org-shorten-string (s maxlength)
  20052. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  20053. If the string is shorter or has length MAXLENGTH, just return the
  20054. original string. If it is longer, the functions finds a space in the
  20055. string, breaks this string off at that locations and adds three dots
  20056. as ellipsis. Including the ellipsis, the string will not be longer
  20057. than MAXLENGTH. If finding a good breaking point in the string does
  20058. not work, the string is just chopped off in the middle of a word
  20059. if necessary."
  20060. (if (<= (length s) maxlength)
  20061. s
  20062. (let* ((n (max (- maxlength 4) 1))
  20063. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  20064. (if (string-match re s)
  20065. (concat (match-string 1 s) "...")
  20066. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  20067. (defun org-get-indentation (&optional line)
  20068. "Get the indentation of the current line, interpreting tabs.
  20069. When LINE is given, assume it represents a line and compute its indentation."
  20070. (if line
  20071. (if (string-match "^ *" (org-remove-tabs line))
  20072. (match-end 0))
  20073. (save-excursion
  20074. (beginning-of-line 1)
  20075. (skip-chars-forward " \t")
  20076. (current-column))))
  20077. (defun org-get-string-indentation (s)
  20078. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  20079. (let ((n -1) (i 0) (w tab-width) c)
  20080. (catch 'exit
  20081. (while (< (setq n (1+ n)) (length s))
  20082. (setq c (aref s n))
  20083. (cond ((= c ?\ ) (setq i (1+ i)))
  20084. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  20085. (t (throw 'exit t)))))
  20086. i))
  20087. (defun org-remove-tabs (s &optional width)
  20088. "Replace tabulators in S with spaces.
  20089. Assumes that s is a single line, starting in column 0."
  20090. (setq width (or width tab-width))
  20091. (while (string-match "\t" s)
  20092. (setq s (replace-match
  20093. (make-string
  20094. (- (* width (/ (+ (match-beginning 0) width) width))
  20095. (match-beginning 0)) ?\ )
  20096. t t s)))
  20097. s)
  20098. (defun org-fix-indentation (line ind)
  20099. "Fix indentation in LINE.
  20100. IND is a cons cell with target and minimum indentation.
  20101. If the current indentation in LINE is smaller than the minimum,
  20102. leave it alone. If it is larger than ind, set it to the target."
  20103. (let* ((l (org-remove-tabs line))
  20104. (i (org-get-indentation l))
  20105. (i1 (car ind)) (i2 (cdr ind)))
  20106. (if (>= i i2) (setq l (substring line i2)))
  20107. (if (> i1 0)
  20108. (concat (make-string i1 ?\ ) l)
  20109. l)))
  20110. (defun org-remove-indentation (code &optional n)
  20111. "Remove the maximum common indentation from the lines in CODE.
  20112. N may optionally be the number of spaces to remove."
  20113. (with-temp-buffer
  20114. (insert code)
  20115. (org-do-remove-indentation n)
  20116. (buffer-string)))
  20117. (defun org-do-remove-indentation (&optional n)
  20118. "Remove the maximum common indentation from the buffer."
  20119. (untabify (point-min) (point-max))
  20120. (let ((min 10000) re)
  20121. (if n
  20122. (setq min n)
  20123. (goto-char (point-min))
  20124. (while (re-search-forward "^ *[^ \n]" nil t)
  20125. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  20126. (unless (or (= min 0) (= min 10000))
  20127. (setq re (format "^ \\{%d\\}" min))
  20128. (goto-char (point-min))
  20129. (while (re-search-forward re nil t)
  20130. (replace-match "")
  20131. (end-of-line 1))
  20132. min)))
  20133. (defun org-fill-template (template alist)
  20134. "Find each %key of ALIST in TEMPLATE and replace it."
  20135. (let ((case-fold-search nil)
  20136. entry key value)
  20137. (setq alist (sort (copy-sequence alist)
  20138. (lambda (a b) (< (length (car a)) (length (car b))))))
  20139. (while (setq entry (pop alist))
  20140. (setq template
  20141. (replace-regexp-in-string
  20142. (concat "%" (regexp-quote (car entry)))
  20143. (or (cdr entry) "") template t t)))
  20144. template))
  20145. (defun org-base-buffer (buffer)
  20146. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  20147. (if (not buffer)
  20148. buffer
  20149. (or (buffer-base-buffer buffer)
  20150. buffer)))
  20151. (defun org-wrap (string &optional width lines)
  20152. "Wrap string to either a number of lines, or a width in characters.
  20153. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  20154. that costs. If there is a word longer than WIDTH, the text is actually
  20155. wrapped to the length of that word.
  20156. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  20157. many lines, whatever width that takes.
  20158. The return value is a list of lines, without newlines at the end."
  20159. (let* ((words (org-split-string string "[ \t\n]+"))
  20160. (maxword (apply 'max (mapcar 'org-string-width words)))
  20161. w ll)
  20162. (cond (width
  20163. (org-do-wrap words (max maxword width)))
  20164. (lines
  20165. (setq w maxword)
  20166. (setq ll (org-do-wrap words maxword))
  20167. (if (<= (length ll) lines)
  20168. ll
  20169. (setq ll words)
  20170. (while (> (length ll) lines)
  20171. (setq w (1+ w))
  20172. (setq ll (org-do-wrap words w)))
  20173. ll))
  20174. (t (error "Cannot wrap this")))))
  20175. (defun org-do-wrap (words width)
  20176. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  20177. (let (lines line)
  20178. (while words
  20179. (setq line (pop words))
  20180. (while (and words (< (+ (length line) (length (car words))) width))
  20181. (setq line (concat line " " (pop words))))
  20182. (setq lines (push line lines)))
  20183. (nreverse lines)))
  20184. (defun org-split-string (string &optional separators)
  20185. "Splits STRING into substrings at SEPARATORS.
  20186. No empty strings are returned if there are matches at the beginning
  20187. and end of string."
  20188. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  20189. (start 0)
  20190. notfirst
  20191. (list nil))
  20192. (while (and (string-match rexp string
  20193. (if (and notfirst
  20194. (= start (match-beginning 0))
  20195. (< start (length string)))
  20196. (1+ start) start))
  20197. (< (match-beginning 0) (length string)))
  20198. (setq notfirst t)
  20199. (or (eq (match-beginning 0) 0)
  20200. (and (eq (match-beginning 0) (match-end 0))
  20201. (eq (match-beginning 0) start))
  20202. (setq list
  20203. (cons (substring string start (match-beginning 0))
  20204. list)))
  20205. (setq start (match-end 0)))
  20206. (or (eq start (length string))
  20207. (setq list
  20208. (cons (substring string start)
  20209. list)))
  20210. (nreverse list)))
  20211. (defun org-quote-vert (s)
  20212. "Replace \"|\" with \"\\vert\"."
  20213. (while (string-match "|" s)
  20214. (setq s (replace-match "\\vert" t t s)))
  20215. s)
  20216. (defun org-uuidgen-p (s)
  20217. "Is S an ID created by UUIDGEN?"
  20218. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  20219. (defun org-in-src-block-p (&optional inside)
  20220. "Whether point is in a code source block.
  20221. When INSIDE is non-nil, don't consider we are within a src block
  20222. when point is at #+BEGIN_SRC or #+END_SRC."
  20223. (let ((case-fold-search t) ov)
  20224. (or (and (eq (get-char-property (point) 'src-block) t))
  20225. (and (not inside)
  20226. (save-match-data
  20227. (save-excursion
  20228. (beginning-of-line)
  20229. (looking-at ".*#\\+\\(begin\\|end\\)_src")))))))
  20230. (defun org-context ()
  20231. "Return a list of contexts of the current cursor position.
  20232. If several contexts apply, all are returned.
  20233. Each context entry is a list with a symbol naming the context, and
  20234. two positions indicating start and end of the context. Possible
  20235. contexts are:
  20236. :headline anywhere in a headline
  20237. :headline-stars on the leading stars in a headline
  20238. :todo-keyword on a TODO keyword (including DONE) in a headline
  20239. :tags on the TAGS in a headline
  20240. :priority on the priority cookie in a headline
  20241. :item on the first line of a plain list item
  20242. :item-bullet on the bullet/number of a plain list item
  20243. :checkbox on the checkbox in a plain list item
  20244. :table in an org-mode table
  20245. :table-special on a special filed in a table
  20246. :table-table in a table.el table
  20247. :clocktable in a clocktable
  20248. :src-block in a source block
  20249. :link on a hyperlink
  20250. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT.
  20251. :target on a <<target>>
  20252. :radio-target on a <<<radio-target>>>
  20253. :latex-fragment on a LaTeX fragment
  20254. :latex-preview on a LaTeX fragment with overlaid preview image
  20255. This function expects the position to be visible because it uses font-lock
  20256. faces as a help to recognize the following contexts: :table-special, :link,
  20257. and :keyword."
  20258. (let* ((f (get-text-property (point) 'face))
  20259. (faces (if (listp f) f (list f)))
  20260. (case-fold-search t)
  20261. (p (point)) clist o)
  20262. ;; First the large context
  20263. (cond
  20264. ((org-at-heading-p t)
  20265. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  20266. (when (progn
  20267. (beginning-of-line 1)
  20268. (looking-at org-todo-line-tags-regexp))
  20269. (push (org-point-in-group p 1 :headline-stars) clist)
  20270. (push (org-point-in-group p 2 :todo-keyword) clist)
  20271. (push (org-point-in-group p 4 :tags) clist))
  20272. (goto-char p)
  20273. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  20274. (if (looking-at "\\[#[A-Z0-9]\\]")
  20275. (push (org-point-in-group p 0 :priority) clist)))
  20276. ((org-at-item-p)
  20277. (push (org-point-in-group p 2 :item-bullet) clist)
  20278. (push (list :item (point-at-bol)
  20279. (save-excursion (org-end-of-item) (point)))
  20280. clist)
  20281. (and (org-at-item-checkbox-p)
  20282. (push (org-point-in-group p 0 :checkbox) clist)))
  20283. ((org-at-table-p)
  20284. (push (list :table (org-table-begin) (org-table-end)) clist)
  20285. (if (memq 'org-formula faces)
  20286. (push (list :table-special
  20287. (previous-single-property-change p 'face)
  20288. (next-single-property-change p 'face)) clist)))
  20289. ((org-at-table-p 'any)
  20290. (push (list :table-table) clist)))
  20291. (goto-char p)
  20292. (let ((case-fold-search t))
  20293. ;; New the "medium" contexts: clocktables, source blocks
  20294. (cond ((org-in-clocktable-p)
  20295. (push (list :clocktable
  20296. (and (or (looking-at "[ \t]*\\(#\\+BEGIN: clocktable\\)")
  20297. (re-search-backward "[ \t]*\\(#+BEGIN: clocktable\\)" nil t))
  20298. (match-beginning 1))
  20299. (and (re-search-forward "[ \t]*#\\+END:?" nil t)
  20300. (match-end 0))) clist))
  20301. ((org-in-src-block-p)
  20302. (push (list :src-block
  20303. (and (or (looking-at "[ \t]*\\(#\\+BEGIN_SRC\\)")
  20304. (re-search-backward "[ \t]*\\(#+BEGIN_SRC\\)" nil t))
  20305. (match-beginning 1))
  20306. (and (search-forward "#+END_SRC" nil t)
  20307. (match-beginning 0))) clist))))
  20308. (goto-char p)
  20309. ;; Now the small context
  20310. (cond
  20311. ((org-at-timestamp-p)
  20312. (push (org-point-in-group p 0 :timestamp) clist))
  20313. ((memq 'org-link faces)
  20314. (push (list :link
  20315. (previous-single-property-change p 'face)
  20316. (next-single-property-change p 'face)) clist))
  20317. ((memq 'org-special-keyword faces)
  20318. (push (list :keyword
  20319. (previous-single-property-change p 'face)
  20320. (next-single-property-change p 'face)) clist))
  20321. ((org-at-target-p)
  20322. (push (org-point-in-group p 0 :target) clist)
  20323. (goto-char (1- (match-beginning 0)))
  20324. (if (looking-at org-radio-target-regexp)
  20325. (push (org-point-in-group p 0 :radio-target) clist))
  20326. (goto-char p))
  20327. ((setq o (car (delq nil
  20328. (mapcar
  20329. (lambda (x)
  20330. (if (memq x org-latex-fragment-image-overlays) x))
  20331. (overlays-at (point))))))
  20332. (push (list :latex-fragment
  20333. (overlay-start o) (overlay-end o)) clist)
  20334. (push (list :latex-preview
  20335. (overlay-start o) (overlay-end o)) clist))
  20336. ((org-inside-LaTeX-fragment-p)
  20337. ;; FIXME: positions wrong.
  20338. (push (list :latex-fragment (point) (point)) clist)))
  20339. (setq clist (nreverse (delq nil clist)))
  20340. clist))
  20341. ;; FIXME: Compare with at-regexp-p Do we need both?
  20342. (defun org-in-regexp (re &optional nlines visually)
  20343. "Check if point is inside a match of regexp.
  20344. Normally only the current line is checked, but you can include NLINES extra
  20345. lines both before and after point into the search.
  20346. If VISUALLY is set, require that the cursor is not after the match but
  20347. really on, so that the block visually is on the match."
  20348. (catch 'exit
  20349. (let ((pos (point))
  20350. (eol (point-at-eol (+ 1 (or nlines 0))))
  20351. (inc (if visually 1 0)))
  20352. (save-excursion
  20353. (beginning-of-line (- 1 (or nlines 0)))
  20354. (while (re-search-forward re eol t)
  20355. (if (and (<= (match-beginning 0) pos)
  20356. (>= (+ inc (match-end 0)) pos))
  20357. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  20358. (defun org-at-regexp-p (regexp)
  20359. "Is point inside a match of REGEXP in the current line?"
  20360. (catch 'exit
  20361. (save-excursion
  20362. (let ((pos (point)) (end (point-at-eol)))
  20363. (beginning-of-line 1)
  20364. (while (re-search-forward regexp end t)
  20365. (if (and (<= (match-beginning 0) pos)
  20366. (>= (match-end 0) pos))
  20367. (throw 'exit t)))
  20368. nil))))
  20369. (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
  20370. "Non-nil when point is between matches of START-RE and END-RE.
  20371. Also return a non-nil value when point is on one of the matches.
  20372. Optional arguments LIM-UP and LIM-DOWN bound the search; they are
  20373. buffer positions. Default values are the positions of headlines
  20374. surrounding the point.
  20375. The functions returns a cons cell whose car (resp. cdr) is the
  20376. position before START-RE (resp. after END-RE)."
  20377. (save-match-data
  20378. (let ((pos (point))
  20379. (limit-up (or lim-up (save-excursion (outline-previous-heading))))
  20380. (limit-down (or lim-down (save-excursion (outline-next-heading))))
  20381. beg end)
  20382. (save-excursion
  20383. ;; Point is on a block when on START-RE or if START-RE can be
  20384. ;; found before it...
  20385. (and (or (org-at-regexp-p start-re)
  20386. (re-search-backward start-re limit-up t))
  20387. (setq beg (match-beginning 0))
  20388. ;; ... and END-RE after it...
  20389. (goto-char (match-end 0))
  20390. (re-search-forward end-re limit-down t)
  20391. (> (setq end (match-end 0)) pos)
  20392. ;; ... without another START-RE in-between.
  20393. (goto-char (match-beginning 0))
  20394. (not (re-search-backward start-re (1+ beg) t))
  20395. ;; Return value.
  20396. (cons beg end))))))
  20397. (defun org-in-block-p (names)
  20398. "Non-nil when point belongs to a block whose name belongs to NAMES.
  20399. NAMES is a list of strings containing names of blocks.
  20400. Return first block name matched, or nil. Beware that in case of
  20401. nested blocks, the returned name may not belong to the closest
  20402. block from point."
  20403. (save-match-data
  20404. (catch 'exit
  20405. (let ((case-fold-search t)
  20406. (lim-up (save-excursion (outline-previous-heading)))
  20407. (lim-down (save-excursion (outline-next-heading))))
  20408. (mapc (lambda (name)
  20409. (let ((n (regexp-quote name)))
  20410. (when (org-between-regexps-p
  20411. (concat "^[ \t]*#\\+begin_" n)
  20412. (concat "^[ \t]*#\\+end_" n)
  20413. lim-up lim-down)
  20414. (throw 'exit n))))
  20415. names))
  20416. nil)))
  20417. (defun org-in-drawer-p ()
  20418. "Non-nil if point is within a drawer.
  20419. If point is within a drawer, return it, as parsed data."
  20420. (let ((element (save-match-data (org-element-at-point))))
  20421. (while (and element (not (memq (org-element-type element)
  20422. '(drawer property-drawer))))
  20423. (setq element (org-element-property :parent element)))
  20424. element))
  20425. (defun org-occur-in-agenda-files (regexp &optional nlines)
  20426. "Call `multi-occur' with buffers for all agenda files."
  20427. (interactive "sOrg-files matching: \np")
  20428. (let* ((files (org-agenda-files))
  20429. (tnames (mapcar 'file-truename files))
  20430. (extra org-agenda-text-search-extra-files)
  20431. f)
  20432. (when (eq (car extra) 'agenda-archives)
  20433. (setq extra (cdr extra))
  20434. (setq files (org-add-archive-files files)))
  20435. (while (setq f (pop extra))
  20436. (unless (member (file-truename f) tnames)
  20437. (add-to-list 'files f 'append)
  20438. (add-to-list 'tnames (file-truename f) 'append)))
  20439. (multi-occur
  20440. (mapcar (lambda (x)
  20441. (with-current-buffer
  20442. (or (get-file-buffer x) (find-file-noselect x))
  20443. (widen)
  20444. (current-buffer)))
  20445. files)
  20446. regexp)))
  20447. (if (boundp 'occur-mode-find-occurrence-hook)
  20448. ;; Emacs 23
  20449. (add-hook 'occur-mode-find-occurrence-hook
  20450. (lambda ()
  20451. (when (derived-mode-p 'org-mode)
  20452. (org-reveal))))
  20453. ;; Emacs 22
  20454. (defadvice occur-mode-goto-occurrence
  20455. (after org-occur-reveal activate)
  20456. (and (derived-mode-p 'org-mode) (org-reveal)))
  20457. (defadvice occur-mode-goto-occurrence-other-window
  20458. (after org-occur-reveal activate)
  20459. (and (derived-mode-p 'org-mode) (org-reveal)))
  20460. (defadvice occur-mode-display-occurrence
  20461. (after org-occur-reveal activate)
  20462. (when (derived-mode-p 'org-mode)
  20463. (let ((pos (occur-mode-find-occurrence)))
  20464. (with-current-buffer (marker-buffer pos)
  20465. (save-excursion
  20466. (goto-char pos)
  20467. (org-reveal)))))))
  20468. (defun org-occur-link-in-agenda-files ()
  20469. "Create a link and search for it in the agendas.
  20470. The link is not stored in `org-stored-links', it is just created
  20471. for the search purpose."
  20472. (interactive)
  20473. (let ((link (condition-case nil
  20474. (org-store-link nil)
  20475. (error "Unable to create a link to here"))))
  20476. (org-occur-in-agenda-files (regexp-quote link))))
  20477. (defun org-reverse-string (string)
  20478. "Return the reverse of STRING."
  20479. (apply 'string (reverse (string-to-list string))))
  20480. ;; defsubst org-uniquify must be defined before first use
  20481. (defun org-uniquify-alist (alist)
  20482. "Merge elements of ALIST with the same key.
  20483. For example, in this alist:
  20484. \(org-uniquify-alist '((a 1) (b 2) (a 3)))
  20485. => '((a 1 3) (b 2))
  20486. merge (a 1) and (a 3) into (a 1 3).
  20487. The function returns the new ALIST."
  20488. (let (rtn)
  20489. (mapc
  20490. (lambda (e)
  20491. (let (n)
  20492. (if (not (assoc (car e) rtn))
  20493. (push e rtn)
  20494. (setq n (cons (car e) (append (cdr (assoc (car e) rtn)) (cdr e))))
  20495. (setq rtn (assq-delete-all (car e) rtn))
  20496. (push n rtn))))
  20497. alist)
  20498. rtn))
  20499. (defun org-delete-all (elts list)
  20500. "Remove all elements in ELTS from LIST."
  20501. (while elts
  20502. (setq list (delete (pop elts) list)))
  20503. list)
  20504. (defun org-count (cl-item cl-seq)
  20505. "Count the number of occurrences of ITEM in SEQ.
  20506. Taken from `count' in cl-seq.el with all keyword arguments removed."
  20507. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  20508. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  20509. (while (< cl-start cl-end)
  20510. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  20511. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  20512. (setq cl-start (1+ cl-start)))
  20513. cl-count))
  20514. (defun org-remove-if (predicate seq)
  20515. "Remove everything from SEQ that fulfills PREDICATE."
  20516. (let (res e)
  20517. (while seq
  20518. (setq e (pop seq))
  20519. (if (not (funcall predicate e)) (push e res)))
  20520. (nreverse res)))
  20521. (defun org-remove-if-not (predicate seq)
  20522. "Remove everything from SEQ that does not fulfill PREDICATE."
  20523. (let (res e)
  20524. (while seq
  20525. (setq e (pop seq))
  20526. (if (funcall predicate e) (push e res)))
  20527. (nreverse res)))
  20528. (defun org-reduce (cl-func cl-seq &rest cl-keys)
  20529. "Reduce two-argument FUNCTION across SEQ.
  20530. Taken from `reduce' in cl-seq.el with all keyword arguments but
  20531. \":initial-value\" removed."
  20532. (let ((cl-accum (cond ((memq :initial-value cl-keys)
  20533. (cadr (memq :initial-value cl-keys)))
  20534. (cl-seq (pop cl-seq))
  20535. (t (funcall cl-func)))))
  20536. (while cl-seq
  20537. (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
  20538. cl-accum))
  20539. (defun org-every (pred seq)
  20540. "Return true if PREDICATE is true of every element of SEQ.
  20541. Adapted from `every' in cl.el."
  20542. (catch 'org-every
  20543. (mapc (lambda (e) (unless (funcall pred e) (throw 'org-every nil))) seq)
  20544. t))
  20545. (defun org-some (pred seq)
  20546. "Return true if PREDICATE is true of any element of SEQ.
  20547. Adapted from `some' in cl.el."
  20548. (catch 'org-some
  20549. (mapc (lambda (e) (when (funcall pred e) (throw 'org-some t))) seq)
  20550. nil))
  20551. (defun org-back-over-empty-lines ()
  20552. "Move backwards over whitespace, to the beginning of the first empty line.
  20553. Returns the number of empty lines passed."
  20554. (let ((pos (point)))
  20555. (if (cdr (assoc 'heading org-blank-before-new-entry))
  20556. (skip-chars-backward " \t\n\r")
  20557. (unless (eobp)
  20558. (forward-line -1)))
  20559. (beginning-of-line 2)
  20560. (goto-char (min (point) pos))
  20561. (count-lines (point) pos)))
  20562. (defun org-skip-whitespace ()
  20563. (skip-chars-forward " \t\n\r"))
  20564. (defun org-point-in-group (point group &optional context)
  20565. "Check if POINT is in match-group GROUP.
  20566. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  20567. match. If the match group does not exist or point is not inside it,
  20568. return nil."
  20569. (and (match-beginning group)
  20570. (>= point (match-beginning group))
  20571. (<= point (match-end group))
  20572. (if context
  20573. (list context (match-beginning group) (match-end group))
  20574. t)))
  20575. (defun org-switch-to-buffer-other-window (&rest args)
  20576. "Switch to buffer in a second window on the current frame.
  20577. In particular, do not allow pop-up frames.
  20578. Returns the newly created buffer."
  20579. (org-no-popups
  20580. (apply 'switch-to-buffer-other-window args)))
  20581. (defun org-combine-plists (&rest plists)
  20582. "Create a single property list from all plists in PLISTS.
  20583. The process starts by copying the first list, and then setting properties
  20584. from the other lists. Settings in the last list are the most significant
  20585. ones and overrule settings in the other lists."
  20586. (let ((rtn (copy-sequence (pop plists)))
  20587. p v ls)
  20588. (while plists
  20589. (setq ls (pop plists))
  20590. (while ls
  20591. (setq p (pop ls) v (pop ls))
  20592. (setq rtn (plist-put rtn p v))))
  20593. rtn))
  20594. (defun org-replace-escapes (string table)
  20595. "Replace %-escapes in STRING with values in TABLE.
  20596. TABLE is an association list with keys like \"%a\" and string values.
  20597. The sequences in STRING may contain normal field width and padding information,
  20598. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  20599. so values can contain further %-escapes if they are define later in TABLE."
  20600. (let ((tbl (copy-alist table))
  20601. (case-fold-search nil)
  20602. (pchg 0)
  20603. e re rpl)
  20604. (while (setq e (pop tbl))
  20605. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  20606. (when (and (cdr e) (string-match re (cdr e)))
  20607. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  20608. (safe "SREF"))
  20609. (add-text-properties 0 3 (list 'sref sref) safe)
  20610. (setcdr e (replace-match safe t t (cdr e)))))
  20611. (while (string-match re string)
  20612. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  20613. (cdr e)))
  20614. (setq string (replace-match rpl t t string))))
  20615. (while (setq pchg (next-property-change pchg string))
  20616. (let ((sref (get-text-property pchg 'sref string)))
  20617. (when (and sref (string-match "SREF" string pchg))
  20618. (setq string (replace-match sref t t string)))))
  20619. string))
  20620. (defun org-sublist (list start end)
  20621. "Return a section of LIST, from START to END.
  20622. Counting starts at 1."
  20623. (let (rtn (c start))
  20624. (setq list (nthcdr (1- start) list))
  20625. (while (and list (<= c end))
  20626. (push (pop list) rtn)
  20627. (setq c (1+ c)))
  20628. (nreverse rtn)))
  20629. (defun org-find-base-buffer-visiting (file)
  20630. "Like `find-buffer-visiting' but always return the base buffer and
  20631. not an indirect buffer."
  20632. (let ((buf (or (get-file-buffer file)
  20633. (find-buffer-visiting file))))
  20634. (if buf
  20635. (or (buffer-base-buffer buf) buf)
  20636. nil)))
  20637. (defun org-image-file-name-regexp (&optional extensions)
  20638. "Return regexp matching the file names of images.
  20639. If EXTENSIONS is given, only match these."
  20640. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  20641. (image-file-name-regexp)
  20642. (let ((image-file-name-extensions
  20643. (or extensions
  20644. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  20645. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  20646. (concat "\\."
  20647. (regexp-opt (nconc (mapcar 'upcase
  20648. image-file-name-extensions)
  20649. image-file-name-extensions)
  20650. t)
  20651. "\\'"))))
  20652. (defun org-file-image-p (file &optional extensions)
  20653. "Return non-nil if FILE is an image."
  20654. (save-match-data
  20655. (string-match (org-image-file-name-regexp extensions) file)))
  20656. (defun org-get-cursor-date (&optional with-time)
  20657. "Return the date at cursor in as a time.
  20658. This works in the calendar and in the agenda, anywhere else it just
  20659. returns the current time.
  20660. If WITH-TIME is non-nil, returns the time of the event at point (in
  20661. the agenda) or the current time of the day."
  20662. (let (date day defd tp tm hod mod)
  20663. (when with-time
  20664. (setq tp (get-text-property (point) 'time))
  20665. (when (and tp (string-match "\\([0-9][0-9]\\):\\([0-9][0-9]\\)" tp))
  20666. (setq hod (string-to-number (match-string 1 tp))
  20667. mod (string-to-number (match-string 2 tp))))
  20668. (or tp (setq hod (nth 2 (decode-time (current-time)))
  20669. mod (nth 1 (decode-time (current-time))))))
  20670. (cond
  20671. ((eq major-mode 'calendar-mode)
  20672. (setq date (calendar-cursor-to-date)
  20673. defd (encode-time 0 (or mod 0) (or hod 0)
  20674. (nth 1 date) (nth 0 date) (nth 2 date))))
  20675. ((eq major-mode 'org-agenda-mode)
  20676. (setq day (get-text-property (point) 'day))
  20677. (if day
  20678. (setq date (calendar-gregorian-from-absolute day)
  20679. defd (encode-time 0 (or mod 0) (or hod 0)
  20680. (nth 1 date) (nth 0 date) (nth 2 date))))))
  20681. (or defd (current-time))))
  20682. (defun org-mark-subtree (&optional up)
  20683. "Mark the current subtree.
  20684. This puts point at the start of the current subtree, and mark at
  20685. the end. If a numeric prefix UP is given, move up into the
  20686. hierarchy of headlines by UP levels before marking the subtree."
  20687. (interactive "P")
  20688. (org-with-limited-levels
  20689. (cond ((org-at-heading-p) (beginning-of-line))
  20690. ((org-before-first-heading-p) (user-error "Not in a subtree"))
  20691. (t (outline-previous-visible-heading 1))))
  20692. (when up (while (and (> up 0) (org-up-heading-safe)) (decf up)))
  20693. (if (org-called-interactively-p 'any)
  20694. (call-interactively 'org-mark-element)
  20695. (org-mark-element)))
  20696. ;;; Indentation
  20697. (defun org--get-expected-indentation (element contentsp)
  20698. "Expected indentation column for current line, according to ELEMENT.
  20699. ELEMENT is an element containing point. CONTENTSP is non-nil
  20700. when indentation is to be computed according to contents of
  20701. ELEMENT."
  20702. (let ((type (org-element-type element))
  20703. (start (org-element-property :begin element)))
  20704. (org-with-wide-buffer
  20705. (cond
  20706. (contentsp
  20707. (case type
  20708. (footnote-definition 0)
  20709. ((headline inlinetask nil)
  20710. (if (not org-adapt-indentation) 0
  20711. (let ((level (org-current-level)))
  20712. (if level (1+ level) 0))))
  20713. (item
  20714. (org-list-item-body-column
  20715. (org-element-property :post-affiliated element)))
  20716. (plain-list
  20717. (save-excursion
  20718. (goto-char (org-element-property :post-affiliated element))
  20719. (org-get-indentation)))
  20720. (otherwise
  20721. (goto-char start)
  20722. (org-get-indentation))))
  20723. ((memq type '(headline inlinetask nil))
  20724. (if (save-excursion (beginning-of-line) (looking-at "[ \t]*$"))
  20725. (org--get-expected-indentation element t)
  20726. 0))
  20727. ((eq type 'footnote-definition) 0)
  20728. ;; First paragraph of a footnote definition or an item.
  20729. ;; Indent like parent.
  20730. ((< (line-beginning-position) start)
  20731. (org--get-expected-indentation
  20732. (org-element-property :parent element) t))
  20733. ;; At first line: indent according to previous sibling, if any,
  20734. ;; ignoring footnote definitions and inline tasks, or parent's
  20735. ;; contents.
  20736. ((= (line-beginning-position) start)
  20737. (catch 'exit
  20738. (while t
  20739. (if (= (point-min) start) (throw 'exit 0)
  20740. (goto-char (1- start))
  20741. (let* ((previous (org-element-at-point))
  20742. (parent previous))
  20743. (while (and parent (<= (org-element-property :end parent) start))
  20744. (setq previous parent
  20745. parent (org-element-property :parent parent)))
  20746. (cond
  20747. ((not previous) (throw 'exit 0))
  20748. ((> (org-element-property :end previous) start)
  20749. (throw 'exit (org--get-expected-indentation previous t)))
  20750. ((memq (org-element-type previous)
  20751. '(footnote-definition inlinetask))
  20752. (setq start (org-element-property :begin previous)))
  20753. (t (goto-char (org-element-property :begin previous))
  20754. (throw 'exit
  20755. (if (bolp) (org-get-indentation)
  20756. ;; At first paragraph in an item or
  20757. ;; a footnote definition.
  20758. (org--get-expected-indentation
  20759. (org-element-property :parent previous) t))))))))))
  20760. ;; Otherwise, move to the first non-blank line above.
  20761. (t
  20762. (beginning-of-line)
  20763. (let ((pos (point)))
  20764. (skip-chars-backward " \r\t\n")
  20765. (cond
  20766. ;; Two blank lines end a footnote definition or a plain
  20767. ;; list. When we indent an empty line after them, the
  20768. ;; containing list or footnote definition is over, so it
  20769. ;; qualifies as a previous sibling. Therefore, we indent
  20770. ;; like its first line.
  20771. ((and (memq type '(footnote-definition plain-list))
  20772. (> (count-lines (point) pos) 2))
  20773. (goto-char start)
  20774. (org-get-indentation))
  20775. ;; Line above is the first one of a paragraph at the
  20776. ;; beginning of an item or a footnote definition. Indent
  20777. ;; like parent.
  20778. ((< (line-beginning-position) start)
  20779. (org--get-expected-indentation
  20780. (org-element-property :parent element) t))
  20781. ;; POS is after contents in a greater element. Indent like
  20782. ;; the beginning of the element.
  20783. ;;
  20784. ;; As a special case, if point is at the end of a footnote
  20785. ;; definition or an item, indent like the very last element
  20786. ;; within.
  20787. ((and (not (eq type 'paragraph))
  20788. (let ((cend (org-element-property :contents-end element)))
  20789. (and cend (<= cend pos))))
  20790. (if (memq type '(footnote-definition item plain-list))
  20791. (org--get-expected-indentation (org-element-at-point) nil)
  20792. (goto-char start)
  20793. (org-get-indentation)))
  20794. ;; In any other case, indent like the current line.
  20795. (t (org-get-indentation)))))))))
  20796. (defun org--align-node-property ()
  20797. "Align node property at point.
  20798. Alignment is done according to `org-property-format', which see."
  20799. (when (save-excursion
  20800. (beginning-of-line)
  20801. (looking-at org-property-re))
  20802. (replace-match
  20803. (concat (match-string 4)
  20804. (org-trim
  20805. (format org-property-format (match-string 1) (match-string 3))))
  20806. t t)))
  20807. (defun org-indent-line ()
  20808. "Indent line depending on context.
  20809. Indentation is done according to the following rules:
  20810. - Footnote definitions, headlines and inline tasks have to
  20811. start at column 0.
  20812. - On the very first line of an element, consider, in order, the
  20813. next rules until one matches:
  20814. 1. If there's a sibling element before, ignoring footnote
  20815. definitions and inline tasks, indent like its first line.
  20816. 2. If element has a parent, indent like its contents. More
  20817. precisely, if parent is an item, indent after the
  20818. description part, if any, or the bullet (see
  20819. `org-list-description-max-indent'). Else, indent like
  20820. parent's first line.
  20821. 3. Otherwise, indent relatively to current level, if
  20822. `org-adapt-indentation' is non-nil, or to left margin.
  20823. - On a blank line at the end of an element, indent according to
  20824. the type of the element. More precisely
  20825. 1. If element is a plain list, an item, or a footnote
  20826. definition, indent like the very last element within.
  20827. 2. If element is a paragraph, indent like its last non blank
  20828. line.
  20829. 3. Otherwise, indent like its very first line.
  20830. - In the code part of a source block, use language major mode
  20831. to indent current line if `org-src-tab-acts-natively' is
  20832. non-nil. If it is nil, do nothing.
  20833. - Otherwise, indent like the first non-blank line above.
  20834. The function doesn't indent an item as it could break the whole
  20835. list structure. Instead, use \\<org-mode-map>\\[org-shiftmetaleft] or \
  20836. \\[org-shiftmetaright].
  20837. Also align node properties according to `org-property-format'."
  20838. (interactive)
  20839. (cond
  20840. (orgstruct-is-++
  20841. (let ((indent-line-function
  20842. (cadadr (assq 'indent-line-function org-fb-vars))))
  20843. (indent-according-to-mode)))
  20844. ((org-at-heading-p) 'noindent)
  20845. (t
  20846. (let* ((element (save-excursion (beginning-of-line) (org-element-at-point)))
  20847. (type (org-element-type element)))
  20848. (cond ((and (memq type '(plain-list item))
  20849. (= (line-beginning-position)
  20850. (org-element-property :post-affiliated element)))
  20851. 'noindent)
  20852. ((and (eq type 'src-block)
  20853. org-src-tab-acts-natively
  20854. (> (line-beginning-position)
  20855. (org-element-property :post-affiliated element))
  20856. (< (line-beginning-position)
  20857. (org-with-wide-buffer
  20858. (goto-char (org-element-property :end element))
  20859. (skip-chars-backward " \r\t\n")
  20860. (line-beginning-position))))
  20861. (org-babel-do-key-sequence-in-edit-buffer (kbd "TAB")))
  20862. (t
  20863. (let ((column (org--get-expected-indentation element nil)))
  20864. ;; Preserve current column.
  20865. (if (<= (current-column) (current-indentation))
  20866. (org-indent-line-to column)
  20867. (save-excursion (org-indent-line-to column))))
  20868. ;; Align node property. Also preserve current column.
  20869. (when (eq type 'node-property)
  20870. (let ((column (current-column)))
  20871. (org--align-node-property)
  20872. (org-move-to-column column)))))))))
  20873. (defun org-indent-region (start end)
  20874. "Indent each non-blank line in the region.
  20875. Called from a program, START and END specify the region to
  20876. indent. The function will not indent contents of example blocks,
  20877. verse blocks and export blocks as leading white spaces are
  20878. assumed to be significant there."
  20879. (interactive "r")
  20880. (save-excursion
  20881. (goto-char start)
  20882. (skip-chars-forward " \r\t\n")
  20883. (unless (eobp) (beginning-of-line))
  20884. (let ((indent-to
  20885. (lambda (ind pos)
  20886. ;; Set IND as indentation for all lines between point and
  20887. ;; POS or END, whichever comes first. Blank lines are
  20888. ;; ignored. Leave point after POS once done.
  20889. (let ((limit (copy-marker (min end pos))))
  20890. (while (< (point) limit)
  20891. (unless (org-looking-at-p "[ \t]*$") (org-indent-line-to ind))
  20892. (forward-line))
  20893. (set-marker limit nil))))
  20894. (end (copy-marker end)))
  20895. (while (< (point) end)
  20896. (if (or (org-looking-at-p " \r\t\n") (org-at-heading-p)) (forward-line)
  20897. (let* ((element (org-element-at-point))
  20898. (type (org-element-type element))
  20899. (element-end (copy-marker (org-element-property :end element)))
  20900. (ind (org--get-expected-indentation element nil)))
  20901. (cond
  20902. ((or (memq type '(paragraph table table-row))
  20903. (not (or (org-element-property :contents-begin element)
  20904. (memq type
  20905. '(example-block export-block src-block)))))
  20906. ;; Elements here are indented as a single block. Also
  20907. ;; align node properties.
  20908. (when (eq type 'node-property)
  20909. (org--align-node-property)
  20910. (beginning-of-line))
  20911. (funcall indent-to ind element-end))
  20912. (t
  20913. ;; Elements in this category consist of three parts:
  20914. ;; before the contents, the contents, and after the
  20915. ;; contents. The contents are treated specially,
  20916. ;; according to the element type, or not indented at
  20917. ;; all. Other parts are indented as a single block.
  20918. (let* ((post (copy-marker
  20919. (org-element-property :post-affiliated element)))
  20920. (cbeg
  20921. (copy-marker
  20922. (cond
  20923. ((not (org-element-property :contents-begin element))
  20924. ;; Fake contents for source blocks.
  20925. (org-with-wide-buffer
  20926. (goto-char post)
  20927. (forward-line)
  20928. (point)))
  20929. ((memq type '(footnote-definition item plain-list))
  20930. ;; Contents in these elements could start on
  20931. ;; the same line as the beginning of the
  20932. ;; element. Make sure we start indenting
  20933. ;; from the second line.
  20934. (org-with-wide-buffer
  20935. (goto-char post)
  20936. (end-of-line)
  20937. (skip-chars-forward " \r\t\n")
  20938. (if (eobp) (point) (line-beginning-position))))
  20939. (t (org-element-property :contents-begin element)))))
  20940. (cend (copy-marker
  20941. (or (org-element-property :contents-end element)
  20942. ;; Fake contents for source blocks.
  20943. (org-with-wide-buffer
  20944. (goto-char element-end)
  20945. (skip-chars-backward " \r\t\n")
  20946. (line-beginning-position)))
  20947. t)))
  20948. ;; Do not change items indentation individually as it
  20949. ;; might break the list as a whole. On the other
  20950. ;; hand, when at a plain list, indent it as a whole.
  20951. (cond ((eq type 'plain-list)
  20952. (let ((offset (- ind (org-get-indentation))))
  20953. (unless (zerop offset)
  20954. (indent-rigidly (org-element-property :begin element)
  20955. (org-element-property :end element)
  20956. offset))
  20957. (goto-char cbeg)))
  20958. ((eq type 'item) (goto-char cbeg))
  20959. (t (funcall indent-to ind cbeg)))
  20960. (when (< (point) end)
  20961. (case type
  20962. ((example-block export-block verse-block))
  20963. (src-block
  20964. ;; In a source block, indent source code
  20965. ;; according to language major mode, but only if
  20966. ;; `org-src-tab-acts-natively' is non-nil.
  20967. (when (and (< (point) end) org-src-tab-acts-natively)
  20968. (ignore-errors
  20969. (org-babel-do-in-edit-buffer
  20970. (indent-region (point-min) (point-max))))))
  20971. (t (org-indent-region (point) (min cend end))))
  20972. (goto-char (min cend end))
  20973. (when (< (point) end) (funcall indent-to ind element-end)))
  20974. (set-marker post nil)
  20975. (set-marker cbeg nil)
  20976. (set-marker cend nil))))
  20977. (set-marker element-end nil))))
  20978. (set-marker end nil))))
  20979. (defun org-indent-drawer ()
  20980. "Indent the drawer at point."
  20981. (interactive)
  20982. (unless (save-excursion
  20983. (beginning-of-line)
  20984. (org-looking-at-p org-drawer-regexp))
  20985. (user-error "Not at a drawer"))
  20986. (let ((element (org-element-at-point)))
  20987. (unless (memq (org-element-type element) '(drawer property-drawer))
  20988. (user-error "Not at a drawer"))
  20989. (org-with-wide-buffer
  20990. (org-indent-region (org-element-property :begin element)
  20991. (org-element-property :end element))))
  20992. (message "Drawer at point indented"))
  20993. (defun org-indent-block ()
  20994. "Indent the block at point."
  20995. (interactive)
  20996. (unless (save-excursion
  20997. (beginning-of-line)
  20998. (let ((case-fold-search t))
  20999. (org-looking-at-p "[ \t]*#\\+\\(begin\\|end\\)_")))
  21000. (user-error "Not at a block"))
  21001. (let ((element (org-element-at-point)))
  21002. (unless (memq (org-element-type element)
  21003. '(comment-block center-block dynamic-block example-block
  21004. export-block quote-block special-block
  21005. src-block verse-block))
  21006. (user-error "Not at a block"))
  21007. (org-with-wide-buffer
  21008. (org-indent-region (org-element-property :begin element)
  21009. (org-element-property :end element))))
  21010. (message "Block at point indented"))
  21011. ;;; Filling
  21012. ;; We use our own fill-paragraph and auto-fill functions.
  21013. ;; `org-fill-paragraph' relies on adaptive filling and context
  21014. ;; checking. Appropriate `fill-prefix' is computed with
  21015. ;; `org-adaptive-fill-function'.
  21016. ;; `org-auto-fill-function' takes care of auto-filling. It calls
  21017. ;; `do-auto-fill' only on valid areas with `fill-prefix' shadowed with
  21018. ;; `org-adaptive-fill-function' value. Internally,
  21019. ;; `org-comment-line-break-function' breaks the line.
  21020. ;; `org-setup-filling' installs filling and auto-filling related
  21021. ;; variables during `org-mode' initialization.
  21022. (defvar org-element-paragraph-separate) ; org-element.el
  21023. (defun org-setup-filling ()
  21024. (require 'org-element)
  21025. ;; Prevent auto-fill from inserting unwanted new items.
  21026. (when (boundp 'fill-nobreak-predicate)
  21027. (org-set-local
  21028. 'fill-nobreak-predicate
  21029. (org-uniquify
  21030. (append fill-nobreak-predicate
  21031. '(org-fill-line-break-nobreak-p
  21032. org-fill-paragraph-with-timestamp-nobreak-p)))))
  21033. (let ((paragraph-ending (substring org-element-paragraph-separate 1)))
  21034. (org-set-local 'paragraph-start paragraph-ending)
  21035. (org-set-local 'paragraph-separate paragraph-ending))
  21036. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  21037. (org-set-local 'auto-fill-inhibit-regexp nil)
  21038. (org-set-local 'adaptive-fill-function 'org-adaptive-fill-function)
  21039. (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
  21040. (org-set-local 'comment-line-break-function 'org-comment-line-break-function))
  21041. (defun org-fill-line-break-nobreak-p ()
  21042. "Non-nil when a new line at point would create an Org line break."
  21043. (save-excursion
  21044. (skip-chars-backward "[ \t]")
  21045. (skip-chars-backward "\\\\")
  21046. (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
  21047. (defun org-fill-paragraph-with-timestamp-nobreak-p ()
  21048. "Non-nil when a new line at point would split a timestamp."
  21049. (and (org-at-timestamp-p t)
  21050. (not (looking-at org-ts-regexp-both))))
  21051. (declare-function message-in-body-p "message" ())
  21052. (defvar orgtbl-line-start-regexp) ; From org-table.el
  21053. (defun org-adaptive-fill-function ()
  21054. "Compute a fill prefix for the current line.
  21055. Return fill prefix, as a string, or nil if current line isn't
  21056. meant to be filled. For convenience, if `adaptive-fill-regexp'
  21057. matches in paragraphs or comments, use it."
  21058. (catch 'exit
  21059. (when (derived-mode-p 'message-mode)
  21060. (save-excursion
  21061. (beginning-of-line)
  21062. (cond ((or (not (message-in-body-p))
  21063. (looking-at orgtbl-line-start-regexp))
  21064. (throw 'exit nil))
  21065. ((looking-at message-cite-prefix-regexp)
  21066. (throw 'exit (match-string-no-properties 0)))
  21067. ((looking-at org-outline-regexp)
  21068. (throw 'exit (make-string (length (match-string 0)) ?\s))))))
  21069. (org-with-wide-buffer
  21070. (unless (org-at-heading-p)
  21071. (let* ((p (line-beginning-position))
  21072. (element (save-excursion
  21073. (beginning-of-line)
  21074. (org-element-at-point)))
  21075. (type (org-element-type element))
  21076. (post-affiliated (org-element-property :post-affiliated element)))
  21077. (unless (< p post-affiliated)
  21078. (case type
  21079. (comment
  21080. (save-excursion
  21081. (beginning-of-line)
  21082. (looking-at "[ \t]*")
  21083. (concat (match-string 0) "# ")))
  21084. (footnote-definition "")
  21085. ((item plain-list)
  21086. (make-string (org-list-item-body-column post-affiliated) ?\s))
  21087. (paragraph
  21088. ;; Fill prefix is usually the same as the current line,
  21089. ;; unless the paragraph is at the beginning of an item.
  21090. (let ((parent (org-element-property :parent element)))
  21091. (save-excursion
  21092. (beginning-of-line)
  21093. (cond ((eq (org-element-type parent) 'item)
  21094. (make-string (org-list-item-body-column
  21095. (org-element-property :begin parent))
  21096. ?\s))
  21097. ((and adaptive-fill-regexp
  21098. ;; Locally disable
  21099. ;; `adaptive-fill-function' to let
  21100. ;; `fill-context-prefix' handle
  21101. ;; `adaptive-fill-regexp' variable.
  21102. (let (adaptive-fill-function)
  21103. (fill-context-prefix
  21104. post-affiliated
  21105. (org-element-property :end element)))))
  21106. ((looking-at "[ \t]+") (match-string 0))
  21107. (t "")))))
  21108. (comment-block
  21109. ;; Only fill contents if P is within block boundaries.
  21110. (let* ((cbeg (save-excursion (goto-char post-affiliated)
  21111. (forward-line)
  21112. (point)))
  21113. (cend (save-excursion
  21114. (goto-char (org-element-property :end element))
  21115. (skip-chars-backward " \r\t\n")
  21116. (line-beginning-position))))
  21117. (when (and (>= p cbeg) (< p cend))
  21118. (if (save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  21119. (match-string 0)
  21120. "")))))))))))
  21121. (declare-function message-goto-body "message" ())
  21122. (defvar message-cite-prefix-regexp) ; From message.el
  21123. (defun org-fill-paragraph (&optional justify)
  21124. "Fill element at point, when applicable.
  21125. This function only applies to comment blocks, comments, example
  21126. blocks and paragraphs. Also, as a special case, re-align table
  21127. when point is at one.
  21128. If JUSTIFY is non-nil (interactively, with prefix argument),
  21129. justify as well. If `sentence-end-double-space' is non-nil, then
  21130. period followed by one space does not end a sentence, so don't
  21131. break a line there. The variable `fill-column' controls the
  21132. width for filling.
  21133. For convenience, when point is at a plain list, an item or
  21134. a footnote definition, try to fill the first paragraph within."
  21135. (interactive)
  21136. (if (and (derived-mode-p 'message-mode)
  21137. (or (not (message-in-body-p))
  21138. (save-excursion (move-beginning-of-line 1)
  21139. (looking-at message-cite-prefix-regexp))))
  21140. ;; First ensure filling is correct in message-mode.
  21141. (let ((fill-paragraph-function
  21142. (cadadr (assoc 'fill-paragraph-function org-fb-vars)))
  21143. (fill-prefix (cadadr (assoc 'fill-prefix org-fb-vars)))
  21144. (paragraph-start (cadadr (assoc 'paragraph-start org-fb-vars)))
  21145. (paragraph-separate
  21146. (cadadr (assoc 'paragraph-separate org-fb-vars))))
  21147. (fill-paragraph nil))
  21148. (with-syntax-table org-mode-transpose-word-syntax-table
  21149. ;; Move to end of line in order to get the first paragraph
  21150. ;; within a plain list or a footnote definition.
  21151. (let ((element (save-excursion
  21152. (end-of-line)
  21153. (or (ignore-errors (org-element-at-point))
  21154. (user-error "An element cannot be parsed line %d"
  21155. (line-number-at-pos (point)))))))
  21156. ;; First check if point is in a blank line at the beginning of
  21157. ;; the buffer. In that case, ignore filling.
  21158. (case (org-element-type element)
  21159. ;; Use major mode filling function is src blocks.
  21160. (src-block (org-babel-do-key-sequence-in-edit-buffer (kbd "M-q")))
  21161. ;; Align Org tables, leave table.el tables as-is.
  21162. (table-row (org-table-align) t)
  21163. (table
  21164. (when (eq (org-element-property :type element) 'org)
  21165. (save-excursion
  21166. (goto-char (org-element-property :post-affiliated element))
  21167. (org-table-align)))
  21168. t)
  21169. (paragraph
  21170. ;; Paragraphs may contain `line-break' type objects.
  21171. (let ((beg (max (point-min)
  21172. (org-element-property :contents-begin element)))
  21173. (end (min (point-max)
  21174. (org-element-property :contents-end element))))
  21175. ;; Do nothing if point is at an affiliated keyword.
  21176. (if (< (line-end-position) beg) t
  21177. (when (derived-mode-p 'message-mode)
  21178. ;; In `message-mode', do not fill following citation
  21179. ;; in current paragraph nor text before message body.
  21180. (let ((body-start (save-excursion (message-goto-body))))
  21181. (when body-start (setq beg (max body-start beg))))
  21182. (when (save-excursion
  21183. (re-search-forward
  21184. (concat "^" message-cite-prefix-regexp) end t))
  21185. (setq end (match-beginning 0))))
  21186. ;; Fill paragraph, taking line breaks into account.
  21187. (save-excursion
  21188. (goto-char beg)
  21189. (let ((cuts (list beg)))
  21190. (while (re-search-forward "\\\\\\\\[ \t]*\n" end t)
  21191. (when (eq 'line-break
  21192. (org-element-type
  21193. (save-excursion (backward-char)
  21194. (org-element-context))))
  21195. (push (point) cuts)))
  21196. (dolist (c (delq end cuts))
  21197. (fill-region-as-paragraph c end justify)
  21198. (setq end c))))
  21199. t)))
  21200. ;; Contents of `comment-block' type elements should be
  21201. ;; filled as plain text, but only if point is within block
  21202. ;; markers.
  21203. (comment-block
  21204. (let* ((case-fold-search t)
  21205. (beg (save-excursion
  21206. (goto-char (org-element-property :begin element))
  21207. (re-search-forward "^[ \t]*#\\+begin_comment" nil t)
  21208. (forward-line)
  21209. (point)))
  21210. (end (save-excursion
  21211. (goto-char (org-element-property :end element))
  21212. (re-search-backward "^[ \t]*#\\+end_comment" nil t)
  21213. (line-beginning-position))))
  21214. (if (or (< (point) beg) (> (point) end)) t
  21215. (fill-region-as-paragraph
  21216. (save-excursion (end-of-line)
  21217. (re-search-backward "^[ \t]*$" beg 'move)
  21218. (line-beginning-position))
  21219. (save-excursion (beginning-of-line)
  21220. (re-search-forward "^[ \t]*$" end 'move)
  21221. (line-beginning-position))
  21222. justify))))
  21223. ;; Fill comments.
  21224. (comment
  21225. (let ((begin (org-element-property :post-affiliated element))
  21226. (end (org-element-property :end element)))
  21227. (when (and (>= (point) begin) (<= (point) end))
  21228. (let ((begin (save-excursion
  21229. (end-of-line)
  21230. (if (re-search-backward "^[ \t]*#[ \t]*$" begin t)
  21231. (progn (forward-line) (point))
  21232. begin)))
  21233. (end (save-excursion
  21234. (end-of-line)
  21235. (if (re-search-forward "^[ \t]*#[ \t]*$" end 'move)
  21236. (1- (line-beginning-position))
  21237. (skip-chars-backward " \r\t\n")
  21238. (line-end-position)))))
  21239. ;; Do not fill comments when at a blank line.
  21240. (when (> end begin)
  21241. (let ((fill-prefix
  21242. (save-excursion
  21243. (beginning-of-line)
  21244. (looking-at "[ \t]*#")
  21245. (let ((comment-prefix (match-string 0)))
  21246. (goto-char (match-end 0))
  21247. (if (looking-at adaptive-fill-regexp)
  21248. (concat comment-prefix (match-string 0))
  21249. (concat comment-prefix " "))))))
  21250. (save-excursion
  21251. (fill-region-as-paragraph begin end justify))))))
  21252. t))
  21253. ;; Ignore every other element.
  21254. (otherwise t))))))
  21255. (defun org-auto-fill-function ()
  21256. "Auto-fill function."
  21257. ;; Check if auto-filling is meaningful.
  21258. (let ((fc (current-fill-column)))
  21259. (when (and fc (> (current-column) fc))
  21260. (let* ((fill-prefix (org-adaptive-fill-function))
  21261. ;; Enforce empty fill prefix, if required. Otherwise, it
  21262. ;; will be computed again.
  21263. (adaptive-fill-mode (not (equal fill-prefix ""))))
  21264. (when fill-prefix (do-auto-fill))))))
  21265. (defun org-comment-line-break-function (&optional soft)
  21266. "Break line at point and indent, continuing comment if within one.
  21267. The inserted newline is marked hard if variable
  21268. `use-hard-newlines' is true, unless optional argument SOFT is
  21269. non-nil."
  21270. (if soft (insert-and-inherit ?\n) (newline 1))
  21271. (save-excursion (forward-char -1) (delete-horizontal-space))
  21272. (delete-horizontal-space)
  21273. (indent-to-left-margin)
  21274. (insert-before-markers-and-inherit fill-prefix))
  21275. ;;; Fixed Width Areas
  21276. (defun org-toggle-fixed-width ()
  21277. "Toggle fixed-width markup.
  21278. Add or remove fixed-width markup on current line, whenever it
  21279. makes sense. Return an error otherwise.
  21280. If a region is active and if it contains only fixed-width areas
  21281. or blank lines, remove all fixed-width markup in it. If the
  21282. region contains anything else, convert all non-fixed-width lines
  21283. to fixed-width ones.
  21284. Blank lines at the end of the region are ignored unless the
  21285. region only contains such lines."
  21286. (interactive)
  21287. (if (not (org-region-active-p))
  21288. ;; No region:
  21289. ;;
  21290. ;; Remove fixed width marker only in a fixed-with element.
  21291. ;;
  21292. ;; Add fixed width maker in paragraphs, in blank lines after
  21293. ;; elements or at the beginning of a headline or an inlinetask,
  21294. ;; and before any one-line elements (e.g., a clock).
  21295. (progn
  21296. (beginning-of-line)
  21297. (let* ((element (org-element-at-point))
  21298. (type (org-element-type element)))
  21299. (cond
  21300. ((and (eq type 'fixed-width)
  21301. (looking-at "[ \t]*\\(:\\(?: \\|$\\)\\)"))
  21302. (replace-match
  21303. "" nil nil nil (if (= (line-end-position) (match-end 0)) 0 1)))
  21304. ((and (memq type '(babel-call clock comment diary-sexp headline
  21305. horizontal-rule keyword paragraph
  21306. planning))
  21307. (<= (org-element-property :post-affiliated element) (point)))
  21308. (skip-chars-forward " \t")
  21309. (insert ": "))
  21310. ((and (org-looking-at-p "[ \t]*$")
  21311. (or (eq type 'inlinetask)
  21312. (save-excursion
  21313. (skip-chars-forward " \r\t\n")
  21314. (<= (org-element-property :end element) (point)))))
  21315. (delete-region (point) (line-end-position))
  21316. (org-indent-line)
  21317. (insert ": "))
  21318. (t (user-error "Cannot insert a fixed-width line here")))))
  21319. ;; Region active.
  21320. (let* ((begin (save-excursion
  21321. (goto-char (region-beginning))
  21322. (line-beginning-position)))
  21323. (end (copy-marker
  21324. (save-excursion
  21325. (goto-char (region-end))
  21326. (unless (eolp) (beginning-of-line))
  21327. (if (save-excursion (re-search-backward "\\S-" begin t))
  21328. (progn (skip-chars-backward " \r\t\n") (point))
  21329. (point)))))
  21330. (all-fixed-width-p
  21331. (catch 'not-all-p
  21332. (save-excursion
  21333. (goto-char begin)
  21334. (skip-chars-forward " \r\t\n")
  21335. (when (eobp) (throw 'not-all-p nil))
  21336. (while (< (point) end)
  21337. (let ((element (org-element-at-point)))
  21338. (if (eq (org-element-type element) 'fixed-width)
  21339. (goto-char (org-element-property :end element))
  21340. (throw 'not-all-p nil))))
  21341. t))))
  21342. (if all-fixed-width-p
  21343. (save-excursion
  21344. (goto-char begin)
  21345. (while (< (point) end)
  21346. (when (looking-at "[ \t]*\\(:\\(?: \\|$\\)\\)")
  21347. (replace-match
  21348. "" nil nil nil
  21349. (if (= (line-end-position) (match-end 0)) 0 1)))
  21350. (forward-line)))
  21351. (let ((min-ind (point-max)))
  21352. ;; Find minimum indentation across all lines.
  21353. (save-excursion
  21354. (goto-char begin)
  21355. (if (not (save-excursion (re-search-forward "\\S-" end t)))
  21356. (setq min-ind 0)
  21357. (catch 'zerop
  21358. (while (< (point) end)
  21359. (unless (org-looking-at-p "[ \t]*$")
  21360. (let ((ind (org-get-indentation)))
  21361. (setq min-ind (min min-ind ind))
  21362. (when (zerop ind) (throw 'zerop t))))
  21363. (forward-line)))))
  21364. ;; Loop over all lines and add fixed-width markup everywhere
  21365. ;; but in fixed-width lines.
  21366. (save-excursion
  21367. (goto-char begin)
  21368. (while (< (point) end)
  21369. (cond
  21370. ((org-at-heading-p)
  21371. (insert ": ")
  21372. (forward-line)
  21373. (while (and (< (point) end) (org-looking-at-p "[ \t]*$"))
  21374. (insert ":")
  21375. (forward-line)))
  21376. ((org-looking-at-p "[ \t]*:\\( \\|$\\)")
  21377. (let* ((element (org-element-at-point))
  21378. (element-end (org-element-property :end element)))
  21379. (if (eq (org-element-type element) 'fixed-width)
  21380. (progn (goto-char element-end)
  21381. (skip-chars-backward " \r\t\n")
  21382. (forward-line))
  21383. (let ((limit (min end element-end)))
  21384. (while (< (point) limit)
  21385. (org-move-to-column min-ind t)
  21386. (insert ": ")
  21387. (forward-line))))))
  21388. (t
  21389. (org-move-to-column min-ind t)
  21390. (insert ": ")
  21391. (forward-line)))))))
  21392. (set-marker end nil))))
  21393. ;;; Comments
  21394. ;; Org comments syntax is quite complex. It requires the entire line
  21395. ;; to be just a comment. Also, even with the right syntax at the
  21396. ;; beginning of line, some some elements (i.e. verse-block or
  21397. ;; example-block) don't accept comments. Usual Emacs comment commands
  21398. ;; cannot cope with those requirements. Therefore, Org replaces them.
  21399. ;; Org still relies on `comment-dwim', but cannot trust
  21400. ;; `comment-only-p'. So, `comment-region-function' and
  21401. ;; `uncomment-region-function' both point
  21402. ;; to`org-comment-or-uncomment-region'. Eventually,
  21403. ;; `org-insert-comment' takes care of insertion of comments at the
  21404. ;; beginning of line.
  21405. ;; `org-setup-comments-handling' install comments related variables
  21406. ;; during `org-mode' initialization.
  21407. (defun org-setup-comments-handling ()
  21408. (interactive)
  21409. (org-set-local 'comment-use-syntax nil)
  21410. (org-set-local 'comment-start "# ")
  21411. (org-set-local 'comment-start-skip "^\\s-*#\\(?: \\|$\\)")
  21412. (org-set-local 'comment-insert-comment-function 'org-insert-comment)
  21413. (org-set-local 'comment-region-function 'org-comment-or-uncomment-region)
  21414. (org-set-local 'uncomment-region-function 'org-comment-or-uncomment-region))
  21415. (defun org-insert-comment ()
  21416. "Insert an empty comment above current line.
  21417. If the line is empty, insert comment at its beginning. When
  21418. point is within a source block, comment according to the related
  21419. major mode."
  21420. (if (let ((element (org-element-at-point)))
  21421. (and (eq (org-element-type element) 'src-block)
  21422. (< (save-excursion
  21423. (goto-char (org-element-property :post-affiliated element))
  21424. (line-end-position))
  21425. (point))
  21426. (> (save-excursion
  21427. (goto-char (org-element-property :end element))
  21428. (skip-chars-backward " \r\t\n")
  21429. (line-beginning-position))
  21430. (point))))
  21431. (org-babel-do-in-edit-buffer (call-interactively 'comment-dwim))
  21432. (beginning-of-line)
  21433. (if (looking-at "\\s-*$") (delete-region (point) (point-at-eol))
  21434. (open-line 1))
  21435. (org-indent-line)
  21436. (insert "# ")))
  21437. (defvar comment-empty-lines) ; From newcomment.el.
  21438. (defun org-comment-or-uncomment-region (beg end &rest ignore)
  21439. "Comment or uncomment each non-blank line in the region.
  21440. Uncomment each non-blank line between BEG and END if it only
  21441. contains commented lines. Otherwise, comment them. If region is
  21442. strictly within a source block, use appropriate comment syntax."
  21443. (if (let ((element (org-element-at-point)))
  21444. (and (eq (org-element-type element) 'src-block)
  21445. (< (save-excursion
  21446. (goto-char (org-element-property :post-affiliated element))
  21447. (line-end-position))
  21448. beg)
  21449. (>= (save-excursion
  21450. (goto-char (org-element-property :end element))
  21451. (skip-chars-backward " \r\t\n")
  21452. (line-beginning-position))
  21453. end)))
  21454. (org-babel-do-in-edit-buffer (call-interactively 'comment-dwim))
  21455. (save-restriction
  21456. ;; Restrict region
  21457. (narrow-to-region (save-excursion (goto-char beg)
  21458. (skip-chars-forward " \r\t\n" end)
  21459. (line-beginning-position))
  21460. (save-excursion (goto-char end)
  21461. (skip-chars-backward " \r\t\n" beg)
  21462. (line-end-position)))
  21463. (let ((uncommentp
  21464. ;; UNCOMMENTP is non-nil when every non blank line between
  21465. ;; BEG and END is a comment.
  21466. (save-excursion
  21467. (goto-char (point-min))
  21468. (while (and (not (eobp))
  21469. (let ((element (org-element-at-point)))
  21470. (and (eq (org-element-type element) 'comment)
  21471. (goto-char (min (point-max)
  21472. (org-element-property
  21473. :end element)))))))
  21474. (eobp))))
  21475. (if uncommentp
  21476. ;; Only blank lines and comments in region: uncomment it.
  21477. (save-excursion
  21478. (goto-char (point-min))
  21479. (while (not (eobp))
  21480. (when (looking-at "[ \t]*\\(#\\(?: \\|$\\)\\)")
  21481. (replace-match "" nil nil nil 1))
  21482. (forward-line)))
  21483. ;; Comment each line in region.
  21484. (let ((min-indent (point-max)))
  21485. ;; First find the minimum indentation across all lines.
  21486. (save-excursion
  21487. (goto-char (point-min))
  21488. (while (and (not (eobp)) (not (zerop min-indent)))
  21489. (unless (looking-at "[ \t]*$")
  21490. (setq min-indent (min min-indent (current-indentation))))
  21491. (forward-line)))
  21492. ;; Then loop over all lines.
  21493. (save-excursion
  21494. (goto-char (point-min))
  21495. (while (not (eobp))
  21496. (unless (and (not comment-empty-lines) (looking-at "[ \t]*$"))
  21497. ;; Don't get fooled by invisible text (e.g. link path)
  21498. ;; when moving to column MIN-INDENT.
  21499. (let ((buffer-invisibility-spec nil))
  21500. (org-move-to-column min-indent t))
  21501. (insert comment-start))
  21502. (forward-line)))))))))
  21503. (defun org-comment-dwim (arg)
  21504. "Call `comment-dwim' within a source edit buffer if needed."
  21505. (interactive "*P")
  21506. (if (org-in-src-block-p)
  21507. (org-babel-do-in-edit-buffer (call-interactively 'comment-dwim))
  21508. (call-interactively 'comment-dwim)))
  21509. ;;; Planning
  21510. ;; This section contains tools to operate on timestamp objects, as
  21511. ;; returned by, e.g. `org-element-context'.
  21512. (defun org-timestamp-has-time-p (timestamp)
  21513. "Non-nil when TIMESTAMP has a time specified."
  21514. (org-element-property :hour-start timestamp))
  21515. (defun org-timestamp-format (timestamp format &optional end utc)
  21516. "Format a TIMESTAMP element into a string.
  21517. FORMAT is a format specifier to be passed to
  21518. `format-time-string'.
  21519. When optional argument END is non-nil, use end of date-range or
  21520. time-range, if possible.
  21521. When optional argument UTC is non-nil, time will be expressed as
  21522. Universal Time."
  21523. (format-time-string
  21524. format
  21525. (apply 'encode-time
  21526. (cons 0
  21527. (mapcar
  21528. (lambda (prop) (or (org-element-property prop timestamp) 0))
  21529. (if end '(:minute-end :hour-end :day-end :month-end :year-end)
  21530. '(:minute-start :hour-start :day-start :month-start
  21531. :year-start)))))
  21532. utc))
  21533. (defun org-timestamp-split-range (timestamp &optional end)
  21534. "Extract a timestamp object from a date or time range.
  21535. TIMESTAMP is a timestamp object. END, when non-nil, means extract
  21536. the end of the range. Otherwise, extract its start.
  21537. Return a new timestamp object sharing the same parent as
  21538. TIMESTAMP."
  21539. (let ((type (org-element-property :type timestamp)))
  21540. (if (memq type '(active inactive diary)) timestamp
  21541. (let ((split-ts (list 'timestamp (copy-sequence (nth 1 timestamp)))))
  21542. ;; Set new type.
  21543. (org-element-put-property
  21544. split-ts :type (if (eq type 'active-range) 'active 'inactive))
  21545. ;; Copy start properties over end properties if END is
  21546. ;; non-nil. Otherwise, copy end properties over `start' ones.
  21547. (let ((p-alist '((:minute-start . :minute-end)
  21548. (:hour-start . :hour-end)
  21549. (:day-start . :day-end)
  21550. (:month-start . :month-end)
  21551. (:year-start . :year-end))))
  21552. (dolist (p-cell p-alist)
  21553. (org-element-put-property
  21554. split-ts
  21555. (funcall (if end 'car 'cdr) p-cell)
  21556. (org-element-property
  21557. (funcall (if end 'cdr 'car) p-cell) split-ts)))
  21558. ;; Eventually refresh `:raw-value'.
  21559. (org-element-put-property split-ts :raw-value nil)
  21560. (org-element-put-property
  21561. split-ts :raw-value (org-element-interpret-data split-ts)))))))
  21562. (defun org-timestamp-translate (timestamp &optional boundary)
  21563. "Apply `org-translate-time' on a TIMESTAMP object.
  21564. When optional argument BOUNDARY is non-nil, it is either the
  21565. symbol `start' or `end'. In this case, only translate the
  21566. starting or ending part of TIMESTAMP if it is a date or time
  21567. range. Otherwise, translate both parts."
  21568. (if (and (not boundary)
  21569. (memq (org-element-property :type timestamp)
  21570. '(active-range inactive-range)))
  21571. (concat
  21572. (org-translate-time
  21573. (org-element-property :raw-value
  21574. (org-timestamp-split-range timestamp)))
  21575. "--"
  21576. (org-translate-time
  21577. (org-element-property :raw-value
  21578. (org-timestamp-split-range timestamp t))))
  21579. (org-translate-time
  21580. (org-element-property
  21581. :raw-value
  21582. (if (not boundary) timestamp
  21583. (org-timestamp-split-range timestamp (eq boundary 'end)))))))
  21584. ;;; Other stuff.
  21585. (defun org-reftex-citation ()
  21586. "Use reftex-citation to insert a citation into the buffer.
  21587. This looks for a line like
  21588. #+BIBLIOGRAPHY: foo plain option:-d
  21589. and derives from it that foo.bib is the bibliography file relevant
  21590. for this document. It then installs the necessary environment for RefTeX
  21591. to work in this buffer and calls `reftex-citation' to insert a citation
  21592. into the buffer.
  21593. Export of such citations to both LaTeX and HTML is handled by the contributed
  21594. package ox-bibtex by Taru Karttunen."
  21595. (interactive)
  21596. (let ((reftex-docstruct-symbol 'rds)
  21597. (reftex-cite-format "\\cite{%l}")
  21598. rds bib)
  21599. (save-excursion
  21600. (save-restriction
  21601. (widen)
  21602. (let ((case-fold-search t)
  21603. (re "^[ \t]*#\\+BIBLIOGRAPHY:[ \t]+\\([^ \t\n]+\\)"))
  21604. (if (not (save-excursion
  21605. (or (re-search-forward re nil t)
  21606. (re-search-backward re nil t))))
  21607. (user-error "No bibliography defined in file")
  21608. (setq bib (concat (match-string 1) ".bib")
  21609. rds (list (list 'bib bib)))))))
  21610. (call-interactively 'reftex-citation)))
  21611. ;;;; Functions extending outline functionality
  21612. (defun org-beginning-of-line (&optional arg)
  21613. "Go to the beginning of the current line. If that is invisible, continue
  21614. to a visible line beginning. This makes the function of C-a more intuitive.
  21615. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  21616. first attempt, and only move to after the tags when the cursor is already
  21617. beyond the end of the headline."
  21618. (interactive "P")
  21619. (let ((pos (point))
  21620. (special (if (consp org-special-ctrl-a/e)
  21621. (car org-special-ctrl-a/e)
  21622. org-special-ctrl-a/e))
  21623. deactivate-mark refpos)
  21624. (if (org-bound-and-true-p visual-line-mode)
  21625. (beginning-of-visual-line 1)
  21626. (beginning-of-line 1))
  21627. (if (and arg (fboundp 'move-beginning-of-line))
  21628. (call-interactively 'move-beginning-of-line)
  21629. (if (bobp)
  21630. nil
  21631. (backward-char 1)
  21632. (if (org-truely-invisible-p)
  21633. (while (and (not (bobp)) (org-truely-invisible-p))
  21634. (backward-char 1)
  21635. (beginning-of-line 1))
  21636. (forward-char 1))))
  21637. (when special
  21638. (cond
  21639. ((and (looking-at org-complex-heading-regexp)
  21640. (= (char-after (match-end 1)) ?\ ))
  21641. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  21642. (point-at-eol)))
  21643. (goto-char
  21644. (if (eq special t)
  21645. (cond ((> pos refpos) refpos)
  21646. ((= pos (point)) refpos)
  21647. (t (point)))
  21648. (cond ((> pos (point)) (point))
  21649. ((not (eq last-command this-command)) (point))
  21650. (t refpos)))))
  21651. ((org-at-item-p)
  21652. ;; Being at an item and not looking at an the item means point
  21653. ;; was previously moved to beginning of a visual line, which
  21654. ;; doesn't contain the item. Therefore, do nothing special,
  21655. ;; just stay here.
  21656. (when (looking-at org-list-full-item-re)
  21657. ;; Set special position at first white space character after
  21658. ;; bullet, and check-box, if any.
  21659. (let ((after-bullet
  21660. (let ((box (match-end 3)))
  21661. (if (not box) (match-end 1)
  21662. (let ((after (char-after box)))
  21663. (if (and after (= after ? )) (1+ box) box))))))
  21664. ;; Special case: Move point to special position when
  21665. ;; currently after it or at beginning of line.
  21666. (if (eq special t)
  21667. (when (or (> pos after-bullet) (= (point) pos))
  21668. (goto-char after-bullet))
  21669. ;; Reversed case: Move point to special position when
  21670. ;; point was already at beginning of line and command is
  21671. ;; repeated.
  21672. (when (and (= (point) pos) (eq last-command this-command))
  21673. (goto-char after-bullet))))))))
  21674. (org-no-warnings
  21675. (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  21676. (setq disable-point-adjustment
  21677. (or (not (invisible-p (point)))
  21678. (not (invisible-p (max (point-min) (1- (point))))))))
  21679. (defun org-end-of-line (&optional arg)
  21680. "Go to the end of the line.
  21681. If this is a headline, and `org-special-ctrl-a/e' is set, ignore
  21682. tags on the first attempt, and only move to after the tags when
  21683. the cursor is already beyond the end of the headline."
  21684. (interactive "P")
  21685. (let ((special (if (consp org-special-ctrl-a/e) (cdr org-special-ctrl-a/e)
  21686. org-special-ctrl-a/e))
  21687. (move-fun (cond ((org-bound-and-true-p visual-line-mode)
  21688. 'end-of-visual-line)
  21689. ((fboundp 'move-end-of-line) 'move-end-of-line)
  21690. (t 'end-of-line)))
  21691. deactivate-mark)
  21692. (if (or (not special) arg) (call-interactively move-fun)
  21693. (let* ((element (save-excursion (beginning-of-line)
  21694. (org-element-at-point)))
  21695. (type (org-element-type element)))
  21696. (cond
  21697. ((memq type '(headline inlinetask))
  21698. (let ((pos (point)))
  21699. (beginning-of-line 1)
  21700. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  21701. (if (eq special t)
  21702. (if (or (< pos (match-beginning 1)) (= pos (match-end 0)))
  21703. (goto-char (match-beginning 1))
  21704. (goto-char (match-end 0)))
  21705. (if (or (< pos (match-end 0))
  21706. (not (eq this-command last-command)))
  21707. (goto-char (match-end 0))
  21708. (goto-char (match-beginning 1))))
  21709. (call-interactively move-fun))))
  21710. ((outline-invisible-p (line-end-position))
  21711. ;; If element is hidden, `move-end-of-line' would put point
  21712. ;; after it. Use `end-of-line' to stay on current line.
  21713. (call-interactively 'end-of-line))
  21714. (t (call-interactively move-fun)))))
  21715. (org-no-warnings (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  21716. (setq disable-point-adjustment
  21717. (or (not (invisible-p (point)))
  21718. (not (invisible-p (max (point-min) (1- (point))))))))
  21719. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  21720. (define-key org-mode-map "\C-e" 'org-end-of-line)
  21721. (defun org-backward-sentence (&optional arg)
  21722. "Go to beginning of sentence, or beginning of table field.
  21723. This will call `backward-sentence' or `org-table-beginning-of-field',
  21724. depending on context."
  21725. (interactive "P")
  21726. (cond
  21727. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  21728. (t (call-interactively 'backward-sentence))))
  21729. (defun org-forward-sentence (&optional arg)
  21730. "Go to end of sentence, or end of table field.
  21731. This will call `forward-sentence' or `org-table-end-of-field',
  21732. depending on context."
  21733. (interactive "P")
  21734. (cond
  21735. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  21736. (t (call-interactively 'forward-sentence))))
  21737. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  21738. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  21739. (defun org-kill-line (&optional arg)
  21740. "Kill line, to tags or end of line."
  21741. (interactive "P")
  21742. (cond
  21743. ((or (not org-special-ctrl-k)
  21744. (bolp)
  21745. (not (org-at-heading-p)))
  21746. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  21747. org-ctrl-k-protect-subtree)
  21748. (if (or (eq org-ctrl-k-protect-subtree 'error)
  21749. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  21750. (user-error "C-k aborted as it would kill a hidden subtree")))
  21751. (call-interactively
  21752. (if (org-bound-and-true-p visual-line-mode) 'kill-visual-line 'kill-line)))
  21753. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  21754. (kill-region (point) (match-beginning 1))
  21755. (org-set-tags nil t))
  21756. (t (kill-region (point) (point-at-eol)))))
  21757. (define-key org-mode-map "\C-k" 'org-kill-line)
  21758. (defun org-yank (&optional arg)
  21759. "Yank. If the kill is a subtree, treat it specially.
  21760. This command will look at the current kill and check if is a single
  21761. subtree, or a series of subtrees[1]. If it passes the test, and if the
  21762. cursor is at the beginning of a line or after the stars of a currently
  21763. empty headline, then the yank is handled specially. How exactly depends
  21764. on the value of the following variables, both set by default.
  21765. `org-yank-folded-subtrees'
  21766. When set, the subtree(s) will be folded after insertion, but only
  21767. if doing so would now swallow text after the yanked text.
  21768. `org-yank-adjusted-subtrees'
  21769. When set, the subtree will be promoted or demoted in order to
  21770. fit into the local outline tree structure, which means that the
  21771. level will be adjusted so that it becomes the smaller one of the
  21772. two *visible* surrounding headings.
  21773. Any prefix to this command will cause `yank' to be called directly with
  21774. no special treatment. In particular, a simple \\[universal-argument] prefix \
  21775. will just
  21776. plainly yank the text as it is.
  21777. \[1] The test checks if the first non-white line is a heading
  21778. and if there are no other headings with fewer stars."
  21779. (interactive "P")
  21780. (org-yank-generic 'yank arg))
  21781. (defun org-yank-generic (command arg)
  21782. "Perform some yank-like command.
  21783. This function implements the behavior described in the `org-yank'
  21784. documentation. However, it has been generalized to work for any
  21785. interactive command with similar behavior."
  21786. ;; pretend to be command COMMAND
  21787. (setq this-command command)
  21788. (if arg
  21789. (call-interactively command)
  21790. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  21791. (and (org-kill-is-subtree-p)
  21792. (or (bolp)
  21793. (and (looking-at "[ \t]*$")
  21794. (string-match
  21795. "\\`\\*+\\'"
  21796. (buffer-substring (point-at-bol) (point)))))))
  21797. swallowp)
  21798. (cond
  21799. ((and subtreep org-yank-folded-subtrees)
  21800. (let ((beg (point))
  21801. end)
  21802. (if (and subtreep org-yank-adjusted-subtrees)
  21803. (org-paste-subtree nil nil 'for-yank)
  21804. (call-interactively command))
  21805. (setq end (point))
  21806. (goto-char beg)
  21807. (when (and (bolp) subtreep
  21808. (not (setq swallowp
  21809. (org-yank-folding-would-swallow-text beg end))))
  21810. (org-with-limited-levels
  21811. (or (looking-at org-outline-regexp)
  21812. (re-search-forward org-outline-regexp-bol end t))
  21813. (while (and (< (point) end) (looking-at org-outline-regexp))
  21814. (hide-subtree)
  21815. (org-cycle-show-empty-lines 'folded)
  21816. (condition-case nil
  21817. (outline-forward-same-level 1)
  21818. (error (goto-char end))))))
  21819. (when swallowp
  21820. (message
  21821. "Inserted text not folded because that would swallow text"))
  21822. (goto-char end)
  21823. (skip-chars-forward " \t\n\r")
  21824. (beginning-of-line 1)
  21825. (push-mark beg 'nomsg)))
  21826. ((and subtreep org-yank-adjusted-subtrees)
  21827. (let ((beg (point-at-bol)))
  21828. (org-paste-subtree nil nil 'for-yank)
  21829. (push-mark beg 'nomsg)))
  21830. (t
  21831. (call-interactively command))))))
  21832. (defun org-yank-folding-would-swallow-text (beg end)
  21833. "Would hide-subtree at BEG swallow any text after END?"
  21834. (let (level)
  21835. (org-with-limited-levels
  21836. (save-excursion
  21837. (goto-char beg)
  21838. (when (or (looking-at org-outline-regexp)
  21839. (re-search-forward org-outline-regexp-bol end t))
  21840. (setq level (org-outline-level)))
  21841. (goto-char end)
  21842. (skip-chars-forward " \t\r\n\v\f")
  21843. (if (or (eobp)
  21844. (and (bolp) (looking-at org-outline-regexp)
  21845. (<= (org-outline-level) level)))
  21846. nil ; Nothing would be swallowed
  21847. t))))) ; something would swallow
  21848. (define-key org-mode-map "\C-y" 'org-yank)
  21849. (defun org-truely-invisible-p ()
  21850. "Check if point is at a character currently not visible.
  21851. This version does not only check the character property, but also
  21852. `visible-mode'."
  21853. ;; Early versions of noutline don't have `outline-invisible-p'.
  21854. (if (org-bound-and-true-p visible-mode)
  21855. nil
  21856. (outline-invisible-p)))
  21857. (defun org-invisible-p2 ()
  21858. "Check if point is at a character currently not visible."
  21859. (save-excursion
  21860. (if (and (eolp) (not (bobp))) (backward-char 1))
  21861. ;; Early versions of noutline don't have `outline-invisible-p'.
  21862. (outline-invisible-p)))
  21863. (defun org-back-to-heading (&optional invisible-ok)
  21864. "Call `outline-back-to-heading', but provide a better error message."
  21865. (condition-case nil
  21866. (outline-back-to-heading invisible-ok)
  21867. (error (error "Before first headline at position %d in buffer %s"
  21868. (point) (current-buffer)))))
  21869. (defun org-before-first-heading-p ()
  21870. "Before first heading?"
  21871. (save-excursion
  21872. (end-of-line)
  21873. (null (re-search-backward org-outline-regexp-bol nil t))))
  21874. (defun org-at-heading-p (&optional ignored)
  21875. (outline-on-heading-p t))
  21876. ;; Compatibility alias with Org versions < 7.8.03
  21877. (defalias 'org-on-heading-p 'org-at-heading-p)
  21878. (defun org-in-commented-heading-p (&optional no-inheritance)
  21879. "Non-nil if point is under a commented heading.
  21880. This function also checks ancestors of the current headline,
  21881. unless optional argument NO-INHERITANCE is non-nil."
  21882. (cond
  21883. ((org-before-first-heading-p) nil)
  21884. ((let ((headline (nth 4 (org-heading-components))))
  21885. (and headline
  21886. (let ((case-fold-search nil))
  21887. (org-string-match-p (concat "^" org-comment-string "\\(?: \\|$\\)")
  21888. headline)))))
  21889. (no-inheritance nil)
  21890. (t
  21891. (save-excursion (and (org-up-heading-safe) (org-in-commented-heading-p))))))
  21892. (defun org-at-comment-p nil
  21893. "Is cursor in a commented line?"
  21894. (save-excursion
  21895. (save-match-data
  21896. (beginning-of-line)
  21897. (looking-at "^[ \t]*# "))))
  21898. (defun org-at-drawer-p nil
  21899. "Is cursor at a drawer keyword?"
  21900. (save-excursion
  21901. (move-beginning-of-line 1)
  21902. (looking-at org-drawer-regexp)))
  21903. (defun org-at-block-p nil
  21904. "Is cursor at a block keyword?"
  21905. (save-excursion
  21906. (move-beginning-of-line 1)
  21907. (looking-at org-block-regexp)))
  21908. (defun org-point-at-end-of-empty-headline ()
  21909. "If point is at the end of an empty headline, return t, else nil.
  21910. If the heading only contains a TODO keyword, it is still still considered
  21911. empty."
  21912. (and (looking-at "[ \t]*$")
  21913. (when org-todo-line-regexp
  21914. (save-excursion
  21915. (beginning-of-line 1)
  21916. (let ((case-fold-search nil))
  21917. (looking-at org-todo-line-regexp)
  21918. (string= (match-string 3) ""))))))
  21919. (defun org-at-heading-or-item-p ()
  21920. (or (org-at-heading-p) (org-at-item-p)))
  21921. (defun org-at-target-p ()
  21922. (or (org-in-regexp org-radio-target-regexp)
  21923. (org-in-regexp org-target-regexp)))
  21924. ;; Compatibility alias with Org versions < 7.8.03
  21925. (defalias 'org-on-target-p 'org-at-target-p)
  21926. (defun org-up-heading-all (arg)
  21927. "Move to the heading line of which the present line is a subheading.
  21928. This function considers both visible and invisible heading lines.
  21929. With argument, move up ARG levels."
  21930. (if (fboundp 'outline-up-heading-all)
  21931. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  21932. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  21933. (defun org-up-heading-safe ()
  21934. "Move to the heading line of which the present line is a subheading.
  21935. This version will not throw an error. It will return the level of the
  21936. headline found, or nil if no higher level is found.
  21937. Also, this function will be a lot faster than `outline-up-heading',
  21938. because it relies on stars being the outline starters. This can really
  21939. make a significant difference in outlines with very many siblings."
  21940. (when (ignore-errors (org-back-to-heading t))
  21941. (let ((level-up (1- (funcall outline-level))))
  21942. (and (> level-up 0)
  21943. (re-search-backward (format "^\\*\\{1,%d\\} " level-up) nil t)
  21944. (funcall outline-level)))))
  21945. (defun org-first-sibling-p ()
  21946. "Is this heading the first child of its parents?"
  21947. (interactive)
  21948. (let ((re org-outline-regexp-bol)
  21949. level l)
  21950. (unless (org-at-heading-p t)
  21951. (user-error "Not at a heading"))
  21952. (setq level (funcall outline-level))
  21953. (save-excursion
  21954. (if (not (re-search-backward re nil t))
  21955. t
  21956. (setq l (funcall outline-level))
  21957. (< l level)))))
  21958. (defun org-goto-sibling (&optional previous)
  21959. "Goto the next sibling, even if it is invisible.
  21960. When PREVIOUS is set, go to the previous sibling instead. Returns t
  21961. when a sibling was found. When none is found, return nil and don't
  21962. move point."
  21963. (let ((fun (if previous 're-search-backward 're-search-forward))
  21964. (pos (point))
  21965. (re org-outline-regexp-bol)
  21966. level l)
  21967. (when (ignore-errors (org-back-to-heading t))
  21968. (setq level (funcall outline-level))
  21969. (catch 'exit
  21970. (or previous (forward-char 1))
  21971. (while (funcall fun re nil t)
  21972. (setq l (funcall outline-level))
  21973. (when (< l level) (goto-char pos) (throw 'exit nil))
  21974. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  21975. (goto-char pos)
  21976. nil))))
  21977. (defun org-show-siblings ()
  21978. "Show all siblings of the current headline."
  21979. (save-excursion
  21980. (while (org-goto-sibling) (org-flag-heading nil)))
  21981. (save-excursion
  21982. (while (org-goto-sibling 'previous)
  21983. (org-flag-heading nil))))
  21984. (defun org-goto-first-child ()
  21985. "Goto the first child, even if it is invisible.
  21986. Return t when a child was found. Otherwise don't move point and
  21987. return nil."
  21988. (let (level (pos (point)) (re org-outline-regexp-bol))
  21989. (when (ignore-errors (org-back-to-heading t))
  21990. (setq level (outline-level))
  21991. (forward-char 1)
  21992. (if (and (re-search-forward re nil t) (> (outline-level) level))
  21993. (progn (goto-char (match-beginning 0)) t)
  21994. (goto-char pos) nil))))
  21995. (defun org-show-hidden-entry ()
  21996. "Show an entry where even the heading is hidden."
  21997. (save-excursion
  21998. (org-show-entry)))
  21999. (defun org-flag-heading (flag &optional entry)
  22000. "Flag the current heading. FLAG non-nil means make invisible.
  22001. When ENTRY is non-nil, show the entire entry."
  22002. (save-excursion
  22003. (org-back-to-heading t)
  22004. ;; Check if we should show the entire entry
  22005. (if entry
  22006. (progn
  22007. (org-show-entry)
  22008. (save-excursion
  22009. (and (outline-next-heading)
  22010. (org-flag-heading nil))))
  22011. (outline-flag-region (max (point-min) (1- (point)))
  22012. (save-excursion (outline-end-of-heading) (point))
  22013. flag))))
  22014. (defun org-get-next-sibling ()
  22015. "Move to next heading of the same level, and return point.
  22016. If there is no such heading, return nil.
  22017. This is like outline-next-sibling, but invisible headings are ok."
  22018. (let ((level (funcall outline-level)))
  22019. (outline-next-heading)
  22020. (while (and (not (eobp)) (> (funcall outline-level) level))
  22021. (outline-next-heading))
  22022. (if (or (eobp) (< (funcall outline-level) level))
  22023. nil
  22024. (point))))
  22025. (defun org-get-last-sibling ()
  22026. "Move to previous heading of the same level, and return point.
  22027. If there is no such heading, return nil."
  22028. (let ((opoint (point))
  22029. (level (funcall outline-level)))
  22030. (outline-previous-heading)
  22031. (when (and (/= (point) opoint) (outline-on-heading-p t))
  22032. (while (and (> (funcall outline-level) level)
  22033. (not (bobp)))
  22034. (outline-previous-heading))
  22035. (if (< (funcall outline-level) level)
  22036. nil
  22037. (point)))))
  22038. (defun org-end-of-subtree (&optional invisible-ok to-heading)
  22039. "Goto to the end of a subtree."
  22040. ;; This contains an exact copy of the original function, but it uses
  22041. ;; `org-back-to-heading', to make it work also in invisible
  22042. ;; trees. And is uses an invisible-ok argument.
  22043. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  22044. ;; Furthermore, when used inside Org, finding the end of a large subtree
  22045. ;; with many children and grandchildren etc, this can be much faster
  22046. ;; than the outline version.
  22047. (org-back-to-heading invisible-ok)
  22048. (let ((first t)
  22049. (level (funcall outline-level)))
  22050. (if (and (derived-mode-p 'org-mode) (< level 1000))
  22051. ;; A true heading (not a plain list item), in Org-mode
  22052. ;; This means we can easily find the end by looking
  22053. ;; only for the right number of stars. Using a regexp to do
  22054. ;; this is so much faster than using a Lisp loop.
  22055. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  22056. (forward-char 1)
  22057. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  22058. ;; something else, do it the slow way
  22059. (while (and (not (eobp))
  22060. (or first (> (funcall outline-level) level)))
  22061. (setq first nil)
  22062. (outline-next-heading)))
  22063. (unless to-heading
  22064. (if (memq (preceding-char) '(?\n ?\^M))
  22065. (progn
  22066. ;; Go to end of line before heading
  22067. (forward-char -1)
  22068. (if (memq (preceding-char) '(?\n ?\^M))
  22069. ;; leave blank line before heading
  22070. (forward-char -1))))))
  22071. (point))
  22072. (defun org-end-of-meta-data-and-drawers ()
  22073. "Jump to the first text after meta data and drawers in the current entry.
  22074. This will move over empty lines, lines with planning time stamps,
  22075. clocking lines, and drawers."
  22076. (org-back-to-heading t)
  22077. (let ((end (save-excursion (outline-next-heading) (point)))
  22078. (re (concat "\\(" org-drawer-regexp "\\)"
  22079. "\\|" "[ \t]*" org-keyword-time-regexp)))
  22080. (forward-line 1)
  22081. (while (re-search-forward re end t)
  22082. (if (not (match-end 1))
  22083. ;; empty or planning line
  22084. (forward-line 1)
  22085. ;; a drawer, find the end
  22086. (re-search-forward "^[ \t]*:END:" end 'move)
  22087. (forward-line 1)))
  22088. (and (re-search-forward "[^\n]" nil t) (backward-char 1))
  22089. (point)))
  22090. (defun org-forward-heading-same-level (arg &optional invisible-ok)
  22091. "Move forward to the ARG'th subheading at same level as this one.
  22092. Stop at the first and last subheadings of a superior heading.
  22093. Normally this only looks at visible headings, but when INVISIBLE-OK is
  22094. non-nil it will also look at invisible ones."
  22095. (interactive "p")
  22096. (if (not (ignore-errors (org-back-to-heading invisible-ok)))
  22097. (if (and arg (< arg 0))
  22098. (goto-char (point-min))
  22099. (outline-next-heading))
  22100. (org-at-heading-p)
  22101. (let ((level (- (match-end 0) (match-beginning 0) 1))
  22102. (f (if (and arg (< arg 0))
  22103. 're-search-backward
  22104. 're-search-forward))
  22105. (count (if arg (abs arg) 1))
  22106. (result (point)))
  22107. (while (and (prog1 (> count 0)
  22108. (forward-char (if (and arg (< arg 0)) -1 1)))
  22109. (funcall f org-outline-regexp-bol nil 'move))
  22110. (let ((l (- (match-end 0) (match-beginning 0) 1)))
  22111. (cond ((< l level) (setq count 0))
  22112. ((and (= l level)
  22113. (or invisible-ok
  22114. (progn
  22115. (goto-char (line-beginning-position))
  22116. (not (outline-invisible-p)))))
  22117. (setq count (1- count))
  22118. (when (eq l level)
  22119. (setq result (point)))))))
  22120. (goto-char result))
  22121. (beginning-of-line 1)))
  22122. (defun org-backward-heading-same-level (arg &optional invisible-ok)
  22123. "Move backward to the ARG'th subheading at same level as this one.
  22124. Stop at the first and last subheadings of a superior heading."
  22125. (interactive "p")
  22126. (org-forward-heading-same-level (if arg (- arg) -1) invisible-ok))
  22127. (defun org-next-block (arg &optional backward block-regexp)
  22128. "Jump to the next block.
  22129. With a prefix argument ARG, jump forward ARG many source blocks.
  22130. When BACKWARD is non-nil, jump to the previous block.
  22131. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  22132. (interactive "p")
  22133. (let ((re (or block-regexp org-block-regexp))
  22134. (re-search-fn (or (and backward 're-search-backward)
  22135. 're-search-forward)))
  22136. (if (looking-at re) (forward-char 1))
  22137. (condition-case nil
  22138. (funcall re-search-fn re nil nil arg)
  22139. (error (user-error "No %s code blocks"
  22140. (if backward "previous" "further" ))))
  22141. (goto-char (match-beginning 0)) (org-show-context)))
  22142. (defun org-previous-block (arg &optional block-regexp)
  22143. "Jump to the previous block.
  22144. With a prefix argument ARG, jump backward ARG many source blocks.
  22145. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  22146. (interactive "p")
  22147. (org-next-block arg t block-regexp))
  22148. (defun org-forward-paragraph ()
  22149. "Move forward to beginning of next paragraph or equivalent.
  22150. The function moves point to the beginning of the next visible
  22151. structural element, which can be a paragraph, a table, a list
  22152. item, etc. It also provides some special moves for convenience:
  22153. - On an affiliated keyword, jump to the beginning of the
  22154. relative element.
  22155. - On an item or a footnote definition, move to the second
  22156. element inside, if any.
  22157. - On a table or a property drawer, jump after it.
  22158. - On a verse or source block, stop after blank lines."
  22159. (interactive)
  22160. (when (eobp) (user-error "Cannot move further down"))
  22161. (let* ((deactivate-mark nil)
  22162. (element (org-element-at-point))
  22163. (type (org-element-type element))
  22164. (post-affiliated (org-element-property :post-affiliated element))
  22165. (contents-begin (org-element-property :contents-begin element))
  22166. (contents-end (org-element-property :contents-end element))
  22167. (end (let ((end (org-element-property :end element)) (parent element))
  22168. (while (and (setq parent (org-element-property :parent parent))
  22169. (= (org-element-property :contents-end parent) end))
  22170. (setq end (org-element-property :end parent)))
  22171. end)))
  22172. (cond ((not element)
  22173. (skip-chars-forward " \r\t\n")
  22174. (or (eobp) (beginning-of-line)))
  22175. ;; On affiliated keywords, move to element's beginning.
  22176. ((< (point) post-affiliated)
  22177. (goto-char post-affiliated))
  22178. ;; At a table row, move to the end of the table. Similarly,
  22179. ;; at a node property, move to the end of the property
  22180. ;; drawer.
  22181. ((memq type '(node-property table-row))
  22182. (goto-char (org-element-property
  22183. :end (org-element-property :parent element))))
  22184. ((memq type '(property-drawer table)) (goto-char end))
  22185. ;; Consider blank lines as separators in verse and source
  22186. ;; blocks to ease editing.
  22187. ((memq type '(src-block verse-block))
  22188. (when (eq type 'src-block)
  22189. (setq contents-end
  22190. (save-excursion (goto-char end)
  22191. (skip-chars-backward " \r\t\n")
  22192. (line-beginning-position))))
  22193. (beginning-of-line)
  22194. (when (looking-at "[ \t]*$") (skip-chars-forward " \r\t\n"))
  22195. (if (not (re-search-forward "^[ \t]*$" contents-end t))
  22196. (goto-char end)
  22197. (skip-chars-forward " \r\t\n")
  22198. (if (= (point) contents-end) (goto-char end)
  22199. (beginning-of-line))))
  22200. ;; With no contents, just skip element.
  22201. ((not contents-begin) (goto-char end))
  22202. ;; If contents are invisible, skip the element altogether.
  22203. ((outline-invisible-p (line-end-position))
  22204. (case type
  22205. (headline
  22206. (org-with-limited-levels (outline-next-visible-heading 1)))
  22207. ;; At a plain list, make sure we move to the next item
  22208. ;; instead of skipping the whole list.
  22209. (plain-list (forward-char)
  22210. (org-forward-paragraph))
  22211. (otherwise (goto-char end))))
  22212. ((>= (point) contents-end) (goto-char end))
  22213. ((>= (point) contents-begin)
  22214. ;; This can only happen on paragraphs and plain lists.
  22215. (case type
  22216. (paragraph (goto-char end))
  22217. ;; At a plain list, try to move to second element in
  22218. ;; first item, if possible.
  22219. (plain-list (end-of-line)
  22220. (org-forward-paragraph))))
  22221. ;; When contents start on the middle of a line (e.g. in
  22222. ;; items and footnote definitions), try to reach first
  22223. ;; element starting after current line.
  22224. ((> (line-end-position) contents-begin)
  22225. (end-of-line)
  22226. (org-forward-paragraph))
  22227. (t (goto-char contents-begin)))))
  22228. (defun org-backward-paragraph ()
  22229. "Move backward to start of previous paragraph or equivalent.
  22230. The function moves point to the beginning of the current
  22231. structural element, which can be a paragraph, a table, a list
  22232. item, etc., or to the beginning of the previous visible one if
  22233. point is already there. It also provides some special moves for
  22234. convenience:
  22235. - On an affiliated keyword, jump to the first one.
  22236. - On a table or a property drawer, move to its beginning.
  22237. - On a verse or source block, stop before blank lines."
  22238. (interactive)
  22239. (when (bobp) (user-error "Cannot move further up"))
  22240. (let* ((deactivate-mark nil)
  22241. (element (org-element-at-point))
  22242. (type (org-element-type element))
  22243. (contents-begin (org-element-property :contents-begin element))
  22244. (contents-end (org-element-property :contents-end element))
  22245. (post-affiliated (org-element-property :post-affiliated element))
  22246. (begin (org-element-property :begin element)))
  22247. (cond
  22248. ((not element) (goto-char (point-min)))
  22249. ((= (point) begin)
  22250. (backward-char)
  22251. (org-backward-paragraph))
  22252. ((<= (point) post-affiliated) (goto-char begin))
  22253. ((memq type '(node-property table-row))
  22254. (goto-char (org-element-property
  22255. :post-affiliated (org-element-property :parent element))))
  22256. ((memq type '(property-drawer table)) (goto-char begin))
  22257. ((memq type '(src-block verse-block))
  22258. (when (eq type 'src-block)
  22259. (setq contents-begin
  22260. (save-excursion (goto-char begin) (forward-line) (point))))
  22261. (if (= (point) contents-begin) (goto-char post-affiliated)
  22262. ;; Inside a verse block, see blank lines as paragraph
  22263. ;; separators.
  22264. (let ((origin (point)))
  22265. (skip-chars-backward " \r\t\n" contents-begin)
  22266. (when (re-search-backward "^[ \t]*$" contents-begin 'move)
  22267. (skip-chars-forward " \r\t\n" origin)
  22268. (if (= (point) origin) (goto-char contents-begin)
  22269. (beginning-of-line))))))
  22270. ((not contents-begin) (goto-char (or post-affiliated begin)))
  22271. ((eq type 'paragraph)
  22272. (goto-char contents-begin)
  22273. ;; When at first paragraph in an item or a footnote definition,
  22274. ;; move directly to beginning of line.
  22275. (let ((parent-contents
  22276. (org-element-property
  22277. :contents-begin (org-element-property :parent element))))
  22278. (when (and parent-contents (= parent-contents contents-begin))
  22279. (beginning-of-line))))
  22280. ;; At the end of a greater element, move to the beginning of the
  22281. ;; last element within.
  22282. ((>= (point) contents-end)
  22283. (goto-char (1- contents-end))
  22284. (org-backward-paragraph))
  22285. (t (goto-char (or post-affiliated begin))))
  22286. ;; Ensure we never leave point invisible.
  22287. (when (outline-invisible-p (point)) (beginning-of-visual-line))))
  22288. (defun org-forward-element ()
  22289. "Move forward by one element.
  22290. Move to the next element at the same level, when possible."
  22291. (interactive)
  22292. (cond ((eobp) (user-error "Cannot move further down"))
  22293. ((org-with-limited-levels (org-at-heading-p))
  22294. (let ((origin (point)))
  22295. (goto-char (org-end-of-subtree nil t))
  22296. (unless (org-with-limited-levels (org-at-heading-p))
  22297. (goto-char origin)
  22298. (user-error "Cannot move further down"))))
  22299. (t
  22300. (let* ((elem (org-element-at-point))
  22301. (end (org-element-property :end elem))
  22302. (parent (org-element-property :parent elem)))
  22303. (cond ((and parent (= (org-element-property :contents-end parent) end))
  22304. (goto-char (org-element-property :end parent)))
  22305. ((integer-or-marker-p end) (goto-char end))
  22306. (t (message "No element at point")))))))
  22307. (defun org-backward-element ()
  22308. "Move backward by one element.
  22309. Move to the previous element at the same level, when possible."
  22310. (interactive)
  22311. (cond ((bobp) (user-error "Cannot move further up"))
  22312. ((org-with-limited-levels (org-at-heading-p))
  22313. ;; At a headline, move to the previous one, if any, or stay
  22314. ;; here.
  22315. (let ((origin (point)))
  22316. (org-with-limited-levels (org-backward-heading-same-level 1))
  22317. ;; When current headline has no sibling above, move to its
  22318. ;; parent.
  22319. (when (= (point) origin)
  22320. (or (org-with-limited-levels (org-up-heading-safe))
  22321. (progn (goto-char origin)
  22322. (user-error "Cannot move further up"))))))
  22323. (t
  22324. (let* ((elem (org-element-at-point))
  22325. (beg (org-element-property :begin elem)))
  22326. (cond
  22327. ;; Move to beginning of current element if point isn't
  22328. ;; there already.
  22329. ((null beg) (message "No element at point"))
  22330. ((/= (point) beg) (goto-char beg))
  22331. (t (goto-char beg)
  22332. (skip-chars-backward " \r\t\n")
  22333. (unless (bobp)
  22334. (let ((prev (org-element-at-point)))
  22335. (goto-char (org-element-property :begin prev))
  22336. (while (and (setq prev (org-element-property :parent prev))
  22337. (<= (org-element-property :end prev) beg))
  22338. (goto-char (org-element-property :begin prev)))))))))))
  22339. (defun org-up-element ()
  22340. "Move to upper element."
  22341. (interactive)
  22342. (if (org-with-limited-levels (org-at-heading-p))
  22343. (unless (org-up-heading-safe) (user-error "No surrounding element"))
  22344. (let* ((elem (org-element-at-point))
  22345. (parent (org-element-property :parent elem)))
  22346. (if parent (goto-char (org-element-property :begin parent))
  22347. (if (org-with-limited-levels (org-before-first-heading-p))
  22348. (user-error "No surrounding element")
  22349. (org-with-limited-levels (org-back-to-heading)))))))
  22350. (defvar org-element-greater-elements)
  22351. (defun org-down-element ()
  22352. "Move to inner element."
  22353. (interactive)
  22354. (let ((element (org-element-at-point)))
  22355. (cond
  22356. ((memq (org-element-type element) '(plain-list table))
  22357. (goto-char (org-element-property :contents-begin element))
  22358. (forward-char))
  22359. ((memq (org-element-type element) org-element-greater-elements)
  22360. ;; If contents are hidden, first disclose them.
  22361. (when (outline-invisible-p (line-end-position)) (org-cycle))
  22362. (goto-char (or (org-element-property :contents-begin element)
  22363. (user-error "No content for this element"))))
  22364. (t (user-error "No inner element")))))
  22365. (defun org-drag-element-backward ()
  22366. "Move backward element at point."
  22367. (interactive)
  22368. (if (org-with-limited-levels (org-at-heading-p)) (org-move-subtree-up)
  22369. (let* ((elem (org-element-at-point))
  22370. (prev-elem
  22371. (save-excursion
  22372. (goto-char (org-element-property :begin elem))
  22373. (skip-chars-backward " \r\t\n")
  22374. (unless (bobp)
  22375. (let* ((beg (org-element-property :begin elem))
  22376. (prev (org-element-at-point))
  22377. (up prev))
  22378. (while (and (setq up (org-element-property :parent up))
  22379. (<= (org-element-property :end up) beg))
  22380. (setq prev up))
  22381. prev)))))
  22382. ;; Error out if no previous element or previous element is
  22383. ;; a parent of the current one.
  22384. (if (or (not prev-elem) (org-element-nested-p elem prev-elem))
  22385. (user-error "Cannot drag element backward")
  22386. (let ((pos (point)))
  22387. (org-element-swap-A-B prev-elem elem)
  22388. (goto-char (+ (org-element-property :begin prev-elem)
  22389. (- pos (org-element-property :begin elem)))))))))
  22390. (defun org-drag-element-forward ()
  22391. "Move forward element at point."
  22392. (interactive)
  22393. (let* ((pos (point))
  22394. (elem (org-element-at-point)))
  22395. (when (= (point-max) (org-element-property :end elem))
  22396. (user-error "Cannot drag element forward"))
  22397. (goto-char (org-element-property :end elem))
  22398. (let ((next-elem (org-element-at-point)))
  22399. (when (or (org-element-nested-p elem next-elem)
  22400. (and (eq (org-element-type next-elem) 'headline)
  22401. (not (eq (org-element-type elem) 'headline))))
  22402. (goto-char pos)
  22403. (user-error "Cannot drag element forward"))
  22404. ;; Compute new position of point: it's shifted by NEXT-ELEM
  22405. ;; body's length (without final blanks) and by the length of
  22406. ;; blanks between ELEM and NEXT-ELEM.
  22407. (let ((size-next (- (save-excursion
  22408. (goto-char (org-element-property :end next-elem))
  22409. (skip-chars-backward " \r\t\n")
  22410. (forward-line)
  22411. ;; Small correction if buffer doesn't end
  22412. ;; with a newline character.
  22413. (if (and (eolp) (not (bolp))) (1+ (point)) (point)))
  22414. (org-element-property :begin next-elem)))
  22415. (size-blank (- (org-element-property :end elem)
  22416. (save-excursion
  22417. (goto-char (org-element-property :end elem))
  22418. (skip-chars-backward " \r\t\n")
  22419. (forward-line)
  22420. (point)))))
  22421. (org-element-swap-A-B elem next-elem)
  22422. (goto-char (+ pos size-next size-blank))))))
  22423. (defun org-drag-line-forward (arg)
  22424. "Drag the line at point ARG lines forward."
  22425. (interactive "p")
  22426. (dotimes (n (abs arg))
  22427. (let ((c (current-column)))
  22428. (if (< 0 arg)
  22429. (progn
  22430. (beginning-of-line 2)
  22431. (transpose-lines 1)
  22432. (beginning-of-line 0))
  22433. (transpose-lines 1)
  22434. (beginning-of-line -1))
  22435. (org-move-to-column c))))
  22436. (defun org-drag-line-backward (arg)
  22437. "Drag the line at point ARG lines backward."
  22438. (interactive "p")
  22439. (org-drag-line-forward (- arg)))
  22440. (defun org-mark-element ()
  22441. "Put point at beginning of this element, mark at end.
  22442. Interactively, if this command is repeated or (in Transient Mark
  22443. mode) if the mark is active, it marks the next element after the
  22444. ones already marked."
  22445. (interactive)
  22446. (let (deactivate-mark)
  22447. (if (and (org-called-interactively-p 'any)
  22448. (or (and (eq last-command this-command) (mark t))
  22449. (and transient-mark-mode mark-active)))
  22450. (set-mark
  22451. (save-excursion
  22452. (goto-char (mark))
  22453. (goto-char (org-element-property :end (org-element-at-point)))))
  22454. (let ((element (org-element-at-point)))
  22455. (end-of-line)
  22456. (push-mark (org-element-property :end element) t t)
  22457. (goto-char (org-element-property :begin element))))))
  22458. (defun org-narrow-to-element ()
  22459. "Narrow buffer to current element."
  22460. (interactive)
  22461. (let ((elem (org-element-at-point)))
  22462. (cond
  22463. ((eq (car elem) 'headline)
  22464. (narrow-to-region
  22465. (org-element-property :begin elem)
  22466. (org-element-property :end elem)))
  22467. ((memq (car elem) org-element-greater-elements)
  22468. (narrow-to-region
  22469. (org-element-property :contents-begin elem)
  22470. (org-element-property :contents-end elem)))
  22471. (t
  22472. (narrow-to-region
  22473. (org-element-property :begin elem)
  22474. (org-element-property :end elem))))))
  22475. (defun org-transpose-element ()
  22476. "Transpose current and previous elements, keeping blank lines between.
  22477. Point is moved after both elements."
  22478. (interactive)
  22479. (org-skip-whitespace)
  22480. (let ((end (org-element-property :end (org-element-at-point))))
  22481. (org-drag-element-backward)
  22482. (goto-char end)))
  22483. (defun org-unindent-buffer ()
  22484. "Un-indent the visible part of the buffer.
  22485. Relative indentation (between items, inside blocks, etc.) isn't
  22486. modified."
  22487. (interactive)
  22488. (unless (eq major-mode 'org-mode)
  22489. (user-error "Cannot un-indent a buffer not in Org mode"))
  22490. (let* ((parse-tree (org-element-parse-buffer 'greater-element))
  22491. unindent-tree ; For byte-compiler.
  22492. (unindent-tree
  22493. (function
  22494. (lambda (contents)
  22495. (mapc
  22496. (lambda (element)
  22497. (if (memq (org-element-type element) '(headline section))
  22498. (funcall unindent-tree (org-element-contents element))
  22499. (save-excursion
  22500. (save-restriction
  22501. (narrow-to-region
  22502. (org-element-property :begin element)
  22503. (org-element-property :end element))
  22504. (org-do-remove-indentation)))))
  22505. (reverse contents))))))
  22506. (funcall unindent-tree (org-element-contents parse-tree))))
  22507. (defun org-show-subtree ()
  22508. "Show everything after this heading at deeper levels."
  22509. (interactive)
  22510. (outline-flag-region
  22511. (point)
  22512. (save-excursion
  22513. (org-end-of-subtree t t))
  22514. nil))
  22515. (defun org-show-entry ()
  22516. "Show the body directly following this heading.
  22517. Show the heading too, if it is currently invisible."
  22518. (interactive)
  22519. (save-excursion
  22520. (ignore-errors
  22521. (org-back-to-heading t)
  22522. (outline-flag-region
  22523. (max (point-min) (1- (point)))
  22524. (save-excursion
  22525. (if (re-search-forward
  22526. (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
  22527. (match-beginning 1)
  22528. (point-max)))
  22529. nil)
  22530. (org-cycle-hide-drawers 'children))))
  22531. (defun org-make-options-regexp (kwds &optional extra)
  22532. "Make a regular expression for keyword lines.
  22533. KWDS is a list of keywords, as strings. Optional argument EXTRA,
  22534. when non-nil, is a regexp matching keywords names."
  22535. (concat "^[ \t]*#\\+\\("
  22536. (regexp-opt kwds)
  22537. (and extra (concat (and kwds "\\|") extra))
  22538. "\\):[ \t]*\\(.*\\)"))
  22539. ;; Make isearch reveal the necessary context
  22540. (defun org-isearch-end ()
  22541. "Reveal context after isearch exits."
  22542. (when isearch-success ; only if search was successful
  22543. (if (featurep 'xemacs)
  22544. ;; Under XEmacs, the hook is run in the correct place,
  22545. ;; we directly show the context.
  22546. (org-show-context 'isearch)
  22547. ;; In Emacs the hook runs *before* restoring the overlays.
  22548. ;; So we have to use a one-time post-command-hook to do this.
  22549. ;; (Emacs 22 has a special variable, see function `org-mode')
  22550. (unless (and (boundp 'isearch-mode-end-hook-quit)
  22551. isearch-mode-end-hook-quit)
  22552. ;; Only when the isearch was not quitted.
  22553. (org-add-hook 'post-command-hook 'org-isearch-post-command
  22554. 'append 'local)))))
  22555. (defun org-isearch-post-command ()
  22556. "Remove self from hook, and show context."
  22557. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  22558. (org-show-context 'isearch))
  22559. ;;;; Integration with and fixes for other packages
  22560. ;;; Imenu support
  22561. (defvar org-imenu-markers nil
  22562. "All markers currently used by Imenu.")
  22563. (make-variable-buffer-local 'org-imenu-markers)
  22564. (defun org-imenu-new-marker (&optional pos)
  22565. "Return a new marker for use by Imenu, and remember the marker."
  22566. (let ((m (make-marker)))
  22567. (move-marker m (or pos (point)))
  22568. (push m org-imenu-markers)
  22569. m))
  22570. (defun org-imenu-get-tree ()
  22571. "Produce the index for Imenu."
  22572. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  22573. (setq org-imenu-markers nil)
  22574. (let* ((n org-imenu-depth)
  22575. (re (concat "^" (org-get-limited-outline-regexp)))
  22576. (subs (make-vector (1+ n) nil))
  22577. (last-level 0)
  22578. m level head0 head)
  22579. (save-excursion
  22580. (save-restriction
  22581. (widen)
  22582. (goto-char (point-max))
  22583. (while (re-search-backward re nil t)
  22584. (setq level (org-reduced-level (funcall outline-level)))
  22585. (when (and (<= level n)
  22586. (looking-at org-complex-heading-regexp)
  22587. (setq head0 (org-match-string-no-properties 4)))
  22588. (setq head (org-link-display-format head0)
  22589. m (org-imenu-new-marker))
  22590. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  22591. (if (>= level last-level)
  22592. (push (cons head m) (aref subs level))
  22593. (push (cons head (aref subs (1+ level))) (aref subs level))
  22594. (loop for i from (1+ level) to n do (aset subs i nil)))
  22595. (setq last-level level)))))
  22596. (aref subs 1)))
  22597. (eval-after-load "imenu"
  22598. '(progn
  22599. (add-hook 'imenu-after-jump-hook
  22600. (lambda ()
  22601. (if (derived-mode-p 'org-mode)
  22602. (org-show-context 'org-goto))))))
  22603. (defun org-link-display-format (link)
  22604. "Replace a link with its the description.
  22605. If there is no description, use the link target."
  22606. (save-match-data
  22607. (if (string-match org-bracket-link-analytic-regexp link)
  22608. (replace-match (if (match-end 5)
  22609. (match-string 5 link)
  22610. (concat (match-string 1 link)
  22611. (match-string 3 link)))
  22612. nil t link)
  22613. link)))
  22614. (defun org-toggle-link-display ()
  22615. "Toggle the literal or descriptive display of links."
  22616. (interactive)
  22617. (if org-descriptive-links
  22618. (progn (org-remove-from-invisibility-spec '(org-link))
  22619. (org-restart-font-lock)
  22620. (setq org-descriptive-links nil))
  22621. (progn (add-to-invisibility-spec '(org-link))
  22622. (org-restart-font-lock)
  22623. (setq org-descriptive-links t))))
  22624. ;; Speedbar support
  22625. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  22626. "Overlay marking the agenda restriction line in speedbar.")
  22627. (overlay-put org-speedbar-restriction-lock-overlay
  22628. 'face 'org-agenda-restriction-lock)
  22629. (overlay-put org-speedbar-restriction-lock-overlay
  22630. 'help-echo "Agendas are currently limited to this item.")
  22631. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  22632. (defun org-speedbar-set-agenda-restriction ()
  22633. "Restrict future agenda commands to the location at point in speedbar.
  22634. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  22635. (interactive)
  22636. (require 'org-agenda)
  22637. (let (p m tp np dir txt)
  22638. (cond
  22639. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  22640. 'org-imenu t))
  22641. (setq m (get-text-property p 'org-imenu-marker))
  22642. (with-current-buffer (marker-buffer m)
  22643. (goto-char m)
  22644. (org-agenda-set-restriction-lock 'subtree)))
  22645. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  22646. 'speedbar-function 'speedbar-find-file))
  22647. (setq tp (previous-single-property-change
  22648. (1+ p) 'speedbar-function)
  22649. np (next-single-property-change
  22650. tp 'speedbar-function)
  22651. dir (speedbar-line-directory)
  22652. txt (buffer-substring-no-properties (or tp (point-min))
  22653. (or np (point-max))))
  22654. (with-current-buffer (find-file-noselect
  22655. (let ((default-directory dir))
  22656. (expand-file-name txt)))
  22657. (unless (derived-mode-p 'org-mode)
  22658. (user-error "Cannot restrict to non-Org-mode file"))
  22659. (org-agenda-set-restriction-lock 'file)))
  22660. (t (user-error "Don't know how to restrict Org-mode's agenda")))
  22661. (move-overlay org-speedbar-restriction-lock-overlay
  22662. (point-at-bol) (point-at-eol))
  22663. (setq current-prefix-arg nil)
  22664. (org-agenda-maybe-redo)))
  22665. (defvar speedbar-file-key-map)
  22666. (declare-function speedbar-add-supported-extension "speedbar" (extension))
  22667. (eval-after-load "speedbar"
  22668. '(progn
  22669. (speedbar-add-supported-extension ".org")
  22670. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  22671. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  22672. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  22673. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  22674. (add-hook 'speedbar-visiting-tag-hook
  22675. (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
  22676. ;;; Fixes and Hacks for problems with other packages
  22677. (defun org--flyspell-object-check-p (element)
  22678. "Non-nil when Flyspell can check object at point.
  22679. ELEMENT is the element at point."
  22680. (let ((object (save-excursion
  22681. (when (org-looking-at-p "\\>") (backward-char))
  22682. (org-element-context element))))
  22683. (case (org-element-type object)
  22684. ;; Prevent checks in links due to keybinding conflict with
  22685. ;; Flyspell.
  22686. ((code entity export-snippet inline-babel-call
  22687. inline-src-block line-break latex-fragment link macro
  22688. statistics-cookie target timestamp verbatim)
  22689. nil)
  22690. (footnote-reference
  22691. ;; Only in inline footnotes, within the definition.
  22692. (and (eq (org-element-property :type object) 'inline)
  22693. (< (save-excursion
  22694. (goto-char (org-element-property :begin object))
  22695. (search-forward ":" nil t 2))
  22696. (point))))
  22697. (otherwise t))))
  22698. (defun org-mode-flyspell-verify ()
  22699. "Function used for `flyspell-generic-check-word-predicate'."
  22700. (if (org-at-heading-p)
  22701. ;; At a headline or an inlinetask, check title only. This is
  22702. ;; faster than relying on `org-element-at-point'.
  22703. (and (save-excursion (beginning-of-line)
  22704. (and (let ((case-fold-search t))
  22705. (not (looking-at "\\*+ END[ \t]*$")))
  22706. (looking-at org-complex-heading-regexp)))
  22707. (match-beginning 4)
  22708. (>= (point) (match-beginning 4))
  22709. (or (not (match-beginning 5))
  22710. (< (point) (match-beginning 5))))
  22711. (let* ((element (org-element-at-point))
  22712. (post-affiliated (org-element-property :post-affiliated element)))
  22713. (cond
  22714. ;; Ignore checks in all affiliated keywords but captions.
  22715. ((< (point) post-affiliated)
  22716. (and (save-excursion
  22717. (beginning-of-line)
  22718. (let ((case-fold-search t)) (looking-at "[ \t]*#\\+CAPTION:")))
  22719. (> (point) (match-end 0))
  22720. (org--flyspell-object-check-p element)))
  22721. ;; Ignore checks in LOGBOOK (or equivalent) drawer.
  22722. ((and org-log-into-drawer
  22723. (let ((log (or (org-string-nw-p org-log-into-drawer) "LOGBOOK"))
  22724. (parent element))
  22725. (while (and parent (not (eq (org-element-type parent) 'drawer)))
  22726. (setq parent (org-element-property :parent parent)))
  22727. (and parent
  22728. (eq (compare-strings
  22729. log nil nil
  22730. (org-element-property :drawer-name parent) nil nil t)
  22731. t))))
  22732. nil)
  22733. (t
  22734. (case (org-element-type element)
  22735. ((comment quote-section) t)
  22736. (comment-block
  22737. ;; Allow checks between block markers, not on them.
  22738. (and (> (line-beginning-position) post-affiliated)
  22739. (save-excursion
  22740. (end-of-line)
  22741. (skip-chars-forward " \r\t\n")
  22742. (< (point) (org-element-property :end element)))))
  22743. ;; Arbitrary list of keywords where checks are meaningful.
  22744. ;; Make sure point is on the value part of the element.
  22745. (keyword
  22746. (and (member (org-element-property :key element)
  22747. '("DESCRIPTION" "TITLE"))
  22748. (< (save-excursion
  22749. (beginning-of-line) (search-forward ":") (point))
  22750. (point))))
  22751. ;; Check is globally allowed in paragraphs verse blocks and
  22752. ;; table rows (after affiliated keywords) but some objects
  22753. ;; must not be affected.
  22754. ((paragraph table-row verse-block)
  22755. (let ((cbeg (org-element-property :contents-begin element))
  22756. (cend (org-element-property :contents-end element)))
  22757. (and cbeg (>= (point) cbeg) (< (point) cend)
  22758. (org--flyspell-object-check-p element))))))))))
  22759. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  22760. (defun org-remove-flyspell-overlays-in (beg end)
  22761. "Remove flyspell overlays in region."
  22762. (and (org-bound-and-true-p flyspell-mode)
  22763. (fboundp 'flyspell-delete-region-overlays)
  22764. (flyspell-delete-region-overlays beg end)))
  22765. (defvar flyspell-delayed-commands)
  22766. (eval-after-load "flyspell"
  22767. '(add-to-list 'flyspell-delayed-commands 'org-self-insert-command))
  22768. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  22769. (eval-after-load "bookmark"
  22770. '(if (boundp 'bookmark-after-jump-hook)
  22771. ;; We can use the hook
  22772. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  22773. ;; Hook not available, use advice
  22774. (defadvice bookmark-jump (after org-make-visible activate)
  22775. "Make the position visible."
  22776. (org-bookmark-jump-unhide))))
  22777. ;; Make sure saveplace shows the location if it was hidden
  22778. (eval-after-load "saveplace"
  22779. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  22780. "Make the position visible."
  22781. (org-bookmark-jump-unhide)))
  22782. ;; Make sure ecb shows the location if it was hidden
  22783. (eval-after-load "ecb"
  22784. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  22785. "Make hierarchy visible when jumping into location from ECB tree buffer."
  22786. (if (derived-mode-p 'org-mode)
  22787. (org-show-context))))
  22788. (defun org-bookmark-jump-unhide ()
  22789. "Unhide the current position, to show the bookmark location."
  22790. (and (derived-mode-p 'org-mode)
  22791. (or (outline-invisible-p)
  22792. (save-excursion (goto-char (max (point-min) (1- (point))))
  22793. (outline-invisible-p)))
  22794. (org-show-context 'bookmark-jump)))
  22795. (defun org-mark-jump-unhide ()
  22796. "Make the point visible with `org-show-context' after jumping to the mark."
  22797. (when (and (derived-mode-p 'org-mode)
  22798. (outline-invisible-p))
  22799. (org-show-context 'mark-goto)))
  22800. (eval-after-load "simple"
  22801. '(defadvice pop-to-mark-command (after org-make-visible activate)
  22802. "Make the point visible with `org-show-context'."
  22803. (org-mark-jump-unhide)))
  22804. (eval-after-load "simple"
  22805. '(defadvice exchange-point-and-mark (after org-make-visible activate)
  22806. "Make the point visible with `org-show-context'."
  22807. (org-mark-jump-unhide)))
  22808. (eval-after-load "simple"
  22809. '(defadvice pop-global-mark (after org-make-visible activate)
  22810. "Make the point visible with `org-show-context'."
  22811. (org-mark-jump-unhide)))
  22812. ;; Make session.el ignore our circular variable
  22813. (defvar session-globals-exclude)
  22814. (eval-after-load "session"
  22815. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  22816. ;;;; Finish up
  22817. (provide 'org)
  22818. (run-hooks 'org-load-hook)
  22819. ;;; org.el ends here